xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 31de73501ac92ef62971d4b7f6c941508422f3af)
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  */
142f0f59a00SVille Syrjälä static void gen5_assert_iir_is_zero(struct drm_i915_private *dev_priv,
143f0f59a00SVille Syrjälä 				    i915_reg_t reg)
144b51a2842SVille Syrjälä {
145b51a2842SVille Syrjälä 	u32 val = I915_READ(reg);
146b51a2842SVille Syrjälä 
147b51a2842SVille Syrjälä 	if (val == 0)
148b51a2842SVille Syrjälä 		return;
149b51a2842SVille Syrjälä 
150b51a2842SVille Syrjälä 	WARN(1, "Interrupt register 0x%x is not zero: 0x%08x\n",
151f0f59a00SVille Syrjälä 	     i915_mmio_reg_offset(reg), val);
152b51a2842SVille Syrjälä 	I915_WRITE(reg, 0xffffffff);
153b51a2842SVille Syrjälä 	POSTING_READ(reg);
154b51a2842SVille Syrjälä 	I915_WRITE(reg, 0xffffffff);
155b51a2842SVille Syrjälä 	POSTING_READ(reg);
156b51a2842SVille Syrjälä }
157337ba017SPaulo Zanoni 
15835079899SPaulo Zanoni #define GEN8_IRQ_INIT_NDX(type, which, imr_val, ier_val) do { \
159b51a2842SVille Syrjälä 	gen5_assert_iir_is_zero(dev_priv, GEN8_##type##_IIR(which)); \
16035079899SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IER(which), (ier_val)); \
1617d1bd539SVille Syrjälä 	I915_WRITE(GEN8_##type##_IMR(which), (imr_val)); \
1627d1bd539SVille Syrjälä 	POSTING_READ(GEN8_##type##_IMR(which)); \
16335079899SPaulo Zanoni } while (0)
16435079899SPaulo Zanoni 
16535079899SPaulo Zanoni #define GEN5_IRQ_INIT(type, imr_val, ier_val) do { \
166b51a2842SVille Syrjälä 	gen5_assert_iir_is_zero(dev_priv, type##IIR); \
16735079899SPaulo Zanoni 	I915_WRITE(type##IER, (ier_val)); \
1687d1bd539SVille Syrjälä 	I915_WRITE(type##IMR, (imr_val)); \
1697d1bd539SVille Syrjälä 	POSTING_READ(type##IMR); \
17035079899SPaulo Zanoni } while (0)
17135079899SPaulo Zanoni 
172c9a9a268SImre Deak static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir);
17326705e20SSagar Arun Kamble static void gen9_guc_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir);
174c9a9a268SImre Deak 
1750706f17cSEgbert Eich /* For display hotplug interrupt */
1760706f17cSEgbert Eich static inline void
1770706f17cSEgbert Eich i915_hotplug_interrupt_update_locked(struct drm_i915_private *dev_priv,
1780706f17cSEgbert Eich 				     uint32_t mask,
1790706f17cSEgbert Eich 				     uint32_t bits)
1800706f17cSEgbert Eich {
1810706f17cSEgbert Eich 	uint32_t val;
1820706f17cSEgbert Eich 
18367520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
1840706f17cSEgbert Eich 	WARN_ON(bits & ~mask);
1850706f17cSEgbert Eich 
1860706f17cSEgbert Eich 	val = I915_READ(PORT_HOTPLUG_EN);
1870706f17cSEgbert Eich 	val &= ~mask;
1880706f17cSEgbert Eich 	val |= bits;
1890706f17cSEgbert Eich 	I915_WRITE(PORT_HOTPLUG_EN, val);
1900706f17cSEgbert Eich }
1910706f17cSEgbert Eich 
1920706f17cSEgbert Eich /**
1930706f17cSEgbert Eich  * i915_hotplug_interrupt_update - update hotplug interrupt enable
1940706f17cSEgbert Eich  * @dev_priv: driver private
1950706f17cSEgbert Eich  * @mask: bits to update
1960706f17cSEgbert Eich  * @bits: bits to enable
1970706f17cSEgbert Eich  * NOTE: the HPD enable bits are modified both inside and outside
1980706f17cSEgbert Eich  * of an interrupt context. To avoid that read-modify-write cycles
1990706f17cSEgbert Eich  * interfer, these bits are protected by a spinlock. Since this
2000706f17cSEgbert Eich  * function is usually not called from a context where the lock is
2010706f17cSEgbert Eich  * held already, this function acquires the lock itself. A non-locking
2020706f17cSEgbert Eich  * version is also available.
2030706f17cSEgbert Eich  */
2040706f17cSEgbert Eich void i915_hotplug_interrupt_update(struct drm_i915_private *dev_priv,
2050706f17cSEgbert Eich 				   uint32_t mask,
2060706f17cSEgbert Eich 				   uint32_t bits)
2070706f17cSEgbert Eich {
2080706f17cSEgbert Eich 	spin_lock_irq(&dev_priv->irq_lock);
2090706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv, mask, bits);
2100706f17cSEgbert Eich 	spin_unlock_irq(&dev_priv->irq_lock);
2110706f17cSEgbert Eich }
2120706f17cSEgbert Eich 
213d9dc34f1SVille Syrjälä /**
214d9dc34f1SVille Syrjälä  * ilk_update_display_irq - update DEIMR
215d9dc34f1SVille Syrjälä  * @dev_priv: driver private
216d9dc34f1SVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
217d9dc34f1SVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
218d9dc34f1SVille Syrjälä  */
219fbdedaeaSVille Syrjälä void ilk_update_display_irq(struct drm_i915_private *dev_priv,
220d9dc34f1SVille Syrjälä 			    uint32_t interrupt_mask,
221d9dc34f1SVille Syrjälä 			    uint32_t enabled_irq_mask)
222036a4a7dSZhenyu Wang {
223d9dc34f1SVille Syrjälä 	uint32_t new_val;
224d9dc34f1SVille Syrjälä 
22567520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
2264bc9d430SDaniel Vetter 
227d9dc34f1SVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
228d9dc34f1SVille Syrjälä 
2299df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
230c67a470bSPaulo Zanoni 		return;
231c67a470bSPaulo Zanoni 
232d9dc34f1SVille Syrjälä 	new_val = dev_priv->irq_mask;
233d9dc34f1SVille Syrjälä 	new_val &= ~interrupt_mask;
234d9dc34f1SVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
235d9dc34f1SVille Syrjälä 
236d9dc34f1SVille Syrjälä 	if (new_val != dev_priv->irq_mask) {
237d9dc34f1SVille Syrjälä 		dev_priv->irq_mask = new_val;
2381ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
2393143a2bfSChris Wilson 		POSTING_READ(DEIMR);
240036a4a7dSZhenyu Wang 	}
241036a4a7dSZhenyu Wang }
242036a4a7dSZhenyu Wang 
24343eaea13SPaulo Zanoni /**
24443eaea13SPaulo Zanoni  * ilk_update_gt_irq - update GTIMR
24543eaea13SPaulo Zanoni  * @dev_priv: driver private
24643eaea13SPaulo Zanoni  * @interrupt_mask: mask of interrupt bits to update
24743eaea13SPaulo Zanoni  * @enabled_irq_mask: mask of interrupt bits to enable
24843eaea13SPaulo Zanoni  */
24943eaea13SPaulo Zanoni static void ilk_update_gt_irq(struct drm_i915_private *dev_priv,
25043eaea13SPaulo Zanoni 			      uint32_t interrupt_mask,
25143eaea13SPaulo Zanoni 			      uint32_t enabled_irq_mask)
25243eaea13SPaulo Zanoni {
25367520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
25443eaea13SPaulo Zanoni 
25515a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
25615a17aaeSDaniel Vetter 
2579df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
258c67a470bSPaulo Zanoni 		return;
259c67a470bSPaulo Zanoni 
26043eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask &= ~interrupt_mask;
26143eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask |= (~enabled_irq_mask & interrupt_mask);
26243eaea13SPaulo Zanoni 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
26343eaea13SPaulo Zanoni }
26443eaea13SPaulo Zanoni 
265480c8033SDaniel Vetter void gen5_enable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
26643eaea13SPaulo Zanoni {
26743eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, mask);
26831bb59ccSChris Wilson 	POSTING_READ_FW(GTIMR);
26943eaea13SPaulo Zanoni }
27043eaea13SPaulo Zanoni 
271480c8033SDaniel Vetter void gen5_disable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
27243eaea13SPaulo Zanoni {
27343eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, 0);
27443eaea13SPaulo Zanoni }
27543eaea13SPaulo Zanoni 
276f0f59a00SVille Syrjälä static i915_reg_t gen6_pm_iir(struct drm_i915_private *dev_priv)
277b900b949SImre Deak {
278b900b949SImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IIR(2) : GEN6_PMIIR;
279b900b949SImre Deak }
280b900b949SImre Deak 
281f0f59a00SVille Syrjälä static i915_reg_t gen6_pm_imr(struct drm_i915_private *dev_priv)
282a72fbc3aSImre Deak {
283a72fbc3aSImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IMR(2) : GEN6_PMIMR;
284a72fbc3aSImre Deak }
285a72fbc3aSImre Deak 
286f0f59a00SVille Syrjälä static i915_reg_t gen6_pm_ier(struct drm_i915_private *dev_priv)
287b900b949SImre Deak {
288b900b949SImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IER(2) : GEN6_PMIER;
289b900b949SImre Deak }
290b900b949SImre Deak 
291edbfdb45SPaulo Zanoni /**
292edbfdb45SPaulo Zanoni  * snb_update_pm_irq - update GEN6_PMIMR
293edbfdb45SPaulo Zanoni  * @dev_priv: driver private
294edbfdb45SPaulo Zanoni  * @interrupt_mask: mask of interrupt bits to update
295edbfdb45SPaulo Zanoni  * @enabled_irq_mask: mask of interrupt bits to enable
296edbfdb45SPaulo Zanoni  */
297edbfdb45SPaulo Zanoni static void snb_update_pm_irq(struct drm_i915_private *dev_priv,
298edbfdb45SPaulo Zanoni 			      uint32_t interrupt_mask,
299edbfdb45SPaulo Zanoni 			      uint32_t enabled_irq_mask)
300edbfdb45SPaulo Zanoni {
301605cd25bSPaulo Zanoni 	uint32_t new_val;
302edbfdb45SPaulo Zanoni 
30315a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
30415a17aaeSDaniel Vetter 
30567520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
306edbfdb45SPaulo Zanoni 
307f4e9af4fSAkash Goel 	new_val = dev_priv->pm_imr;
308f52ecbcfSPaulo Zanoni 	new_val &= ~interrupt_mask;
309f52ecbcfSPaulo Zanoni 	new_val |= (~enabled_irq_mask & interrupt_mask);
310f52ecbcfSPaulo Zanoni 
311f4e9af4fSAkash Goel 	if (new_val != dev_priv->pm_imr) {
312f4e9af4fSAkash Goel 		dev_priv->pm_imr = new_val;
313f4e9af4fSAkash Goel 		I915_WRITE(gen6_pm_imr(dev_priv), dev_priv->pm_imr);
314a72fbc3aSImre Deak 		POSTING_READ(gen6_pm_imr(dev_priv));
315edbfdb45SPaulo Zanoni 	}
316f52ecbcfSPaulo Zanoni }
317edbfdb45SPaulo Zanoni 
318f4e9af4fSAkash Goel void gen6_unmask_pm_irq(struct drm_i915_private *dev_priv, u32 mask)
319edbfdb45SPaulo Zanoni {
3209939fba2SImre Deak 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
3219939fba2SImre Deak 		return;
3229939fba2SImre Deak 
323edbfdb45SPaulo Zanoni 	snb_update_pm_irq(dev_priv, mask, mask);
324edbfdb45SPaulo Zanoni }
325edbfdb45SPaulo Zanoni 
326f4e9af4fSAkash Goel static void __gen6_mask_pm_irq(struct drm_i915_private *dev_priv, u32 mask)
3279939fba2SImre Deak {
3289939fba2SImre Deak 	snb_update_pm_irq(dev_priv, mask, 0);
3299939fba2SImre Deak }
3309939fba2SImre Deak 
331f4e9af4fSAkash Goel void gen6_mask_pm_irq(struct drm_i915_private *dev_priv, u32 mask)
332edbfdb45SPaulo Zanoni {
3339939fba2SImre Deak 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
3349939fba2SImre Deak 		return;
3359939fba2SImre Deak 
336f4e9af4fSAkash Goel 	__gen6_mask_pm_irq(dev_priv, mask);
337f4e9af4fSAkash Goel }
338f4e9af4fSAkash Goel 
339f4e9af4fSAkash Goel void gen6_reset_pm_iir(struct drm_i915_private *dev_priv, u32 reset_mask)
340f4e9af4fSAkash Goel {
341f4e9af4fSAkash Goel 	i915_reg_t reg = gen6_pm_iir(dev_priv);
342f4e9af4fSAkash Goel 
34367520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
344f4e9af4fSAkash Goel 
345f4e9af4fSAkash Goel 	I915_WRITE(reg, reset_mask);
346f4e9af4fSAkash Goel 	I915_WRITE(reg, reset_mask);
347f4e9af4fSAkash Goel 	POSTING_READ(reg);
348f4e9af4fSAkash Goel }
349f4e9af4fSAkash Goel 
350f4e9af4fSAkash Goel void gen6_enable_pm_irq(struct drm_i915_private *dev_priv, u32 enable_mask)
351f4e9af4fSAkash Goel {
35267520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
353f4e9af4fSAkash Goel 
354f4e9af4fSAkash Goel 	dev_priv->pm_ier |= enable_mask;
355f4e9af4fSAkash Goel 	I915_WRITE(gen6_pm_ier(dev_priv), dev_priv->pm_ier);
356f4e9af4fSAkash Goel 	gen6_unmask_pm_irq(dev_priv, enable_mask);
357f4e9af4fSAkash Goel 	/* unmask_pm_irq provides an implicit barrier (POSTING_READ) */
358f4e9af4fSAkash Goel }
359f4e9af4fSAkash Goel 
360f4e9af4fSAkash Goel void gen6_disable_pm_irq(struct drm_i915_private *dev_priv, u32 disable_mask)
361f4e9af4fSAkash Goel {
36267520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
363f4e9af4fSAkash Goel 
364f4e9af4fSAkash Goel 	dev_priv->pm_ier &= ~disable_mask;
365f4e9af4fSAkash Goel 	__gen6_mask_pm_irq(dev_priv, disable_mask);
366f4e9af4fSAkash Goel 	I915_WRITE(gen6_pm_ier(dev_priv), dev_priv->pm_ier);
367f4e9af4fSAkash Goel 	/* though a barrier is missing here, but don't really need a one */
368edbfdb45SPaulo Zanoni }
369edbfdb45SPaulo Zanoni 
370dc97997aSChris Wilson void gen6_reset_rps_interrupts(struct drm_i915_private *dev_priv)
3713cc134e3SImre Deak {
3723cc134e3SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
373f4e9af4fSAkash Goel 	gen6_reset_pm_iir(dev_priv, dev_priv->pm_rps_events);
374096fad9eSImre Deak 	dev_priv->rps.pm_iir = 0;
3753cc134e3SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
3763cc134e3SImre Deak }
3773cc134e3SImre Deak 
37891d14251STvrtko Ursulin void gen6_enable_rps_interrupts(struct drm_i915_private *dev_priv)
379b900b949SImre Deak {
380f2a91d1aSChris Wilson 	if (READ_ONCE(dev_priv->rps.interrupts_enabled))
381f2a91d1aSChris Wilson 		return;
382f2a91d1aSChris Wilson 
383b900b949SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
384c33d247dSChris Wilson 	WARN_ON_ONCE(dev_priv->rps.pm_iir);
385c33d247dSChris Wilson 	WARN_ON_ONCE(I915_READ(gen6_pm_iir(dev_priv)) & dev_priv->pm_rps_events);
386d4d70aa5SImre Deak 	dev_priv->rps.interrupts_enabled = true;
387b900b949SImre Deak 	gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
38878e68d36SImre Deak 
389b900b949SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
390b900b949SImre Deak }
391b900b949SImre Deak 
39291d14251STvrtko Ursulin void gen6_disable_rps_interrupts(struct drm_i915_private *dev_priv)
393b900b949SImre Deak {
394f2a91d1aSChris Wilson 	if (!READ_ONCE(dev_priv->rps.interrupts_enabled))
395f2a91d1aSChris Wilson 		return;
396f2a91d1aSChris Wilson 
397d4d70aa5SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
398d4d70aa5SImre Deak 	dev_priv->rps.interrupts_enabled = false;
3999939fba2SImre Deak 
400b20e3cfeSDave Gordon 	I915_WRITE(GEN6_PMINTRMSK, gen6_sanitize_rps_pm_mask(dev_priv, ~0u));
4019939fba2SImre Deak 
402f4e9af4fSAkash Goel 	gen6_disable_pm_irq(dev_priv, dev_priv->pm_rps_events);
40358072ccbSImre Deak 
40458072ccbSImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
40591c8a326SChris Wilson 	synchronize_irq(dev_priv->drm.irq);
406c33d247dSChris Wilson 
407c33d247dSChris Wilson 	/* Now that we will not be generating any more work, flush any
408c33d247dSChris Wilson 	 * outsanding tasks. As we are called on the RPS idle path,
409c33d247dSChris Wilson 	 * we will reset the GPU to minimum frequencies, so the current
410c33d247dSChris Wilson 	 * state of the worker can be discarded.
411c33d247dSChris Wilson 	 */
412c33d247dSChris Wilson 	cancel_work_sync(&dev_priv->rps.work);
413c33d247dSChris Wilson 	gen6_reset_rps_interrupts(dev_priv);
414b900b949SImre Deak }
415b900b949SImre Deak 
41626705e20SSagar Arun Kamble void gen9_reset_guc_interrupts(struct drm_i915_private *dev_priv)
41726705e20SSagar Arun Kamble {
41826705e20SSagar Arun Kamble 	spin_lock_irq(&dev_priv->irq_lock);
41926705e20SSagar Arun Kamble 	gen6_reset_pm_iir(dev_priv, dev_priv->pm_guc_events);
42026705e20SSagar Arun Kamble 	spin_unlock_irq(&dev_priv->irq_lock);
42126705e20SSagar Arun Kamble }
42226705e20SSagar Arun Kamble 
42326705e20SSagar Arun Kamble void gen9_enable_guc_interrupts(struct drm_i915_private *dev_priv)
42426705e20SSagar Arun Kamble {
42526705e20SSagar Arun Kamble 	spin_lock_irq(&dev_priv->irq_lock);
42626705e20SSagar Arun Kamble 	if (!dev_priv->guc.interrupts_enabled) {
42726705e20SSagar Arun Kamble 		WARN_ON_ONCE(I915_READ(gen6_pm_iir(dev_priv)) &
42826705e20SSagar Arun Kamble 				       dev_priv->pm_guc_events);
42926705e20SSagar Arun Kamble 		dev_priv->guc.interrupts_enabled = true;
43026705e20SSagar Arun Kamble 		gen6_enable_pm_irq(dev_priv, dev_priv->pm_guc_events);
43126705e20SSagar Arun Kamble 	}
43226705e20SSagar Arun Kamble 	spin_unlock_irq(&dev_priv->irq_lock);
43326705e20SSagar Arun Kamble }
43426705e20SSagar Arun Kamble 
43526705e20SSagar Arun Kamble void gen9_disable_guc_interrupts(struct drm_i915_private *dev_priv)
43626705e20SSagar Arun Kamble {
43726705e20SSagar Arun Kamble 	spin_lock_irq(&dev_priv->irq_lock);
43826705e20SSagar Arun Kamble 	dev_priv->guc.interrupts_enabled = false;
43926705e20SSagar Arun Kamble 
44026705e20SSagar Arun Kamble 	gen6_disable_pm_irq(dev_priv, dev_priv->pm_guc_events);
44126705e20SSagar Arun Kamble 
44226705e20SSagar Arun Kamble 	spin_unlock_irq(&dev_priv->irq_lock);
44326705e20SSagar Arun Kamble 	synchronize_irq(dev_priv->drm.irq);
44426705e20SSagar Arun Kamble 
44526705e20SSagar Arun Kamble 	gen9_reset_guc_interrupts(dev_priv);
44626705e20SSagar Arun Kamble }
44726705e20SSagar Arun Kamble 
4480961021aSBen Widawsky /**
4493a3b3c7dSVille Syrjälä  * bdw_update_port_irq - update DE port interrupt
4503a3b3c7dSVille Syrjälä  * @dev_priv: driver private
4513a3b3c7dSVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
4523a3b3c7dSVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
4533a3b3c7dSVille Syrjälä  */
4543a3b3c7dSVille Syrjälä static void bdw_update_port_irq(struct drm_i915_private *dev_priv,
4553a3b3c7dSVille Syrjälä 				uint32_t interrupt_mask,
4563a3b3c7dSVille Syrjälä 				uint32_t enabled_irq_mask)
4573a3b3c7dSVille Syrjälä {
4583a3b3c7dSVille Syrjälä 	uint32_t new_val;
4593a3b3c7dSVille Syrjälä 	uint32_t old_val;
4603a3b3c7dSVille Syrjälä 
46167520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
4623a3b3c7dSVille Syrjälä 
4633a3b3c7dSVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
4643a3b3c7dSVille Syrjälä 
4653a3b3c7dSVille Syrjälä 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
4663a3b3c7dSVille Syrjälä 		return;
4673a3b3c7dSVille Syrjälä 
4683a3b3c7dSVille Syrjälä 	old_val = I915_READ(GEN8_DE_PORT_IMR);
4693a3b3c7dSVille Syrjälä 
4703a3b3c7dSVille Syrjälä 	new_val = old_val;
4713a3b3c7dSVille Syrjälä 	new_val &= ~interrupt_mask;
4723a3b3c7dSVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
4733a3b3c7dSVille Syrjälä 
4743a3b3c7dSVille Syrjälä 	if (new_val != old_val) {
4753a3b3c7dSVille Syrjälä 		I915_WRITE(GEN8_DE_PORT_IMR, new_val);
4763a3b3c7dSVille Syrjälä 		POSTING_READ(GEN8_DE_PORT_IMR);
4773a3b3c7dSVille Syrjälä 	}
4783a3b3c7dSVille Syrjälä }
4793a3b3c7dSVille Syrjälä 
4803a3b3c7dSVille Syrjälä /**
481013d3752SVille Syrjälä  * bdw_update_pipe_irq - update DE pipe interrupt
482013d3752SVille Syrjälä  * @dev_priv: driver private
483013d3752SVille Syrjälä  * @pipe: pipe whose interrupt to update
484013d3752SVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
485013d3752SVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
486013d3752SVille Syrjälä  */
487013d3752SVille Syrjälä void bdw_update_pipe_irq(struct drm_i915_private *dev_priv,
488013d3752SVille Syrjälä 			 enum pipe pipe,
489013d3752SVille Syrjälä 			 uint32_t interrupt_mask,
490013d3752SVille Syrjälä 			 uint32_t enabled_irq_mask)
491013d3752SVille Syrjälä {
492013d3752SVille Syrjälä 	uint32_t new_val;
493013d3752SVille Syrjälä 
49467520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
495013d3752SVille Syrjälä 
496013d3752SVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
497013d3752SVille Syrjälä 
498013d3752SVille Syrjälä 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
499013d3752SVille Syrjälä 		return;
500013d3752SVille Syrjälä 
501013d3752SVille Syrjälä 	new_val = dev_priv->de_irq_mask[pipe];
502013d3752SVille Syrjälä 	new_val &= ~interrupt_mask;
503013d3752SVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
504013d3752SVille Syrjälä 
505013d3752SVille Syrjälä 	if (new_val != dev_priv->de_irq_mask[pipe]) {
506013d3752SVille Syrjälä 		dev_priv->de_irq_mask[pipe] = new_val;
507013d3752SVille Syrjälä 		I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
508013d3752SVille Syrjälä 		POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
509013d3752SVille Syrjälä 	}
510013d3752SVille Syrjälä }
511013d3752SVille Syrjälä 
512013d3752SVille Syrjälä /**
513fee884edSDaniel Vetter  * ibx_display_interrupt_update - update SDEIMR
514fee884edSDaniel Vetter  * @dev_priv: driver private
515fee884edSDaniel Vetter  * @interrupt_mask: mask of interrupt bits to update
516fee884edSDaniel Vetter  * @enabled_irq_mask: mask of interrupt bits to enable
517fee884edSDaniel Vetter  */
51847339cd9SDaniel Vetter void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
519fee884edSDaniel Vetter 				  uint32_t interrupt_mask,
520fee884edSDaniel Vetter 				  uint32_t enabled_irq_mask)
521fee884edSDaniel Vetter {
522fee884edSDaniel Vetter 	uint32_t sdeimr = I915_READ(SDEIMR);
523fee884edSDaniel Vetter 	sdeimr &= ~interrupt_mask;
524fee884edSDaniel Vetter 	sdeimr |= (~enabled_irq_mask & interrupt_mask);
525fee884edSDaniel Vetter 
52615a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
52715a17aaeSDaniel Vetter 
52867520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
529fee884edSDaniel Vetter 
5309df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
531c67a470bSPaulo Zanoni 		return;
532c67a470bSPaulo Zanoni 
533fee884edSDaniel Vetter 	I915_WRITE(SDEIMR, sdeimr);
534fee884edSDaniel Vetter 	POSTING_READ(SDEIMR);
535fee884edSDaniel Vetter }
5368664281bSPaulo Zanoni 
537b5ea642aSDaniel Vetter static void
538755e9019SImre Deak __i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
539755e9019SImre Deak 		       u32 enable_mask, u32 status_mask)
5407c463586SKeith Packard {
541f0f59a00SVille Syrjälä 	i915_reg_t reg = PIPESTAT(pipe);
542755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
5437c463586SKeith Packard 
54467520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
545d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
546b79480baSDaniel Vetter 
54704feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
54804feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
54904feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
55004feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
551755e9019SImre Deak 		return;
552755e9019SImre Deak 
553755e9019SImre Deak 	if ((pipestat & enable_mask) == enable_mask)
55446c06a30SVille Syrjälä 		return;
55546c06a30SVille Syrjälä 
55691d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] |= status_mask;
55791d181ddSImre Deak 
5587c463586SKeith Packard 	/* Enable the interrupt, clear any pending status */
559755e9019SImre Deak 	pipestat |= enable_mask | status_mask;
56046c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
5613143a2bfSChris Wilson 	POSTING_READ(reg);
5627c463586SKeith Packard }
5637c463586SKeith Packard 
564b5ea642aSDaniel Vetter static void
565755e9019SImre Deak __i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
566755e9019SImre Deak 		        u32 enable_mask, u32 status_mask)
5677c463586SKeith Packard {
568f0f59a00SVille Syrjälä 	i915_reg_t reg = PIPESTAT(pipe);
569755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
5707c463586SKeith Packard 
57167520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
572d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
573b79480baSDaniel Vetter 
57404feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
57504feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
57604feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
57704feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
57846c06a30SVille Syrjälä 		return;
57946c06a30SVille Syrjälä 
580755e9019SImre Deak 	if ((pipestat & enable_mask) == 0)
581755e9019SImre Deak 		return;
582755e9019SImre Deak 
58391d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] &= ~status_mask;
58491d181ddSImre Deak 
585755e9019SImre Deak 	pipestat &= ~enable_mask;
58646c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
5873143a2bfSChris Wilson 	POSTING_READ(reg);
5887c463586SKeith Packard }
5897c463586SKeith Packard 
59010c59c51SImre Deak static u32 vlv_get_pipestat_enable_mask(struct drm_device *dev, u32 status_mask)
59110c59c51SImre Deak {
59210c59c51SImre Deak 	u32 enable_mask = status_mask << 16;
59310c59c51SImre Deak 
59410c59c51SImre Deak 	/*
595724a6905SVille Syrjälä 	 * On pipe A we don't support the PSR interrupt yet,
596724a6905SVille Syrjälä 	 * on pipe B and C the same bit MBZ.
59710c59c51SImre Deak 	 */
59810c59c51SImre Deak 	if (WARN_ON_ONCE(status_mask & PIPE_A_PSR_STATUS_VLV))
59910c59c51SImre Deak 		return 0;
600724a6905SVille Syrjälä 	/*
601724a6905SVille Syrjälä 	 * On pipe B and C we don't support the PSR interrupt yet, on pipe
602724a6905SVille Syrjälä 	 * A the same bit is for perf counters which we don't use either.
603724a6905SVille Syrjälä 	 */
604724a6905SVille Syrjälä 	if (WARN_ON_ONCE(status_mask & PIPE_B_PSR_STATUS_VLV))
605724a6905SVille Syrjälä 		return 0;
60610c59c51SImre Deak 
60710c59c51SImre Deak 	enable_mask &= ~(PIPE_FIFO_UNDERRUN_STATUS |
60810c59c51SImre Deak 			 SPRITE0_FLIP_DONE_INT_EN_VLV |
60910c59c51SImre Deak 			 SPRITE1_FLIP_DONE_INT_EN_VLV);
61010c59c51SImre Deak 	if (status_mask & SPRITE0_FLIP_DONE_INT_STATUS_VLV)
61110c59c51SImre Deak 		enable_mask |= SPRITE0_FLIP_DONE_INT_EN_VLV;
61210c59c51SImre Deak 	if (status_mask & SPRITE1_FLIP_DONE_INT_STATUS_VLV)
61310c59c51SImre Deak 		enable_mask |= SPRITE1_FLIP_DONE_INT_EN_VLV;
61410c59c51SImre Deak 
61510c59c51SImre Deak 	return enable_mask;
61610c59c51SImre Deak }
61710c59c51SImre Deak 
618755e9019SImre Deak void
619755e9019SImre Deak i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
620755e9019SImre Deak 		     u32 status_mask)
621755e9019SImre Deak {
622755e9019SImre Deak 	u32 enable_mask;
623755e9019SImre Deak 
624666a4537SWayne Boyer 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
62591c8a326SChris Wilson 		enable_mask = vlv_get_pipestat_enable_mask(&dev_priv->drm,
62610c59c51SImre Deak 							   status_mask);
62710c59c51SImre Deak 	else
628755e9019SImre Deak 		enable_mask = status_mask << 16;
629755e9019SImre Deak 	__i915_enable_pipestat(dev_priv, pipe, enable_mask, status_mask);
630755e9019SImre Deak }
631755e9019SImre Deak 
632755e9019SImre Deak void
633755e9019SImre Deak i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
634755e9019SImre Deak 		      u32 status_mask)
635755e9019SImre Deak {
636755e9019SImre Deak 	u32 enable_mask;
637755e9019SImre Deak 
638666a4537SWayne Boyer 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
63991c8a326SChris Wilson 		enable_mask = vlv_get_pipestat_enable_mask(&dev_priv->drm,
64010c59c51SImre Deak 							   status_mask);
64110c59c51SImre Deak 	else
642755e9019SImre Deak 		enable_mask = status_mask << 16;
643755e9019SImre Deak 	__i915_disable_pipestat(dev_priv, pipe, enable_mask, status_mask);
644755e9019SImre Deak }
645755e9019SImre Deak 
646c0e09200SDave Airlie /**
647f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
64814bb2c11STvrtko Ursulin  * @dev_priv: i915 device private
64901c66889SZhao Yakui  */
65091d14251STvrtko Ursulin static void i915_enable_asle_pipestat(struct drm_i915_private *dev_priv)
65101c66889SZhao Yakui {
65291d14251STvrtko Ursulin 	if (!dev_priv->opregion.asle || !IS_MOBILE(dev_priv))
653f49e38ddSJani Nikula 		return;
654f49e38ddSJani Nikula 
65513321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
65601c66889SZhao Yakui 
657755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_STATUS);
65891d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 4)
6593b6c42e8SDaniel Vetter 		i915_enable_pipestat(dev_priv, PIPE_A,
660755e9019SImre Deak 				     PIPE_LEGACY_BLC_EVENT_STATUS);
6611ec14ad3SChris Wilson 
66213321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
66301c66889SZhao Yakui }
66401c66889SZhao Yakui 
665f75f3746SVille Syrjälä /*
666f75f3746SVille Syrjälä  * This timing diagram depicts the video signal in and
667f75f3746SVille Syrjälä  * around the vertical blanking period.
668f75f3746SVille Syrjälä  *
669f75f3746SVille Syrjälä  * Assumptions about the fictitious mode used in this example:
670f75f3746SVille Syrjälä  *  vblank_start >= 3
671f75f3746SVille Syrjälä  *  vsync_start = vblank_start + 1
672f75f3746SVille Syrjälä  *  vsync_end = vblank_start + 2
673f75f3746SVille Syrjälä  *  vtotal = vblank_start + 3
674f75f3746SVille Syrjälä  *
675f75f3746SVille Syrjälä  *           start of vblank:
676f75f3746SVille Syrjälä  *           latch double buffered registers
677f75f3746SVille Syrjälä  *           increment frame counter (ctg+)
678f75f3746SVille Syrjälä  *           generate start of vblank interrupt (gen4+)
679f75f3746SVille Syrjälä  *           |
680f75f3746SVille Syrjälä  *           |          frame start:
681f75f3746SVille Syrjälä  *           |          generate frame start interrupt (aka. vblank interrupt) (gmch)
682f75f3746SVille Syrjälä  *           |          may be shifted forward 1-3 extra lines via PIPECONF
683f75f3746SVille Syrjälä  *           |          |
684f75f3746SVille Syrjälä  *           |          |  start of vsync:
685f75f3746SVille Syrjälä  *           |          |  generate vsync interrupt
686f75f3746SVille Syrjälä  *           |          |  |
687f75f3746SVille Syrjälä  * ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx
688f75f3746SVille Syrjälä  *       .   \hs/   .      \hs/          \hs/          \hs/   .      \hs/
689f75f3746SVille Syrjälä  * ----va---> <-----------------vb--------------------> <--------va-------------
690f75f3746SVille Syrjälä  *       |          |       <----vs----->                     |
691f75f3746SVille Syrjälä  * -vbs-----> <---vbs+1---> <---vbs+2---> <-----0-----> <-----1-----> <-----2--- (scanline counter gen2)
692f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2---> <-----0--- (scanline counter gen3+)
693f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2- (scanline counter hsw+ hdmi)
694f75f3746SVille Syrjälä  *       |          |                                         |
695f75f3746SVille Syrjälä  *       last visible pixel                                   first visible pixel
696f75f3746SVille Syrjälä  *                  |                                         increment frame counter (gen3/4)
697f75f3746SVille Syrjälä  *                  pixel counter = vblank_start * htotal     pixel counter = 0 (gen3/4)
698f75f3746SVille Syrjälä  *
699f75f3746SVille Syrjälä  * x  = horizontal active
700f75f3746SVille Syrjälä  * _  = horizontal blanking
701f75f3746SVille Syrjälä  * hs = horizontal sync
702f75f3746SVille Syrjälä  * va = vertical active
703f75f3746SVille Syrjälä  * vb = vertical blanking
704f75f3746SVille Syrjälä  * vs = vertical sync
705f75f3746SVille Syrjälä  * vbs = vblank_start (number)
706f75f3746SVille Syrjälä  *
707f75f3746SVille Syrjälä  * Summary:
708f75f3746SVille Syrjälä  * - most events happen at the start of horizontal sync
709f75f3746SVille Syrjälä  * - frame start happens at the start of horizontal blank, 1-4 lines
710f75f3746SVille Syrjälä  *   (depending on PIPECONF settings) after the start of vblank
711f75f3746SVille Syrjälä  * - gen3/4 pixel and frame counter are synchronized with the start
712f75f3746SVille Syrjälä  *   of horizontal active on the first line of vertical active
713f75f3746SVille Syrjälä  */
714f75f3746SVille Syrjälä 
71542f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
71642f52ef8SKeith Packard  * we use as a pipe index
71742f52ef8SKeith Packard  */
71888e72717SThierry Reding static u32 i915_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
7190a3e67a4SJesse Barnes {
720fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
721f0f59a00SVille Syrjälä 	i915_reg_t high_frame, low_frame;
7220b2a8e09SVille Syrjälä 	u32 high1, high2, low, pixel, vbl_start, hsync_start, htotal;
72398187836SVille Syrjälä 	struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
72498187836SVille Syrjälä 								pipe);
725fc467a22SMaarten Lankhorst 	const struct drm_display_mode *mode = &intel_crtc->base.hwmode;
726694e409dSVille Syrjälä 	unsigned long irqflags;
727391f75e2SVille Syrjälä 
7280b2a8e09SVille Syrjälä 	htotal = mode->crtc_htotal;
7290b2a8e09SVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
7300b2a8e09SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
7310b2a8e09SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
7320b2a8e09SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
733391f75e2SVille Syrjälä 
7340b2a8e09SVille Syrjälä 	/* Convert to pixel count */
7350b2a8e09SVille Syrjälä 	vbl_start *= htotal;
7360b2a8e09SVille Syrjälä 
7370b2a8e09SVille Syrjälä 	/* Start of vblank event occurs at start of hsync */
7380b2a8e09SVille Syrjälä 	vbl_start -= htotal - hsync_start;
7390b2a8e09SVille Syrjälä 
7409db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
7419db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
7425eddb70bSChris Wilson 
743694e409dSVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
744694e409dSVille Syrjälä 
7450a3e67a4SJesse Barnes 	/*
7460a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
7470a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
7480a3e67a4SJesse Barnes 	 * register.
7490a3e67a4SJesse Barnes 	 */
7500a3e67a4SJesse Barnes 	do {
751694e409dSVille Syrjälä 		high1 = I915_READ_FW(high_frame) & PIPE_FRAME_HIGH_MASK;
752694e409dSVille Syrjälä 		low   = I915_READ_FW(low_frame);
753694e409dSVille Syrjälä 		high2 = I915_READ_FW(high_frame) & PIPE_FRAME_HIGH_MASK;
7540a3e67a4SJesse Barnes 	} while (high1 != high2);
7550a3e67a4SJesse Barnes 
756694e409dSVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
757694e409dSVille Syrjälä 
7585eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
759391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
7605eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
761391f75e2SVille Syrjälä 
762391f75e2SVille Syrjälä 	/*
763391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
764391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
765391f75e2SVille Syrjälä 	 * counter against vblank start.
766391f75e2SVille Syrjälä 	 */
767edc08d0aSVille Syrjälä 	return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
7680a3e67a4SJesse Barnes }
7690a3e67a4SJesse Barnes 
770974e59baSDave Airlie static u32 g4x_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
7719880b7a5SJesse Barnes {
772fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
7739880b7a5SJesse Barnes 
774649636efSVille Syrjälä 	return I915_READ(PIPE_FRMCOUNT_G4X(pipe));
7759880b7a5SJesse Barnes }
7769880b7a5SJesse Barnes 
77775aa3f63SVille Syrjälä /* I915_READ_FW, only for fast reads of display block, no need for forcewake etc. */
778a225f079SVille Syrjälä static int __intel_get_crtc_scanline(struct intel_crtc *crtc)
779a225f079SVille Syrjälä {
780a225f079SVille Syrjälä 	struct drm_device *dev = crtc->base.dev;
781fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
782fc467a22SMaarten Lankhorst 	const struct drm_display_mode *mode = &crtc->base.hwmode;
783a225f079SVille Syrjälä 	enum pipe pipe = crtc->pipe;
78480715b2fSVille Syrjälä 	int position, vtotal;
785a225f079SVille Syrjälä 
78672259536SVille Syrjälä 	if (!crtc->active)
78772259536SVille Syrjälä 		return -1;
78872259536SVille Syrjälä 
78980715b2fSVille Syrjälä 	vtotal = mode->crtc_vtotal;
790a225f079SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
791a225f079SVille Syrjälä 		vtotal /= 2;
792a225f079SVille Syrjälä 
79391d14251STvrtko Ursulin 	if (IS_GEN2(dev_priv))
79475aa3f63SVille Syrjälä 		position = I915_READ_FW(PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
795a225f079SVille Syrjälä 	else
79675aa3f63SVille Syrjälä 		position = I915_READ_FW(PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
797a225f079SVille Syrjälä 
798a225f079SVille Syrjälä 	/*
79941b578fbSJesse Barnes 	 * On HSW, the DSL reg (0x70000) appears to return 0 if we
80041b578fbSJesse Barnes 	 * read it just before the start of vblank.  So try it again
80141b578fbSJesse Barnes 	 * so we don't accidentally end up spanning a vblank frame
80241b578fbSJesse Barnes 	 * increment, causing the pipe_update_end() code to squak at us.
80341b578fbSJesse Barnes 	 *
80441b578fbSJesse Barnes 	 * The nature of this problem means we can't simply check the ISR
80541b578fbSJesse Barnes 	 * bit and return the vblank start value; nor can we use the scanline
80641b578fbSJesse Barnes 	 * debug register in the transcoder as it appears to have the same
80741b578fbSJesse Barnes 	 * problem.  We may need to extend this to include other platforms,
80841b578fbSJesse Barnes 	 * but so far testing only shows the problem on HSW.
80941b578fbSJesse Barnes 	 */
81091d14251STvrtko Ursulin 	if (HAS_DDI(dev_priv) && !position) {
81141b578fbSJesse Barnes 		int i, temp;
81241b578fbSJesse Barnes 
81341b578fbSJesse Barnes 		for (i = 0; i < 100; i++) {
81441b578fbSJesse Barnes 			udelay(1);
815707bdd3fSVille Syrjälä 			temp = I915_READ_FW(PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
81641b578fbSJesse Barnes 			if (temp != position) {
81741b578fbSJesse Barnes 				position = temp;
81841b578fbSJesse Barnes 				break;
81941b578fbSJesse Barnes 			}
82041b578fbSJesse Barnes 		}
82141b578fbSJesse Barnes 	}
82241b578fbSJesse Barnes 
82341b578fbSJesse Barnes 	/*
82480715b2fSVille Syrjälä 	 * See update_scanline_offset() for the details on the
82580715b2fSVille Syrjälä 	 * scanline_offset adjustment.
826a225f079SVille Syrjälä 	 */
82780715b2fSVille Syrjälä 	return (position + crtc->scanline_offset) % vtotal;
828a225f079SVille Syrjälä }
829a225f079SVille Syrjälä 
83088e72717SThierry Reding static int i915_get_crtc_scanoutpos(struct drm_device *dev, unsigned int pipe,
831abca9e45SVille Syrjälä 				    unsigned int flags, int *vpos, int *hpos,
8323bb403bfSVille Syrjälä 				    ktime_t *stime, ktime_t *etime,
8333bb403bfSVille Syrjälä 				    const struct drm_display_mode *mode)
8340af7e4dfSMario Kleiner {
835fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
83698187836SVille Syrjälä 	struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
83798187836SVille Syrjälä 								pipe);
8383aa18df8SVille Syrjälä 	int position;
83978e8fc6bSVille Syrjälä 	int vbl_start, vbl_end, hsync_start, htotal, vtotal;
8400af7e4dfSMario Kleiner 	bool in_vbl = true;
8410af7e4dfSMario Kleiner 	int ret = 0;
842ad3543edSMario Kleiner 	unsigned long irqflags;
8430af7e4dfSMario Kleiner 
844fc467a22SMaarten Lankhorst 	if (WARN_ON(!mode->crtc_clock)) {
8450af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
8469db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
8470af7e4dfSMario Kleiner 		return 0;
8480af7e4dfSMario Kleiner 	}
8490af7e4dfSMario Kleiner 
850c2baf4b7SVille Syrjälä 	htotal = mode->crtc_htotal;
85178e8fc6bSVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
852c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
853c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
854c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
8550af7e4dfSMario Kleiner 
856d31faf65SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
857d31faf65SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
858d31faf65SVille Syrjälä 		vbl_end /= 2;
859d31faf65SVille Syrjälä 		vtotal /= 2;
860d31faf65SVille Syrjälä 	}
861d31faf65SVille Syrjälä 
862c2baf4b7SVille Syrjälä 	ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
863c2baf4b7SVille Syrjälä 
864ad3543edSMario Kleiner 	/*
865ad3543edSMario Kleiner 	 * Lock uncore.lock, as we will do multiple timing critical raw
866ad3543edSMario Kleiner 	 * register reads, potentially with preemption disabled, so the
867ad3543edSMario Kleiner 	 * following code must not block on uncore.lock.
868ad3543edSMario Kleiner 	 */
869ad3543edSMario Kleiner 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
870ad3543edSMario Kleiner 
871ad3543edSMario Kleiner 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
872ad3543edSMario Kleiner 
873ad3543edSMario Kleiner 	/* Get optional system timestamp before query. */
874ad3543edSMario Kleiner 	if (stime)
875ad3543edSMario Kleiner 		*stime = ktime_get();
876ad3543edSMario Kleiner 
87791d14251STvrtko Ursulin 	if (IS_GEN2(dev_priv) || IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5) {
8780af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
8790af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
8800af7e4dfSMario Kleiner 		 */
881a225f079SVille Syrjälä 		position = __intel_get_crtc_scanline(intel_crtc);
8820af7e4dfSMario Kleiner 	} else {
8830af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
8840af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
8850af7e4dfSMario Kleiner 		 * scanout position.
8860af7e4dfSMario Kleiner 		 */
88775aa3f63SVille Syrjälä 		position = (I915_READ_FW(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
8880af7e4dfSMario Kleiner 
8893aa18df8SVille Syrjälä 		/* convert to pixel counts */
8903aa18df8SVille Syrjälä 		vbl_start *= htotal;
8913aa18df8SVille Syrjälä 		vbl_end *= htotal;
8923aa18df8SVille Syrjälä 		vtotal *= htotal;
89378e8fc6bSVille Syrjälä 
89478e8fc6bSVille Syrjälä 		/*
8957e78f1cbSVille Syrjälä 		 * In interlaced modes, the pixel counter counts all pixels,
8967e78f1cbSVille Syrjälä 		 * so one field will have htotal more pixels. In order to avoid
8977e78f1cbSVille Syrjälä 		 * the reported position from jumping backwards when the pixel
8987e78f1cbSVille Syrjälä 		 * counter is beyond the length of the shorter field, just
8997e78f1cbSVille Syrjälä 		 * clamp the position the length of the shorter field. This
9007e78f1cbSVille Syrjälä 		 * matches how the scanline counter based position works since
9017e78f1cbSVille Syrjälä 		 * the scanline counter doesn't count the two half lines.
9027e78f1cbSVille Syrjälä 		 */
9037e78f1cbSVille Syrjälä 		if (position >= vtotal)
9047e78f1cbSVille Syrjälä 			position = vtotal - 1;
9057e78f1cbSVille Syrjälä 
9067e78f1cbSVille Syrjälä 		/*
90778e8fc6bSVille Syrjälä 		 * Start of vblank interrupt is triggered at start of hsync,
90878e8fc6bSVille Syrjälä 		 * just prior to the first active line of vblank. However we
90978e8fc6bSVille Syrjälä 		 * consider lines to start at the leading edge of horizontal
91078e8fc6bSVille Syrjälä 		 * active. So, should we get here before we've crossed into
91178e8fc6bSVille Syrjälä 		 * the horizontal active of the first line in vblank, we would
91278e8fc6bSVille Syrjälä 		 * not set the DRM_SCANOUTPOS_INVBL flag. In order to fix that,
91378e8fc6bSVille Syrjälä 		 * always add htotal-hsync_start to the current pixel position.
91478e8fc6bSVille Syrjälä 		 */
91578e8fc6bSVille Syrjälä 		position = (position + htotal - hsync_start) % vtotal;
9163aa18df8SVille Syrjälä 	}
9173aa18df8SVille Syrjälä 
918ad3543edSMario Kleiner 	/* Get optional system timestamp after query. */
919ad3543edSMario Kleiner 	if (etime)
920ad3543edSMario Kleiner 		*etime = ktime_get();
921ad3543edSMario Kleiner 
922ad3543edSMario Kleiner 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
923ad3543edSMario Kleiner 
924ad3543edSMario Kleiner 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
925ad3543edSMario Kleiner 
9263aa18df8SVille Syrjälä 	in_vbl = position >= vbl_start && position < vbl_end;
9273aa18df8SVille Syrjälä 
9283aa18df8SVille Syrjälä 	/*
9293aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
9303aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
9313aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
9323aa18df8SVille Syrjälä 	 * up since vbl_end.
9333aa18df8SVille Syrjälä 	 */
9343aa18df8SVille Syrjälä 	if (position >= vbl_start)
9353aa18df8SVille Syrjälä 		position -= vbl_end;
9363aa18df8SVille Syrjälä 	else
9373aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
9383aa18df8SVille Syrjälä 
93991d14251STvrtko Ursulin 	if (IS_GEN2(dev_priv) || IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5) {
9403aa18df8SVille Syrjälä 		*vpos = position;
9413aa18df8SVille Syrjälä 		*hpos = 0;
9423aa18df8SVille Syrjälä 	} else {
9430af7e4dfSMario Kleiner 		*vpos = position / htotal;
9440af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
9450af7e4dfSMario Kleiner 	}
9460af7e4dfSMario Kleiner 
9470af7e4dfSMario Kleiner 	/* In vblank? */
9480af7e4dfSMario Kleiner 	if (in_vbl)
9493d3cbd84SDaniel Vetter 		ret |= DRM_SCANOUTPOS_IN_VBLANK;
9500af7e4dfSMario Kleiner 
9510af7e4dfSMario Kleiner 	return ret;
9520af7e4dfSMario Kleiner }
9530af7e4dfSMario Kleiner 
954a225f079SVille Syrjälä int intel_get_crtc_scanline(struct intel_crtc *crtc)
955a225f079SVille Syrjälä {
956fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
957a225f079SVille Syrjälä 	unsigned long irqflags;
958a225f079SVille Syrjälä 	int position;
959a225f079SVille Syrjälä 
960a225f079SVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
961a225f079SVille Syrjälä 	position = __intel_get_crtc_scanline(crtc);
962a225f079SVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
963a225f079SVille Syrjälä 
964a225f079SVille Syrjälä 	return position;
965a225f079SVille Syrjälä }
966a225f079SVille Syrjälä 
96788e72717SThierry Reding static int i915_get_vblank_timestamp(struct drm_device *dev, unsigned int pipe,
9680af7e4dfSMario Kleiner 			      int *max_error,
9690af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
9700af7e4dfSMario Kleiner 			      unsigned flags)
9710af7e4dfSMario Kleiner {
972b91eb5ccSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(dev);
973e2af48c6SVille Syrjälä 	struct intel_crtc *crtc;
9740af7e4dfSMario Kleiner 
975b91eb5ccSVille Syrjälä 	if (pipe >= INTEL_INFO(dev_priv)->num_pipes) {
97688e72717SThierry Reding 		DRM_ERROR("Invalid crtc %u\n", pipe);
9770af7e4dfSMario Kleiner 		return -EINVAL;
9780af7e4dfSMario Kleiner 	}
9790af7e4dfSMario Kleiner 
9800af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
981b91eb5ccSVille Syrjälä 	crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
9824041b853SChris Wilson 	if (crtc == NULL) {
98388e72717SThierry Reding 		DRM_ERROR("Invalid crtc %u\n", pipe);
9844041b853SChris Wilson 		return -EINVAL;
9854041b853SChris Wilson 	}
9864041b853SChris Wilson 
987e2af48c6SVille Syrjälä 	if (!crtc->base.hwmode.crtc_clock) {
98888e72717SThierry Reding 		DRM_DEBUG_KMS("crtc %u is disabled\n", pipe);
9894041b853SChris Wilson 		return -EBUSY;
9904041b853SChris Wilson 	}
9910af7e4dfSMario Kleiner 
9920af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
9934041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
9944041b853SChris Wilson 						     vblank_time, flags,
995e2af48c6SVille Syrjälä 						     &crtc->base.hwmode);
9960af7e4dfSMario Kleiner }
9970af7e4dfSMario Kleiner 
99891d14251STvrtko Ursulin static void ironlake_rps_change_irq_handler(struct drm_i915_private *dev_priv)
999f97108d1SJesse Barnes {
1000b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
10019270388eSDaniel Vetter 	u8 new_delay;
10029270388eSDaniel Vetter 
1003d0ecd7e2SDaniel Vetter 	spin_lock(&mchdev_lock);
1004f97108d1SJesse Barnes 
100573edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
100673edd18fSDaniel Vetter 
100720e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
10089270388eSDaniel Vetter 
10097648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
1010b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
1011b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
1012f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
1013f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
1014f97108d1SJesse Barnes 
1015f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
1016b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
101720e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
101820e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
101920e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
102020e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
1021b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
102220e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
102320e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
102420e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
102520e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
1026f97108d1SJesse Barnes 	}
1027f97108d1SJesse Barnes 
102891d14251STvrtko Ursulin 	if (ironlake_set_drps(dev_priv, new_delay))
102920e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
1030f97108d1SJesse Barnes 
1031d0ecd7e2SDaniel Vetter 	spin_unlock(&mchdev_lock);
10329270388eSDaniel Vetter 
1033f97108d1SJesse Barnes 	return;
1034f97108d1SJesse Barnes }
1035f97108d1SJesse Barnes 
10360bc40be8STvrtko Ursulin static void notify_ring(struct intel_engine_cs *engine)
1037549f7365SChris Wilson {
103856299fb7SChris Wilson 	struct drm_i915_gem_request *rq = NULL;
103956299fb7SChris Wilson 	struct intel_wait *wait;
1040dffabc8fSTvrtko Ursulin 
10412246bea6SChris Wilson 	atomic_inc(&engine->irq_count);
1042538b257dSChris Wilson 	set_bit(ENGINE_IRQ_BREADCRUMB, &engine->irq_posted);
104356299fb7SChris Wilson 
104461d3dc70SChris Wilson 	spin_lock(&engine->breadcrumbs.irq_lock);
104561d3dc70SChris Wilson 	wait = engine->breadcrumbs.irq_wait;
104656299fb7SChris Wilson 	if (wait) {
104756299fb7SChris Wilson 		/* We use a callback from the dma-fence to submit
104856299fb7SChris Wilson 		 * requests after waiting on our own requests. To
104956299fb7SChris Wilson 		 * ensure minimum delay in queuing the next request to
105056299fb7SChris Wilson 		 * hardware, signal the fence now rather than wait for
105156299fb7SChris Wilson 		 * the signaler to be woken up. We still wake up the
105256299fb7SChris Wilson 		 * waiter in order to handle the irq-seqno coherency
105356299fb7SChris Wilson 		 * issues (we may receive the interrupt before the
105456299fb7SChris Wilson 		 * seqno is written, see __i915_request_irq_complete())
105556299fb7SChris Wilson 		 * and to handle coalescing of multiple seqno updates
105656299fb7SChris Wilson 		 * and many waiters.
105756299fb7SChris Wilson 		 */
105856299fb7SChris Wilson 		if (i915_seqno_passed(intel_engine_get_seqno(engine),
1059db93991bSChris Wilson 				      wait->seqno) &&
1060db93991bSChris Wilson 		    !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
1061db93991bSChris Wilson 			      &wait->request->fence.flags))
106224754d75SChris Wilson 			rq = i915_gem_request_get(wait->request);
106356299fb7SChris Wilson 
106456299fb7SChris Wilson 		wake_up_process(wait->tsk);
106567b807a8SChris Wilson 	} else {
106667b807a8SChris Wilson 		__intel_engine_disarm_breadcrumbs(engine);
106756299fb7SChris Wilson 	}
106861d3dc70SChris Wilson 	spin_unlock(&engine->breadcrumbs.irq_lock);
106956299fb7SChris Wilson 
107024754d75SChris Wilson 	if (rq) {
107156299fb7SChris Wilson 		dma_fence_signal(&rq->fence);
107224754d75SChris Wilson 		i915_gem_request_put(rq);
107324754d75SChris Wilson 	}
107456299fb7SChris Wilson 
107556299fb7SChris Wilson 	trace_intel_engine_notify(engine, wait);
1076549f7365SChris Wilson }
1077549f7365SChris Wilson 
107843cf3bf0SChris Wilson static void vlv_c0_read(struct drm_i915_private *dev_priv,
107943cf3bf0SChris Wilson 			struct intel_rps_ei *ei)
108031685c25SDeepak S {
1081679cb6c1SMika Kuoppala 	ei->ktime = ktime_get_raw();
108243cf3bf0SChris Wilson 	ei->render_c0 = I915_READ(VLV_RENDER_C0_COUNT);
108343cf3bf0SChris Wilson 	ei->media_c0 = I915_READ(VLV_MEDIA_C0_COUNT);
108431685c25SDeepak S }
108531685c25SDeepak S 
108643cf3bf0SChris Wilson void gen6_rps_reset_ei(struct drm_i915_private *dev_priv)
108743cf3bf0SChris Wilson {
1088e0e8c7cbSChris Wilson 	memset(&dev_priv->rps.ei, 0, sizeof(dev_priv->rps.ei));
108943cf3bf0SChris Wilson }
109043cf3bf0SChris Wilson 
109143cf3bf0SChris Wilson static u32 vlv_wa_c0_ei(struct drm_i915_private *dev_priv, u32 pm_iir)
109243cf3bf0SChris Wilson {
1093e0e8c7cbSChris Wilson 	const struct intel_rps_ei *prev = &dev_priv->rps.ei;
109443cf3bf0SChris Wilson 	struct intel_rps_ei now;
109543cf3bf0SChris Wilson 	u32 events = 0;
109643cf3bf0SChris Wilson 
1097e0e8c7cbSChris Wilson 	if ((pm_iir & GEN6_PM_RP_UP_EI_EXPIRED) == 0)
109843cf3bf0SChris Wilson 		return 0;
109943cf3bf0SChris Wilson 
110043cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &now);
110131685c25SDeepak S 
1102679cb6c1SMika Kuoppala 	if (prev->ktime) {
1103e0e8c7cbSChris Wilson 		u64 time, c0;
1104569884e3SChris Wilson 		u32 render, media;
1105e0e8c7cbSChris Wilson 
1106679cb6c1SMika Kuoppala 		time = ktime_us_delta(now.ktime, prev->ktime);
1107e0e8c7cbSChris Wilson 		time *= dev_priv->czclk_freq;
1108e0e8c7cbSChris Wilson 
1109e0e8c7cbSChris Wilson 		/* Workload can be split between render + media,
1110e0e8c7cbSChris Wilson 		 * e.g. SwapBuffers being blitted in X after being rendered in
1111e0e8c7cbSChris Wilson 		 * mesa. To account for this we need to combine both engines
1112e0e8c7cbSChris Wilson 		 * into our activity counter.
1113e0e8c7cbSChris Wilson 		 */
1114569884e3SChris Wilson 		render = now.render_c0 - prev->render_c0;
1115569884e3SChris Wilson 		media = now.media_c0 - prev->media_c0;
1116569884e3SChris Wilson 		c0 = max(render, media);
11176b7f6aa7SMika Kuoppala 		c0 *= 1000 * 100 << 8; /* to usecs and scale to threshold% */
1118e0e8c7cbSChris Wilson 
1119e0e8c7cbSChris Wilson 		if (c0 > time * dev_priv->rps.up_threshold)
1120e0e8c7cbSChris Wilson 			events = GEN6_PM_RP_UP_THRESHOLD;
1121e0e8c7cbSChris Wilson 		else if (c0 < time * dev_priv->rps.down_threshold)
1122e0e8c7cbSChris Wilson 			events = GEN6_PM_RP_DOWN_THRESHOLD;
112331685c25SDeepak S 	}
112431685c25SDeepak S 
1125e0e8c7cbSChris Wilson 	dev_priv->rps.ei = now;
112643cf3bf0SChris Wilson 	return events;
112731685c25SDeepak S }
112831685c25SDeepak S 
1129f5a4c67dSChris Wilson static bool any_waiters(struct drm_i915_private *dev_priv)
1130f5a4c67dSChris Wilson {
1131e2f80391STvrtko Ursulin 	struct intel_engine_cs *engine;
11323b3f1650SAkash Goel 	enum intel_engine_id id;
1133f5a4c67dSChris Wilson 
11343b3f1650SAkash Goel 	for_each_engine(engine, dev_priv, id)
1135688e6c72SChris Wilson 		if (intel_engine_has_waiter(engine))
1136f5a4c67dSChris Wilson 			return true;
1137f5a4c67dSChris Wilson 
1138f5a4c67dSChris Wilson 	return false;
1139f5a4c67dSChris Wilson }
1140f5a4c67dSChris Wilson 
11414912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
11423b8d8d91SJesse Barnes {
11432d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
11442d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, rps.work);
11457c0a16adSChris Wilson 	bool client_boost = false;
11468d3afd7dSChris Wilson 	int new_delay, adj, min, max;
11477c0a16adSChris Wilson 	u32 pm_iir = 0;
11483b8d8d91SJesse Barnes 
114959cdb63dSDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
11507c0a16adSChris Wilson 	if (dev_priv->rps.interrupts_enabled) {
11517c0a16adSChris Wilson 		pm_iir = fetch_and_zero(&dev_priv->rps.pm_iir);
11527c0a16adSChris Wilson 		client_boost = fetch_and_zero(&dev_priv->rps.client_boost);
1153d4d70aa5SImre Deak 	}
115459cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
11554912d041SBen Widawsky 
115660611c13SPaulo Zanoni 	/* Make sure we didn't queue anything we're not going to process. */
1157a6706b45SDeepak S 	WARN_ON(pm_iir & ~dev_priv->pm_rps_events);
11588d3afd7dSChris Wilson 	if ((pm_iir & dev_priv->pm_rps_events) == 0 && !client_boost)
11597c0a16adSChris Wilson 		goto out;
11603b8d8d91SJesse Barnes 
11614fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
11627b9e0ae6SChris Wilson 
116343cf3bf0SChris Wilson 	pm_iir |= vlv_wa_c0_ei(dev_priv, pm_iir);
116443cf3bf0SChris Wilson 
1165dd75fdc8SChris Wilson 	adj = dev_priv->rps.last_adj;
1166edcf284bSChris Wilson 	new_delay = dev_priv->rps.cur_freq;
11678d3afd7dSChris Wilson 	min = dev_priv->rps.min_freq_softlimit;
11688d3afd7dSChris Wilson 	max = dev_priv->rps.max_freq_softlimit;
116929ecd78dSChris Wilson 	if (client_boost || any_waiters(dev_priv))
117029ecd78dSChris Wilson 		max = dev_priv->rps.max_freq;
117129ecd78dSChris Wilson 	if (client_boost && new_delay < dev_priv->rps.boost_freq) {
117229ecd78dSChris Wilson 		new_delay = dev_priv->rps.boost_freq;
11738d3afd7dSChris Wilson 		adj = 0;
11748d3afd7dSChris Wilson 	} else if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
1175dd75fdc8SChris Wilson 		if (adj > 0)
1176dd75fdc8SChris Wilson 			adj *= 2;
1177edcf284bSChris Wilson 		else /* CHV needs even encode values */
1178edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? 2 : 1;
11797e79a683SSagar Arun Kamble 
11807e79a683SSagar Arun Kamble 		if (new_delay >= dev_priv->rps.max_freq_softlimit)
11817e79a683SSagar Arun Kamble 			adj = 0;
118229ecd78dSChris Wilson 	} else if (client_boost || any_waiters(dev_priv)) {
1183f5a4c67dSChris Wilson 		adj = 0;
1184dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
1185b39fb297SBen Widawsky 		if (dev_priv->rps.cur_freq > dev_priv->rps.efficient_freq)
1186b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
118717136d54SChris Wilson 		else if (dev_priv->rps.cur_freq > dev_priv->rps.min_freq_softlimit)
1188b39fb297SBen Widawsky 			new_delay = dev_priv->rps.min_freq_softlimit;
1189dd75fdc8SChris Wilson 		adj = 0;
1190dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1191dd75fdc8SChris Wilson 		if (adj < 0)
1192dd75fdc8SChris Wilson 			adj *= 2;
1193edcf284bSChris Wilson 		else /* CHV needs even encode values */
1194edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? -2 : -1;
11957e79a683SSagar Arun Kamble 
11967e79a683SSagar Arun Kamble 		if (new_delay <= dev_priv->rps.min_freq_softlimit)
11977e79a683SSagar Arun Kamble 			adj = 0;
1198dd75fdc8SChris Wilson 	} else { /* unknown event */
1199edcf284bSChris Wilson 		adj = 0;
1200dd75fdc8SChris Wilson 	}
12013b8d8d91SJesse Barnes 
1202edcf284bSChris Wilson 	dev_priv->rps.last_adj = adj;
1203edcf284bSChris Wilson 
120479249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
120579249636SBen Widawsky 	 * interrupt
120679249636SBen Widawsky 	 */
1207edcf284bSChris Wilson 	new_delay += adj;
12088d3afd7dSChris Wilson 	new_delay = clamp_t(int, new_delay, min, max);
120927544369SDeepak S 
12109fcee2f7SChris Wilson 	if (intel_set_rps(dev_priv, new_delay)) {
12119fcee2f7SChris Wilson 		DRM_DEBUG_DRIVER("Failed to set new GPU frequency\n");
12129fcee2f7SChris Wilson 		dev_priv->rps.last_adj = 0;
12139fcee2f7SChris Wilson 	}
12143b8d8d91SJesse Barnes 
12154fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
12167c0a16adSChris Wilson 
12177c0a16adSChris Wilson out:
12187c0a16adSChris Wilson 	/* Make sure not to corrupt PMIMR state used by ringbuffer on GEN6 */
12197c0a16adSChris Wilson 	spin_lock_irq(&dev_priv->irq_lock);
12207c0a16adSChris Wilson 	if (dev_priv->rps.interrupts_enabled)
12217c0a16adSChris Wilson 		gen6_unmask_pm_irq(dev_priv, dev_priv->pm_rps_events);
12227c0a16adSChris Wilson 	spin_unlock_irq(&dev_priv->irq_lock);
12233b8d8d91SJesse Barnes }
12243b8d8d91SJesse Barnes 
1225e3689190SBen Widawsky 
1226e3689190SBen Widawsky /**
1227e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
1228e3689190SBen Widawsky  * occurred.
1229e3689190SBen Widawsky  * @work: workqueue struct
1230e3689190SBen Widawsky  *
1231e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
1232e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
1233e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
1234e3689190SBen Widawsky  */
1235e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
1236e3689190SBen Widawsky {
12372d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
12382d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, l3_parity.error_work);
1239e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
124035a85ac6SBen Widawsky 	char *parity_event[6];
1241e3689190SBen Widawsky 	uint32_t misccpctl;
124235a85ac6SBen Widawsky 	uint8_t slice = 0;
1243e3689190SBen Widawsky 
1244e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
1245e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
1246e3689190SBen Widawsky 	 * any time we access those registers.
1247e3689190SBen Widawsky 	 */
124891c8a326SChris Wilson 	mutex_lock(&dev_priv->drm.struct_mutex);
1249e3689190SBen Widawsky 
125035a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
125135a85ac6SBen Widawsky 	if (WARN_ON(!dev_priv->l3_parity.which_slice))
125235a85ac6SBen Widawsky 		goto out;
125335a85ac6SBen Widawsky 
1254e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
1255e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1256e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
1257e3689190SBen Widawsky 
125835a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
1259f0f59a00SVille Syrjälä 		i915_reg_t reg;
126035a85ac6SBen Widawsky 
126135a85ac6SBen Widawsky 		slice--;
12622d1fe073SJoonas Lahtinen 		if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv)))
126335a85ac6SBen Widawsky 			break;
126435a85ac6SBen Widawsky 
126535a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
126635a85ac6SBen Widawsky 
12676fa1c5f1SVille Syrjälä 		reg = GEN7_L3CDERRST1(slice);
126835a85ac6SBen Widawsky 
126935a85ac6SBen Widawsky 		error_status = I915_READ(reg);
1270e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
1271e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
1272e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1273e3689190SBen Widawsky 
127435a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
127535a85ac6SBen Widawsky 		POSTING_READ(reg);
1276e3689190SBen Widawsky 
1277cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1278e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1279e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1280e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
128135a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
128235a85ac6SBen Widawsky 		parity_event[5] = NULL;
1283e3689190SBen Widawsky 
128491c8a326SChris Wilson 		kobject_uevent_env(&dev_priv->drm.primary->kdev->kobj,
1285e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
1286e3689190SBen Widawsky 
128735a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
128835a85ac6SBen Widawsky 			  slice, row, bank, subbank);
1289e3689190SBen Widawsky 
129035a85ac6SBen Widawsky 		kfree(parity_event[4]);
1291e3689190SBen Widawsky 		kfree(parity_event[3]);
1292e3689190SBen Widawsky 		kfree(parity_event[2]);
1293e3689190SBen Widawsky 		kfree(parity_event[1]);
1294e3689190SBen Widawsky 	}
1295e3689190SBen Widawsky 
129635a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
129735a85ac6SBen Widawsky 
129835a85ac6SBen Widawsky out:
129935a85ac6SBen Widawsky 	WARN_ON(dev_priv->l3_parity.which_slice);
13004cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
13012d1fe073SJoonas Lahtinen 	gen5_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv));
13024cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
130335a85ac6SBen Widawsky 
130491c8a326SChris Wilson 	mutex_unlock(&dev_priv->drm.struct_mutex);
130535a85ac6SBen Widawsky }
130635a85ac6SBen Widawsky 
1307261e40b8SVille Syrjälä static void ivybridge_parity_error_irq_handler(struct drm_i915_private *dev_priv,
1308261e40b8SVille Syrjälä 					       u32 iir)
1309e3689190SBen Widawsky {
1310261e40b8SVille Syrjälä 	if (!HAS_L3_DPF(dev_priv))
1311e3689190SBen Widawsky 		return;
1312e3689190SBen Widawsky 
1313d0ecd7e2SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1314261e40b8SVille Syrjälä 	gen5_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv));
1315d0ecd7e2SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
1316e3689190SBen Widawsky 
1317261e40b8SVille Syrjälä 	iir &= GT_PARITY_ERROR(dev_priv);
131835a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
131935a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 1;
132035a85ac6SBen Widawsky 
132135a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
132235a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 0;
132335a85ac6SBen Widawsky 
1324a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
1325e3689190SBen Widawsky }
1326e3689190SBen Widawsky 
1327261e40b8SVille Syrjälä static void ilk_gt_irq_handler(struct drm_i915_private *dev_priv,
1328f1af8fc1SPaulo Zanoni 			       u32 gt_iir)
1329f1af8fc1SPaulo Zanoni {
1330f8973c21SChris Wilson 	if (gt_iir & GT_RENDER_USER_INTERRUPT)
13313b3f1650SAkash Goel 		notify_ring(dev_priv->engine[RCS]);
1332f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
13333b3f1650SAkash Goel 		notify_ring(dev_priv->engine[VCS]);
1334f1af8fc1SPaulo Zanoni }
1335f1af8fc1SPaulo Zanoni 
1336261e40b8SVille Syrjälä static void snb_gt_irq_handler(struct drm_i915_private *dev_priv,
1337e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1338e7b4c6b1SDaniel Vetter {
1339f8973c21SChris Wilson 	if (gt_iir & GT_RENDER_USER_INTERRUPT)
13403b3f1650SAkash Goel 		notify_ring(dev_priv->engine[RCS]);
1341cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
13423b3f1650SAkash Goel 		notify_ring(dev_priv->engine[VCS]);
1343cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
13443b3f1650SAkash Goel 		notify_ring(dev_priv->engine[BCS]);
1345e7b4c6b1SDaniel Vetter 
1346cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1347cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
1348aaecdf61SDaniel Vetter 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT))
1349aaecdf61SDaniel Vetter 		DRM_DEBUG("Command parser error, gt_iir 0x%08x\n", gt_iir);
1350e3689190SBen Widawsky 
1351261e40b8SVille Syrjälä 	if (gt_iir & GT_PARITY_ERROR(dev_priv))
1352261e40b8SVille Syrjälä 		ivybridge_parity_error_irq_handler(dev_priv, gt_iir);
1353e7b4c6b1SDaniel Vetter }
1354e7b4c6b1SDaniel Vetter 
1355fbcc1a0cSNick Hoath static __always_inline void
13560bc40be8STvrtko Ursulin gen8_cs_irq_handler(struct intel_engine_cs *engine, u32 iir, int test_shift)
1357fbcc1a0cSNick Hoath {
1358*31de7350SChris Wilson 	bool tasklet = false;
1359f747026cSChris Wilson 
1360f747026cSChris Wilson 	if (iir & (GT_CONTEXT_SWITCH_INTERRUPT << test_shift)) {
1361f747026cSChris Wilson 		set_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
1362*31de7350SChris Wilson 		tasklet = true;
1363f747026cSChris Wilson 	}
1364*31de7350SChris Wilson 
1365*31de7350SChris Wilson 	if (iir & (GT_RENDER_USER_INTERRUPT << test_shift)) {
1366*31de7350SChris Wilson 		notify_ring(engine);
1367*31de7350SChris Wilson 		tasklet |= i915.enable_guc_submission;
1368*31de7350SChris Wilson 	}
1369*31de7350SChris Wilson 
1370*31de7350SChris Wilson 	if (tasklet)
1371*31de7350SChris Wilson 		tasklet_hi_schedule(&engine->irq_tasklet);
1372fbcc1a0cSNick Hoath }
1373fbcc1a0cSNick Hoath 
1374e30e251aSVille Syrjälä static irqreturn_t gen8_gt_irq_ack(struct drm_i915_private *dev_priv,
1375e30e251aSVille Syrjälä 				   u32 master_ctl,
1376e30e251aSVille Syrjälä 				   u32 gt_iir[4])
1377abd58f01SBen Widawsky {
1378abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
1379abd58f01SBen Widawsky 
1380abd58f01SBen Widawsky 	if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
1381e30e251aSVille Syrjälä 		gt_iir[0] = I915_READ_FW(GEN8_GT_IIR(0));
1382e30e251aSVille Syrjälä 		if (gt_iir[0]) {
1383e30e251aSVille Syrjälä 			I915_WRITE_FW(GEN8_GT_IIR(0), gt_iir[0]);
1384abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1385abd58f01SBen Widawsky 		} else
1386abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT0)!\n");
1387abd58f01SBen Widawsky 	}
1388abd58f01SBen Widawsky 
138985f9b5f9SZhao Yakui 	if (master_ctl & (GEN8_GT_VCS1_IRQ | GEN8_GT_VCS2_IRQ)) {
1390e30e251aSVille Syrjälä 		gt_iir[1] = I915_READ_FW(GEN8_GT_IIR(1));
1391e30e251aSVille Syrjälä 		if (gt_iir[1]) {
1392e30e251aSVille Syrjälä 			I915_WRITE_FW(GEN8_GT_IIR(1), gt_iir[1]);
1393abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1394abd58f01SBen Widawsky 		} else
1395abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT1)!\n");
1396abd58f01SBen Widawsky 	}
1397abd58f01SBen Widawsky 
139874cdb337SChris Wilson 	if (master_ctl & GEN8_GT_VECS_IRQ) {
1399e30e251aSVille Syrjälä 		gt_iir[3] = I915_READ_FW(GEN8_GT_IIR(3));
1400e30e251aSVille Syrjälä 		if (gt_iir[3]) {
1401e30e251aSVille Syrjälä 			I915_WRITE_FW(GEN8_GT_IIR(3), gt_iir[3]);
140274cdb337SChris Wilson 			ret = IRQ_HANDLED;
140374cdb337SChris Wilson 		} else
140474cdb337SChris Wilson 			DRM_ERROR("The master control interrupt lied (GT3)!\n");
140574cdb337SChris Wilson 	}
140674cdb337SChris Wilson 
140726705e20SSagar Arun Kamble 	if (master_ctl & (GEN8_GT_PM_IRQ | GEN8_GT_GUC_IRQ)) {
1408e30e251aSVille Syrjälä 		gt_iir[2] = I915_READ_FW(GEN8_GT_IIR(2));
140926705e20SSagar Arun Kamble 		if (gt_iir[2] & (dev_priv->pm_rps_events |
141026705e20SSagar Arun Kamble 				 dev_priv->pm_guc_events)) {
1411cb0d205eSChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(2),
141226705e20SSagar Arun Kamble 				      gt_iir[2] & (dev_priv->pm_rps_events |
141326705e20SSagar Arun Kamble 						   dev_priv->pm_guc_events));
141438cc46d7SOscar Mateo 			ret = IRQ_HANDLED;
14150961021aSBen Widawsky 		} else
14160961021aSBen Widawsky 			DRM_ERROR("The master control interrupt lied (PM)!\n");
14170961021aSBen Widawsky 	}
14180961021aSBen Widawsky 
1419abd58f01SBen Widawsky 	return ret;
1420abd58f01SBen Widawsky }
1421abd58f01SBen Widawsky 
1422e30e251aSVille Syrjälä static void gen8_gt_irq_handler(struct drm_i915_private *dev_priv,
1423e30e251aSVille Syrjälä 				u32 gt_iir[4])
1424e30e251aSVille Syrjälä {
1425e30e251aSVille Syrjälä 	if (gt_iir[0]) {
14263b3f1650SAkash Goel 		gen8_cs_irq_handler(dev_priv->engine[RCS],
1427e30e251aSVille Syrjälä 				    gt_iir[0], GEN8_RCS_IRQ_SHIFT);
14283b3f1650SAkash Goel 		gen8_cs_irq_handler(dev_priv->engine[BCS],
1429e30e251aSVille Syrjälä 				    gt_iir[0], GEN8_BCS_IRQ_SHIFT);
1430e30e251aSVille Syrjälä 	}
1431e30e251aSVille Syrjälä 
1432e30e251aSVille Syrjälä 	if (gt_iir[1]) {
14333b3f1650SAkash Goel 		gen8_cs_irq_handler(dev_priv->engine[VCS],
1434e30e251aSVille Syrjälä 				    gt_iir[1], GEN8_VCS1_IRQ_SHIFT);
14353b3f1650SAkash Goel 		gen8_cs_irq_handler(dev_priv->engine[VCS2],
1436e30e251aSVille Syrjälä 				    gt_iir[1], GEN8_VCS2_IRQ_SHIFT);
1437e30e251aSVille Syrjälä 	}
1438e30e251aSVille Syrjälä 
1439e30e251aSVille Syrjälä 	if (gt_iir[3])
14403b3f1650SAkash Goel 		gen8_cs_irq_handler(dev_priv->engine[VECS],
1441e30e251aSVille Syrjälä 				    gt_iir[3], GEN8_VECS_IRQ_SHIFT);
1442e30e251aSVille Syrjälä 
1443e30e251aSVille Syrjälä 	if (gt_iir[2] & dev_priv->pm_rps_events)
1444e30e251aSVille Syrjälä 		gen6_rps_irq_handler(dev_priv, gt_iir[2]);
144526705e20SSagar Arun Kamble 
144626705e20SSagar Arun Kamble 	if (gt_iir[2] & dev_priv->pm_guc_events)
144726705e20SSagar Arun Kamble 		gen9_guc_irq_handler(dev_priv, gt_iir[2]);
1448e30e251aSVille Syrjälä }
1449e30e251aSVille Syrjälä 
145063c88d22SImre Deak static bool bxt_port_hotplug_long_detect(enum port port, u32 val)
145163c88d22SImre Deak {
145263c88d22SImre Deak 	switch (port) {
145363c88d22SImre Deak 	case PORT_A:
1454195baa06SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
145563c88d22SImre Deak 	case PORT_B:
145663c88d22SImre Deak 		return val & PORTB_HOTPLUG_LONG_DETECT;
145763c88d22SImre Deak 	case PORT_C:
145863c88d22SImre Deak 		return val & PORTC_HOTPLUG_LONG_DETECT;
145963c88d22SImre Deak 	default:
146063c88d22SImre Deak 		return false;
146163c88d22SImre Deak 	}
146263c88d22SImre Deak }
146363c88d22SImre Deak 
14646dbf30ceSVille Syrjälä static bool spt_port_hotplug2_long_detect(enum port port, u32 val)
14656dbf30ceSVille Syrjälä {
14666dbf30ceSVille Syrjälä 	switch (port) {
14676dbf30ceSVille Syrjälä 	case PORT_E:
14686dbf30ceSVille Syrjälä 		return val & PORTE_HOTPLUG_LONG_DETECT;
14696dbf30ceSVille Syrjälä 	default:
14706dbf30ceSVille Syrjälä 		return false;
14716dbf30ceSVille Syrjälä 	}
14726dbf30ceSVille Syrjälä }
14736dbf30ceSVille Syrjälä 
147474c0b395SVille Syrjälä static bool spt_port_hotplug_long_detect(enum port port, u32 val)
147574c0b395SVille Syrjälä {
147674c0b395SVille Syrjälä 	switch (port) {
147774c0b395SVille Syrjälä 	case PORT_A:
147874c0b395SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
147974c0b395SVille Syrjälä 	case PORT_B:
148074c0b395SVille Syrjälä 		return val & PORTB_HOTPLUG_LONG_DETECT;
148174c0b395SVille Syrjälä 	case PORT_C:
148274c0b395SVille Syrjälä 		return val & PORTC_HOTPLUG_LONG_DETECT;
148374c0b395SVille Syrjälä 	case PORT_D:
148474c0b395SVille Syrjälä 		return val & PORTD_HOTPLUG_LONG_DETECT;
148574c0b395SVille Syrjälä 	default:
148674c0b395SVille Syrjälä 		return false;
148774c0b395SVille Syrjälä 	}
148874c0b395SVille Syrjälä }
148974c0b395SVille Syrjälä 
1490e4ce95aaSVille Syrjälä static bool ilk_port_hotplug_long_detect(enum port port, u32 val)
1491e4ce95aaSVille Syrjälä {
1492e4ce95aaSVille Syrjälä 	switch (port) {
1493e4ce95aaSVille Syrjälä 	case PORT_A:
1494e4ce95aaSVille Syrjälä 		return val & DIGITAL_PORTA_HOTPLUG_LONG_DETECT;
1495e4ce95aaSVille Syrjälä 	default:
1496e4ce95aaSVille Syrjälä 		return false;
1497e4ce95aaSVille Syrjälä 	}
1498e4ce95aaSVille Syrjälä }
1499e4ce95aaSVille Syrjälä 
1500676574dfSJani Nikula static bool pch_port_hotplug_long_detect(enum port port, u32 val)
150113cf5504SDave Airlie {
150213cf5504SDave Airlie 	switch (port) {
150313cf5504SDave Airlie 	case PORT_B:
1504676574dfSJani Nikula 		return val & PORTB_HOTPLUG_LONG_DETECT;
150513cf5504SDave Airlie 	case PORT_C:
1506676574dfSJani Nikula 		return val & PORTC_HOTPLUG_LONG_DETECT;
150713cf5504SDave Airlie 	case PORT_D:
1508676574dfSJani Nikula 		return val & PORTD_HOTPLUG_LONG_DETECT;
1509676574dfSJani Nikula 	default:
1510676574dfSJani Nikula 		return false;
151113cf5504SDave Airlie 	}
151213cf5504SDave Airlie }
151313cf5504SDave Airlie 
1514676574dfSJani Nikula static bool i9xx_port_hotplug_long_detect(enum port port, u32 val)
151513cf5504SDave Airlie {
151613cf5504SDave Airlie 	switch (port) {
151713cf5504SDave Airlie 	case PORT_B:
1518676574dfSJani Nikula 		return val & PORTB_HOTPLUG_INT_LONG_PULSE;
151913cf5504SDave Airlie 	case PORT_C:
1520676574dfSJani Nikula 		return val & PORTC_HOTPLUG_INT_LONG_PULSE;
152113cf5504SDave Airlie 	case PORT_D:
1522676574dfSJani Nikula 		return val & PORTD_HOTPLUG_INT_LONG_PULSE;
1523676574dfSJani Nikula 	default:
1524676574dfSJani Nikula 		return false;
152513cf5504SDave Airlie 	}
152613cf5504SDave Airlie }
152713cf5504SDave Airlie 
152842db67d6SVille Syrjälä /*
152942db67d6SVille Syrjälä  * Get a bit mask of pins that have triggered, and which ones may be long.
153042db67d6SVille Syrjälä  * This can be called multiple times with the same masks to accumulate
153142db67d6SVille Syrjälä  * hotplug detection results from several registers.
153242db67d6SVille Syrjälä  *
153342db67d6SVille Syrjälä  * Note that the caller is expected to zero out the masks initially.
153442db67d6SVille Syrjälä  */
1535fd63e2a9SImre Deak static void intel_get_hpd_pins(u32 *pin_mask, u32 *long_mask,
15368c841e57SJani Nikula 			     u32 hotplug_trigger, u32 dig_hotplug_reg,
1537fd63e2a9SImre Deak 			     const u32 hpd[HPD_NUM_PINS],
1538fd63e2a9SImre Deak 			     bool long_pulse_detect(enum port port, u32 val))
1539676574dfSJani Nikula {
15408c841e57SJani Nikula 	enum port port;
1541676574dfSJani Nikula 	int i;
1542676574dfSJani Nikula 
1543676574dfSJani Nikula 	for_each_hpd_pin(i) {
15448c841e57SJani Nikula 		if ((hpd[i] & hotplug_trigger) == 0)
15458c841e57SJani Nikula 			continue;
15468c841e57SJani Nikula 
1547676574dfSJani Nikula 		*pin_mask |= BIT(i);
1548676574dfSJani Nikula 
1549cc24fcdcSImre Deak 		if (!intel_hpd_pin_to_port(i, &port))
1550cc24fcdcSImre Deak 			continue;
1551cc24fcdcSImre Deak 
1552fd63e2a9SImre Deak 		if (long_pulse_detect(port, dig_hotplug_reg))
1553676574dfSJani Nikula 			*long_mask |= BIT(i);
1554676574dfSJani Nikula 	}
1555676574dfSJani Nikula 
1556676574dfSJani Nikula 	DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x, dig 0x%08x, pins 0x%08x\n",
1557676574dfSJani Nikula 			 hotplug_trigger, dig_hotplug_reg, *pin_mask);
1558676574dfSJani Nikula 
1559676574dfSJani Nikula }
1560676574dfSJani Nikula 
156191d14251STvrtko Ursulin static void gmbus_irq_handler(struct drm_i915_private *dev_priv)
1562515ac2bbSDaniel Vetter {
156328c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1564515ac2bbSDaniel Vetter }
1565515ac2bbSDaniel Vetter 
156691d14251STvrtko Ursulin static void dp_aux_irq_handler(struct drm_i915_private *dev_priv)
1567ce99c256SDaniel Vetter {
15689ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1569ce99c256SDaniel Vetter }
1570ce99c256SDaniel Vetter 
15718bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
157291d14251STvrtko Ursulin static void display_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
157391d14251STvrtko Ursulin 					 enum pipe pipe,
1574eba94eb9SDaniel Vetter 					 uint32_t crc0, uint32_t crc1,
1575eba94eb9SDaniel Vetter 					 uint32_t crc2, uint32_t crc3,
15768bc5e955SDaniel Vetter 					 uint32_t crc4)
15778bf1e9f1SShuang He {
15788bf1e9f1SShuang He 	struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
15798bf1e9f1SShuang He 	struct intel_pipe_crc_entry *entry;
15808c6b709dSTomeu Vizoso 	struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
15818c6b709dSTomeu Vizoso 	struct drm_driver *driver = dev_priv->drm.driver;
15828c6b709dSTomeu Vizoso 	uint32_t crcs[5];
1583ac2300d4SDamien Lespiau 	int head, tail;
1584b2c88f5bSDamien Lespiau 
1585d538bbdfSDamien Lespiau 	spin_lock(&pipe_crc->lock);
15868c6b709dSTomeu Vizoso 	if (pipe_crc->source) {
15870c912c79SDamien Lespiau 		if (!pipe_crc->entries) {
1588d538bbdfSDamien Lespiau 			spin_unlock(&pipe_crc->lock);
158934273620SDaniel Vetter 			DRM_DEBUG_KMS("spurious interrupt\n");
15900c912c79SDamien Lespiau 			return;
15910c912c79SDamien Lespiau 		}
15920c912c79SDamien Lespiau 
1593d538bbdfSDamien Lespiau 		head = pipe_crc->head;
1594d538bbdfSDamien Lespiau 		tail = pipe_crc->tail;
1595b2c88f5bSDamien Lespiau 
1596b2c88f5bSDamien Lespiau 		if (CIRC_SPACE(head, tail, INTEL_PIPE_CRC_ENTRIES_NR) < 1) {
1597d538bbdfSDamien Lespiau 			spin_unlock(&pipe_crc->lock);
1598b2c88f5bSDamien Lespiau 			DRM_ERROR("CRC buffer overflowing\n");
1599b2c88f5bSDamien Lespiau 			return;
1600b2c88f5bSDamien Lespiau 		}
1601b2c88f5bSDamien Lespiau 
1602b2c88f5bSDamien Lespiau 		entry = &pipe_crc->entries[head];
16038bf1e9f1SShuang He 
16048c6b709dSTomeu Vizoso 		entry->frame = driver->get_vblank_counter(&dev_priv->drm, pipe);
1605eba94eb9SDaniel Vetter 		entry->crc[0] = crc0;
1606eba94eb9SDaniel Vetter 		entry->crc[1] = crc1;
1607eba94eb9SDaniel Vetter 		entry->crc[2] = crc2;
1608eba94eb9SDaniel Vetter 		entry->crc[3] = crc3;
1609eba94eb9SDaniel Vetter 		entry->crc[4] = crc4;
1610b2c88f5bSDamien Lespiau 
1611b2c88f5bSDamien Lespiau 		head = (head + 1) & (INTEL_PIPE_CRC_ENTRIES_NR - 1);
1612d538bbdfSDamien Lespiau 		pipe_crc->head = head;
1613d538bbdfSDamien Lespiau 
1614d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
161507144428SDamien Lespiau 
161607144428SDamien Lespiau 		wake_up_interruptible(&pipe_crc->wq);
16178c6b709dSTomeu Vizoso 	} else {
16188c6b709dSTomeu Vizoso 		/*
16198c6b709dSTomeu Vizoso 		 * For some not yet identified reason, the first CRC is
16208c6b709dSTomeu Vizoso 		 * bonkers. So let's just wait for the next vblank and read
16218c6b709dSTomeu Vizoso 		 * out the buggy result.
16228c6b709dSTomeu Vizoso 		 *
16238c6b709dSTomeu Vizoso 		 * On CHV sometimes the second CRC is bonkers as well, so
16248c6b709dSTomeu Vizoso 		 * don't trust that one either.
16258c6b709dSTomeu Vizoso 		 */
16268c6b709dSTomeu Vizoso 		if (pipe_crc->skipped == 0 ||
16278c6b709dSTomeu Vizoso 		    (IS_CHERRYVIEW(dev_priv) && pipe_crc->skipped == 1)) {
16288c6b709dSTomeu Vizoso 			pipe_crc->skipped++;
16298c6b709dSTomeu Vizoso 			spin_unlock(&pipe_crc->lock);
16308c6b709dSTomeu Vizoso 			return;
16318c6b709dSTomeu Vizoso 		}
16328c6b709dSTomeu Vizoso 		spin_unlock(&pipe_crc->lock);
16338c6b709dSTomeu Vizoso 		crcs[0] = crc0;
16348c6b709dSTomeu Vizoso 		crcs[1] = crc1;
16358c6b709dSTomeu Vizoso 		crcs[2] = crc2;
16368c6b709dSTomeu Vizoso 		crcs[3] = crc3;
16378c6b709dSTomeu Vizoso 		crcs[4] = crc4;
1638246ee524STomeu Vizoso 		drm_crtc_add_crc_entry(&crtc->base, true,
1639246ee524STomeu Vizoso 				       drm_accurate_vblank_count(&crtc->base),
1640246ee524STomeu Vizoso 				       crcs);
16418c6b709dSTomeu Vizoso 	}
16428bf1e9f1SShuang He }
1643277de95eSDaniel Vetter #else
1644277de95eSDaniel Vetter static inline void
164591d14251STvrtko Ursulin display_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
164691d14251STvrtko Ursulin 			     enum pipe pipe,
1647277de95eSDaniel Vetter 			     uint32_t crc0, uint32_t crc1,
1648277de95eSDaniel Vetter 			     uint32_t crc2, uint32_t crc3,
1649277de95eSDaniel Vetter 			     uint32_t crc4) {}
1650277de95eSDaniel Vetter #endif
1651eba94eb9SDaniel Vetter 
1652277de95eSDaniel Vetter 
165391d14251STvrtko Ursulin static void hsw_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
165491d14251STvrtko Ursulin 				     enum pipe pipe)
16555a69b89fSDaniel Vetter {
165691d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
16575a69b89fSDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
16585a69b89fSDaniel Vetter 				     0, 0, 0, 0);
16595a69b89fSDaniel Vetter }
16605a69b89fSDaniel Vetter 
166191d14251STvrtko Ursulin static void ivb_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
166291d14251STvrtko Ursulin 				     enum pipe pipe)
1663eba94eb9SDaniel Vetter {
166491d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
1665eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1666eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1667eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1668eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
16698bc5e955SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1670eba94eb9SDaniel Vetter }
16715b3a856bSDaniel Vetter 
167291d14251STvrtko Ursulin static void i9xx_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
167391d14251STvrtko Ursulin 				      enum pipe pipe)
16745b3a856bSDaniel Vetter {
16750b5c5ed0SDaniel Vetter 	uint32_t res1, res2;
16760b5c5ed0SDaniel Vetter 
167791d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 3)
16780b5c5ed0SDaniel Vetter 		res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
16790b5c5ed0SDaniel Vetter 	else
16800b5c5ed0SDaniel Vetter 		res1 = 0;
16810b5c5ed0SDaniel Vetter 
168291d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
16830b5c5ed0SDaniel Vetter 		res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
16840b5c5ed0SDaniel Vetter 	else
16850b5c5ed0SDaniel Vetter 		res2 = 0;
16865b3a856bSDaniel Vetter 
168791d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
16880b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_RED(pipe)),
16890b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_GREEN(pipe)),
16900b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_BLUE(pipe)),
16910b5c5ed0SDaniel Vetter 				     res1, res2);
16925b3a856bSDaniel Vetter }
16938bf1e9f1SShuang He 
16941403c0d4SPaulo Zanoni /* The RPS events need forcewake, so we add them to a work queue and mask their
16951403c0d4SPaulo Zanoni  * IMR bits until the work is done. Other interrupts can be processed without
16961403c0d4SPaulo Zanoni  * the work queue. */
16971403c0d4SPaulo Zanoni static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1698baf02a1fSBen Widawsky {
1699a6706b45SDeepak S 	if (pm_iir & dev_priv->pm_rps_events) {
170059cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
1701f4e9af4fSAkash Goel 		gen6_mask_pm_irq(dev_priv, pm_iir & dev_priv->pm_rps_events);
1702d4d70aa5SImre Deak 		if (dev_priv->rps.interrupts_enabled) {
1703d4d70aa5SImre Deak 			dev_priv->rps.pm_iir |= pm_iir & dev_priv->pm_rps_events;
1704c33d247dSChris Wilson 			schedule_work(&dev_priv->rps.work);
170541a05a3aSDaniel Vetter 		}
1706d4d70aa5SImre Deak 		spin_unlock(&dev_priv->irq_lock);
1707d4d70aa5SImre Deak 	}
1708baf02a1fSBen Widawsky 
1709c9a9a268SImre Deak 	if (INTEL_INFO(dev_priv)->gen >= 8)
1710c9a9a268SImre Deak 		return;
1711c9a9a268SImre Deak 
17122d1fe073SJoonas Lahtinen 	if (HAS_VEBOX(dev_priv)) {
171312638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
17143b3f1650SAkash Goel 			notify_ring(dev_priv->engine[VECS]);
171512638c57SBen Widawsky 
1716aaecdf61SDaniel Vetter 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT)
1717aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, pm_iir 0x%08x\n", pm_iir);
171812638c57SBen Widawsky 	}
17191403c0d4SPaulo Zanoni }
1720baf02a1fSBen Widawsky 
172126705e20SSagar Arun Kamble static void gen9_guc_irq_handler(struct drm_i915_private *dev_priv, u32 gt_iir)
172226705e20SSagar Arun Kamble {
172326705e20SSagar Arun Kamble 	if (gt_iir & GEN9_GUC_TO_HOST_INT_EVENT) {
17244100b2abSSagar Arun Kamble 		/* Sample the log buffer flush related bits & clear them out now
17254100b2abSSagar Arun Kamble 		 * itself from the message identity register to minimize the
17264100b2abSSagar Arun Kamble 		 * probability of losing a flush interrupt, when there are back
17274100b2abSSagar Arun Kamble 		 * to back flush interrupts.
17284100b2abSSagar Arun Kamble 		 * There can be a new flush interrupt, for different log buffer
17294100b2abSSagar Arun Kamble 		 * type (like for ISR), whilst Host is handling one (for DPC).
17304100b2abSSagar Arun Kamble 		 * Since same bit is used in message register for ISR & DPC, it
17314100b2abSSagar Arun Kamble 		 * could happen that GuC sets the bit for 2nd interrupt but Host
17324100b2abSSagar Arun Kamble 		 * clears out the bit on handling the 1st interrupt.
17334100b2abSSagar Arun Kamble 		 */
17344100b2abSSagar Arun Kamble 		u32 msg, flush;
17354100b2abSSagar Arun Kamble 
17364100b2abSSagar Arun Kamble 		msg = I915_READ(SOFT_SCRATCH(15));
1737a80bc45fSArkadiusz Hiler 		flush = msg & (INTEL_GUC_RECV_MSG_CRASH_DUMP_POSTED |
1738a80bc45fSArkadiusz Hiler 			       INTEL_GUC_RECV_MSG_FLUSH_LOG_BUFFER);
17394100b2abSSagar Arun Kamble 		if (flush) {
17404100b2abSSagar Arun Kamble 			/* Clear the message bits that are handled */
17414100b2abSSagar Arun Kamble 			I915_WRITE(SOFT_SCRATCH(15), msg & ~flush);
17424100b2abSSagar Arun Kamble 
17434100b2abSSagar Arun Kamble 			/* Handle flush interrupt in bottom half */
17444100b2abSSagar Arun Kamble 			queue_work(dev_priv->guc.log.flush_wq,
17454100b2abSSagar Arun Kamble 				   &dev_priv->guc.log.flush_work);
17465aa1ee4bSAkash Goel 
17475aa1ee4bSAkash Goel 			dev_priv->guc.log.flush_interrupt_count++;
17484100b2abSSagar Arun Kamble 		} else {
17494100b2abSSagar Arun Kamble 			/* Not clearing of unhandled event bits won't result in
17504100b2abSSagar Arun Kamble 			 * re-triggering of the interrupt.
17514100b2abSSagar Arun Kamble 			 */
17524100b2abSSagar Arun Kamble 		}
175326705e20SSagar Arun Kamble 	}
175426705e20SSagar Arun Kamble }
175526705e20SSagar Arun Kamble 
17565a21b665SDaniel Vetter static bool intel_pipe_handle_vblank(struct drm_i915_private *dev_priv,
175791d14251STvrtko Ursulin 				     enum pipe pipe)
17588d7849dbSVille Syrjälä {
17595a21b665SDaniel Vetter 	bool ret;
17605a21b665SDaniel Vetter 
176191c8a326SChris Wilson 	ret = drm_handle_vblank(&dev_priv->drm, pipe);
17625a21b665SDaniel Vetter 	if (ret)
176351cbaf01SMaarten Lankhorst 		intel_finish_page_flip_mmio(dev_priv, pipe);
17645a21b665SDaniel Vetter 
17655a21b665SDaniel Vetter 	return ret;
17668d7849dbSVille Syrjälä }
17678d7849dbSVille Syrjälä 
176891d14251STvrtko Ursulin static void valleyview_pipestat_irq_ack(struct drm_i915_private *dev_priv,
176991d14251STvrtko Ursulin 					u32 iir, u32 pipe_stats[I915_MAX_PIPES])
17707e231dbeSJesse Barnes {
17717e231dbeSJesse Barnes 	int pipe;
17727e231dbeSJesse Barnes 
177358ead0d7SImre Deak 	spin_lock(&dev_priv->irq_lock);
17741ca993d2SVille Syrjälä 
17751ca993d2SVille Syrjälä 	if (!dev_priv->display_irqs_enabled) {
17761ca993d2SVille Syrjälä 		spin_unlock(&dev_priv->irq_lock);
17771ca993d2SVille Syrjälä 		return;
17781ca993d2SVille Syrjälä 	}
17791ca993d2SVille Syrjälä 
1780055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1781f0f59a00SVille Syrjälä 		i915_reg_t reg;
1782bbb5eebfSDaniel Vetter 		u32 mask, iir_bit = 0;
178391d181ddSImre Deak 
1784bbb5eebfSDaniel Vetter 		/*
1785bbb5eebfSDaniel Vetter 		 * PIPESTAT bits get signalled even when the interrupt is
1786bbb5eebfSDaniel Vetter 		 * disabled with the mask bits, and some of the status bits do
1787bbb5eebfSDaniel Vetter 		 * not generate interrupts at all (like the underrun bit). Hence
1788bbb5eebfSDaniel Vetter 		 * we need to be careful that we only handle what we want to
1789bbb5eebfSDaniel Vetter 		 * handle.
1790bbb5eebfSDaniel Vetter 		 */
17910f239f4cSDaniel Vetter 
17920f239f4cSDaniel Vetter 		/* fifo underruns are filterered in the underrun handler. */
17930f239f4cSDaniel Vetter 		mask = PIPE_FIFO_UNDERRUN_STATUS;
1794bbb5eebfSDaniel Vetter 
1795bbb5eebfSDaniel Vetter 		switch (pipe) {
1796bbb5eebfSDaniel Vetter 		case PIPE_A:
1797bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
1798bbb5eebfSDaniel Vetter 			break;
1799bbb5eebfSDaniel Vetter 		case PIPE_B:
1800bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
1801bbb5eebfSDaniel Vetter 			break;
18023278f67fSVille Syrjälä 		case PIPE_C:
18033278f67fSVille Syrjälä 			iir_bit = I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
18043278f67fSVille Syrjälä 			break;
1805bbb5eebfSDaniel Vetter 		}
1806bbb5eebfSDaniel Vetter 		if (iir & iir_bit)
1807bbb5eebfSDaniel Vetter 			mask |= dev_priv->pipestat_irq_mask[pipe];
1808bbb5eebfSDaniel Vetter 
1809bbb5eebfSDaniel Vetter 		if (!mask)
181091d181ddSImre Deak 			continue;
181191d181ddSImre Deak 
181291d181ddSImre Deak 		reg = PIPESTAT(pipe);
1813bbb5eebfSDaniel Vetter 		mask |= PIPESTAT_INT_ENABLE_MASK;
1814bbb5eebfSDaniel Vetter 		pipe_stats[pipe] = I915_READ(reg) & mask;
18157e231dbeSJesse Barnes 
18167e231dbeSJesse Barnes 		/*
18177e231dbeSJesse Barnes 		 * Clear the PIPE*STAT regs before the IIR
18187e231dbeSJesse Barnes 		 */
181991d181ddSImre Deak 		if (pipe_stats[pipe] & (PIPE_FIFO_UNDERRUN_STATUS |
182091d181ddSImre Deak 					PIPESTAT_INT_STATUS_MASK))
18217e231dbeSJesse Barnes 			I915_WRITE(reg, pipe_stats[pipe]);
18227e231dbeSJesse Barnes 	}
182358ead0d7SImre Deak 	spin_unlock(&dev_priv->irq_lock);
18242ecb8ca4SVille Syrjälä }
18252ecb8ca4SVille Syrjälä 
182691d14251STvrtko Ursulin static void valleyview_pipestat_irq_handler(struct drm_i915_private *dev_priv,
18272ecb8ca4SVille Syrjälä 					    u32 pipe_stats[I915_MAX_PIPES])
18282ecb8ca4SVille Syrjälä {
18292ecb8ca4SVille Syrjälä 	enum pipe pipe;
18307e231dbeSJesse Barnes 
1831055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
18325a21b665SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
18335a21b665SDaniel Vetter 		    intel_pipe_handle_vblank(dev_priv, pipe))
18345a21b665SDaniel Vetter 			intel_check_page_flip(dev_priv, pipe);
183531acc7f5SJesse Barnes 
18365251f04eSMaarten Lankhorst 		if (pipe_stats[pipe] & PLANE_FLIP_DONE_INT_STATUS_VLV)
183751cbaf01SMaarten Lankhorst 			intel_finish_page_flip_cs(dev_priv, pipe);
18384356d586SDaniel Vetter 
18394356d586SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
184091d14251STvrtko Ursulin 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
18412d9d2b0bSVille Syrjälä 
18421f7247c0SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
18431f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
184431acc7f5SJesse Barnes 	}
184531acc7f5SJesse Barnes 
1846c1874ed7SImre Deak 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
184791d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
1848c1874ed7SImre Deak }
1849c1874ed7SImre Deak 
18501ae3c34cSVille Syrjälä static u32 i9xx_hpd_irq_ack(struct drm_i915_private *dev_priv)
185116c6c56bSVille Syrjälä {
185216c6c56bSVille Syrjälä 	u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
185316c6c56bSVille Syrjälä 
18541ae3c34cSVille Syrjälä 	if (hotplug_status)
18553ff60f89SOscar Mateo 		I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
18561ae3c34cSVille Syrjälä 
18571ae3c34cSVille Syrjälä 	return hotplug_status;
18581ae3c34cSVille Syrjälä }
18591ae3c34cSVille Syrjälä 
186091d14251STvrtko Ursulin static void i9xx_hpd_irq_handler(struct drm_i915_private *dev_priv,
18611ae3c34cSVille Syrjälä 				 u32 hotplug_status)
18621ae3c34cSVille Syrjälä {
18631ae3c34cSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
18643ff60f89SOscar Mateo 
186591d14251STvrtko Ursulin 	if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
186691d14251STvrtko Ursulin 	    IS_CHERRYVIEW(dev_priv)) {
186716c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
186816c6c56bSVille Syrjälä 
186958f2cf24SVille Syrjälä 		if (hotplug_trigger) {
1870fd63e2a9SImre Deak 			intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
1871fd63e2a9SImre Deak 					   hotplug_trigger, hpd_status_g4x,
1872fd63e2a9SImre Deak 					   i9xx_port_hotplug_long_detect);
187358f2cf24SVille Syrjälä 
187491d14251STvrtko Ursulin 			intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
187558f2cf24SVille Syrjälä 		}
1876369712e8SJani Nikula 
1877369712e8SJani Nikula 		if (hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
187891d14251STvrtko Ursulin 			dp_aux_irq_handler(dev_priv);
187916c6c56bSVille Syrjälä 	} else {
188016c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
188116c6c56bSVille Syrjälä 
188258f2cf24SVille Syrjälä 		if (hotplug_trigger) {
1883fd63e2a9SImre Deak 			intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
18844e3d1e26SVille Syrjälä 					   hotplug_trigger, hpd_status_i915,
1885fd63e2a9SImre Deak 					   i9xx_port_hotplug_long_detect);
188691d14251STvrtko Ursulin 			intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
188716c6c56bSVille Syrjälä 		}
18883ff60f89SOscar Mateo 	}
188958f2cf24SVille Syrjälä }
189016c6c56bSVille Syrjälä 
1891c1874ed7SImre Deak static irqreturn_t valleyview_irq_handler(int irq, void *arg)
1892c1874ed7SImre Deak {
189345a83f84SDaniel Vetter 	struct drm_device *dev = arg;
1894fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
1895c1874ed7SImre Deak 	irqreturn_t ret = IRQ_NONE;
1896c1874ed7SImre Deak 
18972dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
18982dd2a883SImre Deak 		return IRQ_NONE;
18992dd2a883SImre Deak 
19001f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
19011f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
19021f814dacSImre Deak 
19031e1cace9SVille Syrjälä 	do {
19046e814800SVille Syrjälä 		u32 iir, gt_iir, pm_iir;
19052ecb8ca4SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
19061ae3c34cSVille Syrjälä 		u32 hotplug_status = 0;
1907a5e485a9SVille Syrjälä 		u32 ier = 0;
19083ff60f89SOscar Mateo 
1909c1874ed7SImre Deak 		gt_iir = I915_READ(GTIIR);
1910c1874ed7SImre Deak 		pm_iir = I915_READ(GEN6_PMIIR);
19113ff60f89SOscar Mateo 		iir = I915_READ(VLV_IIR);
1912c1874ed7SImre Deak 
1913c1874ed7SImre Deak 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
19141e1cace9SVille Syrjälä 			break;
1915c1874ed7SImre Deak 
1916c1874ed7SImre Deak 		ret = IRQ_HANDLED;
1917c1874ed7SImre Deak 
1918a5e485a9SVille Syrjälä 		/*
1919a5e485a9SVille Syrjälä 		 * Theory on interrupt generation, based on empirical evidence:
1920a5e485a9SVille Syrjälä 		 *
1921a5e485a9SVille Syrjälä 		 * x = ((VLV_IIR & VLV_IER) ||
1922a5e485a9SVille Syrjälä 		 *      (((GT_IIR & GT_IER) || (GEN6_PMIIR & GEN6_PMIER)) &&
1923a5e485a9SVille Syrjälä 		 *       (VLV_MASTER_IER & MASTER_INTERRUPT_ENABLE)));
1924a5e485a9SVille Syrjälä 		 *
1925a5e485a9SVille Syrjälä 		 * A CPU interrupt will only be raised when 'x' has a 0->1 edge.
1926a5e485a9SVille Syrjälä 		 * Hence we clear MASTER_INTERRUPT_ENABLE and VLV_IER to
1927a5e485a9SVille Syrjälä 		 * guarantee the CPU interrupt will be raised again even if we
1928a5e485a9SVille Syrjälä 		 * don't end up clearing all the VLV_IIR, GT_IIR, GEN6_PMIIR
1929a5e485a9SVille Syrjälä 		 * bits this time around.
1930a5e485a9SVille Syrjälä 		 */
19314a0a0202SVille Syrjälä 		I915_WRITE(VLV_MASTER_IER, 0);
1932a5e485a9SVille Syrjälä 		ier = I915_READ(VLV_IER);
1933a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, 0);
19344a0a0202SVille Syrjälä 
19354a0a0202SVille Syrjälä 		if (gt_iir)
19364a0a0202SVille Syrjälä 			I915_WRITE(GTIIR, gt_iir);
19374a0a0202SVille Syrjälä 		if (pm_iir)
19384a0a0202SVille Syrjälä 			I915_WRITE(GEN6_PMIIR, pm_iir);
19394a0a0202SVille Syrjälä 
19407ce4d1f2SVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
19411ae3c34cSVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
19427ce4d1f2SVille Syrjälä 
19433ff60f89SOscar Mateo 		/* Call regardless, as some status bits might not be
19443ff60f89SOscar Mateo 		 * signalled in iir */
194591d14251STvrtko Ursulin 		valleyview_pipestat_irq_ack(dev_priv, iir, pipe_stats);
19467ce4d1f2SVille Syrjälä 
1947eef57324SJerome Anand 		if (iir & (I915_LPE_PIPE_A_INTERRUPT |
1948eef57324SJerome Anand 			   I915_LPE_PIPE_B_INTERRUPT))
1949eef57324SJerome Anand 			intel_lpe_audio_irq_handler(dev_priv);
1950eef57324SJerome Anand 
19517ce4d1f2SVille Syrjälä 		/*
19527ce4d1f2SVille Syrjälä 		 * VLV_IIR is single buffered, and reflects the level
19537ce4d1f2SVille Syrjälä 		 * from PIPESTAT/PORT_HOTPLUG_STAT, hence clear it last.
19547ce4d1f2SVille Syrjälä 		 */
19557ce4d1f2SVille Syrjälä 		if (iir)
19567ce4d1f2SVille Syrjälä 			I915_WRITE(VLV_IIR, iir);
19574a0a0202SVille Syrjälä 
1958a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, ier);
19594a0a0202SVille Syrjälä 		I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
19604a0a0202SVille Syrjälä 		POSTING_READ(VLV_MASTER_IER);
19611ae3c34cSVille Syrjälä 
196252894874SVille Syrjälä 		if (gt_iir)
1963261e40b8SVille Syrjälä 			snb_gt_irq_handler(dev_priv, gt_iir);
196452894874SVille Syrjälä 		if (pm_iir)
196552894874SVille Syrjälä 			gen6_rps_irq_handler(dev_priv, pm_iir);
196652894874SVille Syrjälä 
19671ae3c34cSVille Syrjälä 		if (hotplug_status)
196891d14251STvrtko Ursulin 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
19692ecb8ca4SVille Syrjälä 
197091d14251STvrtko Ursulin 		valleyview_pipestat_irq_handler(dev_priv, pipe_stats);
19711e1cace9SVille Syrjälä 	} while (0);
19727e231dbeSJesse Barnes 
19731f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
19741f814dacSImre Deak 
19757e231dbeSJesse Barnes 	return ret;
19767e231dbeSJesse Barnes }
19777e231dbeSJesse Barnes 
197843f328d7SVille Syrjälä static irqreturn_t cherryview_irq_handler(int irq, void *arg)
197943f328d7SVille Syrjälä {
198045a83f84SDaniel Vetter 	struct drm_device *dev = arg;
1981fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
198243f328d7SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
198343f328d7SVille Syrjälä 
19842dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
19852dd2a883SImre Deak 		return IRQ_NONE;
19862dd2a883SImre Deak 
19871f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
19881f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
19891f814dacSImre Deak 
1990579de73bSChris Wilson 	do {
19916e814800SVille Syrjälä 		u32 master_ctl, iir;
1992e30e251aSVille Syrjälä 		u32 gt_iir[4] = {};
19932ecb8ca4SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
19941ae3c34cSVille Syrjälä 		u32 hotplug_status = 0;
1995a5e485a9SVille Syrjälä 		u32 ier = 0;
1996a5e485a9SVille Syrjälä 
19978e5fd599SVille Syrjälä 		master_ctl = I915_READ(GEN8_MASTER_IRQ) & ~GEN8_MASTER_IRQ_CONTROL;
19983278f67fSVille Syrjälä 		iir = I915_READ(VLV_IIR);
19993278f67fSVille Syrjälä 
20003278f67fSVille Syrjälä 		if (master_ctl == 0 && iir == 0)
20018e5fd599SVille Syrjälä 			break;
200243f328d7SVille Syrjälä 
200327b6c122SOscar Mateo 		ret = IRQ_HANDLED;
200427b6c122SOscar Mateo 
2005a5e485a9SVille Syrjälä 		/*
2006a5e485a9SVille Syrjälä 		 * Theory on interrupt generation, based on empirical evidence:
2007a5e485a9SVille Syrjälä 		 *
2008a5e485a9SVille Syrjälä 		 * x = ((VLV_IIR & VLV_IER) ||
2009a5e485a9SVille Syrjälä 		 *      ((GEN8_MASTER_IRQ & ~GEN8_MASTER_IRQ_CONTROL) &&
2010a5e485a9SVille Syrjälä 		 *       (GEN8_MASTER_IRQ & GEN8_MASTER_IRQ_CONTROL)));
2011a5e485a9SVille Syrjälä 		 *
2012a5e485a9SVille Syrjälä 		 * A CPU interrupt will only be raised when 'x' has a 0->1 edge.
2013a5e485a9SVille Syrjälä 		 * Hence we clear GEN8_MASTER_IRQ_CONTROL and VLV_IER to
2014a5e485a9SVille Syrjälä 		 * guarantee the CPU interrupt will be raised again even if we
2015a5e485a9SVille Syrjälä 		 * don't end up clearing all the VLV_IIR and GEN8_MASTER_IRQ_CONTROL
2016a5e485a9SVille Syrjälä 		 * bits this time around.
2017a5e485a9SVille Syrjälä 		 */
201843f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, 0);
2019a5e485a9SVille Syrjälä 		ier = I915_READ(VLV_IER);
2020a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, 0);
202143f328d7SVille Syrjälä 
2022e30e251aSVille Syrjälä 		gen8_gt_irq_ack(dev_priv, master_ctl, gt_iir);
202327b6c122SOscar Mateo 
202427b6c122SOscar Mateo 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
20251ae3c34cSVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
202643f328d7SVille Syrjälä 
202727b6c122SOscar Mateo 		/* Call regardless, as some status bits might not be
202827b6c122SOscar Mateo 		 * signalled in iir */
202991d14251STvrtko Ursulin 		valleyview_pipestat_irq_ack(dev_priv, iir, pipe_stats);
203043f328d7SVille Syrjälä 
2031eef57324SJerome Anand 		if (iir & (I915_LPE_PIPE_A_INTERRUPT |
2032eef57324SJerome Anand 			   I915_LPE_PIPE_B_INTERRUPT |
2033eef57324SJerome Anand 			   I915_LPE_PIPE_C_INTERRUPT))
2034eef57324SJerome Anand 			intel_lpe_audio_irq_handler(dev_priv);
2035eef57324SJerome Anand 
20367ce4d1f2SVille Syrjälä 		/*
20377ce4d1f2SVille Syrjälä 		 * VLV_IIR is single buffered, and reflects the level
20387ce4d1f2SVille Syrjälä 		 * from PIPESTAT/PORT_HOTPLUG_STAT, hence clear it last.
20397ce4d1f2SVille Syrjälä 		 */
20407ce4d1f2SVille Syrjälä 		if (iir)
20417ce4d1f2SVille Syrjälä 			I915_WRITE(VLV_IIR, iir);
20427ce4d1f2SVille Syrjälä 
2043a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, ier);
2044e5328c43SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
204543f328d7SVille Syrjälä 		POSTING_READ(GEN8_MASTER_IRQ);
20461ae3c34cSVille Syrjälä 
2047e30e251aSVille Syrjälä 		gen8_gt_irq_handler(dev_priv, gt_iir);
2048e30e251aSVille Syrjälä 
20491ae3c34cSVille Syrjälä 		if (hotplug_status)
205091d14251STvrtko Ursulin 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
20512ecb8ca4SVille Syrjälä 
205291d14251STvrtko Ursulin 		valleyview_pipestat_irq_handler(dev_priv, pipe_stats);
2053579de73bSChris Wilson 	} while (0);
20543278f67fSVille Syrjälä 
20551f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
20561f814dacSImre Deak 
205743f328d7SVille Syrjälä 	return ret;
205843f328d7SVille Syrjälä }
205943f328d7SVille Syrjälä 
206091d14251STvrtko Ursulin static void ibx_hpd_irq_handler(struct drm_i915_private *dev_priv,
206191d14251STvrtko Ursulin 				u32 hotplug_trigger,
206240e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2063776ad806SJesse Barnes {
206442db67d6SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2065776ad806SJesse Barnes 
20666a39d7c9SJani Nikula 	/*
20676a39d7c9SJani Nikula 	 * Somehow the PCH doesn't seem to really ack the interrupt to the CPU
20686a39d7c9SJani Nikula 	 * unless we touch the hotplug register, even if hotplug_trigger is
20696a39d7c9SJani Nikula 	 * zero. Not acking leads to "The master control interrupt lied (SDE)!"
20706a39d7c9SJani Nikula 	 * errors.
20716a39d7c9SJani Nikula 	 */
207213cf5504SDave Airlie 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
20736a39d7c9SJani Nikula 	if (!hotplug_trigger) {
20746a39d7c9SJani Nikula 		u32 mask = PORTA_HOTPLUG_STATUS_MASK |
20756a39d7c9SJani Nikula 			PORTD_HOTPLUG_STATUS_MASK |
20766a39d7c9SJani Nikula 			PORTC_HOTPLUG_STATUS_MASK |
20776a39d7c9SJani Nikula 			PORTB_HOTPLUG_STATUS_MASK;
20786a39d7c9SJani Nikula 		dig_hotplug_reg &= ~mask;
20796a39d7c9SJani Nikula 	}
20806a39d7c9SJani Nikula 
208113cf5504SDave Airlie 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
20826a39d7c9SJani Nikula 	if (!hotplug_trigger)
20836a39d7c9SJani Nikula 		return;
208413cf5504SDave Airlie 
2085fd63e2a9SImre Deak 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
208640e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2087fd63e2a9SImre Deak 			   pch_port_hotplug_long_detect);
208840e56410SVille Syrjälä 
208991d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2090aaf5ec2eSSonika Jindal }
209191d131d2SDaniel Vetter 
209291d14251STvrtko Ursulin static void ibx_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
209340e56410SVille Syrjälä {
209440e56410SVille Syrjälä 	int pipe;
209540e56410SVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
209640e56410SVille Syrjälä 
209791d14251STvrtko Ursulin 	ibx_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ibx);
209840e56410SVille Syrjälä 
2099cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
2100cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
2101776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
2102cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
2103cfc33bf7SVille Syrjälä 				 port_name(port));
2104cfc33bf7SVille Syrjälä 	}
2105776ad806SJesse Barnes 
2106ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
210791d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
2108ce99c256SDaniel Vetter 
2109776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
211091d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
2111776ad806SJesse Barnes 
2112776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
2113776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
2114776ad806SJesse Barnes 
2115776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
2116776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
2117776ad806SJesse Barnes 
2118776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
2119776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
2120776ad806SJesse Barnes 
21219db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
2122055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
21239db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
21249db4a9c7SJesse Barnes 					 pipe_name(pipe),
21259db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
2126776ad806SJesse Barnes 
2127776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
2128776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
2129776ad806SJesse Barnes 
2130776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
2131776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
2132776ad806SJesse Barnes 
2133776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
21341f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
21358664281bSPaulo Zanoni 
21368664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
21371f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
21388664281bSPaulo Zanoni }
21398664281bSPaulo Zanoni 
214091d14251STvrtko Ursulin static void ivb_err_int_handler(struct drm_i915_private *dev_priv)
21418664281bSPaulo Zanoni {
21428664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
21435a69b89fSDaniel Vetter 	enum pipe pipe;
21448664281bSPaulo Zanoni 
2145de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
2146de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
2147de032bf4SPaulo Zanoni 
2148055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
21491f7247c0SDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe))
21501f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
21518664281bSPaulo Zanoni 
21525a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
215391d14251STvrtko Ursulin 			if (IS_IVYBRIDGE(dev_priv))
215491d14251STvrtko Ursulin 				ivb_pipe_crc_irq_handler(dev_priv, pipe);
21555a69b89fSDaniel Vetter 			else
215691d14251STvrtko Ursulin 				hsw_pipe_crc_irq_handler(dev_priv, pipe);
21575a69b89fSDaniel Vetter 		}
21585a69b89fSDaniel Vetter 	}
21598bf1e9f1SShuang He 
21608664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
21618664281bSPaulo Zanoni }
21628664281bSPaulo Zanoni 
216391d14251STvrtko Ursulin static void cpt_serr_int_handler(struct drm_i915_private *dev_priv)
21648664281bSPaulo Zanoni {
21658664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
21668664281bSPaulo Zanoni 
2167de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
2168de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
2169de032bf4SPaulo Zanoni 
21708664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
21711f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
21728664281bSPaulo Zanoni 
21738664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
21741f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
21758664281bSPaulo Zanoni 
21768664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
21771f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_C);
21788664281bSPaulo Zanoni 
21798664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
2180776ad806SJesse Barnes }
2181776ad806SJesse Barnes 
218291d14251STvrtko Ursulin static void cpt_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
218323e81d69SAdam Jackson {
218423e81d69SAdam Jackson 	int pipe;
21856dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
2186aaf5ec2eSSonika Jindal 
218791d14251STvrtko Ursulin 	ibx_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_cpt);
218891d131d2SDaniel Vetter 
2189cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
2190cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
219123e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
2192cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
2193cfc33bf7SVille Syrjälä 				 port_name(port));
2194cfc33bf7SVille Syrjälä 	}
219523e81d69SAdam Jackson 
219623e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
219791d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
219823e81d69SAdam Jackson 
219923e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
220091d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
220123e81d69SAdam Jackson 
220223e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
220323e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
220423e81d69SAdam Jackson 
220523e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
220623e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
220723e81d69SAdam Jackson 
220823e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
2209055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
221023e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
221123e81d69SAdam Jackson 					 pipe_name(pipe),
221223e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
22138664281bSPaulo Zanoni 
22148664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
221591d14251STvrtko Ursulin 		cpt_serr_int_handler(dev_priv);
221623e81d69SAdam Jackson }
221723e81d69SAdam Jackson 
221891d14251STvrtko Ursulin static void spt_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
22196dbf30ceSVille Syrjälä {
22206dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_SPT &
22216dbf30ceSVille Syrjälä 		~SDE_PORTE_HOTPLUG_SPT;
22226dbf30ceSVille Syrjälä 	u32 hotplug2_trigger = pch_iir & SDE_PORTE_HOTPLUG_SPT;
22236dbf30ceSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
22246dbf30ceSVille Syrjälä 
22256dbf30ceSVille Syrjälä 	if (hotplug_trigger) {
22266dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
22276dbf30ceSVille Syrjälä 
22286dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
22296dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
22306dbf30ceSVille Syrjälä 
22316dbf30ceSVille Syrjälä 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
22326dbf30ceSVille Syrjälä 				   dig_hotplug_reg, hpd_spt,
223374c0b395SVille Syrjälä 				   spt_port_hotplug_long_detect);
22346dbf30ceSVille Syrjälä 	}
22356dbf30ceSVille Syrjälä 
22366dbf30ceSVille Syrjälä 	if (hotplug2_trigger) {
22376dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
22386dbf30ceSVille Syrjälä 
22396dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG2);
22406dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG2, dig_hotplug_reg);
22416dbf30ceSVille Syrjälä 
22426dbf30ceSVille Syrjälä 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug2_trigger,
22436dbf30ceSVille Syrjälä 				   dig_hotplug_reg, hpd_spt,
22446dbf30ceSVille Syrjälä 				   spt_port_hotplug2_long_detect);
22456dbf30ceSVille Syrjälä 	}
22466dbf30ceSVille Syrjälä 
22476dbf30ceSVille Syrjälä 	if (pin_mask)
224891d14251STvrtko Ursulin 		intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
22496dbf30ceSVille Syrjälä 
22506dbf30ceSVille Syrjälä 	if (pch_iir & SDE_GMBUS_CPT)
225191d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
22526dbf30ceSVille Syrjälä }
22536dbf30ceSVille Syrjälä 
225491d14251STvrtko Ursulin static void ilk_hpd_irq_handler(struct drm_i915_private *dev_priv,
225591d14251STvrtko Ursulin 				u32 hotplug_trigger,
225640e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2257c008bc6eSPaulo Zanoni {
2258e4ce95aaSVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2259e4ce95aaSVille Syrjälä 
2260e4ce95aaSVille Syrjälä 	dig_hotplug_reg = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
2261e4ce95aaSVille Syrjälä 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, dig_hotplug_reg);
2262e4ce95aaSVille Syrjälä 
2263e4ce95aaSVille Syrjälä 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
226440e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2265e4ce95aaSVille Syrjälä 			   ilk_port_hotplug_long_detect);
226640e56410SVille Syrjälä 
226791d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2268e4ce95aaSVille Syrjälä }
2269c008bc6eSPaulo Zanoni 
227091d14251STvrtko Ursulin static void ilk_display_irq_handler(struct drm_i915_private *dev_priv,
227191d14251STvrtko Ursulin 				    u32 de_iir)
227240e56410SVille Syrjälä {
227340e56410SVille Syrjälä 	enum pipe pipe;
227440e56410SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG;
227540e56410SVille Syrjälä 
227640e56410SVille Syrjälä 	if (hotplug_trigger)
227791d14251STvrtko Ursulin 		ilk_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ilk);
227840e56410SVille Syrjälä 
2279c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
228091d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
2281c008bc6eSPaulo Zanoni 
2282c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
228391d14251STvrtko Ursulin 		intel_opregion_asle_intr(dev_priv);
2284c008bc6eSPaulo Zanoni 
2285c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
2286c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
2287c008bc6eSPaulo Zanoni 
2288055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
22895a21b665SDaniel Vetter 		if (de_iir & DE_PIPE_VBLANK(pipe) &&
22905a21b665SDaniel Vetter 		    intel_pipe_handle_vblank(dev_priv, pipe))
22915a21b665SDaniel Vetter 			intel_check_page_flip(dev_priv, pipe);
2292c008bc6eSPaulo Zanoni 
229340da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
22941f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
2295c008bc6eSPaulo Zanoni 
229640da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
229791d14251STvrtko Ursulin 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
22985b3a856bSDaniel Vetter 
229940da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
23005251f04eSMaarten Lankhorst 		if (de_iir & DE_PLANE_FLIP_DONE(pipe))
230151cbaf01SMaarten Lankhorst 			intel_finish_page_flip_cs(dev_priv, pipe);
2302c008bc6eSPaulo Zanoni 	}
2303c008bc6eSPaulo Zanoni 
2304c008bc6eSPaulo Zanoni 	/* check event from PCH */
2305c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
2306c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
2307c008bc6eSPaulo Zanoni 
230891d14251STvrtko Ursulin 		if (HAS_PCH_CPT(dev_priv))
230991d14251STvrtko Ursulin 			cpt_irq_handler(dev_priv, pch_iir);
2310c008bc6eSPaulo Zanoni 		else
231191d14251STvrtko Ursulin 			ibx_irq_handler(dev_priv, pch_iir);
2312c008bc6eSPaulo Zanoni 
2313c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
2314c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
2315c008bc6eSPaulo Zanoni 	}
2316c008bc6eSPaulo Zanoni 
231791d14251STvrtko Ursulin 	if (IS_GEN5(dev_priv) && de_iir & DE_PCU_EVENT)
231891d14251STvrtko Ursulin 		ironlake_rps_change_irq_handler(dev_priv);
2319c008bc6eSPaulo Zanoni }
2320c008bc6eSPaulo Zanoni 
232191d14251STvrtko Ursulin static void ivb_display_irq_handler(struct drm_i915_private *dev_priv,
232291d14251STvrtko Ursulin 				    u32 de_iir)
23239719fb98SPaulo Zanoni {
232407d27e20SDamien Lespiau 	enum pipe pipe;
232523bb4cb5SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG_IVB;
232623bb4cb5SVille Syrjälä 
232740e56410SVille Syrjälä 	if (hotplug_trigger)
232891d14251STvrtko Ursulin 		ilk_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ivb);
23299719fb98SPaulo Zanoni 
23309719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
233191d14251STvrtko Ursulin 		ivb_err_int_handler(dev_priv);
23329719fb98SPaulo Zanoni 
23339719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
233491d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
23359719fb98SPaulo Zanoni 
23369719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
233791d14251STvrtko Ursulin 		intel_opregion_asle_intr(dev_priv);
23389719fb98SPaulo Zanoni 
2339055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
23405a21b665SDaniel Vetter 		if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)) &&
23415a21b665SDaniel Vetter 		    intel_pipe_handle_vblank(dev_priv, pipe))
23425a21b665SDaniel Vetter 			intel_check_page_flip(dev_priv, pipe);
234340da17c2SDaniel Vetter 
234440da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
23455251f04eSMaarten Lankhorst 		if (de_iir & DE_PLANE_FLIP_DONE_IVB(pipe))
234651cbaf01SMaarten Lankhorst 			intel_finish_page_flip_cs(dev_priv, pipe);
23479719fb98SPaulo Zanoni 	}
23489719fb98SPaulo Zanoni 
23499719fb98SPaulo Zanoni 	/* check event from PCH */
235091d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv) && (de_iir & DE_PCH_EVENT_IVB)) {
23519719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
23529719fb98SPaulo Zanoni 
235391d14251STvrtko Ursulin 		cpt_irq_handler(dev_priv, pch_iir);
23549719fb98SPaulo Zanoni 
23559719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
23569719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
23579719fb98SPaulo Zanoni 	}
23589719fb98SPaulo Zanoni }
23599719fb98SPaulo Zanoni 
236072c90f62SOscar Mateo /*
236172c90f62SOscar Mateo  * To handle irqs with the minimum potential races with fresh interrupts, we:
236272c90f62SOscar Mateo  * 1 - Disable Master Interrupt Control.
236372c90f62SOscar Mateo  * 2 - Find the source(s) of the interrupt.
236472c90f62SOscar Mateo  * 3 - Clear the Interrupt Identity bits (IIR).
236572c90f62SOscar Mateo  * 4 - Process the interrupt(s) that had bits set in the IIRs.
236672c90f62SOscar Mateo  * 5 - Re-enable Master Interrupt Control.
236772c90f62SOscar Mateo  */
2368f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
2369b1f14ad0SJesse Barnes {
237045a83f84SDaniel Vetter 	struct drm_device *dev = arg;
2371fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2372f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
23730e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
2374b1f14ad0SJesse Barnes 
23752dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
23762dd2a883SImre Deak 		return IRQ_NONE;
23772dd2a883SImre Deak 
23781f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
23791f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
23801f814dacSImre Deak 
2381b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
2382b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
2383b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
238423a78516SPaulo Zanoni 	POSTING_READ(DEIER);
23850e43406bSChris Wilson 
238644498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
238744498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
238844498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
238944498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
239044498aeaSPaulo Zanoni 	 * due to its back queue). */
239191d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv)) {
239244498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
239344498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
239444498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2395ab5c608bSBen Widawsky 	}
239644498aeaSPaulo Zanoni 
239772c90f62SOscar Mateo 	/* Find, clear, then process each source of interrupt */
239872c90f62SOscar Mateo 
23990e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
24000e43406bSChris Wilson 	if (gt_iir) {
240172c90f62SOscar Mateo 		I915_WRITE(GTIIR, gt_iir);
240272c90f62SOscar Mateo 		ret = IRQ_HANDLED;
240391d14251STvrtko Ursulin 		if (INTEL_GEN(dev_priv) >= 6)
2404261e40b8SVille Syrjälä 			snb_gt_irq_handler(dev_priv, gt_iir);
2405d8fc8a47SPaulo Zanoni 		else
2406261e40b8SVille Syrjälä 			ilk_gt_irq_handler(dev_priv, gt_iir);
24070e43406bSChris Wilson 	}
2408b1f14ad0SJesse Barnes 
2409b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
24100e43406bSChris Wilson 	if (de_iir) {
241172c90f62SOscar Mateo 		I915_WRITE(DEIIR, de_iir);
241272c90f62SOscar Mateo 		ret = IRQ_HANDLED;
241391d14251STvrtko Ursulin 		if (INTEL_GEN(dev_priv) >= 7)
241491d14251STvrtko Ursulin 			ivb_display_irq_handler(dev_priv, de_iir);
2415f1af8fc1SPaulo Zanoni 		else
241691d14251STvrtko Ursulin 			ilk_display_irq_handler(dev_priv, de_iir);
24170e43406bSChris Wilson 	}
24180e43406bSChris Wilson 
241991d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 6) {
2420f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
24210e43406bSChris Wilson 		if (pm_iir) {
2422b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
24230e43406bSChris Wilson 			ret = IRQ_HANDLED;
242472c90f62SOscar Mateo 			gen6_rps_irq_handler(dev_priv, pm_iir);
24250e43406bSChris Wilson 		}
2426f1af8fc1SPaulo Zanoni 	}
2427b1f14ad0SJesse Barnes 
2428b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
2429b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
243091d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv)) {
243144498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
243244498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2433ab5c608bSBen Widawsky 	}
2434b1f14ad0SJesse Barnes 
24351f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
24361f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
24371f814dacSImre Deak 
2438b1f14ad0SJesse Barnes 	return ret;
2439b1f14ad0SJesse Barnes }
2440b1f14ad0SJesse Barnes 
244191d14251STvrtko Ursulin static void bxt_hpd_irq_handler(struct drm_i915_private *dev_priv,
244291d14251STvrtko Ursulin 				u32 hotplug_trigger,
244340e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2444d04a492dSShashank Sharma {
2445cebd87a0SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2446d04a492dSShashank Sharma 
2447a52bb15bSVille Syrjälä 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
2448a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
2449d04a492dSShashank Sharma 
2450cebd87a0SVille Syrjälä 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
245140e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2452cebd87a0SVille Syrjälä 			   bxt_port_hotplug_long_detect);
245340e56410SVille Syrjälä 
245491d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2455d04a492dSShashank Sharma }
2456d04a492dSShashank Sharma 
2457f11a0f46STvrtko Ursulin static irqreturn_t
2458f11a0f46STvrtko Ursulin gen8_de_irq_handler(struct drm_i915_private *dev_priv, u32 master_ctl)
2459abd58f01SBen Widawsky {
2460abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
2461f11a0f46STvrtko Ursulin 	u32 iir;
2462c42664ccSDaniel Vetter 	enum pipe pipe;
246388e04703SJesse Barnes 
2464abd58f01SBen Widawsky 	if (master_ctl & GEN8_DE_MISC_IRQ) {
2465e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_MISC_IIR);
2466e32192e1STvrtko Ursulin 		if (iir) {
2467e32192e1STvrtko Ursulin 			I915_WRITE(GEN8_DE_MISC_IIR, iir);
2468abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
2469e32192e1STvrtko Ursulin 			if (iir & GEN8_DE_MISC_GSE)
247091d14251STvrtko Ursulin 				intel_opregion_asle_intr(dev_priv);
247138cc46d7SOscar Mateo 			else
247238cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Misc interrupt\n");
2473abd58f01SBen Widawsky 		}
247438cc46d7SOscar Mateo 		else
247538cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
2476abd58f01SBen Widawsky 	}
2477abd58f01SBen Widawsky 
24786d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
2479e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_PORT_IIR);
2480e32192e1STvrtko Ursulin 		if (iir) {
2481e32192e1STvrtko Ursulin 			u32 tmp_mask;
2482d04a492dSShashank Sharma 			bool found = false;
2483cebd87a0SVille Syrjälä 
2484e32192e1STvrtko Ursulin 			I915_WRITE(GEN8_DE_PORT_IIR, iir);
24856d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
248688e04703SJesse Barnes 
2487e32192e1STvrtko Ursulin 			tmp_mask = GEN8_AUX_CHANNEL_A;
2488e32192e1STvrtko Ursulin 			if (INTEL_INFO(dev_priv)->gen >= 9)
2489e32192e1STvrtko Ursulin 				tmp_mask |= GEN9_AUX_CHANNEL_B |
2490e32192e1STvrtko Ursulin 					    GEN9_AUX_CHANNEL_C |
2491e32192e1STvrtko Ursulin 					    GEN9_AUX_CHANNEL_D;
2492e32192e1STvrtko Ursulin 
2493e32192e1STvrtko Ursulin 			if (iir & tmp_mask) {
249491d14251STvrtko Ursulin 				dp_aux_irq_handler(dev_priv);
2495d04a492dSShashank Sharma 				found = true;
2496d04a492dSShashank Sharma 			}
2497d04a492dSShashank Sharma 
2498cc3f90f0SAnder Conselvan de Oliveira 			if (IS_GEN9_LP(dev_priv)) {
2499e32192e1STvrtko Ursulin 				tmp_mask = iir & BXT_DE_PORT_HOTPLUG_MASK;
2500e32192e1STvrtko Ursulin 				if (tmp_mask) {
250191d14251STvrtko Ursulin 					bxt_hpd_irq_handler(dev_priv, tmp_mask,
250291d14251STvrtko Ursulin 							    hpd_bxt);
2503d04a492dSShashank Sharma 					found = true;
2504d04a492dSShashank Sharma 				}
2505e32192e1STvrtko Ursulin 			} else if (IS_BROADWELL(dev_priv)) {
2506e32192e1STvrtko Ursulin 				tmp_mask = iir & GEN8_PORT_DP_A_HOTPLUG;
2507e32192e1STvrtko Ursulin 				if (tmp_mask) {
250891d14251STvrtko Ursulin 					ilk_hpd_irq_handler(dev_priv,
250991d14251STvrtko Ursulin 							    tmp_mask, hpd_bdw);
2510e32192e1STvrtko Ursulin 					found = true;
2511e32192e1STvrtko Ursulin 				}
2512e32192e1STvrtko Ursulin 			}
2513d04a492dSShashank Sharma 
2514cc3f90f0SAnder Conselvan de Oliveira 			if (IS_GEN9_LP(dev_priv) && (iir & BXT_DE_PORT_GMBUS)) {
251591d14251STvrtko Ursulin 				gmbus_irq_handler(dev_priv);
25169e63743eSShashank Sharma 				found = true;
25179e63743eSShashank Sharma 			}
25189e63743eSShashank Sharma 
2519d04a492dSShashank Sharma 			if (!found)
252038cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Port interrupt\n");
25216d766f02SDaniel Vetter 		}
252238cc46d7SOscar Mateo 		else
252338cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
25246d766f02SDaniel Vetter 	}
25256d766f02SDaniel Vetter 
2526055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2527e32192e1STvrtko Ursulin 		u32 flip_done, fault_errors;
2528abd58f01SBen Widawsky 
2529c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2530c42664ccSDaniel Vetter 			continue;
2531c42664ccSDaniel Vetter 
2532e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
2533e32192e1STvrtko Ursulin 		if (!iir) {
2534e32192e1STvrtko Ursulin 			DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
2535e32192e1STvrtko Ursulin 			continue;
2536e32192e1STvrtko Ursulin 		}
2537770de83dSDamien Lespiau 
2538e32192e1STvrtko Ursulin 		ret = IRQ_HANDLED;
2539e32192e1STvrtko Ursulin 		I915_WRITE(GEN8_DE_PIPE_IIR(pipe), iir);
2540e32192e1STvrtko Ursulin 
25415a21b665SDaniel Vetter 		if (iir & GEN8_PIPE_VBLANK &&
25425a21b665SDaniel Vetter 		    intel_pipe_handle_vblank(dev_priv, pipe))
25435a21b665SDaniel Vetter 			intel_check_page_flip(dev_priv, pipe);
2544abd58f01SBen Widawsky 
2545e32192e1STvrtko Ursulin 		flip_done = iir;
2546b4834a50SRodrigo Vivi 		if (INTEL_INFO(dev_priv)->gen >= 9)
2547e32192e1STvrtko Ursulin 			flip_done &= GEN9_PIPE_PLANE1_FLIP_DONE;
2548770de83dSDamien Lespiau 		else
2549e32192e1STvrtko Ursulin 			flip_done &= GEN8_PIPE_PRIMARY_FLIP_DONE;
2550770de83dSDamien Lespiau 
25515251f04eSMaarten Lankhorst 		if (flip_done)
255251cbaf01SMaarten Lankhorst 			intel_finish_page_flip_cs(dev_priv, pipe);
2553abd58f01SBen Widawsky 
2554e32192e1STvrtko Ursulin 		if (iir & GEN8_PIPE_CDCLK_CRC_DONE)
255591d14251STvrtko Ursulin 			hsw_pipe_crc_irq_handler(dev_priv, pipe);
25560fbe7870SDaniel Vetter 
2557e32192e1STvrtko Ursulin 		if (iir & GEN8_PIPE_FIFO_UNDERRUN)
2558e32192e1STvrtko Ursulin 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
255938d83c96SDaniel Vetter 
2560e32192e1STvrtko Ursulin 		fault_errors = iir;
2561b4834a50SRodrigo Vivi 		if (INTEL_INFO(dev_priv)->gen >= 9)
2562e32192e1STvrtko Ursulin 			fault_errors &= GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
2563770de83dSDamien Lespiau 		else
2564e32192e1STvrtko Ursulin 			fault_errors &= GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
2565770de83dSDamien Lespiau 
2566770de83dSDamien Lespiau 		if (fault_errors)
25671353ec38STvrtko Ursulin 			DRM_ERROR("Fault errors on pipe %c: 0x%08x\n",
256830100f2bSDaniel Vetter 				  pipe_name(pipe),
2569e32192e1STvrtko Ursulin 				  fault_errors);
2570abd58f01SBen Widawsky 	}
2571abd58f01SBen Widawsky 
257291d14251STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv) && !HAS_PCH_NOP(dev_priv) &&
2573266ea3d9SShashank Sharma 	    master_ctl & GEN8_DE_PCH_IRQ) {
257492d03a80SDaniel Vetter 		/*
257592d03a80SDaniel Vetter 		 * FIXME(BDW): Assume for now that the new interrupt handling
257692d03a80SDaniel Vetter 		 * scheme also closed the SDE interrupt handling race we've seen
257792d03a80SDaniel Vetter 		 * on older pch-split platforms. But this needs testing.
257892d03a80SDaniel Vetter 		 */
2579e32192e1STvrtko Ursulin 		iir = I915_READ(SDEIIR);
2580e32192e1STvrtko Ursulin 		if (iir) {
2581e32192e1STvrtko Ursulin 			I915_WRITE(SDEIIR, iir);
258292d03a80SDaniel Vetter 			ret = IRQ_HANDLED;
25836dbf30ceSVille Syrjälä 
258422dea0beSRodrigo Vivi 			if (HAS_PCH_SPT(dev_priv) || HAS_PCH_KBP(dev_priv))
258591d14251STvrtko Ursulin 				spt_irq_handler(dev_priv, iir);
25866dbf30ceSVille Syrjälä 			else
258791d14251STvrtko Ursulin 				cpt_irq_handler(dev_priv, iir);
25882dfb0b81SJani Nikula 		} else {
25892dfb0b81SJani Nikula 			/*
25902dfb0b81SJani Nikula 			 * Like on previous PCH there seems to be something
25912dfb0b81SJani Nikula 			 * fishy going on with forwarding PCH interrupts.
25922dfb0b81SJani Nikula 			 */
25932dfb0b81SJani Nikula 			DRM_DEBUG_DRIVER("The master control interrupt lied (SDE)!\n");
25942dfb0b81SJani Nikula 		}
259592d03a80SDaniel Vetter 	}
259692d03a80SDaniel Vetter 
2597f11a0f46STvrtko Ursulin 	return ret;
2598f11a0f46STvrtko Ursulin }
2599f11a0f46STvrtko Ursulin 
2600f11a0f46STvrtko Ursulin static irqreturn_t gen8_irq_handler(int irq, void *arg)
2601f11a0f46STvrtko Ursulin {
2602f11a0f46STvrtko Ursulin 	struct drm_device *dev = arg;
2603fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2604f11a0f46STvrtko Ursulin 	u32 master_ctl;
2605e30e251aSVille Syrjälä 	u32 gt_iir[4] = {};
2606f11a0f46STvrtko Ursulin 	irqreturn_t ret;
2607f11a0f46STvrtko Ursulin 
2608f11a0f46STvrtko Ursulin 	if (!intel_irqs_enabled(dev_priv))
2609f11a0f46STvrtko Ursulin 		return IRQ_NONE;
2610f11a0f46STvrtko Ursulin 
2611f11a0f46STvrtko Ursulin 	master_ctl = I915_READ_FW(GEN8_MASTER_IRQ);
2612f11a0f46STvrtko Ursulin 	master_ctl &= ~GEN8_MASTER_IRQ_CONTROL;
2613f11a0f46STvrtko Ursulin 	if (!master_ctl)
2614f11a0f46STvrtko Ursulin 		return IRQ_NONE;
2615f11a0f46STvrtko Ursulin 
2616f11a0f46STvrtko Ursulin 	I915_WRITE_FW(GEN8_MASTER_IRQ, 0);
2617f11a0f46STvrtko Ursulin 
2618f11a0f46STvrtko Ursulin 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
2619f11a0f46STvrtko Ursulin 	disable_rpm_wakeref_asserts(dev_priv);
2620f11a0f46STvrtko Ursulin 
2621f11a0f46STvrtko Ursulin 	/* Find, clear, then process each source of interrupt */
2622e30e251aSVille Syrjälä 	ret = gen8_gt_irq_ack(dev_priv, master_ctl, gt_iir);
2623e30e251aSVille Syrjälä 	gen8_gt_irq_handler(dev_priv, gt_iir);
2624f11a0f46STvrtko Ursulin 	ret |= gen8_de_irq_handler(dev_priv, master_ctl);
2625f11a0f46STvrtko Ursulin 
2626cb0d205eSChris Wilson 	I915_WRITE_FW(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
2627cb0d205eSChris Wilson 	POSTING_READ_FW(GEN8_MASTER_IRQ);
2628abd58f01SBen Widawsky 
26291f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
26301f814dacSImre Deak 
2631abd58f01SBen Widawsky 	return ret;
2632abd58f01SBen Widawsky }
2633abd58f01SBen Widawsky 
26341f15b76fSChris Wilson static void i915_error_wake_up(struct drm_i915_private *dev_priv)
263517e1df07SDaniel Vetter {
263617e1df07SDaniel Vetter 	/*
263717e1df07SDaniel Vetter 	 * Notify all waiters for GPU completion events that reset state has
263817e1df07SDaniel Vetter 	 * been changed, and that they need to restart their wait after
263917e1df07SDaniel Vetter 	 * checking for potential errors (and bail out to drop locks if there is
264017e1df07SDaniel Vetter 	 * a gpu reset pending so that i915_error_work_func can acquire them).
264117e1df07SDaniel Vetter 	 */
264217e1df07SDaniel Vetter 
264317e1df07SDaniel Vetter 	/* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
26441f15b76fSChris Wilson 	wake_up_all(&dev_priv->gpu_error.wait_queue);
264517e1df07SDaniel Vetter 
264617e1df07SDaniel Vetter 	/* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
264717e1df07SDaniel Vetter 	wake_up_all(&dev_priv->pending_flip_queue);
264817e1df07SDaniel Vetter }
264917e1df07SDaniel Vetter 
26508a905236SJesse Barnes /**
2651b8d24a06SMika Kuoppala  * i915_reset_and_wakeup - do process context error handling work
265214bb2c11STvrtko Ursulin  * @dev_priv: i915 device private
26538a905236SJesse Barnes  *
26548a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
26558a905236SJesse Barnes  * was detected.
26568a905236SJesse Barnes  */
2657c033666aSChris Wilson static void i915_reset_and_wakeup(struct drm_i915_private *dev_priv)
26588a905236SJesse Barnes {
265991c8a326SChris Wilson 	struct kobject *kobj = &dev_priv->drm.primary->kdev->kobj;
2660cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
2661cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
2662cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
26638a905236SJesse Barnes 
2664c033666aSChris Wilson 	kobject_uevent_env(kobj, KOBJ_CHANGE, error_event);
26658a905236SJesse Barnes 
266644d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("resetting chip\n");
2667c033666aSChris Wilson 	kobject_uevent_env(kobj, KOBJ_CHANGE, reset_event);
26681f83fee0SDaniel Vetter 
2669c033666aSChris Wilson 	intel_prepare_reset(dev_priv);
26707514747dSVille Syrjälä 
2671780f262aSChris Wilson 	do {
2672f454c694SImre Deak 		/*
267317e1df07SDaniel Vetter 		 * All state reset _must_ be completed before we update the
267417e1df07SDaniel Vetter 		 * reset counter, for otherwise waiters might miss the reset
267517e1df07SDaniel Vetter 		 * pending state and not properly drop locks, resulting in
267617e1df07SDaniel Vetter 		 * deadlocks with the reset work.
267717e1df07SDaniel Vetter 		 */
2678780f262aSChris Wilson 		if (mutex_trylock(&dev_priv->drm.struct_mutex)) {
2679780f262aSChris Wilson 			i915_reset(dev_priv);
2680221fe799SChris Wilson 			mutex_unlock(&dev_priv->drm.struct_mutex);
2681780f262aSChris Wilson 		}
2682780f262aSChris Wilson 
2683780f262aSChris Wilson 		/* We need to wait for anyone holding the lock to wakeup */
2684780f262aSChris Wilson 	} while (wait_on_bit_timeout(&dev_priv->gpu_error.flags,
2685780f262aSChris Wilson 				     I915_RESET_IN_PROGRESS,
2686780f262aSChris Wilson 				     TASK_UNINTERRUPTIBLE,
2687780f262aSChris Wilson 				     HZ));
2688f69061beSDaniel Vetter 
2689c033666aSChris Wilson 	intel_finish_reset(dev_priv);
2690f454c694SImre Deak 
2691780f262aSChris Wilson 	if (!test_bit(I915_WEDGED, &dev_priv->gpu_error.flags))
2692c033666aSChris Wilson 		kobject_uevent_env(kobj,
2693f69061beSDaniel Vetter 				   KOBJ_CHANGE, reset_done_event);
26941f83fee0SDaniel Vetter 
269517e1df07SDaniel Vetter 	/*
269617e1df07SDaniel Vetter 	 * Note: The wake_up also serves as a memory barrier so that
26978af29b0cSChris Wilson 	 * waiters see the updated value of the dev_priv->gpu_error.
269817e1df07SDaniel Vetter 	 */
26991f15b76fSChris Wilson 	wake_up_all(&dev_priv->gpu_error.reset_queue);
2700f316a42cSBen Gamari }
27018a905236SJesse Barnes 
2702d636951eSBen Widawsky static inline void
2703d636951eSBen Widawsky i915_err_print_instdone(struct drm_i915_private *dev_priv,
2704d636951eSBen Widawsky 			struct intel_instdone *instdone)
2705d636951eSBen Widawsky {
2706f9e61372SBen Widawsky 	int slice;
2707f9e61372SBen Widawsky 	int subslice;
2708f9e61372SBen Widawsky 
2709d636951eSBen Widawsky 	pr_err("  INSTDONE: 0x%08x\n", instdone->instdone);
2710d636951eSBen Widawsky 
2711d636951eSBen Widawsky 	if (INTEL_GEN(dev_priv) <= 3)
2712d636951eSBen Widawsky 		return;
2713d636951eSBen Widawsky 
2714d636951eSBen Widawsky 	pr_err("  SC_INSTDONE: 0x%08x\n", instdone->slice_common);
2715d636951eSBen Widawsky 
2716d636951eSBen Widawsky 	if (INTEL_GEN(dev_priv) <= 6)
2717d636951eSBen Widawsky 		return;
2718d636951eSBen Widawsky 
2719f9e61372SBen Widawsky 	for_each_instdone_slice_subslice(dev_priv, slice, subslice)
2720f9e61372SBen Widawsky 		pr_err("  SAMPLER_INSTDONE[%d][%d]: 0x%08x\n",
2721f9e61372SBen Widawsky 		       slice, subslice, instdone->sampler[slice][subslice]);
2722f9e61372SBen Widawsky 
2723f9e61372SBen Widawsky 	for_each_instdone_slice_subslice(dev_priv, slice, subslice)
2724f9e61372SBen Widawsky 		pr_err("  ROW_INSTDONE[%d][%d]: 0x%08x\n",
2725f9e61372SBen Widawsky 		       slice, subslice, instdone->row[slice][subslice]);
2726d636951eSBen Widawsky }
2727d636951eSBen Widawsky 
2728eaa14c24SChris Wilson static void i915_clear_error_registers(struct drm_i915_private *dev_priv)
2729c0e09200SDave Airlie {
2730eaa14c24SChris Wilson 	u32 eir;
273163eeaf38SJesse Barnes 
2732eaa14c24SChris Wilson 	if (!IS_GEN2(dev_priv))
2733eaa14c24SChris Wilson 		I915_WRITE(PGTBL_ER, I915_READ(PGTBL_ER));
273463eeaf38SJesse Barnes 
2735eaa14c24SChris Wilson 	if (INTEL_GEN(dev_priv) < 4)
2736eaa14c24SChris Wilson 		I915_WRITE(IPEIR, I915_READ(IPEIR));
2737eaa14c24SChris Wilson 	else
2738eaa14c24SChris Wilson 		I915_WRITE(IPEIR_I965, I915_READ(IPEIR_I965));
27398a905236SJesse Barnes 
2740eaa14c24SChris Wilson 	I915_WRITE(EIR, I915_READ(EIR));
274163eeaf38SJesse Barnes 	eir = I915_READ(EIR);
274263eeaf38SJesse Barnes 	if (eir) {
274363eeaf38SJesse Barnes 		/*
274463eeaf38SJesse Barnes 		 * some errors might have become stuck,
274563eeaf38SJesse Barnes 		 * mask them.
274663eeaf38SJesse Barnes 		 */
2747eaa14c24SChris Wilson 		DRM_DEBUG_DRIVER("EIR stuck: 0x%08x, masking\n", eir);
274863eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
274963eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
275063eeaf38SJesse Barnes 	}
275135aed2e6SChris Wilson }
275235aed2e6SChris Wilson 
275335aed2e6SChris Wilson /**
2754b8d24a06SMika Kuoppala  * i915_handle_error - handle a gpu error
275514bb2c11STvrtko Ursulin  * @dev_priv: i915 device private
275614b730fcSarun.siluvery@linux.intel.com  * @engine_mask: mask representing engines that are hung
275787c390b6SMichel Thierry  * @fmt: Error message format string
275887c390b6SMichel Thierry  *
2759aafd8581SJavier Martinez Canillas  * Do some basic checking of register state at error time and
276035aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
276135aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
276235aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
276335aed2e6SChris Wilson  * of a ring dump etc.).
276435aed2e6SChris Wilson  */
2765c033666aSChris Wilson void i915_handle_error(struct drm_i915_private *dev_priv,
2766c033666aSChris Wilson 		       u32 engine_mask,
276758174462SMika Kuoppala 		       const char *fmt, ...)
276835aed2e6SChris Wilson {
276958174462SMika Kuoppala 	va_list args;
277058174462SMika Kuoppala 	char error_msg[80];
277135aed2e6SChris Wilson 
277258174462SMika Kuoppala 	va_start(args, fmt);
277358174462SMika Kuoppala 	vscnprintf(error_msg, sizeof(error_msg), fmt, args);
277458174462SMika Kuoppala 	va_end(args);
277558174462SMika Kuoppala 
27761604a86dSChris Wilson 	/*
27771604a86dSChris Wilson 	 * In most cases it's guaranteed that we get here with an RPM
27781604a86dSChris Wilson 	 * reference held, for example because there is a pending GPU
27791604a86dSChris Wilson 	 * request that won't finish until the reset is done. This
27801604a86dSChris Wilson 	 * isn't the case at least when we get here by doing a
27811604a86dSChris Wilson 	 * simulated reset via debugfs, so get an RPM reference.
27821604a86dSChris Wilson 	 */
27831604a86dSChris Wilson 	intel_runtime_pm_get(dev_priv);
27841604a86dSChris Wilson 
2785c033666aSChris Wilson 	i915_capture_error_state(dev_priv, engine_mask, error_msg);
2786eaa14c24SChris Wilson 	i915_clear_error_registers(dev_priv);
27878a905236SJesse Barnes 
27888af29b0cSChris Wilson 	if (!engine_mask)
27891604a86dSChris Wilson 		goto out;
27908af29b0cSChris Wilson 
27918af29b0cSChris Wilson 	if (test_and_set_bit(I915_RESET_IN_PROGRESS,
27928af29b0cSChris Wilson 			     &dev_priv->gpu_error.flags))
27931604a86dSChris Wilson 		goto out;
2794ba1234d1SBen Gamari 
279511ed50ecSBen Gamari 	/*
2796b8d24a06SMika Kuoppala 	 * Wakeup waiting processes so that the reset function
2797b8d24a06SMika Kuoppala 	 * i915_reset_and_wakeup doesn't deadlock trying to grab
2798b8d24a06SMika Kuoppala 	 * various locks. By bumping the reset counter first, the woken
279917e1df07SDaniel Vetter 	 * processes will see a reset in progress and back off,
280017e1df07SDaniel Vetter 	 * releasing their locks and then wait for the reset completion.
280117e1df07SDaniel Vetter 	 * We must do this for _all_ gpu waiters that might hold locks
280217e1df07SDaniel Vetter 	 * that the reset work needs to acquire.
280317e1df07SDaniel Vetter 	 *
28048af29b0cSChris Wilson 	 * Note: The wake_up also provides a memory barrier to ensure that the
28058af29b0cSChris Wilson 	 * waiters see the updated value of the reset flags.
280611ed50ecSBen Gamari 	 */
28071f15b76fSChris Wilson 	i915_error_wake_up(dev_priv);
280811ed50ecSBen Gamari 
2809c033666aSChris Wilson 	i915_reset_and_wakeup(dev_priv);
28101604a86dSChris Wilson 
28111604a86dSChris Wilson out:
28121604a86dSChris Wilson 	intel_runtime_pm_put(dev_priv);
28138a905236SJesse Barnes }
28148a905236SJesse Barnes 
281542f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
281642f52ef8SKeith Packard  * we use as a pipe index
281742f52ef8SKeith Packard  */
281886e83e35SChris Wilson static int i8xx_enable_vblank(struct drm_device *dev, unsigned int pipe)
28190a3e67a4SJesse Barnes {
2820fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2821e9d21d7fSKeith Packard 	unsigned long irqflags;
282271e0ffa5SJesse Barnes 
28231ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
282486e83e35SChris Wilson 	i915_enable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_STATUS);
282586e83e35SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
282686e83e35SChris Wilson 
282786e83e35SChris Wilson 	return 0;
282886e83e35SChris Wilson }
282986e83e35SChris Wilson 
283086e83e35SChris Wilson static int i965_enable_vblank(struct drm_device *dev, unsigned int pipe)
283186e83e35SChris Wilson {
283286e83e35SChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
283386e83e35SChris Wilson 	unsigned long irqflags;
283486e83e35SChris Wilson 
283586e83e35SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
28367c463586SKeith Packard 	i915_enable_pipestat(dev_priv, pipe,
2837755e9019SImre Deak 			     PIPE_START_VBLANK_INTERRUPT_STATUS);
28381ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
28398692d00eSChris Wilson 
28400a3e67a4SJesse Barnes 	return 0;
28410a3e67a4SJesse Barnes }
28420a3e67a4SJesse Barnes 
284388e72717SThierry Reding static int ironlake_enable_vblank(struct drm_device *dev, unsigned int pipe)
2844f796cf8fSJesse Barnes {
2845fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2846f796cf8fSJesse Barnes 	unsigned long irqflags;
284755b8f2a7STvrtko Ursulin 	uint32_t bit = INTEL_GEN(dev_priv) >= 7 ?
284886e83e35SChris Wilson 		DE_PIPE_VBLANK_IVB(pipe) : DE_PIPE_VBLANK(pipe);
2849f796cf8fSJesse Barnes 
2850f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2851fbdedaeaSVille Syrjälä 	ilk_enable_display_irq(dev_priv, bit);
2852b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2853b1f14ad0SJesse Barnes 
2854b1f14ad0SJesse Barnes 	return 0;
2855b1f14ad0SJesse Barnes }
2856b1f14ad0SJesse Barnes 
285788e72717SThierry Reding static int gen8_enable_vblank(struct drm_device *dev, unsigned int pipe)
2858abd58f01SBen Widawsky {
2859fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2860abd58f01SBen Widawsky 	unsigned long irqflags;
2861abd58f01SBen Widawsky 
2862abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2863013d3752SVille Syrjälä 	bdw_enable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
2864abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2865013d3752SVille Syrjälä 
2866abd58f01SBen Widawsky 	return 0;
2867abd58f01SBen Widawsky }
2868abd58f01SBen Widawsky 
286942f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
287042f52ef8SKeith Packard  * we use as a pipe index
287142f52ef8SKeith Packard  */
287286e83e35SChris Wilson static void i8xx_disable_vblank(struct drm_device *dev, unsigned int pipe)
287386e83e35SChris Wilson {
287486e83e35SChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
287586e83e35SChris Wilson 	unsigned long irqflags;
287686e83e35SChris Wilson 
287786e83e35SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
287886e83e35SChris Wilson 	i915_disable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_STATUS);
287986e83e35SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
288086e83e35SChris Wilson }
288186e83e35SChris Wilson 
288286e83e35SChris Wilson static void i965_disable_vblank(struct drm_device *dev, unsigned int pipe)
28830a3e67a4SJesse Barnes {
2884fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2885e9d21d7fSKeith Packard 	unsigned long irqflags;
28860a3e67a4SJesse Barnes 
28871ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
28887c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
2889755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
28901ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
28910a3e67a4SJesse Barnes }
28920a3e67a4SJesse Barnes 
289388e72717SThierry Reding static void ironlake_disable_vblank(struct drm_device *dev, unsigned int pipe)
2894f796cf8fSJesse Barnes {
2895fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2896f796cf8fSJesse Barnes 	unsigned long irqflags;
289755b8f2a7STvrtko Ursulin 	uint32_t bit = INTEL_GEN(dev_priv) >= 7 ?
289886e83e35SChris Wilson 		DE_PIPE_VBLANK_IVB(pipe) : DE_PIPE_VBLANK(pipe);
2899f796cf8fSJesse Barnes 
2900f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2901fbdedaeaSVille Syrjälä 	ilk_disable_display_irq(dev_priv, bit);
2902b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2903b1f14ad0SJesse Barnes }
2904b1f14ad0SJesse Barnes 
290588e72717SThierry Reding static void gen8_disable_vblank(struct drm_device *dev, unsigned int pipe)
2906abd58f01SBen Widawsky {
2907fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2908abd58f01SBen Widawsky 	unsigned long irqflags;
2909abd58f01SBen Widawsky 
2910abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2911013d3752SVille Syrjälä 	bdw_disable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
2912abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2913abd58f01SBen Widawsky }
2914abd58f01SBen Widawsky 
2915b243f530STvrtko Ursulin static void ibx_irq_reset(struct drm_i915_private *dev_priv)
291691738a95SPaulo Zanoni {
29176e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
291891738a95SPaulo Zanoni 		return;
291991738a95SPaulo Zanoni 
2920f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(SDE);
2921105b122eSPaulo Zanoni 
29226e266956STvrtko Ursulin 	if (HAS_PCH_CPT(dev_priv) || HAS_PCH_LPT(dev_priv))
2923105b122eSPaulo Zanoni 		I915_WRITE(SERR_INT, 0xffffffff);
2924622364b6SPaulo Zanoni }
2925105b122eSPaulo Zanoni 
292691738a95SPaulo Zanoni /*
2927622364b6SPaulo Zanoni  * SDEIER is also touched by the interrupt handler to work around missed PCH
2928622364b6SPaulo Zanoni  * interrupts. Hence we can't update it after the interrupt handler is enabled -
2929622364b6SPaulo Zanoni  * instead we unconditionally enable all PCH interrupt sources here, but then
2930622364b6SPaulo Zanoni  * only unmask them as needed with SDEIMR.
2931622364b6SPaulo Zanoni  *
2932622364b6SPaulo Zanoni  * This function needs to be called before interrupts are enabled.
293391738a95SPaulo Zanoni  */
2934622364b6SPaulo Zanoni static void ibx_irq_pre_postinstall(struct drm_device *dev)
2935622364b6SPaulo Zanoni {
2936fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2937622364b6SPaulo Zanoni 
29386e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
2939622364b6SPaulo Zanoni 		return;
2940622364b6SPaulo Zanoni 
2941622364b6SPaulo Zanoni 	WARN_ON(I915_READ(SDEIER) != 0);
294291738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
294391738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
294491738a95SPaulo Zanoni }
294591738a95SPaulo Zanoni 
2946b243f530STvrtko Ursulin static void gen5_gt_irq_reset(struct drm_i915_private *dev_priv)
2947d18ea1b5SDaniel Vetter {
2948f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GT);
2949b243f530STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 6)
2950f86f3fb0SPaulo Zanoni 		GEN5_IRQ_RESET(GEN6_PM);
2951d18ea1b5SDaniel Vetter }
2952d18ea1b5SDaniel Vetter 
295370591a41SVille Syrjälä static void vlv_display_irq_reset(struct drm_i915_private *dev_priv)
295470591a41SVille Syrjälä {
295570591a41SVille Syrjälä 	enum pipe pipe;
295670591a41SVille Syrjälä 
295771b8b41dSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
295871b8b41dSVille Syrjälä 		I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
295971b8b41dSVille Syrjälä 	else
296071b8b41dSVille Syrjälä 		I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
296171b8b41dSVille Syrjälä 
2962ad22d106SVille Syrjälä 	i915_hotplug_interrupt_update_locked(dev_priv, 0xffffffff, 0);
296370591a41SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
296470591a41SVille Syrjälä 
2965ad22d106SVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
2966ad22d106SVille Syrjälä 		I915_WRITE(PIPESTAT(pipe),
2967ad22d106SVille Syrjälä 			   PIPE_FIFO_UNDERRUN_STATUS |
2968ad22d106SVille Syrjälä 			   PIPESTAT_INT_STATUS_MASK);
2969ad22d106SVille Syrjälä 		dev_priv->pipestat_irq_mask[pipe] = 0;
2970ad22d106SVille Syrjälä 	}
297170591a41SVille Syrjälä 
297270591a41SVille Syrjälä 	GEN5_IRQ_RESET(VLV_);
2973ad22d106SVille Syrjälä 	dev_priv->irq_mask = ~0;
297470591a41SVille Syrjälä }
297570591a41SVille Syrjälä 
29768bb61306SVille Syrjälä static void vlv_display_irq_postinstall(struct drm_i915_private *dev_priv)
29778bb61306SVille Syrjälä {
29788bb61306SVille Syrjälä 	u32 pipestat_mask;
29799ab981f2SVille Syrjälä 	u32 enable_mask;
29808bb61306SVille Syrjälä 	enum pipe pipe;
2981eef57324SJerome Anand 	u32 val;
29828bb61306SVille Syrjälä 
29838bb61306SVille Syrjälä 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
29848bb61306SVille Syrjälä 			PIPE_CRC_DONE_INTERRUPT_STATUS;
29858bb61306SVille Syrjälä 
29868bb61306SVille Syrjälä 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
29878bb61306SVille Syrjälä 	for_each_pipe(dev_priv, pipe)
29888bb61306SVille Syrjälä 		i915_enable_pipestat(dev_priv, pipe, pipestat_mask);
29898bb61306SVille Syrjälä 
29909ab981f2SVille Syrjälä 	enable_mask = I915_DISPLAY_PORT_INTERRUPT |
29918bb61306SVille Syrjälä 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
29928bb61306SVille Syrjälä 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
29938bb61306SVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
29949ab981f2SVille Syrjälä 		enable_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
29956b7eafc1SVille Syrjälä 
29966b7eafc1SVille Syrjälä 	WARN_ON(dev_priv->irq_mask != ~0);
29976b7eafc1SVille Syrjälä 
2998eef57324SJerome Anand 	val = (I915_LPE_PIPE_A_INTERRUPT |
2999eef57324SJerome Anand 		I915_LPE_PIPE_B_INTERRUPT |
3000eef57324SJerome Anand 		I915_LPE_PIPE_C_INTERRUPT);
3001eef57324SJerome Anand 
3002eef57324SJerome Anand 	enable_mask |= val;
3003eef57324SJerome Anand 
30049ab981f2SVille Syrjälä 	dev_priv->irq_mask = ~enable_mask;
30058bb61306SVille Syrjälä 
30069ab981f2SVille Syrjälä 	GEN5_IRQ_INIT(VLV_, dev_priv->irq_mask, enable_mask);
30078bb61306SVille Syrjälä }
30088bb61306SVille Syrjälä 
30098bb61306SVille Syrjälä /* drm_dma.h hooks
30108bb61306SVille Syrjälä */
30118bb61306SVille Syrjälä static void ironlake_irq_reset(struct drm_device *dev)
30128bb61306SVille Syrjälä {
3013fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
30148bb61306SVille Syrjälä 
30158bb61306SVille Syrjälä 	I915_WRITE(HWSTAM, 0xffffffff);
30168bb61306SVille Syrjälä 
30178bb61306SVille Syrjälä 	GEN5_IRQ_RESET(DE);
30185db94019STvrtko Ursulin 	if (IS_GEN7(dev_priv))
30198bb61306SVille Syrjälä 		I915_WRITE(GEN7_ERR_INT, 0xffffffff);
30208bb61306SVille Syrjälä 
3021b243f530STvrtko Ursulin 	gen5_gt_irq_reset(dev_priv);
30228bb61306SVille Syrjälä 
3023b243f530STvrtko Ursulin 	ibx_irq_reset(dev_priv);
30248bb61306SVille Syrjälä }
30258bb61306SVille Syrjälä 
30267e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
30277e231dbeSJesse Barnes {
3028fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
30297e231dbeSJesse Barnes 
303034c7b8a7SVille Syrjälä 	I915_WRITE(VLV_MASTER_IER, 0);
303134c7b8a7SVille Syrjälä 	POSTING_READ(VLV_MASTER_IER);
303234c7b8a7SVille Syrjälä 
3033b243f530STvrtko Ursulin 	gen5_gt_irq_reset(dev_priv);
30347e231dbeSJesse Barnes 
3035ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
30369918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
303770591a41SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3038ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
30397e231dbeSJesse Barnes }
30407e231dbeSJesse Barnes 
3041d6e3cca3SDaniel Vetter static void gen8_gt_irq_reset(struct drm_i915_private *dev_priv)
3042d6e3cca3SDaniel Vetter {
3043d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 0);
3044d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 1);
3045d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 2);
3046d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 3);
3047d6e3cca3SDaniel Vetter }
3048d6e3cca3SDaniel Vetter 
3049823f6b38SPaulo Zanoni static void gen8_irq_reset(struct drm_device *dev)
3050abd58f01SBen Widawsky {
3051fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3052abd58f01SBen Widawsky 	int pipe;
3053abd58f01SBen Widawsky 
3054abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
3055abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3056abd58f01SBen Widawsky 
3057d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
3058abd58f01SBen Widawsky 
3059055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3060f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3061813bde43SPaulo Zanoni 						   POWER_DOMAIN_PIPE(pipe)))
3062f86f3fb0SPaulo Zanoni 			GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3063abd58f01SBen Widawsky 
3064f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_PORT_);
3065f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_MISC_);
3066f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_PCU_);
3067abd58f01SBen Widawsky 
30686e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
3069b243f530STvrtko Ursulin 		ibx_irq_reset(dev_priv);
3070abd58f01SBen Widawsky }
3071abd58f01SBen Widawsky 
30724c6c03beSDamien Lespiau void gen8_irq_power_well_post_enable(struct drm_i915_private *dev_priv,
30734c6c03beSDamien Lespiau 				     unsigned int pipe_mask)
3074d49bdb0eSPaulo Zanoni {
30751180e206SPaulo Zanoni 	uint32_t extra_ier = GEN8_PIPE_VBLANK | GEN8_PIPE_FIFO_UNDERRUN;
30766831f3e3SVille Syrjälä 	enum pipe pipe;
3077d49bdb0eSPaulo Zanoni 
307813321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
30796831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
30806831f3e3SVille Syrjälä 		GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
30816831f3e3SVille Syrjälä 				  dev_priv->de_irq_mask[pipe],
30826831f3e3SVille Syrjälä 				  ~dev_priv->de_irq_mask[pipe] | extra_ier);
308313321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3084d49bdb0eSPaulo Zanoni }
3085d49bdb0eSPaulo Zanoni 
3086aae8ba84SVille Syrjälä void gen8_irq_power_well_pre_disable(struct drm_i915_private *dev_priv,
3087aae8ba84SVille Syrjälä 				     unsigned int pipe_mask)
3088aae8ba84SVille Syrjälä {
30896831f3e3SVille Syrjälä 	enum pipe pipe;
30906831f3e3SVille Syrjälä 
3091aae8ba84SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
30926831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
30936831f3e3SVille Syrjälä 		GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3094aae8ba84SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3095aae8ba84SVille Syrjälä 
3096aae8ba84SVille Syrjälä 	/* make sure we're done processing display irqs */
309791c8a326SChris Wilson 	synchronize_irq(dev_priv->drm.irq);
3098aae8ba84SVille Syrjälä }
3099aae8ba84SVille Syrjälä 
310043f328d7SVille Syrjälä static void cherryview_irq_preinstall(struct drm_device *dev)
310143f328d7SVille Syrjälä {
3102fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
310343f328d7SVille Syrjälä 
310443f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
310543f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
310643f328d7SVille Syrjälä 
3107d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
310843f328d7SVille Syrjälä 
310943f328d7SVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
311043f328d7SVille Syrjälä 
3111ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
31129918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
311370591a41SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3114ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
311543f328d7SVille Syrjälä }
311643f328d7SVille Syrjälä 
311791d14251STvrtko Ursulin static u32 intel_hpd_enabled_irqs(struct drm_i915_private *dev_priv,
311887a02106SVille Syrjälä 				  const u32 hpd[HPD_NUM_PINS])
311987a02106SVille Syrjälä {
312087a02106SVille Syrjälä 	struct intel_encoder *encoder;
312187a02106SVille Syrjälä 	u32 enabled_irqs = 0;
312287a02106SVille Syrjälä 
312391c8a326SChris Wilson 	for_each_intel_encoder(&dev_priv->drm, encoder)
312487a02106SVille Syrjälä 		if (dev_priv->hotplug.stats[encoder->hpd_pin].state == HPD_ENABLED)
312587a02106SVille Syrjälä 			enabled_irqs |= hpd[encoder->hpd_pin];
312687a02106SVille Syrjälä 
312787a02106SVille Syrjälä 	return enabled_irqs;
312887a02106SVille Syrjälä }
312987a02106SVille Syrjälä 
31301a56b1a2SImre Deak static void ibx_hpd_detection_setup(struct drm_i915_private *dev_priv)
31311a56b1a2SImre Deak {
31321a56b1a2SImre Deak 	u32 hotplug;
31331a56b1a2SImre Deak 
31341a56b1a2SImre Deak 	/*
31351a56b1a2SImre Deak 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
31361a56b1a2SImre Deak 	 * duration to 2ms (which is the minimum in the Display Port spec).
31371a56b1a2SImre Deak 	 * The pulse duration bits are reserved on LPT+.
31381a56b1a2SImre Deak 	 */
31391a56b1a2SImre Deak 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
31401a56b1a2SImre Deak 	hotplug &= ~(PORTB_PULSE_DURATION_MASK |
31411a56b1a2SImre Deak 		     PORTC_PULSE_DURATION_MASK |
31421a56b1a2SImre Deak 		     PORTD_PULSE_DURATION_MASK);
31431a56b1a2SImre Deak 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
31441a56b1a2SImre Deak 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
31451a56b1a2SImre Deak 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
31461a56b1a2SImre Deak 	/*
31471a56b1a2SImre Deak 	 * When CPU and PCH are on the same package, port A
31481a56b1a2SImre Deak 	 * HPD must be enabled in both north and south.
31491a56b1a2SImre Deak 	 */
31501a56b1a2SImre Deak 	if (HAS_PCH_LPT_LP(dev_priv))
31511a56b1a2SImre Deak 		hotplug |= PORTA_HOTPLUG_ENABLE;
31521a56b1a2SImre Deak 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
31531a56b1a2SImre Deak }
31541a56b1a2SImre Deak 
315591d14251STvrtko Ursulin static void ibx_hpd_irq_setup(struct drm_i915_private *dev_priv)
315682a28bcfSDaniel Vetter {
31571a56b1a2SImre Deak 	u32 hotplug_irqs, enabled_irqs;
315882a28bcfSDaniel Vetter 
315991d14251STvrtko Ursulin 	if (HAS_PCH_IBX(dev_priv)) {
3160fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
316191d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ibx);
316282a28bcfSDaniel Vetter 	} else {
3163fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
316491d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_cpt);
316582a28bcfSDaniel Vetter 	}
316682a28bcfSDaniel Vetter 
3167fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
316882a28bcfSDaniel Vetter 
31691a56b1a2SImre Deak 	ibx_hpd_detection_setup(dev_priv);
31706dbf30ceSVille Syrjälä }
317126951cafSXiong Zhang 
31722a57d9ccSImre Deak static void spt_hpd_detection_setup(struct drm_i915_private *dev_priv)
31732a57d9ccSImre Deak {
31742a57d9ccSImre Deak 	u32 hotplug;
31752a57d9ccSImre Deak 
31762a57d9ccSImre Deak 	/* Enable digital hotplug on the PCH */
31772a57d9ccSImre Deak 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
31782a57d9ccSImre Deak 	hotplug |= PORTA_HOTPLUG_ENABLE |
31792a57d9ccSImre Deak 		   PORTB_HOTPLUG_ENABLE |
31802a57d9ccSImre Deak 		   PORTC_HOTPLUG_ENABLE |
31812a57d9ccSImre Deak 		   PORTD_HOTPLUG_ENABLE;
31822a57d9ccSImre Deak 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
31832a57d9ccSImre Deak 
31842a57d9ccSImre Deak 	hotplug = I915_READ(PCH_PORT_HOTPLUG2);
31852a57d9ccSImre Deak 	hotplug |= PORTE_HOTPLUG_ENABLE;
31862a57d9ccSImre Deak 	I915_WRITE(PCH_PORT_HOTPLUG2, hotplug);
31872a57d9ccSImre Deak }
31882a57d9ccSImre Deak 
318991d14251STvrtko Ursulin static void spt_hpd_irq_setup(struct drm_i915_private *dev_priv)
31906dbf30ceSVille Syrjälä {
31912a57d9ccSImre Deak 	u32 hotplug_irqs, enabled_irqs;
31926dbf30ceSVille Syrjälä 
31936dbf30ceSVille Syrjälä 	hotplug_irqs = SDE_HOTPLUG_MASK_SPT;
319491d14251STvrtko Ursulin 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_spt);
31956dbf30ceSVille Syrjälä 
31966dbf30ceSVille Syrjälä 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
31976dbf30ceSVille Syrjälä 
31982a57d9ccSImre Deak 	spt_hpd_detection_setup(dev_priv);
319926951cafSXiong Zhang }
32007fe0b973SKeith Packard 
32011a56b1a2SImre Deak static void ilk_hpd_detection_setup(struct drm_i915_private *dev_priv)
32021a56b1a2SImre Deak {
32031a56b1a2SImre Deak 	u32 hotplug;
32041a56b1a2SImre Deak 
32051a56b1a2SImre Deak 	/*
32061a56b1a2SImre Deak 	 * Enable digital hotplug on the CPU, and configure the DP short pulse
32071a56b1a2SImre Deak 	 * duration to 2ms (which is the minimum in the Display Port spec)
32081a56b1a2SImre Deak 	 * The pulse duration bits are reserved on HSW+.
32091a56b1a2SImre Deak 	 */
32101a56b1a2SImre Deak 	hotplug = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
32111a56b1a2SImre Deak 	hotplug &= ~DIGITAL_PORTA_PULSE_DURATION_MASK;
32121a56b1a2SImre Deak 	hotplug |= DIGITAL_PORTA_HOTPLUG_ENABLE |
32131a56b1a2SImre Deak 		   DIGITAL_PORTA_PULSE_DURATION_2ms;
32141a56b1a2SImre Deak 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, hotplug);
32151a56b1a2SImre Deak }
32161a56b1a2SImre Deak 
321791d14251STvrtko Ursulin static void ilk_hpd_irq_setup(struct drm_i915_private *dev_priv)
3218e4ce95aaSVille Syrjälä {
32191a56b1a2SImre Deak 	u32 hotplug_irqs, enabled_irqs;
3220e4ce95aaSVille Syrjälä 
322191d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 8) {
32223a3b3c7dSVille Syrjälä 		hotplug_irqs = GEN8_PORT_DP_A_HOTPLUG;
322391d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_bdw);
32243a3b3c7dSVille Syrjälä 
32253a3b3c7dSVille Syrjälä 		bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
322691d14251STvrtko Ursulin 	} else if (INTEL_GEN(dev_priv) >= 7) {
322723bb4cb5SVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG_IVB;
322891d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ivb);
32293a3b3c7dSVille Syrjälä 
32303a3b3c7dSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
323123bb4cb5SVille Syrjälä 	} else {
3232e4ce95aaSVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG;
323391d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ilk);
3234e4ce95aaSVille Syrjälä 
3235e4ce95aaSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
32363a3b3c7dSVille Syrjälä 	}
3237e4ce95aaSVille Syrjälä 
32381a56b1a2SImre Deak 	ilk_hpd_detection_setup(dev_priv);
3239e4ce95aaSVille Syrjälä 
324091d14251STvrtko Ursulin 	ibx_hpd_irq_setup(dev_priv);
3241e4ce95aaSVille Syrjälä }
3242e4ce95aaSVille Syrjälä 
32432a57d9ccSImre Deak static void __bxt_hpd_detection_setup(struct drm_i915_private *dev_priv,
32442a57d9ccSImre Deak 				      u32 enabled_irqs)
3245e0a20ad7SShashank Sharma {
32462a57d9ccSImre Deak 	u32 hotplug;
3247e0a20ad7SShashank Sharma 
3248a52bb15bSVille Syrjälä 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
32492a57d9ccSImre Deak 	hotplug |= PORTA_HOTPLUG_ENABLE |
32502a57d9ccSImre Deak 		   PORTB_HOTPLUG_ENABLE |
32512a57d9ccSImre Deak 		   PORTC_HOTPLUG_ENABLE;
3252d252bf68SShubhangi Shrivastava 
3253d252bf68SShubhangi Shrivastava 	DRM_DEBUG_KMS("Invert bit setting: hp_ctl:%x hp_port:%x\n",
3254d252bf68SShubhangi Shrivastava 		      hotplug, enabled_irqs);
3255d252bf68SShubhangi Shrivastava 	hotplug &= ~BXT_DDI_HPD_INVERT_MASK;
3256d252bf68SShubhangi Shrivastava 
3257d252bf68SShubhangi Shrivastava 	/*
3258d252bf68SShubhangi Shrivastava 	 * For BXT invert bit has to be set based on AOB design
3259d252bf68SShubhangi Shrivastava 	 * for HPD detection logic, update it based on VBT fields.
3260d252bf68SShubhangi Shrivastava 	 */
3261d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIA) &&
3262d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_A))
3263d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIA_HPD_INVERT;
3264d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIB) &&
3265d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_B))
3266d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIB_HPD_INVERT;
3267d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIC) &&
3268d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_C))
3269d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIC_HPD_INVERT;
3270d252bf68SShubhangi Shrivastava 
3271a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
3272e0a20ad7SShashank Sharma }
3273e0a20ad7SShashank Sharma 
32742a57d9ccSImre Deak static void bxt_hpd_detection_setup(struct drm_i915_private *dev_priv)
32752a57d9ccSImre Deak {
32762a57d9ccSImre Deak 	__bxt_hpd_detection_setup(dev_priv, BXT_DE_PORT_HOTPLUG_MASK);
32772a57d9ccSImre Deak }
32782a57d9ccSImre Deak 
32792a57d9ccSImre Deak static void bxt_hpd_irq_setup(struct drm_i915_private *dev_priv)
32802a57d9ccSImre Deak {
32812a57d9ccSImre Deak 	u32 hotplug_irqs, enabled_irqs;
32822a57d9ccSImre Deak 
32832a57d9ccSImre Deak 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_bxt);
32842a57d9ccSImre Deak 	hotplug_irqs = BXT_DE_PORT_HOTPLUG_MASK;
32852a57d9ccSImre Deak 
32862a57d9ccSImre Deak 	bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
32872a57d9ccSImre Deak 
32882a57d9ccSImre Deak 	__bxt_hpd_detection_setup(dev_priv, enabled_irqs);
32892a57d9ccSImre Deak }
32902a57d9ccSImre Deak 
3291d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
3292d46da437SPaulo Zanoni {
3293fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
329482a28bcfSDaniel Vetter 	u32 mask;
3295d46da437SPaulo Zanoni 
32966e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
3297692a04cfSDaniel Vetter 		return;
3298692a04cfSDaniel Vetter 
32996e266956STvrtko Ursulin 	if (HAS_PCH_IBX(dev_priv))
33005c673b60SDaniel Vetter 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
3301105b122eSPaulo Zanoni 	else
33025c673b60SDaniel Vetter 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
33038664281bSPaulo Zanoni 
3304b51a2842SVille Syrjälä 	gen5_assert_iir_is_zero(dev_priv, SDEIIR);
3305d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
33062a57d9ccSImre Deak 
33072a57d9ccSImre Deak 	if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
33082a57d9ccSImre Deak 	    HAS_PCH_LPT(dev_priv))
33091a56b1a2SImre Deak 		ibx_hpd_detection_setup(dev_priv);
33102a57d9ccSImre Deak 	else
33112a57d9ccSImre Deak 		spt_hpd_detection_setup(dev_priv);
3312d46da437SPaulo Zanoni }
3313d46da437SPaulo Zanoni 
33140a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
33150a9a8c91SDaniel Vetter {
3316fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
33170a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
33180a9a8c91SDaniel Vetter 
33190a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
33200a9a8c91SDaniel Vetter 
33210a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
33223c9192bcSTvrtko Ursulin 	if (HAS_L3_DPF(dev_priv)) {
33230a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
3324772c2a51STvrtko Ursulin 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev_priv);
3325772c2a51STvrtko Ursulin 		gt_irqs |= GT_PARITY_ERROR(dev_priv);
33260a9a8c91SDaniel Vetter 	}
33270a9a8c91SDaniel Vetter 
33280a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
33295db94019STvrtko Ursulin 	if (IS_GEN5(dev_priv)) {
3330f8973c21SChris Wilson 		gt_irqs |= ILK_BSD_USER_INTERRUPT;
33310a9a8c91SDaniel Vetter 	} else {
33320a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
33330a9a8c91SDaniel Vetter 	}
33340a9a8c91SDaniel Vetter 
333535079899SPaulo Zanoni 	GEN5_IRQ_INIT(GT, dev_priv->gt_irq_mask, gt_irqs);
33360a9a8c91SDaniel Vetter 
3337b243f530STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 6) {
333878e68d36SImre Deak 		/*
333978e68d36SImre Deak 		 * RPS interrupts will get enabled/disabled on demand when RPS
334078e68d36SImre Deak 		 * itself is enabled/disabled.
334178e68d36SImre Deak 		 */
3342f4e9af4fSAkash Goel 		if (HAS_VEBOX(dev_priv)) {
33430a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
3344f4e9af4fSAkash Goel 			dev_priv->pm_ier |= PM_VEBOX_USER_INTERRUPT;
3345f4e9af4fSAkash Goel 		}
33460a9a8c91SDaniel Vetter 
3347f4e9af4fSAkash Goel 		dev_priv->pm_imr = 0xffffffff;
3348f4e9af4fSAkash Goel 		GEN5_IRQ_INIT(GEN6_PM, dev_priv->pm_imr, pm_irqs);
33490a9a8c91SDaniel Vetter 	}
33500a9a8c91SDaniel Vetter }
33510a9a8c91SDaniel Vetter 
3352f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
3353036a4a7dSZhenyu Wang {
3354fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
33558e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
33568e76f8dcSPaulo Zanoni 
3357b243f530STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 7) {
33588e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
33598e76f8dcSPaulo Zanoni 				DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
33608e76f8dcSPaulo Zanoni 				DE_PLANEB_FLIP_DONE_IVB |
33615c673b60SDaniel Vetter 				DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB);
33628e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
336323bb4cb5SVille Syrjälä 			      DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB |
336423bb4cb5SVille Syrjälä 			      DE_DP_A_HOTPLUG_IVB);
33658e76f8dcSPaulo Zanoni 	} else {
33668e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3367ce99c256SDaniel Vetter 				DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
33685b3a856bSDaniel Vetter 				DE_AUX_CHANNEL_A |
33695b3a856bSDaniel Vetter 				DE_PIPEB_CRC_DONE | DE_PIPEA_CRC_DONE |
33705b3a856bSDaniel Vetter 				DE_POISON);
3371e4ce95aaSVille Syrjälä 		extra_mask = (DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
3372e4ce95aaSVille Syrjälä 			      DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN |
3373e4ce95aaSVille Syrjälä 			      DE_DP_A_HOTPLUG);
33748e76f8dcSPaulo Zanoni 	}
3375036a4a7dSZhenyu Wang 
33761ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3377036a4a7dSZhenyu Wang 
33780c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xeffe);
33790c841212SPaulo Zanoni 
3380622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3381622364b6SPaulo Zanoni 
338235079899SPaulo Zanoni 	GEN5_IRQ_INIT(DE, dev_priv->irq_mask, display_mask | extra_mask);
3383036a4a7dSZhenyu Wang 
33840a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
3385036a4a7dSZhenyu Wang 
33861a56b1a2SImre Deak 	ilk_hpd_detection_setup(dev_priv);
33871a56b1a2SImre Deak 
3388d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
33897fe0b973SKeith Packard 
339050a0bc90STvrtko Ursulin 	if (IS_IRONLAKE_M(dev_priv)) {
33916005ce42SDaniel Vetter 		/* Enable PCU event interrupts
33926005ce42SDaniel Vetter 		 *
33936005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
33944bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
33954bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
3396d6207435SDaniel Vetter 		spin_lock_irq(&dev_priv->irq_lock);
3397fbdedaeaSVille Syrjälä 		ilk_enable_display_irq(dev_priv, DE_PCU_EVENT);
3398d6207435SDaniel Vetter 		spin_unlock_irq(&dev_priv->irq_lock);
3399f97108d1SJesse Barnes 	}
3400f97108d1SJesse Barnes 
3401036a4a7dSZhenyu Wang 	return 0;
3402036a4a7dSZhenyu Wang }
3403036a4a7dSZhenyu Wang 
3404f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3405f8b79e58SImre Deak {
340667520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
3407f8b79e58SImre Deak 
3408f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3409f8b79e58SImre Deak 		return;
3410f8b79e58SImre Deak 
3411f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3412f8b79e58SImre Deak 
3413d6c69803SVille Syrjälä 	if (intel_irqs_enabled(dev_priv)) {
3414d6c69803SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3415ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3416f8b79e58SImre Deak 	}
3417d6c69803SVille Syrjälä }
3418f8b79e58SImre Deak 
3419f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3420f8b79e58SImre Deak {
342167520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
3422f8b79e58SImre Deak 
3423f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3424f8b79e58SImre Deak 		return;
3425f8b79e58SImre Deak 
3426f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3427f8b79e58SImre Deak 
3428950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3429ad22d106SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3430f8b79e58SImre Deak }
3431f8b79e58SImre Deak 
34320e6c9a9eSVille Syrjälä 
34330e6c9a9eSVille Syrjälä static int valleyview_irq_postinstall(struct drm_device *dev)
34340e6c9a9eSVille Syrjälä {
3435fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
34360e6c9a9eSVille Syrjälä 
34370a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
34387e231dbeSJesse Barnes 
3439ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
34409918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3441ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3442ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3443ad22d106SVille Syrjälä 
34447e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
344534c7b8a7SVille Syrjälä 	POSTING_READ(VLV_MASTER_IER);
344620afbda2SDaniel Vetter 
344720afbda2SDaniel Vetter 	return 0;
344820afbda2SDaniel Vetter }
344920afbda2SDaniel Vetter 
3450abd58f01SBen Widawsky static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
3451abd58f01SBen Widawsky {
3452abd58f01SBen Widawsky 	/* These are interrupts we'll toggle with the ring mask register */
3453abd58f01SBen Widawsky 	uint32_t gt_interrupts[] = {
3454abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
345573d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
345673d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT |
345773d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT,
3458abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
345973d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
346073d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT |
346173d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT,
3462abd58f01SBen Widawsky 		0,
346373d477f6SOscar Mateo 		GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT |
346473d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT
3465abd58f01SBen Widawsky 		};
3466abd58f01SBen Widawsky 
346798735739STvrtko Ursulin 	if (HAS_L3_DPF(dev_priv))
346898735739STvrtko Ursulin 		gt_interrupts[0] |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
346998735739STvrtko Ursulin 
3470f4e9af4fSAkash Goel 	dev_priv->pm_ier = 0x0;
3471f4e9af4fSAkash Goel 	dev_priv->pm_imr = ~dev_priv->pm_ier;
34729a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 0, ~gt_interrupts[0], gt_interrupts[0]);
34739a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 1, ~gt_interrupts[1], gt_interrupts[1]);
347478e68d36SImre Deak 	/*
347578e68d36SImre Deak 	 * RPS interrupts will get enabled/disabled on demand when RPS itself
347626705e20SSagar Arun Kamble 	 * is enabled/disabled. Same wil be the case for GuC interrupts.
347778e68d36SImre Deak 	 */
3478f4e9af4fSAkash Goel 	GEN8_IRQ_INIT_NDX(GT, 2, dev_priv->pm_imr, dev_priv->pm_ier);
34799a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 3, ~gt_interrupts[3], gt_interrupts[3]);
3480abd58f01SBen Widawsky }
3481abd58f01SBen Widawsky 
3482abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3483abd58f01SBen Widawsky {
3484770de83dSDamien Lespiau 	uint32_t de_pipe_masked = GEN8_PIPE_CDCLK_CRC_DONE;
3485770de83dSDamien Lespiau 	uint32_t de_pipe_enables;
34863a3b3c7dSVille Syrjälä 	u32 de_port_masked = GEN8_AUX_CHANNEL_A;
34873a3b3c7dSVille Syrjälä 	u32 de_port_enables;
348811825b0dSVille Syrjälä 	u32 de_misc_masked = GEN8_DE_MISC_GSE;
34893a3b3c7dSVille Syrjälä 	enum pipe pipe;
3490770de83dSDamien Lespiau 
3491b4834a50SRodrigo Vivi 	if (INTEL_INFO(dev_priv)->gen >= 9) {
3492770de83dSDamien Lespiau 		de_pipe_masked |= GEN9_PIPE_PLANE1_FLIP_DONE |
3493770de83dSDamien Lespiau 				  GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
34943a3b3c7dSVille Syrjälä 		de_port_masked |= GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
349588e04703SJesse Barnes 				  GEN9_AUX_CHANNEL_D;
3496cc3f90f0SAnder Conselvan de Oliveira 		if (IS_GEN9_LP(dev_priv))
34973a3b3c7dSVille Syrjälä 			de_port_masked |= BXT_DE_PORT_GMBUS;
34983a3b3c7dSVille Syrjälä 	} else {
3499770de83dSDamien Lespiau 		de_pipe_masked |= GEN8_PIPE_PRIMARY_FLIP_DONE |
3500770de83dSDamien Lespiau 				  GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
35013a3b3c7dSVille Syrjälä 	}
3502770de83dSDamien Lespiau 
3503770de83dSDamien Lespiau 	de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
3504770de83dSDamien Lespiau 					   GEN8_PIPE_FIFO_UNDERRUN;
3505770de83dSDamien Lespiau 
35063a3b3c7dSVille Syrjälä 	de_port_enables = de_port_masked;
3507cc3f90f0SAnder Conselvan de Oliveira 	if (IS_GEN9_LP(dev_priv))
3508a52bb15bSVille Syrjälä 		de_port_enables |= BXT_DE_PORT_HOTPLUG_MASK;
3509a52bb15bSVille Syrjälä 	else if (IS_BROADWELL(dev_priv))
35103a3b3c7dSVille Syrjälä 		de_port_enables |= GEN8_PORT_DP_A_HOTPLUG;
35113a3b3c7dSVille Syrjälä 
351213b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_A] = ~de_pipe_masked;
351313b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_B] = ~de_pipe_masked;
351413b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_C] = ~de_pipe_masked;
3515abd58f01SBen Widawsky 
3516055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3517f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3518813bde43SPaulo Zanoni 				POWER_DOMAIN_PIPE(pipe)))
3519813bde43SPaulo Zanoni 			GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
3520813bde43SPaulo Zanoni 					  dev_priv->de_irq_mask[pipe],
352135079899SPaulo Zanoni 					  de_pipe_enables);
3522abd58f01SBen Widawsky 
35233a3b3c7dSVille Syrjälä 	GEN5_IRQ_INIT(GEN8_DE_PORT_, ~de_port_masked, de_port_enables);
352411825b0dSVille Syrjälä 	GEN5_IRQ_INIT(GEN8_DE_MISC_, ~de_misc_masked, de_misc_masked);
35252a57d9ccSImre Deak 
35262a57d9ccSImre Deak 	if (IS_GEN9_LP(dev_priv))
35272a57d9ccSImre Deak 		bxt_hpd_detection_setup(dev_priv);
35281a56b1a2SImre Deak 	else if (IS_BROADWELL(dev_priv))
35291a56b1a2SImre Deak 		ilk_hpd_detection_setup(dev_priv);
3530abd58f01SBen Widawsky }
3531abd58f01SBen Widawsky 
3532abd58f01SBen Widawsky static int gen8_irq_postinstall(struct drm_device *dev)
3533abd58f01SBen Widawsky {
3534fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3535abd58f01SBen Widawsky 
35366e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
3537622364b6SPaulo Zanoni 		ibx_irq_pre_postinstall(dev);
3538622364b6SPaulo Zanoni 
3539abd58f01SBen Widawsky 	gen8_gt_irq_postinstall(dev_priv);
3540abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3541abd58f01SBen Widawsky 
35426e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
3543abd58f01SBen Widawsky 		ibx_irq_postinstall(dev);
3544abd58f01SBen Widawsky 
3545e5328c43SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
3546abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3547abd58f01SBen Widawsky 
3548abd58f01SBen Widawsky 	return 0;
3549abd58f01SBen Widawsky }
3550abd58f01SBen Widawsky 
355143f328d7SVille Syrjälä static int cherryview_irq_postinstall(struct drm_device *dev)
355243f328d7SVille Syrjälä {
3553fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
355443f328d7SVille Syrjälä 
355543f328d7SVille Syrjälä 	gen8_gt_irq_postinstall(dev_priv);
355643f328d7SVille Syrjälä 
3557ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
35589918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3559ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3560ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3561ad22d106SVille Syrjälä 
3562e5328c43SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
356343f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
356443f328d7SVille Syrjälä 
356543f328d7SVille Syrjälä 	return 0;
356643f328d7SVille Syrjälä }
356743f328d7SVille Syrjälä 
3568abd58f01SBen Widawsky static void gen8_irq_uninstall(struct drm_device *dev)
3569abd58f01SBen Widawsky {
3570fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3571abd58f01SBen Widawsky 
3572abd58f01SBen Widawsky 	if (!dev_priv)
3573abd58f01SBen Widawsky 		return;
3574abd58f01SBen Widawsky 
3575823f6b38SPaulo Zanoni 	gen8_irq_reset(dev);
3576abd58f01SBen Widawsky }
3577abd58f01SBen Widawsky 
35787e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
35797e231dbeSJesse Barnes {
3580fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
35817e231dbeSJesse Barnes 
35827e231dbeSJesse Barnes 	if (!dev_priv)
35837e231dbeSJesse Barnes 		return;
35847e231dbeSJesse Barnes 
3585843d0e7dSImre Deak 	I915_WRITE(VLV_MASTER_IER, 0);
358634c7b8a7SVille Syrjälä 	POSTING_READ(VLV_MASTER_IER);
3587843d0e7dSImre Deak 
3588b243f530STvrtko Ursulin 	gen5_gt_irq_reset(dev_priv);
3589893fce8eSVille Syrjälä 
35907e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
3591f8b79e58SImre Deak 
3592ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
35939918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3594ad22d106SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3595ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
35967e231dbeSJesse Barnes }
35977e231dbeSJesse Barnes 
359843f328d7SVille Syrjälä static void cherryview_irq_uninstall(struct drm_device *dev)
359943f328d7SVille Syrjälä {
3600fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
360143f328d7SVille Syrjälä 
360243f328d7SVille Syrjälä 	if (!dev_priv)
360343f328d7SVille Syrjälä 		return;
360443f328d7SVille Syrjälä 
360543f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
360643f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
360743f328d7SVille Syrjälä 
3608a2c30fbaSVille Syrjälä 	gen8_gt_irq_reset(dev_priv);
360943f328d7SVille Syrjälä 
3610a2c30fbaSVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
361143f328d7SVille Syrjälä 
3612ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
36139918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3614ad22d106SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3615ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
361643f328d7SVille Syrjälä }
361743f328d7SVille Syrjälä 
3618f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
3619036a4a7dSZhenyu Wang {
3620fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
36214697995bSJesse Barnes 
36224697995bSJesse Barnes 	if (!dev_priv)
36234697995bSJesse Barnes 		return;
36244697995bSJesse Barnes 
3625be30b29fSPaulo Zanoni 	ironlake_irq_reset(dev);
3626036a4a7dSZhenyu Wang }
3627036a4a7dSZhenyu Wang 
3628c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
3629c2798b19SChris Wilson {
3630fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3631c2798b19SChris Wilson 	int pipe;
3632c2798b19SChris Wilson 
3633055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3634c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3635c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3636c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3637c2798b19SChris Wilson 	POSTING_READ16(IER);
3638c2798b19SChris Wilson }
3639c2798b19SChris Wilson 
3640c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
3641c2798b19SChris Wilson {
3642fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3643c2798b19SChris Wilson 
3644c2798b19SChris Wilson 	I915_WRITE16(EMR,
3645c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3646c2798b19SChris Wilson 
3647c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
3648c2798b19SChris Wilson 	dev_priv->irq_mask =
3649c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3650c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3651c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
365237ef01abSDaniel Vetter 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
3653c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
3654c2798b19SChris Wilson 
3655c2798b19SChris Wilson 	I915_WRITE16(IER,
3656c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3657c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3658c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
3659c2798b19SChris Wilson 	POSTING_READ16(IER);
3660c2798b19SChris Wilson 
3661379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3662379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3663d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3664755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3665755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3666d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3667379ef82dSDaniel Vetter 
3668c2798b19SChris Wilson 	return 0;
3669c2798b19SChris Wilson }
3670c2798b19SChris Wilson 
36715a21b665SDaniel Vetter /*
36725a21b665SDaniel Vetter  * Returns true when a page flip has completed.
36735a21b665SDaniel Vetter  */
36745a21b665SDaniel Vetter static bool i8xx_handle_vblank(struct drm_i915_private *dev_priv,
36755a21b665SDaniel Vetter 			       int plane, int pipe, u32 iir)
36765a21b665SDaniel Vetter {
36775a21b665SDaniel Vetter 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
36785a21b665SDaniel Vetter 
36795a21b665SDaniel Vetter 	if (!intel_pipe_handle_vblank(dev_priv, pipe))
36805a21b665SDaniel Vetter 		return false;
36815a21b665SDaniel Vetter 
36825a21b665SDaniel Vetter 	if ((iir & flip_pending) == 0)
36835a21b665SDaniel Vetter 		goto check_page_flip;
36845a21b665SDaniel Vetter 
36855a21b665SDaniel Vetter 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
36865a21b665SDaniel Vetter 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
36875a21b665SDaniel Vetter 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
36885a21b665SDaniel Vetter 	 * the flip is completed (no longer pending). Since this doesn't raise
36895a21b665SDaniel Vetter 	 * an interrupt per se, we watch for the change at vblank.
36905a21b665SDaniel Vetter 	 */
36915a21b665SDaniel Vetter 	if (I915_READ16(ISR) & flip_pending)
36925a21b665SDaniel Vetter 		goto check_page_flip;
36935a21b665SDaniel Vetter 
36945a21b665SDaniel Vetter 	intel_finish_page_flip_cs(dev_priv, pipe);
36955a21b665SDaniel Vetter 	return true;
36965a21b665SDaniel Vetter 
36975a21b665SDaniel Vetter check_page_flip:
36985a21b665SDaniel Vetter 	intel_check_page_flip(dev_priv, pipe);
36995a21b665SDaniel Vetter 	return false;
37005a21b665SDaniel Vetter }
37015a21b665SDaniel Vetter 
3702ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3703c2798b19SChris Wilson {
370445a83f84SDaniel Vetter 	struct drm_device *dev = arg;
3705fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3706c2798b19SChris Wilson 	u16 iir, new_iir;
3707c2798b19SChris Wilson 	u32 pipe_stats[2];
3708c2798b19SChris Wilson 	int pipe;
3709c2798b19SChris Wilson 	u16 flip_mask =
3710c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3711c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
37121f814dacSImre Deak 	irqreturn_t ret;
3713c2798b19SChris Wilson 
37142dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
37152dd2a883SImre Deak 		return IRQ_NONE;
37162dd2a883SImre Deak 
37171f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
37181f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
37191f814dacSImre Deak 
37201f814dacSImre Deak 	ret = IRQ_NONE;
3721c2798b19SChris Wilson 	iir = I915_READ16(IIR);
3722c2798b19SChris Wilson 	if (iir == 0)
37231f814dacSImre Deak 		goto out;
3724c2798b19SChris Wilson 
3725c2798b19SChris Wilson 	while (iir & ~flip_mask) {
3726c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3727c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3728c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3729c2798b19SChris Wilson 		 * interrupts (for non-MSI).
3730c2798b19SChris Wilson 		 */
3731222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
3732c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3733aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
3734c2798b19SChris Wilson 
3735055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
3736f0f59a00SVille Syrjälä 			i915_reg_t reg = PIPESTAT(pipe);
3737c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3738c2798b19SChris Wilson 
3739c2798b19SChris Wilson 			/*
3740c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3741c2798b19SChris Wilson 			 */
37422d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & 0x8000ffff)
3743c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3744c2798b19SChris Wilson 		}
3745222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
3746c2798b19SChris Wilson 
3747c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
3748c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
3749c2798b19SChris Wilson 
3750c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
37513b3f1650SAkash Goel 			notify_ring(dev_priv->engine[RCS]);
3752c2798b19SChris Wilson 
3753055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
37545a21b665SDaniel Vetter 			int plane = pipe;
37555a21b665SDaniel Vetter 			if (HAS_FBC(dev_priv))
37565a21b665SDaniel Vetter 				plane = !plane;
37575a21b665SDaniel Vetter 
37585a21b665SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
37595a21b665SDaniel Vetter 			    i8xx_handle_vblank(dev_priv, plane, pipe, iir))
37605a21b665SDaniel Vetter 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3761c2798b19SChris Wilson 
37624356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
376391d14251STvrtko Ursulin 				i9xx_pipe_crc_irq_handler(dev_priv, pipe);
37642d9d2b0bSVille Syrjälä 
37651f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
37661f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
37671f7247c0SDaniel Vetter 								    pipe);
37684356d586SDaniel Vetter 		}
3769c2798b19SChris Wilson 
3770c2798b19SChris Wilson 		iir = new_iir;
3771c2798b19SChris Wilson 	}
37721f814dacSImre Deak 	ret = IRQ_HANDLED;
3773c2798b19SChris Wilson 
37741f814dacSImre Deak out:
37751f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
37761f814dacSImre Deak 
37771f814dacSImre Deak 	return ret;
3778c2798b19SChris Wilson }
3779c2798b19SChris Wilson 
3780c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
3781c2798b19SChris Wilson {
3782fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3783c2798b19SChris Wilson 	int pipe;
3784c2798b19SChris Wilson 
3785055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
3786c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
3787c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3788c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
3789c2798b19SChris Wilson 	}
3790c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3791c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3792c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
3793c2798b19SChris Wilson }
3794c2798b19SChris Wilson 
3795a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
3796a266c7d5SChris Wilson {
3797fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3798a266c7d5SChris Wilson 	int pipe;
3799a266c7d5SChris Wilson 
380056b857a5STvrtko Ursulin 	if (I915_HAS_HOTPLUG(dev_priv)) {
38010706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
3802a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3803a266c7d5SChris Wilson 	}
3804a266c7d5SChris Wilson 
380500d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
3806055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3807a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3808a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3809a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3810a266c7d5SChris Wilson 	POSTING_READ(IER);
3811a266c7d5SChris Wilson }
3812a266c7d5SChris Wilson 
3813a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
3814a266c7d5SChris Wilson {
3815fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
381638bde180SChris Wilson 	u32 enable_mask;
3817a266c7d5SChris Wilson 
381838bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
381938bde180SChris Wilson 
382038bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
382138bde180SChris Wilson 	dev_priv->irq_mask =
382238bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
382338bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
382438bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
382538bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
382637ef01abSDaniel Vetter 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
382738bde180SChris Wilson 
382838bde180SChris Wilson 	enable_mask =
382938bde180SChris Wilson 		I915_ASLE_INTERRUPT |
383038bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
383138bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
383238bde180SChris Wilson 		I915_USER_INTERRUPT;
383338bde180SChris Wilson 
383456b857a5STvrtko Ursulin 	if (I915_HAS_HOTPLUG(dev_priv)) {
38350706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
383620afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
383720afbda2SDaniel Vetter 
3838a266c7d5SChris Wilson 		/* Enable in IER... */
3839a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
3840a266c7d5SChris Wilson 		/* and unmask in IMR */
3841a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
3842a266c7d5SChris Wilson 	}
3843a266c7d5SChris Wilson 
3844a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3845a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3846a266c7d5SChris Wilson 	POSTING_READ(IER);
3847a266c7d5SChris Wilson 
384891d14251STvrtko Ursulin 	i915_enable_asle_pipestat(dev_priv);
384920afbda2SDaniel Vetter 
3850379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3851379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3852d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3853755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3854755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3855d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3856379ef82dSDaniel Vetter 
385720afbda2SDaniel Vetter 	return 0;
385820afbda2SDaniel Vetter }
385920afbda2SDaniel Vetter 
38605a21b665SDaniel Vetter /*
38615a21b665SDaniel Vetter  * Returns true when a page flip has completed.
38625a21b665SDaniel Vetter  */
38635a21b665SDaniel Vetter static bool i915_handle_vblank(struct drm_i915_private *dev_priv,
38645a21b665SDaniel Vetter 			       int plane, int pipe, u32 iir)
38655a21b665SDaniel Vetter {
38665a21b665SDaniel Vetter 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
38675a21b665SDaniel Vetter 
38685a21b665SDaniel Vetter 	if (!intel_pipe_handle_vblank(dev_priv, pipe))
38695a21b665SDaniel Vetter 		return false;
38705a21b665SDaniel Vetter 
38715a21b665SDaniel Vetter 	if ((iir & flip_pending) == 0)
38725a21b665SDaniel Vetter 		goto check_page_flip;
38735a21b665SDaniel Vetter 
38745a21b665SDaniel Vetter 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
38755a21b665SDaniel Vetter 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
38765a21b665SDaniel Vetter 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
38775a21b665SDaniel Vetter 	 * the flip is completed (no longer pending). Since this doesn't raise
38785a21b665SDaniel Vetter 	 * an interrupt per se, we watch for the change at vblank.
38795a21b665SDaniel Vetter 	 */
38805a21b665SDaniel Vetter 	if (I915_READ(ISR) & flip_pending)
38815a21b665SDaniel Vetter 		goto check_page_flip;
38825a21b665SDaniel Vetter 
38835a21b665SDaniel Vetter 	intel_finish_page_flip_cs(dev_priv, pipe);
38845a21b665SDaniel Vetter 	return true;
38855a21b665SDaniel Vetter 
38865a21b665SDaniel Vetter check_page_flip:
38875a21b665SDaniel Vetter 	intel_check_page_flip(dev_priv, pipe);
38885a21b665SDaniel Vetter 	return false;
38895a21b665SDaniel Vetter }
38905a21b665SDaniel Vetter 
3891ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
3892a266c7d5SChris Wilson {
389345a83f84SDaniel Vetter 	struct drm_device *dev = arg;
3894fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
38958291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
389638bde180SChris Wilson 	u32 flip_mask =
389738bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
389838bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
389938bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
3900a266c7d5SChris Wilson 
39012dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
39022dd2a883SImre Deak 		return IRQ_NONE;
39032dd2a883SImre Deak 
39041f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
39051f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
39061f814dacSImre Deak 
3907a266c7d5SChris Wilson 	iir = I915_READ(IIR);
390838bde180SChris Wilson 	do {
390938bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
39108291ee90SChris Wilson 		bool blc_event = false;
3911a266c7d5SChris Wilson 
3912a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3913a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3914a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3915a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3916a266c7d5SChris Wilson 		 */
3917222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
3918a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3919aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
3920a266c7d5SChris Wilson 
3921055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
3922f0f59a00SVille Syrjälä 			i915_reg_t reg = PIPESTAT(pipe);
3923a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3924a266c7d5SChris Wilson 
392538bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
3926a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3927a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
392838bde180SChris Wilson 				irq_received = true;
3929a266c7d5SChris Wilson 			}
3930a266c7d5SChris Wilson 		}
3931222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
3932a266c7d5SChris Wilson 
3933a266c7d5SChris Wilson 		if (!irq_received)
3934a266c7d5SChris Wilson 			break;
3935a266c7d5SChris Wilson 
3936a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
393791d14251STvrtko Ursulin 		if (I915_HAS_HOTPLUG(dev_priv) &&
39381ae3c34cSVille Syrjälä 		    iir & I915_DISPLAY_PORT_INTERRUPT) {
39391ae3c34cSVille Syrjälä 			u32 hotplug_status = i9xx_hpd_irq_ack(dev_priv);
39401ae3c34cSVille Syrjälä 			if (hotplug_status)
394191d14251STvrtko Ursulin 				i9xx_hpd_irq_handler(dev_priv, hotplug_status);
39421ae3c34cSVille Syrjälä 		}
3943a266c7d5SChris Wilson 
394438bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
3945a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3946a266c7d5SChris Wilson 
3947a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
39483b3f1650SAkash Goel 			notify_ring(dev_priv->engine[RCS]);
3949a266c7d5SChris Wilson 
3950055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
39515a21b665SDaniel Vetter 			int plane = pipe;
39525a21b665SDaniel Vetter 			if (HAS_FBC(dev_priv))
39535a21b665SDaniel Vetter 				plane = !plane;
39545a21b665SDaniel Vetter 
39555a21b665SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
39565a21b665SDaniel Vetter 			    i915_handle_vblank(dev_priv, plane, pipe, iir))
39575a21b665SDaniel Vetter 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3958a266c7d5SChris Wilson 
3959a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3960a266c7d5SChris Wilson 				blc_event = true;
39614356d586SDaniel Vetter 
39624356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
396391d14251STvrtko Ursulin 				i9xx_pipe_crc_irq_handler(dev_priv, pipe);
39642d9d2b0bSVille Syrjälä 
39651f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
39661f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
39671f7247c0SDaniel Vetter 								    pipe);
3968a266c7d5SChris Wilson 		}
3969a266c7d5SChris Wilson 
3970a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
397191d14251STvrtko Ursulin 			intel_opregion_asle_intr(dev_priv);
3972a266c7d5SChris Wilson 
3973a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3974a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3975a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3976a266c7d5SChris Wilson 		 * we would never get another interrupt.
3977a266c7d5SChris Wilson 		 *
3978a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3979a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3980a266c7d5SChris Wilson 		 * another one.
3981a266c7d5SChris Wilson 		 *
3982a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3983a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3984a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3985a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3986a266c7d5SChris Wilson 		 * stray interrupts.
3987a266c7d5SChris Wilson 		 */
398838bde180SChris Wilson 		ret = IRQ_HANDLED;
3989a266c7d5SChris Wilson 		iir = new_iir;
399038bde180SChris Wilson 	} while (iir & ~flip_mask);
3991a266c7d5SChris Wilson 
39921f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
39931f814dacSImre Deak 
3994a266c7d5SChris Wilson 	return ret;
3995a266c7d5SChris Wilson }
3996a266c7d5SChris Wilson 
3997a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
3998a266c7d5SChris Wilson {
3999fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4000a266c7d5SChris Wilson 	int pipe;
4001a266c7d5SChris Wilson 
400256b857a5STvrtko Ursulin 	if (I915_HAS_HOTPLUG(dev_priv)) {
40030706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4004a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4005a266c7d5SChris Wilson 	}
4006a266c7d5SChris Wilson 
400700d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
4008055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
400955b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
4010a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
401155b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
401255b39755SChris Wilson 	}
4013a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4014a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4015a266c7d5SChris Wilson 
4016a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4017a266c7d5SChris Wilson }
4018a266c7d5SChris Wilson 
4019a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
4020a266c7d5SChris Wilson {
4021fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4022a266c7d5SChris Wilson 	int pipe;
4023a266c7d5SChris Wilson 
40240706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4025a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4026a266c7d5SChris Wilson 
4027a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
4028055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4029a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4030a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4031a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4032a266c7d5SChris Wilson 	POSTING_READ(IER);
4033a266c7d5SChris Wilson }
4034a266c7d5SChris Wilson 
4035a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
4036a266c7d5SChris Wilson {
4037fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4038bbba0a97SChris Wilson 	u32 enable_mask;
4039a266c7d5SChris Wilson 	u32 error_mask;
4040a266c7d5SChris Wilson 
4041a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
4042bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
4043adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
4044bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4045bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
4046bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
4047bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
4048bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
4049bbba0a97SChris Wilson 
4050bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
405121ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
405221ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
4053bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
4054bbba0a97SChris Wilson 
405591d14251STvrtko Ursulin 	if (IS_G4X(dev_priv))
4056bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
4057a266c7d5SChris Wilson 
4058b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4059b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4060d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4061755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
4062755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4063755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4064d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4065a266c7d5SChris Wilson 
4066a266c7d5SChris Wilson 	/*
4067a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
4068a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
4069a266c7d5SChris Wilson 	 */
407091d14251STvrtko Ursulin 	if (IS_G4X(dev_priv)) {
4071a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
4072a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
4073a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
4074a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4075a266c7d5SChris Wilson 	} else {
4076a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
4077a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4078a266c7d5SChris Wilson 	}
4079a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
4080a266c7d5SChris Wilson 
4081a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
4082a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
4083a266c7d5SChris Wilson 	POSTING_READ(IER);
4084a266c7d5SChris Wilson 
40850706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
408620afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
408720afbda2SDaniel Vetter 
408891d14251STvrtko Ursulin 	i915_enable_asle_pipestat(dev_priv);
408920afbda2SDaniel Vetter 
409020afbda2SDaniel Vetter 	return 0;
409120afbda2SDaniel Vetter }
409220afbda2SDaniel Vetter 
409391d14251STvrtko Ursulin static void i915_hpd_irq_setup(struct drm_i915_private *dev_priv)
409420afbda2SDaniel Vetter {
409520afbda2SDaniel Vetter 	u32 hotplug_en;
409620afbda2SDaniel Vetter 
409767520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
4098b5ea2d56SDaniel Vetter 
4099adca4730SChris Wilson 	/* Note HDMI and DP share hotplug bits */
4100e5868a31SEgbert Eich 	/* enable bits are the same for all generations */
410191d14251STvrtko Ursulin 	hotplug_en = intel_hpd_enabled_irqs(dev_priv, hpd_mask_i915);
4102a266c7d5SChris Wilson 	/* Programming the CRT detection parameters tends
4103a266c7d5SChris Wilson 	   to generate a spurious hotplug event about three
4104a266c7d5SChris Wilson 	   seconds later.  So just do it once.
4105a266c7d5SChris Wilson 	*/
410691d14251STvrtko Ursulin 	if (IS_G4X(dev_priv))
4107a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
4108a266c7d5SChris Wilson 	hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
4109a266c7d5SChris Wilson 
4110a266c7d5SChris Wilson 	/* Ignore TV since it's buggy */
41110706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv,
4112f9e3dc78SJani Nikula 					     HOTPLUG_INT_EN_MASK |
4113f9e3dc78SJani Nikula 					     CRT_HOTPLUG_VOLTAGE_COMPARE_MASK |
4114f9e3dc78SJani Nikula 					     CRT_HOTPLUG_ACTIVATION_PERIOD_64,
41150706f17cSEgbert Eich 					     hotplug_en);
4116a266c7d5SChris Wilson }
4117a266c7d5SChris Wilson 
4118ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
4119a266c7d5SChris Wilson {
412045a83f84SDaniel Vetter 	struct drm_device *dev = arg;
4121fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4122a266c7d5SChris Wilson 	u32 iir, new_iir;
4123a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
4124a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
412521ad8330SVille Syrjälä 	u32 flip_mask =
412621ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
412721ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
4128a266c7d5SChris Wilson 
41292dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
41302dd2a883SImre Deak 		return IRQ_NONE;
41312dd2a883SImre Deak 
41321f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
41331f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
41341f814dacSImre Deak 
4135a266c7d5SChris Wilson 	iir = I915_READ(IIR);
4136a266c7d5SChris Wilson 
4137a266c7d5SChris Wilson 	for (;;) {
4138501e01d7SVille Syrjälä 		bool irq_received = (iir & ~flip_mask) != 0;
41392c8ba29fSChris Wilson 		bool blc_event = false;
41402c8ba29fSChris Wilson 
4141a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4142a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4143a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4144a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
4145a266c7d5SChris Wilson 		 */
4146222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
4147a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4148aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4149a266c7d5SChris Wilson 
4150055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4151f0f59a00SVille Syrjälä 			i915_reg_t reg = PIPESTAT(pipe);
4152a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4153a266c7d5SChris Wilson 
4154a266c7d5SChris Wilson 			/*
4155a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
4156a266c7d5SChris Wilson 			 */
4157a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4158a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
4159501e01d7SVille Syrjälä 				irq_received = true;
4160a266c7d5SChris Wilson 			}
4161a266c7d5SChris Wilson 		}
4162222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4163a266c7d5SChris Wilson 
4164a266c7d5SChris Wilson 		if (!irq_received)
4165a266c7d5SChris Wilson 			break;
4166a266c7d5SChris Wilson 
4167a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
4168a266c7d5SChris Wilson 
4169a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
41701ae3c34cSVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
41711ae3c34cSVille Syrjälä 			u32 hotplug_status = i9xx_hpd_irq_ack(dev_priv);
41721ae3c34cSVille Syrjälä 			if (hotplug_status)
417391d14251STvrtko Ursulin 				i9xx_hpd_irq_handler(dev_priv, hotplug_status);
41741ae3c34cSVille Syrjälä 		}
4175a266c7d5SChris Wilson 
417621ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
4177a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4178a266c7d5SChris Wilson 
4179a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
41803b3f1650SAkash Goel 			notify_ring(dev_priv->engine[RCS]);
4181a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
41823b3f1650SAkash Goel 			notify_ring(dev_priv->engine[VCS]);
4183a266c7d5SChris Wilson 
4184055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
41855a21b665SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
41865a21b665SDaniel Vetter 			    i915_handle_vblank(dev_priv, pipe, pipe, iir))
41875a21b665SDaniel Vetter 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
4188a266c7d5SChris Wilson 
4189a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4190a266c7d5SChris Wilson 				blc_event = true;
41914356d586SDaniel Vetter 
41924356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
419391d14251STvrtko Ursulin 				i9xx_pipe_crc_irq_handler(dev_priv, pipe);
4194a266c7d5SChris Wilson 
41951f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
41961f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
41972d9d2b0bSVille Syrjälä 		}
4198a266c7d5SChris Wilson 
4199a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
420091d14251STvrtko Ursulin 			intel_opregion_asle_intr(dev_priv);
4201a266c7d5SChris Wilson 
4202515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
420391d14251STvrtko Ursulin 			gmbus_irq_handler(dev_priv);
4204515ac2bbSDaniel Vetter 
4205a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4206a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4207a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4208a266c7d5SChris Wilson 		 * we would never get another interrupt.
4209a266c7d5SChris Wilson 		 *
4210a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4211a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4212a266c7d5SChris Wilson 		 * another one.
4213a266c7d5SChris Wilson 		 *
4214a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4215a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4216a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4217a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4218a266c7d5SChris Wilson 		 * stray interrupts.
4219a266c7d5SChris Wilson 		 */
4220a266c7d5SChris Wilson 		iir = new_iir;
4221a266c7d5SChris Wilson 	}
4222a266c7d5SChris Wilson 
42231f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
42241f814dacSImre Deak 
4225a266c7d5SChris Wilson 	return ret;
4226a266c7d5SChris Wilson }
4227a266c7d5SChris Wilson 
4228a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
4229a266c7d5SChris Wilson {
4230fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4231a266c7d5SChris Wilson 	int pipe;
4232a266c7d5SChris Wilson 
4233a266c7d5SChris Wilson 	if (!dev_priv)
4234a266c7d5SChris Wilson 		return;
4235a266c7d5SChris Wilson 
42360706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4237a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4238a266c7d5SChris Wilson 
4239a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
4240055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4241a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4242a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4243a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4244a266c7d5SChris Wilson 
4245055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4246a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
4247a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
4248a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4249a266c7d5SChris Wilson }
4250a266c7d5SChris Wilson 
4251fca52a55SDaniel Vetter /**
4252fca52a55SDaniel Vetter  * intel_irq_init - initializes irq support
4253fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4254fca52a55SDaniel Vetter  *
4255fca52a55SDaniel Vetter  * This function initializes all the irq support including work items, timers
4256fca52a55SDaniel Vetter  * and all the vtables. It does not setup the interrupt itself though.
4257fca52a55SDaniel Vetter  */
4258b963291cSDaniel Vetter void intel_irq_init(struct drm_i915_private *dev_priv)
4259f71d4af4SJesse Barnes {
426091c8a326SChris Wilson 	struct drm_device *dev = &dev_priv->drm;
42618b2e326dSChris Wilson 
426277913b39SJani Nikula 	intel_hpd_init_work(dev_priv);
426377913b39SJani Nikula 
4264c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
4265a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
42668b2e326dSChris Wilson 
42674805fe82STvrtko Ursulin 	if (HAS_GUC_SCHED(dev_priv))
426826705e20SSagar Arun Kamble 		dev_priv->pm_guc_events = GEN9_GUC_TO_HOST_INT_EVENT;
426926705e20SSagar Arun Kamble 
4270a6706b45SDeepak S 	/* Let's track the enabled rps events */
4271666a4537SWayne Boyer 	if (IS_VALLEYVIEW(dev_priv))
42726c65a587SVille Syrjälä 		/* WaGsvRC0ResidencyMethod:vlv */
4273e0e8c7cbSChris Wilson 		dev_priv->pm_rps_events = GEN6_PM_RP_UP_EI_EXPIRED;
427431685c25SDeepak S 	else
4275a6706b45SDeepak S 		dev_priv->pm_rps_events = GEN6_PM_RPS_EVENTS;
4276a6706b45SDeepak S 
42775dd04556SSagar Arun Kamble 	dev_priv->rps.pm_intrmsk_mbz = 0;
42781800ad25SSagar Arun Kamble 
42791800ad25SSagar Arun Kamble 	/*
42801800ad25SSagar Arun Kamble 	 * SNB,IVB can while VLV,CHV may hard hang on looping batchbuffer
42811800ad25SSagar Arun Kamble 	 * if GEN6_PM_UP_EI_EXPIRED is masked.
42821800ad25SSagar Arun Kamble 	 *
42831800ad25SSagar Arun Kamble 	 * TODO: verify if this can be reproduced on VLV,CHV.
42841800ad25SSagar Arun Kamble 	 */
42851800ad25SSagar Arun Kamble 	if (INTEL_INFO(dev_priv)->gen <= 7 && !IS_HASWELL(dev_priv))
42865dd04556SSagar Arun Kamble 		dev_priv->rps.pm_intrmsk_mbz |= GEN6_PM_RP_UP_EI_EXPIRED;
42871800ad25SSagar Arun Kamble 
42881800ad25SSagar Arun Kamble 	if (INTEL_INFO(dev_priv)->gen >= 8)
4289655d49efSChris Wilson 		dev_priv->rps.pm_intrmsk_mbz |= GEN8_PMINTR_DISABLE_REDIRECT_TO_GUC;
42901800ad25SSagar Arun Kamble 
4291b963291cSDaniel Vetter 	if (IS_GEN2(dev_priv)) {
42924194c088SRodrigo Vivi 		/* Gen2 doesn't have a hardware frame counter */
42934cdb83ecSVille Syrjälä 		dev->max_vblank_count = 0;
4294b963291cSDaniel Vetter 	} else if (IS_G4X(dev_priv) || INTEL_INFO(dev_priv)->gen >= 5) {
4295f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
4296fd8f507cSVille Syrjälä 		dev->driver->get_vblank_counter = g4x_get_vblank_counter;
4297391f75e2SVille Syrjälä 	} else {
4298391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
4299391f75e2SVille Syrjälä 		dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
4300f71d4af4SJesse Barnes 	}
4301f71d4af4SJesse Barnes 
430221da2700SVille Syrjälä 	/*
430321da2700SVille Syrjälä 	 * Opt out of the vblank disable timer on everything except gen2.
430421da2700SVille Syrjälä 	 * Gen2 doesn't have a hardware frame counter and so depends on
430521da2700SVille Syrjälä 	 * vblank interrupts to produce sane vblank seuquence numbers.
430621da2700SVille Syrjälä 	 */
4307b963291cSDaniel Vetter 	if (!IS_GEN2(dev_priv))
430821da2700SVille Syrjälä 		dev->vblank_disable_immediate = true;
430921da2700SVille Syrjälä 
4310262fd485SChris Wilson 	/* Most platforms treat the display irq block as an always-on
4311262fd485SChris Wilson 	 * power domain. vlv/chv can disable it at runtime and need
4312262fd485SChris Wilson 	 * special care to avoid writing any of the display block registers
4313262fd485SChris Wilson 	 * outside of the power domain. We defer setting up the display irqs
4314262fd485SChris Wilson 	 * in this case to the runtime pm.
4315262fd485SChris Wilson 	 */
4316262fd485SChris Wilson 	dev_priv->display_irqs_enabled = true;
4317262fd485SChris Wilson 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4318262fd485SChris Wilson 		dev_priv->display_irqs_enabled = false;
4319262fd485SChris Wilson 
4320317eaa95SLyude 	dev_priv->hotplug.hpd_storm_threshold = HPD_STORM_DEFAULT_THRESHOLD;
4321317eaa95SLyude 
4322f71d4af4SJesse Barnes 	dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
4323f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
4324f71d4af4SJesse Barnes 
4325b963291cSDaniel Vetter 	if (IS_CHERRYVIEW(dev_priv)) {
432643f328d7SVille Syrjälä 		dev->driver->irq_handler = cherryview_irq_handler;
432743f328d7SVille Syrjälä 		dev->driver->irq_preinstall = cherryview_irq_preinstall;
432843f328d7SVille Syrjälä 		dev->driver->irq_postinstall = cherryview_irq_postinstall;
432943f328d7SVille Syrjälä 		dev->driver->irq_uninstall = cherryview_irq_uninstall;
433086e83e35SChris Wilson 		dev->driver->enable_vblank = i965_enable_vblank;
433186e83e35SChris Wilson 		dev->driver->disable_vblank = i965_disable_vblank;
433243f328d7SVille Syrjälä 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4333b963291cSDaniel Vetter 	} else if (IS_VALLEYVIEW(dev_priv)) {
43347e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
43357e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
43367e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
43377e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
433886e83e35SChris Wilson 		dev->driver->enable_vblank = i965_enable_vblank;
433986e83e35SChris Wilson 		dev->driver->disable_vblank = i965_disable_vblank;
4340fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4341b963291cSDaniel Vetter 	} else if (INTEL_INFO(dev_priv)->gen >= 8) {
4342abd58f01SBen Widawsky 		dev->driver->irq_handler = gen8_irq_handler;
4343723761b8SDaniel Vetter 		dev->driver->irq_preinstall = gen8_irq_reset;
4344abd58f01SBen Widawsky 		dev->driver->irq_postinstall = gen8_irq_postinstall;
4345abd58f01SBen Widawsky 		dev->driver->irq_uninstall = gen8_irq_uninstall;
4346abd58f01SBen Widawsky 		dev->driver->enable_vblank = gen8_enable_vblank;
4347abd58f01SBen Widawsky 		dev->driver->disable_vblank = gen8_disable_vblank;
4348cc3f90f0SAnder Conselvan de Oliveira 		if (IS_GEN9_LP(dev_priv))
4349e0a20ad7SShashank Sharma 			dev_priv->display.hpd_irq_setup = bxt_hpd_irq_setup;
43506e266956STvrtko Ursulin 		else if (HAS_PCH_SPT(dev_priv) || HAS_PCH_KBP(dev_priv))
43516dbf30ceSVille Syrjälä 			dev_priv->display.hpd_irq_setup = spt_hpd_irq_setup;
43526dbf30ceSVille Syrjälä 		else
43533a3b3c7dSVille Syrjälä 			dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
43546e266956STvrtko Ursulin 	} else if (HAS_PCH_SPLIT(dev_priv)) {
4355f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
4356723761b8SDaniel Vetter 		dev->driver->irq_preinstall = ironlake_irq_reset;
4357f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
4358f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
4359f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
4360f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
4361e4ce95aaSVille Syrjälä 		dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
4362f71d4af4SJesse Barnes 	} else {
43637e22dbbbSTvrtko Ursulin 		if (IS_GEN2(dev_priv)) {
4364c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
4365c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
4366c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
4367c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
436886e83e35SChris Wilson 			dev->driver->enable_vblank = i8xx_enable_vblank;
436986e83e35SChris Wilson 			dev->driver->disable_vblank = i8xx_disable_vblank;
43707e22dbbbSTvrtko Ursulin 		} else if (IS_GEN3(dev_priv)) {
4371a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
4372a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
4373a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
4374a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
437586e83e35SChris Wilson 			dev->driver->enable_vblank = i8xx_enable_vblank;
437686e83e35SChris Wilson 			dev->driver->disable_vblank = i8xx_disable_vblank;
4377c2798b19SChris Wilson 		} else {
4378a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
4379a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
4380a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
4381a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
438286e83e35SChris Wilson 			dev->driver->enable_vblank = i965_enable_vblank;
438386e83e35SChris Wilson 			dev->driver->disable_vblank = i965_disable_vblank;
4384c2798b19SChris Wilson 		}
4385778eb334SVille Syrjälä 		if (I915_HAS_HOTPLUG(dev_priv))
4386778eb334SVille Syrjälä 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4387f71d4af4SJesse Barnes 	}
4388f71d4af4SJesse Barnes }
438920afbda2SDaniel Vetter 
4390fca52a55SDaniel Vetter /**
4391fca52a55SDaniel Vetter  * intel_irq_install - enables the hardware interrupt
4392fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4393fca52a55SDaniel Vetter  *
4394fca52a55SDaniel Vetter  * This function enables the hardware interrupt handling, but leaves the hotplug
4395fca52a55SDaniel Vetter  * handling still disabled. It is called after intel_irq_init().
4396fca52a55SDaniel Vetter  *
4397fca52a55SDaniel Vetter  * In the driver load and resume code we need working interrupts in a few places
4398fca52a55SDaniel Vetter  * but don't want to deal with the hassle of concurrent probe and hotplug
4399fca52a55SDaniel Vetter  * workers. Hence the split into this two-stage approach.
4400fca52a55SDaniel Vetter  */
44012aeb7d3aSDaniel Vetter int intel_irq_install(struct drm_i915_private *dev_priv)
44022aeb7d3aSDaniel Vetter {
44032aeb7d3aSDaniel Vetter 	/*
44042aeb7d3aSDaniel Vetter 	 * We enable some interrupt sources in our postinstall hooks, so mark
44052aeb7d3aSDaniel Vetter 	 * interrupts as enabled _before_ actually enabling them to avoid
44062aeb7d3aSDaniel Vetter 	 * special cases in our ordering checks.
44072aeb7d3aSDaniel Vetter 	 */
44082aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
44092aeb7d3aSDaniel Vetter 
441091c8a326SChris Wilson 	return drm_irq_install(&dev_priv->drm, dev_priv->drm.pdev->irq);
44112aeb7d3aSDaniel Vetter }
44122aeb7d3aSDaniel Vetter 
4413fca52a55SDaniel Vetter /**
4414fca52a55SDaniel Vetter  * intel_irq_uninstall - finilizes all irq handling
4415fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4416fca52a55SDaniel Vetter  *
4417fca52a55SDaniel Vetter  * This stops interrupt and hotplug handling and unregisters and frees all
4418fca52a55SDaniel Vetter  * resources acquired in the init functions.
4419fca52a55SDaniel Vetter  */
44202aeb7d3aSDaniel Vetter void intel_irq_uninstall(struct drm_i915_private *dev_priv)
44212aeb7d3aSDaniel Vetter {
442291c8a326SChris Wilson 	drm_irq_uninstall(&dev_priv->drm);
44232aeb7d3aSDaniel Vetter 	intel_hpd_cancel_work(dev_priv);
44242aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
44252aeb7d3aSDaniel Vetter }
44262aeb7d3aSDaniel Vetter 
4427fca52a55SDaniel Vetter /**
4428fca52a55SDaniel Vetter  * intel_runtime_pm_disable_interrupts - runtime interrupt disabling
4429fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4430fca52a55SDaniel Vetter  *
4431fca52a55SDaniel Vetter  * This function is used to disable interrupts at runtime, both in the runtime
4432fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4433fca52a55SDaniel Vetter  */
4434b963291cSDaniel Vetter void intel_runtime_pm_disable_interrupts(struct drm_i915_private *dev_priv)
4435c67a470bSPaulo Zanoni {
443691c8a326SChris Wilson 	dev_priv->drm.driver->irq_uninstall(&dev_priv->drm);
44372aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
443891c8a326SChris Wilson 	synchronize_irq(dev_priv->drm.irq);
4439c67a470bSPaulo Zanoni }
4440c67a470bSPaulo Zanoni 
4441fca52a55SDaniel Vetter /**
4442fca52a55SDaniel Vetter  * intel_runtime_pm_enable_interrupts - runtime interrupt enabling
4443fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4444fca52a55SDaniel Vetter  *
4445fca52a55SDaniel Vetter  * This function is used to enable interrupts at runtime, both in the runtime
4446fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4447fca52a55SDaniel Vetter  */
4448b963291cSDaniel Vetter void intel_runtime_pm_enable_interrupts(struct drm_i915_private *dev_priv)
4449c67a470bSPaulo Zanoni {
44502aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
445191c8a326SChris Wilson 	dev_priv->drm.driver->irq_preinstall(&dev_priv->drm);
445291c8a326SChris Wilson 	dev_priv->drm.driver->irq_postinstall(&dev_priv->drm);
4453c67a470bSPaulo Zanoni }
4454