xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision ca814b25538a5b2c0a8de6665191725f41608f2c)
1c0e09200SDave Airlie /* i915_irq.c -- IRQ support for the I915 -*- linux-c -*-
2c0e09200SDave Airlie  */
3c0e09200SDave Airlie /*
4c0e09200SDave Airlie  * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
5c0e09200SDave Airlie  * All Rights Reserved.
6c0e09200SDave Airlie  *
7c0e09200SDave Airlie  * Permission is hereby granted, free of charge, to any person obtaining a
8c0e09200SDave Airlie  * copy of this software and associated documentation files (the
9c0e09200SDave Airlie  * "Software"), to deal in the Software without restriction, including
10c0e09200SDave Airlie  * without limitation the rights to use, copy, modify, merge, publish,
11c0e09200SDave Airlie  * distribute, sub license, and/or sell copies of the Software, and to
12c0e09200SDave Airlie  * permit persons to whom the Software is furnished to do so, subject to
13c0e09200SDave Airlie  * the following conditions:
14c0e09200SDave Airlie  *
15c0e09200SDave Airlie  * The above copyright notice and this permission notice (including the
16c0e09200SDave Airlie  * next paragraph) shall be included in all copies or substantial portions
17c0e09200SDave Airlie  * of the Software.
18c0e09200SDave Airlie  *
19c0e09200SDave Airlie  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20c0e09200SDave Airlie  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21c0e09200SDave Airlie  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22c0e09200SDave Airlie  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23c0e09200SDave Airlie  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24c0e09200SDave Airlie  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25c0e09200SDave Airlie  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26c0e09200SDave Airlie  *
27c0e09200SDave Airlie  */
28c0e09200SDave Airlie 
29a70491ccSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30a70491ccSJoe Perches 
3163eeaf38SJesse Barnes #include <linux/sysrq.h>
325a0e3ad6STejun Heo #include <linux/slab.h>
33b2c88f5bSDamien Lespiau #include <linux/circ_buf.h>
34760285e7SDavid Howells #include <drm/drmP.h>
35760285e7SDavid Howells #include <drm/i915_drm.h>
36c0e09200SDave Airlie #include "i915_drv.h"
371c5d22f7SChris Wilson #include "i915_trace.h"
3879e53945SJesse Barnes #include "intel_drv.h"
39c0e09200SDave Airlie 
40fca52a55SDaniel Vetter /**
41fca52a55SDaniel Vetter  * DOC: interrupt handling
42fca52a55SDaniel Vetter  *
43fca52a55SDaniel Vetter  * These functions provide the basic support for enabling and disabling the
44fca52a55SDaniel Vetter  * interrupt handling support. There's a lot more functionality in i915_irq.c
45fca52a55SDaniel Vetter  * and related files, but that will be described in separate chapters.
46fca52a55SDaniel Vetter  */
47fca52a55SDaniel Vetter 
48e4ce95aaSVille Syrjälä static const u32 hpd_ilk[HPD_NUM_PINS] = {
49e4ce95aaSVille Syrjälä 	[HPD_PORT_A] = DE_DP_A_HOTPLUG,
50e4ce95aaSVille Syrjälä };
51e4ce95aaSVille Syrjälä 
5223bb4cb5SVille Syrjälä static const u32 hpd_ivb[HPD_NUM_PINS] = {
5323bb4cb5SVille Syrjälä 	[HPD_PORT_A] = DE_DP_A_HOTPLUG_IVB,
5423bb4cb5SVille Syrjälä };
5523bb4cb5SVille Syrjälä 
563a3b3c7dSVille Syrjälä static const u32 hpd_bdw[HPD_NUM_PINS] = {
573a3b3c7dSVille Syrjälä 	[HPD_PORT_A] = GEN8_PORT_DP_A_HOTPLUG,
583a3b3c7dSVille Syrjälä };
593a3b3c7dSVille Syrjälä 
607c7e10dbSVille Syrjälä static const u32 hpd_ibx[HPD_NUM_PINS] = {
61e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG,
62e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG,
63e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG,
64e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG,
65e5868a31SEgbert Eich 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG
66e5868a31SEgbert Eich };
67e5868a31SEgbert Eich 
687c7e10dbSVille Syrjälä static const u32 hpd_cpt[HPD_NUM_PINS] = {
69e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG_CPT,
7073c352a2SDaniel Vetter 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG_CPT,
71e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
72e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
73e5868a31SEgbert Eich 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT
74e5868a31SEgbert Eich };
75e5868a31SEgbert Eich 
7626951cafSXiong Zhang static const u32 hpd_spt[HPD_NUM_PINS] = {
7774c0b395SVille Syrjälä 	[HPD_PORT_A] = SDE_PORTA_HOTPLUG_SPT,
7826951cafSXiong Zhang 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
7926951cafSXiong Zhang 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
8026951cafSXiong Zhang 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT,
8126951cafSXiong Zhang 	[HPD_PORT_E] = SDE_PORTE_HOTPLUG_SPT
8226951cafSXiong Zhang };
8326951cafSXiong Zhang 
847c7e10dbSVille Syrjälä static const u32 hpd_mask_i915[HPD_NUM_PINS] = {
85e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_EN,
86e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_EN,
87e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_EN,
88e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_EN,
89e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_EN,
90e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_EN
91e5868a31SEgbert Eich };
92e5868a31SEgbert Eich 
937c7e10dbSVille Syrjälä static const u32 hpd_status_g4x[HPD_NUM_PINS] = {
94e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
95e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_G4X,
96e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_G4X,
97e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
98e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
99e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
100e5868a31SEgbert Eich };
101e5868a31SEgbert Eich 
1024bca26d0SVille Syrjälä static const u32 hpd_status_i915[HPD_NUM_PINS] = {
103e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
104e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_I915,
105e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_I915,
106e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
107e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
108e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
109e5868a31SEgbert Eich };
110e5868a31SEgbert Eich 
111e0a20ad7SShashank Sharma /* BXT hpd list */
112e0a20ad7SShashank Sharma static const u32 hpd_bxt[HPD_NUM_PINS] = {
1137f3561beSSonika Jindal 	[HPD_PORT_A] = BXT_DE_PORT_HP_DDIA,
114e0a20ad7SShashank Sharma 	[HPD_PORT_B] = BXT_DE_PORT_HP_DDIB,
115e0a20ad7SShashank Sharma 	[HPD_PORT_C] = BXT_DE_PORT_HP_DDIC
116e0a20ad7SShashank Sharma };
117e0a20ad7SShashank Sharma 
1185c502442SPaulo Zanoni /* IIR can theoretically queue up two events. Be paranoid. */
119f86f3fb0SPaulo Zanoni #define GEN8_IRQ_RESET_NDX(type, which) do { \
1205c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IMR(which), 0xffffffff); \
1215c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IMR(which)); \
1225c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IER(which), 0); \
1235c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
1245c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IIR(which)); \
1255c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
1265c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IIR(which)); \
1275c502442SPaulo Zanoni } while (0)
1285c502442SPaulo Zanoni 
129f86f3fb0SPaulo Zanoni #define GEN5_IRQ_RESET(type) do { \
130a9d356a6SPaulo Zanoni 	I915_WRITE(type##IMR, 0xffffffff); \
1315c502442SPaulo Zanoni 	POSTING_READ(type##IMR); \
132a9d356a6SPaulo Zanoni 	I915_WRITE(type##IER, 0); \
1335c502442SPaulo Zanoni 	I915_WRITE(type##IIR, 0xffffffff); \
1345c502442SPaulo Zanoni 	POSTING_READ(type##IIR); \
1355c502442SPaulo Zanoni 	I915_WRITE(type##IIR, 0xffffffff); \
1365c502442SPaulo Zanoni 	POSTING_READ(type##IIR); \
137a9d356a6SPaulo Zanoni } while (0)
138a9d356a6SPaulo Zanoni 
139337ba017SPaulo Zanoni /*
140337ba017SPaulo Zanoni  * We should clear IMR at preinstall/uninstall, and just check at postinstall.
141337ba017SPaulo Zanoni  */
142f0f59a00SVille Syrjälä static void gen5_assert_iir_is_zero(struct drm_i915_private *dev_priv,
143f0f59a00SVille Syrjälä 				    i915_reg_t reg)
144b51a2842SVille Syrjälä {
145b51a2842SVille Syrjälä 	u32 val = I915_READ(reg);
146b51a2842SVille Syrjälä 
147b51a2842SVille Syrjälä 	if (val == 0)
148b51a2842SVille Syrjälä 		return;
149b51a2842SVille Syrjälä 
150b51a2842SVille Syrjälä 	WARN(1, "Interrupt register 0x%x is not zero: 0x%08x\n",
151f0f59a00SVille Syrjälä 	     i915_mmio_reg_offset(reg), val);
152b51a2842SVille Syrjälä 	I915_WRITE(reg, 0xffffffff);
153b51a2842SVille Syrjälä 	POSTING_READ(reg);
154b51a2842SVille Syrjälä 	I915_WRITE(reg, 0xffffffff);
155b51a2842SVille Syrjälä 	POSTING_READ(reg);
156b51a2842SVille Syrjälä }
157337ba017SPaulo Zanoni 
15835079899SPaulo Zanoni #define GEN8_IRQ_INIT_NDX(type, which, imr_val, ier_val) do { \
159b51a2842SVille Syrjälä 	gen5_assert_iir_is_zero(dev_priv, GEN8_##type##_IIR(which)); \
16035079899SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IER(which), (ier_val)); \
1617d1bd539SVille Syrjälä 	I915_WRITE(GEN8_##type##_IMR(which), (imr_val)); \
1627d1bd539SVille Syrjälä 	POSTING_READ(GEN8_##type##_IMR(which)); \
16335079899SPaulo Zanoni } while (0)
16435079899SPaulo Zanoni 
16535079899SPaulo Zanoni #define GEN5_IRQ_INIT(type, imr_val, ier_val) do { \
166b51a2842SVille Syrjälä 	gen5_assert_iir_is_zero(dev_priv, type##IIR); \
16735079899SPaulo Zanoni 	I915_WRITE(type##IER, (ier_val)); \
1687d1bd539SVille Syrjälä 	I915_WRITE(type##IMR, (imr_val)); \
1697d1bd539SVille Syrjälä 	POSTING_READ(type##IMR); \
17035079899SPaulo Zanoni } while (0)
17135079899SPaulo Zanoni 
172c9a9a268SImre Deak static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir);
17326705e20SSagar Arun Kamble static void gen9_guc_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir);
174c9a9a268SImre Deak 
1750706f17cSEgbert Eich /* For display hotplug interrupt */
1760706f17cSEgbert Eich static inline void
1770706f17cSEgbert Eich i915_hotplug_interrupt_update_locked(struct drm_i915_private *dev_priv,
1780706f17cSEgbert Eich 				     uint32_t mask,
1790706f17cSEgbert Eich 				     uint32_t bits)
1800706f17cSEgbert Eich {
1810706f17cSEgbert Eich 	uint32_t val;
1820706f17cSEgbert Eich 
18367520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
1840706f17cSEgbert Eich 	WARN_ON(bits & ~mask);
1850706f17cSEgbert Eich 
1860706f17cSEgbert Eich 	val = I915_READ(PORT_HOTPLUG_EN);
1870706f17cSEgbert Eich 	val &= ~mask;
1880706f17cSEgbert Eich 	val |= bits;
1890706f17cSEgbert Eich 	I915_WRITE(PORT_HOTPLUG_EN, val);
1900706f17cSEgbert Eich }
1910706f17cSEgbert Eich 
1920706f17cSEgbert Eich /**
1930706f17cSEgbert Eich  * i915_hotplug_interrupt_update - update hotplug interrupt enable
1940706f17cSEgbert Eich  * @dev_priv: driver private
1950706f17cSEgbert Eich  * @mask: bits to update
1960706f17cSEgbert Eich  * @bits: bits to enable
1970706f17cSEgbert Eich  * NOTE: the HPD enable bits are modified both inside and outside
1980706f17cSEgbert Eich  * of an interrupt context. To avoid that read-modify-write cycles
1990706f17cSEgbert Eich  * interfer, these bits are protected by a spinlock. Since this
2000706f17cSEgbert Eich  * function is usually not called from a context where the lock is
2010706f17cSEgbert Eich  * held already, this function acquires the lock itself. A non-locking
2020706f17cSEgbert Eich  * version is also available.
2030706f17cSEgbert Eich  */
2040706f17cSEgbert Eich void i915_hotplug_interrupt_update(struct drm_i915_private *dev_priv,
2050706f17cSEgbert Eich 				   uint32_t mask,
2060706f17cSEgbert Eich 				   uint32_t bits)
2070706f17cSEgbert Eich {
2080706f17cSEgbert Eich 	spin_lock_irq(&dev_priv->irq_lock);
2090706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv, mask, bits);
2100706f17cSEgbert Eich 	spin_unlock_irq(&dev_priv->irq_lock);
2110706f17cSEgbert Eich }
2120706f17cSEgbert Eich 
213d9dc34f1SVille Syrjälä /**
214d9dc34f1SVille Syrjälä  * ilk_update_display_irq - update DEIMR
215d9dc34f1SVille Syrjälä  * @dev_priv: driver private
216d9dc34f1SVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
217d9dc34f1SVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
218d9dc34f1SVille Syrjälä  */
219fbdedaeaSVille Syrjälä void ilk_update_display_irq(struct drm_i915_private *dev_priv,
220d9dc34f1SVille Syrjälä 			    uint32_t interrupt_mask,
221d9dc34f1SVille Syrjälä 			    uint32_t enabled_irq_mask)
222036a4a7dSZhenyu Wang {
223d9dc34f1SVille Syrjälä 	uint32_t new_val;
224d9dc34f1SVille Syrjälä 
22567520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
2264bc9d430SDaniel Vetter 
227d9dc34f1SVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
228d9dc34f1SVille Syrjälä 
2299df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
230c67a470bSPaulo Zanoni 		return;
231c67a470bSPaulo Zanoni 
232d9dc34f1SVille Syrjälä 	new_val = dev_priv->irq_mask;
233d9dc34f1SVille Syrjälä 	new_val &= ~interrupt_mask;
234d9dc34f1SVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
235d9dc34f1SVille Syrjälä 
236d9dc34f1SVille Syrjälä 	if (new_val != dev_priv->irq_mask) {
237d9dc34f1SVille Syrjälä 		dev_priv->irq_mask = new_val;
2381ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
2393143a2bfSChris Wilson 		POSTING_READ(DEIMR);
240036a4a7dSZhenyu Wang 	}
241036a4a7dSZhenyu Wang }
242036a4a7dSZhenyu Wang 
24343eaea13SPaulo Zanoni /**
24443eaea13SPaulo Zanoni  * ilk_update_gt_irq - update GTIMR
24543eaea13SPaulo Zanoni  * @dev_priv: driver private
24643eaea13SPaulo Zanoni  * @interrupt_mask: mask of interrupt bits to update
24743eaea13SPaulo Zanoni  * @enabled_irq_mask: mask of interrupt bits to enable
24843eaea13SPaulo Zanoni  */
24943eaea13SPaulo Zanoni static void ilk_update_gt_irq(struct drm_i915_private *dev_priv,
25043eaea13SPaulo Zanoni 			      uint32_t interrupt_mask,
25143eaea13SPaulo Zanoni 			      uint32_t enabled_irq_mask)
25243eaea13SPaulo Zanoni {
25367520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
25443eaea13SPaulo Zanoni 
25515a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
25615a17aaeSDaniel Vetter 
2579df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
258c67a470bSPaulo Zanoni 		return;
259c67a470bSPaulo Zanoni 
26043eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask &= ~interrupt_mask;
26143eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask |= (~enabled_irq_mask & interrupt_mask);
26243eaea13SPaulo Zanoni 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
26343eaea13SPaulo Zanoni }
26443eaea13SPaulo Zanoni 
265480c8033SDaniel Vetter void gen5_enable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
26643eaea13SPaulo Zanoni {
26743eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, mask);
26831bb59ccSChris Wilson 	POSTING_READ_FW(GTIMR);
26943eaea13SPaulo Zanoni }
27043eaea13SPaulo Zanoni 
271480c8033SDaniel Vetter void gen5_disable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
27243eaea13SPaulo Zanoni {
27343eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, 0);
27443eaea13SPaulo Zanoni }
27543eaea13SPaulo Zanoni 
276f0f59a00SVille Syrjälä static i915_reg_t gen6_pm_iir(struct drm_i915_private *dev_priv)
277b900b949SImre Deak {
278b900b949SImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IIR(2) : GEN6_PMIIR;
279b900b949SImre Deak }
280b900b949SImre Deak 
281f0f59a00SVille Syrjälä static i915_reg_t gen6_pm_imr(struct drm_i915_private *dev_priv)
282a72fbc3aSImre Deak {
283a72fbc3aSImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IMR(2) : GEN6_PMIMR;
284a72fbc3aSImre Deak }
285a72fbc3aSImre Deak 
286f0f59a00SVille Syrjälä static i915_reg_t gen6_pm_ier(struct drm_i915_private *dev_priv)
287b900b949SImre Deak {
288b900b949SImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IER(2) : GEN6_PMIER;
289b900b949SImre Deak }
290b900b949SImre Deak 
291edbfdb45SPaulo Zanoni /**
292edbfdb45SPaulo Zanoni  * snb_update_pm_irq - update GEN6_PMIMR
293edbfdb45SPaulo Zanoni  * @dev_priv: driver private
294edbfdb45SPaulo Zanoni  * @interrupt_mask: mask of interrupt bits to update
295edbfdb45SPaulo Zanoni  * @enabled_irq_mask: mask of interrupt bits to enable
296edbfdb45SPaulo Zanoni  */
297edbfdb45SPaulo Zanoni static void snb_update_pm_irq(struct drm_i915_private *dev_priv,
298edbfdb45SPaulo Zanoni 			      uint32_t interrupt_mask,
299edbfdb45SPaulo Zanoni 			      uint32_t enabled_irq_mask)
300edbfdb45SPaulo Zanoni {
301605cd25bSPaulo Zanoni 	uint32_t new_val;
302edbfdb45SPaulo Zanoni 
30315a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
30415a17aaeSDaniel Vetter 
30567520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
306edbfdb45SPaulo Zanoni 
307f4e9af4fSAkash Goel 	new_val = dev_priv->pm_imr;
308f52ecbcfSPaulo Zanoni 	new_val &= ~interrupt_mask;
309f52ecbcfSPaulo Zanoni 	new_val |= (~enabled_irq_mask & interrupt_mask);
310f52ecbcfSPaulo Zanoni 
311f4e9af4fSAkash Goel 	if (new_val != dev_priv->pm_imr) {
312f4e9af4fSAkash Goel 		dev_priv->pm_imr = new_val;
313f4e9af4fSAkash Goel 		I915_WRITE(gen6_pm_imr(dev_priv), dev_priv->pm_imr);
314a72fbc3aSImre Deak 		POSTING_READ(gen6_pm_imr(dev_priv));
315edbfdb45SPaulo Zanoni 	}
316f52ecbcfSPaulo Zanoni }
317edbfdb45SPaulo Zanoni 
318f4e9af4fSAkash Goel void gen6_unmask_pm_irq(struct drm_i915_private *dev_priv, u32 mask)
319edbfdb45SPaulo Zanoni {
3209939fba2SImre Deak 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
3219939fba2SImre Deak 		return;
3229939fba2SImre Deak 
323edbfdb45SPaulo Zanoni 	snb_update_pm_irq(dev_priv, mask, mask);
324edbfdb45SPaulo Zanoni }
325edbfdb45SPaulo Zanoni 
326f4e9af4fSAkash Goel static void __gen6_mask_pm_irq(struct drm_i915_private *dev_priv, u32 mask)
3279939fba2SImre Deak {
3289939fba2SImre Deak 	snb_update_pm_irq(dev_priv, mask, 0);
3299939fba2SImre Deak }
3309939fba2SImre Deak 
331f4e9af4fSAkash Goel void gen6_mask_pm_irq(struct drm_i915_private *dev_priv, u32 mask)
332edbfdb45SPaulo Zanoni {
3339939fba2SImre Deak 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
3349939fba2SImre Deak 		return;
3359939fba2SImre Deak 
336f4e9af4fSAkash Goel 	__gen6_mask_pm_irq(dev_priv, mask);
337f4e9af4fSAkash Goel }
338f4e9af4fSAkash Goel 
339f4e9af4fSAkash Goel void gen6_reset_pm_iir(struct drm_i915_private *dev_priv, u32 reset_mask)
340f4e9af4fSAkash Goel {
341f4e9af4fSAkash Goel 	i915_reg_t reg = gen6_pm_iir(dev_priv);
342f4e9af4fSAkash Goel 
34367520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
344f4e9af4fSAkash Goel 
345f4e9af4fSAkash Goel 	I915_WRITE(reg, reset_mask);
346f4e9af4fSAkash Goel 	I915_WRITE(reg, reset_mask);
347f4e9af4fSAkash Goel 	POSTING_READ(reg);
348f4e9af4fSAkash Goel }
349f4e9af4fSAkash Goel 
350f4e9af4fSAkash Goel void gen6_enable_pm_irq(struct drm_i915_private *dev_priv, u32 enable_mask)
351f4e9af4fSAkash Goel {
35267520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
353f4e9af4fSAkash Goel 
354f4e9af4fSAkash Goel 	dev_priv->pm_ier |= enable_mask;
355f4e9af4fSAkash Goel 	I915_WRITE(gen6_pm_ier(dev_priv), dev_priv->pm_ier);
356f4e9af4fSAkash Goel 	gen6_unmask_pm_irq(dev_priv, enable_mask);
357f4e9af4fSAkash Goel 	/* unmask_pm_irq provides an implicit barrier (POSTING_READ) */
358f4e9af4fSAkash Goel }
359f4e9af4fSAkash Goel 
360f4e9af4fSAkash Goel void gen6_disable_pm_irq(struct drm_i915_private *dev_priv, u32 disable_mask)
361f4e9af4fSAkash Goel {
36267520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
363f4e9af4fSAkash Goel 
364f4e9af4fSAkash Goel 	dev_priv->pm_ier &= ~disable_mask;
365f4e9af4fSAkash Goel 	__gen6_mask_pm_irq(dev_priv, disable_mask);
366f4e9af4fSAkash Goel 	I915_WRITE(gen6_pm_ier(dev_priv), dev_priv->pm_ier);
367f4e9af4fSAkash Goel 	/* though a barrier is missing here, but don't really need a one */
368edbfdb45SPaulo Zanoni }
369edbfdb45SPaulo Zanoni 
370dc97997aSChris Wilson void gen6_reset_rps_interrupts(struct drm_i915_private *dev_priv)
3713cc134e3SImre Deak {
3723cc134e3SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
373f4e9af4fSAkash Goel 	gen6_reset_pm_iir(dev_priv, dev_priv->pm_rps_events);
374096fad9eSImre Deak 	dev_priv->rps.pm_iir = 0;
3753cc134e3SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
3763cc134e3SImre Deak }
3773cc134e3SImre Deak 
37891d14251STvrtko Ursulin void gen6_enable_rps_interrupts(struct drm_i915_private *dev_priv)
379b900b949SImre Deak {
380f2a91d1aSChris Wilson 	if (READ_ONCE(dev_priv->rps.interrupts_enabled))
381f2a91d1aSChris Wilson 		return;
382f2a91d1aSChris Wilson 
383b900b949SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
384c33d247dSChris Wilson 	WARN_ON_ONCE(dev_priv->rps.pm_iir);
385c33d247dSChris Wilson 	WARN_ON_ONCE(I915_READ(gen6_pm_iir(dev_priv)) & dev_priv->pm_rps_events);
386d4d70aa5SImre Deak 	dev_priv->rps.interrupts_enabled = true;
387b900b949SImre Deak 	gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
38878e68d36SImre Deak 
389b900b949SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
390b900b949SImre Deak }
391b900b949SImre Deak 
39291d14251STvrtko Ursulin void gen6_disable_rps_interrupts(struct drm_i915_private *dev_priv)
393b900b949SImre Deak {
394f2a91d1aSChris Wilson 	if (!READ_ONCE(dev_priv->rps.interrupts_enabled))
395f2a91d1aSChris Wilson 		return;
396f2a91d1aSChris Wilson 
397d4d70aa5SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
398d4d70aa5SImre Deak 	dev_priv->rps.interrupts_enabled = false;
3999939fba2SImre Deak 
400b20e3cfeSDave Gordon 	I915_WRITE(GEN6_PMINTRMSK, gen6_sanitize_rps_pm_mask(dev_priv, ~0u));
4019939fba2SImre Deak 
402f4e9af4fSAkash Goel 	gen6_disable_pm_irq(dev_priv, dev_priv->pm_rps_events);
40358072ccbSImre Deak 
40458072ccbSImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
40591c8a326SChris Wilson 	synchronize_irq(dev_priv->drm.irq);
406c33d247dSChris Wilson 
407c33d247dSChris Wilson 	/* Now that we will not be generating any more work, flush any
408c33d247dSChris Wilson 	 * outsanding tasks. As we are called on the RPS idle path,
409c33d247dSChris Wilson 	 * we will reset the GPU to minimum frequencies, so the current
410c33d247dSChris Wilson 	 * state of the worker can be discarded.
411c33d247dSChris Wilson 	 */
412c33d247dSChris Wilson 	cancel_work_sync(&dev_priv->rps.work);
413c33d247dSChris Wilson 	gen6_reset_rps_interrupts(dev_priv);
414b900b949SImre Deak }
415b900b949SImre Deak 
41626705e20SSagar Arun Kamble void gen9_reset_guc_interrupts(struct drm_i915_private *dev_priv)
41726705e20SSagar Arun Kamble {
41826705e20SSagar Arun Kamble 	spin_lock_irq(&dev_priv->irq_lock);
41926705e20SSagar Arun Kamble 	gen6_reset_pm_iir(dev_priv, dev_priv->pm_guc_events);
42026705e20SSagar Arun Kamble 	spin_unlock_irq(&dev_priv->irq_lock);
42126705e20SSagar Arun Kamble }
42226705e20SSagar Arun Kamble 
42326705e20SSagar Arun Kamble void gen9_enable_guc_interrupts(struct drm_i915_private *dev_priv)
42426705e20SSagar Arun Kamble {
42526705e20SSagar Arun Kamble 	spin_lock_irq(&dev_priv->irq_lock);
42626705e20SSagar Arun Kamble 	if (!dev_priv->guc.interrupts_enabled) {
42726705e20SSagar Arun Kamble 		WARN_ON_ONCE(I915_READ(gen6_pm_iir(dev_priv)) &
42826705e20SSagar Arun Kamble 				       dev_priv->pm_guc_events);
42926705e20SSagar Arun Kamble 		dev_priv->guc.interrupts_enabled = true;
43026705e20SSagar Arun Kamble 		gen6_enable_pm_irq(dev_priv, dev_priv->pm_guc_events);
43126705e20SSagar Arun Kamble 	}
43226705e20SSagar Arun Kamble 	spin_unlock_irq(&dev_priv->irq_lock);
43326705e20SSagar Arun Kamble }
43426705e20SSagar Arun Kamble 
43526705e20SSagar Arun Kamble void gen9_disable_guc_interrupts(struct drm_i915_private *dev_priv)
43626705e20SSagar Arun Kamble {
43726705e20SSagar Arun Kamble 	spin_lock_irq(&dev_priv->irq_lock);
43826705e20SSagar Arun Kamble 	dev_priv->guc.interrupts_enabled = false;
43926705e20SSagar Arun Kamble 
44026705e20SSagar Arun Kamble 	gen6_disable_pm_irq(dev_priv, dev_priv->pm_guc_events);
44126705e20SSagar Arun Kamble 
44226705e20SSagar Arun Kamble 	spin_unlock_irq(&dev_priv->irq_lock);
44326705e20SSagar Arun Kamble 	synchronize_irq(dev_priv->drm.irq);
44426705e20SSagar Arun Kamble 
44526705e20SSagar Arun Kamble 	gen9_reset_guc_interrupts(dev_priv);
44626705e20SSagar Arun Kamble }
44726705e20SSagar Arun Kamble 
4480961021aSBen Widawsky /**
4493a3b3c7dSVille Syrjälä  * bdw_update_port_irq - update DE port interrupt
4503a3b3c7dSVille Syrjälä  * @dev_priv: driver private
4513a3b3c7dSVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
4523a3b3c7dSVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
4533a3b3c7dSVille Syrjälä  */
4543a3b3c7dSVille Syrjälä static void bdw_update_port_irq(struct drm_i915_private *dev_priv,
4553a3b3c7dSVille Syrjälä 				uint32_t interrupt_mask,
4563a3b3c7dSVille Syrjälä 				uint32_t enabled_irq_mask)
4573a3b3c7dSVille Syrjälä {
4583a3b3c7dSVille Syrjälä 	uint32_t new_val;
4593a3b3c7dSVille Syrjälä 	uint32_t old_val;
4603a3b3c7dSVille Syrjälä 
46167520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
4623a3b3c7dSVille Syrjälä 
4633a3b3c7dSVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
4643a3b3c7dSVille Syrjälä 
4653a3b3c7dSVille Syrjälä 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
4663a3b3c7dSVille Syrjälä 		return;
4673a3b3c7dSVille Syrjälä 
4683a3b3c7dSVille Syrjälä 	old_val = I915_READ(GEN8_DE_PORT_IMR);
4693a3b3c7dSVille Syrjälä 
4703a3b3c7dSVille Syrjälä 	new_val = old_val;
4713a3b3c7dSVille Syrjälä 	new_val &= ~interrupt_mask;
4723a3b3c7dSVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
4733a3b3c7dSVille Syrjälä 
4743a3b3c7dSVille Syrjälä 	if (new_val != old_val) {
4753a3b3c7dSVille Syrjälä 		I915_WRITE(GEN8_DE_PORT_IMR, new_val);
4763a3b3c7dSVille Syrjälä 		POSTING_READ(GEN8_DE_PORT_IMR);
4773a3b3c7dSVille Syrjälä 	}
4783a3b3c7dSVille Syrjälä }
4793a3b3c7dSVille Syrjälä 
4803a3b3c7dSVille Syrjälä /**
481013d3752SVille Syrjälä  * bdw_update_pipe_irq - update DE pipe interrupt
482013d3752SVille Syrjälä  * @dev_priv: driver private
483013d3752SVille Syrjälä  * @pipe: pipe whose interrupt to update
484013d3752SVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
485013d3752SVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
486013d3752SVille Syrjälä  */
487013d3752SVille Syrjälä void bdw_update_pipe_irq(struct drm_i915_private *dev_priv,
488013d3752SVille Syrjälä 			 enum pipe pipe,
489013d3752SVille Syrjälä 			 uint32_t interrupt_mask,
490013d3752SVille Syrjälä 			 uint32_t enabled_irq_mask)
491013d3752SVille Syrjälä {
492013d3752SVille Syrjälä 	uint32_t new_val;
493013d3752SVille Syrjälä 
49467520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
495013d3752SVille Syrjälä 
496013d3752SVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
497013d3752SVille Syrjälä 
498013d3752SVille Syrjälä 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
499013d3752SVille Syrjälä 		return;
500013d3752SVille Syrjälä 
501013d3752SVille Syrjälä 	new_val = dev_priv->de_irq_mask[pipe];
502013d3752SVille Syrjälä 	new_val &= ~interrupt_mask;
503013d3752SVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
504013d3752SVille Syrjälä 
505013d3752SVille Syrjälä 	if (new_val != dev_priv->de_irq_mask[pipe]) {
506013d3752SVille Syrjälä 		dev_priv->de_irq_mask[pipe] = new_val;
507013d3752SVille Syrjälä 		I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
508013d3752SVille Syrjälä 		POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
509013d3752SVille Syrjälä 	}
510013d3752SVille Syrjälä }
511013d3752SVille Syrjälä 
512013d3752SVille Syrjälä /**
513fee884edSDaniel Vetter  * ibx_display_interrupt_update - update SDEIMR
514fee884edSDaniel Vetter  * @dev_priv: driver private
515fee884edSDaniel Vetter  * @interrupt_mask: mask of interrupt bits to update
516fee884edSDaniel Vetter  * @enabled_irq_mask: mask of interrupt bits to enable
517fee884edSDaniel Vetter  */
51847339cd9SDaniel Vetter void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
519fee884edSDaniel Vetter 				  uint32_t interrupt_mask,
520fee884edSDaniel Vetter 				  uint32_t enabled_irq_mask)
521fee884edSDaniel Vetter {
522fee884edSDaniel Vetter 	uint32_t sdeimr = I915_READ(SDEIMR);
523fee884edSDaniel Vetter 	sdeimr &= ~interrupt_mask;
524fee884edSDaniel Vetter 	sdeimr |= (~enabled_irq_mask & interrupt_mask);
525fee884edSDaniel Vetter 
52615a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
52715a17aaeSDaniel Vetter 
52867520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
529fee884edSDaniel Vetter 
5309df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
531c67a470bSPaulo Zanoni 		return;
532c67a470bSPaulo Zanoni 
533fee884edSDaniel Vetter 	I915_WRITE(SDEIMR, sdeimr);
534fee884edSDaniel Vetter 	POSTING_READ(SDEIMR);
535fee884edSDaniel Vetter }
5368664281bSPaulo Zanoni 
537b5ea642aSDaniel Vetter static void
538755e9019SImre Deak __i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
539755e9019SImre Deak 		       u32 enable_mask, u32 status_mask)
5407c463586SKeith Packard {
541f0f59a00SVille Syrjälä 	i915_reg_t reg = PIPESTAT(pipe);
542755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
5437c463586SKeith Packard 
54467520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
545d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
546b79480baSDaniel Vetter 
54704feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
54804feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
54904feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
55004feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
551755e9019SImre Deak 		return;
552755e9019SImre Deak 
553755e9019SImre Deak 	if ((pipestat & enable_mask) == enable_mask)
55446c06a30SVille Syrjälä 		return;
55546c06a30SVille Syrjälä 
55691d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] |= status_mask;
55791d181ddSImre Deak 
5587c463586SKeith Packard 	/* Enable the interrupt, clear any pending status */
559755e9019SImre Deak 	pipestat |= enable_mask | status_mask;
56046c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
5613143a2bfSChris Wilson 	POSTING_READ(reg);
5627c463586SKeith Packard }
5637c463586SKeith Packard 
564b5ea642aSDaniel Vetter static void
565755e9019SImre Deak __i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
566755e9019SImre Deak 		        u32 enable_mask, u32 status_mask)
5677c463586SKeith Packard {
568f0f59a00SVille Syrjälä 	i915_reg_t reg = PIPESTAT(pipe);
569755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
5707c463586SKeith Packard 
57167520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
572d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
573b79480baSDaniel Vetter 
57404feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
57504feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
57604feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
57704feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
57846c06a30SVille Syrjälä 		return;
57946c06a30SVille Syrjälä 
580755e9019SImre Deak 	if ((pipestat & enable_mask) == 0)
581755e9019SImre Deak 		return;
582755e9019SImre Deak 
58391d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] &= ~status_mask;
58491d181ddSImre Deak 
585755e9019SImre Deak 	pipestat &= ~enable_mask;
58646c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
5873143a2bfSChris Wilson 	POSTING_READ(reg);
5887c463586SKeith Packard }
5897c463586SKeith Packard 
59010c59c51SImre Deak static u32 vlv_get_pipestat_enable_mask(struct drm_device *dev, u32 status_mask)
59110c59c51SImre Deak {
59210c59c51SImre Deak 	u32 enable_mask = status_mask << 16;
59310c59c51SImre Deak 
59410c59c51SImre Deak 	/*
595724a6905SVille Syrjälä 	 * On pipe A we don't support the PSR interrupt yet,
596724a6905SVille Syrjälä 	 * on pipe B and C the same bit MBZ.
59710c59c51SImre Deak 	 */
59810c59c51SImre Deak 	if (WARN_ON_ONCE(status_mask & PIPE_A_PSR_STATUS_VLV))
59910c59c51SImre Deak 		return 0;
600724a6905SVille Syrjälä 	/*
601724a6905SVille Syrjälä 	 * On pipe B and C we don't support the PSR interrupt yet, on pipe
602724a6905SVille Syrjälä 	 * A the same bit is for perf counters which we don't use either.
603724a6905SVille Syrjälä 	 */
604724a6905SVille Syrjälä 	if (WARN_ON_ONCE(status_mask & PIPE_B_PSR_STATUS_VLV))
605724a6905SVille Syrjälä 		return 0;
60610c59c51SImre Deak 
60710c59c51SImre Deak 	enable_mask &= ~(PIPE_FIFO_UNDERRUN_STATUS |
60810c59c51SImre Deak 			 SPRITE0_FLIP_DONE_INT_EN_VLV |
60910c59c51SImre Deak 			 SPRITE1_FLIP_DONE_INT_EN_VLV);
61010c59c51SImre Deak 	if (status_mask & SPRITE0_FLIP_DONE_INT_STATUS_VLV)
61110c59c51SImre Deak 		enable_mask |= SPRITE0_FLIP_DONE_INT_EN_VLV;
61210c59c51SImre Deak 	if (status_mask & SPRITE1_FLIP_DONE_INT_STATUS_VLV)
61310c59c51SImre Deak 		enable_mask |= SPRITE1_FLIP_DONE_INT_EN_VLV;
61410c59c51SImre Deak 
61510c59c51SImre Deak 	return enable_mask;
61610c59c51SImre Deak }
61710c59c51SImre Deak 
618755e9019SImre Deak void
619755e9019SImre Deak i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
620755e9019SImre Deak 		     u32 status_mask)
621755e9019SImre Deak {
622755e9019SImre Deak 	u32 enable_mask;
623755e9019SImre Deak 
624666a4537SWayne Boyer 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
62591c8a326SChris Wilson 		enable_mask = vlv_get_pipestat_enable_mask(&dev_priv->drm,
62610c59c51SImre Deak 							   status_mask);
62710c59c51SImre Deak 	else
628755e9019SImre Deak 		enable_mask = status_mask << 16;
629755e9019SImre Deak 	__i915_enable_pipestat(dev_priv, pipe, enable_mask, status_mask);
630755e9019SImre Deak }
631755e9019SImre Deak 
632755e9019SImre Deak void
633755e9019SImre Deak i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
634755e9019SImre Deak 		      u32 status_mask)
635755e9019SImre Deak {
636755e9019SImre Deak 	u32 enable_mask;
637755e9019SImre Deak 
638666a4537SWayne Boyer 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
63991c8a326SChris Wilson 		enable_mask = vlv_get_pipestat_enable_mask(&dev_priv->drm,
64010c59c51SImre Deak 							   status_mask);
64110c59c51SImre Deak 	else
642755e9019SImre Deak 		enable_mask = status_mask << 16;
643755e9019SImre Deak 	__i915_disable_pipestat(dev_priv, pipe, enable_mask, status_mask);
644755e9019SImre Deak }
645755e9019SImre Deak 
646c0e09200SDave Airlie /**
647f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
64814bb2c11STvrtko Ursulin  * @dev_priv: i915 device private
64901c66889SZhao Yakui  */
65091d14251STvrtko Ursulin static void i915_enable_asle_pipestat(struct drm_i915_private *dev_priv)
65101c66889SZhao Yakui {
65291d14251STvrtko Ursulin 	if (!dev_priv->opregion.asle || !IS_MOBILE(dev_priv))
653f49e38ddSJani Nikula 		return;
654f49e38ddSJani Nikula 
65513321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
65601c66889SZhao Yakui 
657755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_STATUS);
65891d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 4)
6593b6c42e8SDaniel Vetter 		i915_enable_pipestat(dev_priv, PIPE_A,
660755e9019SImre Deak 				     PIPE_LEGACY_BLC_EVENT_STATUS);
6611ec14ad3SChris Wilson 
66213321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
66301c66889SZhao Yakui }
66401c66889SZhao Yakui 
665f75f3746SVille Syrjälä /*
666f75f3746SVille Syrjälä  * This timing diagram depicts the video signal in and
667f75f3746SVille Syrjälä  * around the vertical blanking period.
668f75f3746SVille Syrjälä  *
669f75f3746SVille Syrjälä  * Assumptions about the fictitious mode used in this example:
670f75f3746SVille Syrjälä  *  vblank_start >= 3
671f75f3746SVille Syrjälä  *  vsync_start = vblank_start + 1
672f75f3746SVille Syrjälä  *  vsync_end = vblank_start + 2
673f75f3746SVille Syrjälä  *  vtotal = vblank_start + 3
674f75f3746SVille Syrjälä  *
675f75f3746SVille Syrjälä  *           start of vblank:
676f75f3746SVille Syrjälä  *           latch double buffered registers
677f75f3746SVille Syrjälä  *           increment frame counter (ctg+)
678f75f3746SVille Syrjälä  *           generate start of vblank interrupt (gen4+)
679f75f3746SVille Syrjälä  *           |
680f75f3746SVille Syrjälä  *           |          frame start:
681f75f3746SVille Syrjälä  *           |          generate frame start interrupt (aka. vblank interrupt) (gmch)
682f75f3746SVille Syrjälä  *           |          may be shifted forward 1-3 extra lines via PIPECONF
683f75f3746SVille Syrjälä  *           |          |
684f75f3746SVille Syrjälä  *           |          |  start of vsync:
685f75f3746SVille Syrjälä  *           |          |  generate vsync interrupt
686f75f3746SVille Syrjälä  *           |          |  |
687f75f3746SVille Syrjälä  * ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx
688f75f3746SVille Syrjälä  *       .   \hs/   .      \hs/          \hs/          \hs/   .      \hs/
689f75f3746SVille Syrjälä  * ----va---> <-----------------vb--------------------> <--------va-------------
690f75f3746SVille Syrjälä  *       |          |       <----vs----->                     |
691f75f3746SVille Syrjälä  * -vbs-----> <---vbs+1---> <---vbs+2---> <-----0-----> <-----1-----> <-----2--- (scanline counter gen2)
692f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2---> <-----0--- (scanline counter gen3+)
693f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2- (scanline counter hsw+ hdmi)
694f75f3746SVille Syrjälä  *       |          |                                         |
695f75f3746SVille Syrjälä  *       last visible pixel                                   first visible pixel
696f75f3746SVille Syrjälä  *                  |                                         increment frame counter (gen3/4)
697f75f3746SVille Syrjälä  *                  pixel counter = vblank_start * htotal     pixel counter = 0 (gen3/4)
698f75f3746SVille Syrjälä  *
699f75f3746SVille Syrjälä  * x  = horizontal active
700f75f3746SVille Syrjälä  * _  = horizontal blanking
701f75f3746SVille Syrjälä  * hs = horizontal sync
702f75f3746SVille Syrjälä  * va = vertical active
703f75f3746SVille Syrjälä  * vb = vertical blanking
704f75f3746SVille Syrjälä  * vs = vertical sync
705f75f3746SVille Syrjälä  * vbs = vblank_start (number)
706f75f3746SVille Syrjälä  *
707f75f3746SVille Syrjälä  * Summary:
708f75f3746SVille Syrjälä  * - most events happen at the start of horizontal sync
709f75f3746SVille Syrjälä  * - frame start happens at the start of horizontal blank, 1-4 lines
710f75f3746SVille Syrjälä  *   (depending on PIPECONF settings) after the start of vblank
711f75f3746SVille Syrjälä  * - gen3/4 pixel and frame counter are synchronized with the start
712f75f3746SVille Syrjälä  *   of horizontal active on the first line of vertical active
713f75f3746SVille Syrjälä  */
714f75f3746SVille Syrjälä 
71542f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
71642f52ef8SKeith Packard  * we use as a pipe index
71742f52ef8SKeith Packard  */
71888e72717SThierry Reding static u32 i915_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
7190a3e67a4SJesse Barnes {
720fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
721f0f59a00SVille Syrjälä 	i915_reg_t high_frame, low_frame;
7220b2a8e09SVille Syrjälä 	u32 high1, high2, low, pixel, vbl_start, hsync_start, htotal;
7235caa0feaSDaniel Vetter 	const struct drm_display_mode *mode = &dev->vblank[pipe].hwmode;
724694e409dSVille Syrjälä 	unsigned long irqflags;
725391f75e2SVille Syrjälä 
7260b2a8e09SVille Syrjälä 	htotal = mode->crtc_htotal;
7270b2a8e09SVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
7280b2a8e09SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
7290b2a8e09SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
7300b2a8e09SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
731391f75e2SVille Syrjälä 
7320b2a8e09SVille Syrjälä 	/* Convert to pixel count */
7330b2a8e09SVille Syrjälä 	vbl_start *= htotal;
7340b2a8e09SVille Syrjälä 
7350b2a8e09SVille Syrjälä 	/* Start of vblank event occurs at start of hsync */
7360b2a8e09SVille Syrjälä 	vbl_start -= htotal - hsync_start;
7370b2a8e09SVille Syrjälä 
7389db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
7399db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
7405eddb70bSChris Wilson 
741694e409dSVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
742694e409dSVille Syrjälä 
7430a3e67a4SJesse Barnes 	/*
7440a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
7450a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
7460a3e67a4SJesse Barnes 	 * register.
7470a3e67a4SJesse Barnes 	 */
7480a3e67a4SJesse Barnes 	do {
749694e409dSVille Syrjälä 		high1 = I915_READ_FW(high_frame) & PIPE_FRAME_HIGH_MASK;
750694e409dSVille Syrjälä 		low   = I915_READ_FW(low_frame);
751694e409dSVille Syrjälä 		high2 = I915_READ_FW(high_frame) & PIPE_FRAME_HIGH_MASK;
7520a3e67a4SJesse Barnes 	} while (high1 != high2);
7530a3e67a4SJesse Barnes 
754694e409dSVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
755694e409dSVille Syrjälä 
7565eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
757391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
7585eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
759391f75e2SVille Syrjälä 
760391f75e2SVille Syrjälä 	/*
761391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
762391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
763391f75e2SVille Syrjälä 	 * counter against vblank start.
764391f75e2SVille Syrjälä 	 */
765edc08d0aSVille Syrjälä 	return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
7660a3e67a4SJesse Barnes }
7670a3e67a4SJesse Barnes 
768974e59baSDave Airlie static u32 g4x_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
7699880b7a5SJesse Barnes {
770fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
7719880b7a5SJesse Barnes 
772649636efSVille Syrjälä 	return I915_READ(PIPE_FRMCOUNT_G4X(pipe));
7739880b7a5SJesse Barnes }
7749880b7a5SJesse Barnes 
77575aa3f63SVille Syrjälä /* I915_READ_FW, only for fast reads of display block, no need for forcewake etc. */
776a225f079SVille Syrjälä static int __intel_get_crtc_scanline(struct intel_crtc *crtc)
777a225f079SVille Syrjälä {
778a225f079SVille Syrjälä 	struct drm_device *dev = crtc->base.dev;
779fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
7805caa0feaSDaniel Vetter 	const struct drm_display_mode *mode;
7815caa0feaSDaniel Vetter 	struct drm_vblank_crtc *vblank;
782a225f079SVille Syrjälä 	enum pipe pipe = crtc->pipe;
78380715b2fSVille Syrjälä 	int position, vtotal;
784a225f079SVille Syrjälä 
78572259536SVille Syrjälä 	if (!crtc->active)
78672259536SVille Syrjälä 		return -1;
78772259536SVille Syrjälä 
7885caa0feaSDaniel Vetter 	vblank = &crtc->base.dev->vblank[drm_crtc_index(&crtc->base)];
7895caa0feaSDaniel Vetter 	mode = &vblank->hwmode;
7905caa0feaSDaniel Vetter 
79180715b2fSVille Syrjälä 	vtotal = mode->crtc_vtotal;
792a225f079SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
793a225f079SVille Syrjälä 		vtotal /= 2;
794a225f079SVille Syrjälä 
79591d14251STvrtko Ursulin 	if (IS_GEN2(dev_priv))
79675aa3f63SVille Syrjälä 		position = I915_READ_FW(PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
797a225f079SVille Syrjälä 	else
79875aa3f63SVille Syrjälä 		position = I915_READ_FW(PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
799a225f079SVille Syrjälä 
800a225f079SVille Syrjälä 	/*
80141b578fbSJesse Barnes 	 * On HSW, the DSL reg (0x70000) appears to return 0 if we
80241b578fbSJesse Barnes 	 * read it just before the start of vblank.  So try it again
80341b578fbSJesse Barnes 	 * so we don't accidentally end up spanning a vblank frame
80441b578fbSJesse Barnes 	 * increment, causing the pipe_update_end() code to squak at us.
80541b578fbSJesse Barnes 	 *
80641b578fbSJesse Barnes 	 * The nature of this problem means we can't simply check the ISR
80741b578fbSJesse Barnes 	 * bit and return the vblank start value; nor can we use the scanline
80841b578fbSJesse Barnes 	 * debug register in the transcoder as it appears to have the same
80941b578fbSJesse Barnes 	 * problem.  We may need to extend this to include other platforms,
81041b578fbSJesse Barnes 	 * but so far testing only shows the problem on HSW.
81141b578fbSJesse Barnes 	 */
81291d14251STvrtko Ursulin 	if (HAS_DDI(dev_priv) && !position) {
81341b578fbSJesse Barnes 		int i, temp;
81441b578fbSJesse Barnes 
81541b578fbSJesse Barnes 		for (i = 0; i < 100; i++) {
81641b578fbSJesse Barnes 			udelay(1);
817707bdd3fSVille Syrjälä 			temp = I915_READ_FW(PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
81841b578fbSJesse Barnes 			if (temp != position) {
81941b578fbSJesse Barnes 				position = temp;
82041b578fbSJesse Barnes 				break;
82141b578fbSJesse Barnes 			}
82241b578fbSJesse Barnes 		}
82341b578fbSJesse Barnes 	}
82441b578fbSJesse Barnes 
82541b578fbSJesse Barnes 	/*
82680715b2fSVille Syrjälä 	 * See update_scanline_offset() for the details on the
82780715b2fSVille Syrjälä 	 * scanline_offset adjustment.
828a225f079SVille Syrjälä 	 */
82980715b2fSVille Syrjälä 	return (position + crtc->scanline_offset) % vtotal;
830a225f079SVille Syrjälä }
831a225f079SVille Syrjälä 
8321bf6ad62SDaniel Vetter static bool i915_get_crtc_scanoutpos(struct drm_device *dev, unsigned int pipe,
8331bf6ad62SDaniel Vetter 				     bool in_vblank_irq, int *vpos, int *hpos,
8343bb403bfSVille Syrjälä 				     ktime_t *stime, ktime_t *etime,
8353bb403bfSVille Syrjälä 				     const struct drm_display_mode *mode)
8360af7e4dfSMario Kleiner {
837fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
83898187836SVille Syrjälä 	struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
83998187836SVille Syrjälä 								pipe);
8403aa18df8SVille Syrjälä 	int position;
84178e8fc6bSVille Syrjälä 	int vbl_start, vbl_end, hsync_start, htotal, vtotal;
8420af7e4dfSMario Kleiner 	bool in_vbl = true;
843ad3543edSMario Kleiner 	unsigned long irqflags;
8440af7e4dfSMario Kleiner 
845fc467a22SMaarten Lankhorst 	if (WARN_ON(!mode->crtc_clock)) {
8460af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
8479db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
8481bf6ad62SDaniel Vetter 		return false;
8490af7e4dfSMario Kleiner 	}
8500af7e4dfSMario Kleiner 
851c2baf4b7SVille Syrjälä 	htotal = mode->crtc_htotal;
85278e8fc6bSVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
853c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
854c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
855c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
8560af7e4dfSMario Kleiner 
857d31faf65SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
858d31faf65SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
859d31faf65SVille Syrjälä 		vbl_end /= 2;
860d31faf65SVille Syrjälä 		vtotal /= 2;
861d31faf65SVille Syrjälä 	}
862d31faf65SVille Syrjälä 
863ad3543edSMario Kleiner 	/*
864ad3543edSMario Kleiner 	 * Lock uncore.lock, as we will do multiple timing critical raw
865ad3543edSMario Kleiner 	 * register reads, potentially with preemption disabled, so the
866ad3543edSMario Kleiner 	 * following code must not block on uncore.lock.
867ad3543edSMario Kleiner 	 */
868ad3543edSMario Kleiner 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
869ad3543edSMario Kleiner 
870ad3543edSMario Kleiner 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
871ad3543edSMario Kleiner 
872ad3543edSMario Kleiner 	/* Get optional system timestamp before query. */
873ad3543edSMario Kleiner 	if (stime)
874ad3543edSMario Kleiner 		*stime = ktime_get();
875ad3543edSMario Kleiner 
87691d14251STvrtko Ursulin 	if (IS_GEN2(dev_priv) || IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5) {
8770af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
8780af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
8790af7e4dfSMario Kleiner 		 */
880a225f079SVille Syrjälä 		position = __intel_get_crtc_scanline(intel_crtc);
8810af7e4dfSMario Kleiner 	} else {
8820af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
8830af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
8840af7e4dfSMario Kleiner 		 * scanout position.
8850af7e4dfSMario Kleiner 		 */
88675aa3f63SVille Syrjälä 		position = (I915_READ_FW(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
8870af7e4dfSMario Kleiner 
8883aa18df8SVille Syrjälä 		/* convert to pixel counts */
8893aa18df8SVille Syrjälä 		vbl_start *= htotal;
8903aa18df8SVille Syrjälä 		vbl_end *= htotal;
8913aa18df8SVille Syrjälä 		vtotal *= htotal;
89278e8fc6bSVille Syrjälä 
89378e8fc6bSVille Syrjälä 		/*
8947e78f1cbSVille Syrjälä 		 * In interlaced modes, the pixel counter counts all pixels,
8957e78f1cbSVille Syrjälä 		 * so one field will have htotal more pixels. In order to avoid
8967e78f1cbSVille Syrjälä 		 * the reported position from jumping backwards when the pixel
8977e78f1cbSVille Syrjälä 		 * counter is beyond the length of the shorter field, just
8987e78f1cbSVille Syrjälä 		 * clamp the position the length of the shorter field. This
8997e78f1cbSVille Syrjälä 		 * matches how the scanline counter based position works since
9007e78f1cbSVille Syrjälä 		 * the scanline counter doesn't count the two half lines.
9017e78f1cbSVille Syrjälä 		 */
9027e78f1cbSVille Syrjälä 		if (position >= vtotal)
9037e78f1cbSVille Syrjälä 			position = vtotal - 1;
9047e78f1cbSVille Syrjälä 
9057e78f1cbSVille Syrjälä 		/*
90678e8fc6bSVille Syrjälä 		 * Start of vblank interrupt is triggered at start of hsync,
90778e8fc6bSVille Syrjälä 		 * just prior to the first active line of vblank. However we
90878e8fc6bSVille Syrjälä 		 * consider lines to start at the leading edge of horizontal
90978e8fc6bSVille Syrjälä 		 * active. So, should we get here before we've crossed into
91078e8fc6bSVille Syrjälä 		 * the horizontal active of the first line in vblank, we would
91178e8fc6bSVille Syrjälä 		 * not set the DRM_SCANOUTPOS_INVBL flag. In order to fix that,
91278e8fc6bSVille Syrjälä 		 * always add htotal-hsync_start to the current pixel position.
91378e8fc6bSVille Syrjälä 		 */
91478e8fc6bSVille Syrjälä 		position = (position + htotal - hsync_start) % vtotal;
9153aa18df8SVille Syrjälä 	}
9163aa18df8SVille Syrjälä 
917ad3543edSMario Kleiner 	/* Get optional system timestamp after query. */
918ad3543edSMario Kleiner 	if (etime)
919ad3543edSMario Kleiner 		*etime = ktime_get();
920ad3543edSMario Kleiner 
921ad3543edSMario Kleiner 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
922ad3543edSMario Kleiner 
923ad3543edSMario Kleiner 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
924ad3543edSMario Kleiner 
9253aa18df8SVille Syrjälä 	in_vbl = position >= vbl_start && position < vbl_end;
9263aa18df8SVille Syrjälä 
9273aa18df8SVille Syrjälä 	/*
9283aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
9293aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
9303aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
9313aa18df8SVille Syrjälä 	 * up since vbl_end.
9323aa18df8SVille Syrjälä 	 */
9333aa18df8SVille Syrjälä 	if (position >= vbl_start)
9343aa18df8SVille Syrjälä 		position -= vbl_end;
9353aa18df8SVille Syrjälä 	else
9363aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
9373aa18df8SVille Syrjälä 
93891d14251STvrtko Ursulin 	if (IS_GEN2(dev_priv) || IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5) {
9393aa18df8SVille Syrjälä 		*vpos = position;
9403aa18df8SVille Syrjälä 		*hpos = 0;
9413aa18df8SVille Syrjälä 	} else {
9420af7e4dfSMario Kleiner 		*vpos = position / htotal;
9430af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
9440af7e4dfSMario Kleiner 	}
9450af7e4dfSMario Kleiner 
9461bf6ad62SDaniel Vetter 	return true;
9470af7e4dfSMario Kleiner }
9480af7e4dfSMario Kleiner 
949a225f079SVille Syrjälä int intel_get_crtc_scanline(struct intel_crtc *crtc)
950a225f079SVille Syrjälä {
951fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
952a225f079SVille Syrjälä 	unsigned long irqflags;
953a225f079SVille Syrjälä 	int position;
954a225f079SVille Syrjälä 
955a225f079SVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
956a225f079SVille Syrjälä 	position = __intel_get_crtc_scanline(crtc);
957a225f079SVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
958a225f079SVille Syrjälä 
959a225f079SVille Syrjälä 	return position;
960a225f079SVille Syrjälä }
961a225f079SVille Syrjälä 
96291d14251STvrtko Ursulin static void ironlake_rps_change_irq_handler(struct drm_i915_private *dev_priv)
963f97108d1SJesse Barnes {
964b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
9659270388eSDaniel Vetter 	u8 new_delay;
9669270388eSDaniel Vetter 
967d0ecd7e2SDaniel Vetter 	spin_lock(&mchdev_lock);
968f97108d1SJesse Barnes 
96973edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
97073edd18fSDaniel Vetter 
97120e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
9729270388eSDaniel Vetter 
9737648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
974b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
975b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
976f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
977f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
978f97108d1SJesse Barnes 
979f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
980b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
98120e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
98220e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
98320e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
98420e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
985b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
98620e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
98720e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
98820e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
98920e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
990f97108d1SJesse Barnes 	}
991f97108d1SJesse Barnes 
99291d14251STvrtko Ursulin 	if (ironlake_set_drps(dev_priv, new_delay))
99320e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
994f97108d1SJesse Barnes 
995d0ecd7e2SDaniel Vetter 	spin_unlock(&mchdev_lock);
9969270388eSDaniel Vetter 
997f97108d1SJesse Barnes 	return;
998f97108d1SJesse Barnes }
999f97108d1SJesse Barnes 
10000bc40be8STvrtko Ursulin static void notify_ring(struct intel_engine_cs *engine)
1001549f7365SChris Wilson {
100256299fb7SChris Wilson 	struct drm_i915_gem_request *rq = NULL;
100356299fb7SChris Wilson 	struct intel_wait *wait;
1004dffabc8fSTvrtko Ursulin 
10052246bea6SChris Wilson 	atomic_inc(&engine->irq_count);
1006538b257dSChris Wilson 	set_bit(ENGINE_IRQ_BREADCRUMB, &engine->irq_posted);
100756299fb7SChris Wilson 
100861d3dc70SChris Wilson 	spin_lock(&engine->breadcrumbs.irq_lock);
100961d3dc70SChris Wilson 	wait = engine->breadcrumbs.irq_wait;
101056299fb7SChris Wilson 	if (wait) {
101156299fb7SChris Wilson 		/* We use a callback from the dma-fence to submit
101256299fb7SChris Wilson 		 * requests after waiting on our own requests. To
101356299fb7SChris Wilson 		 * ensure minimum delay in queuing the next request to
101456299fb7SChris Wilson 		 * hardware, signal the fence now rather than wait for
101556299fb7SChris Wilson 		 * the signaler to be woken up. We still wake up the
101656299fb7SChris Wilson 		 * waiter in order to handle the irq-seqno coherency
101756299fb7SChris Wilson 		 * issues (we may receive the interrupt before the
101856299fb7SChris Wilson 		 * seqno is written, see __i915_request_irq_complete())
101956299fb7SChris Wilson 		 * and to handle coalescing of multiple seqno updates
102056299fb7SChris Wilson 		 * and many waiters.
102156299fb7SChris Wilson 		 */
102256299fb7SChris Wilson 		if (i915_seqno_passed(intel_engine_get_seqno(engine),
1023db93991bSChris Wilson 				      wait->seqno) &&
1024db93991bSChris Wilson 		    !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
1025db93991bSChris Wilson 			      &wait->request->fence.flags))
102624754d75SChris Wilson 			rq = i915_gem_request_get(wait->request);
102756299fb7SChris Wilson 
102856299fb7SChris Wilson 		wake_up_process(wait->tsk);
102967b807a8SChris Wilson 	} else {
103067b807a8SChris Wilson 		__intel_engine_disarm_breadcrumbs(engine);
103156299fb7SChris Wilson 	}
103261d3dc70SChris Wilson 	spin_unlock(&engine->breadcrumbs.irq_lock);
103356299fb7SChris Wilson 
103424754d75SChris Wilson 	if (rq) {
103556299fb7SChris Wilson 		dma_fence_signal(&rq->fence);
103624754d75SChris Wilson 		i915_gem_request_put(rq);
103724754d75SChris Wilson 	}
103856299fb7SChris Wilson 
103956299fb7SChris Wilson 	trace_intel_engine_notify(engine, wait);
1040549f7365SChris Wilson }
1041549f7365SChris Wilson 
104243cf3bf0SChris Wilson static void vlv_c0_read(struct drm_i915_private *dev_priv,
104343cf3bf0SChris Wilson 			struct intel_rps_ei *ei)
104431685c25SDeepak S {
1045679cb6c1SMika Kuoppala 	ei->ktime = ktime_get_raw();
104643cf3bf0SChris Wilson 	ei->render_c0 = I915_READ(VLV_RENDER_C0_COUNT);
104743cf3bf0SChris Wilson 	ei->media_c0 = I915_READ(VLV_MEDIA_C0_COUNT);
104831685c25SDeepak S }
104931685c25SDeepak S 
105043cf3bf0SChris Wilson void gen6_rps_reset_ei(struct drm_i915_private *dev_priv)
105143cf3bf0SChris Wilson {
1052e0e8c7cbSChris Wilson 	memset(&dev_priv->rps.ei, 0, sizeof(dev_priv->rps.ei));
105343cf3bf0SChris Wilson }
105443cf3bf0SChris Wilson 
105543cf3bf0SChris Wilson static u32 vlv_wa_c0_ei(struct drm_i915_private *dev_priv, u32 pm_iir)
105643cf3bf0SChris Wilson {
1057e0e8c7cbSChris Wilson 	const struct intel_rps_ei *prev = &dev_priv->rps.ei;
105843cf3bf0SChris Wilson 	struct intel_rps_ei now;
105943cf3bf0SChris Wilson 	u32 events = 0;
106043cf3bf0SChris Wilson 
1061e0e8c7cbSChris Wilson 	if ((pm_iir & GEN6_PM_RP_UP_EI_EXPIRED) == 0)
106243cf3bf0SChris Wilson 		return 0;
106343cf3bf0SChris Wilson 
106443cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &now);
106531685c25SDeepak S 
1066679cb6c1SMika Kuoppala 	if (prev->ktime) {
1067e0e8c7cbSChris Wilson 		u64 time, c0;
1068569884e3SChris Wilson 		u32 render, media;
1069e0e8c7cbSChris Wilson 
1070679cb6c1SMika Kuoppala 		time = ktime_us_delta(now.ktime, prev->ktime);
10718f68d591SChris Wilson 
1072e0e8c7cbSChris Wilson 		time *= dev_priv->czclk_freq;
1073e0e8c7cbSChris Wilson 
1074e0e8c7cbSChris Wilson 		/* Workload can be split between render + media,
1075e0e8c7cbSChris Wilson 		 * e.g. SwapBuffers being blitted in X after being rendered in
1076e0e8c7cbSChris Wilson 		 * mesa. To account for this we need to combine both engines
1077e0e8c7cbSChris Wilson 		 * into our activity counter.
1078e0e8c7cbSChris Wilson 		 */
1079569884e3SChris Wilson 		render = now.render_c0 - prev->render_c0;
1080569884e3SChris Wilson 		media = now.media_c0 - prev->media_c0;
1081569884e3SChris Wilson 		c0 = max(render, media);
10826b7f6aa7SMika Kuoppala 		c0 *= 1000 * 100 << 8; /* to usecs and scale to threshold% */
1083e0e8c7cbSChris Wilson 
1084e0e8c7cbSChris Wilson 		if (c0 > time * dev_priv->rps.up_threshold)
1085e0e8c7cbSChris Wilson 			events = GEN6_PM_RP_UP_THRESHOLD;
1086e0e8c7cbSChris Wilson 		else if (c0 < time * dev_priv->rps.down_threshold)
1087e0e8c7cbSChris Wilson 			events = GEN6_PM_RP_DOWN_THRESHOLD;
108831685c25SDeepak S 	}
108931685c25SDeepak S 
1090e0e8c7cbSChris Wilson 	dev_priv->rps.ei = now;
109143cf3bf0SChris Wilson 	return events;
109231685c25SDeepak S }
109331685c25SDeepak S 
1094f5a4c67dSChris Wilson static bool any_waiters(struct drm_i915_private *dev_priv)
1095f5a4c67dSChris Wilson {
1096e2f80391STvrtko Ursulin 	struct intel_engine_cs *engine;
10973b3f1650SAkash Goel 	enum intel_engine_id id;
1098f5a4c67dSChris Wilson 
10993b3f1650SAkash Goel 	for_each_engine(engine, dev_priv, id)
1100688e6c72SChris Wilson 		if (intel_engine_has_waiter(engine))
1101f5a4c67dSChris Wilson 			return true;
1102f5a4c67dSChris Wilson 
1103f5a4c67dSChris Wilson 	return false;
1104f5a4c67dSChris Wilson }
1105f5a4c67dSChris Wilson 
11064912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
11073b8d8d91SJesse Barnes {
11082d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
11092d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, rps.work);
11107c0a16adSChris Wilson 	bool client_boost = false;
11118d3afd7dSChris Wilson 	int new_delay, adj, min, max;
11127c0a16adSChris Wilson 	u32 pm_iir = 0;
11133b8d8d91SJesse Barnes 
111459cdb63dSDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
11157c0a16adSChris Wilson 	if (dev_priv->rps.interrupts_enabled) {
11167c0a16adSChris Wilson 		pm_iir = fetch_and_zero(&dev_priv->rps.pm_iir);
11177c0a16adSChris Wilson 		client_boost = fetch_and_zero(&dev_priv->rps.client_boost);
1118d4d70aa5SImre Deak 	}
111959cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
11204912d041SBen Widawsky 
112160611c13SPaulo Zanoni 	/* Make sure we didn't queue anything we're not going to process. */
1122a6706b45SDeepak S 	WARN_ON(pm_iir & ~dev_priv->pm_rps_events);
11238d3afd7dSChris Wilson 	if ((pm_iir & dev_priv->pm_rps_events) == 0 && !client_boost)
11247c0a16adSChris Wilson 		goto out;
11253b8d8d91SJesse Barnes 
11264fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
11277b9e0ae6SChris Wilson 
112843cf3bf0SChris Wilson 	pm_iir |= vlv_wa_c0_ei(dev_priv, pm_iir);
112943cf3bf0SChris Wilson 
1130dd75fdc8SChris Wilson 	adj = dev_priv->rps.last_adj;
1131edcf284bSChris Wilson 	new_delay = dev_priv->rps.cur_freq;
11328d3afd7dSChris Wilson 	min = dev_priv->rps.min_freq_softlimit;
11338d3afd7dSChris Wilson 	max = dev_priv->rps.max_freq_softlimit;
113429ecd78dSChris Wilson 	if (client_boost || any_waiters(dev_priv))
113529ecd78dSChris Wilson 		max = dev_priv->rps.max_freq;
113629ecd78dSChris Wilson 	if (client_boost && new_delay < dev_priv->rps.boost_freq) {
113729ecd78dSChris Wilson 		new_delay = dev_priv->rps.boost_freq;
11388d3afd7dSChris Wilson 		adj = 0;
11398d3afd7dSChris Wilson 	} else if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
1140dd75fdc8SChris Wilson 		if (adj > 0)
1141dd75fdc8SChris Wilson 			adj *= 2;
1142edcf284bSChris Wilson 		else /* CHV needs even encode values */
1143edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? 2 : 1;
11447e79a683SSagar Arun Kamble 
11457e79a683SSagar Arun Kamble 		if (new_delay >= dev_priv->rps.max_freq_softlimit)
11467e79a683SSagar Arun Kamble 			adj = 0;
114729ecd78dSChris Wilson 	} else if (client_boost || any_waiters(dev_priv)) {
1148f5a4c67dSChris Wilson 		adj = 0;
1149dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
1150b39fb297SBen Widawsky 		if (dev_priv->rps.cur_freq > dev_priv->rps.efficient_freq)
1151b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
115217136d54SChris Wilson 		else if (dev_priv->rps.cur_freq > dev_priv->rps.min_freq_softlimit)
1153b39fb297SBen Widawsky 			new_delay = dev_priv->rps.min_freq_softlimit;
1154dd75fdc8SChris Wilson 		adj = 0;
1155dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1156dd75fdc8SChris Wilson 		if (adj < 0)
1157dd75fdc8SChris Wilson 			adj *= 2;
1158edcf284bSChris Wilson 		else /* CHV needs even encode values */
1159edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? -2 : -1;
11607e79a683SSagar Arun Kamble 
11617e79a683SSagar Arun Kamble 		if (new_delay <= dev_priv->rps.min_freq_softlimit)
11627e79a683SSagar Arun Kamble 			adj = 0;
1163dd75fdc8SChris Wilson 	} else { /* unknown event */
1164edcf284bSChris Wilson 		adj = 0;
1165dd75fdc8SChris Wilson 	}
11663b8d8d91SJesse Barnes 
1167edcf284bSChris Wilson 	dev_priv->rps.last_adj = adj;
1168edcf284bSChris Wilson 
116979249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
117079249636SBen Widawsky 	 * interrupt
117179249636SBen Widawsky 	 */
1172edcf284bSChris Wilson 	new_delay += adj;
11738d3afd7dSChris Wilson 	new_delay = clamp_t(int, new_delay, min, max);
117427544369SDeepak S 
11759fcee2f7SChris Wilson 	if (intel_set_rps(dev_priv, new_delay)) {
11769fcee2f7SChris Wilson 		DRM_DEBUG_DRIVER("Failed to set new GPU frequency\n");
11779fcee2f7SChris Wilson 		dev_priv->rps.last_adj = 0;
11789fcee2f7SChris Wilson 	}
11793b8d8d91SJesse Barnes 
11804fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
11817c0a16adSChris Wilson 
11827c0a16adSChris Wilson out:
11837c0a16adSChris Wilson 	/* Make sure not to corrupt PMIMR state used by ringbuffer on GEN6 */
11847c0a16adSChris Wilson 	spin_lock_irq(&dev_priv->irq_lock);
11857c0a16adSChris Wilson 	if (dev_priv->rps.interrupts_enabled)
11867c0a16adSChris Wilson 		gen6_unmask_pm_irq(dev_priv, dev_priv->pm_rps_events);
11877c0a16adSChris Wilson 	spin_unlock_irq(&dev_priv->irq_lock);
11883b8d8d91SJesse Barnes }
11893b8d8d91SJesse Barnes 
1190e3689190SBen Widawsky 
1191e3689190SBen Widawsky /**
1192e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
1193e3689190SBen Widawsky  * occurred.
1194e3689190SBen Widawsky  * @work: workqueue struct
1195e3689190SBen Widawsky  *
1196e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
1197e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
1198e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
1199e3689190SBen Widawsky  */
1200e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
1201e3689190SBen Widawsky {
12022d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
12032d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, l3_parity.error_work);
1204e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
120535a85ac6SBen Widawsky 	char *parity_event[6];
1206e3689190SBen Widawsky 	uint32_t misccpctl;
120735a85ac6SBen Widawsky 	uint8_t slice = 0;
1208e3689190SBen Widawsky 
1209e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
1210e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
1211e3689190SBen Widawsky 	 * any time we access those registers.
1212e3689190SBen Widawsky 	 */
121391c8a326SChris Wilson 	mutex_lock(&dev_priv->drm.struct_mutex);
1214e3689190SBen Widawsky 
121535a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
121635a85ac6SBen Widawsky 	if (WARN_ON(!dev_priv->l3_parity.which_slice))
121735a85ac6SBen Widawsky 		goto out;
121835a85ac6SBen Widawsky 
1219e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
1220e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1221e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
1222e3689190SBen Widawsky 
122335a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
1224f0f59a00SVille Syrjälä 		i915_reg_t reg;
122535a85ac6SBen Widawsky 
122635a85ac6SBen Widawsky 		slice--;
12272d1fe073SJoonas Lahtinen 		if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv)))
122835a85ac6SBen Widawsky 			break;
122935a85ac6SBen Widawsky 
123035a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
123135a85ac6SBen Widawsky 
12326fa1c5f1SVille Syrjälä 		reg = GEN7_L3CDERRST1(slice);
123335a85ac6SBen Widawsky 
123435a85ac6SBen Widawsky 		error_status = I915_READ(reg);
1235e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
1236e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
1237e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1238e3689190SBen Widawsky 
123935a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
124035a85ac6SBen Widawsky 		POSTING_READ(reg);
1241e3689190SBen Widawsky 
1242cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1243e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1244e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1245e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
124635a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
124735a85ac6SBen Widawsky 		parity_event[5] = NULL;
1248e3689190SBen Widawsky 
124991c8a326SChris Wilson 		kobject_uevent_env(&dev_priv->drm.primary->kdev->kobj,
1250e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
1251e3689190SBen Widawsky 
125235a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
125335a85ac6SBen Widawsky 			  slice, row, bank, subbank);
1254e3689190SBen Widawsky 
125535a85ac6SBen Widawsky 		kfree(parity_event[4]);
1256e3689190SBen Widawsky 		kfree(parity_event[3]);
1257e3689190SBen Widawsky 		kfree(parity_event[2]);
1258e3689190SBen Widawsky 		kfree(parity_event[1]);
1259e3689190SBen Widawsky 	}
1260e3689190SBen Widawsky 
126135a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
126235a85ac6SBen Widawsky 
126335a85ac6SBen Widawsky out:
126435a85ac6SBen Widawsky 	WARN_ON(dev_priv->l3_parity.which_slice);
12654cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
12662d1fe073SJoonas Lahtinen 	gen5_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv));
12674cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
126835a85ac6SBen Widawsky 
126991c8a326SChris Wilson 	mutex_unlock(&dev_priv->drm.struct_mutex);
127035a85ac6SBen Widawsky }
127135a85ac6SBen Widawsky 
1272261e40b8SVille Syrjälä static void ivybridge_parity_error_irq_handler(struct drm_i915_private *dev_priv,
1273261e40b8SVille Syrjälä 					       u32 iir)
1274e3689190SBen Widawsky {
1275261e40b8SVille Syrjälä 	if (!HAS_L3_DPF(dev_priv))
1276e3689190SBen Widawsky 		return;
1277e3689190SBen Widawsky 
1278d0ecd7e2SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1279261e40b8SVille Syrjälä 	gen5_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv));
1280d0ecd7e2SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
1281e3689190SBen Widawsky 
1282261e40b8SVille Syrjälä 	iir &= GT_PARITY_ERROR(dev_priv);
128335a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
128435a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 1;
128535a85ac6SBen Widawsky 
128635a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
128735a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 0;
128835a85ac6SBen Widawsky 
1289a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
1290e3689190SBen Widawsky }
1291e3689190SBen Widawsky 
1292261e40b8SVille Syrjälä static void ilk_gt_irq_handler(struct drm_i915_private *dev_priv,
1293f1af8fc1SPaulo Zanoni 			       u32 gt_iir)
1294f1af8fc1SPaulo Zanoni {
1295f8973c21SChris Wilson 	if (gt_iir & GT_RENDER_USER_INTERRUPT)
12963b3f1650SAkash Goel 		notify_ring(dev_priv->engine[RCS]);
1297f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
12983b3f1650SAkash Goel 		notify_ring(dev_priv->engine[VCS]);
1299f1af8fc1SPaulo Zanoni }
1300f1af8fc1SPaulo Zanoni 
1301261e40b8SVille Syrjälä static void snb_gt_irq_handler(struct drm_i915_private *dev_priv,
1302e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1303e7b4c6b1SDaniel Vetter {
1304f8973c21SChris Wilson 	if (gt_iir & GT_RENDER_USER_INTERRUPT)
13053b3f1650SAkash Goel 		notify_ring(dev_priv->engine[RCS]);
1306cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
13073b3f1650SAkash Goel 		notify_ring(dev_priv->engine[VCS]);
1308cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
13093b3f1650SAkash Goel 		notify_ring(dev_priv->engine[BCS]);
1310e7b4c6b1SDaniel Vetter 
1311cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1312cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
1313aaecdf61SDaniel Vetter 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT))
1314aaecdf61SDaniel Vetter 		DRM_DEBUG("Command parser error, gt_iir 0x%08x\n", gt_iir);
1315e3689190SBen Widawsky 
1316261e40b8SVille Syrjälä 	if (gt_iir & GT_PARITY_ERROR(dev_priv))
1317261e40b8SVille Syrjälä 		ivybridge_parity_error_irq_handler(dev_priv, gt_iir);
1318e7b4c6b1SDaniel Vetter }
1319e7b4c6b1SDaniel Vetter 
1320fbcc1a0cSNick Hoath static __always_inline void
13210bc40be8STvrtko Ursulin gen8_cs_irq_handler(struct intel_engine_cs *engine, u32 iir, int test_shift)
1322fbcc1a0cSNick Hoath {
132331de7350SChris Wilson 	bool tasklet = false;
1324f747026cSChris Wilson 
1325f747026cSChris Wilson 	if (iir & (GT_CONTEXT_SWITCH_INTERRUPT << test_shift)) {
1326f747026cSChris Wilson 		set_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
132731de7350SChris Wilson 		tasklet = true;
1328f747026cSChris Wilson 	}
132931de7350SChris Wilson 
133031de7350SChris Wilson 	if (iir & (GT_RENDER_USER_INTERRUPT << test_shift)) {
133131de7350SChris Wilson 		notify_ring(engine);
133231de7350SChris Wilson 		tasklet |= i915.enable_guc_submission;
133331de7350SChris Wilson 	}
133431de7350SChris Wilson 
133531de7350SChris Wilson 	if (tasklet)
133631de7350SChris Wilson 		tasklet_hi_schedule(&engine->irq_tasklet);
1337fbcc1a0cSNick Hoath }
1338fbcc1a0cSNick Hoath 
1339e30e251aSVille Syrjälä static irqreturn_t gen8_gt_irq_ack(struct drm_i915_private *dev_priv,
1340e30e251aSVille Syrjälä 				   u32 master_ctl,
1341e30e251aSVille Syrjälä 				   u32 gt_iir[4])
1342abd58f01SBen Widawsky {
1343abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
1344abd58f01SBen Widawsky 
1345abd58f01SBen Widawsky 	if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
1346e30e251aSVille Syrjälä 		gt_iir[0] = I915_READ_FW(GEN8_GT_IIR(0));
1347e30e251aSVille Syrjälä 		if (gt_iir[0]) {
1348e30e251aSVille Syrjälä 			I915_WRITE_FW(GEN8_GT_IIR(0), gt_iir[0]);
1349abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1350abd58f01SBen Widawsky 		} else
1351abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT0)!\n");
1352abd58f01SBen Widawsky 	}
1353abd58f01SBen Widawsky 
135485f9b5f9SZhao Yakui 	if (master_ctl & (GEN8_GT_VCS1_IRQ | GEN8_GT_VCS2_IRQ)) {
1355e30e251aSVille Syrjälä 		gt_iir[1] = I915_READ_FW(GEN8_GT_IIR(1));
1356e30e251aSVille Syrjälä 		if (gt_iir[1]) {
1357e30e251aSVille Syrjälä 			I915_WRITE_FW(GEN8_GT_IIR(1), gt_iir[1]);
1358abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1359abd58f01SBen Widawsky 		} else
1360abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT1)!\n");
1361abd58f01SBen Widawsky 	}
1362abd58f01SBen Widawsky 
136374cdb337SChris Wilson 	if (master_ctl & GEN8_GT_VECS_IRQ) {
1364e30e251aSVille Syrjälä 		gt_iir[3] = I915_READ_FW(GEN8_GT_IIR(3));
1365e30e251aSVille Syrjälä 		if (gt_iir[3]) {
1366e30e251aSVille Syrjälä 			I915_WRITE_FW(GEN8_GT_IIR(3), gt_iir[3]);
136774cdb337SChris Wilson 			ret = IRQ_HANDLED;
136874cdb337SChris Wilson 		} else
136974cdb337SChris Wilson 			DRM_ERROR("The master control interrupt lied (GT3)!\n");
137074cdb337SChris Wilson 	}
137174cdb337SChris Wilson 
137226705e20SSagar Arun Kamble 	if (master_ctl & (GEN8_GT_PM_IRQ | GEN8_GT_GUC_IRQ)) {
1373e30e251aSVille Syrjälä 		gt_iir[2] = I915_READ_FW(GEN8_GT_IIR(2));
137426705e20SSagar Arun Kamble 		if (gt_iir[2] & (dev_priv->pm_rps_events |
137526705e20SSagar Arun Kamble 				 dev_priv->pm_guc_events)) {
1376cb0d205eSChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(2),
137726705e20SSagar Arun Kamble 				      gt_iir[2] & (dev_priv->pm_rps_events |
137826705e20SSagar Arun Kamble 						   dev_priv->pm_guc_events));
137938cc46d7SOscar Mateo 			ret = IRQ_HANDLED;
13800961021aSBen Widawsky 		} else
13810961021aSBen Widawsky 			DRM_ERROR("The master control interrupt lied (PM)!\n");
13820961021aSBen Widawsky 	}
13830961021aSBen Widawsky 
1384abd58f01SBen Widawsky 	return ret;
1385abd58f01SBen Widawsky }
1386abd58f01SBen Widawsky 
1387e30e251aSVille Syrjälä static void gen8_gt_irq_handler(struct drm_i915_private *dev_priv,
1388e30e251aSVille Syrjälä 				u32 gt_iir[4])
1389e30e251aSVille Syrjälä {
1390e30e251aSVille Syrjälä 	if (gt_iir[0]) {
13913b3f1650SAkash Goel 		gen8_cs_irq_handler(dev_priv->engine[RCS],
1392e30e251aSVille Syrjälä 				    gt_iir[0], GEN8_RCS_IRQ_SHIFT);
13933b3f1650SAkash Goel 		gen8_cs_irq_handler(dev_priv->engine[BCS],
1394e30e251aSVille Syrjälä 				    gt_iir[0], GEN8_BCS_IRQ_SHIFT);
1395e30e251aSVille Syrjälä 	}
1396e30e251aSVille Syrjälä 
1397e30e251aSVille Syrjälä 	if (gt_iir[1]) {
13983b3f1650SAkash Goel 		gen8_cs_irq_handler(dev_priv->engine[VCS],
1399e30e251aSVille Syrjälä 				    gt_iir[1], GEN8_VCS1_IRQ_SHIFT);
14003b3f1650SAkash Goel 		gen8_cs_irq_handler(dev_priv->engine[VCS2],
1401e30e251aSVille Syrjälä 				    gt_iir[1], GEN8_VCS2_IRQ_SHIFT);
1402e30e251aSVille Syrjälä 	}
1403e30e251aSVille Syrjälä 
1404e30e251aSVille Syrjälä 	if (gt_iir[3])
14053b3f1650SAkash Goel 		gen8_cs_irq_handler(dev_priv->engine[VECS],
1406e30e251aSVille Syrjälä 				    gt_iir[3], GEN8_VECS_IRQ_SHIFT);
1407e30e251aSVille Syrjälä 
1408e30e251aSVille Syrjälä 	if (gt_iir[2] & dev_priv->pm_rps_events)
1409e30e251aSVille Syrjälä 		gen6_rps_irq_handler(dev_priv, gt_iir[2]);
141026705e20SSagar Arun Kamble 
141126705e20SSagar Arun Kamble 	if (gt_iir[2] & dev_priv->pm_guc_events)
141226705e20SSagar Arun Kamble 		gen9_guc_irq_handler(dev_priv, gt_iir[2]);
1413e30e251aSVille Syrjälä }
1414e30e251aSVille Syrjälä 
141563c88d22SImre Deak static bool bxt_port_hotplug_long_detect(enum port port, u32 val)
141663c88d22SImre Deak {
141763c88d22SImre Deak 	switch (port) {
141863c88d22SImre Deak 	case PORT_A:
1419195baa06SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
142063c88d22SImre Deak 	case PORT_B:
142163c88d22SImre Deak 		return val & PORTB_HOTPLUG_LONG_DETECT;
142263c88d22SImre Deak 	case PORT_C:
142363c88d22SImre Deak 		return val & PORTC_HOTPLUG_LONG_DETECT;
142463c88d22SImre Deak 	default:
142563c88d22SImre Deak 		return false;
142663c88d22SImre Deak 	}
142763c88d22SImre Deak }
142863c88d22SImre Deak 
14296dbf30ceSVille Syrjälä static bool spt_port_hotplug2_long_detect(enum port port, u32 val)
14306dbf30ceSVille Syrjälä {
14316dbf30ceSVille Syrjälä 	switch (port) {
14326dbf30ceSVille Syrjälä 	case PORT_E:
14336dbf30ceSVille Syrjälä 		return val & PORTE_HOTPLUG_LONG_DETECT;
14346dbf30ceSVille Syrjälä 	default:
14356dbf30ceSVille Syrjälä 		return false;
14366dbf30ceSVille Syrjälä 	}
14376dbf30ceSVille Syrjälä }
14386dbf30ceSVille Syrjälä 
143974c0b395SVille Syrjälä static bool spt_port_hotplug_long_detect(enum port port, u32 val)
144074c0b395SVille Syrjälä {
144174c0b395SVille Syrjälä 	switch (port) {
144274c0b395SVille Syrjälä 	case PORT_A:
144374c0b395SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
144474c0b395SVille Syrjälä 	case PORT_B:
144574c0b395SVille Syrjälä 		return val & PORTB_HOTPLUG_LONG_DETECT;
144674c0b395SVille Syrjälä 	case PORT_C:
144774c0b395SVille Syrjälä 		return val & PORTC_HOTPLUG_LONG_DETECT;
144874c0b395SVille Syrjälä 	case PORT_D:
144974c0b395SVille Syrjälä 		return val & PORTD_HOTPLUG_LONG_DETECT;
145074c0b395SVille Syrjälä 	default:
145174c0b395SVille Syrjälä 		return false;
145274c0b395SVille Syrjälä 	}
145374c0b395SVille Syrjälä }
145474c0b395SVille Syrjälä 
1455e4ce95aaSVille Syrjälä static bool ilk_port_hotplug_long_detect(enum port port, u32 val)
1456e4ce95aaSVille Syrjälä {
1457e4ce95aaSVille Syrjälä 	switch (port) {
1458e4ce95aaSVille Syrjälä 	case PORT_A:
1459e4ce95aaSVille Syrjälä 		return val & DIGITAL_PORTA_HOTPLUG_LONG_DETECT;
1460e4ce95aaSVille Syrjälä 	default:
1461e4ce95aaSVille Syrjälä 		return false;
1462e4ce95aaSVille Syrjälä 	}
1463e4ce95aaSVille Syrjälä }
1464e4ce95aaSVille Syrjälä 
1465676574dfSJani Nikula static bool pch_port_hotplug_long_detect(enum port port, u32 val)
146613cf5504SDave Airlie {
146713cf5504SDave Airlie 	switch (port) {
146813cf5504SDave Airlie 	case PORT_B:
1469676574dfSJani Nikula 		return val & PORTB_HOTPLUG_LONG_DETECT;
147013cf5504SDave Airlie 	case PORT_C:
1471676574dfSJani Nikula 		return val & PORTC_HOTPLUG_LONG_DETECT;
147213cf5504SDave Airlie 	case PORT_D:
1473676574dfSJani Nikula 		return val & PORTD_HOTPLUG_LONG_DETECT;
1474676574dfSJani Nikula 	default:
1475676574dfSJani Nikula 		return false;
147613cf5504SDave Airlie 	}
147713cf5504SDave Airlie }
147813cf5504SDave Airlie 
1479676574dfSJani Nikula static bool i9xx_port_hotplug_long_detect(enum port port, u32 val)
148013cf5504SDave Airlie {
148113cf5504SDave Airlie 	switch (port) {
148213cf5504SDave Airlie 	case PORT_B:
1483676574dfSJani Nikula 		return val & PORTB_HOTPLUG_INT_LONG_PULSE;
148413cf5504SDave Airlie 	case PORT_C:
1485676574dfSJani Nikula 		return val & PORTC_HOTPLUG_INT_LONG_PULSE;
148613cf5504SDave Airlie 	case PORT_D:
1487676574dfSJani Nikula 		return val & PORTD_HOTPLUG_INT_LONG_PULSE;
1488676574dfSJani Nikula 	default:
1489676574dfSJani Nikula 		return false;
149013cf5504SDave Airlie 	}
149113cf5504SDave Airlie }
149213cf5504SDave Airlie 
149342db67d6SVille Syrjälä /*
149442db67d6SVille Syrjälä  * Get a bit mask of pins that have triggered, and which ones may be long.
149542db67d6SVille Syrjälä  * This can be called multiple times with the same masks to accumulate
149642db67d6SVille Syrjälä  * hotplug detection results from several registers.
149742db67d6SVille Syrjälä  *
149842db67d6SVille Syrjälä  * Note that the caller is expected to zero out the masks initially.
149942db67d6SVille Syrjälä  */
1500fd63e2a9SImre Deak static void intel_get_hpd_pins(u32 *pin_mask, u32 *long_mask,
15018c841e57SJani Nikula 			     u32 hotplug_trigger, u32 dig_hotplug_reg,
1502fd63e2a9SImre Deak 			     const u32 hpd[HPD_NUM_PINS],
1503fd63e2a9SImre Deak 			     bool long_pulse_detect(enum port port, u32 val))
1504676574dfSJani Nikula {
15058c841e57SJani Nikula 	enum port port;
1506676574dfSJani Nikula 	int i;
1507676574dfSJani Nikula 
1508676574dfSJani Nikula 	for_each_hpd_pin(i) {
15098c841e57SJani Nikula 		if ((hpd[i] & hotplug_trigger) == 0)
15108c841e57SJani Nikula 			continue;
15118c841e57SJani Nikula 
1512676574dfSJani Nikula 		*pin_mask |= BIT(i);
1513676574dfSJani Nikula 
1514cc24fcdcSImre Deak 		if (!intel_hpd_pin_to_port(i, &port))
1515cc24fcdcSImre Deak 			continue;
1516cc24fcdcSImre Deak 
1517fd63e2a9SImre Deak 		if (long_pulse_detect(port, dig_hotplug_reg))
1518676574dfSJani Nikula 			*long_mask |= BIT(i);
1519676574dfSJani Nikula 	}
1520676574dfSJani Nikula 
1521676574dfSJani Nikula 	DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x, dig 0x%08x, pins 0x%08x\n",
1522676574dfSJani Nikula 			 hotplug_trigger, dig_hotplug_reg, *pin_mask);
1523676574dfSJani Nikula 
1524676574dfSJani Nikula }
1525676574dfSJani Nikula 
152691d14251STvrtko Ursulin static void gmbus_irq_handler(struct drm_i915_private *dev_priv)
1527515ac2bbSDaniel Vetter {
152828c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1529515ac2bbSDaniel Vetter }
1530515ac2bbSDaniel Vetter 
153191d14251STvrtko Ursulin static void dp_aux_irq_handler(struct drm_i915_private *dev_priv)
1532ce99c256SDaniel Vetter {
15339ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1534ce99c256SDaniel Vetter }
1535ce99c256SDaniel Vetter 
15368bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
153791d14251STvrtko Ursulin static void display_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
153891d14251STvrtko Ursulin 					 enum pipe pipe,
1539eba94eb9SDaniel Vetter 					 uint32_t crc0, uint32_t crc1,
1540eba94eb9SDaniel Vetter 					 uint32_t crc2, uint32_t crc3,
15418bc5e955SDaniel Vetter 					 uint32_t crc4)
15428bf1e9f1SShuang He {
15438bf1e9f1SShuang He 	struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
15448bf1e9f1SShuang He 	struct intel_pipe_crc_entry *entry;
15458c6b709dSTomeu Vizoso 	struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
15468c6b709dSTomeu Vizoso 	struct drm_driver *driver = dev_priv->drm.driver;
15478c6b709dSTomeu Vizoso 	uint32_t crcs[5];
1548ac2300d4SDamien Lespiau 	int head, tail;
1549b2c88f5bSDamien Lespiau 
1550d538bbdfSDamien Lespiau 	spin_lock(&pipe_crc->lock);
15518c6b709dSTomeu Vizoso 	if (pipe_crc->source) {
15520c912c79SDamien Lespiau 		if (!pipe_crc->entries) {
1553d538bbdfSDamien Lespiau 			spin_unlock(&pipe_crc->lock);
155434273620SDaniel Vetter 			DRM_DEBUG_KMS("spurious interrupt\n");
15550c912c79SDamien Lespiau 			return;
15560c912c79SDamien Lespiau 		}
15570c912c79SDamien Lespiau 
1558d538bbdfSDamien Lespiau 		head = pipe_crc->head;
1559d538bbdfSDamien Lespiau 		tail = pipe_crc->tail;
1560b2c88f5bSDamien Lespiau 
1561b2c88f5bSDamien Lespiau 		if (CIRC_SPACE(head, tail, INTEL_PIPE_CRC_ENTRIES_NR) < 1) {
1562d538bbdfSDamien Lespiau 			spin_unlock(&pipe_crc->lock);
1563b2c88f5bSDamien Lespiau 			DRM_ERROR("CRC buffer overflowing\n");
1564b2c88f5bSDamien Lespiau 			return;
1565b2c88f5bSDamien Lespiau 		}
1566b2c88f5bSDamien Lespiau 
1567b2c88f5bSDamien Lespiau 		entry = &pipe_crc->entries[head];
15688bf1e9f1SShuang He 
15698c6b709dSTomeu Vizoso 		entry->frame = driver->get_vblank_counter(&dev_priv->drm, pipe);
1570eba94eb9SDaniel Vetter 		entry->crc[0] = crc0;
1571eba94eb9SDaniel Vetter 		entry->crc[1] = crc1;
1572eba94eb9SDaniel Vetter 		entry->crc[2] = crc2;
1573eba94eb9SDaniel Vetter 		entry->crc[3] = crc3;
1574eba94eb9SDaniel Vetter 		entry->crc[4] = crc4;
1575b2c88f5bSDamien Lespiau 
1576b2c88f5bSDamien Lespiau 		head = (head + 1) & (INTEL_PIPE_CRC_ENTRIES_NR - 1);
1577d538bbdfSDamien Lespiau 		pipe_crc->head = head;
1578d538bbdfSDamien Lespiau 
1579d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
158007144428SDamien Lespiau 
158107144428SDamien Lespiau 		wake_up_interruptible(&pipe_crc->wq);
15828c6b709dSTomeu Vizoso 	} else {
15838c6b709dSTomeu Vizoso 		/*
15848c6b709dSTomeu Vizoso 		 * For some not yet identified reason, the first CRC is
15858c6b709dSTomeu Vizoso 		 * bonkers. So let's just wait for the next vblank and read
15868c6b709dSTomeu Vizoso 		 * out the buggy result.
15878c6b709dSTomeu Vizoso 		 *
15888c6b709dSTomeu Vizoso 		 * On CHV sometimes the second CRC is bonkers as well, so
15898c6b709dSTomeu Vizoso 		 * don't trust that one either.
15908c6b709dSTomeu Vizoso 		 */
15918c6b709dSTomeu Vizoso 		if (pipe_crc->skipped == 0 ||
15928c6b709dSTomeu Vizoso 		    (IS_CHERRYVIEW(dev_priv) && pipe_crc->skipped == 1)) {
15938c6b709dSTomeu Vizoso 			pipe_crc->skipped++;
15948c6b709dSTomeu Vizoso 			spin_unlock(&pipe_crc->lock);
15958c6b709dSTomeu Vizoso 			return;
15968c6b709dSTomeu Vizoso 		}
15978c6b709dSTomeu Vizoso 		spin_unlock(&pipe_crc->lock);
15988c6b709dSTomeu Vizoso 		crcs[0] = crc0;
15998c6b709dSTomeu Vizoso 		crcs[1] = crc1;
16008c6b709dSTomeu Vizoso 		crcs[2] = crc2;
16018c6b709dSTomeu Vizoso 		crcs[3] = crc3;
16028c6b709dSTomeu Vizoso 		crcs[4] = crc4;
1603246ee524STomeu Vizoso 		drm_crtc_add_crc_entry(&crtc->base, true,
1604*ca814b25SDaniel Vetter 				       drm_crtc_accurate_vblank_count(&crtc->base),
1605246ee524STomeu Vizoso 				       crcs);
16068c6b709dSTomeu Vizoso 	}
16078bf1e9f1SShuang He }
1608277de95eSDaniel Vetter #else
1609277de95eSDaniel Vetter static inline void
161091d14251STvrtko Ursulin display_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
161191d14251STvrtko Ursulin 			     enum pipe pipe,
1612277de95eSDaniel Vetter 			     uint32_t crc0, uint32_t crc1,
1613277de95eSDaniel Vetter 			     uint32_t crc2, uint32_t crc3,
1614277de95eSDaniel Vetter 			     uint32_t crc4) {}
1615277de95eSDaniel Vetter #endif
1616eba94eb9SDaniel Vetter 
1617277de95eSDaniel Vetter 
161891d14251STvrtko Ursulin static void hsw_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
161991d14251STvrtko Ursulin 				     enum pipe pipe)
16205a69b89fSDaniel Vetter {
162191d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
16225a69b89fSDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
16235a69b89fSDaniel Vetter 				     0, 0, 0, 0);
16245a69b89fSDaniel Vetter }
16255a69b89fSDaniel Vetter 
162691d14251STvrtko Ursulin static void ivb_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
162791d14251STvrtko Ursulin 				     enum pipe pipe)
1628eba94eb9SDaniel Vetter {
162991d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
1630eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1631eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1632eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1633eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
16348bc5e955SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1635eba94eb9SDaniel Vetter }
16365b3a856bSDaniel Vetter 
163791d14251STvrtko Ursulin static void i9xx_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
163891d14251STvrtko Ursulin 				      enum pipe pipe)
16395b3a856bSDaniel Vetter {
16400b5c5ed0SDaniel Vetter 	uint32_t res1, res2;
16410b5c5ed0SDaniel Vetter 
164291d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 3)
16430b5c5ed0SDaniel Vetter 		res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
16440b5c5ed0SDaniel Vetter 	else
16450b5c5ed0SDaniel Vetter 		res1 = 0;
16460b5c5ed0SDaniel Vetter 
164791d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
16480b5c5ed0SDaniel Vetter 		res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
16490b5c5ed0SDaniel Vetter 	else
16500b5c5ed0SDaniel Vetter 		res2 = 0;
16515b3a856bSDaniel Vetter 
165291d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
16530b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_RED(pipe)),
16540b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_GREEN(pipe)),
16550b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_BLUE(pipe)),
16560b5c5ed0SDaniel Vetter 				     res1, res2);
16575b3a856bSDaniel Vetter }
16588bf1e9f1SShuang He 
16591403c0d4SPaulo Zanoni /* The RPS events need forcewake, so we add them to a work queue and mask their
16601403c0d4SPaulo Zanoni  * IMR bits until the work is done. Other interrupts can be processed without
16611403c0d4SPaulo Zanoni  * the work queue. */
16621403c0d4SPaulo Zanoni static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1663baf02a1fSBen Widawsky {
1664a6706b45SDeepak S 	if (pm_iir & dev_priv->pm_rps_events) {
166559cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
1666f4e9af4fSAkash Goel 		gen6_mask_pm_irq(dev_priv, pm_iir & dev_priv->pm_rps_events);
1667d4d70aa5SImre Deak 		if (dev_priv->rps.interrupts_enabled) {
1668d4d70aa5SImre Deak 			dev_priv->rps.pm_iir |= pm_iir & dev_priv->pm_rps_events;
1669c33d247dSChris Wilson 			schedule_work(&dev_priv->rps.work);
167041a05a3aSDaniel Vetter 		}
1671d4d70aa5SImre Deak 		spin_unlock(&dev_priv->irq_lock);
1672d4d70aa5SImre Deak 	}
1673baf02a1fSBen Widawsky 
1674c9a9a268SImre Deak 	if (INTEL_INFO(dev_priv)->gen >= 8)
1675c9a9a268SImre Deak 		return;
1676c9a9a268SImre Deak 
16772d1fe073SJoonas Lahtinen 	if (HAS_VEBOX(dev_priv)) {
167812638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
16793b3f1650SAkash Goel 			notify_ring(dev_priv->engine[VECS]);
168012638c57SBen Widawsky 
1681aaecdf61SDaniel Vetter 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT)
1682aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, pm_iir 0x%08x\n", pm_iir);
168312638c57SBen Widawsky 	}
16841403c0d4SPaulo Zanoni }
1685baf02a1fSBen Widawsky 
168626705e20SSagar Arun Kamble static void gen9_guc_irq_handler(struct drm_i915_private *dev_priv, u32 gt_iir)
168726705e20SSagar Arun Kamble {
168826705e20SSagar Arun Kamble 	if (gt_iir & GEN9_GUC_TO_HOST_INT_EVENT) {
16894100b2abSSagar Arun Kamble 		/* Sample the log buffer flush related bits & clear them out now
16904100b2abSSagar Arun Kamble 		 * itself from the message identity register to minimize the
16914100b2abSSagar Arun Kamble 		 * probability of losing a flush interrupt, when there are back
16924100b2abSSagar Arun Kamble 		 * to back flush interrupts.
16934100b2abSSagar Arun Kamble 		 * There can be a new flush interrupt, for different log buffer
16944100b2abSSagar Arun Kamble 		 * type (like for ISR), whilst Host is handling one (for DPC).
16954100b2abSSagar Arun Kamble 		 * Since same bit is used in message register for ISR & DPC, it
16964100b2abSSagar Arun Kamble 		 * could happen that GuC sets the bit for 2nd interrupt but Host
16974100b2abSSagar Arun Kamble 		 * clears out the bit on handling the 1st interrupt.
16984100b2abSSagar Arun Kamble 		 */
16994100b2abSSagar Arun Kamble 		u32 msg, flush;
17004100b2abSSagar Arun Kamble 
17014100b2abSSagar Arun Kamble 		msg = I915_READ(SOFT_SCRATCH(15));
1702a80bc45fSArkadiusz Hiler 		flush = msg & (INTEL_GUC_RECV_MSG_CRASH_DUMP_POSTED |
1703a80bc45fSArkadiusz Hiler 			       INTEL_GUC_RECV_MSG_FLUSH_LOG_BUFFER);
17044100b2abSSagar Arun Kamble 		if (flush) {
17054100b2abSSagar Arun Kamble 			/* Clear the message bits that are handled */
17064100b2abSSagar Arun Kamble 			I915_WRITE(SOFT_SCRATCH(15), msg & ~flush);
17074100b2abSSagar Arun Kamble 
17084100b2abSSagar Arun Kamble 			/* Handle flush interrupt in bottom half */
1709e7465473SOscar Mateo 			queue_work(dev_priv->guc.log.runtime.flush_wq,
1710e7465473SOscar Mateo 				   &dev_priv->guc.log.runtime.flush_work);
17115aa1ee4bSAkash Goel 
17125aa1ee4bSAkash Goel 			dev_priv->guc.log.flush_interrupt_count++;
17134100b2abSSagar Arun Kamble 		} else {
17144100b2abSSagar Arun Kamble 			/* Not clearing of unhandled event bits won't result in
17154100b2abSSagar Arun Kamble 			 * re-triggering of the interrupt.
17164100b2abSSagar Arun Kamble 			 */
17174100b2abSSagar Arun Kamble 		}
171826705e20SSagar Arun Kamble 	}
171926705e20SSagar Arun Kamble }
172026705e20SSagar Arun Kamble 
17215a21b665SDaniel Vetter static bool intel_pipe_handle_vblank(struct drm_i915_private *dev_priv,
172291d14251STvrtko Ursulin 				     enum pipe pipe)
17238d7849dbSVille Syrjälä {
17245a21b665SDaniel Vetter 	bool ret;
17255a21b665SDaniel Vetter 
172691c8a326SChris Wilson 	ret = drm_handle_vblank(&dev_priv->drm, pipe);
17275a21b665SDaniel Vetter 	if (ret)
172851cbaf01SMaarten Lankhorst 		intel_finish_page_flip_mmio(dev_priv, pipe);
17295a21b665SDaniel Vetter 
17305a21b665SDaniel Vetter 	return ret;
17318d7849dbSVille Syrjälä }
17328d7849dbSVille Syrjälä 
173391d14251STvrtko Ursulin static void valleyview_pipestat_irq_ack(struct drm_i915_private *dev_priv,
173491d14251STvrtko Ursulin 					u32 iir, u32 pipe_stats[I915_MAX_PIPES])
17357e231dbeSJesse Barnes {
17367e231dbeSJesse Barnes 	int pipe;
17377e231dbeSJesse Barnes 
173858ead0d7SImre Deak 	spin_lock(&dev_priv->irq_lock);
17391ca993d2SVille Syrjälä 
17401ca993d2SVille Syrjälä 	if (!dev_priv->display_irqs_enabled) {
17411ca993d2SVille Syrjälä 		spin_unlock(&dev_priv->irq_lock);
17421ca993d2SVille Syrjälä 		return;
17431ca993d2SVille Syrjälä 	}
17441ca993d2SVille Syrjälä 
1745055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1746f0f59a00SVille Syrjälä 		i915_reg_t reg;
1747bbb5eebfSDaniel Vetter 		u32 mask, iir_bit = 0;
174891d181ddSImre Deak 
1749bbb5eebfSDaniel Vetter 		/*
1750bbb5eebfSDaniel Vetter 		 * PIPESTAT bits get signalled even when the interrupt is
1751bbb5eebfSDaniel Vetter 		 * disabled with the mask bits, and some of the status bits do
1752bbb5eebfSDaniel Vetter 		 * not generate interrupts at all (like the underrun bit). Hence
1753bbb5eebfSDaniel Vetter 		 * we need to be careful that we only handle what we want to
1754bbb5eebfSDaniel Vetter 		 * handle.
1755bbb5eebfSDaniel Vetter 		 */
17560f239f4cSDaniel Vetter 
17570f239f4cSDaniel Vetter 		/* fifo underruns are filterered in the underrun handler. */
17580f239f4cSDaniel Vetter 		mask = PIPE_FIFO_UNDERRUN_STATUS;
1759bbb5eebfSDaniel Vetter 
1760bbb5eebfSDaniel Vetter 		switch (pipe) {
1761bbb5eebfSDaniel Vetter 		case PIPE_A:
1762bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
1763bbb5eebfSDaniel Vetter 			break;
1764bbb5eebfSDaniel Vetter 		case PIPE_B:
1765bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
1766bbb5eebfSDaniel Vetter 			break;
17673278f67fSVille Syrjälä 		case PIPE_C:
17683278f67fSVille Syrjälä 			iir_bit = I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
17693278f67fSVille Syrjälä 			break;
1770bbb5eebfSDaniel Vetter 		}
1771bbb5eebfSDaniel Vetter 		if (iir & iir_bit)
1772bbb5eebfSDaniel Vetter 			mask |= dev_priv->pipestat_irq_mask[pipe];
1773bbb5eebfSDaniel Vetter 
1774bbb5eebfSDaniel Vetter 		if (!mask)
177591d181ddSImre Deak 			continue;
177691d181ddSImre Deak 
177791d181ddSImre Deak 		reg = PIPESTAT(pipe);
1778bbb5eebfSDaniel Vetter 		mask |= PIPESTAT_INT_ENABLE_MASK;
1779bbb5eebfSDaniel Vetter 		pipe_stats[pipe] = I915_READ(reg) & mask;
17807e231dbeSJesse Barnes 
17817e231dbeSJesse Barnes 		/*
17827e231dbeSJesse Barnes 		 * Clear the PIPE*STAT regs before the IIR
17837e231dbeSJesse Barnes 		 */
178491d181ddSImre Deak 		if (pipe_stats[pipe] & (PIPE_FIFO_UNDERRUN_STATUS |
178591d181ddSImre Deak 					PIPESTAT_INT_STATUS_MASK))
17867e231dbeSJesse Barnes 			I915_WRITE(reg, pipe_stats[pipe]);
17877e231dbeSJesse Barnes 	}
178858ead0d7SImre Deak 	spin_unlock(&dev_priv->irq_lock);
17892ecb8ca4SVille Syrjälä }
17902ecb8ca4SVille Syrjälä 
179191d14251STvrtko Ursulin static void valleyview_pipestat_irq_handler(struct drm_i915_private *dev_priv,
17922ecb8ca4SVille Syrjälä 					    u32 pipe_stats[I915_MAX_PIPES])
17932ecb8ca4SVille Syrjälä {
17942ecb8ca4SVille Syrjälä 	enum pipe pipe;
17957e231dbeSJesse Barnes 
1796055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
17975a21b665SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
17985a21b665SDaniel Vetter 		    intel_pipe_handle_vblank(dev_priv, pipe))
17995a21b665SDaniel Vetter 			intel_check_page_flip(dev_priv, pipe);
180031acc7f5SJesse Barnes 
18015251f04eSMaarten Lankhorst 		if (pipe_stats[pipe] & PLANE_FLIP_DONE_INT_STATUS_VLV)
180251cbaf01SMaarten Lankhorst 			intel_finish_page_flip_cs(dev_priv, pipe);
18034356d586SDaniel Vetter 
18044356d586SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
180591d14251STvrtko Ursulin 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
18062d9d2b0bSVille Syrjälä 
18071f7247c0SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
18081f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
180931acc7f5SJesse Barnes 	}
181031acc7f5SJesse Barnes 
1811c1874ed7SImre Deak 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
181291d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
1813c1874ed7SImre Deak }
1814c1874ed7SImre Deak 
18151ae3c34cSVille Syrjälä static u32 i9xx_hpd_irq_ack(struct drm_i915_private *dev_priv)
181616c6c56bSVille Syrjälä {
181716c6c56bSVille Syrjälä 	u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
181816c6c56bSVille Syrjälä 
18191ae3c34cSVille Syrjälä 	if (hotplug_status)
18203ff60f89SOscar Mateo 		I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
18211ae3c34cSVille Syrjälä 
18221ae3c34cSVille Syrjälä 	return hotplug_status;
18231ae3c34cSVille Syrjälä }
18241ae3c34cSVille Syrjälä 
182591d14251STvrtko Ursulin static void i9xx_hpd_irq_handler(struct drm_i915_private *dev_priv,
18261ae3c34cSVille Syrjälä 				 u32 hotplug_status)
18271ae3c34cSVille Syrjälä {
18281ae3c34cSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
18293ff60f89SOscar Mateo 
183091d14251STvrtko Ursulin 	if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
183191d14251STvrtko Ursulin 	    IS_CHERRYVIEW(dev_priv)) {
183216c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
183316c6c56bSVille Syrjälä 
183458f2cf24SVille Syrjälä 		if (hotplug_trigger) {
1835fd63e2a9SImre Deak 			intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
1836fd63e2a9SImre Deak 					   hotplug_trigger, hpd_status_g4x,
1837fd63e2a9SImre Deak 					   i9xx_port_hotplug_long_detect);
183858f2cf24SVille Syrjälä 
183991d14251STvrtko Ursulin 			intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
184058f2cf24SVille Syrjälä 		}
1841369712e8SJani Nikula 
1842369712e8SJani Nikula 		if (hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
184391d14251STvrtko Ursulin 			dp_aux_irq_handler(dev_priv);
184416c6c56bSVille Syrjälä 	} else {
184516c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
184616c6c56bSVille Syrjälä 
184758f2cf24SVille Syrjälä 		if (hotplug_trigger) {
1848fd63e2a9SImre Deak 			intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
18494e3d1e26SVille Syrjälä 					   hotplug_trigger, hpd_status_i915,
1850fd63e2a9SImre Deak 					   i9xx_port_hotplug_long_detect);
185191d14251STvrtko Ursulin 			intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
185216c6c56bSVille Syrjälä 		}
18533ff60f89SOscar Mateo 	}
185458f2cf24SVille Syrjälä }
185516c6c56bSVille Syrjälä 
1856c1874ed7SImre Deak static irqreturn_t valleyview_irq_handler(int irq, void *arg)
1857c1874ed7SImre Deak {
185845a83f84SDaniel Vetter 	struct drm_device *dev = arg;
1859fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
1860c1874ed7SImre Deak 	irqreturn_t ret = IRQ_NONE;
1861c1874ed7SImre Deak 
18622dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
18632dd2a883SImre Deak 		return IRQ_NONE;
18642dd2a883SImre Deak 
18651f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
18661f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
18671f814dacSImre Deak 
18681e1cace9SVille Syrjälä 	do {
18696e814800SVille Syrjälä 		u32 iir, gt_iir, pm_iir;
18702ecb8ca4SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
18711ae3c34cSVille Syrjälä 		u32 hotplug_status = 0;
1872a5e485a9SVille Syrjälä 		u32 ier = 0;
18733ff60f89SOscar Mateo 
1874c1874ed7SImre Deak 		gt_iir = I915_READ(GTIIR);
1875c1874ed7SImre Deak 		pm_iir = I915_READ(GEN6_PMIIR);
18763ff60f89SOscar Mateo 		iir = I915_READ(VLV_IIR);
1877c1874ed7SImre Deak 
1878c1874ed7SImre Deak 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
18791e1cace9SVille Syrjälä 			break;
1880c1874ed7SImre Deak 
1881c1874ed7SImre Deak 		ret = IRQ_HANDLED;
1882c1874ed7SImre Deak 
1883a5e485a9SVille Syrjälä 		/*
1884a5e485a9SVille Syrjälä 		 * Theory on interrupt generation, based on empirical evidence:
1885a5e485a9SVille Syrjälä 		 *
1886a5e485a9SVille Syrjälä 		 * x = ((VLV_IIR & VLV_IER) ||
1887a5e485a9SVille Syrjälä 		 *      (((GT_IIR & GT_IER) || (GEN6_PMIIR & GEN6_PMIER)) &&
1888a5e485a9SVille Syrjälä 		 *       (VLV_MASTER_IER & MASTER_INTERRUPT_ENABLE)));
1889a5e485a9SVille Syrjälä 		 *
1890a5e485a9SVille Syrjälä 		 * A CPU interrupt will only be raised when 'x' has a 0->1 edge.
1891a5e485a9SVille Syrjälä 		 * Hence we clear MASTER_INTERRUPT_ENABLE and VLV_IER to
1892a5e485a9SVille Syrjälä 		 * guarantee the CPU interrupt will be raised again even if we
1893a5e485a9SVille Syrjälä 		 * don't end up clearing all the VLV_IIR, GT_IIR, GEN6_PMIIR
1894a5e485a9SVille Syrjälä 		 * bits this time around.
1895a5e485a9SVille Syrjälä 		 */
18964a0a0202SVille Syrjälä 		I915_WRITE(VLV_MASTER_IER, 0);
1897a5e485a9SVille Syrjälä 		ier = I915_READ(VLV_IER);
1898a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, 0);
18994a0a0202SVille Syrjälä 
19004a0a0202SVille Syrjälä 		if (gt_iir)
19014a0a0202SVille Syrjälä 			I915_WRITE(GTIIR, gt_iir);
19024a0a0202SVille Syrjälä 		if (pm_iir)
19034a0a0202SVille Syrjälä 			I915_WRITE(GEN6_PMIIR, pm_iir);
19044a0a0202SVille Syrjälä 
19057ce4d1f2SVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
19061ae3c34cSVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
19077ce4d1f2SVille Syrjälä 
19083ff60f89SOscar Mateo 		/* Call regardless, as some status bits might not be
19093ff60f89SOscar Mateo 		 * signalled in iir */
191091d14251STvrtko Ursulin 		valleyview_pipestat_irq_ack(dev_priv, iir, pipe_stats);
19117ce4d1f2SVille Syrjälä 
1912eef57324SJerome Anand 		if (iir & (I915_LPE_PIPE_A_INTERRUPT |
1913eef57324SJerome Anand 			   I915_LPE_PIPE_B_INTERRUPT))
1914eef57324SJerome Anand 			intel_lpe_audio_irq_handler(dev_priv);
1915eef57324SJerome Anand 
19167ce4d1f2SVille Syrjälä 		/*
19177ce4d1f2SVille Syrjälä 		 * VLV_IIR is single buffered, and reflects the level
19187ce4d1f2SVille Syrjälä 		 * from PIPESTAT/PORT_HOTPLUG_STAT, hence clear it last.
19197ce4d1f2SVille Syrjälä 		 */
19207ce4d1f2SVille Syrjälä 		if (iir)
19217ce4d1f2SVille Syrjälä 			I915_WRITE(VLV_IIR, iir);
19224a0a0202SVille Syrjälä 
1923a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, ier);
19244a0a0202SVille Syrjälä 		I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
19254a0a0202SVille Syrjälä 		POSTING_READ(VLV_MASTER_IER);
19261ae3c34cSVille Syrjälä 
192752894874SVille Syrjälä 		if (gt_iir)
1928261e40b8SVille Syrjälä 			snb_gt_irq_handler(dev_priv, gt_iir);
192952894874SVille Syrjälä 		if (pm_iir)
193052894874SVille Syrjälä 			gen6_rps_irq_handler(dev_priv, pm_iir);
193152894874SVille Syrjälä 
19321ae3c34cSVille Syrjälä 		if (hotplug_status)
193391d14251STvrtko Ursulin 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
19342ecb8ca4SVille Syrjälä 
193591d14251STvrtko Ursulin 		valleyview_pipestat_irq_handler(dev_priv, pipe_stats);
19361e1cace9SVille Syrjälä 	} while (0);
19377e231dbeSJesse Barnes 
19381f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
19391f814dacSImre Deak 
19407e231dbeSJesse Barnes 	return ret;
19417e231dbeSJesse Barnes }
19427e231dbeSJesse Barnes 
194343f328d7SVille Syrjälä static irqreturn_t cherryview_irq_handler(int irq, void *arg)
194443f328d7SVille Syrjälä {
194545a83f84SDaniel Vetter 	struct drm_device *dev = arg;
1946fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
194743f328d7SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
194843f328d7SVille Syrjälä 
19492dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
19502dd2a883SImre Deak 		return IRQ_NONE;
19512dd2a883SImre Deak 
19521f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
19531f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
19541f814dacSImre Deak 
1955579de73bSChris Wilson 	do {
19566e814800SVille Syrjälä 		u32 master_ctl, iir;
1957e30e251aSVille Syrjälä 		u32 gt_iir[4] = {};
19582ecb8ca4SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
19591ae3c34cSVille Syrjälä 		u32 hotplug_status = 0;
1960a5e485a9SVille Syrjälä 		u32 ier = 0;
1961a5e485a9SVille Syrjälä 
19628e5fd599SVille Syrjälä 		master_ctl = I915_READ(GEN8_MASTER_IRQ) & ~GEN8_MASTER_IRQ_CONTROL;
19633278f67fSVille Syrjälä 		iir = I915_READ(VLV_IIR);
19643278f67fSVille Syrjälä 
19653278f67fSVille Syrjälä 		if (master_ctl == 0 && iir == 0)
19668e5fd599SVille Syrjälä 			break;
196743f328d7SVille Syrjälä 
196827b6c122SOscar Mateo 		ret = IRQ_HANDLED;
196927b6c122SOscar Mateo 
1970a5e485a9SVille Syrjälä 		/*
1971a5e485a9SVille Syrjälä 		 * Theory on interrupt generation, based on empirical evidence:
1972a5e485a9SVille Syrjälä 		 *
1973a5e485a9SVille Syrjälä 		 * x = ((VLV_IIR & VLV_IER) ||
1974a5e485a9SVille Syrjälä 		 *      ((GEN8_MASTER_IRQ & ~GEN8_MASTER_IRQ_CONTROL) &&
1975a5e485a9SVille Syrjälä 		 *       (GEN8_MASTER_IRQ & GEN8_MASTER_IRQ_CONTROL)));
1976a5e485a9SVille Syrjälä 		 *
1977a5e485a9SVille Syrjälä 		 * A CPU interrupt will only be raised when 'x' has a 0->1 edge.
1978a5e485a9SVille Syrjälä 		 * Hence we clear GEN8_MASTER_IRQ_CONTROL and VLV_IER to
1979a5e485a9SVille Syrjälä 		 * guarantee the CPU interrupt will be raised again even if we
1980a5e485a9SVille Syrjälä 		 * don't end up clearing all the VLV_IIR and GEN8_MASTER_IRQ_CONTROL
1981a5e485a9SVille Syrjälä 		 * bits this time around.
1982a5e485a9SVille Syrjälä 		 */
198343f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, 0);
1984a5e485a9SVille Syrjälä 		ier = I915_READ(VLV_IER);
1985a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, 0);
198643f328d7SVille Syrjälä 
1987e30e251aSVille Syrjälä 		gen8_gt_irq_ack(dev_priv, master_ctl, gt_iir);
198827b6c122SOscar Mateo 
198927b6c122SOscar Mateo 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
19901ae3c34cSVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
199143f328d7SVille Syrjälä 
199227b6c122SOscar Mateo 		/* Call regardless, as some status bits might not be
199327b6c122SOscar Mateo 		 * signalled in iir */
199491d14251STvrtko Ursulin 		valleyview_pipestat_irq_ack(dev_priv, iir, pipe_stats);
199543f328d7SVille Syrjälä 
1996eef57324SJerome Anand 		if (iir & (I915_LPE_PIPE_A_INTERRUPT |
1997eef57324SJerome Anand 			   I915_LPE_PIPE_B_INTERRUPT |
1998eef57324SJerome Anand 			   I915_LPE_PIPE_C_INTERRUPT))
1999eef57324SJerome Anand 			intel_lpe_audio_irq_handler(dev_priv);
2000eef57324SJerome Anand 
20017ce4d1f2SVille Syrjälä 		/*
20027ce4d1f2SVille Syrjälä 		 * VLV_IIR is single buffered, and reflects the level
20037ce4d1f2SVille Syrjälä 		 * from PIPESTAT/PORT_HOTPLUG_STAT, hence clear it last.
20047ce4d1f2SVille Syrjälä 		 */
20057ce4d1f2SVille Syrjälä 		if (iir)
20067ce4d1f2SVille Syrjälä 			I915_WRITE(VLV_IIR, iir);
20077ce4d1f2SVille Syrjälä 
2008a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, ier);
2009e5328c43SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
201043f328d7SVille Syrjälä 		POSTING_READ(GEN8_MASTER_IRQ);
20111ae3c34cSVille Syrjälä 
2012e30e251aSVille Syrjälä 		gen8_gt_irq_handler(dev_priv, gt_iir);
2013e30e251aSVille Syrjälä 
20141ae3c34cSVille Syrjälä 		if (hotplug_status)
201591d14251STvrtko Ursulin 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
20162ecb8ca4SVille Syrjälä 
201791d14251STvrtko Ursulin 		valleyview_pipestat_irq_handler(dev_priv, pipe_stats);
2018579de73bSChris Wilson 	} while (0);
20193278f67fSVille Syrjälä 
20201f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
20211f814dacSImre Deak 
202243f328d7SVille Syrjälä 	return ret;
202343f328d7SVille Syrjälä }
202443f328d7SVille Syrjälä 
202591d14251STvrtko Ursulin static void ibx_hpd_irq_handler(struct drm_i915_private *dev_priv,
202691d14251STvrtko Ursulin 				u32 hotplug_trigger,
202740e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2028776ad806SJesse Barnes {
202942db67d6SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2030776ad806SJesse Barnes 
20316a39d7c9SJani Nikula 	/*
20326a39d7c9SJani Nikula 	 * Somehow the PCH doesn't seem to really ack the interrupt to the CPU
20336a39d7c9SJani Nikula 	 * unless we touch the hotplug register, even if hotplug_trigger is
20346a39d7c9SJani Nikula 	 * zero. Not acking leads to "The master control interrupt lied (SDE)!"
20356a39d7c9SJani Nikula 	 * errors.
20366a39d7c9SJani Nikula 	 */
203713cf5504SDave Airlie 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
20386a39d7c9SJani Nikula 	if (!hotplug_trigger) {
20396a39d7c9SJani Nikula 		u32 mask = PORTA_HOTPLUG_STATUS_MASK |
20406a39d7c9SJani Nikula 			PORTD_HOTPLUG_STATUS_MASK |
20416a39d7c9SJani Nikula 			PORTC_HOTPLUG_STATUS_MASK |
20426a39d7c9SJani Nikula 			PORTB_HOTPLUG_STATUS_MASK;
20436a39d7c9SJani Nikula 		dig_hotplug_reg &= ~mask;
20446a39d7c9SJani Nikula 	}
20456a39d7c9SJani Nikula 
204613cf5504SDave Airlie 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
20476a39d7c9SJani Nikula 	if (!hotplug_trigger)
20486a39d7c9SJani Nikula 		return;
204913cf5504SDave Airlie 
2050fd63e2a9SImre Deak 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
205140e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2052fd63e2a9SImre Deak 			   pch_port_hotplug_long_detect);
205340e56410SVille Syrjälä 
205491d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2055aaf5ec2eSSonika Jindal }
205691d131d2SDaniel Vetter 
205791d14251STvrtko Ursulin static void ibx_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
205840e56410SVille Syrjälä {
205940e56410SVille Syrjälä 	int pipe;
206040e56410SVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
206140e56410SVille Syrjälä 
206291d14251STvrtko Ursulin 	ibx_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ibx);
206340e56410SVille Syrjälä 
2064cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
2065cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
2066776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
2067cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
2068cfc33bf7SVille Syrjälä 				 port_name(port));
2069cfc33bf7SVille Syrjälä 	}
2070776ad806SJesse Barnes 
2071ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
207291d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
2073ce99c256SDaniel Vetter 
2074776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
207591d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
2076776ad806SJesse Barnes 
2077776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
2078776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
2079776ad806SJesse Barnes 
2080776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
2081776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
2082776ad806SJesse Barnes 
2083776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
2084776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
2085776ad806SJesse Barnes 
20869db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
2087055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
20889db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
20899db4a9c7SJesse Barnes 					 pipe_name(pipe),
20909db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
2091776ad806SJesse Barnes 
2092776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
2093776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
2094776ad806SJesse Barnes 
2095776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
2096776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
2097776ad806SJesse Barnes 
2098776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
20991f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
21008664281bSPaulo Zanoni 
21018664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
21021f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
21038664281bSPaulo Zanoni }
21048664281bSPaulo Zanoni 
210591d14251STvrtko Ursulin static void ivb_err_int_handler(struct drm_i915_private *dev_priv)
21068664281bSPaulo Zanoni {
21078664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
21085a69b89fSDaniel Vetter 	enum pipe pipe;
21098664281bSPaulo Zanoni 
2110de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
2111de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
2112de032bf4SPaulo Zanoni 
2113055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
21141f7247c0SDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe))
21151f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
21168664281bSPaulo Zanoni 
21175a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
211891d14251STvrtko Ursulin 			if (IS_IVYBRIDGE(dev_priv))
211991d14251STvrtko Ursulin 				ivb_pipe_crc_irq_handler(dev_priv, pipe);
21205a69b89fSDaniel Vetter 			else
212191d14251STvrtko Ursulin 				hsw_pipe_crc_irq_handler(dev_priv, pipe);
21225a69b89fSDaniel Vetter 		}
21235a69b89fSDaniel Vetter 	}
21248bf1e9f1SShuang He 
21258664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
21268664281bSPaulo Zanoni }
21278664281bSPaulo Zanoni 
212891d14251STvrtko Ursulin static void cpt_serr_int_handler(struct drm_i915_private *dev_priv)
21298664281bSPaulo Zanoni {
21308664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
21318664281bSPaulo Zanoni 
2132de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
2133de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
2134de032bf4SPaulo Zanoni 
21358664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
21361f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
21378664281bSPaulo Zanoni 
21388664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
21391f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
21408664281bSPaulo Zanoni 
21418664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
21421f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_C);
21438664281bSPaulo Zanoni 
21448664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
2145776ad806SJesse Barnes }
2146776ad806SJesse Barnes 
214791d14251STvrtko Ursulin static void cpt_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
214823e81d69SAdam Jackson {
214923e81d69SAdam Jackson 	int pipe;
21506dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
2151aaf5ec2eSSonika Jindal 
215291d14251STvrtko Ursulin 	ibx_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_cpt);
215391d131d2SDaniel Vetter 
2154cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
2155cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
215623e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
2157cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
2158cfc33bf7SVille Syrjälä 				 port_name(port));
2159cfc33bf7SVille Syrjälä 	}
216023e81d69SAdam Jackson 
216123e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
216291d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
216323e81d69SAdam Jackson 
216423e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
216591d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
216623e81d69SAdam Jackson 
216723e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
216823e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
216923e81d69SAdam Jackson 
217023e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
217123e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
217223e81d69SAdam Jackson 
217323e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
2174055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
217523e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
217623e81d69SAdam Jackson 					 pipe_name(pipe),
217723e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
21788664281bSPaulo Zanoni 
21798664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
218091d14251STvrtko Ursulin 		cpt_serr_int_handler(dev_priv);
218123e81d69SAdam Jackson }
218223e81d69SAdam Jackson 
218391d14251STvrtko Ursulin static void spt_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
21846dbf30ceSVille Syrjälä {
21856dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_SPT &
21866dbf30ceSVille Syrjälä 		~SDE_PORTE_HOTPLUG_SPT;
21876dbf30ceSVille Syrjälä 	u32 hotplug2_trigger = pch_iir & SDE_PORTE_HOTPLUG_SPT;
21886dbf30ceSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
21896dbf30ceSVille Syrjälä 
21906dbf30ceSVille Syrjälä 	if (hotplug_trigger) {
21916dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
21926dbf30ceSVille Syrjälä 
21936dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
21946dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
21956dbf30ceSVille Syrjälä 
21966dbf30ceSVille Syrjälä 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
21976dbf30ceSVille Syrjälä 				   dig_hotplug_reg, hpd_spt,
219874c0b395SVille Syrjälä 				   spt_port_hotplug_long_detect);
21996dbf30ceSVille Syrjälä 	}
22006dbf30ceSVille Syrjälä 
22016dbf30ceSVille Syrjälä 	if (hotplug2_trigger) {
22026dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
22036dbf30ceSVille Syrjälä 
22046dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG2);
22056dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG2, dig_hotplug_reg);
22066dbf30ceSVille Syrjälä 
22076dbf30ceSVille Syrjälä 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug2_trigger,
22086dbf30ceSVille Syrjälä 				   dig_hotplug_reg, hpd_spt,
22096dbf30ceSVille Syrjälä 				   spt_port_hotplug2_long_detect);
22106dbf30ceSVille Syrjälä 	}
22116dbf30ceSVille Syrjälä 
22126dbf30ceSVille Syrjälä 	if (pin_mask)
221391d14251STvrtko Ursulin 		intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
22146dbf30ceSVille Syrjälä 
22156dbf30ceSVille Syrjälä 	if (pch_iir & SDE_GMBUS_CPT)
221691d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
22176dbf30ceSVille Syrjälä }
22186dbf30ceSVille Syrjälä 
221991d14251STvrtko Ursulin static void ilk_hpd_irq_handler(struct drm_i915_private *dev_priv,
222091d14251STvrtko Ursulin 				u32 hotplug_trigger,
222140e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2222c008bc6eSPaulo Zanoni {
2223e4ce95aaSVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2224e4ce95aaSVille Syrjälä 
2225e4ce95aaSVille Syrjälä 	dig_hotplug_reg = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
2226e4ce95aaSVille Syrjälä 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, dig_hotplug_reg);
2227e4ce95aaSVille Syrjälä 
2228e4ce95aaSVille Syrjälä 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
222940e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2230e4ce95aaSVille Syrjälä 			   ilk_port_hotplug_long_detect);
223140e56410SVille Syrjälä 
223291d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2233e4ce95aaSVille Syrjälä }
2234c008bc6eSPaulo Zanoni 
223591d14251STvrtko Ursulin static void ilk_display_irq_handler(struct drm_i915_private *dev_priv,
223691d14251STvrtko Ursulin 				    u32 de_iir)
223740e56410SVille Syrjälä {
223840e56410SVille Syrjälä 	enum pipe pipe;
223940e56410SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG;
224040e56410SVille Syrjälä 
224140e56410SVille Syrjälä 	if (hotplug_trigger)
224291d14251STvrtko Ursulin 		ilk_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ilk);
224340e56410SVille Syrjälä 
2244c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
224591d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
2246c008bc6eSPaulo Zanoni 
2247c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
224891d14251STvrtko Ursulin 		intel_opregion_asle_intr(dev_priv);
2249c008bc6eSPaulo Zanoni 
2250c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
2251c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
2252c008bc6eSPaulo Zanoni 
2253055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
22545a21b665SDaniel Vetter 		if (de_iir & DE_PIPE_VBLANK(pipe) &&
22555a21b665SDaniel Vetter 		    intel_pipe_handle_vblank(dev_priv, pipe))
22565a21b665SDaniel Vetter 			intel_check_page_flip(dev_priv, pipe);
2257c008bc6eSPaulo Zanoni 
225840da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
22591f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
2260c008bc6eSPaulo Zanoni 
226140da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
226291d14251STvrtko Ursulin 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
22635b3a856bSDaniel Vetter 
226440da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
22655251f04eSMaarten Lankhorst 		if (de_iir & DE_PLANE_FLIP_DONE(pipe))
226651cbaf01SMaarten Lankhorst 			intel_finish_page_flip_cs(dev_priv, pipe);
2267c008bc6eSPaulo Zanoni 	}
2268c008bc6eSPaulo Zanoni 
2269c008bc6eSPaulo Zanoni 	/* check event from PCH */
2270c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
2271c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
2272c008bc6eSPaulo Zanoni 
227391d14251STvrtko Ursulin 		if (HAS_PCH_CPT(dev_priv))
227491d14251STvrtko Ursulin 			cpt_irq_handler(dev_priv, pch_iir);
2275c008bc6eSPaulo Zanoni 		else
227691d14251STvrtko Ursulin 			ibx_irq_handler(dev_priv, pch_iir);
2277c008bc6eSPaulo Zanoni 
2278c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
2279c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
2280c008bc6eSPaulo Zanoni 	}
2281c008bc6eSPaulo Zanoni 
228291d14251STvrtko Ursulin 	if (IS_GEN5(dev_priv) && de_iir & DE_PCU_EVENT)
228391d14251STvrtko Ursulin 		ironlake_rps_change_irq_handler(dev_priv);
2284c008bc6eSPaulo Zanoni }
2285c008bc6eSPaulo Zanoni 
228691d14251STvrtko Ursulin static void ivb_display_irq_handler(struct drm_i915_private *dev_priv,
228791d14251STvrtko Ursulin 				    u32 de_iir)
22889719fb98SPaulo Zanoni {
228907d27e20SDamien Lespiau 	enum pipe pipe;
229023bb4cb5SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG_IVB;
229123bb4cb5SVille Syrjälä 
229240e56410SVille Syrjälä 	if (hotplug_trigger)
229391d14251STvrtko Ursulin 		ilk_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ivb);
22949719fb98SPaulo Zanoni 
22959719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
229691d14251STvrtko Ursulin 		ivb_err_int_handler(dev_priv);
22979719fb98SPaulo Zanoni 
22989719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
229991d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
23009719fb98SPaulo Zanoni 
23019719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
230291d14251STvrtko Ursulin 		intel_opregion_asle_intr(dev_priv);
23039719fb98SPaulo Zanoni 
2304055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
23055a21b665SDaniel Vetter 		if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)) &&
23065a21b665SDaniel Vetter 		    intel_pipe_handle_vblank(dev_priv, pipe))
23075a21b665SDaniel Vetter 			intel_check_page_flip(dev_priv, pipe);
230840da17c2SDaniel Vetter 
230940da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
23105251f04eSMaarten Lankhorst 		if (de_iir & DE_PLANE_FLIP_DONE_IVB(pipe))
231151cbaf01SMaarten Lankhorst 			intel_finish_page_flip_cs(dev_priv, pipe);
23129719fb98SPaulo Zanoni 	}
23139719fb98SPaulo Zanoni 
23149719fb98SPaulo Zanoni 	/* check event from PCH */
231591d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv) && (de_iir & DE_PCH_EVENT_IVB)) {
23169719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
23179719fb98SPaulo Zanoni 
231891d14251STvrtko Ursulin 		cpt_irq_handler(dev_priv, pch_iir);
23199719fb98SPaulo Zanoni 
23209719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
23219719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
23229719fb98SPaulo Zanoni 	}
23239719fb98SPaulo Zanoni }
23249719fb98SPaulo Zanoni 
232572c90f62SOscar Mateo /*
232672c90f62SOscar Mateo  * To handle irqs with the minimum potential races with fresh interrupts, we:
232772c90f62SOscar Mateo  * 1 - Disable Master Interrupt Control.
232872c90f62SOscar Mateo  * 2 - Find the source(s) of the interrupt.
232972c90f62SOscar Mateo  * 3 - Clear the Interrupt Identity bits (IIR).
233072c90f62SOscar Mateo  * 4 - Process the interrupt(s) that had bits set in the IIRs.
233172c90f62SOscar Mateo  * 5 - Re-enable Master Interrupt Control.
233272c90f62SOscar Mateo  */
2333f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
2334b1f14ad0SJesse Barnes {
233545a83f84SDaniel Vetter 	struct drm_device *dev = arg;
2336fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2337f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
23380e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
2339b1f14ad0SJesse Barnes 
23402dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
23412dd2a883SImre Deak 		return IRQ_NONE;
23422dd2a883SImre Deak 
23431f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
23441f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
23451f814dacSImre Deak 
2346b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
2347b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
2348b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
234923a78516SPaulo Zanoni 	POSTING_READ(DEIER);
23500e43406bSChris Wilson 
235144498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
235244498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
235344498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
235444498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
235544498aeaSPaulo Zanoni 	 * due to its back queue). */
235691d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv)) {
235744498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
235844498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
235944498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2360ab5c608bSBen Widawsky 	}
236144498aeaSPaulo Zanoni 
236272c90f62SOscar Mateo 	/* Find, clear, then process each source of interrupt */
236372c90f62SOscar Mateo 
23640e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
23650e43406bSChris Wilson 	if (gt_iir) {
236672c90f62SOscar Mateo 		I915_WRITE(GTIIR, gt_iir);
236772c90f62SOscar Mateo 		ret = IRQ_HANDLED;
236891d14251STvrtko Ursulin 		if (INTEL_GEN(dev_priv) >= 6)
2369261e40b8SVille Syrjälä 			snb_gt_irq_handler(dev_priv, gt_iir);
2370d8fc8a47SPaulo Zanoni 		else
2371261e40b8SVille Syrjälä 			ilk_gt_irq_handler(dev_priv, gt_iir);
23720e43406bSChris Wilson 	}
2373b1f14ad0SJesse Barnes 
2374b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
23750e43406bSChris Wilson 	if (de_iir) {
237672c90f62SOscar Mateo 		I915_WRITE(DEIIR, de_iir);
237772c90f62SOscar Mateo 		ret = IRQ_HANDLED;
237891d14251STvrtko Ursulin 		if (INTEL_GEN(dev_priv) >= 7)
237991d14251STvrtko Ursulin 			ivb_display_irq_handler(dev_priv, de_iir);
2380f1af8fc1SPaulo Zanoni 		else
238191d14251STvrtko Ursulin 			ilk_display_irq_handler(dev_priv, de_iir);
23820e43406bSChris Wilson 	}
23830e43406bSChris Wilson 
238491d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 6) {
2385f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
23860e43406bSChris Wilson 		if (pm_iir) {
2387b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
23880e43406bSChris Wilson 			ret = IRQ_HANDLED;
238972c90f62SOscar Mateo 			gen6_rps_irq_handler(dev_priv, pm_iir);
23900e43406bSChris Wilson 		}
2391f1af8fc1SPaulo Zanoni 	}
2392b1f14ad0SJesse Barnes 
2393b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
2394b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
239591d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv)) {
239644498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
239744498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2398ab5c608bSBen Widawsky 	}
2399b1f14ad0SJesse Barnes 
24001f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
24011f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
24021f814dacSImre Deak 
2403b1f14ad0SJesse Barnes 	return ret;
2404b1f14ad0SJesse Barnes }
2405b1f14ad0SJesse Barnes 
240691d14251STvrtko Ursulin static void bxt_hpd_irq_handler(struct drm_i915_private *dev_priv,
240791d14251STvrtko Ursulin 				u32 hotplug_trigger,
240840e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2409d04a492dSShashank Sharma {
2410cebd87a0SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2411d04a492dSShashank Sharma 
2412a52bb15bSVille Syrjälä 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
2413a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
2414d04a492dSShashank Sharma 
2415cebd87a0SVille Syrjälä 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
241640e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2417cebd87a0SVille Syrjälä 			   bxt_port_hotplug_long_detect);
241840e56410SVille Syrjälä 
241991d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2420d04a492dSShashank Sharma }
2421d04a492dSShashank Sharma 
2422f11a0f46STvrtko Ursulin static irqreturn_t
2423f11a0f46STvrtko Ursulin gen8_de_irq_handler(struct drm_i915_private *dev_priv, u32 master_ctl)
2424abd58f01SBen Widawsky {
2425abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
2426f11a0f46STvrtko Ursulin 	u32 iir;
2427c42664ccSDaniel Vetter 	enum pipe pipe;
242888e04703SJesse Barnes 
2429abd58f01SBen Widawsky 	if (master_ctl & GEN8_DE_MISC_IRQ) {
2430e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_MISC_IIR);
2431e32192e1STvrtko Ursulin 		if (iir) {
2432e32192e1STvrtko Ursulin 			I915_WRITE(GEN8_DE_MISC_IIR, iir);
2433abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
2434e32192e1STvrtko Ursulin 			if (iir & GEN8_DE_MISC_GSE)
243591d14251STvrtko Ursulin 				intel_opregion_asle_intr(dev_priv);
243638cc46d7SOscar Mateo 			else
243738cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Misc interrupt\n");
2438abd58f01SBen Widawsky 		}
243938cc46d7SOscar Mateo 		else
244038cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
2441abd58f01SBen Widawsky 	}
2442abd58f01SBen Widawsky 
24436d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
2444e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_PORT_IIR);
2445e32192e1STvrtko Ursulin 		if (iir) {
2446e32192e1STvrtko Ursulin 			u32 tmp_mask;
2447d04a492dSShashank Sharma 			bool found = false;
2448cebd87a0SVille Syrjälä 
2449e32192e1STvrtko Ursulin 			I915_WRITE(GEN8_DE_PORT_IIR, iir);
24506d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
245188e04703SJesse Barnes 
2452e32192e1STvrtko Ursulin 			tmp_mask = GEN8_AUX_CHANNEL_A;
2453e32192e1STvrtko Ursulin 			if (INTEL_INFO(dev_priv)->gen >= 9)
2454e32192e1STvrtko Ursulin 				tmp_mask |= GEN9_AUX_CHANNEL_B |
2455e32192e1STvrtko Ursulin 					    GEN9_AUX_CHANNEL_C |
2456e32192e1STvrtko Ursulin 					    GEN9_AUX_CHANNEL_D;
2457e32192e1STvrtko Ursulin 
2458e32192e1STvrtko Ursulin 			if (iir & tmp_mask) {
245991d14251STvrtko Ursulin 				dp_aux_irq_handler(dev_priv);
2460d04a492dSShashank Sharma 				found = true;
2461d04a492dSShashank Sharma 			}
2462d04a492dSShashank Sharma 
2463cc3f90f0SAnder Conselvan de Oliveira 			if (IS_GEN9_LP(dev_priv)) {
2464e32192e1STvrtko Ursulin 				tmp_mask = iir & BXT_DE_PORT_HOTPLUG_MASK;
2465e32192e1STvrtko Ursulin 				if (tmp_mask) {
246691d14251STvrtko Ursulin 					bxt_hpd_irq_handler(dev_priv, tmp_mask,
246791d14251STvrtko Ursulin 							    hpd_bxt);
2468d04a492dSShashank Sharma 					found = true;
2469d04a492dSShashank Sharma 				}
2470e32192e1STvrtko Ursulin 			} else if (IS_BROADWELL(dev_priv)) {
2471e32192e1STvrtko Ursulin 				tmp_mask = iir & GEN8_PORT_DP_A_HOTPLUG;
2472e32192e1STvrtko Ursulin 				if (tmp_mask) {
247391d14251STvrtko Ursulin 					ilk_hpd_irq_handler(dev_priv,
247491d14251STvrtko Ursulin 							    tmp_mask, hpd_bdw);
2475e32192e1STvrtko Ursulin 					found = true;
2476e32192e1STvrtko Ursulin 				}
2477e32192e1STvrtko Ursulin 			}
2478d04a492dSShashank Sharma 
2479cc3f90f0SAnder Conselvan de Oliveira 			if (IS_GEN9_LP(dev_priv) && (iir & BXT_DE_PORT_GMBUS)) {
248091d14251STvrtko Ursulin 				gmbus_irq_handler(dev_priv);
24819e63743eSShashank Sharma 				found = true;
24829e63743eSShashank Sharma 			}
24839e63743eSShashank Sharma 
2484d04a492dSShashank Sharma 			if (!found)
248538cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Port interrupt\n");
24866d766f02SDaniel Vetter 		}
248738cc46d7SOscar Mateo 		else
248838cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
24896d766f02SDaniel Vetter 	}
24906d766f02SDaniel Vetter 
2491055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2492e32192e1STvrtko Ursulin 		u32 flip_done, fault_errors;
2493abd58f01SBen Widawsky 
2494c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2495c42664ccSDaniel Vetter 			continue;
2496c42664ccSDaniel Vetter 
2497e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
2498e32192e1STvrtko Ursulin 		if (!iir) {
2499e32192e1STvrtko Ursulin 			DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
2500e32192e1STvrtko Ursulin 			continue;
2501e32192e1STvrtko Ursulin 		}
2502770de83dSDamien Lespiau 
2503e32192e1STvrtko Ursulin 		ret = IRQ_HANDLED;
2504e32192e1STvrtko Ursulin 		I915_WRITE(GEN8_DE_PIPE_IIR(pipe), iir);
2505e32192e1STvrtko Ursulin 
25065a21b665SDaniel Vetter 		if (iir & GEN8_PIPE_VBLANK &&
25075a21b665SDaniel Vetter 		    intel_pipe_handle_vblank(dev_priv, pipe))
25085a21b665SDaniel Vetter 			intel_check_page_flip(dev_priv, pipe);
2509abd58f01SBen Widawsky 
2510e32192e1STvrtko Ursulin 		flip_done = iir;
2511b4834a50SRodrigo Vivi 		if (INTEL_INFO(dev_priv)->gen >= 9)
2512e32192e1STvrtko Ursulin 			flip_done &= GEN9_PIPE_PLANE1_FLIP_DONE;
2513770de83dSDamien Lespiau 		else
2514e32192e1STvrtko Ursulin 			flip_done &= GEN8_PIPE_PRIMARY_FLIP_DONE;
2515770de83dSDamien Lespiau 
25165251f04eSMaarten Lankhorst 		if (flip_done)
251751cbaf01SMaarten Lankhorst 			intel_finish_page_flip_cs(dev_priv, pipe);
2518abd58f01SBen Widawsky 
2519e32192e1STvrtko Ursulin 		if (iir & GEN8_PIPE_CDCLK_CRC_DONE)
252091d14251STvrtko Ursulin 			hsw_pipe_crc_irq_handler(dev_priv, pipe);
25210fbe7870SDaniel Vetter 
2522e32192e1STvrtko Ursulin 		if (iir & GEN8_PIPE_FIFO_UNDERRUN)
2523e32192e1STvrtko Ursulin 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
252438d83c96SDaniel Vetter 
2525e32192e1STvrtko Ursulin 		fault_errors = iir;
2526b4834a50SRodrigo Vivi 		if (INTEL_INFO(dev_priv)->gen >= 9)
2527e32192e1STvrtko Ursulin 			fault_errors &= GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
2528770de83dSDamien Lespiau 		else
2529e32192e1STvrtko Ursulin 			fault_errors &= GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
2530770de83dSDamien Lespiau 
2531770de83dSDamien Lespiau 		if (fault_errors)
25321353ec38STvrtko Ursulin 			DRM_ERROR("Fault errors on pipe %c: 0x%08x\n",
253330100f2bSDaniel Vetter 				  pipe_name(pipe),
2534e32192e1STvrtko Ursulin 				  fault_errors);
2535abd58f01SBen Widawsky 	}
2536abd58f01SBen Widawsky 
253791d14251STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv) && !HAS_PCH_NOP(dev_priv) &&
2538266ea3d9SShashank Sharma 	    master_ctl & GEN8_DE_PCH_IRQ) {
253992d03a80SDaniel Vetter 		/*
254092d03a80SDaniel Vetter 		 * FIXME(BDW): Assume for now that the new interrupt handling
254192d03a80SDaniel Vetter 		 * scheme also closed the SDE interrupt handling race we've seen
254292d03a80SDaniel Vetter 		 * on older pch-split platforms. But this needs testing.
254392d03a80SDaniel Vetter 		 */
2544e32192e1STvrtko Ursulin 		iir = I915_READ(SDEIIR);
2545e32192e1STvrtko Ursulin 		if (iir) {
2546e32192e1STvrtko Ursulin 			I915_WRITE(SDEIIR, iir);
254792d03a80SDaniel Vetter 			ret = IRQ_HANDLED;
25486dbf30ceSVille Syrjälä 
254922dea0beSRodrigo Vivi 			if (HAS_PCH_SPT(dev_priv) || HAS_PCH_KBP(dev_priv))
255091d14251STvrtko Ursulin 				spt_irq_handler(dev_priv, iir);
25516dbf30ceSVille Syrjälä 			else
255291d14251STvrtko Ursulin 				cpt_irq_handler(dev_priv, iir);
25532dfb0b81SJani Nikula 		} else {
25542dfb0b81SJani Nikula 			/*
25552dfb0b81SJani Nikula 			 * Like on previous PCH there seems to be something
25562dfb0b81SJani Nikula 			 * fishy going on with forwarding PCH interrupts.
25572dfb0b81SJani Nikula 			 */
25582dfb0b81SJani Nikula 			DRM_DEBUG_DRIVER("The master control interrupt lied (SDE)!\n");
25592dfb0b81SJani Nikula 		}
256092d03a80SDaniel Vetter 	}
256192d03a80SDaniel Vetter 
2562f11a0f46STvrtko Ursulin 	return ret;
2563f11a0f46STvrtko Ursulin }
2564f11a0f46STvrtko Ursulin 
2565f11a0f46STvrtko Ursulin static irqreturn_t gen8_irq_handler(int irq, void *arg)
2566f11a0f46STvrtko Ursulin {
2567f11a0f46STvrtko Ursulin 	struct drm_device *dev = arg;
2568fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2569f11a0f46STvrtko Ursulin 	u32 master_ctl;
2570e30e251aSVille Syrjälä 	u32 gt_iir[4] = {};
2571f11a0f46STvrtko Ursulin 	irqreturn_t ret;
2572f11a0f46STvrtko Ursulin 
2573f11a0f46STvrtko Ursulin 	if (!intel_irqs_enabled(dev_priv))
2574f11a0f46STvrtko Ursulin 		return IRQ_NONE;
2575f11a0f46STvrtko Ursulin 
2576f11a0f46STvrtko Ursulin 	master_ctl = I915_READ_FW(GEN8_MASTER_IRQ);
2577f11a0f46STvrtko Ursulin 	master_ctl &= ~GEN8_MASTER_IRQ_CONTROL;
2578f11a0f46STvrtko Ursulin 	if (!master_ctl)
2579f11a0f46STvrtko Ursulin 		return IRQ_NONE;
2580f11a0f46STvrtko Ursulin 
2581f11a0f46STvrtko Ursulin 	I915_WRITE_FW(GEN8_MASTER_IRQ, 0);
2582f11a0f46STvrtko Ursulin 
2583f11a0f46STvrtko Ursulin 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
2584f11a0f46STvrtko Ursulin 	disable_rpm_wakeref_asserts(dev_priv);
2585f11a0f46STvrtko Ursulin 
2586f11a0f46STvrtko Ursulin 	/* Find, clear, then process each source of interrupt */
2587e30e251aSVille Syrjälä 	ret = gen8_gt_irq_ack(dev_priv, master_ctl, gt_iir);
2588e30e251aSVille Syrjälä 	gen8_gt_irq_handler(dev_priv, gt_iir);
2589f11a0f46STvrtko Ursulin 	ret |= gen8_de_irq_handler(dev_priv, master_ctl);
2590f11a0f46STvrtko Ursulin 
2591cb0d205eSChris Wilson 	I915_WRITE_FW(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
2592cb0d205eSChris Wilson 	POSTING_READ_FW(GEN8_MASTER_IRQ);
2593abd58f01SBen Widawsky 
25941f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
25951f814dacSImre Deak 
2596abd58f01SBen Widawsky 	return ret;
2597abd58f01SBen Widawsky }
2598abd58f01SBen Widawsky 
25998a905236SJesse Barnes /**
2600b8d24a06SMika Kuoppala  * i915_reset_and_wakeup - do process context error handling work
260114bb2c11STvrtko Ursulin  * @dev_priv: i915 device private
26028a905236SJesse Barnes  *
26038a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
26048a905236SJesse Barnes  * was detected.
26058a905236SJesse Barnes  */
2606c033666aSChris Wilson static void i915_reset_and_wakeup(struct drm_i915_private *dev_priv)
26078a905236SJesse Barnes {
260891c8a326SChris Wilson 	struct kobject *kobj = &dev_priv->drm.primary->kdev->kobj;
2609cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
2610cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
2611cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
26128a905236SJesse Barnes 
2613c033666aSChris Wilson 	kobject_uevent_env(kobj, KOBJ_CHANGE, error_event);
26148a905236SJesse Barnes 
261544d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("resetting chip\n");
2616c033666aSChris Wilson 	kobject_uevent_env(kobj, KOBJ_CHANGE, reset_event);
26171f83fee0SDaniel Vetter 
2618c033666aSChris Wilson 	intel_prepare_reset(dev_priv);
26197514747dSVille Syrjälä 
26208c185ecaSChris Wilson 	set_bit(I915_RESET_HANDOFF, &dev_priv->gpu_error.flags);
26218c185ecaSChris Wilson 	wake_up_all(&dev_priv->gpu_error.wait_queue);
26228c185ecaSChris Wilson 
2623780f262aSChris Wilson 	do {
2624f454c694SImre Deak 		/*
262517e1df07SDaniel Vetter 		 * All state reset _must_ be completed before we update the
262617e1df07SDaniel Vetter 		 * reset counter, for otherwise waiters might miss the reset
262717e1df07SDaniel Vetter 		 * pending state and not properly drop locks, resulting in
262817e1df07SDaniel Vetter 		 * deadlocks with the reset work.
262917e1df07SDaniel Vetter 		 */
2630780f262aSChris Wilson 		if (mutex_trylock(&dev_priv->drm.struct_mutex)) {
2631780f262aSChris Wilson 			i915_reset(dev_priv);
2632221fe799SChris Wilson 			mutex_unlock(&dev_priv->drm.struct_mutex);
2633780f262aSChris Wilson 		}
2634780f262aSChris Wilson 
2635780f262aSChris Wilson 		/* We need to wait for anyone holding the lock to wakeup */
2636780f262aSChris Wilson 	} while (wait_on_bit_timeout(&dev_priv->gpu_error.flags,
26378c185ecaSChris Wilson 				     I915_RESET_HANDOFF,
2638780f262aSChris Wilson 				     TASK_UNINTERRUPTIBLE,
2639780f262aSChris Wilson 				     HZ));
2640f69061beSDaniel Vetter 
2641c033666aSChris Wilson 	intel_finish_reset(dev_priv);
2642f454c694SImre Deak 
2643780f262aSChris Wilson 	if (!test_bit(I915_WEDGED, &dev_priv->gpu_error.flags))
2644c033666aSChris Wilson 		kobject_uevent_env(kobj,
2645f69061beSDaniel Vetter 				   KOBJ_CHANGE, reset_done_event);
26461f83fee0SDaniel Vetter 
264717e1df07SDaniel Vetter 	/*
264817e1df07SDaniel Vetter 	 * Note: The wake_up also serves as a memory barrier so that
26498af29b0cSChris Wilson 	 * waiters see the updated value of the dev_priv->gpu_error.
265017e1df07SDaniel Vetter 	 */
26518c185ecaSChris Wilson 	clear_bit(I915_RESET_BACKOFF, &dev_priv->gpu_error.flags);
26521f15b76fSChris Wilson 	wake_up_all(&dev_priv->gpu_error.reset_queue);
2653f316a42cSBen Gamari }
26548a905236SJesse Barnes 
2655d636951eSBen Widawsky static inline void
2656d636951eSBen Widawsky i915_err_print_instdone(struct drm_i915_private *dev_priv,
2657d636951eSBen Widawsky 			struct intel_instdone *instdone)
2658d636951eSBen Widawsky {
2659f9e61372SBen Widawsky 	int slice;
2660f9e61372SBen Widawsky 	int subslice;
2661f9e61372SBen Widawsky 
2662d636951eSBen Widawsky 	pr_err("  INSTDONE: 0x%08x\n", instdone->instdone);
2663d636951eSBen Widawsky 
2664d636951eSBen Widawsky 	if (INTEL_GEN(dev_priv) <= 3)
2665d636951eSBen Widawsky 		return;
2666d636951eSBen Widawsky 
2667d636951eSBen Widawsky 	pr_err("  SC_INSTDONE: 0x%08x\n", instdone->slice_common);
2668d636951eSBen Widawsky 
2669d636951eSBen Widawsky 	if (INTEL_GEN(dev_priv) <= 6)
2670d636951eSBen Widawsky 		return;
2671d636951eSBen Widawsky 
2672f9e61372SBen Widawsky 	for_each_instdone_slice_subslice(dev_priv, slice, subslice)
2673f9e61372SBen Widawsky 		pr_err("  SAMPLER_INSTDONE[%d][%d]: 0x%08x\n",
2674f9e61372SBen Widawsky 		       slice, subslice, instdone->sampler[slice][subslice]);
2675f9e61372SBen Widawsky 
2676f9e61372SBen Widawsky 	for_each_instdone_slice_subslice(dev_priv, slice, subslice)
2677f9e61372SBen Widawsky 		pr_err("  ROW_INSTDONE[%d][%d]: 0x%08x\n",
2678f9e61372SBen Widawsky 		       slice, subslice, instdone->row[slice][subslice]);
2679d636951eSBen Widawsky }
2680d636951eSBen Widawsky 
2681eaa14c24SChris Wilson static void i915_clear_error_registers(struct drm_i915_private *dev_priv)
2682c0e09200SDave Airlie {
2683eaa14c24SChris Wilson 	u32 eir;
268463eeaf38SJesse Barnes 
2685eaa14c24SChris Wilson 	if (!IS_GEN2(dev_priv))
2686eaa14c24SChris Wilson 		I915_WRITE(PGTBL_ER, I915_READ(PGTBL_ER));
268763eeaf38SJesse Barnes 
2688eaa14c24SChris Wilson 	if (INTEL_GEN(dev_priv) < 4)
2689eaa14c24SChris Wilson 		I915_WRITE(IPEIR, I915_READ(IPEIR));
2690eaa14c24SChris Wilson 	else
2691eaa14c24SChris Wilson 		I915_WRITE(IPEIR_I965, I915_READ(IPEIR_I965));
26928a905236SJesse Barnes 
2693eaa14c24SChris Wilson 	I915_WRITE(EIR, I915_READ(EIR));
269463eeaf38SJesse Barnes 	eir = I915_READ(EIR);
269563eeaf38SJesse Barnes 	if (eir) {
269663eeaf38SJesse Barnes 		/*
269763eeaf38SJesse Barnes 		 * some errors might have become stuck,
269863eeaf38SJesse Barnes 		 * mask them.
269963eeaf38SJesse Barnes 		 */
2700eaa14c24SChris Wilson 		DRM_DEBUG_DRIVER("EIR stuck: 0x%08x, masking\n", eir);
270163eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
270263eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
270363eeaf38SJesse Barnes 	}
270435aed2e6SChris Wilson }
270535aed2e6SChris Wilson 
270635aed2e6SChris Wilson /**
2707b8d24a06SMika Kuoppala  * i915_handle_error - handle a gpu error
270814bb2c11STvrtko Ursulin  * @dev_priv: i915 device private
270914b730fcSarun.siluvery@linux.intel.com  * @engine_mask: mask representing engines that are hung
271087c390b6SMichel Thierry  * @fmt: Error message format string
271187c390b6SMichel Thierry  *
2712aafd8581SJavier Martinez Canillas  * Do some basic checking of register state at error time and
271335aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
271435aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
271535aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
271635aed2e6SChris Wilson  * of a ring dump etc.).
271735aed2e6SChris Wilson  */
2718c033666aSChris Wilson void i915_handle_error(struct drm_i915_private *dev_priv,
2719c033666aSChris Wilson 		       u32 engine_mask,
272058174462SMika Kuoppala 		       const char *fmt, ...)
272135aed2e6SChris Wilson {
272258174462SMika Kuoppala 	va_list args;
272358174462SMika Kuoppala 	char error_msg[80];
272435aed2e6SChris Wilson 
272558174462SMika Kuoppala 	va_start(args, fmt);
272658174462SMika Kuoppala 	vscnprintf(error_msg, sizeof(error_msg), fmt, args);
272758174462SMika Kuoppala 	va_end(args);
272858174462SMika Kuoppala 
27291604a86dSChris Wilson 	/*
27301604a86dSChris Wilson 	 * In most cases it's guaranteed that we get here with an RPM
27311604a86dSChris Wilson 	 * reference held, for example because there is a pending GPU
27321604a86dSChris Wilson 	 * request that won't finish until the reset is done. This
27331604a86dSChris Wilson 	 * isn't the case at least when we get here by doing a
27341604a86dSChris Wilson 	 * simulated reset via debugfs, so get an RPM reference.
27351604a86dSChris Wilson 	 */
27361604a86dSChris Wilson 	intel_runtime_pm_get(dev_priv);
27371604a86dSChris Wilson 
2738c033666aSChris Wilson 	i915_capture_error_state(dev_priv, engine_mask, error_msg);
2739eaa14c24SChris Wilson 	i915_clear_error_registers(dev_priv);
27408a905236SJesse Barnes 
27418af29b0cSChris Wilson 	if (!engine_mask)
27421604a86dSChris Wilson 		goto out;
27438af29b0cSChris Wilson 
27448c185ecaSChris Wilson 	if (test_and_set_bit(I915_RESET_BACKOFF,
27458af29b0cSChris Wilson 			     &dev_priv->gpu_error.flags))
27461604a86dSChris Wilson 		goto out;
2747ba1234d1SBen Gamari 
2748c033666aSChris Wilson 	i915_reset_and_wakeup(dev_priv);
27491604a86dSChris Wilson 
27501604a86dSChris Wilson out:
27511604a86dSChris Wilson 	intel_runtime_pm_put(dev_priv);
27528a905236SJesse Barnes }
27538a905236SJesse Barnes 
275442f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
275542f52ef8SKeith Packard  * we use as a pipe index
275642f52ef8SKeith Packard  */
275786e83e35SChris Wilson static int i8xx_enable_vblank(struct drm_device *dev, unsigned int pipe)
27580a3e67a4SJesse Barnes {
2759fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2760e9d21d7fSKeith Packard 	unsigned long irqflags;
276171e0ffa5SJesse Barnes 
27621ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
276386e83e35SChris Wilson 	i915_enable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_STATUS);
276486e83e35SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
276586e83e35SChris Wilson 
276686e83e35SChris Wilson 	return 0;
276786e83e35SChris Wilson }
276886e83e35SChris Wilson 
276986e83e35SChris Wilson static int i965_enable_vblank(struct drm_device *dev, unsigned int pipe)
277086e83e35SChris Wilson {
277186e83e35SChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
277286e83e35SChris Wilson 	unsigned long irqflags;
277386e83e35SChris Wilson 
277486e83e35SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
27757c463586SKeith Packard 	i915_enable_pipestat(dev_priv, pipe,
2776755e9019SImre Deak 			     PIPE_START_VBLANK_INTERRUPT_STATUS);
27771ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
27788692d00eSChris Wilson 
27790a3e67a4SJesse Barnes 	return 0;
27800a3e67a4SJesse Barnes }
27810a3e67a4SJesse Barnes 
278288e72717SThierry Reding static int ironlake_enable_vblank(struct drm_device *dev, unsigned int pipe)
2783f796cf8fSJesse Barnes {
2784fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2785f796cf8fSJesse Barnes 	unsigned long irqflags;
278655b8f2a7STvrtko Ursulin 	uint32_t bit = INTEL_GEN(dev_priv) >= 7 ?
278786e83e35SChris Wilson 		DE_PIPE_VBLANK_IVB(pipe) : DE_PIPE_VBLANK(pipe);
2788f796cf8fSJesse Barnes 
2789f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2790fbdedaeaSVille Syrjälä 	ilk_enable_display_irq(dev_priv, bit);
2791b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2792b1f14ad0SJesse Barnes 
2793b1f14ad0SJesse Barnes 	return 0;
2794b1f14ad0SJesse Barnes }
2795b1f14ad0SJesse Barnes 
279688e72717SThierry Reding static int gen8_enable_vblank(struct drm_device *dev, unsigned int pipe)
2797abd58f01SBen Widawsky {
2798fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2799abd58f01SBen Widawsky 	unsigned long irqflags;
2800abd58f01SBen Widawsky 
2801abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2802013d3752SVille Syrjälä 	bdw_enable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
2803abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2804013d3752SVille Syrjälä 
2805abd58f01SBen Widawsky 	return 0;
2806abd58f01SBen Widawsky }
2807abd58f01SBen Widawsky 
280842f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
280942f52ef8SKeith Packard  * we use as a pipe index
281042f52ef8SKeith Packard  */
281186e83e35SChris Wilson static void i8xx_disable_vblank(struct drm_device *dev, unsigned int pipe)
281286e83e35SChris Wilson {
281386e83e35SChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
281486e83e35SChris Wilson 	unsigned long irqflags;
281586e83e35SChris Wilson 
281686e83e35SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
281786e83e35SChris Wilson 	i915_disable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_STATUS);
281886e83e35SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
281986e83e35SChris Wilson }
282086e83e35SChris Wilson 
282186e83e35SChris Wilson static void i965_disable_vblank(struct drm_device *dev, unsigned int pipe)
28220a3e67a4SJesse Barnes {
2823fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2824e9d21d7fSKeith Packard 	unsigned long irqflags;
28250a3e67a4SJesse Barnes 
28261ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
28277c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
2828755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
28291ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
28300a3e67a4SJesse Barnes }
28310a3e67a4SJesse Barnes 
283288e72717SThierry Reding static void ironlake_disable_vblank(struct drm_device *dev, unsigned int pipe)
2833f796cf8fSJesse Barnes {
2834fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2835f796cf8fSJesse Barnes 	unsigned long irqflags;
283655b8f2a7STvrtko Ursulin 	uint32_t bit = INTEL_GEN(dev_priv) >= 7 ?
283786e83e35SChris Wilson 		DE_PIPE_VBLANK_IVB(pipe) : DE_PIPE_VBLANK(pipe);
2838f796cf8fSJesse Barnes 
2839f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2840fbdedaeaSVille Syrjälä 	ilk_disable_display_irq(dev_priv, bit);
2841b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2842b1f14ad0SJesse Barnes }
2843b1f14ad0SJesse Barnes 
284488e72717SThierry Reding static void gen8_disable_vblank(struct drm_device *dev, unsigned int pipe)
2845abd58f01SBen Widawsky {
2846fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2847abd58f01SBen Widawsky 	unsigned long irqflags;
2848abd58f01SBen Widawsky 
2849abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2850013d3752SVille Syrjälä 	bdw_disable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
2851abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2852abd58f01SBen Widawsky }
2853abd58f01SBen Widawsky 
2854b243f530STvrtko Ursulin static void ibx_irq_reset(struct drm_i915_private *dev_priv)
285591738a95SPaulo Zanoni {
28566e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
285791738a95SPaulo Zanoni 		return;
285891738a95SPaulo Zanoni 
2859f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(SDE);
2860105b122eSPaulo Zanoni 
28616e266956STvrtko Ursulin 	if (HAS_PCH_CPT(dev_priv) || HAS_PCH_LPT(dev_priv))
2862105b122eSPaulo Zanoni 		I915_WRITE(SERR_INT, 0xffffffff);
2863622364b6SPaulo Zanoni }
2864105b122eSPaulo Zanoni 
286591738a95SPaulo Zanoni /*
2866622364b6SPaulo Zanoni  * SDEIER is also touched by the interrupt handler to work around missed PCH
2867622364b6SPaulo Zanoni  * interrupts. Hence we can't update it after the interrupt handler is enabled -
2868622364b6SPaulo Zanoni  * instead we unconditionally enable all PCH interrupt sources here, but then
2869622364b6SPaulo Zanoni  * only unmask them as needed with SDEIMR.
2870622364b6SPaulo Zanoni  *
2871622364b6SPaulo Zanoni  * This function needs to be called before interrupts are enabled.
287291738a95SPaulo Zanoni  */
2873622364b6SPaulo Zanoni static void ibx_irq_pre_postinstall(struct drm_device *dev)
2874622364b6SPaulo Zanoni {
2875fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2876622364b6SPaulo Zanoni 
28776e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
2878622364b6SPaulo Zanoni 		return;
2879622364b6SPaulo Zanoni 
2880622364b6SPaulo Zanoni 	WARN_ON(I915_READ(SDEIER) != 0);
288191738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
288291738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
288391738a95SPaulo Zanoni }
288491738a95SPaulo Zanoni 
2885b243f530STvrtko Ursulin static void gen5_gt_irq_reset(struct drm_i915_private *dev_priv)
2886d18ea1b5SDaniel Vetter {
2887f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GT);
2888b243f530STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 6)
2889f86f3fb0SPaulo Zanoni 		GEN5_IRQ_RESET(GEN6_PM);
2890d18ea1b5SDaniel Vetter }
2891d18ea1b5SDaniel Vetter 
289270591a41SVille Syrjälä static void vlv_display_irq_reset(struct drm_i915_private *dev_priv)
289370591a41SVille Syrjälä {
289470591a41SVille Syrjälä 	enum pipe pipe;
289570591a41SVille Syrjälä 
289671b8b41dSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
289771b8b41dSVille Syrjälä 		I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
289871b8b41dSVille Syrjälä 	else
289971b8b41dSVille Syrjälä 		I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
290071b8b41dSVille Syrjälä 
2901ad22d106SVille Syrjälä 	i915_hotplug_interrupt_update_locked(dev_priv, 0xffffffff, 0);
290270591a41SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
290370591a41SVille Syrjälä 
2904ad22d106SVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
2905ad22d106SVille Syrjälä 		I915_WRITE(PIPESTAT(pipe),
2906ad22d106SVille Syrjälä 			   PIPE_FIFO_UNDERRUN_STATUS |
2907ad22d106SVille Syrjälä 			   PIPESTAT_INT_STATUS_MASK);
2908ad22d106SVille Syrjälä 		dev_priv->pipestat_irq_mask[pipe] = 0;
2909ad22d106SVille Syrjälä 	}
291070591a41SVille Syrjälä 
291170591a41SVille Syrjälä 	GEN5_IRQ_RESET(VLV_);
2912ad22d106SVille Syrjälä 	dev_priv->irq_mask = ~0;
291370591a41SVille Syrjälä }
291470591a41SVille Syrjälä 
29158bb61306SVille Syrjälä static void vlv_display_irq_postinstall(struct drm_i915_private *dev_priv)
29168bb61306SVille Syrjälä {
29178bb61306SVille Syrjälä 	u32 pipestat_mask;
29189ab981f2SVille Syrjälä 	u32 enable_mask;
29198bb61306SVille Syrjälä 	enum pipe pipe;
2920eef57324SJerome Anand 	u32 val;
29218bb61306SVille Syrjälä 
29228bb61306SVille Syrjälä 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
29238bb61306SVille Syrjälä 			PIPE_CRC_DONE_INTERRUPT_STATUS;
29248bb61306SVille Syrjälä 
29258bb61306SVille Syrjälä 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
29268bb61306SVille Syrjälä 	for_each_pipe(dev_priv, pipe)
29278bb61306SVille Syrjälä 		i915_enable_pipestat(dev_priv, pipe, pipestat_mask);
29288bb61306SVille Syrjälä 
29299ab981f2SVille Syrjälä 	enable_mask = I915_DISPLAY_PORT_INTERRUPT |
29308bb61306SVille Syrjälä 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
29318bb61306SVille Syrjälä 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
29328bb61306SVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
29339ab981f2SVille Syrjälä 		enable_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
29346b7eafc1SVille Syrjälä 
29356b7eafc1SVille Syrjälä 	WARN_ON(dev_priv->irq_mask != ~0);
29366b7eafc1SVille Syrjälä 
2937eef57324SJerome Anand 	val = (I915_LPE_PIPE_A_INTERRUPT |
2938eef57324SJerome Anand 		I915_LPE_PIPE_B_INTERRUPT |
2939eef57324SJerome Anand 		I915_LPE_PIPE_C_INTERRUPT);
2940eef57324SJerome Anand 
2941eef57324SJerome Anand 	enable_mask |= val;
2942eef57324SJerome Anand 
29439ab981f2SVille Syrjälä 	dev_priv->irq_mask = ~enable_mask;
29448bb61306SVille Syrjälä 
29459ab981f2SVille Syrjälä 	GEN5_IRQ_INIT(VLV_, dev_priv->irq_mask, enable_mask);
29468bb61306SVille Syrjälä }
29478bb61306SVille Syrjälä 
29488bb61306SVille Syrjälä /* drm_dma.h hooks
29498bb61306SVille Syrjälä */
29508bb61306SVille Syrjälä static void ironlake_irq_reset(struct drm_device *dev)
29518bb61306SVille Syrjälä {
2952fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
29538bb61306SVille Syrjälä 
29548bb61306SVille Syrjälä 	I915_WRITE(HWSTAM, 0xffffffff);
29558bb61306SVille Syrjälä 
29568bb61306SVille Syrjälä 	GEN5_IRQ_RESET(DE);
29575db94019STvrtko Ursulin 	if (IS_GEN7(dev_priv))
29588bb61306SVille Syrjälä 		I915_WRITE(GEN7_ERR_INT, 0xffffffff);
29598bb61306SVille Syrjälä 
2960b243f530STvrtko Ursulin 	gen5_gt_irq_reset(dev_priv);
29618bb61306SVille Syrjälä 
2962b243f530STvrtko Ursulin 	ibx_irq_reset(dev_priv);
29638bb61306SVille Syrjälä }
29648bb61306SVille Syrjälä 
29657e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
29667e231dbeSJesse Barnes {
2967fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
29687e231dbeSJesse Barnes 
296934c7b8a7SVille Syrjälä 	I915_WRITE(VLV_MASTER_IER, 0);
297034c7b8a7SVille Syrjälä 	POSTING_READ(VLV_MASTER_IER);
297134c7b8a7SVille Syrjälä 
2972b243f530STvrtko Ursulin 	gen5_gt_irq_reset(dev_priv);
29737e231dbeSJesse Barnes 
2974ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
29759918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
297670591a41SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
2977ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
29787e231dbeSJesse Barnes }
29797e231dbeSJesse Barnes 
2980d6e3cca3SDaniel Vetter static void gen8_gt_irq_reset(struct drm_i915_private *dev_priv)
2981d6e3cca3SDaniel Vetter {
2982d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 0);
2983d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 1);
2984d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 2);
2985d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 3);
2986d6e3cca3SDaniel Vetter }
2987d6e3cca3SDaniel Vetter 
2988823f6b38SPaulo Zanoni static void gen8_irq_reset(struct drm_device *dev)
2989abd58f01SBen Widawsky {
2990fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2991abd58f01SBen Widawsky 	int pipe;
2992abd58f01SBen Widawsky 
2993abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
2994abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
2995abd58f01SBen Widawsky 
2996d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
2997abd58f01SBen Widawsky 
2998055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
2999f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3000813bde43SPaulo Zanoni 						   POWER_DOMAIN_PIPE(pipe)))
3001f86f3fb0SPaulo Zanoni 			GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3002abd58f01SBen Widawsky 
3003f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_PORT_);
3004f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_MISC_);
3005f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_PCU_);
3006abd58f01SBen Widawsky 
30076e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
3008b243f530STvrtko Ursulin 		ibx_irq_reset(dev_priv);
3009abd58f01SBen Widawsky }
3010abd58f01SBen Widawsky 
30114c6c03beSDamien Lespiau void gen8_irq_power_well_post_enable(struct drm_i915_private *dev_priv,
30124c6c03beSDamien Lespiau 				     unsigned int pipe_mask)
3013d49bdb0eSPaulo Zanoni {
30141180e206SPaulo Zanoni 	uint32_t extra_ier = GEN8_PIPE_VBLANK | GEN8_PIPE_FIFO_UNDERRUN;
30156831f3e3SVille Syrjälä 	enum pipe pipe;
3016d49bdb0eSPaulo Zanoni 
301713321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
30186831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
30196831f3e3SVille Syrjälä 		GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
30206831f3e3SVille Syrjälä 				  dev_priv->de_irq_mask[pipe],
30216831f3e3SVille Syrjälä 				  ~dev_priv->de_irq_mask[pipe] | extra_ier);
302213321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3023d49bdb0eSPaulo Zanoni }
3024d49bdb0eSPaulo Zanoni 
3025aae8ba84SVille Syrjälä void gen8_irq_power_well_pre_disable(struct drm_i915_private *dev_priv,
3026aae8ba84SVille Syrjälä 				     unsigned int pipe_mask)
3027aae8ba84SVille Syrjälä {
30286831f3e3SVille Syrjälä 	enum pipe pipe;
30296831f3e3SVille Syrjälä 
3030aae8ba84SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
30316831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
30326831f3e3SVille Syrjälä 		GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3033aae8ba84SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3034aae8ba84SVille Syrjälä 
3035aae8ba84SVille Syrjälä 	/* make sure we're done processing display irqs */
303691c8a326SChris Wilson 	synchronize_irq(dev_priv->drm.irq);
3037aae8ba84SVille Syrjälä }
3038aae8ba84SVille Syrjälä 
303943f328d7SVille Syrjälä static void cherryview_irq_preinstall(struct drm_device *dev)
304043f328d7SVille Syrjälä {
3041fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
304243f328d7SVille Syrjälä 
304343f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
304443f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
304543f328d7SVille Syrjälä 
3046d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
304743f328d7SVille Syrjälä 
304843f328d7SVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
304943f328d7SVille Syrjälä 
3050ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
30519918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
305270591a41SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3053ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
305443f328d7SVille Syrjälä }
305543f328d7SVille Syrjälä 
305691d14251STvrtko Ursulin static u32 intel_hpd_enabled_irqs(struct drm_i915_private *dev_priv,
305787a02106SVille Syrjälä 				  const u32 hpd[HPD_NUM_PINS])
305887a02106SVille Syrjälä {
305987a02106SVille Syrjälä 	struct intel_encoder *encoder;
306087a02106SVille Syrjälä 	u32 enabled_irqs = 0;
306187a02106SVille Syrjälä 
306291c8a326SChris Wilson 	for_each_intel_encoder(&dev_priv->drm, encoder)
306387a02106SVille Syrjälä 		if (dev_priv->hotplug.stats[encoder->hpd_pin].state == HPD_ENABLED)
306487a02106SVille Syrjälä 			enabled_irqs |= hpd[encoder->hpd_pin];
306587a02106SVille Syrjälä 
306687a02106SVille Syrjälä 	return enabled_irqs;
306787a02106SVille Syrjälä }
306887a02106SVille Syrjälä 
30691a56b1a2SImre Deak static void ibx_hpd_detection_setup(struct drm_i915_private *dev_priv)
30701a56b1a2SImre Deak {
30711a56b1a2SImre Deak 	u32 hotplug;
30721a56b1a2SImre Deak 
30731a56b1a2SImre Deak 	/*
30741a56b1a2SImre Deak 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
30751a56b1a2SImre Deak 	 * duration to 2ms (which is the minimum in the Display Port spec).
30761a56b1a2SImre Deak 	 * The pulse duration bits are reserved on LPT+.
30771a56b1a2SImre Deak 	 */
30781a56b1a2SImre Deak 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
30791a56b1a2SImre Deak 	hotplug &= ~(PORTB_PULSE_DURATION_MASK |
30801a56b1a2SImre Deak 		     PORTC_PULSE_DURATION_MASK |
30811a56b1a2SImre Deak 		     PORTD_PULSE_DURATION_MASK);
30821a56b1a2SImre Deak 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
30831a56b1a2SImre Deak 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
30841a56b1a2SImre Deak 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
30851a56b1a2SImre Deak 	/*
30861a56b1a2SImre Deak 	 * When CPU and PCH are on the same package, port A
30871a56b1a2SImre Deak 	 * HPD must be enabled in both north and south.
30881a56b1a2SImre Deak 	 */
30891a56b1a2SImre Deak 	if (HAS_PCH_LPT_LP(dev_priv))
30901a56b1a2SImre Deak 		hotplug |= PORTA_HOTPLUG_ENABLE;
30911a56b1a2SImre Deak 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
30921a56b1a2SImre Deak }
30931a56b1a2SImre Deak 
309491d14251STvrtko Ursulin static void ibx_hpd_irq_setup(struct drm_i915_private *dev_priv)
309582a28bcfSDaniel Vetter {
30961a56b1a2SImre Deak 	u32 hotplug_irqs, enabled_irqs;
309782a28bcfSDaniel Vetter 
309891d14251STvrtko Ursulin 	if (HAS_PCH_IBX(dev_priv)) {
3099fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
310091d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ibx);
310182a28bcfSDaniel Vetter 	} else {
3102fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
310391d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_cpt);
310482a28bcfSDaniel Vetter 	}
310582a28bcfSDaniel Vetter 
3106fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
310782a28bcfSDaniel Vetter 
31081a56b1a2SImre Deak 	ibx_hpd_detection_setup(dev_priv);
31096dbf30ceSVille Syrjälä }
311026951cafSXiong Zhang 
31112a57d9ccSImre Deak static void spt_hpd_detection_setup(struct drm_i915_private *dev_priv)
31122a57d9ccSImre Deak {
31132a57d9ccSImre Deak 	u32 hotplug;
31142a57d9ccSImre Deak 
31152a57d9ccSImre Deak 	/* Enable digital hotplug on the PCH */
31162a57d9ccSImre Deak 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
31172a57d9ccSImre Deak 	hotplug |= PORTA_HOTPLUG_ENABLE |
31182a57d9ccSImre Deak 		   PORTB_HOTPLUG_ENABLE |
31192a57d9ccSImre Deak 		   PORTC_HOTPLUG_ENABLE |
31202a57d9ccSImre Deak 		   PORTD_HOTPLUG_ENABLE;
31212a57d9ccSImre Deak 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
31222a57d9ccSImre Deak 
31232a57d9ccSImre Deak 	hotplug = I915_READ(PCH_PORT_HOTPLUG2);
31242a57d9ccSImre Deak 	hotplug |= PORTE_HOTPLUG_ENABLE;
31252a57d9ccSImre Deak 	I915_WRITE(PCH_PORT_HOTPLUG2, hotplug);
31262a57d9ccSImre Deak }
31272a57d9ccSImre Deak 
312891d14251STvrtko Ursulin static void spt_hpd_irq_setup(struct drm_i915_private *dev_priv)
31296dbf30ceSVille Syrjälä {
31302a57d9ccSImre Deak 	u32 hotplug_irqs, enabled_irqs;
31316dbf30ceSVille Syrjälä 
31326dbf30ceSVille Syrjälä 	hotplug_irqs = SDE_HOTPLUG_MASK_SPT;
313391d14251STvrtko Ursulin 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_spt);
31346dbf30ceSVille Syrjälä 
31356dbf30ceSVille Syrjälä 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
31366dbf30ceSVille Syrjälä 
31372a57d9ccSImre Deak 	spt_hpd_detection_setup(dev_priv);
313826951cafSXiong Zhang }
31397fe0b973SKeith Packard 
31401a56b1a2SImre Deak static void ilk_hpd_detection_setup(struct drm_i915_private *dev_priv)
31411a56b1a2SImre Deak {
31421a56b1a2SImre Deak 	u32 hotplug;
31431a56b1a2SImre Deak 
31441a56b1a2SImre Deak 	/*
31451a56b1a2SImre Deak 	 * Enable digital hotplug on the CPU, and configure the DP short pulse
31461a56b1a2SImre Deak 	 * duration to 2ms (which is the minimum in the Display Port spec)
31471a56b1a2SImre Deak 	 * The pulse duration bits are reserved on HSW+.
31481a56b1a2SImre Deak 	 */
31491a56b1a2SImre Deak 	hotplug = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
31501a56b1a2SImre Deak 	hotplug &= ~DIGITAL_PORTA_PULSE_DURATION_MASK;
31511a56b1a2SImre Deak 	hotplug |= DIGITAL_PORTA_HOTPLUG_ENABLE |
31521a56b1a2SImre Deak 		   DIGITAL_PORTA_PULSE_DURATION_2ms;
31531a56b1a2SImre Deak 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, hotplug);
31541a56b1a2SImre Deak }
31551a56b1a2SImre Deak 
315691d14251STvrtko Ursulin static void ilk_hpd_irq_setup(struct drm_i915_private *dev_priv)
3157e4ce95aaSVille Syrjälä {
31581a56b1a2SImre Deak 	u32 hotplug_irqs, enabled_irqs;
3159e4ce95aaSVille Syrjälä 
316091d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 8) {
31613a3b3c7dSVille Syrjälä 		hotplug_irqs = GEN8_PORT_DP_A_HOTPLUG;
316291d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_bdw);
31633a3b3c7dSVille Syrjälä 
31643a3b3c7dSVille Syrjälä 		bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
316591d14251STvrtko Ursulin 	} else if (INTEL_GEN(dev_priv) >= 7) {
316623bb4cb5SVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG_IVB;
316791d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ivb);
31683a3b3c7dSVille Syrjälä 
31693a3b3c7dSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
317023bb4cb5SVille Syrjälä 	} else {
3171e4ce95aaSVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG;
317291d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ilk);
3173e4ce95aaSVille Syrjälä 
3174e4ce95aaSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
31753a3b3c7dSVille Syrjälä 	}
3176e4ce95aaSVille Syrjälä 
31771a56b1a2SImre Deak 	ilk_hpd_detection_setup(dev_priv);
3178e4ce95aaSVille Syrjälä 
317991d14251STvrtko Ursulin 	ibx_hpd_irq_setup(dev_priv);
3180e4ce95aaSVille Syrjälä }
3181e4ce95aaSVille Syrjälä 
31822a57d9ccSImre Deak static void __bxt_hpd_detection_setup(struct drm_i915_private *dev_priv,
31832a57d9ccSImre Deak 				      u32 enabled_irqs)
3184e0a20ad7SShashank Sharma {
31852a57d9ccSImre Deak 	u32 hotplug;
3186e0a20ad7SShashank Sharma 
3187a52bb15bSVille Syrjälä 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
31882a57d9ccSImre Deak 	hotplug |= PORTA_HOTPLUG_ENABLE |
31892a57d9ccSImre Deak 		   PORTB_HOTPLUG_ENABLE |
31902a57d9ccSImre Deak 		   PORTC_HOTPLUG_ENABLE;
3191d252bf68SShubhangi Shrivastava 
3192d252bf68SShubhangi Shrivastava 	DRM_DEBUG_KMS("Invert bit setting: hp_ctl:%x hp_port:%x\n",
3193d252bf68SShubhangi Shrivastava 		      hotplug, enabled_irqs);
3194d252bf68SShubhangi Shrivastava 	hotplug &= ~BXT_DDI_HPD_INVERT_MASK;
3195d252bf68SShubhangi Shrivastava 
3196d252bf68SShubhangi Shrivastava 	/*
3197d252bf68SShubhangi Shrivastava 	 * For BXT invert bit has to be set based on AOB design
3198d252bf68SShubhangi Shrivastava 	 * for HPD detection logic, update it based on VBT fields.
3199d252bf68SShubhangi Shrivastava 	 */
3200d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIA) &&
3201d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_A))
3202d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIA_HPD_INVERT;
3203d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIB) &&
3204d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_B))
3205d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIB_HPD_INVERT;
3206d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIC) &&
3207d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_C))
3208d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIC_HPD_INVERT;
3209d252bf68SShubhangi Shrivastava 
3210a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
3211e0a20ad7SShashank Sharma }
3212e0a20ad7SShashank Sharma 
32132a57d9ccSImre Deak static void bxt_hpd_detection_setup(struct drm_i915_private *dev_priv)
32142a57d9ccSImre Deak {
32152a57d9ccSImre Deak 	__bxt_hpd_detection_setup(dev_priv, BXT_DE_PORT_HOTPLUG_MASK);
32162a57d9ccSImre Deak }
32172a57d9ccSImre Deak 
32182a57d9ccSImre Deak static void bxt_hpd_irq_setup(struct drm_i915_private *dev_priv)
32192a57d9ccSImre Deak {
32202a57d9ccSImre Deak 	u32 hotplug_irqs, enabled_irqs;
32212a57d9ccSImre Deak 
32222a57d9ccSImre Deak 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_bxt);
32232a57d9ccSImre Deak 	hotplug_irqs = BXT_DE_PORT_HOTPLUG_MASK;
32242a57d9ccSImre Deak 
32252a57d9ccSImre Deak 	bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
32262a57d9ccSImre Deak 
32272a57d9ccSImre Deak 	__bxt_hpd_detection_setup(dev_priv, enabled_irqs);
32282a57d9ccSImre Deak }
32292a57d9ccSImre Deak 
3230d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
3231d46da437SPaulo Zanoni {
3232fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
323382a28bcfSDaniel Vetter 	u32 mask;
3234d46da437SPaulo Zanoni 
32356e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
3236692a04cfSDaniel Vetter 		return;
3237692a04cfSDaniel Vetter 
32386e266956STvrtko Ursulin 	if (HAS_PCH_IBX(dev_priv))
32395c673b60SDaniel Vetter 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
3240105b122eSPaulo Zanoni 	else
32415c673b60SDaniel Vetter 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
32428664281bSPaulo Zanoni 
3243b51a2842SVille Syrjälä 	gen5_assert_iir_is_zero(dev_priv, SDEIIR);
3244d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
32452a57d9ccSImre Deak 
32462a57d9ccSImre Deak 	if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
32472a57d9ccSImre Deak 	    HAS_PCH_LPT(dev_priv))
32481a56b1a2SImre Deak 		ibx_hpd_detection_setup(dev_priv);
32492a57d9ccSImre Deak 	else
32502a57d9ccSImre Deak 		spt_hpd_detection_setup(dev_priv);
3251d46da437SPaulo Zanoni }
3252d46da437SPaulo Zanoni 
32530a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
32540a9a8c91SDaniel Vetter {
3255fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
32560a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
32570a9a8c91SDaniel Vetter 
32580a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
32590a9a8c91SDaniel Vetter 
32600a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
32613c9192bcSTvrtko Ursulin 	if (HAS_L3_DPF(dev_priv)) {
32620a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
3263772c2a51STvrtko Ursulin 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev_priv);
3264772c2a51STvrtko Ursulin 		gt_irqs |= GT_PARITY_ERROR(dev_priv);
32650a9a8c91SDaniel Vetter 	}
32660a9a8c91SDaniel Vetter 
32670a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
32685db94019STvrtko Ursulin 	if (IS_GEN5(dev_priv)) {
3269f8973c21SChris Wilson 		gt_irqs |= ILK_BSD_USER_INTERRUPT;
32700a9a8c91SDaniel Vetter 	} else {
32710a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
32720a9a8c91SDaniel Vetter 	}
32730a9a8c91SDaniel Vetter 
327435079899SPaulo Zanoni 	GEN5_IRQ_INIT(GT, dev_priv->gt_irq_mask, gt_irqs);
32750a9a8c91SDaniel Vetter 
3276b243f530STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 6) {
327778e68d36SImre Deak 		/*
327878e68d36SImre Deak 		 * RPS interrupts will get enabled/disabled on demand when RPS
327978e68d36SImre Deak 		 * itself is enabled/disabled.
328078e68d36SImre Deak 		 */
3281f4e9af4fSAkash Goel 		if (HAS_VEBOX(dev_priv)) {
32820a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
3283f4e9af4fSAkash Goel 			dev_priv->pm_ier |= PM_VEBOX_USER_INTERRUPT;
3284f4e9af4fSAkash Goel 		}
32850a9a8c91SDaniel Vetter 
3286f4e9af4fSAkash Goel 		dev_priv->pm_imr = 0xffffffff;
3287f4e9af4fSAkash Goel 		GEN5_IRQ_INIT(GEN6_PM, dev_priv->pm_imr, pm_irqs);
32880a9a8c91SDaniel Vetter 	}
32890a9a8c91SDaniel Vetter }
32900a9a8c91SDaniel Vetter 
3291f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
3292036a4a7dSZhenyu Wang {
3293fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
32948e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
32958e76f8dcSPaulo Zanoni 
3296b243f530STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 7) {
32978e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
32988e76f8dcSPaulo Zanoni 				DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
32998e76f8dcSPaulo Zanoni 				DE_PLANEB_FLIP_DONE_IVB |
33005c673b60SDaniel Vetter 				DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB);
33018e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
330223bb4cb5SVille Syrjälä 			      DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB |
330323bb4cb5SVille Syrjälä 			      DE_DP_A_HOTPLUG_IVB);
33048e76f8dcSPaulo Zanoni 	} else {
33058e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3306ce99c256SDaniel Vetter 				DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
33075b3a856bSDaniel Vetter 				DE_AUX_CHANNEL_A |
33085b3a856bSDaniel Vetter 				DE_PIPEB_CRC_DONE | DE_PIPEA_CRC_DONE |
33095b3a856bSDaniel Vetter 				DE_POISON);
3310e4ce95aaSVille Syrjälä 		extra_mask = (DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
3311e4ce95aaSVille Syrjälä 			      DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN |
3312e4ce95aaSVille Syrjälä 			      DE_DP_A_HOTPLUG);
33138e76f8dcSPaulo Zanoni 	}
3314036a4a7dSZhenyu Wang 
33151ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3316036a4a7dSZhenyu Wang 
33170c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xeffe);
33180c841212SPaulo Zanoni 
3319622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3320622364b6SPaulo Zanoni 
332135079899SPaulo Zanoni 	GEN5_IRQ_INIT(DE, dev_priv->irq_mask, display_mask | extra_mask);
3322036a4a7dSZhenyu Wang 
33230a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
3324036a4a7dSZhenyu Wang 
33251a56b1a2SImre Deak 	ilk_hpd_detection_setup(dev_priv);
33261a56b1a2SImre Deak 
3327d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
33287fe0b973SKeith Packard 
332950a0bc90STvrtko Ursulin 	if (IS_IRONLAKE_M(dev_priv)) {
33306005ce42SDaniel Vetter 		/* Enable PCU event interrupts
33316005ce42SDaniel Vetter 		 *
33326005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
33334bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
33344bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
3335d6207435SDaniel Vetter 		spin_lock_irq(&dev_priv->irq_lock);
3336fbdedaeaSVille Syrjälä 		ilk_enable_display_irq(dev_priv, DE_PCU_EVENT);
3337d6207435SDaniel Vetter 		spin_unlock_irq(&dev_priv->irq_lock);
3338f97108d1SJesse Barnes 	}
3339f97108d1SJesse Barnes 
3340036a4a7dSZhenyu Wang 	return 0;
3341036a4a7dSZhenyu Wang }
3342036a4a7dSZhenyu Wang 
3343f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3344f8b79e58SImre Deak {
334567520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
3346f8b79e58SImre Deak 
3347f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3348f8b79e58SImre Deak 		return;
3349f8b79e58SImre Deak 
3350f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3351f8b79e58SImre Deak 
3352d6c69803SVille Syrjälä 	if (intel_irqs_enabled(dev_priv)) {
3353d6c69803SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3354ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3355f8b79e58SImre Deak 	}
3356d6c69803SVille Syrjälä }
3357f8b79e58SImre Deak 
3358f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3359f8b79e58SImre Deak {
336067520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
3361f8b79e58SImre Deak 
3362f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3363f8b79e58SImre Deak 		return;
3364f8b79e58SImre Deak 
3365f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3366f8b79e58SImre Deak 
3367950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3368ad22d106SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3369f8b79e58SImre Deak }
3370f8b79e58SImre Deak 
33710e6c9a9eSVille Syrjälä 
33720e6c9a9eSVille Syrjälä static int valleyview_irq_postinstall(struct drm_device *dev)
33730e6c9a9eSVille Syrjälä {
3374fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
33750e6c9a9eSVille Syrjälä 
33760a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
33777e231dbeSJesse Barnes 
3378ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
33799918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3380ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3381ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3382ad22d106SVille Syrjälä 
33837e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
338434c7b8a7SVille Syrjälä 	POSTING_READ(VLV_MASTER_IER);
338520afbda2SDaniel Vetter 
338620afbda2SDaniel Vetter 	return 0;
338720afbda2SDaniel Vetter }
338820afbda2SDaniel Vetter 
3389abd58f01SBen Widawsky static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
3390abd58f01SBen Widawsky {
3391abd58f01SBen Widawsky 	/* These are interrupts we'll toggle with the ring mask register */
3392abd58f01SBen Widawsky 	uint32_t gt_interrupts[] = {
3393abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
339473d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
339573d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT |
339673d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT,
3397abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
339873d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
339973d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT |
340073d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT,
3401abd58f01SBen Widawsky 		0,
340273d477f6SOscar Mateo 		GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT |
340373d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT
3404abd58f01SBen Widawsky 		};
3405abd58f01SBen Widawsky 
340698735739STvrtko Ursulin 	if (HAS_L3_DPF(dev_priv))
340798735739STvrtko Ursulin 		gt_interrupts[0] |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
340898735739STvrtko Ursulin 
3409f4e9af4fSAkash Goel 	dev_priv->pm_ier = 0x0;
3410f4e9af4fSAkash Goel 	dev_priv->pm_imr = ~dev_priv->pm_ier;
34119a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 0, ~gt_interrupts[0], gt_interrupts[0]);
34129a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 1, ~gt_interrupts[1], gt_interrupts[1]);
341378e68d36SImre Deak 	/*
341478e68d36SImre Deak 	 * RPS interrupts will get enabled/disabled on demand when RPS itself
341526705e20SSagar Arun Kamble 	 * is enabled/disabled. Same wil be the case for GuC interrupts.
341678e68d36SImre Deak 	 */
3417f4e9af4fSAkash Goel 	GEN8_IRQ_INIT_NDX(GT, 2, dev_priv->pm_imr, dev_priv->pm_ier);
34189a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 3, ~gt_interrupts[3], gt_interrupts[3]);
3419abd58f01SBen Widawsky }
3420abd58f01SBen Widawsky 
3421abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3422abd58f01SBen Widawsky {
3423770de83dSDamien Lespiau 	uint32_t de_pipe_masked = GEN8_PIPE_CDCLK_CRC_DONE;
3424770de83dSDamien Lespiau 	uint32_t de_pipe_enables;
34253a3b3c7dSVille Syrjälä 	u32 de_port_masked = GEN8_AUX_CHANNEL_A;
34263a3b3c7dSVille Syrjälä 	u32 de_port_enables;
342711825b0dSVille Syrjälä 	u32 de_misc_masked = GEN8_DE_MISC_GSE;
34283a3b3c7dSVille Syrjälä 	enum pipe pipe;
3429770de83dSDamien Lespiau 
3430b4834a50SRodrigo Vivi 	if (INTEL_INFO(dev_priv)->gen >= 9) {
3431770de83dSDamien Lespiau 		de_pipe_masked |= GEN9_PIPE_PLANE1_FLIP_DONE |
3432770de83dSDamien Lespiau 				  GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
34333a3b3c7dSVille Syrjälä 		de_port_masked |= GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
343488e04703SJesse Barnes 				  GEN9_AUX_CHANNEL_D;
3435cc3f90f0SAnder Conselvan de Oliveira 		if (IS_GEN9_LP(dev_priv))
34363a3b3c7dSVille Syrjälä 			de_port_masked |= BXT_DE_PORT_GMBUS;
34373a3b3c7dSVille Syrjälä 	} else {
3438770de83dSDamien Lespiau 		de_pipe_masked |= GEN8_PIPE_PRIMARY_FLIP_DONE |
3439770de83dSDamien Lespiau 				  GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
34403a3b3c7dSVille Syrjälä 	}
3441770de83dSDamien Lespiau 
3442770de83dSDamien Lespiau 	de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
3443770de83dSDamien Lespiau 					   GEN8_PIPE_FIFO_UNDERRUN;
3444770de83dSDamien Lespiau 
34453a3b3c7dSVille Syrjälä 	de_port_enables = de_port_masked;
3446cc3f90f0SAnder Conselvan de Oliveira 	if (IS_GEN9_LP(dev_priv))
3447a52bb15bSVille Syrjälä 		de_port_enables |= BXT_DE_PORT_HOTPLUG_MASK;
3448a52bb15bSVille Syrjälä 	else if (IS_BROADWELL(dev_priv))
34493a3b3c7dSVille Syrjälä 		de_port_enables |= GEN8_PORT_DP_A_HOTPLUG;
34503a3b3c7dSVille Syrjälä 
345113b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_A] = ~de_pipe_masked;
345213b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_B] = ~de_pipe_masked;
345313b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_C] = ~de_pipe_masked;
3454abd58f01SBen Widawsky 
3455055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3456f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3457813bde43SPaulo Zanoni 				POWER_DOMAIN_PIPE(pipe)))
3458813bde43SPaulo Zanoni 			GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
3459813bde43SPaulo Zanoni 					  dev_priv->de_irq_mask[pipe],
346035079899SPaulo Zanoni 					  de_pipe_enables);
3461abd58f01SBen Widawsky 
34623a3b3c7dSVille Syrjälä 	GEN5_IRQ_INIT(GEN8_DE_PORT_, ~de_port_masked, de_port_enables);
346311825b0dSVille Syrjälä 	GEN5_IRQ_INIT(GEN8_DE_MISC_, ~de_misc_masked, de_misc_masked);
34642a57d9ccSImre Deak 
34652a57d9ccSImre Deak 	if (IS_GEN9_LP(dev_priv))
34662a57d9ccSImre Deak 		bxt_hpd_detection_setup(dev_priv);
34671a56b1a2SImre Deak 	else if (IS_BROADWELL(dev_priv))
34681a56b1a2SImre Deak 		ilk_hpd_detection_setup(dev_priv);
3469abd58f01SBen Widawsky }
3470abd58f01SBen Widawsky 
3471abd58f01SBen Widawsky static int gen8_irq_postinstall(struct drm_device *dev)
3472abd58f01SBen Widawsky {
3473fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3474abd58f01SBen Widawsky 
34756e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
3476622364b6SPaulo Zanoni 		ibx_irq_pre_postinstall(dev);
3477622364b6SPaulo Zanoni 
3478abd58f01SBen Widawsky 	gen8_gt_irq_postinstall(dev_priv);
3479abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3480abd58f01SBen Widawsky 
34816e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
3482abd58f01SBen Widawsky 		ibx_irq_postinstall(dev);
3483abd58f01SBen Widawsky 
3484e5328c43SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
3485abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3486abd58f01SBen Widawsky 
3487abd58f01SBen Widawsky 	return 0;
3488abd58f01SBen Widawsky }
3489abd58f01SBen Widawsky 
349043f328d7SVille Syrjälä static int cherryview_irq_postinstall(struct drm_device *dev)
349143f328d7SVille Syrjälä {
3492fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
349343f328d7SVille Syrjälä 
349443f328d7SVille Syrjälä 	gen8_gt_irq_postinstall(dev_priv);
349543f328d7SVille Syrjälä 
3496ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
34979918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3498ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3499ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3500ad22d106SVille Syrjälä 
3501e5328c43SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
350243f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
350343f328d7SVille Syrjälä 
350443f328d7SVille Syrjälä 	return 0;
350543f328d7SVille Syrjälä }
350643f328d7SVille Syrjälä 
3507abd58f01SBen Widawsky static void gen8_irq_uninstall(struct drm_device *dev)
3508abd58f01SBen Widawsky {
3509fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3510abd58f01SBen Widawsky 
3511abd58f01SBen Widawsky 	if (!dev_priv)
3512abd58f01SBen Widawsky 		return;
3513abd58f01SBen Widawsky 
3514823f6b38SPaulo Zanoni 	gen8_irq_reset(dev);
3515abd58f01SBen Widawsky }
3516abd58f01SBen Widawsky 
35177e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
35187e231dbeSJesse Barnes {
3519fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
35207e231dbeSJesse Barnes 
35217e231dbeSJesse Barnes 	if (!dev_priv)
35227e231dbeSJesse Barnes 		return;
35237e231dbeSJesse Barnes 
3524843d0e7dSImre Deak 	I915_WRITE(VLV_MASTER_IER, 0);
352534c7b8a7SVille Syrjälä 	POSTING_READ(VLV_MASTER_IER);
3526843d0e7dSImre Deak 
3527b243f530STvrtko Ursulin 	gen5_gt_irq_reset(dev_priv);
3528893fce8eSVille Syrjälä 
35297e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
3530f8b79e58SImre Deak 
3531ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
35329918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3533ad22d106SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3534ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
35357e231dbeSJesse Barnes }
35367e231dbeSJesse Barnes 
353743f328d7SVille Syrjälä static void cherryview_irq_uninstall(struct drm_device *dev)
353843f328d7SVille Syrjälä {
3539fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
354043f328d7SVille Syrjälä 
354143f328d7SVille Syrjälä 	if (!dev_priv)
354243f328d7SVille Syrjälä 		return;
354343f328d7SVille Syrjälä 
354443f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
354543f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
354643f328d7SVille Syrjälä 
3547a2c30fbaSVille Syrjälä 	gen8_gt_irq_reset(dev_priv);
354843f328d7SVille Syrjälä 
3549a2c30fbaSVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
355043f328d7SVille Syrjälä 
3551ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
35529918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3553ad22d106SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3554ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
355543f328d7SVille Syrjälä }
355643f328d7SVille Syrjälä 
3557f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
3558036a4a7dSZhenyu Wang {
3559fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
35604697995bSJesse Barnes 
35614697995bSJesse Barnes 	if (!dev_priv)
35624697995bSJesse Barnes 		return;
35634697995bSJesse Barnes 
3564be30b29fSPaulo Zanoni 	ironlake_irq_reset(dev);
3565036a4a7dSZhenyu Wang }
3566036a4a7dSZhenyu Wang 
3567c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
3568c2798b19SChris Wilson {
3569fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3570c2798b19SChris Wilson 	int pipe;
3571c2798b19SChris Wilson 
3572055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3573c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3574c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3575c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3576c2798b19SChris Wilson 	POSTING_READ16(IER);
3577c2798b19SChris Wilson }
3578c2798b19SChris Wilson 
3579c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
3580c2798b19SChris Wilson {
3581fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3582c2798b19SChris Wilson 
3583c2798b19SChris Wilson 	I915_WRITE16(EMR,
3584c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3585c2798b19SChris Wilson 
3586c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
3587c2798b19SChris Wilson 	dev_priv->irq_mask =
3588c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3589c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3590c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
359137ef01abSDaniel Vetter 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
3592c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
3593c2798b19SChris Wilson 
3594c2798b19SChris Wilson 	I915_WRITE16(IER,
3595c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3596c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3597c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
3598c2798b19SChris Wilson 	POSTING_READ16(IER);
3599c2798b19SChris Wilson 
3600379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3601379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3602d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3603755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3604755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3605d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3606379ef82dSDaniel Vetter 
3607c2798b19SChris Wilson 	return 0;
3608c2798b19SChris Wilson }
3609c2798b19SChris Wilson 
36105a21b665SDaniel Vetter /*
36115a21b665SDaniel Vetter  * Returns true when a page flip has completed.
36125a21b665SDaniel Vetter  */
36135a21b665SDaniel Vetter static bool i8xx_handle_vblank(struct drm_i915_private *dev_priv,
36145a21b665SDaniel Vetter 			       int plane, int pipe, u32 iir)
36155a21b665SDaniel Vetter {
36165a21b665SDaniel Vetter 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
36175a21b665SDaniel Vetter 
36185a21b665SDaniel Vetter 	if (!intel_pipe_handle_vblank(dev_priv, pipe))
36195a21b665SDaniel Vetter 		return false;
36205a21b665SDaniel Vetter 
36215a21b665SDaniel Vetter 	if ((iir & flip_pending) == 0)
36225a21b665SDaniel Vetter 		goto check_page_flip;
36235a21b665SDaniel Vetter 
36245a21b665SDaniel Vetter 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
36255a21b665SDaniel Vetter 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
36265a21b665SDaniel Vetter 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
36275a21b665SDaniel Vetter 	 * the flip is completed (no longer pending). Since this doesn't raise
36285a21b665SDaniel Vetter 	 * an interrupt per se, we watch for the change at vblank.
36295a21b665SDaniel Vetter 	 */
36305a21b665SDaniel Vetter 	if (I915_READ16(ISR) & flip_pending)
36315a21b665SDaniel Vetter 		goto check_page_flip;
36325a21b665SDaniel Vetter 
36335a21b665SDaniel Vetter 	intel_finish_page_flip_cs(dev_priv, pipe);
36345a21b665SDaniel Vetter 	return true;
36355a21b665SDaniel Vetter 
36365a21b665SDaniel Vetter check_page_flip:
36375a21b665SDaniel Vetter 	intel_check_page_flip(dev_priv, pipe);
36385a21b665SDaniel Vetter 	return false;
36395a21b665SDaniel Vetter }
36405a21b665SDaniel Vetter 
3641ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3642c2798b19SChris Wilson {
364345a83f84SDaniel Vetter 	struct drm_device *dev = arg;
3644fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3645c2798b19SChris Wilson 	u16 iir, new_iir;
3646c2798b19SChris Wilson 	u32 pipe_stats[2];
3647c2798b19SChris Wilson 	int pipe;
3648c2798b19SChris Wilson 	u16 flip_mask =
3649c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3650c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
36511f814dacSImre Deak 	irqreturn_t ret;
3652c2798b19SChris Wilson 
36532dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
36542dd2a883SImre Deak 		return IRQ_NONE;
36552dd2a883SImre Deak 
36561f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
36571f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
36581f814dacSImre Deak 
36591f814dacSImre Deak 	ret = IRQ_NONE;
3660c2798b19SChris Wilson 	iir = I915_READ16(IIR);
3661c2798b19SChris Wilson 	if (iir == 0)
36621f814dacSImre Deak 		goto out;
3663c2798b19SChris Wilson 
3664c2798b19SChris Wilson 	while (iir & ~flip_mask) {
3665c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3666c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3667c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3668c2798b19SChris Wilson 		 * interrupts (for non-MSI).
3669c2798b19SChris Wilson 		 */
3670222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
3671c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3672aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
3673c2798b19SChris Wilson 
3674055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
3675f0f59a00SVille Syrjälä 			i915_reg_t reg = PIPESTAT(pipe);
3676c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3677c2798b19SChris Wilson 
3678c2798b19SChris Wilson 			/*
3679c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3680c2798b19SChris Wilson 			 */
36812d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & 0x8000ffff)
3682c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3683c2798b19SChris Wilson 		}
3684222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
3685c2798b19SChris Wilson 
3686c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
3687c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
3688c2798b19SChris Wilson 
3689c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
36903b3f1650SAkash Goel 			notify_ring(dev_priv->engine[RCS]);
3691c2798b19SChris Wilson 
3692055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
36935a21b665SDaniel Vetter 			int plane = pipe;
36945a21b665SDaniel Vetter 			if (HAS_FBC(dev_priv))
36955a21b665SDaniel Vetter 				plane = !plane;
36965a21b665SDaniel Vetter 
36975a21b665SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
36985a21b665SDaniel Vetter 			    i8xx_handle_vblank(dev_priv, plane, pipe, iir))
36995a21b665SDaniel Vetter 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3700c2798b19SChris Wilson 
37014356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
370291d14251STvrtko Ursulin 				i9xx_pipe_crc_irq_handler(dev_priv, pipe);
37032d9d2b0bSVille Syrjälä 
37041f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
37051f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
37061f7247c0SDaniel Vetter 								    pipe);
37074356d586SDaniel Vetter 		}
3708c2798b19SChris Wilson 
3709c2798b19SChris Wilson 		iir = new_iir;
3710c2798b19SChris Wilson 	}
37111f814dacSImre Deak 	ret = IRQ_HANDLED;
3712c2798b19SChris Wilson 
37131f814dacSImre Deak out:
37141f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
37151f814dacSImre Deak 
37161f814dacSImre Deak 	return ret;
3717c2798b19SChris Wilson }
3718c2798b19SChris Wilson 
3719c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
3720c2798b19SChris Wilson {
3721fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3722c2798b19SChris Wilson 	int pipe;
3723c2798b19SChris Wilson 
3724055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
3725c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
3726c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3727c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
3728c2798b19SChris Wilson 	}
3729c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3730c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3731c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
3732c2798b19SChris Wilson }
3733c2798b19SChris Wilson 
3734a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
3735a266c7d5SChris Wilson {
3736fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3737a266c7d5SChris Wilson 	int pipe;
3738a266c7d5SChris Wilson 
373956b857a5STvrtko Ursulin 	if (I915_HAS_HOTPLUG(dev_priv)) {
37400706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
3741a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3742a266c7d5SChris Wilson 	}
3743a266c7d5SChris Wilson 
374400d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
3745055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3746a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3747a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3748a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3749a266c7d5SChris Wilson 	POSTING_READ(IER);
3750a266c7d5SChris Wilson }
3751a266c7d5SChris Wilson 
3752a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
3753a266c7d5SChris Wilson {
3754fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
375538bde180SChris Wilson 	u32 enable_mask;
3756a266c7d5SChris Wilson 
375738bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
375838bde180SChris Wilson 
375938bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
376038bde180SChris Wilson 	dev_priv->irq_mask =
376138bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
376238bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
376338bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
376438bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
376537ef01abSDaniel Vetter 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
376638bde180SChris Wilson 
376738bde180SChris Wilson 	enable_mask =
376838bde180SChris Wilson 		I915_ASLE_INTERRUPT |
376938bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
377038bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
377138bde180SChris Wilson 		I915_USER_INTERRUPT;
377238bde180SChris Wilson 
377356b857a5STvrtko Ursulin 	if (I915_HAS_HOTPLUG(dev_priv)) {
37740706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
377520afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
377620afbda2SDaniel Vetter 
3777a266c7d5SChris Wilson 		/* Enable in IER... */
3778a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
3779a266c7d5SChris Wilson 		/* and unmask in IMR */
3780a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
3781a266c7d5SChris Wilson 	}
3782a266c7d5SChris Wilson 
3783a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3784a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3785a266c7d5SChris Wilson 	POSTING_READ(IER);
3786a266c7d5SChris Wilson 
378791d14251STvrtko Ursulin 	i915_enable_asle_pipestat(dev_priv);
378820afbda2SDaniel Vetter 
3789379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3790379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3791d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3792755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3793755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3794d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3795379ef82dSDaniel Vetter 
379620afbda2SDaniel Vetter 	return 0;
379720afbda2SDaniel Vetter }
379820afbda2SDaniel Vetter 
37995a21b665SDaniel Vetter /*
38005a21b665SDaniel Vetter  * Returns true when a page flip has completed.
38015a21b665SDaniel Vetter  */
38025a21b665SDaniel Vetter static bool i915_handle_vblank(struct drm_i915_private *dev_priv,
38035a21b665SDaniel Vetter 			       int plane, int pipe, u32 iir)
38045a21b665SDaniel Vetter {
38055a21b665SDaniel Vetter 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
38065a21b665SDaniel Vetter 
38075a21b665SDaniel Vetter 	if (!intel_pipe_handle_vblank(dev_priv, pipe))
38085a21b665SDaniel Vetter 		return false;
38095a21b665SDaniel Vetter 
38105a21b665SDaniel Vetter 	if ((iir & flip_pending) == 0)
38115a21b665SDaniel Vetter 		goto check_page_flip;
38125a21b665SDaniel Vetter 
38135a21b665SDaniel Vetter 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
38145a21b665SDaniel Vetter 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
38155a21b665SDaniel Vetter 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
38165a21b665SDaniel Vetter 	 * the flip is completed (no longer pending). Since this doesn't raise
38175a21b665SDaniel Vetter 	 * an interrupt per se, we watch for the change at vblank.
38185a21b665SDaniel Vetter 	 */
38195a21b665SDaniel Vetter 	if (I915_READ(ISR) & flip_pending)
38205a21b665SDaniel Vetter 		goto check_page_flip;
38215a21b665SDaniel Vetter 
38225a21b665SDaniel Vetter 	intel_finish_page_flip_cs(dev_priv, pipe);
38235a21b665SDaniel Vetter 	return true;
38245a21b665SDaniel Vetter 
38255a21b665SDaniel Vetter check_page_flip:
38265a21b665SDaniel Vetter 	intel_check_page_flip(dev_priv, pipe);
38275a21b665SDaniel Vetter 	return false;
38285a21b665SDaniel Vetter }
38295a21b665SDaniel Vetter 
3830ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
3831a266c7d5SChris Wilson {
383245a83f84SDaniel Vetter 	struct drm_device *dev = arg;
3833fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
38348291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
383538bde180SChris Wilson 	u32 flip_mask =
383638bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
383738bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
383838bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
3839a266c7d5SChris Wilson 
38402dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
38412dd2a883SImre Deak 		return IRQ_NONE;
38422dd2a883SImre Deak 
38431f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
38441f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
38451f814dacSImre Deak 
3846a266c7d5SChris Wilson 	iir = I915_READ(IIR);
384738bde180SChris Wilson 	do {
384838bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
38498291ee90SChris Wilson 		bool blc_event = false;
3850a266c7d5SChris Wilson 
3851a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3852a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3853a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3854a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3855a266c7d5SChris Wilson 		 */
3856222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
3857a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3858aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
3859a266c7d5SChris Wilson 
3860055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
3861f0f59a00SVille Syrjälä 			i915_reg_t reg = PIPESTAT(pipe);
3862a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3863a266c7d5SChris Wilson 
386438bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
3865a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3866a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
386738bde180SChris Wilson 				irq_received = true;
3868a266c7d5SChris Wilson 			}
3869a266c7d5SChris Wilson 		}
3870222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
3871a266c7d5SChris Wilson 
3872a266c7d5SChris Wilson 		if (!irq_received)
3873a266c7d5SChris Wilson 			break;
3874a266c7d5SChris Wilson 
3875a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
387691d14251STvrtko Ursulin 		if (I915_HAS_HOTPLUG(dev_priv) &&
38771ae3c34cSVille Syrjälä 		    iir & I915_DISPLAY_PORT_INTERRUPT) {
38781ae3c34cSVille Syrjälä 			u32 hotplug_status = i9xx_hpd_irq_ack(dev_priv);
38791ae3c34cSVille Syrjälä 			if (hotplug_status)
388091d14251STvrtko Ursulin 				i9xx_hpd_irq_handler(dev_priv, hotplug_status);
38811ae3c34cSVille Syrjälä 		}
3882a266c7d5SChris Wilson 
388338bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
3884a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3885a266c7d5SChris Wilson 
3886a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
38873b3f1650SAkash Goel 			notify_ring(dev_priv->engine[RCS]);
3888a266c7d5SChris Wilson 
3889055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
38905a21b665SDaniel Vetter 			int plane = pipe;
38915a21b665SDaniel Vetter 			if (HAS_FBC(dev_priv))
38925a21b665SDaniel Vetter 				plane = !plane;
38935a21b665SDaniel Vetter 
38945a21b665SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
38955a21b665SDaniel Vetter 			    i915_handle_vblank(dev_priv, plane, pipe, iir))
38965a21b665SDaniel Vetter 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3897a266c7d5SChris Wilson 
3898a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3899a266c7d5SChris Wilson 				blc_event = true;
39004356d586SDaniel Vetter 
39014356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
390291d14251STvrtko Ursulin 				i9xx_pipe_crc_irq_handler(dev_priv, pipe);
39032d9d2b0bSVille Syrjälä 
39041f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
39051f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
39061f7247c0SDaniel Vetter 								    pipe);
3907a266c7d5SChris Wilson 		}
3908a266c7d5SChris Wilson 
3909a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
391091d14251STvrtko Ursulin 			intel_opregion_asle_intr(dev_priv);
3911a266c7d5SChris Wilson 
3912a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3913a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3914a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3915a266c7d5SChris Wilson 		 * we would never get another interrupt.
3916a266c7d5SChris Wilson 		 *
3917a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3918a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3919a266c7d5SChris Wilson 		 * another one.
3920a266c7d5SChris Wilson 		 *
3921a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3922a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3923a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3924a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3925a266c7d5SChris Wilson 		 * stray interrupts.
3926a266c7d5SChris Wilson 		 */
392738bde180SChris Wilson 		ret = IRQ_HANDLED;
3928a266c7d5SChris Wilson 		iir = new_iir;
392938bde180SChris Wilson 	} while (iir & ~flip_mask);
3930a266c7d5SChris Wilson 
39311f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
39321f814dacSImre Deak 
3933a266c7d5SChris Wilson 	return ret;
3934a266c7d5SChris Wilson }
3935a266c7d5SChris Wilson 
3936a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
3937a266c7d5SChris Wilson {
3938fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3939a266c7d5SChris Wilson 	int pipe;
3940a266c7d5SChris Wilson 
394156b857a5STvrtko Ursulin 	if (I915_HAS_HOTPLUG(dev_priv)) {
39420706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
3943a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3944a266c7d5SChris Wilson 	}
3945a266c7d5SChris Wilson 
394600d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
3947055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
394855b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
3949a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
395055b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
395155b39755SChris Wilson 	}
3952a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3953a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3954a266c7d5SChris Wilson 
3955a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3956a266c7d5SChris Wilson }
3957a266c7d5SChris Wilson 
3958a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
3959a266c7d5SChris Wilson {
3960fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3961a266c7d5SChris Wilson 	int pipe;
3962a266c7d5SChris Wilson 
39630706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
3964a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3965a266c7d5SChris Wilson 
3966a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
3967055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3968a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3969a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3970a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3971a266c7d5SChris Wilson 	POSTING_READ(IER);
3972a266c7d5SChris Wilson }
3973a266c7d5SChris Wilson 
3974a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
3975a266c7d5SChris Wilson {
3976fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3977bbba0a97SChris Wilson 	u32 enable_mask;
3978a266c7d5SChris Wilson 	u32 error_mask;
3979a266c7d5SChris Wilson 
3980a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
3981bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
3982adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
3983bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3984bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3985bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3986bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3987bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3988bbba0a97SChris Wilson 
3989bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
399021ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
399121ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
3992bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
3993bbba0a97SChris Wilson 
399491d14251STvrtko Ursulin 	if (IS_G4X(dev_priv))
3995bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
3996a266c7d5SChris Wilson 
3997b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3998b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3999d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4000755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
4001755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4002755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4003d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4004a266c7d5SChris Wilson 
4005a266c7d5SChris Wilson 	/*
4006a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
4007a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
4008a266c7d5SChris Wilson 	 */
400991d14251STvrtko Ursulin 	if (IS_G4X(dev_priv)) {
4010a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
4011a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
4012a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
4013a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4014a266c7d5SChris Wilson 	} else {
4015a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
4016a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4017a266c7d5SChris Wilson 	}
4018a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
4019a266c7d5SChris Wilson 
4020a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
4021a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
4022a266c7d5SChris Wilson 	POSTING_READ(IER);
4023a266c7d5SChris Wilson 
40240706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
402520afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
402620afbda2SDaniel Vetter 
402791d14251STvrtko Ursulin 	i915_enable_asle_pipestat(dev_priv);
402820afbda2SDaniel Vetter 
402920afbda2SDaniel Vetter 	return 0;
403020afbda2SDaniel Vetter }
403120afbda2SDaniel Vetter 
403291d14251STvrtko Ursulin static void i915_hpd_irq_setup(struct drm_i915_private *dev_priv)
403320afbda2SDaniel Vetter {
403420afbda2SDaniel Vetter 	u32 hotplug_en;
403520afbda2SDaniel Vetter 
403667520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
4037b5ea2d56SDaniel Vetter 
4038adca4730SChris Wilson 	/* Note HDMI and DP share hotplug bits */
4039e5868a31SEgbert Eich 	/* enable bits are the same for all generations */
404091d14251STvrtko Ursulin 	hotplug_en = intel_hpd_enabled_irqs(dev_priv, hpd_mask_i915);
4041a266c7d5SChris Wilson 	/* Programming the CRT detection parameters tends
4042a266c7d5SChris Wilson 	   to generate a spurious hotplug event about three
4043a266c7d5SChris Wilson 	   seconds later.  So just do it once.
4044a266c7d5SChris Wilson 	*/
404591d14251STvrtko Ursulin 	if (IS_G4X(dev_priv))
4046a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
4047a266c7d5SChris Wilson 	hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
4048a266c7d5SChris Wilson 
4049a266c7d5SChris Wilson 	/* Ignore TV since it's buggy */
40500706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv,
4051f9e3dc78SJani Nikula 					     HOTPLUG_INT_EN_MASK |
4052f9e3dc78SJani Nikula 					     CRT_HOTPLUG_VOLTAGE_COMPARE_MASK |
4053f9e3dc78SJani Nikula 					     CRT_HOTPLUG_ACTIVATION_PERIOD_64,
40540706f17cSEgbert Eich 					     hotplug_en);
4055a266c7d5SChris Wilson }
4056a266c7d5SChris Wilson 
4057ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
4058a266c7d5SChris Wilson {
405945a83f84SDaniel Vetter 	struct drm_device *dev = arg;
4060fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4061a266c7d5SChris Wilson 	u32 iir, new_iir;
4062a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
4063a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
406421ad8330SVille Syrjälä 	u32 flip_mask =
406521ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
406621ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
4067a266c7d5SChris Wilson 
40682dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
40692dd2a883SImre Deak 		return IRQ_NONE;
40702dd2a883SImre Deak 
40711f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
40721f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
40731f814dacSImre Deak 
4074a266c7d5SChris Wilson 	iir = I915_READ(IIR);
4075a266c7d5SChris Wilson 
4076a266c7d5SChris Wilson 	for (;;) {
4077501e01d7SVille Syrjälä 		bool irq_received = (iir & ~flip_mask) != 0;
40782c8ba29fSChris Wilson 		bool blc_event = false;
40792c8ba29fSChris Wilson 
4080a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4081a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4082a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4083a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
4084a266c7d5SChris Wilson 		 */
4085222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
4086a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4087aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4088a266c7d5SChris Wilson 
4089055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4090f0f59a00SVille Syrjälä 			i915_reg_t reg = PIPESTAT(pipe);
4091a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4092a266c7d5SChris Wilson 
4093a266c7d5SChris Wilson 			/*
4094a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
4095a266c7d5SChris Wilson 			 */
4096a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4097a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
4098501e01d7SVille Syrjälä 				irq_received = true;
4099a266c7d5SChris Wilson 			}
4100a266c7d5SChris Wilson 		}
4101222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4102a266c7d5SChris Wilson 
4103a266c7d5SChris Wilson 		if (!irq_received)
4104a266c7d5SChris Wilson 			break;
4105a266c7d5SChris Wilson 
4106a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
4107a266c7d5SChris Wilson 
4108a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
41091ae3c34cSVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
41101ae3c34cSVille Syrjälä 			u32 hotplug_status = i9xx_hpd_irq_ack(dev_priv);
41111ae3c34cSVille Syrjälä 			if (hotplug_status)
411291d14251STvrtko Ursulin 				i9xx_hpd_irq_handler(dev_priv, hotplug_status);
41131ae3c34cSVille Syrjälä 		}
4114a266c7d5SChris Wilson 
411521ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
4116a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4117a266c7d5SChris Wilson 
4118a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
41193b3f1650SAkash Goel 			notify_ring(dev_priv->engine[RCS]);
4120a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
41213b3f1650SAkash Goel 			notify_ring(dev_priv->engine[VCS]);
4122a266c7d5SChris Wilson 
4123055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
41245a21b665SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
41255a21b665SDaniel Vetter 			    i915_handle_vblank(dev_priv, pipe, pipe, iir))
41265a21b665SDaniel Vetter 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
4127a266c7d5SChris Wilson 
4128a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4129a266c7d5SChris Wilson 				blc_event = true;
41304356d586SDaniel Vetter 
41314356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
413291d14251STvrtko Ursulin 				i9xx_pipe_crc_irq_handler(dev_priv, pipe);
4133a266c7d5SChris Wilson 
41341f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
41351f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
41362d9d2b0bSVille Syrjälä 		}
4137a266c7d5SChris Wilson 
4138a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
413991d14251STvrtko Ursulin 			intel_opregion_asle_intr(dev_priv);
4140a266c7d5SChris Wilson 
4141515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
414291d14251STvrtko Ursulin 			gmbus_irq_handler(dev_priv);
4143515ac2bbSDaniel Vetter 
4144a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4145a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4146a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4147a266c7d5SChris Wilson 		 * we would never get another interrupt.
4148a266c7d5SChris Wilson 		 *
4149a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4150a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4151a266c7d5SChris Wilson 		 * another one.
4152a266c7d5SChris Wilson 		 *
4153a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4154a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4155a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4156a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4157a266c7d5SChris Wilson 		 * stray interrupts.
4158a266c7d5SChris Wilson 		 */
4159a266c7d5SChris Wilson 		iir = new_iir;
4160a266c7d5SChris Wilson 	}
4161a266c7d5SChris Wilson 
41621f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
41631f814dacSImre Deak 
4164a266c7d5SChris Wilson 	return ret;
4165a266c7d5SChris Wilson }
4166a266c7d5SChris Wilson 
4167a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
4168a266c7d5SChris Wilson {
4169fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4170a266c7d5SChris Wilson 	int pipe;
4171a266c7d5SChris Wilson 
4172a266c7d5SChris Wilson 	if (!dev_priv)
4173a266c7d5SChris Wilson 		return;
4174a266c7d5SChris Wilson 
41750706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4176a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4177a266c7d5SChris Wilson 
4178a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
4179055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4180a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4181a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4182a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4183a266c7d5SChris Wilson 
4184055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4185a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
4186a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
4187a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4188a266c7d5SChris Wilson }
4189a266c7d5SChris Wilson 
4190fca52a55SDaniel Vetter /**
4191fca52a55SDaniel Vetter  * intel_irq_init - initializes irq support
4192fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4193fca52a55SDaniel Vetter  *
4194fca52a55SDaniel Vetter  * This function initializes all the irq support including work items, timers
4195fca52a55SDaniel Vetter  * and all the vtables. It does not setup the interrupt itself though.
4196fca52a55SDaniel Vetter  */
4197b963291cSDaniel Vetter void intel_irq_init(struct drm_i915_private *dev_priv)
4198f71d4af4SJesse Barnes {
419991c8a326SChris Wilson 	struct drm_device *dev = &dev_priv->drm;
42008b2e326dSChris Wilson 
420177913b39SJani Nikula 	intel_hpd_init_work(dev_priv);
420277913b39SJani Nikula 
4203c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
4204a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
42058b2e326dSChris Wilson 
42064805fe82STvrtko Ursulin 	if (HAS_GUC_SCHED(dev_priv))
420726705e20SSagar Arun Kamble 		dev_priv->pm_guc_events = GEN9_GUC_TO_HOST_INT_EVENT;
420826705e20SSagar Arun Kamble 
4209a6706b45SDeepak S 	/* Let's track the enabled rps events */
4210666a4537SWayne Boyer 	if (IS_VALLEYVIEW(dev_priv))
42116c65a587SVille Syrjälä 		/* WaGsvRC0ResidencyMethod:vlv */
4212e0e8c7cbSChris Wilson 		dev_priv->pm_rps_events = GEN6_PM_RP_UP_EI_EXPIRED;
421331685c25SDeepak S 	else
4214a6706b45SDeepak S 		dev_priv->pm_rps_events = GEN6_PM_RPS_EVENTS;
4215a6706b45SDeepak S 
42165dd04556SSagar Arun Kamble 	dev_priv->rps.pm_intrmsk_mbz = 0;
42171800ad25SSagar Arun Kamble 
42181800ad25SSagar Arun Kamble 	/*
4219acf2dc22SMika Kuoppala 	 * SNB,IVB,HSW can while VLV,CHV may hard hang on looping batchbuffer
42201800ad25SSagar Arun Kamble 	 * if GEN6_PM_UP_EI_EXPIRED is masked.
42211800ad25SSagar Arun Kamble 	 *
42221800ad25SSagar Arun Kamble 	 * TODO: verify if this can be reproduced on VLV,CHV.
42231800ad25SSagar Arun Kamble 	 */
4224acf2dc22SMika Kuoppala 	if (INTEL_INFO(dev_priv)->gen <= 7)
42255dd04556SSagar Arun Kamble 		dev_priv->rps.pm_intrmsk_mbz |= GEN6_PM_RP_UP_EI_EXPIRED;
42261800ad25SSagar Arun Kamble 
42271800ad25SSagar Arun Kamble 	if (INTEL_INFO(dev_priv)->gen >= 8)
4228655d49efSChris Wilson 		dev_priv->rps.pm_intrmsk_mbz |= GEN8_PMINTR_DISABLE_REDIRECT_TO_GUC;
42291800ad25SSagar Arun Kamble 
4230b963291cSDaniel Vetter 	if (IS_GEN2(dev_priv)) {
42314194c088SRodrigo Vivi 		/* Gen2 doesn't have a hardware frame counter */
42324cdb83ecSVille Syrjälä 		dev->max_vblank_count = 0;
4233b963291cSDaniel Vetter 	} else if (IS_G4X(dev_priv) || INTEL_INFO(dev_priv)->gen >= 5) {
4234f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
4235fd8f507cSVille Syrjälä 		dev->driver->get_vblank_counter = g4x_get_vblank_counter;
4236391f75e2SVille Syrjälä 	} else {
4237391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
4238391f75e2SVille Syrjälä 		dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
4239f71d4af4SJesse Barnes 	}
4240f71d4af4SJesse Barnes 
424121da2700SVille Syrjälä 	/*
424221da2700SVille Syrjälä 	 * Opt out of the vblank disable timer on everything except gen2.
424321da2700SVille Syrjälä 	 * Gen2 doesn't have a hardware frame counter and so depends on
424421da2700SVille Syrjälä 	 * vblank interrupts to produce sane vblank seuquence numbers.
424521da2700SVille Syrjälä 	 */
4246b963291cSDaniel Vetter 	if (!IS_GEN2(dev_priv))
424721da2700SVille Syrjälä 		dev->vblank_disable_immediate = true;
424821da2700SVille Syrjälä 
4249262fd485SChris Wilson 	/* Most platforms treat the display irq block as an always-on
4250262fd485SChris Wilson 	 * power domain. vlv/chv can disable it at runtime and need
4251262fd485SChris Wilson 	 * special care to avoid writing any of the display block registers
4252262fd485SChris Wilson 	 * outside of the power domain. We defer setting up the display irqs
4253262fd485SChris Wilson 	 * in this case to the runtime pm.
4254262fd485SChris Wilson 	 */
4255262fd485SChris Wilson 	dev_priv->display_irqs_enabled = true;
4256262fd485SChris Wilson 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4257262fd485SChris Wilson 		dev_priv->display_irqs_enabled = false;
4258262fd485SChris Wilson 
4259317eaa95SLyude 	dev_priv->hotplug.hpd_storm_threshold = HPD_STORM_DEFAULT_THRESHOLD;
4260317eaa95SLyude 
42611bf6ad62SDaniel Vetter 	dev->driver->get_vblank_timestamp = drm_calc_vbltimestamp_from_scanoutpos;
4262f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
4263f71d4af4SJesse Barnes 
4264b963291cSDaniel Vetter 	if (IS_CHERRYVIEW(dev_priv)) {
426543f328d7SVille Syrjälä 		dev->driver->irq_handler = cherryview_irq_handler;
426643f328d7SVille Syrjälä 		dev->driver->irq_preinstall = cherryview_irq_preinstall;
426743f328d7SVille Syrjälä 		dev->driver->irq_postinstall = cherryview_irq_postinstall;
426843f328d7SVille Syrjälä 		dev->driver->irq_uninstall = cherryview_irq_uninstall;
426986e83e35SChris Wilson 		dev->driver->enable_vblank = i965_enable_vblank;
427086e83e35SChris Wilson 		dev->driver->disable_vblank = i965_disable_vblank;
427143f328d7SVille Syrjälä 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4272b963291cSDaniel Vetter 	} else if (IS_VALLEYVIEW(dev_priv)) {
42737e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
42747e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
42757e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
42767e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
427786e83e35SChris Wilson 		dev->driver->enable_vblank = i965_enable_vblank;
427886e83e35SChris Wilson 		dev->driver->disable_vblank = i965_disable_vblank;
4279fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4280b963291cSDaniel Vetter 	} else if (INTEL_INFO(dev_priv)->gen >= 8) {
4281abd58f01SBen Widawsky 		dev->driver->irq_handler = gen8_irq_handler;
4282723761b8SDaniel Vetter 		dev->driver->irq_preinstall = gen8_irq_reset;
4283abd58f01SBen Widawsky 		dev->driver->irq_postinstall = gen8_irq_postinstall;
4284abd58f01SBen Widawsky 		dev->driver->irq_uninstall = gen8_irq_uninstall;
4285abd58f01SBen Widawsky 		dev->driver->enable_vblank = gen8_enable_vblank;
4286abd58f01SBen Widawsky 		dev->driver->disable_vblank = gen8_disable_vblank;
4287cc3f90f0SAnder Conselvan de Oliveira 		if (IS_GEN9_LP(dev_priv))
4288e0a20ad7SShashank Sharma 			dev_priv->display.hpd_irq_setup = bxt_hpd_irq_setup;
42896e266956STvrtko Ursulin 		else if (HAS_PCH_SPT(dev_priv) || HAS_PCH_KBP(dev_priv))
42906dbf30ceSVille Syrjälä 			dev_priv->display.hpd_irq_setup = spt_hpd_irq_setup;
42916dbf30ceSVille Syrjälä 		else
42923a3b3c7dSVille Syrjälä 			dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
42936e266956STvrtko Ursulin 	} else if (HAS_PCH_SPLIT(dev_priv)) {
4294f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
4295723761b8SDaniel Vetter 		dev->driver->irq_preinstall = ironlake_irq_reset;
4296f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
4297f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
4298f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
4299f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
4300e4ce95aaSVille Syrjälä 		dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
4301f71d4af4SJesse Barnes 	} else {
43027e22dbbbSTvrtko Ursulin 		if (IS_GEN2(dev_priv)) {
4303c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
4304c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
4305c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
4306c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
430786e83e35SChris Wilson 			dev->driver->enable_vblank = i8xx_enable_vblank;
430886e83e35SChris Wilson 			dev->driver->disable_vblank = i8xx_disable_vblank;
43097e22dbbbSTvrtko Ursulin 		} else if (IS_GEN3(dev_priv)) {
4310a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
4311a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
4312a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
4313a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
431486e83e35SChris Wilson 			dev->driver->enable_vblank = i8xx_enable_vblank;
431586e83e35SChris Wilson 			dev->driver->disable_vblank = i8xx_disable_vblank;
4316c2798b19SChris Wilson 		} else {
4317a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
4318a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
4319a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
4320a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
432186e83e35SChris Wilson 			dev->driver->enable_vblank = i965_enable_vblank;
432286e83e35SChris Wilson 			dev->driver->disable_vblank = i965_disable_vblank;
4323c2798b19SChris Wilson 		}
4324778eb334SVille Syrjälä 		if (I915_HAS_HOTPLUG(dev_priv))
4325778eb334SVille Syrjälä 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4326f71d4af4SJesse Barnes 	}
4327f71d4af4SJesse Barnes }
432820afbda2SDaniel Vetter 
4329fca52a55SDaniel Vetter /**
4330fca52a55SDaniel Vetter  * intel_irq_install - enables the hardware interrupt
4331fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4332fca52a55SDaniel Vetter  *
4333fca52a55SDaniel Vetter  * This function enables the hardware interrupt handling, but leaves the hotplug
4334fca52a55SDaniel Vetter  * handling still disabled. It is called after intel_irq_init().
4335fca52a55SDaniel Vetter  *
4336fca52a55SDaniel Vetter  * In the driver load and resume code we need working interrupts in a few places
4337fca52a55SDaniel Vetter  * but don't want to deal with the hassle of concurrent probe and hotplug
4338fca52a55SDaniel Vetter  * workers. Hence the split into this two-stage approach.
4339fca52a55SDaniel Vetter  */
43402aeb7d3aSDaniel Vetter int intel_irq_install(struct drm_i915_private *dev_priv)
43412aeb7d3aSDaniel Vetter {
43422aeb7d3aSDaniel Vetter 	/*
43432aeb7d3aSDaniel Vetter 	 * We enable some interrupt sources in our postinstall hooks, so mark
43442aeb7d3aSDaniel Vetter 	 * interrupts as enabled _before_ actually enabling them to avoid
43452aeb7d3aSDaniel Vetter 	 * special cases in our ordering checks.
43462aeb7d3aSDaniel Vetter 	 */
43472aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
43482aeb7d3aSDaniel Vetter 
434991c8a326SChris Wilson 	return drm_irq_install(&dev_priv->drm, dev_priv->drm.pdev->irq);
43502aeb7d3aSDaniel Vetter }
43512aeb7d3aSDaniel Vetter 
4352fca52a55SDaniel Vetter /**
4353fca52a55SDaniel Vetter  * intel_irq_uninstall - finilizes all irq handling
4354fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4355fca52a55SDaniel Vetter  *
4356fca52a55SDaniel Vetter  * This stops interrupt and hotplug handling and unregisters and frees all
4357fca52a55SDaniel Vetter  * resources acquired in the init functions.
4358fca52a55SDaniel Vetter  */
43592aeb7d3aSDaniel Vetter void intel_irq_uninstall(struct drm_i915_private *dev_priv)
43602aeb7d3aSDaniel Vetter {
436191c8a326SChris Wilson 	drm_irq_uninstall(&dev_priv->drm);
43622aeb7d3aSDaniel Vetter 	intel_hpd_cancel_work(dev_priv);
43632aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
43642aeb7d3aSDaniel Vetter }
43652aeb7d3aSDaniel Vetter 
4366fca52a55SDaniel Vetter /**
4367fca52a55SDaniel Vetter  * intel_runtime_pm_disable_interrupts - runtime interrupt disabling
4368fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4369fca52a55SDaniel Vetter  *
4370fca52a55SDaniel Vetter  * This function is used to disable interrupts at runtime, both in the runtime
4371fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4372fca52a55SDaniel Vetter  */
4373b963291cSDaniel Vetter void intel_runtime_pm_disable_interrupts(struct drm_i915_private *dev_priv)
4374c67a470bSPaulo Zanoni {
437591c8a326SChris Wilson 	dev_priv->drm.driver->irq_uninstall(&dev_priv->drm);
43762aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
437791c8a326SChris Wilson 	synchronize_irq(dev_priv->drm.irq);
4378c67a470bSPaulo Zanoni }
4379c67a470bSPaulo Zanoni 
4380fca52a55SDaniel Vetter /**
4381fca52a55SDaniel Vetter  * intel_runtime_pm_enable_interrupts - runtime interrupt enabling
4382fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4383fca52a55SDaniel Vetter  *
4384fca52a55SDaniel Vetter  * This function is used to enable interrupts at runtime, both in the runtime
4385fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4386fca52a55SDaniel Vetter  */
4387b963291cSDaniel Vetter void intel_runtime_pm_enable_interrupts(struct drm_i915_private *dev_priv)
4388c67a470bSPaulo Zanoni {
43892aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
439091c8a326SChris Wilson 	dev_priv->drm.driver->irq_preinstall(&dev_priv->drm);
439191c8a326SChris Wilson 	dev_priv->drm.driver->irq_postinstall(&dev_priv->drm);
4392c67a470bSPaulo Zanoni }
4393