xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision b91eb5cce65047eced9d13e8e15b46dced0c3c53)
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 
1830706f17cSEgbert Eich 	assert_spin_locked(&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ä 
2254bc9d430SDaniel Vetter 	assert_spin_locked(&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 {
25343eaea13SPaulo Zanoni 	assert_spin_locked(&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 
305edbfdb45SPaulo Zanoni 	assert_spin_locked(&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 
343f4e9af4fSAkash Goel 	assert_spin_locked(&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 {
352f4e9af4fSAkash Goel 	assert_spin_locked(&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 {
362f4e9af4fSAkash Goel 	assert_spin_locked(&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 
39259d02a1fSImre Deak u32 gen6_sanitize_rps_pm_mask(struct drm_i915_private *dev_priv, u32 mask)
39359d02a1fSImre Deak {
3941800ad25SSagar Arun Kamble 	return (mask & ~dev_priv->rps.pm_intr_keep);
39559d02a1fSImre Deak }
39659d02a1fSImre Deak 
39791d14251STvrtko Ursulin void gen6_disable_rps_interrupts(struct drm_i915_private *dev_priv)
398b900b949SImre Deak {
399f2a91d1aSChris Wilson 	if (!READ_ONCE(dev_priv->rps.interrupts_enabled))
400f2a91d1aSChris Wilson 		return;
401f2a91d1aSChris Wilson 
402d4d70aa5SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
403d4d70aa5SImre Deak 	dev_priv->rps.interrupts_enabled = false;
4049939fba2SImre Deak 
405b20e3cfeSDave Gordon 	I915_WRITE(GEN6_PMINTRMSK, gen6_sanitize_rps_pm_mask(dev_priv, ~0u));
4069939fba2SImre Deak 
407f4e9af4fSAkash Goel 	gen6_disable_pm_irq(dev_priv, dev_priv->pm_rps_events);
40858072ccbSImre Deak 
40958072ccbSImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
41091c8a326SChris Wilson 	synchronize_irq(dev_priv->drm.irq);
411c33d247dSChris Wilson 
412c33d247dSChris Wilson 	/* Now that we will not be generating any more work, flush any
413c33d247dSChris Wilson 	 * outsanding tasks. As we are called on the RPS idle path,
414c33d247dSChris Wilson 	 * we will reset the GPU to minimum frequencies, so the current
415c33d247dSChris Wilson 	 * state of the worker can be discarded.
416c33d247dSChris Wilson 	 */
417c33d247dSChris Wilson 	cancel_work_sync(&dev_priv->rps.work);
418c33d247dSChris Wilson 	gen6_reset_rps_interrupts(dev_priv);
419b900b949SImre Deak }
420b900b949SImre Deak 
42126705e20SSagar Arun Kamble void gen9_reset_guc_interrupts(struct drm_i915_private *dev_priv)
42226705e20SSagar Arun Kamble {
42326705e20SSagar Arun Kamble 	spin_lock_irq(&dev_priv->irq_lock);
42426705e20SSagar Arun Kamble 	gen6_reset_pm_iir(dev_priv, dev_priv->pm_guc_events);
42526705e20SSagar Arun Kamble 	spin_unlock_irq(&dev_priv->irq_lock);
42626705e20SSagar Arun Kamble }
42726705e20SSagar Arun Kamble 
42826705e20SSagar Arun Kamble void gen9_enable_guc_interrupts(struct drm_i915_private *dev_priv)
42926705e20SSagar Arun Kamble {
43026705e20SSagar Arun Kamble 	spin_lock_irq(&dev_priv->irq_lock);
43126705e20SSagar Arun Kamble 	if (!dev_priv->guc.interrupts_enabled) {
43226705e20SSagar Arun Kamble 		WARN_ON_ONCE(I915_READ(gen6_pm_iir(dev_priv)) &
43326705e20SSagar Arun Kamble 				       dev_priv->pm_guc_events);
43426705e20SSagar Arun Kamble 		dev_priv->guc.interrupts_enabled = true;
43526705e20SSagar Arun Kamble 		gen6_enable_pm_irq(dev_priv, dev_priv->pm_guc_events);
43626705e20SSagar Arun Kamble 	}
43726705e20SSagar Arun Kamble 	spin_unlock_irq(&dev_priv->irq_lock);
43826705e20SSagar Arun Kamble }
43926705e20SSagar Arun Kamble 
44026705e20SSagar Arun Kamble void gen9_disable_guc_interrupts(struct drm_i915_private *dev_priv)
44126705e20SSagar Arun Kamble {
44226705e20SSagar Arun Kamble 	spin_lock_irq(&dev_priv->irq_lock);
44326705e20SSagar Arun Kamble 	dev_priv->guc.interrupts_enabled = false;
44426705e20SSagar Arun Kamble 
44526705e20SSagar Arun Kamble 	gen6_disable_pm_irq(dev_priv, dev_priv->pm_guc_events);
44626705e20SSagar Arun Kamble 
44726705e20SSagar Arun Kamble 	spin_unlock_irq(&dev_priv->irq_lock);
44826705e20SSagar Arun Kamble 	synchronize_irq(dev_priv->drm.irq);
44926705e20SSagar Arun Kamble 
45026705e20SSagar Arun Kamble 	gen9_reset_guc_interrupts(dev_priv);
45126705e20SSagar Arun Kamble }
45226705e20SSagar Arun Kamble 
4530961021aSBen Widawsky /**
4543a3b3c7dSVille Syrjälä  * bdw_update_port_irq - update DE port interrupt
4553a3b3c7dSVille Syrjälä  * @dev_priv: driver private
4563a3b3c7dSVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
4573a3b3c7dSVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
4583a3b3c7dSVille Syrjälä  */
4593a3b3c7dSVille Syrjälä static void bdw_update_port_irq(struct drm_i915_private *dev_priv,
4603a3b3c7dSVille Syrjälä 				uint32_t interrupt_mask,
4613a3b3c7dSVille Syrjälä 				uint32_t enabled_irq_mask)
4623a3b3c7dSVille Syrjälä {
4633a3b3c7dSVille Syrjälä 	uint32_t new_val;
4643a3b3c7dSVille Syrjälä 	uint32_t old_val;
4653a3b3c7dSVille Syrjälä 
4663a3b3c7dSVille Syrjälä 	assert_spin_locked(&dev_priv->irq_lock);
4673a3b3c7dSVille Syrjälä 
4683a3b3c7dSVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
4693a3b3c7dSVille Syrjälä 
4703a3b3c7dSVille Syrjälä 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
4713a3b3c7dSVille Syrjälä 		return;
4723a3b3c7dSVille Syrjälä 
4733a3b3c7dSVille Syrjälä 	old_val = I915_READ(GEN8_DE_PORT_IMR);
4743a3b3c7dSVille Syrjälä 
4753a3b3c7dSVille Syrjälä 	new_val = old_val;
4763a3b3c7dSVille Syrjälä 	new_val &= ~interrupt_mask;
4773a3b3c7dSVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
4783a3b3c7dSVille Syrjälä 
4793a3b3c7dSVille Syrjälä 	if (new_val != old_val) {
4803a3b3c7dSVille Syrjälä 		I915_WRITE(GEN8_DE_PORT_IMR, new_val);
4813a3b3c7dSVille Syrjälä 		POSTING_READ(GEN8_DE_PORT_IMR);
4823a3b3c7dSVille Syrjälä 	}
4833a3b3c7dSVille Syrjälä }
4843a3b3c7dSVille Syrjälä 
4853a3b3c7dSVille Syrjälä /**
486013d3752SVille Syrjälä  * bdw_update_pipe_irq - update DE pipe interrupt
487013d3752SVille Syrjälä  * @dev_priv: driver private
488013d3752SVille Syrjälä  * @pipe: pipe whose interrupt to update
489013d3752SVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
490013d3752SVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
491013d3752SVille Syrjälä  */
492013d3752SVille Syrjälä void bdw_update_pipe_irq(struct drm_i915_private *dev_priv,
493013d3752SVille Syrjälä 			 enum pipe pipe,
494013d3752SVille Syrjälä 			 uint32_t interrupt_mask,
495013d3752SVille Syrjälä 			 uint32_t enabled_irq_mask)
496013d3752SVille Syrjälä {
497013d3752SVille Syrjälä 	uint32_t new_val;
498013d3752SVille Syrjälä 
499013d3752SVille Syrjälä 	assert_spin_locked(&dev_priv->irq_lock);
500013d3752SVille Syrjälä 
501013d3752SVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
502013d3752SVille Syrjälä 
503013d3752SVille Syrjälä 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
504013d3752SVille Syrjälä 		return;
505013d3752SVille Syrjälä 
506013d3752SVille Syrjälä 	new_val = dev_priv->de_irq_mask[pipe];
507013d3752SVille Syrjälä 	new_val &= ~interrupt_mask;
508013d3752SVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
509013d3752SVille Syrjälä 
510013d3752SVille Syrjälä 	if (new_val != dev_priv->de_irq_mask[pipe]) {
511013d3752SVille Syrjälä 		dev_priv->de_irq_mask[pipe] = new_val;
512013d3752SVille Syrjälä 		I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
513013d3752SVille Syrjälä 		POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
514013d3752SVille Syrjälä 	}
515013d3752SVille Syrjälä }
516013d3752SVille Syrjälä 
517013d3752SVille Syrjälä /**
518fee884edSDaniel Vetter  * ibx_display_interrupt_update - update SDEIMR
519fee884edSDaniel Vetter  * @dev_priv: driver private
520fee884edSDaniel Vetter  * @interrupt_mask: mask of interrupt bits to update
521fee884edSDaniel Vetter  * @enabled_irq_mask: mask of interrupt bits to enable
522fee884edSDaniel Vetter  */
52347339cd9SDaniel Vetter void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
524fee884edSDaniel Vetter 				  uint32_t interrupt_mask,
525fee884edSDaniel Vetter 				  uint32_t enabled_irq_mask)
526fee884edSDaniel Vetter {
527fee884edSDaniel Vetter 	uint32_t sdeimr = I915_READ(SDEIMR);
528fee884edSDaniel Vetter 	sdeimr &= ~interrupt_mask;
529fee884edSDaniel Vetter 	sdeimr |= (~enabled_irq_mask & interrupt_mask);
530fee884edSDaniel Vetter 
53115a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
53215a17aaeSDaniel Vetter 
533fee884edSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
534fee884edSDaniel Vetter 
5359df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
536c67a470bSPaulo Zanoni 		return;
537c67a470bSPaulo Zanoni 
538fee884edSDaniel Vetter 	I915_WRITE(SDEIMR, sdeimr);
539fee884edSDaniel Vetter 	POSTING_READ(SDEIMR);
540fee884edSDaniel Vetter }
5418664281bSPaulo Zanoni 
542b5ea642aSDaniel Vetter static void
543755e9019SImre Deak __i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
544755e9019SImre Deak 		       u32 enable_mask, u32 status_mask)
5457c463586SKeith Packard {
546f0f59a00SVille Syrjälä 	i915_reg_t reg = PIPESTAT(pipe);
547755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
5487c463586SKeith Packard 
549b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
550d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
551b79480baSDaniel Vetter 
55204feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
55304feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
55404feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
55504feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
556755e9019SImre Deak 		return;
557755e9019SImre Deak 
558755e9019SImre Deak 	if ((pipestat & enable_mask) == enable_mask)
55946c06a30SVille Syrjälä 		return;
56046c06a30SVille Syrjälä 
56191d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] |= status_mask;
56291d181ddSImre Deak 
5637c463586SKeith Packard 	/* Enable the interrupt, clear any pending status */
564755e9019SImre Deak 	pipestat |= enable_mask | status_mask;
56546c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
5663143a2bfSChris Wilson 	POSTING_READ(reg);
5677c463586SKeith Packard }
5687c463586SKeith Packard 
569b5ea642aSDaniel Vetter static void
570755e9019SImre Deak __i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
571755e9019SImre Deak 		        u32 enable_mask, u32 status_mask)
5727c463586SKeith Packard {
573f0f59a00SVille Syrjälä 	i915_reg_t reg = PIPESTAT(pipe);
574755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
5757c463586SKeith Packard 
576b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
577d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
578b79480baSDaniel Vetter 
57904feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
58004feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
58104feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
58204feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
58346c06a30SVille Syrjälä 		return;
58446c06a30SVille Syrjälä 
585755e9019SImre Deak 	if ((pipestat & enable_mask) == 0)
586755e9019SImre Deak 		return;
587755e9019SImre Deak 
58891d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] &= ~status_mask;
58991d181ddSImre Deak 
590755e9019SImre Deak 	pipestat &= ~enable_mask;
59146c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
5923143a2bfSChris Wilson 	POSTING_READ(reg);
5937c463586SKeith Packard }
5947c463586SKeith Packard 
59510c59c51SImre Deak static u32 vlv_get_pipestat_enable_mask(struct drm_device *dev, u32 status_mask)
59610c59c51SImre Deak {
59710c59c51SImre Deak 	u32 enable_mask = status_mask << 16;
59810c59c51SImre Deak 
59910c59c51SImre Deak 	/*
600724a6905SVille Syrjälä 	 * On pipe A we don't support the PSR interrupt yet,
601724a6905SVille Syrjälä 	 * on pipe B and C the same bit MBZ.
60210c59c51SImre Deak 	 */
60310c59c51SImre Deak 	if (WARN_ON_ONCE(status_mask & PIPE_A_PSR_STATUS_VLV))
60410c59c51SImre Deak 		return 0;
605724a6905SVille Syrjälä 	/*
606724a6905SVille Syrjälä 	 * On pipe B and C we don't support the PSR interrupt yet, on pipe
607724a6905SVille Syrjälä 	 * A the same bit is for perf counters which we don't use either.
608724a6905SVille Syrjälä 	 */
609724a6905SVille Syrjälä 	if (WARN_ON_ONCE(status_mask & PIPE_B_PSR_STATUS_VLV))
610724a6905SVille Syrjälä 		return 0;
61110c59c51SImre Deak 
61210c59c51SImre Deak 	enable_mask &= ~(PIPE_FIFO_UNDERRUN_STATUS |
61310c59c51SImre Deak 			 SPRITE0_FLIP_DONE_INT_EN_VLV |
61410c59c51SImre Deak 			 SPRITE1_FLIP_DONE_INT_EN_VLV);
61510c59c51SImre Deak 	if (status_mask & SPRITE0_FLIP_DONE_INT_STATUS_VLV)
61610c59c51SImre Deak 		enable_mask |= SPRITE0_FLIP_DONE_INT_EN_VLV;
61710c59c51SImre Deak 	if (status_mask & SPRITE1_FLIP_DONE_INT_STATUS_VLV)
61810c59c51SImre Deak 		enable_mask |= SPRITE1_FLIP_DONE_INT_EN_VLV;
61910c59c51SImre Deak 
62010c59c51SImre Deak 	return enable_mask;
62110c59c51SImre Deak }
62210c59c51SImre Deak 
623755e9019SImre Deak void
624755e9019SImre Deak i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
625755e9019SImre Deak 		     u32 status_mask)
626755e9019SImre Deak {
627755e9019SImre Deak 	u32 enable_mask;
628755e9019SImre Deak 
629666a4537SWayne Boyer 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
63091c8a326SChris Wilson 		enable_mask = vlv_get_pipestat_enable_mask(&dev_priv->drm,
63110c59c51SImre Deak 							   status_mask);
63210c59c51SImre Deak 	else
633755e9019SImre Deak 		enable_mask = status_mask << 16;
634755e9019SImre Deak 	__i915_enable_pipestat(dev_priv, pipe, enable_mask, status_mask);
635755e9019SImre Deak }
636755e9019SImre Deak 
637755e9019SImre Deak void
638755e9019SImre Deak i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
639755e9019SImre Deak 		      u32 status_mask)
640755e9019SImre Deak {
641755e9019SImre Deak 	u32 enable_mask;
642755e9019SImre Deak 
643666a4537SWayne Boyer 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
64491c8a326SChris Wilson 		enable_mask = vlv_get_pipestat_enable_mask(&dev_priv->drm,
64510c59c51SImre Deak 							   status_mask);
64610c59c51SImre Deak 	else
647755e9019SImre Deak 		enable_mask = status_mask << 16;
648755e9019SImre Deak 	__i915_disable_pipestat(dev_priv, pipe, enable_mask, status_mask);
649755e9019SImre Deak }
650755e9019SImre Deak 
651c0e09200SDave Airlie /**
652f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
65314bb2c11STvrtko Ursulin  * @dev_priv: i915 device private
65401c66889SZhao Yakui  */
65591d14251STvrtko Ursulin static void i915_enable_asle_pipestat(struct drm_i915_private *dev_priv)
65601c66889SZhao Yakui {
65791d14251STvrtko Ursulin 	if (!dev_priv->opregion.asle || !IS_MOBILE(dev_priv))
658f49e38ddSJani Nikula 		return;
659f49e38ddSJani Nikula 
66013321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
66101c66889SZhao Yakui 
662755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_STATUS);
66391d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 4)
6643b6c42e8SDaniel Vetter 		i915_enable_pipestat(dev_priv, PIPE_A,
665755e9019SImre Deak 				     PIPE_LEGACY_BLC_EVENT_STATUS);
6661ec14ad3SChris Wilson 
66713321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
66801c66889SZhao Yakui }
66901c66889SZhao Yakui 
670f75f3746SVille Syrjälä /*
671f75f3746SVille Syrjälä  * This timing diagram depicts the video signal in and
672f75f3746SVille Syrjälä  * around the vertical blanking period.
673f75f3746SVille Syrjälä  *
674f75f3746SVille Syrjälä  * Assumptions about the fictitious mode used in this example:
675f75f3746SVille Syrjälä  *  vblank_start >= 3
676f75f3746SVille Syrjälä  *  vsync_start = vblank_start + 1
677f75f3746SVille Syrjälä  *  vsync_end = vblank_start + 2
678f75f3746SVille Syrjälä  *  vtotal = vblank_start + 3
679f75f3746SVille Syrjälä  *
680f75f3746SVille Syrjälä  *           start of vblank:
681f75f3746SVille Syrjälä  *           latch double buffered registers
682f75f3746SVille Syrjälä  *           increment frame counter (ctg+)
683f75f3746SVille Syrjälä  *           generate start of vblank interrupt (gen4+)
684f75f3746SVille Syrjälä  *           |
685f75f3746SVille Syrjälä  *           |          frame start:
686f75f3746SVille Syrjälä  *           |          generate frame start interrupt (aka. vblank interrupt) (gmch)
687f75f3746SVille Syrjälä  *           |          may be shifted forward 1-3 extra lines via PIPECONF
688f75f3746SVille Syrjälä  *           |          |
689f75f3746SVille Syrjälä  *           |          |  start of vsync:
690f75f3746SVille Syrjälä  *           |          |  generate vsync interrupt
691f75f3746SVille Syrjälä  *           |          |  |
692f75f3746SVille Syrjälä  * ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx
693f75f3746SVille Syrjälä  *       .   \hs/   .      \hs/          \hs/          \hs/   .      \hs/
694f75f3746SVille Syrjälä  * ----va---> <-----------------vb--------------------> <--------va-------------
695f75f3746SVille Syrjälä  *       |          |       <----vs----->                     |
696f75f3746SVille Syrjälä  * -vbs-----> <---vbs+1---> <---vbs+2---> <-----0-----> <-----1-----> <-----2--- (scanline counter gen2)
697f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2---> <-----0--- (scanline counter gen3+)
698f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2- (scanline counter hsw+ hdmi)
699f75f3746SVille Syrjälä  *       |          |                                         |
700f75f3746SVille Syrjälä  *       last visible pixel                                   first visible pixel
701f75f3746SVille Syrjälä  *                  |                                         increment frame counter (gen3/4)
702f75f3746SVille Syrjälä  *                  pixel counter = vblank_start * htotal     pixel counter = 0 (gen3/4)
703f75f3746SVille Syrjälä  *
704f75f3746SVille Syrjälä  * x  = horizontal active
705f75f3746SVille Syrjälä  * _  = horizontal blanking
706f75f3746SVille Syrjälä  * hs = horizontal sync
707f75f3746SVille Syrjälä  * va = vertical active
708f75f3746SVille Syrjälä  * vb = vertical blanking
709f75f3746SVille Syrjälä  * vs = vertical sync
710f75f3746SVille Syrjälä  * vbs = vblank_start (number)
711f75f3746SVille Syrjälä  *
712f75f3746SVille Syrjälä  * Summary:
713f75f3746SVille Syrjälä  * - most events happen at the start of horizontal sync
714f75f3746SVille Syrjälä  * - frame start happens at the start of horizontal blank, 1-4 lines
715f75f3746SVille Syrjälä  *   (depending on PIPECONF settings) after the start of vblank
716f75f3746SVille Syrjälä  * - gen3/4 pixel and frame counter are synchronized with the start
717f75f3746SVille Syrjälä  *   of horizontal active on the first line of vertical active
718f75f3746SVille Syrjälä  */
719f75f3746SVille Syrjälä 
72042f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
72142f52ef8SKeith Packard  * we use as a pipe index
72242f52ef8SKeith Packard  */
72388e72717SThierry Reding static u32 i915_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
7240a3e67a4SJesse Barnes {
725fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
726f0f59a00SVille Syrjälä 	i915_reg_t high_frame, low_frame;
7270b2a8e09SVille Syrjälä 	u32 high1, high2, low, pixel, vbl_start, hsync_start, htotal;
728e2af48c6SVille Syrjälä 	struct intel_crtc *intel_crtc = dev_priv->pipe_to_crtc_mapping[pipe];
729fc467a22SMaarten Lankhorst 	const struct drm_display_mode *mode = &intel_crtc->base.hwmode;
730391f75e2SVille Syrjälä 
7310b2a8e09SVille Syrjälä 	htotal = mode->crtc_htotal;
7320b2a8e09SVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
7330b2a8e09SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
7340b2a8e09SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
7350b2a8e09SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
736391f75e2SVille Syrjälä 
7370b2a8e09SVille Syrjälä 	/* Convert to pixel count */
7380b2a8e09SVille Syrjälä 	vbl_start *= htotal;
7390b2a8e09SVille Syrjälä 
7400b2a8e09SVille Syrjälä 	/* Start of vblank event occurs at start of hsync */
7410b2a8e09SVille Syrjälä 	vbl_start -= htotal - hsync_start;
7420b2a8e09SVille Syrjälä 
7439db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
7449db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
7455eddb70bSChris Wilson 
7460a3e67a4SJesse Barnes 	/*
7470a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
7480a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
7490a3e67a4SJesse Barnes 	 * register.
7500a3e67a4SJesse Barnes 	 */
7510a3e67a4SJesse Barnes 	do {
7525eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
753391f75e2SVille Syrjälä 		low   = I915_READ(low_frame);
7545eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
7550a3e67a4SJesse Barnes 	} while (high1 != high2);
7560a3e67a4SJesse Barnes 
7575eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
758391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
7595eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
760391f75e2SVille Syrjälä 
761391f75e2SVille Syrjälä 	/*
762391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
763391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
764391f75e2SVille Syrjälä 	 * counter against vblank start.
765391f75e2SVille Syrjälä 	 */
766edc08d0aSVille Syrjälä 	return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
7670a3e67a4SJesse Barnes }
7680a3e67a4SJesse Barnes 
769974e59baSDave Airlie static u32 g4x_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
7709880b7a5SJesse Barnes {
771fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
7729880b7a5SJesse Barnes 
773649636efSVille Syrjälä 	return I915_READ(PIPE_FRMCOUNT_G4X(pipe));
7749880b7a5SJesse Barnes }
7759880b7a5SJesse Barnes 
77675aa3f63SVille Syrjälä /* I915_READ_FW, only for fast reads of display block, no need for forcewake etc. */
777a225f079SVille Syrjälä static int __intel_get_crtc_scanline(struct intel_crtc *crtc)
778a225f079SVille Syrjälä {
779a225f079SVille Syrjälä 	struct drm_device *dev = crtc->base.dev;
780fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
781fc467a22SMaarten Lankhorst 	const struct drm_display_mode *mode = &crtc->base.hwmode;
782a225f079SVille Syrjälä 	enum pipe pipe = crtc->pipe;
78380715b2fSVille Syrjälä 	int position, vtotal;
784a225f079SVille Syrjälä 
78580715b2fSVille Syrjälä 	vtotal = mode->crtc_vtotal;
786a225f079SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
787a225f079SVille Syrjälä 		vtotal /= 2;
788a225f079SVille Syrjälä 
78991d14251STvrtko Ursulin 	if (IS_GEN2(dev_priv))
79075aa3f63SVille Syrjälä 		position = I915_READ_FW(PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
791a225f079SVille Syrjälä 	else
79275aa3f63SVille Syrjälä 		position = I915_READ_FW(PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
793a225f079SVille Syrjälä 
794a225f079SVille Syrjälä 	/*
79541b578fbSJesse Barnes 	 * On HSW, the DSL reg (0x70000) appears to return 0 if we
79641b578fbSJesse Barnes 	 * read it just before the start of vblank.  So try it again
79741b578fbSJesse Barnes 	 * so we don't accidentally end up spanning a vblank frame
79841b578fbSJesse Barnes 	 * increment, causing the pipe_update_end() code to squak at us.
79941b578fbSJesse Barnes 	 *
80041b578fbSJesse Barnes 	 * The nature of this problem means we can't simply check the ISR
80141b578fbSJesse Barnes 	 * bit and return the vblank start value; nor can we use the scanline
80241b578fbSJesse Barnes 	 * debug register in the transcoder as it appears to have the same
80341b578fbSJesse Barnes 	 * problem.  We may need to extend this to include other platforms,
80441b578fbSJesse Barnes 	 * but so far testing only shows the problem on HSW.
80541b578fbSJesse Barnes 	 */
80691d14251STvrtko Ursulin 	if (HAS_DDI(dev_priv) && !position) {
80741b578fbSJesse Barnes 		int i, temp;
80841b578fbSJesse Barnes 
80941b578fbSJesse Barnes 		for (i = 0; i < 100; i++) {
81041b578fbSJesse Barnes 			udelay(1);
81141b578fbSJesse Barnes 			temp = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) &
81241b578fbSJesse Barnes 				DSL_LINEMASK_GEN3;
81341b578fbSJesse Barnes 			if (temp != position) {
81441b578fbSJesse Barnes 				position = temp;
81541b578fbSJesse Barnes 				break;
81641b578fbSJesse Barnes 			}
81741b578fbSJesse Barnes 		}
81841b578fbSJesse Barnes 	}
81941b578fbSJesse Barnes 
82041b578fbSJesse Barnes 	/*
82180715b2fSVille Syrjälä 	 * See update_scanline_offset() for the details on the
82280715b2fSVille Syrjälä 	 * scanline_offset adjustment.
823a225f079SVille Syrjälä 	 */
82480715b2fSVille Syrjälä 	return (position + crtc->scanline_offset) % vtotal;
825a225f079SVille Syrjälä }
826a225f079SVille Syrjälä 
82788e72717SThierry Reding static int i915_get_crtc_scanoutpos(struct drm_device *dev, unsigned int pipe,
828abca9e45SVille Syrjälä 				    unsigned int flags, int *vpos, int *hpos,
8293bb403bfSVille Syrjälä 				    ktime_t *stime, ktime_t *etime,
8303bb403bfSVille Syrjälä 				    const struct drm_display_mode *mode)
8310af7e4dfSMario Kleiner {
832fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
833e2af48c6SVille Syrjälä 	struct intel_crtc *intel_crtc = dev_priv->pipe_to_crtc_mapping[pipe];
8343aa18df8SVille Syrjälä 	int position;
83578e8fc6bSVille Syrjälä 	int vbl_start, vbl_end, hsync_start, htotal, vtotal;
8360af7e4dfSMario Kleiner 	bool in_vbl = true;
8370af7e4dfSMario Kleiner 	int ret = 0;
838ad3543edSMario Kleiner 	unsigned long irqflags;
8390af7e4dfSMario Kleiner 
840fc467a22SMaarten Lankhorst 	if (WARN_ON(!mode->crtc_clock)) {
8410af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
8429db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
8430af7e4dfSMario Kleiner 		return 0;
8440af7e4dfSMario Kleiner 	}
8450af7e4dfSMario Kleiner 
846c2baf4b7SVille Syrjälä 	htotal = mode->crtc_htotal;
84778e8fc6bSVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
848c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
849c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
850c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
8510af7e4dfSMario Kleiner 
852d31faf65SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
853d31faf65SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
854d31faf65SVille Syrjälä 		vbl_end /= 2;
855d31faf65SVille Syrjälä 		vtotal /= 2;
856d31faf65SVille Syrjälä 	}
857d31faf65SVille Syrjälä 
858c2baf4b7SVille Syrjälä 	ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
859c2baf4b7SVille Syrjälä 
860ad3543edSMario Kleiner 	/*
861ad3543edSMario Kleiner 	 * Lock uncore.lock, as we will do multiple timing critical raw
862ad3543edSMario Kleiner 	 * register reads, potentially with preemption disabled, so the
863ad3543edSMario Kleiner 	 * following code must not block on uncore.lock.
864ad3543edSMario Kleiner 	 */
865ad3543edSMario Kleiner 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
866ad3543edSMario Kleiner 
867ad3543edSMario Kleiner 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
868ad3543edSMario Kleiner 
869ad3543edSMario Kleiner 	/* Get optional system timestamp before query. */
870ad3543edSMario Kleiner 	if (stime)
871ad3543edSMario Kleiner 		*stime = ktime_get();
872ad3543edSMario Kleiner 
87391d14251STvrtko Ursulin 	if (IS_GEN2(dev_priv) || IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5) {
8740af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
8750af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
8760af7e4dfSMario Kleiner 		 */
877a225f079SVille Syrjälä 		position = __intel_get_crtc_scanline(intel_crtc);
8780af7e4dfSMario Kleiner 	} else {
8790af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
8800af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
8810af7e4dfSMario Kleiner 		 * scanout position.
8820af7e4dfSMario Kleiner 		 */
88375aa3f63SVille Syrjälä 		position = (I915_READ_FW(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
8840af7e4dfSMario Kleiner 
8853aa18df8SVille Syrjälä 		/* convert to pixel counts */
8863aa18df8SVille Syrjälä 		vbl_start *= htotal;
8873aa18df8SVille Syrjälä 		vbl_end *= htotal;
8883aa18df8SVille Syrjälä 		vtotal *= htotal;
88978e8fc6bSVille Syrjälä 
89078e8fc6bSVille Syrjälä 		/*
8917e78f1cbSVille Syrjälä 		 * In interlaced modes, the pixel counter counts all pixels,
8927e78f1cbSVille Syrjälä 		 * so one field will have htotal more pixels. In order to avoid
8937e78f1cbSVille Syrjälä 		 * the reported position from jumping backwards when the pixel
8947e78f1cbSVille Syrjälä 		 * counter is beyond the length of the shorter field, just
8957e78f1cbSVille Syrjälä 		 * clamp the position the length of the shorter field. This
8967e78f1cbSVille Syrjälä 		 * matches how the scanline counter based position works since
8977e78f1cbSVille Syrjälä 		 * the scanline counter doesn't count the two half lines.
8987e78f1cbSVille Syrjälä 		 */
8997e78f1cbSVille Syrjälä 		if (position >= vtotal)
9007e78f1cbSVille Syrjälä 			position = vtotal - 1;
9017e78f1cbSVille Syrjälä 
9027e78f1cbSVille Syrjälä 		/*
90378e8fc6bSVille Syrjälä 		 * Start of vblank interrupt is triggered at start of hsync,
90478e8fc6bSVille Syrjälä 		 * just prior to the first active line of vblank. However we
90578e8fc6bSVille Syrjälä 		 * consider lines to start at the leading edge of horizontal
90678e8fc6bSVille Syrjälä 		 * active. So, should we get here before we've crossed into
90778e8fc6bSVille Syrjälä 		 * the horizontal active of the first line in vblank, we would
90878e8fc6bSVille Syrjälä 		 * not set the DRM_SCANOUTPOS_INVBL flag. In order to fix that,
90978e8fc6bSVille Syrjälä 		 * always add htotal-hsync_start to the current pixel position.
91078e8fc6bSVille Syrjälä 		 */
91178e8fc6bSVille Syrjälä 		position = (position + htotal - hsync_start) % vtotal;
9123aa18df8SVille Syrjälä 	}
9133aa18df8SVille Syrjälä 
914ad3543edSMario Kleiner 	/* Get optional system timestamp after query. */
915ad3543edSMario Kleiner 	if (etime)
916ad3543edSMario Kleiner 		*etime = ktime_get();
917ad3543edSMario Kleiner 
918ad3543edSMario Kleiner 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
919ad3543edSMario Kleiner 
920ad3543edSMario Kleiner 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
921ad3543edSMario Kleiner 
9223aa18df8SVille Syrjälä 	in_vbl = position >= vbl_start && position < vbl_end;
9233aa18df8SVille Syrjälä 
9243aa18df8SVille Syrjälä 	/*
9253aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
9263aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
9273aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
9283aa18df8SVille Syrjälä 	 * up since vbl_end.
9293aa18df8SVille Syrjälä 	 */
9303aa18df8SVille Syrjälä 	if (position >= vbl_start)
9313aa18df8SVille Syrjälä 		position -= vbl_end;
9323aa18df8SVille Syrjälä 	else
9333aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
9343aa18df8SVille Syrjälä 
93591d14251STvrtko Ursulin 	if (IS_GEN2(dev_priv) || IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5) {
9363aa18df8SVille Syrjälä 		*vpos = position;
9373aa18df8SVille Syrjälä 		*hpos = 0;
9383aa18df8SVille Syrjälä 	} else {
9390af7e4dfSMario Kleiner 		*vpos = position / htotal;
9400af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
9410af7e4dfSMario Kleiner 	}
9420af7e4dfSMario Kleiner 
9430af7e4dfSMario Kleiner 	/* In vblank? */
9440af7e4dfSMario Kleiner 	if (in_vbl)
9453d3cbd84SDaniel Vetter 		ret |= DRM_SCANOUTPOS_IN_VBLANK;
9460af7e4dfSMario Kleiner 
9470af7e4dfSMario Kleiner 	return ret;
9480af7e4dfSMario Kleiner }
9490af7e4dfSMario Kleiner 
950a225f079SVille Syrjälä int intel_get_crtc_scanline(struct intel_crtc *crtc)
951a225f079SVille Syrjälä {
952fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
953a225f079SVille Syrjälä 	unsigned long irqflags;
954a225f079SVille Syrjälä 	int position;
955a225f079SVille Syrjälä 
956a225f079SVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
957a225f079SVille Syrjälä 	position = __intel_get_crtc_scanline(crtc);
958a225f079SVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
959a225f079SVille Syrjälä 
960a225f079SVille Syrjälä 	return position;
961a225f079SVille Syrjälä }
962a225f079SVille Syrjälä 
96388e72717SThierry Reding static int i915_get_vblank_timestamp(struct drm_device *dev, unsigned int pipe,
9640af7e4dfSMario Kleiner 			      int *max_error,
9650af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
9660af7e4dfSMario Kleiner 			      unsigned flags)
9670af7e4dfSMario Kleiner {
968*b91eb5ccSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(dev);
969e2af48c6SVille Syrjälä 	struct intel_crtc *crtc;
9700af7e4dfSMario Kleiner 
971*b91eb5ccSVille Syrjälä 	if (pipe >= INTEL_INFO(dev_priv)->num_pipes) {
97288e72717SThierry Reding 		DRM_ERROR("Invalid crtc %u\n", pipe);
9730af7e4dfSMario Kleiner 		return -EINVAL;
9740af7e4dfSMario Kleiner 	}
9750af7e4dfSMario Kleiner 
9760af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
977*b91eb5ccSVille Syrjälä 	crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
9784041b853SChris Wilson 	if (crtc == NULL) {
97988e72717SThierry Reding 		DRM_ERROR("Invalid crtc %u\n", pipe);
9804041b853SChris Wilson 		return -EINVAL;
9814041b853SChris Wilson 	}
9824041b853SChris Wilson 
983e2af48c6SVille Syrjälä 	if (!crtc->base.hwmode.crtc_clock) {
98488e72717SThierry Reding 		DRM_DEBUG_KMS("crtc %u is disabled\n", pipe);
9854041b853SChris Wilson 		return -EBUSY;
9864041b853SChris Wilson 	}
9870af7e4dfSMario Kleiner 
9880af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
9894041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
9904041b853SChris Wilson 						     vblank_time, flags,
991e2af48c6SVille Syrjälä 						     &crtc->base.hwmode);
9920af7e4dfSMario Kleiner }
9930af7e4dfSMario Kleiner 
99491d14251STvrtko Ursulin static void ironlake_rps_change_irq_handler(struct drm_i915_private *dev_priv)
995f97108d1SJesse Barnes {
996b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
9979270388eSDaniel Vetter 	u8 new_delay;
9989270388eSDaniel Vetter 
999d0ecd7e2SDaniel Vetter 	spin_lock(&mchdev_lock);
1000f97108d1SJesse Barnes 
100173edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
100273edd18fSDaniel Vetter 
100320e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
10049270388eSDaniel Vetter 
10057648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
1006b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
1007b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
1008f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
1009f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
1010f97108d1SJesse Barnes 
1011f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
1012b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
101320e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
101420e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
101520e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
101620e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
1017b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
101820e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
101920e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
102020e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
102120e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
1022f97108d1SJesse Barnes 	}
1023f97108d1SJesse Barnes 
102491d14251STvrtko Ursulin 	if (ironlake_set_drps(dev_priv, new_delay))
102520e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
1026f97108d1SJesse Barnes 
1027d0ecd7e2SDaniel Vetter 	spin_unlock(&mchdev_lock);
10289270388eSDaniel Vetter 
1029f97108d1SJesse Barnes 	return;
1030f97108d1SJesse Barnes }
1031f97108d1SJesse Barnes 
10320bc40be8STvrtko Ursulin static void notify_ring(struct intel_engine_cs *engine)
1033549f7365SChris Wilson {
1034aca34b6eSChris Wilson 	smp_store_mb(engine->breadcrumbs.irq_posted, true);
103583348ba8SChris Wilson 	if (intel_engine_wakeup(engine))
10360bc40be8STvrtko Ursulin 		trace_i915_gem_request_notify(engine);
1037549f7365SChris Wilson }
1038549f7365SChris Wilson 
103943cf3bf0SChris Wilson static void vlv_c0_read(struct drm_i915_private *dev_priv,
104043cf3bf0SChris Wilson 			struct intel_rps_ei *ei)
104131685c25SDeepak S {
104243cf3bf0SChris Wilson 	ei->cz_clock = vlv_punit_read(dev_priv, PUNIT_REG_CZ_TIMESTAMP);
104343cf3bf0SChris Wilson 	ei->render_c0 = I915_READ(VLV_RENDER_C0_COUNT);
104443cf3bf0SChris Wilson 	ei->media_c0 = I915_READ(VLV_MEDIA_C0_COUNT);
104531685c25SDeepak S }
104631685c25SDeepak S 
104743cf3bf0SChris Wilson static bool vlv_c0_above(struct drm_i915_private *dev_priv,
104843cf3bf0SChris Wilson 			 const struct intel_rps_ei *old,
104943cf3bf0SChris Wilson 			 const struct intel_rps_ei *now,
105043cf3bf0SChris Wilson 			 int threshold)
105131685c25SDeepak S {
105243cf3bf0SChris Wilson 	u64 time, c0;
10537bad74d5SVille Syrjälä 	unsigned int mul = 100;
105431685c25SDeepak S 
105543cf3bf0SChris Wilson 	if (old->cz_clock == 0)
105643cf3bf0SChris Wilson 		return false;
105731685c25SDeepak S 
10587bad74d5SVille Syrjälä 	if (I915_READ(VLV_COUNTER_CONTROL) & VLV_COUNT_RANGE_HIGH)
10597bad74d5SVille Syrjälä 		mul <<= 8;
10607bad74d5SVille Syrjälä 
106143cf3bf0SChris Wilson 	time = now->cz_clock - old->cz_clock;
10627bad74d5SVille Syrjälä 	time *= threshold * dev_priv->czclk_freq;
106331685c25SDeepak S 
106443cf3bf0SChris Wilson 	/* Workload can be split between render + media, e.g. SwapBuffers
106543cf3bf0SChris Wilson 	 * being blitted in X after being rendered in mesa. To account for
106643cf3bf0SChris Wilson 	 * this we need to combine both engines into our activity counter.
106743cf3bf0SChris Wilson 	 */
106843cf3bf0SChris Wilson 	c0 = now->render_c0 - old->render_c0;
106943cf3bf0SChris Wilson 	c0 += now->media_c0 - old->media_c0;
10707bad74d5SVille Syrjälä 	c0 *= mul * VLV_CZ_CLOCK_TO_MILLI_SEC;
107131685c25SDeepak S 
107243cf3bf0SChris Wilson 	return c0 >= time;
107331685c25SDeepak S }
107431685c25SDeepak S 
107543cf3bf0SChris Wilson void gen6_rps_reset_ei(struct drm_i915_private *dev_priv)
107643cf3bf0SChris Wilson {
107743cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &dev_priv->rps.down_ei);
107843cf3bf0SChris Wilson 	dev_priv->rps.up_ei = dev_priv->rps.down_ei;
107943cf3bf0SChris Wilson }
108043cf3bf0SChris Wilson 
108143cf3bf0SChris Wilson static u32 vlv_wa_c0_ei(struct drm_i915_private *dev_priv, u32 pm_iir)
108243cf3bf0SChris Wilson {
108343cf3bf0SChris Wilson 	struct intel_rps_ei now;
108443cf3bf0SChris Wilson 	u32 events = 0;
108543cf3bf0SChris Wilson 
10866f4b12f8SChris Wilson 	if ((pm_iir & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED)) == 0)
108743cf3bf0SChris Wilson 		return 0;
108843cf3bf0SChris Wilson 
108943cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &now);
109043cf3bf0SChris Wilson 	if (now.cz_clock == 0)
109143cf3bf0SChris Wilson 		return 0;
109231685c25SDeepak S 
109343cf3bf0SChris Wilson 	if (pm_iir & GEN6_PM_RP_DOWN_EI_EXPIRED) {
109443cf3bf0SChris Wilson 		if (!vlv_c0_above(dev_priv,
109543cf3bf0SChris Wilson 				  &dev_priv->rps.down_ei, &now,
10968fb55197SChris Wilson 				  dev_priv->rps.down_threshold))
109743cf3bf0SChris Wilson 			events |= GEN6_PM_RP_DOWN_THRESHOLD;
109843cf3bf0SChris Wilson 		dev_priv->rps.down_ei = now;
109931685c25SDeepak S 	}
110031685c25SDeepak S 
110143cf3bf0SChris Wilson 	if (pm_iir & GEN6_PM_RP_UP_EI_EXPIRED) {
110243cf3bf0SChris Wilson 		if (vlv_c0_above(dev_priv,
110343cf3bf0SChris Wilson 				 &dev_priv->rps.up_ei, &now,
11048fb55197SChris Wilson 				 dev_priv->rps.up_threshold))
110543cf3bf0SChris Wilson 			events |= GEN6_PM_RP_UP_THRESHOLD;
110643cf3bf0SChris Wilson 		dev_priv->rps.up_ei = now;
110743cf3bf0SChris Wilson 	}
110843cf3bf0SChris Wilson 
110943cf3bf0SChris Wilson 	return events;
111031685c25SDeepak S }
111131685c25SDeepak S 
1112f5a4c67dSChris Wilson static bool any_waiters(struct drm_i915_private *dev_priv)
1113f5a4c67dSChris Wilson {
1114e2f80391STvrtko Ursulin 	struct intel_engine_cs *engine;
11153b3f1650SAkash Goel 	enum intel_engine_id id;
1116f5a4c67dSChris Wilson 
11173b3f1650SAkash Goel 	for_each_engine(engine, dev_priv, id)
1118688e6c72SChris Wilson 		if (intel_engine_has_waiter(engine))
1119f5a4c67dSChris Wilson 			return true;
1120f5a4c67dSChris Wilson 
1121f5a4c67dSChris Wilson 	return false;
1122f5a4c67dSChris Wilson }
1123f5a4c67dSChris Wilson 
11244912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
11253b8d8d91SJesse Barnes {
11262d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
11272d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, rps.work);
11288d3afd7dSChris Wilson 	bool client_boost;
11298d3afd7dSChris Wilson 	int new_delay, adj, min, max;
1130edbfdb45SPaulo Zanoni 	u32 pm_iir;
11313b8d8d91SJesse Barnes 
113259cdb63dSDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
1133d4d70aa5SImre Deak 	/* Speed up work cancelation during disabling rps interrupts. */
1134d4d70aa5SImre Deak 	if (!dev_priv->rps.interrupts_enabled) {
1135d4d70aa5SImre Deak 		spin_unlock_irq(&dev_priv->irq_lock);
1136d4d70aa5SImre Deak 		return;
1137d4d70aa5SImre Deak 	}
11381f814dacSImre Deak 
1139c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
1140c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
1141a72fbc3aSImre Deak 	/* Make sure not to corrupt PMIMR state used by ringbuffer on GEN6 */
1142f4e9af4fSAkash Goel 	gen6_unmask_pm_irq(dev_priv, dev_priv->pm_rps_events);
11438d3afd7dSChris Wilson 	client_boost = dev_priv->rps.client_boost;
11448d3afd7dSChris Wilson 	dev_priv->rps.client_boost = false;
114559cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
11464912d041SBen Widawsky 
114760611c13SPaulo Zanoni 	/* Make sure we didn't queue anything we're not going to process. */
1148a6706b45SDeepak S 	WARN_ON(pm_iir & ~dev_priv->pm_rps_events);
114960611c13SPaulo Zanoni 
11508d3afd7dSChris Wilson 	if ((pm_iir & dev_priv->pm_rps_events) == 0 && !client_boost)
1151c33d247dSChris Wilson 		return;
11523b8d8d91SJesse Barnes 
11534fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
11547b9e0ae6SChris Wilson 
115543cf3bf0SChris Wilson 	pm_iir |= vlv_wa_c0_ei(dev_priv, pm_iir);
115643cf3bf0SChris Wilson 
1157dd75fdc8SChris Wilson 	adj = dev_priv->rps.last_adj;
1158edcf284bSChris Wilson 	new_delay = dev_priv->rps.cur_freq;
11598d3afd7dSChris Wilson 	min = dev_priv->rps.min_freq_softlimit;
11608d3afd7dSChris Wilson 	max = dev_priv->rps.max_freq_softlimit;
116129ecd78dSChris Wilson 	if (client_boost || any_waiters(dev_priv))
116229ecd78dSChris Wilson 		max = dev_priv->rps.max_freq;
116329ecd78dSChris Wilson 	if (client_boost && new_delay < dev_priv->rps.boost_freq) {
116429ecd78dSChris Wilson 		new_delay = dev_priv->rps.boost_freq;
11658d3afd7dSChris Wilson 		adj = 0;
11668d3afd7dSChris Wilson 	} else if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
1167dd75fdc8SChris Wilson 		if (adj > 0)
1168dd75fdc8SChris Wilson 			adj *= 2;
1169edcf284bSChris Wilson 		else /* CHV needs even encode values */
1170edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? 2 : 1;
11717425034aSVille Syrjälä 		/*
11727425034aSVille Syrjälä 		 * For better performance, jump directly
11737425034aSVille Syrjälä 		 * to RPe if we're below it.
11747425034aSVille Syrjälä 		 */
1175edcf284bSChris Wilson 		if (new_delay < dev_priv->rps.efficient_freq - adj) {
1176b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1177edcf284bSChris Wilson 			adj = 0;
1178edcf284bSChris Wilson 		}
117929ecd78dSChris Wilson 	} else if (client_boost || any_waiters(dev_priv)) {
1180f5a4c67dSChris Wilson 		adj = 0;
1181dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
1182b39fb297SBen Widawsky 		if (dev_priv->rps.cur_freq > dev_priv->rps.efficient_freq)
1183b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1184dd75fdc8SChris Wilson 		else
1185b39fb297SBen Widawsky 			new_delay = dev_priv->rps.min_freq_softlimit;
1186dd75fdc8SChris Wilson 		adj = 0;
1187dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1188dd75fdc8SChris Wilson 		if (adj < 0)
1189dd75fdc8SChris Wilson 			adj *= 2;
1190edcf284bSChris Wilson 		else /* CHV needs even encode values */
1191edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? -2 : -1;
1192dd75fdc8SChris Wilson 	} else { /* unknown event */
1193edcf284bSChris Wilson 		adj = 0;
1194dd75fdc8SChris Wilson 	}
11953b8d8d91SJesse Barnes 
1196edcf284bSChris Wilson 	dev_priv->rps.last_adj = adj;
1197edcf284bSChris Wilson 
119879249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
119979249636SBen Widawsky 	 * interrupt
120079249636SBen Widawsky 	 */
1201edcf284bSChris Wilson 	new_delay += adj;
12028d3afd7dSChris Wilson 	new_delay = clamp_t(int, new_delay, min, max);
120327544369SDeepak S 
1204dc97997aSChris Wilson 	intel_set_rps(dev_priv, new_delay);
12053b8d8d91SJesse Barnes 
12064fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
12073b8d8d91SJesse Barnes }
12083b8d8d91SJesse Barnes 
1209e3689190SBen Widawsky 
1210e3689190SBen Widawsky /**
1211e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
1212e3689190SBen Widawsky  * occurred.
1213e3689190SBen Widawsky  * @work: workqueue struct
1214e3689190SBen Widawsky  *
1215e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
1216e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
1217e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
1218e3689190SBen Widawsky  */
1219e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
1220e3689190SBen Widawsky {
12212d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
12222d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, l3_parity.error_work);
1223e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
122435a85ac6SBen Widawsky 	char *parity_event[6];
1225e3689190SBen Widawsky 	uint32_t misccpctl;
122635a85ac6SBen Widawsky 	uint8_t slice = 0;
1227e3689190SBen Widawsky 
1228e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
1229e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
1230e3689190SBen Widawsky 	 * any time we access those registers.
1231e3689190SBen Widawsky 	 */
123291c8a326SChris Wilson 	mutex_lock(&dev_priv->drm.struct_mutex);
1233e3689190SBen Widawsky 
123435a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
123535a85ac6SBen Widawsky 	if (WARN_ON(!dev_priv->l3_parity.which_slice))
123635a85ac6SBen Widawsky 		goto out;
123735a85ac6SBen Widawsky 
1238e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
1239e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1240e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
1241e3689190SBen Widawsky 
124235a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
1243f0f59a00SVille Syrjälä 		i915_reg_t reg;
124435a85ac6SBen Widawsky 
124535a85ac6SBen Widawsky 		slice--;
12462d1fe073SJoonas Lahtinen 		if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv)))
124735a85ac6SBen Widawsky 			break;
124835a85ac6SBen Widawsky 
124935a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
125035a85ac6SBen Widawsky 
12516fa1c5f1SVille Syrjälä 		reg = GEN7_L3CDERRST1(slice);
125235a85ac6SBen Widawsky 
125335a85ac6SBen Widawsky 		error_status = I915_READ(reg);
1254e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
1255e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
1256e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1257e3689190SBen Widawsky 
125835a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
125935a85ac6SBen Widawsky 		POSTING_READ(reg);
1260e3689190SBen Widawsky 
1261cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1262e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1263e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1264e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
126535a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
126635a85ac6SBen Widawsky 		parity_event[5] = NULL;
1267e3689190SBen Widawsky 
126891c8a326SChris Wilson 		kobject_uevent_env(&dev_priv->drm.primary->kdev->kobj,
1269e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
1270e3689190SBen Widawsky 
127135a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
127235a85ac6SBen Widawsky 			  slice, row, bank, subbank);
1273e3689190SBen Widawsky 
127435a85ac6SBen Widawsky 		kfree(parity_event[4]);
1275e3689190SBen Widawsky 		kfree(parity_event[3]);
1276e3689190SBen Widawsky 		kfree(parity_event[2]);
1277e3689190SBen Widawsky 		kfree(parity_event[1]);
1278e3689190SBen Widawsky 	}
1279e3689190SBen Widawsky 
128035a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
128135a85ac6SBen Widawsky 
128235a85ac6SBen Widawsky out:
128335a85ac6SBen Widawsky 	WARN_ON(dev_priv->l3_parity.which_slice);
12844cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
12852d1fe073SJoonas Lahtinen 	gen5_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv));
12864cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
128735a85ac6SBen Widawsky 
128891c8a326SChris Wilson 	mutex_unlock(&dev_priv->drm.struct_mutex);
128935a85ac6SBen Widawsky }
129035a85ac6SBen Widawsky 
1291261e40b8SVille Syrjälä static void ivybridge_parity_error_irq_handler(struct drm_i915_private *dev_priv,
1292261e40b8SVille Syrjälä 					       u32 iir)
1293e3689190SBen Widawsky {
1294261e40b8SVille Syrjälä 	if (!HAS_L3_DPF(dev_priv))
1295e3689190SBen Widawsky 		return;
1296e3689190SBen Widawsky 
1297d0ecd7e2SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1298261e40b8SVille Syrjälä 	gen5_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv));
1299d0ecd7e2SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
1300e3689190SBen Widawsky 
1301261e40b8SVille Syrjälä 	iir &= GT_PARITY_ERROR(dev_priv);
130235a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
130335a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 1;
130435a85ac6SBen Widawsky 
130535a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
130635a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 0;
130735a85ac6SBen Widawsky 
1308a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
1309e3689190SBen Widawsky }
1310e3689190SBen Widawsky 
1311261e40b8SVille Syrjälä static void ilk_gt_irq_handler(struct drm_i915_private *dev_priv,
1312f1af8fc1SPaulo Zanoni 			       u32 gt_iir)
1313f1af8fc1SPaulo Zanoni {
1314f8973c21SChris Wilson 	if (gt_iir & GT_RENDER_USER_INTERRUPT)
13153b3f1650SAkash Goel 		notify_ring(dev_priv->engine[RCS]);
1316f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
13173b3f1650SAkash Goel 		notify_ring(dev_priv->engine[VCS]);
1318f1af8fc1SPaulo Zanoni }
1319f1af8fc1SPaulo Zanoni 
1320261e40b8SVille Syrjälä static void snb_gt_irq_handler(struct drm_i915_private *dev_priv,
1321e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1322e7b4c6b1SDaniel Vetter {
1323f8973c21SChris Wilson 	if (gt_iir & GT_RENDER_USER_INTERRUPT)
13243b3f1650SAkash Goel 		notify_ring(dev_priv->engine[RCS]);
1325cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
13263b3f1650SAkash Goel 		notify_ring(dev_priv->engine[VCS]);
1327cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
13283b3f1650SAkash Goel 		notify_ring(dev_priv->engine[BCS]);
1329e7b4c6b1SDaniel Vetter 
1330cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1331cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
1332aaecdf61SDaniel Vetter 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT))
1333aaecdf61SDaniel Vetter 		DRM_DEBUG("Command parser error, gt_iir 0x%08x\n", gt_iir);
1334e3689190SBen Widawsky 
1335261e40b8SVille Syrjälä 	if (gt_iir & GT_PARITY_ERROR(dev_priv))
1336261e40b8SVille Syrjälä 		ivybridge_parity_error_irq_handler(dev_priv, gt_iir);
1337e7b4c6b1SDaniel Vetter }
1338e7b4c6b1SDaniel Vetter 
1339fbcc1a0cSNick Hoath static __always_inline void
13400bc40be8STvrtko Ursulin gen8_cs_irq_handler(struct intel_engine_cs *engine, u32 iir, int test_shift)
1341fbcc1a0cSNick Hoath {
1342fbcc1a0cSNick Hoath 	if (iir & (GT_RENDER_USER_INTERRUPT << test_shift))
13430bc40be8STvrtko Ursulin 		notify_ring(engine);
1344fbcc1a0cSNick Hoath 	if (iir & (GT_CONTEXT_SWITCH_INTERRUPT << test_shift))
134527af5eeaSTvrtko Ursulin 		tasklet_schedule(&engine->irq_tasklet);
1346fbcc1a0cSNick Hoath }
1347fbcc1a0cSNick Hoath 
1348e30e251aSVille Syrjälä static irqreturn_t gen8_gt_irq_ack(struct drm_i915_private *dev_priv,
1349e30e251aSVille Syrjälä 				   u32 master_ctl,
1350e30e251aSVille Syrjälä 				   u32 gt_iir[4])
1351abd58f01SBen Widawsky {
1352abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
1353abd58f01SBen Widawsky 
1354abd58f01SBen Widawsky 	if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
1355e30e251aSVille Syrjälä 		gt_iir[0] = I915_READ_FW(GEN8_GT_IIR(0));
1356e30e251aSVille Syrjälä 		if (gt_iir[0]) {
1357e30e251aSVille Syrjälä 			I915_WRITE_FW(GEN8_GT_IIR(0), gt_iir[0]);
1358abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1359abd58f01SBen Widawsky 		} else
1360abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT0)!\n");
1361abd58f01SBen Widawsky 	}
1362abd58f01SBen Widawsky 
136385f9b5f9SZhao Yakui 	if (master_ctl & (GEN8_GT_VCS1_IRQ | GEN8_GT_VCS2_IRQ)) {
1364e30e251aSVille Syrjälä 		gt_iir[1] = I915_READ_FW(GEN8_GT_IIR(1));
1365e30e251aSVille Syrjälä 		if (gt_iir[1]) {
1366e30e251aSVille Syrjälä 			I915_WRITE_FW(GEN8_GT_IIR(1), gt_iir[1]);
1367abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1368abd58f01SBen Widawsky 		} else
1369abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT1)!\n");
1370abd58f01SBen Widawsky 	}
1371abd58f01SBen Widawsky 
137274cdb337SChris Wilson 	if (master_ctl & GEN8_GT_VECS_IRQ) {
1373e30e251aSVille Syrjälä 		gt_iir[3] = I915_READ_FW(GEN8_GT_IIR(3));
1374e30e251aSVille Syrjälä 		if (gt_iir[3]) {
1375e30e251aSVille Syrjälä 			I915_WRITE_FW(GEN8_GT_IIR(3), gt_iir[3]);
137674cdb337SChris Wilson 			ret = IRQ_HANDLED;
137774cdb337SChris Wilson 		} else
137874cdb337SChris Wilson 			DRM_ERROR("The master control interrupt lied (GT3)!\n");
137974cdb337SChris Wilson 	}
138074cdb337SChris Wilson 
138126705e20SSagar Arun Kamble 	if (master_ctl & (GEN8_GT_PM_IRQ | GEN8_GT_GUC_IRQ)) {
1382e30e251aSVille Syrjälä 		gt_iir[2] = I915_READ_FW(GEN8_GT_IIR(2));
138326705e20SSagar Arun Kamble 		if (gt_iir[2] & (dev_priv->pm_rps_events |
138426705e20SSagar Arun Kamble 				 dev_priv->pm_guc_events)) {
1385cb0d205eSChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(2),
138626705e20SSagar Arun Kamble 				      gt_iir[2] & (dev_priv->pm_rps_events |
138726705e20SSagar Arun Kamble 						   dev_priv->pm_guc_events));
138838cc46d7SOscar Mateo 			ret = IRQ_HANDLED;
13890961021aSBen Widawsky 		} else
13900961021aSBen Widawsky 			DRM_ERROR("The master control interrupt lied (PM)!\n");
13910961021aSBen Widawsky 	}
13920961021aSBen Widawsky 
1393abd58f01SBen Widawsky 	return ret;
1394abd58f01SBen Widawsky }
1395abd58f01SBen Widawsky 
1396e30e251aSVille Syrjälä static void gen8_gt_irq_handler(struct drm_i915_private *dev_priv,
1397e30e251aSVille Syrjälä 				u32 gt_iir[4])
1398e30e251aSVille Syrjälä {
1399e30e251aSVille Syrjälä 	if (gt_iir[0]) {
14003b3f1650SAkash Goel 		gen8_cs_irq_handler(dev_priv->engine[RCS],
1401e30e251aSVille Syrjälä 				    gt_iir[0], GEN8_RCS_IRQ_SHIFT);
14023b3f1650SAkash Goel 		gen8_cs_irq_handler(dev_priv->engine[BCS],
1403e30e251aSVille Syrjälä 				    gt_iir[0], GEN8_BCS_IRQ_SHIFT);
1404e30e251aSVille Syrjälä 	}
1405e30e251aSVille Syrjälä 
1406e30e251aSVille Syrjälä 	if (gt_iir[1]) {
14073b3f1650SAkash Goel 		gen8_cs_irq_handler(dev_priv->engine[VCS],
1408e30e251aSVille Syrjälä 				    gt_iir[1], GEN8_VCS1_IRQ_SHIFT);
14093b3f1650SAkash Goel 		gen8_cs_irq_handler(dev_priv->engine[VCS2],
1410e30e251aSVille Syrjälä 				    gt_iir[1], GEN8_VCS2_IRQ_SHIFT);
1411e30e251aSVille Syrjälä 	}
1412e30e251aSVille Syrjälä 
1413e30e251aSVille Syrjälä 	if (gt_iir[3])
14143b3f1650SAkash Goel 		gen8_cs_irq_handler(dev_priv->engine[VECS],
1415e30e251aSVille Syrjälä 				    gt_iir[3], GEN8_VECS_IRQ_SHIFT);
1416e30e251aSVille Syrjälä 
1417e30e251aSVille Syrjälä 	if (gt_iir[2] & dev_priv->pm_rps_events)
1418e30e251aSVille Syrjälä 		gen6_rps_irq_handler(dev_priv, gt_iir[2]);
141926705e20SSagar Arun Kamble 
142026705e20SSagar Arun Kamble 	if (gt_iir[2] & dev_priv->pm_guc_events)
142126705e20SSagar Arun Kamble 		gen9_guc_irq_handler(dev_priv, gt_iir[2]);
1422e30e251aSVille Syrjälä }
1423e30e251aSVille Syrjälä 
142463c88d22SImre Deak static bool bxt_port_hotplug_long_detect(enum port port, u32 val)
142563c88d22SImre Deak {
142663c88d22SImre Deak 	switch (port) {
142763c88d22SImre Deak 	case PORT_A:
1428195baa06SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
142963c88d22SImre Deak 	case PORT_B:
143063c88d22SImre Deak 		return val & PORTB_HOTPLUG_LONG_DETECT;
143163c88d22SImre Deak 	case PORT_C:
143263c88d22SImre Deak 		return val & PORTC_HOTPLUG_LONG_DETECT;
143363c88d22SImre Deak 	default:
143463c88d22SImre Deak 		return false;
143563c88d22SImre Deak 	}
143663c88d22SImre Deak }
143763c88d22SImre Deak 
14386dbf30ceSVille Syrjälä static bool spt_port_hotplug2_long_detect(enum port port, u32 val)
14396dbf30ceSVille Syrjälä {
14406dbf30ceSVille Syrjälä 	switch (port) {
14416dbf30ceSVille Syrjälä 	case PORT_E:
14426dbf30ceSVille Syrjälä 		return val & PORTE_HOTPLUG_LONG_DETECT;
14436dbf30ceSVille Syrjälä 	default:
14446dbf30ceSVille Syrjälä 		return false;
14456dbf30ceSVille Syrjälä 	}
14466dbf30ceSVille Syrjälä }
14476dbf30ceSVille Syrjälä 
144874c0b395SVille Syrjälä static bool spt_port_hotplug_long_detect(enum port port, u32 val)
144974c0b395SVille Syrjälä {
145074c0b395SVille Syrjälä 	switch (port) {
145174c0b395SVille Syrjälä 	case PORT_A:
145274c0b395SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
145374c0b395SVille Syrjälä 	case PORT_B:
145474c0b395SVille Syrjälä 		return val & PORTB_HOTPLUG_LONG_DETECT;
145574c0b395SVille Syrjälä 	case PORT_C:
145674c0b395SVille Syrjälä 		return val & PORTC_HOTPLUG_LONG_DETECT;
145774c0b395SVille Syrjälä 	case PORT_D:
145874c0b395SVille Syrjälä 		return val & PORTD_HOTPLUG_LONG_DETECT;
145974c0b395SVille Syrjälä 	default:
146074c0b395SVille Syrjälä 		return false;
146174c0b395SVille Syrjälä 	}
146274c0b395SVille Syrjälä }
146374c0b395SVille Syrjälä 
1464e4ce95aaSVille Syrjälä static bool ilk_port_hotplug_long_detect(enum port port, u32 val)
1465e4ce95aaSVille Syrjälä {
1466e4ce95aaSVille Syrjälä 	switch (port) {
1467e4ce95aaSVille Syrjälä 	case PORT_A:
1468e4ce95aaSVille Syrjälä 		return val & DIGITAL_PORTA_HOTPLUG_LONG_DETECT;
1469e4ce95aaSVille Syrjälä 	default:
1470e4ce95aaSVille Syrjälä 		return false;
1471e4ce95aaSVille Syrjälä 	}
1472e4ce95aaSVille Syrjälä }
1473e4ce95aaSVille Syrjälä 
1474676574dfSJani Nikula static bool pch_port_hotplug_long_detect(enum port port, u32 val)
147513cf5504SDave Airlie {
147613cf5504SDave Airlie 	switch (port) {
147713cf5504SDave Airlie 	case PORT_B:
1478676574dfSJani Nikula 		return val & PORTB_HOTPLUG_LONG_DETECT;
147913cf5504SDave Airlie 	case PORT_C:
1480676574dfSJani Nikula 		return val & PORTC_HOTPLUG_LONG_DETECT;
148113cf5504SDave Airlie 	case PORT_D:
1482676574dfSJani Nikula 		return val & PORTD_HOTPLUG_LONG_DETECT;
1483676574dfSJani Nikula 	default:
1484676574dfSJani Nikula 		return false;
148513cf5504SDave Airlie 	}
148613cf5504SDave Airlie }
148713cf5504SDave Airlie 
1488676574dfSJani Nikula static bool i9xx_port_hotplug_long_detect(enum port port, u32 val)
148913cf5504SDave Airlie {
149013cf5504SDave Airlie 	switch (port) {
149113cf5504SDave Airlie 	case PORT_B:
1492676574dfSJani Nikula 		return val & PORTB_HOTPLUG_INT_LONG_PULSE;
149313cf5504SDave Airlie 	case PORT_C:
1494676574dfSJani Nikula 		return val & PORTC_HOTPLUG_INT_LONG_PULSE;
149513cf5504SDave Airlie 	case PORT_D:
1496676574dfSJani Nikula 		return val & PORTD_HOTPLUG_INT_LONG_PULSE;
1497676574dfSJani Nikula 	default:
1498676574dfSJani Nikula 		return false;
149913cf5504SDave Airlie 	}
150013cf5504SDave Airlie }
150113cf5504SDave Airlie 
150242db67d6SVille Syrjälä /*
150342db67d6SVille Syrjälä  * Get a bit mask of pins that have triggered, and which ones may be long.
150442db67d6SVille Syrjälä  * This can be called multiple times with the same masks to accumulate
150542db67d6SVille Syrjälä  * hotplug detection results from several registers.
150642db67d6SVille Syrjälä  *
150742db67d6SVille Syrjälä  * Note that the caller is expected to zero out the masks initially.
150842db67d6SVille Syrjälä  */
1509fd63e2a9SImre Deak static void intel_get_hpd_pins(u32 *pin_mask, u32 *long_mask,
15108c841e57SJani Nikula 			     u32 hotplug_trigger, u32 dig_hotplug_reg,
1511fd63e2a9SImre Deak 			     const u32 hpd[HPD_NUM_PINS],
1512fd63e2a9SImre Deak 			     bool long_pulse_detect(enum port port, u32 val))
1513676574dfSJani Nikula {
15148c841e57SJani Nikula 	enum port port;
1515676574dfSJani Nikula 	int i;
1516676574dfSJani Nikula 
1517676574dfSJani Nikula 	for_each_hpd_pin(i) {
15188c841e57SJani Nikula 		if ((hpd[i] & hotplug_trigger) == 0)
15198c841e57SJani Nikula 			continue;
15208c841e57SJani Nikula 
1521676574dfSJani Nikula 		*pin_mask |= BIT(i);
1522676574dfSJani Nikula 
1523cc24fcdcSImre Deak 		if (!intel_hpd_pin_to_port(i, &port))
1524cc24fcdcSImre Deak 			continue;
1525cc24fcdcSImre Deak 
1526fd63e2a9SImre Deak 		if (long_pulse_detect(port, dig_hotplug_reg))
1527676574dfSJani Nikula 			*long_mask |= BIT(i);
1528676574dfSJani Nikula 	}
1529676574dfSJani Nikula 
1530676574dfSJani Nikula 	DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x, dig 0x%08x, pins 0x%08x\n",
1531676574dfSJani Nikula 			 hotplug_trigger, dig_hotplug_reg, *pin_mask);
1532676574dfSJani Nikula 
1533676574dfSJani Nikula }
1534676574dfSJani Nikula 
153591d14251STvrtko Ursulin static void gmbus_irq_handler(struct drm_i915_private *dev_priv)
1536515ac2bbSDaniel Vetter {
153728c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1538515ac2bbSDaniel Vetter }
1539515ac2bbSDaniel Vetter 
154091d14251STvrtko Ursulin static void dp_aux_irq_handler(struct drm_i915_private *dev_priv)
1541ce99c256SDaniel Vetter {
15429ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1543ce99c256SDaniel Vetter }
1544ce99c256SDaniel Vetter 
15458bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
154691d14251STvrtko Ursulin static void display_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
154791d14251STvrtko Ursulin 					 enum pipe pipe,
1548eba94eb9SDaniel Vetter 					 uint32_t crc0, uint32_t crc1,
1549eba94eb9SDaniel Vetter 					 uint32_t crc2, uint32_t crc3,
15508bc5e955SDaniel Vetter 					 uint32_t crc4)
15518bf1e9f1SShuang He {
15528bf1e9f1SShuang He 	struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
15538bf1e9f1SShuang He 	struct intel_pipe_crc_entry *entry;
1554ac2300d4SDamien Lespiau 	int head, tail;
1555b2c88f5bSDamien Lespiau 
1556d538bbdfSDamien Lespiau 	spin_lock(&pipe_crc->lock);
1557d538bbdfSDamien Lespiau 
15580c912c79SDamien Lespiau 	if (!pipe_crc->entries) {
1559d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
156034273620SDaniel Vetter 		DRM_DEBUG_KMS("spurious interrupt\n");
15610c912c79SDamien Lespiau 		return;
15620c912c79SDamien Lespiau 	}
15630c912c79SDamien Lespiau 
1564d538bbdfSDamien Lespiau 	head = pipe_crc->head;
1565d538bbdfSDamien Lespiau 	tail = pipe_crc->tail;
1566b2c88f5bSDamien Lespiau 
1567b2c88f5bSDamien Lespiau 	if (CIRC_SPACE(head, tail, INTEL_PIPE_CRC_ENTRIES_NR) < 1) {
1568d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
1569b2c88f5bSDamien Lespiau 		DRM_ERROR("CRC buffer overflowing\n");
1570b2c88f5bSDamien Lespiau 		return;
1571b2c88f5bSDamien Lespiau 	}
1572b2c88f5bSDamien Lespiau 
1573b2c88f5bSDamien Lespiau 	entry = &pipe_crc->entries[head];
15748bf1e9f1SShuang He 
157591c8a326SChris Wilson 	entry->frame = dev_priv->drm.driver->get_vblank_counter(&dev_priv->drm,
157691d14251STvrtko Ursulin 								 pipe);
1577eba94eb9SDaniel Vetter 	entry->crc[0] = crc0;
1578eba94eb9SDaniel Vetter 	entry->crc[1] = crc1;
1579eba94eb9SDaniel Vetter 	entry->crc[2] = crc2;
1580eba94eb9SDaniel Vetter 	entry->crc[3] = crc3;
1581eba94eb9SDaniel Vetter 	entry->crc[4] = crc4;
1582b2c88f5bSDamien Lespiau 
1583b2c88f5bSDamien Lespiau 	head = (head + 1) & (INTEL_PIPE_CRC_ENTRIES_NR - 1);
1584d538bbdfSDamien Lespiau 	pipe_crc->head = head;
1585d538bbdfSDamien Lespiau 
1586d538bbdfSDamien Lespiau 	spin_unlock(&pipe_crc->lock);
158707144428SDamien Lespiau 
158807144428SDamien Lespiau 	wake_up_interruptible(&pipe_crc->wq);
15898bf1e9f1SShuang He }
1590277de95eSDaniel Vetter #else
1591277de95eSDaniel Vetter static inline void
159291d14251STvrtko Ursulin display_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
159391d14251STvrtko Ursulin 			     enum pipe pipe,
1594277de95eSDaniel Vetter 			     uint32_t crc0, uint32_t crc1,
1595277de95eSDaniel Vetter 			     uint32_t crc2, uint32_t crc3,
1596277de95eSDaniel Vetter 			     uint32_t crc4) {}
1597277de95eSDaniel Vetter #endif
1598eba94eb9SDaniel Vetter 
1599277de95eSDaniel Vetter 
160091d14251STvrtko Ursulin static void hsw_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
160191d14251STvrtko Ursulin 				     enum pipe pipe)
16025a69b89fSDaniel Vetter {
160391d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
16045a69b89fSDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
16055a69b89fSDaniel Vetter 				     0, 0, 0, 0);
16065a69b89fSDaniel Vetter }
16075a69b89fSDaniel Vetter 
160891d14251STvrtko Ursulin static void ivb_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
160991d14251STvrtko Ursulin 				     enum pipe pipe)
1610eba94eb9SDaniel Vetter {
161191d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
1612eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1613eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1614eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1615eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
16168bc5e955SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1617eba94eb9SDaniel Vetter }
16185b3a856bSDaniel Vetter 
161991d14251STvrtko Ursulin static void i9xx_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
162091d14251STvrtko Ursulin 				      enum pipe pipe)
16215b3a856bSDaniel Vetter {
16220b5c5ed0SDaniel Vetter 	uint32_t res1, res2;
16230b5c5ed0SDaniel Vetter 
162491d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 3)
16250b5c5ed0SDaniel Vetter 		res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
16260b5c5ed0SDaniel Vetter 	else
16270b5c5ed0SDaniel Vetter 		res1 = 0;
16280b5c5ed0SDaniel Vetter 
162991d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
16300b5c5ed0SDaniel Vetter 		res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
16310b5c5ed0SDaniel Vetter 	else
16320b5c5ed0SDaniel Vetter 		res2 = 0;
16335b3a856bSDaniel Vetter 
163491d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
16350b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_RED(pipe)),
16360b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_GREEN(pipe)),
16370b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_BLUE(pipe)),
16380b5c5ed0SDaniel Vetter 				     res1, res2);
16395b3a856bSDaniel Vetter }
16408bf1e9f1SShuang He 
16411403c0d4SPaulo Zanoni /* The RPS events need forcewake, so we add them to a work queue and mask their
16421403c0d4SPaulo Zanoni  * IMR bits until the work is done. Other interrupts can be processed without
16431403c0d4SPaulo Zanoni  * the work queue. */
16441403c0d4SPaulo Zanoni static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1645baf02a1fSBen Widawsky {
1646a6706b45SDeepak S 	if (pm_iir & dev_priv->pm_rps_events) {
164759cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
1648f4e9af4fSAkash Goel 		gen6_mask_pm_irq(dev_priv, pm_iir & dev_priv->pm_rps_events);
1649d4d70aa5SImre Deak 		if (dev_priv->rps.interrupts_enabled) {
1650d4d70aa5SImre Deak 			dev_priv->rps.pm_iir |= pm_iir & dev_priv->pm_rps_events;
1651c33d247dSChris Wilson 			schedule_work(&dev_priv->rps.work);
165241a05a3aSDaniel Vetter 		}
1653d4d70aa5SImre Deak 		spin_unlock(&dev_priv->irq_lock);
1654d4d70aa5SImre Deak 	}
1655baf02a1fSBen Widawsky 
1656c9a9a268SImre Deak 	if (INTEL_INFO(dev_priv)->gen >= 8)
1657c9a9a268SImre Deak 		return;
1658c9a9a268SImre Deak 
16592d1fe073SJoonas Lahtinen 	if (HAS_VEBOX(dev_priv)) {
166012638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
16613b3f1650SAkash Goel 			notify_ring(dev_priv->engine[VECS]);
166212638c57SBen Widawsky 
1663aaecdf61SDaniel Vetter 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT)
1664aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, pm_iir 0x%08x\n", pm_iir);
166512638c57SBen Widawsky 	}
16661403c0d4SPaulo Zanoni }
1667baf02a1fSBen Widawsky 
166826705e20SSagar Arun Kamble static void gen9_guc_irq_handler(struct drm_i915_private *dev_priv, u32 gt_iir)
166926705e20SSagar Arun Kamble {
167026705e20SSagar Arun Kamble 	if (gt_iir & GEN9_GUC_TO_HOST_INT_EVENT) {
16714100b2abSSagar Arun Kamble 		/* Sample the log buffer flush related bits & clear them out now
16724100b2abSSagar Arun Kamble 		 * itself from the message identity register to minimize the
16734100b2abSSagar Arun Kamble 		 * probability of losing a flush interrupt, when there are back
16744100b2abSSagar Arun Kamble 		 * to back flush interrupts.
16754100b2abSSagar Arun Kamble 		 * There can be a new flush interrupt, for different log buffer
16764100b2abSSagar Arun Kamble 		 * type (like for ISR), whilst Host is handling one (for DPC).
16774100b2abSSagar Arun Kamble 		 * Since same bit is used in message register for ISR & DPC, it
16784100b2abSSagar Arun Kamble 		 * could happen that GuC sets the bit for 2nd interrupt but Host
16794100b2abSSagar Arun Kamble 		 * clears out the bit on handling the 1st interrupt.
16804100b2abSSagar Arun Kamble 		 */
16814100b2abSSagar Arun Kamble 		u32 msg, flush;
16824100b2abSSagar Arun Kamble 
16834100b2abSSagar Arun Kamble 		msg = I915_READ(SOFT_SCRATCH(15));
16844100b2abSSagar Arun Kamble 		flush = msg & (GUC2HOST_MSG_CRASH_DUMP_POSTED |
16854100b2abSSagar Arun Kamble 			       GUC2HOST_MSG_FLUSH_LOG_BUFFER);
16864100b2abSSagar Arun Kamble 		if (flush) {
16874100b2abSSagar Arun Kamble 			/* Clear the message bits that are handled */
16884100b2abSSagar Arun Kamble 			I915_WRITE(SOFT_SCRATCH(15), msg & ~flush);
16894100b2abSSagar Arun Kamble 
16904100b2abSSagar Arun Kamble 			/* Handle flush interrupt in bottom half */
16914100b2abSSagar Arun Kamble 			queue_work(dev_priv->guc.log.flush_wq,
16924100b2abSSagar Arun Kamble 				   &dev_priv->guc.log.flush_work);
16935aa1ee4bSAkash Goel 
16945aa1ee4bSAkash Goel 			dev_priv->guc.log.flush_interrupt_count++;
16954100b2abSSagar Arun Kamble 		} else {
16964100b2abSSagar Arun Kamble 			/* Not clearing of unhandled event bits won't result in
16974100b2abSSagar Arun Kamble 			 * re-triggering of the interrupt.
16984100b2abSSagar Arun Kamble 			 */
16994100b2abSSagar Arun Kamble 		}
170026705e20SSagar Arun Kamble 	}
170126705e20SSagar Arun Kamble }
170226705e20SSagar Arun Kamble 
17035a21b665SDaniel Vetter static bool intel_pipe_handle_vblank(struct drm_i915_private *dev_priv,
170491d14251STvrtko Ursulin 				     enum pipe pipe)
17058d7849dbSVille Syrjälä {
17065a21b665SDaniel Vetter 	bool ret;
17075a21b665SDaniel Vetter 
170891c8a326SChris Wilson 	ret = drm_handle_vblank(&dev_priv->drm, pipe);
17095a21b665SDaniel Vetter 	if (ret)
171051cbaf01SMaarten Lankhorst 		intel_finish_page_flip_mmio(dev_priv, pipe);
17115a21b665SDaniel Vetter 
17125a21b665SDaniel Vetter 	return ret;
17138d7849dbSVille Syrjälä }
17148d7849dbSVille Syrjälä 
171591d14251STvrtko Ursulin static void valleyview_pipestat_irq_ack(struct drm_i915_private *dev_priv,
171691d14251STvrtko Ursulin 					u32 iir, u32 pipe_stats[I915_MAX_PIPES])
17177e231dbeSJesse Barnes {
17187e231dbeSJesse Barnes 	int pipe;
17197e231dbeSJesse Barnes 
172058ead0d7SImre Deak 	spin_lock(&dev_priv->irq_lock);
17211ca993d2SVille Syrjälä 
17221ca993d2SVille Syrjälä 	if (!dev_priv->display_irqs_enabled) {
17231ca993d2SVille Syrjälä 		spin_unlock(&dev_priv->irq_lock);
17241ca993d2SVille Syrjälä 		return;
17251ca993d2SVille Syrjälä 	}
17261ca993d2SVille Syrjälä 
1727055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1728f0f59a00SVille Syrjälä 		i915_reg_t reg;
1729bbb5eebfSDaniel Vetter 		u32 mask, iir_bit = 0;
173091d181ddSImre Deak 
1731bbb5eebfSDaniel Vetter 		/*
1732bbb5eebfSDaniel Vetter 		 * PIPESTAT bits get signalled even when the interrupt is
1733bbb5eebfSDaniel Vetter 		 * disabled with the mask bits, and some of the status bits do
1734bbb5eebfSDaniel Vetter 		 * not generate interrupts at all (like the underrun bit). Hence
1735bbb5eebfSDaniel Vetter 		 * we need to be careful that we only handle what we want to
1736bbb5eebfSDaniel Vetter 		 * handle.
1737bbb5eebfSDaniel Vetter 		 */
17380f239f4cSDaniel Vetter 
17390f239f4cSDaniel Vetter 		/* fifo underruns are filterered in the underrun handler. */
17400f239f4cSDaniel Vetter 		mask = PIPE_FIFO_UNDERRUN_STATUS;
1741bbb5eebfSDaniel Vetter 
1742bbb5eebfSDaniel Vetter 		switch (pipe) {
1743bbb5eebfSDaniel Vetter 		case PIPE_A:
1744bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
1745bbb5eebfSDaniel Vetter 			break;
1746bbb5eebfSDaniel Vetter 		case PIPE_B:
1747bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
1748bbb5eebfSDaniel Vetter 			break;
17493278f67fSVille Syrjälä 		case PIPE_C:
17503278f67fSVille Syrjälä 			iir_bit = I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
17513278f67fSVille Syrjälä 			break;
1752bbb5eebfSDaniel Vetter 		}
1753bbb5eebfSDaniel Vetter 		if (iir & iir_bit)
1754bbb5eebfSDaniel Vetter 			mask |= dev_priv->pipestat_irq_mask[pipe];
1755bbb5eebfSDaniel Vetter 
1756bbb5eebfSDaniel Vetter 		if (!mask)
175791d181ddSImre Deak 			continue;
175891d181ddSImre Deak 
175991d181ddSImre Deak 		reg = PIPESTAT(pipe);
1760bbb5eebfSDaniel Vetter 		mask |= PIPESTAT_INT_ENABLE_MASK;
1761bbb5eebfSDaniel Vetter 		pipe_stats[pipe] = I915_READ(reg) & mask;
17627e231dbeSJesse Barnes 
17637e231dbeSJesse Barnes 		/*
17647e231dbeSJesse Barnes 		 * Clear the PIPE*STAT regs before the IIR
17657e231dbeSJesse Barnes 		 */
176691d181ddSImre Deak 		if (pipe_stats[pipe] & (PIPE_FIFO_UNDERRUN_STATUS |
176791d181ddSImre Deak 					PIPESTAT_INT_STATUS_MASK))
17687e231dbeSJesse Barnes 			I915_WRITE(reg, pipe_stats[pipe]);
17697e231dbeSJesse Barnes 	}
177058ead0d7SImre Deak 	spin_unlock(&dev_priv->irq_lock);
17712ecb8ca4SVille Syrjälä }
17722ecb8ca4SVille Syrjälä 
177391d14251STvrtko Ursulin static void valleyview_pipestat_irq_handler(struct drm_i915_private *dev_priv,
17742ecb8ca4SVille Syrjälä 					    u32 pipe_stats[I915_MAX_PIPES])
17752ecb8ca4SVille Syrjälä {
17762ecb8ca4SVille Syrjälä 	enum pipe pipe;
17777e231dbeSJesse Barnes 
1778055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
17795a21b665SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
17805a21b665SDaniel Vetter 		    intel_pipe_handle_vblank(dev_priv, pipe))
17815a21b665SDaniel Vetter 			intel_check_page_flip(dev_priv, pipe);
178231acc7f5SJesse Barnes 
17835251f04eSMaarten Lankhorst 		if (pipe_stats[pipe] & PLANE_FLIP_DONE_INT_STATUS_VLV)
178451cbaf01SMaarten Lankhorst 			intel_finish_page_flip_cs(dev_priv, pipe);
17854356d586SDaniel Vetter 
17864356d586SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
178791d14251STvrtko Ursulin 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
17882d9d2b0bSVille Syrjälä 
17891f7247c0SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
17901f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
179131acc7f5SJesse Barnes 	}
179231acc7f5SJesse Barnes 
1793c1874ed7SImre Deak 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
179491d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
1795c1874ed7SImre Deak }
1796c1874ed7SImre Deak 
17971ae3c34cSVille Syrjälä static u32 i9xx_hpd_irq_ack(struct drm_i915_private *dev_priv)
179816c6c56bSVille Syrjälä {
179916c6c56bSVille Syrjälä 	u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
180016c6c56bSVille Syrjälä 
18011ae3c34cSVille Syrjälä 	if (hotplug_status)
18023ff60f89SOscar Mateo 		I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
18031ae3c34cSVille Syrjälä 
18041ae3c34cSVille Syrjälä 	return hotplug_status;
18051ae3c34cSVille Syrjälä }
18061ae3c34cSVille Syrjälä 
180791d14251STvrtko Ursulin static void i9xx_hpd_irq_handler(struct drm_i915_private *dev_priv,
18081ae3c34cSVille Syrjälä 				 u32 hotplug_status)
18091ae3c34cSVille Syrjälä {
18101ae3c34cSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
18113ff60f89SOscar Mateo 
181291d14251STvrtko Ursulin 	if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
181391d14251STvrtko Ursulin 	    IS_CHERRYVIEW(dev_priv)) {
181416c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
181516c6c56bSVille Syrjälä 
181658f2cf24SVille Syrjälä 		if (hotplug_trigger) {
1817fd63e2a9SImre Deak 			intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
1818fd63e2a9SImre Deak 					   hotplug_trigger, hpd_status_g4x,
1819fd63e2a9SImre Deak 					   i9xx_port_hotplug_long_detect);
182058f2cf24SVille Syrjälä 
182191d14251STvrtko Ursulin 			intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
182258f2cf24SVille Syrjälä 		}
1823369712e8SJani Nikula 
1824369712e8SJani Nikula 		if (hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
182591d14251STvrtko Ursulin 			dp_aux_irq_handler(dev_priv);
182616c6c56bSVille Syrjälä 	} else {
182716c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
182816c6c56bSVille Syrjälä 
182958f2cf24SVille Syrjälä 		if (hotplug_trigger) {
1830fd63e2a9SImre Deak 			intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
18314e3d1e26SVille Syrjälä 					   hotplug_trigger, hpd_status_i915,
1832fd63e2a9SImre Deak 					   i9xx_port_hotplug_long_detect);
183391d14251STvrtko Ursulin 			intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
183416c6c56bSVille Syrjälä 		}
18353ff60f89SOscar Mateo 	}
183658f2cf24SVille Syrjälä }
183716c6c56bSVille Syrjälä 
1838c1874ed7SImre Deak static irqreturn_t valleyview_irq_handler(int irq, void *arg)
1839c1874ed7SImre Deak {
184045a83f84SDaniel Vetter 	struct drm_device *dev = arg;
1841fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
1842c1874ed7SImre Deak 	irqreturn_t ret = IRQ_NONE;
1843c1874ed7SImre Deak 
18442dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
18452dd2a883SImre Deak 		return IRQ_NONE;
18462dd2a883SImre Deak 
18471f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
18481f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
18491f814dacSImre Deak 
18501e1cace9SVille Syrjälä 	do {
18516e814800SVille Syrjälä 		u32 iir, gt_iir, pm_iir;
18522ecb8ca4SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
18531ae3c34cSVille Syrjälä 		u32 hotplug_status = 0;
1854a5e485a9SVille Syrjälä 		u32 ier = 0;
18553ff60f89SOscar Mateo 
1856c1874ed7SImre Deak 		gt_iir = I915_READ(GTIIR);
1857c1874ed7SImre Deak 		pm_iir = I915_READ(GEN6_PMIIR);
18583ff60f89SOscar Mateo 		iir = I915_READ(VLV_IIR);
1859c1874ed7SImre Deak 
1860c1874ed7SImre Deak 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
18611e1cace9SVille Syrjälä 			break;
1862c1874ed7SImre Deak 
1863c1874ed7SImre Deak 		ret = IRQ_HANDLED;
1864c1874ed7SImre Deak 
1865a5e485a9SVille Syrjälä 		/*
1866a5e485a9SVille Syrjälä 		 * Theory on interrupt generation, based on empirical evidence:
1867a5e485a9SVille Syrjälä 		 *
1868a5e485a9SVille Syrjälä 		 * x = ((VLV_IIR & VLV_IER) ||
1869a5e485a9SVille Syrjälä 		 *      (((GT_IIR & GT_IER) || (GEN6_PMIIR & GEN6_PMIER)) &&
1870a5e485a9SVille Syrjälä 		 *       (VLV_MASTER_IER & MASTER_INTERRUPT_ENABLE)));
1871a5e485a9SVille Syrjälä 		 *
1872a5e485a9SVille Syrjälä 		 * A CPU interrupt will only be raised when 'x' has a 0->1 edge.
1873a5e485a9SVille Syrjälä 		 * Hence we clear MASTER_INTERRUPT_ENABLE and VLV_IER to
1874a5e485a9SVille Syrjälä 		 * guarantee the CPU interrupt will be raised again even if we
1875a5e485a9SVille Syrjälä 		 * don't end up clearing all the VLV_IIR, GT_IIR, GEN6_PMIIR
1876a5e485a9SVille Syrjälä 		 * bits this time around.
1877a5e485a9SVille Syrjälä 		 */
18784a0a0202SVille Syrjälä 		I915_WRITE(VLV_MASTER_IER, 0);
1879a5e485a9SVille Syrjälä 		ier = I915_READ(VLV_IER);
1880a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, 0);
18814a0a0202SVille Syrjälä 
18824a0a0202SVille Syrjälä 		if (gt_iir)
18834a0a0202SVille Syrjälä 			I915_WRITE(GTIIR, gt_iir);
18844a0a0202SVille Syrjälä 		if (pm_iir)
18854a0a0202SVille Syrjälä 			I915_WRITE(GEN6_PMIIR, pm_iir);
18864a0a0202SVille Syrjälä 
18877ce4d1f2SVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
18881ae3c34cSVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
18897ce4d1f2SVille Syrjälä 
18903ff60f89SOscar Mateo 		/* Call regardless, as some status bits might not be
18913ff60f89SOscar Mateo 		 * signalled in iir */
189291d14251STvrtko Ursulin 		valleyview_pipestat_irq_ack(dev_priv, iir, pipe_stats);
18937ce4d1f2SVille Syrjälä 
18947ce4d1f2SVille Syrjälä 		/*
18957ce4d1f2SVille Syrjälä 		 * VLV_IIR is single buffered, and reflects the level
18967ce4d1f2SVille Syrjälä 		 * from PIPESTAT/PORT_HOTPLUG_STAT, hence clear it last.
18977ce4d1f2SVille Syrjälä 		 */
18987ce4d1f2SVille Syrjälä 		if (iir)
18997ce4d1f2SVille Syrjälä 			I915_WRITE(VLV_IIR, iir);
19004a0a0202SVille Syrjälä 
1901a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, ier);
19024a0a0202SVille Syrjälä 		I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
19034a0a0202SVille Syrjälä 		POSTING_READ(VLV_MASTER_IER);
19041ae3c34cSVille Syrjälä 
190552894874SVille Syrjälä 		if (gt_iir)
1906261e40b8SVille Syrjälä 			snb_gt_irq_handler(dev_priv, gt_iir);
190752894874SVille Syrjälä 		if (pm_iir)
190852894874SVille Syrjälä 			gen6_rps_irq_handler(dev_priv, pm_iir);
190952894874SVille Syrjälä 
19101ae3c34cSVille Syrjälä 		if (hotplug_status)
191191d14251STvrtko Ursulin 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
19122ecb8ca4SVille Syrjälä 
191391d14251STvrtko Ursulin 		valleyview_pipestat_irq_handler(dev_priv, pipe_stats);
19141e1cace9SVille Syrjälä 	} while (0);
19157e231dbeSJesse Barnes 
19161f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
19171f814dacSImre Deak 
19187e231dbeSJesse Barnes 	return ret;
19197e231dbeSJesse Barnes }
19207e231dbeSJesse Barnes 
192143f328d7SVille Syrjälä static irqreturn_t cherryview_irq_handler(int irq, void *arg)
192243f328d7SVille Syrjälä {
192345a83f84SDaniel Vetter 	struct drm_device *dev = arg;
1924fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
192543f328d7SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
192643f328d7SVille Syrjälä 
19272dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
19282dd2a883SImre Deak 		return IRQ_NONE;
19292dd2a883SImre Deak 
19301f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
19311f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
19321f814dacSImre Deak 
1933579de73bSChris Wilson 	do {
19346e814800SVille Syrjälä 		u32 master_ctl, iir;
1935e30e251aSVille Syrjälä 		u32 gt_iir[4] = {};
19362ecb8ca4SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
19371ae3c34cSVille Syrjälä 		u32 hotplug_status = 0;
1938a5e485a9SVille Syrjälä 		u32 ier = 0;
1939a5e485a9SVille Syrjälä 
19408e5fd599SVille Syrjälä 		master_ctl = I915_READ(GEN8_MASTER_IRQ) & ~GEN8_MASTER_IRQ_CONTROL;
19413278f67fSVille Syrjälä 		iir = I915_READ(VLV_IIR);
19423278f67fSVille Syrjälä 
19433278f67fSVille Syrjälä 		if (master_ctl == 0 && iir == 0)
19448e5fd599SVille Syrjälä 			break;
194543f328d7SVille Syrjälä 
194627b6c122SOscar Mateo 		ret = IRQ_HANDLED;
194727b6c122SOscar Mateo 
1948a5e485a9SVille Syrjälä 		/*
1949a5e485a9SVille Syrjälä 		 * Theory on interrupt generation, based on empirical evidence:
1950a5e485a9SVille Syrjälä 		 *
1951a5e485a9SVille Syrjälä 		 * x = ((VLV_IIR & VLV_IER) ||
1952a5e485a9SVille Syrjälä 		 *      ((GEN8_MASTER_IRQ & ~GEN8_MASTER_IRQ_CONTROL) &&
1953a5e485a9SVille Syrjälä 		 *       (GEN8_MASTER_IRQ & GEN8_MASTER_IRQ_CONTROL)));
1954a5e485a9SVille Syrjälä 		 *
1955a5e485a9SVille Syrjälä 		 * A CPU interrupt will only be raised when 'x' has a 0->1 edge.
1956a5e485a9SVille Syrjälä 		 * Hence we clear GEN8_MASTER_IRQ_CONTROL and VLV_IER to
1957a5e485a9SVille Syrjälä 		 * guarantee the CPU interrupt will be raised again even if we
1958a5e485a9SVille Syrjälä 		 * don't end up clearing all the VLV_IIR and GEN8_MASTER_IRQ_CONTROL
1959a5e485a9SVille Syrjälä 		 * bits this time around.
1960a5e485a9SVille Syrjälä 		 */
196143f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, 0);
1962a5e485a9SVille Syrjälä 		ier = I915_READ(VLV_IER);
1963a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, 0);
196443f328d7SVille Syrjälä 
1965e30e251aSVille Syrjälä 		gen8_gt_irq_ack(dev_priv, master_ctl, gt_iir);
196627b6c122SOscar Mateo 
196727b6c122SOscar Mateo 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
19681ae3c34cSVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
196943f328d7SVille Syrjälä 
197027b6c122SOscar Mateo 		/* Call regardless, as some status bits might not be
197127b6c122SOscar Mateo 		 * signalled in iir */
197291d14251STvrtko Ursulin 		valleyview_pipestat_irq_ack(dev_priv, iir, pipe_stats);
197343f328d7SVille Syrjälä 
19747ce4d1f2SVille Syrjälä 		/*
19757ce4d1f2SVille Syrjälä 		 * VLV_IIR is single buffered, and reflects the level
19767ce4d1f2SVille Syrjälä 		 * from PIPESTAT/PORT_HOTPLUG_STAT, hence clear it last.
19777ce4d1f2SVille Syrjälä 		 */
19787ce4d1f2SVille Syrjälä 		if (iir)
19797ce4d1f2SVille Syrjälä 			I915_WRITE(VLV_IIR, iir);
19807ce4d1f2SVille Syrjälä 
1981a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, ier);
1982e5328c43SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
198343f328d7SVille Syrjälä 		POSTING_READ(GEN8_MASTER_IRQ);
19841ae3c34cSVille Syrjälä 
1985e30e251aSVille Syrjälä 		gen8_gt_irq_handler(dev_priv, gt_iir);
1986e30e251aSVille Syrjälä 
19871ae3c34cSVille Syrjälä 		if (hotplug_status)
198891d14251STvrtko Ursulin 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
19892ecb8ca4SVille Syrjälä 
199091d14251STvrtko Ursulin 		valleyview_pipestat_irq_handler(dev_priv, pipe_stats);
1991579de73bSChris Wilson 	} while (0);
19923278f67fSVille Syrjälä 
19931f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
19941f814dacSImre Deak 
199543f328d7SVille Syrjälä 	return ret;
199643f328d7SVille Syrjälä }
199743f328d7SVille Syrjälä 
199891d14251STvrtko Ursulin static void ibx_hpd_irq_handler(struct drm_i915_private *dev_priv,
199991d14251STvrtko Ursulin 				u32 hotplug_trigger,
200040e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2001776ad806SJesse Barnes {
200242db67d6SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2003776ad806SJesse Barnes 
20046a39d7c9SJani Nikula 	/*
20056a39d7c9SJani Nikula 	 * Somehow the PCH doesn't seem to really ack the interrupt to the CPU
20066a39d7c9SJani Nikula 	 * unless we touch the hotplug register, even if hotplug_trigger is
20076a39d7c9SJani Nikula 	 * zero. Not acking leads to "The master control interrupt lied (SDE)!"
20086a39d7c9SJani Nikula 	 * errors.
20096a39d7c9SJani Nikula 	 */
201013cf5504SDave Airlie 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
20116a39d7c9SJani Nikula 	if (!hotplug_trigger) {
20126a39d7c9SJani Nikula 		u32 mask = PORTA_HOTPLUG_STATUS_MASK |
20136a39d7c9SJani Nikula 			PORTD_HOTPLUG_STATUS_MASK |
20146a39d7c9SJani Nikula 			PORTC_HOTPLUG_STATUS_MASK |
20156a39d7c9SJani Nikula 			PORTB_HOTPLUG_STATUS_MASK;
20166a39d7c9SJani Nikula 		dig_hotplug_reg &= ~mask;
20176a39d7c9SJani Nikula 	}
20186a39d7c9SJani Nikula 
201913cf5504SDave Airlie 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
20206a39d7c9SJani Nikula 	if (!hotplug_trigger)
20216a39d7c9SJani Nikula 		return;
202213cf5504SDave Airlie 
2023fd63e2a9SImre Deak 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
202440e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2025fd63e2a9SImre Deak 			   pch_port_hotplug_long_detect);
202640e56410SVille Syrjälä 
202791d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2028aaf5ec2eSSonika Jindal }
202991d131d2SDaniel Vetter 
203091d14251STvrtko Ursulin static void ibx_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
203140e56410SVille Syrjälä {
203240e56410SVille Syrjälä 	int pipe;
203340e56410SVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
203440e56410SVille Syrjälä 
203591d14251STvrtko Ursulin 	ibx_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ibx);
203640e56410SVille Syrjälä 
2037cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
2038cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
2039776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
2040cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
2041cfc33bf7SVille Syrjälä 				 port_name(port));
2042cfc33bf7SVille Syrjälä 	}
2043776ad806SJesse Barnes 
2044ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
204591d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
2046ce99c256SDaniel Vetter 
2047776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
204891d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
2049776ad806SJesse Barnes 
2050776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
2051776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
2052776ad806SJesse Barnes 
2053776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
2054776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
2055776ad806SJesse Barnes 
2056776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
2057776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
2058776ad806SJesse Barnes 
20599db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
2060055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
20619db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
20629db4a9c7SJesse Barnes 					 pipe_name(pipe),
20639db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
2064776ad806SJesse Barnes 
2065776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
2066776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
2067776ad806SJesse Barnes 
2068776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
2069776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
2070776ad806SJesse Barnes 
2071776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
20721f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
20738664281bSPaulo Zanoni 
20748664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
20751f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
20768664281bSPaulo Zanoni }
20778664281bSPaulo Zanoni 
207891d14251STvrtko Ursulin static void ivb_err_int_handler(struct drm_i915_private *dev_priv)
20798664281bSPaulo Zanoni {
20808664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
20815a69b89fSDaniel Vetter 	enum pipe pipe;
20828664281bSPaulo Zanoni 
2083de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
2084de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
2085de032bf4SPaulo Zanoni 
2086055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
20871f7247c0SDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe))
20881f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
20898664281bSPaulo Zanoni 
20905a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
209191d14251STvrtko Ursulin 			if (IS_IVYBRIDGE(dev_priv))
209291d14251STvrtko Ursulin 				ivb_pipe_crc_irq_handler(dev_priv, pipe);
20935a69b89fSDaniel Vetter 			else
209491d14251STvrtko Ursulin 				hsw_pipe_crc_irq_handler(dev_priv, pipe);
20955a69b89fSDaniel Vetter 		}
20965a69b89fSDaniel Vetter 	}
20978bf1e9f1SShuang He 
20988664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
20998664281bSPaulo Zanoni }
21008664281bSPaulo Zanoni 
210191d14251STvrtko Ursulin static void cpt_serr_int_handler(struct drm_i915_private *dev_priv)
21028664281bSPaulo Zanoni {
21038664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
21048664281bSPaulo Zanoni 
2105de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
2106de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
2107de032bf4SPaulo Zanoni 
21088664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
21091f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
21108664281bSPaulo Zanoni 
21118664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
21121f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
21138664281bSPaulo Zanoni 
21148664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
21151f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_C);
21168664281bSPaulo Zanoni 
21178664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
2118776ad806SJesse Barnes }
2119776ad806SJesse Barnes 
212091d14251STvrtko Ursulin static void cpt_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
212123e81d69SAdam Jackson {
212223e81d69SAdam Jackson 	int pipe;
21236dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
2124aaf5ec2eSSonika Jindal 
212591d14251STvrtko Ursulin 	ibx_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_cpt);
212691d131d2SDaniel Vetter 
2127cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
2128cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
212923e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
2130cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
2131cfc33bf7SVille Syrjälä 				 port_name(port));
2132cfc33bf7SVille Syrjälä 	}
213323e81d69SAdam Jackson 
213423e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
213591d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
213623e81d69SAdam Jackson 
213723e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
213891d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
213923e81d69SAdam Jackson 
214023e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
214123e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
214223e81d69SAdam Jackson 
214323e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
214423e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
214523e81d69SAdam Jackson 
214623e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
2147055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
214823e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
214923e81d69SAdam Jackson 					 pipe_name(pipe),
215023e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
21518664281bSPaulo Zanoni 
21528664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
215391d14251STvrtko Ursulin 		cpt_serr_int_handler(dev_priv);
215423e81d69SAdam Jackson }
215523e81d69SAdam Jackson 
215691d14251STvrtko Ursulin static void spt_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
21576dbf30ceSVille Syrjälä {
21586dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_SPT &
21596dbf30ceSVille Syrjälä 		~SDE_PORTE_HOTPLUG_SPT;
21606dbf30ceSVille Syrjälä 	u32 hotplug2_trigger = pch_iir & SDE_PORTE_HOTPLUG_SPT;
21616dbf30ceSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
21626dbf30ceSVille Syrjälä 
21636dbf30ceSVille Syrjälä 	if (hotplug_trigger) {
21646dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
21656dbf30ceSVille Syrjälä 
21666dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
21676dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
21686dbf30ceSVille Syrjälä 
21696dbf30ceSVille Syrjälä 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
21706dbf30ceSVille Syrjälä 				   dig_hotplug_reg, hpd_spt,
217174c0b395SVille Syrjälä 				   spt_port_hotplug_long_detect);
21726dbf30ceSVille Syrjälä 	}
21736dbf30ceSVille Syrjälä 
21746dbf30ceSVille Syrjälä 	if (hotplug2_trigger) {
21756dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
21766dbf30ceSVille Syrjälä 
21776dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG2);
21786dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG2, dig_hotplug_reg);
21796dbf30ceSVille Syrjälä 
21806dbf30ceSVille Syrjälä 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug2_trigger,
21816dbf30ceSVille Syrjälä 				   dig_hotplug_reg, hpd_spt,
21826dbf30ceSVille Syrjälä 				   spt_port_hotplug2_long_detect);
21836dbf30ceSVille Syrjälä 	}
21846dbf30ceSVille Syrjälä 
21856dbf30ceSVille Syrjälä 	if (pin_mask)
218691d14251STvrtko Ursulin 		intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
21876dbf30ceSVille Syrjälä 
21886dbf30ceSVille Syrjälä 	if (pch_iir & SDE_GMBUS_CPT)
218991d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
21906dbf30ceSVille Syrjälä }
21916dbf30ceSVille Syrjälä 
219291d14251STvrtko Ursulin static void ilk_hpd_irq_handler(struct drm_i915_private *dev_priv,
219391d14251STvrtko Ursulin 				u32 hotplug_trigger,
219440e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2195c008bc6eSPaulo Zanoni {
2196e4ce95aaSVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2197e4ce95aaSVille Syrjälä 
2198e4ce95aaSVille Syrjälä 	dig_hotplug_reg = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
2199e4ce95aaSVille Syrjälä 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, dig_hotplug_reg);
2200e4ce95aaSVille Syrjälä 
2201e4ce95aaSVille Syrjälä 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
220240e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2203e4ce95aaSVille Syrjälä 			   ilk_port_hotplug_long_detect);
220440e56410SVille Syrjälä 
220591d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2206e4ce95aaSVille Syrjälä }
2207c008bc6eSPaulo Zanoni 
220891d14251STvrtko Ursulin static void ilk_display_irq_handler(struct drm_i915_private *dev_priv,
220991d14251STvrtko Ursulin 				    u32 de_iir)
221040e56410SVille Syrjälä {
221140e56410SVille Syrjälä 	enum pipe pipe;
221240e56410SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG;
221340e56410SVille Syrjälä 
221440e56410SVille Syrjälä 	if (hotplug_trigger)
221591d14251STvrtko Ursulin 		ilk_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ilk);
221640e56410SVille Syrjälä 
2217c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
221891d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
2219c008bc6eSPaulo Zanoni 
2220c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
222191d14251STvrtko Ursulin 		intel_opregion_asle_intr(dev_priv);
2222c008bc6eSPaulo Zanoni 
2223c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
2224c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
2225c008bc6eSPaulo Zanoni 
2226055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
22275a21b665SDaniel Vetter 		if (de_iir & DE_PIPE_VBLANK(pipe) &&
22285a21b665SDaniel Vetter 		    intel_pipe_handle_vblank(dev_priv, pipe))
22295a21b665SDaniel Vetter 			intel_check_page_flip(dev_priv, pipe);
2230c008bc6eSPaulo Zanoni 
223140da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
22321f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
2233c008bc6eSPaulo Zanoni 
223440da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
223591d14251STvrtko Ursulin 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
22365b3a856bSDaniel Vetter 
223740da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
22385251f04eSMaarten Lankhorst 		if (de_iir & DE_PLANE_FLIP_DONE(pipe))
223951cbaf01SMaarten Lankhorst 			intel_finish_page_flip_cs(dev_priv, pipe);
2240c008bc6eSPaulo Zanoni 	}
2241c008bc6eSPaulo Zanoni 
2242c008bc6eSPaulo Zanoni 	/* check event from PCH */
2243c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
2244c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
2245c008bc6eSPaulo Zanoni 
224691d14251STvrtko Ursulin 		if (HAS_PCH_CPT(dev_priv))
224791d14251STvrtko Ursulin 			cpt_irq_handler(dev_priv, pch_iir);
2248c008bc6eSPaulo Zanoni 		else
224991d14251STvrtko Ursulin 			ibx_irq_handler(dev_priv, pch_iir);
2250c008bc6eSPaulo Zanoni 
2251c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
2252c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
2253c008bc6eSPaulo Zanoni 	}
2254c008bc6eSPaulo Zanoni 
225591d14251STvrtko Ursulin 	if (IS_GEN5(dev_priv) && de_iir & DE_PCU_EVENT)
225691d14251STvrtko Ursulin 		ironlake_rps_change_irq_handler(dev_priv);
2257c008bc6eSPaulo Zanoni }
2258c008bc6eSPaulo Zanoni 
225991d14251STvrtko Ursulin static void ivb_display_irq_handler(struct drm_i915_private *dev_priv,
226091d14251STvrtko Ursulin 				    u32 de_iir)
22619719fb98SPaulo Zanoni {
226207d27e20SDamien Lespiau 	enum pipe pipe;
226323bb4cb5SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG_IVB;
226423bb4cb5SVille Syrjälä 
226540e56410SVille Syrjälä 	if (hotplug_trigger)
226691d14251STvrtko Ursulin 		ilk_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ivb);
22679719fb98SPaulo Zanoni 
22689719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
226991d14251STvrtko Ursulin 		ivb_err_int_handler(dev_priv);
22709719fb98SPaulo Zanoni 
22719719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
227291d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
22739719fb98SPaulo Zanoni 
22749719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
227591d14251STvrtko Ursulin 		intel_opregion_asle_intr(dev_priv);
22769719fb98SPaulo Zanoni 
2277055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
22785a21b665SDaniel Vetter 		if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)) &&
22795a21b665SDaniel Vetter 		    intel_pipe_handle_vblank(dev_priv, pipe))
22805a21b665SDaniel Vetter 			intel_check_page_flip(dev_priv, pipe);
228140da17c2SDaniel Vetter 
228240da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
22835251f04eSMaarten Lankhorst 		if (de_iir & DE_PLANE_FLIP_DONE_IVB(pipe))
228451cbaf01SMaarten Lankhorst 			intel_finish_page_flip_cs(dev_priv, pipe);
22859719fb98SPaulo Zanoni 	}
22869719fb98SPaulo Zanoni 
22879719fb98SPaulo Zanoni 	/* check event from PCH */
228891d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv) && (de_iir & DE_PCH_EVENT_IVB)) {
22899719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
22909719fb98SPaulo Zanoni 
229191d14251STvrtko Ursulin 		cpt_irq_handler(dev_priv, pch_iir);
22929719fb98SPaulo Zanoni 
22939719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
22949719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
22959719fb98SPaulo Zanoni 	}
22969719fb98SPaulo Zanoni }
22979719fb98SPaulo Zanoni 
229872c90f62SOscar Mateo /*
229972c90f62SOscar Mateo  * To handle irqs with the minimum potential races with fresh interrupts, we:
230072c90f62SOscar Mateo  * 1 - Disable Master Interrupt Control.
230172c90f62SOscar Mateo  * 2 - Find the source(s) of the interrupt.
230272c90f62SOscar Mateo  * 3 - Clear the Interrupt Identity bits (IIR).
230372c90f62SOscar Mateo  * 4 - Process the interrupt(s) that had bits set in the IIRs.
230472c90f62SOscar Mateo  * 5 - Re-enable Master Interrupt Control.
230572c90f62SOscar Mateo  */
2306f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
2307b1f14ad0SJesse Barnes {
230845a83f84SDaniel Vetter 	struct drm_device *dev = arg;
2309fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2310f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
23110e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
2312b1f14ad0SJesse Barnes 
23132dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
23142dd2a883SImre Deak 		return IRQ_NONE;
23152dd2a883SImre Deak 
23161f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
23171f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
23181f814dacSImre Deak 
2319b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
2320b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
2321b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
232223a78516SPaulo Zanoni 	POSTING_READ(DEIER);
23230e43406bSChris Wilson 
232444498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
232544498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
232644498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
232744498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
232844498aeaSPaulo Zanoni 	 * due to its back queue). */
232991d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv)) {
233044498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
233144498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
233244498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2333ab5c608bSBen Widawsky 	}
233444498aeaSPaulo Zanoni 
233572c90f62SOscar Mateo 	/* Find, clear, then process each source of interrupt */
233672c90f62SOscar Mateo 
23370e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
23380e43406bSChris Wilson 	if (gt_iir) {
233972c90f62SOscar Mateo 		I915_WRITE(GTIIR, gt_iir);
234072c90f62SOscar Mateo 		ret = IRQ_HANDLED;
234191d14251STvrtko Ursulin 		if (INTEL_GEN(dev_priv) >= 6)
2342261e40b8SVille Syrjälä 			snb_gt_irq_handler(dev_priv, gt_iir);
2343d8fc8a47SPaulo Zanoni 		else
2344261e40b8SVille Syrjälä 			ilk_gt_irq_handler(dev_priv, gt_iir);
23450e43406bSChris Wilson 	}
2346b1f14ad0SJesse Barnes 
2347b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
23480e43406bSChris Wilson 	if (de_iir) {
234972c90f62SOscar Mateo 		I915_WRITE(DEIIR, de_iir);
235072c90f62SOscar Mateo 		ret = IRQ_HANDLED;
235191d14251STvrtko Ursulin 		if (INTEL_GEN(dev_priv) >= 7)
235291d14251STvrtko Ursulin 			ivb_display_irq_handler(dev_priv, de_iir);
2353f1af8fc1SPaulo Zanoni 		else
235491d14251STvrtko Ursulin 			ilk_display_irq_handler(dev_priv, de_iir);
23550e43406bSChris Wilson 	}
23560e43406bSChris Wilson 
235791d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 6) {
2358f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
23590e43406bSChris Wilson 		if (pm_iir) {
2360b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
23610e43406bSChris Wilson 			ret = IRQ_HANDLED;
236272c90f62SOscar Mateo 			gen6_rps_irq_handler(dev_priv, pm_iir);
23630e43406bSChris Wilson 		}
2364f1af8fc1SPaulo Zanoni 	}
2365b1f14ad0SJesse Barnes 
2366b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
2367b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
236891d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv)) {
236944498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
237044498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2371ab5c608bSBen Widawsky 	}
2372b1f14ad0SJesse Barnes 
23731f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
23741f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
23751f814dacSImre Deak 
2376b1f14ad0SJesse Barnes 	return ret;
2377b1f14ad0SJesse Barnes }
2378b1f14ad0SJesse Barnes 
237991d14251STvrtko Ursulin static void bxt_hpd_irq_handler(struct drm_i915_private *dev_priv,
238091d14251STvrtko Ursulin 				u32 hotplug_trigger,
238140e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2382d04a492dSShashank Sharma {
2383cebd87a0SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2384d04a492dSShashank Sharma 
2385a52bb15bSVille Syrjälä 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
2386a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
2387d04a492dSShashank Sharma 
2388cebd87a0SVille Syrjälä 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
238940e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2390cebd87a0SVille Syrjälä 			   bxt_port_hotplug_long_detect);
239140e56410SVille Syrjälä 
239291d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2393d04a492dSShashank Sharma }
2394d04a492dSShashank Sharma 
2395f11a0f46STvrtko Ursulin static irqreturn_t
2396f11a0f46STvrtko Ursulin gen8_de_irq_handler(struct drm_i915_private *dev_priv, u32 master_ctl)
2397abd58f01SBen Widawsky {
2398abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
2399f11a0f46STvrtko Ursulin 	u32 iir;
2400c42664ccSDaniel Vetter 	enum pipe pipe;
240188e04703SJesse Barnes 
2402abd58f01SBen Widawsky 	if (master_ctl & GEN8_DE_MISC_IRQ) {
2403e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_MISC_IIR);
2404e32192e1STvrtko Ursulin 		if (iir) {
2405e32192e1STvrtko Ursulin 			I915_WRITE(GEN8_DE_MISC_IIR, iir);
2406abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
2407e32192e1STvrtko Ursulin 			if (iir & GEN8_DE_MISC_GSE)
240891d14251STvrtko Ursulin 				intel_opregion_asle_intr(dev_priv);
240938cc46d7SOscar Mateo 			else
241038cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Misc interrupt\n");
2411abd58f01SBen Widawsky 		}
241238cc46d7SOscar Mateo 		else
241338cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
2414abd58f01SBen Widawsky 	}
2415abd58f01SBen Widawsky 
24166d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
2417e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_PORT_IIR);
2418e32192e1STvrtko Ursulin 		if (iir) {
2419e32192e1STvrtko Ursulin 			u32 tmp_mask;
2420d04a492dSShashank Sharma 			bool found = false;
2421cebd87a0SVille Syrjälä 
2422e32192e1STvrtko Ursulin 			I915_WRITE(GEN8_DE_PORT_IIR, iir);
24236d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
242488e04703SJesse Barnes 
2425e32192e1STvrtko Ursulin 			tmp_mask = GEN8_AUX_CHANNEL_A;
2426e32192e1STvrtko Ursulin 			if (INTEL_INFO(dev_priv)->gen >= 9)
2427e32192e1STvrtko Ursulin 				tmp_mask |= GEN9_AUX_CHANNEL_B |
2428e32192e1STvrtko Ursulin 					    GEN9_AUX_CHANNEL_C |
2429e32192e1STvrtko Ursulin 					    GEN9_AUX_CHANNEL_D;
2430e32192e1STvrtko Ursulin 
2431e32192e1STvrtko Ursulin 			if (iir & tmp_mask) {
243291d14251STvrtko Ursulin 				dp_aux_irq_handler(dev_priv);
2433d04a492dSShashank Sharma 				found = true;
2434d04a492dSShashank Sharma 			}
2435d04a492dSShashank Sharma 
2436e32192e1STvrtko Ursulin 			if (IS_BROXTON(dev_priv)) {
2437e32192e1STvrtko Ursulin 				tmp_mask = iir & BXT_DE_PORT_HOTPLUG_MASK;
2438e32192e1STvrtko Ursulin 				if (tmp_mask) {
243991d14251STvrtko Ursulin 					bxt_hpd_irq_handler(dev_priv, tmp_mask,
244091d14251STvrtko Ursulin 							    hpd_bxt);
2441d04a492dSShashank Sharma 					found = true;
2442d04a492dSShashank Sharma 				}
2443e32192e1STvrtko Ursulin 			} else if (IS_BROADWELL(dev_priv)) {
2444e32192e1STvrtko Ursulin 				tmp_mask = iir & GEN8_PORT_DP_A_HOTPLUG;
2445e32192e1STvrtko Ursulin 				if (tmp_mask) {
244691d14251STvrtko Ursulin 					ilk_hpd_irq_handler(dev_priv,
244791d14251STvrtko Ursulin 							    tmp_mask, hpd_bdw);
2448e32192e1STvrtko Ursulin 					found = true;
2449e32192e1STvrtko Ursulin 				}
2450e32192e1STvrtko Ursulin 			}
2451d04a492dSShashank Sharma 
245291d14251STvrtko Ursulin 			if (IS_BROXTON(dev_priv) && (iir & BXT_DE_PORT_GMBUS)) {
245391d14251STvrtko Ursulin 				gmbus_irq_handler(dev_priv);
24549e63743eSShashank Sharma 				found = true;
24559e63743eSShashank Sharma 			}
24569e63743eSShashank Sharma 
2457d04a492dSShashank Sharma 			if (!found)
245838cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Port interrupt\n");
24596d766f02SDaniel Vetter 		}
246038cc46d7SOscar Mateo 		else
246138cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
24626d766f02SDaniel Vetter 	}
24636d766f02SDaniel Vetter 
2464055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2465e32192e1STvrtko Ursulin 		u32 flip_done, fault_errors;
2466abd58f01SBen Widawsky 
2467c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2468c42664ccSDaniel Vetter 			continue;
2469c42664ccSDaniel Vetter 
2470e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
2471e32192e1STvrtko Ursulin 		if (!iir) {
2472e32192e1STvrtko Ursulin 			DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
2473e32192e1STvrtko Ursulin 			continue;
2474e32192e1STvrtko Ursulin 		}
2475770de83dSDamien Lespiau 
2476e32192e1STvrtko Ursulin 		ret = IRQ_HANDLED;
2477e32192e1STvrtko Ursulin 		I915_WRITE(GEN8_DE_PIPE_IIR(pipe), iir);
2478e32192e1STvrtko Ursulin 
24795a21b665SDaniel Vetter 		if (iir & GEN8_PIPE_VBLANK &&
24805a21b665SDaniel Vetter 		    intel_pipe_handle_vblank(dev_priv, pipe))
24815a21b665SDaniel Vetter 			intel_check_page_flip(dev_priv, pipe);
2482abd58f01SBen Widawsky 
2483e32192e1STvrtko Ursulin 		flip_done = iir;
2484b4834a50SRodrigo Vivi 		if (INTEL_INFO(dev_priv)->gen >= 9)
2485e32192e1STvrtko Ursulin 			flip_done &= GEN9_PIPE_PLANE1_FLIP_DONE;
2486770de83dSDamien Lespiau 		else
2487e32192e1STvrtko Ursulin 			flip_done &= GEN8_PIPE_PRIMARY_FLIP_DONE;
2488770de83dSDamien Lespiau 
24895251f04eSMaarten Lankhorst 		if (flip_done)
249051cbaf01SMaarten Lankhorst 			intel_finish_page_flip_cs(dev_priv, pipe);
2491abd58f01SBen Widawsky 
2492e32192e1STvrtko Ursulin 		if (iir & GEN8_PIPE_CDCLK_CRC_DONE)
249391d14251STvrtko Ursulin 			hsw_pipe_crc_irq_handler(dev_priv, pipe);
24940fbe7870SDaniel Vetter 
2495e32192e1STvrtko Ursulin 		if (iir & GEN8_PIPE_FIFO_UNDERRUN)
2496e32192e1STvrtko Ursulin 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
249738d83c96SDaniel Vetter 
2498e32192e1STvrtko Ursulin 		fault_errors = iir;
2499b4834a50SRodrigo Vivi 		if (INTEL_INFO(dev_priv)->gen >= 9)
2500e32192e1STvrtko Ursulin 			fault_errors &= GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
2501770de83dSDamien Lespiau 		else
2502e32192e1STvrtko Ursulin 			fault_errors &= GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
2503770de83dSDamien Lespiau 
2504770de83dSDamien Lespiau 		if (fault_errors)
25051353ec38STvrtko Ursulin 			DRM_ERROR("Fault errors on pipe %c: 0x%08x\n",
250630100f2bSDaniel Vetter 				  pipe_name(pipe),
2507e32192e1STvrtko Ursulin 				  fault_errors);
2508abd58f01SBen Widawsky 	}
2509abd58f01SBen Widawsky 
251091d14251STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv) && !HAS_PCH_NOP(dev_priv) &&
2511266ea3d9SShashank Sharma 	    master_ctl & GEN8_DE_PCH_IRQ) {
251292d03a80SDaniel Vetter 		/*
251392d03a80SDaniel Vetter 		 * FIXME(BDW): Assume for now that the new interrupt handling
251492d03a80SDaniel Vetter 		 * scheme also closed the SDE interrupt handling race we've seen
251592d03a80SDaniel Vetter 		 * on older pch-split platforms. But this needs testing.
251692d03a80SDaniel Vetter 		 */
2517e32192e1STvrtko Ursulin 		iir = I915_READ(SDEIIR);
2518e32192e1STvrtko Ursulin 		if (iir) {
2519e32192e1STvrtko Ursulin 			I915_WRITE(SDEIIR, iir);
252092d03a80SDaniel Vetter 			ret = IRQ_HANDLED;
25216dbf30ceSVille Syrjälä 
252222dea0beSRodrigo Vivi 			if (HAS_PCH_SPT(dev_priv) || HAS_PCH_KBP(dev_priv))
252391d14251STvrtko Ursulin 				spt_irq_handler(dev_priv, iir);
25246dbf30ceSVille Syrjälä 			else
252591d14251STvrtko Ursulin 				cpt_irq_handler(dev_priv, iir);
25262dfb0b81SJani Nikula 		} else {
25272dfb0b81SJani Nikula 			/*
25282dfb0b81SJani Nikula 			 * Like on previous PCH there seems to be something
25292dfb0b81SJani Nikula 			 * fishy going on with forwarding PCH interrupts.
25302dfb0b81SJani Nikula 			 */
25312dfb0b81SJani Nikula 			DRM_DEBUG_DRIVER("The master control interrupt lied (SDE)!\n");
25322dfb0b81SJani Nikula 		}
253392d03a80SDaniel Vetter 	}
253492d03a80SDaniel Vetter 
2535f11a0f46STvrtko Ursulin 	return ret;
2536f11a0f46STvrtko Ursulin }
2537f11a0f46STvrtko Ursulin 
2538f11a0f46STvrtko Ursulin static irqreturn_t gen8_irq_handler(int irq, void *arg)
2539f11a0f46STvrtko Ursulin {
2540f11a0f46STvrtko Ursulin 	struct drm_device *dev = arg;
2541fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2542f11a0f46STvrtko Ursulin 	u32 master_ctl;
2543e30e251aSVille Syrjälä 	u32 gt_iir[4] = {};
2544f11a0f46STvrtko Ursulin 	irqreturn_t ret;
2545f11a0f46STvrtko Ursulin 
2546f11a0f46STvrtko Ursulin 	if (!intel_irqs_enabled(dev_priv))
2547f11a0f46STvrtko Ursulin 		return IRQ_NONE;
2548f11a0f46STvrtko Ursulin 
2549f11a0f46STvrtko Ursulin 	master_ctl = I915_READ_FW(GEN8_MASTER_IRQ);
2550f11a0f46STvrtko Ursulin 	master_ctl &= ~GEN8_MASTER_IRQ_CONTROL;
2551f11a0f46STvrtko Ursulin 	if (!master_ctl)
2552f11a0f46STvrtko Ursulin 		return IRQ_NONE;
2553f11a0f46STvrtko Ursulin 
2554f11a0f46STvrtko Ursulin 	I915_WRITE_FW(GEN8_MASTER_IRQ, 0);
2555f11a0f46STvrtko Ursulin 
2556f11a0f46STvrtko Ursulin 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
2557f11a0f46STvrtko Ursulin 	disable_rpm_wakeref_asserts(dev_priv);
2558f11a0f46STvrtko Ursulin 
2559f11a0f46STvrtko Ursulin 	/* Find, clear, then process each source of interrupt */
2560e30e251aSVille Syrjälä 	ret = gen8_gt_irq_ack(dev_priv, master_ctl, gt_iir);
2561e30e251aSVille Syrjälä 	gen8_gt_irq_handler(dev_priv, gt_iir);
2562f11a0f46STvrtko Ursulin 	ret |= gen8_de_irq_handler(dev_priv, master_ctl);
2563f11a0f46STvrtko Ursulin 
2564cb0d205eSChris Wilson 	I915_WRITE_FW(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
2565cb0d205eSChris Wilson 	POSTING_READ_FW(GEN8_MASTER_IRQ);
2566abd58f01SBen Widawsky 
25671f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
25681f814dacSImre Deak 
2569abd58f01SBen Widawsky 	return ret;
2570abd58f01SBen Widawsky }
2571abd58f01SBen Widawsky 
25721f15b76fSChris Wilson static void i915_error_wake_up(struct drm_i915_private *dev_priv)
257317e1df07SDaniel Vetter {
257417e1df07SDaniel Vetter 	/*
257517e1df07SDaniel Vetter 	 * Notify all waiters for GPU completion events that reset state has
257617e1df07SDaniel Vetter 	 * been changed, and that they need to restart their wait after
257717e1df07SDaniel Vetter 	 * checking for potential errors (and bail out to drop locks if there is
257817e1df07SDaniel Vetter 	 * a gpu reset pending so that i915_error_work_func can acquire them).
257917e1df07SDaniel Vetter 	 */
258017e1df07SDaniel Vetter 
258117e1df07SDaniel Vetter 	/* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
25821f15b76fSChris Wilson 	wake_up_all(&dev_priv->gpu_error.wait_queue);
258317e1df07SDaniel Vetter 
258417e1df07SDaniel Vetter 	/* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
258517e1df07SDaniel Vetter 	wake_up_all(&dev_priv->pending_flip_queue);
258617e1df07SDaniel Vetter }
258717e1df07SDaniel Vetter 
25888a905236SJesse Barnes /**
2589b8d24a06SMika Kuoppala  * i915_reset_and_wakeup - do process context error handling work
259014bb2c11STvrtko Ursulin  * @dev_priv: i915 device private
25918a905236SJesse Barnes  *
25928a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
25938a905236SJesse Barnes  * was detected.
25948a905236SJesse Barnes  */
2595c033666aSChris Wilson static void i915_reset_and_wakeup(struct drm_i915_private *dev_priv)
25968a905236SJesse Barnes {
259791c8a326SChris Wilson 	struct kobject *kobj = &dev_priv->drm.primary->kdev->kobj;
2598cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
2599cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
2600cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
26018a905236SJesse Barnes 
2602c033666aSChris Wilson 	kobject_uevent_env(kobj, KOBJ_CHANGE, error_event);
26038a905236SJesse Barnes 
260444d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("resetting chip\n");
2605c033666aSChris Wilson 	kobject_uevent_env(kobj, KOBJ_CHANGE, reset_event);
26061f83fee0SDaniel Vetter 
260717e1df07SDaniel Vetter 	/*
2608f454c694SImre Deak 	 * In most cases it's guaranteed that we get here with an RPM
2609f454c694SImre Deak 	 * reference held, for example because there is a pending GPU
2610f454c694SImre Deak 	 * request that won't finish until the reset is done. This
2611f454c694SImre Deak 	 * isn't the case at least when we get here by doing a
2612f454c694SImre Deak 	 * simulated reset via debugs, so get an RPM reference.
2613f454c694SImre Deak 	 */
2614f454c694SImre Deak 	intel_runtime_pm_get(dev_priv);
2615c033666aSChris Wilson 	intel_prepare_reset(dev_priv);
26167514747dSVille Syrjälä 
2617780f262aSChris Wilson 	do {
2618f454c694SImre Deak 		/*
261917e1df07SDaniel Vetter 		 * All state reset _must_ be completed before we update the
262017e1df07SDaniel Vetter 		 * reset counter, for otherwise waiters might miss the reset
262117e1df07SDaniel Vetter 		 * pending state and not properly drop locks, resulting in
262217e1df07SDaniel Vetter 		 * deadlocks with the reset work.
262317e1df07SDaniel Vetter 		 */
2624780f262aSChris Wilson 		if (mutex_trylock(&dev_priv->drm.struct_mutex)) {
2625780f262aSChris Wilson 			i915_reset(dev_priv);
2626221fe799SChris Wilson 			mutex_unlock(&dev_priv->drm.struct_mutex);
2627780f262aSChris Wilson 		}
2628780f262aSChris Wilson 
2629780f262aSChris Wilson 		/* We need to wait for anyone holding the lock to wakeup */
2630780f262aSChris Wilson 	} while (wait_on_bit_timeout(&dev_priv->gpu_error.flags,
2631780f262aSChris Wilson 				     I915_RESET_IN_PROGRESS,
2632780f262aSChris Wilson 				     TASK_UNINTERRUPTIBLE,
2633780f262aSChris Wilson 				     HZ));
2634f69061beSDaniel Vetter 
2635c033666aSChris Wilson 	intel_finish_reset(dev_priv);
2636f454c694SImre Deak 	intel_runtime_pm_put(dev_priv);
2637f454c694SImre Deak 
2638780f262aSChris Wilson 	if (!test_bit(I915_WEDGED, &dev_priv->gpu_error.flags))
2639c033666aSChris Wilson 		kobject_uevent_env(kobj,
2640f69061beSDaniel Vetter 				   KOBJ_CHANGE, reset_done_event);
26411f83fee0SDaniel Vetter 
264217e1df07SDaniel Vetter 	/*
264317e1df07SDaniel Vetter 	 * Note: The wake_up also serves as a memory barrier so that
26448af29b0cSChris Wilson 	 * waiters see the updated value of the dev_priv->gpu_error.
264517e1df07SDaniel Vetter 	 */
26461f15b76fSChris Wilson 	wake_up_all(&dev_priv->gpu_error.reset_queue);
2647f316a42cSBen Gamari }
26488a905236SJesse Barnes 
2649d636951eSBen Widawsky static inline void
2650d636951eSBen Widawsky i915_err_print_instdone(struct drm_i915_private *dev_priv,
2651d636951eSBen Widawsky 			struct intel_instdone *instdone)
2652d636951eSBen Widawsky {
2653f9e61372SBen Widawsky 	int slice;
2654f9e61372SBen Widawsky 	int subslice;
2655f9e61372SBen Widawsky 
2656d636951eSBen Widawsky 	pr_err("  INSTDONE: 0x%08x\n", instdone->instdone);
2657d636951eSBen Widawsky 
2658d636951eSBen Widawsky 	if (INTEL_GEN(dev_priv) <= 3)
2659d636951eSBen Widawsky 		return;
2660d636951eSBen Widawsky 
2661d636951eSBen Widawsky 	pr_err("  SC_INSTDONE: 0x%08x\n", instdone->slice_common);
2662d636951eSBen Widawsky 
2663d636951eSBen Widawsky 	if (INTEL_GEN(dev_priv) <= 6)
2664d636951eSBen Widawsky 		return;
2665d636951eSBen Widawsky 
2666f9e61372SBen Widawsky 	for_each_instdone_slice_subslice(dev_priv, slice, subslice)
2667f9e61372SBen Widawsky 		pr_err("  SAMPLER_INSTDONE[%d][%d]: 0x%08x\n",
2668f9e61372SBen Widawsky 		       slice, subslice, instdone->sampler[slice][subslice]);
2669f9e61372SBen Widawsky 
2670f9e61372SBen Widawsky 	for_each_instdone_slice_subslice(dev_priv, slice, subslice)
2671f9e61372SBen Widawsky 		pr_err("  ROW_INSTDONE[%d][%d]: 0x%08x\n",
2672f9e61372SBen Widawsky 		       slice, subslice, instdone->row[slice][subslice]);
2673d636951eSBen Widawsky }
2674d636951eSBen Widawsky 
2675eaa14c24SChris Wilson static void i915_clear_error_registers(struct drm_i915_private *dev_priv)
2676c0e09200SDave Airlie {
2677eaa14c24SChris Wilson 	u32 eir;
267863eeaf38SJesse Barnes 
2679eaa14c24SChris Wilson 	if (!IS_GEN2(dev_priv))
2680eaa14c24SChris Wilson 		I915_WRITE(PGTBL_ER, I915_READ(PGTBL_ER));
268163eeaf38SJesse Barnes 
2682eaa14c24SChris Wilson 	if (INTEL_GEN(dev_priv) < 4)
2683eaa14c24SChris Wilson 		I915_WRITE(IPEIR, I915_READ(IPEIR));
2684eaa14c24SChris Wilson 	else
2685eaa14c24SChris Wilson 		I915_WRITE(IPEIR_I965, I915_READ(IPEIR_I965));
26868a905236SJesse Barnes 
2687eaa14c24SChris Wilson 	I915_WRITE(EIR, I915_READ(EIR));
268863eeaf38SJesse Barnes 	eir = I915_READ(EIR);
268963eeaf38SJesse Barnes 	if (eir) {
269063eeaf38SJesse Barnes 		/*
269163eeaf38SJesse Barnes 		 * some errors might have become stuck,
269263eeaf38SJesse Barnes 		 * mask them.
269363eeaf38SJesse Barnes 		 */
2694eaa14c24SChris Wilson 		DRM_DEBUG_DRIVER("EIR stuck: 0x%08x, masking\n", eir);
269563eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
269663eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
269763eeaf38SJesse Barnes 	}
269835aed2e6SChris Wilson }
269935aed2e6SChris Wilson 
270035aed2e6SChris Wilson /**
2701b8d24a06SMika Kuoppala  * i915_handle_error - handle a gpu error
270214bb2c11STvrtko Ursulin  * @dev_priv: i915 device private
270314b730fcSarun.siluvery@linux.intel.com  * @engine_mask: mask representing engines that are hung
2704aafd8581SJavier Martinez Canillas  * Do some basic checking of register state at error time and
270535aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
270635aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
270735aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
270835aed2e6SChris Wilson  * of a ring dump etc.).
270914bb2c11STvrtko Ursulin  * @fmt: Error message format string
271035aed2e6SChris Wilson  */
2711c033666aSChris Wilson void i915_handle_error(struct drm_i915_private *dev_priv,
2712c033666aSChris Wilson 		       u32 engine_mask,
271358174462SMika Kuoppala 		       const char *fmt, ...)
271435aed2e6SChris Wilson {
271558174462SMika Kuoppala 	va_list args;
271658174462SMika Kuoppala 	char error_msg[80];
271735aed2e6SChris Wilson 
271858174462SMika Kuoppala 	va_start(args, fmt);
271958174462SMika Kuoppala 	vscnprintf(error_msg, sizeof(error_msg), fmt, args);
272058174462SMika Kuoppala 	va_end(args);
272158174462SMika Kuoppala 
2722c033666aSChris Wilson 	i915_capture_error_state(dev_priv, engine_mask, error_msg);
2723eaa14c24SChris Wilson 	i915_clear_error_registers(dev_priv);
27248a905236SJesse Barnes 
27258af29b0cSChris Wilson 	if (!engine_mask)
27268af29b0cSChris Wilson 		return;
27278af29b0cSChris Wilson 
27288af29b0cSChris Wilson 	if (test_and_set_bit(I915_RESET_IN_PROGRESS,
27298af29b0cSChris Wilson 			     &dev_priv->gpu_error.flags))
27308af29b0cSChris Wilson 		return;
2731ba1234d1SBen Gamari 
273211ed50ecSBen Gamari 	/*
2733b8d24a06SMika Kuoppala 	 * Wakeup waiting processes so that the reset function
2734b8d24a06SMika Kuoppala 	 * i915_reset_and_wakeup doesn't deadlock trying to grab
2735b8d24a06SMika Kuoppala 	 * various locks. By bumping the reset counter first, the woken
273617e1df07SDaniel Vetter 	 * processes will see a reset in progress and back off,
273717e1df07SDaniel Vetter 	 * releasing their locks and then wait for the reset completion.
273817e1df07SDaniel Vetter 	 * We must do this for _all_ gpu waiters that might hold locks
273917e1df07SDaniel Vetter 	 * that the reset work needs to acquire.
274017e1df07SDaniel Vetter 	 *
27418af29b0cSChris Wilson 	 * Note: The wake_up also provides a memory barrier to ensure that the
27428af29b0cSChris Wilson 	 * waiters see the updated value of the reset flags.
274311ed50ecSBen Gamari 	 */
27441f15b76fSChris Wilson 	i915_error_wake_up(dev_priv);
274511ed50ecSBen Gamari 
2746c033666aSChris Wilson 	i915_reset_and_wakeup(dev_priv);
27478a905236SJesse Barnes }
27488a905236SJesse Barnes 
274942f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
275042f52ef8SKeith Packard  * we use as a pipe index
275142f52ef8SKeith Packard  */
275286e83e35SChris Wilson static int i8xx_enable_vblank(struct drm_device *dev, unsigned int pipe)
27530a3e67a4SJesse Barnes {
2754fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2755e9d21d7fSKeith Packard 	unsigned long irqflags;
275671e0ffa5SJesse Barnes 
27571ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
275886e83e35SChris Wilson 	i915_enable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_STATUS);
275986e83e35SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
276086e83e35SChris Wilson 
276186e83e35SChris Wilson 	return 0;
276286e83e35SChris Wilson }
276386e83e35SChris Wilson 
276486e83e35SChris Wilson static int i965_enable_vblank(struct drm_device *dev, unsigned int pipe)
276586e83e35SChris Wilson {
276686e83e35SChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
276786e83e35SChris Wilson 	unsigned long irqflags;
276886e83e35SChris Wilson 
276986e83e35SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
27707c463586SKeith Packard 	i915_enable_pipestat(dev_priv, pipe,
2771755e9019SImre Deak 			     PIPE_START_VBLANK_INTERRUPT_STATUS);
27721ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
27738692d00eSChris Wilson 
27740a3e67a4SJesse Barnes 	return 0;
27750a3e67a4SJesse Barnes }
27760a3e67a4SJesse Barnes 
277788e72717SThierry Reding static int ironlake_enable_vblank(struct drm_device *dev, unsigned int pipe)
2778f796cf8fSJesse Barnes {
2779fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2780f796cf8fSJesse Barnes 	unsigned long irqflags;
278155b8f2a7STvrtko Ursulin 	uint32_t bit = INTEL_GEN(dev_priv) >= 7 ?
278286e83e35SChris Wilson 		DE_PIPE_VBLANK_IVB(pipe) : DE_PIPE_VBLANK(pipe);
2783f796cf8fSJesse Barnes 
2784f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2785fbdedaeaSVille Syrjälä 	ilk_enable_display_irq(dev_priv, bit);
2786b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2787b1f14ad0SJesse Barnes 
2788b1f14ad0SJesse Barnes 	return 0;
2789b1f14ad0SJesse Barnes }
2790b1f14ad0SJesse Barnes 
279188e72717SThierry Reding static int gen8_enable_vblank(struct drm_device *dev, unsigned int pipe)
2792abd58f01SBen Widawsky {
2793fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2794abd58f01SBen Widawsky 	unsigned long irqflags;
2795abd58f01SBen Widawsky 
2796abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2797013d3752SVille Syrjälä 	bdw_enable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
2798abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2799013d3752SVille Syrjälä 
2800abd58f01SBen Widawsky 	return 0;
2801abd58f01SBen Widawsky }
2802abd58f01SBen Widawsky 
280342f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
280442f52ef8SKeith Packard  * we use as a pipe index
280542f52ef8SKeith Packard  */
280686e83e35SChris Wilson static void i8xx_disable_vblank(struct drm_device *dev, unsigned int pipe)
280786e83e35SChris Wilson {
280886e83e35SChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
280986e83e35SChris Wilson 	unsigned long irqflags;
281086e83e35SChris Wilson 
281186e83e35SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
281286e83e35SChris Wilson 	i915_disable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_STATUS);
281386e83e35SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
281486e83e35SChris Wilson }
281586e83e35SChris Wilson 
281686e83e35SChris Wilson static void i965_disable_vblank(struct drm_device *dev, unsigned int pipe)
28170a3e67a4SJesse Barnes {
2818fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2819e9d21d7fSKeith Packard 	unsigned long irqflags;
28200a3e67a4SJesse Barnes 
28211ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
28227c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
2823755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
28241ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
28250a3e67a4SJesse Barnes }
28260a3e67a4SJesse Barnes 
282788e72717SThierry Reding static void ironlake_disable_vblank(struct drm_device *dev, unsigned int pipe)
2828f796cf8fSJesse Barnes {
2829fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2830f796cf8fSJesse Barnes 	unsigned long irqflags;
283155b8f2a7STvrtko Ursulin 	uint32_t bit = INTEL_GEN(dev_priv) >= 7 ?
283286e83e35SChris Wilson 		DE_PIPE_VBLANK_IVB(pipe) : DE_PIPE_VBLANK(pipe);
2833f796cf8fSJesse Barnes 
2834f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2835fbdedaeaSVille Syrjälä 	ilk_disable_display_irq(dev_priv, bit);
2836b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2837b1f14ad0SJesse Barnes }
2838b1f14ad0SJesse Barnes 
283988e72717SThierry Reding static void gen8_disable_vblank(struct drm_device *dev, unsigned int pipe)
2840abd58f01SBen Widawsky {
2841fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2842abd58f01SBen Widawsky 	unsigned long irqflags;
2843abd58f01SBen Widawsky 
2844abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2845013d3752SVille Syrjälä 	bdw_disable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
2846abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2847abd58f01SBen Widawsky }
2848abd58f01SBen Widawsky 
28499107e9d2SChris Wilson static bool
285031bb59ccSChris Wilson ipehr_is_semaphore_wait(struct intel_engine_cs *engine, u32 ipehr)
2851a028c4b0SDaniel Vetter {
285231bb59ccSChris Wilson 	if (INTEL_GEN(engine->i915) >= 8) {
2853a6cdb93aSRodrigo Vivi 		return (ipehr >> 23) == 0x1c;
2854a028c4b0SDaniel Vetter 	} else {
2855a028c4b0SDaniel Vetter 		ipehr &= ~MI_SEMAPHORE_SYNC_MASK;
2856a028c4b0SDaniel Vetter 		return ipehr == (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE |
2857a028c4b0SDaniel Vetter 				 MI_SEMAPHORE_REGISTER);
2858a028c4b0SDaniel Vetter 	}
2859a028c4b0SDaniel Vetter }
2860a028c4b0SDaniel Vetter 
2861a4872ba6SOscar Mateo static struct intel_engine_cs *
28620bc40be8STvrtko Ursulin semaphore_wait_to_signaller_ring(struct intel_engine_cs *engine, u32 ipehr,
28630bc40be8STvrtko Ursulin 				 u64 offset)
2864921d42eaSDaniel Vetter {
2865c033666aSChris Wilson 	struct drm_i915_private *dev_priv = engine->i915;
2866a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
28673b3f1650SAkash Goel 	enum intel_engine_id id;
2868921d42eaSDaniel Vetter 
2869c033666aSChris Wilson 	if (INTEL_GEN(dev_priv) >= 8) {
28703b3f1650SAkash Goel 		for_each_engine(signaller, dev_priv, id) {
28710bc40be8STvrtko Ursulin 			if (engine == signaller)
2872a6cdb93aSRodrigo Vivi 				continue;
2873a6cdb93aSRodrigo Vivi 
2874348b9b11SChris Wilson 			if (offset == signaller->semaphore.signal_ggtt[engine->hw_id])
2875a6cdb93aSRodrigo Vivi 				return signaller;
2876a6cdb93aSRodrigo Vivi 		}
2877921d42eaSDaniel Vetter 	} else {
2878921d42eaSDaniel Vetter 		u32 sync_bits = ipehr & MI_SEMAPHORE_SYNC_MASK;
2879921d42eaSDaniel Vetter 
28803b3f1650SAkash Goel 		for_each_engine(signaller, dev_priv, id) {
28810bc40be8STvrtko Ursulin 			if(engine == signaller)
2882921d42eaSDaniel Vetter 				continue;
2883921d42eaSDaniel Vetter 
2884348b9b11SChris Wilson 			if (sync_bits == signaller->semaphore.mbox.wait[engine->hw_id])
2885921d42eaSDaniel Vetter 				return signaller;
2886921d42eaSDaniel Vetter 		}
2887921d42eaSDaniel Vetter 	}
2888921d42eaSDaniel Vetter 
2889348b9b11SChris Wilson 	DRM_DEBUG_DRIVER("No signaller ring found for %s, ipehr 0x%08x, offset 0x%016llx\n",
2890348b9b11SChris Wilson 			 engine->name, ipehr, offset);
2891921d42eaSDaniel Vetter 
289280b5bdbdSChris Wilson 	return ERR_PTR(-ENODEV);
2893921d42eaSDaniel Vetter }
2894921d42eaSDaniel Vetter 
2895a4872ba6SOscar Mateo static struct intel_engine_cs *
28960bc40be8STvrtko Ursulin semaphore_waits_for(struct intel_engine_cs *engine, u32 *seqno)
2897a24a11e6SChris Wilson {
2898c033666aSChris Wilson 	struct drm_i915_private *dev_priv = engine->i915;
2899406ea8d2SChris Wilson 	void __iomem *vaddr;
290088fe429dSDaniel Vetter 	u32 cmd, ipehr, head;
2901a6cdb93aSRodrigo Vivi 	u64 offset = 0;
2902a6cdb93aSRodrigo Vivi 	int i, backwards;
2903a24a11e6SChris Wilson 
2904381e8ae3STomas Elf 	/*
2905381e8ae3STomas Elf 	 * This function does not support execlist mode - any attempt to
2906381e8ae3STomas Elf 	 * proceed further into this function will result in a kernel panic
2907381e8ae3STomas Elf 	 * when dereferencing ring->buffer, which is not set up in execlist
2908381e8ae3STomas Elf 	 * mode.
2909381e8ae3STomas Elf 	 *
2910381e8ae3STomas Elf 	 * The correct way of doing it would be to derive the currently
2911381e8ae3STomas Elf 	 * executing ring buffer from the current context, which is derived
2912381e8ae3STomas Elf 	 * from the currently running request. Unfortunately, to get the
2913381e8ae3STomas Elf 	 * current request we would have to grab the struct_mutex before doing
2914381e8ae3STomas Elf 	 * anything else, which would be ill-advised since some other thread
2915381e8ae3STomas Elf 	 * might have grabbed it already and managed to hang itself, causing
2916381e8ae3STomas Elf 	 * the hang checker to deadlock.
2917381e8ae3STomas Elf 	 *
2918381e8ae3STomas Elf 	 * Therefore, this function does not support execlist mode in its
2919381e8ae3STomas Elf 	 * current form. Just return NULL and move on.
2920381e8ae3STomas Elf 	 */
29210bc40be8STvrtko Ursulin 	if (engine->buffer == NULL)
2922381e8ae3STomas Elf 		return NULL;
2923381e8ae3STomas Elf 
29240bc40be8STvrtko Ursulin 	ipehr = I915_READ(RING_IPEHR(engine->mmio_base));
292531bb59ccSChris Wilson 	if (!ipehr_is_semaphore_wait(engine, ipehr))
29266274f212SChris Wilson 		return NULL;
2927a24a11e6SChris Wilson 
292888fe429dSDaniel Vetter 	/*
292988fe429dSDaniel Vetter 	 * HEAD is likely pointing to the dword after the actual command,
293088fe429dSDaniel Vetter 	 * so scan backwards until we find the MBOX. But limit it to just 3
2931a6cdb93aSRodrigo Vivi 	 * or 4 dwords depending on the semaphore wait command size.
2932a6cdb93aSRodrigo Vivi 	 * Note that we don't care about ACTHD here since that might
293388fe429dSDaniel Vetter 	 * point at at batch, and semaphores are always emitted into the
293488fe429dSDaniel Vetter 	 * ringbuffer itself.
2935a24a11e6SChris Wilson 	 */
29360bc40be8STvrtko Ursulin 	head = I915_READ_HEAD(engine) & HEAD_ADDR;
2937c033666aSChris Wilson 	backwards = (INTEL_GEN(dev_priv) >= 8) ? 5 : 4;
2938f2f0ed71SChris Wilson 	vaddr = (void __iomem *)engine->buffer->vaddr;
293988fe429dSDaniel Vetter 
2940a6cdb93aSRodrigo Vivi 	for (i = backwards; i; --i) {
294188fe429dSDaniel Vetter 		/*
294288fe429dSDaniel Vetter 		 * Be paranoid and presume the hw has gone off into the wild -
294388fe429dSDaniel Vetter 		 * our ring is smaller than what the hardware (and hence
294488fe429dSDaniel Vetter 		 * HEAD_ADDR) allows. Also handles wrap-around.
294588fe429dSDaniel Vetter 		 */
29460bc40be8STvrtko Ursulin 		head &= engine->buffer->size - 1;
294788fe429dSDaniel Vetter 
294888fe429dSDaniel Vetter 		/* This here seems to blow up */
2949406ea8d2SChris Wilson 		cmd = ioread32(vaddr + head);
2950a24a11e6SChris Wilson 		if (cmd == ipehr)
2951a24a11e6SChris Wilson 			break;
2952a24a11e6SChris Wilson 
295388fe429dSDaniel Vetter 		head -= 4;
295488fe429dSDaniel Vetter 	}
2955a24a11e6SChris Wilson 
295688fe429dSDaniel Vetter 	if (!i)
295788fe429dSDaniel Vetter 		return NULL;
295888fe429dSDaniel Vetter 
2959406ea8d2SChris Wilson 	*seqno = ioread32(vaddr + head + 4) + 1;
2960c033666aSChris Wilson 	if (INTEL_GEN(dev_priv) >= 8) {
2961406ea8d2SChris Wilson 		offset = ioread32(vaddr + head + 12);
2962a6cdb93aSRodrigo Vivi 		offset <<= 32;
2963406ea8d2SChris Wilson 		offset |= ioread32(vaddr + head + 8);
2964a6cdb93aSRodrigo Vivi 	}
29650bc40be8STvrtko Ursulin 	return semaphore_wait_to_signaller_ring(engine, ipehr, offset);
2966a24a11e6SChris Wilson }
2967a24a11e6SChris Wilson 
29680bc40be8STvrtko Ursulin static int semaphore_passed(struct intel_engine_cs *engine)
29696274f212SChris Wilson {
2970c033666aSChris Wilson 	struct drm_i915_private *dev_priv = engine->i915;
2971a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2972a0d036b0SChris Wilson 	u32 seqno;
29736274f212SChris Wilson 
29740bc40be8STvrtko Ursulin 	engine->hangcheck.deadlock++;
29756274f212SChris Wilson 
29760bc40be8STvrtko Ursulin 	signaller = semaphore_waits_for(engine, &seqno);
29774be17381SChris Wilson 	if (signaller == NULL)
29784be17381SChris Wilson 		return -1;
29794be17381SChris Wilson 
298080b5bdbdSChris Wilson 	if (IS_ERR(signaller))
298180b5bdbdSChris Wilson 		return 0;
298280b5bdbdSChris Wilson 
29834be17381SChris Wilson 	/* Prevent pathological recursion due to driver bugs */
2984666796daSTvrtko Ursulin 	if (signaller->hangcheck.deadlock >= I915_NUM_ENGINES)
29856274f212SChris Wilson 		return -1;
29866274f212SChris Wilson 
29871b7744e7SChris Wilson 	if (i915_seqno_passed(intel_engine_get_seqno(signaller), seqno))
29884be17381SChris Wilson 		return 1;
29894be17381SChris Wilson 
2990a0d036b0SChris Wilson 	/* cursory check for an unkickable deadlock */
2991a0d036b0SChris Wilson 	if (I915_READ_CTL(signaller) & RING_WAIT_SEMAPHORE &&
2992a0d036b0SChris Wilson 	    semaphore_passed(signaller) < 0)
29934be17381SChris Wilson 		return -1;
29944be17381SChris Wilson 
29954be17381SChris Wilson 	return 0;
29966274f212SChris Wilson }
29976274f212SChris Wilson 
29986274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
29996274f212SChris Wilson {
3000e2f80391STvrtko Ursulin 	struct intel_engine_cs *engine;
30013b3f1650SAkash Goel 	enum intel_engine_id id;
30026274f212SChris Wilson 
30033b3f1650SAkash Goel 	for_each_engine(engine, dev_priv, id)
3004e2f80391STvrtko Ursulin 		engine->hangcheck.deadlock = 0;
30056274f212SChris Wilson }
30066274f212SChris Wilson 
3007d636951eSBen Widawsky static bool instdone_unchanged(u32 current_instdone, u32 *old_instdone)
3008d636951eSBen Widawsky {
3009d636951eSBen Widawsky 	u32 tmp = current_instdone | *old_instdone;
3010d636951eSBen Widawsky 	bool unchanged;
3011d636951eSBen Widawsky 
3012d636951eSBen Widawsky 	unchanged = tmp == *old_instdone;
3013d636951eSBen Widawsky 	*old_instdone |= tmp;
3014d636951eSBen Widawsky 
3015d636951eSBen Widawsky 	return unchanged;
3016d636951eSBen Widawsky }
3017d636951eSBen Widawsky 
30180bc40be8STvrtko Ursulin static bool subunits_stuck(struct intel_engine_cs *engine)
30191ec14ad3SChris Wilson {
3020d636951eSBen Widawsky 	struct drm_i915_private *dev_priv = engine->i915;
3021d636951eSBen Widawsky 	struct intel_instdone instdone;
3022d636951eSBen Widawsky 	struct intel_instdone *accu_instdone = &engine->hangcheck.instdone;
302361642ff0SMika Kuoppala 	bool stuck;
3024f9e61372SBen Widawsky 	int slice;
3025f9e61372SBen Widawsky 	int subslice;
30269107e9d2SChris Wilson 
30270bc40be8STvrtko Ursulin 	if (engine->id != RCS)
302861642ff0SMika Kuoppala 		return true;
302961642ff0SMika Kuoppala 
30300e704476SChris Wilson 	intel_engine_get_instdone(engine, &instdone);
303161642ff0SMika Kuoppala 
303261642ff0SMika Kuoppala 	/* There might be unstable subunit states even when
303361642ff0SMika Kuoppala 	 * actual head is not moving. Filter out the unstable ones by
303461642ff0SMika Kuoppala 	 * accumulating the undone -> done transitions and only
303561642ff0SMika Kuoppala 	 * consider those as progress.
303661642ff0SMika Kuoppala 	 */
3037d636951eSBen Widawsky 	stuck = instdone_unchanged(instdone.instdone,
3038d636951eSBen Widawsky 				   &accu_instdone->instdone);
3039d636951eSBen Widawsky 	stuck &= instdone_unchanged(instdone.slice_common,
3040d636951eSBen Widawsky 				    &accu_instdone->slice_common);
3041f9e61372SBen Widawsky 
3042f9e61372SBen Widawsky 	for_each_instdone_slice_subslice(dev_priv, slice, subslice) {
3043f9e61372SBen Widawsky 		stuck &= instdone_unchanged(instdone.sampler[slice][subslice],
3044f9e61372SBen Widawsky 					    &accu_instdone->sampler[slice][subslice]);
3045f9e61372SBen Widawsky 		stuck &= instdone_unchanged(instdone.row[slice][subslice],
3046f9e61372SBen Widawsky 					    &accu_instdone->row[slice][subslice]);
3047f9e61372SBen Widawsky 	}
304861642ff0SMika Kuoppala 
304961642ff0SMika Kuoppala 	return stuck;
305061642ff0SMika Kuoppala }
305161642ff0SMika Kuoppala 
30527e37f889SChris Wilson static enum intel_engine_hangcheck_action
30530bc40be8STvrtko Ursulin head_stuck(struct intel_engine_cs *engine, u64 acthd)
305461642ff0SMika Kuoppala {
30550bc40be8STvrtko Ursulin 	if (acthd != engine->hangcheck.acthd) {
305661642ff0SMika Kuoppala 
305761642ff0SMika Kuoppala 		/* Clear subunit states on head movement */
3058d636951eSBen Widawsky 		memset(&engine->hangcheck.instdone, 0,
30590bc40be8STvrtko Ursulin 		       sizeof(engine->hangcheck.instdone));
306061642ff0SMika Kuoppala 
3061f2f4d82fSJani Nikula 		return HANGCHECK_ACTIVE;
3062f260fe7bSMika Kuoppala 	}
3063f260fe7bSMika Kuoppala 
30640bc40be8STvrtko Ursulin 	if (!subunits_stuck(engine))
306561642ff0SMika Kuoppala 		return HANGCHECK_ACTIVE;
306661642ff0SMika Kuoppala 
306761642ff0SMika Kuoppala 	return HANGCHECK_HUNG;
306861642ff0SMika Kuoppala }
306961642ff0SMika Kuoppala 
30707e37f889SChris Wilson static enum intel_engine_hangcheck_action
30717e37f889SChris Wilson engine_stuck(struct intel_engine_cs *engine, u64 acthd)
307261642ff0SMika Kuoppala {
3073c033666aSChris Wilson 	struct drm_i915_private *dev_priv = engine->i915;
30747e37f889SChris Wilson 	enum intel_engine_hangcheck_action ha;
307561642ff0SMika Kuoppala 	u32 tmp;
307661642ff0SMika Kuoppala 
30770bc40be8STvrtko Ursulin 	ha = head_stuck(engine, acthd);
307861642ff0SMika Kuoppala 	if (ha != HANGCHECK_HUNG)
307961642ff0SMika Kuoppala 		return ha;
308061642ff0SMika Kuoppala 
3081c033666aSChris Wilson 	if (IS_GEN2(dev_priv))
3082f2f4d82fSJani Nikula 		return HANGCHECK_HUNG;
30839107e9d2SChris Wilson 
30849107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
30859107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
30869107e9d2SChris Wilson 	 * and break the hang. This should work on
30879107e9d2SChris Wilson 	 * all but the second generation chipsets.
30889107e9d2SChris Wilson 	 */
30890bc40be8STvrtko Ursulin 	tmp = I915_READ_CTL(engine);
30901ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
3091c033666aSChris Wilson 		i915_handle_error(dev_priv, 0,
309258174462SMika Kuoppala 				  "Kicking stuck wait on %s",
30930bc40be8STvrtko Ursulin 				  engine->name);
30940bc40be8STvrtko Ursulin 		I915_WRITE_CTL(engine, tmp);
3095f2f4d82fSJani Nikula 		return HANGCHECK_KICK;
30961ec14ad3SChris Wilson 	}
3097a24a11e6SChris Wilson 
3098c033666aSChris Wilson 	if (INTEL_GEN(dev_priv) >= 6 && tmp & RING_WAIT_SEMAPHORE) {
30990bc40be8STvrtko Ursulin 		switch (semaphore_passed(engine)) {
31006274f212SChris Wilson 		default:
3101f2f4d82fSJani Nikula 			return HANGCHECK_HUNG;
31026274f212SChris Wilson 		case 1:
3103c033666aSChris Wilson 			i915_handle_error(dev_priv, 0,
310458174462SMika Kuoppala 					  "Kicking stuck semaphore on %s",
31050bc40be8STvrtko Ursulin 					  engine->name);
31060bc40be8STvrtko Ursulin 			I915_WRITE_CTL(engine, tmp);
3107f2f4d82fSJani Nikula 			return HANGCHECK_KICK;
31086274f212SChris Wilson 		case 0:
3109f2f4d82fSJani Nikula 			return HANGCHECK_WAIT;
31106274f212SChris Wilson 		}
31119107e9d2SChris Wilson 	}
31129107e9d2SChris Wilson 
3113f2f4d82fSJani Nikula 	return HANGCHECK_HUNG;
3114a24a11e6SChris Wilson }
3115d1e61e7fSChris Wilson 
3116737b1506SChris Wilson /*
3117f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
311805407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
311905407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
312005407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
312105407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
312205407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
3123f65d9421SBen Gamari  */
3124737b1506SChris Wilson static void i915_hangcheck_elapsed(struct work_struct *work)
3125f65d9421SBen Gamari {
3126737b1506SChris Wilson 	struct drm_i915_private *dev_priv =
3127737b1506SChris Wilson 		container_of(work, typeof(*dev_priv),
3128737b1506SChris Wilson 			     gpu_error.hangcheck_work.work);
3129e2f80391STvrtko Ursulin 	struct intel_engine_cs *engine;
31303b3f1650SAkash Goel 	enum intel_engine_id id;
31312b284288SChris Wilson 	unsigned int hung = 0, stuck = 0;
31322b284288SChris Wilson 	int busy_count = 0;
31339107e9d2SChris Wilson #define BUSY 1
31349107e9d2SChris Wilson #define KICK 5
31359107e9d2SChris Wilson #define HUNG 20
313624a65e62SMika Kuoppala #define ACTIVE_DECAY 15
3137893eead0SChris Wilson 
3138d330a953SJani Nikula 	if (!i915.enable_hangcheck)
31393e0dc6b0SBen Widawsky 		return;
31403e0dc6b0SBen Widawsky 
3141b1379d49SChris Wilson 	if (!READ_ONCE(dev_priv->gt.awake))
314267d97da3SChris Wilson 		return;
31431f814dacSImre Deak 
314475714940SMika Kuoppala 	/* As enabling the GPU requires fairly extensive mmio access,
314575714940SMika Kuoppala 	 * periodically arm the mmio checker to see if we are triggering
314675714940SMika Kuoppala 	 * any invalid access.
314775714940SMika Kuoppala 	 */
314875714940SMika Kuoppala 	intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
314975714940SMika Kuoppala 
31503b3f1650SAkash Goel 	for_each_engine(engine, dev_priv, id) {
3151688e6c72SChris Wilson 		bool busy = intel_engine_has_waiter(engine);
315250877445SChris Wilson 		u64 acthd;
315350877445SChris Wilson 		u32 seqno;
315434730fedSChris Wilson 		u32 submit;
3155b4519513SChris Wilson 
31566274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
31576274f212SChris Wilson 
3158c04e0f3bSChris Wilson 		/* We don't strictly need an irq-barrier here, as we are not
3159c04e0f3bSChris Wilson 		 * serving an interrupt request, be paranoid in case the
3160c04e0f3bSChris Wilson 		 * barrier has side-effects (such as preventing a broken
3161c04e0f3bSChris Wilson 		 * cacheline snoop) and so be sure that we can see the seqno
3162c04e0f3bSChris Wilson 		 * advance. If the seqno should stick, due to a stale
3163c04e0f3bSChris Wilson 		 * cacheline, we would erroneously declare the GPU hung.
3164c04e0f3bSChris Wilson 		 */
3165c04e0f3bSChris Wilson 		if (engine->irq_seqno_barrier)
3166c04e0f3bSChris Wilson 			engine->irq_seqno_barrier(engine);
3167c04e0f3bSChris Wilson 
31687e37f889SChris Wilson 		acthd = intel_engine_get_active_head(engine);
31691b7744e7SChris Wilson 		seqno = intel_engine_get_seqno(engine);
3170cb399eabSChris Wilson 		submit = intel_engine_last_submit(engine);
317105407ff8SMika Kuoppala 
3172e2f80391STvrtko Ursulin 		if (engine->hangcheck.seqno == seqno) {
317334730fedSChris Wilson 			if (i915_seqno_passed(seqno, submit)) {
3174e2f80391STvrtko Ursulin 				engine->hangcheck.action = HANGCHECK_IDLE;
317505407ff8SMika Kuoppala 			} else {
31766274f212SChris Wilson 				/* We always increment the hangcheck score
31779930ca1aSChris Wilson 				 * if the engine is busy and still processing
31786274f212SChris Wilson 				 * the same request, so that no single request
31796274f212SChris Wilson 				 * can run indefinitely (such as a chain of
31806274f212SChris Wilson 				 * batches). The only time we do not increment
31816274f212SChris Wilson 				 * the hangcheck score on this ring, if this
31829930ca1aSChris Wilson 				 * engine is in a legitimate wait for another
31839930ca1aSChris Wilson 				 * engine. In that case the waiting engine is a
31846274f212SChris Wilson 				 * victim and we want to be sure we catch the
31856274f212SChris Wilson 				 * right culprit. Then every time we do kick
31866274f212SChris Wilson 				 * the ring, add a small increment to the
31876274f212SChris Wilson 				 * score so that we can catch a batch that is
31886274f212SChris Wilson 				 * being repeatedly kicked and so responsible
31896274f212SChris Wilson 				 * for stalling the machine.
31909107e9d2SChris Wilson 				 */
31917e37f889SChris Wilson 				engine->hangcheck.action =
31927e37f889SChris Wilson 					engine_stuck(engine, acthd);
3193ad8beaeaSMika Kuoppala 
3194e2f80391STvrtko Ursulin 				switch (engine->hangcheck.action) {
3195da661464SMika Kuoppala 				case HANGCHECK_IDLE:
3196f2f4d82fSJani Nikula 				case HANGCHECK_WAIT:
3197f260fe7bSMika Kuoppala 					break;
319824a65e62SMika Kuoppala 				case HANGCHECK_ACTIVE:
3199e2f80391STvrtko Ursulin 					engine->hangcheck.score += BUSY;
32006274f212SChris Wilson 					break;
3201f2f4d82fSJani Nikula 				case HANGCHECK_KICK:
3202e2f80391STvrtko Ursulin 					engine->hangcheck.score += KICK;
32036274f212SChris Wilson 					break;
3204f2f4d82fSJani Nikula 				case HANGCHECK_HUNG:
3205e2f80391STvrtko Ursulin 					engine->hangcheck.score += HUNG;
32066274f212SChris Wilson 					break;
32076274f212SChris Wilson 				}
320805407ff8SMika Kuoppala 			}
32092b284288SChris Wilson 
32102b284288SChris Wilson 			if (engine->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG) {
32112b284288SChris Wilson 				hung |= intel_engine_flag(engine);
32122b284288SChris Wilson 				if (engine->hangcheck.action != HANGCHECK_HUNG)
32132b284288SChris Wilson 					stuck |= intel_engine_flag(engine);
32142b284288SChris Wilson 			}
32159107e9d2SChris Wilson 		} else {
3216e2f80391STvrtko Ursulin 			engine->hangcheck.action = HANGCHECK_ACTIVE;
3217da661464SMika Kuoppala 
32189107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
32199107e9d2SChris Wilson 			 * attempts across multiple batches.
32209107e9d2SChris Wilson 			 */
3221e2f80391STvrtko Ursulin 			if (engine->hangcheck.score > 0)
3222e2f80391STvrtko Ursulin 				engine->hangcheck.score -= ACTIVE_DECAY;
3223e2f80391STvrtko Ursulin 			if (engine->hangcheck.score < 0)
3224e2f80391STvrtko Ursulin 				engine->hangcheck.score = 0;
3225f260fe7bSMika Kuoppala 
322661642ff0SMika Kuoppala 			/* Clear head and subunit states on seqno movement */
322712471ba8SChris Wilson 			acthd = 0;
322861642ff0SMika Kuoppala 
3229d636951eSBen Widawsky 			memset(&engine->hangcheck.instdone, 0,
3230e2f80391STvrtko Ursulin 			       sizeof(engine->hangcheck.instdone));
3231cbb465e7SChris Wilson 		}
3232f65d9421SBen Gamari 
3233e2f80391STvrtko Ursulin 		engine->hangcheck.seqno = seqno;
3234e2f80391STvrtko Ursulin 		engine->hangcheck.acthd = acthd;
32359107e9d2SChris Wilson 		busy_count += busy;
323605407ff8SMika Kuoppala 	}
323705407ff8SMika Kuoppala 
32382b284288SChris Wilson 	if (hung) {
32392b284288SChris Wilson 		char msg[80];
3240bafb0fceSChris Wilson 		unsigned int tmp;
32412b284288SChris Wilson 		int len;
324205407ff8SMika Kuoppala 
32432b284288SChris Wilson 		/* If some rings hung but others were still busy, only
32442b284288SChris Wilson 		 * blame the hanging rings in the synopsis.
32452b284288SChris Wilson 		 */
32462b284288SChris Wilson 		if (stuck != hung)
32472b284288SChris Wilson 			hung &= ~stuck;
32482b284288SChris Wilson 		len = scnprintf(msg, sizeof(msg),
32492b284288SChris Wilson 				"%s on ", stuck == hung ? "No progress" : "Hang");
3250bafb0fceSChris Wilson 		for_each_engine_masked(engine, dev_priv, hung, tmp)
32512b284288SChris Wilson 			len += scnprintf(msg + len, sizeof(msg) - len,
32522b284288SChris Wilson 					 "%s, ", engine->name);
32532b284288SChris Wilson 		msg[len-2] = '\0';
32542b284288SChris Wilson 
32552b284288SChris Wilson 		return i915_handle_error(dev_priv, hung, msg);
32562b284288SChris Wilson 	}
325705407ff8SMika Kuoppala 
325805535726SChris Wilson 	/* Reset timer in case GPU hangs without another request being added */
325905407ff8SMika Kuoppala 	if (busy_count)
3260c033666aSChris Wilson 		i915_queue_hangcheck(dev_priv);
326110cd45b6SMika Kuoppala }
326210cd45b6SMika Kuoppala 
32631c69eb42SPaulo Zanoni static void ibx_irq_reset(struct drm_device *dev)
326491738a95SPaulo Zanoni {
3265fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
326691738a95SPaulo Zanoni 
32676e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
326891738a95SPaulo Zanoni 		return;
326991738a95SPaulo Zanoni 
3270f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(SDE);
3271105b122eSPaulo Zanoni 
32726e266956STvrtko Ursulin 	if (HAS_PCH_CPT(dev_priv) || HAS_PCH_LPT(dev_priv))
3273105b122eSPaulo Zanoni 		I915_WRITE(SERR_INT, 0xffffffff);
3274622364b6SPaulo Zanoni }
3275105b122eSPaulo Zanoni 
327691738a95SPaulo Zanoni /*
3277622364b6SPaulo Zanoni  * SDEIER is also touched by the interrupt handler to work around missed PCH
3278622364b6SPaulo Zanoni  * interrupts. Hence we can't update it after the interrupt handler is enabled -
3279622364b6SPaulo Zanoni  * instead we unconditionally enable all PCH interrupt sources here, but then
3280622364b6SPaulo Zanoni  * only unmask them as needed with SDEIMR.
3281622364b6SPaulo Zanoni  *
3282622364b6SPaulo Zanoni  * This function needs to be called before interrupts are enabled.
328391738a95SPaulo Zanoni  */
3284622364b6SPaulo Zanoni static void ibx_irq_pre_postinstall(struct drm_device *dev)
3285622364b6SPaulo Zanoni {
3286fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3287622364b6SPaulo Zanoni 
32886e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
3289622364b6SPaulo Zanoni 		return;
3290622364b6SPaulo Zanoni 
3291622364b6SPaulo Zanoni 	WARN_ON(I915_READ(SDEIER) != 0);
329291738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
329391738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
329491738a95SPaulo Zanoni }
329591738a95SPaulo Zanoni 
32967c4d664eSPaulo Zanoni static void gen5_gt_irq_reset(struct drm_device *dev)
3297d18ea1b5SDaniel Vetter {
3298fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3299d18ea1b5SDaniel Vetter 
3300f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GT);
3301a9d356a6SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6)
3302f86f3fb0SPaulo Zanoni 		GEN5_IRQ_RESET(GEN6_PM);
3303d18ea1b5SDaniel Vetter }
3304d18ea1b5SDaniel Vetter 
330570591a41SVille Syrjälä static void vlv_display_irq_reset(struct drm_i915_private *dev_priv)
330670591a41SVille Syrjälä {
330770591a41SVille Syrjälä 	enum pipe pipe;
330870591a41SVille Syrjälä 
330971b8b41dSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
331071b8b41dSVille Syrjälä 		I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
331171b8b41dSVille Syrjälä 	else
331271b8b41dSVille Syrjälä 		I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
331371b8b41dSVille Syrjälä 
3314ad22d106SVille Syrjälä 	i915_hotplug_interrupt_update_locked(dev_priv, 0xffffffff, 0);
331570591a41SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
331670591a41SVille Syrjälä 
3317ad22d106SVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
3318ad22d106SVille Syrjälä 		I915_WRITE(PIPESTAT(pipe),
3319ad22d106SVille Syrjälä 			   PIPE_FIFO_UNDERRUN_STATUS |
3320ad22d106SVille Syrjälä 			   PIPESTAT_INT_STATUS_MASK);
3321ad22d106SVille Syrjälä 		dev_priv->pipestat_irq_mask[pipe] = 0;
3322ad22d106SVille Syrjälä 	}
332370591a41SVille Syrjälä 
332470591a41SVille Syrjälä 	GEN5_IRQ_RESET(VLV_);
3325ad22d106SVille Syrjälä 	dev_priv->irq_mask = ~0;
332670591a41SVille Syrjälä }
332770591a41SVille Syrjälä 
33288bb61306SVille Syrjälä static void vlv_display_irq_postinstall(struct drm_i915_private *dev_priv)
33298bb61306SVille Syrjälä {
33308bb61306SVille Syrjälä 	u32 pipestat_mask;
33319ab981f2SVille Syrjälä 	u32 enable_mask;
33328bb61306SVille Syrjälä 	enum pipe pipe;
33338bb61306SVille Syrjälä 
33348bb61306SVille Syrjälä 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
33358bb61306SVille Syrjälä 			PIPE_CRC_DONE_INTERRUPT_STATUS;
33368bb61306SVille Syrjälä 
33378bb61306SVille Syrjälä 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
33388bb61306SVille Syrjälä 	for_each_pipe(dev_priv, pipe)
33398bb61306SVille Syrjälä 		i915_enable_pipestat(dev_priv, pipe, pipestat_mask);
33408bb61306SVille Syrjälä 
33419ab981f2SVille Syrjälä 	enable_mask = I915_DISPLAY_PORT_INTERRUPT |
33428bb61306SVille Syrjälä 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
33438bb61306SVille Syrjälä 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
33448bb61306SVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
33459ab981f2SVille Syrjälä 		enable_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
33466b7eafc1SVille Syrjälä 
33476b7eafc1SVille Syrjälä 	WARN_ON(dev_priv->irq_mask != ~0);
33486b7eafc1SVille Syrjälä 
33499ab981f2SVille Syrjälä 	dev_priv->irq_mask = ~enable_mask;
33508bb61306SVille Syrjälä 
33519ab981f2SVille Syrjälä 	GEN5_IRQ_INIT(VLV_, dev_priv->irq_mask, enable_mask);
33528bb61306SVille Syrjälä }
33538bb61306SVille Syrjälä 
33548bb61306SVille Syrjälä /* drm_dma.h hooks
33558bb61306SVille Syrjälä */
33568bb61306SVille Syrjälä static void ironlake_irq_reset(struct drm_device *dev)
33578bb61306SVille Syrjälä {
3358fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
33598bb61306SVille Syrjälä 
33608bb61306SVille Syrjälä 	I915_WRITE(HWSTAM, 0xffffffff);
33618bb61306SVille Syrjälä 
33628bb61306SVille Syrjälä 	GEN5_IRQ_RESET(DE);
33635db94019STvrtko Ursulin 	if (IS_GEN7(dev_priv))
33648bb61306SVille Syrjälä 		I915_WRITE(GEN7_ERR_INT, 0xffffffff);
33658bb61306SVille Syrjälä 
33668bb61306SVille Syrjälä 	gen5_gt_irq_reset(dev);
33678bb61306SVille Syrjälä 
33688bb61306SVille Syrjälä 	ibx_irq_reset(dev);
33698bb61306SVille Syrjälä }
33708bb61306SVille Syrjälä 
33717e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
33727e231dbeSJesse Barnes {
3373fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
33747e231dbeSJesse Barnes 
337534c7b8a7SVille Syrjälä 	I915_WRITE(VLV_MASTER_IER, 0);
337634c7b8a7SVille Syrjälä 	POSTING_READ(VLV_MASTER_IER);
337734c7b8a7SVille Syrjälä 
33787c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
33797e231dbeSJesse Barnes 
3380ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
33819918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
338270591a41SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3383ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
33847e231dbeSJesse Barnes }
33857e231dbeSJesse Barnes 
3386d6e3cca3SDaniel Vetter static void gen8_gt_irq_reset(struct drm_i915_private *dev_priv)
3387d6e3cca3SDaniel Vetter {
3388d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 0);
3389d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 1);
3390d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 2);
3391d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 3);
3392d6e3cca3SDaniel Vetter }
3393d6e3cca3SDaniel Vetter 
3394823f6b38SPaulo Zanoni static void gen8_irq_reset(struct drm_device *dev)
3395abd58f01SBen Widawsky {
3396fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3397abd58f01SBen Widawsky 	int pipe;
3398abd58f01SBen Widawsky 
3399abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
3400abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3401abd58f01SBen Widawsky 
3402d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
3403abd58f01SBen Widawsky 
3404055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3405f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3406813bde43SPaulo Zanoni 						   POWER_DOMAIN_PIPE(pipe)))
3407f86f3fb0SPaulo Zanoni 			GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3408abd58f01SBen Widawsky 
3409f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_PORT_);
3410f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_MISC_);
3411f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_PCU_);
3412abd58f01SBen Widawsky 
34136e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
34141c69eb42SPaulo Zanoni 		ibx_irq_reset(dev);
3415abd58f01SBen Widawsky }
3416abd58f01SBen Widawsky 
34174c6c03beSDamien Lespiau void gen8_irq_power_well_post_enable(struct drm_i915_private *dev_priv,
34184c6c03beSDamien Lespiau 				     unsigned int pipe_mask)
3419d49bdb0eSPaulo Zanoni {
34201180e206SPaulo Zanoni 	uint32_t extra_ier = GEN8_PIPE_VBLANK | GEN8_PIPE_FIFO_UNDERRUN;
34216831f3e3SVille Syrjälä 	enum pipe pipe;
3422d49bdb0eSPaulo Zanoni 
342313321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
34246831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
34256831f3e3SVille Syrjälä 		GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
34266831f3e3SVille Syrjälä 				  dev_priv->de_irq_mask[pipe],
34276831f3e3SVille Syrjälä 				  ~dev_priv->de_irq_mask[pipe] | extra_ier);
342813321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3429d49bdb0eSPaulo Zanoni }
3430d49bdb0eSPaulo Zanoni 
3431aae8ba84SVille Syrjälä void gen8_irq_power_well_pre_disable(struct drm_i915_private *dev_priv,
3432aae8ba84SVille Syrjälä 				     unsigned int pipe_mask)
3433aae8ba84SVille Syrjälä {
34346831f3e3SVille Syrjälä 	enum pipe pipe;
34356831f3e3SVille Syrjälä 
3436aae8ba84SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
34376831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
34386831f3e3SVille Syrjälä 		GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3439aae8ba84SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3440aae8ba84SVille Syrjälä 
3441aae8ba84SVille Syrjälä 	/* make sure we're done processing display irqs */
344291c8a326SChris Wilson 	synchronize_irq(dev_priv->drm.irq);
3443aae8ba84SVille Syrjälä }
3444aae8ba84SVille Syrjälä 
344543f328d7SVille Syrjälä static void cherryview_irq_preinstall(struct drm_device *dev)
344643f328d7SVille Syrjälä {
3447fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
344843f328d7SVille Syrjälä 
344943f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
345043f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
345143f328d7SVille Syrjälä 
3452d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
345343f328d7SVille Syrjälä 
345443f328d7SVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
345543f328d7SVille Syrjälä 
3456ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
34579918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
345870591a41SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3459ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
346043f328d7SVille Syrjälä }
346143f328d7SVille Syrjälä 
346291d14251STvrtko Ursulin static u32 intel_hpd_enabled_irqs(struct drm_i915_private *dev_priv,
346387a02106SVille Syrjälä 				  const u32 hpd[HPD_NUM_PINS])
346487a02106SVille Syrjälä {
346587a02106SVille Syrjälä 	struct intel_encoder *encoder;
346687a02106SVille Syrjälä 	u32 enabled_irqs = 0;
346787a02106SVille Syrjälä 
346891c8a326SChris Wilson 	for_each_intel_encoder(&dev_priv->drm, encoder)
346987a02106SVille Syrjälä 		if (dev_priv->hotplug.stats[encoder->hpd_pin].state == HPD_ENABLED)
347087a02106SVille Syrjälä 			enabled_irqs |= hpd[encoder->hpd_pin];
347187a02106SVille Syrjälä 
347287a02106SVille Syrjälä 	return enabled_irqs;
347387a02106SVille Syrjälä }
347487a02106SVille Syrjälä 
347591d14251STvrtko Ursulin static void ibx_hpd_irq_setup(struct drm_i915_private *dev_priv)
347682a28bcfSDaniel Vetter {
347787a02106SVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
347882a28bcfSDaniel Vetter 
347991d14251STvrtko Ursulin 	if (HAS_PCH_IBX(dev_priv)) {
3480fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
348191d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ibx);
348282a28bcfSDaniel Vetter 	} else {
3483fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
348491d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_cpt);
348582a28bcfSDaniel Vetter 	}
348682a28bcfSDaniel Vetter 
3487fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
348882a28bcfSDaniel Vetter 
34897fe0b973SKeith Packard 	/*
34907fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
34916dbf30ceSVille Syrjälä 	 * duration to 2ms (which is the minimum in the Display Port spec).
34926dbf30ceSVille Syrjälä 	 * The pulse duration bits are reserved on LPT+.
34937fe0b973SKeith Packard 	 */
34947fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
34957fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
34967fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
34977fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
34987fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
34990b2eb33eSVille Syrjälä 	/*
35000b2eb33eSVille Syrjälä 	 * When CPU and PCH are on the same package, port A
35010b2eb33eSVille Syrjälä 	 * HPD must be enabled in both north and south.
35020b2eb33eSVille Syrjälä 	 */
350391d14251STvrtko Ursulin 	if (HAS_PCH_LPT_LP(dev_priv))
35040b2eb33eSVille Syrjälä 		hotplug |= PORTA_HOTPLUG_ENABLE;
35057fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
35066dbf30ceSVille Syrjälä }
350726951cafSXiong Zhang 
350891d14251STvrtko Ursulin static void spt_hpd_irq_setup(struct drm_i915_private *dev_priv)
35096dbf30ceSVille Syrjälä {
35106dbf30ceSVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
35116dbf30ceSVille Syrjälä 
35126dbf30ceSVille Syrjälä 	hotplug_irqs = SDE_HOTPLUG_MASK_SPT;
351391d14251STvrtko Ursulin 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_spt);
35146dbf30ceSVille Syrjälä 
35156dbf30ceSVille Syrjälä 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
35166dbf30ceSVille Syrjälä 
35176dbf30ceSVille Syrjälä 	/* Enable digital hotplug on the PCH */
35186dbf30ceSVille Syrjälä 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
35196dbf30ceSVille Syrjälä 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTC_HOTPLUG_ENABLE |
352074c0b395SVille Syrjälä 		PORTB_HOTPLUG_ENABLE | PORTA_HOTPLUG_ENABLE;
35216dbf30ceSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
35226dbf30ceSVille Syrjälä 
352326951cafSXiong Zhang 	hotplug = I915_READ(PCH_PORT_HOTPLUG2);
352426951cafSXiong Zhang 	hotplug |= PORTE_HOTPLUG_ENABLE;
352526951cafSXiong Zhang 	I915_WRITE(PCH_PORT_HOTPLUG2, hotplug);
352626951cafSXiong Zhang }
35277fe0b973SKeith Packard 
352891d14251STvrtko Ursulin static void ilk_hpd_irq_setup(struct drm_i915_private *dev_priv)
3529e4ce95aaSVille Syrjälä {
3530e4ce95aaSVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
3531e4ce95aaSVille Syrjälä 
353291d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 8) {
35333a3b3c7dSVille Syrjälä 		hotplug_irqs = GEN8_PORT_DP_A_HOTPLUG;
353491d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_bdw);
35353a3b3c7dSVille Syrjälä 
35363a3b3c7dSVille Syrjälä 		bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
353791d14251STvrtko Ursulin 	} else if (INTEL_GEN(dev_priv) >= 7) {
353823bb4cb5SVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG_IVB;
353991d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ivb);
35403a3b3c7dSVille Syrjälä 
35413a3b3c7dSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
354223bb4cb5SVille Syrjälä 	} else {
3543e4ce95aaSVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG;
354491d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ilk);
3545e4ce95aaSVille Syrjälä 
3546e4ce95aaSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
35473a3b3c7dSVille Syrjälä 	}
3548e4ce95aaSVille Syrjälä 
3549e4ce95aaSVille Syrjälä 	/*
3550e4ce95aaSVille Syrjälä 	 * Enable digital hotplug on the CPU, and configure the DP short pulse
3551e4ce95aaSVille Syrjälä 	 * duration to 2ms (which is the minimum in the Display Port spec)
355223bb4cb5SVille Syrjälä 	 * The pulse duration bits are reserved on HSW+.
3553e4ce95aaSVille Syrjälä 	 */
3554e4ce95aaSVille Syrjälä 	hotplug = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
3555e4ce95aaSVille Syrjälä 	hotplug &= ~DIGITAL_PORTA_PULSE_DURATION_MASK;
3556e4ce95aaSVille Syrjälä 	hotplug |= DIGITAL_PORTA_HOTPLUG_ENABLE | DIGITAL_PORTA_PULSE_DURATION_2ms;
3557e4ce95aaSVille Syrjälä 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, hotplug);
3558e4ce95aaSVille Syrjälä 
355991d14251STvrtko Ursulin 	ibx_hpd_irq_setup(dev_priv);
3560e4ce95aaSVille Syrjälä }
3561e4ce95aaSVille Syrjälä 
356291d14251STvrtko Ursulin static void bxt_hpd_irq_setup(struct drm_i915_private *dev_priv)
3563e0a20ad7SShashank Sharma {
3564a52bb15bSVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
3565e0a20ad7SShashank Sharma 
356691d14251STvrtko Ursulin 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_bxt);
3567a52bb15bSVille Syrjälä 	hotplug_irqs = BXT_DE_PORT_HOTPLUG_MASK;
3568e0a20ad7SShashank Sharma 
3569a52bb15bSVille Syrjälä 	bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
3570e0a20ad7SShashank Sharma 
3571a52bb15bSVille Syrjälä 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
3572a52bb15bSVille Syrjälä 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTB_HOTPLUG_ENABLE |
3573a52bb15bSVille Syrjälä 		PORTA_HOTPLUG_ENABLE;
3574d252bf68SShubhangi Shrivastava 
3575d252bf68SShubhangi Shrivastava 	DRM_DEBUG_KMS("Invert bit setting: hp_ctl:%x hp_port:%x\n",
3576d252bf68SShubhangi Shrivastava 		      hotplug, enabled_irqs);
3577d252bf68SShubhangi Shrivastava 	hotplug &= ~BXT_DDI_HPD_INVERT_MASK;
3578d252bf68SShubhangi Shrivastava 
3579d252bf68SShubhangi Shrivastava 	/*
3580d252bf68SShubhangi Shrivastava 	 * For BXT invert bit has to be set based on AOB design
3581d252bf68SShubhangi Shrivastava 	 * for HPD detection logic, update it based on VBT fields.
3582d252bf68SShubhangi Shrivastava 	 */
3583d252bf68SShubhangi Shrivastava 
3584d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIA) &&
3585d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_A))
3586d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIA_HPD_INVERT;
3587d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIB) &&
3588d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_B))
3589d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIB_HPD_INVERT;
3590d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIC) &&
3591d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_C))
3592d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIC_HPD_INVERT;
3593d252bf68SShubhangi Shrivastava 
3594a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
3595e0a20ad7SShashank Sharma }
3596e0a20ad7SShashank Sharma 
3597d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
3598d46da437SPaulo Zanoni {
3599fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
360082a28bcfSDaniel Vetter 	u32 mask;
3601d46da437SPaulo Zanoni 
36026e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
3603692a04cfSDaniel Vetter 		return;
3604692a04cfSDaniel Vetter 
36056e266956STvrtko Ursulin 	if (HAS_PCH_IBX(dev_priv))
36065c673b60SDaniel Vetter 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
3607105b122eSPaulo Zanoni 	else
36085c673b60SDaniel Vetter 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
36098664281bSPaulo Zanoni 
3610b51a2842SVille Syrjälä 	gen5_assert_iir_is_zero(dev_priv, SDEIIR);
3611d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
3612d46da437SPaulo Zanoni }
3613d46da437SPaulo Zanoni 
36140a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
36150a9a8c91SDaniel Vetter {
3616fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
36170a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
36180a9a8c91SDaniel Vetter 
36190a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
36200a9a8c91SDaniel Vetter 
36210a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
36223c9192bcSTvrtko Ursulin 	if (HAS_L3_DPF(dev_priv)) {
36230a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
3624772c2a51STvrtko Ursulin 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev_priv);
3625772c2a51STvrtko Ursulin 		gt_irqs |= GT_PARITY_ERROR(dev_priv);
36260a9a8c91SDaniel Vetter 	}
36270a9a8c91SDaniel Vetter 
36280a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
36295db94019STvrtko Ursulin 	if (IS_GEN5(dev_priv)) {
3630f8973c21SChris Wilson 		gt_irqs |= ILK_BSD_USER_INTERRUPT;
36310a9a8c91SDaniel Vetter 	} else {
36320a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
36330a9a8c91SDaniel Vetter 	}
36340a9a8c91SDaniel Vetter 
363535079899SPaulo Zanoni 	GEN5_IRQ_INIT(GT, dev_priv->gt_irq_mask, gt_irqs);
36360a9a8c91SDaniel Vetter 
36370a9a8c91SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
363878e68d36SImre Deak 		/*
363978e68d36SImre Deak 		 * RPS interrupts will get enabled/disabled on demand when RPS
364078e68d36SImre Deak 		 * itself is enabled/disabled.
364178e68d36SImre Deak 		 */
3642f4e9af4fSAkash Goel 		if (HAS_VEBOX(dev_priv)) {
36430a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
3644f4e9af4fSAkash Goel 			dev_priv->pm_ier |= PM_VEBOX_USER_INTERRUPT;
3645f4e9af4fSAkash Goel 		}
36460a9a8c91SDaniel Vetter 
3647f4e9af4fSAkash Goel 		dev_priv->pm_imr = 0xffffffff;
3648f4e9af4fSAkash Goel 		GEN5_IRQ_INIT(GEN6_PM, dev_priv->pm_imr, pm_irqs);
36490a9a8c91SDaniel Vetter 	}
36500a9a8c91SDaniel Vetter }
36510a9a8c91SDaniel Vetter 
3652f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
3653036a4a7dSZhenyu Wang {
3654fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
36558e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
36568e76f8dcSPaulo Zanoni 
36578e76f8dcSPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 7) {
36588e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
36598e76f8dcSPaulo Zanoni 				DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
36608e76f8dcSPaulo Zanoni 				DE_PLANEB_FLIP_DONE_IVB |
36615c673b60SDaniel Vetter 				DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB);
36628e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
366323bb4cb5SVille Syrjälä 			      DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB |
366423bb4cb5SVille Syrjälä 			      DE_DP_A_HOTPLUG_IVB);
36658e76f8dcSPaulo Zanoni 	} else {
36668e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3667ce99c256SDaniel Vetter 				DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
36685b3a856bSDaniel Vetter 				DE_AUX_CHANNEL_A |
36695b3a856bSDaniel Vetter 				DE_PIPEB_CRC_DONE | DE_PIPEA_CRC_DONE |
36705b3a856bSDaniel Vetter 				DE_POISON);
3671e4ce95aaSVille Syrjälä 		extra_mask = (DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
3672e4ce95aaSVille Syrjälä 			      DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN |
3673e4ce95aaSVille Syrjälä 			      DE_DP_A_HOTPLUG);
36748e76f8dcSPaulo Zanoni 	}
3675036a4a7dSZhenyu Wang 
36761ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3677036a4a7dSZhenyu Wang 
36780c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xeffe);
36790c841212SPaulo Zanoni 
3680622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3681622364b6SPaulo Zanoni 
368235079899SPaulo Zanoni 	GEN5_IRQ_INIT(DE, dev_priv->irq_mask, display_mask | extra_mask);
3683036a4a7dSZhenyu Wang 
36840a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
3685036a4a7dSZhenyu Wang 
3686d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
36877fe0b973SKeith Packard 
368850a0bc90STvrtko Ursulin 	if (IS_IRONLAKE_M(dev_priv)) {
36896005ce42SDaniel Vetter 		/* Enable PCU event interrupts
36906005ce42SDaniel Vetter 		 *
36916005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
36924bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
36934bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
3694d6207435SDaniel Vetter 		spin_lock_irq(&dev_priv->irq_lock);
3695fbdedaeaSVille Syrjälä 		ilk_enable_display_irq(dev_priv, DE_PCU_EVENT);
3696d6207435SDaniel Vetter 		spin_unlock_irq(&dev_priv->irq_lock);
3697f97108d1SJesse Barnes 	}
3698f97108d1SJesse Barnes 
3699036a4a7dSZhenyu Wang 	return 0;
3700036a4a7dSZhenyu Wang }
3701036a4a7dSZhenyu Wang 
3702f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3703f8b79e58SImre Deak {
3704f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3705f8b79e58SImre Deak 
3706f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3707f8b79e58SImre Deak 		return;
3708f8b79e58SImre Deak 
3709f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3710f8b79e58SImre Deak 
3711d6c69803SVille Syrjälä 	if (intel_irqs_enabled(dev_priv)) {
3712d6c69803SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3713ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3714f8b79e58SImre Deak 	}
3715d6c69803SVille Syrjälä }
3716f8b79e58SImre Deak 
3717f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3718f8b79e58SImre Deak {
3719f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3720f8b79e58SImre Deak 
3721f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3722f8b79e58SImre Deak 		return;
3723f8b79e58SImre Deak 
3724f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3725f8b79e58SImre Deak 
3726950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3727ad22d106SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3728f8b79e58SImre Deak }
3729f8b79e58SImre Deak 
37300e6c9a9eSVille Syrjälä 
37310e6c9a9eSVille Syrjälä static int valleyview_irq_postinstall(struct drm_device *dev)
37320e6c9a9eSVille Syrjälä {
3733fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
37340e6c9a9eSVille Syrjälä 
37350a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
37367e231dbeSJesse Barnes 
3737ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
37389918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3739ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3740ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3741ad22d106SVille Syrjälä 
37427e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
374334c7b8a7SVille Syrjälä 	POSTING_READ(VLV_MASTER_IER);
374420afbda2SDaniel Vetter 
374520afbda2SDaniel Vetter 	return 0;
374620afbda2SDaniel Vetter }
374720afbda2SDaniel Vetter 
3748abd58f01SBen Widawsky static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
3749abd58f01SBen Widawsky {
3750abd58f01SBen Widawsky 	/* These are interrupts we'll toggle with the ring mask register */
3751abd58f01SBen Widawsky 	uint32_t gt_interrupts[] = {
3752abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
375373d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
375473d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT |
375573d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT,
3756abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
375773d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
375873d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT |
375973d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT,
3760abd58f01SBen Widawsky 		0,
376173d477f6SOscar Mateo 		GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT |
376273d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT
3763abd58f01SBen Widawsky 		};
3764abd58f01SBen Widawsky 
376598735739STvrtko Ursulin 	if (HAS_L3_DPF(dev_priv))
376698735739STvrtko Ursulin 		gt_interrupts[0] |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
376798735739STvrtko Ursulin 
3768f4e9af4fSAkash Goel 	dev_priv->pm_ier = 0x0;
3769f4e9af4fSAkash Goel 	dev_priv->pm_imr = ~dev_priv->pm_ier;
37709a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 0, ~gt_interrupts[0], gt_interrupts[0]);
37719a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 1, ~gt_interrupts[1], gt_interrupts[1]);
377278e68d36SImre Deak 	/*
377378e68d36SImre Deak 	 * RPS interrupts will get enabled/disabled on demand when RPS itself
377426705e20SSagar Arun Kamble 	 * is enabled/disabled. Same wil be the case for GuC interrupts.
377578e68d36SImre Deak 	 */
3776f4e9af4fSAkash Goel 	GEN8_IRQ_INIT_NDX(GT, 2, dev_priv->pm_imr, dev_priv->pm_ier);
37779a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 3, ~gt_interrupts[3], gt_interrupts[3]);
3778abd58f01SBen Widawsky }
3779abd58f01SBen Widawsky 
3780abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3781abd58f01SBen Widawsky {
3782770de83dSDamien Lespiau 	uint32_t de_pipe_masked = GEN8_PIPE_CDCLK_CRC_DONE;
3783770de83dSDamien Lespiau 	uint32_t de_pipe_enables;
37843a3b3c7dSVille Syrjälä 	u32 de_port_masked = GEN8_AUX_CHANNEL_A;
37853a3b3c7dSVille Syrjälä 	u32 de_port_enables;
378611825b0dSVille Syrjälä 	u32 de_misc_masked = GEN8_DE_MISC_GSE;
37873a3b3c7dSVille Syrjälä 	enum pipe pipe;
3788770de83dSDamien Lespiau 
3789b4834a50SRodrigo Vivi 	if (INTEL_INFO(dev_priv)->gen >= 9) {
3790770de83dSDamien Lespiau 		de_pipe_masked |= GEN9_PIPE_PLANE1_FLIP_DONE |
3791770de83dSDamien Lespiau 				  GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
37923a3b3c7dSVille Syrjälä 		de_port_masked |= GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
379388e04703SJesse Barnes 				  GEN9_AUX_CHANNEL_D;
37949e63743eSShashank Sharma 		if (IS_BROXTON(dev_priv))
37953a3b3c7dSVille Syrjälä 			de_port_masked |= BXT_DE_PORT_GMBUS;
37963a3b3c7dSVille Syrjälä 	} else {
3797770de83dSDamien Lespiau 		de_pipe_masked |= GEN8_PIPE_PRIMARY_FLIP_DONE |
3798770de83dSDamien Lespiau 				  GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
37993a3b3c7dSVille Syrjälä 	}
3800770de83dSDamien Lespiau 
3801770de83dSDamien Lespiau 	de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
3802770de83dSDamien Lespiau 					   GEN8_PIPE_FIFO_UNDERRUN;
3803770de83dSDamien Lespiau 
38043a3b3c7dSVille Syrjälä 	de_port_enables = de_port_masked;
3805a52bb15bSVille Syrjälä 	if (IS_BROXTON(dev_priv))
3806a52bb15bSVille Syrjälä 		de_port_enables |= BXT_DE_PORT_HOTPLUG_MASK;
3807a52bb15bSVille Syrjälä 	else if (IS_BROADWELL(dev_priv))
38083a3b3c7dSVille Syrjälä 		de_port_enables |= GEN8_PORT_DP_A_HOTPLUG;
38093a3b3c7dSVille Syrjälä 
381013b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_A] = ~de_pipe_masked;
381113b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_B] = ~de_pipe_masked;
381213b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_C] = ~de_pipe_masked;
3813abd58f01SBen Widawsky 
3814055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3815f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3816813bde43SPaulo Zanoni 				POWER_DOMAIN_PIPE(pipe)))
3817813bde43SPaulo Zanoni 			GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
3818813bde43SPaulo Zanoni 					  dev_priv->de_irq_mask[pipe],
381935079899SPaulo Zanoni 					  de_pipe_enables);
3820abd58f01SBen Widawsky 
38213a3b3c7dSVille Syrjälä 	GEN5_IRQ_INIT(GEN8_DE_PORT_, ~de_port_masked, de_port_enables);
382211825b0dSVille Syrjälä 	GEN5_IRQ_INIT(GEN8_DE_MISC_, ~de_misc_masked, de_misc_masked);
3823abd58f01SBen Widawsky }
3824abd58f01SBen Widawsky 
3825abd58f01SBen Widawsky static int gen8_irq_postinstall(struct drm_device *dev)
3826abd58f01SBen Widawsky {
3827fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3828abd58f01SBen Widawsky 
38296e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
3830622364b6SPaulo Zanoni 		ibx_irq_pre_postinstall(dev);
3831622364b6SPaulo Zanoni 
3832abd58f01SBen Widawsky 	gen8_gt_irq_postinstall(dev_priv);
3833abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3834abd58f01SBen Widawsky 
38356e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
3836abd58f01SBen Widawsky 		ibx_irq_postinstall(dev);
3837abd58f01SBen Widawsky 
3838e5328c43SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
3839abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3840abd58f01SBen Widawsky 
3841abd58f01SBen Widawsky 	return 0;
3842abd58f01SBen Widawsky }
3843abd58f01SBen Widawsky 
384443f328d7SVille Syrjälä static int cherryview_irq_postinstall(struct drm_device *dev)
384543f328d7SVille Syrjälä {
3846fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
384743f328d7SVille Syrjälä 
384843f328d7SVille Syrjälä 	gen8_gt_irq_postinstall(dev_priv);
384943f328d7SVille Syrjälä 
3850ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
38519918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3852ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3853ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3854ad22d106SVille Syrjälä 
3855e5328c43SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
385643f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
385743f328d7SVille Syrjälä 
385843f328d7SVille Syrjälä 	return 0;
385943f328d7SVille Syrjälä }
386043f328d7SVille Syrjälä 
3861abd58f01SBen Widawsky static void gen8_irq_uninstall(struct drm_device *dev)
3862abd58f01SBen Widawsky {
3863fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3864abd58f01SBen Widawsky 
3865abd58f01SBen Widawsky 	if (!dev_priv)
3866abd58f01SBen Widawsky 		return;
3867abd58f01SBen Widawsky 
3868823f6b38SPaulo Zanoni 	gen8_irq_reset(dev);
3869abd58f01SBen Widawsky }
3870abd58f01SBen Widawsky 
38717e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
38727e231dbeSJesse Barnes {
3873fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
38747e231dbeSJesse Barnes 
38757e231dbeSJesse Barnes 	if (!dev_priv)
38767e231dbeSJesse Barnes 		return;
38777e231dbeSJesse Barnes 
3878843d0e7dSImre Deak 	I915_WRITE(VLV_MASTER_IER, 0);
387934c7b8a7SVille Syrjälä 	POSTING_READ(VLV_MASTER_IER);
3880843d0e7dSImre Deak 
3881893fce8eSVille Syrjälä 	gen5_gt_irq_reset(dev);
3882893fce8eSVille Syrjälä 
38837e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
3884f8b79e58SImre Deak 
3885ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
38869918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3887ad22d106SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3888ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
38897e231dbeSJesse Barnes }
38907e231dbeSJesse Barnes 
389143f328d7SVille Syrjälä static void cherryview_irq_uninstall(struct drm_device *dev)
389243f328d7SVille Syrjälä {
3893fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
389443f328d7SVille Syrjälä 
389543f328d7SVille Syrjälä 	if (!dev_priv)
389643f328d7SVille Syrjälä 		return;
389743f328d7SVille Syrjälä 
389843f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
389943f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
390043f328d7SVille Syrjälä 
3901a2c30fbaSVille Syrjälä 	gen8_gt_irq_reset(dev_priv);
390243f328d7SVille Syrjälä 
3903a2c30fbaSVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
390443f328d7SVille Syrjälä 
3905ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
39069918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3907ad22d106SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3908ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
390943f328d7SVille Syrjälä }
391043f328d7SVille Syrjälä 
3911f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
3912036a4a7dSZhenyu Wang {
3913fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
39144697995bSJesse Barnes 
39154697995bSJesse Barnes 	if (!dev_priv)
39164697995bSJesse Barnes 		return;
39174697995bSJesse Barnes 
3918be30b29fSPaulo Zanoni 	ironlake_irq_reset(dev);
3919036a4a7dSZhenyu Wang }
3920036a4a7dSZhenyu Wang 
3921c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
3922c2798b19SChris Wilson {
3923fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3924c2798b19SChris Wilson 	int pipe;
3925c2798b19SChris Wilson 
3926055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3927c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3928c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3929c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3930c2798b19SChris Wilson 	POSTING_READ16(IER);
3931c2798b19SChris Wilson }
3932c2798b19SChris Wilson 
3933c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
3934c2798b19SChris Wilson {
3935fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3936c2798b19SChris Wilson 
3937c2798b19SChris Wilson 	I915_WRITE16(EMR,
3938c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3939c2798b19SChris Wilson 
3940c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
3941c2798b19SChris Wilson 	dev_priv->irq_mask =
3942c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3943c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3944c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
394537ef01abSDaniel Vetter 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
3946c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
3947c2798b19SChris Wilson 
3948c2798b19SChris Wilson 	I915_WRITE16(IER,
3949c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3950c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3951c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
3952c2798b19SChris Wilson 	POSTING_READ16(IER);
3953c2798b19SChris Wilson 
3954379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3955379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3956d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3957755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3958755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3959d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3960379ef82dSDaniel Vetter 
3961c2798b19SChris Wilson 	return 0;
3962c2798b19SChris Wilson }
3963c2798b19SChris Wilson 
39645a21b665SDaniel Vetter /*
39655a21b665SDaniel Vetter  * Returns true when a page flip has completed.
39665a21b665SDaniel Vetter  */
39675a21b665SDaniel Vetter static bool i8xx_handle_vblank(struct drm_i915_private *dev_priv,
39685a21b665SDaniel Vetter 			       int plane, int pipe, u32 iir)
39695a21b665SDaniel Vetter {
39705a21b665SDaniel Vetter 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
39715a21b665SDaniel Vetter 
39725a21b665SDaniel Vetter 	if (!intel_pipe_handle_vblank(dev_priv, pipe))
39735a21b665SDaniel Vetter 		return false;
39745a21b665SDaniel Vetter 
39755a21b665SDaniel Vetter 	if ((iir & flip_pending) == 0)
39765a21b665SDaniel Vetter 		goto check_page_flip;
39775a21b665SDaniel Vetter 
39785a21b665SDaniel Vetter 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
39795a21b665SDaniel Vetter 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
39805a21b665SDaniel Vetter 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
39815a21b665SDaniel Vetter 	 * the flip is completed (no longer pending). Since this doesn't raise
39825a21b665SDaniel Vetter 	 * an interrupt per se, we watch for the change at vblank.
39835a21b665SDaniel Vetter 	 */
39845a21b665SDaniel Vetter 	if (I915_READ16(ISR) & flip_pending)
39855a21b665SDaniel Vetter 		goto check_page_flip;
39865a21b665SDaniel Vetter 
39875a21b665SDaniel Vetter 	intel_finish_page_flip_cs(dev_priv, pipe);
39885a21b665SDaniel Vetter 	return true;
39895a21b665SDaniel Vetter 
39905a21b665SDaniel Vetter check_page_flip:
39915a21b665SDaniel Vetter 	intel_check_page_flip(dev_priv, pipe);
39925a21b665SDaniel Vetter 	return false;
39935a21b665SDaniel Vetter }
39945a21b665SDaniel Vetter 
3995ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3996c2798b19SChris Wilson {
399745a83f84SDaniel Vetter 	struct drm_device *dev = arg;
3998fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3999c2798b19SChris Wilson 	u16 iir, new_iir;
4000c2798b19SChris Wilson 	u32 pipe_stats[2];
4001c2798b19SChris Wilson 	int pipe;
4002c2798b19SChris Wilson 	u16 flip_mask =
4003c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
4004c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
40051f814dacSImre Deak 	irqreturn_t ret;
4006c2798b19SChris Wilson 
40072dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
40082dd2a883SImre Deak 		return IRQ_NONE;
40092dd2a883SImre Deak 
40101f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
40111f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
40121f814dacSImre Deak 
40131f814dacSImre Deak 	ret = IRQ_NONE;
4014c2798b19SChris Wilson 	iir = I915_READ16(IIR);
4015c2798b19SChris Wilson 	if (iir == 0)
40161f814dacSImre Deak 		goto out;
4017c2798b19SChris Wilson 
4018c2798b19SChris Wilson 	while (iir & ~flip_mask) {
4019c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4020c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4021c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4022c2798b19SChris Wilson 		 * interrupts (for non-MSI).
4023c2798b19SChris Wilson 		 */
4024222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
4025c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4026aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4027c2798b19SChris Wilson 
4028055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4029f0f59a00SVille Syrjälä 			i915_reg_t reg = PIPESTAT(pipe);
4030c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4031c2798b19SChris Wilson 
4032c2798b19SChris Wilson 			/*
4033c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
4034c2798b19SChris Wilson 			 */
40352d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & 0x8000ffff)
4036c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
4037c2798b19SChris Wilson 		}
4038222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4039c2798b19SChris Wilson 
4040c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
4041c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
4042c2798b19SChris Wilson 
4043c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
40443b3f1650SAkash Goel 			notify_ring(dev_priv->engine[RCS]);
4045c2798b19SChris Wilson 
4046055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
40475a21b665SDaniel Vetter 			int plane = pipe;
40485a21b665SDaniel Vetter 			if (HAS_FBC(dev_priv))
40495a21b665SDaniel Vetter 				plane = !plane;
40505a21b665SDaniel Vetter 
40515a21b665SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
40525a21b665SDaniel Vetter 			    i8xx_handle_vblank(dev_priv, plane, pipe, iir))
40535a21b665SDaniel Vetter 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
4054c2798b19SChris Wilson 
40554356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
405691d14251STvrtko Ursulin 				i9xx_pipe_crc_irq_handler(dev_priv, pipe);
40572d9d2b0bSVille Syrjälä 
40581f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
40591f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
40601f7247c0SDaniel Vetter 								    pipe);
40614356d586SDaniel Vetter 		}
4062c2798b19SChris Wilson 
4063c2798b19SChris Wilson 		iir = new_iir;
4064c2798b19SChris Wilson 	}
40651f814dacSImre Deak 	ret = IRQ_HANDLED;
4066c2798b19SChris Wilson 
40671f814dacSImre Deak out:
40681f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
40691f814dacSImre Deak 
40701f814dacSImre Deak 	return ret;
4071c2798b19SChris Wilson }
4072c2798b19SChris Wilson 
4073c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
4074c2798b19SChris Wilson {
4075fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4076c2798b19SChris Wilson 	int pipe;
4077c2798b19SChris Wilson 
4078055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
4079c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
4080c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4081c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
4082c2798b19SChris Wilson 	}
4083c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
4084c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
4085c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
4086c2798b19SChris Wilson }
4087c2798b19SChris Wilson 
4088a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
4089a266c7d5SChris Wilson {
4090fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4091a266c7d5SChris Wilson 	int pipe;
4092a266c7d5SChris Wilson 
4093a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
40940706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4095a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4096a266c7d5SChris Wilson 	}
4097a266c7d5SChris Wilson 
409800d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
4099055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4100a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4101a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4102a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4103a266c7d5SChris Wilson 	POSTING_READ(IER);
4104a266c7d5SChris Wilson }
4105a266c7d5SChris Wilson 
4106a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
4107a266c7d5SChris Wilson {
4108fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
410938bde180SChris Wilson 	u32 enable_mask;
4110a266c7d5SChris Wilson 
411138bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
411238bde180SChris Wilson 
411338bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
411438bde180SChris Wilson 	dev_priv->irq_mask =
411538bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
411638bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
411738bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
411838bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
411937ef01abSDaniel Vetter 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
412038bde180SChris Wilson 
412138bde180SChris Wilson 	enable_mask =
412238bde180SChris Wilson 		I915_ASLE_INTERRUPT |
412338bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
412438bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
412538bde180SChris Wilson 		I915_USER_INTERRUPT;
412638bde180SChris Wilson 
4127a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
41280706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
412920afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
413020afbda2SDaniel Vetter 
4131a266c7d5SChris Wilson 		/* Enable in IER... */
4132a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
4133a266c7d5SChris Wilson 		/* and unmask in IMR */
4134a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
4135a266c7d5SChris Wilson 	}
4136a266c7d5SChris Wilson 
4137a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
4138a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
4139a266c7d5SChris Wilson 	POSTING_READ(IER);
4140a266c7d5SChris Wilson 
414191d14251STvrtko Ursulin 	i915_enable_asle_pipestat(dev_priv);
414220afbda2SDaniel Vetter 
4143379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4144379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4145d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4146755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4147755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4148d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4149379ef82dSDaniel Vetter 
415020afbda2SDaniel Vetter 	return 0;
415120afbda2SDaniel Vetter }
415220afbda2SDaniel Vetter 
41535a21b665SDaniel Vetter /*
41545a21b665SDaniel Vetter  * Returns true when a page flip has completed.
41555a21b665SDaniel Vetter  */
41565a21b665SDaniel Vetter static bool i915_handle_vblank(struct drm_i915_private *dev_priv,
41575a21b665SDaniel Vetter 			       int plane, int pipe, u32 iir)
41585a21b665SDaniel Vetter {
41595a21b665SDaniel Vetter 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
41605a21b665SDaniel Vetter 
41615a21b665SDaniel Vetter 	if (!intel_pipe_handle_vblank(dev_priv, pipe))
41625a21b665SDaniel Vetter 		return false;
41635a21b665SDaniel Vetter 
41645a21b665SDaniel Vetter 	if ((iir & flip_pending) == 0)
41655a21b665SDaniel Vetter 		goto check_page_flip;
41665a21b665SDaniel Vetter 
41675a21b665SDaniel Vetter 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
41685a21b665SDaniel Vetter 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
41695a21b665SDaniel Vetter 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
41705a21b665SDaniel Vetter 	 * the flip is completed (no longer pending). Since this doesn't raise
41715a21b665SDaniel Vetter 	 * an interrupt per se, we watch for the change at vblank.
41725a21b665SDaniel Vetter 	 */
41735a21b665SDaniel Vetter 	if (I915_READ(ISR) & flip_pending)
41745a21b665SDaniel Vetter 		goto check_page_flip;
41755a21b665SDaniel Vetter 
41765a21b665SDaniel Vetter 	intel_finish_page_flip_cs(dev_priv, pipe);
41775a21b665SDaniel Vetter 	return true;
41785a21b665SDaniel Vetter 
41795a21b665SDaniel Vetter check_page_flip:
41805a21b665SDaniel Vetter 	intel_check_page_flip(dev_priv, pipe);
41815a21b665SDaniel Vetter 	return false;
41825a21b665SDaniel Vetter }
41835a21b665SDaniel Vetter 
4184ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
4185a266c7d5SChris Wilson {
418645a83f84SDaniel Vetter 	struct drm_device *dev = arg;
4187fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
41888291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
418938bde180SChris Wilson 	u32 flip_mask =
419038bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
419138bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
419238bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
4193a266c7d5SChris Wilson 
41942dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
41952dd2a883SImre Deak 		return IRQ_NONE;
41962dd2a883SImre Deak 
41971f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
41981f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
41991f814dacSImre Deak 
4200a266c7d5SChris Wilson 	iir = I915_READ(IIR);
420138bde180SChris Wilson 	do {
420238bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
42038291ee90SChris Wilson 		bool blc_event = false;
4204a266c7d5SChris Wilson 
4205a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4206a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4207a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4208a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
4209a266c7d5SChris Wilson 		 */
4210222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
4211a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4212aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4213a266c7d5SChris Wilson 
4214055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4215f0f59a00SVille Syrjälä 			i915_reg_t reg = PIPESTAT(pipe);
4216a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4217a266c7d5SChris Wilson 
421838bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
4219a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4220a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
422138bde180SChris Wilson 				irq_received = true;
4222a266c7d5SChris Wilson 			}
4223a266c7d5SChris Wilson 		}
4224222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4225a266c7d5SChris Wilson 
4226a266c7d5SChris Wilson 		if (!irq_received)
4227a266c7d5SChris Wilson 			break;
4228a266c7d5SChris Wilson 
4229a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
423091d14251STvrtko Ursulin 		if (I915_HAS_HOTPLUG(dev_priv) &&
42311ae3c34cSVille Syrjälä 		    iir & I915_DISPLAY_PORT_INTERRUPT) {
42321ae3c34cSVille Syrjälä 			u32 hotplug_status = i9xx_hpd_irq_ack(dev_priv);
42331ae3c34cSVille Syrjälä 			if (hotplug_status)
423491d14251STvrtko Ursulin 				i9xx_hpd_irq_handler(dev_priv, hotplug_status);
42351ae3c34cSVille Syrjälä 		}
4236a266c7d5SChris Wilson 
423738bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
4238a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4239a266c7d5SChris Wilson 
4240a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
42413b3f1650SAkash Goel 			notify_ring(dev_priv->engine[RCS]);
4242a266c7d5SChris Wilson 
4243055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
42445a21b665SDaniel Vetter 			int plane = pipe;
42455a21b665SDaniel Vetter 			if (HAS_FBC(dev_priv))
42465a21b665SDaniel Vetter 				plane = !plane;
42475a21b665SDaniel Vetter 
42485a21b665SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
42495a21b665SDaniel Vetter 			    i915_handle_vblank(dev_priv, plane, pipe, iir))
42505a21b665SDaniel Vetter 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
4251a266c7d5SChris Wilson 
4252a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4253a266c7d5SChris Wilson 				blc_event = true;
42544356d586SDaniel Vetter 
42554356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
425691d14251STvrtko Ursulin 				i9xx_pipe_crc_irq_handler(dev_priv, pipe);
42572d9d2b0bSVille Syrjälä 
42581f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
42591f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
42601f7247c0SDaniel Vetter 								    pipe);
4261a266c7d5SChris Wilson 		}
4262a266c7d5SChris Wilson 
4263a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
426491d14251STvrtko Ursulin 			intel_opregion_asle_intr(dev_priv);
4265a266c7d5SChris Wilson 
4266a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4267a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4268a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4269a266c7d5SChris Wilson 		 * we would never get another interrupt.
4270a266c7d5SChris Wilson 		 *
4271a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4272a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4273a266c7d5SChris Wilson 		 * another one.
4274a266c7d5SChris Wilson 		 *
4275a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4276a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4277a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4278a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4279a266c7d5SChris Wilson 		 * stray interrupts.
4280a266c7d5SChris Wilson 		 */
428138bde180SChris Wilson 		ret = IRQ_HANDLED;
4282a266c7d5SChris Wilson 		iir = new_iir;
428338bde180SChris Wilson 	} while (iir & ~flip_mask);
4284a266c7d5SChris Wilson 
42851f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
42861f814dacSImre Deak 
4287a266c7d5SChris Wilson 	return ret;
4288a266c7d5SChris Wilson }
4289a266c7d5SChris Wilson 
4290a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
4291a266c7d5SChris Wilson {
4292fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4293a266c7d5SChris Wilson 	int pipe;
4294a266c7d5SChris Wilson 
4295a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
42960706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4297a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4298a266c7d5SChris Wilson 	}
4299a266c7d5SChris Wilson 
430000d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
4301055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
430255b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
4303a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
430455b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
430555b39755SChris Wilson 	}
4306a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4307a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4308a266c7d5SChris Wilson 
4309a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4310a266c7d5SChris Wilson }
4311a266c7d5SChris Wilson 
4312a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
4313a266c7d5SChris Wilson {
4314fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4315a266c7d5SChris Wilson 	int pipe;
4316a266c7d5SChris Wilson 
43170706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4318a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4319a266c7d5SChris Wilson 
4320a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
4321055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4322a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4323a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4324a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4325a266c7d5SChris Wilson 	POSTING_READ(IER);
4326a266c7d5SChris Wilson }
4327a266c7d5SChris Wilson 
4328a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
4329a266c7d5SChris Wilson {
4330fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4331bbba0a97SChris Wilson 	u32 enable_mask;
4332a266c7d5SChris Wilson 	u32 error_mask;
4333a266c7d5SChris Wilson 
4334a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
4335bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
4336adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
4337bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4338bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
4339bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
4340bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
4341bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
4342bbba0a97SChris Wilson 
4343bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
434421ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
434521ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
4346bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
4347bbba0a97SChris Wilson 
434891d14251STvrtko Ursulin 	if (IS_G4X(dev_priv))
4349bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
4350a266c7d5SChris Wilson 
4351b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4352b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4353d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4354755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
4355755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4356755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4357d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4358a266c7d5SChris Wilson 
4359a266c7d5SChris Wilson 	/*
4360a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
4361a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
4362a266c7d5SChris Wilson 	 */
436391d14251STvrtko Ursulin 	if (IS_G4X(dev_priv)) {
4364a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
4365a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
4366a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
4367a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4368a266c7d5SChris Wilson 	} else {
4369a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
4370a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4371a266c7d5SChris Wilson 	}
4372a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
4373a266c7d5SChris Wilson 
4374a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
4375a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
4376a266c7d5SChris Wilson 	POSTING_READ(IER);
4377a266c7d5SChris Wilson 
43780706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
437920afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
438020afbda2SDaniel Vetter 
438191d14251STvrtko Ursulin 	i915_enable_asle_pipestat(dev_priv);
438220afbda2SDaniel Vetter 
438320afbda2SDaniel Vetter 	return 0;
438420afbda2SDaniel Vetter }
438520afbda2SDaniel Vetter 
438691d14251STvrtko Ursulin static void i915_hpd_irq_setup(struct drm_i915_private *dev_priv)
438720afbda2SDaniel Vetter {
438820afbda2SDaniel Vetter 	u32 hotplug_en;
438920afbda2SDaniel Vetter 
4390b5ea2d56SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
4391b5ea2d56SDaniel Vetter 
4392adca4730SChris Wilson 	/* Note HDMI and DP share hotplug bits */
4393e5868a31SEgbert Eich 	/* enable bits are the same for all generations */
439491d14251STvrtko Ursulin 	hotplug_en = intel_hpd_enabled_irqs(dev_priv, hpd_mask_i915);
4395a266c7d5SChris Wilson 	/* Programming the CRT detection parameters tends
4396a266c7d5SChris Wilson 	   to generate a spurious hotplug event about three
4397a266c7d5SChris Wilson 	   seconds later.  So just do it once.
4398a266c7d5SChris Wilson 	*/
439991d14251STvrtko Ursulin 	if (IS_G4X(dev_priv))
4400a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
4401a266c7d5SChris Wilson 	hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
4402a266c7d5SChris Wilson 
4403a266c7d5SChris Wilson 	/* Ignore TV since it's buggy */
44040706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv,
4405f9e3dc78SJani Nikula 					     HOTPLUG_INT_EN_MASK |
4406f9e3dc78SJani Nikula 					     CRT_HOTPLUG_VOLTAGE_COMPARE_MASK |
4407f9e3dc78SJani Nikula 					     CRT_HOTPLUG_ACTIVATION_PERIOD_64,
44080706f17cSEgbert Eich 					     hotplug_en);
4409a266c7d5SChris Wilson }
4410a266c7d5SChris Wilson 
4411ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
4412a266c7d5SChris Wilson {
441345a83f84SDaniel Vetter 	struct drm_device *dev = arg;
4414fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4415a266c7d5SChris Wilson 	u32 iir, new_iir;
4416a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
4417a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
441821ad8330SVille Syrjälä 	u32 flip_mask =
441921ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
442021ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
4421a266c7d5SChris Wilson 
44222dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
44232dd2a883SImre Deak 		return IRQ_NONE;
44242dd2a883SImre Deak 
44251f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
44261f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
44271f814dacSImre Deak 
4428a266c7d5SChris Wilson 	iir = I915_READ(IIR);
4429a266c7d5SChris Wilson 
4430a266c7d5SChris Wilson 	for (;;) {
4431501e01d7SVille Syrjälä 		bool irq_received = (iir & ~flip_mask) != 0;
44322c8ba29fSChris Wilson 		bool blc_event = false;
44332c8ba29fSChris Wilson 
4434a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4435a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4436a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4437a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
4438a266c7d5SChris Wilson 		 */
4439222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
4440a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4441aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4442a266c7d5SChris Wilson 
4443055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4444f0f59a00SVille Syrjälä 			i915_reg_t reg = PIPESTAT(pipe);
4445a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4446a266c7d5SChris Wilson 
4447a266c7d5SChris Wilson 			/*
4448a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
4449a266c7d5SChris Wilson 			 */
4450a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4451a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
4452501e01d7SVille Syrjälä 				irq_received = true;
4453a266c7d5SChris Wilson 			}
4454a266c7d5SChris Wilson 		}
4455222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4456a266c7d5SChris Wilson 
4457a266c7d5SChris Wilson 		if (!irq_received)
4458a266c7d5SChris Wilson 			break;
4459a266c7d5SChris Wilson 
4460a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
4461a266c7d5SChris Wilson 
4462a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
44631ae3c34cSVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
44641ae3c34cSVille Syrjälä 			u32 hotplug_status = i9xx_hpd_irq_ack(dev_priv);
44651ae3c34cSVille Syrjälä 			if (hotplug_status)
446691d14251STvrtko Ursulin 				i9xx_hpd_irq_handler(dev_priv, hotplug_status);
44671ae3c34cSVille Syrjälä 		}
4468a266c7d5SChris Wilson 
446921ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
4470a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4471a266c7d5SChris Wilson 
4472a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
44733b3f1650SAkash Goel 			notify_ring(dev_priv->engine[RCS]);
4474a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
44753b3f1650SAkash Goel 			notify_ring(dev_priv->engine[VCS]);
4476a266c7d5SChris Wilson 
4477055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
44785a21b665SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
44795a21b665SDaniel Vetter 			    i915_handle_vblank(dev_priv, pipe, pipe, iir))
44805a21b665SDaniel Vetter 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
4481a266c7d5SChris Wilson 
4482a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4483a266c7d5SChris Wilson 				blc_event = true;
44844356d586SDaniel Vetter 
44854356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
448691d14251STvrtko Ursulin 				i9xx_pipe_crc_irq_handler(dev_priv, pipe);
4487a266c7d5SChris Wilson 
44881f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
44891f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
44902d9d2b0bSVille Syrjälä 		}
4491a266c7d5SChris Wilson 
4492a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
449391d14251STvrtko Ursulin 			intel_opregion_asle_intr(dev_priv);
4494a266c7d5SChris Wilson 
4495515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
449691d14251STvrtko Ursulin 			gmbus_irq_handler(dev_priv);
4497515ac2bbSDaniel Vetter 
4498a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4499a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4500a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4501a266c7d5SChris Wilson 		 * we would never get another interrupt.
4502a266c7d5SChris Wilson 		 *
4503a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4504a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4505a266c7d5SChris Wilson 		 * another one.
4506a266c7d5SChris Wilson 		 *
4507a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4508a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4509a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4510a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4511a266c7d5SChris Wilson 		 * stray interrupts.
4512a266c7d5SChris Wilson 		 */
4513a266c7d5SChris Wilson 		iir = new_iir;
4514a266c7d5SChris Wilson 	}
4515a266c7d5SChris Wilson 
45161f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
45171f814dacSImre Deak 
4518a266c7d5SChris Wilson 	return ret;
4519a266c7d5SChris Wilson }
4520a266c7d5SChris Wilson 
4521a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
4522a266c7d5SChris Wilson {
4523fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4524a266c7d5SChris Wilson 	int pipe;
4525a266c7d5SChris Wilson 
4526a266c7d5SChris Wilson 	if (!dev_priv)
4527a266c7d5SChris Wilson 		return;
4528a266c7d5SChris Wilson 
45290706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4530a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4531a266c7d5SChris Wilson 
4532a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
4533055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4534a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4535a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4536a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4537a266c7d5SChris Wilson 
4538055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4539a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
4540a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
4541a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4542a266c7d5SChris Wilson }
4543a266c7d5SChris Wilson 
4544fca52a55SDaniel Vetter /**
4545fca52a55SDaniel Vetter  * intel_irq_init - initializes irq support
4546fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4547fca52a55SDaniel Vetter  *
4548fca52a55SDaniel Vetter  * This function initializes all the irq support including work items, timers
4549fca52a55SDaniel Vetter  * and all the vtables. It does not setup the interrupt itself though.
4550fca52a55SDaniel Vetter  */
4551b963291cSDaniel Vetter void intel_irq_init(struct drm_i915_private *dev_priv)
4552f71d4af4SJesse Barnes {
455391c8a326SChris Wilson 	struct drm_device *dev = &dev_priv->drm;
45548b2e326dSChris Wilson 
455577913b39SJani Nikula 	intel_hpd_init_work(dev_priv);
455677913b39SJani Nikula 
4557c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
4558a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
45598b2e326dSChris Wilson 
456026705e20SSagar Arun Kamble 	if (HAS_GUC_SCHED(dev))
456126705e20SSagar Arun Kamble 		dev_priv->pm_guc_events = GEN9_GUC_TO_HOST_INT_EVENT;
456226705e20SSagar Arun Kamble 
4563a6706b45SDeepak S 	/* Let's track the enabled rps events */
4564666a4537SWayne Boyer 	if (IS_VALLEYVIEW(dev_priv))
45656c65a587SVille Syrjälä 		/* WaGsvRC0ResidencyMethod:vlv */
45666f4b12f8SChris Wilson 		dev_priv->pm_rps_events = GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED;
456731685c25SDeepak S 	else
4568a6706b45SDeepak S 		dev_priv->pm_rps_events = GEN6_PM_RPS_EVENTS;
4569a6706b45SDeepak S 
45701800ad25SSagar Arun Kamble 	dev_priv->rps.pm_intr_keep = 0;
45711800ad25SSagar Arun Kamble 
45721800ad25SSagar Arun Kamble 	/*
45731800ad25SSagar Arun Kamble 	 * SNB,IVB can while VLV,CHV may hard hang on looping batchbuffer
45741800ad25SSagar Arun Kamble 	 * if GEN6_PM_UP_EI_EXPIRED is masked.
45751800ad25SSagar Arun Kamble 	 *
45761800ad25SSagar Arun Kamble 	 * TODO: verify if this can be reproduced on VLV,CHV.
45771800ad25SSagar Arun Kamble 	 */
45781800ad25SSagar Arun Kamble 	if (INTEL_INFO(dev_priv)->gen <= 7 && !IS_HASWELL(dev_priv))
45791800ad25SSagar Arun Kamble 		dev_priv->rps.pm_intr_keep |= GEN6_PM_RP_UP_EI_EXPIRED;
45801800ad25SSagar Arun Kamble 
45811800ad25SSagar Arun Kamble 	if (INTEL_INFO(dev_priv)->gen >= 8)
4582b20e3cfeSDave Gordon 		dev_priv->rps.pm_intr_keep |= GEN8_PMINTR_REDIRECT_TO_GUC;
45831800ad25SSagar Arun Kamble 
4584737b1506SChris Wilson 	INIT_DELAYED_WORK(&dev_priv->gpu_error.hangcheck_work,
4585737b1506SChris Wilson 			  i915_hangcheck_elapsed);
458661bac78eSDaniel Vetter 
4587b963291cSDaniel Vetter 	if (IS_GEN2(dev_priv)) {
45884194c088SRodrigo Vivi 		/* Gen2 doesn't have a hardware frame counter */
45894cdb83ecSVille Syrjälä 		dev->max_vblank_count = 0;
45904194c088SRodrigo Vivi 		dev->driver->get_vblank_counter = drm_vblank_no_hw_counter;
4591b963291cSDaniel Vetter 	} else if (IS_G4X(dev_priv) || INTEL_INFO(dev_priv)->gen >= 5) {
4592f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
4593fd8f507cSVille Syrjälä 		dev->driver->get_vblank_counter = g4x_get_vblank_counter;
4594391f75e2SVille Syrjälä 	} else {
4595391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
4596391f75e2SVille Syrjälä 		dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
4597f71d4af4SJesse Barnes 	}
4598f71d4af4SJesse Barnes 
459921da2700SVille Syrjälä 	/*
460021da2700SVille Syrjälä 	 * Opt out of the vblank disable timer on everything except gen2.
460121da2700SVille Syrjälä 	 * Gen2 doesn't have a hardware frame counter and so depends on
460221da2700SVille Syrjälä 	 * vblank interrupts to produce sane vblank seuquence numbers.
460321da2700SVille Syrjälä 	 */
4604b963291cSDaniel Vetter 	if (!IS_GEN2(dev_priv))
460521da2700SVille Syrjälä 		dev->vblank_disable_immediate = true;
460621da2700SVille Syrjälä 
4607f71d4af4SJesse Barnes 	dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
4608f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
4609f71d4af4SJesse Barnes 
4610b963291cSDaniel Vetter 	if (IS_CHERRYVIEW(dev_priv)) {
461143f328d7SVille Syrjälä 		dev->driver->irq_handler = cherryview_irq_handler;
461243f328d7SVille Syrjälä 		dev->driver->irq_preinstall = cherryview_irq_preinstall;
461343f328d7SVille Syrjälä 		dev->driver->irq_postinstall = cherryview_irq_postinstall;
461443f328d7SVille Syrjälä 		dev->driver->irq_uninstall = cherryview_irq_uninstall;
461586e83e35SChris Wilson 		dev->driver->enable_vblank = i965_enable_vblank;
461686e83e35SChris Wilson 		dev->driver->disable_vblank = i965_disable_vblank;
461743f328d7SVille Syrjälä 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4618b963291cSDaniel Vetter 	} else if (IS_VALLEYVIEW(dev_priv)) {
46197e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
46207e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
46217e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
46227e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
462386e83e35SChris Wilson 		dev->driver->enable_vblank = i965_enable_vblank;
462486e83e35SChris Wilson 		dev->driver->disable_vblank = i965_disable_vblank;
4625fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4626b963291cSDaniel Vetter 	} else if (INTEL_INFO(dev_priv)->gen >= 8) {
4627abd58f01SBen Widawsky 		dev->driver->irq_handler = gen8_irq_handler;
4628723761b8SDaniel Vetter 		dev->driver->irq_preinstall = gen8_irq_reset;
4629abd58f01SBen Widawsky 		dev->driver->irq_postinstall = gen8_irq_postinstall;
4630abd58f01SBen Widawsky 		dev->driver->irq_uninstall = gen8_irq_uninstall;
4631abd58f01SBen Widawsky 		dev->driver->enable_vblank = gen8_enable_vblank;
4632abd58f01SBen Widawsky 		dev->driver->disable_vblank = gen8_disable_vblank;
4633e2d214aeSTvrtko Ursulin 		if (IS_BROXTON(dev_priv))
4634e0a20ad7SShashank Sharma 			dev_priv->display.hpd_irq_setup = bxt_hpd_irq_setup;
46356e266956STvrtko Ursulin 		else if (HAS_PCH_SPT(dev_priv) || HAS_PCH_KBP(dev_priv))
46366dbf30ceSVille Syrjälä 			dev_priv->display.hpd_irq_setup = spt_hpd_irq_setup;
46376dbf30ceSVille Syrjälä 		else
46383a3b3c7dSVille Syrjälä 			dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
46396e266956STvrtko Ursulin 	} else if (HAS_PCH_SPLIT(dev_priv)) {
4640f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
4641723761b8SDaniel Vetter 		dev->driver->irq_preinstall = ironlake_irq_reset;
4642f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
4643f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
4644f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
4645f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
4646e4ce95aaSVille Syrjälä 		dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
4647f71d4af4SJesse Barnes 	} else {
46487e22dbbbSTvrtko Ursulin 		if (IS_GEN2(dev_priv)) {
4649c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
4650c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
4651c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
4652c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
465386e83e35SChris Wilson 			dev->driver->enable_vblank = i8xx_enable_vblank;
465486e83e35SChris Wilson 			dev->driver->disable_vblank = i8xx_disable_vblank;
46557e22dbbbSTvrtko Ursulin 		} else if (IS_GEN3(dev_priv)) {
4656a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
4657a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
4658a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
4659a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
466086e83e35SChris Wilson 			dev->driver->enable_vblank = i8xx_enable_vblank;
466186e83e35SChris Wilson 			dev->driver->disable_vblank = i8xx_disable_vblank;
4662c2798b19SChris Wilson 		} else {
4663a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
4664a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
4665a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
4666a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
466786e83e35SChris Wilson 			dev->driver->enable_vblank = i965_enable_vblank;
466886e83e35SChris Wilson 			dev->driver->disable_vblank = i965_disable_vblank;
4669c2798b19SChris Wilson 		}
4670778eb334SVille Syrjälä 		if (I915_HAS_HOTPLUG(dev_priv))
4671778eb334SVille Syrjälä 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4672f71d4af4SJesse Barnes 	}
4673f71d4af4SJesse Barnes }
467420afbda2SDaniel Vetter 
4675fca52a55SDaniel Vetter /**
4676fca52a55SDaniel Vetter  * intel_irq_install - enables the hardware interrupt
4677fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4678fca52a55SDaniel Vetter  *
4679fca52a55SDaniel Vetter  * This function enables the hardware interrupt handling, but leaves the hotplug
4680fca52a55SDaniel Vetter  * handling still disabled. It is called after intel_irq_init().
4681fca52a55SDaniel Vetter  *
4682fca52a55SDaniel Vetter  * In the driver load and resume code we need working interrupts in a few places
4683fca52a55SDaniel Vetter  * but don't want to deal with the hassle of concurrent probe and hotplug
4684fca52a55SDaniel Vetter  * workers. Hence the split into this two-stage approach.
4685fca52a55SDaniel Vetter  */
46862aeb7d3aSDaniel Vetter int intel_irq_install(struct drm_i915_private *dev_priv)
46872aeb7d3aSDaniel Vetter {
46882aeb7d3aSDaniel Vetter 	/*
46892aeb7d3aSDaniel Vetter 	 * We enable some interrupt sources in our postinstall hooks, so mark
46902aeb7d3aSDaniel Vetter 	 * interrupts as enabled _before_ actually enabling them to avoid
46912aeb7d3aSDaniel Vetter 	 * special cases in our ordering checks.
46922aeb7d3aSDaniel Vetter 	 */
46932aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
46942aeb7d3aSDaniel Vetter 
469591c8a326SChris Wilson 	return drm_irq_install(&dev_priv->drm, dev_priv->drm.pdev->irq);
46962aeb7d3aSDaniel Vetter }
46972aeb7d3aSDaniel Vetter 
4698fca52a55SDaniel Vetter /**
4699fca52a55SDaniel Vetter  * intel_irq_uninstall - finilizes all irq handling
4700fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4701fca52a55SDaniel Vetter  *
4702fca52a55SDaniel Vetter  * This stops interrupt and hotplug handling and unregisters and frees all
4703fca52a55SDaniel Vetter  * resources acquired in the init functions.
4704fca52a55SDaniel Vetter  */
47052aeb7d3aSDaniel Vetter void intel_irq_uninstall(struct drm_i915_private *dev_priv)
47062aeb7d3aSDaniel Vetter {
470791c8a326SChris Wilson 	drm_irq_uninstall(&dev_priv->drm);
47082aeb7d3aSDaniel Vetter 	intel_hpd_cancel_work(dev_priv);
47092aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
47102aeb7d3aSDaniel Vetter }
47112aeb7d3aSDaniel Vetter 
4712fca52a55SDaniel Vetter /**
4713fca52a55SDaniel Vetter  * intel_runtime_pm_disable_interrupts - runtime interrupt disabling
4714fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4715fca52a55SDaniel Vetter  *
4716fca52a55SDaniel Vetter  * This function is used to disable interrupts at runtime, both in the runtime
4717fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4718fca52a55SDaniel Vetter  */
4719b963291cSDaniel Vetter void intel_runtime_pm_disable_interrupts(struct drm_i915_private *dev_priv)
4720c67a470bSPaulo Zanoni {
472191c8a326SChris Wilson 	dev_priv->drm.driver->irq_uninstall(&dev_priv->drm);
47222aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
472391c8a326SChris Wilson 	synchronize_irq(dev_priv->drm.irq);
4724c67a470bSPaulo Zanoni }
4725c67a470bSPaulo Zanoni 
4726fca52a55SDaniel Vetter /**
4727fca52a55SDaniel Vetter  * intel_runtime_pm_enable_interrupts - runtime interrupt enabling
4728fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4729fca52a55SDaniel Vetter  *
4730fca52a55SDaniel Vetter  * This function is used to enable interrupts at runtime, both in the runtime
4731fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4732fca52a55SDaniel Vetter  */
4733b963291cSDaniel Vetter void intel_runtime_pm_enable_interrupts(struct drm_i915_private *dev_priv)
4734c67a470bSPaulo Zanoni {
47352aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
473691c8a326SChris Wilson 	dev_priv->drm.driver->irq_preinstall(&dev_priv->drm);
473791c8a326SChris Wilson 	dev_priv->drm.driver->irq_postinstall(&dev_priv->drm);
4738c67a470bSPaulo Zanoni }
4739