xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 1f15b76f1ec973d1eb5d21b6d98b21aebb9025f1)
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);
173c9a9a268SImre Deak 
1740706f17cSEgbert Eich /* For display hotplug interrupt */
1750706f17cSEgbert Eich static inline void
1760706f17cSEgbert Eich i915_hotplug_interrupt_update_locked(struct drm_i915_private *dev_priv,
1770706f17cSEgbert Eich 				     uint32_t mask,
1780706f17cSEgbert Eich 				     uint32_t bits)
1790706f17cSEgbert Eich {
1800706f17cSEgbert Eich 	uint32_t val;
1810706f17cSEgbert Eich 
1820706f17cSEgbert Eich 	assert_spin_locked(&dev_priv->irq_lock);
1830706f17cSEgbert Eich 	WARN_ON(bits & ~mask);
1840706f17cSEgbert Eich 
1850706f17cSEgbert Eich 	val = I915_READ(PORT_HOTPLUG_EN);
1860706f17cSEgbert Eich 	val &= ~mask;
1870706f17cSEgbert Eich 	val |= bits;
1880706f17cSEgbert Eich 	I915_WRITE(PORT_HOTPLUG_EN, val);
1890706f17cSEgbert Eich }
1900706f17cSEgbert Eich 
1910706f17cSEgbert Eich /**
1920706f17cSEgbert Eich  * i915_hotplug_interrupt_update - update hotplug interrupt enable
1930706f17cSEgbert Eich  * @dev_priv: driver private
1940706f17cSEgbert Eich  * @mask: bits to update
1950706f17cSEgbert Eich  * @bits: bits to enable
1960706f17cSEgbert Eich  * NOTE: the HPD enable bits are modified both inside and outside
1970706f17cSEgbert Eich  * of an interrupt context. To avoid that read-modify-write cycles
1980706f17cSEgbert Eich  * interfer, these bits are protected by a spinlock. Since this
1990706f17cSEgbert Eich  * function is usually not called from a context where the lock is
2000706f17cSEgbert Eich  * held already, this function acquires the lock itself. A non-locking
2010706f17cSEgbert Eich  * version is also available.
2020706f17cSEgbert Eich  */
2030706f17cSEgbert Eich void i915_hotplug_interrupt_update(struct drm_i915_private *dev_priv,
2040706f17cSEgbert Eich 				   uint32_t mask,
2050706f17cSEgbert Eich 				   uint32_t bits)
2060706f17cSEgbert Eich {
2070706f17cSEgbert Eich 	spin_lock_irq(&dev_priv->irq_lock);
2080706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv, mask, bits);
2090706f17cSEgbert Eich 	spin_unlock_irq(&dev_priv->irq_lock);
2100706f17cSEgbert Eich }
2110706f17cSEgbert Eich 
212d9dc34f1SVille Syrjälä /**
213d9dc34f1SVille Syrjälä  * ilk_update_display_irq - update DEIMR
214d9dc34f1SVille Syrjälä  * @dev_priv: driver private
215d9dc34f1SVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
216d9dc34f1SVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
217d9dc34f1SVille Syrjälä  */
218fbdedaeaSVille Syrjälä void ilk_update_display_irq(struct drm_i915_private *dev_priv,
219d9dc34f1SVille Syrjälä 			    uint32_t interrupt_mask,
220d9dc34f1SVille Syrjälä 			    uint32_t enabled_irq_mask)
221036a4a7dSZhenyu Wang {
222d9dc34f1SVille Syrjälä 	uint32_t new_val;
223d9dc34f1SVille Syrjälä 
2244bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
2254bc9d430SDaniel Vetter 
226d9dc34f1SVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
227d9dc34f1SVille Syrjälä 
2289df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
229c67a470bSPaulo Zanoni 		return;
230c67a470bSPaulo Zanoni 
231d9dc34f1SVille Syrjälä 	new_val = dev_priv->irq_mask;
232d9dc34f1SVille Syrjälä 	new_val &= ~interrupt_mask;
233d9dc34f1SVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
234d9dc34f1SVille Syrjälä 
235d9dc34f1SVille Syrjälä 	if (new_val != dev_priv->irq_mask) {
236d9dc34f1SVille Syrjälä 		dev_priv->irq_mask = new_val;
2371ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
2383143a2bfSChris Wilson 		POSTING_READ(DEIMR);
239036a4a7dSZhenyu Wang 	}
240036a4a7dSZhenyu Wang }
241036a4a7dSZhenyu Wang 
24243eaea13SPaulo Zanoni /**
24343eaea13SPaulo Zanoni  * ilk_update_gt_irq - update GTIMR
24443eaea13SPaulo Zanoni  * @dev_priv: driver private
24543eaea13SPaulo Zanoni  * @interrupt_mask: mask of interrupt bits to update
24643eaea13SPaulo Zanoni  * @enabled_irq_mask: mask of interrupt bits to enable
24743eaea13SPaulo Zanoni  */
24843eaea13SPaulo Zanoni static void ilk_update_gt_irq(struct drm_i915_private *dev_priv,
24943eaea13SPaulo Zanoni 			      uint32_t interrupt_mask,
25043eaea13SPaulo Zanoni 			      uint32_t enabled_irq_mask)
25143eaea13SPaulo Zanoni {
25243eaea13SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
25343eaea13SPaulo Zanoni 
25415a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
25515a17aaeSDaniel Vetter 
2569df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
257c67a470bSPaulo Zanoni 		return;
258c67a470bSPaulo Zanoni 
25943eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask &= ~interrupt_mask;
26043eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask |= (~enabled_irq_mask & interrupt_mask);
26143eaea13SPaulo Zanoni 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
26243eaea13SPaulo Zanoni 	POSTING_READ(GTIMR);
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);
26843eaea13SPaulo Zanoni }
26943eaea13SPaulo Zanoni 
270480c8033SDaniel Vetter void gen5_disable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
27143eaea13SPaulo Zanoni {
27243eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, 0);
27343eaea13SPaulo Zanoni }
27443eaea13SPaulo Zanoni 
275f0f59a00SVille Syrjälä static i915_reg_t gen6_pm_iir(struct drm_i915_private *dev_priv)
276b900b949SImre Deak {
277b900b949SImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IIR(2) : GEN6_PMIIR;
278b900b949SImre Deak }
279b900b949SImre Deak 
280f0f59a00SVille Syrjälä static i915_reg_t gen6_pm_imr(struct drm_i915_private *dev_priv)
281a72fbc3aSImre Deak {
282a72fbc3aSImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IMR(2) : GEN6_PMIMR;
283a72fbc3aSImre Deak }
284a72fbc3aSImre Deak 
285f0f59a00SVille Syrjälä static i915_reg_t gen6_pm_ier(struct drm_i915_private *dev_priv)
286b900b949SImre Deak {
287b900b949SImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IER(2) : GEN6_PMIER;
288b900b949SImre Deak }
289b900b949SImre Deak 
290edbfdb45SPaulo Zanoni /**
291edbfdb45SPaulo Zanoni  * snb_update_pm_irq - update GEN6_PMIMR
292edbfdb45SPaulo Zanoni  * @dev_priv: driver private
293edbfdb45SPaulo Zanoni  * @interrupt_mask: mask of interrupt bits to update
294edbfdb45SPaulo Zanoni  * @enabled_irq_mask: mask of interrupt bits to enable
295edbfdb45SPaulo Zanoni  */
296edbfdb45SPaulo Zanoni static void snb_update_pm_irq(struct drm_i915_private *dev_priv,
297edbfdb45SPaulo Zanoni 			      uint32_t interrupt_mask,
298edbfdb45SPaulo Zanoni 			      uint32_t enabled_irq_mask)
299edbfdb45SPaulo Zanoni {
300605cd25bSPaulo Zanoni 	uint32_t new_val;
301edbfdb45SPaulo Zanoni 
30215a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
30315a17aaeSDaniel Vetter 
304edbfdb45SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
305edbfdb45SPaulo Zanoni 
306605cd25bSPaulo Zanoni 	new_val = dev_priv->pm_irq_mask;
307f52ecbcfSPaulo Zanoni 	new_val &= ~interrupt_mask;
308f52ecbcfSPaulo Zanoni 	new_val |= (~enabled_irq_mask & interrupt_mask);
309f52ecbcfSPaulo Zanoni 
310605cd25bSPaulo Zanoni 	if (new_val != dev_priv->pm_irq_mask) {
311605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = new_val;
312a72fbc3aSImre Deak 		I915_WRITE(gen6_pm_imr(dev_priv), dev_priv->pm_irq_mask);
313a72fbc3aSImre Deak 		POSTING_READ(gen6_pm_imr(dev_priv));
314edbfdb45SPaulo Zanoni 	}
315f52ecbcfSPaulo Zanoni }
316edbfdb45SPaulo Zanoni 
317480c8033SDaniel Vetter void gen6_enable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
318edbfdb45SPaulo Zanoni {
3199939fba2SImre Deak 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
3209939fba2SImre Deak 		return;
3219939fba2SImre Deak 
322edbfdb45SPaulo Zanoni 	snb_update_pm_irq(dev_priv, mask, mask);
323edbfdb45SPaulo Zanoni }
324edbfdb45SPaulo Zanoni 
3259939fba2SImre Deak static void __gen6_disable_pm_irq(struct drm_i915_private *dev_priv,
3269939fba2SImre Deak 				  uint32_t mask)
3279939fba2SImre Deak {
3289939fba2SImre Deak 	snb_update_pm_irq(dev_priv, mask, 0);
3299939fba2SImre Deak }
3309939fba2SImre Deak 
331480c8033SDaniel Vetter void gen6_disable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
332edbfdb45SPaulo Zanoni {
3339939fba2SImre Deak 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
3349939fba2SImre Deak 		return;
3359939fba2SImre Deak 
3369939fba2SImre Deak 	__gen6_disable_pm_irq(dev_priv, mask);
337edbfdb45SPaulo Zanoni }
338edbfdb45SPaulo Zanoni 
339dc97997aSChris Wilson void gen6_reset_rps_interrupts(struct drm_i915_private *dev_priv)
3403cc134e3SImre Deak {
341f0f59a00SVille Syrjälä 	i915_reg_t reg = gen6_pm_iir(dev_priv);
3423cc134e3SImre Deak 
3433cc134e3SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
3443cc134e3SImre Deak 	I915_WRITE(reg, dev_priv->pm_rps_events);
3453cc134e3SImre Deak 	I915_WRITE(reg, dev_priv->pm_rps_events);
3463cc134e3SImre Deak 	POSTING_READ(reg);
347096fad9eSImre Deak 	dev_priv->rps.pm_iir = 0;
3483cc134e3SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
3493cc134e3SImre Deak }
3503cc134e3SImre Deak 
35191d14251STvrtko Ursulin void gen6_enable_rps_interrupts(struct drm_i915_private *dev_priv)
352b900b949SImre Deak {
353b900b949SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
35478e68d36SImre Deak 
355b900b949SImre Deak 	WARN_ON(dev_priv->rps.pm_iir);
3563cc134e3SImre Deak 	WARN_ON(I915_READ(gen6_pm_iir(dev_priv)) & dev_priv->pm_rps_events);
357d4d70aa5SImre Deak 	dev_priv->rps.interrupts_enabled = true;
35878e68d36SImre Deak 	I915_WRITE(gen6_pm_ier(dev_priv), I915_READ(gen6_pm_ier(dev_priv)) |
35978e68d36SImre Deak 				dev_priv->pm_rps_events);
360b900b949SImre Deak 	gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
36178e68d36SImre Deak 
362b900b949SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
363b900b949SImre Deak }
364b900b949SImre Deak 
36559d02a1fSImre Deak u32 gen6_sanitize_rps_pm_mask(struct drm_i915_private *dev_priv, u32 mask)
36659d02a1fSImre Deak {
3671800ad25SSagar Arun Kamble 	return (mask & ~dev_priv->rps.pm_intr_keep);
36859d02a1fSImre Deak }
36959d02a1fSImre Deak 
37091d14251STvrtko Ursulin void gen6_disable_rps_interrupts(struct drm_i915_private *dev_priv)
371b900b949SImre Deak {
372d4d70aa5SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
373d4d70aa5SImre Deak 	dev_priv->rps.interrupts_enabled = false;
374d4d70aa5SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
375d4d70aa5SImre Deak 
376d4d70aa5SImre Deak 	cancel_work_sync(&dev_priv->rps.work);
377d4d70aa5SImre Deak 
3789939fba2SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
3799939fba2SImre Deak 
38059d02a1fSImre Deak 	I915_WRITE(GEN6_PMINTRMSK, gen6_sanitize_rps_pm_mask(dev_priv, ~0));
3819939fba2SImre Deak 
3829939fba2SImre Deak 	__gen6_disable_pm_irq(dev_priv, dev_priv->pm_rps_events);
383b900b949SImre Deak 	I915_WRITE(gen6_pm_ier(dev_priv), I915_READ(gen6_pm_ier(dev_priv)) &
384b900b949SImre Deak 				~dev_priv->pm_rps_events);
38558072ccbSImre Deak 
38658072ccbSImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
38758072ccbSImre Deak 
38891d14251STvrtko Ursulin 	synchronize_irq(dev_priv->dev->irq);
389b900b949SImre Deak }
390b900b949SImre Deak 
3910961021aSBen Widawsky /**
3923a3b3c7dSVille Syrjälä  * bdw_update_port_irq - update DE port interrupt
3933a3b3c7dSVille Syrjälä  * @dev_priv: driver private
3943a3b3c7dSVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
3953a3b3c7dSVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
3963a3b3c7dSVille Syrjälä  */
3973a3b3c7dSVille Syrjälä static void bdw_update_port_irq(struct drm_i915_private *dev_priv,
3983a3b3c7dSVille Syrjälä 				uint32_t interrupt_mask,
3993a3b3c7dSVille Syrjälä 				uint32_t enabled_irq_mask)
4003a3b3c7dSVille Syrjälä {
4013a3b3c7dSVille Syrjälä 	uint32_t new_val;
4023a3b3c7dSVille Syrjälä 	uint32_t old_val;
4033a3b3c7dSVille Syrjälä 
4043a3b3c7dSVille Syrjälä 	assert_spin_locked(&dev_priv->irq_lock);
4053a3b3c7dSVille Syrjälä 
4063a3b3c7dSVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
4073a3b3c7dSVille Syrjälä 
4083a3b3c7dSVille Syrjälä 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
4093a3b3c7dSVille Syrjälä 		return;
4103a3b3c7dSVille Syrjälä 
4113a3b3c7dSVille Syrjälä 	old_val = I915_READ(GEN8_DE_PORT_IMR);
4123a3b3c7dSVille Syrjälä 
4133a3b3c7dSVille Syrjälä 	new_val = old_val;
4143a3b3c7dSVille Syrjälä 	new_val &= ~interrupt_mask;
4153a3b3c7dSVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
4163a3b3c7dSVille Syrjälä 
4173a3b3c7dSVille Syrjälä 	if (new_val != old_val) {
4183a3b3c7dSVille Syrjälä 		I915_WRITE(GEN8_DE_PORT_IMR, new_val);
4193a3b3c7dSVille Syrjälä 		POSTING_READ(GEN8_DE_PORT_IMR);
4203a3b3c7dSVille Syrjälä 	}
4213a3b3c7dSVille Syrjälä }
4223a3b3c7dSVille Syrjälä 
4233a3b3c7dSVille Syrjälä /**
424013d3752SVille Syrjälä  * bdw_update_pipe_irq - update DE pipe interrupt
425013d3752SVille Syrjälä  * @dev_priv: driver private
426013d3752SVille Syrjälä  * @pipe: pipe whose interrupt to update
427013d3752SVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
428013d3752SVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
429013d3752SVille Syrjälä  */
430013d3752SVille Syrjälä void bdw_update_pipe_irq(struct drm_i915_private *dev_priv,
431013d3752SVille Syrjälä 			 enum pipe pipe,
432013d3752SVille Syrjälä 			 uint32_t interrupt_mask,
433013d3752SVille Syrjälä 			 uint32_t enabled_irq_mask)
434013d3752SVille Syrjälä {
435013d3752SVille Syrjälä 	uint32_t new_val;
436013d3752SVille Syrjälä 
437013d3752SVille Syrjälä 	assert_spin_locked(&dev_priv->irq_lock);
438013d3752SVille Syrjälä 
439013d3752SVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
440013d3752SVille Syrjälä 
441013d3752SVille Syrjälä 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
442013d3752SVille Syrjälä 		return;
443013d3752SVille Syrjälä 
444013d3752SVille Syrjälä 	new_val = dev_priv->de_irq_mask[pipe];
445013d3752SVille Syrjälä 	new_val &= ~interrupt_mask;
446013d3752SVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
447013d3752SVille Syrjälä 
448013d3752SVille Syrjälä 	if (new_val != dev_priv->de_irq_mask[pipe]) {
449013d3752SVille Syrjälä 		dev_priv->de_irq_mask[pipe] = new_val;
450013d3752SVille Syrjälä 		I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
451013d3752SVille Syrjälä 		POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
452013d3752SVille Syrjälä 	}
453013d3752SVille Syrjälä }
454013d3752SVille Syrjälä 
455013d3752SVille Syrjälä /**
456fee884edSDaniel Vetter  * ibx_display_interrupt_update - update SDEIMR
457fee884edSDaniel Vetter  * @dev_priv: driver private
458fee884edSDaniel Vetter  * @interrupt_mask: mask of interrupt bits to update
459fee884edSDaniel Vetter  * @enabled_irq_mask: mask of interrupt bits to enable
460fee884edSDaniel Vetter  */
46147339cd9SDaniel Vetter void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
462fee884edSDaniel Vetter 				  uint32_t interrupt_mask,
463fee884edSDaniel Vetter 				  uint32_t enabled_irq_mask)
464fee884edSDaniel Vetter {
465fee884edSDaniel Vetter 	uint32_t sdeimr = I915_READ(SDEIMR);
466fee884edSDaniel Vetter 	sdeimr &= ~interrupt_mask;
467fee884edSDaniel Vetter 	sdeimr |= (~enabled_irq_mask & interrupt_mask);
468fee884edSDaniel Vetter 
46915a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
47015a17aaeSDaniel Vetter 
471fee884edSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
472fee884edSDaniel Vetter 
4739df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
474c67a470bSPaulo Zanoni 		return;
475c67a470bSPaulo Zanoni 
476fee884edSDaniel Vetter 	I915_WRITE(SDEIMR, sdeimr);
477fee884edSDaniel Vetter 	POSTING_READ(SDEIMR);
478fee884edSDaniel Vetter }
4798664281bSPaulo Zanoni 
480b5ea642aSDaniel Vetter static void
481755e9019SImre Deak __i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
482755e9019SImre Deak 		       u32 enable_mask, u32 status_mask)
4837c463586SKeith Packard {
484f0f59a00SVille Syrjälä 	i915_reg_t reg = PIPESTAT(pipe);
485755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
4867c463586SKeith Packard 
487b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
488d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
489b79480baSDaniel Vetter 
49004feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
49104feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
49204feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
49304feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
494755e9019SImre Deak 		return;
495755e9019SImre Deak 
496755e9019SImre Deak 	if ((pipestat & enable_mask) == enable_mask)
49746c06a30SVille Syrjälä 		return;
49846c06a30SVille Syrjälä 
49991d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] |= status_mask;
50091d181ddSImre Deak 
5017c463586SKeith Packard 	/* Enable the interrupt, clear any pending status */
502755e9019SImre Deak 	pipestat |= enable_mask | status_mask;
50346c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
5043143a2bfSChris Wilson 	POSTING_READ(reg);
5057c463586SKeith Packard }
5067c463586SKeith Packard 
507b5ea642aSDaniel Vetter static void
508755e9019SImre Deak __i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
509755e9019SImre Deak 		        u32 enable_mask, u32 status_mask)
5107c463586SKeith Packard {
511f0f59a00SVille Syrjälä 	i915_reg_t reg = PIPESTAT(pipe);
512755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
5137c463586SKeith Packard 
514b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
515d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
516b79480baSDaniel Vetter 
51704feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
51804feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
51904feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
52004feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
52146c06a30SVille Syrjälä 		return;
52246c06a30SVille Syrjälä 
523755e9019SImre Deak 	if ((pipestat & enable_mask) == 0)
524755e9019SImre Deak 		return;
525755e9019SImre Deak 
52691d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] &= ~status_mask;
52791d181ddSImre Deak 
528755e9019SImre Deak 	pipestat &= ~enable_mask;
52946c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
5303143a2bfSChris Wilson 	POSTING_READ(reg);
5317c463586SKeith Packard }
5327c463586SKeith Packard 
53310c59c51SImre Deak static u32 vlv_get_pipestat_enable_mask(struct drm_device *dev, u32 status_mask)
53410c59c51SImre Deak {
53510c59c51SImre Deak 	u32 enable_mask = status_mask << 16;
53610c59c51SImre Deak 
53710c59c51SImre Deak 	/*
538724a6905SVille Syrjälä 	 * On pipe A we don't support the PSR interrupt yet,
539724a6905SVille Syrjälä 	 * on pipe B and C the same bit MBZ.
54010c59c51SImre Deak 	 */
54110c59c51SImre Deak 	if (WARN_ON_ONCE(status_mask & PIPE_A_PSR_STATUS_VLV))
54210c59c51SImre Deak 		return 0;
543724a6905SVille Syrjälä 	/*
544724a6905SVille Syrjälä 	 * On pipe B and C we don't support the PSR interrupt yet, on pipe
545724a6905SVille Syrjälä 	 * A the same bit is for perf counters which we don't use either.
546724a6905SVille Syrjälä 	 */
547724a6905SVille Syrjälä 	if (WARN_ON_ONCE(status_mask & PIPE_B_PSR_STATUS_VLV))
548724a6905SVille Syrjälä 		return 0;
54910c59c51SImre Deak 
55010c59c51SImre Deak 	enable_mask &= ~(PIPE_FIFO_UNDERRUN_STATUS |
55110c59c51SImre Deak 			 SPRITE0_FLIP_DONE_INT_EN_VLV |
55210c59c51SImre Deak 			 SPRITE1_FLIP_DONE_INT_EN_VLV);
55310c59c51SImre Deak 	if (status_mask & SPRITE0_FLIP_DONE_INT_STATUS_VLV)
55410c59c51SImre Deak 		enable_mask |= SPRITE0_FLIP_DONE_INT_EN_VLV;
55510c59c51SImre Deak 	if (status_mask & SPRITE1_FLIP_DONE_INT_STATUS_VLV)
55610c59c51SImre Deak 		enable_mask |= SPRITE1_FLIP_DONE_INT_EN_VLV;
55710c59c51SImre Deak 
55810c59c51SImre Deak 	return enable_mask;
55910c59c51SImre Deak }
56010c59c51SImre Deak 
561755e9019SImre Deak void
562755e9019SImre Deak i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
563755e9019SImre Deak 		     u32 status_mask)
564755e9019SImre Deak {
565755e9019SImre Deak 	u32 enable_mask;
566755e9019SImre Deak 
567666a4537SWayne Boyer 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
56810c59c51SImre Deak 		enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
56910c59c51SImre Deak 							   status_mask);
57010c59c51SImre Deak 	else
571755e9019SImre Deak 		enable_mask = status_mask << 16;
572755e9019SImre Deak 	__i915_enable_pipestat(dev_priv, pipe, enable_mask, status_mask);
573755e9019SImre Deak }
574755e9019SImre Deak 
575755e9019SImre Deak void
576755e9019SImre Deak i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
577755e9019SImre Deak 		      u32 status_mask)
578755e9019SImre Deak {
579755e9019SImre Deak 	u32 enable_mask;
580755e9019SImre Deak 
581666a4537SWayne Boyer 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
58210c59c51SImre Deak 		enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
58310c59c51SImre Deak 							   status_mask);
58410c59c51SImre Deak 	else
585755e9019SImre Deak 		enable_mask = status_mask << 16;
586755e9019SImre Deak 	__i915_disable_pipestat(dev_priv, pipe, enable_mask, status_mask);
587755e9019SImre Deak }
588755e9019SImre Deak 
589c0e09200SDave Airlie /**
590f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
59114bb2c11STvrtko Ursulin  * @dev_priv: i915 device private
59201c66889SZhao Yakui  */
59391d14251STvrtko Ursulin static void i915_enable_asle_pipestat(struct drm_i915_private *dev_priv)
59401c66889SZhao Yakui {
59591d14251STvrtko Ursulin 	if (!dev_priv->opregion.asle || !IS_MOBILE(dev_priv))
596f49e38ddSJani Nikula 		return;
597f49e38ddSJani Nikula 
59813321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
59901c66889SZhao Yakui 
600755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_STATUS);
60191d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 4)
6023b6c42e8SDaniel Vetter 		i915_enable_pipestat(dev_priv, PIPE_A,
603755e9019SImre Deak 				     PIPE_LEGACY_BLC_EVENT_STATUS);
6041ec14ad3SChris Wilson 
60513321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
60601c66889SZhao Yakui }
60701c66889SZhao Yakui 
608f75f3746SVille Syrjälä /*
609f75f3746SVille Syrjälä  * This timing diagram depicts the video signal in and
610f75f3746SVille Syrjälä  * around the vertical blanking period.
611f75f3746SVille Syrjälä  *
612f75f3746SVille Syrjälä  * Assumptions about the fictitious mode used in this example:
613f75f3746SVille Syrjälä  *  vblank_start >= 3
614f75f3746SVille Syrjälä  *  vsync_start = vblank_start + 1
615f75f3746SVille Syrjälä  *  vsync_end = vblank_start + 2
616f75f3746SVille Syrjälä  *  vtotal = vblank_start + 3
617f75f3746SVille Syrjälä  *
618f75f3746SVille Syrjälä  *           start of vblank:
619f75f3746SVille Syrjälä  *           latch double buffered registers
620f75f3746SVille Syrjälä  *           increment frame counter (ctg+)
621f75f3746SVille Syrjälä  *           generate start of vblank interrupt (gen4+)
622f75f3746SVille Syrjälä  *           |
623f75f3746SVille Syrjälä  *           |          frame start:
624f75f3746SVille Syrjälä  *           |          generate frame start interrupt (aka. vblank interrupt) (gmch)
625f75f3746SVille Syrjälä  *           |          may be shifted forward 1-3 extra lines via PIPECONF
626f75f3746SVille Syrjälä  *           |          |
627f75f3746SVille Syrjälä  *           |          |  start of vsync:
628f75f3746SVille Syrjälä  *           |          |  generate vsync interrupt
629f75f3746SVille Syrjälä  *           |          |  |
630f75f3746SVille Syrjälä  * ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx
631f75f3746SVille Syrjälä  *       .   \hs/   .      \hs/          \hs/          \hs/   .      \hs/
632f75f3746SVille Syrjälä  * ----va---> <-----------------vb--------------------> <--------va-------------
633f75f3746SVille Syrjälä  *       |          |       <----vs----->                     |
634f75f3746SVille Syrjälä  * -vbs-----> <---vbs+1---> <---vbs+2---> <-----0-----> <-----1-----> <-----2--- (scanline counter gen2)
635f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2---> <-----0--- (scanline counter gen3+)
636f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2- (scanline counter hsw+ hdmi)
637f75f3746SVille Syrjälä  *       |          |                                         |
638f75f3746SVille Syrjälä  *       last visible pixel                                   first visible pixel
639f75f3746SVille Syrjälä  *                  |                                         increment frame counter (gen3/4)
640f75f3746SVille Syrjälä  *                  pixel counter = vblank_start * htotal     pixel counter = 0 (gen3/4)
641f75f3746SVille Syrjälä  *
642f75f3746SVille Syrjälä  * x  = horizontal active
643f75f3746SVille Syrjälä  * _  = horizontal blanking
644f75f3746SVille Syrjälä  * hs = horizontal sync
645f75f3746SVille Syrjälä  * va = vertical active
646f75f3746SVille Syrjälä  * vb = vertical blanking
647f75f3746SVille Syrjälä  * vs = vertical sync
648f75f3746SVille Syrjälä  * vbs = vblank_start (number)
649f75f3746SVille Syrjälä  *
650f75f3746SVille Syrjälä  * Summary:
651f75f3746SVille Syrjälä  * - most events happen at the start of horizontal sync
652f75f3746SVille Syrjälä  * - frame start happens at the start of horizontal blank, 1-4 lines
653f75f3746SVille Syrjälä  *   (depending on PIPECONF settings) after the start of vblank
654f75f3746SVille Syrjälä  * - gen3/4 pixel and frame counter are synchronized with the start
655f75f3746SVille Syrjälä  *   of horizontal active on the first line of vertical active
656f75f3746SVille Syrjälä  */
657f75f3746SVille Syrjälä 
65888e72717SThierry Reding static u32 i8xx_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
6594cdb83ecSVille Syrjälä {
6604cdb83ecSVille Syrjälä 	/* Gen2 doesn't have a hardware frame counter */
6614cdb83ecSVille Syrjälä 	return 0;
6624cdb83ecSVille Syrjälä }
6634cdb83ecSVille Syrjälä 
66442f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
66542f52ef8SKeith Packard  * we use as a pipe index
66642f52ef8SKeith Packard  */
66788e72717SThierry Reding static u32 i915_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
6680a3e67a4SJesse Barnes {
6692d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
670f0f59a00SVille Syrjälä 	i915_reg_t high_frame, low_frame;
6710b2a8e09SVille Syrjälä 	u32 high1, high2, low, pixel, vbl_start, hsync_start, htotal;
672391f75e2SVille Syrjälä 	struct intel_crtc *intel_crtc =
673391f75e2SVille Syrjälä 		to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
674fc467a22SMaarten Lankhorst 	const struct drm_display_mode *mode = &intel_crtc->base.hwmode;
675391f75e2SVille Syrjälä 
6760b2a8e09SVille Syrjälä 	htotal = mode->crtc_htotal;
6770b2a8e09SVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
6780b2a8e09SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
6790b2a8e09SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
6800b2a8e09SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
681391f75e2SVille Syrjälä 
6820b2a8e09SVille Syrjälä 	/* Convert to pixel count */
6830b2a8e09SVille Syrjälä 	vbl_start *= htotal;
6840b2a8e09SVille Syrjälä 
6850b2a8e09SVille Syrjälä 	/* Start of vblank event occurs at start of hsync */
6860b2a8e09SVille Syrjälä 	vbl_start -= htotal - hsync_start;
6870b2a8e09SVille Syrjälä 
6889db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
6899db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
6905eddb70bSChris Wilson 
6910a3e67a4SJesse Barnes 	/*
6920a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
6930a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
6940a3e67a4SJesse Barnes 	 * register.
6950a3e67a4SJesse Barnes 	 */
6960a3e67a4SJesse Barnes 	do {
6975eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
698391f75e2SVille Syrjälä 		low   = I915_READ(low_frame);
6995eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
7000a3e67a4SJesse Barnes 	} while (high1 != high2);
7010a3e67a4SJesse Barnes 
7025eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
703391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
7045eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
705391f75e2SVille Syrjälä 
706391f75e2SVille Syrjälä 	/*
707391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
708391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
709391f75e2SVille Syrjälä 	 * counter against vblank start.
710391f75e2SVille Syrjälä 	 */
711edc08d0aSVille Syrjälä 	return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
7120a3e67a4SJesse Barnes }
7130a3e67a4SJesse Barnes 
714974e59baSDave Airlie static u32 g4x_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
7159880b7a5SJesse Barnes {
7162d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
7179880b7a5SJesse Barnes 
718649636efSVille Syrjälä 	return I915_READ(PIPE_FRMCOUNT_G4X(pipe));
7199880b7a5SJesse Barnes }
7209880b7a5SJesse Barnes 
72175aa3f63SVille Syrjälä /* I915_READ_FW, only for fast reads of display block, no need for forcewake etc. */
722a225f079SVille Syrjälä static int __intel_get_crtc_scanline(struct intel_crtc *crtc)
723a225f079SVille Syrjälä {
724a225f079SVille Syrjälä 	struct drm_device *dev = crtc->base.dev;
725a225f079SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
726fc467a22SMaarten Lankhorst 	const struct drm_display_mode *mode = &crtc->base.hwmode;
727a225f079SVille Syrjälä 	enum pipe pipe = crtc->pipe;
72880715b2fSVille Syrjälä 	int position, vtotal;
729a225f079SVille Syrjälä 
73080715b2fSVille Syrjälä 	vtotal = mode->crtc_vtotal;
731a225f079SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
732a225f079SVille Syrjälä 		vtotal /= 2;
733a225f079SVille Syrjälä 
73491d14251STvrtko Ursulin 	if (IS_GEN2(dev_priv))
73575aa3f63SVille Syrjälä 		position = I915_READ_FW(PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
736a225f079SVille Syrjälä 	else
73775aa3f63SVille Syrjälä 		position = I915_READ_FW(PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
738a225f079SVille Syrjälä 
739a225f079SVille Syrjälä 	/*
74041b578fbSJesse Barnes 	 * On HSW, the DSL reg (0x70000) appears to return 0 if we
74141b578fbSJesse Barnes 	 * read it just before the start of vblank.  So try it again
74241b578fbSJesse Barnes 	 * so we don't accidentally end up spanning a vblank frame
74341b578fbSJesse Barnes 	 * increment, causing the pipe_update_end() code to squak at us.
74441b578fbSJesse Barnes 	 *
74541b578fbSJesse Barnes 	 * The nature of this problem means we can't simply check the ISR
74641b578fbSJesse Barnes 	 * bit and return the vblank start value; nor can we use the scanline
74741b578fbSJesse Barnes 	 * debug register in the transcoder as it appears to have the same
74841b578fbSJesse Barnes 	 * problem.  We may need to extend this to include other platforms,
74941b578fbSJesse Barnes 	 * but so far testing only shows the problem on HSW.
75041b578fbSJesse Barnes 	 */
75191d14251STvrtko Ursulin 	if (HAS_DDI(dev_priv) && !position) {
75241b578fbSJesse Barnes 		int i, temp;
75341b578fbSJesse Barnes 
75441b578fbSJesse Barnes 		for (i = 0; i < 100; i++) {
75541b578fbSJesse Barnes 			udelay(1);
75641b578fbSJesse Barnes 			temp = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) &
75741b578fbSJesse Barnes 				DSL_LINEMASK_GEN3;
75841b578fbSJesse Barnes 			if (temp != position) {
75941b578fbSJesse Barnes 				position = temp;
76041b578fbSJesse Barnes 				break;
76141b578fbSJesse Barnes 			}
76241b578fbSJesse Barnes 		}
76341b578fbSJesse Barnes 	}
76441b578fbSJesse Barnes 
76541b578fbSJesse Barnes 	/*
76680715b2fSVille Syrjälä 	 * See update_scanline_offset() for the details on the
76780715b2fSVille Syrjälä 	 * scanline_offset adjustment.
768a225f079SVille Syrjälä 	 */
76980715b2fSVille Syrjälä 	return (position + crtc->scanline_offset) % vtotal;
770a225f079SVille Syrjälä }
771a225f079SVille Syrjälä 
77288e72717SThierry Reding static int i915_get_crtc_scanoutpos(struct drm_device *dev, unsigned int pipe,
773abca9e45SVille Syrjälä 				    unsigned int flags, int *vpos, int *hpos,
7743bb403bfSVille Syrjälä 				    ktime_t *stime, ktime_t *etime,
7753bb403bfSVille Syrjälä 				    const struct drm_display_mode *mode)
7760af7e4dfSMario Kleiner {
777c2baf4b7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
778c2baf4b7SVille Syrjälä 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
779c2baf4b7SVille Syrjälä 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7803aa18df8SVille Syrjälä 	int position;
78178e8fc6bSVille Syrjälä 	int vbl_start, vbl_end, hsync_start, htotal, vtotal;
7820af7e4dfSMario Kleiner 	bool in_vbl = true;
7830af7e4dfSMario Kleiner 	int ret = 0;
784ad3543edSMario Kleiner 	unsigned long irqflags;
7850af7e4dfSMario Kleiner 
786fc467a22SMaarten Lankhorst 	if (WARN_ON(!mode->crtc_clock)) {
7870af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
7889db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
7890af7e4dfSMario Kleiner 		return 0;
7900af7e4dfSMario Kleiner 	}
7910af7e4dfSMario Kleiner 
792c2baf4b7SVille Syrjälä 	htotal = mode->crtc_htotal;
79378e8fc6bSVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
794c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
795c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
796c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
7970af7e4dfSMario Kleiner 
798d31faf65SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
799d31faf65SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
800d31faf65SVille Syrjälä 		vbl_end /= 2;
801d31faf65SVille Syrjälä 		vtotal /= 2;
802d31faf65SVille Syrjälä 	}
803d31faf65SVille Syrjälä 
804c2baf4b7SVille Syrjälä 	ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
805c2baf4b7SVille Syrjälä 
806ad3543edSMario Kleiner 	/*
807ad3543edSMario Kleiner 	 * Lock uncore.lock, as we will do multiple timing critical raw
808ad3543edSMario Kleiner 	 * register reads, potentially with preemption disabled, so the
809ad3543edSMario Kleiner 	 * following code must not block on uncore.lock.
810ad3543edSMario Kleiner 	 */
811ad3543edSMario Kleiner 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
812ad3543edSMario Kleiner 
813ad3543edSMario Kleiner 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
814ad3543edSMario Kleiner 
815ad3543edSMario Kleiner 	/* Get optional system timestamp before query. */
816ad3543edSMario Kleiner 	if (stime)
817ad3543edSMario Kleiner 		*stime = ktime_get();
818ad3543edSMario Kleiner 
81991d14251STvrtko Ursulin 	if (IS_GEN2(dev_priv) || IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5) {
8200af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
8210af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
8220af7e4dfSMario Kleiner 		 */
823a225f079SVille Syrjälä 		position = __intel_get_crtc_scanline(intel_crtc);
8240af7e4dfSMario Kleiner 	} else {
8250af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
8260af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
8270af7e4dfSMario Kleiner 		 * scanout position.
8280af7e4dfSMario Kleiner 		 */
82975aa3f63SVille Syrjälä 		position = (I915_READ_FW(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
8300af7e4dfSMario Kleiner 
8313aa18df8SVille Syrjälä 		/* convert to pixel counts */
8323aa18df8SVille Syrjälä 		vbl_start *= htotal;
8333aa18df8SVille Syrjälä 		vbl_end *= htotal;
8343aa18df8SVille Syrjälä 		vtotal *= htotal;
83578e8fc6bSVille Syrjälä 
83678e8fc6bSVille Syrjälä 		/*
8377e78f1cbSVille Syrjälä 		 * In interlaced modes, the pixel counter counts all pixels,
8387e78f1cbSVille Syrjälä 		 * so one field will have htotal more pixels. In order to avoid
8397e78f1cbSVille Syrjälä 		 * the reported position from jumping backwards when the pixel
8407e78f1cbSVille Syrjälä 		 * counter is beyond the length of the shorter field, just
8417e78f1cbSVille Syrjälä 		 * clamp the position the length of the shorter field. This
8427e78f1cbSVille Syrjälä 		 * matches how the scanline counter based position works since
8437e78f1cbSVille Syrjälä 		 * the scanline counter doesn't count the two half lines.
8447e78f1cbSVille Syrjälä 		 */
8457e78f1cbSVille Syrjälä 		if (position >= vtotal)
8467e78f1cbSVille Syrjälä 			position = vtotal - 1;
8477e78f1cbSVille Syrjälä 
8487e78f1cbSVille Syrjälä 		/*
84978e8fc6bSVille Syrjälä 		 * Start of vblank interrupt is triggered at start of hsync,
85078e8fc6bSVille Syrjälä 		 * just prior to the first active line of vblank. However we
85178e8fc6bSVille Syrjälä 		 * consider lines to start at the leading edge of horizontal
85278e8fc6bSVille Syrjälä 		 * active. So, should we get here before we've crossed into
85378e8fc6bSVille Syrjälä 		 * the horizontal active of the first line in vblank, we would
85478e8fc6bSVille Syrjälä 		 * not set the DRM_SCANOUTPOS_INVBL flag. In order to fix that,
85578e8fc6bSVille Syrjälä 		 * always add htotal-hsync_start to the current pixel position.
85678e8fc6bSVille Syrjälä 		 */
85778e8fc6bSVille Syrjälä 		position = (position + htotal - hsync_start) % vtotal;
8583aa18df8SVille Syrjälä 	}
8593aa18df8SVille Syrjälä 
860ad3543edSMario Kleiner 	/* Get optional system timestamp after query. */
861ad3543edSMario Kleiner 	if (etime)
862ad3543edSMario Kleiner 		*etime = ktime_get();
863ad3543edSMario Kleiner 
864ad3543edSMario Kleiner 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
865ad3543edSMario Kleiner 
866ad3543edSMario Kleiner 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
867ad3543edSMario Kleiner 
8683aa18df8SVille Syrjälä 	in_vbl = position >= vbl_start && position < vbl_end;
8693aa18df8SVille Syrjälä 
8703aa18df8SVille Syrjälä 	/*
8713aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
8723aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
8733aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
8743aa18df8SVille Syrjälä 	 * up since vbl_end.
8753aa18df8SVille Syrjälä 	 */
8763aa18df8SVille Syrjälä 	if (position >= vbl_start)
8773aa18df8SVille Syrjälä 		position -= vbl_end;
8783aa18df8SVille Syrjälä 	else
8793aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
8803aa18df8SVille Syrjälä 
88191d14251STvrtko Ursulin 	if (IS_GEN2(dev_priv) || IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5) {
8823aa18df8SVille Syrjälä 		*vpos = position;
8833aa18df8SVille Syrjälä 		*hpos = 0;
8843aa18df8SVille Syrjälä 	} else {
8850af7e4dfSMario Kleiner 		*vpos = position / htotal;
8860af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
8870af7e4dfSMario Kleiner 	}
8880af7e4dfSMario Kleiner 
8890af7e4dfSMario Kleiner 	/* In vblank? */
8900af7e4dfSMario Kleiner 	if (in_vbl)
8913d3cbd84SDaniel Vetter 		ret |= DRM_SCANOUTPOS_IN_VBLANK;
8920af7e4dfSMario Kleiner 
8930af7e4dfSMario Kleiner 	return ret;
8940af7e4dfSMario Kleiner }
8950af7e4dfSMario Kleiner 
896a225f079SVille Syrjälä int intel_get_crtc_scanline(struct intel_crtc *crtc)
897a225f079SVille Syrjälä {
898a225f079SVille Syrjälä 	struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
899a225f079SVille Syrjälä 	unsigned long irqflags;
900a225f079SVille Syrjälä 	int position;
901a225f079SVille Syrjälä 
902a225f079SVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
903a225f079SVille Syrjälä 	position = __intel_get_crtc_scanline(crtc);
904a225f079SVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
905a225f079SVille Syrjälä 
906a225f079SVille Syrjälä 	return position;
907a225f079SVille Syrjälä }
908a225f079SVille Syrjälä 
90988e72717SThierry Reding static int i915_get_vblank_timestamp(struct drm_device *dev, unsigned int pipe,
9100af7e4dfSMario Kleiner 			      int *max_error,
9110af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
9120af7e4dfSMario Kleiner 			      unsigned flags)
9130af7e4dfSMario Kleiner {
9144041b853SChris Wilson 	struct drm_crtc *crtc;
9150af7e4dfSMario Kleiner 
91688e72717SThierry Reding 	if (pipe >= INTEL_INFO(dev)->num_pipes) {
91788e72717SThierry Reding 		DRM_ERROR("Invalid crtc %u\n", pipe);
9180af7e4dfSMario Kleiner 		return -EINVAL;
9190af7e4dfSMario Kleiner 	}
9200af7e4dfSMario Kleiner 
9210af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
9224041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
9234041b853SChris Wilson 	if (crtc == NULL) {
92488e72717SThierry Reding 		DRM_ERROR("Invalid crtc %u\n", pipe);
9254041b853SChris Wilson 		return -EINVAL;
9264041b853SChris Wilson 	}
9274041b853SChris Wilson 
928fc467a22SMaarten Lankhorst 	if (!crtc->hwmode.crtc_clock) {
92988e72717SThierry Reding 		DRM_DEBUG_KMS("crtc %u is disabled\n", pipe);
9304041b853SChris Wilson 		return -EBUSY;
9314041b853SChris Wilson 	}
9320af7e4dfSMario Kleiner 
9330af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
9344041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
9354041b853SChris Wilson 						     vblank_time, flags,
936fc467a22SMaarten Lankhorst 						     &crtc->hwmode);
9370af7e4dfSMario Kleiner }
9380af7e4dfSMario Kleiner 
93991d14251STvrtko Ursulin static void ironlake_rps_change_irq_handler(struct drm_i915_private *dev_priv)
940f97108d1SJesse Barnes {
941b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
9429270388eSDaniel Vetter 	u8 new_delay;
9439270388eSDaniel Vetter 
944d0ecd7e2SDaniel Vetter 	spin_lock(&mchdev_lock);
945f97108d1SJesse Barnes 
94673edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
94773edd18fSDaniel Vetter 
94820e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
9499270388eSDaniel Vetter 
9507648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
951b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
952b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
953f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
954f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
955f97108d1SJesse Barnes 
956f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
957b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
95820e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
95920e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
96020e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
96120e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
962b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
96320e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
96420e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
96520e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
96620e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
967f97108d1SJesse Barnes 	}
968f97108d1SJesse Barnes 
96991d14251STvrtko Ursulin 	if (ironlake_set_drps(dev_priv, new_delay))
97020e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
971f97108d1SJesse Barnes 
972d0ecd7e2SDaniel Vetter 	spin_unlock(&mchdev_lock);
9739270388eSDaniel Vetter 
974f97108d1SJesse Barnes 	return;
975f97108d1SJesse Barnes }
976f97108d1SJesse Barnes 
9770bc40be8STvrtko Ursulin static void notify_ring(struct intel_engine_cs *engine)
978549f7365SChris Wilson {
979117897f4STvrtko Ursulin 	if (!intel_engine_initialized(engine))
980475553deSChris Wilson 		return;
981475553deSChris Wilson 
9820bc40be8STvrtko Ursulin 	trace_i915_gem_request_notify(engine);
98312471ba8SChris Wilson 	engine->user_interrupts++;
9849862e600SChris Wilson 
9850bc40be8STvrtko Ursulin 	wake_up_all(&engine->irq_queue);
986549f7365SChris Wilson }
987549f7365SChris Wilson 
98843cf3bf0SChris Wilson static void vlv_c0_read(struct drm_i915_private *dev_priv,
98943cf3bf0SChris Wilson 			struct intel_rps_ei *ei)
99031685c25SDeepak S {
99143cf3bf0SChris Wilson 	ei->cz_clock = vlv_punit_read(dev_priv, PUNIT_REG_CZ_TIMESTAMP);
99243cf3bf0SChris Wilson 	ei->render_c0 = I915_READ(VLV_RENDER_C0_COUNT);
99343cf3bf0SChris Wilson 	ei->media_c0 = I915_READ(VLV_MEDIA_C0_COUNT);
99431685c25SDeepak S }
99531685c25SDeepak S 
99643cf3bf0SChris Wilson static bool vlv_c0_above(struct drm_i915_private *dev_priv,
99743cf3bf0SChris Wilson 			 const struct intel_rps_ei *old,
99843cf3bf0SChris Wilson 			 const struct intel_rps_ei *now,
99943cf3bf0SChris Wilson 			 int threshold)
100031685c25SDeepak S {
100143cf3bf0SChris Wilson 	u64 time, c0;
10027bad74d5SVille Syrjälä 	unsigned int mul = 100;
100331685c25SDeepak S 
100443cf3bf0SChris Wilson 	if (old->cz_clock == 0)
100543cf3bf0SChris Wilson 		return false;
100631685c25SDeepak S 
10077bad74d5SVille Syrjälä 	if (I915_READ(VLV_COUNTER_CONTROL) & VLV_COUNT_RANGE_HIGH)
10087bad74d5SVille Syrjälä 		mul <<= 8;
10097bad74d5SVille Syrjälä 
101043cf3bf0SChris Wilson 	time = now->cz_clock - old->cz_clock;
10117bad74d5SVille Syrjälä 	time *= threshold * dev_priv->czclk_freq;
101231685c25SDeepak S 
101343cf3bf0SChris Wilson 	/* Workload can be split between render + media, e.g. SwapBuffers
101443cf3bf0SChris Wilson 	 * being blitted in X after being rendered in mesa. To account for
101543cf3bf0SChris Wilson 	 * this we need to combine both engines into our activity counter.
101643cf3bf0SChris Wilson 	 */
101743cf3bf0SChris Wilson 	c0 = now->render_c0 - old->render_c0;
101843cf3bf0SChris Wilson 	c0 += now->media_c0 - old->media_c0;
10197bad74d5SVille Syrjälä 	c0 *= mul * VLV_CZ_CLOCK_TO_MILLI_SEC;
102031685c25SDeepak S 
102143cf3bf0SChris Wilson 	return c0 >= time;
102231685c25SDeepak S }
102331685c25SDeepak S 
102443cf3bf0SChris Wilson void gen6_rps_reset_ei(struct drm_i915_private *dev_priv)
102543cf3bf0SChris Wilson {
102643cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &dev_priv->rps.down_ei);
102743cf3bf0SChris Wilson 	dev_priv->rps.up_ei = dev_priv->rps.down_ei;
102843cf3bf0SChris Wilson }
102943cf3bf0SChris Wilson 
103043cf3bf0SChris Wilson static u32 vlv_wa_c0_ei(struct drm_i915_private *dev_priv, u32 pm_iir)
103143cf3bf0SChris Wilson {
103243cf3bf0SChris Wilson 	struct intel_rps_ei now;
103343cf3bf0SChris Wilson 	u32 events = 0;
103443cf3bf0SChris Wilson 
10356f4b12f8SChris Wilson 	if ((pm_iir & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED)) == 0)
103643cf3bf0SChris Wilson 		return 0;
103743cf3bf0SChris Wilson 
103843cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &now);
103943cf3bf0SChris Wilson 	if (now.cz_clock == 0)
104043cf3bf0SChris Wilson 		return 0;
104131685c25SDeepak S 
104243cf3bf0SChris Wilson 	if (pm_iir & GEN6_PM_RP_DOWN_EI_EXPIRED) {
104343cf3bf0SChris Wilson 		if (!vlv_c0_above(dev_priv,
104443cf3bf0SChris Wilson 				  &dev_priv->rps.down_ei, &now,
10458fb55197SChris Wilson 				  dev_priv->rps.down_threshold))
104643cf3bf0SChris Wilson 			events |= GEN6_PM_RP_DOWN_THRESHOLD;
104743cf3bf0SChris Wilson 		dev_priv->rps.down_ei = now;
104831685c25SDeepak S 	}
104931685c25SDeepak S 
105043cf3bf0SChris Wilson 	if (pm_iir & GEN6_PM_RP_UP_EI_EXPIRED) {
105143cf3bf0SChris Wilson 		if (vlv_c0_above(dev_priv,
105243cf3bf0SChris Wilson 				 &dev_priv->rps.up_ei, &now,
10538fb55197SChris Wilson 				 dev_priv->rps.up_threshold))
105443cf3bf0SChris Wilson 			events |= GEN6_PM_RP_UP_THRESHOLD;
105543cf3bf0SChris Wilson 		dev_priv->rps.up_ei = now;
105643cf3bf0SChris Wilson 	}
105743cf3bf0SChris Wilson 
105843cf3bf0SChris Wilson 	return events;
105931685c25SDeepak S }
106031685c25SDeepak S 
1061f5a4c67dSChris Wilson static bool any_waiters(struct drm_i915_private *dev_priv)
1062f5a4c67dSChris Wilson {
1063e2f80391STvrtko Ursulin 	struct intel_engine_cs *engine;
1064f5a4c67dSChris Wilson 
1065b4ac5afcSDave Gordon 	for_each_engine(engine, dev_priv)
1066e2f80391STvrtko Ursulin 		if (engine->irq_refcount)
1067f5a4c67dSChris Wilson 			return true;
1068f5a4c67dSChris Wilson 
1069f5a4c67dSChris Wilson 	return false;
1070f5a4c67dSChris Wilson }
1071f5a4c67dSChris Wilson 
10724912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
10733b8d8d91SJesse Barnes {
10742d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
10752d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, rps.work);
10768d3afd7dSChris Wilson 	bool client_boost;
10778d3afd7dSChris Wilson 	int new_delay, adj, min, max;
1078edbfdb45SPaulo Zanoni 	u32 pm_iir;
10793b8d8d91SJesse Barnes 
108059cdb63dSDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
1081d4d70aa5SImre Deak 	/* Speed up work cancelation during disabling rps interrupts. */
1082d4d70aa5SImre Deak 	if (!dev_priv->rps.interrupts_enabled) {
1083d4d70aa5SImre Deak 		spin_unlock_irq(&dev_priv->irq_lock);
1084d4d70aa5SImre Deak 		return;
1085d4d70aa5SImre Deak 	}
10861f814dacSImre Deak 
10871f814dacSImre Deak 	/*
10881f814dacSImre Deak 	 * The RPS work is synced during runtime suspend, we don't require a
10891f814dacSImre Deak 	 * wakeref. TODO: instead of disabling the asserts make sure that we
10901f814dacSImre Deak 	 * always hold an RPM reference while the work is running.
10911f814dacSImre Deak 	 */
10921f814dacSImre Deak 	DISABLE_RPM_WAKEREF_ASSERTS(dev_priv);
10931f814dacSImre Deak 
1094c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
1095c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
1096a72fbc3aSImre Deak 	/* Make sure not to corrupt PMIMR state used by ringbuffer on GEN6 */
1097480c8033SDaniel Vetter 	gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
10988d3afd7dSChris Wilson 	client_boost = dev_priv->rps.client_boost;
10998d3afd7dSChris Wilson 	dev_priv->rps.client_boost = false;
110059cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
11014912d041SBen Widawsky 
110260611c13SPaulo Zanoni 	/* Make sure we didn't queue anything we're not going to process. */
1103a6706b45SDeepak S 	WARN_ON(pm_iir & ~dev_priv->pm_rps_events);
110460611c13SPaulo Zanoni 
11058d3afd7dSChris Wilson 	if ((pm_iir & dev_priv->pm_rps_events) == 0 && !client_boost)
11061f814dacSImre Deak 		goto out;
11073b8d8d91SJesse Barnes 
11084fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
11097b9e0ae6SChris Wilson 
111043cf3bf0SChris Wilson 	pm_iir |= vlv_wa_c0_ei(dev_priv, pm_iir);
111143cf3bf0SChris Wilson 
1112dd75fdc8SChris Wilson 	adj = dev_priv->rps.last_adj;
1113edcf284bSChris Wilson 	new_delay = dev_priv->rps.cur_freq;
11148d3afd7dSChris Wilson 	min = dev_priv->rps.min_freq_softlimit;
11158d3afd7dSChris Wilson 	max = dev_priv->rps.max_freq_softlimit;
11168d3afd7dSChris Wilson 
11178d3afd7dSChris Wilson 	if (client_boost) {
11188d3afd7dSChris Wilson 		new_delay = dev_priv->rps.max_freq_softlimit;
11198d3afd7dSChris Wilson 		adj = 0;
11208d3afd7dSChris Wilson 	} else if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
1121dd75fdc8SChris Wilson 		if (adj > 0)
1122dd75fdc8SChris Wilson 			adj *= 2;
1123edcf284bSChris Wilson 		else /* CHV needs even encode values */
1124edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? 2 : 1;
11257425034aSVille Syrjälä 		/*
11267425034aSVille Syrjälä 		 * For better performance, jump directly
11277425034aSVille Syrjälä 		 * to RPe if we're below it.
11287425034aSVille Syrjälä 		 */
1129edcf284bSChris Wilson 		if (new_delay < dev_priv->rps.efficient_freq - adj) {
1130b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1131edcf284bSChris Wilson 			adj = 0;
1132edcf284bSChris Wilson 		}
1133f5a4c67dSChris Wilson 	} else if (any_waiters(dev_priv)) {
1134f5a4c67dSChris Wilson 		adj = 0;
1135dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
1136b39fb297SBen Widawsky 		if (dev_priv->rps.cur_freq > dev_priv->rps.efficient_freq)
1137b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1138dd75fdc8SChris Wilson 		else
1139b39fb297SBen Widawsky 			new_delay = dev_priv->rps.min_freq_softlimit;
1140dd75fdc8SChris Wilson 		adj = 0;
1141dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1142dd75fdc8SChris Wilson 		if (adj < 0)
1143dd75fdc8SChris Wilson 			adj *= 2;
1144edcf284bSChris Wilson 		else /* CHV needs even encode values */
1145edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? -2 : -1;
1146dd75fdc8SChris Wilson 	} else { /* unknown event */
1147edcf284bSChris Wilson 		adj = 0;
1148dd75fdc8SChris Wilson 	}
11493b8d8d91SJesse Barnes 
1150edcf284bSChris Wilson 	dev_priv->rps.last_adj = adj;
1151edcf284bSChris Wilson 
115279249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
115379249636SBen Widawsky 	 * interrupt
115479249636SBen Widawsky 	 */
1155edcf284bSChris Wilson 	new_delay += adj;
11568d3afd7dSChris Wilson 	new_delay = clamp_t(int, new_delay, min, max);
115727544369SDeepak S 
1158dc97997aSChris Wilson 	intel_set_rps(dev_priv, new_delay);
11593b8d8d91SJesse Barnes 
11604fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
11611f814dacSImre Deak out:
11621f814dacSImre Deak 	ENABLE_RPM_WAKEREF_ASSERTS(dev_priv);
11633b8d8d91SJesse Barnes }
11643b8d8d91SJesse Barnes 
1165e3689190SBen Widawsky 
1166e3689190SBen Widawsky /**
1167e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
1168e3689190SBen Widawsky  * occurred.
1169e3689190SBen Widawsky  * @work: workqueue struct
1170e3689190SBen Widawsky  *
1171e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
1172e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
1173e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
1174e3689190SBen Widawsky  */
1175e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
1176e3689190SBen Widawsky {
11772d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
11782d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, l3_parity.error_work);
1179e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
118035a85ac6SBen Widawsky 	char *parity_event[6];
1181e3689190SBen Widawsky 	uint32_t misccpctl;
118235a85ac6SBen Widawsky 	uint8_t slice = 0;
1183e3689190SBen Widawsky 
1184e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
1185e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
1186e3689190SBen Widawsky 	 * any time we access those registers.
1187e3689190SBen Widawsky 	 */
1188e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
1189e3689190SBen Widawsky 
119035a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
119135a85ac6SBen Widawsky 	if (WARN_ON(!dev_priv->l3_parity.which_slice))
119235a85ac6SBen Widawsky 		goto out;
119335a85ac6SBen Widawsky 
1194e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
1195e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1196e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
1197e3689190SBen Widawsky 
119835a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
1199f0f59a00SVille Syrjälä 		i915_reg_t reg;
120035a85ac6SBen Widawsky 
120135a85ac6SBen Widawsky 		slice--;
12022d1fe073SJoonas Lahtinen 		if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv)))
120335a85ac6SBen Widawsky 			break;
120435a85ac6SBen Widawsky 
120535a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
120635a85ac6SBen Widawsky 
12076fa1c5f1SVille Syrjälä 		reg = GEN7_L3CDERRST1(slice);
120835a85ac6SBen Widawsky 
120935a85ac6SBen Widawsky 		error_status = I915_READ(reg);
1210e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
1211e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
1212e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1213e3689190SBen Widawsky 
121435a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
121535a85ac6SBen Widawsky 		POSTING_READ(reg);
1216e3689190SBen Widawsky 
1217cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1218e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1219e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1220e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
122135a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
122235a85ac6SBen Widawsky 		parity_event[5] = NULL;
1223e3689190SBen Widawsky 
12245bdebb18SDave Airlie 		kobject_uevent_env(&dev_priv->dev->primary->kdev->kobj,
1225e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
1226e3689190SBen Widawsky 
122735a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
122835a85ac6SBen Widawsky 			  slice, row, bank, subbank);
1229e3689190SBen Widawsky 
123035a85ac6SBen Widawsky 		kfree(parity_event[4]);
1231e3689190SBen Widawsky 		kfree(parity_event[3]);
1232e3689190SBen Widawsky 		kfree(parity_event[2]);
1233e3689190SBen Widawsky 		kfree(parity_event[1]);
1234e3689190SBen Widawsky 	}
1235e3689190SBen Widawsky 
123635a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
123735a85ac6SBen Widawsky 
123835a85ac6SBen Widawsky out:
123935a85ac6SBen Widawsky 	WARN_ON(dev_priv->l3_parity.which_slice);
12404cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
12412d1fe073SJoonas Lahtinen 	gen5_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv));
12424cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
124335a85ac6SBen Widawsky 
124435a85ac6SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
124535a85ac6SBen Widawsky }
124635a85ac6SBen Widawsky 
1247261e40b8SVille Syrjälä static void ivybridge_parity_error_irq_handler(struct drm_i915_private *dev_priv,
1248261e40b8SVille Syrjälä 					       u32 iir)
1249e3689190SBen Widawsky {
1250261e40b8SVille Syrjälä 	if (!HAS_L3_DPF(dev_priv))
1251e3689190SBen Widawsky 		return;
1252e3689190SBen Widawsky 
1253d0ecd7e2SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1254261e40b8SVille Syrjälä 	gen5_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv));
1255d0ecd7e2SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
1256e3689190SBen Widawsky 
1257261e40b8SVille Syrjälä 	iir &= GT_PARITY_ERROR(dev_priv);
125835a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
125935a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 1;
126035a85ac6SBen Widawsky 
126135a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
126235a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 0;
126335a85ac6SBen Widawsky 
1264a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
1265e3689190SBen Widawsky }
1266e3689190SBen Widawsky 
1267261e40b8SVille Syrjälä static void ilk_gt_irq_handler(struct drm_i915_private *dev_priv,
1268f1af8fc1SPaulo Zanoni 			       u32 gt_iir)
1269f1af8fc1SPaulo Zanoni {
1270f1af8fc1SPaulo Zanoni 	if (gt_iir &
1271f1af8fc1SPaulo Zanoni 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
12724a570db5STvrtko Ursulin 		notify_ring(&dev_priv->engine[RCS]);
1273f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
12744a570db5STvrtko Ursulin 		notify_ring(&dev_priv->engine[VCS]);
1275f1af8fc1SPaulo Zanoni }
1276f1af8fc1SPaulo Zanoni 
1277261e40b8SVille Syrjälä static void snb_gt_irq_handler(struct drm_i915_private *dev_priv,
1278e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1279e7b4c6b1SDaniel Vetter {
1280e7b4c6b1SDaniel Vetter 
1281cc609d5dSBen Widawsky 	if (gt_iir &
1282cc609d5dSBen Widawsky 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
12834a570db5STvrtko Ursulin 		notify_ring(&dev_priv->engine[RCS]);
1284cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
12854a570db5STvrtko Ursulin 		notify_ring(&dev_priv->engine[VCS]);
1286cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
12874a570db5STvrtko Ursulin 		notify_ring(&dev_priv->engine[BCS]);
1288e7b4c6b1SDaniel Vetter 
1289cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1290cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
1291aaecdf61SDaniel Vetter 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT))
1292aaecdf61SDaniel Vetter 		DRM_DEBUG("Command parser error, gt_iir 0x%08x\n", gt_iir);
1293e3689190SBen Widawsky 
1294261e40b8SVille Syrjälä 	if (gt_iir & GT_PARITY_ERROR(dev_priv))
1295261e40b8SVille Syrjälä 		ivybridge_parity_error_irq_handler(dev_priv, gt_iir);
1296e7b4c6b1SDaniel Vetter }
1297e7b4c6b1SDaniel Vetter 
1298fbcc1a0cSNick Hoath static __always_inline void
12990bc40be8STvrtko Ursulin gen8_cs_irq_handler(struct intel_engine_cs *engine, u32 iir, int test_shift)
1300fbcc1a0cSNick Hoath {
1301fbcc1a0cSNick Hoath 	if (iir & (GT_RENDER_USER_INTERRUPT << test_shift))
13020bc40be8STvrtko Ursulin 		notify_ring(engine);
1303fbcc1a0cSNick Hoath 	if (iir & (GT_CONTEXT_SWITCH_INTERRUPT << test_shift))
130427af5eeaSTvrtko Ursulin 		tasklet_schedule(&engine->irq_tasklet);
1305fbcc1a0cSNick Hoath }
1306fbcc1a0cSNick Hoath 
1307e30e251aSVille Syrjälä static irqreturn_t gen8_gt_irq_ack(struct drm_i915_private *dev_priv,
1308e30e251aSVille Syrjälä 				   u32 master_ctl,
1309e30e251aSVille Syrjälä 				   u32 gt_iir[4])
1310abd58f01SBen Widawsky {
1311abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
1312abd58f01SBen Widawsky 
1313abd58f01SBen Widawsky 	if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
1314e30e251aSVille Syrjälä 		gt_iir[0] = I915_READ_FW(GEN8_GT_IIR(0));
1315e30e251aSVille Syrjälä 		if (gt_iir[0]) {
1316e30e251aSVille Syrjälä 			I915_WRITE_FW(GEN8_GT_IIR(0), gt_iir[0]);
1317abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1318abd58f01SBen Widawsky 		} else
1319abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT0)!\n");
1320abd58f01SBen Widawsky 	}
1321abd58f01SBen Widawsky 
132285f9b5f9SZhao Yakui 	if (master_ctl & (GEN8_GT_VCS1_IRQ | GEN8_GT_VCS2_IRQ)) {
1323e30e251aSVille Syrjälä 		gt_iir[1] = I915_READ_FW(GEN8_GT_IIR(1));
1324e30e251aSVille Syrjälä 		if (gt_iir[1]) {
1325e30e251aSVille Syrjälä 			I915_WRITE_FW(GEN8_GT_IIR(1), gt_iir[1]);
1326abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1327abd58f01SBen Widawsky 		} else
1328abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT1)!\n");
1329abd58f01SBen Widawsky 	}
1330abd58f01SBen Widawsky 
133174cdb337SChris Wilson 	if (master_ctl & GEN8_GT_VECS_IRQ) {
1332e30e251aSVille Syrjälä 		gt_iir[3] = I915_READ_FW(GEN8_GT_IIR(3));
1333e30e251aSVille Syrjälä 		if (gt_iir[3]) {
1334e30e251aSVille Syrjälä 			I915_WRITE_FW(GEN8_GT_IIR(3), gt_iir[3]);
133574cdb337SChris Wilson 			ret = IRQ_HANDLED;
133674cdb337SChris Wilson 		} else
133774cdb337SChris Wilson 			DRM_ERROR("The master control interrupt lied (GT3)!\n");
133874cdb337SChris Wilson 	}
133974cdb337SChris Wilson 
13400961021aSBen Widawsky 	if (master_ctl & GEN8_GT_PM_IRQ) {
1341e30e251aSVille Syrjälä 		gt_iir[2] = I915_READ_FW(GEN8_GT_IIR(2));
1342e30e251aSVille Syrjälä 		if (gt_iir[2] & dev_priv->pm_rps_events) {
1343cb0d205eSChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(2),
1344e30e251aSVille Syrjälä 				      gt_iir[2] & dev_priv->pm_rps_events);
134538cc46d7SOscar Mateo 			ret = IRQ_HANDLED;
13460961021aSBen Widawsky 		} else
13470961021aSBen Widawsky 			DRM_ERROR("The master control interrupt lied (PM)!\n");
13480961021aSBen Widawsky 	}
13490961021aSBen Widawsky 
1350abd58f01SBen Widawsky 	return ret;
1351abd58f01SBen Widawsky }
1352abd58f01SBen Widawsky 
1353e30e251aSVille Syrjälä static void gen8_gt_irq_handler(struct drm_i915_private *dev_priv,
1354e30e251aSVille Syrjälä 				u32 gt_iir[4])
1355e30e251aSVille Syrjälä {
1356e30e251aSVille Syrjälä 	if (gt_iir[0]) {
1357e30e251aSVille Syrjälä 		gen8_cs_irq_handler(&dev_priv->engine[RCS],
1358e30e251aSVille Syrjälä 				    gt_iir[0], GEN8_RCS_IRQ_SHIFT);
1359e30e251aSVille Syrjälä 		gen8_cs_irq_handler(&dev_priv->engine[BCS],
1360e30e251aSVille Syrjälä 				    gt_iir[0], GEN8_BCS_IRQ_SHIFT);
1361e30e251aSVille Syrjälä 	}
1362e30e251aSVille Syrjälä 
1363e30e251aSVille Syrjälä 	if (gt_iir[1]) {
1364e30e251aSVille Syrjälä 		gen8_cs_irq_handler(&dev_priv->engine[VCS],
1365e30e251aSVille Syrjälä 				    gt_iir[1], GEN8_VCS1_IRQ_SHIFT);
1366e30e251aSVille Syrjälä 		gen8_cs_irq_handler(&dev_priv->engine[VCS2],
1367e30e251aSVille Syrjälä 				    gt_iir[1], GEN8_VCS2_IRQ_SHIFT);
1368e30e251aSVille Syrjälä 	}
1369e30e251aSVille Syrjälä 
1370e30e251aSVille Syrjälä 	if (gt_iir[3])
1371e30e251aSVille Syrjälä 		gen8_cs_irq_handler(&dev_priv->engine[VECS],
1372e30e251aSVille Syrjälä 				    gt_iir[3], GEN8_VECS_IRQ_SHIFT);
1373e30e251aSVille Syrjälä 
1374e30e251aSVille Syrjälä 	if (gt_iir[2] & dev_priv->pm_rps_events)
1375e30e251aSVille Syrjälä 		gen6_rps_irq_handler(dev_priv, gt_iir[2]);
1376e30e251aSVille Syrjälä }
1377e30e251aSVille Syrjälä 
137863c88d22SImre Deak static bool bxt_port_hotplug_long_detect(enum port port, u32 val)
137963c88d22SImre Deak {
138063c88d22SImre Deak 	switch (port) {
138163c88d22SImre Deak 	case PORT_A:
1382195baa06SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
138363c88d22SImre Deak 	case PORT_B:
138463c88d22SImre Deak 		return val & PORTB_HOTPLUG_LONG_DETECT;
138563c88d22SImre Deak 	case PORT_C:
138663c88d22SImre Deak 		return val & PORTC_HOTPLUG_LONG_DETECT;
138763c88d22SImre Deak 	default:
138863c88d22SImre Deak 		return false;
138963c88d22SImre Deak 	}
139063c88d22SImre Deak }
139163c88d22SImre Deak 
13926dbf30ceSVille Syrjälä static bool spt_port_hotplug2_long_detect(enum port port, u32 val)
13936dbf30ceSVille Syrjälä {
13946dbf30ceSVille Syrjälä 	switch (port) {
13956dbf30ceSVille Syrjälä 	case PORT_E:
13966dbf30ceSVille Syrjälä 		return val & PORTE_HOTPLUG_LONG_DETECT;
13976dbf30ceSVille Syrjälä 	default:
13986dbf30ceSVille Syrjälä 		return false;
13996dbf30ceSVille Syrjälä 	}
14006dbf30ceSVille Syrjälä }
14016dbf30ceSVille Syrjälä 
140274c0b395SVille Syrjälä static bool spt_port_hotplug_long_detect(enum port port, u32 val)
140374c0b395SVille Syrjälä {
140474c0b395SVille Syrjälä 	switch (port) {
140574c0b395SVille Syrjälä 	case PORT_A:
140674c0b395SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
140774c0b395SVille Syrjälä 	case PORT_B:
140874c0b395SVille Syrjälä 		return val & PORTB_HOTPLUG_LONG_DETECT;
140974c0b395SVille Syrjälä 	case PORT_C:
141074c0b395SVille Syrjälä 		return val & PORTC_HOTPLUG_LONG_DETECT;
141174c0b395SVille Syrjälä 	case PORT_D:
141274c0b395SVille Syrjälä 		return val & PORTD_HOTPLUG_LONG_DETECT;
141374c0b395SVille Syrjälä 	default:
141474c0b395SVille Syrjälä 		return false;
141574c0b395SVille Syrjälä 	}
141674c0b395SVille Syrjälä }
141774c0b395SVille Syrjälä 
1418e4ce95aaSVille Syrjälä static bool ilk_port_hotplug_long_detect(enum port port, u32 val)
1419e4ce95aaSVille Syrjälä {
1420e4ce95aaSVille Syrjälä 	switch (port) {
1421e4ce95aaSVille Syrjälä 	case PORT_A:
1422e4ce95aaSVille Syrjälä 		return val & DIGITAL_PORTA_HOTPLUG_LONG_DETECT;
1423e4ce95aaSVille Syrjälä 	default:
1424e4ce95aaSVille Syrjälä 		return false;
1425e4ce95aaSVille Syrjälä 	}
1426e4ce95aaSVille Syrjälä }
1427e4ce95aaSVille Syrjälä 
1428676574dfSJani Nikula static bool pch_port_hotplug_long_detect(enum port port, u32 val)
142913cf5504SDave Airlie {
143013cf5504SDave Airlie 	switch (port) {
143113cf5504SDave Airlie 	case PORT_B:
1432676574dfSJani Nikula 		return val & PORTB_HOTPLUG_LONG_DETECT;
143313cf5504SDave Airlie 	case PORT_C:
1434676574dfSJani Nikula 		return val & PORTC_HOTPLUG_LONG_DETECT;
143513cf5504SDave Airlie 	case PORT_D:
1436676574dfSJani Nikula 		return val & PORTD_HOTPLUG_LONG_DETECT;
1437676574dfSJani Nikula 	default:
1438676574dfSJani Nikula 		return false;
143913cf5504SDave Airlie 	}
144013cf5504SDave Airlie }
144113cf5504SDave Airlie 
1442676574dfSJani Nikula static bool i9xx_port_hotplug_long_detect(enum port port, u32 val)
144313cf5504SDave Airlie {
144413cf5504SDave Airlie 	switch (port) {
144513cf5504SDave Airlie 	case PORT_B:
1446676574dfSJani Nikula 		return val & PORTB_HOTPLUG_INT_LONG_PULSE;
144713cf5504SDave Airlie 	case PORT_C:
1448676574dfSJani Nikula 		return val & PORTC_HOTPLUG_INT_LONG_PULSE;
144913cf5504SDave Airlie 	case PORT_D:
1450676574dfSJani Nikula 		return val & PORTD_HOTPLUG_INT_LONG_PULSE;
1451676574dfSJani Nikula 	default:
1452676574dfSJani Nikula 		return false;
145313cf5504SDave Airlie 	}
145413cf5504SDave Airlie }
145513cf5504SDave Airlie 
145642db67d6SVille Syrjälä /*
145742db67d6SVille Syrjälä  * Get a bit mask of pins that have triggered, and which ones may be long.
145842db67d6SVille Syrjälä  * This can be called multiple times with the same masks to accumulate
145942db67d6SVille Syrjälä  * hotplug detection results from several registers.
146042db67d6SVille Syrjälä  *
146142db67d6SVille Syrjälä  * Note that the caller is expected to zero out the masks initially.
146242db67d6SVille Syrjälä  */
1463fd63e2a9SImre Deak static void intel_get_hpd_pins(u32 *pin_mask, u32 *long_mask,
14648c841e57SJani Nikula 			     u32 hotplug_trigger, u32 dig_hotplug_reg,
1465fd63e2a9SImre Deak 			     const u32 hpd[HPD_NUM_PINS],
1466fd63e2a9SImre Deak 			     bool long_pulse_detect(enum port port, u32 val))
1467676574dfSJani Nikula {
14688c841e57SJani Nikula 	enum port port;
1469676574dfSJani Nikula 	int i;
1470676574dfSJani Nikula 
1471676574dfSJani Nikula 	for_each_hpd_pin(i) {
14728c841e57SJani Nikula 		if ((hpd[i] & hotplug_trigger) == 0)
14738c841e57SJani Nikula 			continue;
14748c841e57SJani Nikula 
1475676574dfSJani Nikula 		*pin_mask |= BIT(i);
1476676574dfSJani Nikula 
1477cc24fcdcSImre Deak 		if (!intel_hpd_pin_to_port(i, &port))
1478cc24fcdcSImre Deak 			continue;
1479cc24fcdcSImre Deak 
1480fd63e2a9SImre Deak 		if (long_pulse_detect(port, dig_hotplug_reg))
1481676574dfSJani Nikula 			*long_mask |= BIT(i);
1482676574dfSJani Nikula 	}
1483676574dfSJani Nikula 
1484676574dfSJani Nikula 	DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x, dig 0x%08x, pins 0x%08x\n",
1485676574dfSJani Nikula 			 hotplug_trigger, dig_hotplug_reg, *pin_mask);
1486676574dfSJani Nikula 
1487676574dfSJani Nikula }
1488676574dfSJani Nikula 
148991d14251STvrtko Ursulin static void gmbus_irq_handler(struct drm_i915_private *dev_priv)
1490515ac2bbSDaniel Vetter {
149128c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1492515ac2bbSDaniel Vetter }
1493515ac2bbSDaniel Vetter 
149491d14251STvrtko Ursulin static void dp_aux_irq_handler(struct drm_i915_private *dev_priv)
1495ce99c256SDaniel Vetter {
14969ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1497ce99c256SDaniel Vetter }
1498ce99c256SDaniel Vetter 
14998bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
150091d14251STvrtko Ursulin static void display_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
150191d14251STvrtko Ursulin 					 enum pipe pipe,
1502eba94eb9SDaniel Vetter 					 uint32_t crc0, uint32_t crc1,
1503eba94eb9SDaniel Vetter 					 uint32_t crc2, uint32_t crc3,
15048bc5e955SDaniel Vetter 					 uint32_t crc4)
15058bf1e9f1SShuang He {
15068bf1e9f1SShuang He 	struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
15078bf1e9f1SShuang He 	struct intel_pipe_crc_entry *entry;
1508ac2300d4SDamien Lespiau 	int head, tail;
1509b2c88f5bSDamien Lespiau 
1510d538bbdfSDamien Lespiau 	spin_lock(&pipe_crc->lock);
1511d538bbdfSDamien Lespiau 
15120c912c79SDamien Lespiau 	if (!pipe_crc->entries) {
1513d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
151434273620SDaniel Vetter 		DRM_DEBUG_KMS("spurious interrupt\n");
15150c912c79SDamien Lespiau 		return;
15160c912c79SDamien Lespiau 	}
15170c912c79SDamien Lespiau 
1518d538bbdfSDamien Lespiau 	head = pipe_crc->head;
1519d538bbdfSDamien Lespiau 	tail = pipe_crc->tail;
1520b2c88f5bSDamien Lespiau 
1521b2c88f5bSDamien Lespiau 	if (CIRC_SPACE(head, tail, INTEL_PIPE_CRC_ENTRIES_NR) < 1) {
1522d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
1523b2c88f5bSDamien Lespiau 		DRM_ERROR("CRC buffer overflowing\n");
1524b2c88f5bSDamien Lespiau 		return;
1525b2c88f5bSDamien Lespiau 	}
1526b2c88f5bSDamien Lespiau 
1527b2c88f5bSDamien Lespiau 	entry = &pipe_crc->entries[head];
15288bf1e9f1SShuang He 
152991d14251STvrtko Ursulin 	entry->frame = dev_priv->dev->driver->get_vblank_counter(dev_priv->dev,
153091d14251STvrtko Ursulin 								 pipe);
1531eba94eb9SDaniel Vetter 	entry->crc[0] = crc0;
1532eba94eb9SDaniel Vetter 	entry->crc[1] = crc1;
1533eba94eb9SDaniel Vetter 	entry->crc[2] = crc2;
1534eba94eb9SDaniel Vetter 	entry->crc[3] = crc3;
1535eba94eb9SDaniel Vetter 	entry->crc[4] = crc4;
1536b2c88f5bSDamien Lespiau 
1537b2c88f5bSDamien Lespiau 	head = (head + 1) & (INTEL_PIPE_CRC_ENTRIES_NR - 1);
1538d538bbdfSDamien Lespiau 	pipe_crc->head = head;
1539d538bbdfSDamien Lespiau 
1540d538bbdfSDamien Lespiau 	spin_unlock(&pipe_crc->lock);
154107144428SDamien Lespiau 
154207144428SDamien Lespiau 	wake_up_interruptible(&pipe_crc->wq);
15438bf1e9f1SShuang He }
1544277de95eSDaniel Vetter #else
1545277de95eSDaniel Vetter static inline void
154691d14251STvrtko Ursulin display_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
154791d14251STvrtko Ursulin 			     enum pipe pipe,
1548277de95eSDaniel Vetter 			     uint32_t crc0, uint32_t crc1,
1549277de95eSDaniel Vetter 			     uint32_t crc2, uint32_t crc3,
1550277de95eSDaniel Vetter 			     uint32_t crc4) {}
1551277de95eSDaniel Vetter #endif
1552eba94eb9SDaniel Vetter 
1553277de95eSDaniel Vetter 
155491d14251STvrtko Ursulin static void hsw_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
155591d14251STvrtko Ursulin 				     enum pipe pipe)
15565a69b89fSDaniel Vetter {
155791d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
15585a69b89fSDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
15595a69b89fSDaniel Vetter 				     0, 0, 0, 0);
15605a69b89fSDaniel Vetter }
15615a69b89fSDaniel Vetter 
156291d14251STvrtko Ursulin static void ivb_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
156391d14251STvrtko Ursulin 				     enum pipe pipe)
1564eba94eb9SDaniel Vetter {
156591d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
1566eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1567eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1568eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1569eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
15708bc5e955SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1571eba94eb9SDaniel Vetter }
15725b3a856bSDaniel Vetter 
157391d14251STvrtko Ursulin static void i9xx_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
157491d14251STvrtko Ursulin 				      enum pipe pipe)
15755b3a856bSDaniel Vetter {
15760b5c5ed0SDaniel Vetter 	uint32_t res1, res2;
15770b5c5ed0SDaniel Vetter 
157891d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 3)
15790b5c5ed0SDaniel Vetter 		res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
15800b5c5ed0SDaniel Vetter 	else
15810b5c5ed0SDaniel Vetter 		res1 = 0;
15820b5c5ed0SDaniel Vetter 
158391d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
15840b5c5ed0SDaniel Vetter 		res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
15850b5c5ed0SDaniel Vetter 	else
15860b5c5ed0SDaniel Vetter 		res2 = 0;
15875b3a856bSDaniel Vetter 
158891d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
15890b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_RED(pipe)),
15900b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_GREEN(pipe)),
15910b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_BLUE(pipe)),
15920b5c5ed0SDaniel Vetter 				     res1, res2);
15935b3a856bSDaniel Vetter }
15948bf1e9f1SShuang He 
15951403c0d4SPaulo Zanoni /* The RPS events need forcewake, so we add them to a work queue and mask their
15961403c0d4SPaulo Zanoni  * IMR bits until the work is done. Other interrupts can be processed without
15971403c0d4SPaulo Zanoni  * the work queue. */
15981403c0d4SPaulo Zanoni static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1599baf02a1fSBen Widawsky {
1600a6706b45SDeepak S 	if (pm_iir & dev_priv->pm_rps_events) {
160159cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
1602480c8033SDaniel Vetter 		gen6_disable_pm_irq(dev_priv, pm_iir & dev_priv->pm_rps_events);
1603d4d70aa5SImre Deak 		if (dev_priv->rps.interrupts_enabled) {
1604d4d70aa5SImre Deak 			dev_priv->rps.pm_iir |= pm_iir & dev_priv->pm_rps_events;
16052adbee62SDaniel Vetter 			queue_work(dev_priv->wq, &dev_priv->rps.work);
160641a05a3aSDaniel Vetter 		}
1607d4d70aa5SImre Deak 		spin_unlock(&dev_priv->irq_lock);
1608d4d70aa5SImre Deak 	}
1609baf02a1fSBen Widawsky 
1610c9a9a268SImre Deak 	if (INTEL_INFO(dev_priv)->gen >= 8)
1611c9a9a268SImre Deak 		return;
1612c9a9a268SImre Deak 
16132d1fe073SJoonas Lahtinen 	if (HAS_VEBOX(dev_priv)) {
161412638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
16154a570db5STvrtko Ursulin 			notify_ring(&dev_priv->engine[VECS]);
161612638c57SBen Widawsky 
1617aaecdf61SDaniel Vetter 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT)
1618aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, pm_iir 0x%08x\n", pm_iir);
161912638c57SBen Widawsky 	}
16201403c0d4SPaulo Zanoni }
1621baf02a1fSBen Widawsky 
16225a21b665SDaniel Vetter static bool intel_pipe_handle_vblank(struct drm_i915_private *dev_priv,
162391d14251STvrtko Ursulin 				     enum pipe pipe)
16248d7849dbSVille Syrjälä {
16255a21b665SDaniel Vetter 	bool ret;
16265a21b665SDaniel Vetter 
16275a21b665SDaniel Vetter 	ret = drm_handle_vblank(dev_priv->dev, pipe);
16285a21b665SDaniel Vetter 	if (ret)
162951cbaf01SMaarten Lankhorst 		intel_finish_page_flip_mmio(dev_priv, pipe);
16305a21b665SDaniel Vetter 
16315a21b665SDaniel Vetter 	return ret;
16328d7849dbSVille Syrjälä }
16338d7849dbSVille Syrjälä 
163491d14251STvrtko Ursulin static void valleyview_pipestat_irq_ack(struct drm_i915_private *dev_priv,
163591d14251STvrtko Ursulin 					u32 iir, u32 pipe_stats[I915_MAX_PIPES])
16367e231dbeSJesse Barnes {
16377e231dbeSJesse Barnes 	int pipe;
16387e231dbeSJesse Barnes 
163958ead0d7SImre Deak 	spin_lock(&dev_priv->irq_lock);
16401ca993d2SVille Syrjälä 
16411ca993d2SVille Syrjälä 	if (!dev_priv->display_irqs_enabled) {
16421ca993d2SVille Syrjälä 		spin_unlock(&dev_priv->irq_lock);
16431ca993d2SVille Syrjälä 		return;
16441ca993d2SVille Syrjälä 	}
16451ca993d2SVille Syrjälä 
1646055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1647f0f59a00SVille Syrjälä 		i915_reg_t reg;
1648bbb5eebfSDaniel Vetter 		u32 mask, iir_bit = 0;
164991d181ddSImre Deak 
1650bbb5eebfSDaniel Vetter 		/*
1651bbb5eebfSDaniel Vetter 		 * PIPESTAT bits get signalled even when the interrupt is
1652bbb5eebfSDaniel Vetter 		 * disabled with the mask bits, and some of the status bits do
1653bbb5eebfSDaniel Vetter 		 * not generate interrupts at all (like the underrun bit). Hence
1654bbb5eebfSDaniel Vetter 		 * we need to be careful that we only handle what we want to
1655bbb5eebfSDaniel Vetter 		 * handle.
1656bbb5eebfSDaniel Vetter 		 */
16570f239f4cSDaniel Vetter 
16580f239f4cSDaniel Vetter 		/* fifo underruns are filterered in the underrun handler. */
16590f239f4cSDaniel Vetter 		mask = PIPE_FIFO_UNDERRUN_STATUS;
1660bbb5eebfSDaniel Vetter 
1661bbb5eebfSDaniel Vetter 		switch (pipe) {
1662bbb5eebfSDaniel Vetter 		case PIPE_A:
1663bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
1664bbb5eebfSDaniel Vetter 			break;
1665bbb5eebfSDaniel Vetter 		case PIPE_B:
1666bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
1667bbb5eebfSDaniel Vetter 			break;
16683278f67fSVille Syrjälä 		case PIPE_C:
16693278f67fSVille Syrjälä 			iir_bit = I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
16703278f67fSVille Syrjälä 			break;
1671bbb5eebfSDaniel Vetter 		}
1672bbb5eebfSDaniel Vetter 		if (iir & iir_bit)
1673bbb5eebfSDaniel Vetter 			mask |= dev_priv->pipestat_irq_mask[pipe];
1674bbb5eebfSDaniel Vetter 
1675bbb5eebfSDaniel Vetter 		if (!mask)
167691d181ddSImre Deak 			continue;
167791d181ddSImre Deak 
167891d181ddSImre Deak 		reg = PIPESTAT(pipe);
1679bbb5eebfSDaniel Vetter 		mask |= PIPESTAT_INT_ENABLE_MASK;
1680bbb5eebfSDaniel Vetter 		pipe_stats[pipe] = I915_READ(reg) & mask;
16817e231dbeSJesse Barnes 
16827e231dbeSJesse Barnes 		/*
16837e231dbeSJesse Barnes 		 * Clear the PIPE*STAT regs before the IIR
16847e231dbeSJesse Barnes 		 */
168591d181ddSImre Deak 		if (pipe_stats[pipe] & (PIPE_FIFO_UNDERRUN_STATUS |
168691d181ddSImre Deak 					PIPESTAT_INT_STATUS_MASK))
16877e231dbeSJesse Barnes 			I915_WRITE(reg, pipe_stats[pipe]);
16887e231dbeSJesse Barnes 	}
168958ead0d7SImre Deak 	spin_unlock(&dev_priv->irq_lock);
16902ecb8ca4SVille Syrjälä }
16912ecb8ca4SVille Syrjälä 
169291d14251STvrtko Ursulin static void valleyview_pipestat_irq_handler(struct drm_i915_private *dev_priv,
16932ecb8ca4SVille Syrjälä 					    u32 pipe_stats[I915_MAX_PIPES])
16942ecb8ca4SVille Syrjälä {
16952ecb8ca4SVille Syrjälä 	enum pipe pipe;
16967e231dbeSJesse Barnes 
1697055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
16985a21b665SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
16995a21b665SDaniel Vetter 		    intel_pipe_handle_vblank(dev_priv, pipe))
17005a21b665SDaniel Vetter 			intel_check_page_flip(dev_priv, pipe);
170131acc7f5SJesse Barnes 
17025251f04eSMaarten Lankhorst 		if (pipe_stats[pipe] & PLANE_FLIP_DONE_INT_STATUS_VLV)
170351cbaf01SMaarten Lankhorst 			intel_finish_page_flip_cs(dev_priv, pipe);
17044356d586SDaniel Vetter 
17054356d586SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
170691d14251STvrtko Ursulin 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
17072d9d2b0bSVille Syrjälä 
17081f7247c0SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
17091f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
171031acc7f5SJesse Barnes 	}
171131acc7f5SJesse Barnes 
1712c1874ed7SImre Deak 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
171391d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
1714c1874ed7SImre Deak }
1715c1874ed7SImre Deak 
17161ae3c34cSVille Syrjälä static u32 i9xx_hpd_irq_ack(struct drm_i915_private *dev_priv)
171716c6c56bSVille Syrjälä {
171816c6c56bSVille Syrjälä 	u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
171916c6c56bSVille Syrjälä 
17201ae3c34cSVille Syrjälä 	if (hotplug_status)
17213ff60f89SOscar Mateo 		I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
17221ae3c34cSVille Syrjälä 
17231ae3c34cSVille Syrjälä 	return hotplug_status;
17241ae3c34cSVille Syrjälä }
17251ae3c34cSVille Syrjälä 
172691d14251STvrtko Ursulin static void i9xx_hpd_irq_handler(struct drm_i915_private *dev_priv,
17271ae3c34cSVille Syrjälä 				 u32 hotplug_status)
17281ae3c34cSVille Syrjälä {
17291ae3c34cSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
17303ff60f89SOscar Mateo 
173191d14251STvrtko Ursulin 	if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
173291d14251STvrtko Ursulin 	    IS_CHERRYVIEW(dev_priv)) {
173316c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
173416c6c56bSVille Syrjälä 
173558f2cf24SVille Syrjälä 		if (hotplug_trigger) {
1736fd63e2a9SImre Deak 			intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
1737fd63e2a9SImre Deak 					   hotplug_trigger, hpd_status_g4x,
1738fd63e2a9SImre Deak 					   i9xx_port_hotplug_long_detect);
173958f2cf24SVille Syrjälä 
174091d14251STvrtko Ursulin 			intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
174158f2cf24SVille Syrjälä 		}
1742369712e8SJani Nikula 
1743369712e8SJani Nikula 		if (hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
174491d14251STvrtko Ursulin 			dp_aux_irq_handler(dev_priv);
174516c6c56bSVille Syrjälä 	} else {
174616c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
174716c6c56bSVille Syrjälä 
174858f2cf24SVille Syrjälä 		if (hotplug_trigger) {
1749fd63e2a9SImre Deak 			intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
17504e3d1e26SVille Syrjälä 					   hotplug_trigger, hpd_status_i915,
1751fd63e2a9SImre Deak 					   i9xx_port_hotplug_long_detect);
175291d14251STvrtko Ursulin 			intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
175316c6c56bSVille Syrjälä 		}
17543ff60f89SOscar Mateo 	}
175558f2cf24SVille Syrjälä }
175616c6c56bSVille Syrjälä 
1757c1874ed7SImre Deak static irqreturn_t valleyview_irq_handler(int irq, void *arg)
1758c1874ed7SImre Deak {
175945a83f84SDaniel Vetter 	struct drm_device *dev = arg;
17602d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1761c1874ed7SImre Deak 	irqreturn_t ret = IRQ_NONE;
1762c1874ed7SImre Deak 
17632dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
17642dd2a883SImre Deak 		return IRQ_NONE;
17652dd2a883SImre Deak 
17661f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
17671f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
17681f814dacSImre Deak 
17691e1cace9SVille Syrjälä 	do {
17706e814800SVille Syrjälä 		u32 iir, gt_iir, pm_iir;
17712ecb8ca4SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
17721ae3c34cSVille Syrjälä 		u32 hotplug_status = 0;
1773a5e485a9SVille Syrjälä 		u32 ier = 0;
17743ff60f89SOscar Mateo 
1775c1874ed7SImre Deak 		gt_iir = I915_READ(GTIIR);
1776c1874ed7SImre Deak 		pm_iir = I915_READ(GEN6_PMIIR);
17773ff60f89SOscar Mateo 		iir = I915_READ(VLV_IIR);
1778c1874ed7SImre Deak 
1779c1874ed7SImre Deak 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
17801e1cace9SVille Syrjälä 			break;
1781c1874ed7SImre Deak 
1782c1874ed7SImre Deak 		ret = IRQ_HANDLED;
1783c1874ed7SImre Deak 
1784a5e485a9SVille Syrjälä 		/*
1785a5e485a9SVille Syrjälä 		 * Theory on interrupt generation, based on empirical evidence:
1786a5e485a9SVille Syrjälä 		 *
1787a5e485a9SVille Syrjälä 		 * x = ((VLV_IIR & VLV_IER) ||
1788a5e485a9SVille Syrjälä 		 *      (((GT_IIR & GT_IER) || (GEN6_PMIIR & GEN6_PMIER)) &&
1789a5e485a9SVille Syrjälä 		 *       (VLV_MASTER_IER & MASTER_INTERRUPT_ENABLE)));
1790a5e485a9SVille Syrjälä 		 *
1791a5e485a9SVille Syrjälä 		 * A CPU interrupt will only be raised when 'x' has a 0->1 edge.
1792a5e485a9SVille Syrjälä 		 * Hence we clear MASTER_INTERRUPT_ENABLE and VLV_IER to
1793a5e485a9SVille Syrjälä 		 * guarantee the CPU interrupt will be raised again even if we
1794a5e485a9SVille Syrjälä 		 * don't end up clearing all the VLV_IIR, GT_IIR, GEN6_PMIIR
1795a5e485a9SVille Syrjälä 		 * bits this time around.
1796a5e485a9SVille Syrjälä 		 */
17974a0a0202SVille Syrjälä 		I915_WRITE(VLV_MASTER_IER, 0);
1798a5e485a9SVille Syrjälä 		ier = I915_READ(VLV_IER);
1799a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, 0);
18004a0a0202SVille Syrjälä 
18014a0a0202SVille Syrjälä 		if (gt_iir)
18024a0a0202SVille Syrjälä 			I915_WRITE(GTIIR, gt_iir);
18034a0a0202SVille Syrjälä 		if (pm_iir)
18044a0a0202SVille Syrjälä 			I915_WRITE(GEN6_PMIIR, pm_iir);
18054a0a0202SVille Syrjälä 
18067ce4d1f2SVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
18071ae3c34cSVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
18087ce4d1f2SVille Syrjälä 
18093ff60f89SOscar Mateo 		/* Call regardless, as some status bits might not be
18103ff60f89SOscar Mateo 		 * signalled in iir */
181191d14251STvrtko Ursulin 		valleyview_pipestat_irq_ack(dev_priv, iir, pipe_stats);
18127ce4d1f2SVille Syrjälä 
18137ce4d1f2SVille Syrjälä 		/*
18147ce4d1f2SVille Syrjälä 		 * VLV_IIR is single buffered, and reflects the level
18157ce4d1f2SVille Syrjälä 		 * from PIPESTAT/PORT_HOTPLUG_STAT, hence clear it last.
18167ce4d1f2SVille Syrjälä 		 */
18177ce4d1f2SVille Syrjälä 		if (iir)
18187ce4d1f2SVille Syrjälä 			I915_WRITE(VLV_IIR, iir);
18194a0a0202SVille Syrjälä 
1820a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, ier);
18214a0a0202SVille Syrjälä 		I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
18224a0a0202SVille Syrjälä 		POSTING_READ(VLV_MASTER_IER);
18231ae3c34cSVille Syrjälä 
182452894874SVille Syrjälä 		if (gt_iir)
1825261e40b8SVille Syrjälä 			snb_gt_irq_handler(dev_priv, gt_iir);
182652894874SVille Syrjälä 		if (pm_iir)
182752894874SVille Syrjälä 			gen6_rps_irq_handler(dev_priv, pm_iir);
182852894874SVille Syrjälä 
18291ae3c34cSVille Syrjälä 		if (hotplug_status)
183091d14251STvrtko Ursulin 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
18312ecb8ca4SVille Syrjälä 
183291d14251STvrtko Ursulin 		valleyview_pipestat_irq_handler(dev_priv, pipe_stats);
18331e1cace9SVille Syrjälä 	} while (0);
18347e231dbeSJesse Barnes 
18351f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
18361f814dacSImre Deak 
18377e231dbeSJesse Barnes 	return ret;
18387e231dbeSJesse Barnes }
18397e231dbeSJesse Barnes 
184043f328d7SVille Syrjälä static irqreturn_t cherryview_irq_handler(int irq, void *arg)
184143f328d7SVille Syrjälä {
184245a83f84SDaniel Vetter 	struct drm_device *dev = arg;
184343f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
184443f328d7SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
184543f328d7SVille Syrjälä 
18462dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
18472dd2a883SImre Deak 		return IRQ_NONE;
18482dd2a883SImre Deak 
18491f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
18501f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
18511f814dacSImre Deak 
1852579de73bSChris Wilson 	do {
18536e814800SVille Syrjälä 		u32 master_ctl, iir;
1854e30e251aSVille Syrjälä 		u32 gt_iir[4] = {};
18552ecb8ca4SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
18561ae3c34cSVille Syrjälä 		u32 hotplug_status = 0;
1857a5e485a9SVille Syrjälä 		u32 ier = 0;
1858a5e485a9SVille Syrjälä 
18598e5fd599SVille Syrjälä 		master_ctl = I915_READ(GEN8_MASTER_IRQ) & ~GEN8_MASTER_IRQ_CONTROL;
18603278f67fSVille Syrjälä 		iir = I915_READ(VLV_IIR);
18613278f67fSVille Syrjälä 
18623278f67fSVille Syrjälä 		if (master_ctl == 0 && iir == 0)
18638e5fd599SVille Syrjälä 			break;
186443f328d7SVille Syrjälä 
186527b6c122SOscar Mateo 		ret = IRQ_HANDLED;
186627b6c122SOscar Mateo 
1867a5e485a9SVille Syrjälä 		/*
1868a5e485a9SVille Syrjälä 		 * Theory on interrupt generation, based on empirical evidence:
1869a5e485a9SVille Syrjälä 		 *
1870a5e485a9SVille Syrjälä 		 * x = ((VLV_IIR & VLV_IER) ||
1871a5e485a9SVille Syrjälä 		 *      ((GEN8_MASTER_IRQ & ~GEN8_MASTER_IRQ_CONTROL) &&
1872a5e485a9SVille Syrjälä 		 *       (GEN8_MASTER_IRQ & GEN8_MASTER_IRQ_CONTROL)));
1873a5e485a9SVille Syrjälä 		 *
1874a5e485a9SVille Syrjälä 		 * A CPU interrupt will only be raised when 'x' has a 0->1 edge.
1875a5e485a9SVille Syrjälä 		 * Hence we clear GEN8_MASTER_IRQ_CONTROL and VLV_IER to
1876a5e485a9SVille Syrjälä 		 * guarantee the CPU interrupt will be raised again even if we
1877a5e485a9SVille Syrjälä 		 * don't end up clearing all the VLV_IIR and GEN8_MASTER_IRQ_CONTROL
1878a5e485a9SVille Syrjälä 		 * bits this time around.
1879a5e485a9SVille Syrjälä 		 */
188043f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, 0);
1881a5e485a9SVille Syrjälä 		ier = I915_READ(VLV_IER);
1882a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, 0);
188343f328d7SVille Syrjälä 
1884e30e251aSVille Syrjälä 		gen8_gt_irq_ack(dev_priv, master_ctl, gt_iir);
188527b6c122SOscar Mateo 
188627b6c122SOscar Mateo 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
18871ae3c34cSVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
188843f328d7SVille Syrjälä 
188927b6c122SOscar Mateo 		/* Call regardless, as some status bits might not be
189027b6c122SOscar Mateo 		 * signalled in iir */
189191d14251STvrtko Ursulin 		valleyview_pipestat_irq_ack(dev_priv, iir, pipe_stats);
189243f328d7SVille Syrjälä 
18937ce4d1f2SVille Syrjälä 		/*
18947ce4d1f2SVille Syrjälä 		 * VLV_IIR is single buffered, and reflects the level
18957ce4d1f2SVille Syrjälä 		 * from PIPESTAT/PORT_HOTPLUG_STAT, hence clear it last.
18967ce4d1f2SVille Syrjälä 		 */
18977ce4d1f2SVille Syrjälä 		if (iir)
18987ce4d1f2SVille Syrjälä 			I915_WRITE(VLV_IIR, iir);
18997ce4d1f2SVille Syrjälä 
1900a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, ier);
1901e5328c43SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
190243f328d7SVille Syrjälä 		POSTING_READ(GEN8_MASTER_IRQ);
19031ae3c34cSVille Syrjälä 
1904e30e251aSVille Syrjälä 		gen8_gt_irq_handler(dev_priv, gt_iir);
1905e30e251aSVille Syrjälä 
19061ae3c34cSVille Syrjälä 		if (hotplug_status)
190791d14251STvrtko Ursulin 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
19082ecb8ca4SVille Syrjälä 
190991d14251STvrtko Ursulin 		valleyview_pipestat_irq_handler(dev_priv, pipe_stats);
1910579de73bSChris Wilson 	} while (0);
19113278f67fSVille Syrjälä 
19121f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
19131f814dacSImre Deak 
191443f328d7SVille Syrjälä 	return ret;
191543f328d7SVille Syrjälä }
191643f328d7SVille Syrjälä 
191791d14251STvrtko Ursulin static void ibx_hpd_irq_handler(struct drm_i915_private *dev_priv,
191891d14251STvrtko Ursulin 				u32 hotplug_trigger,
191940e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
1920776ad806SJesse Barnes {
192142db67d6SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
1922776ad806SJesse Barnes 
19236a39d7c9SJani Nikula 	/*
19246a39d7c9SJani Nikula 	 * Somehow the PCH doesn't seem to really ack the interrupt to the CPU
19256a39d7c9SJani Nikula 	 * unless we touch the hotplug register, even if hotplug_trigger is
19266a39d7c9SJani Nikula 	 * zero. Not acking leads to "The master control interrupt lied (SDE)!"
19276a39d7c9SJani Nikula 	 * errors.
19286a39d7c9SJani Nikula 	 */
192913cf5504SDave Airlie 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
19306a39d7c9SJani Nikula 	if (!hotplug_trigger) {
19316a39d7c9SJani Nikula 		u32 mask = PORTA_HOTPLUG_STATUS_MASK |
19326a39d7c9SJani Nikula 			PORTD_HOTPLUG_STATUS_MASK |
19336a39d7c9SJani Nikula 			PORTC_HOTPLUG_STATUS_MASK |
19346a39d7c9SJani Nikula 			PORTB_HOTPLUG_STATUS_MASK;
19356a39d7c9SJani Nikula 		dig_hotplug_reg &= ~mask;
19366a39d7c9SJani Nikula 	}
19376a39d7c9SJani Nikula 
193813cf5504SDave Airlie 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
19396a39d7c9SJani Nikula 	if (!hotplug_trigger)
19406a39d7c9SJani Nikula 		return;
194113cf5504SDave Airlie 
1942fd63e2a9SImre Deak 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
194340e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
1944fd63e2a9SImre Deak 			   pch_port_hotplug_long_detect);
194540e56410SVille Syrjälä 
194691d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
1947aaf5ec2eSSonika Jindal }
194891d131d2SDaniel Vetter 
194991d14251STvrtko Ursulin static void ibx_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
195040e56410SVille Syrjälä {
195140e56410SVille Syrjälä 	int pipe;
195240e56410SVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
195340e56410SVille Syrjälä 
195491d14251STvrtko Ursulin 	ibx_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ibx);
195540e56410SVille Syrjälä 
1956cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1957cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1958776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1959cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1960cfc33bf7SVille Syrjälä 				 port_name(port));
1961cfc33bf7SVille Syrjälä 	}
1962776ad806SJesse Barnes 
1963ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
196491d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
1965ce99c256SDaniel Vetter 
1966776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
196791d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
1968776ad806SJesse Barnes 
1969776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1970776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1971776ad806SJesse Barnes 
1972776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1973776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1974776ad806SJesse Barnes 
1975776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1976776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
1977776ad806SJesse Barnes 
19789db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
1979055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
19809db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
19819db4a9c7SJesse Barnes 					 pipe_name(pipe),
19829db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
1983776ad806SJesse Barnes 
1984776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1985776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1986776ad806SJesse Barnes 
1987776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1988776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1989776ad806SJesse Barnes 
1990776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
19911f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
19928664281bSPaulo Zanoni 
19938664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
19941f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
19958664281bSPaulo Zanoni }
19968664281bSPaulo Zanoni 
199791d14251STvrtko Ursulin static void ivb_err_int_handler(struct drm_i915_private *dev_priv)
19988664281bSPaulo Zanoni {
19998664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
20005a69b89fSDaniel Vetter 	enum pipe pipe;
20018664281bSPaulo Zanoni 
2002de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
2003de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
2004de032bf4SPaulo Zanoni 
2005055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
20061f7247c0SDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe))
20071f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
20088664281bSPaulo Zanoni 
20095a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
201091d14251STvrtko Ursulin 			if (IS_IVYBRIDGE(dev_priv))
201191d14251STvrtko Ursulin 				ivb_pipe_crc_irq_handler(dev_priv, pipe);
20125a69b89fSDaniel Vetter 			else
201391d14251STvrtko Ursulin 				hsw_pipe_crc_irq_handler(dev_priv, pipe);
20145a69b89fSDaniel Vetter 		}
20155a69b89fSDaniel Vetter 	}
20168bf1e9f1SShuang He 
20178664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
20188664281bSPaulo Zanoni }
20198664281bSPaulo Zanoni 
202091d14251STvrtko Ursulin static void cpt_serr_int_handler(struct drm_i915_private *dev_priv)
20218664281bSPaulo Zanoni {
20228664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
20238664281bSPaulo Zanoni 
2024de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
2025de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
2026de032bf4SPaulo Zanoni 
20278664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
20281f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
20298664281bSPaulo Zanoni 
20308664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
20311f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
20328664281bSPaulo Zanoni 
20338664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
20341f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_C);
20358664281bSPaulo Zanoni 
20368664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
2037776ad806SJesse Barnes }
2038776ad806SJesse Barnes 
203991d14251STvrtko Ursulin static void cpt_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
204023e81d69SAdam Jackson {
204123e81d69SAdam Jackson 	int pipe;
20426dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
2043aaf5ec2eSSonika Jindal 
204491d14251STvrtko Ursulin 	ibx_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_cpt);
204591d131d2SDaniel Vetter 
2046cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
2047cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
204823e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
2049cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
2050cfc33bf7SVille Syrjälä 				 port_name(port));
2051cfc33bf7SVille Syrjälä 	}
205223e81d69SAdam Jackson 
205323e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
205491d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
205523e81d69SAdam Jackson 
205623e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
205791d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
205823e81d69SAdam Jackson 
205923e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
206023e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
206123e81d69SAdam Jackson 
206223e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
206323e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
206423e81d69SAdam Jackson 
206523e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
2066055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
206723e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
206823e81d69SAdam Jackson 					 pipe_name(pipe),
206923e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
20708664281bSPaulo Zanoni 
20718664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
207291d14251STvrtko Ursulin 		cpt_serr_int_handler(dev_priv);
207323e81d69SAdam Jackson }
207423e81d69SAdam Jackson 
207591d14251STvrtko Ursulin static void spt_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
20766dbf30ceSVille Syrjälä {
20776dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_SPT &
20786dbf30ceSVille Syrjälä 		~SDE_PORTE_HOTPLUG_SPT;
20796dbf30ceSVille Syrjälä 	u32 hotplug2_trigger = pch_iir & SDE_PORTE_HOTPLUG_SPT;
20806dbf30ceSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
20816dbf30ceSVille Syrjälä 
20826dbf30ceSVille Syrjälä 	if (hotplug_trigger) {
20836dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
20846dbf30ceSVille Syrjälä 
20856dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
20866dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
20876dbf30ceSVille Syrjälä 
20886dbf30ceSVille Syrjälä 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
20896dbf30ceSVille Syrjälä 				   dig_hotplug_reg, hpd_spt,
209074c0b395SVille Syrjälä 				   spt_port_hotplug_long_detect);
20916dbf30ceSVille Syrjälä 	}
20926dbf30ceSVille Syrjälä 
20936dbf30ceSVille Syrjälä 	if (hotplug2_trigger) {
20946dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
20956dbf30ceSVille Syrjälä 
20966dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG2);
20976dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG2, dig_hotplug_reg);
20986dbf30ceSVille Syrjälä 
20996dbf30ceSVille Syrjälä 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug2_trigger,
21006dbf30ceSVille Syrjälä 				   dig_hotplug_reg, hpd_spt,
21016dbf30ceSVille Syrjälä 				   spt_port_hotplug2_long_detect);
21026dbf30ceSVille Syrjälä 	}
21036dbf30ceSVille Syrjälä 
21046dbf30ceSVille Syrjälä 	if (pin_mask)
210591d14251STvrtko Ursulin 		intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
21066dbf30ceSVille Syrjälä 
21076dbf30ceSVille Syrjälä 	if (pch_iir & SDE_GMBUS_CPT)
210891d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
21096dbf30ceSVille Syrjälä }
21106dbf30ceSVille Syrjälä 
211191d14251STvrtko Ursulin static void ilk_hpd_irq_handler(struct drm_i915_private *dev_priv,
211291d14251STvrtko Ursulin 				u32 hotplug_trigger,
211340e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2114c008bc6eSPaulo Zanoni {
2115e4ce95aaSVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2116e4ce95aaSVille Syrjälä 
2117e4ce95aaSVille Syrjälä 	dig_hotplug_reg = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
2118e4ce95aaSVille Syrjälä 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, dig_hotplug_reg);
2119e4ce95aaSVille Syrjälä 
2120e4ce95aaSVille Syrjälä 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
212140e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2122e4ce95aaSVille Syrjälä 			   ilk_port_hotplug_long_detect);
212340e56410SVille Syrjälä 
212491d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2125e4ce95aaSVille Syrjälä }
2126c008bc6eSPaulo Zanoni 
212791d14251STvrtko Ursulin static void ilk_display_irq_handler(struct drm_i915_private *dev_priv,
212891d14251STvrtko Ursulin 				    u32 de_iir)
212940e56410SVille Syrjälä {
213040e56410SVille Syrjälä 	enum pipe pipe;
213140e56410SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG;
213240e56410SVille Syrjälä 
213340e56410SVille Syrjälä 	if (hotplug_trigger)
213491d14251STvrtko Ursulin 		ilk_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ilk);
213540e56410SVille Syrjälä 
2136c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
213791d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
2138c008bc6eSPaulo Zanoni 
2139c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
214091d14251STvrtko Ursulin 		intel_opregion_asle_intr(dev_priv);
2141c008bc6eSPaulo Zanoni 
2142c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
2143c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
2144c008bc6eSPaulo Zanoni 
2145055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
21465a21b665SDaniel Vetter 		if (de_iir & DE_PIPE_VBLANK(pipe) &&
21475a21b665SDaniel Vetter 		    intel_pipe_handle_vblank(dev_priv, pipe))
21485a21b665SDaniel Vetter 			intel_check_page_flip(dev_priv, pipe);
2149c008bc6eSPaulo Zanoni 
215040da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
21511f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
2152c008bc6eSPaulo Zanoni 
215340da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
215491d14251STvrtko Ursulin 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
21555b3a856bSDaniel Vetter 
215640da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
21575251f04eSMaarten Lankhorst 		if (de_iir & DE_PLANE_FLIP_DONE(pipe))
215851cbaf01SMaarten Lankhorst 			intel_finish_page_flip_cs(dev_priv, pipe);
2159c008bc6eSPaulo Zanoni 	}
2160c008bc6eSPaulo Zanoni 
2161c008bc6eSPaulo Zanoni 	/* check event from PCH */
2162c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
2163c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
2164c008bc6eSPaulo Zanoni 
216591d14251STvrtko Ursulin 		if (HAS_PCH_CPT(dev_priv))
216691d14251STvrtko Ursulin 			cpt_irq_handler(dev_priv, pch_iir);
2167c008bc6eSPaulo Zanoni 		else
216891d14251STvrtko Ursulin 			ibx_irq_handler(dev_priv, pch_iir);
2169c008bc6eSPaulo Zanoni 
2170c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
2171c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
2172c008bc6eSPaulo Zanoni 	}
2173c008bc6eSPaulo Zanoni 
217491d14251STvrtko Ursulin 	if (IS_GEN5(dev_priv) && de_iir & DE_PCU_EVENT)
217591d14251STvrtko Ursulin 		ironlake_rps_change_irq_handler(dev_priv);
2176c008bc6eSPaulo Zanoni }
2177c008bc6eSPaulo Zanoni 
217891d14251STvrtko Ursulin static void ivb_display_irq_handler(struct drm_i915_private *dev_priv,
217991d14251STvrtko Ursulin 				    u32 de_iir)
21809719fb98SPaulo Zanoni {
218107d27e20SDamien Lespiau 	enum pipe pipe;
218223bb4cb5SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG_IVB;
218323bb4cb5SVille Syrjälä 
218440e56410SVille Syrjälä 	if (hotplug_trigger)
218591d14251STvrtko Ursulin 		ilk_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ivb);
21869719fb98SPaulo Zanoni 
21879719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
218891d14251STvrtko Ursulin 		ivb_err_int_handler(dev_priv);
21899719fb98SPaulo Zanoni 
21909719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
219191d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
21929719fb98SPaulo Zanoni 
21939719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
219491d14251STvrtko Ursulin 		intel_opregion_asle_intr(dev_priv);
21959719fb98SPaulo Zanoni 
2196055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
21975a21b665SDaniel Vetter 		if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)) &&
21985a21b665SDaniel Vetter 		    intel_pipe_handle_vblank(dev_priv, pipe))
21995a21b665SDaniel Vetter 			intel_check_page_flip(dev_priv, pipe);
220040da17c2SDaniel Vetter 
220140da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
22025251f04eSMaarten Lankhorst 		if (de_iir & DE_PLANE_FLIP_DONE_IVB(pipe))
220351cbaf01SMaarten Lankhorst 			intel_finish_page_flip_cs(dev_priv, pipe);
22049719fb98SPaulo Zanoni 	}
22059719fb98SPaulo Zanoni 
22069719fb98SPaulo Zanoni 	/* check event from PCH */
220791d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv) && (de_iir & DE_PCH_EVENT_IVB)) {
22089719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
22099719fb98SPaulo Zanoni 
221091d14251STvrtko Ursulin 		cpt_irq_handler(dev_priv, pch_iir);
22119719fb98SPaulo Zanoni 
22129719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
22139719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
22149719fb98SPaulo Zanoni 	}
22159719fb98SPaulo Zanoni }
22169719fb98SPaulo Zanoni 
221772c90f62SOscar Mateo /*
221872c90f62SOscar Mateo  * To handle irqs with the minimum potential races with fresh interrupts, we:
221972c90f62SOscar Mateo  * 1 - Disable Master Interrupt Control.
222072c90f62SOscar Mateo  * 2 - Find the source(s) of the interrupt.
222172c90f62SOscar Mateo  * 3 - Clear the Interrupt Identity bits (IIR).
222272c90f62SOscar Mateo  * 4 - Process the interrupt(s) that had bits set in the IIRs.
222372c90f62SOscar Mateo  * 5 - Re-enable Master Interrupt Control.
222472c90f62SOscar Mateo  */
2225f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
2226b1f14ad0SJesse Barnes {
222745a83f84SDaniel Vetter 	struct drm_device *dev = arg;
22282d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2229f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
22300e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
2231b1f14ad0SJesse Barnes 
22322dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
22332dd2a883SImre Deak 		return IRQ_NONE;
22342dd2a883SImre Deak 
22351f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
22361f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
22371f814dacSImre Deak 
2238b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
2239b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
2240b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
224123a78516SPaulo Zanoni 	POSTING_READ(DEIER);
22420e43406bSChris Wilson 
224344498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
224444498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
224544498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
224644498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
224744498aeaSPaulo Zanoni 	 * due to its back queue). */
224891d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv)) {
224944498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
225044498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
225144498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2252ab5c608bSBen Widawsky 	}
225344498aeaSPaulo Zanoni 
225472c90f62SOscar Mateo 	/* Find, clear, then process each source of interrupt */
225572c90f62SOscar Mateo 
22560e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
22570e43406bSChris Wilson 	if (gt_iir) {
225872c90f62SOscar Mateo 		I915_WRITE(GTIIR, gt_iir);
225972c90f62SOscar Mateo 		ret = IRQ_HANDLED;
226091d14251STvrtko Ursulin 		if (INTEL_GEN(dev_priv) >= 6)
2261261e40b8SVille Syrjälä 			snb_gt_irq_handler(dev_priv, gt_iir);
2262d8fc8a47SPaulo Zanoni 		else
2263261e40b8SVille Syrjälä 			ilk_gt_irq_handler(dev_priv, gt_iir);
22640e43406bSChris Wilson 	}
2265b1f14ad0SJesse Barnes 
2266b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
22670e43406bSChris Wilson 	if (de_iir) {
226872c90f62SOscar Mateo 		I915_WRITE(DEIIR, de_iir);
226972c90f62SOscar Mateo 		ret = IRQ_HANDLED;
227091d14251STvrtko Ursulin 		if (INTEL_GEN(dev_priv) >= 7)
227191d14251STvrtko Ursulin 			ivb_display_irq_handler(dev_priv, de_iir);
2272f1af8fc1SPaulo Zanoni 		else
227391d14251STvrtko Ursulin 			ilk_display_irq_handler(dev_priv, de_iir);
22740e43406bSChris Wilson 	}
22750e43406bSChris Wilson 
227691d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 6) {
2277f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
22780e43406bSChris Wilson 		if (pm_iir) {
2279b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
22800e43406bSChris Wilson 			ret = IRQ_HANDLED;
228172c90f62SOscar Mateo 			gen6_rps_irq_handler(dev_priv, pm_iir);
22820e43406bSChris Wilson 		}
2283f1af8fc1SPaulo Zanoni 	}
2284b1f14ad0SJesse Barnes 
2285b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
2286b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
228791d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv)) {
228844498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
228944498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2290ab5c608bSBen Widawsky 	}
2291b1f14ad0SJesse Barnes 
22921f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
22931f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
22941f814dacSImre Deak 
2295b1f14ad0SJesse Barnes 	return ret;
2296b1f14ad0SJesse Barnes }
2297b1f14ad0SJesse Barnes 
229891d14251STvrtko Ursulin static void bxt_hpd_irq_handler(struct drm_i915_private *dev_priv,
229991d14251STvrtko Ursulin 				u32 hotplug_trigger,
230040e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2301d04a492dSShashank Sharma {
2302cebd87a0SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2303d04a492dSShashank Sharma 
2304a52bb15bSVille Syrjälä 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
2305a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
2306d04a492dSShashank Sharma 
2307cebd87a0SVille Syrjälä 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
230840e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2309cebd87a0SVille Syrjälä 			   bxt_port_hotplug_long_detect);
231040e56410SVille Syrjälä 
231191d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2312d04a492dSShashank Sharma }
2313d04a492dSShashank Sharma 
2314f11a0f46STvrtko Ursulin static irqreturn_t
2315f11a0f46STvrtko Ursulin gen8_de_irq_handler(struct drm_i915_private *dev_priv, u32 master_ctl)
2316abd58f01SBen Widawsky {
2317abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
2318f11a0f46STvrtko Ursulin 	u32 iir;
2319c42664ccSDaniel Vetter 	enum pipe pipe;
232088e04703SJesse Barnes 
2321abd58f01SBen Widawsky 	if (master_ctl & GEN8_DE_MISC_IRQ) {
2322e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_MISC_IIR);
2323e32192e1STvrtko Ursulin 		if (iir) {
2324e32192e1STvrtko Ursulin 			I915_WRITE(GEN8_DE_MISC_IIR, iir);
2325abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
2326e32192e1STvrtko Ursulin 			if (iir & GEN8_DE_MISC_GSE)
232791d14251STvrtko Ursulin 				intel_opregion_asle_intr(dev_priv);
232838cc46d7SOscar Mateo 			else
232938cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Misc interrupt\n");
2330abd58f01SBen Widawsky 		}
233138cc46d7SOscar Mateo 		else
233238cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
2333abd58f01SBen Widawsky 	}
2334abd58f01SBen Widawsky 
23356d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
2336e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_PORT_IIR);
2337e32192e1STvrtko Ursulin 		if (iir) {
2338e32192e1STvrtko Ursulin 			u32 tmp_mask;
2339d04a492dSShashank Sharma 			bool found = false;
2340cebd87a0SVille Syrjälä 
2341e32192e1STvrtko Ursulin 			I915_WRITE(GEN8_DE_PORT_IIR, iir);
23426d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
234388e04703SJesse Barnes 
2344e32192e1STvrtko Ursulin 			tmp_mask = GEN8_AUX_CHANNEL_A;
2345e32192e1STvrtko Ursulin 			if (INTEL_INFO(dev_priv)->gen >= 9)
2346e32192e1STvrtko Ursulin 				tmp_mask |= GEN9_AUX_CHANNEL_B |
2347e32192e1STvrtko Ursulin 					    GEN9_AUX_CHANNEL_C |
2348e32192e1STvrtko Ursulin 					    GEN9_AUX_CHANNEL_D;
2349e32192e1STvrtko Ursulin 
2350e32192e1STvrtko Ursulin 			if (iir & tmp_mask) {
235191d14251STvrtko Ursulin 				dp_aux_irq_handler(dev_priv);
2352d04a492dSShashank Sharma 				found = true;
2353d04a492dSShashank Sharma 			}
2354d04a492dSShashank Sharma 
2355e32192e1STvrtko Ursulin 			if (IS_BROXTON(dev_priv)) {
2356e32192e1STvrtko Ursulin 				tmp_mask = iir & BXT_DE_PORT_HOTPLUG_MASK;
2357e32192e1STvrtko Ursulin 				if (tmp_mask) {
235891d14251STvrtko Ursulin 					bxt_hpd_irq_handler(dev_priv, tmp_mask,
235991d14251STvrtko Ursulin 							    hpd_bxt);
2360d04a492dSShashank Sharma 					found = true;
2361d04a492dSShashank Sharma 				}
2362e32192e1STvrtko Ursulin 			} else if (IS_BROADWELL(dev_priv)) {
2363e32192e1STvrtko Ursulin 				tmp_mask = iir & GEN8_PORT_DP_A_HOTPLUG;
2364e32192e1STvrtko Ursulin 				if (tmp_mask) {
236591d14251STvrtko Ursulin 					ilk_hpd_irq_handler(dev_priv,
236691d14251STvrtko Ursulin 							    tmp_mask, hpd_bdw);
2367e32192e1STvrtko Ursulin 					found = true;
2368e32192e1STvrtko Ursulin 				}
2369e32192e1STvrtko Ursulin 			}
2370d04a492dSShashank Sharma 
237191d14251STvrtko Ursulin 			if (IS_BROXTON(dev_priv) && (iir & BXT_DE_PORT_GMBUS)) {
237291d14251STvrtko Ursulin 				gmbus_irq_handler(dev_priv);
23739e63743eSShashank Sharma 				found = true;
23749e63743eSShashank Sharma 			}
23759e63743eSShashank Sharma 
2376d04a492dSShashank Sharma 			if (!found)
237738cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Port interrupt\n");
23786d766f02SDaniel Vetter 		}
237938cc46d7SOscar Mateo 		else
238038cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
23816d766f02SDaniel Vetter 	}
23826d766f02SDaniel Vetter 
2383055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2384e32192e1STvrtko Ursulin 		u32 flip_done, fault_errors;
2385abd58f01SBen Widawsky 
2386c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2387c42664ccSDaniel Vetter 			continue;
2388c42664ccSDaniel Vetter 
2389e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
2390e32192e1STvrtko Ursulin 		if (!iir) {
2391e32192e1STvrtko Ursulin 			DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
2392e32192e1STvrtko Ursulin 			continue;
2393e32192e1STvrtko Ursulin 		}
2394770de83dSDamien Lespiau 
2395e32192e1STvrtko Ursulin 		ret = IRQ_HANDLED;
2396e32192e1STvrtko Ursulin 		I915_WRITE(GEN8_DE_PIPE_IIR(pipe), iir);
2397e32192e1STvrtko Ursulin 
23985a21b665SDaniel Vetter 		if (iir & GEN8_PIPE_VBLANK &&
23995a21b665SDaniel Vetter 		    intel_pipe_handle_vblank(dev_priv, pipe))
24005a21b665SDaniel Vetter 			intel_check_page_flip(dev_priv, pipe);
2401abd58f01SBen Widawsky 
2402e32192e1STvrtko Ursulin 		flip_done = iir;
2403b4834a50SRodrigo Vivi 		if (INTEL_INFO(dev_priv)->gen >= 9)
2404e32192e1STvrtko Ursulin 			flip_done &= GEN9_PIPE_PLANE1_FLIP_DONE;
2405770de83dSDamien Lespiau 		else
2406e32192e1STvrtko Ursulin 			flip_done &= GEN8_PIPE_PRIMARY_FLIP_DONE;
2407770de83dSDamien Lespiau 
24085251f04eSMaarten Lankhorst 		if (flip_done)
240951cbaf01SMaarten Lankhorst 			intel_finish_page_flip_cs(dev_priv, pipe);
2410abd58f01SBen Widawsky 
2411e32192e1STvrtko Ursulin 		if (iir & GEN8_PIPE_CDCLK_CRC_DONE)
241291d14251STvrtko Ursulin 			hsw_pipe_crc_irq_handler(dev_priv, pipe);
24130fbe7870SDaniel Vetter 
2414e32192e1STvrtko Ursulin 		if (iir & GEN8_PIPE_FIFO_UNDERRUN)
2415e32192e1STvrtko Ursulin 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
241638d83c96SDaniel Vetter 
2417e32192e1STvrtko Ursulin 		fault_errors = iir;
2418b4834a50SRodrigo Vivi 		if (INTEL_INFO(dev_priv)->gen >= 9)
2419e32192e1STvrtko Ursulin 			fault_errors &= GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
2420770de83dSDamien Lespiau 		else
2421e32192e1STvrtko Ursulin 			fault_errors &= GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
2422770de83dSDamien Lespiau 
2423770de83dSDamien Lespiau 		if (fault_errors)
242430100f2bSDaniel Vetter 			DRM_ERROR("Fault errors on pipe %c\n: 0x%08x",
242530100f2bSDaniel Vetter 				  pipe_name(pipe),
2426e32192e1STvrtko Ursulin 				  fault_errors);
2427abd58f01SBen Widawsky 	}
2428abd58f01SBen Widawsky 
242991d14251STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv) && !HAS_PCH_NOP(dev_priv) &&
2430266ea3d9SShashank Sharma 	    master_ctl & GEN8_DE_PCH_IRQ) {
243192d03a80SDaniel Vetter 		/*
243292d03a80SDaniel Vetter 		 * FIXME(BDW): Assume for now that the new interrupt handling
243392d03a80SDaniel Vetter 		 * scheme also closed the SDE interrupt handling race we've seen
243492d03a80SDaniel Vetter 		 * on older pch-split platforms. But this needs testing.
243592d03a80SDaniel Vetter 		 */
2436e32192e1STvrtko Ursulin 		iir = I915_READ(SDEIIR);
2437e32192e1STvrtko Ursulin 		if (iir) {
2438e32192e1STvrtko Ursulin 			I915_WRITE(SDEIIR, iir);
243992d03a80SDaniel Vetter 			ret = IRQ_HANDLED;
24406dbf30ceSVille Syrjälä 
24416dbf30ceSVille Syrjälä 			if (HAS_PCH_SPT(dev_priv))
244291d14251STvrtko Ursulin 				spt_irq_handler(dev_priv, iir);
24436dbf30ceSVille Syrjälä 			else
244491d14251STvrtko Ursulin 				cpt_irq_handler(dev_priv, iir);
24452dfb0b81SJani Nikula 		} else {
24462dfb0b81SJani Nikula 			/*
24472dfb0b81SJani Nikula 			 * Like on previous PCH there seems to be something
24482dfb0b81SJani Nikula 			 * fishy going on with forwarding PCH interrupts.
24492dfb0b81SJani Nikula 			 */
24502dfb0b81SJani Nikula 			DRM_DEBUG_DRIVER("The master control interrupt lied (SDE)!\n");
24512dfb0b81SJani Nikula 		}
245292d03a80SDaniel Vetter 	}
245392d03a80SDaniel Vetter 
2454f11a0f46STvrtko Ursulin 	return ret;
2455f11a0f46STvrtko Ursulin }
2456f11a0f46STvrtko Ursulin 
2457f11a0f46STvrtko Ursulin static irqreturn_t gen8_irq_handler(int irq, void *arg)
2458f11a0f46STvrtko Ursulin {
2459f11a0f46STvrtko Ursulin 	struct drm_device *dev = arg;
2460f11a0f46STvrtko Ursulin 	struct drm_i915_private *dev_priv = dev->dev_private;
2461f11a0f46STvrtko Ursulin 	u32 master_ctl;
2462e30e251aSVille Syrjälä 	u32 gt_iir[4] = {};
2463f11a0f46STvrtko Ursulin 	irqreturn_t ret;
2464f11a0f46STvrtko Ursulin 
2465f11a0f46STvrtko Ursulin 	if (!intel_irqs_enabled(dev_priv))
2466f11a0f46STvrtko Ursulin 		return IRQ_NONE;
2467f11a0f46STvrtko Ursulin 
2468f11a0f46STvrtko Ursulin 	master_ctl = I915_READ_FW(GEN8_MASTER_IRQ);
2469f11a0f46STvrtko Ursulin 	master_ctl &= ~GEN8_MASTER_IRQ_CONTROL;
2470f11a0f46STvrtko Ursulin 	if (!master_ctl)
2471f11a0f46STvrtko Ursulin 		return IRQ_NONE;
2472f11a0f46STvrtko Ursulin 
2473f11a0f46STvrtko Ursulin 	I915_WRITE_FW(GEN8_MASTER_IRQ, 0);
2474f11a0f46STvrtko Ursulin 
2475f11a0f46STvrtko Ursulin 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
2476f11a0f46STvrtko Ursulin 	disable_rpm_wakeref_asserts(dev_priv);
2477f11a0f46STvrtko Ursulin 
2478f11a0f46STvrtko Ursulin 	/* Find, clear, then process each source of interrupt */
2479e30e251aSVille Syrjälä 	ret = gen8_gt_irq_ack(dev_priv, master_ctl, gt_iir);
2480e30e251aSVille Syrjälä 	gen8_gt_irq_handler(dev_priv, gt_iir);
2481f11a0f46STvrtko Ursulin 	ret |= gen8_de_irq_handler(dev_priv, master_ctl);
2482f11a0f46STvrtko Ursulin 
2483cb0d205eSChris Wilson 	I915_WRITE_FW(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
2484cb0d205eSChris Wilson 	POSTING_READ_FW(GEN8_MASTER_IRQ);
2485abd58f01SBen Widawsky 
24861f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
24871f814dacSImre Deak 
2488abd58f01SBen Widawsky 	return ret;
2489abd58f01SBen Widawsky }
2490abd58f01SBen Widawsky 
2491*1f15b76fSChris Wilson static void i915_error_wake_up(struct drm_i915_private *dev_priv)
249217e1df07SDaniel Vetter {
249317e1df07SDaniel Vetter 	/*
249417e1df07SDaniel Vetter 	 * Notify all waiters for GPU completion events that reset state has
249517e1df07SDaniel Vetter 	 * been changed, and that they need to restart their wait after
249617e1df07SDaniel Vetter 	 * checking for potential errors (and bail out to drop locks if there is
249717e1df07SDaniel Vetter 	 * a gpu reset pending so that i915_error_work_func can acquire them).
249817e1df07SDaniel Vetter 	 */
249917e1df07SDaniel Vetter 
250017e1df07SDaniel Vetter 	/* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
2501*1f15b76fSChris Wilson 	wake_up_all(&dev_priv->gpu_error.wait_queue);
250217e1df07SDaniel Vetter 
250317e1df07SDaniel Vetter 	/* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
250417e1df07SDaniel Vetter 	wake_up_all(&dev_priv->pending_flip_queue);
250517e1df07SDaniel Vetter }
250617e1df07SDaniel Vetter 
25078a905236SJesse Barnes /**
2508b8d24a06SMika Kuoppala  * i915_reset_and_wakeup - do process context error handling work
250914bb2c11STvrtko Ursulin  * @dev_priv: i915 device private
25108a905236SJesse Barnes  *
25118a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
25128a905236SJesse Barnes  * was detected.
25138a905236SJesse Barnes  */
2514c033666aSChris Wilson static void i915_reset_and_wakeup(struct drm_i915_private *dev_priv)
25158a905236SJesse Barnes {
2516c033666aSChris Wilson 	struct kobject *kobj = &dev_priv->dev->primary->kdev->kobj;
2517cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
2518cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
2519cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
252017e1df07SDaniel Vetter 	int ret;
25218a905236SJesse Barnes 
2522c033666aSChris Wilson 	kobject_uevent_env(kobj, KOBJ_CHANGE, error_event);
25238a905236SJesse Barnes 
25247db0ba24SDaniel Vetter 	/*
25257db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
25267db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
25277db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
25287db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
25297db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
25307db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
25317db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
25327db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
25337db0ba24SDaniel Vetter 	 */
2534d98c52cfSChris Wilson 	if (i915_reset_in_progress(&dev_priv->gpu_error)) {
253544d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
2536c033666aSChris Wilson 		kobject_uevent_env(kobj, KOBJ_CHANGE, reset_event);
25371f83fee0SDaniel Vetter 
253817e1df07SDaniel Vetter 		/*
2539f454c694SImre Deak 		 * In most cases it's guaranteed that we get here with an RPM
2540f454c694SImre Deak 		 * reference held, for example because there is a pending GPU
2541f454c694SImre Deak 		 * request that won't finish until the reset is done. This
2542f454c694SImre Deak 		 * isn't the case at least when we get here by doing a
2543f454c694SImre Deak 		 * simulated reset via debugs, so get an RPM reference.
2544f454c694SImre Deak 		 */
2545f454c694SImre Deak 		intel_runtime_pm_get(dev_priv);
25467514747dSVille Syrjälä 
2547c033666aSChris Wilson 		intel_prepare_reset(dev_priv);
25487514747dSVille Syrjälä 
2549f454c694SImre Deak 		/*
255017e1df07SDaniel Vetter 		 * All state reset _must_ be completed before we update the
255117e1df07SDaniel Vetter 		 * reset counter, for otherwise waiters might miss the reset
255217e1df07SDaniel Vetter 		 * pending state and not properly drop locks, resulting in
255317e1df07SDaniel Vetter 		 * deadlocks with the reset work.
255417e1df07SDaniel Vetter 		 */
2555c033666aSChris Wilson 		ret = i915_reset(dev_priv);
2556f69061beSDaniel Vetter 
2557c033666aSChris Wilson 		intel_finish_reset(dev_priv);
255817e1df07SDaniel Vetter 
2559f454c694SImre Deak 		intel_runtime_pm_put(dev_priv);
2560f454c694SImre Deak 
2561d98c52cfSChris Wilson 		if (ret == 0)
2562c033666aSChris Wilson 			kobject_uevent_env(kobj,
2563f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
25641f83fee0SDaniel Vetter 
256517e1df07SDaniel Vetter 		/*
256617e1df07SDaniel Vetter 		 * Note: The wake_up also serves as a memory barrier so that
256717e1df07SDaniel Vetter 		 * waiters see the update value of the reset counter atomic_t.
256817e1df07SDaniel Vetter 		 */
2569*1f15b76fSChris Wilson 		wake_up_all(&dev_priv->gpu_error.reset_queue);
2570f316a42cSBen Gamari 	}
25718a905236SJesse Barnes }
25728a905236SJesse Barnes 
2573c033666aSChris Wilson static void i915_report_and_clear_eir(struct drm_i915_private *dev_priv)
2574c0e09200SDave Airlie {
2575bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
257663eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
2577050ee91fSBen Widawsky 	int pipe, i;
257863eeaf38SJesse Barnes 
257935aed2e6SChris Wilson 	if (!eir)
258035aed2e6SChris Wilson 		return;
258163eeaf38SJesse Barnes 
2582a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
25838a905236SJesse Barnes 
2584c033666aSChris Wilson 	i915_get_extra_instdone(dev_priv, instdone);
2585bd9854f9SBen Widawsky 
2586c033666aSChris Wilson 	if (IS_G4X(dev_priv)) {
25878a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
25888a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
25898a905236SJesse Barnes 
2590a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2591a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2592050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
2593050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2594a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2595a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
25968a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
25973143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
25988a905236SJesse Barnes 		}
25998a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
26008a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2601a70491ccSJoe Perches 			pr_err("page table error\n");
2602a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
26038a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
26043143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
26058a905236SJesse Barnes 		}
26068a905236SJesse Barnes 	}
26078a905236SJesse Barnes 
2608c033666aSChris Wilson 	if (!IS_GEN2(dev_priv)) {
260963eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
261063eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2611a70491ccSJoe Perches 			pr_err("page table error\n");
2612a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
261363eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
26143143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
261563eeaf38SJesse Barnes 		}
26168a905236SJesse Barnes 	}
26178a905236SJesse Barnes 
261863eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
2619a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
2620055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
2621a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
26229db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
262363eeaf38SJesse Barnes 		/* pipestat has already been acked */
262463eeaf38SJesse Barnes 	}
262563eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
2626a70491ccSJoe Perches 		pr_err("instruction error\n");
2627a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
2628050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
2629050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2630c033666aSChris Wilson 		if (INTEL_GEN(dev_priv) < 4) {
263163eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
263263eeaf38SJesse Barnes 
2633a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
2634a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
2635a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
263663eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
26373143a2bfSChris Wilson 			POSTING_READ(IPEIR);
263863eeaf38SJesse Barnes 		} else {
263963eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
264063eeaf38SJesse Barnes 
2641a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2642a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2643a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2644a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
264563eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
26463143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
264763eeaf38SJesse Barnes 		}
264863eeaf38SJesse Barnes 	}
264963eeaf38SJesse Barnes 
265063eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
26513143a2bfSChris Wilson 	POSTING_READ(EIR);
265263eeaf38SJesse Barnes 	eir = I915_READ(EIR);
265363eeaf38SJesse Barnes 	if (eir) {
265463eeaf38SJesse Barnes 		/*
265563eeaf38SJesse Barnes 		 * some errors might have become stuck,
265663eeaf38SJesse Barnes 		 * mask them.
265763eeaf38SJesse Barnes 		 */
265863eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
265963eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
266063eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
266163eeaf38SJesse Barnes 	}
266235aed2e6SChris Wilson }
266335aed2e6SChris Wilson 
266435aed2e6SChris Wilson /**
2665b8d24a06SMika Kuoppala  * i915_handle_error - handle a gpu error
266614bb2c11STvrtko Ursulin  * @dev_priv: i915 device private
266714b730fcSarun.siluvery@linux.intel.com  * @engine_mask: mask representing engines that are hung
2668aafd8581SJavier Martinez Canillas  * Do some basic checking of register state at error time and
266935aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
267035aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
267135aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
267235aed2e6SChris Wilson  * of a ring dump etc.).
267314bb2c11STvrtko Ursulin  * @fmt: Error message format string
267435aed2e6SChris Wilson  */
2675c033666aSChris Wilson void i915_handle_error(struct drm_i915_private *dev_priv,
2676c033666aSChris Wilson 		       u32 engine_mask,
267758174462SMika Kuoppala 		       const char *fmt, ...)
267835aed2e6SChris Wilson {
267958174462SMika Kuoppala 	va_list args;
268058174462SMika Kuoppala 	char error_msg[80];
268135aed2e6SChris Wilson 
268258174462SMika Kuoppala 	va_start(args, fmt);
268358174462SMika Kuoppala 	vscnprintf(error_msg, sizeof(error_msg), fmt, args);
268458174462SMika Kuoppala 	va_end(args);
268558174462SMika Kuoppala 
2686c033666aSChris Wilson 	i915_capture_error_state(dev_priv, engine_mask, error_msg);
2687c033666aSChris Wilson 	i915_report_and_clear_eir(dev_priv);
26888a905236SJesse Barnes 
268914b730fcSarun.siluvery@linux.intel.com 	if (engine_mask) {
2690805de8f4SPeter Zijlstra 		atomic_or(I915_RESET_IN_PROGRESS_FLAG,
2691f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
2692ba1234d1SBen Gamari 
269311ed50ecSBen Gamari 		/*
2694b8d24a06SMika Kuoppala 		 * Wakeup waiting processes so that the reset function
2695b8d24a06SMika Kuoppala 		 * i915_reset_and_wakeup doesn't deadlock trying to grab
2696b8d24a06SMika Kuoppala 		 * various locks. By bumping the reset counter first, the woken
269717e1df07SDaniel Vetter 		 * processes will see a reset in progress and back off,
269817e1df07SDaniel Vetter 		 * releasing their locks and then wait for the reset completion.
269917e1df07SDaniel Vetter 		 * We must do this for _all_ gpu waiters that might hold locks
270017e1df07SDaniel Vetter 		 * that the reset work needs to acquire.
270117e1df07SDaniel Vetter 		 *
270217e1df07SDaniel Vetter 		 * Note: The wake_up serves as the required memory barrier to
270317e1df07SDaniel Vetter 		 * ensure that the waiters see the updated value of the reset
270417e1df07SDaniel Vetter 		 * counter atomic_t.
270511ed50ecSBen Gamari 		 */
2706*1f15b76fSChris Wilson 		i915_error_wake_up(dev_priv);
270711ed50ecSBen Gamari 	}
270811ed50ecSBen Gamari 
2709c033666aSChris Wilson 	i915_reset_and_wakeup(dev_priv);
27108a905236SJesse Barnes }
27118a905236SJesse Barnes 
271242f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
271342f52ef8SKeith Packard  * we use as a pipe index
271442f52ef8SKeith Packard  */
271588e72717SThierry Reding static int i915_enable_vblank(struct drm_device *dev, unsigned int pipe)
27160a3e67a4SJesse Barnes {
27172d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2718e9d21d7fSKeith Packard 	unsigned long irqflags;
271971e0ffa5SJesse Barnes 
27201ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2721f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
27227c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2723755e9019SImre Deak 				     PIPE_START_VBLANK_INTERRUPT_STATUS);
27240a3e67a4SJesse Barnes 	else
27257c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2726755e9019SImre Deak 				     PIPE_VBLANK_INTERRUPT_STATUS);
27271ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
27288692d00eSChris Wilson 
27290a3e67a4SJesse Barnes 	return 0;
27300a3e67a4SJesse Barnes }
27310a3e67a4SJesse Barnes 
273288e72717SThierry Reding static int ironlake_enable_vblank(struct drm_device *dev, unsigned int pipe)
2733f796cf8fSJesse Barnes {
27342d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2735f796cf8fSJesse Barnes 	unsigned long irqflags;
2736b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
273740da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2738f796cf8fSJesse Barnes 
2739f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2740fbdedaeaSVille Syrjälä 	ilk_enable_display_irq(dev_priv, bit);
2741b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2742b1f14ad0SJesse Barnes 
2743b1f14ad0SJesse Barnes 	return 0;
2744b1f14ad0SJesse Barnes }
2745b1f14ad0SJesse Barnes 
274688e72717SThierry Reding static int valleyview_enable_vblank(struct drm_device *dev, unsigned int pipe)
27477e231dbeSJesse Barnes {
27482d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
27497e231dbeSJesse Barnes 	unsigned long irqflags;
27507e231dbeSJesse Barnes 
27517e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
275231acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
2753755e9019SImre Deak 			     PIPE_START_VBLANK_INTERRUPT_STATUS);
27547e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
27557e231dbeSJesse Barnes 
27567e231dbeSJesse Barnes 	return 0;
27577e231dbeSJesse Barnes }
27587e231dbeSJesse Barnes 
275988e72717SThierry Reding static int gen8_enable_vblank(struct drm_device *dev, unsigned int pipe)
2760abd58f01SBen Widawsky {
2761abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2762abd58f01SBen Widawsky 	unsigned long irqflags;
2763abd58f01SBen Widawsky 
2764abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2765013d3752SVille Syrjälä 	bdw_enable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
2766abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2767013d3752SVille Syrjälä 
2768abd58f01SBen Widawsky 	return 0;
2769abd58f01SBen Widawsky }
2770abd58f01SBen Widawsky 
277142f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
277242f52ef8SKeith Packard  * we use as a pipe index
277342f52ef8SKeith Packard  */
277488e72717SThierry Reding static void i915_disable_vblank(struct drm_device *dev, unsigned int pipe)
27750a3e67a4SJesse Barnes {
27762d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2777e9d21d7fSKeith Packard 	unsigned long irqflags;
27780a3e67a4SJesse Barnes 
27791ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
27807c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
2781755e9019SImre Deak 			      PIPE_VBLANK_INTERRUPT_STATUS |
2782755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
27831ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
27840a3e67a4SJesse Barnes }
27850a3e67a4SJesse Barnes 
278688e72717SThierry Reding static void ironlake_disable_vblank(struct drm_device *dev, unsigned int pipe)
2787f796cf8fSJesse Barnes {
27882d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2789f796cf8fSJesse Barnes 	unsigned long irqflags;
2790b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
279140da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2792f796cf8fSJesse Barnes 
2793f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2794fbdedaeaSVille Syrjälä 	ilk_disable_display_irq(dev_priv, bit);
2795b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2796b1f14ad0SJesse Barnes }
2797b1f14ad0SJesse Barnes 
279888e72717SThierry Reding static void valleyview_disable_vblank(struct drm_device *dev, unsigned int pipe)
27997e231dbeSJesse Barnes {
28002d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
28017e231dbeSJesse Barnes 	unsigned long irqflags;
28027e231dbeSJesse Barnes 
28037e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
280431acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
2805755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
28067e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
28077e231dbeSJesse Barnes }
28087e231dbeSJesse Barnes 
280988e72717SThierry Reding static void gen8_disable_vblank(struct drm_device *dev, unsigned int pipe)
2810abd58f01SBen Widawsky {
2811abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2812abd58f01SBen Widawsky 	unsigned long irqflags;
2813abd58f01SBen Widawsky 
2814abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2815013d3752SVille Syrjälä 	bdw_disable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
2816abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2817abd58f01SBen Widawsky }
2818abd58f01SBen Widawsky 
28199107e9d2SChris Wilson static bool
28200bc40be8STvrtko Ursulin ring_idle(struct intel_engine_cs *engine, u32 seqno)
2821893eead0SChris Wilson {
2822cffa781eSChris Wilson 	return i915_seqno_passed(seqno,
2823cffa781eSChris Wilson 				 READ_ONCE(engine->last_submitted_seqno));
2824f65d9421SBen Gamari }
2825f65d9421SBen Gamari 
2826a028c4b0SDaniel Vetter static bool
2827c033666aSChris Wilson ipehr_is_semaphore_wait(struct drm_i915_private *dev_priv, u32 ipehr)
2828a028c4b0SDaniel Vetter {
2829c033666aSChris Wilson 	if (INTEL_GEN(dev_priv) >= 8) {
2830a6cdb93aSRodrigo Vivi 		return (ipehr >> 23) == 0x1c;
2831a028c4b0SDaniel Vetter 	} else {
2832a028c4b0SDaniel Vetter 		ipehr &= ~MI_SEMAPHORE_SYNC_MASK;
2833a028c4b0SDaniel Vetter 		return ipehr == (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE |
2834a028c4b0SDaniel Vetter 				 MI_SEMAPHORE_REGISTER);
2835a028c4b0SDaniel Vetter 	}
2836a028c4b0SDaniel Vetter }
2837a028c4b0SDaniel Vetter 
2838a4872ba6SOscar Mateo static struct intel_engine_cs *
28390bc40be8STvrtko Ursulin semaphore_wait_to_signaller_ring(struct intel_engine_cs *engine, u32 ipehr,
28400bc40be8STvrtko Ursulin 				 u64 offset)
2841921d42eaSDaniel Vetter {
2842c033666aSChris Wilson 	struct drm_i915_private *dev_priv = engine->i915;
2843a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2844921d42eaSDaniel Vetter 
2845c033666aSChris Wilson 	if (INTEL_GEN(dev_priv) >= 8) {
2846b4ac5afcSDave Gordon 		for_each_engine(signaller, dev_priv) {
28470bc40be8STvrtko Ursulin 			if (engine == signaller)
2848a6cdb93aSRodrigo Vivi 				continue;
2849a6cdb93aSRodrigo Vivi 
28500bc40be8STvrtko Ursulin 			if (offset == signaller->semaphore.signal_ggtt[engine->id])
2851a6cdb93aSRodrigo Vivi 				return signaller;
2852a6cdb93aSRodrigo Vivi 		}
2853921d42eaSDaniel Vetter 	} else {
2854921d42eaSDaniel Vetter 		u32 sync_bits = ipehr & MI_SEMAPHORE_SYNC_MASK;
2855921d42eaSDaniel Vetter 
2856b4ac5afcSDave Gordon 		for_each_engine(signaller, dev_priv) {
28570bc40be8STvrtko Ursulin 			if(engine == signaller)
2858921d42eaSDaniel Vetter 				continue;
2859921d42eaSDaniel Vetter 
28600bc40be8STvrtko Ursulin 			if (sync_bits == signaller->semaphore.mbox.wait[engine->id])
2861921d42eaSDaniel Vetter 				return signaller;
2862921d42eaSDaniel Vetter 		}
2863921d42eaSDaniel Vetter 	}
2864921d42eaSDaniel Vetter 
2865a6cdb93aSRodrigo Vivi 	DRM_ERROR("No signaller ring found for ring %i, ipehr 0x%08x, offset 0x%016llx\n",
28660bc40be8STvrtko Ursulin 		  engine->id, ipehr, offset);
2867921d42eaSDaniel Vetter 
2868921d42eaSDaniel Vetter 	return NULL;
2869921d42eaSDaniel Vetter }
2870921d42eaSDaniel Vetter 
2871a4872ba6SOscar Mateo static struct intel_engine_cs *
28720bc40be8STvrtko Ursulin semaphore_waits_for(struct intel_engine_cs *engine, u32 *seqno)
2873a24a11e6SChris Wilson {
2874c033666aSChris Wilson 	struct drm_i915_private *dev_priv = engine->i915;
287588fe429dSDaniel Vetter 	u32 cmd, ipehr, head;
2876a6cdb93aSRodrigo Vivi 	u64 offset = 0;
2877a6cdb93aSRodrigo Vivi 	int i, backwards;
2878a24a11e6SChris Wilson 
2879381e8ae3STomas Elf 	/*
2880381e8ae3STomas Elf 	 * This function does not support execlist mode - any attempt to
2881381e8ae3STomas Elf 	 * proceed further into this function will result in a kernel panic
2882381e8ae3STomas Elf 	 * when dereferencing ring->buffer, which is not set up in execlist
2883381e8ae3STomas Elf 	 * mode.
2884381e8ae3STomas Elf 	 *
2885381e8ae3STomas Elf 	 * The correct way of doing it would be to derive the currently
2886381e8ae3STomas Elf 	 * executing ring buffer from the current context, which is derived
2887381e8ae3STomas Elf 	 * from the currently running request. Unfortunately, to get the
2888381e8ae3STomas Elf 	 * current request we would have to grab the struct_mutex before doing
2889381e8ae3STomas Elf 	 * anything else, which would be ill-advised since some other thread
2890381e8ae3STomas Elf 	 * might have grabbed it already and managed to hang itself, causing
2891381e8ae3STomas Elf 	 * the hang checker to deadlock.
2892381e8ae3STomas Elf 	 *
2893381e8ae3STomas Elf 	 * Therefore, this function does not support execlist mode in its
2894381e8ae3STomas Elf 	 * current form. Just return NULL and move on.
2895381e8ae3STomas Elf 	 */
28960bc40be8STvrtko Ursulin 	if (engine->buffer == NULL)
2897381e8ae3STomas Elf 		return NULL;
2898381e8ae3STomas Elf 
28990bc40be8STvrtko Ursulin 	ipehr = I915_READ(RING_IPEHR(engine->mmio_base));
2900c033666aSChris Wilson 	if (!ipehr_is_semaphore_wait(engine->i915, ipehr))
29016274f212SChris Wilson 		return NULL;
2902a24a11e6SChris Wilson 
290388fe429dSDaniel Vetter 	/*
290488fe429dSDaniel Vetter 	 * HEAD is likely pointing to the dword after the actual command,
290588fe429dSDaniel Vetter 	 * so scan backwards until we find the MBOX. But limit it to just 3
2906a6cdb93aSRodrigo Vivi 	 * or 4 dwords depending on the semaphore wait command size.
2907a6cdb93aSRodrigo Vivi 	 * Note that we don't care about ACTHD here since that might
290888fe429dSDaniel Vetter 	 * point at at batch, and semaphores are always emitted into the
290988fe429dSDaniel Vetter 	 * ringbuffer itself.
2910a24a11e6SChris Wilson 	 */
29110bc40be8STvrtko Ursulin 	head = I915_READ_HEAD(engine) & HEAD_ADDR;
2912c033666aSChris Wilson 	backwards = (INTEL_GEN(dev_priv) >= 8) ? 5 : 4;
291388fe429dSDaniel Vetter 
2914a6cdb93aSRodrigo Vivi 	for (i = backwards; i; --i) {
291588fe429dSDaniel Vetter 		/*
291688fe429dSDaniel Vetter 		 * Be paranoid and presume the hw has gone off into the wild -
291788fe429dSDaniel Vetter 		 * our ring is smaller than what the hardware (and hence
291888fe429dSDaniel Vetter 		 * HEAD_ADDR) allows. Also handles wrap-around.
291988fe429dSDaniel Vetter 		 */
29200bc40be8STvrtko Ursulin 		head &= engine->buffer->size - 1;
292188fe429dSDaniel Vetter 
292288fe429dSDaniel Vetter 		/* This here seems to blow up */
29230bc40be8STvrtko Ursulin 		cmd = ioread32(engine->buffer->virtual_start + head);
2924a24a11e6SChris Wilson 		if (cmd == ipehr)
2925a24a11e6SChris Wilson 			break;
2926a24a11e6SChris Wilson 
292788fe429dSDaniel Vetter 		head -= 4;
292888fe429dSDaniel Vetter 	}
2929a24a11e6SChris Wilson 
293088fe429dSDaniel Vetter 	if (!i)
293188fe429dSDaniel Vetter 		return NULL;
293288fe429dSDaniel Vetter 
29330bc40be8STvrtko Ursulin 	*seqno = ioread32(engine->buffer->virtual_start + head + 4) + 1;
2934c033666aSChris Wilson 	if (INTEL_GEN(dev_priv) >= 8) {
29350bc40be8STvrtko Ursulin 		offset = ioread32(engine->buffer->virtual_start + head + 12);
2936a6cdb93aSRodrigo Vivi 		offset <<= 32;
29370bc40be8STvrtko Ursulin 		offset = ioread32(engine->buffer->virtual_start + head + 8);
2938a6cdb93aSRodrigo Vivi 	}
29390bc40be8STvrtko Ursulin 	return semaphore_wait_to_signaller_ring(engine, ipehr, offset);
2940a24a11e6SChris Wilson }
2941a24a11e6SChris Wilson 
29420bc40be8STvrtko Ursulin static int semaphore_passed(struct intel_engine_cs *engine)
29436274f212SChris Wilson {
2944c033666aSChris Wilson 	struct drm_i915_private *dev_priv = engine->i915;
2945a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2946a0d036b0SChris Wilson 	u32 seqno;
29476274f212SChris Wilson 
29480bc40be8STvrtko Ursulin 	engine->hangcheck.deadlock++;
29496274f212SChris Wilson 
29500bc40be8STvrtko Ursulin 	signaller = semaphore_waits_for(engine, &seqno);
29514be17381SChris Wilson 	if (signaller == NULL)
29524be17381SChris Wilson 		return -1;
29534be17381SChris Wilson 
29544be17381SChris Wilson 	/* Prevent pathological recursion due to driver bugs */
2955666796daSTvrtko Ursulin 	if (signaller->hangcheck.deadlock >= I915_NUM_ENGINES)
29566274f212SChris Wilson 		return -1;
29576274f212SChris Wilson 
2958c04e0f3bSChris Wilson 	if (i915_seqno_passed(signaller->get_seqno(signaller), seqno))
29594be17381SChris Wilson 		return 1;
29604be17381SChris Wilson 
2961a0d036b0SChris Wilson 	/* cursory check for an unkickable deadlock */
2962a0d036b0SChris Wilson 	if (I915_READ_CTL(signaller) & RING_WAIT_SEMAPHORE &&
2963a0d036b0SChris Wilson 	    semaphore_passed(signaller) < 0)
29644be17381SChris Wilson 		return -1;
29654be17381SChris Wilson 
29664be17381SChris Wilson 	return 0;
29676274f212SChris Wilson }
29686274f212SChris Wilson 
29696274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
29706274f212SChris Wilson {
2971e2f80391STvrtko Ursulin 	struct intel_engine_cs *engine;
29726274f212SChris Wilson 
2973b4ac5afcSDave Gordon 	for_each_engine(engine, dev_priv)
2974e2f80391STvrtko Ursulin 		engine->hangcheck.deadlock = 0;
29756274f212SChris Wilson }
29766274f212SChris Wilson 
29770bc40be8STvrtko Ursulin static bool subunits_stuck(struct intel_engine_cs *engine)
29781ec14ad3SChris Wilson {
297961642ff0SMika Kuoppala 	u32 instdone[I915_NUM_INSTDONE_REG];
298061642ff0SMika Kuoppala 	bool stuck;
298161642ff0SMika Kuoppala 	int i;
29829107e9d2SChris Wilson 
29830bc40be8STvrtko Ursulin 	if (engine->id != RCS)
298461642ff0SMika Kuoppala 		return true;
298561642ff0SMika Kuoppala 
2986c033666aSChris Wilson 	i915_get_extra_instdone(engine->i915, instdone);
298761642ff0SMika Kuoppala 
298861642ff0SMika Kuoppala 	/* There might be unstable subunit states even when
298961642ff0SMika Kuoppala 	 * actual head is not moving. Filter out the unstable ones by
299061642ff0SMika Kuoppala 	 * accumulating the undone -> done transitions and only
299161642ff0SMika Kuoppala 	 * consider those as progress.
299261642ff0SMika Kuoppala 	 */
299361642ff0SMika Kuoppala 	stuck = true;
299461642ff0SMika Kuoppala 	for (i = 0; i < I915_NUM_INSTDONE_REG; i++) {
29950bc40be8STvrtko Ursulin 		const u32 tmp = instdone[i] | engine->hangcheck.instdone[i];
299661642ff0SMika Kuoppala 
29970bc40be8STvrtko Ursulin 		if (tmp != engine->hangcheck.instdone[i])
299861642ff0SMika Kuoppala 			stuck = false;
299961642ff0SMika Kuoppala 
30000bc40be8STvrtko Ursulin 		engine->hangcheck.instdone[i] |= tmp;
300161642ff0SMika Kuoppala 	}
300261642ff0SMika Kuoppala 
300361642ff0SMika Kuoppala 	return stuck;
300461642ff0SMika Kuoppala }
300561642ff0SMika Kuoppala 
300661642ff0SMika Kuoppala static enum intel_ring_hangcheck_action
30070bc40be8STvrtko Ursulin head_stuck(struct intel_engine_cs *engine, u64 acthd)
300861642ff0SMika Kuoppala {
30090bc40be8STvrtko Ursulin 	if (acthd != engine->hangcheck.acthd) {
301061642ff0SMika Kuoppala 
301161642ff0SMika Kuoppala 		/* Clear subunit states on head movement */
30120bc40be8STvrtko Ursulin 		memset(engine->hangcheck.instdone, 0,
30130bc40be8STvrtko Ursulin 		       sizeof(engine->hangcheck.instdone));
301461642ff0SMika Kuoppala 
3015f2f4d82fSJani Nikula 		return HANGCHECK_ACTIVE;
3016f260fe7bSMika Kuoppala 	}
3017f260fe7bSMika Kuoppala 
30180bc40be8STvrtko Ursulin 	if (!subunits_stuck(engine))
301961642ff0SMika Kuoppala 		return HANGCHECK_ACTIVE;
302061642ff0SMika Kuoppala 
302161642ff0SMika Kuoppala 	return HANGCHECK_HUNG;
302261642ff0SMika Kuoppala }
302361642ff0SMika Kuoppala 
302461642ff0SMika Kuoppala static enum intel_ring_hangcheck_action
30250bc40be8STvrtko Ursulin ring_stuck(struct intel_engine_cs *engine, u64 acthd)
302661642ff0SMika Kuoppala {
3027c033666aSChris Wilson 	struct drm_i915_private *dev_priv = engine->i915;
302861642ff0SMika Kuoppala 	enum intel_ring_hangcheck_action ha;
302961642ff0SMika Kuoppala 	u32 tmp;
303061642ff0SMika Kuoppala 
30310bc40be8STvrtko Ursulin 	ha = head_stuck(engine, acthd);
303261642ff0SMika Kuoppala 	if (ha != HANGCHECK_HUNG)
303361642ff0SMika Kuoppala 		return ha;
303461642ff0SMika Kuoppala 
3035c033666aSChris Wilson 	if (IS_GEN2(dev_priv))
3036f2f4d82fSJani Nikula 		return HANGCHECK_HUNG;
30379107e9d2SChris Wilson 
30389107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
30399107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
30409107e9d2SChris Wilson 	 * and break the hang. This should work on
30419107e9d2SChris Wilson 	 * all but the second generation chipsets.
30429107e9d2SChris Wilson 	 */
30430bc40be8STvrtko Ursulin 	tmp = I915_READ_CTL(engine);
30441ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
3045c033666aSChris Wilson 		i915_handle_error(dev_priv, 0,
304658174462SMika Kuoppala 				  "Kicking stuck wait on %s",
30470bc40be8STvrtko Ursulin 				  engine->name);
30480bc40be8STvrtko Ursulin 		I915_WRITE_CTL(engine, tmp);
3049f2f4d82fSJani Nikula 		return HANGCHECK_KICK;
30501ec14ad3SChris Wilson 	}
3051a24a11e6SChris Wilson 
3052c033666aSChris Wilson 	if (INTEL_GEN(dev_priv) >= 6 && tmp & RING_WAIT_SEMAPHORE) {
30530bc40be8STvrtko Ursulin 		switch (semaphore_passed(engine)) {
30546274f212SChris Wilson 		default:
3055f2f4d82fSJani Nikula 			return HANGCHECK_HUNG;
30566274f212SChris Wilson 		case 1:
3057c033666aSChris Wilson 			i915_handle_error(dev_priv, 0,
305858174462SMika Kuoppala 					  "Kicking stuck semaphore on %s",
30590bc40be8STvrtko Ursulin 					  engine->name);
30600bc40be8STvrtko Ursulin 			I915_WRITE_CTL(engine, tmp);
3061f2f4d82fSJani Nikula 			return HANGCHECK_KICK;
30626274f212SChris Wilson 		case 0:
3063f2f4d82fSJani Nikula 			return HANGCHECK_WAIT;
30646274f212SChris Wilson 		}
30659107e9d2SChris Wilson 	}
30669107e9d2SChris Wilson 
3067f2f4d82fSJani Nikula 	return HANGCHECK_HUNG;
3068a24a11e6SChris Wilson }
3069d1e61e7fSChris Wilson 
307012471ba8SChris Wilson static unsigned kick_waiters(struct intel_engine_cs *engine)
307112471ba8SChris Wilson {
3072c033666aSChris Wilson 	struct drm_i915_private *i915 = engine->i915;
307312471ba8SChris Wilson 	unsigned user_interrupts = READ_ONCE(engine->user_interrupts);
307412471ba8SChris Wilson 
307512471ba8SChris Wilson 	if (engine->hangcheck.user_interrupts == user_interrupts &&
307612471ba8SChris Wilson 	    !test_and_set_bit(engine->id, &i915->gpu_error.missed_irq_rings)) {
307712471ba8SChris Wilson 		if (!(i915->gpu_error.test_irq_rings & intel_engine_flag(engine)))
307812471ba8SChris Wilson 			DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
307912471ba8SChris Wilson 				  engine->name);
308012471ba8SChris Wilson 		else
308112471ba8SChris Wilson 			DRM_INFO("Fake missed irq on %s\n",
308212471ba8SChris Wilson 				 engine->name);
308312471ba8SChris Wilson 		wake_up_all(&engine->irq_queue);
308412471ba8SChris Wilson 	}
308512471ba8SChris Wilson 
308612471ba8SChris Wilson 	return user_interrupts;
308712471ba8SChris Wilson }
3088737b1506SChris Wilson /*
3089f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
309005407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
309105407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
309205407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
309305407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
309405407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
3095f65d9421SBen Gamari  */
3096737b1506SChris Wilson static void i915_hangcheck_elapsed(struct work_struct *work)
3097f65d9421SBen Gamari {
3098737b1506SChris Wilson 	struct drm_i915_private *dev_priv =
3099737b1506SChris Wilson 		container_of(work, typeof(*dev_priv),
3100737b1506SChris Wilson 			     gpu_error.hangcheck_work.work);
3101e2f80391STvrtko Ursulin 	struct intel_engine_cs *engine;
3102c3232b18SDave Gordon 	enum intel_engine_id id;
310305407ff8SMika Kuoppala 	int busy_count = 0, rings_hung = 0;
3104666796daSTvrtko Ursulin 	bool stuck[I915_NUM_ENGINES] = { 0 };
31059107e9d2SChris Wilson #define BUSY 1
31069107e9d2SChris Wilson #define KICK 5
31079107e9d2SChris Wilson #define HUNG 20
310824a65e62SMika Kuoppala #define ACTIVE_DECAY 15
3109893eead0SChris Wilson 
3110d330a953SJani Nikula 	if (!i915.enable_hangcheck)
31113e0dc6b0SBen Widawsky 		return;
31123e0dc6b0SBen Widawsky 
31131f814dacSImre Deak 	/*
31141f814dacSImre Deak 	 * The hangcheck work is synced during runtime suspend, we don't
31151f814dacSImre Deak 	 * require a wakeref. TODO: instead of disabling the asserts make
31161f814dacSImre Deak 	 * sure that we hold a reference when this work is running.
31171f814dacSImre Deak 	 */
31181f814dacSImre Deak 	DISABLE_RPM_WAKEREF_ASSERTS(dev_priv);
31191f814dacSImre Deak 
312075714940SMika Kuoppala 	/* As enabling the GPU requires fairly extensive mmio access,
312175714940SMika Kuoppala 	 * periodically arm the mmio checker to see if we are triggering
312275714940SMika Kuoppala 	 * any invalid access.
312375714940SMika Kuoppala 	 */
312475714940SMika Kuoppala 	intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
312575714940SMika Kuoppala 
3126c3232b18SDave Gordon 	for_each_engine_id(engine, dev_priv, id) {
312705535726SChris Wilson 		bool busy = waitqueue_active(&engine->irq_queue);
312850877445SChris Wilson 		u64 acthd;
312950877445SChris Wilson 		u32 seqno;
313012471ba8SChris Wilson 		unsigned user_interrupts;
3131b4519513SChris Wilson 
31326274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
31336274f212SChris Wilson 
3134c04e0f3bSChris Wilson 		/* We don't strictly need an irq-barrier here, as we are not
3135c04e0f3bSChris Wilson 		 * serving an interrupt request, be paranoid in case the
3136c04e0f3bSChris Wilson 		 * barrier has side-effects (such as preventing a broken
3137c04e0f3bSChris Wilson 		 * cacheline snoop) and so be sure that we can see the seqno
3138c04e0f3bSChris Wilson 		 * advance. If the seqno should stick, due to a stale
3139c04e0f3bSChris Wilson 		 * cacheline, we would erroneously declare the GPU hung.
3140c04e0f3bSChris Wilson 		 */
3141c04e0f3bSChris Wilson 		if (engine->irq_seqno_barrier)
3142c04e0f3bSChris Wilson 			engine->irq_seqno_barrier(engine);
3143c04e0f3bSChris Wilson 
3144e2f80391STvrtko Ursulin 		acthd = intel_ring_get_active_head(engine);
3145c04e0f3bSChris Wilson 		seqno = engine->get_seqno(engine);
314605407ff8SMika Kuoppala 
314712471ba8SChris Wilson 		/* Reset stuck interrupts between batch advances */
314812471ba8SChris Wilson 		user_interrupts = 0;
314912471ba8SChris Wilson 
3150e2f80391STvrtko Ursulin 		if (engine->hangcheck.seqno == seqno) {
3151e2f80391STvrtko Ursulin 			if (ring_idle(engine, seqno)) {
3152e2f80391STvrtko Ursulin 				engine->hangcheck.action = HANGCHECK_IDLE;
315305535726SChris Wilson 				if (busy) {
3154094f9a54SChris Wilson 					/* Safeguard against driver failure */
315512471ba8SChris Wilson 					user_interrupts = kick_waiters(engine);
3156e2f80391STvrtko Ursulin 					engine->hangcheck.score += BUSY;
315705535726SChris Wilson 				}
315805407ff8SMika Kuoppala 			} else {
31596274f212SChris Wilson 				/* We always increment the hangcheck score
31606274f212SChris Wilson 				 * if the ring is busy and still processing
31616274f212SChris Wilson 				 * the same request, so that no single request
31626274f212SChris Wilson 				 * can run indefinitely (such as a chain of
31636274f212SChris Wilson 				 * batches). The only time we do not increment
31646274f212SChris Wilson 				 * the hangcheck score on this ring, if this
31656274f212SChris Wilson 				 * ring is in a legitimate wait for another
31666274f212SChris Wilson 				 * ring. In that case the waiting ring is a
31676274f212SChris Wilson 				 * victim and we want to be sure we catch the
31686274f212SChris Wilson 				 * right culprit. Then every time we do kick
31696274f212SChris Wilson 				 * the ring, add a small increment to the
31706274f212SChris Wilson 				 * score so that we can catch a batch that is
31716274f212SChris Wilson 				 * being repeatedly kicked and so responsible
31726274f212SChris Wilson 				 * for stalling the machine.
31739107e9d2SChris Wilson 				 */
3174e2f80391STvrtko Ursulin 				engine->hangcheck.action = ring_stuck(engine,
3175ad8beaeaSMika Kuoppala 								      acthd);
3176ad8beaeaSMika Kuoppala 
3177e2f80391STvrtko Ursulin 				switch (engine->hangcheck.action) {
3178da661464SMika Kuoppala 				case HANGCHECK_IDLE:
3179f2f4d82fSJani Nikula 				case HANGCHECK_WAIT:
3180f260fe7bSMika Kuoppala 					break;
318124a65e62SMika Kuoppala 				case HANGCHECK_ACTIVE:
3182e2f80391STvrtko Ursulin 					engine->hangcheck.score += BUSY;
31836274f212SChris Wilson 					break;
3184f2f4d82fSJani Nikula 				case HANGCHECK_KICK:
3185e2f80391STvrtko Ursulin 					engine->hangcheck.score += KICK;
31866274f212SChris Wilson 					break;
3187f2f4d82fSJani Nikula 				case HANGCHECK_HUNG:
3188e2f80391STvrtko Ursulin 					engine->hangcheck.score += HUNG;
3189c3232b18SDave Gordon 					stuck[id] = true;
31906274f212SChris Wilson 					break;
31916274f212SChris Wilson 				}
319205407ff8SMika Kuoppala 			}
31939107e9d2SChris Wilson 		} else {
3194e2f80391STvrtko Ursulin 			engine->hangcheck.action = HANGCHECK_ACTIVE;
3195da661464SMika Kuoppala 
31969107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
31979107e9d2SChris Wilson 			 * attempts across multiple batches.
31989107e9d2SChris Wilson 			 */
3199e2f80391STvrtko Ursulin 			if (engine->hangcheck.score > 0)
3200e2f80391STvrtko Ursulin 				engine->hangcheck.score -= ACTIVE_DECAY;
3201e2f80391STvrtko Ursulin 			if (engine->hangcheck.score < 0)
3202e2f80391STvrtko Ursulin 				engine->hangcheck.score = 0;
3203f260fe7bSMika Kuoppala 
320461642ff0SMika Kuoppala 			/* Clear head and subunit states on seqno movement */
320512471ba8SChris Wilson 			acthd = 0;
320661642ff0SMika Kuoppala 
3207e2f80391STvrtko Ursulin 			memset(engine->hangcheck.instdone, 0,
3208e2f80391STvrtko Ursulin 			       sizeof(engine->hangcheck.instdone));
3209cbb465e7SChris Wilson 		}
3210f65d9421SBen Gamari 
3211e2f80391STvrtko Ursulin 		engine->hangcheck.seqno = seqno;
3212e2f80391STvrtko Ursulin 		engine->hangcheck.acthd = acthd;
321312471ba8SChris Wilson 		engine->hangcheck.user_interrupts = user_interrupts;
32149107e9d2SChris Wilson 		busy_count += busy;
321505407ff8SMika Kuoppala 	}
321605407ff8SMika Kuoppala 
3217c3232b18SDave Gordon 	for_each_engine_id(engine, dev_priv, id) {
3218e2f80391STvrtko Ursulin 		if (engine->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG) {
3219b8d88d1dSDaniel Vetter 			DRM_INFO("%s on %s\n",
3220c3232b18SDave Gordon 				 stuck[id] ? "stuck" : "no progress",
3221e2f80391STvrtko Ursulin 				 engine->name);
322214b730fcSarun.siluvery@linux.intel.com 			rings_hung |= intel_engine_flag(engine);
322305407ff8SMika Kuoppala 		}
322405407ff8SMika Kuoppala 	}
322505407ff8SMika Kuoppala 
32261f814dacSImre Deak 	if (rings_hung) {
3227c033666aSChris Wilson 		i915_handle_error(dev_priv, rings_hung, "Engine(s) hung");
32281f814dacSImre Deak 		goto out;
32291f814dacSImre Deak 	}
323005407ff8SMika Kuoppala 
323105535726SChris Wilson 	/* Reset timer in case GPU hangs without another request being added */
323205407ff8SMika Kuoppala 	if (busy_count)
3233c033666aSChris Wilson 		i915_queue_hangcheck(dev_priv);
32341f814dacSImre Deak 
32351f814dacSImre Deak out:
32361f814dacSImre Deak 	ENABLE_RPM_WAKEREF_ASSERTS(dev_priv);
323710cd45b6SMika Kuoppala }
323810cd45b6SMika Kuoppala 
32391c69eb42SPaulo Zanoni static void ibx_irq_reset(struct drm_device *dev)
324091738a95SPaulo Zanoni {
324191738a95SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
324291738a95SPaulo Zanoni 
324391738a95SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
324491738a95SPaulo Zanoni 		return;
324591738a95SPaulo Zanoni 
3246f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(SDE);
3247105b122eSPaulo Zanoni 
3248105b122eSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
3249105b122eSPaulo Zanoni 		I915_WRITE(SERR_INT, 0xffffffff);
3250622364b6SPaulo Zanoni }
3251105b122eSPaulo Zanoni 
325291738a95SPaulo Zanoni /*
3253622364b6SPaulo Zanoni  * SDEIER is also touched by the interrupt handler to work around missed PCH
3254622364b6SPaulo Zanoni  * interrupts. Hence we can't update it after the interrupt handler is enabled -
3255622364b6SPaulo Zanoni  * instead we unconditionally enable all PCH interrupt sources here, but then
3256622364b6SPaulo Zanoni  * only unmask them as needed with SDEIMR.
3257622364b6SPaulo Zanoni  *
3258622364b6SPaulo Zanoni  * This function needs to be called before interrupts are enabled.
325991738a95SPaulo Zanoni  */
3260622364b6SPaulo Zanoni static void ibx_irq_pre_postinstall(struct drm_device *dev)
3261622364b6SPaulo Zanoni {
3262622364b6SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3263622364b6SPaulo Zanoni 
3264622364b6SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
3265622364b6SPaulo Zanoni 		return;
3266622364b6SPaulo Zanoni 
3267622364b6SPaulo Zanoni 	WARN_ON(I915_READ(SDEIER) != 0);
326891738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
326991738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
327091738a95SPaulo Zanoni }
327191738a95SPaulo Zanoni 
32727c4d664eSPaulo Zanoni static void gen5_gt_irq_reset(struct drm_device *dev)
3273d18ea1b5SDaniel Vetter {
3274d18ea1b5SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
3275d18ea1b5SDaniel Vetter 
3276f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GT);
3277a9d356a6SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6)
3278f86f3fb0SPaulo Zanoni 		GEN5_IRQ_RESET(GEN6_PM);
3279d18ea1b5SDaniel Vetter }
3280d18ea1b5SDaniel Vetter 
328170591a41SVille Syrjälä static void vlv_display_irq_reset(struct drm_i915_private *dev_priv)
328270591a41SVille Syrjälä {
328370591a41SVille Syrjälä 	enum pipe pipe;
328470591a41SVille Syrjälä 
328571b8b41dSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
328671b8b41dSVille Syrjälä 		I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
328771b8b41dSVille Syrjälä 	else
328871b8b41dSVille Syrjälä 		I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
328971b8b41dSVille Syrjälä 
3290ad22d106SVille Syrjälä 	i915_hotplug_interrupt_update_locked(dev_priv, 0xffffffff, 0);
329170591a41SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
329270591a41SVille Syrjälä 
3293ad22d106SVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
3294ad22d106SVille Syrjälä 		I915_WRITE(PIPESTAT(pipe),
3295ad22d106SVille Syrjälä 			   PIPE_FIFO_UNDERRUN_STATUS |
3296ad22d106SVille Syrjälä 			   PIPESTAT_INT_STATUS_MASK);
3297ad22d106SVille Syrjälä 		dev_priv->pipestat_irq_mask[pipe] = 0;
3298ad22d106SVille Syrjälä 	}
329970591a41SVille Syrjälä 
330070591a41SVille Syrjälä 	GEN5_IRQ_RESET(VLV_);
3301ad22d106SVille Syrjälä 	dev_priv->irq_mask = ~0;
330270591a41SVille Syrjälä }
330370591a41SVille Syrjälä 
33048bb61306SVille Syrjälä static void vlv_display_irq_postinstall(struct drm_i915_private *dev_priv)
33058bb61306SVille Syrjälä {
33068bb61306SVille Syrjälä 	u32 pipestat_mask;
33079ab981f2SVille Syrjälä 	u32 enable_mask;
33088bb61306SVille Syrjälä 	enum pipe pipe;
33098bb61306SVille Syrjälä 
33108bb61306SVille Syrjälä 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
33118bb61306SVille Syrjälä 			PIPE_CRC_DONE_INTERRUPT_STATUS;
33128bb61306SVille Syrjälä 
33138bb61306SVille Syrjälä 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
33148bb61306SVille Syrjälä 	for_each_pipe(dev_priv, pipe)
33158bb61306SVille Syrjälä 		i915_enable_pipestat(dev_priv, pipe, pipestat_mask);
33168bb61306SVille Syrjälä 
33179ab981f2SVille Syrjälä 	enable_mask = I915_DISPLAY_PORT_INTERRUPT |
33188bb61306SVille Syrjälä 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
33198bb61306SVille Syrjälä 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
33208bb61306SVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
33219ab981f2SVille Syrjälä 		enable_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
33226b7eafc1SVille Syrjälä 
33236b7eafc1SVille Syrjälä 	WARN_ON(dev_priv->irq_mask != ~0);
33246b7eafc1SVille Syrjälä 
33259ab981f2SVille Syrjälä 	dev_priv->irq_mask = ~enable_mask;
33268bb61306SVille Syrjälä 
33279ab981f2SVille Syrjälä 	GEN5_IRQ_INIT(VLV_, dev_priv->irq_mask, enable_mask);
33288bb61306SVille Syrjälä }
33298bb61306SVille Syrjälä 
33308bb61306SVille Syrjälä /* drm_dma.h hooks
33318bb61306SVille Syrjälä */
33328bb61306SVille Syrjälä static void ironlake_irq_reset(struct drm_device *dev)
33338bb61306SVille Syrjälä {
33348bb61306SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
33358bb61306SVille Syrjälä 
33368bb61306SVille Syrjälä 	I915_WRITE(HWSTAM, 0xffffffff);
33378bb61306SVille Syrjälä 
33388bb61306SVille Syrjälä 	GEN5_IRQ_RESET(DE);
33398bb61306SVille Syrjälä 	if (IS_GEN7(dev))
33408bb61306SVille Syrjälä 		I915_WRITE(GEN7_ERR_INT, 0xffffffff);
33418bb61306SVille Syrjälä 
33428bb61306SVille Syrjälä 	gen5_gt_irq_reset(dev);
33438bb61306SVille Syrjälä 
33448bb61306SVille Syrjälä 	ibx_irq_reset(dev);
33458bb61306SVille Syrjälä }
33468bb61306SVille Syrjälä 
33477e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
33487e231dbeSJesse Barnes {
33492d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
33507e231dbeSJesse Barnes 
335134c7b8a7SVille Syrjälä 	I915_WRITE(VLV_MASTER_IER, 0);
335234c7b8a7SVille Syrjälä 	POSTING_READ(VLV_MASTER_IER);
335334c7b8a7SVille Syrjälä 
33547c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
33557e231dbeSJesse Barnes 
3356ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
33579918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
335870591a41SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3359ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
33607e231dbeSJesse Barnes }
33617e231dbeSJesse Barnes 
3362d6e3cca3SDaniel Vetter static void gen8_gt_irq_reset(struct drm_i915_private *dev_priv)
3363d6e3cca3SDaniel Vetter {
3364d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 0);
3365d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 1);
3366d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 2);
3367d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 3);
3368d6e3cca3SDaniel Vetter }
3369d6e3cca3SDaniel Vetter 
3370823f6b38SPaulo Zanoni static void gen8_irq_reset(struct drm_device *dev)
3371abd58f01SBen Widawsky {
3372abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3373abd58f01SBen Widawsky 	int pipe;
3374abd58f01SBen Widawsky 
3375abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
3376abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3377abd58f01SBen Widawsky 
3378d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
3379abd58f01SBen Widawsky 
3380055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3381f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3382813bde43SPaulo Zanoni 						   POWER_DOMAIN_PIPE(pipe)))
3383f86f3fb0SPaulo Zanoni 			GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3384abd58f01SBen Widawsky 
3385f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_PORT_);
3386f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_MISC_);
3387f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_PCU_);
3388abd58f01SBen Widawsky 
3389266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev))
33901c69eb42SPaulo Zanoni 		ibx_irq_reset(dev);
3391abd58f01SBen Widawsky }
3392abd58f01SBen Widawsky 
33934c6c03beSDamien Lespiau void gen8_irq_power_well_post_enable(struct drm_i915_private *dev_priv,
33944c6c03beSDamien Lespiau 				     unsigned int pipe_mask)
3395d49bdb0eSPaulo Zanoni {
33961180e206SPaulo Zanoni 	uint32_t extra_ier = GEN8_PIPE_VBLANK | GEN8_PIPE_FIFO_UNDERRUN;
33976831f3e3SVille Syrjälä 	enum pipe pipe;
3398d49bdb0eSPaulo Zanoni 
339913321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
34006831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
34016831f3e3SVille Syrjälä 		GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
34026831f3e3SVille Syrjälä 				  dev_priv->de_irq_mask[pipe],
34036831f3e3SVille Syrjälä 				  ~dev_priv->de_irq_mask[pipe] | extra_ier);
340413321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3405d49bdb0eSPaulo Zanoni }
3406d49bdb0eSPaulo Zanoni 
3407aae8ba84SVille Syrjälä void gen8_irq_power_well_pre_disable(struct drm_i915_private *dev_priv,
3408aae8ba84SVille Syrjälä 				     unsigned int pipe_mask)
3409aae8ba84SVille Syrjälä {
34106831f3e3SVille Syrjälä 	enum pipe pipe;
34116831f3e3SVille Syrjälä 
3412aae8ba84SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
34136831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
34146831f3e3SVille Syrjälä 		GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3415aae8ba84SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3416aae8ba84SVille Syrjälä 
3417aae8ba84SVille Syrjälä 	/* make sure we're done processing display irqs */
3418aae8ba84SVille Syrjälä 	synchronize_irq(dev_priv->dev->irq);
3419aae8ba84SVille Syrjälä }
3420aae8ba84SVille Syrjälä 
342143f328d7SVille Syrjälä static void cherryview_irq_preinstall(struct drm_device *dev)
342243f328d7SVille Syrjälä {
342343f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
342443f328d7SVille Syrjälä 
342543f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
342643f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
342743f328d7SVille Syrjälä 
3428d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
342943f328d7SVille Syrjälä 
343043f328d7SVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
343143f328d7SVille Syrjälä 
3432ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
34339918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
343470591a41SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3435ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
343643f328d7SVille Syrjälä }
343743f328d7SVille Syrjälä 
343891d14251STvrtko Ursulin static u32 intel_hpd_enabled_irqs(struct drm_i915_private *dev_priv,
343987a02106SVille Syrjälä 				  const u32 hpd[HPD_NUM_PINS])
344087a02106SVille Syrjälä {
344187a02106SVille Syrjälä 	struct intel_encoder *encoder;
344287a02106SVille Syrjälä 	u32 enabled_irqs = 0;
344387a02106SVille Syrjälä 
344491d14251STvrtko Ursulin 	for_each_intel_encoder(dev_priv->dev, encoder)
344587a02106SVille Syrjälä 		if (dev_priv->hotplug.stats[encoder->hpd_pin].state == HPD_ENABLED)
344687a02106SVille Syrjälä 			enabled_irqs |= hpd[encoder->hpd_pin];
344787a02106SVille Syrjälä 
344887a02106SVille Syrjälä 	return enabled_irqs;
344987a02106SVille Syrjälä }
345087a02106SVille Syrjälä 
345191d14251STvrtko Ursulin static void ibx_hpd_irq_setup(struct drm_i915_private *dev_priv)
345282a28bcfSDaniel Vetter {
345387a02106SVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
345482a28bcfSDaniel Vetter 
345591d14251STvrtko Ursulin 	if (HAS_PCH_IBX(dev_priv)) {
3456fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
345791d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ibx);
345882a28bcfSDaniel Vetter 	} else {
3459fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
346091d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_cpt);
346182a28bcfSDaniel Vetter 	}
346282a28bcfSDaniel Vetter 
3463fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
346482a28bcfSDaniel Vetter 
34657fe0b973SKeith Packard 	/*
34667fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
34676dbf30ceSVille Syrjälä 	 * duration to 2ms (which is the minimum in the Display Port spec).
34686dbf30ceSVille Syrjälä 	 * The pulse duration bits are reserved on LPT+.
34697fe0b973SKeith Packard 	 */
34707fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
34717fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
34727fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
34737fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
34747fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
34750b2eb33eSVille Syrjälä 	/*
34760b2eb33eSVille Syrjälä 	 * When CPU and PCH are on the same package, port A
34770b2eb33eSVille Syrjälä 	 * HPD must be enabled in both north and south.
34780b2eb33eSVille Syrjälä 	 */
347991d14251STvrtko Ursulin 	if (HAS_PCH_LPT_LP(dev_priv))
34800b2eb33eSVille Syrjälä 		hotplug |= PORTA_HOTPLUG_ENABLE;
34817fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
34826dbf30ceSVille Syrjälä }
348326951cafSXiong Zhang 
348491d14251STvrtko Ursulin static void spt_hpd_irq_setup(struct drm_i915_private *dev_priv)
34856dbf30ceSVille Syrjälä {
34866dbf30ceSVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
34876dbf30ceSVille Syrjälä 
34886dbf30ceSVille Syrjälä 	hotplug_irqs = SDE_HOTPLUG_MASK_SPT;
348991d14251STvrtko Ursulin 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_spt);
34906dbf30ceSVille Syrjälä 
34916dbf30ceSVille Syrjälä 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
34926dbf30ceSVille Syrjälä 
34936dbf30ceSVille Syrjälä 	/* Enable digital hotplug on the PCH */
34946dbf30ceSVille Syrjälä 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
34956dbf30ceSVille Syrjälä 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTC_HOTPLUG_ENABLE |
349674c0b395SVille Syrjälä 		PORTB_HOTPLUG_ENABLE | PORTA_HOTPLUG_ENABLE;
34976dbf30ceSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
34986dbf30ceSVille Syrjälä 
349926951cafSXiong Zhang 	hotplug = I915_READ(PCH_PORT_HOTPLUG2);
350026951cafSXiong Zhang 	hotplug |= PORTE_HOTPLUG_ENABLE;
350126951cafSXiong Zhang 	I915_WRITE(PCH_PORT_HOTPLUG2, hotplug);
350226951cafSXiong Zhang }
35037fe0b973SKeith Packard 
350491d14251STvrtko Ursulin static void ilk_hpd_irq_setup(struct drm_i915_private *dev_priv)
3505e4ce95aaSVille Syrjälä {
3506e4ce95aaSVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
3507e4ce95aaSVille Syrjälä 
350891d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 8) {
35093a3b3c7dSVille Syrjälä 		hotplug_irqs = GEN8_PORT_DP_A_HOTPLUG;
351091d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_bdw);
35113a3b3c7dSVille Syrjälä 
35123a3b3c7dSVille Syrjälä 		bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
351391d14251STvrtko Ursulin 	} else if (INTEL_GEN(dev_priv) >= 7) {
351423bb4cb5SVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG_IVB;
351591d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ivb);
35163a3b3c7dSVille Syrjälä 
35173a3b3c7dSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
351823bb4cb5SVille Syrjälä 	} else {
3519e4ce95aaSVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG;
352091d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ilk);
3521e4ce95aaSVille Syrjälä 
3522e4ce95aaSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
35233a3b3c7dSVille Syrjälä 	}
3524e4ce95aaSVille Syrjälä 
3525e4ce95aaSVille Syrjälä 	/*
3526e4ce95aaSVille Syrjälä 	 * Enable digital hotplug on the CPU, and configure the DP short pulse
3527e4ce95aaSVille Syrjälä 	 * duration to 2ms (which is the minimum in the Display Port spec)
352823bb4cb5SVille Syrjälä 	 * The pulse duration bits are reserved on HSW+.
3529e4ce95aaSVille Syrjälä 	 */
3530e4ce95aaSVille Syrjälä 	hotplug = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
3531e4ce95aaSVille Syrjälä 	hotplug &= ~DIGITAL_PORTA_PULSE_DURATION_MASK;
3532e4ce95aaSVille Syrjälä 	hotplug |= DIGITAL_PORTA_HOTPLUG_ENABLE | DIGITAL_PORTA_PULSE_DURATION_2ms;
3533e4ce95aaSVille Syrjälä 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, hotplug);
3534e4ce95aaSVille Syrjälä 
353591d14251STvrtko Ursulin 	ibx_hpd_irq_setup(dev_priv);
3536e4ce95aaSVille Syrjälä }
3537e4ce95aaSVille Syrjälä 
353891d14251STvrtko Ursulin static void bxt_hpd_irq_setup(struct drm_i915_private *dev_priv)
3539e0a20ad7SShashank Sharma {
3540a52bb15bSVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
3541e0a20ad7SShashank Sharma 
354291d14251STvrtko Ursulin 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_bxt);
3543a52bb15bSVille Syrjälä 	hotplug_irqs = BXT_DE_PORT_HOTPLUG_MASK;
3544e0a20ad7SShashank Sharma 
3545a52bb15bSVille Syrjälä 	bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
3546e0a20ad7SShashank Sharma 
3547a52bb15bSVille Syrjälä 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
3548a52bb15bSVille Syrjälä 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTB_HOTPLUG_ENABLE |
3549a52bb15bSVille Syrjälä 		PORTA_HOTPLUG_ENABLE;
3550d252bf68SShubhangi Shrivastava 
3551d252bf68SShubhangi Shrivastava 	DRM_DEBUG_KMS("Invert bit setting: hp_ctl:%x hp_port:%x\n",
3552d252bf68SShubhangi Shrivastava 		      hotplug, enabled_irqs);
3553d252bf68SShubhangi Shrivastava 	hotplug &= ~BXT_DDI_HPD_INVERT_MASK;
3554d252bf68SShubhangi Shrivastava 
3555d252bf68SShubhangi Shrivastava 	/*
3556d252bf68SShubhangi Shrivastava 	 * For BXT invert bit has to be set based on AOB design
3557d252bf68SShubhangi Shrivastava 	 * for HPD detection logic, update it based on VBT fields.
3558d252bf68SShubhangi Shrivastava 	 */
3559d252bf68SShubhangi Shrivastava 
3560d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIA) &&
3561d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_A))
3562d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIA_HPD_INVERT;
3563d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIB) &&
3564d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_B))
3565d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIB_HPD_INVERT;
3566d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIC) &&
3567d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_C))
3568d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIC_HPD_INVERT;
3569d252bf68SShubhangi Shrivastava 
3570a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
3571e0a20ad7SShashank Sharma }
3572e0a20ad7SShashank Sharma 
3573d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
3574d46da437SPaulo Zanoni {
35752d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
357682a28bcfSDaniel Vetter 	u32 mask;
3577d46da437SPaulo Zanoni 
3578692a04cfSDaniel Vetter 	if (HAS_PCH_NOP(dev))
3579692a04cfSDaniel Vetter 		return;
3580692a04cfSDaniel Vetter 
3581105b122eSPaulo Zanoni 	if (HAS_PCH_IBX(dev))
35825c673b60SDaniel Vetter 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
3583105b122eSPaulo Zanoni 	else
35845c673b60SDaniel Vetter 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
35858664281bSPaulo Zanoni 
3586b51a2842SVille Syrjälä 	gen5_assert_iir_is_zero(dev_priv, SDEIIR);
3587d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
3588d46da437SPaulo Zanoni }
3589d46da437SPaulo Zanoni 
35900a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
35910a9a8c91SDaniel Vetter {
35920a9a8c91SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
35930a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
35940a9a8c91SDaniel Vetter 
35950a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
35960a9a8c91SDaniel Vetter 
35970a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
3598040d2baaSBen Widawsky 	if (HAS_L3_DPF(dev)) {
35990a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
360035a85ac6SBen Widawsky 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev);
360135a85ac6SBen Widawsky 		gt_irqs |= GT_PARITY_ERROR(dev);
36020a9a8c91SDaniel Vetter 	}
36030a9a8c91SDaniel Vetter 
36040a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
36050a9a8c91SDaniel Vetter 	if (IS_GEN5(dev)) {
36060a9a8c91SDaniel Vetter 		gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
36070a9a8c91SDaniel Vetter 			   ILK_BSD_USER_INTERRUPT;
36080a9a8c91SDaniel Vetter 	} else {
36090a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
36100a9a8c91SDaniel Vetter 	}
36110a9a8c91SDaniel Vetter 
361235079899SPaulo Zanoni 	GEN5_IRQ_INIT(GT, dev_priv->gt_irq_mask, gt_irqs);
36130a9a8c91SDaniel Vetter 
36140a9a8c91SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
361578e68d36SImre Deak 		/*
361678e68d36SImre Deak 		 * RPS interrupts will get enabled/disabled on demand when RPS
361778e68d36SImre Deak 		 * itself is enabled/disabled.
361878e68d36SImre Deak 		 */
36190a9a8c91SDaniel Vetter 		if (HAS_VEBOX(dev))
36200a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
36210a9a8c91SDaniel Vetter 
3622605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = 0xffffffff;
362335079899SPaulo Zanoni 		GEN5_IRQ_INIT(GEN6_PM, dev_priv->pm_irq_mask, pm_irqs);
36240a9a8c91SDaniel Vetter 	}
36250a9a8c91SDaniel Vetter }
36260a9a8c91SDaniel Vetter 
3627f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
3628036a4a7dSZhenyu Wang {
36292d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
36308e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
36318e76f8dcSPaulo Zanoni 
36328e76f8dcSPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 7) {
36338e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
36348e76f8dcSPaulo Zanoni 				DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
36358e76f8dcSPaulo Zanoni 				DE_PLANEB_FLIP_DONE_IVB |
36365c673b60SDaniel Vetter 				DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB);
36378e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
363823bb4cb5SVille Syrjälä 			      DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB |
363923bb4cb5SVille Syrjälä 			      DE_DP_A_HOTPLUG_IVB);
36408e76f8dcSPaulo Zanoni 	} else {
36418e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3642ce99c256SDaniel Vetter 				DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
36435b3a856bSDaniel Vetter 				DE_AUX_CHANNEL_A |
36445b3a856bSDaniel Vetter 				DE_PIPEB_CRC_DONE | DE_PIPEA_CRC_DONE |
36455b3a856bSDaniel Vetter 				DE_POISON);
3646e4ce95aaSVille Syrjälä 		extra_mask = (DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
3647e4ce95aaSVille Syrjälä 			      DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN |
3648e4ce95aaSVille Syrjälä 			      DE_DP_A_HOTPLUG);
36498e76f8dcSPaulo Zanoni 	}
3650036a4a7dSZhenyu Wang 
36511ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3652036a4a7dSZhenyu Wang 
36530c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xeffe);
36540c841212SPaulo Zanoni 
3655622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3656622364b6SPaulo Zanoni 
365735079899SPaulo Zanoni 	GEN5_IRQ_INIT(DE, dev_priv->irq_mask, display_mask | extra_mask);
3658036a4a7dSZhenyu Wang 
36590a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
3660036a4a7dSZhenyu Wang 
3661d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
36627fe0b973SKeith Packard 
3663f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
36646005ce42SDaniel Vetter 		/* Enable PCU event interrupts
36656005ce42SDaniel Vetter 		 *
36666005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
36674bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
36684bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
3669d6207435SDaniel Vetter 		spin_lock_irq(&dev_priv->irq_lock);
3670fbdedaeaSVille Syrjälä 		ilk_enable_display_irq(dev_priv, DE_PCU_EVENT);
3671d6207435SDaniel Vetter 		spin_unlock_irq(&dev_priv->irq_lock);
3672f97108d1SJesse Barnes 	}
3673f97108d1SJesse Barnes 
3674036a4a7dSZhenyu Wang 	return 0;
3675036a4a7dSZhenyu Wang }
3676036a4a7dSZhenyu Wang 
3677f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3678f8b79e58SImre Deak {
3679f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3680f8b79e58SImre Deak 
3681f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3682f8b79e58SImre Deak 		return;
3683f8b79e58SImre Deak 
3684f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3685f8b79e58SImre Deak 
3686d6c69803SVille Syrjälä 	if (intel_irqs_enabled(dev_priv)) {
3687d6c69803SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3688ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3689f8b79e58SImre Deak 	}
3690d6c69803SVille Syrjälä }
3691f8b79e58SImre Deak 
3692f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3693f8b79e58SImre Deak {
3694f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3695f8b79e58SImre Deak 
3696f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3697f8b79e58SImre Deak 		return;
3698f8b79e58SImre Deak 
3699f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3700f8b79e58SImre Deak 
3701950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3702ad22d106SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3703f8b79e58SImre Deak }
3704f8b79e58SImre Deak 
37050e6c9a9eSVille Syrjälä 
37060e6c9a9eSVille Syrjälä static int valleyview_irq_postinstall(struct drm_device *dev)
37070e6c9a9eSVille Syrjälä {
37080e6c9a9eSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
37090e6c9a9eSVille Syrjälä 
37100a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
37117e231dbeSJesse Barnes 
3712ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
37139918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3714ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3715ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3716ad22d106SVille Syrjälä 
37177e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
371834c7b8a7SVille Syrjälä 	POSTING_READ(VLV_MASTER_IER);
371920afbda2SDaniel Vetter 
372020afbda2SDaniel Vetter 	return 0;
372120afbda2SDaniel Vetter }
372220afbda2SDaniel Vetter 
3723abd58f01SBen Widawsky static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
3724abd58f01SBen Widawsky {
3725abd58f01SBen Widawsky 	/* These are interrupts we'll toggle with the ring mask register */
3726abd58f01SBen Widawsky 	uint32_t gt_interrupts[] = {
3727abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
372873d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
372973d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT |
373073d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT,
3731abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
373273d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
373373d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT |
373473d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT,
3735abd58f01SBen Widawsky 		0,
373673d477f6SOscar Mateo 		GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT |
373773d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT
3738abd58f01SBen Widawsky 		};
3739abd58f01SBen Widawsky 
374098735739STvrtko Ursulin 	if (HAS_L3_DPF(dev_priv))
374198735739STvrtko Ursulin 		gt_interrupts[0] |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
374298735739STvrtko Ursulin 
37430961021aSBen Widawsky 	dev_priv->pm_irq_mask = 0xffffffff;
37449a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 0, ~gt_interrupts[0], gt_interrupts[0]);
37459a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 1, ~gt_interrupts[1], gt_interrupts[1]);
374678e68d36SImre Deak 	/*
374778e68d36SImre Deak 	 * RPS interrupts will get enabled/disabled on demand when RPS itself
374878e68d36SImre Deak 	 * is enabled/disabled.
374978e68d36SImre Deak 	 */
375078e68d36SImre Deak 	GEN8_IRQ_INIT_NDX(GT, 2, dev_priv->pm_irq_mask, 0);
37519a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 3, ~gt_interrupts[3], gt_interrupts[3]);
3752abd58f01SBen Widawsky }
3753abd58f01SBen Widawsky 
3754abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3755abd58f01SBen Widawsky {
3756770de83dSDamien Lespiau 	uint32_t de_pipe_masked = GEN8_PIPE_CDCLK_CRC_DONE;
3757770de83dSDamien Lespiau 	uint32_t de_pipe_enables;
37583a3b3c7dSVille Syrjälä 	u32 de_port_masked = GEN8_AUX_CHANNEL_A;
37593a3b3c7dSVille Syrjälä 	u32 de_port_enables;
376011825b0dSVille Syrjälä 	u32 de_misc_masked = GEN8_DE_MISC_GSE;
37613a3b3c7dSVille Syrjälä 	enum pipe pipe;
3762770de83dSDamien Lespiau 
3763b4834a50SRodrigo Vivi 	if (INTEL_INFO(dev_priv)->gen >= 9) {
3764770de83dSDamien Lespiau 		de_pipe_masked |= GEN9_PIPE_PLANE1_FLIP_DONE |
3765770de83dSDamien Lespiau 				  GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
37663a3b3c7dSVille Syrjälä 		de_port_masked |= GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
376788e04703SJesse Barnes 				  GEN9_AUX_CHANNEL_D;
37689e63743eSShashank Sharma 		if (IS_BROXTON(dev_priv))
37693a3b3c7dSVille Syrjälä 			de_port_masked |= BXT_DE_PORT_GMBUS;
37703a3b3c7dSVille Syrjälä 	} else {
3771770de83dSDamien Lespiau 		de_pipe_masked |= GEN8_PIPE_PRIMARY_FLIP_DONE |
3772770de83dSDamien Lespiau 				  GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
37733a3b3c7dSVille Syrjälä 	}
3774770de83dSDamien Lespiau 
3775770de83dSDamien Lespiau 	de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
3776770de83dSDamien Lespiau 					   GEN8_PIPE_FIFO_UNDERRUN;
3777770de83dSDamien Lespiau 
37783a3b3c7dSVille Syrjälä 	de_port_enables = de_port_masked;
3779a52bb15bSVille Syrjälä 	if (IS_BROXTON(dev_priv))
3780a52bb15bSVille Syrjälä 		de_port_enables |= BXT_DE_PORT_HOTPLUG_MASK;
3781a52bb15bSVille Syrjälä 	else if (IS_BROADWELL(dev_priv))
37823a3b3c7dSVille Syrjälä 		de_port_enables |= GEN8_PORT_DP_A_HOTPLUG;
37833a3b3c7dSVille Syrjälä 
378413b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_A] = ~de_pipe_masked;
378513b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_B] = ~de_pipe_masked;
378613b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_C] = ~de_pipe_masked;
3787abd58f01SBen Widawsky 
3788055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3789f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3790813bde43SPaulo Zanoni 				POWER_DOMAIN_PIPE(pipe)))
3791813bde43SPaulo Zanoni 			GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
3792813bde43SPaulo Zanoni 					  dev_priv->de_irq_mask[pipe],
379335079899SPaulo Zanoni 					  de_pipe_enables);
3794abd58f01SBen Widawsky 
37953a3b3c7dSVille Syrjälä 	GEN5_IRQ_INIT(GEN8_DE_PORT_, ~de_port_masked, de_port_enables);
379611825b0dSVille Syrjälä 	GEN5_IRQ_INIT(GEN8_DE_MISC_, ~de_misc_masked, de_misc_masked);
3797abd58f01SBen Widawsky }
3798abd58f01SBen Widawsky 
3799abd58f01SBen Widawsky static int gen8_irq_postinstall(struct drm_device *dev)
3800abd58f01SBen Widawsky {
3801abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3802abd58f01SBen Widawsky 
3803266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev))
3804622364b6SPaulo Zanoni 		ibx_irq_pre_postinstall(dev);
3805622364b6SPaulo Zanoni 
3806abd58f01SBen Widawsky 	gen8_gt_irq_postinstall(dev_priv);
3807abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3808abd58f01SBen Widawsky 
3809266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev))
3810abd58f01SBen Widawsky 		ibx_irq_postinstall(dev);
3811abd58f01SBen Widawsky 
3812e5328c43SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
3813abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3814abd58f01SBen Widawsky 
3815abd58f01SBen Widawsky 	return 0;
3816abd58f01SBen Widawsky }
3817abd58f01SBen Widawsky 
381843f328d7SVille Syrjälä static int cherryview_irq_postinstall(struct drm_device *dev)
381943f328d7SVille Syrjälä {
382043f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
382143f328d7SVille Syrjälä 
382243f328d7SVille Syrjälä 	gen8_gt_irq_postinstall(dev_priv);
382343f328d7SVille Syrjälä 
3824ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
38259918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3826ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3827ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3828ad22d106SVille Syrjälä 
3829e5328c43SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
383043f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
383143f328d7SVille Syrjälä 
383243f328d7SVille Syrjälä 	return 0;
383343f328d7SVille Syrjälä }
383443f328d7SVille Syrjälä 
3835abd58f01SBen Widawsky static void gen8_irq_uninstall(struct drm_device *dev)
3836abd58f01SBen Widawsky {
3837abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3838abd58f01SBen Widawsky 
3839abd58f01SBen Widawsky 	if (!dev_priv)
3840abd58f01SBen Widawsky 		return;
3841abd58f01SBen Widawsky 
3842823f6b38SPaulo Zanoni 	gen8_irq_reset(dev);
3843abd58f01SBen Widawsky }
3844abd58f01SBen Widawsky 
38457e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
38467e231dbeSJesse Barnes {
38472d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
38487e231dbeSJesse Barnes 
38497e231dbeSJesse Barnes 	if (!dev_priv)
38507e231dbeSJesse Barnes 		return;
38517e231dbeSJesse Barnes 
3852843d0e7dSImre Deak 	I915_WRITE(VLV_MASTER_IER, 0);
385334c7b8a7SVille Syrjälä 	POSTING_READ(VLV_MASTER_IER);
3854843d0e7dSImre Deak 
3855893fce8eSVille Syrjälä 	gen5_gt_irq_reset(dev);
3856893fce8eSVille Syrjälä 
38577e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
3858f8b79e58SImre Deak 
3859ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
38609918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3861ad22d106SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3862ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
38637e231dbeSJesse Barnes }
38647e231dbeSJesse Barnes 
386543f328d7SVille Syrjälä static void cherryview_irq_uninstall(struct drm_device *dev)
386643f328d7SVille Syrjälä {
386743f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
386843f328d7SVille Syrjälä 
386943f328d7SVille Syrjälä 	if (!dev_priv)
387043f328d7SVille Syrjälä 		return;
387143f328d7SVille Syrjälä 
387243f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
387343f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
387443f328d7SVille Syrjälä 
3875a2c30fbaSVille Syrjälä 	gen8_gt_irq_reset(dev_priv);
387643f328d7SVille Syrjälä 
3877a2c30fbaSVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
387843f328d7SVille Syrjälä 
3879ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
38809918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3881ad22d106SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3882ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
388343f328d7SVille Syrjälä }
388443f328d7SVille Syrjälä 
3885f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
3886036a4a7dSZhenyu Wang {
38872d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
38884697995bSJesse Barnes 
38894697995bSJesse Barnes 	if (!dev_priv)
38904697995bSJesse Barnes 		return;
38914697995bSJesse Barnes 
3892be30b29fSPaulo Zanoni 	ironlake_irq_reset(dev);
3893036a4a7dSZhenyu Wang }
3894036a4a7dSZhenyu Wang 
3895c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
3896c2798b19SChris Wilson {
38972d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3898c2798b19SChris Wilson 	int pipe;
3899c2798b19SChris Wilson 
3900055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3901c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3902c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3903c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3904c2798b19SChris Wilson 	POSTING_READ16(IER);
3905c2798b19SChris Wilson }
3906c2798b19SChris Wilson 
3907c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
3908c2798b19SChris Wilson {
39092d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3910c2798b19SChris Wilson 
3911c2798b19SChris Wilson 	I915_WRITE16(EMR,
3912c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3913c2798b19SChris Wilson 
3914c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
3915c2798b19SChris Wilson 	dev_priv->irq_mask =
3916c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3917c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3918c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
391937ef01abSDaniel Vetter 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
3920c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
3921c2798b19SChris Wilson 
3922c2798b19SChris Wilson 	I915_WRITE16(IER,
3923c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3924c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3925c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
3926c2798b19SChris Wilson 	POSTING_READ16(IER);
3927c2798b19SChris Wilson 
3928379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3929379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3930d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3931755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3932755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3933d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3934379ef82dSDaniel Vetter 
3935c2798b19SChris Wilson 	return 0;
3936c2798b19SChris Wilson }
3937c2798b19SChris Wilson 
39385a21b665SDaniel Vetter /*
39395a21b665SDaniel Vetter  * Returns true when a page flip has completed.
39405a21b665SDaniel Vetter  */
39415a21b665SDaniel Vetter static bool i8xx_handle_vblank(struct drm_i915_private *dev_priv,
39425a21b665SDaniel Vetter 			       int plane, int pipe, u32 iir)
39435a21b665SDaniel Vetter {
39445a21b665SDaniel Vetter 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
39455a21b665SDaniel Vetter 
39465a21b665SDaniel Vetter 	if (!intel_pipe_handle_vblank(dev_priv, pipe))
39475a21b665SDaniel Vetter 		return false;
39485a21b665SDaniel Vetter 
39495a21b665SDaniel Vetter 	if ((iir & flip_pending) == 0)
39505a21b665SDaniel Vetter 		goto check_page_flip;
39515a21b665SDaniel Vetter 
39525a21b665SDaniel Vetter 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
39535a21b665SDaniel Vetter 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
39545a21b665SDaniel Vetter 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
39555a21b665SDaniel Vetter 	 * the flip is completed (no longer pending). Since this doesn't raise
39565a21b665SDaniel Vetter 	 * an interrupt per se, we watch for the change at vblank.
39575a21b665SDaniel Vetter 	 */
39585a21b665SDaniel Vetter 	if (I915_READ16(ISR) & flip_pending)
39595a21b665SDaniel Vetter 		goto check_page_flip;
39605a21b665SDaniel Vetter 
39615a21b665SDaniel Vetter 	intel_finish_page_flip_cs(dev_priv, pipe);
39625a21b665SDaniel Vetter 	return true;
39635a21b665SDaniel Vetter 
39645a21b665SDaniel Vetter check_page_flip:
39655a21b665SDaniel Vetter 	intel_check_page_flip(dev_priv, pipe);
39665a21b665SDaniel Vetter 	return false;
39675a21b665SDaniel Vetter }
39685a21b665SDaniel Vetter 
3969ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3970c2798b19SChris Wilson {
397145a83f84SDaniel Vetter 	struct drm_device *dev = arg;
39722d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3973c2798b19SChris Wilson 	u16 iir, new_iir;
3974c2798b19SChris Wilson 	u32 pipe_stats[2];
3975c2798b19SChris Wilson 	int pipe;
3976c2798b19SChris Wilson 	u16 flip_mask =
3977c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3978c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
39791f814dacSImre Deak 	irqreturn_t ret;
3980c2798b19SChris Wilson 
39812dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
39822dd2a883SImre Deak 		return IRQ_NONE;
39832dd2a883SImre Deak 
39841f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
39851f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
39861f814dacSImre Deak 
39871f814dacSImre Deak 	ret = IRQ_NONE;
3988c2798b19SChris Wilson 	iir = I915_READ16(IIR);
3989c2798b19SChris Wilson 	if (iir == 0)
39901f814dacSImre Deak 		goto out;
3991c2798b19SChris Wilson 
3992c2798b19SChris Wilson 	while (iir & ~flip_mask) {
3993c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3994c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3995c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3996c2798b19SChris Wilson 		 * interrupts (for non-MSI).
3997c2798b19SChris Wilson 		 */
3998222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
3999c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4000aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4001c2798b19SChris Wilson 
4002055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4003f0f59a00SVille Syrjälä 			i915_reg_t reg = PIPESTAT(pipe);
4004c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4005c2798b19SChris Wilson 
4006c2798b19SChris Wilson 			/*
4007c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
4008c2798b19SChris Wilson 			 */
40092d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & 0x8000ffff)
4010c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
4011c2798b19SChris Wilson 		}
4012222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4013c2798b19SChris Wilson 
4014c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
4015c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
4016c2798b19SChris Wilson 
4017c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
40184a570db5STvrtko Ursulin 			notify_ring(&dev_priv->engine[RCS]);
4019c2798b19SChris Wilson 
4020055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
40215a21b665SDaniel Vetter 			int plane = pipe;
40225a21b665SDaniel Vetter 			if (HAS_FBC(dev_priv))
40235a21b665SDaniel Vetter 				plane = !plane;
40245a21b665SDaniel Vetter 
40255a21b665SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
40265a21b665SDaniel Vetter 			    i8xx_handle_vblank(dev_priv, plane, pipe, iir))
40275a21b665SDaniel Vetter 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
4028c2798b19SChris Wilson 
40294356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
403091d14251STvrtko Ursulin 				i9xx_pipe_crc_irq_handler(dev_priv, pipe);
40312d9d2b0bSVille Syrjälä 
40321f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
40331f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
40341f7247c0SDaniel Vetter 								    pipe);
40354356d586SDaniel Vetter 		}
4036c2798b19SChris Wilson 
4037c2798b19SChris Wilson 		iir = new_iir;
4038c2798b19SChris Wilson 	}
40391f814dacSImre Deak 	ret = IRQ_HANDLED;
4040c2798b19SChris Wilson 
40411f814dacSImre Deak out:
40421f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
40431f814dacSImre Deak 
40441f814dacSImre Deak 	return ret;
4045c2798b19SChris Wilson }
4046c2798b19SChris Wilson 
4047c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
4048c2798b19SChris Wilson {
40492d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4050c2798b19SChris Wilson 	int pipe;
4051c2798b19SChris Wilson 
4052055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
4053c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
4054c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4055c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
4056c2798b19SChris Wilson 	}
4057c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
4058c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
4059c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
4060c2798b19SChris Wilson }
4061c2798b19SChris Wilson 
4062a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
4063a266c7d5SChris Wilson {
40642d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4065a266c7d5SChris Wilson 	int pipe;
4066a266c7d5SChris Wilson 
4067a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
40680706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4069a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4070a266c7d5SChris Wilson 	}
4071a266c7d5SChris Wilson 
407200d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
4073055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4074a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4075a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4076a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4077a266c7d5SChris Wilson 	POSTING_READ(IER);
4078a266c7d5SChris Wilson }
4079a266c7d5SChris Wilson 
4080a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
4081a266c7d5SChris Wilson {
40822d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
408338bde180SChris Wilson 	u32 enable_mask;
4084a266c7d5SChris Wilson 
408538bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
408638bde180SChris Wilson 
408738bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
408838bde180SChris Wilson 	dev_priv->irq_mask =
408938bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
409038bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
409138bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
409238bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
409337ef01abSDaniel Vetter 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
409438bde180SChris Wilson 
409538bde180SChris Wilson 	enable_mask =
409638bde180SChris Wilson 		I915_ASLE_INTERRUPT |
409738bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
409838bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
409938bde180SChris Wilson 		I915_USER_INTERRUPT;
410038bde180SChris Wilson 
4101a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
41020706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
410320afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
410420afbda2SDaniel Vetter 
4105a266c7d5SChris Wilson 		/* Enable in IER... */
4106a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
4107a266c7d5SChris Wilson 		/* and unmask in IMR */
4108a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
4109a266c7d5SChris Wilson 	}
4110a266c7d5SChris Wilson 
4111a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
4112a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
4113a266c7d5SChris Wilson 	POSTING_READ(IER);
4114a266c7d5SChris Wilson 
411591d14251STvrtko Ursulin 	i915_enable_asle_pipestat(dev_priv);
411620afbda2SDaniel Vetter 
4117379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4118379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4119d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4120755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4121755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4122d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4123379ef82dSDaniel Vetter 
412420afbda2SDaniel Vetter 	return 0;
412520afbda2SDaniel Vetter }
412620afbda2SDaniel Vetter 
41275a21b665SDaniel Vetter /*
41285a21b665SDaniel Vetter  * Returns true when a page flip has completed.
41295a21b665SDaniel Vetter  */
41305a21b665SDaniel Vetter static bool i915_handle_vblank(struct drm_i915_private *dev_priv,
41315a21b665SDaniel Vetter 			       int plane, int pipe, u32 iir)
41325a21b665SDaniel Vetter {
41335a21b665SDaniel Vetter 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
41345a21b665SDaniel Vetter 
41355a21b665SDaniel Vetter 	if (!intel_pipe_handle_vblank(dev_priv, pipe))
41365a21b665SDaniel Vetter 		return false;
41375a21b665SDaniel Vetter 
41385a21b665SDaniel Vetter 	if ((iir & flip_pending) == 0)
41395a21b665SDaniel Vetter 		goto check_page_flip;
41405a21b665SDaniel Vetter 
41415a21b665SDaniel Vetter 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
41425a21b665SDaniel Vetter 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
41435a21b665SDaniel Vetter 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
41445a21b665SDaniel Vetter 	 * the flip is completed (no longer pending). Since this doesn't raise
41455a21b665SDaniel Vetter 	 * an interrupt per se, we watch for the change at vblank.
41465a21b665SDaniel Vetter 	 */
41475a21b665SDaniel Vetter 	if (I915_READ(ISR) & flip_pending)
41485a21b665SDaniel Vetter 		goto check_page_flip;
41495a21b665SDaniel Vetter 
41505a21b665SDaniel Vetter 	intel_finish_page_flip_cs(dev_priv, pipe);
41515a21b665SDaniel Vetter 	return true;
41525a21b665SDaniel Vetter 
41535a21b665SDaniel Vetter check_page_flip:
41545a21b665SDaniel Vetter 	intel_check_page_flip(dev_priv, pipe);
41555a21b665SDaniel Vetter 	return false;
41565a21b665SDaniel Vetter }
41575a21b665SDaniel Vetter 
4158ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
4159a266c7d5SChris Wilson {
416045a83f84SDaniel Vetter 	struct drm_device *dev = arg;
41612d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
41628291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
416338bde180SChris Wilson 	u32 flip_mask =
416438bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
416538bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
416638bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
4167a266c7d5SChris Wilson 
41682dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
41692dd2a883SImre Deak 		return IRQ_NONE;
41702dd2a883SImre Deak 
41711f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
41721f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
41731f814dacSImre Deak 
4174a266c7d5SChris Wilson 	iir = I915_READ(IIR);
417538bde180SChris Wilson 	do {
417638bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
41778291ee90SChris Wilson 		bool blc_event = false;
4178a266c7d5SChris Wilson 
4179a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4180a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4181a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4182a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
4183a266c7d5SChris Wilson 		 */
4184222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
4185a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4186aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4187a266c7d5SChris Wilson 
4188055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4189f0f59a00SVille Syrjälä 			i915_reg_t reg = PIPESTAT(pipe);
4190a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4191a266c7d5SChris Wilson 
419238bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
4193a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4194a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
419538bde180SChris Wilson 				irq_received = true;
4196a266c7d5SChris Wilson 			}
4197a266c7d5SChris Wilson 		}
4198222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4199a266c7d5SChris Wilson 
4200a266c7d5SChris Wilson 		if (!irq_received)
4201a266c7d5SChris Wilson 			break;
4202a266c7d5SChris Wilson 
4203a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
420491d14251STvrtko Ursulin 		if (I915_HAS_HOTPLUG(dev_priv) &&
42051ae3c34cSVille Syrjälä 		    iir & I915_DISPLAY_PORT_INTERRUPT) {
42061ae3c34cSVille Syrjälä 			u32 hotplug_status = i9xx_hpd_irq_ack(dev_priv);
42071ae3c34cSVille Syrjälä 			if (hotplug_status)
420891d14251STvrtko Ursulin 				i9xx_hpd_irq_handler(dev_priv, hotplug_status);
42091ae3c34cSVille Syrjälä 		}
4210a266c7d5SChris Wilson 
421138bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
4212a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4213a266c7d5SChris Wilson 
4214a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
42154a570db5STvrtko Ursulin 			notify_ring(&dev_priv->engine[RCS]);
4216a266c7d5SChris Wilson 
4217055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
42185a21b665SDaniel Vetter 			int plane = pipe;
42195a21b665SDaniel Vetter 			if (HAS_FBC(dev_priv))
42205a21b665SDaniel Vetter 				plane = !plane;
42215a21b665SDaniel Vetter 
42225a21b665SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
42235a21b665SDaniel Vetter 			    i915_handle_vblank(dev_priv, plane, pipe, iir))
42245a21b665SDaniel Vetter 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
4225a266c7d5SChris Wilson 
4226a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4227a266c7d5SChris Wilson 				blc_event = true;
42284356d586SDaniel Vetter 
42294356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
423091d14251STvrtko Ursulin 				i9xx_pipe_crc_irq_handler(dev_priv, pipe);
42312d9d2b0bSVille Syrjälä 
42321f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
42331f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
42341f7247c0SDaniel Vetter 								    pipe);
4235a266c7d5SChris Wilson 		}
4236a266c7d5SChris Wilson 
4237a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
423891d14251STvrtko Ursulin 			intel_opregion_asle_intr(dev_priv);
4239a266c7d5SChris Wilson 
4240a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4241a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4242a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4243a266c7d5SChris Wilson 		 * we would never get another interrupt.
4244a266c7d5SChris Wilson 		 *
4245a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4246a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4247a266c7d5SChris Wilson 		 * another one.
4248a266c7d5SChris Wilson 		 *
4249a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4250a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4251a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4252a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4253a266c7d5SChris Wilson 		 * stray interrupts.
4254a266c7d5SChris Wilson 		 */
425538bde180SChris Wilson 		ret = IRQ_HANDLED;
4256a266c7d5SChris Wilson 		iir = new_iir;
425738bde180SChris Wilson 	} while (iir & ~flip_mask);
4258a266c7d5SChris Wilson 
42591f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
42601f814dacSImre Deak 
4261a266c7d5SChris Wilson 	return ret;
4262a266c7d5SChris Wilson }
4263a266c7d5SChris Wilson 
4264a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
4265a266c7d5SChris Wilson {
42662d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4267a266c7d5SChris Wilson 	int pipe;
4268a266c7d5SChris Wilson 
4269a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
42700706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4271a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4272a266c7d5SChris Wilson 	}
4273a266c7d5SChris Wilson 
427400d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
4275055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
427655b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
4277a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
427855b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
427955b39755SChris Wilson 	}
4280a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4281a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4282a266c7d5SChris Wilson 
4283a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4284a266c7d5SChris Wilson }
4285a266c7d5SChris Wilson 
4286a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
4287a266c7d5SChris Wilson {
42882d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4289a266c7d5SChris Wilson 	int pipe;
4290a266c7d5SChris Wilson 
42910706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4292a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4293a266c7d5SChris Wilson 
4294a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
4295055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4296a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4297a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4298a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4299a266c7d5SChris Wilson 	POSTING_READ(IER);
4300a266c7d5SChris Wilson }
4301a266c7d5SChris Wilson 
4302a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
4303a266c7d5SChris Wilson {
43042d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4305bbba0a97SChris Wilson 	u32 enable_mask;
4306a266c7d5SChris Wilson 	u32 error_mask;
4307a266c7d5SChris Wilson 
4308a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
4309bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
4310adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
4311bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4312bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
4313bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
4314bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
4315bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
4316bbba0a97SChris Wilson 
4317bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
431821ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
431921ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
4320bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
4321bbba0a97SChris Wilson 
432291d14251STvrtko Ursulin 	if (IS_G4X(dev_priv))
4323bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
4324a266c7d5SChris Wilson 
4325b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4326b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4327d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4328755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
4329755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4330755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4331d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4332a266c7d5SChris Wilson 
4333a266c7d5SChris Wilson 	/*
4334a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
4335a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
4336a266c7d5SChris Wilson 	 */
433791d14251STvrtko Ursulin 	if (IS_G4X(dev_priv)) {
4338a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
4339a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
4340a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
4341a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4342a266c7d5SChris Wilson 	} else {
4343a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
4344a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4345a266c7d5SChris Wilson 	}
4346a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
4347a266c7d5SChris Wilson 
4348a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
4349a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
4350a266c7d5SChris Wilson 	POSTING_READ(IER);
4351a266c7d5SChris Wilson 
43520706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
435320afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
435420afbda2SDaniel Vetter 
435591d14251STvrtko Ursulin 	i915_enable_asle_pipestat(dev_priv);
435620afbda2SDaniel Vetter 
435720afbda2SDaniel Vetter 	return 0;
435820afbda2SDaniel Vetter }
435920afbda2SDaniel Vetter 
436091d14251STvrtko Ursulin static void i915_hpd_irq_setup(struct drm_i915_private *dev_priv)
436120afbda2SDaniel Vetter {
436220afbda2SDaniel Vetter 	u32 hotplug_en;
436320afbda2SDaniel Vetter 
4364b5ea2d56SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
4365b5ea2d56SDaniel Vetter 
4366adca4730SChris Wilson 	/* Note HDMI and DP share hotplug bits */
4367e5868a31SEgbert Eich 	/* enable bits are the same for all generations */
436891d14251STvrtko Ursulin 	hotplug_en = intel_hpd_enabled_irqs(dev_priv, hpd_mask_i915);
4369a266c7d5SChris Wilson 	/* Programming the CRT detection parameters tends
4370a266c7d5SChris Wilson 	   to generate a spurious hotplug event about three
4371a266c7d5SChris Wilson 	   seconds later.  So just do it once.
4372a266c7d5SChris Wilson 	*/
437391d14251STvrtko Ursulin 	if (IS_G4X(dev_priv))
4374a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
4375a266c7d5SChris Wilson 	hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
4376a266c7d5SChris Wilson 
4377a266c7d5SChris Wilson 	/* Ignore TV since it's buggy */
43780706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv,
4379f9e3dc78SJani Nikula 					     HOTPLUG_INT_EN_MASK |
4380f9e3dc78SJani Nikula 					     CRT_HOTPLUG_VOLTAGE_COMPARE_MASK |
4381f9e3dc78SJani Nikula 					     CRT_HOTPLUG_ACTIVATION_PERIOD_64,
43820706f17cSEgbert Eich 					     hotplug_en);
4383a266c7d5SChris Wilson }
4384a266c7d5SChris Wilson 
4385ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
4386a266c7d5SChris Wilson {
438745a83f84SDaniel Vetter 	struct drm_device *dev = arg;
43882d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4389a266c7d5SChris Wilson 	u32 iir, new_iir;
4390a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
4391a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
439221ad8330SVille Syrjälä 	u32 flip_mask =
439321ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
439421ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
4395a266c7d5SChris Wilson 
43962dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
43972dd2a883SImre Deak 		return IRQ_NONE;
43982dd2a883SImre Deak 
43991f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
44001f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
44011f814dacSImre Deak 
4402a266c7d5SChris Wilson 	iir = I915_READ(IIR);
4403a266c7d5SChris Wilson 
4404a266c7d5SChris Wilson 	for (;;) {
4405501e01d7SVille Syrjälä 		bool irq_received = (iir & ~flip_mask) != 0;
44062c8ba29fSChris Wilson 		bool blc_event = false;
44072c8ba29fSChris Wilson 
4408a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4409a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4410a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4411a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
4412a266c7d5SChris Wilson 		 */
4413222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
4414a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4415aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4416a266c7d5SChris Wilson 
4417055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4418f0f59a00SVille Syrjälä 			i915_reg_t reg = PIPESTAT(pipe);
4419a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4420a266c7d5SChris Wilson 
4421a266c7d5SChris Wilson 			/*
4422a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
4423a266c7d5SChris Wilson 			 */
4424a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4425a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
4426501e01d7SVille Syrjälä 				irq_received = true;
4427a266c7d5SChris Wilson 			}
4428a266c7d5SChris Wilson 		}
4429222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4430a266c7d5SChris Wilson 
4431a266c7d5SChris Wilson 		if (!irq_received)
4432a266c7d5SChris Wilson 			break;
4433a266c7d5SChris Wilson 
4434a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
4435a266c7d5SChris Wilson 
4436a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
44371ae3c34cSVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
44381ae3c34cSVille Syrjälä 			u32 hotplug_status = i9xx_hpd_irq_ack(dev_priv);
44391ae3c34cSVille Syrjälä 			if (hotplug_status)
444091d14251STvrtko Ursulin 				i9xx_hpd_irq_handler(dev_priv, hotplug_status);
44411ae3c34cSVille Syrjälä 		}
4442a266c7d5SChris Wilson 
444321ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
4444a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4445a266c7d5SChris Wilson 
4446a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
44474a570db5STvrtko Ursulin 			notify_ring(&dev_priv->engine[RCS]);
4448a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
44494a570db5STvrtko Ursulin 			notify_ring(&dev_priv->engine[VCS]);
4450a266c7d5SChris Wilson 
4451055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
44525a21b665SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
44535a21b665SDaniel Vetter 			    i915_handle_vblank(dev_priv, pipe, pipe, iir))
44545a21b665SDaniel Vetter 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
4455a266c7d5SChris Wilson 
4456a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4457a266c7d5SChris Wilson 				blc_event = true;
44584356d586SDaniel Vetter 
44594356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
446091d14251STvrtko Ursulin 				i9xx_pipe_crc_irq_handler(dev_priv, pipe);
4461a266c7d5SChris Wilson 
44621f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
44631f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
44642d9d2b0bSVille Syrjälä 		}
4465a266c7d5SChris Wilson 
4466a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
446791d14251STvrtko Ursulin 			intel_opregion_asle_intr(dev_priv);
4468a266c7d5SChris Wilson 
4469515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
447091d14251STvrtko Ursulin 			gmbus_irq_handler(dev_priv);
4471515ac2bbSDaniel Vetter 
4472a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4473a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4474a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4475a266c7d5SChris Wilson 		 * we would never get another interrupt.
4476a266c7d5SChris Wilson 		 *
4477a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4478a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4479a266c7d5SChris Wilson 		 * another one.
4480a266c7d5SChris Wilson 		 *
4481a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4482a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4483a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4484a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4485a266c7d5SChris Wilson 		 * stray interrupts.
4486a266c7d5SChris Wilson 		 */
4487a266c7d5SChris Wilson 		iir = new_iir;
4488a266c7d5SChris Wilson 	}
4489a266c7d5SChris Wilson 
44901f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
44911f814dacSImre Deak 
4492a266c7d5SChris Wilson 	return ret;
4493a266c7d5SChris Wilson }
4494a266c7d5SChris Wilson 
4495a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
4496a266c7d5SChris Wilson {
44972d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4498a266c7d5SChris Wilson 	int pipe;
4499a266c7d5SChris Wilson 
4500a266c7d5SChris Wilson 	if (!dev_priv)
4501a266c7d5SChris Wilson 		return;
4502a266c7d5SChris Wilson 
45030706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4504a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4505a266c7d5SChris Wilson 
4506a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
4507055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4508a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4509a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4510a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4511a266c7d5SChris Wilson 
4512055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4513a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
4514a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
4515a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4516a266c7d5SChris Wilson }
4517a266c7d5SChris Wilson 
4518fca52a55SDaniel Vetter /**
4519fca52a55SDaniel Vetter  * intel_irq_init - initializes irq support
4520fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4521fca52a55SDaniel Vetter  *
4522fca52a55SDaniel Vetter  * This function initializes all the irq support including work items, timers
4523fca52a55SDaniel Vetter  * and all the vtables. It does not setup the interrupt itself though.
4524fca52a55SDaniel Vetter  */
4525b963291cSDaniel Vetter void intel_irq_init(struct drm_i915_private *dev_priv)
4526f71d4af4SJesse Barnes {
4527b963291cSDaniel Vetter 	struct drm_device *dev = dev_priv->dev;
45288b2e326dSChris Wilson 
452977913b39SJani Nikula 	intel_hpd_init_work(dev_priv);
453077913b39SJani Nikula 
4531c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
4532a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
45338b2e326dSChris Wilson 
4534a6706b45SDeepak S 	/* Let's track the enabled rps events */
4535666a4537SWayne Boyer 	if (IS_VALLEYVIEW(dev_priv))
45366c65a587SVille Syrjälä 		/* WaGsvRC0ResidencyMethod:vlv */
45376f4b12f8SChris Wilson 		dev_priv->pm_rps_events = GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED;
453831685c25SDeepak S 	else
4539a6706b45SDeepak S 		dev_priv->pm_rps_events = GEN6_PM_RPS_EVENTS;
4540a6706b45SDeepak S 
45411800ad25SSagar Arun Kamble 	dev_priv->rps.pm_intr_keep = 0;
45421800ad25SSagar Arun Kamble 
45431800ad25SSagar Arun Kamble 	/*
45441800ad25SSagar Arun Kamble 	 * SNB,IVB can while VLV,CHV may hard hang on looping batchbuffer
45451800ad25SSagar Arun Kamble 	 * if GEN6_PM_UP_EI_EXPIRED is masked.
45461800ad25SSagar Arun Kamble 	 *
45471800ad25SSagar Arun Kamble 	 * TODO: verify if this can be reproduced on VLV,CHV.
45481800ad25SSagar Arun Kamble 	 */
45491800ad25SSagar Arun Kamble 	if (INTEL_INFO(dev_priv)->gen <= 7 && !IS_HASWELL(dev_priv))
45501800ad25SSagar Arun Kamble 		dev_priv->rps.pm_intr_keep |= GEN6_PM_RP_UP_EI_EXPIRED;
45511800ad25SSagar Arun Kamble 
45521800ad25SSagar Arun Kamble 	if (INTEL_INFO(dev_priv)->gen >= 8)
45531800ad25SSagar Arun Kamble 		dev_priv->rps.pm_intr_keep |= GEN8_PMINTR_REDIRECT_TO_NON_DISP;
45541800ad25SSagar Arun Kamble 
4555737b1506SChris Wilson 	INIT_DELAYED_WORK(&dev_priv->gpu_error.hangcheck_work,
4556737b1506SChris Wilson 			  i915_hangcheck_elapsed);
455761bac78eSDaniel Vetter 
4558b963291cSDaniel Vetter 	if (IS_GEN2(dev_priv)) {
45594cdb83ecSVille Syrjälä 		dev->max_vblank_count = 0;
45604cdb83ecSVille Syrjälä 		dev->driver->get_vblank_counter = i8xx_get_vblank_counter;
4561b963291cSDaniel Vetter 	} else if (IS_G4X(dev_priv) || INTEL_INFO(dev_priv)->gen >= 5) {
4562f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
4563fd8f507cSVille Syrjälä 		dev->driver->get_vblank_counter = g4x_get_vblank_counter;
4564391f75e2SVille Syrjälä 	} else {
4565391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
4566391f75e2SVille Syrjälä 		dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
4567f71d4af4SJesse Barnes 	}
4568f71d4af4SJesse Barnes 
456921da2700SVille Syrjälä 	/*
457021da2700SVille Syrjälä 	 * Opt out of the vblank disable timer on everything except gen2.
457121da2700SVille Syrjälä 	 * Gen2 doesn't have a hardware frame counter and so depends on
457221da2700SVille Syrjälä 	 * vblank interrupts to produce sane vblank seuquence numbers.
457321da2700SVille Syrjälä 	 */
4574b963291cSDaniel Vetter 	if (!IS_GEN2(dev_priv))
457521da2700SVille Syrjälä 		dev->vblank_disable_immediate = true;
457621da2700SVille Syrjälä 
4577f71d4af4SJesse Barnes 	dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
4578f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
4579f71d4af4SJesse Barnes 
4580b963291cSDaniel Vetter 	if (IS_CHERRYVIEW(dev_priv)) {
458143f328d7SVille Syrjälä 		dev->driver->irq_handler = cherryview_irq_handler;
458243f328d7SVille Syrjälä 		dev->driver->irq_preinstall = cherryview_irq_preinstall;
458343f328d7SVille Syrjälä 		dev->driver->irq_postinstall = cherryview_irq_postinstall;
458443f328d7SVille Syrjälä 		dev->driver->irq_uninstall = cherryview_irq_uninstall;
458543f328d7SVille Syrjälä 		dev->driver->enable_vblank = valleyview_enable_vblank;
458643f328d7SVille Syrjälä 		dev->driver->disable_vblank = valleyview_disable_vblank;
458743f328d7SVille Syrjälä 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4588b963291cSDaniel Vetter 	} else if (IS_VALLEYVIEW(dev_priv)) {
45897e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
45907e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
45917e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
45927e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
45937e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
45947e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
4595fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4596b963291cSDaniel Vetter 	} else if (INTEL_INFO(dev_priv)->gen >= 8) {
4597abd58f01SBen Widawsky 		dev->driver->irq_handler = gen8_irq_handler;
4598723761b8SDaniel Vetter 		dev->driver->irq_preinstall = gen8_irq_reset;
4599abd58f01SBen Widawsky 		dev->driver->irq_postinstall = gen8_irq_postinstall;
4600abd58f01SBen Widawsky 		dev->driver->irq_uninstall = gen8_irq_uninstall;
4601abd58f01SBen Widawsky 		dev->driver->enable_vblank = gen8_enable_vblank;
4602abd58f01SBen Widawsky 		dev->driver->disable_vblank = gen8_disable_vblank;
46036dbf30ceSVille Syrjälä 		if (IS_BROXTON(dev))
4604e0a20ad7SShashank Sharma 			dev_priv->display.hpd_irq_setup = bxt_hpd_irq_setup;
46056dbf30ceSVille Syrjälä 		else if (HAS_PCH_SPT(dev))
46066dbf30ceSVille Syrjälä 			dev_priv->display.hpd_irq_setup = spt_hpd_irq_setup;
46076dbf30ceSVille Syrjälä 		else
46083a3b3c7dSVille Syrjälä 			dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
4609f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
4610f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
4611723761b8SDaniel Vetter 		dev->driver->irq_preinstall = ironlake_irq_reset;
4612f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
4613f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
4614f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
4615f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
4616e4ce95aaSVille Syrjälä 		dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
4617f71d4af4SJesse Barnes 	} else {
46187e22dbbbSTvrtko Ursulin 		if (IS_GEN2(dev_priv)) {
4619c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
4620c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
4621c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
4622c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
46237e22dbbbSTvrtko Ursulin 		} else if (IS_GEN3(dev_priv)) {
4624a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
4625a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
4626a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
4627a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
4628c2798b19SChris Wilson 		} else {
4629a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
4630a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
4631a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
4632a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
4633c2798b19SChris Wilson 		}
4634778eb334SVille Syrjälä 		if (I915_HAS_HOTPLUG(dev_priv))
4635778eb334SVille Syrjälä 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4636f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
4637f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
4638f71d4af4SJesse Barnes 	}
4639f71d4af4SJesse Barnes }
464020afbda2SDaniel Vetter 
4641fca52a55SDaniel Vetter /**
4642fca52a55SDaniel Vetter  * intel_irq_install - enables the hardware interrupt
4643fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4644fca52a55SDaniel Vetter  *
4645fca52a55SDaniel Vetter  * This function enables the hardware interrupt handling, but leaves the hotplug
4646fca52a55SDaniel Vetter  * handling still disabled. It is called after intel_irq_init().
4647fca52a55SDaniel Vetter  *
4648fca52a55SDaniel Vetter  * In the driver load and resume code we need working interrupts in a few places
4649fca52a55SDaniel Vetter  * but don't want to deal with the hassle of concurrent probe and hotplug
4650fca52a55SDaniel Vetter  * workers. Hence the split into this two-stage approach.
4651fca52a55SDaniel Vetter  */
46522aeb7d3aSDaniel Vetter int intel_irq_install(struct drm_i915_private *dev_priv)
46532aeb7d3aSDaniel Vetter {
46542aeb7d3aSDaniel Vetter 	/*
46552aeb7d3aSDaniel Vetter 	 * We enable some interrupt sources in our postinstall hooks, so mark
46562aeb7d3aSDaniel Vetter 	 * interrupts as enabled _before_ actually enabling them to avoid
46572aeb7d3aSDaniel Vetter 	 * special cases in our ordering checks.
46582aeb7d3aSDaniel Vetter 	 */
46592aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
46602aeb7d3aSDaniel Vetter 
46612aeb7d3aSDaniel Vetter 	return drm_irq_install(dev_priv->dev, dev_priv->dev->pdev->irq);
46622aeb7d3aSDaniel Vetter }
46632aeb7d3aSDaniel Vetter 
4664fca52a55SDaniel Vetter /**
4665fca52a55SDaniel Vetter  * intel_irq_uninstall - finilizes all irq handling
4666fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4667fca52a55SDaniel Vetter  *
4668fca52a55SDaniel Vetter  * This stops interrupt and hotplug handling and unregisters and frees all
4669fca52a55SDaniel Vetter  * resources acquired in the init functions.
4670fca52a55SDaniel Vetter  */
46712aeb7d3aSDaniel Vetter void intel_irq_uninstall(struct drm_i915_private *dev_priv)
46722aeb7d3aSDaniel Vetter {
46732aeb7d3aSDaniel Vetter 	drm_irq_uninstall(dev_priv->dev);
46742aeb7d3aSDaniel Vetter 	intel_hpd_cancel_work(dev_priv);
46752aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
46762aeb7d3aSDaniel Vetter }
46772aeb7d3aSDaniel Vetter 
4678fca52a55SDaniel Vetter /**
4679fca52a55SDaniel Vetter  * intel_runtime_pm_disable_interrupts - runtime interrupt disabling
4680fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4681fca52a55SDaniel Vetter  *
4682fca52a55SDaniel Vetter  * This function is used to disable interrupts at runtime, both in the runtime
4683fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4684fca52a55SDaniel Vetter  */
4685b963291cSDaniel Vetter void intel_runtime_pm_disable_interrupts(struct drm_i915_private *dev_priv)
4686c67a470bSPaulo Zanoni {
4687b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_uninstall(dev_priv->dev);
46882aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
46892dd2a883SImre Deak 	synchronize_irq(dev_priv->dev->irq);
4690c67a470bSPaulo Zanoni }
4691c67a470bSPaulo Zanoni 
4692fca52a55SDaniel Vetter /**
4693fca52a55SDaniel Vetter  * intel_runtime_pm_enable_interrupts - runtime interrupt enabling
4694fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4695fca52a55SDaniel Vetter  *
4696fca52a55SDaniel Vetter  * This function is used to enable interrupts at runtime, both in the runtime
4697fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4698fca52a55SDaniel Vetter  */
4699b963291cSDaniel Vetter void intel_runtime_pm_enable_interrupts(struct drm_i915_private *dev_priv)
4700c67a470bSPaulo Zanoni {
47012aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
4702b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_preinstall(dev_priv->dev);
4703b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_postinstall(dev_priv->dev);
4704c67a470bSPaulo Zanoni }
4705