xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 98187836fc75b7a003db101e19cfa8685da78905)
1c0e09200SDave Airlie /* i915_irq.c -- IRQ support for the I915 -*- linux-c -*-
2c0e09200SDave Airlie  */
3c0e09200SDave Airlie /*
4c0e09200SDave Airlie  * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
5c0e09200SDave Airlie  * All Rights Reserved.
6c0e09200SDave Airlie  *
7c0e09200SDave Airlie  * Permission is hereby granted, free of charge, to any person obtaining a
8c0e09200SDave Airlie  * copy of this software and associated documentation files (the
9c0e09200SDave Airlie  * "Software"), to deal in the Software without restriction, including
10c0e09200SDave Airlie  * without limitation the rights to use, copy, modify, merge, publish,
11c0e09200SDave Airlie  * distribute, sub license, and/or sell copies of the Software, and to
12c0e09200SDave Airlie  * permit persons to whom the Software is furnished to do so, subject to
13c0e09200SDave Airlie  * the following conditions:
14c0e09200SDave Airlie  *
15c0e09200SDave Airlie  * The above copyright notice and this permission notice (including the
16c0e09200SDave Airlie  * next paragraph) shall be included in all copies or substantial portions
17c0e09200SDave Airlie  * of the Software.
18c0e09200SDave Airlie  *
19c0e09200SDave Airlie  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20c0e09200SDave Airlie  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21c0e09200SDave Airlie  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22c0e09200SDave Airlie  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23c0e09200SDave Airlie  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24c0e09200SDave Airlie  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25c0e09200SDave Airlie  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26c0e09200SDave Airlie  *
27c0e09200SDave Airlie  */
28c0e09200SDave Airlie 
29a70491ccSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30a70491ccSJoe Perches 
3163eeaf38SJesse Barnes #include <linux/sysrq.h>
325a0e3ad6STejun Heo #include <linux/slab.h>
33b2c88f5bSDamien Lespiau #include <linux/circ_buf.h>
34760285e7SDavid Howells #include <drm/drmP.h>
35760285e7SDavid Howells #include <drm/i915_drm.h>
36c0e09200SDave Airlie #include "i915_drv.h"
371c5d22f7SChris Wilson #include "i915_trace.h"
3879e53945SJesse Barnes #include "intel_drv.h"
39c0e09200SDave Airlie 
40fca52a55SDaniel Vetter /**
41fca52a55SDaniel Vetter  * DOC: interrupt handling
42fca52a55SDaniel Vetter  *
43fca52a55SDaniel Vetter  * These functions provide the basic support for enabling and disabling the
44fca52a55SDaniel Vetter  * interrupt handling support. There's a lot more functionality in i915_irq.c
45fca52a55SDaniel Vetter  * and related files, but that will be described in separate chapters.
46fca52a55SDaniel Vetter  */
47fca52a55SDaniel Vetter 
48e4ce95aaSVille Syrjälä static const u32 hpd_ilk[HPD_NUM_PINS] = {
49e4ce95aaSVille Syrjälä 	[HPD_PORT_A] = DE_DP_A_HOTPLUG,
50e4ce95aaSVille Syrjälä };
51e4ce95aaSVille Syrjälä 
5223bb4cb5SVille Syrjälä static const u32 hpd_ivb[HPD_NUM_PINS] = {
5323bb4cb5SVille Syrjälä 	[HPD_PORT_A] = DE_DP_A_HOTPLUG_IVB,
5423bb4cb5SVille Syrjälä };
5523bb4cb5SVille Syrjälä 
563a3b3c7dSVille Syrjälä static const u32 hpd_bdw[HPD_NUM_PINS] = {
573a3b3c7dSVille Syrjälä 	[HPD_PORT_A] = GEN8_PORT_DP_A_HOTPLUG,
583a3b3c7dSVille Syrjälä };
593a3b3c7dSVille Syrjälä 
607c7e10dbSVille Syrjälä static const u32 hpd_ibx[HPD_NUM_PINS] = {
61e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG,
62e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG,
63e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG,
64e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG,
65e5868a31SEgbert Eich 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG
66e5868a31SEgbert Eich };
67e5868a31SEgbert Eich 
687c7e10dbSVille Syrjälä static const u32 hpd_cpt[HPD_NUM_PINS] = {
69e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG_CPT,
7073c352a2SDaniel Vetter 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG_CPT,
71e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
72e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
73e5868a31SEgbert Eich 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT
74e5868a31SEgbert Eich };
75e5868a31SEgbert Eich 
7626951cafSXiong Zhang static const u32 hpd_spt[HPD_NUM_PINS] = {
7774c0b395SVille Syrjälä 	[HPD_PORT_A] = SDE_PORTA_HOTPLUG_SPT,
7826951cafSXiong Zhang 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
7926951cafSXiong Zhang 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
8026951cafSXiong Zhang 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT,
8126951cafSXiong Zhang 	[HPD_PORT_E] = SDE_PORTE_HOTPLUG_SPT
8226951cafSXiong Zhang };
8326951cafSXiong Zhang 
847c7e10dbSVille Syrjälä static const u32 hpd_mask_i915[HPD_NUM_PINS] = {
85e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_EN,
86e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_EN,
87e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_EN,
88e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_EN,
89e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_EN,
90e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_EN
91e5868a31SEgbert Eich };
92e5868a31SEgbert Eich 
937c7e10dbSVille Syrjälä static const u32 hpd_status_g4x[HPD_NUM_PINS] = {
94e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
95e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_G4X,
96e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_G4X,
97e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
98e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
99e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
100e5868a31SEgbert Eich };
101e5868a31SEgbert Eich 
1024bca26d0SVille Syrjälä static const u32 hpd_status_i915[HPD_NUM_PINS] = {
103e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
104e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_I915,
105e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_I915,
106e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
107e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
108e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
109e5868a31SEgbert Eich };
110e5868a31SEgbert Eich 
111e0a20ad7SShashank Sharma /* BXT hpd list */
112e0a20ad7SShashank Sharma static const u32 hpd_bxt[HPD_NUM_PINS] = {
1137f3561beSSonika Jindal 	[HPD_PORT_A] = BXT_DE_PORT_HP_DDIA,
114e0a20ad7SShashank Sharma 	[HPD_PORT_B] = BXT_DE_PORT_HP_DDIB,
115e0a20ad7SShashank Sharma 	[HPD_PORT_C] = BXT_DE_PORT_HP_DDIC
116e0a20ad7SShashank Sharma };
117e0a20ad7SShashank Sharma 
1185c502442SPaulo Zanoni /* IIR can theoretically queue up two events. Be paranoid. */
119f86f3fb0SPaulo Zanoni #define GEN8_IRQ_RESET_NDX(type, which) do { \
1205c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IMR(which), 0xffffffff); \
1215c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IMR(which)); \
1225c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IER(which), 0); \
1235c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
1245c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IIR(which)); \
1255c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
1265c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IIR(which)); \
1275c502442SPaulo Zanoni } while (0)
1285c502442SPaulo Zanoni 
129f86f3fb0SPaulo Zanoni #define GEN5_IRQ_RESET(type) do { \
130a9d356a6SPaulo Zanoni 	I915_WRITE(type##IMR, 0xffffffff); \
1315c502442SPaulo Zanoni 	POSTING_READ(type##IMR); \
132a9d356a6SPaulo Zanoni 	I915_WRITE(type##IER, 0); \
1335c502442SPaulo Zanoni 	I915_WRITE(type##IIR, 0xffffffff); \
1345c502442SPaulo Zanoni 	POSTING_READ(type##IIR); \
1355c502442SPaulo Zanoni 	I915_WRITE(type##IIR, 0xffffffff); \
1365c502442SPaulo Zanoni 	POSTING_READ(type##IIR); \
137a9d356a6SPaulo Zanoni } while (0)
138a9d356a6SPaulo Zanoni 
139337ba017SPaulo Zanoni /*
140337ba017SPaulo Zanoni  * We should clear IMR at preinstall/uninstall, and just check at postinstall.
141337ba017SPaulo Zanoni  */
142f0f59a00SVille Syrjälä static void gen5_assert_iir_is_zero(struct drm_i915_private *dev_priv,
143f0f59a00SVille Syrjälä 				    i915_reg_t reg)
144b51a2842SVille Syrjälä {
145b51a2842SVille Syrjälä 	u32 val = I915_READ(reg);
146b51a2842SVille Syrjälä 
147b51a2842SVille Syrjälä 	if (val == 0)
148b51a2842SVille Syrjälä 		return;
149b51a2842SVille Syrjälä 
150b51a2842SVille Syrjälä 	WARN(1, "Interrupt register 0x%x is not zero: 0x%08x\n",
151f0f59a00SVille Syrjälä 	     i915_mmio_reg_offset(reg), val);
152b51a2842SVille Syrjälä 	I915_WRITE(reg, 0xffffffff);
153b51a2842SVille Syrjälä 	POSTING_READ(reg);
154b51a2842SVille Syrjälä 	I915_WRITE(reg, 0xffffffff);
155b51a2842SVille Syrjälä 	POSTING_READ(reg);
156b51a2842SVille Syrjälä }
157337ba017SPaulo Zanoni 
15835079899SPaulo Zanoni #define GEN8_IRQ_INIT_NDX(type, which, imr_val, ier_val) do { \
159b51a2842SVille Syrjälä 	gen5_assert_iir_is_zero(dev_priv, GEN8_##type##_IIR(which)); \
16035079899SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IER(which), (ier_val)); \
1617d1bd539SVille Syrjälä 	I915_WRITE(GEN8_##type##_IMR(which), (imr_val)); \
1627d1bd539SVille Syrjälä 	POSTING_READ(GEN8_##type##_IMR(which)); \
16335079899SPaulo Zanoni } while (0)
16435079899SPaulo Zanoni 
16535079899SPaulo Zanoni #define GEN5_IRQ_INIT(type, imr_val, ier_val) do { \
166b51a2842SVille Syrjälä 	gen5_assert_iir_is_zero(dev_priv, type##IIR); \
16735079899SPaulo Zanoni 	I915_WRITE(type##IER, (ier_val)); \
1687d1bd539SVille Syrjälä 	I915_WRITE(type##IMR, (imr_val)); \
1697d1bd539SVille Syrjälä 	POSTING_READ(type##IMR); \
17035079899SPaulo Zanoni } while (0)
17135079899SPaulo Zanoni 
172c9a9a268SImre Deak static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir);
17326705e20SSagar Arun Kamble static void gen9_guc_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir);
174c9a9a268SImre Deak 
1750706f17cSEgbert Eich /* For display hotplug interrupt */
1760706f17cSEgbert Eich static inline void
1770706f17cSEgbert Eich i915_hotplug_interrupt_update_locked(struct drm_i915_private *dev_priv,
1780706f17cSEgbert Eich 				     uint32_t mask,
1790706f17cSEgbert Eich 				     uint32_t bits)
1800706f17cSEgbert Eich {
1810706f17cSEgbert Eich 	uint32_t val;
1820706f17cSEgbert Eich 
1830706f17cSEgbert Eich 	assert_spin_locked(&dev_priv->irq_lock);
1840706f17cSEgbert Eich 	WARN_ON(bits & ~mask);
1850706f17cSEgbert Eich 
1860706f17cSEgbert Eich 	val = I915_READ(PORT_HOTPLUG_EN);
1870706f17cSEgbert Eich 	val &= ~mask;
1880706f17cSEgbert Eich 	val |= bits;
1890706f17cSEgbert Eich 	I915_WRITE(PORT_HOTPLUG_EN, val);
1900706f17cSEgbert Eich }
1910706f17cSEgbert Eich 
1920706f17cSEgbert Eich /**
1930706f17cSEgbert Eich  * i915_hotplug_interrupt_update - update hotplug interrupt enable
1940706f17cSEgbert Eich  * @dev_priv: driver private
1950706f17cSEgbert Eich  * @mask: bits to update
1960706f17cSEgbert Eich  * @bits: bits to enable
1970706f17cSEgbert Eich  * NOTE: the HPD enable bits are modified both inside and outside
1980706f17cSEgbert Eich  * of an interrupt context. To avoid that read-modify-write cycles
1990706f17cSEgbert Eich  * interfer, these bits are protected by a spinlock. Since this
2000706f17cSEgbert Eich  * function is usually not called from a context where the lock is
2010706f17cSEgbert Eich  * held already, this function acquires the lock itself. A non-locking
2020706f17cSEgbert Eich  * version is also available.
2030706f17cSEgbert Eich  */
2040706f17cSEgbert Eich void i915_hotplug_interrupt_update(struct drm_i915_private *dev_priv,
2050706f17cSEgbert Eich 				   uint32_t mask,
2060706f17cSEgbert Eich 				   uint32_t bits)
2070706f17cSEgbert Eich {
2080706f17cSEgbert Eich 	spin_lock_irq(&dev_priv->irq_lock);
2090706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv, mask, bits);
2100706f17cSEgbert Eich 	spin_unlock_irq(&dev_priv->irq_lock);
2110706f17cSEgbert Eich }
2120706f17cSEgbert Eich 
213d9dc34f1SVille Syrjälä /**
214d9dc34f1SVille Syrjälä  * ilk_update_display_irq - update DEIMR
215d9dc34f1SVille Syrjälä  * @dev_priv: driver private
216d9dc34f1SVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
217d9dc34f1SVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
218d9dc34f1SVille Syrjälä  */
219fbdedaeaSVille Syrjälä void ilk_update_display_irq(struct drm_i915_private *dev_priv,
220d9dc34f1SVille Syrjälä 			    uint32_t interrupt_mask,
221d9dc34f1SVille Syrjälä 			    uint32_t enabled_irq_mask)
222036a4a7dSZhenyu Wang {
223d9dc34f1SVille Syrjälä 	uint32_t new_val;
224d9dc34f1SVille Syrjälä 
2254bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
2264bc9d430SDaniel Vetter 
227d9dc34f1SVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
228d9dc34f1SVille Syrjälä 
2299df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
230c67a470bSPaulo Zanoni 		return;
231c67a470bSPaulo Zanoni 
232d9dc34f1SVille Syrjälä 	new_val = dev_priv->irq_mask;
233d9dc34f1SVille Syrjälä 	new_val &= ~interrupt_mask;
234d9dc34f1SVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
235d9dc34f1SVille Syrjälä 
236d9dc34f1SVille Syrjälä 	if (new_val != dev_priv->irq_mask) {
237d9dc34f1SVille Syrjälä 		dev_priv->irq_mask = new_val;
2381ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
2393143a2bfSChris Wilson 		POSTING_READ(DEIMR);
240036a4a7dSZhenyu Wang 	}
241036a4a7dSZhenyu Wang }
242036a4a7dSZhenyu Wang 
24343eaea13SPaulo Zanoni /**
24443eaea13SPaulo Zanoni  * ilk_update_gt_irq - update GTIMR
24543eaea13SPaulo Zanoni  * @dev_priv: driver private
24643eaea13SPaulo Zanoni  * @interrupt_mask: mask of interrupt bits to update
24743eaea13SPaulo Zanoni  * @enabled_irq_mask: mask of interrupt bits to enable
24843eaea13SPaulo Zanoni  */
24943eaea13SPaulo Zanoni static void ilk_update_gt_irq(struct drm_i915_private *dev_priv,
25043eaea13SPaulo Zanoni 			      uint32_t interrupt_mask,
25143eaea13SPaulo Zanoni 			      uint32_t enabled_irq_mask)
25243eaea13SPaulo Zanoni {
25343eaea13SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
25443eaea13SPaulo Zanoni 
25515a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
25615a17aaeSDaniel Vetter 
2579df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
258c67a470bSPaulo Zanoni 		return;
259c67a470bSPaulo Zanoni 
26043eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask &= ~interrupt_mask;
26143eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask |= (~enabled_irq_mask & interrupt_mask);
26243eaea13SPaulo Zanoni 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
26343eaea13SPaulo Zanoni }
26443eaea13SPaulo Zanoni 
265480c8033SDaniel Vetter void gen5_enable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
26643eaea13SPaulo Zanoni {
26743eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, mask);
26831bb59ccSChris Wilson 	POSTING_READ_FW(GTIMR);
26943eaea13SPaulo Zanoni }
27043eaea13SPaulo Zanoni 
271480c8033SDaniel Vetter void gen5_disable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
27243eaea13SPaulo Zanoni {
27343eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, 0);
27443eaea13SPaulo Zanoni }
27543eaea13SPaulo Zanoni 
276f0f59a00SVille Syrjälä static i915_reg_t gen6_pm_iir(struct drm_i915_private *dev_priv)
277b900b949SImre Deak {
278b900b949SImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IIR(2) : GEN6_PMIIR;
279b900b949SImre Deak }
280b900b949SImre Deak 
281f0f59a00SVille Syrjälä static i915_reg_t gen6_pm_imr(struct drm_i915_private *dev_priv)
282a72fbc3aSImre Deak {
283a72fbc3aSImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IMR(2) : GEN6_PMIMR;
284a72fbc3aSImre Deak }
285a72fbc3aSImre Deak 
286f0f59a00SVille Syrjälä static i915_reg_t gen6_pm_ier(struct drm_i915_private *dev_priv)
287b900b949SImre Deak {
288b900b949SImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IER(2) : GEN6_PMIER;
289b900b949SImre Deak }
290b900b949SImre Deak 
291edbfdb45SPaulo Zanoni /**
292edbfdb45SPaulo Zanoni  * snb_update_pm_irq - update GEN6_PMIMR
293edbfdb45SPaulo Zanoni  * @dev_priv: driver private
294edbfdb45SPaulo Zanoni  * @interrupt_mask: mask of interrupt bits to update
295edbfdb45SPaulo Zanoni  * @enabled_irq_mask: mask of interrupt bits to enable
296edbfdb45SPaulo Zanoni  */
297edbfdb45SPaulo Zanoni static void snb_update_pm_irq(struct drm_i915_private *dev_priv,
298edbfdb45SPaulo Zanoni 			      uint32_t interrupt_mask,
299edbfdb45SPaulo Zanoni 			      uint32_t enabled_irq_mask)
300edbfdb45SPaulo Zanoni {
301605cd25bSPaulo Zanoni 	uint32_t new_val;
302edbfdb45SPaulo Zanoni 
30315a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
30415a17aaeSDaniel Vetter 
305edbfdb45SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
306edbfdb45SPaulo Zanoni 
307f4e9af4fSAkash Goel 	new_val = dev_priv->pm_imr;
308f52ecbcfSPaulo Zanoni 	new_val &= ~interrupt_mask;
309f52ecbcfSPaulo Zanoni 	new_val |= (~enabled_irq_mask & interrupt_mask);
310f52ecbcfSPaulo Zanoni 
311f4e9af4fSAkash Goel 	if (new_val != dev_priv->pm_imr) {
312f4e9af4fSAkash Goel 		dev_priv->pm_imr = new_val;
313f4e9af4fSAkash Goel 		I915_WRITE(gen6_pm_imr(dev_priv), dev_priv->pm_imr);
314a72fbc3aSImre Deak 		POSTING_READ(gen6_pm_imr(dev_priv));
315edbfdb45SPaulo Zanoni 	}
316f52ecbcfSPaulo Zanoni }
317edbfdb45SPaulo Zanoni 
318f4e9af4fSAkash Goel void gen6_unmask_pm_irq(struct drm_i915_private *dev_priv, u32 mask)
319edbfdb45SPaulo Zanoni {
3209939fba2SImre Deak 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
3219939fba2SImre Deak 		return;
3229939fba2SImre Deak 
323edbfdb45SPaulo Zanoni 	snb_update_pm_irq(dev_priv, mask, mask);
324edbfdb45SPaulo Zanoni }
325edbfdb45SPaulo Zanoni 
326f4e9af4fSAkash Goel static void __gen6_mask_pm_irq(struct drm_i915_private *dev_priv, u32 mask)
3279939fba2SImre Deak {
3289939fba2SImre Deak 	snb_update_pm_irq(dev_priv, mask, 0);
3299939fba2SImre Deak }
3309939fba2SImre Deak 
331f4e9af4fSAkash Goel void gen6_mask_pm_irq(struct drm_i915_private *dev_priv, u32 mask)
332edbfdb45SPaulo Zanoni {
3339939fba2SImre Deak 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
3349939fba2SImre Deak 		return;
3359939fba2SImre Deak 
336f4e9af4fSAkash Goel 	__gen6_mask_pm_irq(dev_priv, mask);
337f4e9af4fSAkash Goel }
338f4e9af4fSAkash Goel 
339f4e9af4fSAkash Goel void gen6_reset_pm_iir(struct drm_i915_private *dev_priv, u32 reset_mask)
340f4e9af4fSAkash Goel {
341f4e9af4fSAkash Goel 	i915_reg_t reg = gen6_pm_iir(dev_priv);
342f4e9af4fSAkash Goel 
343f4e9af4fSAkash Goel 	assert_spin_locked(&dev_priv->irq_lock);
344f4e9af4fSAkash Goel 
345f4e9af4fSAkash Goel 	I915_WRITE(reg, reset_mask);
346f4e9af4fSAkash Goel 	I915_WRITE(reg, reset_mask);
347f4e9af4fSAkash Goel 	POSTING_READ(reg);
348f4e9af4fSAkash Goel }
349f4e9af4fSAkash Goel 
350f4e9af4fSAkash Goel void gen6_enable_pm_irq(struct drm_i915_private *dev_priv, u32 enable_mask)
351f4e9af4fSAkash Goel {
352f4e9af4fSAkash Goel 	assert_spin_locked(&dev_priv->irq_lock);
353f4e9af4fSAkash Goel 
354f4e9af4fSAkash Goel 	dev_priv->pm_ier |= enable_mask;
355f4e9af4fSAkash Goel 	I915_WRITE(gen6_pm_ier(dev_priv), dev_priv->pm_ier);
356f4e9af4fSAkash Goel 	gen6_unmask_pm_irq(dev_priv, enable_mask);
357f4e9af4fSAkash Goel 	/* unmask_pm_irq provides an implicit barrier (POSTING_READ) */
358f4e9af4fSAkash Goel }
359f4e9af4fSAkash Goel 
360f4e9af4fSAkash Goel void gen6_disable_pm_irq(struct drm_i915_private *dev_priv, u32 disable_mask)
361f4e9af4fSAkash Goel {
362f4e9af4fSAkash Goel 	assert_spin_locked(&dev_priv->irq_lock);
363f4e9af4fSAkash Goel 
364f4e9af4fSAkash Goel 	dev_priv->pm_ier &= ~disable_mask;
365f4e9af4fSAkash Goel 	__gen6_mask_pm_irq(dev_priv, disable_mask);
366f4e9af4fSAkash Goel 	I915_WRITE(gen6_pm_ier(dev_priv), dev_priv->pm_ier);
367f4e9af4fSAkash Goel 	/* though a barrier is missing here, but don't really need a one */
368edbfdb45SPaulo Zanoni }
369edbfdb45SPaulo Zanoni 
370dc97997aSChris Wilson void gen6_reset_rps_interrupts(struct drm_i915_private *dev_priv)
3713cc134e3SImre Deak {
3723cc134e3SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
373f4e9af4fSAkash Goel 	gen6_reset_pm_iir(dev_priv, dev_priv->pm_rps_events);
374096fad9eSImre Deak 	dev_priv->rps.pm_iir = 0;
3753cc134e3SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
3763cc134e3SImre Deak }
3773cc134e3SImre Deak 
37891d14251STvrtko Ursulin void gen6_enable_rps_interrupts(struct drm_i915_private *dev_priv)
379b900b949SImre Deak {
380f2a91d1aSChris Wilson 	if (READ_ONCE(dev_priv->rps.interrupts_enabled))
381f2a91d1aSChris Wilson 		return;
382f2a91d1aSChris Wilson 
383b900b949SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
384c33d247dSChris Wilson 	WARN_ON_ONCE(dev_priv->rps.pm_iir);
385c33d247dSChris Wilson 	WARN_ON_ONCE(I915_READ(gen6_pm_iir(dev_priv)) & dev_priv->pm_rps_events);
386d4d70aa5SImre Deak 	dev_priv->rps.interrupts_enabled = true;
387b900b949SImre Deak 	gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
38878e68d36SImre Deak 
389b900b949SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
390b900b949SImre Deak }
391b900b949SImre Deak 
39259d02a1fSImre Deak u32 gen6_sanitize_rps_pm_mask(struct drm_i915_private *dev_priv, u32 mask)
39359d02a1fSImre Deak {
3941800ad25SSagar Arun Kamble 	return (mask & ~dev_priv->rps.pm_intr_keep);
39559d02a1fSImre Deak }
39659d02a1fSImre Deak 
39791d14251STvrtko Ursulin void gen6_disable_rps_interrupts(struct drm_i915_private *dev_priv)
398b900b949SImre Deak {
399f2a91d1aSChris Wilson 	if (!READ_ONCE(dev_priv->rps.interrupts_enabled))
400f2a91d1aSChris Wilson 		return;
401f2a91d1aSChris Wilson 
402d4d70aa5SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
403d4d70aa5SImre Deak 	dev_priv->rps.interrupts_enabled = false;
4049939fba2SImre Deak 
405b20e3cfeSDave Gordon 	I915_WRITE(GEN6_PMINTRMSK, gen6_sanitize_rps_pm_mask(dev_priv, ~0u));
4069939fba2SImre Deak 
407f4e9af4fSAkash Goel 	gen6_disable_pm_irq(dev_priv, dev_priv->pm_rps_events);
40858072ccbSImre Deak 
40958072ccbSImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
41091c8a326SChris Wilson 	synchronize_irq(dev_priv->drm.irq);
411c33d247dSChris Wilson 
412c33d247dSChris Wilson 	/* Now that we will not be generating any more work, flush any
413c33d247dSChris Wilson 	 * outsanding tasks. As we are called on the RPS idle path,
414c33d247dSChris Wilson 	 * we will reset the GPU to minimum frequencies, so the current
415c33d247dSChris Wilson 	 * state of the worker can be discarded.
416c33d247dSChris Wilson 	 */
417c33d247dSChris Wilson 	cancel_work_sync(&dev_priv->rps.work);
418c33d247dSChris Wilson 	gen6_reset_rps_interrupts(dev_priv);
419b900b949SImre Deak }
420b900b949SImre Deak 
42126705e20SSagar Arun Kamble void gen9_reset_guc_interrupts(struct drm_i915_private *dev_priv)
42226705e20SSagar Arun Kamble {
42326705e20SSagar Arun Kamble 	spin_lock_irq(&dev_priv->irq_lock);
42426705e20SSagar Arun Kamble 	gen6_reset_pm_iir(dev_priv, dev_priv->pm_guc_events);
42526705e20SSagar Arun Kamble 	spin_unlock_irq(&dev_priv->irq_lock);
42626705e20SSagar Arun Kamble }
42726705e20SSagar Arun Kamble 
42826705e20SSagar Arun Kamble void gen9_enable_guc_interrupts(struct drm_i915_private *dev_priv)
42926705e20SSagar Arun Kamble {
43026705e20SSagar Arun Kamble 	spin_lock_irq(&dev_priv->irq_lock);
43126705e20SSagar Arun Kamble 	if (!dev_priv->guc.interrupts_enabled) {
43226705e20SSagar Arun Kamble 		WARN_ON_ONCE(I915_READ(gen6_pm_iir(dev_priv)) &
43326705e20SSagar Arun Kamble 				       dev_priv->pm_guc_events);
43426705e20SSagar Arun Kamble 		dev_priv->guc.interrupts_enabled = true;
43526705e20SSagar Arun Kamble 		gen6_enable_pm_irq(dev_priv, dev_priv->pm_guc_events);
43626705e20SSagar Arun Kamble 	}
43726705e20SSagar Arun Kamble 	spin_unlock_irq(&dev_priv->irq_lock);
43826705e20SSagar Arun Kamble }
43926705e20SSagar Arun Kamble 
44026705e20SSagar Arun Kamble void gen9_disable_guc_interrupts(struct drm_i915_private *dev_priv)
44126705e20SSagar Arun Kamble {
44226705e20SSagar Arun Kamble 	spin_lock_irq(&dev_priv->irq_lock);
44326705e20SSagar Arun Kamble 	dev_priv->guc.interrupts_enabled = false;
44426705e20SSagar Arun Kamble 
44526705e20SSagar Arun Kamble 	gen6_disable_pm_irq(dev_priv, dev_priv->pm_guc_events);
44626705e20SSagar Arun Kamble 
44726705e20SSagar Arun Kamble 	spin_unlock_irq(&dev_priv->irq_lock);
44826705e20SSagar Arun Kamble 	synchronize_irq(dev_priv->drm.irq);
44926705e20SSagar Arun Kamble 
45026705e20SSagar Arun Kamble 	gen9_reset_guc_interrupts(dev_priv);
45126705e20SSagar Arun Kamble }
45226705e20SSagar Arun Kamble 
4530961021aSBen Widawsky /**
4543a3b3c7dSVille Syrjälä  * bdw_update_port_irq - update DE port interrupt
4553a3b3c7dSVille Syrjälä  * @dev_priv: driver private
4563a3b3c7dSVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
4573a3b3c7dSVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
4583a3b3c7dSVille Syrjälä  */
4593a3b3c7dSVille Syrjälä static void bdw_update_port_irq(struct drm_i915_private *dev_priv,
4603a3b3c7dSVille Syrjälä 				uint32_t interrupt_mask,
4613a3b3c7dSVille Syrjälä 				uint32_t enabled_irq_mask)
4623a3b3c7dSVille Syrjälä {
4633a3b3c7dSVille Syrjälä 	uint32_t new_val;
4643a3b3c7dSVille Syrjälä 	uint32_t old_val;
4653a3b3c7dSVille Syrjälä 
4663a3b3c7dSVille Syrjälä 	assert_spin_locked(&dev_priv->irq_lock);
4673a3b3c7dSVille Syrjälä 
4683a3b3c7dSVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
4693a3b3c7dSVille Syrjälä 
4703a3b3c7dSVille Syrjälä 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
4713a3b3c7dSVille Syrjälä 		return;
4723a3b3c7dSVille Syrjälä 
4733a3b3c7dSVille Syrjälä 	old_val = I915_READ(GEN8_DE_PORT_IMR);
4743a3b3c7dSVille Syrjälä 
4753a3b3c7dSVille Syrjälä 	new_val = old_val;
4763a3b3c7dSVille Syrjälä 	new_val &= ~interrupt_mask;
4773a3b3c7dSVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
4783a3b3c7dSVille Syrjälä 
4793a3b3c7dSVille Syrjälä 	if (new_val != old_val) {
4803a3b3c7dSVille Syrjälä 		I915_WRITE(GEN8_DE_PORT_IMR, new_val);
4813a3b3c7dSVille Syrjälä 		POSTING_READ(GEN8_DE_PORT_IMR);
4823a3b3c7dSVille Syrjälä 	}
4833a3b3c7dSVille Syrjälä }
4843a3b3c7dSVille Syrjälä 
4853a3b3c7dSVille Syrjälä /**
486013d3752SVille Syrjälä  * bdw_update_pipe_irq - update DE pipe interrupt
487013d3752SVille Syrjälä  * @dev_priv: driver private
488013d3752SVille Syrjälä  * @pipe: pipe whose interrupt to update
489013d3752SVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
490013d3752SVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
491013d3752SVille Syrjälä  */
492013d3752SVille Syrjälä void bdw_update_pipe_irq(struct drm_i915_private *dev_priv,
493013d3752SVille Syrjälä 			 enum pipe pipe,
494013d3752SVille Syrjälä 			 uint32_t interrupt_mask,
495013d3752SVille Syrjälä 			 uint32_t enabled_irq_mask)
496013d3752SVille Syrjälä {
497013d3752SVille Syrjälä 	uint32_t new_val;
498013d3752SVille Syrjälä 
499013d3752SVille Syrjälä 	assert_spin_locked(&dev_priv->irq_lock);
500013d3752SVille Syrjälä 
501013d3752SVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
502013d3752SVille Syrjälä 
503013d3752SVille Syrjälä 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
504013d3752SVille Syrjälä 		return;
505013d3752SVille Syrjälä 
506013d3752SVille Syrjälä 	new_val = dev_priv->de_irq_mask[pipe];
507013d3752SVille Syrjälä 	new_val &= ~interrupt_mask;
508013d3752SVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
509013d3752SVille Syrjälä 
510013d3752SVille Syrjälä 	if (new_val != dev_priv->de_irq_mask[pipe]) {
511013d3752SVille Syrjälä 		dev_priv->de_irq_mask[pipe] = new_val;
512013d3752SVille Syrjälä 		I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
513013d3752SVille Syrjälä 		POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
514013d3752SVille Syrjälä 	}
515013d3752SVille Syrjälä }
516013d3752SVille Syrjälä 
517013d3752SVille Syrjälä /**
518fee884edSDaniel Vetter  * ibx_display_interrupt_update - update SDEIMR
519fee884edSDaniel Vetter  * @dev_priv: driver private
520fee884edSDaniel Vetter  * @interrupt_mask: mask of interrupt bits to update
521fee884edSDaniel Vetter  * @enabled_irq_mask: mask of interrupt bits to enable
522fee884edSDaniel Vetter  */
52347339cd9SDaniel Vetter void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
524fee884edSDaniel Vetter 				  uint32_t interrupt_mask,
525fee884edSDaniel Vetter 				  uint32_t enabled_irq_mask)
526fee884edSDaniel Vetter {
527fee884edSDaniel Vetter 	uint32_t sdeimr = I915_READ(SDEIMR);
528fee884edSDaniel Vetter 	sdeimr &= ~interrupt_mask;
529fee884edSDaniel Vetter 	sdeimr |= (~enabled_irq_mask & interrupt_mask);
530fee884edSDaniel Vetter 
53115a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
53215a17aaeSDaniel Vetter 
533fee884edSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
534fee884edSDaniel Vetter 
5359df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
536c67a470bSPaulo Zanoni 		return;
537c67a470bSPaulo Zanoni 
538fee884edSDaniel Vetter 	I915_WRITE(SDEIMR, sdeimr);
539fee884edSDaniel Vetter 	POSTING_READ(SDEIMR);
540fee884edSDaniel Vetter }
5418664281bSPaulo Zanoni 
542b5ea642aSDaniel Vetter static void
543755e9019SImre Deak __i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
544755e9019SImre Deak 		       u32 enable_mask, u32 status_mask)
5457c463586SKeith Packard {
546f0f59a00SVille Syrjälä 	i915_reg_t reg = PIPESTAT(pipe);
547755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
5487c463586SKeith Packard 
549b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
550d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
551b79480baSDaniel Vetter 
55204feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
55304feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
55404feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
55504feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
556755e9019SImre Deak 		return;
557755e9019SImre Deak 
558755e9019SImre Deak 	if ((pipestat & enable_mask) == enable_mask)
55946c06a30SVille Syrjälä 		return;
56046c06a30SVille Syrjälä 
56191d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] |= status_mask;
56291d181ddSImre Deak 
5637c463586SKeith Packard 	/* Enable the interrupt, clear any pending status */
564755e9019SImre Deak 	pipestat |= enable_mask | status_mask;
56546c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
5663143a2bfSChris Wilson 	POSTING_READ(reg);
5677c463586SKeith Packard }
5687c463586SKeith Packard 
569b5ea642aSDaniel Vetter static void
570755e9019SImre Deak __i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
571755e9019SImre Deak 		        u32 enable_mask, u32 status_mask)
5727c463586SKeith Packard {
573f0f59a00SVille Syrjälä 	i915_reg_t reg = PIPESTAT(pipe);
574755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
5757c463586SKeith Packard 
576b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
577d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
578b79480baSDaniel Vetter 
57904feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
58004feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
58104feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
58204feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
58346c06a30SVille Syrjälä 		return;
58446c06a30SVille Syrjälä 
585755e9019SImre Deak 	if ((pipestat & enable_mask) == 0)
586755e9019SImre Deak 		return;
587755e9019SImre Deak 
58891d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] &= ~status_mask;
58991d181ddSImre Deak 
590755e9019SImre Deak 	pipestat &= ~enable_mask;
59146c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
5923143a2bfSChris Wilson 	POSTING_READ(reg);
5937c463586SKeith Packard }
5947c463586SKeith Packard 
59510c59c51SImre Deak static u32 vlv_get_pipestat_enable_mask(struct drm_device *dev, u32 status_mask)
59610c59c51SImre Deak {
59710c59c51SImre Deak 	u32 enable_mask = status_mask << 16;
59810c59c51SImre Deak 
59910c59c51SImre Deak 	/*
600724a6905SVille Syrjälä 	 * On pipe A we don't support the PSR interrupt yet,
601724a6905SVille Syrjälä 	 * on pipe B and C the same bit MBZ.
60210c59c51SImre Deak 	 */
60310c59c51SImre Deak 	if (WARN_ON_ONCE(status_mask & PIPE_A_PSR_STATUS_VLV))
60410c59c51SImre Deak 		return 0;
605724a6905SVille Syrjälä 	/*
606724a6905SVille Syrjälä 	 * On pipe B and C we don't support the PSR interrupt yet, on pipe
607724a6905SVille Syrjälä 	 * A the same bit is for perf counters which we don't use either.
608724a6905SVille Syrjälä 	 */
609724a6905SVille Syrjälä 	if (WARN_ON_ONCE(status_mask & PIPE_B_PSR_STATUS_VLV))
610724a6905SVille Syrjälä 		return 0;
61110c59c51SImre Deak 
61210c59c51SImre Deak 	enable_mask &= ~(PIPE_FIFO_UNDERRUN_STATUS |
61310c59c51SImre Deak 			 SPRITE0_FLIP_DONE_INT_EN_VLV |
61410c59c51SImre Deak 			 SPRITE1_FLIP_DONE_INT_EN_VLV);
61510c59c51SImre Deak 	if (status_mask & SPRITE0_FLIP_DONE_INT_STATUS_VLV)
61610c59c51SImre Deak 		enable_mask |= SPRITE0_FLIP_DONE_INT_EN_VLV;
61710c59c51SImre Deak 	if (status_mask & SPRITE1_FLIP_DONE_INT_STATUS_VLV)
61810c59c51SImre Deak 		enable_mask |= SPRITE1_FLIP_DONE_INT_EN_VLV;
61910c59c51SImre Deak 
62010c59c51SImre Deak 	return enable_mask;
62110c59c51SImre Deak }
62210c59c51SImre Deak 
623755e9019SImre Deak void
624755e9019SImre Deak i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
625755e9019SImre Deak 		     u32 status_mask)
626755e9019SImre Deak {
627755e9019SImre Deak 	u32 enable_mask;
628755e9019SImre Deak 
629666a4537SWayne Boyer 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
63091c8a326SChris Wilson 		enable_mask = vlv_get_pipestat_enable_mask(&dev_priv->drm,
63110c59c51SImre Deak 							   status_mask);
63210c59c51SImre Deak 	else
633755e9019SImre Deak 		enable_mask = status_mask << 16;
634755e9019SImre Deak 	__i915_enable_pipestat(dev_priv, pipe, enable_mask, status_mask);
635755e9019SImre Deak }
636755e9019SImre Deak 
637755e9019SImre Deak void
638755e9019SImre Deak i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
639755e9019SImre Deak 		      u32 status_mask)
640755e9019SImre Deak {
641755e9019SImre Deak 	u32 enable_mask;
642755e9019SImre Deak 
643666a4537SWayne Boyer 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
64491c8a326SChris Wilson 		enable_mask = vlv_get_pipestat_enable_mask(&dev_priv->drm,
64510c59c51SImre Deak 							   status_mask);
64610c59c51SImre Deak 	else
647755e9019SImre Deak 		enable_mask = status_mask << 16;
648755e9019SImre Deak 	__i915_disable_pipestat(dev_priv, pipe, enable_mask, status_mask);
649755e9019SImre Deak }
650755e9019SImre Deak 
651c0e09200SDave Airlie /**
652f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
65314bb2c11STvrtko Ursulin  * @dev_priv: i915 device private
65401c66889SZhao Yakui  */
65591d14251STvrtko Ursulin static void i915_enable_asle_pipestat(struct drm_i915_private *dev_priv)
65601c66889SZhao Yakui {
65791d14251STvrtko Ursulin 	if (!dev_priv->opregion.asle || !IS_MOBILE(dev_priv))
658f49e38ddSJani Nikula 		return;
659f49e38ddSJani Nikula 
66013321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
66101c66889SZhao Yakui 
662755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_STATUS);
66391d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 4)
6643b6c42e8SDaniel Vetter 		i915_enable_pipestat(dev_priv, PIPE_A,
665755e9019SImre Deak 				     PIPE_LEGACY_BLC_EVENT_STATUS);
6661ec14ad3SChris Wilson 
66713321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
66801c66889SZhao Yakui }
66901c66889SZhao Yakui 
670f75f3746SVille Syrjälä /*
671f75f3746SVille Syrjälä  * This timing diagram depicts the video signal in and
672f75f3746SVille Syrjälä  * around the vertical blanking period.
673f75f3746SVille Syrjälä  *
674f75f3746SVille Syrjälä  * Assumptions about the fictitious mode used in this example:
675f75f3746SVille Syrjälä  *  vblank_start >= 3
676f75f3746SVille Syrjälä  *  vsync_start = vblank_start + 1
677f75f3746SVille Syrjälä  *  vsync_end = vblank_start + 2
678f75f3746SVille Syrjälä  *  vtotal = vblank_start + 3
679f75f3746SVille Syrjälä  *
680f75f3746SVille Syrjälä  *           start of vblank:
681f75f3746SVille Syrjälä  *           latch double buffered registers
682f75f3746SVille Syrjälä  *           increment frame counter (ctg+)
683f75f3746SVille Syrjälä  *           generate start of vblank interrupt (gen4+)
684f75f3746SVille Syrjälä  *           |
685f75f3746SVille Syrjälä  *           |          frame start:
686f75f3746SVille Syrjälä  *           |          generate frame start interrupt (aka. vblank interrupt) (gmch)
687f75f3746SVille Syrjälä  *           |          may be shifted forward 1-3 extra lines via PIPECONF
688f75f3746SVille Syrjälä  *           |          |
689f75f3746SVille Syrjälä  *           |          |  start of vsync:
690f75f3746SVille Syrjälä  *           |          |  generate vsync interrupt
691f75f3746SVille Syrjälä  *           |          |  |
692f75f3746SVille Syrjälä  * ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx
693f75f3746SVille Syrjälä  *       .   \hs/   .      \hs/          \hs/          \hs/   .      \hs/
694f75f3746SVille Syrjälä  * ----va---> <-----------------vb--------------------> <--------va-------------
695f75f3746SVille Syrjälä  *       |          |       <----vs----->                     |
696f75f3746SVille Syrjälä  * -vbs-----> <---vbs+1---> <---vbs+2---> <-----0-----> <-----1-----> <-----2--- (scanline counter gen2)
697f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2---> <-----0--- (scanline counter gen3+)
698f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2- (scanline counter hsw+ hdmi)
699f75f3746SVille Syrjälä  *       |          |                                         |
700f75f3746SVille Syrjälä  *       last visible pixel                                   first visible pixel
701f75f3746SVille Syrjälä  *                  |                                         increment frame counter (gen3/4)
702f75f3746SVille Syrjälä  *                  pixel counter = vblank_start * htotal     pixel counter = 0 (gen3/4)
703f75f3746SVille Syrjälä  *
704f75f3746SVille Syrjälä  * x  = horizontal active
705f75f3746SVille Syrjälä  * _  = horizontal blanking
706f75f3746SVille Syrjälä  * hs = horizontal sync
707f75f3746SVille Syrjälä  * va = vertical active
708f75f3746SVille Syrjälä  * vb = vertical blanking
709f75f3746SVille Syrjälä  * vs = vertical sync
710f75f3746SVille Syrjälä  * vbs = vblank_start (number)
711f75f3746SVille Syrjälä  *
712f75f3746SVille Syrjälä  * Summary:
713f75f3746SVille Syrjälä  * - most events happen at the start of horizontal sync
714f75f3746SVille Syrjälä  * - frame start happens at the start of horizontal blank, 1-4 lines
715f75f3746SVille Syrjälä  *   (depending on PIPECONF settings) after the start of vblank
716f75f3746SVille Syrjälä  * - gen3/4 pixel and frame counter are synchronized with the start
717f75f3746SVille Syrjälä  *   of horizontal active on the first line of vertical active
718f75f3746SVille Syrjälä  */
719f75f3746SVille Syrjälä 
72042f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
72142f52ef8SKeith Packard  * we use as a pipe index
72242f52ef8SKeith Packard  */
72388e72717SThierry Reding static u32 i915_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
7240a3e67a4SJesse Barnes {
725fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
726f0f59a00SVille Syrjälä 	i915_reg_t high_frame, low_frame;
7270b2a8e09SVille Syrjälä 	u32 high1, high2, low, pixel, vbl_start, hsync_start, htotal;
728*98187836SVille Syrjälä 	struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
729*98187836SVille Syrjälä 								pipe);
730fc467a22SMaarten Lankhorst 	const struct drm_display_mode *mode = &intel_crtc->base.hwmode;
731391f75e2SVille Syrjälä 
7320b2a8e09SVille Syrjälä 	htotal = mode->crtc_htotal;
7330b2a8e09SVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
7340b2a8e09SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
7350b2a8e09SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
7360b2a8e09SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
737391f75e2SVille Syrjälä 
7380b2a8e09SVille Syrjälä 	/* Convert to pixel count */
7390b2a8e09SVille Syrjälä 	vbl_start *= htotal;
7400b2a8e09SVille Syrjälä 
7410b2a8e09SVille Syrjälä 	/* Start of vblank event occurs at start of hsync */
7420b2a8e09SVille Syrjälä 	vbl_start -= htotal - hsync_start;
7430b2a8e09SVille Syrjälä 
7449db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
7459db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
7465eddb70bSChris Wilson 
7470a3e67a4SJesse Barnes 	/*
7480a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
7490a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
7500a3e67a4SJesse Barnes 	 * register.
7510a3e67a4SJesse Barnes 	 */
7520a3e67a4SJesse Barnes 	do {
7535eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
754391f75e2SVille Syrjälä 		low   = I915_READ(low_frame);
7555eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
7560a3e67a4SJesse Barnes 	} while (high1 != high2);
7570a3e67a4SJesse Barnes 
7585eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
759391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
7605eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
761391f75e2SVille Syrjälä 
762391f75e2SVille Syrjälä 	/*
763391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
764391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
765391f75e2SVille Syrjälä 	 * counter against vblank start.
766391f75e2SVille Syrjälä 	 */
767edc08d0aSVille Syrjälä 	return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
7680a3e67a4SJesse Barnes }
7690a3e67a4SJesse Barnes 
770974e59baSDave Airlie static u32 g4x_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
7719880b7a5SJesse Barnes {
772fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
7739880b7a5SJesse Barnes 
774649636efSVille Syrjälä 	return I915_READ(PIPE_FRMCOUNT_G4X(pipe));
7759880b7a5SJesse Barnes }
7769880b7a5SJesse Barnes 
77775aa3f63SVille Syrjälä /* I915_READ_FW, only for fast reads of display block, no need for forcewake etc. */
778a225f079SVille Syrjälä static int __intel_get_crtc_scanline(struct intel_crtc *crtc)
779a225f079SVille Syrjälä {
780a225f079SVille Syrjälä 	struct drm_device *dev = crtc->base.dev;
781fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
782fc467a22SMaarten Lankhorst 	const struct drm_display_mode *mode = &crtc->base.hwmode;
783a225f079SVille Syrjälä 	enum pipe pipe = crtc->pipe;
78480715b2fSVille Syrjälä 	int position, vtotal;
785a225f079SVille Syrjälä 
78680715b2fSVille Syrjälä 	vtotal = mode->crtc_vtotal;
787a225f079SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
788a225f079SVille Syrjälä 		vtotal /= 2;
789a225f079SVille Syrjälä 
79091d14251STvrtko Ursulin 	if (IS_GEN2(dev_priv))
79175aa3f63SVille Syrjälä 		position = I915_READ_FW(PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
792a225f079SVille Syrjälä 	else
79375aa3f63SVille Syrjälä 		position = I915_READ_FW(PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
794a225f079SVille Syrjälä 
795a225f079SVille Syrjälä 	/*
79641b578fbSJesse Barnes 	 * On HSW, the DSL reg (0x70000) appears to return 0 if we
79741b578fbSJesse Barnes 	 * read it just before the start of vblank.  So try it again
79841b578fbSJesse Barnes 	 * so we don't accidentally end up spanning a vblank frame
79941b578fbSJesse Barnes 	 * increment, causing the pipe_update_end() code to squak at us.
80041b578fbSJesse Barnes 	 *
80141b578fbSJesse Barnes 	 * The nature of this problem means we can't simply check the ISR
80241b578fbSJesse Barnes 	 * bit and return the vblank start value; nor can we use the scanline
80341b578fbSJesse Barnes 	 * debug register in the transcoder as it appears to have the same
80441b578fbSJesse Barnes 	 * problem.  We may need to extend this to include other platforms,
80541b578fbSJesse Barnes 	 * but so far testing only shows the problem on HSW.
80641b578fbSJesse Barnes 	 */
80791d14251STvrtko Ursulin 	if (HAS_DDI(dev_priv) && !position) {
80841b578fbSJesse Barnes 		int i, temp;
80941b578fbSJesse Barnes 
81041b578fbSJesse Barnes 		for (i = 0; i < 100; i++) {
81141b578fbSJesse Barnes 			udelay(1);
81241b578fbSJesse Barnes 			temp = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) &
81341b578fbSJesse Barnes 				DSL_LINEMASK_GEN3;
81441b578fbSJesse Barnes 			if (temp != position) {
81541b578fbSJesse Barnes 				position = temp;
81641b578fbSJesse Barnes 				break;
81741b578fbSJesse Barnes 			}
81841b578fbSJesse Barnes 		}
81941b578fbSJesse Barnes 	}
82041b578fbSJesse Barnes 
82141b578fbSJesse Barnes 	/*
82280715b2fSVille Syrjälä 	 * See update_scanline_offset() for the details on the
82380715b2fSVille Syrjälä 	 * scanline_offset adjustment.
824a225f079SVille Syrjälä 	 */
82580715b2fSVille Syrjälä 	return (position + crtc->scanline_offset) % vtotal;
826a225f079SVille Syrjälä }
827a225f079SVille Syrjälä 
82888e72717SThierry Reding static int i915_get_crtc_scanoutpos(struct drm_device *dev, unsigned int pipe,
829abca9e45SVille Syrjälä 				    unsigned int flags, int *vpos, int *hpos,
8303bb403bfSVille Syrjälä 				    ktime_t *stime, ktime_t *etime,
8313bb403bfSVille Syrjälä 				    const struct drm_display_mode *mode)
8320af7e4dfSMario Kleiner {
833fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
834*98187836SVille Syrjälä 	struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
835*98187836SVille Syrjälä 								pipe);
8363aa18df8SVille Syrjälä 	int position;
83778e8fc6bSVille Syrjälä 	int vbl_start, vbl_end, hsync_start, htotal, vtotal;
8380af7e4dfSMario Kleiner 	bool in_vbl = true;
8390af7e4dfSMario Kleiner 	int ret = 0;
840ad3543edSMario Kleiner 	unsigned long irqflags;
8410af7e4dfSMario Kleiner 
842fc467a22SMaarten Lankhorst 	if (WARN_ON(!mode->crtc_clock)) {
8430af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
8449db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
8450af7e4dfSMario Kleiner 		return 0;
8460af7e4dfSMario Kleiner 	}
8470af7e4dfSMario Kleiner 
848c2baf4b7SVille Syrjälä 	htotal = mode->crtc_htotal;
84978e8fc6bSVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
850c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
851c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
852c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
8530af7e4dfSMario Kleiner 
854d31faf65SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
855d31faf65SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
856d31faf65SVille Syrjälä 		vbl_end /= 2;
857d31faf65SVille Syrjälä 		vtotal /= 2;
858d31faf65SVille Syrjälä 	}
859d31faf65SVille Syrjälä 
860c2baf4b7SVille Syrjälä 	ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
861c2baf4b7SVille Syrjälä 
862ad3543edSMario Kleiner 	/*
863ad3543edSMario Kleiner 	 * Lock uncore.lock, as we will do multiple timing critical raw
864ad3543edSMario Kleiner 	 * register reads, potentially with preemption disabled, so the
865ad3543edSMario Kleiner 	 * following code must not block on uncore.lock.
866ad3543edSMario Kleiner 	 */
867ad3543edSMario Kleiner 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
868ad3543edSMario Kleiner 
869ad3543edSMario Kleiner 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
870ad3543edSMario Kleiner 
871ad3543edSMario Kleiner 	/* Get optional system timestamp before query. */
872ad3543edSMario Kleiner 	if (stime)
873ad3543edSMario Kleiner 		*stime = ktime_get();
874ad3543edSMario Kleiner 
87591d14251STvrtko Ursulin 	if (IS_GEN2(dev_priv) || IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5) {
8760af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
8770af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
8780af7e4dfSMario Kleiner 		 */
879a225f079SVille Syrjälä 		position = __intel_get_crtc_scanline(intel_crtc);
8800af7e4dfSMario Kleiner 	} else {
8810af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
8820af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
8830af7e4dfSMario Kleiner 		 * scanout position.
8840af7e4dfSMario Kleiner 		 */
88575aa3f63SVille Syrjälä 		position = (I915_READ_FW(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
8860af7e4dfSMario Kleiner 
8873aa18df8SVille Syrjälä 		/* convert to pixel counts */
8883aa18df8SVille Syrjälä 		vbl_start *= htotal;
8893aa18df8SVille Syrjälä 		vbl_end *= htotal;
8903aa18df8SVille Syrjälä 		vtotal *= htotal;
89178e8fc6bSVille Syrjälä 
89278e8fc6bSVille Syrjälä 		/*
8937e78f1cbSVille Syrjälä 		 * In interlaced modes, the pixel counter counts all pixels,
8947e78f1cbSVille Syrjälä 		 * so one field will have htotal more pixels. In order to avoid
8957e78f1cbSVille Syrjälä 		 * the reported position from jumping backwards when the pixel
8967e78f1cbSVille Syrjälä 		 * counter is beyond the length of the shorter field, just
8977e78f1cbSVille Syrjälä 		 * clamp the position the length of the shorter field. This
8987e78f1cbSVille Syrjälä 		 * matches how the scanline counter based position works since
8997e78f1cbSVille Syrjälä 		 * the scanline counter doesn't count the two half lines.
9007e78f1cbSVille Syrjälä 		 */
9017e78f1cbSVille Syrjälä 		if (position >= vtotal)
9027e78f1cbSVille Syrjälä 			position = vtotal - 1;
9037e78f1cbSVille Syrjälä 
9047e78f1cbSVille Syrjälä 		/*
90578e8fc6bSVille Syrjälä 		 * Start of vblank interrupt is triggered at start of hsync,
90678e8fc6bSVille Syrjälä 		 * just prior to the first active line of vblank. However we
90778e8fc6bSVille Syrjälä 		 * consider lines to start at the leading edge of horizontal
90878e8fc6bSVille Syrjälä 		 * active. So, should we get here before we've crossed into
90978e8fc6bSVille Syrjälä 		 * the horizontal active of the first line in vblank, we would
91078e8fc6bSVille Syrjälä 		 * not set the DRM_SCANOUTPOS_INVBL flag. In order to fix that,
91178e8fc6bSVille Syrjälä 		 * always add htotal-hsync_start to the current pixel position.
91278e8fc6bSVille Syrjälä 		 */
91378e8fc6bSVille Syrjälä 		position = (position + htotal - hsync_start) % vtotal;
9143aa18df8SVille Syrjälä 	}
9153aa18df8SVille Syrjälä 
916ad3543edSMario Kleiner 	/* Get optional system timestamp after query. */
917ad3543edSMario Kleiner 	if (etime)
918ad3543edSMario Kleiner 		*etime = ktime_get();
919ad3543edSMario Kleiner 
920ad3543edSMario Kleiner 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
921ad3543edSMario Kleiner 
922ad3543edSMario Kleiner 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
923ad3543edSMario Kleiner 
9243aa18df8SVille Syrjälä 	in_vbl = position >= vbl_start && position < vbl_end;
9253aa18df8SVille Syrjälä 
9263aa18df8SVille Syrjälä 	/*
9273aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
9283aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
9293aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
9303aa18df8SVille Syrjälä 	 * up since vbl_end.
9313aa18df8SVille Syrjälä 	 */
9323aa18df8SVille Syrjälä 	if (position >= vbl_start)
9333aa18df8SVille Syrjälä 		position -= vbl_end;
9343aa18df8SVille Syrjälä 	else
9353aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
9363aa18df8SVille Syrjälä 
93791d14251STvrtko Ursulin 	if (IS_GEN2(dev_priv) || IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5) {
9383aa18df8SVille Syrjälä 		*vpos = position;
9393aa18df8SVille Syrjälä 		*hpos = 0;
9403aa18df8SVille Syrjälä 	} else {
9410af7e4dfSMario Kleiner 		*vpos = position / htotal;
9420af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
9430af7e4dfSMario Kleiner 	}
9440af7e4dfSMario Kleiner 
9450af7e4dfSMario Kleiner 	/* In vblank? */
9460af7e4dfSMario Kleiner 	if (in_vbl)
9473d3cbd84SDaniel Vetter 		ret |= DRM_SCANOUTPOS_IN_VBLANK;
9480af7e4dfSMario Kleiner 
9490af7e4dfSMario Kleiner 	return ret;
9500af7e4dfSMario Kleiner }
9510af7e4dfSMario Kleiner 
952a225f079SVille Syrjälä int intel_get_crtc_scanline(struct intel_crtc *crtc)
953a225f079SVille Syrjälä {
954fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
955a225f079SVille Syrjälä 	unsigned long irqflags;
956a225f079SVille Syrjälä 	int position;
957a225f079SVille Syrjälä 
958a225f079SVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
959a225f079SVille Syrjälä 	position = __intel_get_crtc_scanline(crtc);
960a225f079SVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
961a225f079SVille Syrjälä 
962a225f079SVille Syrjälä 	return position;
963a225f079SVille Syrjälä }
964a225f079SVille Syrjälä 
96588e72717SThierry Reding static int i915_get_vblank_timestamp(struct drm_device *dev, unsigned int pipe,
9660af7e4dfSMario Kleiner 			      int *max_error,
9670af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
9680af7e4dfSMario Kleiner 			      unsigned flags)
9690af7e4dfSMario Kleiner {
970b91eb5ccSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(dev);
971e2af48c6SVille Syrjälä 	struct intel_crtc *crtc;
9720af7e4dfSMario Kleiner 
973b91eb5ccSVille Syrjälä 	if (pipe >= INTEL_INFO(dev_priv)->num_pipes) {
97488e72717SThierry Reding 		DRM_ERROR("Invalid crtc %u\n", pipe);
9750af7e4dfSMario Kleiner 		return -EINVAL;
9760af7e4dfSMario Kleiner 	}
9770af7e4dfSMario Kleiner 
9780af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
979b91eb5ccSVille Syrjälä 	crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
9804041b853SChris Wilson 	if (crtc == NULL) {
98188e72717SThierry Reding 		DRM_ERROR("Invalid crtc %u\n", pipe);
9824041b853SChris Wilson 		return -EINVAL;
9834041b853SChris Wilson 	}
9844041b853SChris Wilson 
985e2af48c6SVille Syrjälä 	if (!crtc->base.hwmode.crtc_clock) {
98688e72717SThierry Reding 		DRM_DEBUG_KMS("crtc %u is disabled\n", pipe);
9874041b853SChris Wilson 		return -EBUSY;
9884041b853SChris Wilson 	}
9890af7e4dfSMario Kleiner 
9900af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
9914041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
9924041b853SChris Wilson 						     vblank_time, flags,
993e2af48c6SVille Syrjälä 						     &crtc->base.hwmode);
9940af7e4dfSMario Kleiner }
9950af7e4dfSMario Kleiner 
99691d14251STvrtko Ursulin static void ironlake_rps_change_irq_handler(struct drm_i915_private *dev_priv)
997f97108d1SJesse Barnes {
998b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
9999270388eSDaniel Vetter 	u8 new_delay;
10009270388eSDaniel Vetter 
1001d0ecd7e2SDaniel Vetter 	spin_lock(&mchdev_lock);
1002f97108d1SJesse Barnes 
100373edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
100473edd18fSDaniel Vetter 
100520e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
10069270388eSDaniel Vetter 
10077648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
1008b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
1009b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
1010f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
1011f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
1012f97108d1SJesse Barnes 
1013f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
1014b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
101520e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
101620e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
101720e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
101820e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
1019b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
102020e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
102120e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
102220e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
102320e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
1024f97108d1SJesse Barnes 	}
1025f97108d1SJesse Barnes 
102691d14251STvrtko Ursulin 	if (ironlake_set_drps(dev_priv, new_delay))
102720e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
1028f97108d1SJesse Barnes 
1029d0ecd7e2SDaniel Vetter 	spin_unlock(&mchdev_lock);
10309270388eSDaniel Vetter 
1031f97108d1SJesse Barnes 	return;
1032f97108d1SJesse Barnes }
1033f97108d1SJesse Barnes 
10340bc40be8STvrtko Ursulin static void notify_ring(struct intel_engine_cs *engine)
1035549f7365SChris Wilson {
1036aca34b6eSChris Wilson 	smp_store_mb(engine->breadcrumbs.irq_posted, true);
103783348ba8SChris Wilson 	if (intel_engine_wakeup(engine))
10380bc40be8STvrtko Ursulin 		trace_i915_gem_request_notify(engine);
1039549f7365SChris Wilson }
1040549f7365SChris Wilson 
104143cf3bf0SChris Wilson static void vlv_c0_read(struct drm_i915_private *dev_priv,
104243cf3bf0SChris Wilson 			struct intel_rps_ei *ei)
104331685c25SDeepak S {
104443cf3bf0SChris Wilson 	ei->cz_clock = vlv_punit_read(dev_priv, PUNIT_REG_CZ_TIMESTAMP);
104543cf3bf0SChris Wilson 	ei->render_c0 = I915_READ(VLV_RENDER_C0_COUNT);
104643cf3bf0SChris Wilson 	ei->media_c0 = I915_READ(VLV_MEDIA_C0_COUNT);
104731685c25SDeepak S }
104831685c25SDeepak S 
104943cf3bf0SChris Wilson static bool vlv_c0_above(struct drm_i915_private *dev_priv,
105043cf3bf0SChris Wilson 			 const struct intel_rps_ei *old,
105143cf3bf0SChris Wilson 			 const struct intel_rps_ei *now,
105243cf3bf0SChris Wilson 			 int threshold)
105331685c25SDeepak S {
105443cf3bf0SChris Wilson 	u64 time, c0;
10557bad74d5SVille Syrjälä 	unsigned int mul = 100;
105631685c25SDeepak S 
105743cf3bf0SChris Wilson 	if (old->cz_clock == 0)
105843cf3bf0SChris Wilson 		return false;
105931685c25SDeepak S 
10607bad74d5SVille Syrjälä 	if (I915_READ(VLV_COUNTER_CONTROL) & VLV_COUNT_RANGE_HIGH)
10617bad74d5SVille Syrjälä 		mul <<= 8;
10627bad74d5SVille Syrjälä 
106343cf3bf0SChris Wilson 	time = now->cz_clock - old->cz_clock;
10647bad74d5SVille Syrjälä 	time *= threshold * dev_priv->czclk_freq;
106531685c25SDeepak S 
106643cf3bf0SChris Wilson 	/* Workload can be split between render + media, e.g. SwapBuffers
106743cf3bf0SChris Wilson 	 * being blitted in X after being rendered in mesa. To account for
106843cf3bf0SChris Wilson 	 * this we need to combine both engines into our activity counter.
106943cf3bf0SChris Wilson 	 */
107043cf3bf0SChris Wilson 	c0 = now->render_c0 - old->render_c0;
107143cf3bf0SChris Wilson 	c0 += now->media_c0 - old->media_c0;
10727bad74d5SVille Syrjälä 	c0 *= mul * VLV_CZ_CLOCK_TO_MILLI_SEC;
107331685c25SDeepak S 
107443cf3bf0SChris Wilson 	return c0 >= time;
107531685c25SDeepak S }
107631685c25SDeepak S 
107743cf3bf0SChris Wilson void gen6_rps_reset_ei(struct drm_i915_private *dev_priv)
107843cf3bf0SChris Wilson {
107943cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &dev_priv->rps.down_ei);
108043cf3bf0SChris Wilson 	dev_priv->rps.up_ei = dev_priv->rps.down_ei;
108143cf3bf0SChris Wilson }
108243cf3bf0SChris Wilson 
108343cf3bf0SChris Wilson static u32 vlv_wa_c0_ei(struct drm_i915_private *dev_priv, u32 pm_iir)
108443cf3bf0SChris Wilson {
108543cf3bf0SChris Wilson 	struct intel_rps_ei now;
108643cf3bf0SChris Wilson 	u32 events = 0;
108743cf3bf0SChris Wilson 
10886f4b12f8SChris Wilson 	if ((pm_iir & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED)) == 0)
108943cf3bf0SChris Wilson 		return 0;
109043cf3bf0SChris Wilson 
109143cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &now);
109243cf3bf0SChris Wilson 	if (now.cz_clock == 0)
109343cf3bf0SChris Wilson 		return 0;
109431685c25SDeepak S 
109543cf3bf0SChris Wilson 	if (pm_iir & GEN6_PM_RP_DOWN_EI_EXPIRED) {
109643cf3bf0SChris Wilson 		if (!vlv_c0_above(dev_priv,
109743cf3bf0SChris Wilson 				  &dev_priv->rps.down_ei, &now,
10988fb55197SChris Wilson 				  dev_priv->rps.down_threshold))
109943cf3bf0SChris Wilson 			events |= GEN6_PM_RP_DOWN_THRESHOLD;
110043cf3bf0SChris Wilson 		dev_priv->rps.down_ei = now;
110131685c25SDeepak S 	}
110231685c25SDeepak S 
110343cf3bf0SChris Wilson 	if (pm_iir & GEN6_PM_RP_UP_EI_EXPIRED) {
110443cf3bf0SChris Wilson 		if (vlv_c0_above(dev_priv,
110543cf3bf0SChris Wilson 				 &dev_priv->rps.up_ei, &now,
11068fb55197SChris Wilson 				 dev_priv->rps.up_threshold))
110743cf3bf0SChris Wilson 			events |= GEN6_PM_RP_UP_THRESHOLD;
110843cf3bf0SChris Wilson 		dev_priv->rps.up_ei = now;
110943cf3bf0SChris Wilson 	}
111043cf3bf0SChris Wilson 
111143cf3bf0SChris Wilson 	return events;
111231685c25SDeepak S }
111331685c25SDeepak S 
1114f5a4c67dSChris Wilson static bool any_waiters(struct drm_i915_private *dev_priv)
1115f5a4c67dSChris Wilson {
1116e2f80391STvrtko Ursulin 	struct intel_engine_cs *engine;
11173b3f1650SAkash Goel 	enum intel_engine_id id;
1118f5a4c67dSChris Wilson 
11193b3f1650SAkash Goel 	for_each_engine(engine, dev_priv, id)
1120688e6c72SChris Wilson 		if (intel_engine_has_waiter(engine))
1121f5a4c67dSChris Wilson 			return true;
1122f5a4c67dSChris Wilson 
1123f5a4c67dSChris Wilson 	return false;
1124f5a4c67dSChris Wilson }
1125f5a4c67dSChris Wilson 
11264912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
11273b8d8d91SJesse Barnes {
11282d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
11292d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, rps.work);
11308d3afd7dSChris Wilson 	bool client_boost;
11318d3afd7dSChris Wilson 	int new_delay, adj, min, max;
1132edbfdb45SPaulo Zanoni 	u32 pm_iir;
11333b8d8d91SJesse Barnes 
113459cdb63dSDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
1135d4d70aa5SImre Deak 	/* Speed up work cancelation during disabling rps interrupts. */
1136d4d70aa5SImre Deak 	if (!dev_priv->rps.interrupts_enabled) {
1137d4d70aa5SImre Deak 		spin_unlock_irq(&dev_priv->irq_lock);
1138d4d70aa5SImre Deak 		return;
1139d4d70aa5SImre Deak 	}
11401f814dacSImre Deak 
1141c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
1142c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
1143a72fbc3aSImre Deak 	/* Make sure not to corrupt PMIMR state used by ringbuffer on GEN6 */
1144f4e9af4fSAkash Goel 	gen6_unmask_pm_irq(dev_priv, dev_priv->pm_rps_events);
11458d3afd7dSChris Wilson 	client_boost = dev_priv->rps.client_boost;
11468d3afd7dSChris Wilson 	dev_priv->rps.client_boost = false;
114759cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
11484912d041SBen Widawsky 
114960611c13SPaulo Zanoni 	/* Make sure we didn't queue anything we're not going to process. */
1150a6706b45SDeepak S 	WARN_ON(pm_iir & ~dev_priv->pm_rps_events);
115160611c13SPaulo Zanoni 
11528d3afd7dSChris Wilson 	if ((pm_iir & dev_priv->pm_rps_events) == 0 && !client_boost)
1153c33d247dSChris Wilson 		return;
11543b8d8d91SJesse Barnes 
11554fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
11567b9e0ae6SChris Wilson 
115743cf3bf0SChris Wilson 	pm_iir |= vlv_wa_c0_ei(dev_priv, pm_iir);
115843cf3bf0SChris Wilson 
1159dd75fdc8SChris Wilson 	adj = dev_priv->rps.last_adj;
1160edcf284bSChris Wilson 	new_delay = dev_priv->rps.cur_freq;
11618d3afd7dSChris Wilson 	min = dev_priv->rps.min_freq_softlimit;
11628d3afd7dSChris Wilson 	max = dev_priv->rps.max_freq_softlimit;
116329ecd78dSChris Wilson 	if (client_boost || any_waiters(dev_priv))
116429ecd78dSChris Wilson 		max = dev_priv->rps.max_freq;
116529ecd78dSChris Wilson 	if (client_boost && new_delay < dev_priv->rps.boost_freq) {
116629ecd78dSChris Wilson 		new_delay = dev_priv->rps.boost_freq;
11678d3afd7dSChris Wilson 		adj = 0;
11688d3afd7dSChris Wilson 	} else if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
1169dd75fdc8SChris Wilson 		if (adj > 0)
1170dd75fdc8SChris Wilson 			adj *= 2;
1171edcf284bSChris Wilson 		else /* CHV needs even encode values */
1172edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? 2 : 1;
11737425034aSVille Syrjälä 		/*
11747425034aSVille Syrjälä 		 * For better performance, jump directly
11757425034aSVille Syrjälä 		 * to RPe if we're below it.
11767425034aSVille Syrjälä 		 */
1177edcf284bSChris Wilson 		if (new_delay < dev_priv->rps.efficient_freq - adj) {
1178b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1179edcf284bSChris Wilson 			adj = 0;
1180edcf284bSChris Wilson 		}
118129ecd78dSChris Wilson 	} else if (client_boost || any_waiters(dev_priv)) {
1182f5a4c67dSChris Wilson 		adj = 0;
1183dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
1184b39fb297SBen Widawsky 		if (dev_priv->rps.cur_freq > dev_priv->rps.efficient_freq)
1185b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1186dd75fdc8SChris Wilson 		else
1187b39fb297SBen Widawsky 			new_delay = dev_priv->rps.min_freq_softlimit;
1188dd75fdc8SChris Wilson 		adj = 0;
1189dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1190dd75fdc8SChris Wilson 		if (adj < 0)
1191dd75fdc8SChris Wilson 			adj *= 2;
1192edcf284bSChris Wilson 		else /* CHV needs even encode values */
1193edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? -2 : -1;
1194dd75fdc8SChris Wilson 	} else { /* unknown event */
1195edcf284bSChris Wilson 		adj = 0;
1196dd75fdc8SChris Wilson 	}
11973b8d8d91SJesse Barnes 
1198edcf284bSChris Wilson 	dev_priv->rps.last_adj = adj;
1199edcf284bSChris Wilson 
120079249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
120179249636SBen Widawsky 	 * interrupt
120279249636SBen Widawsky 	 */
1203edcf284bSChris Wilson 	new_delay += adj;
12048d3afd7dSChris Wilson 	new_delay = clamp_t(int, new_delay, min, max);
120527544369SDeepak S 
1206dc97997aSChris Wilson 	intel_set_rps(dev_priv, new_delay);
12073b8d8d91SJesse Barnes 
12084fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
12093b8d8d91SJesse Barnes }
12103b8d8d91SJesse Barnes 
1211e3689190SBen Widawsky 
1212e3689190SBen Widawsky /**
1213e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
1214e3689190SBen Widawsky  * occurred.
1215e3689190SBen Widawsky  * @work: workqueue struct
1216e3689190SBen Widawsky  *
1217e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
1218e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
1219e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
1220e3689190SBen Widawsky  */
1221e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
1222e3689190SBen Widawsky {
12232d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
12242d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, l3_parity.error_work);
1225e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
122635a85ac6SBen Widawsky 	char *parity_event[6];
1227e3689190SBen Widawsky 	uint32_t misccpctl;
122835a85ac6SBen Widawsky 	uint8_t slice = 0;
1229e3689190SBen Widawsky 
1230e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
1231e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
1232e3689190SBen Widawsky 	 * any time we access those registers.
1233e3689190SBen Widawsky 	 */
123491c8a326SChris Wilson 	mutex_lock(&dev_priv->drm.struct_mutex);
1235e3689190SBen Widawsky 
123635a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
123735a85ac6SBen Widawsky 	if (WARN_ON(!dev_priv->l3_parity.which_slice))
123835a85ac6SBen Widawsky 		goto out;
123935a85ac6SBen Widawsky 
1240e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
1241e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1242e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
1243e3689190SBen Widawsky 
124435a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
1245f0f59a00SVille Syrjälä 		i915_reg_t reg;
124635a85ac6SBen Widawsky 
124735a85ac6SBen Widawsky 		slice--;
12482d1fe073SJoonas Lahtinen 		if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv)))
124935a85ac6SBen Widawsky 			break;
125035a85ac6SBen Widawsky 
125135a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
125235a85ac6SBen Widawsky 
12536fa1c5f1SVille Syrjälä 		reg = GEN7_L3CDERRST1(slice);
125435a85ac6SBen Widawsky 
125535a85ac6SBen Widawsky 		error_status = I915_READ(reg);
1256e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
1257e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
1258e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1259e3689190SBen Widawsky 
126035a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
126135a85ac6SBen Widawsky 		POSTING_READ(reg);
1262e3689190SBen Widawsky 
1263cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1264e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1265e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1266e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
126735a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
126835a85ac6SBen Widawsky 		parity_event[5] = NULL;
1269e3689190SBen Widawsky 
127091c8a326SChris Wilson 		kobject_uevent_env(&dev_priv->drm.primary->kdev->kobj,
1271e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
1272e3689190SBen Widawsky 
127335a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
127435a85ac6SBen Widawsky 			  slice, row, bank, subbank);
1275e3689190SBen Widawsky 
127635a85ac6SBen Widawsky 		kfree(parity_event[4]);
1277e3689190SBen Widawsky 		kfree(parity_event[3]);
1278e3689190SBen Widawsky 		kfree(parity_event[2]);
1279e3689190SBen Widawsky 		kfree(parity_event[1]);
1280e3689190SBen Widawsky 	}
1281e3689190SBen Widawsky 
128235a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
128335a85ac6SBen Widawsky 
128435a85ac6SBen Widawsky out:
128535a85ac6SBen Widawsky 	WARN_ON(dev_priv->l3_parity.which_slice);
12864cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
12872d1fe073SJoonas Lahtinen 	gen5_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv));
12884cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
128935a85ac6SBen Widawsky 
129091c8a326SChris Wilson 	mutex_unlock(&dev_priv->drm.struct_mutex);
129135a85ac6SBen Widawsky }
129235a85ac6SBen Widawsky 
1293261e40b8SVille Syrjälä static void ivybridge_parity_error_irq_handler(struct drm_i915_private *dev_priv,
1294261e40b8SVille Syrjälä 					       u32 iir)
1295e3689190SBen Widawsky {
1296261e40b8SVille Syrjälä 	if (!HAS_L3_DPF(dev_priv))
1297e3689190SBen Widawsky 		return;
1298e3689190SBen Widawsky 
1299d0ecd7e2SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1300261e40b8SVille Syrjälä 	gen5_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv));
1301d0ecd7e2SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
1302e3689190SBen Widawsky 
1303261e40b8SVille Syrjälä 	iir &= GT_PARITY_ERROR(dev_priv);
130435a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
130535a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 1;
130635a85ac6SBen Widawsky 
130735a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
130835a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 0;
130935a85ac6SBen Widawsky 
1310a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
1311e3689190SBen Widawsky }
1312e3689190SBen Widawsky 
1313261e40b8SVille Syrjälä static void ilk_gt_irq_handler(struct drm_i915_private *dev_priv,
1314f1af8fc1SPaulo Zanoni 			       u32 gt_iir)
1315f1af8fc1SPaulo Zanoni {
1316f8973c21SChris Wilson 	if (gt_iir & GT_RENDER_USER_INTERRUPT)
13173b3f1650SAkash Goel 		notify_ring(dev_priv->engine[RCS]);
1318f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
13193b3f1650SAkash Goel 		notify_ring(dev_priv->engine[VCS]);
1320f1af8fc1SPaulo Zanoni }
1321f1af8fc1SPaulo Zanoni 
1322261e40b8SVille Syrjälä static void snb_gt_irq_handler(struct drm_i915_private *dev_priv,
1323e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1324e7b4c6b1SDaniel Vetter {
1325f8973c21SChris Wilson 	if (gt_iir & GT_RENDER_USER_INTERRUPT)
13263b3f1650SAkash Goel 		notify_ring(dev_priv->engine[RCS]);
1327cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
13283b3f1650SAkash Goel 		notify_ring(dev_priv->engine[VCS]);
1329cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
13303b3f1650SAkash Goel 		notify_ring(dev_priv->engine[BCS]);
1331e7b4c6b1SDaniel Vetter 
1332cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1333cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
1334aaecdf61SDaniel Vetter 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT))
1335aaecdf61SDaniel Vetter 		DRM_DEBUG("Command parser error, gt_iir 0x%08x\n", gt_iir);
1336e3689190SBen Widawsky 
1337261e40b8SVille Syrjälä 	if (gt_iir & GT_PARITY_ERROR(dev_priv))
1338261e40b8SVille Syrjälä 		ivybridge_parity_error_irq_handler(dev_priv, gt_iir);
1339e7b4c6b1SDaniel Vetter }
1340e7b4c6b1SDaniel Vetter 
1341fbcc1a0cSNick Hoath static __always_inline void
13420bc40be8STvrtko Ursulin gen8_cs_irq_handler(struct intel_engine_cs *engine, u32 iir, int test_shift)
1343fbcc1a0cSNick Hoath {
1344fbcc1a0cSNick Hoath 	if (iir & (GT_RENDER_USER_INTERRUPT << test_shift))
13450bc40be8STvrtko Ursulin 		notify_ring(engine);
1346fbcc1a0cSNick Hoath 	if (iir & (GT_CONTEXT_SWITCH_INTERRUPT << test_shift))
134727af5eeaSTvrtko Ursulin 		tasklet_schedule(&engine->irq_tasklet);
1348fbcc1a0cSNick Hoath }
1349fbcc1a0cSNick Hoath 
1350e30e251aSVille Syrjälä static irqreturn_t gen8_gt_irq_ack(struct drm_i915_private *dev_priv,
1351e30e251aSVille Syrjälä 				   u32 master_ctl,
1352e30e251aSVille Syrjälä 				   u32 gt_iir[4])
1353abd58f01SBen Widawsky {
1354abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
1355abd58f01SBen Widawsky 
1356abd58f01SBen Widawsky 	if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
1357e30e251aSVille Syrjälä 		gt_iir[0] = I915_READ_FW(GEN8_GT_IIR(0));
1358e30e251aSVille Syrjälä 		if (gt_iir[0]) {
1359e30e251aSVille Syrjälä 			I915_WRITE_FW(GEN8_GT_IIR(0), gt_iir[0]);
1360abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1361abd58f01SBen Widawsky 		} else
1362abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT0)!\n");
1363abd58f01SBen Widawsky 	}
1364abd58f01SBen Widawsky 
136585f9b5f9SZhao Yakui 	if (master_ctl & (GEN8_GT_VCS1_IRQ | GEN8_GT_VCS2_IRQ)) {
1366e30e251aSVille Syrjälä 		gt_iir[1] = I915_READ_FW(GEN8_GT_IIR(1));
1367e30e251aSVille Syrjälä 		if (gt_iir[1]) {
1368e30e251aSVille Syrjälä 			I915_WRITE_FW(GEN8_GT_IIR(1), gt_iir[1]);
1369abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1370abd58f01SBen Widawsky 		} else
1371abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT1)!\n");
1372abd58f01SBen Widawsky 	}
1373abd58f01SBen Widawsky 
137474cdb337SChris Wilson 	if (master_ctl & GEN8_GT_VECS_IRQ) {
1375e30e251aSVille Syrjälä 		gt_iir[3] = I915_READ_FW(GEN8_GT_IIR(3));
1376e30e251aSVille Syrjälä 		if (gt_iir[3]) {
1377e30e251aSVille Syrjälä 			I915_WRITE_FW(GEN8_GT_IIR(3), gt_iir[3]);
137874cdb337SChris Wilson 			ret = IRQ_HANDLED;
137974cdb337SChris Wilson 		} else
138074cdb337SChris Wilson 			DRM_ERROR("The master control interrupt lied (GT3)!\n");
138174cdb337SChris Wilson 	}
138274cdb337SChris Wilson 
138326705e20SSagar Arun Kamble 	if (master_ctl & (GEN8_GT_PM_IRQ | GEN8_GT_GUC_IRQ)) {
1384e30e251aSVille Syrjälä 		gt_iir[2] = I915_READ_FW(GEN8_GT_IIR(2));
138526705e20SSagar Arun Kamble 		if (gt_iir[2] & (dev_priv->pm_rps_events |
138626705e20SSagar Arun Kamble 				 dev_priv->pm_guc_events)) {
1387cb0d205eSChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(2),
138826705e20SSagar Arun Kamble 				      gt_iir[2] & (dev_priv->pm_rps_events |
138926705e20SSagar Arun Kamble 						   dev_priv->pm_guc_events));
139038cc46d7SOscar Mateo 			ret = IRQ_HANDLED;
13910961021aSBen Widawsky 		} else
13920961021aSBen Widawsky 			DRM_ERROR("The master control interrupt lied (PM)!\n");
13930961021aSBen Widawsky 	}
13940961021aSBen Widawsky 
1395abd58f01SBen Widawsky 	return ret;
1396abd58f01SBen Widawsky }
1397abd58f01SBen Widawsky 
1398e30e251aSVille Syrjälä static void gen8_gt_irq_handler(struct drm_i915_private *dev_priv,
1399e30e251aSVille Syrjälä 				u32 gt_iir[4])
1400e30e251aSVille Syrjälä {
1401e30e251aSVille Syrjälä 	if (gt_iir[0]) {
14023b3f1650SAkash Goel 		gen8_cs_irq_handler(dev_priv->engine[RCS],
1403e30e251aSVille Syrjälä 				    gt_iir[0], GEN8_RCS_IRQ_SHIFT);
14043b3f1650SAkash Goel 		gen8_cs_irq_handler(dev_priv->engine[BCS],
1405e30e251aSVille Syrjälä 				    gt_iir[0], GEN8_BCS_IRQ_SHIFT);
1406e30e251aSVille Syrjälä 	}
1407e30e251aSVille Syrjälä 
1408e30e251aSVille Syrjälä 	if (gt_iir[1]) {
14093b3f1650SAkash Goel 		gen8_cs_irq_handler(dev_priv->engine[VCS],
1410e30e251aSVille Syrjälä 				    gt_iir[1], GEN8_VCS1_IRQ_SHIFT);
14113b3f1650SAkash Goel 		gen8_cs_irq_handler(dev_priv->engine[VCS2],
1412e30e251aSVille Syrjälä 				    gt_iir[1], GEN8_VCS2_IRQ_SHIFT);
1413e30e251aSVille Syrjälä 	}
1414e30e251aSVille Syrjälä 
1415e30e251aSVille Syrjälä 	if (gt_iir[3])
14163b3f1650SAkash Goel 		gen8_cs_irq_handler(dev_priv->engine[VECS],
1417e30e251aSVille Syrjälä 				    gt_iir[3], GEN8_VECS_IRQ_SHIFT);
1418e30e251aSVille Syrjälä 
1419e30e251aSVille Syrjälä 	if (gt_iir[2] & dev_priv->pm_rps_events)
1420e30e251aSVille Syrjälä 		gen6_rps_irq_handler(dev_priv, gt_iir[2]);
142126705e20SSagar Arun Kamble 
142226705e20SSagar Arun Kamble 	if (gt_iir[2] & dev_priv->pm_guc_events)
142326705e20SSagar Arun Kamble 		gen9_guc_irq_handler(dev_priv, gt_iir[2]);
1424e30e251aSVille Syrjälä }
1425e30e251aSVille Syrjälä 
142663c88d22SImre Deak static bool bxt_port_hotplug_long_detect(enum port port, u32 val)
142763c88d22SImre Deak {
142863c88d22SImre Deak 	switch (port) {
142963c88d22SImre Deak 	case PORT_A:
1430195baa06SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
143163c88d22SImre Deak 	case PORT_B:
143263c88d22SImre Deak 		return val & PORTB_HOTPLUG_LONG_DETECT;
143363c88d22SImre Deak 	case PORT_C:
143463c88d22SImre Deak 		return val & PORTC_HOTPLUG_LONG_DETECT;
143563c88d22SImre Deak 	default:
143663c88d22SImre Deak 		return false;
143763c88d22SImre Deak 	}
143863c88d22SImre Deak }
143963c88d22SImre Deak 
14406dbf30ceSVille Syrjälä static bool spt_port_hotplug2_long_detect(enum port port, u32 val)
14416dbf30ceSVille Syrjälä {
14426dbf30ceSVille Syrjälä 	switch (port) {
14436dbf30ceSVille Syrjälä 	case PORT_E:
14446dbf30ceSVille Syrjälä 		return val & PORTE_HOTPLUG_LONG_DETECT;
14456dbf30ceSVille Syrjälä 	default:
14466dbf30ceSVille Syrjälä 		return false;
14476dbf30ceSVille Syrjälä 	}
14486dbf30ceSVille Syrjälä }
14496dbf30ceSVille Syrjälä 
145074c0b395SVille Syrjälä static bool spt_port_hotplug_long_detect(enum port port, u32 val)
145174c0b395SVille Syrjälä {
145274c0b395SVille Syrjälä 	switch (port) {
145374c0b395SVille Syrjälä 	case PORT_A:
145474c0b395SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
145574c0b395SVille Syrjälä 	case PORT_B:
145674c0b395SVille Syrjälä 		return val & PORTB_HOTPLUG_LONG_DETECT;
145774c0b395SVille Syrjälä 	case PORT_C:
145874c0b395SVille Syrjälä 		return val & PORTC_HOTPLUG_LONG_DETECT;
145974c0b395SVille Syrjälä 	case PORT_D:
146074c0b395SVille Syrjälä 		return val & PORTD_HOTPLUG_LONG_DETECT;
146174c0b395SVille Syrjälä 	default:
146274c0b395SVille Syrjälä 		return false;
146374c0b395SVille Syrjälä 	}
146474c0b395SVille Syrjälä }
146574c0b395SVille Syrjälä 
1466e4ce95aaSVille Syrjälä static bool ilk_port_hotplug_long_detect(enum port port, u32 val)
1467e4ce95aaSVille Syrjälä {
1468e4ce95aaSVille Syrjälä 	switch (port) {
1469e4ce95aaSVille Syrjälä 	case PORT_A:
1470e4ce95aaSVille Syrjälä 		return val & DIGITAL_PORTA_HOTPLUG_LONG_DETECT;
1471e4ce95aaSVille Syrjälä 	default:
1472e4ce95aaSVille Syrjälä 		return false;
1473e4ce95aaSVille Syrjälä 	}
1474e4ce95aaSVille Syrjälä }
1475e4ce95aaSVille Syrjälä 
1476676574dfSJani Nikula static bool pch_port_hotplug_long_detect(enum port port, u32 val)
147713cf5504SDave Airlie {
147813cf5504SDave Airlie 	switch (port) {
147913cf5504SDave Airlie 	case PORT_B:
1480676574dfSJani Nikula 		return val & PORTB_HOTPLUG_LONG_DETECT;
148113cf5504SDave Airlie 	case PORT_C:
1482676574dfSJani Nikula 		return val & PORTC_HOTPLUG_LONG_DETECT;
148313cf5504SDave Airlie 	case PORT_D:
1484676574dfSJani Nikula 		return val & PORTD_HOTPLUG_LONG_DETECT;
1485676574dfSJani Nikula 	default:
1486676574dfSJani Nikula 		return false;
148713cf5504SDave Airlie 	}
148813cf5504SDave Airlie }
148913cf5504SDave Airlie 
1490676574dfSJani Nikula static bool i9xx_port_hotplug_long_detect(enum port port, u32 val)
149113cf5504SDave Airlie {
149213cf5504SDave Airlie 	switch (port) {
149313cf5504SDave Airlie 	case PORT_B:
1494676574dfSJani Nikula 		return val & PORTB_HOTPLUG_INT_LONG_PULSE;
149513cf5504SDave Airlie 	case PORT_C:
1496676574dfSJani Nikula 		return val & PORTC_HOTPLUG_INT_LONG_PULSE;
149713cf5504SDave Airlie 	case PORT_D:
1498676574dfSJani Nikula 		return val & PORTD_HOTPLUG_INT_LONG_PULSE;
1499676574dfSJani Nikula 	default:
1500676574dfSJani Nikula 		return false;
150113cf5504SDave Airlie 	}
150213cf5504SDave Airlie }
150313cf5504SDave Airlie 
150442db67d6SVille Syrjälä /*
150542db67d6SVille Syrjälä  * Get a bit mask of pins that have triggered, and which ones may be long.
150642db67d6SVille Syrjälä  * This can be called multiple times with the same masks to accumulate
150742db67d6SVille Syrjälä  * hotplug detection results from several registers.
150842db67d6SVille Syrjälä  *
150942db67d6SVille Syrjälä  * Note that the caller is expected to zero out the masks initially.
151042db67d6SVille Syrjälä  */
1511fd63e2a9SImre Deak static void intel_get_hpd_pins(u32 *pin_mask, u32 *long_mask,
15128c841e57SJani Nikula 			     u32 hotplug_trigger, u32 dig_hotplug_reg,
1513fd63e2a9SImre Deak 			     const u32 hpd[HPD_NUM_PINS],
1514fd63e2a9SImre Deak 			     bool long_pulse_detect(enum port port, u32 val))
1515676574dfSJani Nikula {
15168c841e57SJani Nikula 	enum port port;
1517676574dfSJani Nikula 	int i;
1518676574dfSJani Nikula 
1519676574dfSJani Nikula 	for_each_hpd_pin(i) {
15208c841e57SJani Nikula 		if ((hpd[i] & hotplug_trigger) == 0)
15218c841e57SJani Nikula 			continue;
15228c841e57SJani Nikula 
1523676574dfSJani Nikula 		*pin_mask |= BIT(i);
1524676574dfSJani Nikula 
1525cc24fcdcSImre Deak 		if (!intel_hpd_pin_to_port(i, &port))
1526cc24fcdcSImre Deak 			continue;
1527cc24fcdcSImre Deak 
1528fd63e2a9SImre Deak 		if (long_pulse_detect(port, dig_hotplug_reg))
1529676574dfSJani Nikula 			*long_mask |= BIT(i);
1530676574dfSJani Nikula 	}
1531676574dfSJani Nikula 
1532676574dfSJani Nikula 	DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x, dig 0x%08x, pins 0x%08x\n",
1533676574dfSJani Nikula 			 hotplug_trigger, dig_hotplug_reg, *pin_mask);
1534676574dfSJani Nikula 
1535676574dfSJani Nikula }
1536676574dfSJani Nikula 
153791d14251STvrtko Ursulin static void gmbus_irq_handler(struct drm_i915_private *dev_priv)
1538515ac2bbSDaniel Vetter {
153928c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1540515ac2bbSDaniel Vetter }
1541515ac2bbSDaniel Vetter 
154291d14251STvrtko Ursulin static void dp_aux_irq_handler(struct drm_i915_private *dev_priv)
1543ce99c256SDaniel Vetter {
15449ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1545ce99c256SDaniel Vetter }
1546ce99c256SDaniel Vetter 
15478bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
154891d14251STvrtko Ursulin static void display_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
154991d14251STvrtko Ursulin 					 enum pipe pipe,
1550eba94eb9SDaniel Vetter 					 uint32_t crc0, uint32_t crc1,
1551eba94eb9SDaniel Vetter 					 uint32_t crc2, uint32_t crc3,
15528bc5e955SDaniel Vetter 					 uint32_t crc4)
15538bf1e9f1SShuang He {
15548bf1e9f1SShuang He 	struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
15558bf1e9f1SShuang He 	struct intel_pipe_crc_entry *entry;
1556ac2300d4SDamien Lespiau 	int head, tail;
1557b2c88f5bSDamien Lespiau 
1558d538bbdfSDamien Lespiau 	spin_lock(&pipe_crc->lock);
1559d538bbdfSDamien Lespiau 
15600c912c79SDamien Lespiau 	if (!pipe_crc->entries) {
1561d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
156234273620SDaniel Vetter 		DRM_DEBUG_KMS("spurious interrupt\n");
15630c912c79SDamien Lespiau 		return;
15640c912c79SDamien Lespiau 	}
15650c912c79SDamien Lespiau 
1566d538bbdfSDamien Lespiau 	head = pipe_crc->head;
1567d538bbdfSDamien Lespiau 	tail = pipe_crc->tail;
1568b2c88f5bSDamien Lespiau 
1569b2c88f5bSDamien Lespiau 	if (CIRC_SPACE(head, tail, INTEL_PIPE_CRC_ENTRIES_NR) < 1) {
1570d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
1571b2c88f5bSDamien Lespiau 		DRM_ERROR("CRC buffer overflowing\n");
1572b2c88f5bSDamien Lespiau 		return;
1573b2c88f5bSDamien Lespiau 	}
1574b2c88f5bSDamien Lespiau 
1575b2c88f5bSDamien Lespiau 	entry = &pipe_crc->entries[head];
15768bf1e9f1SShuang He 
157791c8a326SChris Wilson 	entry->frame = dev_priv->drm.driver->get_vblank_counter(&dev_priv->drm,
157891d14251STvrtko Ursulin 								 pipe);
1579eba94eb9SDaniel Vetter 	entry->crc[0] = crc0;
1580eba94eb9SDaniel Vetter 	entry->crc[1] = crc1;
1581eba94eb9SDaniel Vetter 	entry->crc[2] = crc2;
1582eba94eb9SDaniel Vetter 	entry->crc[3] = crc3;
1583eba94eb9SDaniel Vetter 	entry->crc[4] = crc4;
1584b2c88f5bSDamien Lespiau 
1585b2c88f5bSDamien Lespiau 	head = (head + 1) & (INTEL_PIPE_CRC_ENTRIES_NR - 1);
1586d538bbdfSDamien Lespiau 	pipe_crc->head = head;
1587d538bbdfSDamien Lespiau 
1588d538bbdfSDamien Lespiau 	spin_unlock(&pipe_crc->lock);
158907144428SDamien Lespiau 
159007144428SDamien Lespiau 	wake_up_interruptible(&pipe_crc->wq);
15918bf1e9f1SShuang He }
1592277de95eSDaniel Vetter #else
1593277de95eSDaniel Vetter static inline void
159491d14251STvrtko Ursulin display_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
159591d14251STvrtko Ursulin 			     enum pipe pipe,
1596277de95eSDaniel Vetter 			     uint32_t crc0, uint32_t crc1,
1597277de95eSDaniel Vetter 			     uint32_t crc2, uint32_t crc3,
1598277de95eSDaniel Vetter 			     uint32_t crc4) {}
1599277de95eSDaniel Vetter #endif
1600eba94eb9SDaniel Vetter 
1601277de95eSDaniel Vetter 
160291d14251STvrtko Ursulin static void hsw_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
160391d14251STvrtko Ursulin 				     enum pipe pipe)
16045a69b89fSDaniel Vetter {
160591d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
16065a69b89fSDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
16075a69b89fSDaniel Vetter 				     0, 0, 0, 0);
16085a69b89fSDaniel Vetter }
16095a69b89fSDaniel Vetter 
161091d14251STvrtko Ursulin static void ivb_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
161191d14251STvrtko Ursulin 				     enum pipe pipe)
1612eba94eb9SDaniel Vetter {
161391d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
1614eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1615eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1616eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1617eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
16188bc5e955SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1619eba94eb9SDaniel Vetter }
16205b3a856bSDaniel Vetter 
162191d14251STvrtko Ursulin static void i9xx_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
162291d14251STvrtko Ursulin 				      enum pipe pipe)
16235b3a856bSDaniel Vetter {
16240b5c5ed0SDaniel Vetter 	uint32_t res1, res2;
16250b5c5ed0SDaniel Vetter 
162691d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 3)
16270b5c5ed0SDaniel Vetter 		res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
16280b5c5ed0SDaniel Vetter 	else
16290b5c5ed0SDaniel Vetter 		res1 = 0;
16300b5c5ed0SDaniel Vetter 
163191d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
16320b5c5ed0SDaniel Vetter 		res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
16330b5c5ed0SDaniel Vetter 	else
16340b5c5ed0SDaniel Vetter 		res2 = 0;
16355b3a856bSDaniel Vetter 
163691d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
16370b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_RED(pipe)),
16380b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_GREEN(pipe)),
16390b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_BLUE(pipe)),
16400b5c5ed0SDaniel Vetter 				     res1, res2);
16415b3a856bSDaniel Vetter }
16428bf1e9f1SShuang He 
16431403c0d4SPaulo Zanoni /* The RPS events need forcewake, so we add them to a work queue and mask their
16441403c0d4SPaulo Zanoni  * IMR bits until the work is done. Other interrupts can be processed without
16451403c0d4SPaulo Zanoni  * the work queue. */
16461403c0d4SPaulo Zanoni static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1647baf02a1fSBen Widawsky {
1648a6706b45SDeepak S 	if (pm_iir & dev_priv->pm_rps_events) {
164959cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
1650f4e9af4fSAkash Goel 		gen6_mask_pm_irq(dev_priv, pm_iir & dev_priv->pm_rps_events);
1651d4d70aa5SImre Deak 		if (dev_priv->rps.interrupts_enabled) {
1652d4d70aa5SImre Deak 			dev_priv->rps.pm_iir |= pm_iir & dev_priv->pm_rps_events;
1653c33d247dSChris Wilson 			schedule_work(&dev_priv->rps.work);
165441a05a3aSDaniel Vetter 		}
1655d4d70aa5SImre Deak 		spin_unlock(&dev_priv->irq_lock);
1656d4d70aa5SImre Deak 	}
1657baf02a1fSBen Widawsky 
1658c9a9a268SImre Deak 	if (INTEL_INFO(dev_priv)->gen >= 8)
1659c9a9a268SImre Deak 		return;
1660c9a9a268SImre Deak 
16612d1fe073SJoonas Lahtinen 	if (HAS_VEBOX(dev_priv)) {
166212638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
16633b3f1650SAkash Goel 			notify_ring(dev_priv->engine[VECS]);
166412638c57SBen Widawsky 
1665aaecdf61SDaniel Vetter 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT)
1666aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, pm_iir 0x%08x\n", pm_iir);
166712638c57SBen Widawsky 	}
16681403c0d4SPaulo Zanoni }
1669baf02a1fSBen Widawsky 
167026705e20SSagar Arun Kamble static void gen9_guc_irq_handler(struct drm_i915_private *dev_priv, u32 gt_iir)
167126705e20SSagar Arun Kamble {
167226705e20SSagar Arun Kamble 	if (gt_iir & GEN9_GUC_TO_HOST_INT_EVENT) {
16734100b2abSSagar Arun Kamble 		/* Sample the log buffer flush related bits & clear them out now
16744100b2abSSagar Arun Kamble 		 * itself from the message identity register to minimize the
16754100b2abSSagar Arun Kamble 		 * probability of losing a flush interrupt, when there are back
16764100b2abSSagar Arun Kamble 		 * to back flush interrupts.
16774100b2abSSagar Arun Kamble 		 * There can be a new flush interrupt, for different log buffer
16784100b2abSSagar Arun Kamble 		 * type (like for ISR), whilst Host is handling one (for DPC).
16794100b2abSSagar Arun Kamble 		 * Since same bit is used in message register for ISR & DPC, it
16804100b2abSSagar Arun Kamble 		 * could happen that GuC sets the bit for 2nd interrupt but Host
16814100b2abSSagar Arun Kamble 		 * clears out the bit on handling the 1st interrupt.
16824100b2abSSagar Arun Kamble 		 */
16834100b2abSSagar Arun Kamble 		u32 msg, flush;
16844100b2abSSagar Arun Kamble 
16854100b2abSSagar Arun Kamble 		msg = I915_READ(SOFT_SCRATCH(15));
16864100b2abSSagar Arun Kamble 		flush = msg & (GUC2HOST_MSG_CRASH_DUMP_POSTED |
16874100b2abSSagar Arun Kamble 			       GUC2HOST_MSG_FLUSH_LOG_BUFFER);
16884100b2abSSagar Arun Kamble 		if (flush) {
16894100b2abSSagar Arun Kamble 			/* Clear the message bits that are handled */
16904100b2abSSagar Arun Kamble 			I915_WRITE(SOFT_SCRATCH(15), msg & ~flush);
16914100b2abSSagar Arun Kamble 
16924100b2abSSagar Arun Kamble 			/* Handle flush interrupt in bottom half */
16934100b2abSSagar Arun Kamble 			queue_work(dev_priv->guc.log.flush_wq,
16944100b2abSSagar Arun Kamble 				   &dev_priv->guc.log.flush_work);
16955aa1ee4bSAkash Goel 
16965aa1ee4bSAkash Goel 			dev_priv->guc.log.flush_interrupt_count++;
16974100b2abSSagar Arun Kamble 		} else {
16984100b2abSSagar Arun Kamble 			/* Not clearing of unhandled event bits won't result in
16994100b2abSSagar Arun Kamble 			 * re-triggering of the interrupt.
17004100b2abSSagar Arun Kamble 			 */
17014100b2abSSagar Arun Kamble 		}
170226705e20SSagar Arun Kamble 	}
170326705e20SSagar Arun Kamble }
170426705e20SSagar Arun Kamble 
17055a21b665SDaniel Vetter static bool intel_pipe_handle_vblank(struct drm_i915_private *dev_priv,
170691d14251STvrtko Ursulin 				     enum pipe pipe)
17078d7849dbSVille Syrjälä {
17085a21b665SDaniel Vetter 	bool ret;
17095a21b665SDaniel Vetter 
171091c8a326SChris Wilson 	ret = drm_handle_vblank(&dev_priv->drm, pipe);
17115a21b665SDaniel Vetter 	if (ret)
171251cbaf01SMaarten Lankhorst 		intel_finish_page_flip_mmio(dev_priv, pipe);
17135a21b665SDaniel Vetter 
17145a21b665SDaniel Vetter 	return ret;
17158d7849dbSVille Syrjälä }
17168d7849dbSVille Syrjälä 
171791d14251STvrtko Ursulin static void valleyview_pipestat_irq_ack(struct drm_i915_private *dev_priv,
171891d14251STvrtko Ursulin 					u32 iir, u32 pipe_stats[I915_MAX_PIPES])
17197e231dbeSJesse Barnes {
17207e231dbeSJesse Barnes 	int pipe;
17217e231dbeSJesse Barnes 
172258ead0d7SImre Deak 	spin_lock(&dev_priv->irq_lock);
17231ca993d2SVille Syrjälä 
17241ca993d2SVille Syrjälä 	if (!dev_priv->display_irqs_enabled) {
17251ca993d2SVille Syrjälä 		spin_unlock(&dev_priv->irq_lock);
17261ca993d2SVille Syrjälä 		return;
17271ca993d2SVille Syrjälä 	}
17281ca993d2SVille Syrjälä 
1729055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1730f0f59a00SVille Syrjälä 		i915_reg_t reg;
1731bbb5eebfSDaniel Vetter 		u32 mask, iir_bit = 0;
173291d181ddSImre Deak 
1733bbb5eebfSDaniel Vetter 		/*
1734bbb5eebfSDaniel Vetter 		 * PIPESTAT bits get signalled even when the interrupt is
1735bbb5eebfSDaniel Vetter 		 * disabled with the mask bits, and some of the status bits do
1736bbb5eebfSDaniel Vetter 		 * not generate interrupts at all (like the underrun bit). Hence
1737bbb5eebfSDaniel Vetter 		 * we need to be careful that we only handle what we want to
1738bbb5eebfSDaniel Vetter 		 * handle.
1739bbb5eebfSDaniel Vetter 		 */
17400f239f4cSDaniel Vetter 
17410f239f4cSDaniel Vetter 		/* fifo underruns are filterered in the underrun handler. */
17420f239f4cSDaniel Vetter 		mask = PIPE_FIFO_UNDERRUN_STATUS;
1743bbb5eebfSDaniel Vetter 
1744bbb5eebfSDaniel Vetter 		switch (pipe) {
1745bbb5eebfSDaniel Vetter 		case PIPE_A:
1746bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
1747bbb5eebfSDaniel Vetter 			break;
1748bbb5eebfSDaniel Vetter 		case PIPE_B:
1749bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
1750bbb5eebfSDaniel Vetter 			break;
17513278f67fSVille Syrjälä 		case PIPE_C:
17523278f67fSVille Syrjälä 			iir_bit = I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
17533278f67fSVille Syrjälä 			break;
1754bbb5eebfSDaniel Vetter 		}
1755bbb5eebfSDaniel Vetter 		if (iir & iir_bit)
1756bbb5eebfSDaniel Vetter 			mask |= dev_priv->pipestat_irq_mask[pipe];
1757bbb5eebfSDaniel Vetter 
1758bbb5eebfSDaniel Vetter 		if (!mask)
175991d181ddSImre Deak 			continue;
176091d181ddSImre Deak 
176191d181ddSImre Deak 		reg = PIPESTAT(pipe);
1762bbb5eebfSDaniel Vetter 		mask |= PIPESTAT_INT_ENABLE_MASK;
1763bbb5eebfSDaniel Vetter 		pipe_stats[pipe] = I915_READ(reg) & mask;
17647e231dbeSJesse Barnes 
17657e231dbeSJesse Barnes 		/*
17667e231dbeSJesse Barnes 		 * Clear the PIPE*STAT regs before the IIR
17677e231dbeSJesse Barnes 		 */
176891d181ddSImre Deak 		if (pipe_stats[pipe] & (PIPE_FIFO_UNDERRUN_STATUS |
176991d181ddSImre Deak 					PIPESTAT_INT_STATUS_MASK))
17707e231dbeSJesse Barnes 			I915_WRITE(reg, pipe_stats[pipe]);
17717e231dbeSJesse Barnes 	}
177258ead0d7SImre Deak 	spin_unlock(&dev_priv->irq_lock);
17732ecb8ca4SVille Syrjälä }
17742ecb8ca4SVille Syrjälä 
177591d14251STvrtko Ursulin static void valleyview_pipestat_irq_handler(struct drm_i915_private *dev_priv,
17762ecb8ca4SVille Syrjälä 					    u32 pipe_stats[I915_MAX_PIPES])
17772ecb8ca4SVille Syrjälä {
17782ecb8ca4SVille Syrjälä 	enum pipe pipe;
17797e231dbeSJesse Barnes 
1780055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
17815a21b665SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
17825a21b665SDaniel Vetter 		    intel_pipe_handle_vblank(dev_priv, pipe))
17835a21b665SDaniel Vetter 			intel_check_page_flip(dev_priv, pipe);
178431acc7f5SJesse Barnes 
17855251f04eSMaarten Lankhorst 		if (pipe_stats[pipe] & PLANE_FLIP_DONE_INT_STATUS_VLV)
178651cbaf01SMaarten Lankhorst 			intel_finish_page_flip_cs(dev_priv, pipe);
17874356d586SDaniel Vetter 
17884356d586SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
178991d14251STvrtko Ursulin 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
17902d9d2b0bSVille Syrjälä 
17911f7247c0SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
17921f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
179331acc7f5SJesse Barnes 	}
179431acc7f5SJesse Barnes 
1795c1874ed7SImre Deak 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
179691d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
1797c1874ed7SImre Deak }
1798c1874ed7SImre Deak 
17991ae3c34cSVille Syrjälä static u32 i9xx_hpd_irq_ack(struct drm_i915_private *dev_priv)
180016c6c56bSVille Syrjälä {
180116c6c56bSVille Syrjälä 	u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
180216c6c56bSVille Syrjälä 
18031ae3c34cSVille Syrjälä 	if (hotplug_status)
18043ff60f89SOscar Mateo 		I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
18051ae3c34cSVille Syrjälä 
18061ae3c34cSVille Syrjälä 	return hotplug_status;
18071ae3c34cSVille Syrjälä }
18081ae3c34cSVille Syrjälä 
180991d14251STvrtko Ursulin static void i9xx_hpd_irq_handler(struct drm_i915_private *dev_priv,
18101ae3c34cSVille Syrjälä 				 u32 hotplug_status)
18111ae3c34cSVille Syrjälä {
18121ae3c34cSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
18133ff60f89SOscar Mateo 
181491d14251STvrtko Ursulin 	if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
181591d14251STvrtko Ursulin 	    IS_CHERRYVIEW(dev_priv)) {
181616c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
181716c6c56bSVille Syrjälä 
181858f2cf24SVille Syrjälä 		if (hotplug_trigger) {
1819fd63e2a9SImre Deak 			intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
1820fd63e2a9SImre Deak 					   hotplug_trigger, hpd_status_g4x,
1821fd63e2a9SImre Deak 					   i9xx_port_hotplug_long_detect);
182258f2cf24SVille Syrjälä 
182391d14251STvrtko Ursulin 			intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
182458f2cf24SVille Syrjälä 		}
1825369712e8SJani Nikula 
1826369712e8SJani Nikula 		if (hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
182791d14251STvrtko Ursulin 			dp_aux_irq_handler(dev_priv);
182816c6c56bSVille Syrjälä 	} else {
182916c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
183016c6c56bSVille Syrjälä 
183158f2cf24SVille Syrjälä 		if (hotplug_trigger) {
1832fd63e2a9SImre Deak 			intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
18334e3d1e26SVille Syrjälä 					   hotplug_trigger, hpd_status_i915,
1834fd63e2a9SImre Deak 					   i9xx_port_hotplug_long_detect);
183591d14251STvrtko Ursulin 			intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
183616c6c56bSVille Syrjälä 		}
18373ff60f89SOscar Mateo 	}
183858f2cf24SVille Syrjälä }
183916c6c56bSVille Syrjälä 
1840c1874ed7SImre Deak static irqreturn_t valleyview_irq_handler(int irq, void *arg)
1841c1874ed7SImre Deak {
184245a83f84SDaniel Vetter 	struct drm_device *dev = arg;
1843fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
1844c1874ed7SImre Deak 	irqreturn_t ret = IRQ_NONE;
1845c1874ed7SImre Deak 
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 
18521e1cace9SVille Syrjälä 	do {
18536e814800SVille Syrjälä 		u32 iir, gt_iir, pm_iir;
18542ecb8ca4SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
18551ae3c34cSVille Syrjälä 		u32 hotplug_status = 0;
1856a5e485a9SVille Syrjälä 		u32 ier = 0;
18573ff60f89SOscar Mateo 
1858c1874ed7SImre Deak 		gt_iir = I915_READ(GTIIR);
1859c1874ed7SImre Deak 		pm_iir = I915_READ(GEN6_PMIIR);
18603ff60f89SOscar Mateo 		iir = I915_READ(VLV_IIR);
1861c1874ed7SImre Deak 
1862c1874ed7SImre Deak 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
18631e1cace9SVille Syrjälä 			break;
1864c1874ed7SImre Deak 
1865c1874ed7SImre Deak 		ret = IRQ_HANDLED;
1866c1874ed7SImre Deak 
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ä 		 *      (((GT_IIR & GT_IER) || (GEN6_PMIIR & GEN6_PMIER)) &&
1872a5e485a9SVille Syrjälä 		 *       (VLV_MASTER_IER & MASTER_INTERRUPT_ENABLE)));
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 MASTER_INTERRUPT_ENABLE 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, GT_IIR, GEN6_PMIIR
1878a5e485a9SVille Syrjälä 		 * bits this time around.
1879a5e485a9SVille Syrjälä 		 */
18804a0a0202SVille Syrjälä 		I915_WRITE(VLV_MASTER_IER, 0);
1881a5e485a9SVille Syrjälä 		ier = I915_READ(VLV_IER);
1882a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, 0);
18834a0a0202SVille Syrjälä 
18844a0a0202SVille Syrjälä 		if (gt_iir)
18854a0a0202SVille Syrjälä 			I915_WRITE(GTIIR, gt_iir);
18864a0a0202SVille Syrjälä 		if (pm_iir)
18874a0a0202SVille Syrjälä 			I915_WRITE(GEN6_PMIIR, pm_iir);
18884a0a0202SVille Syrjälä 
18897ce4d1f2SVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
18901ae3c34cSVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
18917ce4d1f2SVille Syrjälä 
18923ff60f89SOscar Mateo 		/* Call regardless, as some status bits might not be
18933ff60f89SOscar Mateo 		 * signalled in iir */
189491d14251STvrtko Ursulin 		valleyview_pipestat_irq_ack(dev_priv, iir, pipe_stats);
18957ce4d1f2SVille Syrjälä 
18967ce4d1f2SVille Syrjälä 		/*
18977ce4d1f2SVille Syrjälä 		 * VLV_IIR is single buffered, and reflects the level
18987ce4d1f2SVille Syrjälä 		 * from PIPESTAT/PORT_HOTPLUG_STAT, hence clear it last.
18997ce4d1f2SVille Syrjälä 		 */
19007ce4d1f2SVille Syrjälä 		if (iir)
19017ce4d1f2SVille Syrjälä 			I915_WRITE(VLV_IIR, iir);
19024a0a0202SVille Syrjälä 
1903a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, ier);
19044a0a0202SVille Syrjälä 		I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
19054a0a0202SVille Syrjälä 		POSTING_READ(VLV_MASTER_IER);
19061ae3c34cSVille Syrjälä 
190752894874SVille Syrjälä 		if (gt_iir)
1908261e40b8SVille Syrjälä 			snb_gt_irq_handler(dev_priv, gt_iir);
190952894874SVille Syrjälä 		if (pm_iir)
191052894874SVille Syrjälä 			gen6_rps_irq_handler(dev_priv, pm_iir);
191152894874SVille Syrjälä 
19121ae3c34cSVille Syrjälä 		if (hotplug_status)
191391d14251STvrtko Ursulin 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
19142ecb8ca4SVille Syrjälä 
191591d14251STvrtko Ursulin 		valleyview_pipestat_irq_handler(dev_priv, pipe_stats);
19161e1cace9SVille Syrjälä 	} while (0);
19177e231dbeSJesse Barnes 
19181f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
19191f814dacSImre Deak 
19207e231dbeSJesse Barnes 	return ret;
19217e231dbeSJesse Barnes }
19227e231dbeSJesse Barnes 
192343f328d7SVille Syrjälä static irqreturn_t cherryview_irq_handler(int irq, void *arg)
192443f328d7SVille Syrjälä {
192545a83f84SDaniel Vetter 	struct drm_device *dev = arg;
1926fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
192743f328d7SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
192843f328d7SVille Syrjälä 
19292dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
19302dd2a883SImre Deak 		return IRQ_NONE;
19312dd2a883SImre Deak 
19321f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
19331f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
19341f814dacSImre Deak 
1935579de73bSChris Wilson 	do {
19366e814800SVille Syrjälä 		u32 master_ctl, iir;
1937e30e251aSVille Syrjälä 		u32 gt_iir[4] = {};
19382ecb8ca4SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
19391ae3c34cSVille Syrjälä 		u32 hotplug_status = 0;
1940a5e485a9SVille Syrjälä 		u32 ier = 0;
1941a5e485a9SVille Syrjälä 
19428e5fd599SVille Syrjälä 		master_ctl = I915_READ(GEN8_MASTER_IRQ) & ~GEN8_MASTER_IRQ_CONTROL;
19433278f67fSVille Syrjälä 		iir = I915_READ(VLV_IIR);
19443278f67fSVille Syrjälä 
19453278f67fSVille Syrjälä 		if (master_ctl == 0 && iir == 0)
19468e5fd599SVille Syrjälä 			break;
194743f328d7SVille Syrjälä 
194827b6c122SOscar Mateo 		ret = IRQ_HANDLED;
194927b6c122SOscar Mateo 
1950a5e485a9SVille Syrjälä 		/*
1951a5e485a9SVille Syrjälä 		 * Theory on interrupt generation, based on empirical evidence:
1952a5e485a9SVille Syrjälä 		 *
1953a5e485a9SVille Syrjälä 		 * x = ((VLV_IIR & VLV_IER) ||
1954a5e485a9SVille Syrjälä 		 *      ((GEN8_MASTER_IRQ & ~GEN8_MASTER_IRQ_CONTROL) &&
1955a5e485a9SVille Syrjälä 		 *       (GEN8_MASTER_IRQ & GEN8_MASTER_IRQ_CONTROL)));
1956a5e485a9SVille Syrjälä 		 *
1957a5e485a9SVille Syrjälä 		 * A CPU interrupt will only be raised when 'x' has a 0->1 edge.
1958a5e485a9SVille Syrjälä 		 * Hence we clear GEN8_MASTER_IRQ_CONTROL and VLV_IER to
1959a5e485a9SVille Syrjälä 		 * guarantee the CPU interrupt will be raised again even if we
1960a5e485a9SVille Syrjälä 		 * don't end up clearing all the VLV_IIR and GEN8_MASTER_IRQ_CONTROL
1961a5e485a9SVille Syrjälä 		 * bits this time around.
1962a5e485a9SVille Syrjälä 		 */
196343f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, 0);
1964a5e485a9SVille Syrjälä 		ier = I915_READ(VLV_IER);
1965a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, 0);
196643f328d7SVille Syrjälä 
1967e30e251aSVille Syrjälä 		gen8_gt_irq_ack(dev_priv, master_ctl, gt_iir);
196827b6c122SOscar Mateo 
196927b6c122SOscar Mateo 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
19701ae3c34cSVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
197143f328d7SVille Syrjälä 
197227b6c122SOscar Mateo 		/* Call regardless, as some status bits might not be
197327b6c122SOscar Mateo 		 * signalled in iir */
197491d14251STvrtko Ursulin 		valleyview_pipestat_irq_ack(dev_priv, iir, pipe_stats);
197543f328d7SVille Syrjälä 
19767ce4d1f2SVille Syrjälä 		/*
19777ce4d1f2SVille Syrjälä 		 * VLV_IIR is single buffered, and reflects the level
19787ce4d1f2SVille Syrjälä 		 * from PIPESTAT/PORT_HOTPLUG_STAT, hence clear it last.
19797ce4d1f2SVille Syrjälä 		 */
19807ce4d1f2SVille Syrjälä 		if (iir)
19817ce4d1f2SVille Syrjälä 			I915_WRITE(VLV_IIR, iir);
19827ce4d1f2SVille Syrjälä 
1983a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, ier);
1984e5328c43SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
198543f328d7SVille Syrjälä 		POSTING_READ(GEN8_MASTER_IRQ);
19861ae3c34cSVille Syrjälä 
1987e30e251aSVille Syrjälä 		gen8_gt_irq_handler(dev_priv, gt_iir);
1988e30e251aSVille Syrjälä 
19891ae3c34cSVille Syrjälä 		if (hotplug_status)
199091d14251STvrtko Ursulin 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
19912ecb8ca4SVille Syrjälä 
199291d14251STvrtko Ursulin 		valleyview_pipestat_irq_handler(dev_priv, pipe_stats);
1993579de73bSChris Wilson 	} while (0);
19943278f67fSVille Syrjälä 
19951f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
19961f814dacSImre Deak 
199743f328d7SVille Syrjälä 	return ret;
199843f328d7SVille Syrjälä }
199943f328d7SVille Syrjälä 
200091d14251STvrtko Ursulin static void ibx_hpd_irq_handler(struct drm_i915_private *dev_priv,
200191d14251STvrtko Ursulin 				u32 hotplug_trigger,
200240e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2003776ad806SJesse Barnes {
200442db67d6SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2005776ad806SJesse Barnes 
20066a39d7c9SJani Nikula 	/*
20076a39d7c9SJani Nikula 	 * Somehow the PCH doesn't seem to really ack the interrupt to the CPU
20086a39d7c9SJani Nikula 	 * unless we touch the hotplug register, even if hotplug_trigger is
20096a39d7c9SJani Nikula 	 * zero. Not acking leads to "The master control interrupt lied (SDE)!"
20106a39d7c9SJani Nikula 	 * errors.
20116a39d7c9SJani Nikula 	 */
201213cf5504SDave Airlie 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
20136a39d7c9SJani Nikula 	if (!hotplug_trigger) {
20146a39d7c9SJani Nikula 		u32 mask = PORTA_HOTPLUG_STATUS_MASK |
20156a39d7c9SJani Nikula 			PORTD_HOTPLUG_STATUS_MASK |
20166a39d7c9SJani Nikula 			PORTC_HOTPLUG_STATUS_MASK |
20176a39d7c9SJani Nikula 			PORTB_HOTPLUG_STATUS_MASK;
20186a39d7c9SJani Nikula 		dig_hotplug_reg &= ~mask;
20196a39d7c9SJani Nikula 	}
20206a39d7c9SJani Nikula 
202113cf5504SDave Airlie 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
20226a39d7c9SJani Nikula 	if (!hotplug_trigger)
20236a39d7c9SJani Nikula 		return;
202413cf5504SDave Airlie 
2025fd63e2a9SImre Deak 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
202640e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2027fd63e2a9SImre Deak 			   pch_port_hotplug_long_detect);
202840e56410SVille Syrjälä 
202991d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2030aaf5ec2eSSonika Jindal }
203191d131d2SDaniel Vetter 
203291d14251STvrtko Ursulin static void ibx_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
203340e56410SVille Syrjälä {
203440e56410SVille Syrjälä 	int pipe;
203540e56410SVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
203640e56410SVille Syrjälä 
203791d14251STvrtko Ursulin 	ibx_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ibx);
203840e56410SVille Syrjälä 
2039cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
2040cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
2041776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
2042cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
2043cfc33bf7SVille Syrjälä 				 port_name(port));
2044cfc33bf7SVille Syrjälä 	}
2045776ad806SJesse Barnes 
2046ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
204791d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
2048ce99c256SDaniel Vetter 
2049776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
205091d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
2051776ad806SJesse Barnes 
2052776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
2053776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
2054776ad806SJesse Barnes 
2055776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
2056776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
2057776ad806SJesse Barnes 
2058776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
2059776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
2060776ad806SJesse Barnes 
20619db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
2062055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
20639db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
20649db4a9c7SJesse Barnes 					 pipe_name(pipe),
20659db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
2066776ad806SJesse Barnes 
2067776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
2068776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
2069776ad806SJesse Barnes 
2070776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
2071776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
2072776ad806SJesse Barnes 
2073776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
20741f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
20758664281bSPaulo Zanoni 
20768664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
20771f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
20788664281bSPaulo Zanoni }
20798664281bSPaulo Zanoni 
208091d14251STvrtko Ursulin static void ivb_err_int_handler(struct drm_i915_private *dev_priv)
20818664281bSPaulo Zanoni {
20828664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
20835a69b89fSDaniel Vetter 	enum pipe pipe;
20848664281bSPaulo Zanoni 
2085de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
2086de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
2087de032bf4SPaulo Zanoni 
2088055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
20891f7247c0SDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe))
20901f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
20918664281bSPaulo Zanoni 
20925a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
209391d14251STvrtko Ursulin 			if (IS_IVYBRIDGE(dev_priv))
209491d14251STvrtko Ursulin 				ivb_pipe_crc_irq_handler(dev_priv, pipe);
20955a69b89fSDaniel Vetter 			else
209691d14251STvrtko Ursulin 				hsw_pipe_crc_irq_handler(dev_priv, pipe);
20975a69b89fSDaniel Vetter 		}
20985a69b89fSDaniel Vetter 	}
20998bf1e9f1SShuang He 
21008664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
21018664281bSPaulo Zanoni }
21028664281bSPaulo Zanoni 
210391d14251STvrtko Ursulin static void cpt_serr_int_handler(struct drm_i915_private *dev_priv)
21048664281bSPaulo Zanoni {
21058664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
21068664281bSPaulo Zanoni 
2107de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
2108de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
2109de032bf4SPaulo Zanoni 
21108664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
21111f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
21128664281bSPaulo Zanoni 
21138664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
21141f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
21158664281bSPaulo Zanoni 
21168664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
21171f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_C);
21188664281bSPaulo Zanoni 
21198664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
2120776ad806SJesse Barnes }
2121776ad806SJesse Barnes 
212291d14251STvrtko Ursulin static void cpt_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
212323e81d69SAdam Jackson {
212423e81d69SAdam Jackson 	int pipe;
21256dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
2126aaf5ec2eSSonika Jindal 
212791d14251STvrtko Ursulin 	ibx_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_cpt);
212891d131d2SDaniel Vetter 
2129cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
2130cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
213123e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
2132cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
2133cfc33bf7SVille Syrjälä 				 port_name(port));
2134cfc33bf7SVille Syrjälä 	}
213523e81d69SAdam Jackson 
213623e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
213791d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
213823e81d69SAdam Jackson 
213923e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
214091d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
214123e81d69SAdam Jackson 
214223e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
214323e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
214423e81d69SAdam Jackson 
214523e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
214623e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
214723e81d69SAdam Jackson 
214823e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
2149055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
215023e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
215123e81d69SAdam Jackson 					 pipe_name(pipe),
215223e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
21538664281bSPaulo Zanoni 
21548664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
215591d14251STvrtko Ursulin 		cpt_serr_int_handler(dev_priv);
215623e81d69SAdam Jackson }
215723e81d69SAdam Jackson 
215891d14251STvrtko Ursulin static void spt_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
21596dbf30ceSVille Syrjälä {
21606dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_SPT &
21616dbf30ceSVille Syrjälä 		~SDE_PORTE_HOTPLUG_SPT;
21626dbf30ceSVille Syrjälä 	u32 hotplug2_trigger = pch_iir & SDE_PORTE_HOTPLUG_SPT;
21636dbf30ceSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
21646dbf30ceSVille Syrjälä 
21656dbf30ceSVille Syrjälä 	if (hotplug_trigger) {
21666dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
21676dbf30ceSVille Syrjälä 
21686dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
21696dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
21706dbf30ceSVille Syrjälä 
21716dbf30ceSVille Syrjälä 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
21726dbf30ceSVille Syrjälä 				   dig_hotplug_reg, hpd_spt,
217374c0b395SVille Syrjälä 				   spt_port_hotplug_long_detect);
21746dbf30ceSVille Syrjälä 	}
21756dbf30ceSVille Syrjälä 
21766dbf30ceSVille Syrjälä 	if (hotplug2_trigger) {
21776dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
21786dbf30ceSVille Syrjälä 
21796dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG2);
21806dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG2, dig_hotplug_reg);
21816dbf30ceSVille Syrjälä 
21826dbf30ceSVille Syrjälä 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug2_trigger,
21836dbf30ceSVille Syrjälä 				   dig_hotplug_reg, hpd_spt,
21846dbf30ceSVille Syrjälä 				   spt_port_hotplug2_long_detect);
21856dbf30ceSVille Syrjälä 	}
21866dbf30ceSVille Syrjälä 
21876dbf30ceSVille Syrjälä 	if (pin_mask)
218891d14251STvrtko Ursulin 		intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
21896dbf30ceSVille Syrjälä 
21906dbf30ceSVille Syrjälä 	if (pch_iir & SDE_GMBUS_CPT)
219191d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
21926dbf30ceSVille Syrjälä }
21936dbf30ceSVille Syrjälä 
219491d14251STvrtko Ursulin static void ilk_hpd_irq_handler(struct drm_i915_private *dev_priv,
219591d14251STvrtko Ursulin 				u32 hotplug_trigger,
219640e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2197c008bc6eSPaulo Zanoni {
2198e4ce95aaSVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2199e4ce95aaSVille Syrjälä 
2200e4ce95aaSVille Syrjälä 	dig_hotplug_reg = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
2201e4ce95aaSVille Syrjälä 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, dig_hotplug_reg);
2202e4ce95aaSVille Syrjälä 
2203e4ce95aaSVille Syrjälä 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
220440e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2205e4ce95aaSVille Syrjälä 			   ilk_port_hotplug_long_detect);
220640e56410SVille Syrjälä 
220791d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2208e4ce95aaSVille Syrjälä }
2209c008bc6eSPaulo Zanoni 
221091d14251STvrtko Ursulin static void ilk_display_irq_handler(struct drm_i915_private *dev_priv,
221191d14251STvrtko Ursulin 				    u32 de_iir)
221240e56410SVille Syrjälä {
221340e56410SVille Syrjälä 	enum pipe pipe;
221440e56410SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG;
221540e56410SVille Syrjälä 
221640e56410SVille Syrjälä 	if (hotplug_trigger)
221791d14251STvrtko Ursulin 		ilk_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ilk);
221840e56410SVille Syrjälä 
2219c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
222091d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
2221c008bc6eSPaulo Zanoni 
2222c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
222391d14251STvrtko Ursulin 		intel_opregion_asle_intr(dev_priv);
2224c008bc6eSPaulo Zanoni 
2225c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
2226c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
2227c008bc6eSPaulo Zanoni 
2228055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
22295a21b665SDaniel Vetter 		if (de_iir & DE_PIPE_VBLANK(pipe) &&
22305a21b665SDaniel Vetter 		    intel_pipe_handle_vblank(dev_priv, pipe))
22315a21b665SDaniel Vetter 			intel_check_page_flip(dev_priv, pipe);
2232c008bc6eSPaulo Zanoni 
223340da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
22341f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
2235c008bc6eSPaulo Zanoni 
223640da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
223791d14251STvrtko Ursulin 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
22385b3a856bSDaniel Vetter 
223940da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
22405251f04eSMaarten Lankhorst 		if (de_iir & DE_PLANE_FLIP_DONE(pipe))
224151cbaf01SMaarten Lankhorst 			intel_finish_page_flip_cs(dev_priv, pipe);
2242c008bc6eSPaulo Zanoni 	}
2243c008bc6eSPaulo Zanoni 
2244c008bc6eSPaulo Zanoni 	/* check event from PCH */
2245c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
2246c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
2247c008bc6eSPaulo Zanoni 
224891d14251STvrtko Ursulin 		if (HAS_PCH_CPT(dev_priv))
224991d14251STvrtko Ursulin 			cpt_irq_handler(dev_priv, pch_iir);
2250c008bc6eSPaulo Zanoni 		else
225191d14251STvrtko Ursulin 			ibx_irq_handler(dev_priv, pch_iir);
2252c008bc6eSPaulo Zanoni 
2253c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
2254c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
2255c008bc6eSPaulo Zanoni 	}
2256c008bc6eSPaulo Zanoni 
225791d14251STvrtko Ursulin 	if (IS_GEN5(dev_priv) && de_iir & DE_PCU_EVENT)
225891d14251STvrtko Ursulin 		ironlake_rps_change_irq_handler(dev_priv);
2259c008bc6eSPaulo Zanoni }
2260c008bc6eSPaulo Zanoni 
226191d14251STvrtko Ursulin static void ivb_display_irq_handler(struct drm_i915_private *dev_priv,
226291d14251STvrtko Ursulin 				    u32 de_iir)
22639719fb98SPaulo Zanoni {
226407d27e20SDamien Lespiau 	enum pipe pipe;
226523bb4cb5SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG_IVB;
226623bb4cb5SVille Syrjälä 
226740e56410SVille Syrjälä 	if (hotplug_trigger)
226891d14251STvrtko Ursulin 		ilk_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ivb);
22699719fb98SPaulo Zanoni 
22709719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
227191d14251STvrtko Ursulin 		ivb_err_int_handler(dev_priv);
22729719fb98SPaulo Zanoni 
22739719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
227491d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
22759719fb98SPaulo Zanoni 
22769719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
227791d14251STvrtko Ursulin 		intel_opregion_asle_intr(dev_priv);
22789719fb98SPaulo Zanoni 
2279055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
22805a21b665SDaniel Vetter 		if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)) &&
22815a21b665SDaniel Vetter 		    intel_pipe_handle_vblank(dev_priv, pipe))
22825a21b665SDaniel Vetter 			intel_check_page_flip(dev_priv, pipe);
228340da17c2SDaniel Vetter 
228440da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
22855251f04eSMaarten Lankhorst 		if (de_iir & DE_PLANE_FLIP_DONE_IVB(pipe))
228651cbaf01SMaarten Lankhorst 			intel_finish_page_flip_cs(dev_priv, pipe);
22879719fb98SPaulo Zanoni 	}
22889719fb98SPaulo Zanoni 
22899719fb98SPaulo Zanoni 	/* check event from PCH */
229091d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv) && (de_iir & DE_PCH_EVENT_IVB)) {
22919719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
22929719fb98SPaulo Zanoni 
229391d14251STvrtko Ursulin 		cpt_irq_handler(dev_priv, pch_iir);
22949719fb98SPaulo Zanoni 
22959719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
22969719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
22979719fb98SPaulo Zanoni 	}
22989719fb98SPaulo Zanoni }
22999719fb98SPaulo Zanoni 
230072c90f62SOscar Mateo /*
230172c90f62SOscar Mateo  * To handle irqs with the minimum potential races with fresh interrupts, we:
230272c90f62SOscar Mateo  * 1 - Disable Master Interrupt Control.
230372c90f62SOscar Mateo  * 2 - Find the source(s) of the interrupt.
230472c90f62SOscar Mateo  * 3 - Clear the Interrupt Identity bits (IIR).
230572c90f62SOscar Mateo  * 4 - Process the interrupt(s) that had bits set in the IIRs.
230672c90f62SOscar Mateo  * 5 - Re-enable Master Interrupt Control.
230772c90f62SOscar Mateo  */
2308f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
2309b1f14ad0SJesse Barnes {
231045a83f84SDaniel Vetter 	struct drm_device *dev = arg;
2311fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2312f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
23130e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
2314b1f14ad0SJesse Barnes 
23152dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
23162dd2a883SImre Deak 		return IRQ_NONE;
23172dd2a883SImre Deak 
23181f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
23191f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
23201f814dacSImre Deak 
2321b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
2322b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
2323b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
232423a78516SPaulo Zanoni 	POSTING_READ(DEIER);
23250e43406bSChris Wilson 
232644498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
232744498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
232844498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
232944498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
233044498aeaSPaulo Zanoni 	 * due to its back queue). */
233191d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv)) {
233244498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
233344498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
233444498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2335ab5c608bSBen Widawsky 	}
233644498aeaSPaulo Zanoni 
233772c90f62SOscar Mateo 	/* Find, clear, then process each source of interrupt */
233872c90f62SOscar Mateo 
23390e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
23400e43406bSChris Wilson 	if (gt_iir) {
234172c90f62SOscar Mateo 		I915_WRITE(GTIIR, gt_iir);
234272c90f62SOscar Mateo 		ret = IRQ_HANDLED;
234391d14251STvrtko Ursulin 		if (INTEL_GEN(dev_priv) >= 6)
2344261e40b8SVille Syrjälä 			snb_gt_irq_handler(dev_priv, gt_iir);
2345d8fc8a47SPaulo Zanoni 		else
2346261e40b8SVille Syrjälä 			ilk_gt_irq_handler(dev_priv, gt_iir);
23470e43406bSChris Wilson 	}
2348b1f14ad0SJesse Barnes 
2349b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
23500e43406bSChris Wilson 	if (de_iir) {
235172c90f62SOscar Mateo 		I915_WRITE(DEIIR, de_iir);
235272c90f62SOscar Mateo 		ret = IRQ_HANDLED;
235391d14251STvrtko Ursulin 		if (INTEL_GEN(dev_priv) >= 7)
235491d14251STvrtko Ursulin 			ivb_display_irq_handler(dev_priv, de_iir);
2355f1af8fc1SPaulo Zanoni 		else
235691d14251STvrtko Ursulin 			ilk_display_irq_handler(dev_priv, de_iir);
23570e43406bSChris Wilson 	}
23580e43406bSChris Wilson 
235991d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 6) {
2360f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
23610e43406bSChris Wilson 		if (pm_iir) {
2362b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
23630e43406bSChris Wilson 			ret = IRQ_HANDLED;
236472c90f62SOscar Mateo 			gen6_rps_irq_handler(dev_priv, pm_iir);
23650e43406bSChris Wilson 		}
2366f1af8fc1SPaulo Zanoni 	}
2367b1f14ad0SJesse Barnes 
2368b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
2369b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
237091d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv)) {
237144498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
237244498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2373ab5c608bSBen Widawsky 	}
2374b1f14ad0SJesse Barnes 
23751f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
23761f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
23771f814dacSImre Deak 
2378b1f14ad0SJesse Barnes 	return ret;
2379b1f14ad0SJesse Barnes }
2380b1f14ad0SJesse Barnes 
238191d14251STvrtko Ursulin static void bxt_hpd_irq_handler(struct drm_i915_private *dev_priv,
238291d14251STvrtko Ursulin 				u32 hotplug_trigger,
238340e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2384d04a492dSShashank Sharma {
2385cebd87a0SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2386d04a492dSShashank Sharma 
2387a52bb15bSVille Syrjälä 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
2388a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
2389d04a492dSShashank Sharma 
2390cebd87a0SVille Syrjälä 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
239140e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2392cebd87a0SVille Syrjälä 			   bxt_port_hotplug_long_detect);
239340e56410SVille Syrjälä 
239491d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2395d04a492dSShashank Sharma }
2396d04a492dSShashank Sharma 
2397f11a0f46STvrtko Ursulin static irqreturn_t
2398f11a0f46STvrtko Ursulin gen8_de_irq_handler(struct drm_i915_private *dev_priv, u32 master_ctl)
2399abd58f01SBen Widawsky {
2400abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
2401f11a0f46STvrtko Ursulin 	u32 iir;
2402c42664ccSDaniel Vetter 	enum pipe pipe;
240388e04703SJesse Barnes 
2404abd58f01SBen Widawsky 	if (master_ctl & GEN8_DE_MISC_IRQ) {
2405e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_MISC_IIR);
2406e32192e1STvrtko Ursulin 		if (iir) {
2407e32192e1STvrtko Ursulin 			I915_WRITE(GEN8_DE_MISC_IIR, iir);
2408abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
2409e32192e1STvrtko Ursulin 			if (iir & GEN8_DE_MISC_GSE)
241091d14251STvrtko Ursulin 				intel_opregion_asle_intr(dev_priv);
241138cc46d7SOscar Mateo 			else
241238cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Misc interrupt\n");
2413abd58f01SBen Widawsky 		}
241438cc46d7SOscar Mateo 		else
241538cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
2416abd58f01SBen Widawsky 	}
2417abd58f01SBen Widawsky 
24186d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
2419e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_PORT_IIR);
2420e32192e1STvrtko Ursulin 		if (iir) {
2421e32192e1STvrtko Ursulin 			u32 tmp_mask;
2422d04a492dSShashank Sharma 			bool found = false;
2423cebd87a0SVille Syrjälä 
2424e32192e1STvrtko Ursulin 			I915_WRITE(GEN8_DE_PORT_IIR, iir);
24256d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
242688e04703SJesse Barnes 
2427e32192e1STvrtko Ursulin 			tmp_mask = GEN8_AUX_CHANNEL_A;
2428e32192e1STvrtko Ursulin 			if (INTEL_INFO(dev_priv)->gen >= 9)
2429e32192e1STvrtko Ursulin 				tmp_mask |= GEN9_AUX_CHANNEL_B |
2430e32192e1STvrtko Ursulin 					    GEN9_AUX_CHANNEL_C |
2431e32192e1STvrtko Ursulin 					    GEN9_AUX_CHANNEL_D;
2432e32192e1STvrtko Ursulin 
2433e32192e1STvrtko Ursulin 			if (iir & tmp_mask) {
243491d14251STvrtko Ursulin 				dp_aux_irq_handler(dev_priv);
2435d04a492dSShashank Sharma 				found = true;
2436d04a492dSShashank Sharma 			}
2437d04a492dSShashank Sharma 
2438e32192e1STvrtko Ursulin 			if (IS_BROXTON(dev_priv)) {
2439e32192e1STvrtko Ursulin 				tmp_mask = iir & BXT_DE_PORT_HOTPLUG_MASK;
2440e32192e1STvrtko Ursulin 				if (tmp_mask) {
244191d14251STvrtko Ursulin 					bxt_hpd_irq_handler(dev_priv, tmp_mask,
244291d14251STvrtko Ursulin 							    hpd_bxt);
2443d04a492dSShashank Sharma 					found = true;
2444d04a492dSShashank Sharma 				}
2445e32192e1STvrtko Ursulin 			} else if (IS_BROADWELL(dev_priv)) {
2446e32192e1STvrtko Ursulin 				tmp_mask = iir & GEN8_PORT_DP_A_HOTPLUG;
2447e32192e1STvrtko Ursulin 				if (tmp_mask) {
244891d14251STvrtko Ursulin 					ilk_hpd_irq_handler(dev_priv,
244991d14251STvrtko Ursulin 							    tmp_mask, hpd_bdw);
2450e32192e1STvrtko Ursulin 					found = true;
2451e32192e1STvrtko Ursulin 				}
2452e32192e1STvrtko Ursulin 			}
2453d04a492dSShashank Sharma 
245491d14251STvrtko Ursulin 			if (IS_BROXTON(dev_priv) && (iir & BXT_DE_PORT_GMBUS)) {
245591d14251STvrtko Ursulin 				gmbus_irq_handler(dev_priv);
24569e63743eSShashank Sharma 				found = true;
24579e63743eSShashank Sharma 			}
24589e63743eSShashank Sharma 
2459d04a492dSShashank Sharma 			if (!found)
246038cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Port interrupt\n");
24616d766f02SDaniel Vetter 		}
246238cc46d7SOscar Mateo 		else
246338cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
24646d766f02SDaniel Vetter 	}
24656d766f02SDaniel Vetter 
2466055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2467e32192e1STvrtko Ursulin 		u32 flip_done, fault_errors;
2468abd58f01SBen Widawsky 
2469c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2470c42664ccSDaniel Vetter 			continue;
2471c42664ccSDaniel Vetter 
2472e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
2473e32192e1STvrtko Ursulin 		if (!iir) {
2474e32192e1STvrtko Ursulin 			DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
2475e32192e1STvrtko Ursulin 			continue;
2476e32192e1STvrtko Ursulin 		}
2477770de83dSDamien Lespiau 
2478e32192e1STvrtko Ursulin 		ret = IRQ_HANDLED;
2479e32192e1STvrtko Ursulin 		I915_WRITE(GEN8_DE_PIPE_IIR(pipe), iir);
2480e32192e1STvrtko Ursulin 
24815a21b665SDaniel Vetter 		if (iir & GEN8_PIPE_VBLANK &&
24825a21b665SDaniel Vetter 		    intel_pipe_handle_vblank(dev_priv, pipe))
24835a21b665SDaniel Vetter 			intel_check_page_flip(dev_priv, pipe);
2484abd58f01SBen Widawsky 
2485e32192e1STvrtko Ursulin 		flip_done = iir;
2486b4834a50SRodrigo Vivi 		if (INTEL_INFO(dev_priv)->gen >= 9)
2487e32192e1STvrtko Ursulin 			flip_done &= GEN9_PIPE_PLANE1_FLIP_DONE;
2488770de83dSDamien Lespiau 		else
2489e32192e1STvrtko Ursulin 			flip_done &= GEN8_PIPE_PRIMARY_FLIP_DONE;
2490770de83dSDamien Lespiau 
24915251f04eSMaarten Lankhorst 		if (flip_done)
249251cbaf01SMaarten Lankhorst 			intel_finish_page_flip_cs(dev_priv, pipe);
2493abd58f01SBen Widawsky 
2494e32192e1STvrtko Ursulin 		if (iir & GEN8_PIPE_CDCLK_CRC_DONE)
249591d14251STvrtko Ursulin 			hsw_pipe_crc_irq_handler(dev_priv, pipe);
24960fbe7870SDaniel Vetter 
2497e32192e1STvrtko Ursulin 		if (iir & GEN8_PIPE_FIFO_UNDERRUN)
2498e32192e1STvrtko Ursulin 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
249938d83c96SDaniel Vetter 
2500e32192e1STvrtko Ursulin 		fault_errors = iir;
2501b4834a50SRodrigo Vivi 		if (INTEL_INFO(dev_priv)->gen >= 9)
2502e32192e1STvrtko Ursulin 			fault_errors &= GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
2503770de83dSDamien Lespiau 		else
2504e32192e1STvrtko Ursulin 			fault_errors &= GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
2505770de83dSDamien Lespiau 
2506770de83dSDamien Lespiau 		if (fault_errors)
25071353ec38STvrtko Ursulin 			DRM_ERROR("Fault errors on pipe %c: 0x%08x\n",
250830100f2bSDaniel Vetter 				  pipe_name(pipe),
2509e32192e1STvrtko Ursulin 				  fault_errors);
2510abd58f01SBen Widawsky 	}
2511abd58f01SBen Widawsky 
251291d14251STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv) && !HAS_PCH_NOP(dev_priv) &&
2513266ea3d9SShashank Sharma 	    master_ctl & GEN8_DE_PCH_IRQ) {
251492d03a80SDaniel Vetter 		/*
251592d03a80SDaniel Vetter 		 * FIXME(BDW): Assume for now that the new interrupt handling
251692d03a80SDaniel Vetter 		 * scheme also closed the SDE interrupt handling race we've seen
251792d03a80SDaniel Vetter 		 * on older pch-split platforms. But this needs testing.
251892d03a80SDaniel Vetter 		 */
2519e32192e1STvrtko Ursulin 		iir = I915_READ(SDEIIR);
2520e32192e1STvrtko Ursulin 		if (iir) {
2521e32192e1STvrtko Ursulin 			I915_WRITE(SDEIIR, iir);
252292d03a80SDaniel Vetter 			ret = IRQ_HANDLED;
25236dbf30ceSVille Syrjälä 
252422dea0beSRodrigo Vivi 			if (HAS_PCH_SPT(dev_priv) || HAS_PCH_KBP(dev_priv))
252591d14251STvrtko Ursulin 				spt_irq_handler(dev_priv, iir);
25266dbf30ceSVille Syrjälä 			else
252791d14251STvrtko Ursulin 				cpt_irq_handler(dev_priv, iir);
25282dfb0b81SJani Nikula 		} else {
25292dfb0b81SJani Nikula 			/*
25302dfb0b81SJani Nikula 			 * Like on previous PCH there seems to be something
25312dfb0b81SJani Nikula 			 * fishy going on with forwarding PCH interrupts.
25322dfb0b81SJani Nikula 			 */
25332dfb0b81SJani Nikula 			DRM_DEBUG_DRIVER("The master control interrupt lied (SDE)!\n");
25342dfb0b81SJani Nikula 		}
253592d03a80SDaniel Vetter 	}
253692d03a80SDaniel Vetter 
2537f11a0f46STvrtko Ursulin 	return ret;
2538f11a0f46STvrtko Ursulin }
2539f11a0f46STvrtko Ursulin 
2540f11a0f46STvrtko Ursulin static irqreturn_t gen8_irq_handler(int irq, void *arg)
2541f11a0f46STvrtko Ursulin {
2542f11a0f46STvrtko Ursulin 	struct drm_device *dev = arg;
2543fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2544f11a0f46STvrtko Ursulin 	u32 master_ctl;
2545e30e251aSVille Syrjälä 	u32 gt_iir[4] = {};
2546f11a0f46STvrtko Ursulin 	irqreturn_t ret;
2547f11a0f46STvrtko Ursulin 
2548f11a0f46STvrtko Ursulin 	if (!intel_irqs_enabled(dev_priv))
2549f11a0f46STvrtko Ursulin 		return IRQ_NONE;
2550f11a0f46STvrtko Ursulin 
2551f11a0f46STvrtko Ursulin 	master_ctl = I915_READ_FW(GEN8_MASTER_IRQ);
2552f11a0f46STvrtko Ursulin 	master_ctl &= ~GEN8_MASTER_IRQ_CONTROL;
2553f11a0f46STvrtko Ursulin 	if (!master_ctl)
2554f11a0f46STvrtko Ursulin 		return IRQ_NONE;
2555f11a0f46STvrtko Ursulin 
2556f11a0f46STvrtko Ursulin 	I915_WRITE_FW(GEN8_MASTER_IRQ, 0);
2557f11a0f46STvrtko Ursulin 
2558f11a0f46STvrtko Ursulin 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
2559f11a0f46STvrtko Ursulin 	disable_rpm_wakeref_asserts(dev_priv);
2560f11a0f46STvrtko Ursulin 
2561f11a0f46STvrtko Ursulin 	/* Find, clear, then process each source of interrupt */
2562e30e251aSVille Syrjälä 	ret = gen8_gt_irq_ack(dev_priv, master_ctl, gt_iir);
2563e30e251aSVille Syrjälä 	gen8_gt_irq_handler(dev_priv, gt_iir);
2564f11a0f46STvrtko Ursulin 	ret |= gen8_de_irq_handler(dev_priv, master_ctl);
2565f11a0f46STvrtko Ursulin 
2566cb0d205eSChris Wilson 	I915_WRITE_FW(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
2567cb0d205eSChris Wilson 	POSTING_READ_FW(GEN8_MASTER_IRQ);
2568abd58f01SBen Widawsky 
25691f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
25701f814dacSImre Deak 
2571abd58f01SBen Widawsky 	return ret;
2572abd58f01SBen Widawsky }
2573abd58f01SBen Widawsky 
25741f15b76fSChris Wilson static void i915_error_wake_up(struct drm_i915_private *dev_priv)
257517e1df07SDaniel Vetter {
257617e1df07SDaniel Vetter 	/*
257717e1df07SDaniel Vetter 	 * Notify all waiters for GPU completion events that reset state has
257817e1df07SDaniel Vetter 	 * been changed, and that they need to restart their wait after
257917e1df07SDaniel Vetter 	 * checking for potential errors (and bail out to drop locks if there is
258017e1df07SDaniel Vetter 	 * a gpu reset pending so that i915_error_work_func can acquire them).
258117e1df07SDaniel Vetter 	 */
258217e1df07SDaniel Vetter 
258317e1df07SDaniel Vetter 	/* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
25841f15b76fSChris Wilson 	wake_up_all(&dev_priv->gpu_error.wait_queue);
258517e1df07SDaniel Vetter 
258617e1df07SDaniel Vetter 	/* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
258717e1df07SDaniel Vetter 	wake_up_all(&dev_priv->pending_flip_queue);
258817e1df07SDaniel Vetter }
258917e1df07SDaniel Vetter 
25908a905236SJesse Barnes /**
2591b8d24a06SMika Kuoppala  * i915_reset_and_wakeup - do process context error handling work
259214bb2c11STvrtko Ursulin  * @dev_priv: i915 device private
25938a905236SJesse Barnes  *
25948a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
25958a905236SJesse Barnes  * was detected.
25968a905236SJesse Barnes  */
2597c033666aSChris Wilson static void i915_reset_and_wakeup(struct drm_i915_private *dev_priv)
25988a905236SJesse Barnes {
259991c8a326SChris Wilson 	struct kobject *kobj = &dev_priv->drm.primary->kdev->kobj;
2600cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
2601cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
2602cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
26038a905236SJesse Barnes 
2604c033666aSChris Wilson 	kobject_uevent_env(kobj, KOBJ_CHANGE, error_event);
26058a905236SJesse Barnes 
260644d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("resetting chip\n");
2607c033666aSChris Wilson 	kobject_uevent_env(kobj, KOBJ_CHANGE, reset_event);
26081f83fee0SDaniel Vetter 
260917e1df07SDaniel Vetter 	/*
2610f454c694SImre Deak 	 * In most cases it's guaranteed that we get here with an RPM
2611f454c694SImre Deak 	 * reference held, for example because there is a pending GPU
2612f454c694SImre Deak 	 * request that won't finish until the reset is done. This
2613f454c694SImre Deak 	 * isn't the case at least when we get here by doing a
2614f454c694SImre Deak 	 * simulated reset via debugs, so get an RPM reference.
2615f454c694SImre Deak 	 */
2616f454c694SImre Deak 	intel_runtime_pm_get(dev_priv);
2617c033666aSChris Wilson 	intel_prepare_reset(dev_priv);
26187514747dSVille Syrjälä 
2619780f262aSChris Wilson 	do {
2620f454c694SImre Deak 		/*
262117e1df07SDaniel Vetter 		 * All state reset _must_ be completed before we update the
262217e1df07SDaniel Vetter 		 * reset counter, for otherwise waiters might miss the reset
262317e1df07SDaniel Vetter 		 * pending state and not properly drop locks, resulting in
262417e1df07SDaniel Vetter 		 * deadlocks with the reset work.
262517e1df07SDaniel Vetter 		 */
2626780f262aSChris Wilson 		if (mutex_trylock(&dev_priv->drm.struct_mutex)) {
2627780f262aSChris Wilson 			i915_reset(dev_priv);
2628221fe799SChris Wilson 			mutex_unlock(&dev_priv->drm.struct_mutex);
2629780f262aSChris Wilson 		}
2630780f262aSChris Wilson 
2631780f262aSChris Wilson 		/* We need to wait for anyone holding the lock to wakeup */
2632780f262aSChris Wilson 	} while (wait_on_bit_timeout(&dev_priv->gpu_error.flags,
2633780f262aSChris Wilson 				     I915_RESET_IN_PROGRESS,
2634780f262aSChris Wilson 				     TASK_UNINTERRUPTIBLE,
2635780f262aSChris Wilson 				     HZ));
2636f69061beSDaniel Vetter 
2637c033666aSChris Wilson 	intel_finish_reset(dev_priv);
2638f454c694SImre Deak 	intel_runtime_pm_put(dev_priv);
2639f454c694SImre Deak 
2640780f262aSChris Wilson 	if (!test_bit(I915_WEDGED, &dev_priv->gpu_error.flags))
2641c033666aSChris Wilson 		kobject_uevent_env(kobj,
2642f69061beSDaniel Vetter 				   KOBJ_CHANGE, reset_done_event);
26431f83fee0SDaniel Vetter 
264417e1df07SDaniel Vetter 	/*
264517e1df07SDaniel Vetter 	 * Note: The wake_up also serves as a memory barrier so that
26468af29b0cSChris Wilson 	 * waiters see the updated value of the dev_priv->gpu_error.
264717e1df07SDaniel Vetter 	 */
26481f15b76fSChris Wilson 	wake_up_all(&dev_priv->gpu_error.reset_queue);
2649f316a42cSBen Gamari }
26508a905236SJesse Barnes 
2651d636951eSBen Widawsky static inline void
2652d636951eSBen Widawsky i915_err_print_instdone(struct drm_i915_private *dev_priv,
2653d636951eSBen Widawsky 			struct intel_instdone *instdone)
2654d636951eSBen Widawsky {
2655f9e61372SBen Widawsky 	int slice;
2656f9e61372SBen Widawsky 	int subslice;
2657f9e61372SBen Widawsky 
2658d636951eSBen Widawsky 	pr_err("  INSTDONE: 0x%08x\n", instdone->instdone);
2659d636951eSBen Widawsky 
2660d636951eSBen Widawsky 	if (INTEL_GEN(dev_priv) <= 3)
2661d636951eSBen Widawsky 		return;
2662d636951eSBen Widawsky 
2663d636951eSBen Widawsky 	pr_err("  SC_INSTDONE: 0x%08x\n", instdone->slice_common);
2664d636951eSBen Widawsky 
2665d636951eSBen Widawsky 	if (INTEL_GEN(dev_priv) <= 6)
2666d636951eSBen Widawsky 		return;
2667d636951eSBen Widawsky 
2668f9e61372SBen Widawsky 	for_each_instdone_slice_subslice(dev_priv, slice, subslice)
2669f9e61372SBen Widawsky 		pr_err("  SAMPLER_INSTDONE[%d][%d]: 0x%08x\n",
2670f9e61372SBen Widawsky 		       slice, subslice, instdone->sampler[slice][subslice]);
2671f9e61372SBen Widawsky 
2672f9e61372SBen Widawsky 	for_each_instdone_slice_subslice(dev_priv, slice, subslice)
2673f9e61372SBen Widawsky 		pr_err("  ROW_INSTDONE[%d][%d]: 0x%08x\n",
2674f9e61372SBen Widawsky 		       slice, subslice, instdone->row[slice][subslice]);
2675d636951eSBen Widawsky }
2676d636951eSBen Widawsky 
2677eaa14c24SChris Wilson static void i915_clear_error_registers(struct drm_i915_private *dev_priv)
2678c0e09200SDave Airlie {
2679eaa14c24SChris Wilson 	u32 eir;
268063eeaf38SJesse Barnes 
2681eaa14c24SChris Wilson 	if (!IS_GEN2(dev_priv))
2682eaa14c24SChris Wilson 		I915_WRITE(PGTBL_ER, I915_READ(PGTBL_ER));
268363eeaf38SJesse Barnes 
2684eaa14c24SChris Wilson 	if (INTEL_GEN(dev_priv) < 4)
2685eaa14c24SChris Wilson 		I915_WRITE(IPEIR, I915_READ(IPEIR));
2686eaa14c24SChris Wilson 	else
2687eaa14c24SChris Wilson 		I915_WRITE(IPEIR_I965, I915_READ(IPEIR_I965));
26888a905236SJesse Barnes 
2689eaa14c24SChris Wilson 	I915_WRITE(EIR, I915_READ(EIR));
269063eeaf38SJesse Barnes 	eir = I915_READ(EIR);
269163eeaf38SJesse Barnes 	if (eir) {
269263eeaf38SJesse Barnes 		/*
269363eeaf38SJesse Barnes 		 * some errors might have become stuck,
269463eeaf38SJesse Barnes 		 * mask them.
269563eeaf38SJesse Barnes 		 */
2696eaa14c24SChris Wilson 		DRM_DEBUG_DRIVER("EIR stuck: 0x%08x, masking\n", eir);
269763eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
269863eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
269963eeaf38SJesse Barnes 	}
270035aed2e6SChris Wilson }
270135aed2e6SChris Wilson 
270235aed2e6SChris Wilson /**
2703b8d24a06SMika Kuoppala  * i915_handle_error - handle a gpu error
270414bb2c11STvrtko Ursulin  * @dev_priv: i915 device private
270514b730fcSarun.siluvery@linux.intel.com  * @engine_mask: mask representing engines that are hung
2706aafd8581SJavier Martinez Canillas  * Do some basic checking of register state at error time and
270735aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
270835aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
270935aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
271035aed2e6SChris Wilson  * of a ring dump etc.).
271114bb2c11STvrtko Ursulin  * @fmt: Error message format string
271235aed2e6SChris Wilson  */
2713c033666aSChris Wilson void i915_handle_error(struct drm_i915_private *dev_priv,
2714c033666aSChris Wilson 		       u32 engine_mask,
271558174462SMika Kuoppala 		       const char *fmt, ...)
271635aed2e6SChris Wilson {
271758174462SMika Kuoppala 	va_list args;
271858174462SMika Kuoppala 	char error_msg[80];
271935aed2e6SChris Wilson 
272058174462SMika Kuoppala 	va_start(args, fmt);
272158174462SMika Kuoppala 	vscnprintf(error_msg, sizeof(error_msg), fmt, args);
272258174462SMika Kuoppala 	va_end(args);
272358174462SMika Kuoppala 
2724c033666aSChris Wilson 	i915_capture_error_state(dev_priv, engine_mask, error_msg);
2725eaa14c24SChris Wilson 	i915_clear_error_registers(dev_priv);
27268a905236SJesse Barnes 
27278af29b0cSChris Wilson 	if (!engine_mask)
27288af29b0cSChris Wilson 		return;
27298af29b0cSChris Wilson 
27308af29b0cSChris Wilson 	if (test_and_set_bit(I915_RESET_IN_PROGRESS,
27318af29b0cSChris Wilson 			     &dev_priv->gpu_error.flags))
27328af29b0cSChris Wilson 		return;
2733ba1234d1SBen Gamari 
273411ed50ecSBen Gamari 	/*
2735b8d24a06SMika Kuoppala 	 * Wakeup waiting processes so that the reset function
2736b8d24a06SMika Kuoppala 	 * i915_reset_and_wakeup doesn't deadlock trying to grab
2737b8d24a06SMika Kuoppala 	 * various locks. By bumping the reset counter first, the woken
273817e1df07SDaniel Vetter 	 * processes will see a reset in progress and back off,
273917e1df07SDaniel Vetter 	 * releasing their locks and then wait for the reset completion.
274017e1df07SDaniel Vetter 	 * We must do this for _all_ gpu waiters that might hold locks
274117e1df07SDaniel Vetter 	 * that the reset work needs to acquire.
274217e1df07SDaniel Vetter 	 *
27438af29b0cSChris Wilson 	 * Note: The wake_up also provides a memory barrier to ensure that the
27448af29b0cSChris Wilson 	 * waiters see the updated value of the reset flags.
274511ed50ecSBen Gamari 	 */
27461f15b76fSChris Wilson 	i915_error_wake_up(dev_priv);
274711ed50ecSBen Gamari 
2748c033666aSChris Wilson 	i915_reset_and_wakeup(dev_priv);
27498a905236SJesse Barnes }
27508a905236SJesse Barnes 
275142f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
275242f52ef8SKeith Packard  * we use as a pipe index
275342f52ef8SKeith Packard  */
275486e83e35SChris Wilson static int i8xx_enable_vblank(struct drm_device *dev, unsigned int pipe)
27550a3e67a4SJesse Barnes {
2756fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2757e9d21d7fSKeith Packard 	unsigned long irqflags;
275871e0ffa5SJesse Barnes 
27591ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
276086e83e35SChris Wilson 	i915_enable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_STATUS);
276186e83e35SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
276286e83e35SChris Wilson 
276386e83e35SChris Wilson 	return 0;
276486e83e35SChris Wilson }
276586e83e35SChris Wilson 
276686e83e35SChris Wilson static int i965_enable_vblank(struct drm_device *dev, unsigned int pipe)
276786e83e35SChris Wilson {
276886e83e35SChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
276986e83e35SChris Wilson 	unsigned long irqflags;
277086e83e35SChris Wilson 
277186e83e35SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
27727c463586SKeith Packard 	i915_enable_pipestat(dev_priv, pipe,
2773755e9019SImre Deak 			     PIPE_START_VBLANK_INTERRUPT_STATUS);
27741ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
27758692d00eSChris Wilson 
27760a3e67a4SJesse Barnes 	return 0;
27770a3e67a4SJesse Barnes }
27780a3e67a4SJesse Barnes 
277988e72717SThierry Reding static int ironlake_enable_vblank(struct drm_device *dev, unsigned int pipe)
2780f796cf8fSJesse Barnes {
2781fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2782f796cf8fSJesse Barnes 	unsigned long irqflags;
278355b8f2a7STvrtko Ursulin 	uint32_t bit = INTEL_GEN(dev_priv) >= 7 ?
278486e83e35SChris Wilson 		DE_PIPE_VBLANK_IVB(pipe) : DE_PIPE_VBLANK(pipe);
2785f796cf8fSJesse Barnes 
2786f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2787fbdedaeaSVille Syrjälä 	ilk_enable_display_irq(dev_priv, bit);
2788b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2789b1f14ad0SJesse Barnes 
2790b1f14ad0SJesse Barnes 	return 0;
2791b1f14ad0SJesse Barnes }
2792b1f14ad0SJesse Barnes 
279388e72717SThierry Reding static int gen8_enable_vblank(struct drm_device *dev, unsigned int pipe)
2794abd58f01SBen Widawsky {
2795fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2796abd58f01SBen Widawsky 	unsigned long irqflags;
2797abd58f01SBen Widawsky 
2798abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2799013d3752SVille Syrjälä 	bdw_enable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
2800abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2801013d3752SVille Syrjälä 
2802abd58f01SBen Widawsky 	return 0;
2803abd58f01SBen Widawsky }
2804abd58f01SBen Widawsky 
280542f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
280642f52ef8SKeith Packard  * we use as a pipe index
280742f52ef8SKeith Packard  */
280886e83e35SChris Wilson static void i8xx_disable_vblank(struct drm_device *dev, unsigned int pipe)
280986e83e35SChris Wilson {
281086e83e35SChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
281186e83e35SChris Wilson 	unsigned long irqflags;
281286e83e35SChris Wilson 
281386e83e35SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
281486e83e35SChris Wilson 	i915_disable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_STATUS);
281586e83e35SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
281686e83e35SChris Wilson }
281786e83e35SChris Wilson 
281886e83e35SChris Wilson static void i965_disable_vblank(struct drm_device *dev, unsigned int pipe)
28190a3e67a4SJesse Barnes {
2820fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2821e9d21d7fSKeith Packard 	unsigned long irqflags;
28220a3e67a4SJesse Barnes 
28231ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
28247c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
2825755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
28261ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
28270a3e67a4SJesse Barnes }
28280a3e67a4SJesse Barnes 
282988e72717SThierry Reding static void ironlake_disable_vblank(struct drm_device *dev, unsigned int pipe)
2830f796cf8fSJesse Barnes {
2831fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2832f796cf8fSJesse Barnes 	unsigned long irqflags;
283355b8f2a7STvrtko Ursulin 	uint32_t bit = INTEL_GEN(dev_priv) >= 7 ?
283486e83e35SChris Wilson 		DE_PIPE_VBLANK_IVB(pipe) : DE_PIPE_VBLANK(pipe);
2835f796cf8fSJesse Barnes 
2836f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2837fbdedaeaSVille Syrjälä 	ilk_disable_display_irq(dev_priv, bit);
2838b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2839b1f14ad0SJesse Barnes }
2840b1f14ad0SJesse Barnes 
284188e72717SThierry Reding static void gen8_disable_vblank(struct drm_device *dev, unsigned int pipe)
2842abd58f01SBen Widawsky {
2843fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2844abd58f01SBen Widawsky 	unsigned long irqflags;
2845abd58f01SBen Widawsky 
2846abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2847013d3752SVille Syrjälä 	bdw_disable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
2848abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2849abd58f01SBen Widawsky }
2850abd58f01SBen Widawsky 
28519107e9d2SChris Wilson static bool
285231bb59ccSChris Wilson ipehr_is_semaphore_wait(struct intel_engine_cs *engine, u32 ipehr)
2853a028c4b0SDaniel Vetter {
285431bb59ccSChris Wilson 	if (INTEL_GEN(engine->i915) >= 8) {
2855a6cdb93aSRodrigo Vivi 		return (ipehr >> 23) == 0x1c;
2856a028c4b0SDaniel Vetter 	} else {
2857a028c4b0SDaniel Vetter 		ipehr &= ~MI_SEMAPHORE_SYNC_MASK;
2858a028c4b0SDaniel Vetter 		return ipehr == (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE |
2859a028c4b0SDaniel Vetter 				 MI_SEMAPHORE_REGISTER);
2860a028c4b0SDaniel Vetter 	}
2861a028c4b0SDaniel Vetter }
2862a028c4b0SDaniel Vetter 
2863a4872ba6SOscar Mateo static struct intel_engine_cs *
28640bc40be8STvrtko Ursulin semaphore_wait_to_signaller_ring(struct intel_engine_cs *engine, u32 ipehr,
28650bc40be8STvrtko Ursulin 				 u64 offset)
2866921d42eaSDaniel Vetter {
2867c033666aSChris Wilson 	struct drm_i915_private *dev_priv = engine->i915;
2868a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
28693b3f1650SAkash Goel 	enum intel_engine_id id;
2870921d42eaSDaniel Vetter 
2871c033666aSChris Wilson 	if (INTEL_GEN(dev_priv) >= 8) {
28723b3f1650SAkash Goel 		for_each_engine(signaller, dev_priv, id) {
28730bc40be8STvrtko Ursulin 			if (engine == signaller)
2874a6cdb93aSRodrigo Vivi 				continue;
2875a6cdb93aSRodrigo Vivi 
2876348b9b11SChris Wilson 			if (offset == signaller->semaphore.signal_ggtt[engine->hw_id])
2877a6cdb93aSRodrigo Vivi 				return signaller;
2878a6cdb93aSRodrigo Vivi 		}
2879921d42eaSDaniel Vetter 	} else {
2880921d42eaSDaniel Vetter 		u32 sync_bits = ipehr & MI_SEMAPHORE_SYNC_MASK;
2881921d42eaSDaniel Vetter 
28823b3f1650SAkash Goel 		for_each_engine(signaller, dev_priv, id) {
28830bc40be8STvrtko Ursulin 			if(engine == signaller)
2884921d42eaSDaniel Vetter 				continue;
2885921d42eaSDaniel Vetter 
2886348b9b11SChris Wilson 			if (sync_bits == signaller->semaphore.mbox.wait[engine->hw_id])
2887921d42eaSDaniel Vetter 				return signaller;
2888921d42eaSDaniel Vetter 		}
2889921d42eaSDaniel Vetter 	}
2890921d42eaSDaniel Vetter 
2891348b9b11SChris Wilson 	DRM_DEBUG_DRIVER("No signaller ring found for %s, ipehr 0x%08x, offset 0x%016llx\n",
2892348b9b11SChris Wilson 			 engine->name, ipehr, offset);
2893921d42eaSDaniel Vetter 
289480b5bdbdSChris Wilson 	return ERR_PTR(-ENODEV);
2895921d42eaSDaniel Vetter }
2896921d42eaSDaniel Vetter 
2897a4872ba6SOscar Mateo static struct intel_engine_cs *
28980bc40be8STvrtko Ursulin semaphore_waits_for(struct intel_engine_cs *engine, u32 *seqno)
2899a24a11e6SChris Wilson {
2900c033666aSChris Wilson 	struct drm_i915_private *dev_priv = engine->i915;
2901406ea8d2SChris Wilson 	void __iomem *vaddr;
290288fe429dSDaniel Vetter 	u32 cmd, ipehr, head;
2903a6cdb93aSRodrigo Vivi 	u64 offset = 0;
2904a6cdb93aSRodrigo Vivi 	int i, backwards;
2905a24a11e6SChris Wilson 
2906381e8ae3STomas Elf 	/*
2907381e8ae3STomas Elf 	 * This function does not support execlist mode - any attempt to
2908381e8ae3STomas Elf 	 * proceed further into this function will result in a kernel panic
2909381e8ae3STomas Elf 	 * when dereferencing ring->buffer, which is not set up in execlist
2910381e8ae3STomas Elf 	 * mode.
2911381e8ae3STomas Elf 	 *
2912381e8ae3STomas Elf 	 * The correct way of doing it would be to derive the currently
2913381e8ae3STomas Elf 	 * executing ring buffer from the current context, which is derived
2914381e8ae3STomas Elf 	 * from the currently running request. Unfortunately, to get the
2915381e8ae3STomas Elf 	 * current request we would have to grab the struct_mutex before doing
2916381e8ae3STomas Elf 	 * anything else, which would be ill-advised since some other thread
2917381e8ae3STomas Elf 	 * might have grabbed it already and managed to hang itself, causing
2918381e8ae3STomas Elf 	 * the hang checker to deadlock.
2919381e8ae3STomas Elf 	 *
2920381e8ae3STomas Elf 	 * Therefore, this function does not support execlist mode in its
2921381e8ae3STomas Elf 	 * current form. Just return NULL and move on.
2922381e8ae3STomas Elf 	 */
29230bc40be8STvrtko Ursulin 	if (engine->buffer == NULL)
2924381e8ae3STomas Elf 		return NULL;
2925381e8ae3STomas Elf 
29260bc40be8STvrtko Ursulin 	ipehr = I915_READ(RING_IPEHR(engine->mmio_base));
292731bb59ccSChris Wilson 	if (!ipehr_is_semaphore_wait(engine, ipehr))
29286274f212SChris Wilson 		return NULL;
2929a24a11e6SChris Wilson 
293088fe429dSDaniel Vetter 	/*
293188fe429dSDaniel Vetter 	 * HEAD is likely pointing to the dword after the actual command,
293288fe429dSDaniel Vetter 	 * so scan backwards until we find the MBOX. But limit it to just 3
2933a6cdb93aSRodrigo Vivi 	 * or 4 dwords depending on the semaphore wait command size.
2934a6cdb93aSRodrigo Vivi 	 * Note that we don't care about ACTHD here since that might
293588fe429dSDaniel Vetter 	 * point at at batch, and semaphores are always emitted into the
293688fe429dSDaniel Vetter 	 * ringbuffer itself.
2937a24a11e6SChris Wilson 	 */
29380bc40be8STvrtko Ursulin 	head = I915_READ_HEAD(engine) & HEAD_ADDR;
2939c033666aSChris Wilson 	backwards = (INTEL_GEN(dev_priv) >= 8) ? 5 : 4;
2940f2f0ed71SChris Wilson 	vaddr = (void __iomem *)engine->buffer->vaddr;
294188fe429dSDaniel Vetter 
2942a6cdb93aSRodrigo Vivi 	for (i = backwards; i; --i) {
294388fe429dSDaniel Vetter 		/*
294488fe429dSDaniel Vetter 		 * Be paranoid and presume the hw has gone off into the wild -
294588fe429dSDaniel Vetter 		 * our ring is smaller than what the hardware (and hence
294688fe429dSDaniel Vetter 		 * HEAD_ADDR) allows. Also handles wrap-around.
294788fe429dSDaniel Vetter 		 */
29480bc40be8STvrtko Ursulin 		head &= engine->buffer->size - 1;
294988fe429dSDaniel Vetter 
295088fe429dSDaniel Vetter 		/* This here seems to blow up */
2951406ea8d2SChris Wilson 		cmd = ioread32(vaddr + head);
2952a24a11e6SChris Wilson 		if (cmd == ipehr)
2953a24a11e6SChris Wilson 			break;
2954a24a11e6SChris Wilson 
295588fe429dSDaniel Vetter 		head -= 4;
295688fe429dSDaniel Vetter 	}
2957a24a11e6SChris Wilson 
295888fe429dSDaniel Vetter 	if (!i)
295988fe429dSDaniel Vetter 		return NULL;
296088fe429dSDaniel Vetter 
2961406ea8d2SChris Wilson 	*seqno = ioread32(vaddr + head + 4) + 1;
2962c033666aSChris Wilson 	if (INTEL_GEN(dev_priv) >= 8) {
2963406ea8d2SChris Wilson 		offset = ioread32(vaddr + head + 12);
2964a6cdb93aSRodrigo Vivi 		offset <<= 32;
2965406ea8d2SChris Wilson 		offset |= ioread32(vaddr + head + 8);
2966a6cdb93aSRodrigo Vivi 	}
29670bc40be8STvrtko Ursulin 	return semaphore_wait_to_signaller_ring(engine, ipehr, offset);
2968a24a11e6SChris Wilson }
2969a24a11e6SChris Wilson 
29700bc40be8STvrtko Ursulin static int semaphore_passed(struct intel_engine_cs *engine)
29716274f212SChris Wilson {
2972c033666aSChris Wilson 	struct drm_i915_private *dev_priv = engine->i915;
2973a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2974a0d036b0SChris Wilson 	u32 seqno;
29756274f212SChris Wilson 
29760bc40be8STvrtko Ursulin 	engine->hangcheck.deadlock++;
29776274f212SChris Wilson 
29780bc40be8STvrtko Ursulin 	signaller = semaphore_waits_for(engine, &seqno);
29794be17381SChris Wilson 	if (signaller == NULL)
29804be17381SChris Wilson 		return -1;
29814be17381SChris Wilson 
298280b5bdbdSChris Wilson 	if (IS_ERR(signaller))
298380b5bdbdSChris Wilson 		return 0;
298480b5bdbdSChris Wilson 
29854be17381SChris Wilson 	/* Prevent pathological recursion due to driver bugs */
2986666796daSTvrtko Ursulin 	if (signaller->hangcheck.deadlock >= I915_NUM_ENGINES)
29876274f212SChris Wilson 		return -1;
29886274f212SChris Wilson 
29891b7744e7SChris Wilson 	if (i915_seqno_passed(intel_engine_get_seqno(signaller), seqno))
29904be17381SChris Wilson 		return 1;
29914be17381SChris Wilson 
2992a0d036b0SChris Wilson 	/* cursory check for an unkickable deadlock */
2993a0d036b0SChris Wilson 	if (I915_READ_CTL(signaller) & RING_WAIT_SEMAPHORE &&
2994a0d036b0SChris Wilson 	    semaphore_passed(signaller) < 0)
29954be17381SChris Wilson 		return -1;
29964be17381SChris Wilson 
29974be17381SChris Wilson 	return 0;
29986274f212SChris Wilson }
29996274f212SChris Wilson 
30006274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
30016274f212SChris Wilson {
3002e2f80391STvrtko Ursulin 	struct intel_engine_cs *engine;
30033b3f1650SAkash Goel 	enum intel_engine_id id;
30046274f212SChris Wilson 
30053b3f1650SAkash Goel 	for_each_engine(engine, dev_priv, id)
3006e2f80391STvrtko Ursulin 		engine->hangcheck.deadlock = 0;
30076274f212SChris Wilson }
30086274f212SChris Wilson 
3009d636951eSBen Widawsky static bool instdone_unchanged(u32 current_instdone, u32 *old_instdone)
3010d636951eSBen Widawsky {
3011d636951eSBen Widawsky 	u32 tmp = current_instdone | *old_instdone;
3012d636951eSBen Widawsky 	bool unchanged;
3013d636951eSBen Widawsky 
3014d636951eSBen Widawsky 	unchanged = tmp == *old_instdone;
3015d636951eSBen Widawsky 	*old_instdone |= tmp;
3016d636951eSBen Widawsky 
3017d636951eSBen Widawsky 	return unchanged;
3018d636951eSBen Widawsky }
3019d636951eSBen Widawsky 
30200bc40be8STvrtko Ursulin static bool subunits_stuck(struct intel_engine_cs *engine)
30211ec14ad3SChris Wilson {
3022d636951eSBen Widawsky 	struct drm_i915_private *dev_priv = engine->i915;
3023d636951eSBen Widawsky 	struct intel_instdone instdone;
3024d636951eSBen Widawsky 	struct intel_instdone *accu_instdone = &engine->hangcheck.instdone;
302561642ff0SMika Kuoppala 	bool stuck;
3026f9e61372SBen Widawsky 	int slice;
3027f9e61372SBen Widawsky 	int subslice;
30289107e9d2SChris Wilson 
30290bc40be8STvrtko Ursulin 	if (engine->id != RCS)
303061642ff0SMika Kuoppala 		return true;
303161642ff0SMika Kuoppala 
30320e704476SChris Wilson 	intel_engine_get_instdone(engine, &instdone);
303361642ff0SMika Kuoppala 
303461642ff0SMika Kuoppala 	/* There might be unstable subunit states even when
303561642ff0SMika Kuoppala 	 * actual head is not moving. Filter out the unstable ones by
303661642ff0SMika Kuoppala 	 * accumulating the undone -> done transitions and only
303761642ff0SMika Kuoppala 	 * consider those as progress.
303861642ff0SMika Kuoppala 	 */
3039d636951eSBen Widawsky 	stuck = instdone_unchanged(instdone.instdone,
3040d636951eSBen Widawsky 				   &accu_instdone->instdone);
3041d636951eSBen Widawsky 	stuck &= instdone_unchanged(instdone.slice_common,
3042d636951eSBen Widawsky 				    &accu_instdone->slice_common);
3043f9e61372SBen Widawsky 
3044f9e61372SBen Widawsky 	for_each_instdone_slice_subslice(dev_priv, slice, subslice) {
3045f9e61372SBen Widawsky 		stuck &= instdone_unchanged(instdone.sampler[slice][subslice],
3046f9e61372SBen Widawsky 					    &accu_instdone->sampler[slice][subslice]);
3047f9e61372SBen Widawsky 		stuck &= instdone_unchanged(instdone.row[slice][subslice],
3048f9e61372SBen Widawsky 					    &accu_instdone->row[slice][subslice]);
3049f9e61372SBen Widawsky 	}
305061642ff0SMika Kuoppala 
305161642ff0SMika Kuoppala 	return stuck;
305261642ff0SMika Kuoppala }
305361642ff0SMika Kuoppala 
30547e37f889SChris Wilson static enum intel_engine_hangcheck_action
30550bc40be8STvrtko Ursulin head_stuck(struct intel_engine_cs *engine, u64 acthd)
305661642ff0SMika Kuoppala {
30570bc40be8STvrtko Ursulin 	if (acthd != engine->hangcheck.acthd) {
305861642ff0SMika Kuoppala 
305961642ff0SMika Kuoppala 		/* Clear subunit states on head movement */
3060d636951eSBen Widawsky 		memset(&engine->hangcheck.instdone, 0,
30610bc40be8STvrtko Ursulin 		       sizeof(engine->hangcheck.instdone));
306261642ff0SMika Kuoppala 
3063f2f4d82fSJani Nikula 		return HANGCHECK_ACTIVE;
3064f260fe7bSMika Kuoppala 	}
3065f260fe7bSMika Kuoppala 
30660bc40be8STvrtko Ursulin 	if (!subunits_stuck(engine))
306761642ff0SMika Kuoppala 		return HANGCHECK_ACTIVE;
306861642ff0SMika Kuoppala 
306961642ff0SMika Kuoppala 	return HANGCHECK_HUNG;
307061642ff0SMika Kuoppala }
307161642ff0SMika Kuoppala 
30727e37f889SChris Wilson static enum intel_engine_hangcheck_action
30737e37f889SChris Wilson engine_stuck(struct intel_engine_cs *engine, u64 acthd)
307461642ff0SMika Kuoppala {
3075c033666aSChris Wilson 	struct drm_i915_private *dev_priv = engine->i915;
30767e37f889SChris Wilson 	enum intel_engine_hangcheck_action ha;
307761642ff0SMika Kuoppala 	u32 tmp;
307861642ff0SMika Kuoppala 
30790bc40be8STvrtko Ursulin 	ha = head_stuck(engine, acthd);
308061642ff0SMika Kuoppala 	if (ha != HANGCHECK_HUNG)
308161642ff0SMika Kuoppala 		return ha;
308261642ff0SMika Kuoppala 
3083c033666aSChris Wilson 	if (IS_GEN2(dev_priv))
3084f2f4d82fSJani Nikula 		return HANGCHECK_HUNG;
30859107e9d2SChris Wilson 
30869107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
30879107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
30889107e9d2SChris Wilson 	 * and break the hang. This should work on
30899107e9d2SChris Wilson 	 * all but the second generation chipsets.
30909107e9d2SChris Wilson 	 */
30910bc40be8STvrtko Ursulin 	tmp = I915_READ_CTL(engine);
30921ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
3093c033666aSChris Wilson 		i915_handle_error(dev_priv, 0,
309458174462SMika Kuoppala 				  "Kicking stuck wait on %s",
30950bc40be8STvrtko Ursulin 				  engine->name);
30960bc40be8STvrtko Ursulin 		I915_WRITE_CTL(engine, tmp);
3097f2f4d82fSJani Nikula 		return HANGCHECK_KICK;
30981ec14ad3SChris Wilson 	}
3099a24a11e6SChris Wilson 
3100c033666aSChris Wilson 	if (INTEL_GEN(dev_priv) >= 6 && tmp & RING_WAIT_SEMAPHORE) {
31010bc40be8STvrtko Ursulin 		switch (semaphore_passed(engine)) {
31026274f212SChris Wilson 		default:
3103f2f4d82fSJani Nikula 			return HANGCHECK_HUNG;
31046274f212SChris Wilson 		case 1:
3105c033666aSChris Wilson 			i915_handle_error(dev_priv, 0,
310658174462SMika Kuoppala 					  "Kicking stuck semaphore on %s",
31070bc40be8STvrtko Ursulin 					  engine->name);
31080bc40be8STvrtko Ursulin 			I915_WRITE_CTL(engine, tmp);
3109f2f4d82fSJani Nikula 			return HANGCHECK_KICK;
31106274f212SChris Wilson 		case 0:
3111f2f4d82fSJani Nikula 			return HANGCHECK_WAIT;
31126274f212SChris Wilson 		}
31139107e9d2SChris Wilson 	}
31149107e9d2SChris Wilson 
3115f2f4d82fSJani Nikula 	return HANGCHECK_HUNG;
3116a24a11e6SChris Wilson }
3117d1e61e7fSChris Wilson 
3118737b1506SChris Wilson /*
3119f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
312005407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
312105407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
312205407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
312305407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
312405407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
3125f65d9421SBen Gamari  */
3126737b1506SChris Wilson static void i915_hangcheck_elapsed(struct work_struct *work)
3127f65d9421SBen Gamari {
3128737b1506SChris Wilson 	struct drm_i915_private *dev_priv =
3129737b1506SChris Wilson 		container_of(work, typeof(*dev_priv),
3130737b1506SChris Wilson 			     gpu_error.hangcheck_work.work);
3131e2f80391STvrtko Ursulin 	struct intel_engine_cs *engine;
31323b3f1650SAkash Goel 	enum intel_engine_id id;
31332b284288SChris Wilson 	unsigned int hung = 0, stuck = 0;
31342b284288SChris Wilson 	int busy_count = 0;
31359107e9d2SChris Wilson #define BUSY 1
31369107e9d2SChris Wilson #define KICK 5
31379107e9d2SChris Wilson #define HUNG 20
313824a65e62SMika Kuoppala #define ACTIVE_DECAY 15
3139893eead0SChris Wilson 
3140d330a953SJani Nikula 	if (!i915.enable_hangcheck)
31413e0dc6b0SBen Widawsky 		return;
31423e0dc6b0SBen Widawsky 
3143b1379d49SChris Wilson 	if (!READ_ONCE(dev_priv->gt.awake))
314467d97da3SChris Wilson 		return;
31451f814dacSImre Deak 
314675714940SMika Kuoppala 	/* As enabling the GPU requires fairly extensive mmio access,
314775714940SMika Kuoppala 	 * periodically arm the mmio checker to see if we are triggering
314875714940SMika Kuoppala 	 * any invalid access.
314975714940SMika Kuoppala 	 */
315075714940SMika Kuoppala 	intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
315175714940SMika Kuoppala 
31523b3f1650SAkash Goel 	for_each_engine(engine, dev_priv, id) {
3153688e6c72SChris Wilson 		bool busy = intel_engine_has_waiter(engine);
315450877445SChris Wilson 		u64 acthd;
315550877445SChris Wilson 		u32 seqno;
315634730fedSChris Wilson 		u32 submit;
3157b4519513SChris Wilson 
31586274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
31596274f212SChris Wilson 
3160c04e0f3bSChris Wilson 		/* We don't strictly need an irq-barrier here, as we are not
3161c04e0f3bSChris Wilson 		 * serving an interrupt request, be paranoid in case the
3162c04e0f3bSChris Wilson 		 * barrier has side-effects (such as preventing a broken
3163c04e0f3bSChris Wilson 		 * cacheline snoop) and so be sure that we can see the seqno
3164c04e0f3bSChris Wilson 		 * advance. If the seqno should stick, due to a stale
3165c04e0f3bSChris Wilson 		 * cacheline, we would erroneously declare the GPU hung.
3166c04e0f3bSChris Wilson 		 */
3167c04e0f3bSChris Wilson 		if (engine->irq_seqno_barrier)
3168c04e0f3bSChris Wilson 			engine->irq_seqno_barrier(engine);
3169c04e0f3bSChris Wilson 
31707e37f889SChris Wilson 		acthd = intel_engine_get_active_head(engine);
31711b7744e7SChris Wilson 		seqno = intel_engine_get_seqno(engine);
3172cb399eabSChris Wilson 		submit = intel_engine_last_submit(engine);
317305407ff8SMika Kuoppala 
3174e2f80391STvrtko Ursulin 		if (engine->hangcheck.seqno == seqno) {
317534730fedSChris Wilson 			if (i915_seqno_passed(seqno, submit)) {
3176e2f80391STvrtko Ursulin 				engine->hangcheck.action = HANGCHECK_IDLE;
317705407ff8SMika Kuoppala 			} else {
31786274f212SChris Wilson 				/* We always increment the hangcheck score
31799930ca1aSChris Wilson 				 * if the engine is busy and still processing
31806274f212SChris Wilson 				 * the same request, so that no single request
31816274f212SChris Wilson 				 * can run indefinitely (such as a chain of
31826274f212SChris Wilson 				 * batches). The only time we do not increment
31836274f212SChris Wilson 				 * the hangcheck score on this ring, if this
31849930ca1aSChris Wilson 				 * engine is in a legitimate wait for another
31859930ca1aSChris Wilson 				 * engine. In that case the waiting engine is a
31866274f212SChris Wilson 				 * victim and we want to be sure we catch the
31876274f212SChris Wilson 				 * right culprit. Then every time we do kick
31886274f212SChris Wilson 				 * the ring, add a small increment to the
31896274f212SChris Wilson 				 * score so that we can catch a batch that is
31906274f212SChris Wilson 				 * being repeatedly kicked and so responsible
31916274f212SChris Wilson 				 * for stalling the machine.
31929107e9d2SChris Wilson 				 */
31937e37f889SChris Wilson 				engine->hangcheck.action =
31947e37f889SChris Wilson 					engine_stuck(engine, acthd);
3195ad8beaeaSMika Kuoppala 
3196e2f80391STvrtko Ursulin 				switch (engine->hangcheck.action) {
3197da661464SMika Kuoppala 				case HANGCHECK_IDLE:
3198f2f4d82fSJani Nikula 				case HANGCHECK_WAIT:
3199f260fe7bSMika Kuoppala 					break;
320024a65e62SMika Kuoppala 				case HANGCHECK_ACTIVE:
3201e2f80391STvrtko Ursulin 					engine->hangcheck.score += BUSY;
32026274f212SChris Wilson 					break;
3203f2f4d82fSJani Nikula 				case HANGCHECK_KICK:
3204e2f80391STvrtko Ursulin 					engine->hangcheck.score += KICK;
32056274f212SChris Wilson 					break;
3206f2f4d82fSJani Nikula 				case HANGCHECK_HUNG:
3207e2f80391STvrtko Ursulin 					engine->hangcheck.score += HUNG;
32086274f212SChris Wilson 					break;
32096274f212SChris Wilson 				}
321005407ff8SMika Kuoppala 			}
32112b284288SChris Wilson 
32122b284288SChris Wilson 			if (engine->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG) {
32132b284288SChris Wilson 				hung |= intel_engine_flag(engine);
32142b284288SChris Wilson 				if (engine->hangcheck.action != HANGCHECK_HUNG)
32152b284288SChris Wilson 					stuck |= intel_engine_flag(engine);
32162b284288SChris Wilson 			}
32179107e9d2SChris Wilson 		} else {
3218e2f80391STvrtko Ursulin 			engine->hangcheck.action = HANGCHECK_ACTIVE;
3219da661464SMika Kuoppala 
32209107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
32219107e9d2SChris Wilson 			 * attempts across multiple batches.
32229107e9d2SChris Wilson 			 */
3223e2f80391STvrtko Ursulin 			if (engine->hangcheck.score > 0)
3224e2f80391STvrtko Ursulin 				engine->hangcheck.score -= ACTIVE_DECAY;
3225e2f80391STvrtko Ursulin 			if (engine->hangcheck.score < 0)
3226e2f80391STvrtko Ursulin 				engine->hangcheck.score = 0;
3227f260fe7bSMika Kuoppala 
322861642ff0SMika Kuoppala 			/* Clear head and subunit states on seqno movement */
322912471ba8SChris Wilson 			acthd = 0;
323061642ff0SMika Kuoppala 
3231d636951eSBen Widawsky 			memset(&engine->hangcheck.instdone, 0,
3232e2f80391STvrtko Ursulin 			       sizeof(engine->hangcheck.instdone));
3233cbb465e7SChris Wilson 		}
3234f65d9421SBen Gamari 
3235e2f80391STvrtko Ursulin 		engine->hangcheck.seqno = seqno;
3236e2f80391STvrtko Ursulin 		engine->hangcheck.acthd = acthd;
32379107e9d2SChris Wilson 		busy_count += busy;
323805407ff8SMika Kuoppala 	}
323905407ff8SMika Kuoppala 
32402b284288SChris Wilson 	if (hung) {
32412b284288SChris Wilson 		char msg[80];
3242bafb0fceSChris Wilson 		unsigned int tmp;
32432b284288SChris Wilson 		int len;
324405407ff8SMika Kuoppala 
32452b284288SChris Wilson 		/* If some rings hung but others were still busy, only
32462b284288SChris Wilson 		 * blame the hanging rings in the synopsis.
32472b284288SChris Wilson 		 */
32482b284288SChris Wilson 		if (stuck != hung)
32492b284288SChris Wilson 			hung &= ~stuck;
32502b284288SChris Wilson 		len = scnprintf(msg, sizeof(msg),
32512b284288SChris Wilson 				"%s on ", stuck == hung ? "No progress" : "Hang");
3252bafb0fceSChris Wilson 		for_each_engine_masked(engine, dev_priv, hung, tmp)
32532b284288SChris Wilson 			len += scnprintf(msg + len, sizeof(msg) - len,
32542b284288SChris Wilson 					 "%s, ", engine->name);
32552b284288SChris Wilson 		msg[len-2] = '\0';
32562b284288SChris Wilson 
32572b284288SChris Wilson 		return i915_handle_error(dev_priv, hung, msg);
32582b284288SChris Wilson 	}
325905407ff8SMika Kuoppala 
326005535726SChris Wilson 	/* Reset timer in case GPU hangs without another request being added */
326105407ff8SMika Kuoppala 	if (busy_count)
3262c033666aSChris Wilson 		i915_queue_hangcheck(dev_priv);
326310cd45b6SMika Kuoppala }
326410cd45b6SMika Kuoppala 
32651c69eb42SPaulo Zanoni static void ibx_irq_reset(struct drm_device *dev)
326691738a95SPaulo Zanoni {
3267fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
326891738a95SPaulo Zanoni 
32696e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
327091738a95SPaulo Zanoni 		return;
327191738a95SPaulo Zanoni 
3272f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(SDE);
3273105b122eSPaulo Zanoni 
32746e266956STvrtko Ursulin 	if (HAS_PCH_CPT(dev_priv) || HAS_PCH_LPT(dev_priv))
3275105b122eSPaulo Zanoni 		I915_WRITE(SERR_INT, 0xffffffff);
3276622364b6SPaulo Zanoni }
3277105b122eSPaulo Zanoni 
327891738a95SPaulo Zanoni /*
3279622364b6SPaulo Zanoni  * SDEIER is also touched by the interrupt handler to work around missed PCH
3280622364b6SPaulo Zanoni  * interrupts. Hence we can't update it after the interrupt handler is enabled -
3281622364b6SPaulo Zanoni  * instead we unconditionally enable all PCH interrupt sources here, but then
3282622364b6SPaulo Zanoni  * only unmask them as needed with SDEIMR.
3283622364b6SPaulo Zanoni  *
3284622364b6SPaulo Zanoni  * This function needs to be called before interrupts are enabled.
328591738a95SPaulo Zanoni  */
3286622364b6SPaulo Zanoni static void ibx_irq_pre_postinstall(struct drm_device *dev)
3287622364b6SPaulo Zanoni {
3288fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3289622364b6SPaulo Zanoni 
32906e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
3291622364b6SPaulo Zanoni 		return;
3292622364b6SPaulo Zanoni 
3293622364b6SPaulo Zanoni 	WARN_ON(I915_READ(SDEIER) != 0);
329491738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
329591738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
329691738a95SPaulo Zanoni }
329791738a95SPaulo Zanoni 
32987c4d664eSPaulo Zanoni static void gen5_gt_irq_reset(struct drm_device *dev)
3299d18ea1b5SDaniel Vetter {
3300fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3301d18ea1b5SDaniel Vetter 
3302f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GT);
3303a9d356a6SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6)
3304f86f3fb0SPaulo Zanoni 		GEN5_IRQ_RESET(GEN6_PM);
3305d18ea1b5SDaniel Vetter }
3306d18ea1b5SDaniel Vetter 
330770591a41SVille Syrjälä static void vlv_display_irq_reset(struct drm_i915_private *dev_priv)
330870591a41SVille Syrjälä {
330970591a41SVille Syrjälä 	enum pipe pipe;
331070591a41SVille Syrjälä 
331171b8b41dSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
331271b8b41dSVille Syrjälä 		I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
331371b8b41dSVille Syrjälä 	else
331471b8b41dSVille Syrjälä 		I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
331571b8b41dSVille Syrjälä 
3316ad22d106SVille Syrjälä 	i915_hotplug_interrupt_update_locked(dev_priv, 0xffffffff, 0);
331770591a41SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
331870591a41SVille Syrjälä 
3319ad22d106SVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
3320ad22d106SVille Syrjälä 		I915_WRITE(PIPESTAT(pipe),
3321ad22d106SVille Syrjälä 			   PIPE_FIFO_UNDERRUN_STATUS |
3322ad22d106SVille Syrjälä 			   PIPESTAT_INT_STATUS_MASK);
3323ad22d106SVille Syrjälä 		dev_priv->pipestat_irq_mask[pipe] = 0;
3324ad22d106SVille Syrjälä 	}
332570591a41SVille Syrjälä 
332670591a41SVille Syrjälä 	GEN5_IRQ_RESET(VLV_);
3327ad22d106SVille Syrjälä 	dev_priv->irq_mask = ~0;
332870591a41SVille Syrjälä }
332970591a41SVille Syrjälä 
33308bb61306SVille Syrjälä static void vlv_display_irq_postinstall(struct drm_i915_private *dev_priv)
33318bb61306SVille Syrjälä {
33328bb61306SVille Syrjälä 	u32 pipestat_mask;
33339ab981f2SVille Syrjälä 	u32 enable_mask;
33348bb61306SVille Syrjälä 	enum pipe pipe;
33358bb61306SVille Syrjälä 
33368bb61306SVille Syrjälä 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
33378bb61306SVille Syrjälä 			PIPE_CRC_DONE_INTERRUPT_STATUS;
33388bb61306SVille Syrjälä 
33398bb61306SVille Syrjälä 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
33408bb61306SVille Syrjälä 	for_each_pipe(dev_priv, pipe)
33418bb61306SVille Syrjälä 		i915_enable_pipestat(dev_priv, pipe, pipestat_mask);
33428bb61306SVille Syrjälä 
33439ab981f2SVille Syrjälä 	enable_mask = I915_DISPLAY_PORT_INTERRUPT |
33448bb61306SVille Syrjälä 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
33458bb61306SVille Syrjälä 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
33468bb61306SVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
33479ab981f2SVille Syrjälä 		enable_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
33486b7eafc1SVille Syrjälä 
33496b7eafc1SVille Syrjälä 	WARN_ON(dev_priv->irq_mask != ~0);
33506b7eafc1SVille Syrjälä 
33519ab981f2SVille Syrjälä 	dev_priv->irq_mask = ~enable_mask;
33528bb61306SVille Syrjälä 
33539ab981f2SVille Syrjälä 	GEN5_IRQ_INIT(VLV_, dev_priv->irq_mask, enable_mask);
33548bb61306SVille Syrjälä }
33558bb61306SVille Syrjälä 
33568bb61306SVille Syrjälä /* drm_dma.h hooks
33578bb61306SVille Syrjälä */
33588bb61306SVille Syrjälä static void ironlake_irq_reset(struct drm_device *dev)
33598bb61306SVille Syrjälä {
3360fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
33618bb61306SVille Syrjälä 
33628bb61306SVille Syrjälä 	I915_WRITE(HWSTAM, 0xffffffff);
33638bb61306SVille Syrjälä 
33648bb61306SVille Syrjälä 	GEN5_IRQ_RESET(DE);
33655db94019STvrtko Ursulin 	if (IS_GEN7(dev_priv))
33668bb61306SVille Syrjälä 		I915_WRITE(GEN7_ERR_INT, 0xffffffff);
33678bb61306SVille Syrjälä 
33688bb61306SVille Syrjälä 	gen5_gt_irq_reset(dev);
33698bb61306SVille Syrjälä 
33708bb61306SVille Syrjälä 	ibx_irq_reset(dev);
33718bb61306SVille Syrjälä }
33728bb61306SVille Syrjälä 
33737e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
33747e231dbeSJesse Barnes {
3375fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
33767e231dbeSJesse Barnes 
337734c7b8a7SVille Syrjälä 	I915_WRITE(VLV_MASTER_IER, 0);
337834c7b8a7SVille Syrjälä 	POSTING_READ(VLV_MASTER_IER);
337934c7b8a7SVille Syrjälä 
33807c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
33817e231dbeSJesse Barnes 
3382ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
33839918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
338470591a41SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3385ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
33867e231dbeSJesse Barnes }
33877e231dbeSJesse Barnes 
3388d6e3cca3SDaniel Vetter static void gen8_gt_irq_reset(struct drm_i915_private *dev_priv)
3389d6e3cca3SDaniel Vetter {
3390d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 0);
3391d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 1);
3392d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 2);
3393d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 3);
3394d6e3cca3SDaniel Vetter }
3395d6e3cca3SDaniel Vetter 
3396823f6b38SPaulo Zanoni static void gen8_irq_reset(struct drm_device *dev)
3397abd58f01SBen Widawsky {
3398fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3399abd58f01SBen Widawsky 	int pipe;
3400abd58f01SBen Widawsky 
3401abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
3402abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3403abd58f01SBen Widawsky 
3404d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
3405abd58f01SBen Widawsky 
3406055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3407f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3408813bde43SPaulo Zanoni 						   POWER_DOMAIN_PIPE(pipe)))
3409f86f3fb0SPaulo Zanoni 			GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3410abd58f01SBen Widawsky 
3411f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_PORT_);
3412f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_MISC_);
3413f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_PCU_);
3414abd58f01SBen Widawsky 
34156e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
34161c69eb42SPaulo Zanoni 		ibx_irq_reset(dev);
3417abd58f01SBen Widawsky }
3418abd58f01SBen Widawsky 
34194c6c03beSDamien Lespiau void gen8_irq_power_well_post_enable(struct drm_i915_private *dev_priv,
34204c6c03beSDamien Lespiau 				     unsigned int pipe_mask)
3421d49bdb0eSPaulo Zanoni {
34221180e206SPaulo Zanoni 	uint32_t extra_ier = GEN8_PIPE_VBLANK | GEN8_PIPE_FIFO_UNDERRUN;
34236831f3e3SVille Syrjälä 	enum pipe pipe;
3424d49bdb0eSPaulo Zanoni 
342513321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
34266831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
34276831f3e3SVille Syrjälä 		GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
34286831f3e3SVille Syrjälä 				  dev_priv->de_irq_mask[pipe],
34296831f3e3SVille Syrjälä 				  ~dev_priv->de_irq_mask[pipe] | extra_ier);
343013321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3431d49bdb0eSPaulo Zanoni }
3432d49bdb0eSPaulo Zanoni 
3433aae8ba84SVille Syrjälä void gen8_irq_power_well_pre_disable(struct drm_i915_private *dev_priv,
3434aae8ba84SVille Syrjälä 				     unsigned int pipe_mask)
3435aae8ba84SVille Syrjälä {
34366831f3e3SVille Syrjälä 	enum pipe pipe;
34376831f3e3SVille Syrjälä 
3438aae8ba84SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
34396831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
34406831f3e3SVille Syrjälä 		GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3441aae8ba84SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3442aae8ba84SVille Syrjälä 
3443aae8ba84SVille Syrjälä 	/* make sure we're done processing display irqs */
344491c8a326SChris Wilson 	synchronize_irq(dev_priv->drm.irq);
3445aae8ba84SVille Syrjälä }
3446aae8ba84SVille Syrjälä 
344743f328d7SVille Syrjälä static void cherryview_irq_preinstall(struct drm_device *dev)
344843f328d7SVille Syrjälä {
3449fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
345043f328d7SVille Syrjälä 
345143f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
345243f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
345343f328d7SVille Syrjälä 
3454d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
345543f328d7SVille Syrjälä 
345643f328d7SVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
345743f328d7SVille Syrjälä 
3458ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
34599918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
346070591a41SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3461ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
346243f328d7SVille Syrjälä }
346343f328d7SVille Syrjälä 
346491d14251STvrtko Ursulin static u32 intel_hpd_enabled_irqs(struct drm_i915_private *dev_priv,
346587a02106SVille Syrjälä 				  const u32 hpd[HPD_NUM_PINS])
346687a02106SVille Syrjälä {
346787a02106SVille Syrjälä 	struct intel_encoder *encoder;
346887a02106SVille Syrjälä 	u32 enabled_irqs = 0;
346987a02106SVille Syrjälä 
347091c8a326SChris Wilson 	for_each_intel_encoder(&dev_priv->drm, encoder)
347187a02106SVille Syrjälä 		if (dev_priv->hotplug.stats[encoder->hpd_pin].state == HPD_ENABLED)
347287a02106SVille Syrjälä 			enabled_irqs |= hpd[encoder->hpd_pin];
347387a02106SVille Syrjälä 
347487a02106SVille Syrjälä 	return enabled_irqs;
347587a02106SVille Syrjälä }
347687a02106SVille Syrjälä 
347791d14251STvrtko Ursulin static void ibx_hpd_irq_setup(struct drm_i915_private *dev_priv)
347882a28bcfSDaniel Vetter {
347987a02106SVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
348082a28bcfSDaniel Vetter 
348191d14251STvrtko Ursulin 	if (HAS_PCH_IBX(dev_priv)) {
3482fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
348391d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ibx);
348482a28bcfSDaniel Vetter 	} else {
3485fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
348691d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_cpt);
348782a28bcfSDaniel Vetter 	}
348882a28bcfSDaniel Vetter 
3489fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
349082a28bcfSDaniel Vetter 
34917fe0b973SKeith Packard 	/*
34927fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
34936dbf30ceSVille Syrjälä 	 * duration to 2ms (which is the minimum in the Display Port spec).
34946dbf30ceSVille Syrjälä 	 * The pulse duration bits are reserved on LPT+.
34957fe0b973SKeith Packard 	 */
34967fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
34977fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
34987fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
34997fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
35007fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
35010b2eb33eSVille Syrjälä 	/*
35020b2eb33eSVille Syrjälä 	 * When CPU and PCH are on the same package, port A
35030b2eb33eSVille Syrjälä 	 * HPD must be enabled in both north and south.
35040b2eb33eSVille Syrjälä 	 */
350591d14251STvrtko Ursulin 	if (HAS_PCH_LPT_LP(dev_priv))
35060b2eb33eSVille Syrjälä 		hotplug |= PORTA_HOTPLUG_ENABLE;
35077fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
35086dbf30ceSVille Syrjälä }
350926951cafSXiong Zhang 
351091d14251STvrtko Ursulin static void spt_hpd_irq_setup(struct drm_i915_private *dev_priv)
35116dbf30ceSVille Syrjälä {
35126dbf30ceSVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
35136dbf30ceSVille Syrjälä 
35146dbf30ceSVille Syrjälä 	hotplug_irqs = SDE_HOTPLUG_MASK_SPT;
351591d14251STvrtko Ursulin 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_spt);
35166dbf30ceSVille Syrjälä 
35176dbf30ceSVille Syrjälä 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
35186dbf30ceSVille Syrjälä 
35196dbf30ceSVille Syrjälä 	/* Enable digital hotplug on the PCH */
35206dbf30ceSVille Syrjälä 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
35216dbf30ceSVille Syrjälä 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTC_HOTPLUG_ENABLE |
352274c0b395SVille Syrjälä 		PORTB_HOTPLUG_ENABLE | PORTA_HOTPLUG_ENABLE;
35236dbf30ceSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
35246dbf30ceSVille Syrjälä 
352526951cafSXiong Zhang 	hotplug = I915_READ(PCH_PORT_HOTPLUG2);
352626951cafSXiong Zhang 	hotplug |= PORTE_HOTPLUG_ENABLE;
352726951cafSXiong Zhang 	I915_WRITE(PCH_PORT_HOTPLUG2, hotplug);
352826951cafSXiong Zhang }
35297fe0b973SKeith Packard 
353091d14251STvrtko Ursulin static void ilk_hpd_irq_setup(struct drm_i915_private *dev_priv)
3531e4ce95aaSVille Syrjälä {
3532e4ce95aaSVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
3533e4ce95aaSVille Syrjälä 
353491d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 8) {
35353a3b3c7dSVille Syrjälä 		hotplug_irqs = GEN8_PORT_DP_A_HOTPLUG;
353691d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_bdw);
35373a3b3c7dSVille Syrjälä 
35383a3b3c7dSVille Syrjälä 		bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
353991d14251STvrtko Ursulin 	} else if (INTEL_GEN(dev_priv) >= 7) {
354023bb4cb5SVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG_IVB;
354191d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ivb);
35423a3b3c7dSVille Syrjälä 
35433a3b3c7dSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
354423bb4cb5SVille Syrjälä 	} else {
3545e4ce95aaSVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG;
354691d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ilk);
3547e4ce95aaSVille Syrjälä 
3548e4ce95aaSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
35493a3b3c7dSVille Syrjälä 	}
3550e4ce95aaSVille Syrjälä 
3551e4ce95aaSVille Syrjälä 	/*
3552e4ce95aaSVille Syrjälä 	 * Enable digital hotplug on the CPU, and configure the DP short pulse
3553e4ce95aaSVille Syrjälä 	 * duration to 2ms (which is the minimum in the Display Port spec)
355423bb4cb5SVille Syrjälä 	 * The pulse duration bits are reserved on HSW+.
3555e4ce95aaSVille Syrjälä 	 */
3556e4ce95aaSVille Syrjälä 	hotplug = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
3557e4ce95aaSVille Syrjälä 	hotplug &= ~DIGITAL_PORTA_PULSE_DURATION_MASK;
3558e4ce95aaSVille Syrjälä 	hotplug |= DIGITAL_PORTA_HOTPLUG_ENABLE | DIGITAL_PORTA_PULSE_DURATION_2ms;
3559e4ce95aaSVille Syrjälä 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, hotplug);
3560e4ce95aaSVille Syrjälä 
356191d14251STvrtko Ursulin 	ibx_hpd_irq_setup(dev_priv);
3562e4ce95aaSVille Syrjälä }
3563e4ce95aaSVille Syrjälä 
356491d14251STvrtko Ursulin static void bxt_hpd_irq_setup(struct drm_i915_private *dev_priv)
3565e0a20ad7SShashank Sharma {
3566a52bb15bSVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
3567e0a20ad7SShashank Sharma 
356891d14251STvrtko Ursulin 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_bxt);
3569a52bb15bSVille Syrjälä 	hotplug_irqs = BXT_DE_PORT_HOTPLUG_MASK;
3570e0a20ad7SShashank Sharma 
3571a52bb15bSVille Syrjälä 	bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
3572e0a20ad7SShashank Sharma 
3573a52bb15bSVille Syrjälä 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
3574a52bb15bSVille Syrjälä 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTB_HOTPLUG_ENABLE |
3575a52bb15bSVille Syrjälä 		PORTA_HOTPLUG_ENABLE;
3576d252bf68SShubhangi Shrivastava 
3577d252bf68SShubhangi Shrivastava 	DRM_DEBUG_KMS("Invert bit setting: hp_ctl:%x hp_port:%x\n",
3578d252bf68SShubhangi Shrivastava 		      hotplug, enabled_irqs);
3579d252bf68SShubhangi Shrivastava 	hotplug &= ~BXT_DDI_HPD_INVERT_MASK;
3580d252bf68SShubhangi Shrivastava 
3581d252bf68SShubhangi Shrivastava 	/*
3582d252bf68SShubhangi Shrivastava 	 * For BXT invert bit has to be set based on AOB design
3583d252bf68SShubhangi Shrivastava 	 * for HPD detection logic, update it based on VBT fields.
3584d252bf68SShubhangi Shrivastava 	 */
3585d252bf68SShubhangi Shrivastava 
3586d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIA) &&
3587d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_A))
3588d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIA_HPD_INVERT;
3589d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIB) &&
3590d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_B))
3591d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIB_HPD_INVERT;
3592d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIC) &&
3593d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_C))
3594d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIC_HPD_INVERT;
3595d252bf68SShubhangi Shrivastava 
3596a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
3597e0a20ad7SShashank Sharma }
3598e0a20ad7SShashank Sharma 
3599d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
3600d46da437SPaulo Zanoni {
3601fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
360282a28bcfSDaniel Vetter 	u32 mask;
3603d46da437SPaulo Zanoni 
36046e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
3605692a04cfSDaniel Vetter 		return;
3606692a04cfSDaniel Vetter 
36076e266956STvrtko Ursulin 	if (HAS_PCH_IBX(dev_priv))
36085c673b60SDaniel Vetter 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
3609105b122eSPaulo Zanoni 	else
36105c673b60SDaniel Vetter 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
36118664281bSPaulo Zanoni 
3612b51a2842SVille Syrjälä 	gen5_assert_iir_is_zero(dev_priv, SDEIIR);
3613d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
3614d46da437SPaulo Zanoni }
3615d46da437SPaulo Zanoni 
36160a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
36170a9a8c91SDaniel Vetter {
3618fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
36190a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
36200a9a8c91SDaniel Vetter 
36210a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
36220a9a8c91SDaniel Vetter 
36230a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
36243c9192bcSTvrtko Ursulin 	if (HAS_L3_DPF(dev_priv)) {
36250a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
3626772c2a51STvrtko Ursulin 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev_priv);
3627772c2a51STvrtko Ursulin 		gt_irqs |= GT_PARITY_ERROR(dev_priv);
36280a9a8c91SDaniel Vetter 	}
36290a9a8c91SDaniel Vetter 
36300a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
36315db94019STvrtko Ursulin 	if (IS_GEN5(dev_priv)) {
3632f8973c21SChris Wilson 		gt_irqs |= ILK_BSD_USER_INTERRUPT;
36330a9a8c91SDaniel Vetter 	} else {
36340a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
36350a9a8c91SDaniel Vetter 	}
36360a9a8c91SDaniel Vetter 
363735079899SPaulo Zanoni 	GEN5_IRQ_INIT(GT, dev_priv->gt_irq_mask, gt_irqs);
36380a9a8c91SDaniel Vetter 
36390a9a8c91SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
364078e68d36SImre Deak 		/*
364178e68d36SImre Deak 		 * RPS interrupts will get enabled/disabled on demand when RPS
364278e68d36SImre Deak 		 * itself is enabled/disabled.
364378e68d36SImre Deak 		 */
3644f4e9af4fSAkash Goel 		if (HAS_VEBOX(dev_priv)) {
36450a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
3646f4e9af4fSAkash Goel 			dev_priv->pm_ier |= PM_VEBOX_USER_INTERRUPT;
3647f4e9af4fSAkash Goel 		}
36480a9a8c91SDaniel Vetter 
3649f4e9af4fSAkash Goel 		dev_priv->pm_imr = 0xffffffff;
3650f4e9af4fSAkash Goel 		GEN5_IRQ_INIT(GEN6_PM, dev_priv->pm_imr, pm_irqs);
36510a9a8c91SDaniel Vetter 	}
36520a9a8c91SDaniel Vetter }
36530a9a8c91SDaniel Vetter 
3654f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
3655036a4a7dSZhenyu Wang {
3656fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
36578e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
36588e76f8dcSPaulo Zanoni 
36598e76f8dcSPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 7) {
36608e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
36618e76f8dcSPaulo Zanoni 				DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
36628e76f8dcSPaulo Zanoni 				DE_PLANEB_FLIP_DONE_IVB |
36635c673b60SDaniel Vetter 				DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB);
36648e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
366523bb4cb5SVille Syrjälä 			      DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB |
366623bb4cb5SVille Syrjälä 			      DE_DP_A_HOTPLUG_IVB);
36678e76f8dcSPaulo Zanoni 	} else {
36688e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3669ce99c256SDaniel Vetter 				DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
36705b3a856bSDaniel Vetter 				DE_AUX_CHANNEL_A |
36715b3a856bSDaniel Vetter 				DE_PIPEB_CRC_DONE | DE_PIPEA_CRC_DONE |
36725b3a856bSDaniel Vetter 				DE_POISON);
3673e4ce95aaSVille Syrjälä 		extra_mask = (DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
3674e4ce95aaSVille Syrjälä 			      DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN |
3675e4ce95aaSVille Syrjälä 			      DE_DP_A_HOTPLUG);
36768e76f8dcSPaulo Zanoni 	}
3677036a4a7dSZhenyu Wang 
36781ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3679036a4a7dSZhenyu Wang 
36800c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xeffe);
36810c841212SPaulo Zanoni 
3682622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3683622364b6SPaulo Zanoni 
368435079899SPaulo Zanoni 	GEN5_IRQ_INIT(DE, dev_priv->irq_mask, display_mask | extra_mask);
3685036a4a7dSZhenyu Wang 
36860a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
3687036a4a7dSZhenyu Wang 
3688d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
36897fe0b973SKeith Packard 
369050a0bc90STvrtko Ursulin 	if (IS_IRONLAKE_M(dev_priv)) {
36916005ce42SDaniel Vetter 		/* Enable PCU event interrupts
36926005ce42SDaniel Vetter 		 *
36936005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
36944bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
36954bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
3696d6207435SDaniel Vetter 		spin_lock_irq(&dev_priv->irq_lock);
3697fbdedaeaSVille Syrjälä 		ilk_enable_display_irq(dev_priv, DE_PCU_EVENT);
3698d6207435SDaniel Vetter 		spin_unlock_irq(&dev_priv->irq_lock);
3699f97108d1SJesse Barnes 	}
3700f97108d1SJesse Barnes 
3701036a4a7dSZhenyu Wang 	return 0;
3702036a4a7dSZhenyu Wang }
3703036a4a7dSZhenyu Wang 
3704f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3705f8b79e58SImre Deak {
3706f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3707f8b79e58SImre Deak 
3708f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3709f8b79e58SImre Deak 		return;
3710f8b79e58SImre Deak 
3711f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3712f8b79e58SImre Deak 
3713d6c69803SVille Syrjälä 	if (intel_irqs_enabled(dev_priv)) {
3714d6c69803SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3715ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3716f8b79e58SImre Deak 	}
3717d6c69803SVille Syrjälä }
3718f8b79e58SImre Deak 
3719f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3720f8b79e58SImre Deak {
3721f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3722f8b79e58SImre Deak 
3723f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3724f8b79e58SImre Deak 		return;
3725f8b79e58SImre Deak 
3726f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3727f8b79e58SImre Deak 
3728950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3729ad22d106SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3730f8b79e58SImre Deak }
3731f8b79e58SImre Deak 
37320e6c9a9eSVille Syrjälä 
37330e6c9a9eSVille Syrjälä static int valleyview_irq_postinstall(struct drm_device *dev)
37340e6c9a9eSVille Syrjälä {
3735fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
37360e6c9a9eSVille Syrjälä 
37370a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
37387e231dbeSJesse Barnes 
3739ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
37409918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3741ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3742ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3743ad22d106SVille Syrjälä 
37447e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
374534c7b8a7SVille Syrjälä 	POSTING_READ(VLV_MASTER_IER);
374620afbda2SDaniel Vetter 
374720afbda2SDaniel Vetter 	return 0;
374820afbda2SDaniel Vetter }
374920afbda2SDaniel Vetter 
3750abd58f01SBen Widawsky static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
3751abd58f01SBen Widawsky {
3752abd58f01SBen Widawsky 	/* These are interrupts we'll toggle with the ring mask register */
3753abd58f01SBen Widawsky 	uint32_t gt_interrupts[] = {
3754abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
375573d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
375673d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT |
375773d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT,
3758abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
375973d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
376073d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT |
376173d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT,
3762abd58f01SBen Widawsky 		0,
376373d477f6SOscar Mateo 		GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT |
376473d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT
3765abd58f01SBen Widawsky 		};
3766abd58f01SBen Widawsky 
376798735739STvrtko Ursulin 	if (HAS_L3_DPF(dev_priv))
376898735739STvrtko Ursulin 		gt_interrupts[0] |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
376998735739STvrtko Ursulin 
3770f4e9af4fSAkash Goel 	dev_priv->pm_ier = 0x0;
3771f4e9af4fSAkash Goel 	dev_priv->pm_imr = ~dev_priv->pm_ier;
37729a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 0, ~gt_interrupts[0], gt_interrupts[0]);
37739a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 1, ~gt_interrupts[1], gt_interrupts[1]);
377478e68d36SImre Deak 	/*
377578e68d36SImre Deak 	 * RPS interrupts will get enabled/disabled on demand when RPS itself
377626705e20SSagar Arun Kamble 	 * is enabled/disabled. Same wil be the case for GuC interrupts.
377778e68d36SImre Deak 	 */
3778f4e9af4fSAkash Goel 	GEN8_IRQ_INIT_NDX(GT, 2, dev_priv->pm_imr, dev_priv->pm_ier);
37799a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 3, ~gt_interrupts[3], gt_interrupts[3]);
3780abd58f01SBen Widawsky }
3781abd58f01SBen Widawsky 
3782abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3783abd58f01SBen Widawsky {
3784770de83dSDamien Lespiau 	uint32_t de_pipe_masked = GEN8_PIPE_CDCLK_CRC_DONE;
3785770de83dSDamien Lespiau 	uint32_t de_pipe_enables;
37863a3b3c7dSVille Syrjälä 	u32 de_port_masked = GEN8_AUX_CHANNEL_A;
37873a3b3c7dSVille Syrjälä 	u32 de_port_enables;
378811825b0dSVille Syrjälä 	u32 de_misc_masked = GEN8_DE_MISC_GSE;
37893a3b3c7dSVille Syrjälä 	enum pipe pipe;
3790770de83dSDamien Lespiau 
3791b4834a50SRodrigo Vivi 	if (INTEL_INFO(dev_priv)->gen >= 9) {
3792770de83dSDamien Lespiau 		de_pipe_masked |= GEN9_PIPE_PLANE1_FLIP_DONE |
3793770de83dSDamien Lespiau 				  GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
37943a3b3c7dSVille Syrjälä 		de_port_masked |= GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
379588e04703SJesse Barnes 				  GEN9_AUX_CHANNEL_D;
37969e63743eSShashank Sharma 		if (IS_BROXTON(dev_priv))
37973a3b3c7dSVille Syrjälä 			de_port_masked |= BXT_DE_PORT_GMBUS;
37983a3b3c7dSVille Syrjälä 	} else {
3799770de83dSDamien Lespiau 		de_pipe_masked |= GEN8_PIPE_PRIMARY_FLIP_DONE |
3800770de83dSDamien Lespiau 				  GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
38013a3b3c7dSVille Syrjälä 	}
3802770de83dSDamien Lespiau 
3803770de83dSDamien Lespiau 	de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
3804770de83dSDamien Lespiau 					   GEN8_PIPE_FIFO_UNDERRUN;
3805770de83dSDamien Lespiau 
38063a3b3c7dSVille Syrjälä 	de_port_enables = de_port_masked;
3807a52bb15bSVille Syrjälä 	if (IS_BROXTON(dev_priv))
3808a52bb15bSVille Syrjälä 		de_port_enables |= BXT_DE_PORT_HOTPLUG_MASK;
3809a52bb15bSVille Syrjälä 	else if (IS_BROADWELL(dev_priv))
38103a3b3c7dSVille Syrjälä 		de_port_enables |= GEN8_PORT_DP_A_HOTPLUG;
38113a3b3c7dSVille Syrjälä 
381213b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_A] = ~de_pipe_masked;
381313b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_B] = ~de_pipe_masked;
381413b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_C] = ~de_pipe_masked;
3815abd58f01SBen Widawsky 
3816055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3817f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3818813bde43SPaulo Zanoni 				POWER_DOMAIN_PIPE(pipe)))
3819813bde43SPaulo Zanoni 			GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
3820813bde43SPaulo Zanoni 					  dev_priv->de_irq_mask[pipe],
382135079899SPaulo Zanoni 					  de_pipe_enables);
3822abd58f01SBen Widawsky 
38233a3b3c7dSVille Syrjälä 	GEN5_IRQ_INIT(GEN8_DE_PORT_, ~de_port_masked, de_port_enables);
382411825b0dSVille Syrjälä 	GEN5_IRQ_INIT(GEN8_DE_MISC_, ~de_misc_masked, de_misc_masked);
3825abd58f01SBen Widawsky }
3826abd58f01SBen Widawsky 
3827abd58f01SBen Widawsky static int gen8_irq_postinstall(struct drm_device *dev)
3828abd58f01SBen Widawsky {
3829fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3830abd58f01SBen Widawsky 
38316e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
3832622364b6SPaulo Zanoni 		ibx_irq_pre_postinstall(dev);
3833622364b6SPaulo Zanoni 
3834abd58f01SBen Widawsky 	gen8_gt_irq_postinstall(dev_priv);
3835abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3836abd58f01SBen Widawsky 
38376e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
3838abd58f01SBen Widawsky 		ibx_irq_postinstall(dev);
3839abd58f01SBen Widawsky 
3840e5328c43SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
3841abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3842abd58f01SBen Widawsky 
3843abd58f01SBen Widawsky 	return 0;
3844abd58f01SBen Widawsky }
3845abd58f01SBen Widawsky 
384643f328d7SVille Syrjälä static int cherryview_irq_postinstall(struct drm_device *dev)
384743f328d7SVille Syrjälä {
3848fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
384943f328d7SVille Syrjälä 
385043f328d7SVille Syrjälä 	gen8_gt_irq_postinstall(dev_priv);
385143f328d7SVille Syrjälä 
3852ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
38539918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3854ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3855ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3856ad22d106SVille Syrjälä 
3857e5328c43SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
385843f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
385943f328d7SVille Syrjälä 
386043f328d7SVille Syrjälä 	return 0;
386143f328d7SVille Syrjälä }
386243f328d7SVille Syrjälä 
3863abd58f01SBen Widawsky static void gen8_irq_uninstall(struct drm_device *dev)
3864abd58f01SBen Widawsky {
3865fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3866abd58f01SBen Widawsky 
3867abd58f01SBen Widawsky 	if (!dev_priv)
3868abd58f01SBen Widawsky 		return;
3869abd58f01SBen Widawsky 
3870823f6b38SPaulo Zanoni 	gen8_irq_reset(dev);
3871abd58f01SBen Widawsky }
3872abd58f01SBen Widawsky 
38737e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
38747e231dbeSJesse Barnes {
3875fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
38767e231dbeSJesse Barnes 
38777e231dbeSJesse Barnes 	if (!dev_priv)
38787e231dbeSJesse Barnes 		return;
38797e231dbeSJesse Barnes 
3880843d0e7dSImre Deak 	I915_WRITE(VLV_MASTER_IER, 0);
388134c7b8a7SVille Syrjälä 	POSTING_READ(VLV_MASTER_IER);
3882843d0e7dSImre Deak 
3883893fce8eSVille Syrjälä 	gen5_gt_irq_reset(dev);
3884893fce8eSVille Syrjälä 
38857e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
3886f8b79e58SImre Deak 
3887ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
38889918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3889ad22d106SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3890ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
38917e231dbeSJesse Barnes }
38927e231dbeSJesse Barnes 
389343f328d7SVille Syrjälä static void cherryview_irq_uninstall(struct drm_device *dev)
389443f328d7SVille Syrjälä {
3895fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
389643f328d7SVille Syrjälä 
389743f328d7SVille Syrjälä 	if (!dev_priv)
389843f328d7SVille Syrjälä 		return;
389943f328d7SVille Syrjälä 
390043f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
390143f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
390243f328d7SVille Syrjälä 
3903a2c30fbaSVille Syrjälä 	gen8_gt_irq_reset(dev_priv);
390443f328d7SVille Syrjälä 
3905a2c30fbaSVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
390643f328d7SVille Syrjälä 
3907ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
39089918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3909ad22d106SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3910ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
391143f328d7SVille Syrjälä }
391243f328d7SVille Syrjälä 
3913f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
3914036a4a7dSZhenyu Wang {
3915fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
39164697995bSJesse Barnes 
39174697995bSJesse Barnes 	if (!dev_priv)
39184697995bSJesse Barnes 		return;
39194697995bSJesse Barnes 
3920be30b29fSPaulo Zanoni 	ironlake_irq_reset(dev);
3921036a4a7dSZhenyu Wang }
3922036a4a7dSZhenyu Wang 
3923c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
3924c2798b19SChris Wilson {
3925fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3926c2798b19SChris Wilson 	int pipe;
3927c2798b19SChris Wilson 
3928055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3929c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3930c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3931c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3932c2798b19SChris Wilson 	POSTING_READ16(IER);
3933c2798b19SChris Wilson }
3934c2798b19SChris Wilson 
3935c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
3936c2798b19SChris Wilson {
3937fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3938c2798b19SChris Wilson 
3939c2798b19SChris Wilson 	I915_WRITE16(EMR,
3940c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3941c2798b19SChris Wilson 
3942c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
3943c2798b19SChris Wilson 	dev_priv->irq_mask =
3944c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3945c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3946c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
394737ef01abSDaniel Vetter 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
3948c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
3949c2798b19SChris Wilson 
3950c2798b19SChris Wilson 	I915_WRITE16(IER,
3951c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3952c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3953c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
3954c2798b19SChris Wilson 	POSTING_READ16(IER);
3955c2798b19SChris Wilson 
3956379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3957379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3958d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3959755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3960755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3961d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3962379ef82dSDaniel Vetter 
3963c2798b19SChris Wilson 	return 0;
3964c2798b19SChris Wilson }
3965c2798b19SChris Wilson 
39665a21b665SDaniel Vetter /*
39675a21b665SDaniel Vetter  * Returns true when a page flip has completed.
39685a21b665SDaniel Vetter  */
39695a21b665SDaniel Vetter static bool i8xx_handle_vblank(struct drm_i915_private *dev_priv,
39705a21b665SDaniel Vetter 			       int plane, int pipe, u32 iir)
39715a21b665SDaniel Vetter {
39725a21b665SDaniel Vetter 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
39735a21b665SDaniel Vetter 
39745a21b665SDaniel Vetter 	if (!intel_pipe_handle_vblank(dev_priv, pipe))
39755a21b665SDaniel Vetter 		return false;
39765a21b665SDaniel Vetter 
39775a21b665SDaniel Vetter 	if ((iir & flip_pending) == 0)
39785a21b665SDaniel Vetter 		goto check_page_flip;
39795a21b665SDaniel Vetter 
39805a21b665SDaniel Vetter 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
39815a21b665SDaniel Vetter 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
39825a21b665SDaniel Vetter 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
39835a21b665SDaniel Vetter 	 * the flip is completed (no longer pending). Since this doesn't raise
39845a21b665SDaniel Vetter 	 * an interrupt per se, we watch for the change at vblank.
39855a21b665SDaniel Vetter 	 */
39865a21b665SDaniel Vetter 	if (I915_READ16(ISR) & flip_pending)
39875a21b665SDaniel Vetter 		goto check_page_flip;
39885a21b665SDaniel Vetter 
39895a21b665SDaniel Vetter 	intel_finish_page_flip_cs(dev_priv, pipe);
39905a21b665SDaniel Vetter 	return true;
39915a21b665SDaniel Vetter 
39925a21b665SDaniel Vetter check_page_flip:
39935a21b665SDaniel Vetter 	intel_check_page_flip(dev_priv, pipe);
39945a21b665SDaniel Vetter 	return false;
39955a21b665SDaniel Vetter }
39965a21b665SDaniel Vetter 
3997ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3998c2798b19SChris Wilson {
399945a83f84SDaniel Vetter 	struct drm_device *dev = arg;
4000fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4001c2798b19SChris Wilson 	u16 iir, new_iir;
4002c2798b19SChris Wilson 	u32 pipe_stats[2];
4003c2798b19SChris Wilson 	int pipe;
4004c2798b19SChris Wilson 	u16 flip_mask =
4005c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
4006c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
40071f814dacSImre Deak 	irqreturn_t ret;
4008c2798b19SChris Wilson 
40092dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
40102dd2a883SImre Deak 		return IRQ_NONE;
40112dd2a883SImre Deak 
40121f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
40131f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
40141f814dacSImre Deak 
40151f814dacSImre Deak 	ret = IRQ_NONE;
4016c2798b19SChris Wilson 	iir = I915_READ16(IIR);
4017c2798b19SChris Wilson 	if (iir == 0)
40181f814dacSImre Deak 		goto out;
4019c2798b19SChris Wilson 
4020c2798b19SChris Wilson 	while (iir & ~flip_mask) {
4021c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4022c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4023c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4024c2798b19SChris Wilson 		 * interrupts (for non-MSI).
4025c2798b19SChris Wilson 		 */
4026222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
4027c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4028aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4029c2798b19SChris Wilson 
4030055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4031f0f59a00SVille Syrjälä 			i915_reg_t reg = PIPESTAT(pipe);
4032c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4033c2798b19SChris Wilson 
4034c2798b19SChris Wilson 			/*
4035c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
4036c2798b19SChris Wilson 			 */
40372d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & 0x8000ffff)
4038c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
4039c2798b19SChris Wilson 		}
4040222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4041c2798b19SChris Wilson 
4042c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
4043c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
4044c2798b19SChris Wilson 
4045c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
40463b3f1650SAkash Goel 			notify_ring(dev_priv->engine[RCS]);
4047c2798b19SChris Wilson 
4048055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
40495a21b665SDaniel Vetter 			int plane = pipe;
40505a21b665SDaniel Vetter 			if (HAS_FBC(dev_priv))
40515a21b665SDaniel Vetter 				plane = !plane;
40525a21b665SDaniel Vetter 
40535a21b665SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
40545a21b665SDaniel Vetter 			    i8xx_handle_vblank(dev_priv, plane, pipe, iir))
40555a21b665SDaniel Vetter 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
4056c2798b19SChris Wilson 
40574356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
405891d14251STvrtko Ursulin 				i9xx_pipe_crc_irq_handler(dev_priv, pipe);
40592d9d2b0bSVille Syrjälä 
40601f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
40611f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
40621f7247c0SDaniel Vetter 								    pipe);
40634356d586SDaniel Vetter 		}
4064c2798b19SChris Wilson 
4065c2798b19SChris Wilson 		iir = new_iir;
4066c2798b19SChris Wilson 	}
40671f814dacSImre Deak 	ret = IRQ_HANDLED;
4068c2798b19SChris Wilson 
40691f814dacSImre Deak out:
40701f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
40711f814dacSImre Deak 
40721f814dacSImre Deak 	return ret;
4073c2798b19SChris Wilson }
4074c2798b19SChris Wilson 
4075c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
4076c2798b19SChris Wilson {
4077fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4078c2798b19SChris Wilson 	int pipe;
4079c2798b19SChris Wilson 
4080055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
4081c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
4082c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4083c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
4084c2798b19SChris Wilson 	}
4085c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
4086c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
4087c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
4088c2798b19SChris Wilson }
4089c2798b19SChris Wilson 
4090a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
4091a266c7d5SChris Wilson {
4092fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4093a266c7d5SChris Wilson 	int pipe;
4094a266c7d5SChris Wilson 
4095a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
40960706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4097a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4098a266c7d5SChris Wilson 	}
4099a266c7d5SChris Wilson 
410000d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
4101055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4102a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4103a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4104a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4105a266c7d5SChris Wilson 	POSTING_READ(IER);
4106a266c7d5SChris Wilson }
4107a266c7d5SChris Wilson 
4108a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
4109a266c7d5SChris Wilson {
4110fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
411138bde180SChris Wilson 	u32 enable_mask;
4112a266c7d5SChris Wilson 
411338bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
411438bde180SChris Wilson 
411538bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
411638bde180SChris Wilson 	dev_priv->irq_mask =
411738bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
411838bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
411938bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
412038bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
412137ef01abSDaniel Vetter 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
412238bde180SChris Wilson 
412338bde180SChris Wilson 	enable_mask =
412438bde180SChris Wilson 		I915_ASLE_INTERRUPT |
412538bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
412638bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
412738bde180SChris Wilson 		I915_USER_INTERRUPT;
412838bde180SChris Wilson 
4129a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
41300706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
413120afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
413220afbda2SDaniel Vetter 
4133a266c7d5SChris Wilson 		/* Enable in IER... */
4134a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
4135a266c7d5SChris Wilson 		/* and unmask in IMR */
4136a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
4137a266c7d5SChris Wilson 	}
4138a266c7d5SChris Wilson 
4139a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
4140a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
4141a266c7d5SChris Wilson 	POSTING_READ(IER);
4142a266c7d5SChris Wilson 
414391d14251STvrtko Ursulin 	i915_enable_asle_pipestat(dev_priv);
414420afbda2SDaniel Vetter 
4145379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4146379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4147d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4148755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4149755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4150d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4151379ef82dSDaniel Vetter 
415220afbda2SDaniel Vetter 	return 0;
415320afbda2SDaniel Vetter }
415420afbda2SDaniel Vetter 
41555a21b665SDaniel Vetter /*
41565a21b665SDaniel Vetter  * Returns true when a page flip has completed.
41575a21b665SDaniel Vetter  */
41585a21b665SDaniel Vetter static bool i915_handle_vblank(struct drm_i915_private *dev_priv,
41595a21b665SDaniel Vetter 			       int plane, int pipe, u32 iir)
41605a21b665SDaniel Vetter {
41615a21b665SDaniel Vetter 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
41625a21b665SDaniel Vetter 
41635a21b665SDaniel Vetter 	if (!intel_pipe_handle_vblank(dev_priv, pipe))
41645a21b665SDaniel Vetter 		return false;
41655a21b665SDaniel Vetter 
41665a21b665SDaniel Vetter 	if ((iir & flip_pending) == 0)
41675a21b665SDaniel Vetter 		goto check_page_flip;
41685a21b665SDaniel Vetter 
41695a21b665SDaniel Vetter 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
41705a21b665SDaniel Vetter 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
41715a21b665SDaniel Vetter 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
41725a21b665SDaniel Vetter 	 * the flip is completed (no longer pending). Since this doesn't raise
41735a21b665SDaniel Vetter 	 * an interrupt per se, we watch for the change at vblank.
41745a21b665SDaniel Vetter 	 */
41755a21b665SDaniel Vetter 	if (I915_READ(ISR) & flip_pending)
41765a21b665SDaniel Vetter 		goto check_page_flip;
41775a21b665SDaniel Vetter 
41785a21b665SDaniel Vetter 	intel_finish_page_flip_cs(dev_priv, pipe);
41795a21b665SDaniel Vetter 	return true;
41805a21b665SDaniel Vetter 
41815a21b665SDaniel Vetter check_page_flip:
41825a21b665SDaniel Vetter 	intel_check_page_flip(dev_priv, pipe);
41835a21b665SDaniel Vetter 	return false;
41845a21b665SDaniel Vetter }
41855a21b665SDaniel Vetter 
4186ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
4187a266c7d5SChris Wilson {
418845a83f84SDaniel Vetter 	struct drm_device *dev = arg;
4189fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
41908291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
419138bde180SChris Wilson 	u32 flip_mask =
419238bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
419338bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
419438bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
4195a266c7d5SChris Wilson 
41962dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
41972dd2a883SImre Deak 		return IRQ_NONE;
41982dd2a883SImre Deak 
41991f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
42001f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
42011f814dacSImre Deak 
4202a266c7d5SChris Wilson 	iir = I915_READ(IIR);
420338bde180SChris Wilson 	do {
420438bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
42058291ee90SChris Wilson 		bool blc_event = false;
4206a266c7d5SChris Wilson 
4207a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4208a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4209a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4210a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
4211a266c7d5SChris Wilson 		 */
4212222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
4213a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4214aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4215a266c7d5SChris Wilson 
4216055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4217f0f59a00SVille Syrjälä 			i915_reg_t reg = PIPESTAT(pipe);
4218a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4219a266c7d5SChris Wilson 
422038bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
4221a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4222a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
422338bde180SChris Wilson 				irq_received = true;
4224a266c7d5SChris Wilson 			}
4225a266c7d5SChris Wilson 		}
4226222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4227a266c7d5SChris Wilson 
4228a266c7d5SChris Wilson 		if (!irq_received)
4229a266c7d5SChris Wilson 			break;
4230a266c7d5SChris Wilson 
4231a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
423291d14251STvrtko Ursulin 		if (I915_HAS_HOTPLUG(dev_priv) &&
42331ae3c34cSVille Syrjälä 		    iir & I915_DISPLAY_PORT_INTERRUPT) {
42341ae3c34cSVille Syrjälä 			u32 hotplug_status = i9xx_hpd_irq_ack(dev_priv);
42351ae3c34cSVille Syrjälä 			if (hotplug_status)
423691d14251STvrtko Ursulin 				i9xx_hpd_irq_handler(dev_priv, hotplug_status);
42371ae3c34cSVille Syrjälä 		}
4238a266c7d5SChris Wilson 
423938bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
4240a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4241a266c7d5SChris Wilson 
4242a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
42433b3f1650SAkash Goel 			notify_ring(dev_priv->engine[RCS]);
4244a266c7d5SChris Wilson 
4245055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
42465a21b665SDaniel Vetter 			int plane = pipe;
42475a21b665SDaniel Vetter 			if (HAS_FBC(dev_priv))
42485a21b665SDaniel Vetter 				plane = !plane;
42495a21b665SDaniel Vetter 
42505a21b665SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
42515a21b665SDaniel Vetter 			    i915_handle_vblank(dev_priv, plane, pipe, iir))
42525a21b665SDaniel Vetter 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
4253a266c7d5SChris Wilson 
4254a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4255a266c7d5SChris Wilson 				blc_event = true;
42564356d586SDaniel Vetter 
42574356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
425891d14251STvrtko Ursulin 				i9xx_pipe_crc_irq_handler(dev_priv, pipe);
42592d9d2b0bSVille Syrjälä 
42601f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
42611f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
42621f7247c0SDaniel Vetter 								    pipe);
4263a266c7d5SChris Wilson 		}
4264a266c7d5SChris Wilson 
4265a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
426691d14251STvrtko Ursulin 			intel_opregion_asle_intr(dev_priv);
4267a266c7d5SChris Wilson 
4268a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4269a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4270a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4271a266c7d5SChris Wilson 		 * we would never get another interrupt.
4272a266c7d5SChris Wilson 		 *
4273a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4274a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4275a266c7d5SChris Wilson 		 * another one.
4276a266c7d5SChris Wilson 		 *
4277a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4278a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4279a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4280a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4281a266c7d5SChris Wilson 		 * stray interrupts.
4282a266c7d5SChris Wilson 		 */
428338bde180SChris Wilson 		ret = IRQ_HANDLED;
4284a266c7d5SChris Wilson 		iir = new_iir;
428538bde180SChris Wilson 	} while (iir & ~flip_mask);
4286a266c7d5SChris Wilson 
42871f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
42881f814dacSImre Deak 
4289a266c7d5SChris Wilson 	return ret;
4290a266c7d5SChris Wilson }
4291a266c7d5SChris Wilson 
4292a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
4293a266c7d5SChris Wilson {
4294fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4295a266c7d5SChris Wilson 	int pipe;
4296a266c7d5SChris Wilson 
4297a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
42980706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4299a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4300a266c7d5SChris Wilson 	}
4301a266c7d5SChris Wilson 
430200d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
4303055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
430455b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
4305a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
430655b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
430755b39755SChris Wilson 	}
4308a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4309a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4310a266c7d5SChris Wilson 
4311a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4312a266c7d5SChris Wilson }
4313a266c7d5SChris Wilson 
4314a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
4315a266c7d5SChris Wilson {
4316fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4317a266c7d5SChris Wilson 	int pipe;
4318a266c7d5SChris Wilson 
43190706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4320a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4321a266c7d5SChris Wilson 
4322a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
4323055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4324a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4325a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4326a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4327a266c7d5SChris Wilson 	POSTING_READ(IER);
4328a266c7d5SChris Wilson }
4329a266c7d5SChris Wilson 
4330a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
4331a266c7d5SChris Wilson {
4332fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4333bbba0a97SChris Wilson 	u32 enable_mask;
4334a266c7d5SChris Wilson 	u32 error_mask;
4335a266c7d5SChris Wilson 
4336a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
4337bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
4338adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
4339bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4340bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
4341bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
4342bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
4343bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
4344bbba0a97SChris Wilson 
4345bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
434621ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
434721ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
4348bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
4349bbba0a97SChris Wilson 
435091d14251STvrtko Ursulin 	if (IS_G4X(dev_priv))
4351bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
4352a266c7d5SChris Wilson 
4353b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4354b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4355d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4356755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
4357755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4358755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4359d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4360a266c7d5SChris Wilson 
4361a266c7d5SChris Wilson 	/*
4362a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
4363a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
4364a266c7d5SChris Wilson 	 */
436591d14251STvrtko Ursulin 	if (IS_G4X(dev_priv)) {
4366a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
4367a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
4368a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
4369a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4370a266c7d5SChris Wilson 	} else {
4371a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
4372a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4373a266c7d5SChris Wilson 	}
4374a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
4375a266c7d5SChris Wilson 
4376a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
4377a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
4378a266c7d5SChris Wilson 	POSTING_READ(IER);
4379a266c7d5SChris Wilson 
43800706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
438120afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
438220afbda2SDaniel Vetter 
438391d14251STvrtko Ursulin 	i915_enable_asle_pipestat(dev_priv);
438420afbda2SDaniel Vetter 
438520afbda2SDaniel Vetter 	return 0;
438620afbda2SDaniel Vetter }
438720afbda2SDaniel Vetter 
438891d14251STvrtko Ursulin static void i915_hpd_irq_setup(struct drm_i915_private *dev_priv)
438920afbda2SDaniel Vetter {
439020afbda2SDaniel Vetter 	u32 hotplug_en;
439120afbda2SDaniel Vetter 
4392b5ea2d56SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
4393b5ea2d56SDaniel Vetter 
4394adca4730SChris Wilson 	/* Note HDMI and DP share hotplug bits */
4395e5868a31SEgbert Eich 	/* enable bits are the same for all generations */
439691d14251STvrtko Ursulin 	hotplug_en = intel_hpd_enabled_irqs(dev_priv, hpd_mask_i915);
4397a266c7d5SChris Wilson 	/* Programming the CRT detection parameters tends
4398a266c7d5SChris Wilson 	   to generate a spurious hotplug event about three
4399a266c7d5SChris Wilson 	   seconds later.  So just do it once.
4400a266c7d5SChris Wilson 	*/
440191d14251STvrtko Ursulin 	if (IS_G4X(dev_priv))
4402a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
4403a266c7d5SChris Wilson 	hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
4404a266c7d5SChris Wilson 
4405a266c7d5SChris Wilson 	/* Ignore TV since it's buggy */
44060706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv,
4407f9e3dc78SJani Nikula 					     HOTPLUG_INT_EN_MASK |
4408f9e3dc78SJani Nikula 					     CRT_HOTPLUG_VOLTAGE_COMPARE_MASK |
4409f9e3dc78SJani Nikula 					     CRT_HOTPLUG_ACTIVATION_PERIOD_64,
44100706f17cSEgbert Eich 					     hotplug_en);
4411a266c7d5SChris Wilson }
4412a266c7d5SChris Wilson 
4413ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
4414a266c7d5SChris Wilson {
441545a83f84SDaniel Vetter 	struct drm_device *dev = arg;
4416fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4417a266c7d5SChris Wilson 	u32 iir, new_iir;
4418a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
4419a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
442021ad8330SVille Syrjälä 	u32 flip_mask =
442121ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
442221ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
4423a266c7d5SChris Wilson 
44242dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
44252dd2a883SImre Deak 		return IRQ_NONE;
44262dd2a883SImre Deak 
44271f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
44281f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
44291f814dacSImre Deak 
4430a266c7d5SChris Wilson 	iir = I915_READ(IIR);
4431a266c7d5SChris Wilson 
4432a266c7d5SChris Wilson 	for (;;) {
4433501e01d7SVille Syrjälä 		bool irq_received = (iir & ~flip_mask) != 0;
44342c8ba29fSChris Wilson 		bool blc_event = false;
44352c8ba29fSChris Wilson 
4436a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4437a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4438a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4439a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
4440a266c7d5SChris Wilson 		 */
4441222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
4442a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4443aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4444a266c7d5SChris Wilson 
4445055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4446f0f59a00SVille Syrjälä 			i915_reg_t reg = PIPESTAT(pipe);
4447a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4448a266c7d5SChris Wilson 
4449a266c7d5SChris Wilson 			/*
4450a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
4451a266c7d5SChris Wilson 			 */
4452a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4453a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
4454501e01d7SVille Syrjälä 				irq_received = true;
4455a266c7d5SChris Wilson 			}
4456a266c7d5SChris Wilson 		}
4457222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4458a266c7d5SChris Wilson 
4459a266c7d5SChris Wilson 		if (!irq_received)
4460a266c7d5SChris Wilson 			break;
4461a266c7d5SChris Wilson 
4462a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
4463a266c7d5SChris Wilson 
4464a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
44651ae3c34cSVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
44661ae3c34cSVille Syrjälä 			u32 hotplug_status = i9xx_hpd_irq_ack(dev_priv);
44671ae3c34cSVille Syrjälä 			if (hotplug_status)
446891d14251STvrtko Ursulin 				i9xx_hpd_irq_handler(dev_priv, hotplug_status);
44691ae3c34cSVille Syrjälä 		}
4470a266c7d5SChris Wilson 
447121ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
4472a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4473a266c7d5SChris Wilson 
4474a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
44753b3f1650SAkash Goel 			notify_ring(dev_priv->engine[RCS]);
4476a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
44773b3f1650SAkash Goel 			notify_ring(dev_priv->engine[VCS]);
4478a266c7d5SChris Wilson 
4479055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
44805a21b665SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
44815a21b665SDaniel Vetter 			    i915_handle_vblank(dev_priv, pipe, pipe, iir))
44825a21b665SDaniel Vetter 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
4483a266c7d5SChris Wilson 
4484a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4485a266c7d5SChris Wilson 				blc_event = true;
44864356d586SDaniel Vetter 
44874356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
448891d14251STvrtko Ursulin 				i9xx_pipe_crc_irq_handler(dev_priv, pipe);
4489a266c7d5SChris Wilson 
44901f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
44911f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
44922d9d2b0bSVille Syrjälä 		}
4493a266c7d5SChris Wilson 
4494a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
449591d14251STvrtko Ursulin 			intel_opregion_asle_intr(dev_priv);
4496a266c7d5SChris Wilson 
4497515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
449891d14251STvrtko Ursulin 			gmbus_irq_handler(dev_priv);
4499515ac2bbSDaniel Vetter 
4500a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4501a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4502a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4503a266c7d5SChris Wilson 		 * we would never get another interrupt.
4504a266c7d5SChris Wilson 		 *
4505a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4506a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4507a266c7d5SChris Wilson 		 * another one.
4508a266c7d5SChris Wilson 		 *
4509a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4510a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4511a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4512a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4513a266c7d5SChris Wilson 		 * stray interrupts.
4514a266c7d5SChris Wilson 		 */
4515a266c7d5SChris Wilson 		iir = new_iir;
4516a266c7d5SChris Wilson 	}
4517a266c7d5SChris Wilson 
45181f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
45191f814dacSImre Deak 
4520a266c7d5SChris Wilson 	return ret;
4521a266c7d5SChris Wilson }
4522a266c7d5SChris Wilson 
4523a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
4524a266c7d5SChris Wilson {
4525fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4526a266c7d5SChris Wilson 	int pipe;
4527a266c7d5SChris Wilson 
4528a266c7d5SChris Wilson 	if (!dev_priv)
4529a266c7d5SChris Wilson 		return;
4530a266c7d5SChris Wilson 
45310706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4532a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4533a266c7d5SChris Wilson 
4534a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
4535055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4536a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4537a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4538a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4539a266c7d5SChris Wilson 
4540055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4541a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
4542a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
4543a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4544a266c7d5SChris Wilson }
4545a266c7d5SChris Wilson 
4546fca52a55SDaniel Vetter /**
4547fca52a55SDaniel Vetter  * intel_irq_init - initializes irq support
4548fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4549fca52a55SDaniel Vetter  *
4550fca52a55SDaniel Vetter  * This function initializes all the irq support including work items, timers
4551fca52a55SDaniel Vetter  * and all the vtables. It does not setup the interrupt itself though.
4552fca52a55SDaniel Vetter  */
4553b963291cSDaniel Vetter void intel_irq_init(struct drm_i915_private *dev_priv)
4554f71d4af4SJesse Barnes {
455591c8a326SChris Wilson 	struct drm_device *dev = &dev_priv->drm;
45568b2e326dSChris Wilson 
455777913b39SJani Nikula 	intel_hpd_init_work(dev_priv);
455877913b39SJani Nikula 
4559c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
4560a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
45618b2e326dSChris Wilson 
456226705e20SSagar Arun Kamble 	if (HAS_GUC_SCHED(dev))
456326705e20SSagar Arun Kamble 		dev_priv->pm_guc_events = GEN9_GUC_TO_HOST_INT_EVENT;
456426705e20SSagar Arun Kamble 
4565a6706b45SDeepak S 	/* Let's track the enabled rps events */
4566666a4537SWayne Boyer 	if (IS_VALLEYVIEW(dev_priv))
45676c65a587SVille Syrjälä 		/* WaGsvRC0ResidencyMethod:vlv */
45686f4b12f8SChris Wilson 		dev_priv->pm_rps_events = GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED;
456931685c25SDeepak S 	else
4570a6706b45SDeepak S 		dev_priv->pm_rps_events = GEN6_PM_RPS_EVENTS;
4571a6706b45SDeepak S 
45721800ad25SSagar Arun Kamble 	dev_priv->rps.pm_intr_keep = 0;
45731800ad25SSagar Arun Kamble 
45741800ad25SSagar Arun Kamble 	/*
45751800ad25SSagar Arun Kamble 	 * SNB,IVB can while VLV,CHV may hard hang on looping batchbuffer
45761800ad25SSagar Arun Kamble 	 * if GEN6_PM_UP_EI_EXPIRED is masked.
45771800ad25SSagar Arun Kamble 	 *
45781800ad25SSagar Arun Kamble 	 * TODO: verify if this can be reproduced on VLV,CHV.
45791800ad25SSagar Arun Kamble 	 */
45801800ad25SSagar Arun Kamble 	if (INTEL_INFO(dev_priv)->gen <= 7 && !IS_HASWELL(dev_priv))
45811800ad25SSagar Arun Kamble 		dev_priv->rps.pm_intr_keep |= GEN6_PM_RP_UP_EI_EXPIRED;
45821800ad25SSagar Arun Kamble 
45831800ad25SSagar Arun Kamble 	if (INTEL_INFO(dev_priv)->gen >= 8)
4584b20e3cfeSDave Gordon 		dev_priv->rps.pm_intr_keep |= GEN8_PMINTR_REDIRECT_TO_GUC;
45851800ad25SSagar Arun Kamble 
4586737b1506SChris Wilson 	INIT_DELAYED_WORK(&dev_priv->gpu_error.hangcheck_work,
4587737b1506SChris Wilson 			  i915_hangcheck_elapsed);
458861bac78eSDaniel Vetter 
4589b963291cSDaniel Vetter 	if (IS_GEN2(dev_priv)) {
45904194c088SRodrigo Vivi 		/* Gen2 doesn't have a hardware frame counter */
45914cdb83ecSVille Syrjälä 		dev->max_vblank_count = 0;
45924194c088SRodrigo Vivi 		dev->driver->get_vblank_counter = drm_vblank_no_hw_counter;
4593b963291cSDaniel Vetter 	} else if (IS_G4X(dev_priv) || INTEL_INFO(dev_priv)->gen >= 5) {
4594f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
4595fd8f507cSVille Syrjälä 		dev->driver->get_vblank_counter = g4x_get_vblank_counter;
4596391f75e2SVille Syrjälä 	} else {
4597391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
4598391f75e2SVille Syrjälä 		dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
4599f71d4af4SJesse Barnes 	}
4600f71d4af4SJesse Barnes 
460121da2700SVille Syrjälä 	/*
460221da2700SVille Syrjälä 	 * Opt out of the vblank disable timer on everything except gen2.
460321da2700SVille Syrjälä 	 * Gen2 doesn't have a hardware frame counter and so depends on
460421da2700SVille Syrjälä 	 * vblank interrupts to produce sane vblank seuquence numbers.
460521da2700SVille Syrjälä 	 */
4606b963291cSDaniel Vetter 	if (!IS_GEN2(dev_priv))
460721da2700SVille Syrjälä 		dev->vblank_disable_immediate = true;
460821da2700SVille Syrjälä 
4609f71d4af4SJesse Barnes 	dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
4610f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
4611f71d4af4SJesse Barnes 
4612b963291cSDaniel Vetter 	if (IS_CHERRYVIEW(dev_priv)) {
461343f328d7SVille Syrjälä 		dev->driver->irq_handler = cherryview_irq_handler;
461443f328d7SVille Syrjälä 		dev->driver->irq_preinstall = cherryview_irq_preinstall;
461543f328d7SVille Syrjälä 		dev->driver->irq_postinstall = cherryview_irq_postinstall;
461643f328d7SVille Syrjälä 		dev->driver->irq_uninstall = cherryview_irq_uninstall;
461786e83e35SChris Wilson 		dev->driver->enable_vblank = i965_enable_vblank;
461886e83e35SChris Wilson 		dev->driver->disable_vblank = i965_disable_vblank;
461943f328d7SVille Syrjälä 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4620b963291cSDaniel Vetter 	} else if (IS_VALLEYVIEW(dev_priv)) {
46217e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
46227e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
46237e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
46247e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
462586e83e35SChris Wilson 		dev->driver->enable_vblank = i965_enable_vblank;
462686e83e35SChris Wilson 		dev->driver->disable_vblank = i965_disable_vblank;
4627fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4628b963291cSDaniel Vetter 	} else if (INTEL_INFO(dev_priv)->gen >= 8) {
4629abd58f01SBen Widawsky 		dev->driver->irq_handler = gen8_irq_handler;
4630723761b8SDaniel Vetter 		dev->driver->irq_preinstall = gen8_irq_reset;
4631abd58f01SBen Widawsky 		dev->driver->irq_postinstall = gen8_irq_postinstall;
4632abd58f01SBen Widawsky 		dev->driver->irq_uninstall = gen8_irq_uninstall;
4633abd58f01SBen Widawsky 		dev->driver->enable_vblank = gen8_enable_vblank;
4634abd58f01SBen Widawsky 		dev->driver->disable_vblank = gen8_disable_vblank;
4635e2d214aeSTvrtko Ursulin 		if (IS_BROXTON(dev_priv))
4636e0a20ad7SShashank Sharma 			dev_priv->display.hpd_irq_setup = bxt_hpd_irq_setup;
46376e266956STvrtko Ursulin 		else if (HAS_PCH_SPT(dev_priv) || HAS_PCH_KBP(dev_priv))
46386dbf30ceSVille Syrjälä 			dev_priv->display.hpd_irq_setup = spt_hpd_irq_setup;
46396dbf30ceSVille Syrjälä 		else
46403a3b3c7dSVille Syrjälä 			dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
46416e266956STvrtko Ursulin 	} else if (HAS_PCH_SPLIT(dev_priv)) {
4642f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
4643723761b8SDaniel Vetter 		dev->driver->irq_preinstall = ironlake_irq_reset;
4644f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
4645f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
4646f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
4647f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
4648e4ce95aaSVille Syrjälä 		dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
4649f71d4af4SJesse Barnes 	} else {
46507e22dbbbSTvrtko Ursulin 		if (IS_GEN2(dev_priv)) {
4651c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
4652c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
4653c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
4654c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
465586e83e35SChris Wilson 			dev->driver->enable_vblank = i8xx_enable_vblank;
465686e83e35SChris Wilson 			dev->driver->disable_vblank = i8xx_disable_vblank;
46577e22dbbbSTvrtko Ursulin 		} else if (IS_GEN3(dev_priv)) {
4658a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
4659a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
4660a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
4661a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
466286e83e35SChris Wilson 			dev->driver->enable_vblank = i8xx_enable_vblank;
466386e83e35SChris Wilson 			dev->driver->disable_vblank = i8xx_disable_vblank;
4664c2798b19SChris Wilson 		} else {
4665a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
4666a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
4667a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
4668a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
466986e83e35SChris Wilson 			dev->driver->enable_vblank = i965_enable_vblank;
467086e83e35SChris Wilson 			dev->driver->disable_vblank = i965_disable_vblank;
4671c2798b19SChris Wilson 		}
4672778eb334SVille Syrjälä 		if (I915_HAS_HOTPLUG(dev_priv))
4673778eb334SVille Syrjälä 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4674f71d4af4SJesse Barnes 	}
4675f71d4af4SJesse Barnes }
467620afbda2SDaniel Vetter 
4677fca52a55SDaniel Vetter /**
4678fca52a55SDaniel Vetter  * intel_irq_install - enables the hardware interrupt
4679fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4680fca52a55SDaniel Vetter  *
4681fca52a55SDaniel Vetter  * This function enables the hardware interrupt handling, but leaves the hotplug
4682fca52a55SDaniel Vetter  * handling still disabled. It is called after intel_irq_init().
4683fca52a55SDaniel Vetter  *
4684fca52a55SDaniel Vetter  * In the driver load and resume code we need working interrupts in a few places
4685fca52a55SDaniel Vetter  * but don't want to deal with the hassle of concurrent probe and hotplug
4686fca52a55SDaniel Vetter  * workers. Hence the split into this two-stage approach.
4687fca52a55SDaniel Vetter  */
46882aeb7d3aSDaniel Vetter int intel_irq_install(struct drm_i915_private *dev_priv)
46892aeb7d3aSDaniel Vetter {
46902aeb7d3aSDaniel Vetter 	/*
46912aeb7d3aSDaniel Vetter 	 * We enable some interrupt sources in our postinstall hooks, so mark
46922aeb7d3aSDaniel Vetter 	 * interrupts as enabled _before_ actually enabling them to avoid
46932aeb7d3aSDaniel Vetter 	 * special cases in our ordering checks.
46942aeb7d3aSDaniel Vetter 	 */
46952aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
46962aeb7d3aSDaniel Vetter 
469791c8a326SChris Wilson 	return drm_irq_install(&dev_priv->drm, dev_priv->drm.pdev->irq);
46982aeb7d3aSDaniel Vetter }
46992aeb7d3aSDaniel Vetter 
4700fca52a55SDaniel Vetter /**
4701fca52a55SDaniel Vetter  * intel_irq_uninstall - finilizes all irq handling
4702fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4703fca52a55SDaniel Vetter  *
4704fca52a55SDaniel Vetter  * This stops interrupt and hotplug handling and unregisters and frees all
4705fca52a55SDaniel Vetter  * resources acquired in the init functions.
4706fca52a55SDaniel Vetter  */
47072aeb7d3aSDaniel Vetter void intel_irq_uninstall(struct drm_i915_private *dev_priv)
47082aeb7d3aSDaniel Vetter {
470991c8a326SChris Wilson 	drm_irq_uninstall(&dev_priv->drm);
47102aeb7d3aSDaniel Vetter 	intel_hpd_cancel_work(dev_priv);
47112aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
47122aeb7d3aSDaniel Vetter }
47132aeb7d3aSDaniel Vetter 
4714fca52a55SDaniel Vetter /**
4715fca52a55SDaniel Vetter  * intel_runtime_pm_disable_interrupts - runtime interrupt disabling
4716fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4717fca52a55SDaniel Vetter  *
4718fca52a55SDaniel Vetter  * This function is used to disable interrupts at runtime, both in the runtime
4719fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4720fca52a55SDaniel Vetter  */
4721b963291cSDaniel Vetter void intel_runtime_pm_disable_interrupts(struct drm_i915_private *dev_priv)
4722c67a470bSPaulo Zanoni {
472391c8a326SChris Wilson 	dev_priv->drm.driver->irq_uninstall(&dev_priv->drm);
47242aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
472591c8a326SChris Wilson 	synchronize_irq(dev_priv->drm.irq);
4726c67a470bSPaulo Zanoni }
4727c67a470bSPaulo Zanoni 
4728fca52a55SDaniel Vetter /**
4729fca52a55SDaniel Vetter  * intel_runtime_pm_enable_interrupts - runtime interrupt enabling
4730fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4731fca52a55SDaniel Vetter  *
4732fca52a55SDaniel Vetter  * This function is used to enable interrupts at runtime, both in the runtime
4733fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4734fca52a55SDaniel Vetter  */
4735b963291cSDaniel Vetter void intel_runtime_pm_enable_interrupts(struct drm_i915_private *dev_priv)
4736c67a470bSPaulo Zanoni {
47372aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
473891c8a326SChris Wilson 	dev_priv->drm.driver->irq_preinstall(&dev_priv->drm);
473991c8a326SChris Wilson 	dev_priv->drm.driver->irq_postinstall(&dev_priv->drm);
4740c67a470bSPaulo Zanoni }
4741