xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision aafd858192bc98fab9230f6a634af85c4079f80f)
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 
29a70491ccSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30a70491ccSJoe Perches 
3163eeaf38SJesse Barnes #include <linux/sysrq.h>
325a0e3ad6STejun Heo #include <linux/slab.h>
33b2c88f5bSDamien Lespiau #include <linux/circ_buf.h>
34760285e7SDavid Howells #include <drm/drmP.h>
35760285e7SDavid Howells #include <drm/i915_drm.h>
36c0e09200SDave Airlie #include "i915_drv.h"
371c5d22f7SChris Wilson #include "i915_trace.h"
3879e53945SJesse Barnes #include "intel_drv.h"
39c0e09200SDave Airlie 
40fca52a55SDaniel Vetter /**
41fca52a55SDaniel Vetter  * DOC: interrupt handling
42fca52a55SDaniel Vetter  *
43fca52a55SDaniel Vetter  * These functions provide the basic support for enabling and disabling the
44fca52a55SDaniel Vetter  * interrupt handling support. There's a lot more functionality in i915_irq.c
45fca52a55SDaniel Vetter  * and related files, but that will be described in separate chapters.
46fca52a55SDaniel Vetter  */
47fca52a55SDaniel Vetter 
48e4ce95aaSVille Syrjälä static const u32 hpd_ilk[HPD_NUM_PINS] = {
49e4ce95aaSVille Syrjälä 	[HPD_PORT_A] = DE_DP_A_HOTPLUG,
50e4ce95aaSVille Syrjälä };
51e4ce95aaSVille Syrjälä 
5223bb4cb5SVille Syrjälä static const u32 hpd_ivb[HPD_NUM_PINS] = {
5323bb4cb5SVille Syrjälä 	[HPD_PORT_A] = DE_DP_A_HOTPLUG_IVB,
5423bb4cb5SVille Syrjälä };
5523bb4cb5SVille Syrjälä 
563a3b3c7dSVille Syrjälä static const u32 hpd_bdw[HPD_NUM_PINS] = {
573a3b3c7dSVille Syrjälä 	[HPD_PORT_A] = GEN8_PORT_DP_A_HOTPLUG,
583a3b3c7dSVille Syrjälä };
593a3b3c7dSVille Syrjälä 
607c7e10dbSVille Syrjälä static const u32 hpd_ibx[HPD_NUM_PINS] = {
61e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG,
62e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG,
63e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG,
64e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG,
65e5868a31SEgbert Eich 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG
66e5868a31SEgbert Eich };
67e5868a31SEgbert Eich 
687c7e10dbSVille Syrjälä static const u32 hpd_cpt[HPD_NUM_PINS] = {
69e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG_CPT,
7073c352a2SDaniel Vetter 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG_CPT,
71e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
72e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
73e5868a31SEgbert Eich 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT
74e5868a31SEgbert Eich };
75e5868a31SEgbert Eich 
7626951cafSXiong Zhang static const u32 hpd_spt[HPD_NUM_PINS] = {
7774c0b395SVille Syrjälä 	[HPD_PORT_A] = SDE_PORTA_HOTPLUG_SPT,
7826951cafSXiong Zhang 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
7926951cafSXiong Zhang 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
8026951cafSXiong Zhang 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT,
8126951cafSXiong Zhang 	[HPD_PORT_E] = SDE_PORTE_HOTPLUG_SPT
8226951cafSXiong Zhang };
8326951cafSXiong Zhang 
847c7e10dbSVille Syrjälä static const u32 hpd_mask_i915[HPD_NUM_PINS] = {
85e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_EN,
86e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_EN,
87e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_EN,
88e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_EN,
89e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_EN,
90e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_EN
91e5868a31SEgbert Eich };
92e5868a31SEgbert Eich 
937c7e10dbSVille Syrjälä static const u32 hpd_status_g4x[HPD_NUM_PINS] = {
94e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
95e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_G4X,
96e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_G4X,
97e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
98e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
99e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
100e5868a31SEgbert Eich };
101e5868a31SEgbert Eich 
1024bca26d0SVille Syrjälä static const u32 hpd_status_i915[HPD_NUM_PINS] = {
103e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
104e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_I915,
105e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_I915,
106e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
107e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
108e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
109e5868a31SEgbert Eich };
110e5868a31SEgbert Eich 
111e0a20ad7SShashank Sharma /* BXT hpd list */
112e0a20ad7SShashank Sharma static const u32 hpd_bxt[HPD_NUM_PINS] = {
1137f3561beSSonika Jindal 	[HPD_PORT_A] = BXT_DE_PORT_HP_DDIA,
114e0a20ad7SShashank Sharma 	[HPD_PORT_B] = BXT_DE_PORT_HP_DDIB,
115e0a20ad7SShashank Sharma 	[HPD_PORT_C] = BXT_DE_PORT_HP_DDIC
116e0a20ad7SShashank Sharma };
117e0a20ad7SShashank Sharma 
1185c502442SPaulo Zanoni /* IIR can theoretically queue up two events. Be paranoid. */
119f86f3fb0SPaulo Zanoni #define GEN8_IRQ_RESET_NDX(type, which) do { \
1205c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IMR(which), 0xffffffff); \
1215c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IMR(which)); \
1225c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IER(which), 0); \
1235c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
1245c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IIR(which)); \
1255c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
1265c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IIR(which)); \
1275c502442SPaulo Zanoni } while (0)
1285c502442SPaulo Zanoni 
129f86f3fb0SPaulo Zanoni #define GEN5_IRQ_RESET(type) do { \
130a9d356a6SPaulo Zanoni 	I915_WRITE(type##IMR, 0xffffffff); \
1315c502442SPaulo Zanoni 	POSTING_READ(type##IMR); \
132a9d356a6SPaulo Zanoni 	I915_WRITE(type##IER, 0); \
1335c502442SPaulo Zanoni 	I915_WRITE(type##IIR, 0xffffffff); \
1345c502442SPaulo Zanoni 	POSTING_READ(type##IIR); \
1355c502442SPaulo Zanoni 	I915_WRITE(type##IIR, 0xffffffff); \
1365c502442SPaulo Zanoni 	POSTING_READ(type##IIR); \
137a9d356a6SPaulo Zanoni } while (0)
138a9d356a6SPaulo Zanoni 
139337ba017SPaulo Zanoni /*
140337ba017SPaulo Zanoni  * We should clear IMR at preinstall/uninstall, and just check at postinstall.
141337ba017SPaulo Zanoni  */
142337ba017SPaulo Zanoni #define GEN5_ASSERT_IIR_IS_ZERO(reg) do { \
143337ba017SPaulo Zanoni 	u32 val = I915_READ(reg); \
144337ba017SPaulo Zanoni 	if (val) { \
145337ba017SPaulo Zanoni 		WARN(1, "Interrupt register 0x%x is not zero: 0x%08x\n", \
146337ba017SPaulo Zanoni 		     (reg), val); \
147337ba017SPaulo Zanoni 		I915_WRITE((reg), 0xffffffff); \
148337ba017SPaulo Zanoni 		POSTING_READ(reg); \
149337ba017SPaulo Zanoni 		I915_WRITE((reg), 0xffffffff); \
150337ba017SPaulo Zanoni 		POSTING_READ(reg); \
151337ba017SPaulo Zanoni 	} \
152337ba017SPaulo Zanoni } while (0)
153337ba017SPaulo Zanoni 
15435079899SPaulo Zanoni #define GEN8_IRQ_INIT_NDX(type, which, imr_val, ier_val) do { \
155337ba017SPaulo Zanoni 	GEN5_ASSERT_IIR_IS_ZERO(GEN8_##type##_IIR(which)); \
15635079899SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IER(which), (ier_val)); \
1577d1bd539SVille Syrjälä 	I915_WRITE(GEN8_##type##_IMR(which), (imr_val)); \
1587d1bd539SVille Syrjälä 	POSTING_READ(GEN8_##type##_IMR(which)); \
15935079899SPaulo Zanoni } while (0)
16035079899SPaulo Zanoni 
16135079899SPaulo Zanoni #define GEN5_IRQ_INIT(type, imr_val, ier_val) do { \
162337ba017SPaulo Zanoni 	GEN5_ASSERT_IIR_IS_ZERO(type##IIR); \
16335079899SPaulo Zanoni 	I915_WRITE(type##IER, (ier_val)); \
1647d1bd539SVille Syrjälä 	I915_WRITE(type##IMR, (imr_val)); \
1657d1bd539SVille Syrjälä 	POSTING_READ(type##IMR); \
16635079899SPaulo Zanoni } while (0)
16735079899SPaulo Zanoni 
168c9a9a268SImre Deak static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir);
169c9a9a268SImre Deak 
1700706f17cSEgbert Eich /* For display hotplug interrupt */
1710706f17cSEgbert Eich static inline void
1720706f17cSEgbert Eich i915_hotplug_interrupt_update_locked(struct drm_i915_private *dev_priv,
1730706f17cSEgbert Eich 				     uint32_t mask,
1740706f17cSEgbert Eich 				     uint32_t bits)
1750706f17cSEgbert Eich {
1760706f17cSEgbert Eich 	uint32_t val;
1770706f17cSEgbert Eich 
1780706f17cSEgbert Eich 	assert_spin_locked(&dev_priv->irq_lock);
1790706f17cSEgbert Eich 	WARN_ON(bits & ~mask);
1800706f17cSEgbert Eich 
1810706f17cSEgbert Eich 	val = I915_READ(PORT_HOTPLUG_EN);
1820706f17cSEgbert Eich 	val &= ~mask;
1830706f17cSEgbert Eich 	val |= bits;
1840706f17cSEgbert Eich 	I915_WRITE(PORT_HOTPLUG_EN, val);
1850706f17cSEgbert Eich }
1860706f17cSEgbert Eich 
1870706f17cSEgbert Eich /**
1880706f17cSEgbert Eich  * i915_hotplug_interrupt_update - update hotplug interrupt enable
1890706f17cSEgbert Eich  * @dev_priv: driver private
1900706f17cSEgbert Eich  * @mask: bits to update
1910706f17cSEgbert Eich  * @bits: bits to enable
1920706f17cSEgbert Eich  * NOTE: the HPD enable bits are modified both inside and outside
1930706f17cSEgbert Eich  * of an interrupt context. To avoid that read-modify-write cycles
1940706f17cSEgbert Eich  * interfer, these bits are protected by a spinlock. Since this
1950706f17cSEgbert Eich  * function is usually not called from a context where the lock is
1960706f17cSEgbert Eich  * held already, this function acquires the lock itself. A non-locking
1970706f17cSEgbert Eich  * version is also available.
1980706f17cSEgbert Eich  */
1990706f17cSEgbert Eich void i915_hotplug_interrupt_update(struct drm_i915_private *dev_priv,
2000706f17cSEgbert Eich 				   uint32_t mask,
2010706f17cSEgbert Eich 				   uint32_t bits)
2020706f17cSEgbert Eich {
2030706f17cSEgbert Eich 	spin_lock_irq(&dev_priv->irq_lock);
2040706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv, mask, bits);
2050706f17cSEgbert Eich 	spin_unlock_irq(&dev_priv->irq_lock);
2060706f17cSEgbert Eich }
2070706f17cSEgbert Eich 
208d9dc34f1SVille Syrjälä /**
209d9dc34f1SVille Syrjälä  * ilk_update_display_irq - update DEIMR
210d9dc34f1SVille Syrjälä  * @dev_priv: driver private
211d9dc34f1SVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
212d9dc34f1SVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
213d9dc34f1SVille Syrjälä  */
214d9dc34f1SVille Syrjälä static void ilk_update_display_irq(struct drm_i915_private *dev_priv,
215d9dc34f1SVille Syrjälä 				   uint32_t interrupt_mask,
216d9dc34f1SVille Syrjälä 				   uint32_t enabled_irq_mask)
217036a4a7dSZhenyu Wang {
218d9dc34f1SVille Syrjälä 	uint32_t new_val;
219d9dc34f1SVille Syrjälä 
2204bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
2214bc9d430SDaniel Vetter 
222d9dc34f1SVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
223d9dc34f1SVille Syrjälä 
2249df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
225c67a470bSPaulo Zanoni 		return;
226c67a470bSPaulo Zanoni 
227d9dc34f1SVille Syrjälä 	new_val = dev_priv->irq_mask;
228d9dc34f1SVille Syrjälä 	new_val &= ~interrupt_mask;
229d9dc34f1SVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
230d9dc34f1SVille Syrjälä 
231d9dc34f1SVille Syrjälä 	if (new_val != dev_priv->irq_mask) {
232d9dc34f1SVille Syrjälä 		dev_priv->irq_mask = new_val;
2331ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
2343143a2bfSChris Wilson 		POSTING_READ(DEIMR);
235036a4a7dSZhenyu Wang 	}
236036a4a7dSZhenyu Wang }
237036a4a7dSZhenyu Wang 
23847339cd9SDaniel Vetter void
239d9dc34f1SVille Syrjälä ironlake_enable_display_irq(struct drm_i915_private *dev_priv, u32 mask)
240d9dc34f1SVille Syrjälä {
241d9dc34f1SVille Syrjälä 	ilk_update_display_irq(dev_priv, mask, mask);
242d9dc34f1SVille Syrjälä }
243d9dc34f1SVille Syrjälä 
244d9dc34f1SVille Syrjälä void
2452d1013ddSJani Nikula ironlake_disable_display_irq(struct drm_i915_private *dev_priv, u32 mask)
246036a4a7dSZhenyu Wang {
247d9dc34f1SVille Syrjälä 	ilk_update_display_irq(dev_priv, mask, 0);
248036a4a7dSZhenyu Wang }
249036a4a7dSZhenyu Wang 
25043eaea13SPaulo Zanoni /**
25143eaea13SPaulo Zanoni  * ilk_update_gt_irq - update GTIMR
25243eaea13SPaulo Zanoni  * @dev_priv: driver private
25343eaea13SPaulo Zanoni  * @interrupt_mask: mask of interrupt bits to update
25443eaea13SPaulo Zanoni  * @enabled_irq_mask: mask of interrupt bits to enable
25543eaea13SPaulo Zanoni  */
25643eaea13SPaulo Zanoni static void ilk_update_gt_irq(struct drm_i915_private *dev_priv,
25743eaea13SPaulo Zanoni 			      uint32_t interrupt_mask,
25843eaea13SPaulo Zanoni 			      uint32_t enabled_irq_mask)
25943eaea13SPaulo Zanoni {
26043eaea13SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
26143eaea13SPaulo Zanoni 
26215a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
26315a17aaeSDaniel Vetter 
2649df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
265c67a470bSPaulo Zanoni 		return;
266c67a470bSPaulo Zanoni 
26743eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask &= ~interrupt_mask;
26843eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask |= (~enabled_irq_mask & interrupt_mask);
26943eaea13SPaulo Zanoni 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
27043eaea13SPaulo Zanoni 	POSTING_READ(GTIMR);
27143eaea13SPaulo Zanoni }
27243eaea13SPaulo Zanoni 
273480c8033SDaniel Vetter void gen5_enable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
27443eaea13SPaulo Zanoni {
27543eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, mask);
27643eaea13SPaulo Zanoni }
27743eaea13SPaulo Zanoni 
278480c8033SDaniel Vetter void gen5_disable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
27943eaea13SPaulo Zanoni {
28043eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, 0);
28143eaea13SPaulo Zanoni }
28243eaea13SPaulo Zanoni 
283b900b949SImre Deak static u32 gen6_pm_iir(struct drm_i915_private *dev_priv)
284b900b949SImre Deak {
285b900b949SImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IIR(2) : GEN6_PMIIR;
286b900b949SImre Deak }
287b900b949SImre Deak 
288a72fbc3aSImre Deak static u32 gen6_pm_imr(struct drm_i915_private *dev_priv)
289a72fbc3aSImre Deak {
290a72fbc3aSImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IMR(2) : GEN6_PMIMR;
291a72fbc3aSImre Deak }
292a72fbc3aSImre Deak 
293b900b949SImre Deak static u32 gen6_pm_ier(struct drm_i915_private *dev_priv)
294b900b949SImre Deak {
295b900b949SImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IER(2) : GEN6_PMIER;
296b900b949SImre Deak }
297b900b949SImre Deak 
298edbfdb45SPaulo Zanoni /**
299edbfdb45SPaulo Zanoni   * snb_update_pm_irq - update GEN6_PMIMR
300edbfdb45SPaulo Zanoni   * @dev_priv: driver private
301edbfdb45SPaulo Zanoni   * @interrupt_mask: mask of interrupt bits to update
302edbfdb45SPaulo Zanoni   * @enabled_irq_mask: mask of interrupt bits to enable
303edbfdb45SPaulo Zanoni   */
304edbfdb45SPaulo Zanoni static void snb_update_pm_irq(struct drm_i915_private *dev_priv,
305edbfdb45SPaulo Zanoni 			      uint32_t interrupt_mask,
306edbfdb45SPaulo Zanoni 			      uint32_t enabled_irq_mask)
307edbfdb45SPaulo Zanoni {
308605cd25bSPaulo Zanoni 	uint32_t new_val;
309edbfdb45SPaulo Zanoni 
31015a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
31115a17aaeSDaniel Vetter 
312edbfdb45SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
313edbfdb45SPaulo Zanoni 
314605cd25bSPaulo Zanoni 	new_val = dev_priv->pm_irq_mask;
315f52ecbcfSPaulo Zanoni 	new_val &= ~interrupt_mask;
316f52ecbcfSPaulo Zanoni 	new_val |= (~enabled_irq_mask & interrupt_mask);
317f52ecbcfSPaulo Zanoni 
318605cd25bSPaulo Zanoni 	if (new_val != dev_priv->pm_irq_mask) {
319605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = new_val;
320a72fbc3aSImre Deak 		I915_WRITE(gen6_pm_imr(dev_priv), dev_priv->pm_irq_mask);
321a72fbc3aSImre Deak 		POSTING_READ(gen6_pm_imr(dev_priv));
322edbfdb45SPaulo Zanoni 	}
323f52ecbcfSPaulo Zanoni }
324edbfdb45SPaulo Zanoni 
325480c8033SDaniel Vetter void gen6_enable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
326edbfdb45SPaulo Zanoni {
3279939fba2SImre Deak 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
3289939fba2SImre Deak 		return;
3299939fba2SImre Deak 
330edbfdb45SPaulo Zanoni 	snb_update_pm_irq(dev_priv, mask, mask);
331edbfdb45SPaulo Zanoni }
332edbfdb45SPaulo Zanoni 
3339939fba2SImre Deak static void __gen6_disable_pm_irq(struct drm_i915_private *dev_priv,
3349939fba2SImre Deak 				  uint32_t mask)
3359939fba2SImre Deak {
3369939fba2SImre Deak 	snb_update_pm_irq(dev_priv, mask, 0);
3379939fba2SImre Deak }
3389939fba2SImre Deak 
339480c8033SDaniel Vetter void gen6_disable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
340edbfdb45SPaulo Zanoni {
3419939fba2SImre Deak 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
3429939fba2SImre Deak 		return;
3439939fba2SImre Deak 
3449939fba2SImre Deak 	__gen6_disable_pm_irq(dev_priv, mask);
345edbfdb45SPaulo Zanoni }
346edbfdb45SPaulo Zanoni 
3473cc134e3SImre Deak void gen6_reset_rps_interrupts(struct drm_device *dev)
3483cc134e3SImre Deak {
3493cc134e3SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
3503cc134e3SImre Deak 	uint32_t reg = gen6_pm_iir(dev_priv);
3513cc134e3SImre Deak 
3523cc134e3SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
3533cc134e3SImre Deak 	I915_WRITE(reg, dev_priv->pm_rps_events);
3543cc134e3SImre Deak 	I915_WRITE(reg, dev_priv->pm_rps_events);
3553cc134e3SImre Deak 	POSTING_READ(reg);
356096fad9eSImre Deak 	dev_priv->rps.pm_iir = 0;
3573cc134e3SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
3583cc134e3SImre Deak }
3593cc134e3SImre Deak 
360b900b949SImre Deak void gen6_enable_rps_interrupts(struct drm_device *dev)
361b900b949SImre Deak {
362b900b949SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
363b900b949SImre Deak 
364b900b949SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
36578e68d36SImre Deak 
366b900b949SImre Deak 	WARN_ON(dev_priv->rps.pm_iir);
3673cc134e3SImre Deak 	WARN_ON(I915_READ(gen6_pm_iir(dev_priv)) & dev_priv->pm_rps_events);
368d4d70aa5SImre Deak 	dev_priv->rps.interrupts_enabled = true;
36978e68d36SImre Deak 	I915_WRITE(gen6_pm_ier(dev_priv), I915_READ(gen6_pm_ier(dev_priv)) |
37078e68d36SImre Deak 				dev_priv->pm_rps_events);
371b900b949SImre Deak 	gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
37278e68d36SImre Deak 
373b900b949SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
374b900b949SImre Deak }
375b900b949SImre Deak 
37659d02a1fSImre Deak u32 gen6_sanitize_rps_pm_mask(struct drm_i915_private *dev_priv, u32 mask)
37759d02a1fSImre Deak {
37859d02a1fSImre Deak 	/*
379f24eeb19SImre Deak 	 * SNB,IVB can while VLV,CHV may hard hang on looping batchbuffer
38059d02a1fSImre Deak 	 * if GEN6_PM_UP_EI_EXPIRED is masked.
381f24eeb19SImre Deak 	 *
382f24eeb19SImre Deak 	 * TODO: verify if this can be reproduced on VLV,CHV.
38359d02a1fSImre Deak 	 */
38459d02a1fSImre Deak 	if (INTEL_INFO(dev_priv)->gen <= 7 && !IS_HASWELL(dev_priv))
38559d02a1fSImre Deak 		mask &= ~GEN6_PM_RP_UP_EI_EXPIRED;
38659d02a1fSImre Deak 
38759d02a1fSImre Deak 	if (INTEL_INFO(dev_priv)->gen >= 8)
38859d02a1fSImre Deak 		mask &= ~GEN8_PMINTR_REDIRECT_TO_NON_DISP;
38959d02a1fSImre Deak 
39059d02a1fSImre Deak 	return mask;
39159d02a1fSImre Deak }
39259d02a1fSImre Deak 
393b900b949SImre Deak void gen6_disable_rps_interrupts(struct drm_device *dev)
394b900b949SImre Deak {
395b900b949SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
396b900b949SImre Deak 
397d4d70aa5SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
398d4d70aa5SImre Deak 	dev_priv->rps.interrupts_enabled = false;
399d4d70aa5SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
400d4d70aa5SImre Deak 
401d4d70aa5SImre Deak 	cancel_work_sync(&dev_priv->rps.work);
402d4d70aa5SImre Deak 
4039939fba2SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
4049939fba2SImre Deak 
40559d02a1fSImre Deak 	I915_WRITE(GEN6_PMINTRMSK, gen6_sanitize_rps_pm_mask(dev_priv, ~0));
4069939fba2SImre Deak 
4079939fba2SImre Deak 	__gen6_disable_pm_irq(dev_priv, dev_priv->pm_rps_events);
408b900b949SImre Deak 	I915_WRITE(gen6_pm_ier(dev_priv), I915_READ(gen6_pm_ier(dev_priv)) &
409b900b949SImre Deak 				~dev_priv->pm_rps_events);
41058072ccbSImre Deak 
41158072ccbSImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
41258072ccbSImre Deak 
41358072ccbSImre Deak 	synchronize_irq(dev->irq);
414b900b949SImre Deak }
415b900b949SImre Deak 
4160961021aSBen Widawsky /**
4173a3b3c7dSVille Syrjälä   * bdw_update_port_irq - update DE port interrupt
4183a3b3c7dSVille Syrjälä   * @dev_priv: driver private
4193a3b3c7dSVille Syrjälä   * @interrupt_mask: mask of interrupt bits to update
4203a3b3c7dSVille Syrjälä   * @enabled_irq_mask: mask of interrupt bits to enable
4213a3b3c7dSVille Syrjälä   */
4223a3b3c7dSVille Syrjälä static void bdw_update_port_irq(struct drm_i915_private *dev_priv,
4233a3b3c7dSVille Syrjälä 				uint32_t interrupt_mask,
4243a3b3c7dSVille Syrjälä 				uint32_t enabled_irq_mask)
4253a3b3c7dSVille Syrjälä {
4263a3b3c7dSVille Syrjälä 	uint32_t new_val;
4273a3b3c7dSVille Syrjälä 	uint32_t old_val;
4283a3b3c7dSVille Syrjälä 
4293a3b3c7dSVille Syrjälä 	assert_spin_locked(&dev_priv->irq_lock);
4303a3b3c7dSVille Syrjälä 
4313a3b3c7dSVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
4323a3b3c7dSVille Syrjälä 
4333a3b3c7dSVille Syrjälä 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
4343a3b3c7dSVille Syrjälä 		return;
4353a3b3c7dSVille Syrjälä 
4363a3b3c7dSVille Syrjälä 	old_val = I915_READ(GEN8_DE_PORT_IMR);
4373a3b3c7dSVille Syrjälä 
4383a3b3c7dSVille Syrjälä 	new_val = old_val;
4393a3b3c7dSVille Syrjälä 	new_val &= ~interrupt_mask;
4403a3b3c7dSVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
4413a3b3c7dSVille Syrjälä 
4423a3b3c7dSVille Syrjälä 	if (new_val != old_val) {
4433a3b3c7dSVille Syrjälä 		I915_WRITE(GEN8_DE_PORT_IMR, new_val);
4443a3b3c7dSVille Syrjälä 		POSTING_READ(GEN8_DE_PORT_IMR);
4453a3b3c7dSVille Syrjälä 	}
4463a3b3c7dSVille Syrjälä }
4473a3b3c7dSVille Syrjälä 
4483a3b3c7dSVille Syrjälä /**
449fee884edSDaniel Vetter  * ibx_display_interrupt_update - update SDEIMR
450fee884edSDaniel Vetter  * @dev_priv: driver private
451fee884edSDaniel Vetter  * @interrupt_mask: mask of interrupt bits to update
452fee884edSDaniel Vetter  * @enabled_irq_mask: mask of interrupt bits to enable
453fee884edSDaniel Vetter  */
45447339cd9SDaniel Vetter void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
455fee884edSDaniel Vetter 				  uint32_t interrupt_mask,
456fee884edSDaniel Vetter 				  uint32_t enabled_irq_mask)
457fee884edSDaniel Vetter {
458fee884edSDaniel Vetter 	uint32_t sdeimr = I915_READ(SDEIMR);
459fee884edSDaniel Vetter 	sdeimr &= ~interrupt_mask;
460fee884edSDaniel Vetter 	sdeimr |= (~enabled_irq_mask & interrupt_mask);
461fee884edSDaniel Vetter 
46215a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
46315a17aaeSDaniel Vetter 
464fee884edSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
465fee884edSDaniel Vetter 
4669df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
467c67a470bSPaulo Zanoni 		return;
468c67a470bSPaulo Zanoni 
469fee884edSDaniel Vetter 	I915_WRITE(SDEIMR, sdeimr);
470fee884edSDaniel Vetter 	POSTING_READ(SDEIMR);
471fee884edSDaniel Vetter }
4728664281bSPaulo Zanoni 
473b5ea642aSDaniel Vetter static void
474755e9019SImre Deak __i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
475755e9019SImre Deak 		       u32 enable_mask, u32 status_mask)
4767c463586SKeith Packard {
4779db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
478755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
4797c463586SKeith Packard 
480b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
481d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
482b79480baSDaniel Vetter 
48304feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
48404feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
48504feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
48604feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
487755e9019SImre Deak 		return;
488755e9019SImre Deak 
489755e9019SImre Deak 	if ((pipestat & enable_mask) == enable_mask)
49046c06a30SVille Syrjälä 		return;
49146c06a30SVille Syrjälä 
49291d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] |= status_mask;
49391d181ddSImre Deak 
4947c463586SKeith Packard 	/* Enable the interrupt, clear any pending status */
495755e9019SImre Deak 	pipestat |= enable_mask | status_mask;
49646c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
4973143a2bfSChris Wilson 	POSTING_READ(reg);
4987c463586SKeith Packard }
4997c463586SKeith Packard 
500b5ea642aSDaniel Vetter static void
501755e9019SImre Deak __i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
502755e9019SImre Deak 		        u32 enable_mask, u32 status_mask)
5037c463586SKeith Packard {
5049db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
505755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
5067c463586SKeith Packard 
507b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
508d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
509b79480baSDaniel Vetter 
51004feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
51104feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
51204feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
51304feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
51446c06a30SVille Syrjälä 		return;
51546c06a30SVille Syrjälä 
516755e9019SImre Deak 	if ((pipestat & enable_mask) == 0)
517755e9019SImre Deak 		return;
518755e9019SImre Deak 
51991d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] &= ~status_mask;
52091d181ddSImre Deak 
521755e9019SImre Deak 	pipestat &= ~enable_mask;
52246c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
5233143a2bfSChris Wilson 	POSTING_READ(reg);
5247c463586SKeith Packard }
5257c463586SKeith Packard 
52610c59c51SImre Deak static u32 vlv_get_pipestat_enable_mask(struct drm_device *dev, u32 status_mask)
52710c59c51SImre Deak {
52810c59c51SImre Deak 	u32 enable_mask = status_mask << 16;
52910c59c51SImre Deak 
53010c59c51SImre Deak 	/*
531724a6905SVille Syrjälä 	 * On pipe A we don't support the PSR interrupt yet,
532724a6905SVille Syrjälä 	 * on pipe B and C the same bit MBZ.
53310c59c51SImre Deak 	 */
53410c59c51SImre Deak 	if (WARN_ON_ONCE(status_mask & PIPE_A_PSR_STATUS_VLV))
53510c59c51SImre Deak 		return 0;
536724a6905SVille Syrjälä 	/*
537724a6905SVille Syrjälä 	 * On pipe B and C we don't support the PSR interrupt yet, on pipe
538724a6905SVille Syrjälä 	 * A the same bit is for perf counters which we don't use either.
539724a6905SVille Syrjälä 	 */
540724a6905SVille Syrjälä 	if (WARN_ON_ONCE(status_mask & PIPE_B_PSR_STATUS_VLV))
541724a6905SVille Syrjälä 		return 0;
54210c59c51SImre Deak 
54310c59c51SImre Deak 	enable_mask &= ~(PIPE_FIFO_UNDERRUN_STATUS |
54410c59c51SImre Deak 			 SPRITE0_FLIP_DONE_INT_EN_VLV |
54510c59c51SImre Deak 			 SPRITE1_FLIP_DONE_INT_EN_VLV);
54610c59c51SImre Deak 	if (status_mask & SPRITE0_FLIP_DONE_INT_STATUS_VLV)
54710c59c51SImre Deak 		enable_mask |= SPRITE0_FLIP_DONE_INT_EN_VLV;
54810c59c51SImre Deak 	if (status_mask & SPRITE1_FLIP_DONE_INT_STATUS_VLV)
54910c59c51SImre Deak 		enable_mask |= SPRITE1_FLIP_DONE_INT_EN_VLV;
55010c59c51SImre Deak 
55110c59c51SImre Deak 	return enable_mask;
55210c59c51SImre Deak }
55310c59c51SImre Deak 
554755e9019SImre Deak void
555755e9019SImre Deak i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
556755e9019SImre Deak 		     u32 status_mask)
557755e9019SImre Deak {
558755e9019SImre Deak 	u32 enable_mask;
559755e9019SImre Deak 
56010c59c51SImre Deak 	if (IS_VALLEYVIEW(dev_priv->dev))
56110c59c51SImre Deak 		enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
56210c59c51SImre Deak 							   status_mask);
56310c59c51SImre Deak 	else
564755e9019SImre Deak 		enable_mask = status_mask << 16;
565755e9019SImre Deak 	__i915_enable_pipestat(dev_priv, pipe, enable_mask, status_mask);
566755e9019SImre Deak }
567755e9019SImre Deak 
568755e9019SImre Deak void
569755e9019SImre Deak i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
570755e9019SImre Deak 		      u32 status_mask)
571755e9019SImre Deak {
572755e9019SImre Deak 	u32 enable_mask;
573755e9019SImre Deak 
57410c59c51SImre Deak 	if (IS_VALLEYVIEW(dev_priv->dev))
57510c59c51SImre Deak 		enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
57610c59c51SImre Deak 							   status_mask);
57710c59c51SImre Deak 	else
578755e9019SImre Deak 		enable_mask = status_mask << 16;
579755e9019SImre Deak 	__i915_disable_pipestat(dev_priv, pipe, enable_mask, status_mask);
580755e9019SImre Deak }
581755e9019SImre Deak 
582c0e09200SDave Airlie /**
583f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
584468f9d29SJavier Martinez Canillas  * @dev: drm device
58501c66889SZhao Yakui  */
586f49e38ddSJani Nikula static void i915_enable_asle_pipestat(struct drm_device *dev)
58701c66889SZhao Yakui {
5882d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
5891ec14ad3SChris Wilson 
590f49e38ddSJani Nikula 	if (!dev_priv->opregion.asle || !IS_MOBILE(dev))
591f49e38ddSJani Nikula 		return;
592f49e38ddSJani Nikula 
59313321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
59401c66889SZhao Yakui 
595755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_STATUS);
596a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4)
5973b6c42e8SDaniel Vetter 		i915_enable_pipestat(dev_priv, PIPE_A,
598755e9019SImre Deak 				     PIPE_LEGACY_BLC_EVENT_STATUS);
5991ec14ad3SChris Wilson 
60013321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
60101c66889SZhao Yakui }
60201c66889SZhao Yakui 
603f75f3746SVille Syrjälä /*
604f75f3746SVille Syrjälä  * This timing diagram depicts the video signal in and
605f75f3746SVille Syrjälä  * around the vertical blanking period.
606f75f3746SVille Syrjälä  *
607f75f3746SVille Syrjälä  * Assumptions about the fictitious mode used in this example:
608f75f3746SVille Syrjälä  *  vblank_start >= 3
609f75f3746SVille Syrjälä  *  vsync_start = vblank_start + 1
610f75f3746SVille Syrjälä  *  vsync_end = vblank_start + 2
611f75f3746SVille Syrjälä  *  vtotal = vblank_start + 3
612f75f3746SVille Syrjälä  *
613f75f3746SVille Syrjälä  *           start of vblank:
614f75f3746SVille Syrjälä  *           latch double buffered registers
615f75f3746SVille Syrjälä  *           increment frame counter (ctg+)
616f75f3746SVille Syrjälä  *           generate start of vblank interrupt (gen4+)
617f75f3746SVille Syrjälä  *           |
618f75f3746SVille Syrjälä  *           |          frame start:
619f75f3746SVille Syrjälä  *           |          generate frame start interrupt (aka. vblank interrupt) (gmch)
620f75f3746SVille Syrjälä  *           |          may be shifted forward 1-3 extra lines via PIPECONF
621f75f3746SVille Syrjälä  *           |          |
622f75f3746SVille Syrjälä  *           |          |  start of vsync:
623f75f3746SVille Syrjälä  *           |          |  generate vsync interrupt
624f75f3746SVille Syrjälä  *           |          |  |
625f75f3746SVille Syrjälä  * ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx
626f75f3746SVille Syrjälä  *       .   \hs/   .      \hs/          \hs/          \hs/   .      \hs/
627f75f3746SVille Syrjälä  * ----va---> <-----------------vb--------------------> <--------va-------------
628f75f3746SVille Syrjälä  *       |          |       <----vs----->                     |
629f75f3746SVille Syrjälä  * -vbs-----> <---vbs+1---> <---vbs+2---> <-----0-----> <-----1-----> <-----2--- (scanline counter gen2)
630f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2---> <-----0--- (scanline counter gen3+)
631f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2- (scanline counter hsw+ hdmi)
632f75f3746SVille Syrjälä  *       |          |                                         |
633f75f3746SVille Syrjälä  *       last visible pixel                                   first visible pixel
634f75f3746SVille Syrjälä  *                  |                                         increment frame counter (gen3/4)
635f75f3746SVille Syrjälä  *                  pixel counter = vblank_start * htotal     pixel counter = 0 (gen3/4)
636f75f3746SVille Syrjälä  *
637f75f3746SVille Syrjälä  * x  = horizontal active
638f75f3746SVille Syrjälä  * _  = horizontal blanking
639f75f3746SVille Syrjälä  * hs = horizontal sync
640f75f3746SVille Syrjälä  * va = vertical active
641f75f3746SVille Syrjälä  * vb = vertical blanking
642f75f3746SVille Syrjälä  * vs = vertical sync
643f75f3746SVille Syrjälä  * vbs = vblank_start (number)
644f75f3746SVille Syrjälä  *
645f75f3746SVille Syrjälä  * Summary:
646f75f3746SVille Syrjälä  * - most events happen at the start of horizontal sync
647f75f3746SVille Syrjälä  * - frame start happens at the start of horizontal blank, 1-4 lines
648f75f3746SVille Syrjälä  *   (depending on PIPECONF settings) after the start of vblank
649f75f3746SVille Syrjälä  * - gen3/4 pixel and frame counter are synchronized with the start
650f75f3746SVille Syrjälä  *   of horizontal active on the first line of vertical active
651f75f3746SVille Syrjälä  */
652f75f3746SVille Syrjälä 
6534cdb83ecSVille Syrjälä static u32 i8xx_get_vblank_counter(struct drm_device *dev, int pipe)
6544cdb83ecSVille Syrjälä {
6554cdb83ecSVille Syrjälä 	/* Gen2 doesn't have a hardware frame counter */
6564cdb83ecSVille Syrjälä 	return 0;
6574cdb83ecSVille Syrjälä }
6584cdb83ecSVille Syrjälä 
65942f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
66042f52ef8SKeith Packard  * we use as a pipe index
66142f52ef8SKeith Packard  */
662f71d4af4SJesse Barnes static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
6630a3e67a4SJesse Barnes {
6642d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
6650a3e67a4SJesse Barnes 	unsigned long high_frame;
6660a3e67a4SJesse Barnes 	unsigned long low_frame;
6670b2a8e09SVille Syrjälä 	u32 high1, high2, low, pixel, vbl_start, hsync_start, htotal;
668391f75e2SVille Syrjälä 	struct intel_crtc *intel_crtc =
669391f75e2SVille Syrjälä 		to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
670fc467a22SMaarten Lankhorst 	const struct drm_display_mode *mode = &intel_crtc->base.hwmode;
671391f75e2SVille Syrjälä 
6720b2a8e09SVille Syrjälä 	htotal = mode->crtc_htotal;
6730b2a8e09SVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
6740b2a8e09SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
6750b2a8e09SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
6760b2a8e09SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
677391f75e2SVille Syrjälä 
6780b2a8e09SVille Syrjälä 	/* Convert to pixel count */
6790b2a8e09SVille Syrjälä 	vbl_start *= htotal;
6800b2a8e09SVille Syrjälä 
6810b2a8e09SVille Syrjälä 	/* Start of vblank event occurs at start of hsync */
6820b2a8e09SVille Syrjälä 	vbl_start -= htotal - hsync_start;
6830b2a8e09SVille Syrjälä 
6849db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
6859db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
6865eddb70bSChris Wilson 
6870a3e67a4SJesse Barnes 	/*
6880a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
6890a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
6900a3e67a4SJesse Barnes 	 * register.
6910a3e67a4SJesse Barnes 	 */
6920a3e67a4SJesse Barnes 	do {
6935eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
694391f75e2SVille Syrjälä 		low   = I915_READ(low_frame);
6955eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
6960a3e67a4SJesse Barnes 	} while (high1 != high2);
6970a3e67a4SJesse Barnes 
6985eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
699391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
7005eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
701391f75e2SVille Syrjälä 
702391f75e2SVille Syrjälä 	/*
703391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
704391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
705391f75e2SVille Syrjälä 	 * counter against vblank start.
706391f75e2SVille Syrjälä 	 */
707edc08d0aSVille Syrjälä 	return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
7080a3e67a4SJesse Barnes }
7090a3e67a4SJesse Barnes 
710f71d4af4SJesse Barnes static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
7119880b7a5SJesse Barnes {
7122d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
7139db4a9c7SJesse Barnes 	int reg = PIPE_FRMCOUNT_GM45(pipe);
7149880b7a5SJesse Barnes 
7159880b7a5SJesse Barnes 	return I915_READ(reg);
7169880b7a5SJesse Barnes }
7179880b7a5SJesse Barnes 
718ad3543edSMario Kleiner /* raw reads, only for fast reads of display block, no need for forcewake etc. */
719ad3543edSMario Kleiner #define __raw_i915_read32(dev_priv__, reg__) readl((dev_priv__)->regs + (reg__))
720ad3543edSMario Kleiner 
721a225f079SVille Syrjälä static int __intel_get_crtc_scanline(struct intel_crtc *crtc)
722a225f079SVille Syrjälä {
723a225f079SVille Syrjälä 	struct drm_device *dev = crtc->base.dev;
724a225f079SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
725fc467a22SMaarten Lankhorst 	const struct drm_display_mode *mode = &crtc->base.hwmode;
726a225f079SVille Syrjälä 	enum pipe pipe = crtc->pipe;
72780715b2fSVille Syrjälä 	int position, vtotal;
728a225f079SVille Syrjälä 
72980715b2fSVille Syrjälä 	vtotal = mode->crtc_vtotal;
730a225f079SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
731a225f079SVille Syrjälä 		vtotal /= 2;
732a225f079SVille Syrjälä 
733a225f079SVille Syrjälä 	if (IS_GEN2(dev))
734a225f079SVille Syrjälä 		position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
735a225f079SVille Syrjälä 	else
736a225f079SVille Syrjälä 		position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
737a225f079SVille Syrjälä 
738a225f079SVille Syrjälä 	/*
73941b578fbSJesse Barnes 	 * On HSW, the DSL reg (0x70000) appears to return 0 if we
74041b578fbSJesse Barnes 	 * read it just before the start of vblank.  So try it again
74141b578fbSJesse Barnes 	 * so we don't accidentally end up spanning a vblank frame
74241b578fbSJesse Barnes 	 * increment, causing the pipe_update_end() code to squak at us.
74341b578fbSJesse Barnes 	 *
74441b578fbSJesse Barnes 	 * The nature of this problem means we can't simply check the ISR
74541b578fbSJesse Barnes 	 * bit and return the vblank start value; nor can we use the scanline
74641b578fbSJesse Barnes 	 * debug register in the transcoder as it appears to have the same
74741b578fbSJesse Barnes 	 * problem.  We may need to extend this to include other platforms,
74841b578fbSJesse Barnes 	 * but so far testing only shows the problem on HSW.
74941b578fbSJesse Barnes 	 */
75041b578fbSJesse Barnes 	if (IS_HASWELL(dev) && !position) {
75141b578fbSJesse Barnes 		int i, temp;
75241b578fbSJesse Barnes 
75341b578fbSJesse Barnes 		for (i = 0; i < 100; i++) {
75441b578fbSJesse Barnes 			udelay(1);
75541b578fbSJesse Barnes 			temp = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) &
75641b578fbSJesse Barnes 				DSL_LINEMASK_GEN3;
75741b578fbSJesse Barnes 			if (temp != position) {
75841b578fbSJesse Barnes 				position = temp;
75941b578fbSJesse Barnes 				break;
76041b578fbSJesse Barnes 			}
76141b578fbSJesse Barnes 		}
76241b578fbSJesse Barnes 	}
76341b578fbSJesse Barnes 
76441b578fbSJesse Barnes 	/*
76580715b2fSVille Syrjälä 	 * See update_scanline_offset() for the details on the
76680715b2fSVille Syrjälä 	 * scanline_offset adjustment.
767a225f079SVille Syrjälä 	 */
76880715b2fSVille Syrjälä 	return (position + crtc->scanline_offset) % vtotal;
769a225f079SVille Syrjälä }
770a225f079SVille Syrjälä 
771f71d4af4SJesse Barnes static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
772abca9e45SVille Syrjälä 				    unsigned int flags, int *vpos, int *hpos,
7733bb403bfSVille Syrjälä 				    ktime_t *stime, ktime_t *etime,
7743bb403bfSVille Syrjälä 				    const struct drm_display_mode *mode)
7750af7e4dfSMario Kleiner {
776c2baf4b7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
777c2baf4b7SVille Syrjälä 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
778c2baf4b7SVille Syrjälä 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7793aa18df8SVille Syrjälä 	int position;
78078e8fc6bSVille Syrjälä 	int vbl_start, vbl_end, hsync_start, htotal, vtotal;
7810af7e4dfSMario Kleiner 	bool in_vbl = true;
7820af7e4dfSMario Kleiner 	int ret = 0;
783ad3543edSMario Kleiner 	unsigned long irqflags;
7840af7e4dfSMario Kleiner 
785fc467a22SMaarten Lankhorst 	if (WARN_ON(!mode->crtc_clock)) {
7860af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
7879db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
7880af7e4dfSMario Kleiner 		return 0;
7890af7e4dfSMario Kleiner 	}
7900af7e4dfSMario Kleiner 
791c2baf4b7SVille Syrjälä 	htotal = mode->crtc_htotal;
79278e8fc6bSVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
793c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
794c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
795c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
7960af7e4dfSMario Kleiner 
797d31faf65SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
798d31faf65SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
799d31faf65SVille Syrjälä 		vbl_end /= 2;
800d31faf65SVille Syrjälä 		vtotal /= 2;
801d31faf65SVille Syrjälä 	}
802d31faf65SVille Syrjälä 
803c2baf4b7SVille Syrjälä 	ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
804c2baf4b7SVille Syrjälä 
805ad3543edSMario Kleiner 	/*
806ad3543edSMario Kleiner 	 * Lock uncore.lock, as we will do multiple timing critical raw
807ad3543edSMario Kleiner 	 * register reads, potentially with preemption disabled, so the
808ad3543edSMario Kleiner 	 * following code must not block on uncore.lock.
809ad3543edSMario Kleiner 	 */
810ad3543edSMario Kleiner 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
811ad3543edSMario Kleiner 
812ad3543edSMario Kleiner 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
813ad3543edSMario Kleiner 
814ad3543edSMario Kleiner 	/* Get optional system timestamp before query. */
815ad3543edSMario Kleiner 	if (stime)
816ad3543edSMario Kleiner 		*stime = ktime_get();
817ad3543edSMario Kleiner 
8187c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
8190af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
8200af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
8210af7e4dfSMario Kleiner 		 */
822a225f079SVille Syrjälä 		position = __intel_get_crtc_scanline(intel_crtc);
8230af7e4dfSMario Kleiner 	} else {
8240af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
8250af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
8260af7e4dfSMario Kleiner 		 * scanout position.
8270af7e4dfSMario Kleiner 		 */
828ad3543edSMario Kleiner 		position = (__raw_i915_read32(dev_priv, PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
8290af7e4dfSMario Kleiner 
8303aa18df8SVille Syrjälä 		/* convert to pixel counts */
8313aa18df8SVille Syrjälä 		vbl_start *= htotal;
8323aa18df8SVille Syrjälä 		vbl_end *= htotal;
8333aa18df8SVille Syrjälä 		vtotal *= htotal;
83478e8fc6bSVille Syrjälä 
83578e8fc6bSVille Syrjälä 		/*
8367e78f1cbSVille Syrjälä 		 * In interlaced modes, the pixel counter counts all pixels,
8377e78f1cbSVille Syrjälä 		 * so one field will have htotal more pixels. In order to avoid
8387e78f1cbSVille Syrjälä 		 * the reported position from jumping backwards when the pixel
8397e78f1cbSVille Syrjälä 		 * counter is beyond the length of the shorter field, just
8407e78f1cbSVille Syrjälä 		 * clamp the position the length of the shorter field. This
8417e78f1cbSVille Syrjälä 		 * matches how the scanline counter based position works since
8427e78f1cbSVille Syrjälä 		 * the scanline counter doesn't count the two half lines.
8437e78f1cbSVille Syrjälä 		 */
8447e78f1cbSVille Syrjälä 		if (position >= vtotal)
8457e78f1cbSVille Syrjälä 			position = vtotal - 1;
8467e78f1cbSVille Syrjälä 
8477e78f1cbSVille Syrjälä 		/*
84878e8fc6bSVille Syrjälä 		 * Start of vblank interrupt is triggered at start of hsync,
84978e8fc6bSVille Syrjälä 		 * just prior to the first active line of vblank. However we
85078e8fc6bSVille Syrjälä 		 * consider lines to start at the leading edge of horizontal
85178e8fc6bSVille Syrjälä 		 * active. So, should we get here before we've crossed into
85278e8fc6bSVille Syrjälä 		 * the horizontal active of the first line in vblank, we would
85378e8fc6bSVille Syrjälä 		 * not set the DRM_SCANOUTPOS_INVBL flag. In order to fix that,
85478e8fc6bSVille Syrjälä 		 * always add htotal-hsync_start to the current pixel position.
85578e8fc6bSVille Syrjälä 		 */
85678e8fc6bSVille Syrjälä 		position = (position + htotal - hsync_start) % vtotal;
8573aa18df8SVille Syrjälä 	}
8583aa18df8SVille Syrjälä 
859ad3543edSMario Kleiner 	/* Get optional system timestamp after query. */
860ad3543edSMario Kleiner 	if (etime)
861ad3543edSMario Kleiner 		*etime = ktime_get();
862ad3543edSMario Kleiner 
863ad3543edSMario Kleiner 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
864ad3543edSMario Kleiner 
865ad3543edSMario Kleiner 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
866ad3543edSMario Kleiner 
8673aa18df8SVille Syrjälä 	in_vbl = position >= vbl_start && position < vbl_end;
8683aa18df8SVille Syrjälä 
8693aa18df8SVille Syrjälä 	/*
8703aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
8713aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
8723aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
8733aa18df8SVille Syrjälä 	 * up since vbl_end.
8743aa18df8SVille Syrjälä 	 */
8753aa18df8SVille Syrjälä 	if (position >= vbl_start)
8763aa18df8SVille Syrjälä 		position -= vbl_end;
8773aa18df8SVille Syrjälä 	else
8783aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
8793aa18df8SVille Syrjälä 
8807c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
8813aa18df8SVille Syrjälä 		*vpos = position;
8823aa18df8SVille Syrjälä 		*hpos = 0;
8833aa18df8SVille Syrjälä 	} else {
8840af7e4dfSMario Kleiner 		*vpos = position / htotal;
8850af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
8860af7e4dfSMario Kleiner 	}
8870af7e4dfSMario Kleiner 
8880af7e4dfSMario Kleiner 	/* In vblank? */
8890af7e4dfSMario Kleiner 	if (in_vbl)
8903d3cbd84SDaniel Vetter 		ret |= DRM_SCANOUTPOS_IN_VBLANK;
8910af7e4dfSMario Kleiner 
8920af7e4dfSMario Kleiner 	return ret;
8930af7e4dfSMario Kleiner }
8940af7e4dfSMario Kleiner 
895a225f079SVille Syrjälä int intel_get_crtc_scanline(struct intel_crtc *crtc)
896a225f079SVille Syrjälä {
897a225f079SVille Syrjälä 	struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
898a225f079SVille Syrjälä 	unsigned long irqflags;
899a225f079SVille Syrjälä 	int position;
900a225f079SVille Syrjälä 
901a225f079SVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
902a225f079SVille Syrjälä 	position = __intel_get_crtc_scanline(crtc);
903a225f079SVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
904a225f079SVille Syrjälä 
905a225f079SVille Syrjälä 	return position;
906a225f079SVille Syrjälä }
907a225f079SVille Syrjälä 
908f71d4af4SJesse Barnes static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
9090af7e4dfSMario Kleiner 			      int *max_error,
9100af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
9110af7e4dfSMario Kleiner 			      unsigned flags)
9120af7e4dfSMario Kleiner {
9134041b853SChris Wilson 	struct drm_crtc *crtc;
9140af7e4dfSMario Kleiner 
9157eb552aeSBen Widawsky 	if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
9164041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
9170af7e4dfSMario Kleiner 		return -EINVAL;
9180af7e4dfSMario Kleiner 	}
9190af7e4dfSMario Kleiner 
9200af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
9214041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
9224041b853SChris Wilson 	if (crtc == NULL) {
9234041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
9244041b853SChris Wilson 		return -EINVAL;
9254041b853SChris Wilson 	}
9264041b853SChris Wilson 
927fc467a22SMaarten Lankhorst 	if (!crtc->hwmode.crtc_clock) {
9284041b853SChris Wilson 		DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
9294041b853SChris Wilson 		return -EBUSY;
9304041b853SChris Wilson 	}
9310af7e4dfSMario Kleiner 
9320af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
9334041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
9344041b853SChris Wilson 						     vblank_time, flags,
935fc467a22SMaarten Lankhorst 						     &crtc->hwmode);
9360af7e4dfSMario Kleiner }
9370af7e4dfSMario Kleiner 
938d0ecd7e2SDaniel Vetter static void ironlake_rps_change_irq_handler(struct drm_device *dev)
939f97108d1SJesse Barnes {
9402d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
941b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
9429270388eSDaniel Vetter 	u8 new_delay;
9439270388eSDaniel Vetter 
944d0ecd7e2SDaniel Vetter 	spin_lock(&mchdev_lock);
945f97108d1SJesse Barnes 
94673edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
94773edd18fSDaniel Vetter 
94820e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
9499270388eSDaniel Vetter 
9507648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
951b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
952b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
953f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
954f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
955f97108d1SJesse Barnes 
956f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
957b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
95820e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
95920e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
96020e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
96120e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
962b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
96320e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
96420e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
96520e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
96620e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
967f97108d1SJesse Barnes 	}
968f97108d1SJesse Barnes 
9697648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
97020e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
971f97108d1SJesse Barnes 
972d0ecd7e2SDaniel Vetter 	spin_unlock(&mchdev_lock);
9739270388eSDaniel Vetter 
974f97108d1SJesse Barnes 	return;
975f97108d1SJesse Barnes }
976f97108d1SJesse Barnes 
97774cdb337SChris Wilson static void notify_ring(struct intel_engine_cs *ring)
978549f7365SChris Wilson {
97993b0a4e0SOscar Mateo 	if (!intel_ring_initialized(ring))
980475553deSChris Wilson 		return;
981475553deSChris Wilson 
982bcfcc8baSJohn Harrison 	trace_i915_gem_request_notify(ring);
9839862e600SChris Wilson 
984549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
985549f7365SChris Wilson }
986549f7365SChris Wilson 
98743cf3bf0SChris Wilson static void vlv_c0_read(struct drm_i915_private *dev_priv,
98843cf3bf0SChris Wilson 			struct intel_rps_ei *ei)
98931685c25SDeepak S {
99043cf3bf0SChris Wilson 	ei->cz_clock = vlv_punit_read(dev_priv, PUNIT_REG_CZ_TIMESTAMP);
99143cf3bf0SChris Wilson 	ei->render_c0 = I915_READ(VLV_RENDER_C0_COUNT);
99243cf3bf0SChris Wilson 	ei->media_c0 = I915_READ(VLV_MEDIA_C0_COUNT);
99331685c25SDeepak S }
99431685c25SDeepak S 
99543cf3bf0SChris Wilson static bool vlv_c0_above(struct drm_i915_private *dev_priv,
99643cf3bf0SChris Wilson 			 const struct intel_rps_ei *old,
99743cf3bf0SChris Wilson 			 const struct intel_rps_ei *now,
99843cf3bf0SChris Wilson 			 int threshold)
99931685c25SDeepak S {
100043cf3bf0SChris Wilson 	u64 time, c0;
10017bad74d5SVille Syrjälä 	unsigned int mul = 100;
100231685c25SDeepak S 
100343cf3bf0SChris Wilson 	if (old->cz_clock == 0)
100443cf3bf0SChris Wilson 		return false;
100531685c25SDeepak S 
10067bad74d5SVille Syrjälä 	if (I915_READ(VLV_COUNTER_CONTROL) & VLV_COUNT_RANGE_HIGH)
10077bad74d5SVille Syrjälä 		mul <<= 8;
10087bad74d5SVille Syrjälä 
100943cf3bf0SChris Wilson 	time = now->cz_clock - old->cz_clock;
10107bad74d5SVille Syrjälä 	time *= threshold * dev_priv->czclk_freq;
101131685c25SDeepak S 
101243cf3bf0SChris Wilson 	/* Workload can be split between render + media, e.g. SwapBuffers
101343cf3bf0SChris Wilson 	 * being blitted in X after being rendered in mesa. To account for
101443cf3bf0SChris Wilson 	 * this we need to combine both engines into our activity counter.
101543cf3bf0SChris Wilson 	 */
101643cf3bf0SChris Wilson 	c0 = now->render_c0 - old->render_c0;
101743cf3bf0SChris Wilson 	c0 += now->media_c0 - old->media_c0;
10187bad74d5SVille Syrjälä 	c0 *= mul * VLV_CZ_CLOCK_TO_MILLI_SEC;
101931685c25SDeepak S 
102043cf3bf0SChris Wilson 	return c0 >= time;
102131685c25SDeepak S }
102231685c25SDeepak S 
102343cf3bf0SChris Wilson void gen6_rps_reset_ei(struct drm_i915_private *dev_priv)
102443cf3bf0SChris Wilson {
102543cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &dev_priv->rps.down_ei);
102643cf3bf0SChris Wilson 	dev_priv->rps.up_ei = dev_priv->rps.down_ei;
102743cf3bf0SChris Wilson }
102843cf3bf0SChris Wilson 
102943cf3bf0SChris Wilson static u32 vlv_wa_c0_ei(struct drm_i915_private *dev_priv, u32 pm_iir)
103043cf3bf0SChris Wilson {
103143cf3bf0SChris Wilson 	struct intel_rps_ei now;
103243cf3bf0SChris Wilson 	u32 events = 0;
103343cf3bf0SChris Wilson 
10346f4b12f8SChris Wilson 	if ((pm_iir & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED)) == 0)
103543cf3bf0SChris Wilson 		return 0;
103643cf3bf0SChris Wilson 
103743cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &now);
103843cf3bf0SChris Wilson 	if (now.cz_clock == 0)
103943cf3bf0SChris Wilson 		return 0;
104031685c25SDeepak S 
104143cf3bf0SChris Wilson 	if (pm_iir & GEN6_PM_RP_DOWN_EI_EXPIRED) {
104243cf3bf0SChris Wilson 		if (!vlv_c0_above(dev_priv,
104343cf3bf0SChris Wilson 				  &dev_priv->rps.down_ei, &now,
10448fb55197SChris Wilson 				  dev_priv->rps.down_threshold))
104543cf3bf0SChris Wilson 			events |= GEN6_PM_RP_DOWN_THRESHOLD;
104643cf3bf0SChris Wilson 		dev_priv->rps.down_ei = now;
104731685c25SDeepak S 	}
104831685c25SDeepak S 
104943cf3bf0SChris Wilson 	if (pm_iir & GEN6_PM_RP_UP_EI_EXPIRED) {
105043cf3bf0SChris Wilson 		if (vlv_c0_above(dev_priv,
105143cf3bf0SChris Wilson 				 &dev_priv->rps.up_ei, &now,
10528fb55197SChris Wilson 				 dev_priv->rps.up_threshold))
105343cf3bf0SChris Wilson 			events |= GEN6_PM_RP_UP_THRESHOLD;
105443cf3bf0SChris Wilson 		dev_priv->rps.up_ei = now;
105543cf3bf0SChris Wilson 	}
105643cf3bf0SChris Wilson 
105743cf3bf0SChris Wilson 	return events;
105831685c25SDeepak S }
105931685c25SDeepak S 
1060f5a4c67dSChris Wilson static bool any_waiters(struct drm_i915_private *dev_priv)
1061f5a4c67dSChris Wilson {
1062f5a4c67dSChris Wilson 	struct intel_engine_cs *ring;
1063f5a4c67dSChris Wilson 	int i;
1064f5a4c67dSChris Wilson 
1065f5a4c67dSChris Wilson 	for_each_ring(ring, dev_priv, i)
1066f5a4c67dSChris Wilson 		if (ring->irq_refcount)
1067f5a4c67dSChris Wilson 			return true;
1068f5a4c67dSChris Wilson 
1069f5a4c67dSChris Wilson 	return false;
1070f5a4c67dSChris Wilson }
1071f5a4c67dSChris Wilson 
10724912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
10733b8d8d91SJesse Barnes {
10742d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
10752d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, rps.work);
10768d3afd7dSChris Wilson 	bool client_boost;
10778d3afd7dSChris Wilson 	int new_delay, adj, min, max;
1078edbfdb45SPaulo Zanoni 	u32 pm_iir;
10793b8d8d91SJesse Barnes 
108059cdb63dSDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
1081d4d70aa5SImre Deak 	/* Speed up work cancelation during disabling rps interrupts. */
1082d4d70aa5SImre Deak 	if (!dev_priv->rps.interrupts_enabled) {
1083d4d70aa5SImre Deak 		spin_unlock_irq(&dev_priv->irq_lock);
1084d4d70aa5SImre Deak 		return;
1085d4d70aa5SImre Deak 	}
1086c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
1087c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
1088a72fbc3aSImre Deak 	/* Make sure not to corrupt PMIMR state used by ringbuffer on GEN6 */
1089480c8033SDaniel Vetter 	gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
10908d3afd7dSChris Wilson 	client_boost = dev_priv->rps.client_boost;
10918d3afd7dSChris Wilson 	dev_priv->rps.client_boost = false;
109259cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
10934912d041SBen Widawsky 
109460611c13SPaulo Zanoni 	/* Make sure we didn't queue anything we're not going to process. */
1095a6706b45SDeepak S 	WARN_ON(pm_iir & ~dev_priv->pm_rps_events);
109660611c13SPaulo Zanoni 
10978d3afd7dSChris Wilson 	if ((pm_iir & dev_priv->pm_rps_events) == 0 && !client_boost)
10983b8d8d91SJesse Barnes 		return;
10993b8d8d91SJesse Barnes 
11004fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
11017b9e0ae6SChris Wilson 
110243cf3bf0SChris Wilson 	pm_iir |= vlv_wa_c0_ei(dev_priv, pm_iir);
110343cf3bf0SChris Wilson 
1104dd75fdc8SChris Wilson 	adj = dev_priv->rps.last_adj;
1105edcf284bSChris Wilson 	new_delay = dev_priv->rps.cur_freq;
11068d3afd7dSChris Wilson 	min = dev_priv->rps.min_freq_softlimit;
11078d3afd7dSChris Wilson 	max = dev_priv->rps.max_freq_softlimit;
11088d3afd7dSChris Wilson 
11098d3afd7dSChris Wilson 	if (client_boost) {
11108d3afd7dSChris Wilson 		new_delay = dev_priv->rps.max_freq_softlimit;
11118d3afd7dSChris Wilson 		adj = 0;
11128d3afd7dSChris Wilson 	} else if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
1113dd75fdc8SChris Wilson 		if (adj > 0)
1114dd75fdc8SChris Wilson 			adj *= 2;
1115edcf284bSChris Wilson 		else /* CHV needs even encode values */
1116edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? 2 : 1;
11177425034aSVille Syrjälä 		/*
11187425034aSVille Syrjälä 		 * For better performance, jump directly
11197425034aSVille Syrjälä 		 * to RPe if we're below it.
11207425034aSVille Syrjälä 		 */
1121edcf284bSChris Wilson 		if (new_delay < dev_priv->rps.efficient_freq - adj) {
1122b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1123edcf284bSChris Wilson 			adj = 0;
1124edcf284bSChris Wilson 		}
1125f5a4c67dSChris Wilson 	} else if (any_waiters(dev_priv)) {
1126f5a4c67dSChris Wilson 		adj = 0;
1127dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
1128b39fb297SBen Widawsky 		if (dev_priv->rps.cur_freq > dev_priv->rps.efficient_freq)
1129b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1130dd75fdc8SChris Wilson 		else
1131b39fb297SBen Widawsky 			new_delay = dev_priv->rps.min_freq_softlimit;
1132dd75fdc8SChris Wilson 		adj = 0;
1133dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1134dd75fdc8SChris Wilson 		if (adj < 0)
1135dd75fdc8SChris Wilson 			adj *= 2;
1136edcf284bSChris Wilson 		else /* CHV needs even encode values */
1137edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? -2 : -1;
1138dd75fdc8SChris Wilson 	} else { /* unknown event */
1139edcf284bSChris Wilson 		adj = 0;
1140dd75fdc8SChris Wilson 	}
11413b8d8d91SJesse Barnes 
1142edcf284bSChris Wilson 	dev_priv->rps.last_adj = adj;
1143edcf284bSChris Wilson 
114479249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
114579249636SBen Widawsky 	 * interrupt
114679249636SBen Widawsky 	 */
1147edcf284bSChris Wilson 	new_delay += adj;
11488d3afd7dSChris Wilson 	new_delay = clamp_t(int, new_delay, min, max);
114927544369SDeepak S 
1150ffe02b40SVille Syrjälä 	intel_set_rps(dev_priv->dev, new_delay);
11513b8d8d91SJesse Barnes 
11524fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
11533b8d8d91SJesse Barnes }
11543b8d8d91SJesse Barnes 
1155e3689190SBen Widawsky 
1156e3689190SBen Widawsky /**
1157e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
1158e3689190SBen Widawsky  * occurred.
1159e3689190SBen Widawsky  * @work: workqueue struct
1160e3689190SBen Widawsky  *
1161e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
1162e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
1163e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
1164e3689190SBen Widawsky  */
1165e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
1166e3689190SBen Widawsky {
11672d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
11682d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, l3_parity.error_work);
1169e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
117035a85ac6SBen Widawsky 	char *parity_event[6];
1171e3689190SBen Widawsky 	uint32_t misccpctl;
117235a85ac6SBen Widawsky 	uint8_t slice = 0;
1173e3689190SBen Widawsky 
1174e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
1175e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
1176e3689190SBen Widawsky 	 * any time we access those registers.
1177e3689190SBen Widawsky 	 */
1178e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
1179e3689190SBen Widawsky 
118035a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
118135a85ac6SBen Widawsky 	if (WARN_ON(!dev_priv->l3_parity.which_slice))
118235a85ac6SBen Widawsky 		goto out;
118335a85ac6SBen Widawsky 
1184e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
1185e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1186e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
1187e3689190SBen Widawsky 
118835a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
118935a85ac6SBen Widawsky 		u32 reg;
119035a85ac6SBen Widawsky 
119135a85ac6SBen Widawsky 		slice--;
119235a85ac6SBen Widawsky 		if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv->dev)))
119335a85ac6SBen Widawsky 			break;
119435a85ac6SBen Widawsky 
119535a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
119635a85ac6SBen Widawsky 
119735a85ac6SBen Widawsky 		reg = GEN7_L3CDERRST1 + (slice * 0x200);
119835a85ac6SBen Widawsky 
119935a85ac6SBen Widawsky 		error_status = I915_READ(reg);
1200e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
1201e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
1202e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1203e3689190SBen Widawsky 
120435a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
120535a85ac6SBen Widawsky 		POSTING_READ(reg);
1206e3689190SBen Widawsky 
1207cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1208e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1209e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1210e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
121135a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
121235a85ac6SBen Widawsky 		parity_event[5] = NULL;
1213e3689190SBen Widawsky 
12145bdebb18SDave Airlie 		kobject_uevent_env(&dev_priv->dev->primary->kdev->kobj,
1215e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
1216e3689190SBen Widawsky 
121735a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
121835a85ac6SBen Widawsky 			  slice, row, bank, subbank);
1219e3689190SBen Widawsky 
122035a85ac6SBen Widawsky 		kfree(parity_event[4]);
1221e3689190SBen Widawsky 		kfree(parity_event[3]);
1222e3689190SBen Widawsky 		kfree(parity_event[2]);
1223e3689190SBen Widawsky 		kfree(parity_event[1]);
1224e3689190SBen Widawsky 	}
1225e3689190SBen Widawsky 
122635a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
122735a85ac6SBen Widawsky 
122835a85ac6SBen Widawsky out:
122935a85ac6SBen Widawsky 	WARN_ON(dev_priv->l3_parity.which_slice);
12304cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
1231480c8033SDaniel Vetter 	gen5_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv->dev));
12324cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
123335a85ac6SBen Widawsky 
123435a85ac6SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
123535a85ac6SBen Widawsky }
123635a85ac6SBen Widawsky 
123735a85ac6SBen Widawsky static void ivybridge_parity_error_irq_handler(struct drm_device *dev, u32 iir)
1238e3689190SBen Widawsky {
12392d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1240e3689190SBen Widawsky 
1241040d2baaSBen Widawsky 	if (!HAS_L3_DPF(dev))
1242e3689190SBen Widawsky 		return;
1243e3689190SBen Widawsky 
1244d0ecd7e2SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1245480c8033SDaniel Vetter 	gen5_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev));
1246d0ecd7e2SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
1247e3689190SBen Widawsky 
124835a85ac6SBen Widawsky 	iir &= GT_PARITY_ERROR(dev);
124935a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
125035a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 1;
125135a85ac6SBen Widawsky 
125235a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
125335a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 0;
125435a85ac6SBen Widawsky 
1255a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
1256e3689190SBen Widawsky }
1257e3689190SBen Widawsky 
1258f1af8fc1SPaulo Zanoni static void ilk_gt_irq_handler(struct drm_device *dev,
1259f1af8fc1SPaulo Zanoni 			       struct drm_i915_private *dev_priv,
1260f1af8fc1SPaulo Zanoni 			       u32 gt_iir)
1261f1af8fc1SPaulo Zanoni {
1262f1af8fc1SPaulo Zanoni 	if (gt_iir &
1263f1af8fc1SPaulo Zanoni 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
126474cdb337SChris Wilson 		notify_ring(&dev_priv->ring[RCS]);
1265f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
126674cdb337SChris Wilson 		notify_ring(&dev_priv->ring[VCS]);
1267f1af8fc1SPaulo Zanoni }
1268f1af8fc1SPaulo Zanoni 
1269e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
1270e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
1271e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1272e7b4c6b1SDaniel Vetter {
1273e7b4c6b1SDaniel Vetter 
1274cc609d5dSBen Widawsky 	if (gt_iir &
1275cc609d5dSBen Widawsky 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
127674cdb337SChris Wilson 		notify_ring(&dev_priv->ring[RCS]);
1277cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
127874cdb337SChris Wilson 		notify_ring(&dev_priv->ring[VCS]);
1279cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
128074cdb337SChris Wilson 		notify_ring(&dev_priv->ring[BCS]);
1281e7b4c6b1SDaniel Vetter 
1282cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1283cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
1284aaecdf61SDaniel Vetter 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT))
1285aaecdf61SDaniel Vetter 		DRM_DEBUG("Command parser error, gt_iir 0x%08x\n", gt_iir);
1286e3689190SBen Widawsky 
128735a85ac6SBen Widawsky 	if (gt_iir & GT_PARITY_ERROR(dev))
128835a85ac6SBen Widawsky 		ivybridge_parity_error_irq_handler(dev, gt_iir);
1289e7b4c6b1SDaniel Vetter }
1290e7b4c6b1SDaniel Vetter 
129174cdb337SChris Wilson static irqreturn_t gen8_gt_irq_handler(struct drm_i915_private *dev_priv,
1292abd58f01SBen Widawsky 				       u32 master_ctl)
1293abd58f01SBen Widawsky {
1294abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
1295abd58f01SBen Widawsky 
1296abd58f01SBen Widawsky 	if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
129774cdb337SChris Wilson 		u32 tmp = I915_READ_FW(GEN8_GT_IIR(0));
1298abd58f01SBen Widawsky 		if (tmp) {
1299cb0d205eSChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(0), tmp);
1300abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1301e981e7b1SThomas Daniel 
130274cdb337SChris Wilson 			if (tmp & (GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT))
130374cdb337SChris Wilson 				intel_lrc_irq_handler(&dev_priv->ring[RCS]);
130474cdb337SChris Wilson 			if (tmp & (GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT))
130574cdb337SChris Wilson 				notify_ring(&dev_priv->ring[RCS]);
1306e981e7b1SThomas Daniel 
130774cdb337SChris Wilson 			if (tmp & (GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT))
130874cdb337SChris Wilson 				intel_lrc_irq_handler(&dev_priv->ring[BCS]);
130974cdb337SChris Wilson 			if (tmp & (GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT))
131074cdb337SChris Wilson 				notify_ring(&dev_priv->ring[BCS]);
1311abd58f01SBen Widawsky 		} else
1312abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT0)!\n");
1313abd58f01SBen Widawsky 	}
1314abd58f01SBen Widawsky 
131585f9b5f9SZhao Yakui 	if (master_ctl & (GEN8_GT_VCS1_IRQ | GEN8_GT_VCS2_IRQ)) {
131674cdb337SChris Wilson 		u32 tmp = I915_READ_FW(GEN8_GT_IIR(1));
1317abd58f01SBen Widawsky 		if (tmp) {
1318cb0d205eSChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(1), tmp);
1319abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1320e981e7b1SThomas Daniel 
132174cdb337SChris Wilson 			if (tmp & (GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT))
132274cdb337SChris Wilson 				intel_lrc_irq_handler(&dev_priv->ring[VCS]);
132374cdb337SChris Wilson 			if (tmp & (GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT))
132474cdb337SChris Wilson 				notify_ring(&dev_priv->ring[VCS]);
1325e981e7b1SThomas Daniel 
132674cdb337SChris Wilson 			if (tmp & (GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT))
132774cdb337SChris Wilson 				intel_lrc_irq_handler(&dev_priv->ring[VCS2]);
132874cdb337SChris Wilson 			if (tmp & (GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT))
132974cdb337SChris Wilson 				notify_ring(&dev_priv->ring[VCS2]);
1330abd58f01SBen Widawsky 		} else
1331abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT1)!\n");
1332abd58f01SBen Widawsky 	}
1333abd58f01SBen Widawsky 
133474cdb337SChris Wilson 	if (master_ctl & GEN8_GT_VECS_IRQ) {
133574cdb337SChris Wilson 		u32 tmp = I915_READ_FW(GEN8_GT_IIR(3));
133674cdb337SChris Wilson 		if (tmp) {
133774cdb337SChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(3), tmp);
133874cdb337SChris Wilson 			ret = IRQ_HANDLED;
133974cdb337SChris Wilson 
134074cdb337SChris Wilson 			if (tmp & (GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT))
134174cdb337SChris Wilson 				intel_lrc_irq_handler(&dev_priv->ring[VECS]);
134274cdb337SChris Wilson 			if (tmp & (GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT))
134374cdb337SChris Wilson 				notify_ring(&dev_priv->ring[VECS]);
134474cdb337SChris Wilson 		} else
134574cdb337SChris Wilson 			DRM_ERROR("The master control interrupt lied (GT3)!\n");
134674cdb337SChris Wilson 	}
134774cdb337SChris Wilson 
13480961021aSBen Widawsky 	if (master_ctl & GEN8_GT_PM_IRQ) {
134974cdb337SChris Wilson 		u32 tmp = I915_READ_FW(GEN8_GT_IIR(2));
13500961021aSBen Widawsky 		if (tmp & dev_priv->pm_rps_events) {
1351cb0d205eSChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(2),
13520961021aSBen Widawsky 				      tmp & dev_priv->pm_rps_events);
135338cc46d7SOscar Mateo 			ret = IRQ_HANDLED;
1354c9a9a268SImre Deak 			gen6_rps_irq_handler(dev_priv, tmp);
13550961021aSBen Widawsky 		} else
13560961021aSBen Widawsky 			DRM_ERROR("The master control interrupt lied (PM)!\n");
13570961021aSBen Widawsky 	}
13580961021aSBen Widawsky 
1359abd58f01SBen Widawsky 	return ret;
1360abd58f01SBen Widawsky }
1361abd58f01SBen Widawsky 
136263c88d22SImre Deak static bool bxt_port_hotplug_long_detect(enum port port, u32 val)
136363c88d22SImre Deak {
136463c88d22SImre Deak 	switch (port) {
136563c88d22SImre Deak 	case PORT_A:
1366195baa06SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
136763c88d22SImre Deak 	case PORT_B:
136863c88d22SImre Deak 		return val & PORTB_HOTPLUG_LONG_DETECT;
136963c88d22SImre Deak 	case PORT_C:
137063c88d22SImre Deak 		return val & PORTC_HOTPLUG_LONG_DETECT;
137163c88d22SImre Deak 	default:
137263c88d22SImre Deak 		return false;
137363c88d22SImre Deak 	}
137463c88d22SImre Deak }
137563c88d22SImre Deak 
13766dbf30ceSVille Syrjälä static bool spt_port_hotplug2_long_detect(enum port port, u32 val)
13776dbf30ceSVille Syrjälä {
13786dbf30ceSVille Syrjälä 	switch (port) {
13796dbf30ceSVille Syrjälä 	case PORT_E:
13806dbf30ceSVille Syrjälä 		return val & PORTE_HOTPLUG_LONG_DETECT;
13816dbf30ceSVille Syrjälä 	default:
13826dbf30ceSVille Syrjälä 		return false;
13836dbf30ceSVille Syrjälä 	}
13846dbf30ceSVille Syrjälä }
13856dbf30ceSVille Syrjälä 
138674c0b395SVille Syrjälä static bool spt_port_hotplug_long_detect(enum port port, u32 val)
138774c0b395SVille Syrjälä {
138874c0b395SVille Syrjälä 	switch (port) {
138974c0b395SVille Syrjälä 	case PORT_A:
139074c0b395SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
139174c0b395SVille Syrjälä 	case PORT_B:
139274c0b395SVille Syrjälä 		return val & PORTB_HOTPLUG_LONG_DETECT;
139374c0b395SVille Syrjälä 	case PORT_C:
139474c0b395SVille Syrjälä 		return val & PORTC_HOTPLUG_LONG_DETECT;
139574c0b395SVille Syrjälä 	case PORT_D:
139674c0b395SVille Syrjälä 		return val & PORTD_HOTPLUG_LONG_DETECT;
139774c0b395SVille Syrjälä 	default:
139874c0b395SVille Syrjälä 		return false;
139974c0b395SVille Syrjälä 	}
140074c0b395SVille Syrjälä }
140174c0b395SVille Syrjälä 
1402e4ce95aaSVille Syrjälä static bool ilk_port_hotplug_long_detect(enum port port, u32 val)
1403e4ce95aaSVille Syrjälä {
1404e4ce95aaSVille Syrjälä 	switch (port) {
1405e4ce95aaSVille Syrjälä 	case PORT_A:
1406e4ce95aaSVille Syrjälä 		return val & DIGITAL_PORTA_HOTPLUG_LONG_DETECT;
1407e4ce95aaSVille Syrjälä 	default:
1408e4ce95aaSVille Syrjälä 		return false;
1409e4ce95aaSVille Syrjälä 	}
1410e4ce95aaSVille Syrjälä }
1411e4ce95aaSVille Syrjälä 
1412676574dfSJani Nikula static bool pch_port_hotplug_long_detect(enum port port, u32 val)
141313cf5504SDave Airlie {
141413cf5504SDave Airlie 	switch (port) {
141513cf5504SDave Airlie 	case PORT_B:
1416676574dfSJani Nikula 		return val & PORTB_HOTPLUG_LONG_DETECT;
141713cf5504SDave Airlie 	case PORT_C:
1418676574dfSJani Nikula 		return val & PORTC_HOTPLUG_LONG_DETECT;
141913cf5504SDave Airlie 	case PORT_D:
1420676574dfSJani Nikula 		return val & PORTD_HOTPLUG_LONG_DETECT;
1421676574dfSJani Nikula 	default:
1422676574dfSJani Nikula 		return false;
142313cf5504SDave Airlie 	}
142413cf5504SDave Airlie }
142513cf5504SDave Airlie 
1426676574dfSJani Nikula static bool i9xx_port_hotplug_long_detect(enum port port, u32 val)
142713cf5504SDave Airlie {
142813cf5504SDave Airlie 	switch (port) {
142913cf5504SDave Airlie 	case PORT_B:
1430676574dfSJani Nikula 		return val & PORTB_HOTPLUG_INT_LONG_PULSE;
143113cf5504SDave Airlie 	case PORT_C:
1432676574dfSJani Nikula 		return val & PORTC_HOTPLUG_INT_LONG_PULSE;
143313cf5504SDave Airlie 	case PORT_D:
1434676574dfSJani Nikula 		return val & PORTD_HOTPLUG_INT_LONG_PULSE;
1435676574dfSJani Nikula 	default:
1436676574dfSJani Nikula 		return false;
143713cf5504SDave Airlie 	}
143813cf5504SDave Airlie }
143913cf5504SDave Airlie 
144042db67d6SVille Syrjälä /*
144142db67d6SVille Syrjälä  * Get a bit mask of pins that have triggered, and which ones may be long.
144242db67d6SVille Syrjälä  * This can be called multiple times with the same masks to accumulate
144342db67d6SVille Syrjälä  * hotplug detection results from several registers.
144442db67d6SVille Syrjälä  *
144542db67d6SVille Syrjälä  * Note that the caller is expected to zero out the masks initially.
144642db67d6SVille Syrjälä  */
1447fd63e2a9SImre Deak static void intel_get_hpd_pins(u32 *pin_mask, u32 *long_mask,
14488c841e57SJani Nikula 			     u32 hotplug_trigger, u32 dig_hotplug_reg,
1449fd63e2a9SImre Deak 			     const u32 hpd[HPD_NUM_PINS],
1450fd63e2a9SImre Deak 			     bool long_pulse_detect(enum port port, u32 val))
1451676574dfSJani Nikula {
14528c841e57SJani Nikula 	enum port port;
1453676574dfSJani Nikula 	int i;
1454676574dfSJani Nikula 
1455676574dfSJani Nikula 	for_each_hpd_pin(i) {
14568c841e57SJani Nikula 		if ((hpd[i] & hotplug_trigger) == 0)
14578c841e57SJani Nikula 			continue;
14588c841e57SJani Nikula 
1459676574dfSJani Nikula 		*pin_mask |= BIT(i);
1460676574dfSJani Nikula 
1461cc24fcdcSImre Deak 		if (!intel_hpd_pin_to_port(i, &port))
1462cc24fcdcSImre Deak 			continue;
1463cc24fcdcSImre Deak 
1464fd63e2a9SImre Deak 		if (long_pulse_detect(port, dig_hotplug_reg))
1465676574dfSJani Nikula 			*long_mask |= BIT(i);
1466676574dfSJani Nikula 	}
1467676574dfSJani Nikula 
1468676574dfSJani Nikula 	DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x, dig 0x%08x, pins 0x%08x\n",
1469676574dfSJani Nikula 			 hotplug_trigger, dig_hotplug_reg, *pin_mask);
1470676574dfSJani Nikula 
1471676574dfSJani Nikula }
1472676574dfSJani Nikula 
1473515ac2bbSDaniel Vetter static void gmbus_irq_handler(struct drm_device *dev)
1474515ac2bbSDaniel Vetter {
14752d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
147628c70f16SDaniel Vetter 
147728c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1478515ac2bbSDaniel Vetter }
1479515ac2bbSDaniel Vetter 
1480ce99c256SDaniel Vetter static void dp_aux_irq_handler(struct drm_device *dev)
1481ce99c256SDaniel Vetter {
14822d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
14839ee32feaSDaniel Vetter 
14849ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1485ce99c256SDaniel Vetter }
1486ce99c256SDaniel Vetter 
14878bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
1488277de95eSDaniel Vetter static void display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1489eba94eb9SDaniel Vetter 					 uint32_t crc0, uint32_t crc1,
1490eba94eb9SDaniel Vetter 					 uint32_t crc2, uint32_t crc3,
14918bc5e955SDaniel Vetter 					 uint32_t crc4)
14928bf1e9f1SShuang He {
14938bf1e9f1SShuang He 	struct drm_i915_private *dev_priv = dev->dev_private;
14948bf1e9f1SShuang He 	struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
14958bf1e9f1SShuang He 	struct intel_pipe_crc_entry *entry;
1496ac2300d4SDamien Lespiau 	int head, tail;
1497b2c88f5bSDamien Lespiau 
1498d538bbdfSDamien Lespiau 	spin_lock(&pipe_crc->lock);
1499d538bbdfSDamien Lespiau 
15000c912c79SDamien Lespiau 	if (!pipe_crc->entries) {
1501d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
150234273620SDaniel Vetter 		DRM_DEBUG_KMS("spurious interrupt\n");
15030c912c79SDamien Lespiau 		return;
15040c912c79SDamien Lespiau 	}
15050c912c79SDamien Lespiau 
1506d538bbdfSDamien Lespiau 	head = pipe_crc->head;
1507d538bbdfSDamien Lespiau 	tail = pipe_crc->tail;
1508b2c88f5bSDamien Lespiau 
1509b2c88f5bSDamien Lespiau 	if (CIRC_SPACE(head, tail, INTEL_PIPE_CRC_ENTRIES_NR) < 1) {
1510d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
1511b2c88f5bSDamien Lespiau 		DRM_ERROR("CRC buffer overflowing\n");
1512b2c88f5bSDamien Lespiau 		return;
1513b2c88f5bSDamien Lespiau 	}
1514b2c88f5bSDamien Lespiau 
1515b2c88f5bSDamien Lespiau 	entry = &pipe_crc->entries[head];
15168bf1e9f1SShuang He 
15178bc5e955SDaniel Vetter 	entry->frame = dev->driver->get_vblank_counter(dev, pipe);
1518eba94eb9SDaniel Vetter 	entry->crc[0] = crc0;
1519eba94eb9SDaniel Vetter 	entry->crc[1] = crc1;
1520eba94eb9SDaniel Vetter 	entry->crc[2] = crc2;
1521eba94eb9SDaniel Vetter 	entry->crc[3] = crc3;
1522eba94eb9SDaniel Vetter 	entry->crc[4] = crc4;
1523b2c88f5bSDamien Lespiau 
1524b2c88f5bSDamien Lespiau 	head = (head + 1) & (INTEL_PIPE_CRC_ENTRIES_NR - 1);
1525d538bbdfSDamien Lespiau 	pipe_crc->head = head;
1526d538bbdfSDamien Lespiau 
1527d538bbdfSDamien Lespiau 	spin_unlock(&pipe_crc->lock);
152807144428SDamien Lespiau 
152907144428SDamien Lespiau 	wake_up_interruptible(&pipe_crc->wq);
15308bf1e9f1SShuang He }
1531277de95eSDaniel Vetter #else
1532277de95eSDaniel Vetter static inline void
1533277de95eSDaniel Vetter display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1534277de95eSDaniel Vetter 			     uint32_t crc0, uint32_t crc1,
1535277de95eSDaniel Vetter 			     uint32_t crc2, uint32_t crc3,
1536277de95eSDaniel Vetter 			     uint32_t crc4) {}
1537277de95eSDaniel Vetter #endif
1538eba94eb9SDaniel Vetter 
1539277de95eSDaniel Vetter 
1540277de95eSDaniel Vetter static void hsw_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
15415a69b89fSDaniel Vetter {
15425a69b89fSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
15435a69b89fSDaniel Vetter 
1544277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
15455a69b89fSDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
15465a69b89fSDaniel Vetter 				     0, 0, 0, 0);
15475a69b89fSDaniel Vetter }
15485a69b89fSDaniel Vetter 
1549277de95eSDaniel Vetter static void ivb_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
1550eba94eb9SDaniel Vetter {
1551eba94eb9SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1552eba94eb9SDaniel Vetter 
1553277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
1554eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1555eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1556eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1557eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
15588bc5e955SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1559eba94eb9SDaniel Vetter }
15605b3a856bSDaniel Vetter 
1561277de95eSDaniel Vetter static void i9xx_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
15625b3a856bSDaniel Vetter {
15635b3a856bSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
15640b5c5ed0SDaniel Vetter 	uint32_t res1, res2;
15650b5c5ed0SDaniel Vetter 
15660b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 3)
15670b5c5ed0SDaniel Vetter 		res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
15680b5c5ed0SDaniel Vetter 	else
15690b5c5ed0SDaniel Vetter 		res1 = 0;
15700b5c5ed0SDaniel Vetter 
15710b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
15720b5c5ed0SDaniel Vetter 		res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
15730b5c5ed0SDaniel Vetter 	else
15740b5c5ed0SDaniel Vetter 		res2 = 0;
15755b3a856bSDaniel Vetter 
1576277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
15770b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_RED(pipe)),
15780b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_GREEN(pipe)),
15790b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_BLUE(pipe)),
15800b5c5ed0SDaniel Vetter 				     res1, res2);
15815b3a856bSDaniel Vetter }
15828bf1e9f1SShuang He 
15831403c0d4SPaulo Zanoni /* The RPS events need forcewake, so we add them to a work queue and mask their
15841403c0d4SPaulo Zanoni  * IMR bits until the work is done. Other interrupts can be processed without
15851403c0d4SPaulo Zanoni  * the work queue. */
15861403c0d4SPaulo Zanoni static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1587baf02a1fSBen Widawsky {
1588a6706b45SDeepak S 	if (pm_iir & dev_priv->pm_rps_events) {
158959cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
1590480c8033SDaniel Vetter 		gen6_disable_pm_irq(dev_priv, pm_iir & dev_priv->pm_rps_events);
1591d4d70aa5SImre Deak 		if (dev_priv->rps.interrupts_enabled) {
1592d4d70aa5SImre Deak 			dev_priv->rps.pm_iir |= pm_iir & dev_priv->pm_rps_events;
15932adbee62SDaniel Vetter 			queue_work(dev_priv->wq, &dev_priv->rps.work);
159441a05a3aSDaniel Vetter 		}
1595d4d70aa5SImre Deak 		spin_unlock(&dev_priv->irq_lock);
1596d4d70aa5SImre Deak 	}
1597baf02a1fSBen Widawsky 
1598c9a9a268SImre Deak 	if (INTEL_INFO(dev_priv)->gen >= 8)
1599c9a9a268SImre Deak 		return;
1600c9a9a268SImre Deak 
16011403c0d4SPaulo Zanoni 	if (HAS_VEBOX(dev_priv->dev)) {
160212638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
160374cdb337SChris Wilson 			notify_ring(&dev_priv->ring[VECS]);
160412638c57SBen Widawsky 
1605aaecdf61SDaniel Vetter 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT)
1606aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, pm_iir 0x%08x\n", pm_iir);
160712638c57SBen Widawsky 	}
16081403c0d4SPaulo Zanoni }
1609baf02a1fSBen Widawsky 
16108d7849dbSVille Syrjälä static bool intel_pipe_handle_vblank(struct drm_device *dev, enum pipe pipe)
16118d7849dbSVille Syrjälä {
16128d7849dbSVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
16138d7849dbSVille Syrjälä 		return false;
16148d7849dbSVille Syrjälä 
16158d7849dbSVille Syrjälä 	return true;
16168d7849dbSVille Syrjälä }
16178d7849dbSVille Syrjälä 
1618c1874ed7SImre Deak static void valleyview_pipestat_irq_handler(struct drm_device *dev, u32 iir)
16197e231dbeSJesse Barnes {
1620c1874ed7SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
162191d181ddSImre Deak 	u32 pipe_stats[I915_MAX_PIPES] = { };
16227e231dbeSJesse Barnes 	int pipe;
16237e231dbeSJesse Barnes 
162458ead0d7SImre Deak 	spin_lock(&dev_priv->irq_lock);
1625055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
162691d181ddSImre Deak 		int reg;
1627bbb5eebfSDaniel Vetter 		u32 mask, iir_bit = 0;
162891d181ddSImre Deak 
1629bbb5eebfSDaniel Vetter 		/*
1630bbb5eebfSDaniel Vetter 		 * PIPESTAT bits get signalled even when the interrupt is
1631bbb5eebfSDaniel Vetter 		 * disabled with the mask bits, and some of the status bits do
1632bbb5eebfSDaniel Vetter 		 * not generate interrupts at all (like the underrun bit). Hence
1633bbb5eebfSDaniel Vetter 		 * we need to be careful that we only handle what we want to
1634bbb5eebfSDaniel Vetter 		 * handle.
1635bbb5eebfSDaniel Vetter 		 */
16360f239f4cSDaniel Vetter 
16370f239f4cSDaniel Vetter 		/* fifo underruns are filterered in the underrun handler. */
16380f239f4cSDaniel Vetter 		mask = PIPE_FIFO_UNDERRUN_STATUS;
1639bbb5eebfSDaniel Vetter 
1640bbb5eebfSDaniel Vetter 		switch (pipe) {
1641bbb5eebfSDaniel Vetter 		case PIPE_A:
1642bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
1643bbb5eebfSDaniel Vetter 			break;
1644bbb5eebfSDaniel Vetter 		case PIPE_B:
1645bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
1646bbb5eebfSDaniel Vetter 			break;
16473278f67fSVille Syrjälä 		case PIPE_C:
16483278f67fSVille Syrjälä 			iir_bit = I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
16493278f67fSVille Syrjälä 			break;
1650bbb5eebfSDaniel Vetter 		}
1651bbb5eebfSDaniel Vetter 		if (iir & iir_bit)
1652bbb5eebfSDaniel Vetter 			mask |= dev_priv->pipestat_irq_mask[pipe];
1653bbb5eebfSDaniel Vetter 
1654bbb5eebfSDaniel Vetter 		if (!mask)
165591d181ddSImre Deak 			continue;
165691d181ddSImre Deak 
165791d181ddSImre Deak 		reg = PIPESTAT(pipe);
1658bbb5eebfSDaniel Vetter 		mask |= PIPESTAT_INT_ENABLE_MASK;
1659bbb5eebfSDaniel Vetter 		pipe_stats[pipe] = I915_READ(reg) & mask;
16607e231dbeSJesse Barnes 
16617e231dbeSJesse Barnes 		/*
16627e231dbeSJesse Barnes 		 * Clear the PIPE*STAT regs before the IIR
16637e231dbeSJesse Barnes 		 */
166491d181ddSImre Deak 		if (pipe_stats[pipe] & (PIPE_FIFO_UNDERRUN_STATUS |
166591d181ddSImre Deak 					PIPESTAT_INT_STATUS_MASK))
16667e231dbeSJesse Barnes 			I915_WRITE(reg, pipe_stats[pipe]);
16677e231dbeSJesse Barnes 	}
166858ead0d7SImre Deak 	spin_unlock(&dev_priv->irq_lock);
16697e231dbeSJesse Barnes 
1670055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1671d6bbafa1SChris Wilson 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
1672d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
1673d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
167431acc7f5SJesse Barnes 
1675579a9b0eSImre Deak 		if (pipe_stats[pipe] & PLANE_FLIP_DONE_INT_STATUS_VLV) {
167631acc7f5SJesse Barnes 			intel_prepare_page_flip(dev, pipe);
167731acc7f5SJesse Barnes 			intel_finish_page_flip(dev, pipe);
167831acc7f5SJesse Barnes 		}
16794356d586SDaniel Vetter 
16804356d586SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1681277de95eSDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
16822d9d2b0bSVille Syrjälä 
16831f7247c0SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
16841f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
168531acc7f5SJesse Barnes 	}
168631acc7f5SJesse Barnes 
1687c1874ed7SImre Deak 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1688c1874ed7SImre Deak 		gmbus_irq_handler(dev);
1689c1874ed7SImre Deak }
1690c1874ed7SImre Deak 
169116c6c56bSVille Syrjälä static void i9xx_hpd_irq_handler(struct drm_device *dev)
169216c6c56bSVille Syrjälä {
169316c6c56bSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
169416c6c56bSVille Syrjälä 	u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
169542db67d6SVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
169616c6c56bSVille Syrjälä 
16970d2e4297SJani Nikula 	if (!hotplug_status)
16980d2e4297SJani Nikula 		return;
16990d2e4297SJani Nikula 
17003ff60f89SOscar Mateo 	I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
17013ff60f89SOscar Mateo 	/*
17023ff60f89SOscar Mateo 	 * Make sure hotplug status is cleared before we clear IIR, or else we
17033ff60f89SOscar Mateo 	 * may miss hotplug events.
17043ff60f89SOscar Mateo 	 */
17053ff60f89SOscar Mateo 	POSTING_READ(PORT_HOTPLUG_STAT);
17063ff60f89SOscar Mateo 
17074bca26d0SVille Syrjälä 	if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
170816c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
170916c6c56bSVille Syrjälä 
171058f2cf24SVille Syrjälä 		if (hotplug_trigger) {
1711fd63e2a9SImre Deak 			intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
1712fd63e2a9SImre Deak 					   hotplug_trigger, hpd_status_g4x,
1713fd63e2a9SImre Deak 					   i9xx_port_hotplug_long_detect);
171458f2cf24SVille Syrjälä 
1715676574dfSJani Nikula 			intel_hpd_irq_handler(dev, pin_mask, long_mask);
171658f2cf24SVille Syrjälä 		}
1717369712e8SJani Nikula 
1718369712e8SJani Nikula 		if (hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
1719369712e8SJani Nikula 			dp_aux_irq_handler(dev);
172016c6c56bSVille Syrjälä 	} else {
172116c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
172216c6c56bSVille Syrjälä 
172358f2cf24SVille Syrjälä 		if (hotplug_trigger) {
1724fd63e2a9SImre Deak 			intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
17254e3d1e26SVille Syrjälä 					   hotplug_trigger, hpd_status_i915,
1726fd63e2a9SImre Deak 					   i9xx_port_hotplug_long_detect);
1727676574dfSJani Nikula 			intel_hpd_irq_handler(dev, pin_mask, long_mask);
172816c6c56bSVille Syrjälä 		}
17293ff60f89SOscar Mateo 	}
173058f2cf24SVille Syrjälä }
173116c6c56bSVille Syrjälä 
1732c1874ed7SImre Deak static irqreturn_t valleyview_irq_handler(int irq, void *arg)
1733c1874ed7SImre Deak {
173445a83f84SDaniel Vetter 	struct drm_device *dev = arg;
17352d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1736c1874ed7SImre Deak 	u32 iir, gt_iir, pm_iir;
1737c1874ed7SImre Deak 	irqreturn_t ret = IRQ_NONE;
1738c1874ed7SImre Deak 
17392dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
17402dd2a883SImre Deak 		return IRQ_NONE;
17412dd2a883SImre Deak 
1742c1874ed7SImre Deak 	while (true) {
17433ff60f89SOscar Mateo 		/* Find, clear, then process each source of interrupt */
17443ff60f89SOscar Mateo 
1745c1874ed7SImre Deak 		gt_iir = I915_READ(GTIIR);
17463ff60f89SOscar Mateo 		if (gt_iir)
17473ff60f89SOscar Mateo 			I915_WRITE(GTIIR, gt_iir);
17483ff60f89SOscar Mateo 
1749c1874ed7SImre Deak 		pm_iir = I915_READ(GEN6_PMIIR);
17503ff60f89SOscar Mateo 		if (pm_iir)
17513ff60f89SOscar Mateo 			I915_WRITE(GEN6_PMIIR, pm_iir);
17523ff60f89SOscar Mateo 
17533ff60f89SOscar Mateo 		iir = I915_READ(VLV_IIR);
17543ff60f89SOscar Mateo 		if (iir) {
17553ff60f89SOscar Mateo 			/* Consume port before clearing IIR or we'll miss events */
17563ff60f89SOscar Mateo 			if (iir & I915_DISPLAY_PORT_INTERRUPT)
17573ff60f89SOscar Mateo 				i9xx_hpd_irq_handler(dev);
17583ff60f89SOscar Mateo 			I915_WRITE(VLV_IIR, iir);
17593ff60f89SOscar Mateo 		}
1760c1874ed7SImre Deak 
1761c1874ed7SImre Deak 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
1762c1874ed7SImre Deak 			goto out;
1763c1874ed7SImre Deak 
1764c1874ed7SImre Deak 		ret = IRQ_HANDLED;
1765c1874ed7SImre Deak 
17663ff60f89SOscar Mateo 		if (gt_iir)
1767c1874ed7SImre Deak 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
176860611c13SPaulo Zanoni 		if (pm_iir)
1769d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
17703ff60f89SOscar Mateo 		/* Call regardless, as some status bits might not be
17713ff60f89SOscar Mateo 		 * signalled in iir */
17723ff60f89SOscar Mateo 		valleyview_pipestat_irq_handler(dev, iir);
17737e231dbeSJesse Barnes 	}
17747e231dbeSJesse Barnes 
17757e231dbeSJesse Barnes out:
17767e231dbeSJesse Barnes 	return ret;
17777e231dbeSJesse Barnes }
17787e231dbeSJesse Barnes 
177943f328d7SVille Syrjälä static irqreturn_t cherryview_irq_handler(int irq, void *arg)
178043f328d7SVille Syrjälä {
178145a83f84SDaniel Vetter 	struct drm_device *dev = arg;
178243f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
178343f328d7SVille Syrjälä 	u32 master_ctl, iir;
178443f328d7SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
178543f328d7SVille Syrjälä 
17862dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
17872dd2a883SImre Deak 		return IRQ_NONE;
17882dd2a883SImre Deak 
17898e5fd599SVille Syrjälä 	for (;;) {
17908e5fd599SVille Syrjälä 		master_ctl = I915_READ(GEN8_MASTER_IRQ) & ~GEN8_MASTER_IRQ_CONTROL;
17913278f67fSVille Syrjälä 		iir = I915_READ(VLV_IIR);
17923278f67fSVille Syrjälä 
17933278f67fSVille Syrjälä 		if (master_ctl == 0 && iir == 0)
17948e5fd599SVille Syrjälä 			break;
179543f328d7SVille Syrjälä 
179627b6c122SOscar Mateo 		ret = IRQ_HANDLED;
179727b6c122SOscar Mateo 
179843f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, 0);
179943f328d7SVille Syrjälä 
180027b6c122SOscar Mateo 		/* Find, clear, then process each source of interrupt */
180127b6c122SOscar Mateo 
180227b6c122SOscar Mateo 		if (iir) {
180327b6c122SOscar Mateo 			/* Consume port before clearing IIR or we'll miss events */
180427b6c122SOscar Mateo 			if (iir & I915_DISPLAY_PORT_INTERRUPT)
180527b6c122SOscar Mateo 				i9xx_hpd_irq_handler(dev);
180627b6c122SOscar Mateo 			I915_WRITE(VLV_IIR, iir);
180727b6c122SOscar Mateo 		}
180827b6c122SOscar Mateo 
180974cdb337SChris Wilson 		gen8_gt_irq_handler(dev_priv, master_ctl);
181043f328d7SVille Syrjälä 
181127b6c122SOscar Mateo 		/* Call regardless, as some status bits might not be
181227b6c122SOscar Mateo 		 * signalled in iir */
18133278f67fSVille Syrjälä 		valleyview_pipestat_irq_handler(dev, iir);
181443f328d7SVille Syrjälä 
181543f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
181643f328d7SVille Syrjälä 		POSTING_READ(GEN8_MASTER_IRQ);
18178e5fd599SVille Syrjälä 	}
18183278f67fSVille Syrjälä 
181943f328d7SVille Syrjälä 	return ret;
182043f328d7SVille Syrjälä }
182143f328d7SVille Syrjälä 
182240e56410SVille Syrjälä static void ibx_hpd_irq_handler(struct drm_device *dev, u32 hotplug_trigger,
182340e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
1824776ad806SJesse Barnes {
182540e56410SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(dev);
182642db67d6SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
1827776ad806SJesse Barnes 
182813cf5504SDave Airlie 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
182913cf5504SDave Airlie 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
183013cf5504SDave Airlie 
1831fd63e2a9SImre Deak 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
183240e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
1833fd63e2a9SImre Deak 			   pch_port_hotplug_long_detect);
183440e56410SVille Syrjälä 
1835676574dfSJani Nikula 	intel_hpd_irq_handler(dev, pin_mask, long_mask);
1836aaf5ec2eSSonika Jindal }
183791d131d2SDaniel Vetter 
183840e56410SVille Syrjälä static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
183940e56410SVille Syrjälä {
184040e56410SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
184140e56410SVille Syrjälä 	int pipe;
184240e56410SVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
184340e56410SVille Syrjälä 
184440e56410SVille Syrjälä 	if (hotplug_trigger)
184540e56410SVille Syrjälä 		ibx_hpd_irq_handler(dev, hotplug_trigger, hpd_ibx);
184640e56410SVille Syrjälä 
1847cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1848cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1849776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1850cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1851cfc33bf7SVille Syrjälä 				 port_name(port));
1852cfc33bf7SVille Syrjälä 	}
1853776ad806SJesse Barnes 
1854ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
1855ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1856ce99c256SDaniel Vetter 
1857776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
1858515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
1859776ad806SJesse Barnes 
1860776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1861776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1862776ad806SJesse Barnes 
1863776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1864776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1865776ad806SJesse Barnes 
1866776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1867776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
1868776ad806SJesse Barnes 
18699db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
1870055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
18719db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
18729db4a9c7SJesse Barnes 					 pipe_name(pipe),
18739db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
1874776ad806SJesse Barnes 
1875776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1876776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1877776ad806SJesse Barnes 
1878776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1879776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1880776ad806SJesse Barnes 
1881776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
18821f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
18838664281bSPaulo Zanoni 
18848664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
18851f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
18868664281bSPaulo Zanoni }
18878664281bSPaulo Zanoni 
18888664281bSPaulo Zanoni static void ivb_err_int_handler(struct drm_device *dev)
18898664281bSPaulo Zanoni {
18908664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
18918664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
18925a69b89fSDaniel Vetter 	enum pipe pipe;
18938664281bSPaulo Zanoni 
1894de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
1895de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1896de032bf4SPaulo Zanoni 
1897055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
18981f7247c0SDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe))
18991f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
19008664281bSPaulo Zanoni 
19015a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
19025a69b89fSDaniel Vetter 			if (IS_IVYBRIDGE(dev))
1903277de95eSDaniel Vetter 				ivb_pipe_crc_irq_handler(dev, pipe);
19045a69b89fSDaniel Vetter 			else
1905277de95eSDaniel Vetter 				hsw_pipe_crc_irq_handler(dev, pipe);
19065a69b89fSDaniel Vetter 		}
19075a69b89fSDaniel Vetter 	}
19088bf1e9f1SShuang He 
19098664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
19108664281bSPaulo Zanoni }
19118664281bSPaulo Zanoni 
19128664281bSPaulo Zanoni static void cpt_serr_int_handler(struct drm_device *dev)
19138664281bSPaulo Zanoni {
19148664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
19158664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
19168664281bSPaulo Zanoni 
1917de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
1918de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
1919de032bf4SPaulo Zanoni 
19208664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
19211f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
19228664281bSPaulo Zanoni 
19238664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
19241f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
19258664281bSPaulo Zanoni 
19268664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
19271f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_C);
19288664281bSPaulo Zanoni 
19298664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
1930776ad806SJesse Barnes }
1931776ad806SJesse Barnes 
193223e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
193323e81d69SAdam Jackson {
19342d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
193523e81d69SAdam Jackson 	int pipe;
19366dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
1937aaf5ec2eSSonika Jindal 
193840e56410SVille Syrjälä 	if (hotplug_trigger)
193940e56410SVille Syrjälä 		ibx_hpd_irq_handler(dev, hotplug_trigger, hpd_cpt);
194091d131d2SDaniel Vetter 
1941cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
1942cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
194323e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
1944cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
1945cfc33bf7SVille Syrjälä 				 port_name(port));
1946cfc33bf7SVille Syrjälä 	}
194723e81d69SAdam Jackson 
194823e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
1949ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
195023e81d69SAdam Jackson 
195123e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
1952515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
195323e81d69SAdam Jackson 
195423e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
195523e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
195623e81d69SAdam Jackson 
195723e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
195823e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
195923e81d69SAdam Jackson 
196023e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
1961055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
196223e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
196323e81d69SAdam Jackson 					 pipe_name(pipe),
196423e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
19658664281bSPaulo Zanoni 
19668664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
19678664281bSPaulo Zanoni 		cpt_serr_int_handler(dev);
196823e81d69SAdam Jackson }
196923e81d69SAdam Jackson 
19706dbf30ceSVille Syrjälä static void spt_irq_handler(struct drm_device *dev, u32 pch_iir)
19716dbf30ceSVille Syrjälä {
19726dbf30ceSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
19736dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_SPT &
19746dbf30ceSVille Syrjälä 		~SDE_PORTE_HOTPLUG_SPT;
19756dbf30ceSVille Syrjälä 	u32 hotplug2_trigger = pch_iir & SDE_PORTE_HOTPLUG_SPT;
19766dbf30ceSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
19776dbf30ceSVille Syrjälä 
19786dbf30ceSVille Syrjälä 	if (hotplug_trigger) {
19796dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
19806dbf30ceSVille Syrjälä 
19816dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
19826dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
19836dbf30ceSVille Syrjälä 
19846dbf30ceSVille Syrjälä 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
19856dbf30ceSVille Syrjälä 				   dig_hotplug_reg, hpd_spt,
198674c0b395SVille Syrjälä 				   spt_port_hotplug_long_detect);
19876dbf30ceSVille Syrjälä 	}
19886dbf30ceSVille Syrjälä 
19896dbf30ceSVille Syrjälä 	if (hotplug2_trigger) {
19906dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
19916dbf30ceSVille Syrjälä 
19926dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG2);
19936dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG2, dig_hotplug_reg);
19946dbf30ceSVille Syrjälä 
19956dbf30ceSVille Syrjälä 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug2_trigger,
19966dbf30ceSVille Syrjälä 				   dig_hotplug_reg, hpd_spt,
19976dbf30ceSVille Syrjälä 				   spt_port_hotplug2_long_detect);
19986dbf30ceSVille Syrjälä 	}
19996dbf30ceSVille Syrjälä 
20006dbf30ceSVille Syrjälä 	if (pin_mask)
20016dbf30ceSVille Syrjälä 		intel_hpd_irq_handler(dev, pin_mask, long_mask);
20026dbf30ceSVille Syrjälä 
20036dbf30ceSVille Syrjälä 	if (pch_iir & SDE_GMBUS_CPT)
20046dbf30ceSVille Syrjälä 		gmbus_irq_handler(dev);
20056dbf30ceSVille Syrjälä }
20066dbf30ceSVille Syrjälä 
200740e56410SVille Syrjälä static void ilk_hpd_irq_handler(struct drm_device *dev, u32 hotplug_trigger,
200840e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2009c008bc6eSPaulo Zanoni {
201040e56410SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(dev);
2011e4ce95aaSVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2012e4ce95aaSVille Syrjälä 
2013e4ce95aaSVille Syrjälä 	dig_hotplug_reg = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
2014e4ce95aaSVille Syrjälä 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, dig_hotplug_reg);
2015e4ce95aaSVille Syrjälä 
2016e4ce95aaSVille Syrjälä 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
201740e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2018e4ce95aaSVille Syrjälä 			   ilk_port_hotplug_long_detect);
201940e56410SVille Syrjälä 
2020e4ce95aaSVille Syrjälä 	intel_hpd_irq_handler(dev, pin_mask, long_mask);
2021e4ce95aaSVille Syrjälä }
2022c008bc6eSPaulo Zanoni 
202340e56410SVille Syrjälä static void ilk_display_irq_handler(struct drm_device *dev, u32 de_iir)
202440e56410SVille Syrjälä {
202540e56410SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
202640e56410SVille Syrjälä 	enum pipe pipe;
202740e56410SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG;
202840e56410SVille Syrjälä 
202940e56410SVille Syrjälä 	if (hotplug_trigger)
203040e56410SVille Syrjälä 		ilk_hpd_irq_handler(dev, hotplug_trigger, hpd_ilk);
203140e56410SVille Syrjälä 
2032c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
2033c008bc6eSPaulo Zanoni 		dp_aux_irq_handler(dev);
2034c008bc6eSPaulo Zanoni 
2035c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
2036c008bc6eSPaulo Zanoni 		intel_opregion_asle_intr(dev);
2037c008bc6eSPaulo Zanoni 
2038c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
2039c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
2040c008bc6eSPaulo Zanoni 
2041055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2042d6bbafa1SChris Wilson 		if (de_iir & DE_PIPE_VBLANK(pipe) &&
2043d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
2044d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
2045c008bc6eSPaulo Zanoni 
204640da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
20471f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
2048c008bc6eSPaulo Zanoni 
204940da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
205040da17c2SDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
20515b3a856bSDaniel Vetter 
205240da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
205340da17c2SDaniel Vetter 		if (de_iir & DE_PLANE_FLIP_DONE(pipe)) {
205440da17c2SDaniel Vetter 			intel_prepare_page_flip(dev, pipe);
205540da17c2SDaniel Vetter 			intel_finish_page_flip_plane(dev, pipe);
2056c008bc6eSPaulo Zanoni 		}
2057c008bc6eSPaulo Zanoni 	}
2058c008bc6eSPaulo Zanoni 
2059c008bc6eSPaulo Zanoni 	/* check event from PCH */
2060c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
2061c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
2062c008bc6eSPaulo Zanoni 
2063c008bc6eSPaulo Zanoni 		if (HAS_PCH_CPT(dev))
2064c008bc6eSPaulo Zanoni 			cpt_irq_handler(dev, pch_iir);
2065c008bc6eSPaulo Zanoni 		else
2066c008bc6eSPaulo Zanoni 			ibx_irq_handler(dev, pch_iir);
2067c008bc6eSPaulo Zanoni 
2068c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
2069c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
2070c008bc6eSPaulo Zanoni 	}
2071c008bc6eSPaulo Zanoni 
2072c008bc6eSPaulo Zanoni 	if (IS_GEN5(dev) && de_iir & DE_PCU_EVENT)
2073c008bc6eSPaulo Zanoni 		ironlake_rps_change_irq_handler(dev);
2074c008bc6eSPaulo Zanoni }
2075c008bc6eSPaulo Zanoni 
20769719fb98SPaulo Zanoni static void ivb_display_irq_handler(struct drm_device *dev, u32 de_iir)
20779719fb98SPaulo Zanoni {
20789719fb98SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
207907d27e20SDamien Lespiau 	enum pipe pipe;
208023bb4cb5SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG_IVB;
208123bb4cb5SVille Syrjälä 
208240e56410SVille Syrjälä 	if (hotplug_trigger)
208340e56410SVille Syrjälä 		ilk_hpd_irq_handler(dev, hotplug_trigger, hpd_ivb);
20849719fb98SPaulo Zanoni 
20859719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
20869719fb98SPaulo Zanoni 		ivb_err_int_handler(dev);
20879719fb98SPaulo Zanoni 
20889719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
20899719fb98SPaulo Zanoni 		dp_aux_irq_handler(dev);
20909719fb98SPaulo Zanoni 
20919719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
20929719fb98SPaulo Zanoni 		intel_opregion_asle_intr(dev);
20939719fb98SPaulo Zanoni 
2094055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2095d6bbafa1SChris Wilson 		if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)) &&
2096d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
2097d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
209840da17c2SDaniel Vetter 
209940da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
210007d27e20SDamien Lespiau 		if (de_iir & DE_PLANE_FLIP_DONE_IVB(pipe)) {
210107d27e20SDamien Lespiau 			intel_prepare_page_flip(dev, pipe);
210207d27e20SDamien Lespiau 			intel_finish_page_flip_plane(dev, pipe);
21039719fb98SPaulo Zanoni 		}
21049719fb98SPaulo Zanoni 	}
21059719fb98SPaulo Zanoni 
21069719fb98SPaulo Zanoni 	/* check event from PCH */
21079719fb98SPaulo Zanoni 	if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
21089719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
21099719fb98SPaulo Zanoni 
21109719fb98SPaulo Zanoni 		cpt_irq_handler(dev, pch_iir);
21119719fb98SPaulo Zanoni 
21129719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
21139719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
21149719fb98SPaulo Zanoni 	}
21159719fb98SPaulo Zanoni }
21169719fb98SPaulo Zanoni 
211772c90f62SOscar Mateo /*
211872c90f62SOscar Mateo  * To handle irqs with the minimum potential races with fresh interrupts, we:
211972c90f62SOscar Mateo  * 1 - Disable Master Interrupt Control.
212072c90f62SOscar Mateo  * 2 - Find the source(s) of the interrupt.
212172c90f62SOscar Mateo  * 3 - Clear the Interrupt Identity bits (IIR).
212272c90f62SOscar Mateo  * 4 - Process the interrupt(s) that had bits set in the IIRs.
212372c90f62SOscar Mateo  * 5 - Re-enable Master Interrupt Control.
212472c90f62SOscar Mateo  */
2125f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
2126b1f14ad0SJesse Barnes {
212745a83f84SDaniel Vetter 	struct drm_device *dev = arg;
21282d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2129f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
21300e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
2131b1f14ad0SJesse Barnes 
21322dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
21332dd2a883SImre Deak 		return IRQ_NONE;
21342dd2a883SImre Deak 
21358664281bSPaulo Zanoni 	/* We get interrupts on unclaimed registers, so check for this before we
21368664281bSPaulo Zanoni 	 * do any I915_{READ,WRITE}. */
2137907b28c5SChris Wilson 	intel_uncore_check_errors(dev);
21388664281bSPaulo Zanoni 
2139b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
2140b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
2141b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
214223a78516SPaulo Zanoni 	POSTING_READ(DEIER);
21430e43406bSChris Wilson 
214444498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
214544498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
214644498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
214744498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
214844498aeaSPaulo Zanoni 	 * due to its back queue). */
2149ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
215044498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
215144498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
215244498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2153ab5c608bSBen Widawsky 	}
215444498aeaSPaulo Zanoni 
215572c90f62SOscar Mateo 	/* Find, clear, then process each source of interrupt */
215672c90f62SOscar Mateo 
21570e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
21580e43406bSChris Wilson 	if (gt_iir) {
215972c90f62SOscar Mateo 		I915_WRITE(GTIIR, gt_iir);
216072c90f62SOscar Mateo 		ret = IRQ_HANDLED;
2161d8fc8a47SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 6)
21620e43406bSChris Wilson 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
2163d8fc8a47SPaulo Zanoni 		else
2164d8fc8a47SPaulo Zanoni 			ilk_gt_irq_handler(dev, dev_priv, gt_iir);
21650e43406bSChris Wilson 	}
2166b1f14ad0SJesse Barnes 
2167b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
21680e43406bSChris Wilson 	if (de_iir) {
216972c90f62SOscar Mateo 		I915_WRITE(DEIIR, de_iir);
217072c90f62SOscar Mateo 		ret = IRQ_HANDLED;
2171f1af8fc1SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 7)
21729719fb98SPaulo Zanoni 			ivb_display_irq_handler(dev, de_iir);
2173f1af8fc1SPaulo Zanoni 		else
2174f1af8fc1SPaulo Zanoni 			ilk_display_irq_handler(dev, de_iir);
21750e43406bSChris Wilson 	}
21760e43406bSChris Wilson 
2177f1af8fc1SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6) {
2178f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
21790e43406bSChris Wilson 		if (pm_iir) {
2180b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
21810e43406bSChris Wilson 			ret = IRQ_HANDLED;
218272c90f62SOscar Mateo 			gen6_rps_irq_handler(dev_priv, pm_iir);
21830e43406bSChris Wilson 		}
2184f1af8fc1SPaulo Zanoni 	}
2185b1f14ad0SJesse Barnes 
2186b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
2187b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
2188ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
218944498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
219044498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2191ab5c608bSBen Widawsky 	}
2192b1f14ad0SJesse Barnes 
2193b1f14ad0SJesse Barnes 	return ret;
2194b1f14ad0SJesse Barnes }
2195b1f14ad0SJesse Barnes 
219640e56410SVille Syrjälä static void bxt_hpd_irq_handler(struct drm_device *dev, u32 hotplug_trigger,
219740e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2198d04a492dSShashank Sharma {
2199cebd87a0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(dev);
2200cebd87a0SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2201d04a492dSShashank Sharma 
2202a52bb15bSVille Syrjälä 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
2203a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
2204d04a492dSShashank Sharma 
2205cebd87a0SVille Syrjälä 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
220640e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2207cebd87a0SVille Syrjälä 			   bxt_port_hotplug_long_detect);
220840e56410SVille Syrjälä 
2209475c2e3bSJani Nikula 	intel_hpd_irq_handler(dev, pin_mask, long_mask);
2210d04a492dSShashank Sharma }
2211d04a492dSShashank Sharma 
2212abd58f01SBen Widawsky static irqreturn_t gen8_irq_handler(int irq, void *arg)
2213abd58f01SBen Widawsky {
2214abd58f01SBen Widawsky 	struct drm_device *dev = arg;
2215abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2216abd58f01SBen Widawsky 	u32 master_ctl;
2217abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
2218abd58f01SBen Widawsky 	uint32_t tmp = 0;
2219c42664ccSDaniel Vetter 	enum pipe pipe;
222088e04703SJesse Barnes 	u32 aux_mask = GEN8_AUX_CHANNEL_A;
222188e04703SJesse Barnes 
22222dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
22232dd2a883SImre Deak 		return IRQ_NONE;
22242dd2a883SImre Deak 
2225b4834a50SRodrigo Vivi 	if (INTEL_INFO(dev_priv)->gen >= 9)
222688e04703SJesse Barnes 		aux_mask |=  GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
222788e04703SJesse Barnes 			GEN9_AUX_CHANNEL_D;
2228abd58f01SBen Widawsky 
2229cb0d205eSChris Wilson 	master_ctl = I915_READ_FW(GEN8_MASTER_IRQ);
2230abd58f01SBen Widawsky 	master_ctl &= ~GEN8_MASTER_IRQ_CONTROL;
2231abd58f01SBen Widawsky 	if (!master_ctl)
2232abd58f01SBen Widawsky 		return IRQ_NONE;
2233abd58f01SBen Widawsky 
2234cb0d205eSChris Wilson 	I915_WRITE_FW(GEN8_MASTER_IRQ, 0);
2235abd58f01SBen Widawsky 
223638cc46d7SOscar Mateo 	/* Find, clear, then process each source of interrupt */
223738cc46d7SOscar Mateo 
223874cdb337SChris Wilson 	ret = gen8_gt_irq_handler(dev_priv, master_ctl);
2239abd58f01SBen Widawsky 
2240abd58f01SBen Widawsky 	if (master_ctl & GEN8_DE_MISC_IRQ) {
2241abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_DE_MISC_IIR);
2242abd58f01SBen Widawsky 		if (tmp) {
2243abd58f01SBen Widawsky 			I915_WRITE(GEN8_DE_MISC_IIR, tmp);
2244abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
224538cc46d7SOscar Mateo 			if (tmp & GEN8_DE_MISC_GSE)
224638cc46d7SOscar Mateo 				intel_opregion_asle_intr(dev);
224738cc46d7SOscar Mateo 			else
224838cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Misc interrupt\n");
2249abd58f01SBen Widawsky 		}
225038cc46d7SOscar Mateo 		else
225138cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
2252abd58f01SBen Widawsky 	}
2253abd58f01SBen Widawsky 
22546d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
22556d766f02SDaniel Vetter 		tmp = I915_READ(GEN8_DE_PORT_IIR);
22566d766f02SDaniel Vetter 		if (tmp) {
2257d04a492dSShashank Sharma 			bool found = false;
2258cebd87a0SVille Syrjälä 			u32 hotplug_trigger = 0;
2259cebd87a0SVille Syrjälä 
2260cebd87a0SVille Syrjälä 			if (IS_BROXTON(dev_priv))
2261cebd87a0SVille Syrjälä 				hotplug_trigger = tmp & BXT_DE_PORT_HOTPLUG_MASK;
2262cebd87a0SVille Syrjälä 			else if (IS_BROADWELL(dev_priv))
2263cebd87a0SVille Syrjälä 				hotplug_trigger = tmp & GEN8_PORT_DP_A_HOTPLUG;
2264d04a492dSShashank Sharma 
22656d766f02SDaniel Vetter 			I915_WRITE(GEN8_DE_PORT_IIR, tmp);
22666d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
226788e04703SJesse Barnes 
2268d04a492dSShashank Sharma 			if (tmp & aux_mask) {
226938cc46d7SOscar Mateo 				dp_aux_irq_handler(dev);
2270d04a492dSShashank Sharma 				found = true;
2271d04a492dSShashank Sharma 			}
2272d04a492dSShashank Sharma 
227340e56410SVille Syrjälä 			if (hotplug_trigger) {
227440e56410SVille Syrjälä 				if (IS_BROXTON(dev))
227540e56410SVille Syrjälä 					bxt_hpd_irq_handler(dev, hotplug_trigger, hpd_bxt);
227640e56410SVille Syrjälä 				else
227740e56410SVille Syrjälä 					ilk_hpd_irq_handler(dev, hotplug_trigger, hpd_bdw);
2278d04a492dSShashank Sharma 				found = true;
2279d04a492dSShashank Sharma 			}
2280d04a492dSShashank Sharma 
22819e63743eSShashank Sharma 			if (IS_BROXTON(dev) && (tmp & BXT_DE_PORT_GMBUS)) {
22829e63743eSShashank Sharma 				gmbus_irq_handler(dev);
22839e63743eSShashank Sharma 				found = true;
22849e63743eSShashank Sharma 			}
22859e63743eSShashank Sharma 
2286d04a492dSShashank Sharma 			if (!found)
228738cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Port interrupt\n");
22886d766f02SDaniel Vetter 		}
228938cc46d7SOscar Mateo 		else
229038cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
22916d766f02SDaniel Vetter 	}
22926d766f02SDaniel Vetter 
2293055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2294770de83dSDamien Lespiau 		uint32_t pipe_iir, flip_done = 0, fault_errors = 0;
2295abd58f01SBen Widawsky 
2296c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2297c42664ccSDaniel Vetter 			continue;
2298c42664ccSDaniel Vetter 
2299abd58f01SBen Widawsky 		pipe_iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
230038cc46d7SOscar Mateo 		if (pipe_iir) {
230138cc46d7SOscar Mateo 			ret = IRQ_HANDLED;
230238cc46d7SOscar Mateo 			I915_WRITE(GEN8_DE_PIPE_IIR(pipe), pipe_iir);
2303770de83dSDamien Lespiau 
2304d6bbafa1SChris Wilson 			if (pipe_iir & GEN8_PIPE_VBLANK &&
2305d6bbafa1SChris Wilson 			    intel_pipe_handle_vblank(dev, pipe))
2306d6bbafa1SChris Wilson 				intel_check_page_flip(dev, pipe);
2307abd58f01SBen Widawsky 
2308b4834a50SRodrigo Vivi 			if (INTEL_INFO(dev_priv)->gen >= 9)
2309770de83dSDamien Lespiau 				flip_done = pipe_iir & GEN9_PIPE_PLANE1_FLIP_DONE;
2310770de83dSDamien Lespiau 			else
2311770de83dSDamien Lespiau 				flip_done = pipe_iir & GEN8_PIPE_PRIMARY_FLIP_DONE;
2312770de83dSDamien Lespiau 
2313770de83dSDamien Lespiau 			if (flip_done) {
2314abd58f01SBen Widawsky 				intel_prepare_page_flip(dev, pipe);
2315abd58f01SBen Widawsky 				intel_finish_page_flip_plane(dev, pipe);
2316abd58f01SBen Widawsky 			}
2317abd58f01SBen Widawsky 
23180fbe7870SDaniel Vetter 			if (pipe_iir & GEN8_PIPE_CDCLK_CRC_DONE)
23190fbe7870SDaniel Vetter 				hsw_pipe_crc_irq_handler(dev, pipe);
23200fbe7870SDaniel Vetter 
23211f7247c0SDaniel Vetter 			if (pipe_iir & GEN8_PIPE_FIFO_UNDERRUN)
23221f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
23231f7247c0SDaniel Vetter 								    pipe);
232438d83c96SDaniel Vetter 
2325770de83dSDamien Lespiau 
2326b4834a50SRodrigo Vivi 			if (INTEL_INFO(dev_priv)->gen >= 9)
2327770de83dSDamien Lespiau 				fault_errors = pipe_iir & GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
2328770de83dSDamien Lespiau 			else
2329770de83dSDamien Lespiau 				fault_errors = pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
2330770de83dSDamien Lespiau 
2331770de83dSDamien Lespiau 			if (fault_errors)
233230100f2bSDaniel Vetter 				DRM_ERROR("Fault errors on pipe %c\n: 0x%08x",
233330100f2bSDaniel Vetter 					  pipe_name(pipe),
233430100f2bSDaniel Vetter 					  pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS);
2335c42664ccSDaniel Vetter 		} else
2336abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
2337abd58f01SBen Widawsky 	}
2338abd58f01SBen Widawsky 
2339266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev) && !HAS_PCH_NOP(dev) &&
2340266ea3d9SShashank Sharma 	    master_ctl & GEN8_DE_PCH_IRQ) {
234192d03a80SDaniel Vetter 		/*
234292d03a80SDaniel Vetter 		 * FIXME(BDW): Assume for now that the new interrupt handling
234392d03a80SDaniel Vetter 		 * scheme also closed the SDE interrupt handling race we've seen
234492d03a80SDaniel Vetter 		 * on older pch-split platforms. But this needs testing.
234592d03a80SDaniel Vetter 		 */
234692d03a80SDaniel Vetter 		u32 pch_iir = I915_READ(SDEIIR);
234792d03a80SDaniel Vetter 		if (pch_iir) {
234892d03a80SDaniel Vetter 			I915_WRITE(SDEIIR, pch_iir);
234992d03a80SDaniel Vetter 			ret = IRQ_HANDLED;
23506dbf30ceSVille Syrjälä 
23516dbf30ceSVille Syrjälä 			if (HAS_PCH_SPT(dev_priv))
23526dbf30ceSVille Syrjälä 				spt_irq_handler(dev, pch_iir);
23536dbf30ceSVille Syrjälä 			else
235438cc46d7SOscar Mateo 				cpt_irq_handler(dev, pch_iir);
235538cc46d7SOscar Mateo 		} else
235638cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (SDE)!\n");
235738cc46d7SOscar Mateo 
235892d03a80SDaniel Vetter 	}
235992d03a80SDaniel Vetter 
2360cb0d205eSChris Wilson 	I915_WRITE_FW(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
2361cb0d205eSChris Wilson 	POSTING_READ_FW(GEN8_MASTER_IRQ);
2362abd58f01SBen Widawsky 
2363abd58f01SBen Widawsky 	return ret;
2364abd58f01SBen Widawsky }
2365abd58f01SBen Widawsky 
236617e1df07SDaniel Vetter static void i915_error_wake_up(struct drm_i915_private *dev_priv,
236717e1df07SDaniel Vetter 			       bool reset_completed)
236817e1df07SDaniel Vetter {
2369a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
237017e1df07SDaniel Vetter 	int i;
237117e1df07SDaniel Vetter 
237217e1df07SDaniel Vetter 	/*
237317e1df07SDaniel Vetter 	 * Notify all waiters for GPU completion events that reset state has
237417e1df07SDaniel Vetter 	 * been changed, and that they need to restart their wait after
237517e1df07SDaniel Vetter 	 * checking for potential errors (and bail out to drop locks if there is
237617e1df07SDaniel Vetter 	 * a gpu reset pending so that i915_error_work_func can acquire them).
237717e1df07SDaniel Vetter 	 */
237817e1df07SDaniel Vetter 
237917e1df07SDaniel Vetter 	/* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
238017e1df07SDaniel Vetter 	for_each_ring(ring, dev_priv, i)
238117e1df07SDaniel Vetter 		wake_up_all(&ring->irq_queue);
238217e1df07SDaniel Vetter 
238317e1df07SDaniel Vetter 	/* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
238417e1df07SDaniel Vetter 	wake_up_all(&dev_priv->pending_flip_queue);
238517e1df07SDaniel Vetter 
238617e1df07SDaniel Vetter 	/*
238717e1df07SDaniel Vetter 	 * Signal tasks blocked in i915_gem_wait_for_error that the pending
238817e1df07SDaniel Vetter 	 * reset state is cleared.
238917e1df07SDaniel Vetter 	 */
239017e1df07SDaniel Vetter 	if (reset_completed)
239117e1df07SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
239217e1df07SDaniel Vetter }
239317e1df07SDaniel Vetter 
23948a905236SJesse Barnes /**
2395b8d24a06SMika Kuoppala  * i915_reset_and_wakeup - do process context error handling work
2396468f9d29SJavier Martinez Canillas  * @dev: drm device
23978a905236SJesse Barnes  *
23988a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
23998a905236SJesse Barnes  * was detected.
24008a905236SJesse Barnes  */
2401b8d24a06SMika Kuoppala static void i915_reset_and_wakeup(struct drm_device *dev)
24028a905236SJesse Barnes {
2403b8d24a06SMika Kuoppala 	struct drm_i915_private *dev_priv = to_i915(dev);
2404b8d24a06SMika Kuoppala 	struct i915_gpu_error *error = &dev_priv->gpu_error;
2405cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
2406cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
2407cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
240817e1df07SDaniel Vetter 	int ret;
24098a905236SJesse Barnes 
24105bdebb18SDave Airlie 	kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, error_event);
24118a905236SJesse Barnes 
24127db0ba24SDaniel Vetter 	/*
24137db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
24147db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
24157db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
24167db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
24177db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
24187db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
24197db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
24207db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
24217db0ba24SDaniel Vetter 	 */
24227db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
242344d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
24245bdebb18SDave Airlie 		kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE,
24257db0ba24SDaniel Vetter 				   reset_event);
24261f83fee0SDaniel Vetter 
242717e1df07SDaniel Vetter 		/*
2428f454c694SImre Deak 		 * In most cases it's guaranteed that we get here with an RPM
2429f454c694SImre Deak 		 * reference held, for example because there is a pending GPU
2430f454c694SImre Deak 		 * request that won't finish until the reset is done. This
2431f454c694SImre Deak 		 * isn't the case at least when we get here by doing a
2432f454c694SImre Deak 		 * simulated reset via debugs, so get an RPM reference.
2433f454c694SImre Deak 		 */
2434f454c694SImre Deak 		intel_runtime_pm_get(dev_priv);
24357514747dSVille Syrjälä 
24367514747dSVille Syrjälä 		intel_prepare_reset(dev);
24377514747dSVille Syrjälä 
2438f454c694SImre Deak 		/*
243917e1df07SDaniel Vetter 		 * All state reset _must_ be completed before we update the
244017e1df07SDaniel Vetter 		 * reset counter, for otherwise waiters might miss the reset
244117e1df07SDaniel Vetter 		 * pending state and not properly drop locks, resulting in
244217e1df07SDaniel Vetter 		 * deadlocks with the reset work.
244317e1df07SDaniel Vetter 		 */
2444f69061beSDaniel Vetter 		ret = i915_reset(dev);
2445f69061beSDaniel Vetter 
24467514747dSVille Syrjälä 		intel_finish_reset(dev);
244717e1df07SDaniel Vetter 
2448f454c694SImre Deak 		intel_runtime_pm_put(dev_priv);
2449f454c694SImre Deak 
2450f69061beSDaniel Vetter 		if (ret == 0) {
2451f69061beSDaniel Vetter 			/*
2452f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
2453f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
2454f69061beSDaniel Vetter 			 * complete.
2455f69061beSDaniel Vetter 			 *
2456f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
2457f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
2458f69061beSDaniel Vetter 			 * updates before
2459f69061beSDaniel Vetter 			 * the counter increment.
2460f69061beSDaniel Vetter 			 */
24614e857c58SPeter Zijlstra 			smp_mb__before_atomic();
2462f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
2463f69061beSDaniel Vetter 
24645bdebb18SDave Airlie 			kobject_uevent_env(&dev->primary->kdev->kobj,
2465f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
24661f83fee0SDaniel Vetter 		} else {
2467805de8f4SPeter Zijlstra 			atomic_or(I915_WEDGED, &error->reset_counter);
2468f316a42cSBen Gamari 		}
24691f83fee0SDaniel Vetter 
247017e1df07SDaniel Vetter 		/*
247117e1df07SDaniel Vetter 		 * Note: The wake_up also serves as a memory barrier so that
247217e1df07SDaniel Vetter 		 * waiters see the update value of the reset counter atomic_t.
247317e1df07SDaniel Vetter 		 */
247417e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, true);
2475f316a42cSBen Gamari 	}
24768a905236SJesse Barnes }
24778a905236SJesse Barnes 
247835aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
2479c0e09200SDave Airlie {
24808a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
2481bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
248263eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
2483050ee91fSBen Widawsky 	int pipe, i;
248463eeaf38SJesse Barnes 
248535aed2e6SChris Wilson 	if (!eir)
248635aed2e6SChris Wilson 		return;
248763eeaf38SJesse Barnes 
2488a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
24898a905236SJesse Barnes 
2490bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
2491bd9854f9SBen Widawsky 
24928a905236SJesse Barnes 	if (IS_G4X(dev)) {
24938a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
24948a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
24958a905236SJesse Barnes 
2496a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2497a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2498050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
2499050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2500a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2501a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
25028a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
25033143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
25048a905236SJesse Barnes 		}
25058a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
25068a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2507a70491ccSJoe Perches 			pr_err("page table error\n");
2508a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
25098a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
25103143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
25118a905236SJesse Barnes 		}
25128a905236SJesse Barnes 	}
25138a905236SJesse Barnes 
2514a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
251563eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
251663eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2517a70491ccSJoe Perches 			pr_err("page table error\n");
2518a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
251963eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
25203143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
252163eeaf38SJesse Barnes 		}
25228a905236SJesse Barnes 	}
25238a905236SJesse Barnes 
252463eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
2525a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
2526055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
2527a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
25289db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
252963eeaf38SJesse Barnes 		/* pipestat has already been acked */
253063eeaf38SJesse Barnes 	}
253163eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
2532a70491ccSJoe Perches 		pr_err("instruction error\n");
2533a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
2534050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
2535050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2536a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
253763eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
253863eeaf38SJesse Barnes 
2539a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
2540a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
2541a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
254263eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
25433143a2bfSChris Wilson 			POSTING_READ(IPEIR);
254463eeaf38SJesse Barnes 		} else {
254563eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
254663eeaf38SJesse Barnes 
2547a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2548a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2549a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2550a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
255163eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
25523143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
255363eeaf38SJesse Barnes 		}
255463eeaf38SJesse Barnes 	}
255563eeaf38SJesse Barnes 
255663eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
25573143a2bfSChris Wilson 	POSTING_READ(EIR);
255863eeaf38SJesse Barnes 	eir = I915_READ(EIR);
255963eeaf38SJesse Barnes 	if (eir) {
256063eeaf38SJesse Barnes 		/*
256163eeaf38SJesse Barnes 		 * some errors might have become stuck,
256263eeaf38SJesse Barnes 		 * mask them.
256363eeaf38SJesse Barnes 		 */
256463eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
256563eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
256663eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
256763eeaf38SJesse Barnes 	}
256835aed2e6SChris Wilson }
256935aed2e6SChris Wilson 
257035aed2e6SChris Wilson /**
2571b8d24a06SMika Kuoppala  * i915_handle_error - handle a gpu error
257235aed2e6SChris Wilson  * @dev: drm device
257335aed2e6SChris Wilson  *
2574*aafd8581SJavier Martinez Canillas  * Do some basic checking of register state at error time and
257535aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
257635aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
257735aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
257835aed2e6SChris Wilson  * of a ring dump etc.).
257935aed2e6SChris Wilson  */
258058174462SMika Kuoppala void i915_handle_error(struct drm_device *dev, bool wedged,
258158174462SMika Kuoppala 		       const char *fmt, ...)
258235aed2e6SChris Wilson {
258335aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
258458174462SMika Kuoppala 	va_list args;
258558174462SMika Kuoppala 	char error_msg[80];
258635aed2e6SChris Wilson 
258758174462SMika Kuoppala 	va_start(args, fmt);
258858174462SMika Kuoppala 	vscnprintf(error_msg, sizeof(error_msg), fmt, args);
258958174462SMika Kuoppala 	va_end(args);
259058174462SMika Kuoppala 
259158174462SMika Kuoppala 	i915_capture_error_state(dev, wedged, error_msg);
259235aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
25938a905236SJesse Barnes 
2594ba1234d1SBen Gamari 	if (wedged) {
2595805de8f4SPeter Zijlstra 		atomic_or(I915_RESET_IN_PROGRESS_FLAG,
2596f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
2597ba1234d1SBen Gamari 
259811ed50ecSBen Gamari 		/*
2599b8d24a06SMika Kuoppala 		 * Wakeup waiting processes so that the reset function
2600b8d24a06SMika Kuoppala 		 * i915_reset_and_wakeup doesn't deadlock trying to grab
2601b8d24a06SMika Kuoppala 		 * various locks. By bumping the reset counter first, the woken
260217e1df07SDaniel Vetter 		 * processes will see a reset in progress and back off,
260317e1df07SDaniel Vetter 		 * releasing their locks and then wait for the reset completion.
260417e1df07SDaniel Vetter 		 * We must do this for _all_ gpu waiters that might hold locks
260517e1df07SDaniel Vetter 		 * that the reset work needs to acquire.
260617e1df07SDaniel Vetter 		 *
260717e1df07SDaniel Vetter 		 * Note: The wake_up serves as the required memory barrier to
260817e1df07SDaniel Vetter 		 * ensure that the waiters see the updated value of the reset
260917e1df07SDaniel Vetter 		 * counter atomic_t.
261011ed50ecSBen Gamari 		 */
261117e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, false);
261211ed50ecSBen Gamari 	}
261311ed50ecSBen Gamari 
2614b8d24a06SMika Kuoppala 	i915_reset_and_wakeup(dev);
26158a905236SJesse Barnes }
26168a905236SJesse Barnes 
261742f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
261842f52ef8SKeith Packard  * we use as a pipe index
261942f52ef8SKeith Packard  */
2620f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
26210a3e67a4SJesse Barnes {
26222d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2623e9d21d7fSKeith Packard 	unsigned long irqflags;
262471e0ffa5SJesse Barnes 
26251ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2626f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
26277c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2628755e9019SImre Deak 				     PIPE_START_VBLANK_INTERRUPT_STATUS);
26290a3e67a4SJesse Barnes 	else
26307c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2631755e9019SImre Deak 				     PIPE_VBLANK_INTERRUPT_STATUS);
26321ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26338692d00eSChris Wilson 
26340a3e67a4SJesse Barnes 	return 0;
26350a3e67a4SJesse Barnes }
26360a3e67a4SJesse Barnes 
2637f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
2638f796cf8fSJesse Barnes {
26392d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2640f796cf8fSJesse Barnes 	unsigned long irqflags;
2641b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
264240da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2643f796cf8fSJesse Barnes 
2644f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2645b518421fSPaulo Zanoni 	ironlake_enable_display_irq(dev_priv, bit);
2646b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2647b1f14ad0SJesse Barnes 
2648b1f14ad0SJesse Barnes 	return 0;
2649b1f14ad0SJesse Barnes }
2650b1f14ad0SJesse Barnes 
26517e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
26527e231dbeSJesse Barnes {
26532d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
26547e231dbeSJesse Barnes 	unsigned long irqflags;
26557e231dbeSJesse Barnes 
26567e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
265731acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
2658755e9019SImre Deak 			     PIPE_START_VBLANK_INTERRUPT_STATUS);
26597e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26607e231dbeSJesse Barnes 
26617e231dbeSJesse Barnes 	return 0;
26627e231dbeSJesse Barnes }
26637e231dbeSJesse Barnes 
2664abd58f01SBen Widawsky static int gen8_enable_vblank(struct drm_device *dev, int pipe)
2665abd58f01SBen Widawsky {
2666abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2667abd58f01SBen Widawsky 	unsigned long irqflags;
2668abd58f01SBen Widawsky 
2669abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
26707167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] &= ~GEN8_PIPE_VBLANK;
26717167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2672abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2673abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2674abd58f01SBen Widawsky 	return 0;
2675abd58f01SBen Widawsky }
2676abd58f01SBen Widawsky 
267742f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
267842f52ef8SKeith Packard  * we use as a pipe index
267942f52ef8SKeith Packard  */
2680f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
26810a3e67a4SJesse Barnes {
26822d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2683e9d21d7fSKeith Packard 	unsigned long irqflags;
26840a3e67a4SJesse Barnes 
26851ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
26867c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
2687755e9019SImre Deak 			      PIPE_VBLANK_INTERRUPT_STATUS |
2688755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
26891ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26900a3e67a4SJesse Barnes }
26910a3e67a4SJesse Barnes 
2692f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
2693f796cf8fSJesse Barnes {
26942d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2695f796cf8fSJesse Barnes 	unsigned long irqflags;
2696b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
269740da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2698f796cf8fSJesse Barnes 
2699f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2700b518421fSPaulo Zanoni 	ironlake_disable_display_irq(dev_priv, bit);
2701b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2702b1f14ad0SJesse Barnes }
2703b1f14ad0SJesse Barnes 
27047e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
27057e231dbeSJesse Barnes {
27062d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
27077e231dbeSJesse Barnes 	unsigned long irqflags;
27087e231dbeSJesse Barnes 
27097e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
271031acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
2711755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
27127e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
27137e231dbeSJesse Barnes }
27147e231dbeSJesse Barnes 
2715abd58f01SBen Widawsky static void gen8_disable_vblank(struct drm_device *dev, int pipe)
2716abd58f01SBen Widawsky {
2717abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2718abd58f01SBen Widawsky 	unsigned long irqflags;
2719abd58f01SBen Widawsky 
2720abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
27217167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] |= GEN8_PIPE_VBLANK;
27227167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2723abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2724abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2725abd58f01SBen Widawsky }
2726abd58f01SBen Widawsky 
27279107e9d2SChris Wilson static bool
272894f7bbe1STomas Elf ring_idle(struct intel_engine_cs *ring, u32 seqno)
2729893eead0SChris Wilson {
27309107e9d2SChris Wilson 	return (list_empty(&ring->request_list) ||
273194f7bbe1STomas Elf 		i915_seqno_passed(seqno, ring->last_submitted_seqno));
2732f65d9421SBen Gamari }
2733f65d9421SBen Gamari 
2734a028c4b0SDaniel Vetter static bool
2735a028c4b0SDaniel Vetter ipehr_is_semaphore_wait(struct drm_device *dev, u32 ipehr)
2736a028c4b0SDaniel Vetter {
2737a028c4b0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 8) {
2738a6cdb93aSRodrigo Vivi 		return (ipehr >> 23) == 0x1c;
2739a028c4b0SDaniel Vetter 	} else {
2740a028c4b0SDaniel Vetter 		ipehr &= ~MI_SEMAPHORE_SYNC_MASK;
2741a028c4b0SDaniel Vetter 		return ipehr == (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE |
2742a028c4b0SDaniel Vetter 				 MI_SEMAPHORE_REGISTER);
2743a028c4b0SDaniel Vetter 	}
2744a028c4b0SDaniel Vetter }
2745a028c4b0SDaniel Vetter 
2746a4872ba6SOscar Mateo static struct intel_engine_cs *
2747a6cdb93aSRodrigo Vivi semaphore_wait_to_signaller_ring(struct intel_engine_cs *ring, u32 ipehr, u64 offset)
2748921d42eaSDaniel Vetter {
2749921d42eaSDaniel Vetter 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2750a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2751921d42eaSDaniel Vetter 	int i;
2752921d42eaSDaniel Vetter 
2753921d42eaSDaniel Vetter 	if (INTEL_INFO(dev_priv->dev)->gen >= 8) {
2754a6cdb93aSRodrigo Vivi 		for_each_ring(signaller, dev_priv, i) {
2755a6cdb93aSRodrigo Vivi 			if (ring == signaller)
2756a6cdb93aSRodrigo Vivi 				continue;
2757a6cdb93aSRodrigo Vivi 
2758a6cdb93aSRodrigo Vivi 			if (offset == signaller->semaphore.signal_ggtt[ring->id])
2759a6cdb93aSRodrigo Vivi 				return signaller;
2760a6cdb93aSRodrigo Vivi 		}
2761921d42eaSDaniel Vetter 	} else {
2762921d42eaSDaniel Vetter 		u32 sync_bits = ipehr & MI_SEMAPHORE_SYNC_MASK;
2763921d42eaSDaniel Vetter 
2764921d42eaSDaniel Vetter 		for_each_ring(signaller, dev_priv, i) {
2765921d42eaSDaniel Vetter 			if(ring == signaller)
2766921d42eaSDaniel Vetter 				continue;
2767921d42eaSDaniel Vetter 
2768ebc348b2SBen Widawsky 			if (sync_bits == signaller->semaphore.mbox.wait[ring->id])
2769921d42eaSDaniel Vetter 				return signaller;
2770921d42eaSDaniel Vetter 		}
2771921d42eaSDaniel Vetter 	}
2772921d42eaSDaniel Vetter 
2773a6cdb93aSRodrigo Vivi 	DRM_ERROR("No signaller ring found for ring %i, ipehr 0x%08x, offset 0x%016llx\n",
2774a6cdb93aSRodrigo Vivi 		  ring->id, ipehr, offset);
2775921d42eaSDaniel Vetter 
2776921d42eaSDaniel Vetter 	return NULL;
2777921d42eaSDaniel Vetter }
2778921d42eaSDaniel Vetter 
2779a4872ba6SOscar Mateo static struct intel_engine_cs *
2780a4872ba6SOscar Mateo semaphore_waits_for(struct intel_engine_cs *ring, u32 *seqno)
2781a24a11e6SChris Wilson {
2782a24a11e6SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
278388fe429dSDaniel Vetter 	u32 cmd, ipehr, head;
2784a6cdb93aSRodrigo Vivi 	u64 offset = 0;
2785a6cdb93aSRodrigo Vivi 	int i, backwards;
2786a24a11e6SChris Wilson 
2787a24a11e6SChris Wilson 	ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
2788a028c4b0SDaniel Vetter 	if (!ipehr_is_semaphore_wait(ring->dev, ipehr))
27896274f212SChris Wilson 		return NULL;
2790a24a11e6SChris Wilson 
279188fe429dSDaniel Vetter 	/*
279288fe429dSDaniel Vetter 	 * HEAD is likely pointing to the dword after the actual command,
279388fe429dSDaniel Vetter 	 * so scan backwards until we find the MBOX. But limit it to just 3
2794a6cdb93aSRodrigo Vivi 	 * or 4 dwords depending on the semaphore wait command size.
2795a6cdb93aSRodrigo Vivi 	 * Note that we don't care about ACTHD here since that might
279688fe429dSDaniel Vetter 	 * point at at batch, and semaphores are always emitted into the
279788fe429dSDaniel Vetter 	 * ringbuffer itself.
2798a24a11e6SChris Wilson 	 */
279988fe429dSDaniel Vetter 	head = I915_READ_HEAD(ring) & HEAD_ADDR;
2800a6cdb93aSRodrigo Vivi 	backwards = (INTEL_INFO(ring->dev)->gen >= 8) ? 5 : 4;
280188fe429dSDaniel Vetter 
2802a6cdb93aSRodrigo Vivi 	for (i = backwards; i; --i) {
280388fe429dSDaniel Vetter 		/*
280488fe429dSDaniel Vetter 		 * Be paranoid and presume the hw has gone off into the wild -
280588fe429dSDaniel Vetter 		 * our ring is smaller than what the hardware (and hence
280688fe429dSDaniel Vetter 		 * HEAD_ADDR) allows. Also handles wrap-around.
280788fe429dSDaniel Vetter 		 */
2808ee1b1e5eSOscar Mateo 		head &= ring->buffer->size - 1;
280988fe429dSDaniel Vetter 
281088fe429dSDaniel Vetter 		/* This here seems to blow up */
2811ee1b1e5eSOscar Mateo 		cmd = ioread32(ring->buffer->virtual_start + head);
2812a24a11e6SChris Wilson 		if (cmd == ipehr)
2813a24a11e6SChris Wilson 			break;
2814a24a11e6SChris Wilson 
281588fe429dSDaniel Vetter 		head -= 4;
281688fe429dSDaniel Vetter 	}
2817a24a11e6SChris Wilson 
281888fe429dSDaniel Vetter 	if (!i)
281988fe429dSDaniel Vetter 		return NULL;
282088fe429dSDaniel Vetter 
2821ee1b1e5eSOscar Mateo 	*seqno = ioread32(ring->buffer->virtual_start + head + 4) + 1;
2822a6cdb93aSRodrigo Vivi 	if (INTEL_INFO(ring->dev)->gen >= 8) {
2823a6cdb93aSRodrigo Vivi 		offset = ioread32(ring->buffer->virtual_start + head + 12);
2824a6cdb93aSRodrigo Vivi 		offset <<= 32;
2825a6cdb93aSRodrigo Vivi 		offset = ioread32(ring->buffer->virtual_start + head + 8);
2826a6cdb93aSRodrigo Vivi 	}
2827a6cdb93aSRodrigo Vivi 	return semaphore_wait_to_signaller_ring(ring, ipehr, offset);
2828a24a11e6SChris Wilson }
2829a24a11e6SChris Wilson 
2830a4872ba6SOscar Mateo static int semaphore_passed(struct intel_engine_cs *ring)
28316274f212SChris Wilson {
28326274f212SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2833a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2834a0d036b0SChris Wilson 	u32 seqno;
28356274f212SChris Wilson 
28364be17381SChris Wilson 	ring->hangcheck.deadlock++;
28376274f212SChris Wilson 
28386274f212SChris Wilson 	signaller = semaphore_waits_for(ring, &seqno);
28394be17381SChris Wilson 	if (signaller == NULL)
28404be17381SChris Wilson 		return -1;
28414be17381SChris Wilson 
28424be17381SChris Wilson 	/* Prevent pathological recursion due to driver bugs */
28434be17381SChris Wilson 	if (signaller->hangcheck.deadlock >= I915_NUM_RINGS)
28446274f212SChris Wilson 		return -1;
28456274f212SChris Wilson 
28464be17381SChris Wilson 	if (i915_seqno_passed(signaller->get_seqno(signaller, false), seqno))
28474be17381SChris Wilson 		return 1;
28484be17381SChris Wilson 
2849a0d036b0SChris Wilson 	/* cursory check for an unkickable deadlock */
2850a0d036b0SChris Wilson 	if (I915_READ_CTL(signaller) & RING_WAIT_SEMAPHORE &&
2851a0d036b0SChris Wilson 	    semaphore_passed(signaller) < 0)
28524be17381SChris Wilson 		return -1;
28534be17381SChris Wilson 
28544be17381SChris Wilson 	return 0;
28556274f212SChris Wilson }
28566274f212SChris Wilson 
28576274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
28586274f212SChris Wilson {
2859a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
28606274f212SChris Wilson 	int i;
28616274f212SChris Wilson 
28626274f212SChris Wilson 	for_each_ring(ring, dev_priv, i)
28634be17381SChris Wilson 		ring->hangcheck.deadlock = 0;
28646274f212SChris Wilson }
28656274f212SChris Wilson 
2866ad8beaeaSMika Kuoppala static enum intel_ring_hangcheck_action
2867a4872ba6SOscar Mateo ring_stuck(struct intel_engine_cs *ring, u64 acthd)
28681ec14ad3SChris Wilson {
28691ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
28701ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
28719107e9d2SChris Wilson 	u32 tmp;
28729107e9d2SChris Wilson 
2873f260fe7bSMika Kuoppala 	if (acthd != ring->hangcheck.acthd) {
2874f260fe7bSMika Kuoppala 		if (acthd > ring->hangcheck.max_acthd) {
2875f260fe7bSMika Kuoppala 			ring->hangcheck.max_acthd = acthd;
2876f2f4d82fSJani Nikula 			return HANGCHECK_ACTIVE;
2877f260fe7bSMika Kuoppala 		}
2878f260fe7bSMika Kuoppala 
2879f260fe7bSMika Kuoppala 		return HANGCHECK_ACTIVE_LOOP;
2880f260fe7bSMika Kuoppala 	}
28816274f212SChris Wilson 
28829107e9d2SChris Wilson 	if (IS_GEN2(dev))
2883f2f4d82fSJani Nikula 		return HANGCHECK_HUNG;
28849107e9d2SChris Wilson 
28859107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
28869107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
28879107e9d2SChris Wilson 	 * and break the hang. This should work on
28889107e9d2SChris Wilson 	 * all but the second generation chipsets.
28899107e9d2SChris Wilson 	 */
28909107e9d2SChris Wilson 	tmp = I915_READ_CTL(ring);
28911ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
289258174462SMika Kuoppala 		i915_handle_error(dev, false,
289358174462SMika Kuoppala 				  "Kicking stuck wait on %s",
28941ec14ad3SChris Wilson 				  ring->name);
28951ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
2896f2f4d82fSJani Nikula 		return HANGCHECK_KICK;
28971ec14ad3SChris Wilson 	}
2898a24a11e6SChris Wilson 
28996274f212SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
29006274f212SChris Wilson 		switch (semaphore_passed(ring)) {
29016274f212SChris Wilson 		default:
2902f2f4d82fSJani Nikula 			return HANGCHECK_HUNG;
29036274f212SChris Wilson 		case 1:
290458174462SMika Kuoppala 			i915_handle_error(dev, false,
290558174462SMika Kuoppala 					  "Kicking stuck semaphore on %s",
2906a24a11e6SChris Wilson 					  ring->name);
2907a24a11e6SChris Wilson 			I915_WRITE_CTL(ring, tmp);
2908f2f4d82fSJani Nikula 			return HANGCHECK_KICK;
29096274f212SChris Wilson 		case 0:
2910f2f4d82fSJani Nikula 			return HANGCHECK_WAIT;
29116274f212SChris Wilson 		}
29129107e9d2SChris Wilson 	}
29139107e9d2SChris Wilson 
2914f2f4d82fSJani Nikula 	return HANGCHECK_HUNG;
2915a24a11e6SChris Wilson }
2916d1e61e7fSChris Wilson 
2917737b1506SChris Wilson /*
2918f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
291905407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
292005407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
292105407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
292205407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
292305407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
2924f65d9421SBen Gamari  */
2925737b1506SChris Wilson static void i915_hangcheck_elapsed(struct work_struct *work)
2926f65d9421SBen Gamari {
2927737b1506SChris Wilson 	struct drm_i915_private *dev_priv =
2928737b1506SChris Wilson 		container_of(work, typeof(*dev_priv),
2929737b1506SChris Wilson 			     gpu_error.hangcheck_work.work);
2930737b1506SChris Wilson 	struct drm_device *dev = dev_priv->dev;
2931a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
2932b4519513SChris Wilson 	int i;
293305407ff8SMika Kuoppala 	int busy_count = 0, rings_hung = 0;
29349107e9d2SChris Wilson 	bool stuck[I915_NUM_RINGS] = { 0 };
29359107e9d2SChris Wilson #define BUSY 1
29369107e9d2SChris Wilson #define KICK 5
29379107e9d2SChris Wilson #define HUNG 20
2938893eead0SChris Wilson 
2939d330a953SJani Nikula 	if (!i915.enable_hangcheck)
29403e0dc6b0SBen Widawsky 		return;
29413e0dc6b0SBen Widawsky 
2942b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
294350877445SChris Wilson 		u64 acthd;
294450877445SChris Wilson 		u32 seqno;
29459107e9d2SChris Wilson 		bool busy = true;
2946b4519513SChris Wilson 
29476274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
29486274f212SChris Wilson 
294905407ff8SMika Kuoppala 		seqno = ring->get_seqno(ring, false);
295005407ff8SMika Kuoppala 		acthd = intel_ring_get_active_head(ring);
295105407ff8SMika Kuoppala 
295205407ff8SMika Kuoppala 		if (ring->hangcheck.seqno == seqno) {
295394f7bbe1STomas Elf 			if (ring_idle(ring, seqno)) {
2954da661464SMika Kuoppala 				ring->hangcheck.action = HANGCHECK_IDLE;
2955da661464SMika Kuoppala 
29569107e9d2SChris Wilson 				if (waitqueue_active(&ring->irq_queue)) {
29579107e9d2SChris Wilson 					/* Issue a wake-up to catch stuck h/w. */
2958094f9a54SChris Wilson 					if (!test_and_set_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings)) {
2959f4adcd24SDaniel Vetter 						if (!(dev_priv->gpu_error.test_irq_rings & intel_ring_flag(ring)))
29609107e9d2SChris Wilson 							DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
29619107e9d2SChris Wilson 								  ring->name);
2962f4adcd24SDaniel Vetter 						else
2963f4adcd24SDaniel Vetter 							DRM_INFO("Fake missed irq on %s\n",
2964f4adcd24SDaniel Vetter 								 ring->name);
29659107e9d2SChris Wilson 						wake_up_all(&ring->irq_queue);
2966094f9a54SChris Wilson 					}
2967094f9a54SChris Wilson 					/* Safeguard against driver failure */
2968094f9a54SChris Wilson 					ring->hangcheck.score += BUSY;
29699107e9d2SChris Wilson 				} else
29709107e9d2SChris Wilson 					busy = false;
297105407ff8SMika Kuoppala 			} else {
29726274f212SChris Wilson 				/* We always increment the hangcheck score
29736274f212SChris Wilson 				 * if the ring is busy and still processing
29746274f212SChris Wilson 				 * the same request, so that no single request
29756274f212SChris Wilson 				 * can run indefinitely (such as a chain of
29766274f212SChris Wilson 				 * batches). The only time we do not increment
29776274f212SChris Wilson 				 * the hangcheck score on this ring, if this
29786274f212SChris Wilson 				 * ring is in a legitimate wait for another
29796274f212SChris Wilson 				 * ring. In that case the waiting ring is a
29806274f212SChris Wilson 				 * victim and we want to be sure we catch the
29816274f212SChris Wilson 				 * right culprit. Then every time we do kick
29826274f212SChris Wilson 				 * the ring, add a small increment to the
29836274f212SChris Wilson 				 * score so that we can catch a batch that is
29846274f212SChris Wilson 				 * being repeatedly kicked and so responsible
29856274f212SChris Wilson 				 * for stalling the machine.
29869107e9d2SChris Wilson 				 */
2987ad8beaeaSMika Kuoppala 				ring->hangcheck.action = ring_stuck(ring,
2988ad8beaeaSMika Kuoppala 								    acthd);
2989ad8beaeaSMika Kuoppala 
2990ad8beaeaSMika Kuoppala 				switch (ring->hangcheck.action) {
2991da661464SMika Kuoppala 				case HANGCHECK_IDLE:
2992f2f4d82fSJani Nikula 				case HANGCHECK_WAIT:
2993f2f4d82fSJani Nikula 				case HANGCHECK_ACTIVE:
2994f260fe7bSMika Kuoppala 					break;
2995f260fe7bSMika Kuoppala 				case HANGCHECK_ACTIVE_LOOP:
2996ea04cb31SJani Nikula 					ring->hangcheck.score += BUSY;
29976274f212SChris Wilson 					break;
2998f2f4d82fSJani Nikula 				case HANGCHECK_KICK:
2999ea04cb31SJani Nikula 					ring->hangcheck.score += KICK;
30006274f212SChris Wilson 					break;
3001f2f4d82fSJani Nikula 				case HANGCHECK_HUNG:
3002ea04cb31SJani Nikula 					ring->hangcheck.score += HUNG;
30036274f212SChris Wilson 					stuck[i] = true;
30046274f212SChris Wilson 					break;
30056274f212SChris Wilson 				}
300605407ff8SMika Kuoppala 			}
30079107e9d2SChris Wilson 		} else {
3008da661464SMika Kuoppala 			ring->hangcheck.action = HANGCHECK_ACTIVE;
3009da661464SMika Kuoppala 
30109107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
30119107e9d2SChris Wilson 			 * attempts across multiple batches.
30129107e9d2SChris Wilson 			 */
30139107e9d2SChris Wilson 			if (ring->hangcheck.score > 0)
30149107e9d2SChris Wilson 				ring->hangcheck.score--;
3015f260fe7bSMika Kuoppala 
3016f260fe7bSMika Kuoppala 			ring->hangcheck.acthd = ring->hangcheck.max_acthd = 0;
3017cbb465e7SChris Wilson 		}
3018f65d9421SBen Gamari 
301905407ff8SMika Kuoppala 		ring->hangcheck.seqno = seqno;
302005407ff8SMika Kuoppala 		ring->hangcheck.acthd = acthd;
30219107e9d2SChris Wilson 		busy_count += busy;
302205407ff8SMika Kuoppala 	}
302305407ff8SMika Kuoppala 
302405407ff8SMika Kuoppala 	for_each_ring(ring, dev_priv, i) {
3025b6b0fac0SMika Kuoppala 		if (ring->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG) {
3026b8d88d1dSDaniel Vetter 			DRM_INFO("%s on %s\n",
302705407ff8SMika Kuoppala 				 stuck[i] ? "stuck" : "no progress",
3028a43adf07SChris Wilson 				 ring->name);
3029a43adf07SChris Wilson 			rings_hung++;
303005407ff8SMika Kuoppala 		}
303105407ff8SMika Kuoppala 	}
303205407ff8SMika Kuoppala 
303305407ff8SMika Kuoppala 	if (rings_hung)
303458174462SMika Kuoppala 		return i915_handle_error(dev, true, "Ring hung");
303505407ff8SMika Kuoppala 
303605407ff8SMika Kuoppala 	if (busy_count)
303705407ff8SMika Kuoppala 		/* Reset timer case chip hangs without another request
303805407ff8SMika Kuoppala 		 * being added */
303910cd45b6SMika Kuoppala 		i915_queue_hangcheck(dev);
304010cd45b6SMika Kuoppala }
304110cd45b6SMika Kuoppala 
304210cd45b6SMika Kuoppala void i915_queue_hangcheck(struct drm_device *dev)
304310cd45b6SMika Kuoppala {
3044737b1506SChris Wilson 	struct i915_gpu_error *e = &to_i915(dev)->gpu_error;
3045672e7b7cSChris Wilson 
3046d330a953SJani Nikula 	if (!i915.enable_hangcheck)
304710cd45b6SMika Kuoppala 		return;
304810cd45b6SMika Kuoppala 
3049737b1506SChris Wilson 	/* Don't continually defer the hangcheck so that it is always run at
3050737b1506SChris Wilson 	 * least once after work has been scheduled on any ring. Otherwise,
3051737b1506SChris Wilson 	 * we will ignore a hung ring if a second ring is kept busy.
3052737b1506SChris Wilson 	 */
3053737b1506SChris Wilson 
3054737b1506SChris Wilson 	queue_delayed_work(e->hangcheck_wq, &e->hangcheck_work,
3055737b1506SChris Wilson 			   round_jiffies_up_relative(DRM_I915_HANGCHECK_JIFFIES));
3056f65d9421SBen Gamari }
3057f65d9421SBen Gamari 
30581c69eb42SPaulo Zanoni static void ibx_irq_reset(struct drm_device *dev)
305991738a95SPaulo Zanoni {
306091738a95SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
306191738a95SPaulo Zanoni 
306291738a95SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
306391738a95SPaulo Zanoni 		return;
306491738a95SPaulo Zanoni 
3065f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(SDE);
3066105b122eSPaulo Zanoni 
3067105b122eSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
3068105b122eSPaulo Zanoni 		I915_WRITE(SERR_INT, 0xffffffff);
3069622364b6SPaulo Zanoni }
3070105b122eSPaulo Zanoni 
307191738a95SPaulo Zanoni /*
3072622364b6SPaulo Zanoni  * SDEIER is also touched by the interrupt handler to work around missed PCH
3073622364b6SPaulo Zanoni  * interrupts. Hence we can't update it after the interrupt handler is enabled -
3074622364b6SPaulo Zanoni  * instead we unconditionally enable all PCH interrupt sources here, but then
3075622364b6SPaulo Zanoni  * only unmask them as needed with SDEIMR.
3076622364b6SPaulo Zanoni  *
3077622364b6SPaulo Zanoni  * This function needs to be called before interrupts are enabled.
307891738a95SPaulo Zanoni  */
3079622364b6SPaulo Zanoni static void ibx_irq_pre_postinstall(struct drm_device *dev)
3080622364b6SPaulo Zanoni {
3081622364b6SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3082622364b6SPaulo Zanoni 
3083622364b6SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
3084622364b6SPaulo Zanoni 		return;
3085622364b6SPaulo Zanoni 
3086622364b6SPaulo Zanoni 	WARN_ON(I915_READ(SDEIER) != 0);
308791738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
308891738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
308991738a95SPaulo Zanoni }
309091738a95SPaulo Zanoni 
30917c4d664eSPaulo Zanoni static void gen5_gt_irq_reset(struct drm_device *dev)
3092d18ea1b5SDaniel Vetter {
3093d18ea1b5SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
3094d18ea1b5SDaniel Vetter 
3095f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GT);
3096a9d356a6SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6)
3097f86f3fb0SPaulo Zanoni 		GEN5_IRQ_RESET(GEN6_PM);
3098d18ea1b5SDaniel Vetter }
3099d18ea1b5SDaniel Vetter 
3100c0e09200SDave Airlie /* drm_dma.h hooks
3101c0e09200SDave Airlie */
3102be30b29fSPaulo Zanoni static void ironlake_irq_reset(struct drm_device *dev)
3103036a4a7dSZhenyu Wang {
31042d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3105036a4a7dSZhenyu Wang 
31060c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xffffffff);
3107bdfcdb63SDaniel Vetter 
3108f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(DE);
3109c6d954c1SPaulo Zanoni 	if (IS_GEN7(dev))
3110c6d954c1SPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, 0xffffffff);
3111036a4a7dSZhenyu Wang 
31127c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
3113c650156aSZhenyu Wang 
31141c69eb42SPaulo Zanoni 	ibx_irq_reset(dev);
31157d99163dSBen Widawsky }
31167d99163dSBen Widawsky 
311770591a41SVille Syrjälä static void vlv_display_irq_reset(struct drm_i915_private *dev_priv)
311870591a41SVille Syrjälä {
311970591a41SVille Syrjälä 	enum pipe pipe;
312070591a41SVille Syrjälä 
31210706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xFFFFFFFF, 0);
312270591a41SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
312370591a41SVille Syrjälä 
312470591a41SVille Syrjälä 	for_each_pipe(dev_priv, pipe)
312570591a41SVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), 0xffff);
312670591a41SVille Syrjälä 
312770591a41SVille Syrjälä 	GEN5_IRQ_RESET(VLV_);
312870591a41SVille Syrjälä }
312970591a41SVille Syrjälä 
31307e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
31317e231dbeSJesse Barnes {
31322d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
31337e231dbeSJesse Barnes 
31347e231dbeSJesse Barnes 	/* VLV magic */
31357e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
31367e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
31377e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
31387e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
31397e231dbeSJesse Barnes 
31407c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
31417e231dbeSJesse Barnes 
31427c4cde39SVille Syrjälä 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
31437e231dbeSJesse Barnes 
314470591a41SVille Syrjälä 	vlv_display_irq_reset(dev_priv);
31457e231dbeSJesse Barnes }
31467e231dbeSJesse Barnes 
3147d6e3cca3SDaniel Vetter static void gen8_gt_irq_reset(struct drm_i915_private *dev_priv)
3148d6e3cca3SDaniel Vetter {
3149d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 0);
3150d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 1);
3151d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 2);
3152d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 3);
3153d6e3cca3SDaniel Vetter }
3154d6e3cca3SDaniel Vetter 
3155823f6b38SPaulo Zanoni static void gen8_irq_reset(struct drm_device *dev)
3156abd58f01SBen Widawsky {
3157abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3158abd58f01SBen Widawsky 	int pipe;
3159abd58f01SBen Widawsky 
3160abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
3161abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3162abd58f01SBen Widawsky 
3163d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
3164abd58f01SBen Widawsky 
3165055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3166f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3167813bde43SPaulo Zanoni 						   POWER_DOMAIN_PIPE(pipe)))
3168f86f3fb0SPaulo Zanoni 			GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3169abd58f01SBen Widawsky 
3170f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_PORT_);
3171f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_MISC_);
3172f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_PCU_);
3173abd58f01SBen Widawsky 
3174266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev))
31751c69eb42SPaulo Zanoni 		ibx_irq_reset(dev);
3176abd58f01SBen Widawsky }
3177abd58f01SBen Widawsky 
31784c6c03beSDamien Lespiau void gen8_irq_power_well_post_enable(struct drm_i915_private *dev_priv,
31794c6c03beSDamien Lespiau 				     unsigned int pipe_mask)
3180d49bdb0eSPaulo Zanoni {
31811180e206SPaulo Zanoni 	uint32_t extra_ier = GEN8_PIPE_VBLANK | GEN8_PIPE_FIFO_UNDERRUN;
3182d49bdb0eSPaulo Zanoni 
318313321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3184d14c0343SDamien Lespiau 	if (pipe_mask & 1 << PIPE_A)
3185d14c0343SDamien Lespiau 		GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_A,
3186d14c0343SDamien Lespiau 				  dev_priv->de_irq_mask[PIPE_A],
3187d14c0343SDamien Lespiau 				  ~dev_priv->de_irq_mask[PIPE_A] | extra_ier);
31884c6c03beSDamien Lespiau 	if (pipe_mask & 1 << PIPE_B)
31894c6c03beSDamien Lespiau 		GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_B,
31904c6c03beSDamien Lespiau 				  dev_priv->de_irq_mask[PIPE_B],
31911180e206SPaulo Zanoni 				  ~dev_priv->de_irq_mask[PIPE_B] | extra_ier);
31924c6c03beSDamien Lespiau 	if (pipe_mask & 1 << PIPE_C)
31934c6c03beSDamien Lespiau 		GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_C,
31944c6c03beSDamien Lespiau 				  dev_priv->de_irq_mask[PIPE_C],
31951180e206SPaulo Zanoni 				  ~dev_priv->de_irq_mask[PIPE_C] | extra_ier);
319613321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3197d49bdb0eSPaulo Zanoni }
3198d49bdb0eSPaulo Zanoni 
319943f328d7SVille Syrjälä static void cherryview_irq_preinstall(struct drm_device *dev)
320043f328d7SVille Syrjälä {
320143f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
320243f328d7SVille Syrjälä 
320343f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
320443f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
320543f328d7SVille Syrjälä 
3206d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
320743f328d7SVille Syrjälä 
320843f328d7SVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
320943f328d7SVille Syrjälä 
321043f328d7SVille Syrjälä 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
321143f328d7SVille Syrjälä 
321270591a41SVille Syrjälä 	vlv_display_irq_reset(dev_priv);
321343f328d7SVille Syrjälä }
321443f328d7SVille Syrjälä 
321587a02106SVille Syrjälä static u32 intel_hpd_enabled_irqs(struct drm_device *dev,
321687a02106SVille Syrjälä 				  const u32 hpd[HPD_NUM_PINS])
321787a02106SVille Syrjälä {
321887a02106SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(dev);
321987a02106SVille Syrjälä 	struct intel_encoder *encoder;
322087a02106SVille Syrjälä 	u32 enabled_irqs = 0;
322187a02106SVille Syrjälä 
322287a02106SVille Syrjälä 	for_each_intel_encoder(dev, encoder)
322387a02106SVille Syrjälä 		if (dev_priv->hotplug.stats[encoder->hpd_pin].state == HPD_ENABLED)
322487a02106SVille Syrjälä 			enabled_irqs |= hpd[encoder->hpd_pin];
322587a02106SVille Syrjälä 
322687a02106SVille Syrjälä 	return enabled_irqs;
322787a02106SVille Syrjälä }
322887a02106SVille Syrjälä 
322982a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
323082a28bcfSDaniel Vetter {
32312d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
323287a02106SVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
323382a28bcfSDaniel Vetter 
323482a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
3235fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
323687a02106SVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_ibx);
323782a28bcfSDaniel Vetter 	} else {
3238fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
323987a02106SVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_cpt);
324082a28bcfSDaniel Vetter 	}
324182a28bcfSDaniel Vetter 
3242fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
324382a28bcfSDaniel Vetter 
32447fe0b973SKeith Packard 	/*
32457fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
32466dbf30ceSVille Syrjälä 	 * duration to 2ms (which is the minimum in the Display Port spec).
32476dbf30ceSVille Syrjälä 	 * The pulse duration bits are reserved on LPT+.
32487fe0b973SKeith Packard 	 */
32497fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
32507fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
32517fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
32527fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
32537fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
32540b2eb33eSVille Syrjälä 	/*
32550b2eb33eSVille Syrjälä 	 * When CPU and PCH are on the same package, port A
32560b2eb33eSVille Syrjälä 	 * HPD must be enabled in both north and south.
32570b2eb33eSVille Syrjälä 	 */
32580b2eb33eSVille Syrjälä 	if (HAS_PCH_LPT_LP(dev))
32590b2eb33eSVille Syrjälä 		hotplug |= PORTA_HOTPLUG_ENABLE;
32607fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
32616dbf30ceSVille Syrjälä }
326226951cafSXiong Zhang 
32636dbf30ceSVille Syrjälä static void spt_hpd_irq_setup(struct drm_device *dev)
32646dbf30ceSVille Syrjälä {
32656dbf30ceSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
32666dbf30ceSVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
32676dbf30ceSVille Syrjälä 
32686dbf30ceSVille Syrjälä 	hotplug_irqs = SDE_HOTPLUG_MASK_SPT;
32696dbf30ceSVille Syrjälä 	enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_spt);
32706dbf30ceSVille Syrjälä 
32716dbf30ceSVille Syrjälä 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
32726dbf30ceSVille Syrjälä 
32736dbf30ceSVille Syrjälä 	/* Enable digital hotplug on the PCH */
32746dbf30ceSVille Syrjälä 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
32756dbf30ceSVille Syrjälä 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTC_HOTPLUG_ENABLE |
327674c0b395SVille Syrjälä 		PORTB_HOTPLUG_ENABLE | PORTA_HOTPLUG_ENABLE;
32776dbf30ceSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
32786dbf30ceSVille Syrjälä 
327926951cafSXiong Zhang 	hotplug = I915_READ(PCH_PORT_HOTPLUG2);
328026951cafSXiong Zhang 	hotplug |= PORTE_HOTPLUG_ENABLE;
328126951cafSXiong Zhang 	I915_WRITE(PCH_PORT_HOTPLUG2, hotplug);
328226951cafSXiong Zhang }
32837fe0b973SKeith Packard 
3284e4ce95aaSVille Syrjälä static void ilk_hpd_irq_setup(struct drm_device *dev)
3285e4ce95aaSVille Syrjälä {
3286e4ce95aaSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
3287e4ce95aaSVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
3288e4ce95aaSVille Syrjälä 
32893a3b3c7dSVille Syrjälä 	if (INTEL_INFO(dev)->gen >= 8) {
32903a3b3c7dSVille Syrjälä 		hotplug_irqs = GEN8_PORT_DP_A_HOTPLUG;
32913a3b3c7dSVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_bdw);
32923a3b3c7dSVille Syrjälä 
32933a3b3c7dSVille Syrjälä 		bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
32943a3b3c7dSVille Syrjälä 	} else if (INTEL_INFO(dev)->gen >= 7) {
329523bb4cb5SVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG_IVB;
329623bb4cb5SVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_ivb);
32973a3b3c7dSVille Syrjälä 
32983a3b3c7dSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
329923bb4cb5SVille Syrjälä 	} else {
3300e4ce95aaSVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG;
3301e4ce95aaSVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_ilk);
3302e4ce95aaSVille Syrjälä 
3303e4ce95aaSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
33043a3b3c7dSVille Syrjälä 	}
3305e4ce95aaSVille Syrjälä 
3306e4ce95aaSVille Syrjälä 	/*
3307e4ce95aaSVille Syrjälä 	 * Enable digital hotplug on the CPU, and configure the DP short pulse
3308e4ce95aaSVille Syrjälä 	 * duration to 2ms (which is the minimum in the Display Port spec)
330923bb4cb5SVille Syrjälä 	 * The pulse duration bits are reserved on HSW+.
3310e4ce95aaSVille Syrjälä 	 */
3311e4ce95aaSVille Syrjälä 	hotplug = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
3312e4ce95aaSVille Syrjälä 	hotplug &= ~DIGITAL_PORTA_PULSE_DURATION_MASK;
3313e4ce95aaSVille Syrjälä 	hotplug |= DIGITAL_PORTA_HOTPLUG_ENABLE | DIGITAL_PORTA_PULSE_DURATION_2ms;
3314e4ce95aaSVille Syrjälä 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, hotplug);
3315e4ce95aaSVille Syrjälä 
3316e4ce95aaSVille Syrjälä 	ibx_hpd_irq_setup(dev);
3317e4ce95aaSVille Syrjälä }
3318e4ce95aaSVille Syrjälä 
3319e0a20ad7SShashank Sharma static void bxt_hpd_irq_setup(struct drm_device *dev)
3320e0a20ad7SShashank Sharma {
3321e0a20ad7SShashank Sharma 	struct drm_i915_private *dev_priv = dev->dev_private;
3322a52bb15bSVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
3323e0a20ad7SShashank Sharma 
3324a52bb15bSVille Syrjälä 	enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_bxt);
3325a52bb15bSVille Syrjälä 	hotplug_irqs = BXT_DE_PORT_HOTPLUG_MASK;
3326e0a20ad7SShashank Sharma 
3327a52bb15bSVille Syrjälä 	bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
3328e0a20ad7SShashank Sharma 
3329a52bb15bSVille Syrjälä 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
3330a52bb15bSVille Syrjälä 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTB_HOTPLUG_ENABLE |
3331a52bb15bSVille Syrjälä 		PORTA_HOTPLUG_ENABLE;
3332a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
3333e0a20ad7SShashank Sharma }
3334e0a20ad7SShashank Sharma 
3335d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
3336d46da437SPaulo Zanoni {
33372d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
333882a28bcfSDaniel Vetter 	u32 mask;
3339d46da437SPaulo Zanoni 
3340692a04cfSDaniel Vetter 	if (HAS_PCH_NOP(dev))
3341692a04cfSDaniel Vetter 		return;
3342692a04cfSDaniel Vetter 
3343105b122eSPaulo Zanoni 	if (HAS_PCH_IBX(dev))
33445c673b60SDaniel Vetter 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
3345105b122eSPaulo Zanoni 	else
33465c673b60SDaniel Vetter 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
33478664281bSPaulo Zanoni 
3348337ba017SPaulo Zanoni 	GEN5_ASSERT_IIR_IS_ZERO(SDEIIR);
3349d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
3350d46da437SPaulo Zanoni }
3351d46da437SPaulo Zanoni 
33520a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
33530a9a8c91SDaniel Vetter {
33540a9a8c91SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
33550a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
33560a9a8c91SDaniel Vetter 
33570a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
33580a9a8c91SDaniel Vetter 
33590a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
3360040d2baaSBen Widawsky 	if (HAS_L3_DPF(dev)) {
33610a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
336235a85ac6SBen Widawsky 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev);
336335a85ac6SBen Widawsky 		gt_irqs |= GT_PARITY_ERROR(dev);
33640a9a8c91SDaniel Vetter 	}
33650a9a8c91SDaniel Vetter 
33660a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
33670a9a8c91SDaniel Vetter 	if (IS_GEN5(dev)) {
33680a9a8c91SDaniel Vetter 		gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
33690a9a8c91SDaniel Vetter 			   ILK_BSD_USER_INTERRUPT;
33700a9a8c91SDaniel Vetter 	} else {
33710a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
33720a9a8c91SDaniel Vetter 	}
33730a9a8c91SDaniel Vetter 
337435079899SPaulo Zanoni 	GEN5_IRQ_INIT(GT, dev_priv->gt_irq_mask, gt_irqs);
33750a9a8c91SDaniel Vetter 
33760a9a8c91SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
337778e68d36SImre Deak 		/*
337878e68d36SImre Deak 		 * RPS interrupts will get enabled/disabled on demand when RPS
337978e68d36SImre Deak 		 * itself is enabled/disabled.
338078e68d36SImre Deak 		 */
33810a9a8c91SDaniel Vetter 		if (HAS_VEBOX(dev))
33820a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
33830a9a8c91SDaniel Vetter 
3384605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = 0xffffffff;
338535079899SPaulo Zanoni 		GEN5_IRQ_INIT(GEN6_PM, dev_priv->pm_irq_mask, pm_irqs);
33860a9a8c91SDaniel Vetter 	}
33870a9a8c91SDaniel Vetter }
33880a9a8c91SDaniel Vetter 
3389f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
3390036a4a7dSZhenyu Wang {
33912d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
33928e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
33938e76f8dcSPaulo Zanoni 
33948e76f8dcSPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 7) {
33958e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
33968e76f8dcSPaulo Zanoni 				DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
33978e76f8dcSPaulo Zanoni 				DE_PLANEB_FLIP_DONE_IVB |
33985c673b60SDaniel Vetter 				DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB);
33998e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
340023bb4cb5SVille Syrjälä 			      DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB |
340123bb4cb5SVille Syrjälä 			      DE_DP_A_HOTPLUG_IVB);
34028e76f8dcSPaulo Zanoni 	} else {
34038e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3404ce99c256SDaniel Vetter 				DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
34055b3a856bSDaniel Vetter 				DE_AUX_CHANNEL_A |
34065b3a856bSDaniel Vetter 				DE_PIPEB_CRC_DONE | DE_PIPEA_CRC_DONE |
34075b3a856bSDaniel Vetter 				DE_POISON);
3408e4ce95aaSVille Syrjälä 		extra_mask = (DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
3409e4ce95aaSVille Syrjälä 			      DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN |
3410e4ce95aaSVille Syrjälä 			      DE_DP_A_HOTPLUG);
34118e76f8dcSPaulo Zanoni 	}
3412036a4a7dSZhenyu Wang 
34131ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3414036a4a7dSZhenyu Wang 
34150c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xeffe);
34160c841212SPaulo Zanoni 
3417622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3418622364b6SPaulo Zanoni 
341935079899SPaulo Zanoni 	GEN5_IRQ_INIT(DE, dev_priv->irq_mask, display_mask | extra_mask);
3420036a4a7dSZhenyu Wang 
34210a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
3422036a4a7dSZhenyu Wang 
3423d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
34247fe0b973SKeith Packard 
3425f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
34266005ce42SDaniel Vetter 		/* Enable PCU event interrupts
34276005ce42SDaniel Vetter 		 *
34286005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
34294bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
34304bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
3431d6207435SDaniel Vetter 		spin_lock_irq(&dev_priv->irq_lock);
3432f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
3433d6207435SDaniel Vetter 		spin_unlock_irq(&dev_priv->irq_lock);
3434f97108d1SJesse Barnes 	}
3435f97108d1SJesse Barnes 
3436036a4a7dSZhenyu Wang 	return 0;
3437036a4a7dSZhenyu Wang }
3438036a4a7dSZhenyu Wang 
3439f8b79e58SImre Deak static void valleyview_display_irqs_install(struct drm_i915_private *dev_priv)
3440f8b79e58SImre Deak {
3441f8b79e58SImre Deak 	u32 pipestat_mask;
3442f8b79e58SImre Deak 	u32 iir_mask;
3443120dda4fSVille Syrjälä 	enum pipe pipe;
3444f8b79e58SImre Deak 
3445f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3446f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3447f8b79e58SImre Deak 
3448120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3449120dda4fSVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), pipestat_mask);
3450f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3451f8b79e58SImre Deak 
3452f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3453f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3454f8b79e58SImre Deak 
3455120dda4fSVille Syrjälä 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3456120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3457120dda4fSVille Syrjälä 		      i915_enable_pipestat(dev_priv, pipe, pipestat_mask);
3458f8b79e58SImre Deak 
3459f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3460f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3461f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3462120dda4fSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
3463120dda4fSVille Syrjälä 		iir_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
3464f8b79e58SImre Deak 	dev_priv->irq_mask &= ~iir_mask;
3465f8b79e58SImre Deak 
3466f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3467f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3468f8b79e58SImre Deak 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
346976e41860SVille Syrjälä 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
347076e41860SVille Syrjälä 	POSTING_READ(VLV_IMR);
3471f8b79e58SImre Deak }
3472f8b79e58SImre Deak 
3473f8b79e58SImre Deak static void valleyview_display_irqs_uninstall(struct drm_i915_private *dev_priv)
3474f8b79e58SImre Deak {
3475f8b79e58SImre Deak 	u32 pipestat_mask;
3476f8b79e58SImre Deak 	u32 iir_mask;
3477120dda4fSVille Syrjälä 	enum pipe pipe;
3478f8b79e58SImre Deak 
3479f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3480f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
34816c7fba04SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3482120dda4fSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
3483120dda4fSVille Syrjälä 		iir_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
3484f8b79e58SImre Deak 
3485f8b79e58SImre Deak 	dev_priv->irq_mask |= iir_mask;
3486f8b79e58SImre Deak 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
348776e41860SVille Syrjälä 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
3488f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3489f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3490f8b79e58SImre Deak 	POSTING_READ(VLV_IIR);
3491f8b79e58SImre Deak 
3492f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3493f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3494f8b79e58SImre Deak 
3495120dda4fSVille Syrjälä 	i915_disable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3496120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3497120dda4fSVille Syrjälä 		i915_disable_pipestat(dev_priv, pipe, pipestat_mask);
3498f8b79e58SImre Deak 
3499f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3500f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3501120dda4fSVille Syrjälä 
3502120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3503120dda4fSVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), pipestat_mask);
3504f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3505f8b79e58SImre Deak }
3506f8b79e58SImre Deak 
3507f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3508f8b79e58SImre Deak {
3509f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3510f8b79e58SImre Deak 
3511f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3512f8b79e58SImre Deak 		return;
3513f8b79e58SImre Deak 
3514f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3515f8b79e58SImre Deak 
3516950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3517f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3518f8b79e58SImre Deak }
3519f8b79e58SImre Deak 
3520f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3521f8b79e58SImre Deak {
3522f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3523f8b79e58SImre Deak 
3524f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3525f8b79e58SImre Deak 		return;
3526f8b79e58SImre Deak 
3527f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3528f8b79e58SImre Deak 
3529950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3530f8b79e58SImre Deak 		valleyview_display_irqs_uninstall(dev_priv);
3531f8b79e58SImre Deak }
3532f8b79e58SImre Deak 
35330e6c9a9eSVille Syrjälä static void vlv_display_irq_postinstall(struct drm_i915_private *dev_priv)
35347e231dbeSJesse Barnes {
3535f8b79e58SImre Deak 	dev_priv->irq_mask = ~0;
35367e231dbeSJesse Barnes 
35370706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
353820afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
353920afbda2SDaniel Vetter 
35407e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
354176e41860SVille Syrjälä 	I915_WRITE(VLV_IIR, 0xffffffff);
354276e41860SVille Syrjälä 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
354376e41860SVille Syrjälä 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
354476e41860SVille Syrjälä 	POSTING_READ(VLV_IMR);
35457e231dbeSJesse Barnes 
3546b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3547b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3548d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3549f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3550f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3551d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
35520e6c9a9eSVille Syrjälä }
35530e6c9a9eSVille Syrjälä 
35540e6c9a9eSVille Syrjälä static int valleyview_irq_postinstall(struct drm_device *dev)
35550e6c9a9eSVille Syrjälä {
35560e6c9a9eSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
35570e6c9a9eSVille Syrjälä 
35580e6c9a9eSVille Syrjälä 	vlv_display_irq_postinstall(dev_priv);
35597e231dbeSJesse Barnes 
35600a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
35617e231dbeSJesse Barnes 
35627e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
35637e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
35647e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
35657e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
35667e231dbeSJesse Barnes #endif
35677e231dbeSJesse Barnes 
35687e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
356920afbda2SDaniel Vetter 
357020afbda2SDaniel Vetter 	return 0;
357120afbda2SDaniel Vetter }
357220afbda2SDaniel Vetter 
3573abd58f01SBen Widawsky static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
3574abd58f01SBen Widawsky {
3575abd58f01SBen Widawsky 	/* These are interrupts we'll toggle with the ring mask register */
3576abd58f01SBen Widawsky 	uint32_t gt_interrupts[] = {
3577abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
357873d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
3579abd58f01SBen Widawsky 			GT_RENDER_L3_PARITY_ERROR_INTERRUPT |
358073d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT |
358173d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT,
3582abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
358373d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
358473d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT |
358573d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT,
3586abd58f01SBen Widawsky 		0,
358773d477f6SOscar Mateo 		GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT |
358873d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT
3589abd58f01SBen Widawsky 		};
3590abd58f01SBen Widawsky 
35910961021aSBen Widawsky 	dev_priv->pm_irq_mask = 0xffffffff;
35929a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 0, ~gt_interrupts[0], gt_interrupts[0]);
35939a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 1, ~gt_interrupts[1], gt_interrupts[1]);
359478e68d36SImre Deak 	/*
359578e68d36SImre Deak 	 * RPS interrupts will get enabled/disabled on demand when RPS itself
359678e68d36SImre Deak 	 * is enabled/disabled.
359778e68d36SImre Deak 	 */
359878e68d36SImre Deak 	GEN8_IRQ_INIT_NDX(GT, 2, dev_priv->pm_irq_mask, 0);
35999a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 3, ~gt_interrupts[3], gt_interrupts[3]);
3600abd58f01SBen Widawsky }
3601abd58f01SBen Widawsky 
3602abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3603abd58f01SBen Widawsky {
3604770de83dSDamien Lespiau 	uint32_t de_pipe_masked = GEN8_PIPE_CDCLK_CRC_DONE;
3605770de83dSDamien Lespiau 	uint32_t de_pipe_enables;
36063a3b3c7dSVille Syrjälä 	u32 de_port_masked = GEN8_AUX_CHANNEL_A;
36073a3b3c7dSVille Syrjälä 	u32 de_port_enables;
36083a3b3c7dSVille Syrjälä 	enum pipe pipe;
3609770de83dSDamien Lespiau 
3610b4834a50SRodrigo Vivi 	if (INTEL_INFO(dev_priv)->gen >= 9) {
3611770de83dSDamien Lespiau 		de_pipe_masked |= GEN9_PIPE_PLANE1_FLIP_DONE |
3612770de83dSDamien Lespiau 				  GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
36133a3b3c7dSVille Syrjälä 		de_port_masked |= GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
361488e04703SJesse Barnes 				  GEN9_AUX_CHANNEL_D;
36159e63743eSShashank Sharma 		if (IS_BROXTON(dev_priv))
36163a3b3c7dSVille Syrjälä 			de_port_masked |= BXT_DE_PORT_GMBUS;
36173a3b3c7dSVille Syrjälä 	} else {
3618770de83dSDamien Lespiau 		de_pipe_masked |= GEN8_PIPE_PRIMARY_FLIP_DONE |
3619770de83dSDamien Lespiau 				  GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
36203a3b3c7dSVille Syrjälä 	}
3621770de83dSDamien Lespiau 
3622770de83dSDamien Lespiau 	de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
3623770de83dSDamien Lespiau 					   GEN8_PIPE_FIFO_UNDERRUN;
3624770de83dSDamien Lespiau 
36253a3b3c7dSVille Syrjälä 	de_port_enables = de_port_masked;
3626a52bb15bSVille Syrjälä 	if (IS_BROXTON(dev_priv))
3627a52bb15bSVille Syrjälä 		de_port_enables |= BXT_DE_PORT_HOTPLUG_MASK;
3628a52bb15bSVille Syrjälä 	else if (IS_BROADWELL(dev_priv))
36293a3b3c7dSVille Syrjälä 		de_port_enables |= GEN8_PORT_DP_A_HOTPLUG;
36303a3b3c7dSVille Syrjälä 
363113b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_A] = ~de_pipe_masked;
363213b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_B] = ~de_pipe_masked;
363313b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_C] = ~de_pipe_masked;
3634abd58f01SBen Widawsky 
3635055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3636f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3637813bde43SPaulo Zanoni 				POWER_DOMAIN_PIPE(pipe)))
3638813bde43SPaulo Zanoni 			GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
3639813bde43SPaulo Zanoni 					  dev_priv->de_irq_mask[pipe],
364035079899SPaulo Zanoni 					  de_pipe_enables);
3641abd58f01SBen Widawsky 
36423a3b3c7dSVille Syrjälä 	GEN5_IRQ_INIT(GEN8_DE_PORT_, ~de_port_masked, de_port_enables);
3643abd58f01SBen Widawsky }
3644abd58f01SBen Widawsky 
3645abd58f01SBen Widawsky static int gen8_irq_postinstall(struct drm_device *dev)
3646abd58f01SBen Widawsky {
3647abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3648abd58f01SBen Widawsky 
3649266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev))
3650622364b6SPaulo Zanoni 		ibx_irq_pre_postinstall(dev);
3651622364b6SPaulo Zanoni 
3652abd58f01SBen Widawsky 	gen8_gt_irq_postinstall(dev_priv);
3653abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3654abd58f01SBen Widawsky 
3655266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev))
3656abd58f01SBen Widawsky 		ibx_irq_postinstall(dev);
3657abd58f01SBen Widawsky 
3658abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
3659abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3660abd58f01SBen Widawsky 
3661abd58f01SBen Widawsky 	return 0;
3662abd58f01SBen Widawsky }
3663abd58f01SBen Widawsky 
366443f328d7SVille Syrjälä static int cherryview_irq_postinstall(struct drm_device *dev)
366543f328d7SVille Syrjälä {
366643f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
366743f328d7SVille Syrjälä 
3668c2b66797SVille Syrjälä 	vlv_display_irq_postinstall(dev_priv);
366943f328d7SVille Syrjälä 
367043f328d7SVille Syrjälä 	gen8_gt_irq_postinstall(dev_priv);
367143f328d7SVille Syrjälä 
367243f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, MASTER_INTERRUPT_ENABLE);
367343f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
367443f328d7SVille Syrjälä 
367543f328d7SVille Syrjälä 	return 0;
367643f328d7SVille Syrjälä }
367743f328d7SVille Syrjälä 
3678abd58f01SBen Widawsky static void gen8_irq_uninstall(struct drm_device *dev)
3679abd58f01SBen Widawsky {
3680abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3681abd58f01SBen Widawsky 
3682abd58f01SBen Widawsky 	if (!dev_priv)
3683abd58f01SBen Widawsky 		return;
3684abd58f01SBen Widawsky 
3685823f6b38SPaulo Zanoni 	gen8_irq_reset(dev);
3686abd58f01SBen Widawsky }
3687abd58f01SBen Widawsky 
36888ea0be4fSVille Syrjälä static void vlv_display_irq_uninstall(struct drm_i915_private *dev_priv)
36898ea0be4fSVille Syrjälä {
36908ea0be4fSVille Syrjälä 	/* Interrupt setup is already guaranteed to be single-threaded, this is
36918ea0be4fSVille Syrjälä 	 * just to make the assert_spin_locked check happy. */
36928ea0be4fSVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
36938ea0be4fSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
36948ea0be4fSVille Syrjälä 		valleyview_display_irqs_uninstall(dev_priv);
36958ea0be4fSVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
36968ea0be4fSVille Syrjälä 
36978ea0be4fSVille Syrjälä 	vlv_display_irq_reset(dev_priv);
36988ea0be4fSVille Syrjälä 
3699c352d1baSImre Deak 	dev_priv->irq_mask = ~0;
37008ea0be4fSVille Syrjälä }
37018ea0be4fSVille Syrjälä 
37027e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
37037e231dbeSJesse Barnes {
37042d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
37057e231dbeSJesse Barnes 
37067e231dbeSJesse Barnes 	if (!dev_priv)
37077e231dbeSJesse Barnes 		return;
37087e231dbeSJesse Barnes 
3709843d0e7dSImre Deak 	I915_WRITE(VLV_MASTER_IER, 0);
3710843d0e7dSImre Deak 
3711893fce8eSVille Syrjälä 	gen5_gt_irq_reset(dev);
3712893fce8eSVille Syrjälä 
37137e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
3714f8b79e58SImre Deak 
37158ea0be4fSVille Syrjälä 	vlv_display_irq_uninstall(dev_priv);
37167e231dbeSJesse Barnes }
37177e231dbeSJesse Barnes 
371843f328d7SVille Syrjälä static void cherryview_irq_uninstall(struct drm_device *dev)
371943f328d7SVille Syrjälä {
372043f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
372143f328d7SVille Syrjälä 
372243f328d7SVille Syrjälä 	if (!dev_priv)
372343f328d7SVille Syrjälä 		return;
372443f328d7SVille Syrjälä 
372543f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
372643f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
372743f328d7SVille Syrjälä 
3728a2c30fbaSVille Syrjälä 	gen8_gt_irq_reset(dev_priv);
372943f328d7SVille Syrjälä 
3730a2c30fbaSVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
373143f328d7SVille Syrjälä 
3732c2b66797SVille Syrjälä 	vlv_display_irq_uninstall(dev_priv);
373343f328d7SVille Syrjälä }
373443f328d7SVille Syrjälä 
3735f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
3736036a4a7dSZhenyu Wang {
37372d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
37384697995bSJesse Barnes 
37394697995bSJesse Barnes 	if (!dev_priv)
37404697995bSJesse Barnes 		return;
37414697995bSJesse Barnes 
3742be30b29fSPaulo Zanoni 	ironlake_irq_reset(dev);
3743036a4a7dSZhenyu Wang }
3744036a4a7dSZhenyu Wang 
3745c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
3746c2798b19SChris Wilson {
37472d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3748c2798b19SChris Wilson 	int pipe;
3749c2798b19SChris Wilson 
3750055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3751c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3752c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3753c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3754c2798b19SChris Wilson 	POSTING_READ16(IER);
3755c2798b19SChris Wilson }
3756c2798b19SChris Wilson 
3757c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
3758c2798b19SChris Wilson {
37592d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3760c2798b19SChris Wilson 
3761c2798b19SChris Wilson 	I915_WRITE16(EMR,
3762c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3763c2798b19SChris Wilson 
3764c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
3765c2798b19SChris Wilson 	dev_priv->irq_mask =
3766c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3767c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3768c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
376937ef01abSDaniel Vetter 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
3770c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
3771c2798b19SChris Wilson 
3772c2798b19SChris Wilson 	I915_WRITE16(IER,
3773c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3774c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3775c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
3776c2798b19SChris Wilson 	POSTING_READ16(IER);
3777c2798b19SChris Wilson 
3778379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3779379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3780d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3781755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3782755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3783d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3784379ef82dSDaniel Vetter 
3785c2798b19SChris Wilson 	return 0;
3786c2798b19SChris Wilson }
3787c2798b19SChris Wilson 
378890a72f87SVille Syrjälä /*
378990a72f87SVille Syrjälä  * Returns true when a page flip has completed.
379090a72f87SVille Syrjälä  */
379190a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
37921f1c2e24SVille Syrjälä 			       int plane, int pipe, u32 iir)
379390a72f87SVille Syrjälä {
37942d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
37951f1c2e24SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
379690a72f87SVille Syrjälä 
37978d7849dbSVille Syrjälä 	if (!intel_pipe_handle_vblank(dev, pipe))
379890a72f87SVille Syrjälä 		return false;
379990a72f87SVille Syrjälä 
380090a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
3801d6bbafa1SChris Wilson 		goto check_page_flip;
380290a72f87SVille Syrjälä 
380390a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
380490a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
380590a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
380690a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
380790a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
380890a72f87SVille Syrjälä 	 */
380990a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
3810d6bbafa1SChris Wilson 		goto check_page_flip;
381190a72f87SVille Syrjälä 
38127d47559eSVille Syrjälä 	intel_prepare_page_flip(dev, plane);
381390a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
381490a72f87SVille Syrjälä 	return true;
3815d6bbafa1SChris Wilson 
3816d6bbafa1SChris Wilson check_page_flip:
3817d6bbafa1SChris Wilson 	intel_check_page_flip(dev, pipe);
3818d6bbafa1SChris Wilson 	return false;
381990a72f87SVille Syrjälä }
382090a72f87SVille Syrjälä 
3821ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3822c2798b19SChris Wilson {
382345a83f84SDaniel Vetter 	struct drm_device *dev = arg;
38242d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3825c2798b19SChris Wilson 	u16 iir, new_iir;
3826c2798b19SChris Wilson 	u32 pipe_stats[2];
3827c2798b19SChris Wilson 	int pipe;
3828c2798b19SChris Wilson 	u16 flip_mask =
3829c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3830c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3831c2798b19SChris Wilson 
38322dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
38332dd2a883SImre Deak 		return IRQ_NONE;
38342dd2a883SImre Deak 
3835c2798b19SChris Wilson 	iir = I915_READ16(IIR);
3836c2798b19SChris Wilson 	if (iir == 0)
3837c2798b19SChris Wilson 		return IRQ_NONE;
3838c2798b19SChris Wilson 
3839c2798b19SChris Wilson 	while (iir & ~flip_mask) {
3840c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3841c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3842c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3843c2798b19SChris Wilson 		 * interrupts (for non-MSI).
3844c2798b19SChris Wilson 		 */
3845222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
3846c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3847aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
3848c2798b19SChris Wilson 
3849055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
3850c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
3851c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3852c2798b19SChris Wilson 
3853c2798b19SChris Wilson 			/*
3854c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3855c2798b19SChris Wilson 			 */
38562d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & 0x8000ffff)
3857c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3858c2798b19SChris Wilson 		}
3859222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
3860c2798b19SChris Wilson 
3861c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
3862c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
3863c2798b19SChris Wilson 
3864c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
386574cdb337SChris Wilson 			notify_ring(&dev_priv->ring[RCS]);
3866c2798b19SChris Wilson 
3867055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
38681f1c2e24SVille Syrjälä 			int plane = pipe;
38693a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
38701f1c2e24SVille Syrjälä 				plane = !plane;
38711f1c2e24SVille Syrjälä 
38724356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
38731f1c2e24SVille Syrjälä 			    i8xx_handle_vblank(dev, plane, pipe, iir))
38741f1c2e24SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3875c2798b19SChris Wilson 
38764356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3877277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
38782d9d2b0bSVille Syrjälä 
38791f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
38801f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
38811f7247c0SDaniel Vetter 								    pipe);
38824356d586SDaniel Vetter 		}
3883c2798b19SChris Wilson 
3884c2798b19SChris Wilson 		iir = new_iir;
3885c2798b19SChris Wilson 	}
3886c2798b19SChris Wilson 
3887c2798b19SChris Wilson 	return IRQ_HANDLED;
3888c2798b19SChris Wilson }
3889c2798b19SChris Wilson 
3890c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
3891c2798b19SChris Wilson {
38922d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3893c2798b19SChris Wilson 	int pipe;
3894c2798b19SChris Wilson 
3895055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
3896c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
3897c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3898c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
3899c2798b19SChris Wilson 	}
3900c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3901c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3902c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
3903c2798b19SChris Wilson }
3904c2798b19SChris Wilson 
3905a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
3906a266c7d5SChris Wilson {
39072d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3908a266c7d5SChris Wilson 	int pipe;
3909a266c7d5SChris Wilson 
3910a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
39110706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
3912a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3913a266c7d5SChris Wilson 	}
3914a266c7d5SChris Wilson 
391500d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
3916055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3917a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3918a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3919a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3920a266c7d5SChris Wilson 	POSTING_READ(IER);
3921a266c7d5SChris Wilson }
3922a266c7d5SChris Wilson 
3923a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
3924a266c7d5SChris Wilson {
39252d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
392638bde180SChris Wilson 	u32 enable_mask;
3927a266c7d5SChris Wilson 
392838bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
392938bde180SChris Wilson 
393038bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
393138bde180SChris Wilson 	dev_priv->irq_mask =
393238bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
393338bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
393438bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
393538bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
393637ef01abSDaniel Vetter 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
393738bde180SChris Wilson 
393838bde180SChris Wilson 	enable_mask =
393938bde180SChris Wilson 		I915_ASLE_INTERRUPT |
394038bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
394138bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
394238bde180SChris Wilson 		I915_USER_INTERRUPT;
394338bde180SChris Wilson 
3944a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
39450706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
394620afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
394720afbda2SDaniel Vetter 
3948a266c7d5SChris Wilson 		/* Enable in IER... */
3949a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
3950a266c7d5SChris Wilson 		/* and unmask in IMR */
3951a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
3952a266c7d5SChris Wilson 	}
3953a266c7d5SChris Wilson 
3954a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3955a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3956a266c7d5SChris Wilson 	POSTING_READ(IER);
3957a266c7d5SChris Wilson 
3958f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
395920afbda2SDaniel Vetter 
3960379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3961379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3962d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3963755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3964755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3965d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3966379ef82dSDaniel Vetter 
396720afbda2SDaniel Vetter 	return 0;
396820afbda2SDaniel Vetter }
396920afbda2SDaniel Vetter 
397090a72f87SVille Syrjälä /*
397190a72f87SVille Syrjälä  * Returns true when a page flip has completed.
397290a72f87SVille Syrjälä  */
397390a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
397490a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
397590a72f87SVille Syrjälä {
39762d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
397790a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
397890a72f87SVille Syrjälä 
39798d7849dbSVille Syrjälä 	if (!intel_pipe_handle_vblank(dev, pipe))
398090a72f87SVille Syrjälä 		return false;
398190a72f87SVille Syrjälä 
398290a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
3983d6bbafa1SChris Wilson 		goto check_page_flip;
398490a72f87SVille Syrjälä 
398590a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
398690a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
398790a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
398890a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
398990a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
399090a72f87SVille Syrjälä 	 */
399190a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
3992d6bbafa1SChris Wilson 		goto check_page_flip;
399390a72f87SVille Syrjälä 
39947d47559eSVille Syrjälä 	intel_prepare_page_flip(dev, plane);
399590a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
399690a72f87SVille Syrjälä 	return true;
3997d6bbafa1SChris Wilson 
3998d6bbafa1SChris Wilson check_page_flip:
3999d6bbafa1SChris Wilson 	intel_check_page_flip(dev, pipe);
4000d6bbafa1SChris Wilson 	return false;
400190a72f87SVille Syrjälä }
400290a72f87SVille Syrjälä 
4003ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
4004a266c7d5SChris Wilson {
400545a83f84SDaniel Vetter 	struct drm_device *dev = arg;
40062d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
40078291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
400838bde180SChris Wilson 	u32 flip_mask =
400938bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
401038bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
401138bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
4012a266c7d5SChris Wilson 
40132dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
40142dd2a883SImre Deak 		return IRQ_NONE;
40152dd2a883SImre Deak 
4016a266c7d5SChris Wilson 	iir = I915_READ(IIR);
401738bde180SChris Wilson 	do {
401838bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
40198291ee90SChris Wilson 		bool blc_event = false;
4020a266c7d5SChris Wilson 
4021a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4022a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4023a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4024a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
4025a266c7d5SChris Wilson 		 */
4026222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
4027a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4028aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4029a266c7d5SChris Wilson 
4030055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4031a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
4032a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4033a266c7d5SChris Wilson 
403438bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
4035a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4036a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
403738bde180SChris Wilson 				irq_received = true;
4038a266c7d5SChris Wilson 			}
4039a266c7d5SChris Wilson 		}
4040222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4041a266c7d5SChris Wilson 
4042a266c7d5SChris Wilson 		if (!irq_received)
4043a266c7d5SChris Wilson 			break;
4044a266c7d5SChris Wilson 
4045a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
404616c6c56bSVille Syrjälä 		if (I915_HAS_HOTPLUG(dev) &&
404716c6c56bSVille Syrjälä 		    iir & I915_DISPLAY_PORT_INTERRUPT)
404816c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
4049a266c7d5SChris Wilson 
405038bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
4051a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4052a266c7d5SChris Wilson 
4053a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
405474cdb337SChris Wilson 			notify_ring(&dev_priv->ring[RCS]);
4055a266c7d5SChris Wilson 
4056055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
405738bde180SChris Wilson 			int plane = pipe;
40583a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
405938bde180SChris Wilson 				plane = !plane;
40605e2032d4SVille Syrjälä 
406190a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
406290a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
406390a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
4064a266c7d5SChris Wilson 
4065a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4066a266c7d5SChris Wilson 				blc_event = true;
40674356d586SDaniel Vetter 
40684356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
4069277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
40702d9d2b0bSVille Syrjälä 
40711f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
40721f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
40731f7247c0SDaniel Vetter 								    pipe);
4074a266c7d5SChris Wilson 		}
4075a266c7d5SChris Wilson 
4076a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
4077a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
4078a266c7d5SChris Wilson 
4079a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4080a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4081a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4082a266c7d5SChris Wilson 		 * we would never get another interrupt.
4083a266c7d5SChris Wilson 		 *
4084a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4085a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4086a266c7d5SChris Wilson 		 * another one.
4087a266c7d5SChris Wilson 		 *
4088a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4089a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4090a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4091a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4092a266c7d5SChris Wilson 		 * stray interrupts.
4093a266c7d5SChris Wilson 		 */
409438bde180SChris Wilson 		ret = IRQ_HANDLED;
4095a266c7d5SChris Wilson 		iir = new_iir;
409638bde180SChris Wilson 	} while (iir & ~flip_mask);
4097a266c7d5SChris Wilson 
4098a266c7d5SChris Wilson 	return ret;
4099a266c7d5SChris Wilson }
4100a266c7d5SChris Wilson 
4101a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
4102a266c7d5SChris Wilson {
41032d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4104a266c7d5SChris Wilson 	int pipe;
4105a266c7d5SChris Wilson 
4106a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
41070706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4108a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4109a266c7d5SChris Wilson 	}
4110a266c7d5SChris Wilson 
411100d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
4112055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
411355b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
4114a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
411555b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
411655b39755SChris Wilson 	}
4117a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4118a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4119a266c7d5SChris Wilson 
4120a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4121a266c7d5SChris Wilson }
4122a266c7d5SChris Wilson 
4123a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
4124a266c7d5SChris Wilson {
41252d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4126a266c7d5SChris Wilson 	int pipe;
4127a266c7d5SChris Wilson 
41280706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4129a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4130a266c7d5SChris Wilson 
4131a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
4132055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4133a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4134a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4135a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4136a266c7d5SChris Wilson 	POSTING_READ(IER);
4137a266c7d5SChris Wilson }
4138a266c7d5SChris Wilson 
4139a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
4140a266c7d5SChris Wilson {
41412d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4142bbba0a97SChris Wilson 	u32 enable_mask;
4143a266c7d5SChris Wilson 	u32 error_mask;
4144a266c7d5SChris Wilson 
4145a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
4146bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
4147adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
4148bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4149bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
4150bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
4151bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
4152bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
4153bbba0a97SChris Wilson 
4154bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
415521ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
415621ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
4157bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
4158bbba0a97SChris Wilson 
4159bbba0a97SChris Wilson 	if (IS_G4X(dev))
4160bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
4161a266c7d5SChris Wilson 
4162b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4163b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4164d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4165755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
4166755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4167755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4168d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4169a266c7d5SChris Wilson 
4170a266c7d5SChris Wilson 	/*
4171a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
4172a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
4173a266c7d5SChris Wilson 	 */
4174a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
4175a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
4176a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
4177a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
4178a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4179a266c7d5SChris Wilson 	} else {
4180a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
4181a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4182a266c7d5SChris Wilson 	}
4183a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
4184a266c7d5SChris Wilson 
4185a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
4186a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
4187a266c7d5SChris Wilson 	POSTING_READ(IER);
4188a266c7d5SChris Wilson 
41890706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
419020afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
419120afbda2SDaniel Vetter 
4192f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
419320afbda2SDaniel Vetter 
419420afbda2SDaniel Vetter 	return 0;
419520afbda2SDaniel Vetter }
419620afbda2SDaniel Vetter 
4197bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
419820afbda2SDaniel Vetter {
41992d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
420020afbda2SDaniel Vetter 	u32 hotplug_en;
420120afbda2SDaniel Vetter 
4202b5ea2d56SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
4203b5ea2d56SDaniel Vetter 
4204adca4730SChris Wilson 	/* Note HDMI and DP share hotplug bits */
4205e5868a31SEgbert Eich 	/* enable bits are the same for all generations */
42060706f17cSEgbert Eich 	hotplug_en = intel_hpd_enabled_irqs(dev, hpd_mask_i915);
4207a266c7d5SChris Wilson 	/* Programming the CRT detection parameters tends
4208a266c7d5SChris Wilson 	   to generate a spurious hotplug event about three
4209a266c7d5SChris Wilson 	   seconds later.  So just do it once.
4210a266c7d5SChris Wilson 	*/
4211a266c7d5SChris Wilson 	if (IS_G4X(dev))
4212a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
4213a266c7d5SChris Wilson 	hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
4214a266c7d5SChris Wilson 
4215a266c7d5SChris Wilson 	/* Ignore TV since it's buggy */
42160706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv,
42170706f17cSEgbert Eich 				      (HOTPLUG_INT_EN_MASK
42180706f17cSEgbert Eich 				       | CRT_HOTPLUG_VOLTAGE_COMPARE_MASK),
42190706f17cSEgbert Eich 				      hotplug_en);
4220a266c7d5SChris Wilson }
4221a266c7d5SChris Wilson 
4222ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
4223a266c7d5SChris Wilson {
422445a83f84SDaniel Vetter 	struct drm_device *dev = arg;
42252d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4226a266c7d5SChris Wilson 	u32 iir, new_iir;
4227a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
4228a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
422921ad8330SVille Syrjälä 	u32 flip_mask =
423021ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
423121ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
4232a266c7d5SChris Wilson 
42332dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
42342dd2a883SImre Deak 		return IRQ_NONE;
42352dd2a883SImre Deak 
4236a266c7d5SChris Wilson 	iir = I915_READ(IIR);
4237a266c7d5SChris Wilson 
4238a266c7d5SChris Wilson 	for (;;) {
4239501e01d7SVille Syrjälä 		bool irq_received = (iir & ~flip_mask) != 0;
42402c8ba29fSChris Wilson 		bool blc_event = false;
42412c8ba29fSChris Wilson 
4242a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4243a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4244a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4245a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
4246a266c7d5SChris Wilson 		 */
4247222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
4248a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4249aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4250a266c7d5SChris Wilson 
4251055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4252a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
4253a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4254a266c7d5SChris Wilson 
4255a266c7d5SChris Wilson 			/*
4256a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
4257a266c7d5SChris Wilson 			 */
4258a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4259a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
4260501e01d7SVille Syrjälä 				irq_received = true;
4261a266c7d5SChris Wilson 			}
4262a266c7d5SChris Wilson 		}
4263222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4264a266c7d5SChris Wilson 
4265a266c7d5SChris Wilson 		if (!irq_received)
4266a266c7d5SChris Wilson 			break;
4267a266c7d5SChris Wilson 
4268a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
4269a266c7d5SChris Wilson 
4270a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
427116c6c56bSVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
427216c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
4273a266c7d5SChris Wilson 
427421ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
4275a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4276a266c7d5SChris Wilson 
4277a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
427874cdb337SChris Wilson 			notify_ring(&dev_priv->ring[RCS]);
4279a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
428074cdb337SChris Wilson 			notify_ring(&dev_priv->ring[VCS]);
4281a266c7d5SChris Wilson 
4282055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
42832c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
428490a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
428590a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
4286a266c7d5SChris Wilson 
4287a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4288a266c7d5SChris Wilson 				blc_event = true;
42894356d586SDaniel Vetter 
42904356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
4291277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
4292a266c7d5SChris Wilson 
42931f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
42941f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
42952d9d2b0bSVille Syrjälä 		}
4296a266c7d5SChris Wilson 
4297a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
4298a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
4299a266c7d5SChris Wilson 
4300515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
4301515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
4302515ac2bbSDaniel Vetter 
4303a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4304a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4305a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4306a266c7d5SChris Wilson 		 * we would never get another interrupt.
4307a266c7d5SChris Wilson 		 *
4308a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4309a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4310a266c7d5SChris Wilson 		 * another one.
4311a266c7d5SChris Wilson 		 *
4312a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4313a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4314a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4315a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4316a266c7d5SChris Wilson 		 * stray interrupts.
4317a266c7d5SChris Wilson 		 */
4318a266c7d5SChris Wilson 		iir = new_iir;
4319a266c7d5SChris Wilson 	}
4320a266c7d5SChris Wilson 
4321a266c7d5SChris Wilson 	return ret;
4322a266c7d5SChris Wilson }
4323a266c7d5SChris Wilson 
4324a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
4325a266c7d5SChris Wilson {
43262d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4327a266c7d5SChris Wilson 	int pipe;
4328a266c7d5SChris Wilson 
4329a266c7d5SChris Wilson 	if (!dev_priv)
4330a266c7d5SChris Wilson 		return;
4331a266c7d5SChris Wilson 
43320706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4333a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4334a266c7d5SChris Wilson 
4335a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
4336055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4337a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4338a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4339a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4340a266c7d5SChris Wilson 
4341055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4342a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
4343a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
4344a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4345a266c7d5SChris Wilson }
4346a266c7d5SChris Wilson 
4347fca52a55SDaniel Vetter /**
4348fca52a55SDaniel Vetter  * intel_irq_init - initializes irq support
4349fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4350fca52a55SDaniel Vetter  *
4351fca52a55SDaniel Vetter  * This function initializes all the irq support including work items, timers
4352fca52a55SDaniel Vetter  * and all the vtables. It does not setup the interrupt itself though.
4353fca52a55SDaniel Vetter  */
4354b963291cSDaniel Vetter void intel_irq_init(struct drm_i915_private *dev_priv)
4355f71d4af4SJesse Barnes {
4356b963291cSDaniel Vetter 	struct drm_device *dev = dev_priv->dev;
43578b2e326dSChris Wilson 
435877913b39SJani Nikula 	intel_hpd_init_work(dev_priv);
435977913b39SJani Nikula 
4360c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
4361a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
43628b2e326dSChris Wilson 
4363a6706b45SDeepak S 	/* Let's track the enabled rps events */
4364b963291cSDaniel Vetter 	if (IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv))
43656c65a587SVille Syrjälä 		/* WaGsvRC0ResidencyMethod:vlv */
43666f4b12f8SChris Wilson 		dev_priv->pm_rps_events = GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED;
436731685c25SDeepak S 	else
4368a6706b45SDeepak S 		dev_priv->pm_rps_events = GEN6_PM_RPS_EVENTS;
4369a6706b45SDeepak S 
4370737b1506SChris Wilson 	INIT_DELAYED_WORK(&dev_priv->gpu_error.hangcheck_work,
4371737b1506SChris Wilson 			  i915_hangcheck_elapsed);
437261bac78eSDaniel Vetter 
437397a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
43749ee32feaSDaniel Vetter 
4375b963291cSDaniel Vetter 	if (IS_GEN2(dev_priv)) {
43764cdb83ecSVille Syrjälä 		dev->max_vblank_count = 0;
43774cdb83ecSVille Syrjälä 		dev->driver->get_vblank_counter = i8xx_get_vblank_counter;
4378b963291cSDaniel Vetter 	} else if (IS_G4X(dev_priv) || INTEL_INFO(dev_priv)->gen >= 5) {
4379f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
4380f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
4381391f75e2SVille Syrjälä 	} else {
4382391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
4383391f75e2SVille Syrjälä 		dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
4384f71d4af4SJesse Barnes 	}
4385f71d4af4SJesse Barnes 
438621da2700SVille Syrjälä 	/*
438721da2700SVille Syrjälä 	 * Opt out of the vblank disable timer on everything except gen2.
438821da2700SVille Syrjälä 	 * Gen2 doesn't have a hardware frame counter and so depends on
438921da2700SVille Syrjälä 	 * vblank interrupts to produce sane vblank seuquence numbers.
439021da2700SVille Syrjälä 	 */
4391b963291cSDaniel Vetter 	if (!IS_GEN2(dev_priv))
439221da2700SVille Syrjälä 		dev->vblank_disable_immediate = true;
439321da2700SVille Syrjälä 
4394f71d4af4SJesse Barnes 	dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
4395f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
4396f71d4af4SJesse Barnes 
4397b963291cSDaniel Vetter 	if (IS_CHERRYVIEW(dev_priv)) {
439843f328d7SVille Syrjälä 		dev->driver->irq_handler = cherryview_irq_handler;
439943f328d7SVille Syrjälä 		dev->driver->irq_preinstall = cherryview_irq_preinstall;
440043f328d7SVille Syrjälä 		dev->driver->irq_postinstall = cherryview_irq_postinstall;
440143f328d7SVille Syrjälä 		dev->driver->irq_uninstall = cherryview_irq_uninstall;
440243f328d7SVille Syrjälä 		dev->driver->enable_vblank = valleyview_enable_vblank;
440343f328d7SVille Syrjälä 		dev->driver->disable_vblank = valleyview_disable_vblank;
440443f328d7SVille Syrjälä 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4405b963291cSDaniel Vetter 	} else if (IS_VALLEYVIEW(dev_priv)) {
44067e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
44077e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
44087e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
44097e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
44107e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
44117e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
4412fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4413b963291cSDaniel Vetter 	} else if (INTEL_INFO(dev_priv)->gen >= 8) {
4414abd58f01SBen Widawsky 		dev->driver->irq_handler = gen8_irq_handler;
4415723761b8SDaniel Vetter 		dev->driver->irq_preinstall = gen8_irq_reset;
4416abd58f01SBen Widawsky 		dev->driver->irq_postinstall = gen8_irq_postinstall;
4417abd58f01SBen Widawsky 		dev->driver->irq_uninstall = gen8_irq_uninstall;
4418abd58f01SBen Widawsky 		dev->driver->enable_vblank = gen8_enable_vblank;
4419abd58f01SBen Widawsky 		dev->driver->disable_vblank = gen8_disable_vblank;
44206dbf30ceSVille Syrjälä 		if (IS_BROXTON(dev))
4421e0a20ad7SShashank Sharma 			dev_priv->display.hpd_irq_setup = bxt_hpd_irq_setup;
44226dbf30ceSVille Syrjälä 		else if (HAS_PCH_SPT(dev))
44236dbf30ceSVille Syrjälä 			dev_priv->display.hpd_irq_setup = spt_hpd_irq_setup;
44246dbf30ceSVille Syrjälä 		else
44253a3b3c7dSVille Syrjälä 			dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
4426f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
4427f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
4428723761b8SDaniel Vetter 		dev->driver->irq_preinstall = ironlake_irq_reset;
4429f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
4430f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
4431f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
4432f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
4433e4ce95aaSVille Syrjälä 		dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
4434f71d4af4SJesse Barnes 	} else {
4435b963291cSDaniel Vetter 		if (INTEL_INFO(dev_priv)->gen == 2) {
4436c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
4437c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
4438c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
4439c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
4440b963291cSDaniel Vetter 		} else if (INTEL_INFO(dev_priv)->gen == 3) {
4441a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
4442a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
4443a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
4444a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
4445c2798b19SChris Wilson 		} else {
4446a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
4447a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
4448a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
4449a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
4450c2798b19SChris Wilson 		}
4451778eb334SVille Syrjälä 		if (I915_HAS_HOTPLUG(dev_priv))
4452778eb334SVille Syrjälä 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4453f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
4454f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
4455f71d4af4SJesse Barnes 	}
4456f71d4af4SJesse Barnes }
445720afbda2SDaniel Vetter 
4458fca52a55SDaniel Vetter /**
4459fca52a55SDaniel Vetter  * intel_irq_install - enables the hardware interrupt
4460fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4461fca52a55SDaniel Vetter  *
4462fca52a55SDaniel Vetter  * This function enables the hardware interrupt handling, but leaves the hotplug
4463fca52a55SDaniel Vetter  * handling still disabled. It is called after intel_irq_init().
4464fca52a55SDaniel Vetter  *
4465fca52a55SDaniel Vetter  * In the driver load and resume code we need working interrupts in a few places
4466fca52a55SDaniel Vetter  * but don't want to deal with the hassle of concurrent probe and hotplug
4467fca52a55SDaniel Vetter  * workers. Hence the split into this two-stage approach.
4468fca52a55SDaniel Vetter  */
44692aeb7d3aSDaniel Vetter int intel_irq_install(struct drm_i915_private *dev_priv)
44702aeb7d3aSDaniel Vetter {
44712aeb7d3aSDaniel Vetter 	/*
44722aeb7d3aSDaniel Vetter 	 * We enable some interrupt sources in our postinstall hooks, so mark
44732aeb7d3aSDaniel Vetter 	 * interrupts as enabled _before_ actually enabling them to avoid
44742aeb7d3aSDaniel Vetter 	 * special cases in our ordering checks.
44752aeb7d3aSDaniel Vetter 	 */
44762aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
44772aeb7d3aSDaniel Vetter 
44782aeb7d3aSDaniel Vetter 	return drm_irq_install(dev_priv->dev, dev_priv->dev->pdev->irq);
44792aeb7d3aSDaniel Vetter }
44802aeb7d3aSDaniel Vetter 
4481fca52a55SDaniel Vetter /**
4482fca52a55SDaniel Vetter  * intel_irq_uninstall - finilizes all irq handling
4483fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4484fca52a55SDaniel Vetter  *
4485fca52a55SDaniel Vetter  * This stops interrupt and hotplug handling and unregisters and frees all
4486fca52a55SDaniel Vetter  * resources acquired in the init functions.
4487fca52a55SDaniel Vetter  */
44882aeb7d3aSDaniel Vetter void intel_irq_uninstall(struct drm_i915_private *dev_priv)
44892aeb7d3aSDaniel Vetter {
44902aeb7d3aSDaniel Vetter 	drm_irq_uninstall(dev_priv->dev);
44912aeb7d3aSDaniel Vetter 	intel_hpd_cancel_work(dev_priv);
44922aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
44932aeb7d3aSDaniel Vetter }
44942aeb7d3aSDaniel Vetter 
4495fca52a55SDaniel Vetter /**
4496fca52a55SDaniel Vetter  * intel_runtime_pm_disable_interrupts - runtime interrupt disabling
4497fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4498fca52a55SDaniel Vetter  *
4499fca52a55SDaniel Vetter  * This function is used to disable interrupts at runtime, both in the runtime
4500fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4501fca52a55SDaniel Vetter  */
4502b963291cSDaniel Vetter void intel_runtime_pm_disable_interrupts(struct drm_i915_private *dev_priv)
4503c67a470bSPaulo Zanoni {
4504b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_uninstall(dev_priv->dev);
45052aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
45062dd2a883SImre Deak 	synchronize_irq(dev_priv->dev->irq);
4507c67a470bSPaulo Zanoni }
4508c67a470bSPaulo Zanoni 
4509fca52a55SDaniel Vetter /**
4510fca52a55SDaniel Vetter  * intel_runtime_pm_enable_interrupts - runtime interrupt enabling
4511fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4512fca52a55SDaniel Vetter  *
4513fca52a55SDaniel Vetter  * This function is used to enable interrupts at runtime, both in the runtime
4514fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4515fca52a55SDaniel Vetter  */
4516b963291cSDaniel Vetter void intel_runtime_pm_enable_interrupts(struct drm_i915_private *dev_priv)
4517c67a470bSPaulo Zanoni {
45182aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
4519b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_preinstall(dev_priv->dev);
4520b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_postinstall(dev_priv->dev);
4521c67a470bSPaulo Zanoni }
4522