xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 9dfe2e3ad375a9ba32a13888873ec4586be01ff7)
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 
1293488d4ebSVille Syrjälä #define GEN3_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 
139e9e9848aSVille Syrjälä #define GEN2_IRQ_RESET(type) do { \
140e9e9848aSVille Syrjälä 	I915_WRITE16(type##IMR, 0xffff); \
141e9e9848aSVille Syrjälä 	POSTING_READ16(type##IMR); \
142e9e9848aSVille Syrjälä 	I915_WRITE16(type##IER, 0); \
143e9e9848aSVille Syrjälä 	I915_WRITE16(type##IIR, 0xffff); \
144e9e9848aSVille Syrjälä 	POSTING_READ16(type##IIR); \
145e9e9848aSVille Syrjälä 	I915_WRITE16(type##IIR, 0xffff); \
146e9e9848aSVille Syrjälä 	POSTING_READ16(type##IIR); \
147e9e9848aSVille Syrjälä } while (0)
148e9e9848aSVille Syrjälä 
149337ba017SPaulo Zanoni /*
150337ba017SPaulo Zanoni  * We should clear IMR at preinstall/uninstall, and just check at postinstall.
151337ba017SPaulo Zanoni  */
1523488d4ebSVille Syrjälä static void gen3_assert_iir_is_zero(struct drm_i915_private *dev_priv,
153f0f59a00SVille Syrjälä 				    i915_reg_t reg)
154b51a2842SVille Syrjälä {
155b51a2842SVille Syrjälä 	u32 val = I915_READ(reg);
156b51a2842SVille Syrjälä 
157b51a2842SVille Syrjälä 	if (val == 0)
158b51a2842SVille Syrjälä 		return;
159b51a2842SVille Syrjälä 
160b51a2842SVille Syrjälä 	WARN(1, "Interrupt register 0x%x is not zero: 0x%08x\n",
161f0f59a00SVille Syrjälä 	     i915_mmio_reg_offset(reg), val);
162b51a2842SVille Syrjälä 	I915_WRITE(reg, 0xffffffff);
163b51a2842SVille Syrjälä 	POSTING_READ(reg);
164b51a2842SVille Syrjälä 	I915_WRITE(reg, 0xffffffff);
165b51a2842SVille Syrjälä 	POSTING_READ(reg);
166b51a2842SVille Syrjälä }
167337ba017SPaulo Zanoni 
168e9e9848aSVille Syrjälä static void gen2_assert_iir_is_zero(struct drm_i915_private *dev_priv,
169e9e9848aSVille Syrjälä 				    i915_reg_t reg)
170e9e9848aSVille Syrjälä {
171e9e9848aSVille Syrjälä 	u16 val = I915_READ16(reg);
172e9e9848aSVille Syrjälä 
173e9e9848aSVille Syrjälä 	if (val == 0)
174e9e9848aSVille Syrjälä 		return;
175e9e9848aSVille Syrjälä 
176e9e9848aSVille Syrjälä 	WARN(1, "Interrupt register 0x%x is not zero: 0x%08x\n",
177e9e9848aSVille Syrjälä 	     i915_mmio_reg_offset(reg), val);
178e9e9848aSVille Syrjälä 	I915_WRITE16(reg, 0xffff);
179e9e9848aSVille Syrjälä 	POSTING_READ16(reg);
180e9e9848aSVille Syrjälä 	I915_WRITE16(reg, 0xffff);
181e9e9848aSVille Syrjälä 	POSTING_READ16(reg);
182e9e9848aSVille Syrjälä }
183e9e9848aSVille Syrjälä 
18435079899SPaulo Zanoni #define GEN8_IRQ_INIT_NDX(type, which, imr_val, ier_val) do { \
1853488d4ebSVille Syrjälä 	gen3_assert_iir_is_zero(dev_priv, GEN8_##type##_IIR(which)); \
18635079899SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IER(which), (ier_val)); \
1877d1bd539SVille Syrjälä 	I915_WRITE(GEN8_##type##_IMR(which), (imr_val)); \
1887d1bd539SVille Syrjälä 	POSTING_READ(GEN8_##type##_IMR(which)); \
18935079899SPaulo Zanoni } while (0)
19035079899SPaulo Zanoni 
1913488d4ebSVille Syrjälä #define GEN3_IRQ_INIT(type, imr_val, ier_val) do { \
1923488d4ebSVille Syrjälä 	gen3_assert_iir_is_zero(dev_priv, type##IIR); \
19335079899SPaulo Zanoni 	I915_WRITE(type##IER, (ier_val)); \
1947d1bd539SVille Syrjälä 	I915_WRITE(type##IMR, (imr_val)); \
1957d1bd539SVille Syrjälä 	POSTING_READ(type##IMR); \
19635079899SPaulo Zanoni } while (0)
19735079899SPaulo Zanoni 
198e9e9848aSVille Syrjälä #define GEN2_IRQ_INIT(type, imr_val, ier_val) do { \
199e9e9848aSVille Syrjälä 	gen2_assert_iir_is_zero(dev_priv, type##IIR); \
200e9e9848aSVille Syrjälä 	I915_WRITE16(type##IER, (ier_val)); \
201e9e9848aSVille Syrjälä 	I915_WRITE16(type##IMR, (imr_val)); \
202e9e9848aSVille Syrjälä 	POSTING_READ16(type##IMR); \
203e9e9848aSVille Syrjälä } while (0)
204e9e9848aSVille Syrjälä 
205c9a9a268SImre Deak static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir);
20626705e20SSagar Arun Kamble static void gen9_guc_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir);
207c9a9a268SImre Deak 
2080706f17cSEgbert Eich /* For display hotplug interrupt */
2090706f17cSEgbert Eich static inline void
2100706f17cSEgbert Eich i915_hotplug_interrupt_update_locked(struct drm_i915_private *dev_priv,
2110706f17cSEgbert Eich 				     uint32_t mask,
2120706f17cSEgbert Eich 				     uint32_t bits)
2130706f17cSEgbert Eich {
2140706f17cSEgbert Eich 	uint32_t val;
2150706f17cSEgbert Eich 
21667520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
2170706f17cSEgbert Eich 	WARN_ON(bits & ~mask);
2180706f17cSEgbert Eich 
2190706f17cSEgbert Eich 	val = I915_READ(PORT_HOTPLUG_EN);
2200706f17cSEgbert Eich 	val &= ~mask;
2210706f17cSEgbert Eich 	val |= bits;
2220706f17cSEgbert Eich 	I915_WRITE(PORT_HOTPLUG_EN, val);
2230706f17cSEgbert Eich }
2240706f17cSEgbert Eich 
2250706f17cSEgbert Eich /**
2260706f17cSEgbert Eich  * i915_hotplug_interrupt_update - update hotplug interrupt enable
2270706f17cSEgbert Eich  * @dev_priv: driver private
2280706f17cSEgbert Eich  * @mask: bits to update
2290706f17cSEgbert Eich  * @bits: bits to enable
2300706f17cSEgbert Eich  * NOTE: the HPD enable bits are modified both inside and outside
2310706f17cSEgbert Eich  * of an interrupt context. To avoid that read-modify-write cycles
2320706f17cSEgbert Eich  * interfer, these bits are protected by a spinlock. Since this
2330706f17cSEgbert Eich  * function is usually not called from a context where the lock is
2340706f17cSEgbert Eich  * held already, this function acquires the lock itself. A non-locking
2350706f17cSEgbert Eich  * version is also available.
2360706f17cSEgbert Eich  */
2370706f17cSEgbert Eich void i915_hotplug_interrupt_update(struct drm_i915_private *dev_priv,
2380706f17cSEgbert Eich 				   uint32_t mask,
2390706f17cSEgbert Eich 				   uint32_t bits)
2400706f17cSEgbert Eich {
2410706f17cSEgbert Eich 	spin_lock_irq(&dev_priv->irq_lock);
2420706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv, mask, bits);
2430706f17cSEgbert Eich 	spin_unlock_irq(&dev_priv->irq_lock);
2440706f17cSEgbert Eich }
2450706f17cSEgbert Eich 
246d9dc34f1SVille Syrjälä /**
247d9dc34f1SVille Syrjälä  * ilk_update_display_irq - update DEIMR
248d9dc34f1SVille Syrjälä  * @dev_priv: driver private
249d9dc34f1SVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
250d9dc34f1SVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
251d9dc34f1SVille Syrjälä  */
252fbdedaeaSVille Syrjälä void ilk_update_display_irq(struct drm_i915_private *dev_priv,
253d9dc34f1SVille Syrjälä 			    uint32_t interrupt_mask,
254d9dc34f1SVille Syrjälä 			    uint32_t enabled_irq_mask)
255036a4a7dSZhenyu Wang {
256d9dc34f1SVille Syrjälä 	uint32_t new_val;
257d9dc34f1SVille Syrjälä 
25867520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
2594bc9d430SDaniel Vetter 
260d9dc34f1SVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
261d9dc34f1SVille Syrjälä 
2629df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
263c67a470bSPaulo Zanoni 		return;
264c67a470bSPaulo Zanoni 
265d9dc34f1SVille Syrjälä 	new_val = dev_priv->irq_mask;
266d9dc34f1SVille Syrjälä 	new_val &= ~interrupt_mask;
267d9dc34f1SVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
268d9dc34f1SVille Syrjälä 
269d9dc34f1SVille Syrjälä 	if (new_val != dev_priv->irq_mask) {
270d9dc34f1SVille Syrjälä 		dev_priv->irq_mask = new_val;
2711ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
2723143a2bfSChris Wilson 		POSTING_READ(DEIMR);
273036a4a7dSZhenyu Wang 	}
274036a4a7dSZhenyu Wang }
275036a4a7dSZhenyu Wang 
27643eaea13SPaulo Zanoni /**
27743eaea13SPaulo Zanoni  * ilk_update_gt_irq - update GTIMR
27843eaea13SPaulo Zanoni  * @dev_priv: driver private
27943eaea13SPaulo Zanoni  * @interrupt_mask: mask of interrupt bits to update
28043eaea13SPaulo Zanoni  * @enabled_irq_mask: mask of interrupt bits to enable
28143eaea13SPaulo Zanoni  */
28243eaea13SPaulo Zanoni static void ilk_update_gt_irq(struct drm_i915_private *dev_priv,
28343eaea13SPaulo Zanoni 			      uint32_t interrupt_mask,
28443eaea13SPaulo Zanoni 			      uint32_t enabled_irq_mask)
28543eaea13SPaulo Zanoni {
28667520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
28743eaea13SPaulo Zanoni 
28815a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
28915a17aaeSDaniel Vetter 
2909df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
291c67a470bSPaulo Zanoni 		return;
292c67a470bSPaulo Zanoni 
29343eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask &= ~interrupt_mask;
29443eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask |= (~enabled_irq_mask & interrupt_mask);
29543eaea13SPaulo Zanoni 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
29643eaea13SPaulo Zanoni }
29743eaea13SPaulo Zanoni 
298480c8033SDaniel Vetter void gen5_enable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
29943eaea13SPaulo Zanoni {
30043eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, mask);
30131bb59ccSChris Wilson 	POSTING_READ_FW(GTIMR);
30243eaea13SPaulo Zanoni }
30343eaea13SPaulo Zanoni 
304480c8033SDaniel Vetter void gen5_disable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
30543eaea13SPaulo Zanoni {
30643eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, 0);
30743eaea13SPaulo Zanoni }
30843eaea13SPaulo Zanoni 
309f0f59a00SVille Syrjälä static i915_reg_t gen6_pm_iir(struct drm_i915_private *dev_priv)
310b900b949SImre Deak {
311bca2bf2aSPandiyan, Dhinakaran 	return INTEL_GEN(dev_priv) >= 8 ? GEN8_GT_IIR(2) : GEN6_PMIIR;
312b900b949SImre Deak }
313b900b949SImre Deak 
314f0f59a00SVille Syrjälä static i915_reg_t gen6_pm_imr(struct drm_i915_private *dev_priv)
315a72fbc3aSImre Deak {
316bca2bf2aSPandiyan, Dhinakaran 	return INTEL_GEN(dev_priv) >= 8 ? GEN8_GT_IMR(2) : GEN6_PMIMR;
317a72fbc3aSImre Deak }
318a72fbc3aSImre Deak 
319f0f59a00SVille Syrjälä static i915_reg_t gen6_pm_ier(struct drm_i915_private *dev_priv)
320b900b949SImre Deak {
321bca2bf2aSPandiyan, Dhinakaran 	return INTEL_GEN(dev_priv) >= 8 ? GEN8_GT_IER(2) : GEN6_PMIER;
322b900b949SImre Deak }
323b900b949SImre Deak 
324edbfdb45SPaulo Zanoni /**
325edbfdb45SPaulo Zanoni  * snb_update_pm_irq - update GEN6_PMIMR
326edbfdb45SPaulo Zanoni  * @dev_priv: driver private
327edbfdb45SPaulo Zanoni  * @interrupt_mask: mask of interrupt bits to update
328edbfdb45SPaulo Zanoni  * @enabled_irq_mask: mask of interrupt bits to enable
329edbfdb45SPaulo Zanoni  */
330edbfdb45SPaulo Zanoni static void snb_update_pm_irq(struct drm_i915_private *dev_priv,
331edbfdb45SPaulo Zanoni 			      uint32_t interrupt_mask,
332edbfdb45SPaulo Zanoni 			      uint32_t enabled_irq_mask)
333edbfdb45SPaulo Zanoni {
334605cd25bSPaulo Zanoni 	uint32_t new_val;
335edbfdb45SPaulo Zanoni 
33615a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
33715a17aaeSDaniel Vetter 
33867520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
339edbfdb45SPaulo Zanoni 
340f4e9af4fSAkash Goel 	new_val = dev_priv->pm_imr;
341f52ecbcfSPaulo Zanoni 	new_val &= ~interrupt_mask;
342f52ecbcfSPaulo Zanoni 	new_val |= (~enabled_irq_mask & interrupt_mask);
343f52ecbcfSPaulo Zanoni 
344f4e9af4fSAkash Goel 	if (new_val != dev_priv->pm_imr) {
345f4e9af4fSAkash Goel 		dev_priv->pm_imr = new_val;
346f4e9af4fSAkash Goel 		I915_WRITE(gen6_pm_imr(dev_priv), dev_priv->pm_imr);
347a72fbc3aSImre Deak 		POSTING_READ(gen6_pm_imr(dev_priv));
348edbfdb45SPaulo Zanoni 	}
349f52ecbcfSPaulo Zanoni }
350edbfdb45SPaulo Zanoni 
351f4e9af4fSAkash Goel void gen6_unmask_pm_irq(struct drm_i915_private *dev_priv, u32 mask)
352edbfdb45SPaulo Zanoni {
3539939fba2SImre Deak 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
3549939fba2SImre Deak 		return;
3559939fba2SImre Deak 
356edbfdb45SPaulo Zanoni 	snb_update_pm_irq(dev_priv, mask, mask);
357edbfdb45SPaulo Zanoni }
358edbfdb45SPaulo Zanoni 
359f4e9af4fSAkash Goel static void __gen6_mask_pm_irq(struct drm_i915_private *dev_priv, u32 mask)
3609939fba2SImre Deak {
3619939fba2SImre Deak 	snb_update_pm_irq(dev_priv, mask, 0);
3629939fba2SImre Deak }
3639939fba2SImre Deak 
364f4e9af4fSAkash Goel void gen6_mask_pm_irq(struct drm_i915_private *dev_priv, u32 mask)
365edbfdb45SPaulo Zanoni {
3669939fba2SImre Deak 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
3679939fba2SImre Deak 		return;
3689939fba2SImre Deak 
369f4e9af4fSAkash Goel 	__gen6_mask_pm_irq(dev_priv, mask);
370f4e9af4fSAkash Goel }
371f4e9af4fSAkash Goel 
3723814fd77SOscar Mateo static void gen6_reset_pm_iir(struct drm_i915_private *dev_priv, u32 reset_mask)
373f4e9af4fSAkash Goel {
374f4e9af4fSAkash Goel 	i915_reg_t reg = gen6_pm_iir(dev_priv);
375f4e9af4fSAkash Goel 
37667520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
377f4e9af4fSAkash Goel 
378f4e9af4fSAkash Goel 	I915_WRITE(reg, reset_mask);
379f4e9af4fSAkash Goel 	I915_WRITE(reg, reset_mask);
380f4e9af4fSAkash Goel 	POSTING_READ(reg);
381f4e9af4fSAkash Goel }
382f4e9af4fSAkash Goel 
3833814fd77SOscar Mateo static void gen6_enable_pm_irq(struct drm_i915_private *dev_priv, u32 enable_mask)
384f4e9af4fSAkash Goel {
38567520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
386f4e9af4fSAkash Goel 
387f4e9af4fSAkash Goel 	dev_priv->pm_ier |= enable_mask;
388f4e9af4fSAkash Goel 	I915_WRITE(gen6_pm_ier(dev_priv), dev_priv->pm_ier);
389f4e9af4fSAkash Goel 	gen6_unmask_pm_irq(dev_priv, enable_mask);
390f4e9af4fSAkash Goel 	/* unmask_pm_irq provides an implicit barrier (POSTING_READ) */
391f4e9af4fSAkash Goel }
392f4e9af4fSAkash Goel 
3933814fd77SOscar Mateo static void gen6_disable_pm_irq(struct drm_i915_private *dev_priv, u32 disable_mask)
394f4e9af4fSAkash Goel {
39567520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
396f4e9af4fSAkash Goel 
397f4e9af4fSAkash Goel 	dev_priv->pm_ier &= ~disable_mask;
398f4e9af4fSAkash Goel 	__gen6_mask_pm_irq(dev_priv, disable_mask);
399f4e9af4fSAkash Goel 	I915_WRITE(gen6_pm_ier(dev_priv), dev_priv->pm_ier);
400f4e9af4fSAkash Goel 	/* though a barrier is missing here, but don't really need a one */
401edbfdb45SPaulo Zanoni }
402edbfdb45SPaulo Zanoni 
403dc97997aSChris Wilson void gen6_reset_rps_interrupts(struct drm_i915_private *dev_priv)
4043cc134e3SImre Deak {
4053cc134e3SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
406f4e9af4fSAkash Goel 	gen6_reset_pm_iir(dev_priv, dev_priv->pm_rps_events);
407096fad9eSImre Deak 	dev_priv->rps.pm_iir = 0;
4083cc134e3SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
4093cc134e3SImre Deak }
4103cc134e3SImre Deak 
41191d14251STvrtko Ursulin void gen6_enable_rps_interrupts(struct drm_i915_private *dev_priv)
412b900b949SImre Deak {
413f2a91d1aSChris Wilson 	if (READ_ONCE(dev_priv->rps.interrupts_enabled))
414f2a91d1aSChris Wilson 		return;
415f2a91d1aSChris Wilson 
416b900b949SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
417c33d247dSChris Wilson 	WARN_ON_ONCE(dev_priv->rps.pm_iir);
418c33d247dSChris Wilson 	WARN_ON_ONCE(I915_READ(gen6_pm_iir(dev_priv)) & dev_priv->pm_rps_events);
419d4d70aa5SImre Deak 	dev_priv->rps.interrupts_enabled = true;
420b900b949SImre Deak 	gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
42178e68d36SImre Deak 
422b900b949SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
423b900b949SImre Deak }
424b900b949SImre Deak 
42591d14251STvrtko Ursulin void gen6_disable_rps_interrupts(struct drm_i915_private *dev_priv)
426b900b949SImre Deak {
427f2a91d1aSChris Wilson 	if (!READ_ONCE(dev_priv->rps.interrupts_enabled))
428f2a91d1aSChris Wilson 		return;
429f2a91d1aSChris Wilson 
430d4d70aa5SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
431d4d70aa5SImre Deak 	dev_priv->rps.interrupts_enabled = false;
4329939fba2SImre Deak 
433b20e3cfeSDave Gordon 	I915_WRITE(GEN6_PMINTRMSK, gen6_sanitize_rps_pm_mask(dev_priv, ~0u));
4349939fba2SImre Deak 
435f4e9af4fSAkash Goel 	gen6_disable_pm_irq(dev_priv, dev_priv->pm_rps_events);
43658072ccbSImre Deak 
43758072ccbSImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
43891c8a326SChris Wilson 	synchronize_irq(dev_priv->drm.irq);
439c33d247dSChris Wilson 
440c33d247dSChris Wilson 	/* Now that we will not be generating any more work, flush any
4413814fd77SOscar Mateo 	 * outstanding tasks. As we are called on the RPS idle path,
442c33d247dSChris Wilson 	 * we will reset the GPU to minimum frequencies, so the current
443c33d247dSChris Wilson 	 * state of the worker can be discarded.
444c33d247dSChris Wilson 	 */
445c33d247dSChris Wilson 	cancel_work_sync(&dev_priv->rps.work);
446c33d247dSChris Wilson 	gen6_reset_rps_interrupts(dev_priv);
447b900b949SImre Deak }
448b900b949SImre Deak 
44926705e20SSagar Arun Kamble void gen9_reset_guc_interrupts(struct drm_i915_private *dev_priv)
45026705e20SSagar Arun Kamble {
45126705e20SSagar Arun Kamble 	spin_lock_irq(&dev_priv->irq_lock);
45226705e20SSagar Arun Kamble 	gen6_reset_pm_iir(dev_priv, dev_priv->pm_guc_events);
45326705e20SSagar Arun Kamble 	spin_unlock_irq(&dev_priv->irq_lock);
45426705e20SSagar Arun Kamble }
45526705e20SSagar Arun Kamble 
45626705e20SSagar Arun Kamble void gen9_enable_guc_interrupts(struct drm_i915_private *dev_priv)
45726705e20SSagar Arun Kamble {
45826705e20SSagar Arun Kamble 	spin_lock_irq(&dev_priv->irq_lock);
45926705e20SSagar Arun Kamble 	if (!dev_priv->guc.interrupts_enabled) {
46026705e20SSagar Arun Kamble 		WARN_ON_ONCE(I915_READ(gen6_pm_iir(dev_priv)) &
46126705e20SSagar Arun Kamble 				       dev_priv->pm_guc_events);
46226705e20SSagar Arun Kamble 		dev_priv->guc.interrupts_enabled = true;
46326705e20SSagar Arun Kamble 		gen6_enable_pm_irq(dev_priv, dev_priv->pm_guc_events);
46426705e20SSagar Arun Kamble 	}
46526705e20SSagar Arun Kamble 	spin_unlock_irq(&dev_priv->irq_lock);
46626705e20SSagar Arun Kamble }
46726705e20SSagar Arun Kamble 
46826705e20SSagar Arun Kamble void gen9_disable_guc_interrupts(struct drm_i915_private *dev_priv)
46926705e20SSagar Arun Kamble {
47026705e20SSagar Arun Kamble 	spin_lock_irq(&dev_priv->irq_lock);
47126705e20SSagar Arun Kamble 	dev_priv->guc.interrupts_enabled = false;
47226705e20SSagar Arun Kamble 
47326705e20SSagar Arun Kamble 	gen6_disable_pm_irq(dev_priv, dev_priv->pm_guc_events);
47426705e20SSagar Arun Kamble 
47526705e20SSagar Arun Kamble 	spin_unlock_irq(&dev_priv->irq_lock);
47626705e20SSagar Arun Kamble 	synchronize_irq(dev_priv->drm.irq);
47726705e20SSagar Arun Kamble 
47826705e20SSagar Arun Kamble 	gen9_reset_guc_interrupts(dev_priv);
47926705e20SSagar Arun Kamble }
48026705e20SSagar Arun Kamble 
4810961021aSBen Widawsky /**
4823a3b3c7dSVille Syrjälä  * bdw_update_port_irq - update DE port interrupt
4833a3b3c7dSVille Syrjälä  * @dev_priv: driver private
4843a3b3c7dSVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
4853a3b3c7dSVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
4863a3b3c7dSVille Syrjälä  */
4873a3b3c7dSVille Syrjälä static void bdw_update_port_irq(struct drm_i915_private *dev_priv,
4883a3b3c7dSVille Syrjälä 				uint32_t interrupt_mask,
4893a3b3c7dSVille Syrjälä 				uint32_t enabled_irq_mask)
4903a3b3c7dSVille Syrjälä {
4913a3b3c7dSVille Syrjälä 	uint32_t new_val;
4923a3b3c7dSVille Syrjälä 	uint32_t old_val;
4933a3b3c7dSVille Syrjälä 
49467520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
4953a3b3c7dSVille Syrjälä 
4963a3b3c7dSVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
4973a3b3c7dSVille Syrjälä 
4983a3b3c7dSVille Syrjälä 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
4993a3b3c7dSVille Syrjälä 		return;
5003a3b3c7dSVille Syrjälä 
5013a3b3c7dSVille Syrjälä 	old_val = I915_READ(GEN8_DE_PORT_IMR);
5023a3b3c7dSVille Syrjälä 
5033a3b3c7dSVille Syrjälä 	new_val = old_val;
5043a3b3c7dSVille Syrjälä 	new_val &= ~interrupt_mask;
5053a3b3c7dSVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
5063a3b3c7dSVille Syrjälä 
5073a3b3c7dSVille Syrjälä 	if (new_val != old_val) {
5083a3b3c7dSVille Syrjälä 		I915_WRITE(GEN8_DE_PORT_IMR, new_val);
5093a3b3c7dSVille Syrjälä 		POSTING_READ(GEN8_DE_PORT_IMR);
5103a3b3c7dSVille Syrjälä 	}
5113a3b3c7dSVille Syrjälä }
5123a3b3c7dSVille Syrjälä 
5133a3b3c7dSVille Syrjälä /**
514013d3752SVille Syrjälä  * bdw_update_pipe_irq - update DE pipe interrupt
515013d3752SVille Syrjälä  * @dev_priv: driver private
516013d3752SVille Syrjälä  * @pipe: pipe whose interrupt to update
517013d3752SVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
518013d3752SVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
519013d3752SVille Syrjälä  */
520013d3752SVille Syrjälä void bdw_update_pipe_irq(struct drm_i915_private *dev_priv,
521013d3752SVille Syrjälä 			 enum pipe pipe,
522013d3752SVille Syrjälä 			 uint32_t interrupt_mask,
523013d3752SVille Syrjälä 			 uint32_t enabled_irq_mask)
524013d3752SVille Syrjälä {
525013d3752SVille Syrjälä 	uint32_t new_val;
526013d3752SVille Syrjälä 
52767520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
528013d3752SVille Syrjälä 
529013d3752SVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
530013d3752SVille Syrjälä 
531013d3752SVille Syrjälä 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
532013d3752SVille Syrjälä 		return;
533013d3752SVille Syrjälä 
534013d3752SVille Syrjälä 	new_val = dev_priv->de_irq_mask[pipe];
535013d3752SVille Syrjälä 	new_val &= ~interrupt_mask;
536013d3752SVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
537013d3752SVille Syrjälä 
538013d3752SVille Syrjälä 	if (new_val != dev_priv->de_irq_mask[pipe]) {
539013d3752SVille Syrjälä 		dev_priv->de_irq_mask[pipe] = new_val;
540013d3752SVille Syrjälä 		I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
541013d3752SVille Syrjälä 		POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
542013d3752SVille Syrjälä 	}
543013d3752SVille Syrjälä }
544013d3752SVille Syrjälä 
545013d3752SVille Syrjälä /**
546fee884edSDaniel Vetter  * ibx_display_interrupt_update - update SDEIMR
547fee884edSDaniel Vetter  * @dev_priv: driver private
548fee884edSDaniel Vetter  * @interrupt_mask: mask of interrupt bits to update
549fee884edSDaniel Vetter  * @enabled_irq_mask: mask of interrupt bits to enable
550fee884edSDaniel Vetter  */
55147339cd9SDaniel Vetter void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
552fee884edSDaniel Vetter 				  uint32_t interrupt_mask,
553fee884edSDaniel Vetter 				  uint32_t enabled_irq_mask)
554fee884edSDaniel Vetter {
555fee884edSDaniel Vetter 	uint32_t sdeimr = I915_READ(SDEIMR);
556fee884edSDaniel Vetter 	sdeimr &= ~interrupt_mask;
557fee884edSDaniel Vetter 	sdeimr |= (~enabled_irq_mask & interrupt_mask);
558fee884edSDaniel Vetter 
55915a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
56015a17aaeSDaniel Vetter 
56167520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
562fee884edSDaniel Vetter 
5639df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
564c67a470bSPaulo Zanoni 		return;
565c67a470bSPaulo Zanoni 
566fee884edSDaniel Vetter 	I915_WRITE(SDEIMR, sdeimr);
567fee884edSDaniel Vetter 	POSTING_READ(SDEIMR);
568fee884edSDaniel Vetter }
5698664281bSPaulo Zanoni 
5706b12ca56SVille Syrjälä u32 i915_pipestat_enable_mask(struct drm_i915_private *dev_priv,
5716b12ca56SVille Syrjälä 			      enum pipe pipe)
5727c463586SKeith Packard {
5736b12ca56SVille Syrjälä 	u32 status_mask = dev_priv->pipestat_irq_mask[pipe];
57410c59c51SImre Deak 	u32 enable_mask = status_mask << 16;
57510c59c51SImre Deak 
5766b12ca56SVille Syrjälä 	lockdep_assert_held(&dev_priv->irq_lock);
5776b12ca56SVille Syrjälä 
5786b12ca56SVille Syrjälä 	if (INTEL_GEN(dev_priv) < 5)
5796b12ca56SVille Syrjälä 		goto out;
5806b12ca56SVille Syrjälä 
58110c59c51SImre Deak 	/*
582724a6905SVille Syrjälä 	 * On pipe A we don't support the PSR interrupt yet,
583724a6905SVille Syrjälä 	 * on pipe B and C the same bit MBZ.
58410c59c51SImre Deak 	 */
58510c59c51SImre Deak 	if (WARN_ON_ONCE(status_mask & PIPE_A_PSR_STATUS_VLV))
58610c59c51SImre Deak 		return 0;
587724a6905SVille Syrjälä 	/*
588724a6905SVille Syrjälä 	 * On pipe B and C we don't support the PSR interrupt yet, on pipe
589724a6905SVille Syrjälä 	 * A the same bit is for perf counters which we don't use either.
590724a6905SVille Syrjälä 	 */
591724a6905SVille Syrjälä 	if (WARN_ON_ONCE(status_mask & PIPE_B_PSR_STATUS_VLV))
592724a6905SVille Syrjälä 		return 0;
59310c59c51SImre Deak 
59410c59c51SImre Deak 	enable_mask &= ~(PIPE_FIFO_UNDERRUN_STATUS |
59510c59c51SImre Deak 			 SPRITE0_FLIP_DONE_INT_EN_VLV |
59610c59c51SImre Deak 			 SPRITE1_FLIP_DONE_INT_EN_VLV);
59710c59c51SImre Deak 	if (status_mask & SPRITE0_FLIP_DONE_INT_STATUS_VLV)
59810c59c51SImre Deak 		enable_mask |= SPRITE0_FLIP_DONE_INT_EN_VLV;
59910c59c51SImre Deak 	if (status_mask & SPRITE1_FLIP_DONE_INT_STATUS_VLV)
60010c59c51SImre Deak 		enable_mask |= SPRITE1_FLIP_DONE_INT_EN_VLV;
60110c59c51SImre Deak 
6026b12ca56SVille Syrjälä out:
6036b12ca56SVille Syrjälä 	WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
6046b12ca56SVille Syrjälä 		  status_mask & ~PIPESTAT_INT_STATUS_MASK,
6056b12ca56SVille Syrjälä 		  "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
6066b12ca56SVille Syrjälä 		  pipe_name(pipe), enable_mask, status_mask);
6076b12ca56SVille Syrjälä 
60810c59c51SImre Deak 	return enable_mask;
60910c59c51SImre Deak }
61010c59c51SImre Deak 
6116b12ca56SVille Syrjälä void i915_enable_pipestat(struct drm_i915_private *dev_priv,
6126b12ca56SVille Syrjälä 			  enum pipe pipe, u32 status_mask)
613755e9019SImre Deak {
6146b12ca56SVille Syrjälä 	i915_reg_t reg = PIPESTAT(pipe);
615755e9019SImre Deak 	u32 enable_mask;
616755e9019SImre Deak 
6176b12ca56SVille Syrjälä 	WARN_ONCE(status_mask & ~PIPESTAT_INT_STATUS_MASK,
6186b12ca56SVille Syrjälä 		  "pipe %c: status_mask=0x%x\n",
6196b12ca56SVille Syrjälä 		  pipe_name(pipe), status_mask);
6206b12ca56SVille Syrjälä 
6216b12ca56SVille Syrjälä 	lockdep_assert_held(&dev_priv->irq_lock);
6226b12ca56SVille Syrjälä 	WARN_ON(!intel_irqs_enabled(dev_priv));
6236b12ca56SVille Syrjälä 
6246b12ca56SVille Syrjälä 	if ((dev_priv->pipestat_irq_mask[pipe] & status_mask) == status_mask)
6256b12ca56SVille Syrjälä 		return;
6266b12ca56SVille Syrjälä 
6276b12ca56SVille Syrjälä 	dev_priv->pipestat_irq_mask[pipe] |= status_mask;
6286b12ca56SVille Syrjälä 	enable_mask = i915_pipestat_enable_mask(dev_priv, pipe);
6296b12ca56SVille Syrjälä 
6306b12ca56SVille Syrjälä 	I915_WRITE(reg, enable_mask | status_mask);
6316b12ca56SVille Syrjälä 	POSTING_READ(reg);
632755e9019SImre Deak }
633755e9019SImre Deak 
6346b12ca56SVille Syrjälä void i915_disable_pipestat(struct drm_i915_private *dev_priv,
6356b12ca56SVille Syrjälä 			   enum pipe pipe, u32 status_mask)
636755e9019SImre Deak {
6376b12ca56SVille Syrjälä 	i915_reg_t reg = PIPESTAT(pipe);
638755e9019SImre Deak 	u32 enable_mask;
639755e9019SImre Deak 
6406b12ca56SVille Syrjälä 	WARN_ONCE(status_mask & ~PIPESTAT_INT_STATUS_MASK,
6416b12ca56SVille Syrjälä 		  "pipe %c: status_mask=0x%x\n",
6426b12ca56SVille Syrjälä 		  pipe_name(pipe), status_mask);
6436b12ca56SVille Syrjälä 
6446b12ca56SVille Syrjälä 	lockdep_assert_held(&dev_priv->irq_lock);
6456b12ca56SVille Syrjälä 	WARN_ON(!intel_irqs_enabled(dev_priv));
6466b12ca56SVille Syrjälä 
6476b12ca56SVille Syrjälä 	if ((dev_priv->pipestat_irq_mask[pipe] & status_mask) == 0)
6486b12ca56SVille Syrjälä 		return;
6496b12ca56SVille Syrjälä 
6506b12ca56SVille Syrjälä 	dev_priv->pipestat_irq_mask[pipe] &= ~status_mask;
6516b12ca56SVille Syrjälä 	enable_mask = i915_pipestat_enable_mask(dev_priv, pipe);
6526b12ca56SVille Syrjälä 
6536b12ca56SVille Syrjälä 	I915_WRITE(reg, enable_mask | status_mask);
6546b12ca56SVille Syrjälä 	POSTING_READ(reg);
655755e9019SImre Deak }
656755e9019SImre Deak 
657c0e09200SDave Airlie /**
658f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
65914bb2c11STvrtko Ursulin  * @dev_priv: i915 device private
66001c66889SZhao Yakui  */
66191d14251STvrtko Ursulin static void i915_enable_asle_pipestat(struct drm_i915_private *dev_priv)
66201c66889SZhao Yakui {
66391d14251STvrtko Ursulin 	if (!dev_priv->opregion.asle || !IS_MOBILE(dev_priv))
664f49e38ddSJani Nikula 		return;
665f49e38ddSJani Nikula 
66613321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
66701c66889SZhao Yakui 
668755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_STATUS);
66991d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 4)
6703b6c42e8SDaniel Vetter 		i915_enable_pipestat(dev_priv, PIPE_A,
671755e9019SImre Deak 				     PIPE_LEGACY_BLC_EVENT_STATUS);
6721ec14ad3SChris Wilson 
67313321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
67401c66889SZhao Yakui }
67501c66889SZhao Yakui 
676f75f3746SVille Syrjälä /*
677f75f3746SVille Syrjälä  * This timing diagram depicts the video signal in and
678f75f3746SVille Syrjälä  * around the vertical blanking period.
679f75f3746SVille Syrjälä  *
680f75f3746SVille Syrjälä  * Assumptions about the fictitious mode used in this example:
681f75f3746SVille Syrjälä  *  vblank_start >= 3
682f75f3746SVille Syrjälä  *  vsync_start = vblank_start + 1
683f75f3746SVille Syrjälä  *  vsync_end = vblank_start + 2
684f75f3746SVille Syrjälä  *  vtotal = vblank_start + 3
685f75f3746SVille Syrjälä  *
686f75f3746SVille Syrjälä  *           start of vblank:
687f75f3746SVille Syrjälä  *           latch double buffered registers
688f75f3746SVille Syrjälä  *           increment frame counter (ctg+)
689f75f3746SVille Syrjälä  *           generate start of vblank interrupt (gen4+)
690f75f3746SVille Syrjälä  *           |
691f75f3746SVille Syrjälä  *           |          frame start:
692f75f3746SVille Syrjälä  *           |          generate frame start interrupt (aka. vblank interrupt) (gmch)
693f75f3746SVille Syrjälä  *           |          may be shifted forward 1-3 extra lines via PIPECONF
694f75f3746SVille Syrjälä  *           |          |
695f75f3746SVille Syrjälä  *           |          |  start of vsync:
696f75f3746SVille Syrjälä  *           |          |  generate vsync interrupt
697f75f3746SVille Syrjälä  *           |          |  |
698f75f3746SVille Syrjälä  * ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx
699f75f3746SVille Syrjälä  *       .   \hs/   .      \hs/          \hs/          \hs/   .      \hs/
700f75f3746SVille Syrjälä  * ----va---> <-----------------vb--------------------> <--------va-------------
701f75f3746SVille Syrjälä  *       |          |       <----vs----->                     |
702f75f3746SVille Syrjälä  * -vbs-----> <---vbs+1---> <---vbs+2---> <-----0-----> <-----1-----> <-----2--- (scanline counter gen2)
703f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2---> <-----0--- (scanline counter gen3+)
704f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2- (scanline counter hsw+ hdmi)
705f75f3746SVille Syrjälä  *       |          |                                         |
706f75f3746SVille Syrjälä  *       last visible pixel                                   first visible pixel
707f75f3746SVille Syrjälä  *                  |                                         increment frame counter (gen3/4)
708f75f3746SVille Syrjälä  *                  pixel counter = vblank_start * htotal     pixel counter = 0 (gen3/4)
709f75f3746SVille Syrjälä  *
710f75f3746SVille Syrjälä  * x  = horizontal active
711f75f3746SVille Syrjälä  * _  = horizontal blanking
712f75f3746SVille Syrjälä  * hs = horizontal sync
713f75f3746SVille Syrjälä  * va = vertical active
714f75f3746SVille Syrjälä  * vb = vertical blanking
715f75f3746SVille Syrjälä  * vs = vertical sync
716f75f3746SVille Syrjälä  * vbs = vblank_start (number)
717f75f3746SVille Syrjälä  *
718f75f3746SVille Syrjälä  * Summary:
719f75f3746SVille Syrjälä  * - most events happen at the start of horizontal sync
720f75f3746SVille Syrjälä  * - frame start happens at the start of horizontal blank, 1-4 lines
721f75f3746SVille Syrjälä  *   (depending on PIPECONF settings) after the start of vblank
722f75f3746SVille Syrjälä  * - gen3/4 pixel and frame counter are synchronized with the start
723f75f3746SVille Syrjälä  *   of horizontal active on the first line of vertical active
724f75f3746SVille Syrjälä  */
725f75f3746SVille Syrjälä 
72642f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
72742f52ef8SKeith Packard  * we use as a pipe index
72842f52ef8SKeith Packard  */
72988e72717SThierry Reding static u32 i915_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
7300a3e67a4SJesse Barnes {
731fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
732f0f59a00SVille Syrjälä 	i915_reg_t high_frame, low_frame;
7330b2a8e09SVille Syrjälä 	u32 high1, high2, low, pixel, vbl_start, hsync_start, htotal;
7345caa0feaSDaniel Vetter 	const struct drm_display_mode *mode = &dev->vblank[pipe].hwmode;
735694e409dSVille Syrjälä 	unsigned long irqflags;
736391f75e2SVille Syrjälä 
7370b2a8e09SVille Syrjälä 	htotal = mode->crtc_htotal;
7380b2a8e09SVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
7390b2a8e09SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
7400b2a8e09SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
7410b2a8e09SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
742391f75e2SVille Syrjälä 
7430b2a8e09SVille Syrjälä 	/* Convert to pixel count */
7440b2a8e09SVille Syrjälä 	vbl_start *= htotal;
7450b2a8e09SVille Syrjälä 
7460b2a8e09SVille Syrjälä 	/* Start of vblank event occurs at start of hsync */
7470b2a8e09SVille Syrjälä 	vbl_start -= htotal - hsync_start;
7480b2a8e09SVille Syrjälä 
7499db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
7509db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
7515eddb70bSChris Wilson 
752694e409dSVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
753694e409dSVille Syrjälä 
7540a3e67a4SJesse Barnes 	/*
7550a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
7560a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
7570a3e67a4SJesse Barnes 	 * register.
7580a3e67a4SJesse Barnes 	 */
7590a3e67a4SJesse Barnes 	do {
760694e409dSVille Syrjälä 		high1 = I915_READ_FW(high_frame) & PIPE_FRAME_HIGH_MASK;
761694e409dSVille Syrjälä 		low   = I915_READ_FW(low_frame);
762694e409dSVille Syrjälä 		high2 = I915_READ_FW(high_frame) & PIPE_FRAME_HIGH_MASK;
7630a3e67a4SJesse Barnes 	} while (high1 != high2);
7640a3e67a4SJesse Barnes 
765694e409dSVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
766694e409dSVille Syrjälä 
7675eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
768391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
7695eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
770391f75e2SVille Syrjälä 
771391f75e2SVille Syrjälä 	/*
772391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
773391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
774391f75e2SVille Syrjälä 	 * counter against vblank start.
775391f75e2SVille Syrjälä 	 */
776edc08d0aSVille Syrjälä 	return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
7770a3e67a4SJesse Barnes }
7780a3e67a4SJesse Barnes 
779974e59baSDave Airlie static u32 g4x_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
7809880b7a5SJesse Barnes {
781fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
7829880b7a5SJesse Barnes 
783649636efSVille Syrjälä 	return I915_READ(PIPE_FRMCOUNT_G4X(pipe));
7849880b7a5SJesse Barnes }
7859880b7a5SJesse Barnes 
786aec0246fSUma Shankar /*
787aec0246fSUma Shankar  * On certain encoders on certain platforms, pipe
788aec0246fSUma Shankar  * scanline register will not work to get the scanline,
789aec0246fSUma Shankar  * since the timings are driven from the PORT or issues
790aec0246fSUma Shankar  * with scanline register updates.
791aec0246fSUma Shankar  * This function will use Framestamp and current
792aec0246fSUma Shankar  * timestamp registers to calculate the scanline.
793aec0246fSUma Shankar  */
794aec0246fSUma Shankar static u32 __intel_get_crtc_scanline_from_timestamp(struct intel_crtc *crtc)
795aec0246fSUma Shankar {
796aec0246fSUma Shankar 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
797aec0246fSUma Shankar 	struct drm_vblank_crtc *vblank =
798aec0246fSUma Shankar 		&crtc->base.dev->vblank[drm_crtc_index(&crtc->base)];
799aec0246fSUma Shankar 	const struct drm_display_mode *mode = &vblank->hwmode;
800aec0246fSUma Shankar 	u32 vblank_start = mode->crtc_vblank_start;
801aec0246fSUma Shankar 	u32 vtotal = mode->crtc_vtotal;
802aec0246fSUma Shankar 	u32 htotal = mode->crtc_htotal;
803aec0246fSUma Shankar 	u32 clock = mode->crtc_clock;
804aec0246fSUma Shankar 	u32 scanline, scan_prev_time, scan_curr_time, scan_post_time;
805aec0246fSUma Shankar 
806aec0246fSUma Shankar 	/*
807aec0246fSUma Shankar 	 * To avoid the race condition where we might cross into the
808aec0246fSUma Shankar 	 * next vblank just between the PIPE_FRMTMSTMP and TIMESTAMP_CTR
809aec0246fSUma Shankar 	 * reads. We make sure we read PIPE_FRMTMSTMP and TIMESTAMP_CTR
810aec0246fSUma Shankar 	 * during the same frame.
811aec0246fSUma Shankar 	 */
812aec0246fSUma Shankar 	do {
813aec0246fSUma Shankar 		/*
814aec0246fSUma Shankar 		 * This field provides read back of the display
815aec0246fSUma Shankar 		 * pipe frame time stamp. The time stamp value
816aec0246fSUma Shankar 		 * is sampled at every start of vertical blank.
817aec0246fSUma Shankar 		 */
818aec0246fSUma Shankar 		scan_prev_time = I915_READ_FW(PIPE_FRMTMSTMP(crtc->pipe));
819aec0246fSUma Shankar 
820aec0246fSUma Shankar 		/*
821aec0246fSUma Shankar 		 * The TIMESTAMP_CTR register has the current
822aec0246fSUma Shankar 		 * time stamp value.
823aec0246fSUma Shankar 		 */
824aec0246fSUma Shankar 		scan_curr_time = I915_READ_FW(IVB_TIMESTAMP_CTR);
825aec0246fSUma Shankar 
826aec0246fSUma Shankar 		scan_post_time = I915_READ_FW(PIPE_FRMTMSTMP(crtc->pipe));
827aec0246fSUma Shankar 	} while (scan_post_time != scan_prev_time);
828aec0246fSUma Shankar 
829aec0246fSUma Shankar 	scanline = div_u64(mul_u32_u32(scan_curr_time - scan_prev_time,
830aec0246fSUma Shankar 					clock), 1000 * htotal);
831aec0246fSUma Shankar 	scanline = min(scanline, vtotal - 1);
832aec0246fSUma Shankar 	scanline = (scanline + vblank_start) % vtotal;
833aec0246fSUma Shankar 
834aec0246fSUma Shankar 	return scanline;
835aec0246fSUma Shankar }
836aec0246fSUma Shankar 
83775aa3f63SVille Syrjälä /* I915_READ_FW, only for fast reads of display block, no need for forcewake etc. */
838a225f079SVille Syrjälä static int __intel_get_crtc_scanline(struct intel_crtc *crtc)
839a225f079SVille Syrjälä {
840a225f079SVille Syrjälä 	struct drm_device *dev = crtc->base.dev;
841fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
8425caa0feaSDaniel Vetter 	const struct drm_display_mode *mode;
8435caa0feaSDaniel Vetter 	struct drm_vblank_crtc *vblank;
844a225f079SVille Syrjälä 	enum pipe pipe = crtc->pipe;
84580715b2fSVille Syrjälä 	int position, vtotal;
846a225f079SVille Syrjälä 
84772259536SVille Syrjälä 	if (!crtc->active)
84872259536SVille Syrjälä 		return -1;
84972259536SVille Syrjälä 
8505caa0feaSDaniel Vetter 	vblank = &crtc->base.dev->vblank[drm_crtc_index(&crtc->base)];
8515caa0feaSDaniel Vetter 	mode = &vblank->hwmode;
8525caa0feaSDaniel Vetter 
853aec0246fSUma Shankar 	if (mode->private_flags & I915_MODE_FLAG_GET_SCANLINE_FROM_TIMESTAMP)
854aec0246fSUma Shankar 		return __intel_get_crtc_scanline_from_timestamp(crtc);
855aec0246fSUma Shankar 
85680715b2fSVille Syrjälä 	vtotal = mode->crtc_vtotal;
857a225f079SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
858a225f079SVille Syrjälä 		vtotal /= 2;
859a225f079SVille Syrjälä 
86091d14251STvrtko Ursulin 	if (IS_GEN2(dev_priv))
86175aa3f63SVille Syrjälä 		position = I915_READ_FW(PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
862a225f079SVille Syrjälä 	else
86375aa3f63SVille Syrjälä 		position = I915_READ_FW(PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
864a225f079SVille Syrjälä 
865a225f079SVille Syrjälä 	/*
86641b578fbSJesse Barnes 	 * On HSW, the DSL reg (0x70000) appears to return 0 if we
86741b578fbSJesse Barnes 	 * read it just before the start of vblank.  So try it again
86841b578fbSJesse Barnes 	 * so we don't accidentally end up spanning a vblank frame
86941b578fbSJesse Barnes 	 * increment, causing the pipe_update_end() code to squak at us.
87041b578fbSJesse Barnes 	 *
87141b578fbSJesse Barnes 	 * The nature of this problem means we can't simply check the ISR
87241b578fbSJesse Barnes 	 * bit and return the vblank start value; nor can we use the scanline
87341b578fbSJesse Barnes 	 * debug register in the transcoder as it appears to have the same
87441b578fbSJesse Barnes 	 * problem.  We may need to extend this to include other platforms,
87541b578fbSJesse Barnes 	 * but so far testing only shows the problem on HSW.
87641b578fbSJesse Barnes 	 */
87791d14251STvrtko Ursulin 	if (HAS_DDI(dev_priv) && !position) {
87841b578fbSJesse Barnes 		int i, temp;
87941b578fbSJesse Barnes 
88041b578fbSJesse Barnes 		for (i = 0; i < 100; i++) {
88141b578fbSJesse Barnes 			udelay(1);
882707bdd3fSVille Syrjälä 			temp = I915_READ_FW(PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
88341b578fbSJesse Barnes 			if (temp != position) {
88441b578fbSJesse Barnes 				position = temp;
88541b578fbSJesse Barnes 				break;
88641b578fbSJesse Barnes 			}
88741b578fbSJesse Barnes 		}
88841b578fbSJesse Barnes 	}
88941b578fbSJesse Barnes 
89041b578fbSJesse Barnes 	/*
89180715b2fSVille Syrjälä 	 * See update_scanline_offset() for the details on the
89280715b2fSVille Syrjälä 	 * scanline_offset adjustment.
893a225f079SVille Syrjälä 	 */
89480715b2fSVille Syrjälä 	return (position + crtc->scanline_offset) % vtotal;
895a225f079SVille Syrjälä }
896a225f079SVille Syrjälä 
8971bf6ad62SDaniel Vetter static bool i915_get_crtc_scanoutpos(struct drm_device *dev, unsigned int pipe,
8981bf6ad62SDaniel Vetter 				     bool in_vblank_irq, int *vpos, int *hpos,
8993bb403bfSVille Syrjälä 				     ktime_t *stime, ktime_t *etime,
9003bb403bfSVille Syrjälä 				     const struct drm_display_mode *mode)
9010af7e4dfSMario Kleiner {
902fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
90398187836SVille Syrjälä 	struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
90498187836SVille Syrjälä 								pipe);
9053aa18df8SVille Syrjälä 	int position;
90678e8fc6bSVille Syrjälä 	int vbl_start, vbl_end, hsync_start, htotal, vtotal;
907ad3543edSMario Kleiner 	unsigned long irqflags;
9080af7e4dfSMario Kleiner 
909fc467a22SMaarten Lankhorst 	if (WARN_ON(!mode->crtc_clock)) {
9100af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
9119db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
9121bf6ad62SDaniel Vetter 		return false;
9130af7e4dfSMario Kleiner 	}
9140af7e4dfSMario Kleiner 
915c2baf4b7SVille Syrjälä 	htotal = mode->crtc_htotal;
91678e8fc6bSVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
917c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
918c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
919c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
9200af7e4dfSMario Kleiner 
921d31faf65SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
922d31faf65SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
923d31faf65SVille Syrjälä 		vbl_end /= 2;
924d31faf65SVille Syrjälä 		vtotal /= 2;
925d31faf65SVille Syrjälä 	}
926d31faf65SVille Syrjälä 
927ad3543edSMario Kleiner 	/*
928ad3543edSMario Kleiner 	 * Lock uncore.lock, as we will do multiple timing critical raw
929ad3543edSMario Kleiner 	 * register reads, potentially with preemption disabled, so the
930ad3543edSMario Kleiner 	 * following code must not block on uncore.lock.
931ad3543edSMario Kleiner 	 */
932ad3543edSMario Kleiner 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
933ad3543edSMario Kleiner 
934ad3543edSMario Kleiner 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
935ad3543edSMario Kleiner 
936ad3543edSMario Kleiner 	/* Get optional system timestamp before query. */
937ad3543edSMario Kleiner 	if (stime)
938ad3543edSMario Kleiner 		*stime = ktime_get();
939ad3543edSMario Kleiner 
94091d14251STvrtko Ursulin 	if (IS_GEN2(dev_priv) || IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5) {
9410af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
9420af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
9430af7e4dfSMario Kleiner 		 */
944a225f079SVille Syrjälä 		position = __intel_get_crtc_scanline(intel_crtc);
9450af7e4dfSMario Kleiner 	} else {
9460af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
9470af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
9480af7e4dfSMario Kleiner 		 * scanout position.
9490af7e4dfSMario Kleiner 		 */
95075aa3f63SVille Syrjälä 		position = (I915_READ_FW(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
9510af7e4dfSMario Kleiner 
9523aa18df8SVille Syrjälä 		/* convert to pixel counts */
9533aa18df8SVille Syrjälä 		vbl_start *= htotal;
9543aa18df8SVille Syrjälä 		vbl_end *= htotal;
9553aa18df8SVille Syrjälä 		vtotal *= htotal;
95678e8fc6bSVille Syrjälä 
95778e8fc6bSVille Syrjälä 		/*
9587e78f1cbSVille Syrjälä 		 * In interlaced modes, the pixel counter counts all pixels,
9597e78f1cbSVille Syrjälä 		 * so one field will have htotal more pixels. In order to avoid
9607e78f1cbSVille Syrjälä 		 * the reported position from jumping backwards when the pixel
9617e78f1cbSVille Syrjälä 		 * counter is beyond the length of the shorter field, just
9627e78f1cbSVille Syrjälä 		 * clamp the position the length of the shorter field. This
9637e78f1cbSVille Syrjälä 		 * matches how the scanline counter based position works since
9647e78f1cbSVille Syrjälä 		 * the scanline counter doesn't count the two half lines.
9657e78f1cbSVille Syrjälä 		 */
9667e78f1cbSVille Syrjälä 		if (position >= vtotal)
9677e78f1cbSVille Syrjälä 			position = vtotal - 1;
9687e78f1cbSVille Syrjälä 
9697e78f1cbSVille Syrjälä 		/*
97078e8fc6bSVille Syrjälä 		 * Start of vblank interrupt is triggered at start of hsync,
97178e8fc6bSVille Syrjälä 		 * just prior to the first active line of vblank. However we
97278e8fc6bSVille Syrjälä 		 * consider lines to start at the leading edge of horizontal
97378e8fc6bSVille Syrjälä 		 * active. So, should we get here before we've crossed into
97478e8fc6bSVille Syrjälä 		 * the horizontal active of the first line in vblank, we would
97578e8fc6bSVille Syrjälä 		 * not set the DRM_SCANOUTPOS_INVBL flag. In order to fix that,
97678e8fc6bSVille Syrjälä 		 * always add htotal-hsync_start to the current pixel position.
97778e8fc6bSVille Syrjälä 		 */
97878e8fc6bSVille Syrjälä 		position = (position + htotal - hsync_start) % vtotal;
9793aa18df8SVille Syrjälä 	}
9803aa18df8SVille Syrjälä 
981ad3543edSMario Kleiner 	/* Get optional system timestamp after query. */
982ad3543edSMario Kleiner 	if (etime)
983ad3543edSMario Kleiner 		*etime = ktime_get();
984ad3543edSMario Kleiner 
985ad3543edSMario Kleiner 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
986ad3543edSMario Kleiner 
987ad3543edSMario Kleiner 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
988ad3543edSMario Kleiner 
9893aa18df8SVille Syrjälä 	/*
9903aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
9913aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
9923aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
9933aa18df8SVille Syrjälä 	 * up since vbl_end.
9943aa18df8SVille Syrjälä 	 */
9953aa18df8SVille Syrjälä 	if (position >= vbl_start)
9963aa18df8SVille Syrjälä 		position -= vbl_end;
9973aa18df8SVille Syrjälä 	else
9983aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
9993aa18df8SVille Syrjälä 
100091d14251STvrtko Ursulin 	if (IS_GEN2(dev_priv) || IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5) {
10013aa18df8SVille Syrjälä 		*vpos = position;
10023aa18df8SVille Syrjälä 		*hpos = 0;
10033aa18df8SVille Syrjälä 	} else {
10040af7e4dfSMario Kleiner 		*vpos = position / htotal;
10050af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
10060af7e4dfSMario Kleiner 	}
10070af7e4dfSMario Kleiner 
10081bf6ad62SDaniel Vetter 	return true;
10090af7e4dfSMario Kleiner }
10100af7e4dfSMario Kleiner 
1011a225f079SVille Syrjälä int intel_get_crtc_scanline(struct intel_crtc *crtc)
1012a225f079SVille Syrjälä {
1013fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1014a225f079SVille Syrjälä 	unsigned long irqflags;
1015a225f079SVille Syrjälä 	int position;
1016a225f079SVille Syrjälä 
1017a225f079SVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
1018a225f079SVille Syrjälä 	position = __intel_get_crtc_scanline(crtc);
1019a225f079SVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
1020a225f079SVille Syrjälä 
1021a225f079SVille Syrjälä 	return position;
1022a225f079SVille Syrjälä }
1023a225f079SVille Syrjälä 
102491d14251STvrtko Ursulin static void ironlake_rps_change_irq_handler(struct drm_i915_private *dev_priv)
1025f97108d1SJesse Barnes {
1026b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
10279270388eSDaniel Vetter 	u8 new_delay;
10289270388eSDaniel Vetter 
1029d0ecd7e2SDaniel Vetter 	spin_lock(&mchdev_lock);
1030f97108d1SJesse Barnes 
103173edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
103273edd18fSDaniel Vetter 
103320e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
10349270388eSDaniel Vetter 
10357648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
1036b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
1037b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
1038f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
1039f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
1040f97108d1SJesse Barnes 
1041f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
1042b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
104320e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
104420e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
104520e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
104620e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
1047b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
104820e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
104920e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
105020e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
105120e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
1052f97108d1SJesse Barnes 	}
1053f97108d1SJesse Barnes 
105491d14251STvrtko Ursulin 	if (ironlake_set_drps(dev_priv, new_delay))
105520e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
1056f97108d1SJesse Barnes 
1057d0ecd7e2SDaniel Vetter 	spin_unlock(&mchdev_lock);
10589270388eSDaniel Vetter 
1059f97108d1SJesse Barnes 	return;
1060f97108d1SJesse Barnes }
1061f97108d1SJesse Barnes 
10620bc40be8STvrtko Ursulin static void notify_ring(struct intel_engine_cs *engine)
1063549f7365SChris Wilson {
106456299fb7SChris Wilson 	struct drm_i915_gem_request *rq = NULL;
106556299fb7SChris Wilson 	struct intel_wait *wait;
1066dffabc8fSTvrtko Ursulin 
10672246bea6SChris Wilson 	atomic_inc(&engine->irq_count);
1068538b257dSChris Wilson 	set_bit(ENGINE_IRQ_BREADCRUMB, &engine->irq_posted);
106956299fb7SChris Wilson 
107061d3dc70SChris Wilson 	spin_lock(&engine->breadcrumbs.irq_lock);
107161d3dc70SChris Wilson 	wait = engine->breadcrumbs.irq_wait;
107256299fb7SChris Wilson 	if (wait) {
107317b51ad8SChris Wilson 		bool wakeup = engine->irq_seqno_barrier;
107417b51ad8SChris Wilson 
107556299fb7SChris Wilson 		/* We use a callback from the dma-fence to submit
107656299fb7SChris Wilson 		 * requests after waiting on our own requests. To
107756299fb7SChris Wilson 		 * ensure minimum delay in queuing the next request to
107856299fb7SChris Wilson 		 * hardware, signal the fence now rather than wait for
107956299fb7SChris Wilson 		 * the signaler to be woken up. We still wake up the
108056299fb7SChris Wilson 		 * waiter in order to handle the irq-seqno coherency
108156299fb7SChris Wilson 		 * issues (we may receive the interrupt before the
108256299fb7SChris Wilson 		 * seqno is written, see __i915_request_irq_complete())
108356299fb7SChris Wilson 		 * and to handle coalescing of multiple seqno updates
108456299fb7SChris Wilson 		 * and many waiters.
108556299fb7SChris Wilson 		 */
108656299fb7SChris Wilson 		if (i915_seqno_passed(intel_engine_get_seqno(engine),
108717b51ad8SChris Wilson 				      wait->seqno)) {
1088de4d2106SChris Wilson 			struct drm_i915_gem_request *waiter = wait->request;
1089de4d2106SChris Wilson 
109017b51ad8SChris Wilson 			wakeup = true;
109117b51ad8SChris Wilson 			if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
1092de4d2106SChris Wilson 				      &waiter->fence.flags) &&
1093de4d2106SChris Wilson 			    intel_wait_check_request(wait, waiter))
1094de4d2106SChris Wilson 				rq = i915_gem_request_get(waiter);
109517b51ad8SChris Wilson 		}
109656299fb7SChris Wilson 
109717b51ad8SChris Wilson 		if (wakeup)
109856299fb7SChris Wilson 			wake_up_process(wait->tsk);
109967b807a8SChris Wilson 	} else {
110067b807a8SChris Wilson 		__intel_engine_disarm_breadcrumbs(engine);
110156299fb7SChris Wilson 	}
110261d3dc70SChris Wilson 	spin_unlock(&engine->breadcrumbs.irq_lock);
110356299fb7SChris Wilson 
110424754d75SChris Wilson 	if (rq) {
110556299fb7SChris Wilson 		dma_fence_signal(&rq->fence);
110624754d75SChris Wilson 		i915_gem_request_put(rq);
110724754d75SChris Wilson 	}
110856299fb7SChris Wilson 
110956299fb7SChris Wilson 	trace_intel_engine_notify(engine, wait);
1110549f7365SChris Wilson }
1111549f7365SChris Wilson 
111243cf3bf0SChris Wilson static void vlv_c0_read(struct drm_i915_private *dev_priv,
111343cf3bf0SChris Wilson 			struct intel_rps_ei *ei)
111431685c25SDeepak S {
1115679cb6c1SMika Kuoppala 	ei->ktime = ktime_get_raw();
111643cf3bf0SChris Wilson 	ei->render_c0 = I915_READ(VLV_RENDER_C0_COUNT);
111743cf3bf0SChris Wilson 	ei->media_c0 = I915_READ(VLV_MEDIA_C0_COUNT);
111831685c25SDeepak S }
111931685c25SDeepak S 
112043cf3bf0SChris Wilson void gen6_rps_reset_ei(struct drm_i915_private *dev_priv)
112143cf3bf0SChris Wilson {
1122e0e8c7cbSChris Wilson 	memset(&dev_priv->rps.ei, 0, sizeof(dev_priv->rps.ei));
112343cf3bf0SChris Wilson }
112443cf3bf0SChris Wilson 
112543cf3bf0SChris Wilson static u32 vlv_wa_c0_ei(struct drm_i915_private *dev_priv, u32 pm_iir)
112643cf3bf0SChris Wilson {
1127e0e8c7cbSChris Wilson 	const struct intel_rps_ei *prev = &dev_priv->rps.ei;
112843cf3bf0SChris Wilson 	struct intel_rps_ei now;
112943cf3bf0SChris Wilson 	u32 events = 0;
113043cf3bf0SChris Wilson 
1131e0e8c7cbSChris Wilson 	if ((pm_iir & GEN6_PM_RP_UP_EI_EXPIRED) == 0)
113243cf3bf0SChris Wilson 		return 0;
113343cf3bf0SChris Wilson 
113443cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &now);
113531685c25SDeepak S 
1136679cb6c1SMika Kuoppala 	if (prev->ktime) {
1137e0e8c7cbSChris Wilson 		u64 time, c0;
1138569884e3SChris Wilson 		u32 render, media;
1139e0e8c7cbSChris Wilson 
1140679cb6c1SMika Kuoppala 		time = ktime_us_delta(now.ktime, prev->ktime);
11418f68d591SChris Wilson 
1142e0e8c7cbSChris Wilson 		time *= dev_priv->czclk_freq;
1143e0e8c7cbSChris Wilson 
1144e0e8c7cbSChris Wilson 		/* Workload can be split between render + media,
1145e0e8c7cbSChris Wilson 		 * e.g. SwapBuffers being blitted in X after being rendered in
1146e0e8c7cbSChris Wilson 		 * mesa. To account for this we need to combine both engines
1147e0e8c7cbSChris Wilson 		 * into our activity counter.
1148e0e8c7cbSChris Wilson 		 */
1149569884e3SChris Wilson 		render = now.render_c0 - prev->render_c0;
1150569884e3SChris Wilson 		media = now.media_c0 - prev->media_c0;
1151569884e3SChris Wilson 		c0 = max(render, media);
11526b7f6aa7SMika Kuoppala 		c0 *= 1000 * 100 << 8; /* to usecs and scale to threshold% */
1153e0e8c7cbSChris Wilson 
1154e0e8c7cbSChris Wilson 		if (c0 > time * dev_priv->rps.up_threshold)
1155e0e8c7cbSChris Wilson 			events = GEN6_PM_RP_UP_THRESHOLD;
1156e0e8c7cbSChris Wilson 		else if (c0 < time * dev_priv->rps.down_threshold)
1157e0e8c7cbSChris Wilson 			events = GEN6_PM_RP_DOWN_THRESHOLD;
115831685c25SDeepak S 	}
115931685c25SDeepak S 
1160e0e8c7cbSChris Wilson 	dev_priv->rps.ei = now;
116143cf3bf0SChris Wilson 	return events;
116231685c25SDeepak S }
116331685c25SDeepak S 
11644912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
11653b8d8d91SJesse Barnes {
11662d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
11672d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, rps.work);
11687c0a16adSChris Wilson 	bool client_boost = false;
11698d3afd7dSChris Wilson 	int new_delay, adj, min, max;
11707c0a16adSChris Wilson 	u32 pm_iir = 0;
11713b8d8d91SJesse Barnes 
117259cdb63dSDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
11737c0a16adSChris Wilson 	if (dev_priv->rps.interrupts_enabled) {
11747c0a16adSChris Wilson 		pm_iir = fetch_and_zero(&dev_priv->rps.pm_iir);
11757b92c1bdSChris Wilson 		client_boost = atomic_read(&dev_priv->rps.num_waiters);
1176d4d70aa5SImre Deak 	}
117759cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
11784912d041SBen Widawsky 
117960611c13SPaulo Zanoni 	/* Make sure we didn't queue anything we're not going to process. */
1180a6706b45SDeepak S 	WARN_ON(pm_iir & ~dev_priv->pm_rps_events);
11818d3afd7dSChris Wilson 	if ((pm_iir & dev_priv->pm_rps_events) == 0 && !client_boost)
11827c0a16adSChris Wilson 		goto out;
11833b8d8d91SJesse Barnes 
11844fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
11857b9e0ae6SChris Wilson 
118643cf3bf0SChris Wilson 	pm_iir |= vlv_wa_c0_ei(dev_priv, pm_iir);
118743cf3bf0SChris Wilson 
1188dd75fdc8SChris Wilson 	adj = dev_priv->rps.last_adj;
1189edcf284bSChris Wilson 	new_delay = dev_priv->rps.cur_freq;
11908d3afd7dSChris Wilson 	min = dev_priv->rps.min_freq_softlimit;
11918d3afd7dSChris Wilson 	max = dev_priv->rps.max_freq_softlimit;
11927b92c1bdSChris Wilson 	if (client_boost)
119329ecd78dSChris Wilson 		max = dev_priv->rps.max_freq;
119429ecd78dSChris Wilson 	if (client_boost && new_delay < dev_priv->rps.boost_freq) {
119529ecd78dSChris Wilson 		new_delay = dev_priv->rps.boost_freq;
11968d3afd7dSChris Wilson 		adj = 0;
11978d3afd7dSChris Wilson 	} else if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
1198dd75fdc8SChris Wilson 		if (adj > 0)
1199dd75fdc8SChris Wilson 			adj *= 2;
1200edcf284bSChris Wilson 		else /* CHV needs even encode values */
1201edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? 2 : 1;
12027e79a683SSagar Arun Kamble 
12037e79a683SSagar Arun Kamble 		if (new_delay >= dev_priv->rps.max_freq_softlimit)
12047e79a683SSagar Arun Kamble 			adj = 0;
12057b92c1bdSChris Wilson 	} else if (client_boost) {
1206f5a4c67dSChris Wilson 		adj = 0;
1207dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
1208b39fb297SBen Widawsky 		if (dev_priv->rps.cur_freq > dev_priv->rps.efficient_freq)
1209b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
121017136d54SChris Wilson 		else if (dev_priv->rps.cur_freq > dev_priv->rps.min_freq_softlimit)
1211b39fb297SBen Widawsky 			new_delay = dev_priv->rps.min_freq_softlimit;
1212dd75fdc8SChris Wilson 		adj = 0;
1213dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1214dd75fdc8SChris Wilson 		if (adj < 0)
1215dd75fdc8SChris Wilson 			adj *= 2;
1216edcf284bSChris Wilson 		else /* CHV needs even encode values */
1217edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? -2 : -1;
12187e79a683SSagar Arun Kamble 
12197e79a683SSagar Arun Kamble 		if (new_delay <= dev_priv->rps.min_freq_softlimit)
12207e79a683SSagar Arun Kamble 			adj = 0;
1221dd75fdc8SChris Wilson 	} else { /* unknown event */
1222edcf284bSChris Wilson 		adj = 0;
1223dd75fdc8SChris Wilson 	}
12243b8d8d91SJesse Barnes 
1225edcf284bSChris Wilson 	dev_priv->rps.last_adj = adj;
1226edcf284bSChris Wilson 
122779249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
122879249636SBen Widawsky 	 * interrupt
122979249636SBen Widawsky 	 */
1230edcf284bSChris Wilson 	new_delay += adj;
12318d3afd7dSChris Wilson 	new_delay = clamp_t(int, new_delay, min, max);
123227544369SDeepak S 
12339fcee2f7SChris Wilson 	if (intel_set_rps(dev_priv, new_delay)) {
12349fcee2f7SChris Wilson 		DRM_DEBUG_DRIVER("Failed to set new GPU frequency\n");
12359fcee2f7SChris Wilson 		dev_priv->rps.last_adj = 0;
12369fcee2f7SChris Wilson 	}
12373b8d8d91SJesse Barnes 
12384fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
12397c0a16adSChris Wilson 
12407c0a16adSChris Wilson out:
12417c0a16adSChris Wilson 	/* Make sure not to corrupt PMIMR state used by ringbuffer on GEN6 */
12427c0a16adSChris Wilson 	spin_lock_irq(&dev_priv->irq_lock);
12437c0a16adSChris Wilson 	if (dev_priv->rps.interrupts_enabled)
12447c0a16adSChris Wilson 		gen6_unmask_pm_irq(dev_priv, dev_priv->pm_rps_events);
12457c0a16adSChris Wilson 	spin_unlock_irq(&dev_priv->irq_lock);
12463b8d8d91SJesse Barnes }
12473b8d8d91SJesse Barnes 
1248e3689190SBen Widawsky 
1249e3689190SBen Widawsky /**
1250e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
1251e3689190SBen Widawsky  * occurred.
1252e3689190SBen Widawsky  * @work: workqueue struct
1253e3689190SBen Widawsky  *
1254e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
1255e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
1256e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
1257e3689190SBen Widawsky  */
1258e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
1259e3689190SBen Widawsky {
12602d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
1261cefcff8fSJoonas Lahtinen 		container_of(work, typeof(*dev_priv), l3_parity.error_work);
1262e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
126335a85ac6SBen Widawsky 	char *parity_event[6];
1264e3689190SBen Widawsky 	uint32_t misccpctl;
126535a85ac6SBen Widawsky 	uint8_t slice = 0;
1266e3689190SBen Widawsky 
1267e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
1268e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
1269e3689190SBen Widawsky 	 * any time we access those registers.
1270e3689190SBen Widawsky 	 */
127191c8a326SChris Wilson 	mutex_lock(&dev_priv->drm.struct_mutex);
1272e3689190SBen Widawsky 
127335a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
127435a85ac6SBen Widawsky 	if (WARN_ON(!dev_priv->l3_parity.which_slice))
127535a85ac6SBen Widawsky 		goto out;
127635a85ac6SBen Widawsky 
1277e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
1278e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1279e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
1280e3689190SBen Widawsky 
128135a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
1282f0f59a00SVille Syrjälä 		i915_reg_t reg;
128335a85ac6SBen Widawsky 
128435a85ac6SBen Widawsky 		slice--;
12852d1fe073SJoonas Lahtinen 		if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv)))
128635a85ac6SBen Widawsky 			break;
128735a85ac6SBen Widawsky 
128835a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
128935a85ac6SBen Widawsky 
12906fa1c5f1SVille Syrjälä 		reg = GEN7_L3CDERRST1(slice);
129135a85ac6SBen Widawsky 
129235a85ac6SBen Widawsky 		error_status = I915_READ(reg);
1293e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
1294e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
1295e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1296e3689190SBen Widawsky 
129735a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
129835a85ac6SBen Widawsky 		POSTING_READ(reg);
1299e3689190SBen Widawsky 
1300cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1301e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1302e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1303e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
130435a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
130535a85ac6SBen Widawsky 		parity_event[5] = NULL;
1306e3689190SBen Widawsky 
130791c8a326SChris Wilson 		kobject_uevent_env(&dev_priv->drm.primary->kdev->kobj,
1308e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
1309e3689190SBen Widawsky 
131035a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
131135a85ac6SBen Widawsky 			  slice, row, bank, subbank);
1312e3689190SBen Widawsky 
131335a85ac6SBen Widawsky 		kfree(parity_event[4]);
1314e3689190SBen Widawsky 		kfree(parity_event[3]);
1315e3689190SBen Widawsky 		kfree(parity_event[2]);
1316e3689190SBen Widawsky 		kfree(parity_event[1]);
1317e3689190SBen Widawsky 	}
1318e3689190SBen Widawsky 
131935a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
132035a85ac6SBen Widawsky 
132135a85ac6SBen Widawsky out:
132235a85ac6SBen Widawsky 	WARN_ON(dev_priv->l3_parity.which_slice);
13234cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
13242d1fe073SJoonas Lahtinen 	gen5_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv));
13254cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
132635a85ac6SBen Widawsky 
132791c8a326SChris Wilson 	mutex_unlock(&dev_priv->drm.struct_mutex);
132835a85ac6SBen Widawsky }
132935a85ac6SBen Widawsky 
1330261e40b8SVille Syrjälä static void ivybridge_parity_error_irq_handler(struct drm_i915_private *dev_priv,
1331261e40b8SVille Syrjälä 					       u32 iir)
1332e3689190SBen Widawsky {
1333261e40b8SVille Syrjälä 	if (!HAS_L3_DPF(dev_priv))
1334e3689190SBen Widawsky 		return;
1335e3689190SBen Widawsky 
1336d0ecd7e2SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1337261e40b8SVille Syrjälä 	gen5_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv));
1338d0ecd7e2SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
1339e3689190SBen Widawsky 
1340261e40b8SVille Syrjälä 	iir &= GT_PARITY_ERROR(dev_priv);
134135a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
134235a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 1;
134335a85ac6SBen Widawsky 
134435a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
134535a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 0;
134635a85ac6SBen Widawsky 
1347a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
1348e3689190SBen Widawsky }
1349e3689190SBen Widawsky 
1350261e40b8SVille Syrjälä static void ilk_gt_irq_handler(struct drm_i915_private *dev_priv,
1351f1af8fc1SPaulo Zanoni 			       u32 gt_iir)
1352f1af8fc1SPaulo Zanoni {
1353f8973c21SChris Wilson 	if (gt_iir & GT_RENDER_USER_INTERRUPT)
13543b3f1650SAkash Goel 		notify_ring(dev_priv->engine[RCS]);
1355f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
13563b3f1650SAkash Goel 		notify_ring(dev_priv->engine[VCS]);
1357f1af8fc1SPaulo Zanoni }
1358f1af8fc1SPaulo Zanoni 
1359261e40b8SVille Syrjälä static void snb_gt_irq_handler(struct drm_i915_private *dev_priv,
1360e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1361e7b4c6b1SDaniel Vetter {
1362f8973c21SChris Wilson 	if (gt_iir & GT_RENDER_USER_INTERRUPT)
13633b3f1650SAkash Goel 		notify_ring(dev_priv->engine[RCS]);
1364cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
13653b3f1650SAkash Goel 		notify_ring(dev_priv->engine[VCS]);
1366cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
13673b3f1650SAkash Goel 		notify_ring(dev_priv->engine[BCS]);
1368e7b4c6b1SDaniel Vetter 
1369cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1370cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
1371aaecdf61SDaniel Vetter 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT))
1372aaecdf61SDaniel Vetter 		DRM_DEBUG("Command parser error, gt_iir 0x%08x\n", gt_iir);
1373e3689190SBen Widawsky 
1374261e40b8SVille Syrjälä 	if (gt_iir & GT_PARITY_ERROR(dev_priv))
1375261e40b8SVille Syrjälä 		ivybridge_parity_error_irq_handler(dev_priv, gt_iir);
1376e7b4c6b1SDaniel Vetter }
1377e7b4c6b1SDaniel Vetter 
13785d3d69d5SChris Wilson static void
13790bc40be8STvrtko Ursulin gen8_cs_irq_handler(struct intel_engine_cs *engine, u32 iir, int test_shift)
1380fbcc1a0cSNick Hoath {
1381b620e870SMika Kuoppala 	struct intel_engine_execlists * const execlists = &engine->execlists;
138231de7350SChris Wilson 	bool tasklet = false;
1383f747026cSChris Wilson 
1384f747026cSChris Wilson 	if (iir & (GT_CONTEXT_SWITCH_INTERRUPT << test_shift)) {
1385b620e870SMika Kuoppala 		if (port_count(&execlists->port[0])) {
1386955a4b89SChris Wilson 			__set_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
138731de7350SChris Wilson 			tasklet = true;
1388f747026cSChris Wilson 		}
1389a4b2b015SChris Wilson 	}
139031de7350SChris Wilson 
139131de7350SChris Wilson 	if (iir & (GT_RENDER_USER_INTERRUPT << test_shift)) {
139231de7350SChris Wilson 		notify_ring(engine);
13934f044a88SMichal Wajdeczko 		tasklet |= i915_modparams.enable_guc_submission;
139431de7350SChris Wilson 	}
139531de7350SChris Wilson 
139631de7350SChris Wilson 	if (tasklet)
1397b620e870SMika Kuoppala 		tasklet_hi_schedule(&execlists->irq_tasklet);
1398fbcc1a0cSNick Hoath }
1399fbcc1a0cSNick Hoath 
1400e30e251aSVille Syrjälä static irqreturn_t gen8_gt_irq_ack(struct drm_i915_private *dev_priv,
1401e30e251aSVille Syrjälä 				   u32 master_ctl,
1402e30e251aSVille Syrjälä 				   u32 gt_iir[4])
1403abd58f01SBen Widawsky {
1404abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
1405abd58f01SBen Widawsky 
1406abd58f01SBen Widawsky 	if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
1407e30e251aSVille Syrjälä 		gt_iir[0] = I915_READ_FW(GEN8_GT_IIR(0));
1408e30e251aSVille Syrjälä 		if (gt_iir[0]) {
1409e30e251aSVille Syrjälä 			I915_WRITE_FW(GEN8_GT_IIR(0), gt_iir[0]);
1410abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1411abd58f01SBen Widawsky 		} else
1412abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT0)!\n");
1413abd58f01SBen Widawsky 	}
1414abd58f01SBen Widawsky 
141585f9b5f9SZhao Yakui 	if (master_ctl & (GEN8_GT_VCS1_IRQ | GEN8_GT_VCS2_IRQ)) {
1416e30e251aSVille Syrjälä 		gt_iir[1] = I915_READ_FW(GEN8_GT_IIR(1));
1417e30e251aSVille Syrjälä 		if (gt_iir[1]) {
1418e30e251aSVille Syrjälä 			I915_WRITE_FW(GEN8_GT_IIR(1), gt_iir[1]);
1419abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1420abd58f01SBen Widawsky 		} else
1421abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT1)!\n");
1422abd58f01SBen Widawsky 	}
1423abd58f01SBen Widawsky 
142474cdb337SChris Wilson 	if (master_ctl & GEN8_GT_VECS_IRQ) {
1425e30e251aSVille Syrjälä 		gt_iir[3] = I915_READ_FW(GEN8_GT_IIR(3));
1426e30e251aSVille Syrjälä 		if (gt_iir[3]) {
1427e30e251aSVille Syrjälä 			I915_WRITE_FW(GEN8_GT_IIR(3), gt_iir[3]);
142874cdb337SChris Wilson 			ret = IRQ_HANDLED;
142974cdb337SChris Wilson 		} else
143074cdb337SChris Wilson 			DRM_ERROR("The master control interrupt lied (GT3)!\n");
143174cdb337SChris Wilson 	}
143274cdb337SChris Wilson 
143326705e20SSagar Arun Kamble 	if (master_ctl & (GEN8_GT_PM_IRQ | GEN8_GT_GUC_IRQ)) {
1434e30e251aSVille Syrjälä 		gt_iir[2] = I915_READ_FW(GEN8_GT_IIR(2));
143526705e20SSagar Arun Kamble 		if (gt_iir[2] & (dev_priv->pm_rps_events |
143626705e20SSagar Arun Kamble 				 dev_priv->pm_guc_events)) {
1437cb0d205eSChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(2),
143826705e20SSagar Arun Kamble 				      gt_iir[2] & (dev_priv->pm_rps_events |
143926705e20SSagar Arun Kamble 						   dev_priv->pm_guc_events));
144038cc46d7SOscar Mateo 			ret = IRQ_HANDLED;
14410961021aSBen Widawsky 		} else
14420961021aSBen Widawsky 			DRM_ERROR("The master control interrupt lied (PM)!\n");
14430961021aSBen Widawsky 	}
14440961021aSBen Widawsky 
1445abd58f01SBen Widawsky 	return ret;
1446abd58f01SBen Widawsky }
1447abd58f01SBen Widawsky 
1448e30e251aSVille Syrjälä static void gen8_gt_irq_handler(struct drm_i915_private *dev_priv,
1449e30e251aSVille Syrjälä 				u32 gt_iir[4])
1450e30e251aSVille Syrjälä {
1451e30e251aSVille Syrjälä 	if (gt_iir[0]) {
14523b3f1650SAkash Goel 		gen8_cs_irq_handler(dev_priv->engine[RCS],
1453e30e251aSVille Syrjälä 				    gt_iir[0], GEN8_RCS_IRQ_SHIFT);
14543b3f1650SAkash Goel 		gen8_cs_irq_handler(dev_priv->engine[BCS],
1455e30e251aSVille Syrjälä 				    gt_iir[0], GEN8_BCS_IRQ_SHIFT);
1456e30e251aSVille Syrjälä 	}
1457e30e251aSVille Syrjälä 
1458e30e251aSVille Syrjälä 	if (gt_iir[1]) {
14593b3f1650SAkash Goel 		gen8_cs_irq_handler(dev_priv->engine[VCS],
1460e30e251aSVille Syrjälä 				    gt_iir[1], GEN8_VCS1_IRQ_SHIFT);
14613b3f1650SAkash Goel 		gen8_cs_irq_handler(dev_priv->engine[VCS2],
1462e30e251aSVille Syrjälä 				    gt_iir[1], GEN8_VCS2_IRQ_SHIFT);
1463e30e251aSVille Syrjälä 	}
1464e30e251aSVille Syrjälä 
1465e30e251aSVille Syrjälä 	if (gt_iir[3])
14663b3f1650SAkash Goel 		gen8_cs_irq_handler(dev_priv->engine[VECS],
1467e30e251aSVille Syrjälä 				    gt_iir[3], GEN8_VECS_IRQ_SHIFT);
1468e30e251aSVille Syrjälä 
1469e30e251aSVille Syrjälä 	if (gt_iir[2] & dev_priv->pm_rps_events)
1470e30e251aSVille Syrjälä 		gen6_rps_irq_handler(dev_priv, gt_iir[2]);
147126705e20SSagar Arun Kamble 
147226705e20SSagar Arun Kamble 	if (gt_iir[2] & dev_priv->pm_guc_events)
147326705e20SSagar Arun Kamble 		gen9_guc_irq_handler(dev_priv, gt_iir[2]);
1474e30e251aSVille Syrjälä }
1475e30e251aSVille Syrjälä 
147663c88d22SImre Deak static bool bxt_port_hotplug_long_detect(enum port port, u32 val)
147763c88d22SImre Deak {
147863c88d22SImre Deak 	switch (port) {
147963c88d22SImre Deak 	case PORT_A:
1480195baa06SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
148163c88d22SImre Deak 	case PORT_B:
148263c88d22SImre Deak 		return val & PORTB_HOTPLUG_LONG_DETECT;
148363c88d22SImre Deak 	case PORT_C:
148463c88d22SImre Deak 		return val & PORTC_HOTPLUG_LONG_DETECT;
148563c88d22SImre Deak 	default:
148663c88d22SImre Deak 		return false;
148763c88d22SImre Deak 	}
148863c88d22SImre Deak }
148963c88d22SImre Deak 
14906dbf30ceSVille Syrjälä static bool spt_port_hotplug2_long_detect(enum port port, u32 val)
14916dbf30ceSVille Syrjälä {
14926dbf30ceSVille Syrjälä 	switch (port) {
14936dbf30ceSVille Syrjälä 	case PORT_E:
14946dbf30ceSVille Syrjälä 		return val & PORTE_HOTPLUG_LONG_DETECT;
14956dbf30ceSVille Syrjälä 	default:
14966dbf30ceSVille Syrjälä 		return false;
14976dbf30ceSVille Syrjälä 	}
14986dbf30ceSVille Syrjälä }
14996dbf30ceSVille Syrjälä 
150074c0b395SVille Syrjälä static bool spt_port_hotplug_long_detect(enum port port, u32 val)
150174c0b395SVille Syrjälä {
150274c0b395SVille Syrjälä 	switch (port) {
150374c0b395SVille Syrjälä 	case PORT_A:
150474c0b395SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
150574c0b395SVille Syrjälä 	case PORT_B:
150674c0b395SVille Syrjälä 		return val & PORTB_HOTPLUG_LONG_DETECT;
150774c0b395SVille Syrjälä 	case PORT_C:
150874c0b395SVille Syrjälä 		return val & PORTC_HOTPLUG_LONG_DETECT;
150974c0b395SVille Syrjälä 	case PORT_D:
151074c0b395SVille Syrjälä 		return val & PORTD_HOTPLUG_LONG_DETECT;
151174c0b395SVille Syrjälä 	default:
151274c0b395SVille Syrjälä 		return false;
151374c0b395SVille Syrjälä 	}
151474c0b395SVille Syrjälä }
151574c0b395SVille Syrjälä 
1516e4ce95aaSVille Syrjälä static bool ilk_port_hotplug_long_detect(enum port port, u32 val)
1517e4ce95aaSVille Syrjälä {
1518e4ce95aaSVille Syrjälä 	switch (port) {
1519e4ce95aaSVille Syrjälä 	case PORT_A:
1520e4ce95aaSVille Syrjälä 		return val & DIGITAL_PORTA_HOTPLUG_LONG_DETECT;
1521e4ce95aaSVille Syrjälä 	default:
1522e4ce95aaSVille Syrjälä 		return false;
1523e4ce95aaSVille Syrjälä 	}
1524e4ce95aaSVille Syrjälä }
1525e4ce95aaSVille Syrjälä 
1526676574dfSJani Nikula static bool pch_port_hotplug_long_detect(enum port port, u32 val)
152713cf5504SDave Airlie {
152813cf5504SDave Airlie 	switch (port) {
152913cf5504SDave Airlie 	case PORT_B:
1530676574dfSJani Nikula 		return val & PORTB_HOTPLUG_LONG_DETECT;
153113cf5504SDave Airlie 	case PORT_C:
1532676574dfSJani Nikula 		return val & PORTC_HOTPLUG_LONG_DETECT;
153313cf5504SDave Airlie 	case PORT_D:
1534676574dfSJani Nikula 		return val & PORTD_HOTPLUG_LONG_DETECT;
1535676574dfSJani Nikula 	default:
1536676574dfSJani Nikula 		return false;
153713cf5504SDave Airlie 	}
153813cf5504SDave Airlie }
153913cf5504SDave Airlie 
1540676574dfSJani Nikula static bool i9xx_port_hotplug_long_detect(enum port port, u32 val)
154113cf5504SDave Airlie {
154213cf5504SDave Airlie 	switch (port) {
154313cf5504SDave Airlie 	case PORT_B:
1544676574dfSJani Nikula 		return val & PORTB_HOTPLUG_INT_LONG_PULSE;
154513cf5504SDave Airlie 	case PORT_C:
1546676574dfSJani Nikula 		return val & PORTC_HOTPLUG_INT_LONG_PULSE;
154713cf5504SDave Airlie 	case PORT_D:
1548676574dfSJani Nikula 		return val & PORTD_HOTPLUG_INT_LONG_PULSE;
1549676574dfSJani Nikula 	default:
1550676574dfSJani Nikula 		return false;
155113cf5504SDave Airlie 	}
155213cf5504SDave Airlie }
155313cf5504SDave Airlie 
155442db67d6SVille Syrjälä /*
155542db67d6SVille Syrjälä  * Get a bit mask of pins that have triggered, and which ones may be long.
155642db67d6SVille Syrjälä  * This can be called multiple times with the same masks to accumulate
155742db67d6SVille Syrjälä  * hotplug detection results from several registers.
155842db67d6SVille Syrjälä  *
155942db67d6SVille Syrjälä  * Note that the caller is expected to zero out the masks initially.
156042db67d6SVille Syrjälä  */
1561fd63e2a9SImre Deak static void intel_get_hpd_pins(u32 *pin_mask, u32 *long_mask,
15628c841e57SJani Nikula 			     u32 hotplug_trigger, u32 dig_hotplug_reg,
1563fd63e2a9SImre Deak 			     const u32 hpd[HPD_NUM_PINS],
1564fd63e2a9SImre Deak 			     bool long_pulse_detect(enum port port, u32 val))
1565676574dfSJani Nikula {
15668c841e57SJani Nikula 	enum port port;
1567676574dfSJani Nikula 	int i;
1568676574dfSJani Nikula 
1569676574dfSJani Nikula 	for_each_hpd_pin(i) {
15708c841e57SJani Nikula 		if ((hpd[i] & hotplug_trigger) == 0)
15718c841e57SJani Nikula 			continue;
15728c841e57SJani Nikula 
1573676574dfSJani Nikula 		*pin_mask |= BIT(i);
1574676574dfSJani Nikula 
1575256cfddeSRodrigo Vivi 		port = intel_hpd_pin_to_port(i);
1576256cfddeSRodrigo Vivi 		if (port == PORT_NONE)
1577cc24fcdcSImre Deak 			continue;
1578cc24fcdcSImre Deak 
1579fd63e2a9SImre Deak 		if (long_pulse_detect(port, dig_hotplug_reg))
1580676574dfSJani Nikula 			*long_mask |= BIT(i);
1581676574dfSJani Nikula 	}
1582676574dfSJani Nikula 
1583676574dfSJani Nikula 	DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x, dig 0x%08x, pins 0x%08x\n",
1584676574dfSJani Nikula 			 hotplug_trigger, dig_hotplug_reg, *pin_mask);
1585676574dfSJani Nikula 
1586676574dfSJani Nikula }
1587676574dfSJani Nikula 
158891d14251STvrtko Ursulin static void gmbus_irq_handler(struct drm_i915_private *dev_priv)
1589515ac2bbSDaniel Vetter {
159028c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1591515ac2bbSDaniel Vetter }
1592515ac2bbSDaniel Vetter 
159391d14251STvrtko Ursulin static void dp_aux_irq_handler(struct drm_i915_private *dev_priv)
1594ce99c256SDaniel Vetter {
15959ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1596ce99c256SDaniel Vetter }
1597ce99c256SDaniel Vetter 
15988bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
159991d14251STvrtko Ursulin static void display_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
160091d14251STvrtko Ursulin 					 enum pipe pipe,
1601eba94eb9SDaniel Vetter 					 uint32_t crc0, uint32_t crc1,
1602eba94eb9SDaniel Vetter 					 uint32_t crc2, uint32_t crc3,
16038bc5e955SDaniel Vetter 					 uint32_t crc4)
16048bf1e9f1SShuang He {
16058bf1e9f1SShuang He 	struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
16068bf1e9f1SShuang He 	struct intel_pipe_crc_entry *entry;
16078c6b709dSTomeu Vizoso 	struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
16088c6b709dSTomeu Vizoso 	struct drm_driver *driver = dev_priv->drm.driver;
16098c6b709dSTomeu Vizoso 	uint32_t crcs[5];
1610ac2300d4SDamien Lespiau 	int head, tail;
1611b2c88f5bSDamien Lespiau 
1612d538bbdfSDamien Lespiau 	spin_lock(&pipe_crc->lock);
16138c6b709dSTomeu Vizoso 	if (pipe_crc->source) {
16140c912c79SDamien Lespiau 		if (!pipe_crc->entries) {
1615d538bbdfSDamien Lespiau 			spin_unlock(&pipe_crc->lock);
161634273620SDaniel Vetter 			DRM_DEBUG_KMS("spurious interrupt\n");
16170c912c79SDamien Lespiau 			return;
16180c912c79SDamien Lespiau 		}
16190c912c79SDamien Lespiau 
1620d538bbdfSDamien Lespiau 		head = pipe_crc->head;
1621d538bbdfSDamien Lespiau 		tail = pipe_crc->tail;
1622b2c88f5bSDamien Lespiau 
1623b2c88f5bSDamien Lespiau 		if (CIRC_SPACE(head, tail, INTEL_PIPE_CRC_ENTRIES_NR) < 1) {
1624d538bbdfSDamien Lespiau 			spin_unlock(&pipe_crc->lock);
1625b2c88f5bSDamien Lespiau 			DRM_ERROR("CRC buffer overflowing\n");
1626b2c88f5bSDamien Lespiau 			return;
1627b2c88f5bSDamien Lespiau 		}
1628b2c88f5bSDamien Lespiau 
1629b2c88f5bSDamien Lespiau 		entry = &pipe_crc->entries[head];
16308bf1e9f1SShuang He 
16318c6b709dSTomeu Vizoso 		entry->frame = driver->get_vblank_counter(&dev_priv->drm, pipe);
1632eba94eb9SDaniel Vetter 		entry->crc[0] = crc0;
1633eba94eb9SDaniel Vetter 		entry->crc[1] = crc1;
1634eba94eb9SDaniel Vetter 		entry->crc[2] = crc2;
1635eba94eb9SDaniel Vetter 		entry->crc[3] = crc3;
1636eba94eb9SDaniel Vetter 		entry->crc[4] = crc4;
1637b2c88f5bSDamien Lespiau 
1638b2c88f5bSDamien Lespiau 		head = (head + 1) & (INTEL_PIPE_CRC_ENTRIES_NR - 1);
1639d538bbdfSDamien Lespiau 		pipe_crc->head = head;
1640d538bbdfSDamien Lespiau 
1641d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
164207144428SDamien Lespiau 
164307144428SDamien Lespiau 		wake_up_interruptible(&pipe_crc->wq);
16448c6b709dSTomeu Vizoso 	} else {
16458c6b709dSTomeu Vizoso 		/*
16468c6b709dSTomeu Vizoso 		 * For some not yet identified reason, the first CRC is
16478c6b709dSTomeu Vizoso 		 * bonkers. So let's just wait for the next vblank and read
16488c6b709dSTomeu Vizoso 		 * out the buggy result.
16498c6b709dSTomeu Vizoso 		 *
1650163e8aecSRodrigo Vivi 		 * On GEN8+ sometimes the second CRC is bonkers as well, so
16518c6b709dSTomeu Vizoso 		 * don't trust that one either.
16528c6b709dSTomeu Vizoso 		 */
16538c6b709dSTomeu Vizoso 		if (pipe_crc->skipped == 0 ||
1654163e8aecSRodrigo Vivi 		    (INTEL_GEN(dev_priv) >= 8 && pipe_crc->skipped == 1)) {
16558c6b709dSTomeu Vizoso 			pipe_crc->skipped++;
16568c6b709dSTomeu Vizoso 			spin_unlock(&pipe_crc->lock);
16578c6b709dSTomeu Vizoso 			return;
16588c6b709dSTomeu Vizoso 		}
16598c6b709dSTomeu Vizoso 		spin_unlock(&pipe_crc->lock);
16608c6b709dSTomeu Vizoso 		crcs[0] = crc0;
16618c6b709dSTomeu Vizoso 		crcs[1] = crc1;
16628c6b709dSTomeu Vizoso 		crcs[2] = crc2;
16638c6b709dSTomeu Vizoso 		crcs[3] = crc3;
16648c6b709dSTomeu Vizoso 		crcs[4] = crc4;
1665246ee524STomeu Vizoso 		drm_crtc_add_crc_entry(&crtc->base, true,
1666ca814b25SDaniel Vetter 				       drm_crtc_accurate_vblank_count(&crtc->base),
1667246ee524STomeu Vizoso 				       crcs);
16688c6b709dSTomeu Vizoso 	}
16698bf1e9f1SShuang He }
1670277de95eSDaniel Vetter #else
1671277de95eSDaniel Vetter static inline void
167291d14251STvrtko Ursulin display_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
167391d14251STvrtko Ursulin 			     enum pipe pipe,
1674277de95eSDaniel Vetter 			     uint32_t crc0, uint32_t crc1,
1675277de95eSDaniel Vetter 			     uint32_t crc2, uint32_t crc3,
1676277de95eSDaniel Vetter 			     uint32_t crc4) {}
1677277de95eSDaniel Vetter #endif
1678eba94eb9SDaniel Vetter 
1679277de95eSDaniel Vetter 
168091d14251STvrtko Ursulin static void hsw_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
168191d14251STvrtko Ursulin 				     enum pipe pipe)
16825a69b89fSDaniel Vetter {
168391d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
16845a69b89fSDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
16855a69b89fSDaniel Vetter 				     0, 0, 0, 0);
16865a69b89fSDaniel Vetter }
16875a69b89fSDaniel Vetter 
168891d14251STvrtko Ursulin static void ivb_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
168991d14251STvrtko Ursulin 				     enum pipe pipe)
1690eba94eb9SDaniel Vetter {
169191d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
1692eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1693eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1694eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1695eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
16968bc5e955SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1697eba94eb9SDaniel Vetter }
16985b3a856bSDaniel Vetter 
169991d14251STvrtko Ursulin static void i9xx_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
170091d14251STvrtko Ursulin 				      enum pipe pipe)
17015b3a856bSDaniel Vetter {
17020b5c5ed0SDaniel Vetter 	uint32_t res1, res2;
17030b5c5ed0SDaniel Vetter 
170491d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 3)
17050b5c5ed0SDaniel Vetter 		res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
17060b5c5ed0SDaniel Vetter 	else
17070b5c5ed0SDaniel Vetter 		res1 = 0;
17080b5c5ed0SDaniel Vetter 
170991d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
17100b5c5ed0SDaniel Vetter 		res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
17110b5c5ed0SDaniel Vetter 	else
17120b5c5ed0SDaniel Vetter 		res2 = 0;
17135b3a856bSDaniel Vetter 
171491d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
17150b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_RED(pipe)),
17160b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_GREEN(pipe)),
17170b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_BLUE(pipe)),
17180b5c5ed0SDaniel Vetter 				     res1, res2);
17195b3a856bSDaniel Vetter }
17208bf1e9f1SShuang He 
17211403c0d4SPaulo Zanoni /* The RPS events need forcewake, so we add them to a work queue and mask their
17221403c0d4SPaulo Zanoni  * IMR bits until the work is done. Other interrupts can be processed without
17231403c0d4SPaulo Zanoni  * the work queue. */
17241403c0d4SPaulo Zanoni static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1725baf02a1fSBen Widawsky {
1726a6706b45SDeepak S 	if (pm_iir & dev_priv->pm_rps_events) {
172759cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
1728f4e9af4fSAkash Goel 		gen6_mask_pm_irq(dev_priv, pm_iir & dev_priv->pm_rps_events);
1729d4d70aa5SImre Deak 		if (dev_priv->rps.interrupts_enabled) {
1730d4d70aa5SImre Deak 			dev_priv->rps.pm_iir |= pm_iir & dev_priv->pm_rps_events;
1731c33d247dSChris Wilson 			schedule_work(&dev_priv->rps.work);
173241a05a3aSDaniel Vetter 		}
1733d4d70aa5SImre Deak 		spin_unlock(&dev_priv->irq_lock);
1734d4d70aa5SImre Deak 	}
1735baf02a1fSBen Widawsky 
1736bca2bf2aSPandiyan, Dhinakaran 	if (INTEL_GEN(dev_priv) >= 8)
1737c9a9a268SImre Deak 		return;
1738c9a9a268SImre Deak 
17392d1fe073SJoonas Lahtinen 	if (HAS_VEBOX(dev_priv)) {
174012638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
17413b3f1650SAkash Goel 			notify_ring(dev_priv->engine[VECS]);
174212638c57SBen Widawsky 
1743aaecdf61SDaniel Vetter 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT)
1744aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, pm_iir 0x%08x\n", pm_iir);
174512638c57SBen Widawsky 	}
17461403c0d4SPaulo Zanoni }
1747baf02a1fSBen Widawsky 
174826705e20SSagar Arun Kamble static void gen9_guc_irq_handler(struct drm_i915_private *dev_priv, u32 gt_iir)
174926705e20SSagar Arun Kamble {
175026705e20SSagar Arun Kamble 	if (gt_iir & GEN9_GUC_TO_HOST_INT_EVENT) {
17514100b2abSSagar Arun Kamble 		/* Sample the log buffer flush related bits & clear them out now
17524100b2abSSagar Arun Kamble 		 * itself from the message identity register to minimize the
17534100b2abSSagar Arun Kamble 		 * probability of losing a flush interrupt, when there are back
17544100b2abSSagar Arun Kamble 		 * to back flush interrupts.
17554100b2abSSagar Arun Kamble 		 * There can be a new flush interrupt, for different log buffer
17564100b2abSSagar Arun Kamble 		 * type (like for ISR), whilst Host is handling one (for DPC).
17574100b2abSSagar Arun Kamble 		 * Since same bit is used in message register for ISR & DPC, it
17584100b2abSSagar Arun Kamble 		 * could happen that GuC sets the bit for 2nd interrupt but Host
17594100b2abSSagar Arun Kamble 		 * clears out the bit on handling the 1st interrupt.
17604100b2abSSagar Arun Kamble 		 */
17614100b2abSSagar Arun Kamble 		u32 msg, flush;
17624100b2abSSagar Arun Kamble 
17634100b2abSSagar Arun Kamble 		msg = I915_READ(SOFT_SCRATCH(15));
1764a80bc45fSArkadiusz Hiler 		flush = msg & (INTEL_GUC_RECV_MSG_CRASH_DUMP_POSTED |
1765a80bc45fSArkadiusz Hiler 			       INTEL_GUC_RECV_MSG_FLUSH_LOG_BUFFER);
17664100b2abSSagar Arun Kamble 		if (flush) {
17674100b2abSSagar Arun Kamble 			/* Clear the message bits that are handled */
17684100b2abSSagar Arun Kamble 			I915_WRITE(SOFT_SCRATCH(15), msg & ~flush);
17694100b2abSSagar Arun Kamble 
17704100b2abSSagar Arun Kamble 			/* Handle flush interrupt in bottom half */
1771e7465473SOscar Mateo 			queue_work(dev_priv->guc.log.runtime.flush_wq,
1772e7465473SOscar Mateo 				   &dev_priv->guc.log.runtime.flush_work);
17735aa1ee4bSAkash Goel 
17745aa1ee4bSAkash Goel 			dev_priv->guc.log.flush_interrupt_count++;
17754100b2abSSagar Arun Kamble 		} else {
17764100b2abSSagar Arun Kamble 			/* Not clearing of unhandled event bits won't result in
17774100b2abSSagar Arun Kamble 			 * re-triggering of the interrupt.
17784100b2abSSagar Arun Kamble 			 */
17794100b2abSSagar Arun Kamble 		}
178026705e20SSagar Arun Kamble 	}
178126705e20SSagar Arun Kamble }
178226705e20SSagar Arun Kamble 
178344d9241eSVille Syrjälä static void i9xx_pipestat_irq_reset(struct drm_i915_private *dev_priv)
178444d9241eSVille Syrjälä {
178544d9241eSVille Syrjälä 	enum pipe pipe;
178644d9241eSVille Syrjälä 
178744d9241eSVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
178844d9241eSVille Syrjälä 		I915_WRITE(PIPESTAT(pipe),
178944d9241eSVille Syrjälä 			   PIPESTAT_INT_STATUS_MASK |
179044d9241eSVille Syrjälä 			   PIPE_FIFO_UNDERRUN_STATUS);
179144d9241eSVille Syrjälä 
179244d9241eSVille Syrjälä 		dev_priv->pipestat_irq_mask[pipe] = 0;
179344d9241eSVille Syrjälä 	}
179444d9241eSVille Syrjälä }
179544d9241eSVille Syrjälä 
1796eb64343cSVille Syrjälä static void i9xx_pipestat_irq_ack(struct drm_i915_private *dev_priv,
179791d14251STvrtko Ursulin 				  u32 iir, u32 pipe_stats[I915_MAX_PIPES])
17987e231dbeSJesse Barnes {
17997e231dbeSJesse Barnes 	int pipe;
18007e231dbeSJesse Barnes 
180158ead0d7SImre Deak 	spin_lock(&dev_priv->irq_lock);
18021ca993d2SVille Syrjälä 
18031ca993d2SVille Syrjälä 	if (!dev_priv->display_irqs_enabled) {
18041ca993d2SVille Syrjälä 		spin_unlock(&dev_priv->irq_lock);
18051ca993d2SVille Syrjälä 		return;
18061ca993d2SVille Syrjälä 	}
18071ca993d2SVille Syrjälä 
1808055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1809f0f59a00SVille Syrjälä 		i915_reg_t reg;
18106b12ca56SVille Syrjälä 		u32 status_mask, enable_mask, iir_bit = 0;
181191d181ddSImre Deak 
1812bbb5eebfSDaniel Vetter 		/*
1813bbb5eebfSDaniel Vetter 		 * PIPESTAT bits get signalled even when the interrupt is
1814bbb5eebfSDaniel Vetter 		 * disabled with the mask bits, and some of the status bits do
1815bbb5eebfSDaniel Vetter 		 * not generate interrupts at all (like the underrun bit). Hence
1816bbb5eebfSDaniel Vetter 		 * we need to be careful that we only handle what we want to
1817bbb5eebfSDaniel Vetter 		 * handle.
1818bbb5eebfSDaniel Vetter 		 */
18190f239f4cSDaniel Vetter 
18200f239f4cSDaniel Vetter 		/* fifo underruns are filterered in the underrun handler. */
18216b12ca56SVille Syrjälä 		status_mask = PIPE_FIFO_UNDERRUN_STATUS;
1822bbb5eebfSDaniel Vetter 
1823bbb5eebfSDaniel Vetter 		switch (pipe) {
1824bbb5eebfSDaniel Vetter 		case PIPE_A:
1825bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
1826bbb5eebfSDaniel Vetter 			break;
1827bbb5eebfSDaniel Vetter 		case PIPE_B:
1828bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
1829bbb5eebfSDaniel Vetter 			break;
18303278f67fSVille Syrjälä 		case PIPE_C:
18313278f67fSVille Syrjälä 			iir_bit = I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
18323278f67fSVille Syrjälä 			break;
1833bbb5eebfSDaniel Vetter 		}
1834bbb5eebfSDaniel Vetter 		if (iir & iir_bit)
18356b12ca56SVille Syrjälä 			status_mask |= dev_priv->pipestat_irq_mask[pipe];
1836bbb5eebfSDaniel Vetter 
18376b12ca56SVille Syrjälä 		if (!status_mask)
183891d181ddSImre Deak 			continue;
183991d181ddSImre Deak 
184091d181ddSImre Deak 		reg = PIPESTAT(pipe);
18416b12ca56SVille Syrjälä 		pipe_stats[pipe] = I915_READ(reg) & status_mask;
18426b12ca56SVille Syrjälä 		enable_mask = i915_pipestat_enable_mask(dev_priv, pipe);
18437e231dbeSJesse Barnes 
18447e231dbeSJesse Barnes 		/*
18457e231dbeSJesse Barnes 		 * Clear the PIPE*STAT regs before the IIR
18467e231dbeSJesse Barnes 		 */
18476b12ca56SVille Syrjälä 		if (pipe_stats[pipe])
18486b12ca56SVille Syrjälä 			I915_WRITE(reg, enable_mask | pipe_stats[pipe]);
18497e231dbeSJesse Barnes 	}
185058ead0d7SImre Deak 	spin_unlock(&dev_priv->irq_lock);
18512ecb8ca4SVille Syrjälä }
18522ecb8ca4SVille Syrjälä 
1853eb64343cSVille Syrjälä static void i8xx_pipestat_irq_handler(struct drm_i915_private *dev_priv,
1854eb64343cSVille Syrjälä 				      u16 iir, u32 pipe_stats[I915_MAX_PIPES])
1855eb64343cSVille Syrjälä {
1856eb64343cSVille Syrjälä 	enum pipe pipe;
1857eb64343cSVille Syrjälä 
1858eb64343cSVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
1859eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
1860eb64343cSVille Syrjälä 			drm_handle_vblank(&dev_priv->drm, pipe);
1861eb64343cSVille Syrjälä 
1862eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1863eb64343cSVille Syrjälä 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
1864eb64343cSVille Syrjälä 
1865eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
1866eb64343cSVille Syrjälä 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
1867eb64343cSVille Syrjälä 	}
1868eb64343cSVille Syrjälä }
1869eb64343cSVille Syrjälä 
1870eb64343cSVille Syrjälä static void i915_pipestat_irq_handler(struct drm_i915_private *dev_priv,
1871eb64343cSVille Syrjälä 				      u32 iir, u32 pipe_stats[I915_MAX_PIPES])
1872eb64343cSVille Syrjälä {
1873eb64343cSVille Syrjälä 	bool blc_event = false;
1874eb64343cSVille Syrjälä 	enum pipe pipe;
1875eb64343cSVille Syrjälä 
1876eb64343cSVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
1877eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
1878eb64343cSVille Syrjälä 			drm_handle_vblank(&dev_priv->drm, pipe);
1879eb64343cSVille Syrjälä 
1880eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
1881eb64343cSVille Syrjälä 			blc_event = true;
1882eb64343cSVille Syrjälä 
1883eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1884eb64343cSVille Syrjälä 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
1885eb64343cSVille Syrjälä 
1886eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
1887eb64343cSVille Syrjälä 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
1888eb64343cSVille Syrjälä 	}
1889eb64343cSVille Syrjälä 
1890eb64343cSVille Syrjälä 	if (blc_event || (iir & I915_ASLE_INTERRUPT))
1891eb64343cSVille Syrjälä 		intel_opregion_asle_intr(dev_priv);
1892eb64343cSVille Syrjälä }
1893eb64343cSVille Syrjälä 
1894eb64343cSVille Syrjälä static void i965_pipestat_irq_handler(struct drm_i915_private *dev_priv,
1895eb64343cSVille Syrjälä 				      u32 iir, u32 pipe_stats[I915_MAX_PIPES])
1896eb64343cSVille Syrjälä {
1897eb64343cSVille Syrjälä 	bool blc_event = false;
1898eb64343cSVille Syrjälä 	enum pipe pipe;
1899eb64343cSVille Syrjälä 
1900eb64343cSVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
1901eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS)
1902eb64343cSVille Syrjälä 			drm_handle_vblank(&dev_priv->drm, pipe);
1903eb64343cSVille Syrjälä 
1904eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
1905eb64343cSVille Syrjälä 			blc_event = true;
1906eb64343cSVille Syrjälä 
1907eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1908eb64343cSVille Syrjälä 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
1909eb64343cSVille Syrjälä 
1910eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
1911eb64343cSVille Syrjälä 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
1912eb64343cSVille Syrjälä 	}
1913eb64343cSVille Syrjälä 
1914eb64343cSVille Syrjälä 	if (blc_event || (iir & I915_ASLE_INTERRUPT))
1915eb64343cSVille Syrjälä 		intel_opregion_asle_intr(dev_priv);
1916eb64343cSVille Syrjälä 
1917eb64343cSVille Syrjälä 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1918eb64343cSVille Syrjälä 		gmbus_irq_handler(dev_priv);
1919eb64343cSVille Syrjälä }
1920eb64343cSVille Syrjälä 
192191d14251STvrtko Ursulin static void valleyview_pipestat_irq_handler(struct drm_i915_private *dev_priv,
19222ecb8ca4SVille Syrjälä 					    u32 pipe_stats[I915_MAX_PIPES])
19232ecb8ca4SVille Syrjälä {
19242ecb8ca4SVille Syrjälä 	enum pipe pipe;
19257e231dbeSJesse Barnes 
1926055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1927fd3a4024SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS)
1928fd3a4024SDaniel Vetter 			drm_handle_vblank(&dev_priv->drm, pipe);
19294356d586SDaniel Vetter 
19304356d586SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
193191d14251STvrtko Ursulin 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
19322d9d2b0bSVille Syrjälä 
19331f7247c0SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
19341f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
193531acc7f5SJesse Barnes 	}
193631acc7f5SJesse Barnes 
1937c1874ed7SImre Deak 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
193891d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
1939c1874ed7SImre Deak }
1940c1874ed7SImre Deak 
19411ae3c34cSVille Syrjälä static u32 i9xx_hpd_irq_ack(struct drm_i915_private *dev_priv)
194216c6c56bSVille Syrjälä {
194316c6c56bSVille Syrjälä 	u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
194416c6c56bSVille Syrjälä 
19451ae3c34cSVille Syrjälä 	if (hotplug_status)
19463ff60f89SOscar Mateo 		I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
19471ae3c34cSVille Syrjälä 
19481ae3c34cSVille Syrjälä 	return hotplug_status;
19491ae3c34cSVille Syrjälä }
19501ae3c34cSVille Syrjälä 
195191d14251STvrtko Ursulin static void i9xx_hpd_irq_handler(struct drm_i915_private *dev_priv,
19521ae3c34cSVille Syrjälä 				 u32 hotplug_status)
19531ae3c34cSVille Syrjälä {
19541ae3c34cSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
19553ff60f89SOscar Mateo 
195691d14251STvrtko Ursulin 	if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
195791d14251STvrtko Ursulin 	    IS_CHERRYVIEW(dev_priv)) {
195816c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
195916c6c56bSVille Syrjälä 
196058f2cf24SVille Syrjälä 		if (hotplug_trigger) {
1961fd63e2a9SImre Deak 			intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
1962fd63e2a9SImre Deak 					   hotplug_trigger, hpd_status_g4x,
1963fd63e2a9SImre Deak 					   i9xx_port_hotplug_long_detect);
196458f2cf24SVille Syrjälä 
196591d14251STvrtko Ursulin 			intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
196658f2cf24SVille Syrjälä 		}
1967369712e8SJani Nikula 
1968369712e8SJani Nikula 		if (hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
196991d14251STvrtko Ursulin 			dp_aux_irq_handler(dev_priv);
197016c6c56bSVille Syrjälä 	} else {
197116c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
197216c6c56bSVille Syrjälä 
197358f2cf24SVille Syrjälä 		if (hotplug_trigger) {
1974fd63e2a9SImre Deak 			intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
19754e3d1e26SVille Syrjälä 					   hotplug_trigger, hpd_status_i915,
1976fd63e2a9SImre Deak 					   i9xx_port_hotplug_long_detect);
197791d14251STvrtko Ursulin 			intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
197816c6c56bSVille Syrjälä 		}
19793ff60f89SOscar Mateo 	}
198058f2cf24SVille Syrjälä }
198116c6c56bSVille Syrjälä 
1982c1874ed7SImre Deak static irqreturn_t valleyview_irq_handler(int irq, void *arg)
1983c1874ed7SImre Deak {
198445a83f84SDaniel Vetter 	struct drm_device *dev = arg;
1985fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
1986c1874ed7SImre Deak 	irqreturn_t ret = IRQ_NONE;
1987c1874ed7SImre Deak 
19882dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
19892dd2a883SImre Deak 		return IRQ_NONE;
19902dd2a883SImre Deak 
19911f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
19921f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
19931f814dacSImre Deak 
19941e1cace9SVille Syrjälä 	do {
19956e814800SVille Syrjälä 		u32 iir, gt_iir, pm_iir;
19962ecb8ca4SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
19971ae3c34cSVille Syrjälä 		u32 hotplug_status = 0;
1998a5e485a9SVille Syrjälä 		u32 ier = 0;
19993ff60f89SOscar Mateo 
2000c1874ed7SImre Deak 		gt_iir = I915_READ(GTIIR);
2001c1874ed7SImre Deak 		pm_iir = I915_READ(GEN6_PMIIR);
20023ff60f89SOscar Mateo 		iir = I915_READ(VLV_IIR);
2003c1874ed7SImre Deak 
2004c1874ed7SImre Deak 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
20051e1cace9SVille Syrjälä 			break;
2006c1874ed7SImre Deak 
2007c1874ed7SImre Deak 		ret = IRQ_HANDLED;
2008c1874ed7SImre Deak 
2009a5e485a9SVille Syrjälä 		/*
2010a5e485a9SVille Syrjälä 		 * Theory on interrupt generation, based on empirical evidence:
2011a5e485a9SVille Syrjälä 		 *
2012a5e485a9SVille Syrjälä 		 * x = ((VLV_IIR & VLV_IER) ||
2013a5e485a9SVille Syrjälä 		 *      (((GT_IIR & GT_IER) || (GEN6_PMIIR & GEN6_PMIER)) &&
2014a5e485a9SVille Syrjälä 		 *       (VLV_MASTER_IER & MASTER_INTERRUPT_ENABLE)));
2015a5e485a9SVille Syrjälä 		 *
2016a5e485a9SVille Syrjälä 		 * A CPU interrupt will only be raised when 'x' has a 0->1 edge.
2017a5e485a9SVille Syrjälä 		 * Hence we clear MASTER_INTERRUPT_ENABLE and VLV_IER to
2018a5e485a9SVille Syrjälä 		 * guarantee the CPU interrupt will be raised again even if we
2019a5e485a9SVille Syrjälä 		 * don't end up clearing all the VLV_IIR, GT_IIR, GEN6_PMIIR
2020a5e485a9SVille Syrjälä 		 * bits this time around.
2021a5e485a9SVille Syrjälä 		 */
20224a0a0202SVille Syrjälä 		I915_WRITE(VLV_MASTER_IER, 0);
2023a5e485a9SVille Syrjälä 		ier = I915_READ(VLV_IER);
2024a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, 0);
20254a0a0202SVille Syrjälä 
20264a0a0202SVille Syrjälä 		if (gt_iir)
20274a0a0202SVille Syrjälä 			I915_WRITE(GTIIR, gt_iir);
20284a0a0202SVille Syrjälä 		if (pm_iir)
20294a0a0202SVille Syrjälä 			I915_WRITE(GEN6_PMIIR, pm_iir);
20304a0a0202SVille Syrjälä 
20317ce4d1f2SVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
20321ae3c34cSVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
20337ce4d1f2SVille Syrjälä 
20343ff60f89SOscar Mateo 		/* Call regardless, as some status bits might not be
20353ff60f89SOscar Mateo 		 * signalled in iir */
2036eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
20377ce4d1f2SVille Syrjälä 
2038eef57324SJerome Anand 		if (iir & (I915_LPE_PIPE_A_INTERRUPT |
2039eef57324SJerome Anand 			   I915_LPE_PIPE_B_INTERRUPT))
2040eef57324SJerome Anand 			intel_lpe_audio_irq_handler(dev_priv);
2041eef57324SJerome Anand 
20427ce4d1f2SVille Syrjälä 		/*
20437ce4d1f2SVille Syrjälä 		 * VLV_IIR is single buffered, and reflects the level
20447ce4d1f2SVille Syrjälä 		 * from PIPESTAT/PORT_HOTPLUG_STAT, hence clear it last.
20457ce4d1f2SVille Syrjälä 		 */
20467ce4d1f2SVille Syrjälä 		if (iir)
20477ce4d1f2SVille Syrjälä 			I915_WRITE(VLV_IIR, iir);
20484a0a0202SVille Syrjälä 
2049a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, ier);
20504a0a0202SVille Syrjälä 		I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
20514a0a0202SVille Syrjälä 		POSTING_READ(VLV_MASTER_IER);
20521ae3c34cSVille Syrjälä 
205352894874SVille Syrjälä 		if (gt_iir)
2054261e40b8SVille Syrjälä 			snb_gt_irq_handler(dev_priv, gt_iir);
205552894874SVille Syrjälä 		if (pm_iir)
205652894874SVille Syrjälä 			gen6_rps_irq_handler(dev_priv, pm_iir);
205752894874SVille Syrjälä 
20581ae3c34cSVille Syrjälä 		if (hotplug_status)
205991d14251STvrtko Ursulin 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
20602ecb8ca4SVille Syrjälä 
206191d14251STvrtko Ursulin 		valleyview_pipestat_irq_handler(dev_priv, pipe_stats);
20621e1cace9SVille Syrjälä 	} while (0);
20637e231dbeSJesse Barnes 
20641f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
20651f814dacSImre Deak 
20667e231dbeSJesse Barnes 	return ret;
20677e231dbeSJesse Barnes }
20687e231dbeSJesse Barnes 
206943f328d7SVille Syrjälä static irqreturn_t cherryview_irq_handler(int irq, void *arg)
207043f328d7SVille Syrjälä {
207145a83f84SDaniel Vetter 	struct drm_device *dev = arg;
2072fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
207343f328d7SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
207443f328d7SVille Syrjälä 
20752dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
20762dd2a883SImre Deak 		return IRQ_NONE;
20772dd2a883SImre Deak 
20781f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
20791f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
20801f814dacSImre Deak 
2081579de73bSChris Wilson 	do {
20826e814800SVille Syrjälä 		u32 master_ctl, iir;
2083e30e251aSVille Syrjälä 		u32 gt_iir[4] = {};
20842ecb8ca4SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
20851ae3c34cSVille Syrjälä 		u32 hotplug_status = 0;
2086a5e485a9SVille Syrjälä 		u32 ier = 0;
2087a5e485a9SVille Syrjälä 
20888e5fd599SVille Syrjälä 		master_ctl = I915_READ(GEN8_MASTER_IRQ) & ~GEN8_MASTER_IRQ_CONTROL;
20893278f67fSVille Syrjälä 		iir = I915_READ(VLV_IIR);
20903278f67fSVille Syrjälä 
20913278f67fSVille Syrjälä 		if (master_ctl == 0 && iir == 0)
20928e5fd599SVille Syrjälä 			break;
209343f328d7SVille Syrjälä 
209427b6c122SOscar Mateo 		ret = IRQ_HANDLED;
209527b6c122SOscar Mateo 
2096a5e485a9SVille Syrjälä 		/*
2097a5e485a9SVille Syrjälä 		 * Theory on interrupt generation, based on empirical evidence:
2098a5e485a9SVille Syrjälä 		 *
2099a5e485a9SVille Syrjälä 		 * x = ((VLV_IIR & VLV_IER) ||
2100a5e485a9SVille Syrjälä 		 *      ((GEN8_MASTER_IRQ & ~GEN8_MASTER_IRQ_CONTROL) &&
2101a5e485a9SVille Syrjälä 		 *       (GEN8_MASTER_IRQ & GEN8_MASTER_IRQ_CONTROL)));
2102a5e485a9SVille Syrjälä 		 *
2103a5e485a9SVille Syrjälä 		 * A CPU interrupt will only be raised when 'x' has a 0->1 edge.
2104a5e485a9SVille Syrjälä 		 * Hence we clear GEN8_MASTER_IRQ_CONTROL and VLV_IER to
2105a5e485a9SVille Syrjälä 		 * guarantee the CPU interrupt will be raised again even if we
2106a5e485a9SVille Syrjälä 		 * don't end up clearing all the VLV_IIR and GEN8_MASTER_IRQ_CONTROL
2107a5e485a9SVille Syrjälä 		 * bits this time around.
2108a5e485a9SVille Syrjälä 		 */
210943f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, 0);
2110a5e485a9SVille Syrjälä 		ier = I915_READ(VLV_IER);
2111a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, 0);
211243f328d7SVille Syrjälä 
2113e30e251aSVille Syrjälä 		gen8_gt_irq_ack(dev_priv, master_ctl, gt_iir);
211427b6c122SOscar Mateo 
211527b6c122SOscar Mateo 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
21161ae3c34cSVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
211743f328d7SVille Syrjälä 
211827b6c122SOscar Mateo 		/* Call regardless, as some status bits might not be
211927b6c122SOscar Mateo 		 * signalled in iir */
2120eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
212143f328d7SVille Syrjälä 
2122eef57324SJerome Anand 		if (iir & (I915_LPE_PIPE_A_INTERRUPT |
2123eef57324SJerome Anand 			   I915_LPE_PIPE_B_INTERRUPT |
2124eef57324SJerome Anand 			   I915_LPE_PIPE_C_INTERRUPT))
2125eef57324SJerome Anand 			intel_lpe_audio_irq_handler(dev_priv);
2126eef57324SJerome Anand 
21277ce4d1f2SVille Syrjälä 		/*
21287ce4d1f2SVille Syrjälä 		 * VLV_IIR is single buffered, and reflects the level
21297ce4d1f2SVille Syrjälä 		 * from PIPESTAT/PORT_HOTPLUG_STAT, hence clear it last.
21307ce4d1f2SVille Syrjälä 		 */
21317ce4d1f2SVille Syrjälä 		if (iir)
21327ce4d1f2SVille Syrjälä 			I915_WRITE(VLV_IIR, iir);
21337ce4d1f2SVille Syrjälä 
2134a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, ier);
2135e5328c43SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
213643f328d7SVille Syrjälä 		POSTING_READ(GEN8_MASTER_IRQ);
21371ae3c34cSVille Syrjälä 
2138e30e251aSVille Syrjälä 		gen8_gt_irq_handler(dev_priv, gt_iir);
2139e30e251aSVille Syrjälä 
21401ae3c34cSVille Syrjälä 		if (hotplug_status)
214191d14251STvrtko Ursulin 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
21422ecb8ca4SVille Syrjälä 
214391d14251STvrtko Ursulin 		valleyview_pipestat_irq_handler(dev_priv, pipe_stats);
2144579de73bSChris Wilson 	} while (0);
21453278f67fSVille Syrjälä 
21461f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
21471f814dacSImre Deak 
214843f328d7SVille Syrjälä 	return ret;
214943f328d7SVille Syrjälä }
215043f328d7SVille Syrjälä 
215191d14251STvrtko Ursulin static void ibx_hpd_irq_handler(struct drm_i915_private *dev_priv,
215291d14251STvrtko Ursulin 				u32 hotplug_trigger,
215340e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2154776ad806SJesse Barnes {
215542db67d6SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2156776ad806SJesse Barnes 
21576a39d7c9SJani Nikula 	/*
21586a39d7c9SJani Nikula 	 * Somehow the PCH doesn't seem to really ack the interrupt to the CPU
21596a39d7c9SJani Nikula 	 * unless we touch the hotplug register, even if hotplug_trigger is
21606a39d7c9SJani Nikula 	 * zero. Not acking leads to "The master control interrupt lied (SDE)!"
21616a39d7c9SJani Nikula 	 * errors.
21626a39d7c9SJani Nikula 	 */
216313cf5504SDave Airlie 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
21646a39d7c9SJani Nikula 	if (!hotplug_trigger) {
21656a39d7c9SJani Nikula 		u32 mask = PORTA_HOTPLUG_STATUS_MASK |
21666a39d7c9SJani Nikula 			PORTD_HOTPLUG_STATUS_MASK |
21676a39d7c9SJani Nikula 			PORTC_HOTPLUG_STATUS_MASK |
21686a39d7c9SJani Nikula 			PORTB_HOTPLUG_STATUS_MASK;
21696a39d7c9SJani Nikula 		dig_hotplug_reg &= ~mask;
21706a39d7c9SJani Nikula 	}
21716a39d7c9SJani Nikula 
217213cf5504SDave Airlie 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
21736a39d7c9SJani Nikula 	if (!hotplug_trigger)
21746a39d7c9SJani Nikula 		return;
217513cf5504SDave Airlie 
2176fd63e2a9SImre Deak 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
217740e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2178fd63e2a9SImre Deak 			   pch_port_hotplug_long_detect);
217940e56410SVille Syrjälä 
218091d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2181aaf5ec2eSSonika Jindal }
218291d131d2SDaniel Vetter 
218391d14251STvrtko Ursulin static void ibx_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
218440e56410SVille Syrjälä {
218540e56410SVille Syrjälä 	int pipe;
218640e56410SVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
218740e56410SVille Syrjälä 
218891d14251STvrtko Ursulin 	ibx_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ibx);
218940e56410SVille Syrjälä 
2190cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
2191cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
2192776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
2193cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
2194cfc33bf7SVille Syrjälä 				 port_name(port));
2195cfc33bf7SVille Syrjälä 	}
2196776ad806SJesse Barnes 
2197ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
219891d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
2199ce99c256SDaniel Vetter 
2200776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
220191d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
2202776ad806SJesse Barnes 
2203776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
2204776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
2205776ad806SJesse Barnes 
2206776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
2207776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
2208776ad806SJesse Barnes 
2209776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
2210776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
2211776ad806SJesse Barnes 
22129db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
2213055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
22149db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
22159db4a9c7SJesse Barnes 					 pipe_name(pipe),
22169db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
2217776ad806SJesse Barnes 
2218776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
2219776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
2220776ad806SJesse Barnes 
2221776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
2222776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
2223776ad806SJesse Barnes 
2224776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
2225a2196033SMatthias Kaehlcke 		intel_pch_fifo_underrun_irq_handler(dev_priv, PIPE_A);
22268664281bSPaulo Zanoni 
22278664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
2228a2196033SMatthias Kaehlcke 		intel_pch_fifo_underrun_irq_handler(dev_priv, PIPE_B);
22298664281bSPaulo Zanoni }
22308664281bSPaulo Zanoni 
223191d14251STvrtko Ursulin static void ivb_err_int_handler(struct drm_i915_private *dev_priv)
22328664281bSPaulo Zanoni {
22338664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
22345a69b89fSDaniel Vetter 	enum pipe pipe;
22358664281bSPaulo Zanoni 
2236de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
2237de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
2238de032bf4SPaulo Zanoni 
2239055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
22401f7247c0SDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe))
22411f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
22428664281bSPaulo Zanoni 
22435a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
224491d14251STvrtko Ursulin 			if (IS_IVYBRIDGE(dev_priv))
224591d14251STvrtko Ursulin 				ivb_pipe_crc_irq_handler(dev_priv, pipe);
22465a69b89fSDaniel Vetter 			else
224791d14251STvrtko Ursulin 				hsw_pipe_crc_irq_handler(dev_priv, pipe);
22485a69b89fSDaniel Vetter 		}
22495a69b89fSDaniel Vetter 	}
22508bf1e9f1SShuang He 
22518664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
22528664281bSPaulo Zanoni }
22538664281bSPaulo Zanoni 
225491d14251STvrtko Ursulin static void cpt_serr_int_handler(struct drm_i915_private *dev_priv)
22558664281bSPaulo Zanoni {
22568664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
22578664281bSPaulo Zanoni 
2258de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
2259de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
2260de032bf4SPaulo Zanoni 
22618664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
2262a2196033SMatthias Kaehlcke 		intel_pch_fifo_underrun_irq_handler(dev_priv, PIPE_A);
22638664281bSPaulo Zanoni 
22648664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
2265a2196033SMatthias Kaehlcke 		intel_pch_fifo_underrun_irq_handler(dev_priv, PIPE_B);
22668664281bSPaulo Zanoni 
22678664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
2268a2196033SMatthias Kaehlcke 		intel_pch_fifo_underrun_irq_handler(dev_priv, PIPE_C);
22698664281bSPaulo Zanoni 
22708664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
2271776ad806SJesse Barnes }
2272776ad806SJesse Barnes 
227391d14251STvrtko Ursulin static void cpt_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
227423e81d69SAdam Jackson {
227523e81d69SAdam Jackson 	int pipe;
22766dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
2277aaf5ec2eSSonika Jindal 
227891d14251STvrtko Ursulin 	ibx_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_cpt);
227991d131d2SDaniel Vetter 
2280cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
2281cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
228223e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
2283cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
2284cfc33bf7SVille Syrjälä 				 port_name(port));
2285cfc33bf7SVille Syrjälä 	}
228623e81d69SAdam Jackson 
228723e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
228891d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
228923e81d69SAdam Jackson 
229023e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
229191d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
229223e81d69SAdam Jackson 
229323e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
229423e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
229523e81d69SAdam Jackson 
229623e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
229723e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
229823e81d69SAdam Jackson 
229923e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
2300055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
230123e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
230223e81d69SAdam Jackson 					 pipe_name(pipe),
230323e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
23048664281bSPaulo Zanoni 
23058664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
230691d14251STvrtko Ursulin 		cpt_serr_int_handler(dev_priv);
230723e81d69SAdam Jackson }
230823e81d69SAdam Jackson 
230991d14251STvrtko Ursulin static void spt_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
23106dbf30ceSVille Syrjälä {
23116dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_SPT &
23126dbf30ceSVille Syrjälä 		~SDE_PORTE_HOTPLUG_SPT;
23136dbf30ceSVille Syrjälä 	u32 hotplug2_trigger = pch_iir & SDE_PORTE_HOTPLUG_SPT;
23146dbf30ceSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
23156dbf30ceSVille Syrjälä 
23166dbf30ceSVille Syrjälä 	if (hotplug_trigger) {
23176dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
23186dbf30ceSVille Syrjälä 
23196dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
23206dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
23216dbf30ceSVille Syrjälä 
23226dbf30ceSVille Syrjälä 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
23236dbf30ceSVille Syrjälä 				   dig_hotplug_reg, hpd_spt,
232474c0b395SVille Syrjälä 				   spt_port_hotplug_long_detect);
23256dbf30ceSVille Syrjälä 	}
23266dbf30ceSVille Syrjälä 
23276dbf30ceSVille Syrjälä 	if (hotplug2_trigger) {
23286dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
23296dbf30ceSVille Syrjälä 
23306dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG2);
23316dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG2, dig_hotplug_reg);
23326dbf30ceSVille Syrjälä 
23336dbf30ceSVille Syrjälä 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug2_trigger,
23346dbf30ceSVille Syrjälä 				   dig_hotplug_reg, hpd_spt,
23356dbf30ceSVille Syrjälä 				   spt_port_hotplug2_long_detect);
23366dbf30ceSVille Syrjälä 	}
23376dbf30ceSVille Syrjälä 
23386dbf30ceSVille Syrjälä 	if (pin_mask)
233991d14251STvrtko Ursulin 		intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
23406dbf30ceSVille Syrjälä 
23416dbf30ceSVille Syrjälä 	if (pch_iir & SDE_GMBUS_CPT)
234291d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
23436dbf30ceSVille Syrjälä }
23446dbf30ceSVille Syrjälä 
234591d14251STvrtko Ursulin static void ilk_hpd_irq_handler(struct drm_i915_private *dev_priv,
234691d14251STvrtko Ursulin 				u32 hotplug_trigger,
234740e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2348c008bc6eSPaulo Zanoni {
2349e4ce95aaSVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2350e4ce95aaSVille Syrjälä 
2351e4ce95aaSVille Syrjälä 	dig_hotplug_reg = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
2352e4ce95aaSVille Syrjälä 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, dig_hotplug_reg);
2353e4ce95aaSVille Syrjälä 
2354e4ce95aaSVille Syrjälä 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
235540e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2356e4ce95aaSVille Syrjälä 			   ilk_port_hotplug_long_detect);
235740e56410SVille Syrjälä 
235891d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2359e4ce95aaSVille Syrjälä }
2360c008bc6eSPaulo Zanoni 
236191d14251STvrtko Ursulin static void ilk_display_irq_handler(struct drm_i915_private *dev_priv,
236291d14251STvrtko Ursulin 				    u32 de_iir)
236340e56410SVille Syrjälä {
236440e56410SVille Syrjälä 	enum pipe pipe;
236540e56410SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG;
236640e56410SVille Syrjälä 
236740e56410SVille Syrjälä 	if (hotplug_trigger)
236891d14251STvrtko Ursulin 		ilk_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ilk);
236940e56410SVille Syrjälä 
2370c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
237191d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
2372c008bc6eSPaulo Zanoni 
2373c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
237491d14251STvrtko Ursulin 		intel_opregion_asle_intr(dev_priv);
2375c008bc6eSPaulo Zanoni 
2376c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
2377c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
2378c008bc6eSPaulo Zanoni 
2379055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2380fd3a4024SDaniel Vetter 		if (de_iir & DE_PIPE_VBLANK(pipe))
2381fd3a4024SDaniel Vetter 			drm_handle_vblank(&dev_priv->drm, pipe);
2382c008bc6eSPaulo Zanoni 
238340da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
23841f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
2385c008bc6eSPaulo Zanoni 
238640da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
238791d14251STvrtko Ursulin 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
2388c008bc6eSPaulo Zanoni 	}
2389c008bc6eSPaulo Zanoni 
2390c008bc6eSPaulo Zanoni 	/* check event from PCH */
2391c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
2392c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
2393c008bc6eSPaulo Zanoni 
239491d14251STvrtko Ursulin 		if (HAS_PCH_CPT(dev_priv))
239591d14251STvrtko Ursulin 			cpt_irq_handler(dev_priv, pch_iir);
2396c008bc6eSPaulo Zanoni 		else
239791d14251STvrtko Ursulin 			ibx_irq_handler(dev_priv, pch_iir);
2398c008bc6eSPaulo Zanoni 
2399c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
2400c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
2401c008bc6eSPaulo Zanoni 	}
2402c008bc6eSPaulo Zanoni 
240391d14251STvrtko Ursulin 	if (IS_GEN5(dev_priv) && de_iir & DE_PCU_EVENT)
240491d14251STvrtko Ursulin 		ironlake_rps_change_irq_handler(dev_priv);
2405c008bc6eSPaulo Zanoni }
2406c008bc6eSPaulo Zanoni 
240791d14251STvrtko Ursulin static void ivb_display_irq_handler(struct drm_i915_private *dev_priv,
240891d14251STvrtko Ursulin 				    u32 de_iir)
24099719fb98SPaulo Zanoni {
241007d27e20SDamien Lespiau 	enum pipe pipe;
241123bb4cb5SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG_IVB;
241223bb4cb5SVille Syrjälä 
241340e56410SVille Syrjälä 	if (hotplug_trigger)
241491d14251STvrtko Ursulin 		ilk_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ivb);
24159719fb98SPaulo Zanoni 
24169719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
241791d14251STvrtko Ursulin 		ivb_err_int_handler(dev_priv);
24189719fb98SPaulo Zanoni 
24199719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
242091d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
24219719fb98SPaulo Zanoni 
24229719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
242391d14251STvrtko Ursulin 		intel_opregion_asle_intr(dev_priv);
24249719fb98SPaulo Zanoni 
2425055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2426fd3a4024SDaniel Vetter 		if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)))
2427fd3a4024SDaniel Vetter 			drm_handle_vblank(&dev_priv->drm, pipe);
24289719fb98SPaulo Zanoni 	}
24299719fb98SPaulo Zanoni 
24309719fb98SPaulo Zanoni 	/* check event from PCH */
243191d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv) && (de_iir & DE_PCH_EVENT_IVB)) {
24329719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
24339719fb98SPaulo Zanoni 
243491d14251STvrtko Ursulin 		cpt_irq_handler(dev_priv, pch_iir);
24359719fb98SPaulo Zanoni 
24369719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
24379719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
24389719fb98SPaulo Zanoni 	}
24399719fb98SPaulo Zanoni }
24409719fb98SPaulo Zanoni 
244172c90f62SOscar Mateo /*
244272c90f62SOscar Mateo  * To handle irqs with the minimum potential races with fresh interrupts, we:
244372c90f62SOscar Mateo  * 1 - Disable Master Interrupt Control.
244472c90f62SOscar Mateo  * 2 - Find the source(s) of the interrupt.
244572c90f62SOscar Mateo  * 3 - Clear the Interrupt Identity bits (IIR).
244672c90f62SOscar Mateo  * 4 - Process the interrupt(s) that had bits set in the IIRs.
244772c90f62SOscar Mateo  * 5 - Re-enable Master Interrupt Control.
244872c90f62SOscar Mateo  */
2449f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
2450b1f14ad0SJesse Barnes {
245145a83f84SDaniel Vetter 	struct drm_device *dev = arg;
2452fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2453f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
24540e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
2455b1f14ad0SJesse Barnes 
24562dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
24572dd2a883SImre Deak 		return IRQ_NONE;
24582dd2a883SImre Deak 
24591f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
24601f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
24611f814dacSImre Deak 
2462b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
2463b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
2464b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
246523a78516SPaulo Zanoni 	POSTING_READ(DEIER);
24660e43406bSChris Wilson 
246744498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
246844498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
246944498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
247044498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
247144498aeaSPaulo Zanoni 	 * due to its back queue). */
247291d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv)) {
247344498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
247444498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
247544498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2476ab5c608bSBen Widawsky 	}
247744498aeaSPaulo Zanoni 
247872c90f62SOscar Mateo 	/* Find, clear, then process each source of interrupt */
247972c90f62SOscar Mateo 
24800e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
24810e43406bSChris Wilson 	if (gt_iir) {
248272c90f62SOscar Mateo 		I915_WRITE(GTIIR, gt_iir);
248372c90f62SOscar Mateo 		ret = IRQ_HANDLED;
248491d14251STvrtko Ursulin 		if (INTEL_GEN(dev_priv) >= 6)
2485261e40b8SVille Syrjälä 			snb_gt_irq_handler(dev_priv, gt_iir);
2486d8fc8a47SPaulo Zanoni 		else
2487261e40b8SVille Syrjälä 			ilk_gt_irq_handler(dev_priv, gt_iir);
24880e43406bSChris Wilson 	}
2489b1f14ad0SJesse Barnes 
2490b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
24910e43406bSChris Wilson 	if (de_iir) {
249272c90f62SOscar Mateo 		I915_WRITE(DEIIR, de_iir);
249372c90f62SOscar Mateo 		ret = IRQ_HANDLED;
249491d14251STvrtko Ursulin 		if (INTEL_GEN(dev_priv) >= 7)
249591d14251STvrtko Ursulin 			ivb_display_irq_handler(dev_priv, de_iir);
2496f1af8fc1SPaulo Zanoni 		else
249791d14251STvrtko Ursulin 			ilk_display_irq_handler(dev_priv, de_iir);
24980e43406bSChris Wilson 	}
24990e43406bSChris Wilson 
250091d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 6) {
2501f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
25020e43406bSChris Wilson 		if (pm_iir) {
2503b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
25040e43406bSChris Wilson 			ret = IRQ_HANDLED;
250572c90f62SOscar Mateo 			gen6_rps_irq_handler(dev_priv, pm_iir);
25060e43406bSChris Wilson 		}
2507f1af8fc1SPaulo Zanoni 	}
2508b1f14ad0SJesse Barnes 
2509b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
2510b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
251191d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv)) {
251244498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
251344498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2514ab5c608bSBen Widawsky 	}
2515b1f14ad0SJesse Barnes 
25161f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
25171f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
25181f814dacSImre Deak 
2519b1f14ad0SJesse Barnes 	return ret;
2520b1f14ad0SJesse Barnes }
2521b1f14ad0SJesse Barnes 
252291d14251STvrtko Ursulin static void bxt_hpd_irq_handler(struct drm_i915_private *dev_priv,
252391d14251STvrtko Ursulin 				u32 hotplug_trigger,
252440e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2525d04a492dSShashank Sharma {
2526cebd87a0SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2527d04a492dSShashank Sharma 
2528a52bb15bSVille Syrjälä 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
2529a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
2530d04a492dSShashank Sharma 
2531cebd87a0SVille Syrjälä 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
253240e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2533cebd87a0SVille Syrjälä 			   bxt_port_hotplug_long_detect);
253440e56410SVille Syrjälä 
253591d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2536d04a492dSShashank Sharma }
2537d04a492dSShashank Sharma 
2538f11a0f46STvrtko Ursulin static irqreturn_t
2539f11a0f46STvrtko Ursulin gen8_de_irq_handler(struct drm_i915_private *dev_priv, u32 master_ctl)
2540abd58f01SBen Widawsky {
2541abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
2542f11a0f46STvrtko Ursulin 	u32 iir;
2543c42664ccSDaniel Vetter 	enum pipe pipe;
254488e04703SJesse Barnes 
2545abd58f01SBen Widawsky 	if (master_ctl & GEN8_DE_MISC_IRQ) {
2546e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_MISC_IIR);
2547e32192e1STvrtko Ursulin 		if (iir) {
2548e32192e1STvrtko Ursulin 			I915_WRITE(GEN8_DE_MISC_IIR, iir);
2549abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
2550e32192e1STvrtko Ursulin 			if (iir & GEN8_DE_MISC_GSE)
255191d14251STvrtko Ursulin 				intel_opregion_asle_intr(dev_priv);
255238cc46d7SOscar Mateo 			else
255338cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Misc interrupt\n");
2554abd58f01SBen Widawsky 		}
255538cc46d7SOscar Mateo 		else
255638cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
2557abd58f01SBen Widawsky 	}
2558abd58f01SBen Widawsky 
25596d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
2560e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_PORT_IIR);
2561e32192e1STvrtko Ursulin 		if (iir) {
2562e32192e1STvrtko Ursulin 			u32 tmp_mask;
2563d04a492dSShashank Sharma 			bool found = false;
2564cebd87a0SVille Syrjälä 
2565e32192e1STvrtko Ursulin 			I915_WRITE(GEN8_DE_PORT_IIR, iir);
25666d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
256788e04703SJesse Barnes 
2568e32192e1STvrtko Ursulin 			tmp_mask = GEN8_AUX_CHANNEL_A;
2569bca2bf2aSPandiyan, Dhinakaran 			if (INTEL_GEN(dev_priv) >= 9)
2570e32192e1STvrtko Ursulin 				tmp_mask |= GEN9_AUX_CHANNEL_B |
2571e32192e1STvrtko Ursulin 					    GEN9_AUX_CHANNEL_C |
2572e32192e1STvrtko Ursulin 					    GEN9_AUX_CHANNEL_D;
2573e32192e1STvrtko Ursulin 
2574e32192e1STvrtko Ursulin 			if (iir & tmp_mask) {
257591d14251STvrtko Ursulin 				dp_aux_irq_handler(dev_priv);
2576d04a492dSShashank Sharma 				found = true;
2577d04a492dSShashank Sharma 			}
2578d04a492dSShashank Sharma 
2579cc3f90f0SAnder Conselvan de Oliveira 			if (IS_GEN9_LP(dev_priv)) {
2580e32192e1STvrtko Ursulin 				tmp_mask = iir & BXT_DE_PORT_HOTPLUG_MASK;
2581e32192e1STvrtko Ursulin 				if (tmp_mask) {
258291d14251STvrtko Ursulin 					bxt_hpd_irq_handler(dev_priv, tmp_mask,
258391d14251STvrtko Ursulin 							    hpd_bxt);
2584d04a492dSShashank Sharma 					found = true;
2585d04a492dSShashank Sharma 				}
2586e32192e1STvrtko Ursulin 			} else if (IS_BROADWELL(dev_priv)) {
2587e32192e1STvrtko Ursulin 				tmp_mask = iir & GEN8_PORT_DP_A_HOTPLUG;
2588e32192e1STvrtko Ursulin 				if (tmp_mask) {
258991d14251STvrtko Ursulin 					ilk_hpd_irq_handler(dev_priv,
259091d14251STvrtko Ursulin 							    tmp_mask, hpd_bdw);
2591e32192e1STvrtko Ursulin 					found = true;
2592e32192e1STvrtko Ursulin 				}
2593e32192e1STvrtko Ursulin 			}
2594d04a492dSShashank Sharma 
2595cc3f90f0SAnder Conselvan de Oliveira 			if (IS_GEN9_LP(dev_priv) && (iir & BXT_DE_PORT_GMBUS)) {
259691d14251STvrtko Ursulin 				gmbus_irq_handler(dev_priv);
25979e63743eSShashank Sharma 				found = true;
25989e63743eSShashank Sharma 			}
25999e63743eSShashank Sharma 
2600d04a492dSShashank Sharma 			if (!found)
260138cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Port interrupt\n");
26026d766f02SDaniel Vetter 		}
260338cc46d7SOscar Mateo 		else
260438cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
26056d766f02SDaniel Vetter 	}
26066d766f02SDaniel Vetter 
2607055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2608fd3a4024SDaniel Vetter 		u32 fault_errors;
2609abd58f01SBen Widawsky 
2610c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2611c42664ccSDaniel Vetter 			continue;
2612c42664ccSDaniel Vetter 
2613e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
2614e32192e1STvrtko Ursulin 		if (!iir) {
2615e32192e1STvrtko Ursulin 			DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
2616e32192e1STvrtko Ursulin 			continue;
2617e32192e1STvrtko Ursulin 		}
2618770de83dSDamien Lespiau 
2619e32192e1STvrtko Ursulin 		ret = IRQ_HANDLED;
2620e32192e1STvrtko Ursulin 		I915_WRITE(GEN8_DE_PIPE_IIR(pipe), iir);
2621e32192e1STvrtko Ursulin 
2622fd3a4024SDaniel Vetter 		if (iir & GEN8_PIPE_VBLANK)
2623fd3a4024SDaniel Vetter 			drm_handle_vblank(&dev_priv->drm, pipe);
2624abd58f01SBen Widawsky 
2625e32192e1STvrtko Ursulin 		if (iir & GEN8_PIPE_CDCLK_CRC_DONE)
262691d14251STvrtko Ursulin 			hsw_pipe_crc_irq_handler(dev_priv, pipe);
26270fbe7870SDaniel Vetter 
2628e32192e1STvrtko Ursulin 		if (iir & GEN8_PIPE_FIFO_UNDERRUN)
2629e32192e1STvrtko Ursulin 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
263038d83c96SDaniel Vetter 
2631e32192e1STvrtko Ursulin 		fault_errors = iir;
2632bca2bf2aSPandiyan, Dhinakaran 		if (INTEL_GEN(dev_priv) >= 9)
2633e32192e1STvrtko Ursulin 			fault_errors &= GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
2634770de83dSDamien Lespiau 		else
2635e32192e1STvrtko Ursulin 			fault_errors &= GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
2636770de83dSDamien Lespiau 
2637770de83dSDamien Lespiau 		if (fault_errors)
26381353ec38STvrtko Ursulin 			DRM_ERROR("Fault errors on pipe %c: 0x%08x\n",
263930100f2bSDaniel Vetter 				  pipe_name(pipe),
2640e32192e1STvrtko Ursulin 				  fault_errors);
2641abd58f01SBen Widawsky 	}
2642abd58f01SBen Widawsky 
264391d14251STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv) && !HAS_PCH_NOP(dev_priv) &&
2644266ea3d9SShashank Sharma 	    master_ctl & GEN8_DE_PCH_IRQ) {
264592d03a80SDaniel Vetter 		/*
264692d03a80SDaniel Vetter 		 * FIXME(BDW): Assume for now that the new interrupt handling
264792d03a80SDaniel Vetter 		 * scheme also closed the SDE interrupt handling race we've seen
264892d03a80SDaniel Vetter 		 * on older pch-split platforms. But this needs testing.
264992d03a80SDaniel Vetter 		 */
2650e32192e1STvrtko Ursulin 		iir = I915_READ(SDEIIR);
2651e32192e1STvrtko Ursulin 		if (iir) {
2652e32192e1STvrtko Ursulin 			I915_WRITE(SDEIIR, iir);
265392d03a80SDaniel Vetter 			ret = IRQ_HANDLED;
26546dbf30ceSVille Syrjälä 
26557b22b8c4SRodrigo Vivi 			if (HAS_PCH_SPT(dev_priv) || HAS_PCH_KBP(dev_priv) ||
26567b22b8c4SRodrigo Vivi 			    HAS_PCH_CNP(dev_priv))
265791d14251STvrtko Ursulin 				spt_irq_handler(dev_priv, iir);
26586dbf30ceSVille Syrjälä 			else
265991d14251STvrtko Ursulin 				cpt_irq_handler(dev_priv, iir);
26602dfb0b81SJani Nikula 		} else {
26612dfb0b81SJani Nikula 			/*
26622dfb0b81SJani Nikula 			 * Like on previous PCH there seems to be something
26632dfb0b81SJani Nikula 			 * fishy going on with forwarding PCH interrupts.
26642dfb0b81SJani Nikula 			 */
26652dfb0b81SJani Nikula 			DRM_DEBUG_DRIVER("The master control interrupt lied (SDE)!\n");
26662dfb0b81SJani Nikula 		}
266792d03a80SDaniel Vetter 	}
266892d03a80SDaniel Vetter 
2669f11a0f46STvrtko Ursulin 	return ret;
2670f11a0f46STvrtko Ursulin }
2671f11a0f46STvrtko Ursulin 
2672f11a0f46STvrtko Ursulin static irqreturn_t gen8_irq_handler(int irq, void *arg)
2673f11a0f46STvrtko Ursulin {
2674f11a0f46STvrtko Ursulin 	struct drm_device *dev = arg;
2675fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2676f11a0f46STvrtko Ursulin 	u32 master_ctl;
2677e30e251aSVille Syrjälä 	u32 gt_iir[4] = {};
2678f11a0f46STvrtko Ursulin 	irqreturn_t ret;
2679f11a0f46STvrtko Ursulin 
2680f11a0f46STvrtko Ursulin 	if (!intel_irqs_enabled(dev_priv))
2681f11a0f46STvrtko Ursulin 		return IRQ_NONE;
2682f11a0f46STvrtko Ursulin 
2683f11a0f46STvrtko Ursulin 	master_ctl = I915_READ_FW(GEN8_MASTER_IRQ);
2684f11a0f46STvrtko Ursulin 	master_ctl &= ~GEN8_MASTER_IRQ_CONTROL;
2685f11a0f46STvrtko Ursulin 	if (!master_ctl)
2686f11a0f46STvrtko Ursulin 		return IRQ_NONE;
2687f11a0f46STvrtko Ursulin 
2688f11a0f46STvrtko Ursulin 	I915_WRITE_FW(GEN8_MASTER_IRQ, 0);
2689f11a0f46STvrtko Ursulin 
2690f11a0f46STvrtko Ursulin 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
2691f11a0f46STvrtko Ursulin 	disable_rpm_wakeref_asserts(dev_priv);
2692f11a0f46STvrtko Ursulin 
2693f11a0f46STvrtko Ursulin 	/* Find, clear, then process each source of interrupt */
2694e30e251aSVille Syrjälä 	ret = gen8_gt_irq_ack(dev_priv, master_ctl, gt_iir);
2695e30e251aSVille Syrjälä 	gen8_gt_irq_handler(dev_priv, gt_iir);
2696f11a0f46STvrtko Ursulin 	ret |= gen8_de_irq_handler(dev_priv, master_ctl);
2697f11a0f46STvrtko Ursulin 
2698cb0d205eSChris Wilson 	I915_WRITE_FW(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
2699cb0d205eSChris Wilson 	POSTING_READ_FW(GEN8_MASTER_IRQ);
2700abd58f01SBen Widawsky 
27011f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
27021f814dacSImre Deak 
2703abd58f01SBen Widawsky 	return ret;
2704abd58f01SBen Widawsky }
2705abd58f01SBen Widawsky 
270636703e79SChris Wilson struct wedge_me {
270736703e79SChris Wilson 	struct delayed_work work;
270836703e79SChris Wilson 	struct drm_i915_private *i915;
270936703e79SChris Wilson 	const char *name;
271036703e79SChris Wilson };
271136703e79SChris Wilson 
271236703e79SChris Wilson static void wedge_me(struct work_struct *work)
271336703e79SChris Wilson {
271436703e79SChris Wilson 	struct wedge_me *w = container_of(work, typeof(*w), work.work);
271536703e79SChris Wilson 
271636703e79SChris Wilson 	dev_err(w->i915->drm.dev,
271736703e79SChris Wilson 		"%s timed out, cancelling all in-flight rendering.\n",
271836703e79SChris Wilson 		w->name);
271936703e79SChris Wilson 	i915_gem_set_wedged(w->i915);
272036703e79SChris Wilson }
272136703e79SChris Wilson 
272236703e79SChris Wilson static void __init_wedge(struct wedge_me *w,
272336703e79SChris Wilson 			 struct drm_i915_private *i915,
272436703e79SChris Wilson 			 long timeout,
272536703e79SChris Wilson 			 const char *name)
272636703e79SChris Wilson {
272736703e79SChris Wilson 	w->i915 = i915;
272836703e79SChris Wilson 	w->name = name;
272936703e79SChris Wilson 
273036703e79SChris Wilson 	INIT_DELAYED_WORK_ONSTACK(&w->work, wedge_me);
273136703e79SChris Wilson 	schedule_delayed_work(&w->work, timeout);
273236703e79SChris Wilson }
273336703e79SChris Wilson 
273436703e79SChris Wilson static void __fini_wedge(struct wedge_me *w)
273536703e79SChris Wilson {
273636703e79SChris Wilson 	cancel_delayed_work_sync(&w->work);
273736703e79SChris Wilson 	destroy_delayed_work_on_stack(&w->work);
273836703e79SChris Wilson 	w->i915 = NULL;
273936703e79SChris Wilson }
274036703e79SChris Wilson 
274136703e79SChris Wilson #define i915_wedge_on_timeout(W, DEV, TIMEOUT)				\
274236703e79SChris Wilson 	for (__init_wedge((W), (DEV), (TIMEOUT), __func__);		\
274336703e79SChris Wilson 	     (W)->i915;							\
274436703e79SChris Wilson 	     __fini_wedge((W)))
274536703e79SChris Wilson 
27468a905236SJesse Barnes /**
2747d5367307SChris Wilson  * i915_reset_device - do process context error handling work
274814bb2c11STvrtko Ursulin  * @dev_priv: i915 device private
27498a905236SJesse Barnes  *
27508a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
27518a905236SJesse Barnes  * was detected.
27528a905236SJesse Barnes  */
2753d5367307SChris Wilson static void i915_reset_device(struct drm_i915_private *dev_priv)
27548a905236SJesse Barnes {
275591c8a326SChris Wilson 	struct kobject *kobj = &dev_priv->drm.primary->kdev->kobj;
2756cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
2757cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
2758cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
275936703e79SChris Wilson 	struct wedge_me w;
27608a905236SJesse Barnes 
2761c033666aSChris Wilson 	kobject_uevent_env(kobj, KOBJ_CHANGE, error_event);
27628a905236SJesse Barnes 
276344d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("resetting chip\n");
2764c033666aSChris Wilson 	kobject_uevent_env(kobj, KOBJ_CHANGE, reset_event);
27651f83fee0SDaniel Vetter 
276636703e79SChris Wilson 	/* Use a watchdog to ensure that our reset completes */
276736703e79SChris Wilson 	i915_wedge_on_timeout(&w, dev_priv, 5*HZ) {
2768c033666aSChris Wilson 		intel_prepare_reset(dev_priv);
27697514747dSVille Syrjälä 
277036703e79SChris Wilson 		/* Signal that locked waiters should reset the GPU */
27718c185ecaSChris Wilson 		set_bit(I915_RESET_HANDOFF, &dev_priv->gpu_error.flags);
27728c185ecaSChris Wilson 		wake_up_all(&dev_priv->gpu_error.wait_queue);
27738c185ecaSChris Wilson 
277436703e79SChris Wilson 		/* Wait for anyone holding the lock to wakeup, without
277536703e79SChris Wilson 		 * blocking indefinitely on struct_mutex.
277617e1df07SDaniel Vetter 		 */
277736703e79SChris Wilson 		do {
2778780f262aSChris Wilson 			if (mutex_trylock(&dev_priv->drm.struct_mutex)) {
2779535275d3SChris Wilson 				i915_reset(dev_priv, 0);
2780221fe799SChris Wilson 				mutex_unlock(&dev_priv->drm.struct_mutex);
2781780f262aSChris Wilson 			}
2782780f262aSChris Wilson 		} while (wait_on_bit_timeout(&dev_priv->gpu_error.flags,
27838c185ecaSChris Wilson 					     I915_RESET_HANDOFF,
2784780f262aSChris Wilson 					     TASK_UNINTERRUPTIBLE,
278536703e79SChris Wilson 					     1));
2786f69061beSDaniel Vetter 
2787c033666aSChris Wilson 		intel_finish_reset(dev_priv);
278836703e79SChris Wilson 	}
2789f454c694SImre Deak 
2790780f262aSChris Wilson 	if (!test_bit(I915_WEDGED, &dev_priv->gpu_error.flags))
2791c033666aSChris Wilson 		kobject_uevent_env(kobj,
2792f69061beSDaniel Vetter 				   KOBJ_CHANGE, reset_done_event);
2793f316a42cSBen Gamari }
27948a905236SJesse Barnes 
2795eaa14c24SChris Wilson static void i915_clear_error_registers(struct drm_i915_private *dev_priv)
2796c0e09200SDave Airlie {
2797eaa14c24SChris Wilson 	u32 eir;
279863eeaf38SJesse Barnes 
2799eaa14c24SChris Wilson 	if (!IS_GEN2(dev_priv))
2800eaa14c24SChris Wilson 		I915_WRITE(PGTBL_ER, I915_READ(PGTBL_ER));
280163eeaf38SJesse Barnes 
2802eaa14c24SChris Wilson 	if (INTEL_GEN(dev_priv) < 4)
2803eaa14c24SChris Wilson 		I915_WRITE(IPEIR, I915_READ(IPEIR));
2804eaa14c24SChris Wilson 	else
2805eaa14c24SChris Wilson 		I915_WRITE(IPEIR_I965, I915_READ(IPEIR_I965));
28068a905236SJesse Barnes 
2807eaa14c24SChris Wilson 	I915_WRITE(EIR, I915_READ(EIR));
280863eeaf38SJesse Barnes 	eir = I915_READ(EIR);
280963eeaf38SJesse Barnes 	if (eir) {
281063eeaf38SJesse Barnes 		/*
281163eeaf38SJesse Barnes 		 * some errors might have become stuck,
281263eeaf38SJesse Barnes 		 * mask them.
281363eeaf38SJesse Barnes 		 */
2814eaa14c24SChris Wilson 		DRM_DEBUG_DRIVER("EIR stuck: 0x%08x, masking\n", eir);
281563eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
281663eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
281763eeaf38SJesse Barnes 	}
281835aed2e6SChris Wilson }
281935aed2e6SChris Wilson 
282035aed2e6SChris Wilson /**
2821b8d24a06SMika Kuoppala  * i915_handle_error - handle a gpu error
282214bb2c11STvrtko Ursulin  * @dev_priv: i915 device private
282314b730fcSarun.siluvery@linux.intel.com  * @engine_mask: mask representing engines that are hung
282487c390b6SMichel Thierry  * @fmt: Error message format string
282587c390b6SMichel Thierry  *
2826aafd8581SJavier Martinez Canillas  * Do some basic checking of register state at error time and
282735aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
282835aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
282935aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
283035aed2e6SChris Wilson  * of a ring dump etc.).
283135aed2e6SChris Wilson  */
2832c033666aSChris Wilson void i915_handle_error(struct drm_i915_private *dev_priv,
2833c033666aSChris Wilson 		       u32 engine_mask,
283458174462SMika Kuoppala 		       const char *fmt, ...)
283535aed2e6SChris Wilson {
2836142bc7d9SMichel Thierry 	struct intel_engine_cs *engine;
2837142bc7d9SMichel Thierry 	unsigned int tmp;
283858174462SMika Kuoppala 	va_list args;
283958174462SMika Kuoppala 	char error_msg[80];
284035aed2e6SChris Wilson 
284158174462SMika Kuoppala 	va_start(args, fmt);
284258174462SMika Kuoppala 	vscnprintf(error_msg, sizeof(error_msg), fmt, args);
284358174462SMika Kuoppala 	va_end(args);
284458174462SMika Kuoppala 
28451604a86dSChris Wilson 	/*
28461604a86dSChris Wilson 	 * In most cases it's guaranteed that we get here with an RPM
28471604a86dSChris Wilson 	 * reference held, for example because there is a pending GPU
28481604a86dSChris Wilson 	 * request that won't finish until the reset is done. This
28491604a86dSChris Wilson 	 * isn't the case at least when we get here by doing a
28501604a86dSChris Wilson 	 * simulated reset via debugfs, so get an RPM reference.
28511604a86dSChris Wilson 	 */
28521604a86dSChris Wilson 	intel_runtime_pm_get(dev_priv);
28531604a86dSChris Wilson 
2854c033666aSChris Wilson 	i915_capture_error_state(dev_priv, engine_mask, error_msg);
2855eaa14c24SChris Wilson 	i915_clear_error_registers(dev_priv);
28568a905236SJesse Barnes 
2857142bc7d9SMichel Thierry 	/*
2858142bc7d9SMichel Thierry 	 * Try engine reset when available. We fall back to full reset if
2859142bc7d9SMichel Thierry 	 * single reset fails.
2860142bc7d9SMichel Thierry 	 */
2861142bc7d9SMichel Thierry 	if (intel_has_reset_engine(dev_priv)) {
2862142bc7d9SMichel Thierry 		for_each_engine_masked(engine, dev_priv, engine_mask, tmp) {
28639db529aaSDaniel Vetter 			BUILD_BUG_ON(I915_RESET_MODESET >= I915_RESET_ENGINE);
2864142bc7d9SMichel Thierry 			if (test_and_set_bit(I915_RESET_ENGINE + engine->id,
2865142bc7d9SMichel Thierry 					     &dev_priv->gpu_error.flags))
2866142bc7d9SMichel Thierry 				continue;
2867142bc7d9SMichel Thierry 
2868535275d3SChris Wilson 			if (i915_reset_engine(engine, 0) == 0)
2869142bc7d9SMichel Thierry 				engine_mask &= ~intel_engine_flag(engine);
2870142bc7d9SMichel Thierry 
2871142bc7d9SMichel Thierry 			clear_bit(I915_RESET_ENGINE + engine->id,
2872142bc7d9SMichel Thierry 				  &dev_priv->gpu_error.flags);
2873142bc7d9SMichel Thierry 			wake_up_bit(&dev_priv->gpu_error.flags,
2874142bc7d9SMichel Thierry 				    I915_RESET_ENGINE + engine->id);
2875142bc7d9SMichel Thierry 		}
2876142bc7d9SMichel Thierry 	}
2877142bc7d9SMichel Thierry 
28788af29b0cSChris Wilson 	if (!engine_mask)
28791604a86dSChris Wilson 		goto out;
28808af29b0cSChris Wilson 
2881142bc7d9SMichel Thierry 	/* Full reset needs the mutex, stop any other user trying to do so. */
2882d5367307SChris Wilson 	if (test_and_set_bit(I915_RESET_BACKOFF, &dev_priv->gpu_error.flags)) {
2883d5367307SChris Wilson 		wait_event(dev_priv->gpu_error.reset_queue,
2884d5367307SChris Wilson 			   !test_bit(I915_RESET_BACKOFF,
2885d5367307SChris Wilson 				     &dev_priv->gpu_error.flags));
28861604a86dSChris Wilson 		goto out;
2887d5367307SChris Wilson 	}
2888ba1234d1SBen Gamari 
2889142bc7d9SMichel Thierry 	/* Prevent any other reset-engine attempt. */
2890142bc7d9SMichel Thierry 	for_each_engine(engine, dev_priv, tmp) {
2891142bc7d9SMichel Thierry 		while (test_and_set_bit(I915_RESET_ENGINE + engine->id,
2892142bc7d9SMichel Thierry 					&dev_priv->gpu_error.flags))
2893142bc7d9SMichel Thierry 			wait_on_bit(&dev_priv->gpu_error.flags,
2894142bc7d9SMichel Thierry 				    I915_RESET_ENGINE + engine->id,
2895142bc7d9SMichel Thierry 				    TASK_UNINTERRUPTIBLE);
2896142bc7d9SMichel Thierry 	}
2897142bc7d9SMichel Thierry 
2898d5367307SChris Wilson 	i915_reset_device(dev_priv);
2899d5367307SChris Wilson 
2900142bc7d9SMichel Thierry 	for_each_engine(engine, dev_priv, tmp) {
2901142bc7d9SMichel Thierry 		clear_bit(I915_RESET_ENGINE + engine->id,
2902142bc7d9SMichel Thierry 			  &dev_priv->gpu_error.flags);
2903142bc7d9SMichel Thierry 	}
2904142bc7d9SMichel Thierry 
2905d5367307SChris Wilson 	clear_bit(I915_RESET_BACKOFF, &dev_priv->gpu_error.flags);
2906d5367307SChris Wilson 	wake_up_all(&dev_priv->gpu_error.reset_queue);
29071604a86dSChris Wilson 
29081604a86dSChris Wilson out:
29091604a86dSChris Wilson 	intel_runtime_pm_put(dev_priv);
29108a905236SJesse Barnes }
29118a905236SJesse Barnes 
291242f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
291342f52ef8SKeith Packard  * we use as a pipe index
291442f52ef8SKeith Packard  */
291586e83e35SChris Wilson static int i8xx_enable_vblank(struct drm_device *dev, unsigned int pipe)
29160a3e67a4SJesse Barnes {
2917fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2918e9d21d7fSKeith Packard 	unsigned long irqflags;
291971e0ffa5SJesse Barnes 
29201ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
292186e83e35SChris Wilson 	i915_enable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_STATUS);
292286e83e35SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
292386e83e35SChris Wilson 
292486e83e35SChris Wilson 	return 0;
292586e83e35SChris Wilson }
292686e83e35SChris Wilson 
292786e83e35SChris Wilson static int i965_enable_vblank(struct drm_device *dev, unsigned int pipe)
292886e83e35SChris Wilson {
292986e83e35SChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
293086e83e35SChris Wilson 	unsigned long irqflags;
293186e83e35SChris Wilson 
293286e83e35SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
29337c463586SKeith Packard 	i915_enable_pipestat(dev_priv, pipe,
2934755e9019SImre Deak 			     PIPE_START_VBLANK_INTERRUPT_STATUS);
29351ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
29368692d00eSChris Wilson 
29370a3e67a4SJesse Barnes 	return 0;
29380a3e67a4SJesse Barnes }
29390a3e67a4SJesse Barnes 
294088e72717SThierry Reding static int ironlake_enable_vblank(struct drm_device *dev, unsigned int pipe)
2941f796cf8fSJesse Barnes {
2942fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2943f796cf8fSJesse Barnes 	unsigned long irqflags;
294455b8f2a7STvrtko Ursulin 	uint32_t bit = INTEL_GEN(dev_priv) >= 7 ?
294586e83e35SChris Wilson 		DE_PIPE_VBLANK_IVB(pipe) : DE_PIPE_VBLANK(pipe);
2946f796cf8fSJesse Barnes 
2947f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2948fbdedaeaSVille Syrjälä 	ilk_enable_display_irq(dev_priv, bit);
2949b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2950b1f14ad0SJesse Barnes 
2951b1f14ad0SJesse Barnes 	return 0;
2952b1f14ad0SJesse Barnes }
2953b1f14ad0SJesse Barnes 
295488e72717SThierry Reding static int gen8_enable_vblank(struct drm_device *dev, unsigned int pipe)
2955abd58f01SBen Widawsky {
2956fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2957abd58f01SBen Widawsky 	unsigned long irqflags;
2958abd58f01SBen Widawsky 
2959abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2960013d3752SVille Syrjälä 	bdw_enable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
2961abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2962013d3752SVille Syrjälä 
2963abd58f01SBen Widawsky 	return 0;
2964abd58f01SBen Widawsky }
2965abd58f01SBen Widawsky 
296642f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
296742f52ef8SKeith Packard  * we use as a pipe index
296842f52ef8SKeith Packard  */
296986e83e35SChris Wilson static void i8xx_disable_vblank(struct drm_device *dev, unsigned int pipe)
297086e83e35SChris Wilson {
297186e83e35SChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
297286e83e35SChris Wilson 	unsigned long irqflags;
297386e83e35SChris Wilson 
297486e83e35SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
297586e83e35SChris Wilson 	i915_disable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_STATUS);
297686e83e35SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
297786e83e35SChris Wilson }
297886e83e35SChris Wilson 
297986e83e35SChris Wilson static void i965_disable_vblank(struct drm_device *dev, unsigned int pipe)
29800a3e67a4SJesse Barnes {
2981fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2982e9d21d7fSKeith Packard 	unsigned long irqflags;
29830a3e67a4SJesse Barnes 
29841ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
29857c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
2986755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
29871ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
29880a3e67a4SJesse Barnes }
29890a3e67a4SJesse Barnes 
299088e72717SThierry Reding static void ironlake_disable_vblank(struct drm_device *dev, unsigned int pipe)
2991f796cf8fSJesse Barnes {
2992fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2993f796cf8fSJesse Barnes 	unsigned long irqflags;
299455b8f2a7STvrtko Ursulin 	uint32_t bit = INTEL_GEN(dev_priv) >= 7 ?
299586e83e35SChris Wilson 		DE_PIPE_VBLANK_IVB(pipe) : DE_PIPE_VBLANK(pipe);
2996f796cf8fSJesse Barnes 
2997f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2998fbdedaeaSVille Syrjälä 	ilk_disable_display_irq(dev_priv, bit);
2999b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3000b1f14ad0SJesse Barnes }
3001b1f14ad0SJesse Barnes 
300288e72717SThierry Reding static void gen8_disable_vblank(struct drm_device *dev, unsigned int pipe)
3003abd58f01SBen Widawsky {
3004fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3005abd58f01SBen Widawsky 	unsigned long irqflags;
3006abd58f01SBen Widawsky 
3007abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3008013d3752SVille Syrjälä 	bdw_disable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
3009abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3010abd58f01SBen Widawsky }
3011abd58f01SBen Widawsky 
3012b243f530STvrtko Ursulin static void ibx_irq_reset(struct drm_i915_private *dev_priv)
301391738a95SPaulo Zanoni {
30146e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
301591738a95SPaulo Zanoni 		return;
301691738a95SPaulo Zanoni 
30173488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(SDE);
3018105b122eSPaulo Zanoni 
30196e266956STvrtko Ursulin 	if (HAS_PCH_CPT(dev_priv) || HAS_PCH_LPT(dev_priv))
3020105b122eSPaulo Zanoni 		I915_WRITE(SERR_INT, 0xffffffff);
3021622364b6SPaulo Zanoni }
3022105b122eSPaulo Zanoni 
302391738a95SPaulo Zanoni /*
3024622364b6SPaulo Zanoni  * SDEIER is also touched by the interrupt handler to work around missed PCH
3025622364b6SPaulo Zanoni  * interrupts. Hence we can't update it after the interrupt handler is enabled -
3026622364b6SPaulo Zanoni  * instead we unconditionally enable all PCH interrupt sources here, but then
3027622364b6SPaulo Zanoni  * only unmask them as needed with SDEIMR.
3028622364b6SPaulo Zanoni  *
3029622364b6SPaulo Zanoni  * This function needs to be called before interrupts are enabled.
303091738a95SPaulo Zanoni  */
3031622364b6SPaulo Zanoni static void ibx_irq_pre_postinstall(struct drm_device *dev)
3032622364b6SPaulo Zanoni {
3033fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3034622364b6SPaulo Zanoni 
30356e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
3036622364b6SPaulo Zanoni 		return;
3037622364b6SPaulo Zanoni 
3038622364b6SPaulo Zanoni 	WARN_ON(I915_READ(SDEIER) != 0);
303991738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
304091738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
304191738a95SPaulo Zanoni }
304291738a95SPaulo Zanoni 
3043b243f530STvrtko Ursulin static void gen5_gt_irq_reset(struct drm_i915_private *dev_priv)
3044d18ea1b5SDaniel Vetter {
30453488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(GT);
3046b243f530STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 6)
30473488d4ebSVille Syrjälä 		GEN3_IRQ_RESET(GEN6_PM);
3048d18ea1b5SDaniel Vetter }
3049d18ea1b5SDaniel Vetter 
305070591a41SVille Syrjälä static void vlv_display_irq_reset(struct drm_i915_private *dev_priv)
305170591a41SVille Syrjälä {
305271b8b41dSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
305371b8b41dSVille Syrjälä 		I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
305471b8b41dSVille Syrjälä 	else
305571b8b41dSVille Syrjälä 		I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
305671b8b41dSVille Syrjälä 
3057ad22d106SVille Syrjälä 	i915_hotplug_interrupt_update_locked(dev_priv, 0xffffffff, 0);
305870591a41SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
305970591a41SVille Syrjälä 
306044d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
306170591a41SVille Syrjälä 
30623488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(VLV_);
3063ad22d106SVille Syrjälä 	dev_priv->irq_mask = ~0;
306470591a41SVille Syrjälä }
306570591a41SVille Syrjälä 
30668bb61306SVille Syrjälä static void vlv_display_irq_postinstall(struct drm_i915_private *dev_priv)
30678bb61306SVille Syrjälä {
30688bb61306SVille Syrjälä 	u32 pipestat_mask;
30699ab981f2SVille Syrjälä 	u32 enable_mask;
30708bb61306SVille Syrjälä 	enum pipe pipe;
30718bb61306SVille Syrjälä 
3072842ebf7aSVille Syrjälä 	pipestat_mask = PIPE_CRC_DONE_INTERRUPT_STATUS;
30738bb61306SVille Syrjälä 
30748bb61306SVille Syrjälä 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
30758bb61306SVille Syrjälä 	for_each_pipe(dev_priv, pipe)
30768bb61306SVille Syrjälä 		i915_enable_pipestat(dev_priv, pipe, pipestat_mask);
30778bb61306SVille Syrjälä 
30789ab981f2SVille Syrjälä 	enable_mask = I915_DISPLAY_PORT_INTERRUPT |
30798bb61306SVille Syrjälä 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3080ebf5f921SVille Syrjälä 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3081ebf5f921SVille Syrjälä 		I915_LPE_PIPE_A_INTERRUPT |
3082ebf5f921SVille Syrjälä 		I915_LPE_PIPE_B_INTERRUPT;
3083ebf5f921SVille Syrjälä 
30848bb61306SVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
3085ebf5f921SVille Syrjälä 		enable_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT |
3086ebf5f921SVille Syrjälä 			I915_LPE_PIPE_C_INTERRUPT;
30876b7eafc1SVille Syrjälä 
30886b7eafc1SVille Syrjälä 	WARN_ON(dev_priv->irq_mask != ~0);
30896b7eafc1SVille Syrjälä 
30909ab981f2SVille Syrjälä 	dev_priv->irq_mask = ~enable_mask;
30918bb61306SVille Syrjälä 
30923488d4ebSVille Syrjälä 	GEN3_IRQ_INIT(VLV_, dev_priv->irq_mask, enable_mask);
30938bb61306SVille Syrjälä }
30948bb61306SVille Syrjälä 
30958bb61306SVille Syrjälä /* drm_dma.h hooks
30968bb61306SVille Syrjälä */
30978bb61306SVille Syrjälä static void ironlake_irq_reset(struct drm_device *dev)
30988bb61306SVille Syrjälä {
3099fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
31008bb61306SVille Syrjälä 
3101d420a50cSVille Syrjälä 	if (IS_GEN5(dev_priv))
31028bb61306SVille Syrjälä 		I915_WRITE(HWSTAM, 0xffffffff);
31038bb61306SVille Syrjälä 
31043488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(DE);
31055db94019STvrtko Ursulin 	if (IS_GEN7(dev_priv))
31068bb61306SVille Syrjälä 		I915_WRITE(GEN7_ERR_INT, 0xffffffff);
31078bb61306SVille Syrjälä 
3108b243f530STvrtko Ursulin 	gen5_gt_irq_reset(dev_priv);
31098bb61306SVille Syrjälä 
3110b243f530STvrtko Ursulin 	ibx_irq_reset(dev_priv);
31118bb61306SVille Syrjälä }
31128bb61306SVille Syrjälä 
31136bcdb1c8SVille Syrjälä static void valleyview_irq_reset(struct drm_device *dev)
31147e231dbeSJesse Barnes {
3115fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
31167e231dbeSJesse Barnes 
311734c7b8a7SVille Syrjälä 	I915_WRITE(VLV_MASTER_IER, 0);
311834c7b8a7SVille Syrjälä 	POSTING_READ(VLV_MASTER_IER);
311934c7b8a7SVille Syrjälä 
3120b243f530STvrtko Ursulin 	gen5_gt_irq_reset(dev_priv);
31217e231dbeSJesse Barnes 
3122ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
31239918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
312470591a41SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3125ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
31267e231dbeSJesse Barnes }
31277e231dbeSJesse Barnes 
3128d6e3cca3SDaniel Vetter static void gen8_gt_irq_reset(struct drm_i915_private *dev_priv)
3129d6e3cca3SDaniel Vetter {
3130d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 0);
3131d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 1);
3132d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 2);
3133d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 3);
3134d6e3cca3SDaniel Vetter }
3135d6e3cca3SDaniel Vetter 
3136823f6b38SPaulo Zanoni static void gen8_irq_reset(struct drm_device *dev)
3137abd58f01SBen Widawsky {
3138fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3139abd58f01SBen Widawsky 	int pipe;
3140abd58f01SBen Widawsky 
3141abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
3142abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3143abd58f01SBen Widawsky 
3144d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
3145abd58f01SBen Widawsky 
3146055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3147f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3148813bde43SPaulo Zanoni 						   POWER_DOMAIN_PIPE(pipe)))
3149f86f3fb0SPaulo Zanoni 			GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3150abd58f01SBen Widawsky 
31513488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(GEN8_DE_PORT_);
31523488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(GEN8_DE_MISC_);
31533488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(GEN8_PCU_);
3154abd58f01SBen Widawsky 
31556e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
3156b243f530STvrtko Ursulin 		ibx_irq_reset(dev_priv);
3157abd58f01SBen Widawsky }
3158abd58f01SBen Widawsky 
31594c6c03beSDamien Lespiau void gen8_irq_power_well_post_enable(struct drm_i915_private *dev_priv,
3160001bd2cbSImre Deak 				     u8 pipe_mask)
3161d49bdb0eSPaulo Zanoni {
31621180e206SPaulo Zanoni 	uint32_t extra_ier = GEN8_PIPE_VBLANK | GEN8_PIPE_FIFO_UNDERRUN;
31636831f3e3SVille Syrjälä 	enum pipe pipe;
3164d49bdb0eSPaulo Zanoni 
316513321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3166*9dfe2e3aSImre Deak 
3167*9dfe2e3aSImre Deak 	if (!intel_irqs_enabled(dev_priv)) {
3168*9dfe2e3aSImre Deak 		spin_unlock_irq(&dev_priv->irq_lock);
3169*9dfe2e3aSImre Deak 		return;
3170*9dfe2e3aSImre Deak 	}
3171*9dfe2e3aSImre Deak 
31726831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
31736831f3e3SVille Syrjälä 		GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
31746831f3e3SVille Syrjälä 				  dev_priv->de_irq_mask[pipe],
31756831f3e3SVille Syrjälä 				  ~dev_priv->de_irq_mask[pipe] | extra_ier);
3176*9dfe2e3aSImre Deak 
317713321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3178d49bdb0eSPaulo Zanoni }
3179d49bdb0eSPaulo Zanoni 
3180aae8ba84SVille Syrjälä void gen8_irq_power_well_pre_disable(struct drm_i915_private *dev_priv,
3181001bd2cbSImre Deak 				     u8 pipe_mask)
3182aae8ba84SVille Syrjälä {
31836831f3e3SVille Syrjälä 	enum pipe pipe;
31846831f3e3SVille Syrjälä 
3185aae8ba84SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
3186*9dfe2e3aSImre Deak 
3187*9dfe2e3aSImre Deak 	if (!intel_irqs_enabled(dev_priv)) {
3188*9dfe2e3aSImre Deak 		spin_unlock_irq(&dev_priv->irq_lock);
3189*9dfe2e3aSImre Deak 		return;
3190*9dfe2e3aSImre Deak 	}
3191*9dfe2e3aSImre Deak 
31926831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
31936831f3e3SVille Syrjälä 		GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3194*9dfe2e3aSImre Deak 
3195aae8ba84SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3196aae8ba84SVille Syrjälä 
3197aae8ba84SVille Syrjälä 	/* make sure we're done processing display irqs */
319891c8a326SChris Wilson 	synchronize_irq(dev_priv->drm.irq);
3199aae8ba84SVille Syrjälä }
3200aae8ba84SVille Syrjälä 
32016bcdb1c8SVille Syrjälä static void cherryview_irq_reset(struct drm_device *dev)
320243f328d7SVille Syrjälä {
3203fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
320443f328d7SVille Syrjälä 
320543f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
320643f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
320743f328d7SVille Syrjälä 
3208d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
320943f328d7SVille Syrjälä 
32103488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(GEN8_PCU_);
321143f328d7SVille Syrjälä 
3212ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
32139918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
321470591a41SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3215ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
321643f328d7SVille Syrjälä }
321743f328d7SVille Syrjälä 
321891d14251STvrtko Ursulin static u32 intel_hpd_enabled_irqs(struct drm_i915_private *dev_priv,
321987a02106SVille Syrjälä 				  const u32 hpd[HPD_NUM_PINS])
322087a02106SVille Syrjälä {
322187a02106SVille Syrjälä 	struct intel_encoder *encoder;
322287a02106SVille Syrjälä 	u32 enabled_irqs = 0;
322387a02106SVille Syrjälä 
322491c8a326SChris Wilson 	for_each_intel_encoder(&dev_priv->drm, encoder)
322587a02106SVille Syrjälä 		if (dev_priv->hotplug.stats[encoder->hpd_pin].state == HPD_ENABLED)
322687a02106SVille Syrjälä 			enabled_irqs |= hpd[encoder->hpd_pin];
322787a02106SVille Syrjälä 
322887a02106SVille Syrjälä 	return enabled_irqs;
322987a02106SVille Syrjälä }
323087a02106SVille Syrjälä 
32311a56b1a2SImre Deak static void ibx_hpd_detection_setup(struct drm_i915_private *dev_priv)
32321a56b1a2SImre Deak {
32331a56b1a2SImre Deak 	u32 hotplug;
32341a56b1a2SImre Deak 
32351a56b1a2SImre Deak 	/*
32361a56b1a2SImre Deak 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
32371a56b1a2SImre Deak 	 * duration to 2ms (which is the minimum in the Display Port spec).
32381a56b1a2SImre Deak 	 * The pulse duration bits are reserved on LPT+.
32391a56b1a2SImre Deak 	 */
32401a56b1a2SImre Deak 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
32411a56b1a2SImre Deak 	hotplug &= ~(PORTB_PULSE_DURATION_MASK |
32421a56b1a2SImre Deak 		     PORTC_PULSE_DURATION_MASK |
32431a56b1a2SImre Deak 		     PORTD_PULSE_DURATION_MASK);
32441a56b1a2SImre Deak 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
32451a56b1a2SImre Deak 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
32461a56b1a2SImre Deak 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
32471a56b1a2SImre Deak 	/*
32481a56b1a2SImre Deak 	 * When CPU and PCH are on the same package, port A
32491a56b1a2SImre Deak 	 * HPD must be enabled in both north and south.
32501a56b1a2SImre Deak 	 */
32511a56b1a2SImre Deak 	if (HAS_PCH_LPT_LP(dev_priv))
32521a56b1a2SImre Deak 		hotplug |= PORTA_HOTPLUG_ENABLE;
32531a56b1a2SImre Deak 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
32541a56b1a2SImre Deak }
32551a56b1a2SImre Deak 
325691d14251STvrtko Ursulin static void ibx_hpd_irq_setup(struct drm_i915_private *dev_priv)
325782a28bcfSDaniel Vetter {
32581a56b1a2SImre Deak 	u32 hotplug_irqs, enabled_irqs;
325982a28bcfSDaniel Vetter 
326091d14251STvrtko Ursulin 	if (HAS_PCH_IBX(dev_priv)) {
3261fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
326291d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ibx);
326382a28bcfSDaniel Vetter 	} else {
3264fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
326591d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_cpt);
326682a28bcfSDaniel Vetter 	}
326782a28bcfSDaniel Vetter 
3268fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
326982a28bcfSDaniel Vetter 
32701a56b1a2SImre Deak 	ibx_hpd_detection_setup(dev_priv);
32716dbf30ceSVille Syrjälä }
327226951cafSXiong Zhang 
32732a57d9ccSImre Deak static void spt_hpd_detection_setup(struct drm_i915_private *dev_priv)
32742a57d9ccSImre Deak {
32753b92e263SRodrigo Vivi 	u32 val, hotplug;
32763b92e263SRodrigo Vivi 
32773b92e263SRodrigo Vivi 	/* Display WA #1179 WaHardHangonHotPlug: cnp */
32783b92e263SRodrigo Vivi 	if (HAS_PCH_CNP(dev_priv)) {
32793b92e263SRodrigo Vivi 		val = I915_READ(SOUTH_CHICKEN1);
32803b92e263SRodrigo Vivi 		val &= ~CHASSIS_CLK_REQ_DURATION_MASK;
32813b92e263SRodrigo Vivi 		val |= CHASSIS_CLK_REQ_DURATION(0xf);
32823b92e263SRodrigo Vivi 		I915_WRITE(SOUTH_CHICKEN1, val);
32833b92e263SRodrigo Vivi 	}
32842a57d9ccSImre Deak 
32852a57d9ccSImre Deak 	/* Enable digital hotplug on the PCH */
32862a57d9ccSImre Deak 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
32872a57d9ccSImre Deak 	hotplug |= PORTA_HOTPLUG_ENABLE |
32882a57d9ccSImre Deak 		   PORTB_HOTPLUG_ENABLE |
32892a57d9ccSImre Deak 		   PORTC_HOTPLUG_ENABLE |
32902a57d9ccSImre Deak 		   PORTD_HOTPLUG_ENABLE;
32912a57d9ccSImre Deak 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
32922a57d9ccSImre Deak 
32932a57d9ccSImre Deak 	hotplug = I915_READ(PCH_PORT_HOTPLUG2);
32942a57d9ccSImre Deak 	hotplug |= PORTE_HOTPLUG_ENABLE;
32952a57d9ccSImre Deak 	I915_WRITE(PCH_PORT_HOTPLUG2, hotplug);
32962a57d9ccSImre Deak }
32972a57d9ccSImre Deak 
329891d14251STvrtko Ursulin static void spt_hpd_irq_setup(struct drm_i915_private *dev_priv)
32996dbf30ceSVille Syrjälä {
33002a57d9ccSImre Deak 	u32 hotplug_irqs, enabled_irqs;
33016dbf30ceSVille Syrjälä 
33026dbf30ceSVille Syrjälä 	hotplug_irqs = SDE_HOTPLUG_MASK_SPT;
330391d14251STvrtko Ursulin 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_spt);
33046dbf30ceSVille Syrjälä 
33056dbf30ceSVille Syrjälä 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
33066dbf30ceSVille Syrjälä 
33072a57d9ccSImre Deak 	spt_hpd_detection_setup(dev_priv);
330826951cafSXiong Zhang }
33097fe0b973SKeith Packard 
33101a56b1a2SImre Deak static void ilk_hpd_detection_setup(struct drm_i915_private *dev_priv)
33111a56b1a2SImre Deak {
33121a56b1a2SImre Deak 	u32 hotplug;
33131a56b1a2SImre Deak 
33141a56b1a2SImre Deak 	/*
33151a56b1a2SImre Deak 	 * Enable digital hotplug on the CPU, and configure the DP short pulse
33161a56b1a2SImre Deak 	 * duration to 2ms (which is the minimum in the Display Port spec)
33171a56b1a2SImre Deak 	 * The pulse duration bits are reserved on HSW+.
33181a56b1a2SImre Deak 	 */
33191a56b1a2SImre Deak 	hotplug = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
33201a56b1a2SImre Deak 	hotplug &= ~DIGITAL_PORTA_PULSE_DURATION_MASK;
33211a56b1a2SImre Deak 	hotplug |= DIGITAL_PORTA_HOTPLUG_ENABLE |
33221a56b1a2SImre Deak 		   DIGITAL_PORTA_PULSE_DURATION_2ms;
33231a56b1a2SImre Deak 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, hotplug);
33241a56b1a2SImre Deak }
33251a56b1a2SImre Deak 
332691d14251STvrtko Ursulin static void ilk_hpd_irq_setup(struct drm_i915_private *dev_priv)
3327e4ce95aaSVille Syrjälä {
33281a56b1a2SImre Deak 	u32 hotplug_irqs, enabled_irqs;
3329e4ce95aaSVille Syrjälä 
333091d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 8) {
33313a3b3c7dSVille Syrjälä 		hotplug_irqs = GEN8_PORT_DP_A_HOTPLUG;
333291d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_bdw);
33333a3b3c7dSVille Syrjälä 
33343a3b3c7dSVille Syrjälä 		bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
333591d14251STvrtko Ursulin 	} else if (INTEL_GEN(dev_priv) >= 7) {
333623bb4cb5SVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG_IVB;
333791d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ivb);
33383a3b3c7dSVille Syrjälä 
33393a3b3c7dSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
334023bb4cb5SVille Syrjälä 	} else {
3341e4ce95aaSVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG;
334291d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ilk);
3343e4ce95aaSVille Syrjälä 
3344e4ce95aaSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
33453a3b3c7dSVille Syrjälä 	}
3346e4ce95aaSVille Syrjälä 
33471a56b1a2SImre Deak 	ilk_hpd_detection_setup(dev_priv);
3348e4ce95aaSVille Syrjälä 
334991d14251STvrtko Ursulin 	ibx_hpd_irq_setup(dev_priv);
3350e4ce95aaSVille Syrjälä }
3351e4ce95aaSVille Syrjälä 
33522a57d9ccSImre Deak static void __bxt_hpd_detection_setup(struct drm_i915_private *dev_priv,
33532a57d9ccSImre Deak 				      u32 enabled_irqs)
3354e0a20ad7SShashank Sharma {
33552a57d9ccSImre Deak 	u32 hotplug;
3356e0a20ad7SShashank Sharma 
3357a52bb15bSVille Syrjälä 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
33582a57d9ccSImre Deak 	hotplug |= PORTA_HOTPLUG_ENABLE |
33592a57d9ccSImre Deak 		   PORTB_HOTPLUG_ENABLE |
33602a57d9ccSImre Deak 		   PORTC_HOTPLUG_ENABLE;
3361d252bf68SShubhangi Shrivastava 
3362d252bf68SShubhangi Shrivastava 	DRM_DEBUG_KMS("Invert bit setting: hp_ctl:%x hp_port:%x\n",
3363d252bf68SShubhangi Shrivastava 		      hotplug, enabled_irqs);
3364d252bf68SShubhangi Shrivastava 	hotplug &= ~BXT_DDI_HPD_INVERT_MASK;
3365d252bf68SShubhangi Shrivastava 
3366d252bf68SShubhangi Shrivastava 	/*
3367d252bf68SShubhangi Shrivastava 	 * For BXT invert bit has to be set based on AOB design
3368d252bf68SShubhangi Shrivastava 	 * for HPD detection logic, update it based on VBT fields.
3369d252bf68SShubhangi Shrivastava 	 */
3370d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIA) &&
3371d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_A))
3372d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIA_HPD_INVERT;
3373d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIB) &&
3374d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_B))
3375d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIB_HPD_INVERT;
3376d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIC) &&
3377d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_C))
3378d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIC_HPD_INVERT;
3379d252bf68SShubhangi Shrivastava 
3380a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
3381e0a20ad7SShashank Sharma }
3382e0a20ad7SShashank Sharma 
33832a57d9ccSImre Deak static void bxt_hpd_detection_setup(struct drm_i915_private *dev_priv)
33842a57d9ccSImre Deak {
33852a57d9ccSImre Deak 	__bxt_hpd_detection_setup(dev_priv, BXT_DE_PORT_HOTPLUG_MASK);
33862a57d9ccSImre Deak }
33872a57d9ccSImre Deak 
33882a57d9ccSImre Deak static void bxt_hpd_irq_setup(struct drm_i915_private *dev_priv)
33892a57d9ccSImre Deak {
33902a57d9ccSImre Deak 	u32 hotplug_irqs, enabled_irqs;
33912a57d9ccSImre Deak 
33922a57d9ccSImre Deak 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_bxt);
33932a57d9ccSImre Deak 	hotplug_irqs = BXT_DE_PORT_HOTPLUG_MASK;
33942a57d9ccSImre Deak 
33952a57d9ccSImre Deak 	bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
33962a57d9ccSImre Deak 
33972a57d9ccSImre Deak 	__bxt_hpd_detection_setup(dev_priv, enabled_irqs);
33982a57d9ccSImre Deak }
33992a57d9ccSImre Deak 
3400d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
3401d46da437SPaulo Zanoni {
3402fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
340382a28bcfSDaniel Vetter 	u32 mask;
3404d46da437SPaulo Zanoni 
34056e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
3406692a04cfSDaniel Vetter 		return;
3407692a04cfSDaniel Vetter 
34086e266956STvrtko Ursulin 	if (HAS_PCH_IBX(dev_priv))
34095c673b60SDaniel Vetter 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
34104ebc6509SDhinakaran Pandiyan 	else if (HAS_PCH_CPT(dev_priv) || HAS_PCH_LPT(dev_priv))
34115c673b60SDaniel Vetter 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
34124ebc6509SDhinakaran Pandiyan 	else
34134ebc6509SDhinakaran Pandiyan 		mask = SDE_GMBUS_CPT;
34148664281bSPaulo Zanoni 
34153488d4ebSVille Syrjälä 	gen3_assert_iir_is_zero(dev_priv, SDEIIR);
3416d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
34172a57d9ccSImre Deak 
34182a57d9ccSImre Deak 	if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
34192a57d9ccSImre Deak 	    HAS_PCH_LPT(dev_priv))
34201a56b1a2SImre Deak 		ibx_hpd_detection_setup(dev_priv);
34212a57d9ccSImre Deak 	else
34222a57d9ccSImre Deak 		spt_hpd_detection_setup(dev_priv);
3423d46da437SPaulo Zanoni }
3424d46da437SPaulo Zanoni 
34250a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
34260a9a8c91SDaniel Vetter {
3427fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
34280a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
34290a9a8c91SDaniel Vetter 
34300a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
34310a9a8c91SDaniel Vetter 
34320a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
34333c9192bcSTvrtko Ursulin 	if (HAS_L3_DPF(dev_priv)) {
34340a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
3435772c2a51STvrtko Ursulin 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev_priv);
3436772c2a51STvrtko Ursulin 		gt_irqs |= GT_PARITY_ERROR(dev_priv);
34370a9a8c91SDaniel Vetter 	}
34380a9a8c91SDaniel Vetter 
34390a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
34405db94019STvrtko Ursulin 	if (IS_GEN5(dev_priv)) {
3441f8973c21SChris Wilson 		gt_irqs |= ILK_BSD_USER_INTERRUPT;
34420a9a8c91SDaniel Vetter 	} else {
34430a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
34440a9a8c91SDaniel Vetter 	}
34450a9a8c91SDaniel Vetter 
34463488d4ebSVille Syrjälä 	GEN3_IRQ_INIT(GT, dev_priv->gt_irq_mask, gt_irqs);
34470a9a8c91SDaniel Vetter 
3448b243f530STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 6) {
344978e68d36SImre Deak 		/*
345078e68d36SImre Deak 		 * RPS interrupts will get enabled/disabled on demand when RPS
345178e68d36SImre Deak 		 * itself is enabled/disabled.
345278e68d36SImre Deak 		 */
3453f4e9af4fSAkash Goel 		if (HAS_VEBOX(dev_priv)) {
34540a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
3455f4e9af4fSAkash Goel 			dev_priv->pm_ier |= PM_VEBOX_USER_INTERRUPT;
3456f4e9af4fSAkash Goel 		}
34570a9a8c91SDaniel Vetter 
3458f4e9af4fSAkash Goel 		dev_priv->pm_imr = 0xffffffff;
34593488d4ebSVille Syrjälä 		GEN3_IRQ_INIT(GEN6_PM, dev_priv->pm_imr, pm_irqs);
34600a9a8c91SDaniel Vetter 	}
34610a9a8c91SDaniel Vetter }
34620a9a8c91SDaniel Vetter 
3463f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
3464036a4a7dSZhenyu Wang {
3465fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
34668e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
34678e76f8dcSPaulo Zanoni 
3468b243f530STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 7) {
34698e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
3470842ebf7aSVille Syrjälä 				DE_PCH_EVENT_IVB | DE_AUX_CHANNEL_A_IVB);
34718e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
347223bb4cb5SVille Syrjälä 			      DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB |
347323bb4cb5SVille Syrjälä 			      DE_DP_A_HOTPLUG_IVB);
34748e76f8dcSPaulo Zanoni 	} else {
34758e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3476842ebf7aSVille Syrjälä 				DE_AUX_CHANNEL_A | DE_PIPEB_CRC_DONE |
3477842ebf7aSVille Syrjälä 				DE_PIPEA_CRC_DONE | DE_POISON);
3478e4ce95aaSVille Syrjälä 		extra_mask = (DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
3479e4ce95aaSVille Syrjälä 			      DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN |
3480e4ce95aaSVille Syrjälä 			      DE_DP_A_HOTPLUG);
34818e76f8dcSPaulo Zanoni 	}
3482036a4a7dSZhenyu Wang 
34831ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3484036a4a7dSZhenyu Wang 
3485622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3486622364b6SPaulo Zanoni 
34873488d4ebSVille Syrjälä 	GEN3_IRQ_INIT(DE, dev_priv->irq_mask, display_mask | extra_mask);
3488036a4a7dSZhenyu Wang 
34890a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
3490036a4a7dSZhenyu Wang 
34911a56b1a2SImre Deak 	ilk_hpd_detection_setup(dev_priv);
34921a56b1a2SImre Deak 
3493d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
34947fe0b973SKeith Packard 
349550a0bc90STvrtko Ursulin 	if (IS_IRONLAKE_M(dev_priv)) {
34966005ce42SDaniel Vetter 		/* Enable PCU event interrupts
34976005ce42SDaniel Vetter 		 *
34986005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
34994bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
35004bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
3501d6207435SDaniel Vetter 		spin_lock_irq(&dev_priv->irq_lock);
3502fbdedaeaSVille Syrjälä 		ilk_enable_display_irq(dev_priv, DE_PCU_EVENT);
3503d6207435SDaniel Vetter 		spin_unlock_irq(&dev_priv->irq_lock);
3504f97108d1SJesse Barnes 	}
3505f97108d1SJesse Barnes 
3506036a4a7dSZhenyu Wang 	return 0;
3507036a4a7dSZhenyu Wang }
3508036a4a7dSZhenyu Wang 
3509f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3510f8b79e58SImre Deak {
351167520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
3512f8b79e58SImre Deak 
3513f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3514f8b79e58SImre Deak 		return;
3515f8b79e58SImre Deak 
3516f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3517f8b79e58SImre Deak 
3518d6c69803SVille Syrjälä 	if (intel_irqs_enabled(dev_priv)) {
3519d6c69803SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3520ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3521f8b79e58SImre Deak 	}
3522d6c69803SVille Syrjälä }
3523f8b79e58SImre Deak 
3524f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3525f8b79e58SImre Deak {
352667520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
3527f8b79e58SImre Deak 
3528f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3529f8b79e58SImre Deak 		return;
3530f8b79e58SImre Deak 
3531f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3532f8b79e58SImre Deak 
3533950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3534ad22d106SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3535f8b79e58SImre Deak }
3536f8b79e58SImre Deak 
35370e6c9a9eSVille Syrjälä 
35380e6c9a9eSVille Syrjälä static int valleyview_irq_postinstall(struct drm_device *dev)
35390e6c9a9eSVille Syrjälä {
3540fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
35410e6c9a9eSVille Syrjälä 
35420a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
35437e231dbeSJesse Barnes 
3544ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
35459918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3546ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3547ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3548ad22d106SVille Syrjälä 
35497e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
355034c7b8a7SVille Syrjälä 	POSTING_READ(VLV_MASTER_IER);
355120afbda2SDaniel Vetter 
355220afbda2SDaniel Vetter 	return 0;
355320afbda2SDaniel Vetter }
355420afbda2SDaniel Vetter 
3555abd58f01SBen Widawsky static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
3556abd58f01SBen Widawsky {
3557abd58f01SBen Widawsky 	/* These are interrupts we'll toggle with the ring mask register */
3558abd58f01SBen Widawsky 	uint32_t gt_interrupts[] = {
3559abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
356073d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
356173d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT |
356273d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT,
3563abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
356473d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
356573d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT |
356673d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT,
3567abd58f01SBen Widawsky 		0,
356873d477f6SOscar Mateo 		GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT |
356973d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT
3570abd58f01SBen Widawsky 		};
3571abd58f01SBen Widawsky 
357298735739STvrtko Ursulin 	if (HAS_L3_DPF(dev_priv))
357398735739STvrtko Ursulin 		gt_interrupts[0] |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
357498735739STvrtko Ursulin 
3575f4e9af4fSAkash Goel 	dev_priv->pm_ier = 0x0;
3576f4e9af4fSAkash Goel 	dev_priv->pm_imr = ~dev_priv->pm_ier;
35779a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 0, ~gt_interrupts[0], gt_interrupts[0]);
35789a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 1, ~gt_interrupts[1], gt_interrupts[1]);
357978e68d36SImre Deak 	/*
358078e68d36SImre Deak 	 * RPS interrupts will get enabled/disabled on demand when RPS itself
358126705e20SSagar Arun Kamble 	 * is enabled/disabled. Same wil be the case for GuC interrupts.
358278e68d36SImre Deak 	 */
3583f4e9af4fSAkash Goel 	GEN8_IRQ_INIT_NDX(GT, 2, dev_priv->pm_imr, dev_priv->pm_ier);
35849a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 3, ~gt_interrupts[3], gt_interrupts[3]);
3585abd58f01SBen Widawsky }
3586abd58f01SBen Widawsky 
3587abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3588abd58f01SBen Widawsky {
3589770de83dSDamien Lespiau 	uint32_t de_pipe_masked = GEN8_PIPE_CDCLK_CRC_DONE;
3590770de83dSDamien Lespiau 	uint32_t de_pipe_enables;
35913a3b3c7dSVille Syrjälä 	u32 de_port_masked = GEN8_AUX_CHANNEL_A;
35923a3b3c7dSVille Syrjälä 	u32 de_port_enables;
359311825b0dSVille Syrjälä 	u32 de_misc_masked = GEN8_DE_MISC_GSE;
35943a3b3c7dSVille Syrjälä 	enum pipe pipe;
3595770de83dSDamien Lespiau 
3596bca2bf2aSPandiyan, Dhinakaran 	if (INTEL_GEN(dev_priv) >= 9) {
3597842ebf7aSVille Syrjälä 		de_pipe_masked |= GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
35983a3b3c7dSVille Syrjälä 		de_port_masked |= GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
359988e04703SJesse Barnes 				  GEN9_AUX_CHANNEL_D;
3600cc3f90f0SAnder Conselvan de Oliveira 		if (IS_GEN9_LP(dev_priv))
36013a3b3c7dSVille Syrjälä 			de_port_masked |= BXT_DE_PORT_GMBUS;
36023a3b3c7dSVille Syrjälä 	} else {
3603842ebf7aSVille Syrjälä 		de_pipe_masked |= GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
36043a3b3c7dSVille Syrjälä 	}
3605770de83dSDamien Lespiau 
3606770de83dSDamien Lespiau 	de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
3607770de83dSDamien Lespiau 					   GEN8_PIPE_FIFO_UNDERRUN;
3608770de83dSDamien Lespiau 
36093a3b3c7dSVille Syrjälä 	de_port_enables = de_port_masked;
3610cc3f90f0SAnder Conselvan de Oliveira 	if (IS_GEN9_LP(dev_priv))
3611a52bb15bSVille Syrjälä 		de_port_enables |= BXT_DE_PORT_HOTPLUG_MASK;
3612a52bb15bSVille Syrjälä 	else if (IS_BROADWELL(dev_priv))
36133a3b3c7dSVille Syrjälä 		de_port_enables |= GEN8_PORT_DP_A_HOTPLUG;
36143a3b3c7dSVille Syrjälä 
361513b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_A] = ~de_pipe_masked;
361613b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_B] = ~de_pipe_masked;
361713b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_C] = ~de_pipe_masked;
3618abd58f01SBen Widawsky 
3619055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3620f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3621813bde43SPaulo Zanoni 				POWER_DOMAIN_PIPE(pipe)))
3622813bde43SPaulo Zanoni 			GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
3623813bde43SPaulo Zanoni 					  dev_priv->de_irq_mask[pipe],
362435079899SPaulo Zanoni 					  de_pipe_enables);
3625abd58f01SBen Widawsky 
36263488d4ebSVille Syrjälä 	GEN3_IRQ_INIT(GEN8_DE_PORT_, ~de_port_masked, de_port_enables);
36273488d4ebSVille Syrjälä 	GEN3_IRQ_INIT(GEN8_DE_MISC_, ~de_misc_masked, de_misc_masked);
36282a57d9ccSImre Deak 
36292a57d9ccSImre Deak 	if (IS_GEN9_LP(dev_priv))
36302a57d9ccSImre Deak 		bxt_hpd_detection_setup(dev_priv);
36311a56b1a2SImre Deak 	else if (IS_BROADWELL(dev_priv))
36321a56b1a2SImre Deak 		ilk_hpd_detection_setup(dev_priv);
3633abd58f01SBen Widawsky }
3634abd58f01SBen Widawsky 
3635abd58f01SBen Widawsky static int gen8_irq_postinstall(struct drm_device *dev)
3636abd58f01SBen Widawsky {
3637fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3638abd58f01SBen Widawsky 
36396e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
3640622364b6SPaulo Zanoni 		ibx_irq_pre_postinstall(dev);
3641622364b6SPaulo Zanoni 
3642abd58f01SBen Widawsky 	gen8_gt_irq_postinstall(dev_priv);
3643abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3644abd58f01SBen Widawsky 
36456e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
3646abd58f01SBen Widawsky 		ibx_irq_postinstall(dev);
3647abd58f01SBen Widawsky 
3648e5328c43SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
3649abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3650abd58f01SBen Widawsky 
3651abd58f01SBen Widawsky 	return 0;
3652abd58f01SBen Widawsky }
3653abd58f01SBen Widawsky 
365443f328d7SVille Syrjälä static int cherryview_irq_postinstall(struct drm_device *dev)
365543f328d7SVille Syrjälä {
3656fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
365743f328d7SVille Syrjälä 
365843f328d7SVille Syrjälä 	gen8_gt_irq_postinstall(dev_priv);
365943f328d7SVille Syrjälä 
3660ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
36619918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3662ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3663ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3664ad22d106SVille Syrjälä 
3665e5328c43SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
366643f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
366743f328d7SVille Syrjälä 
366843f328d7SVille Syrjälä 	return 0;
366943f328d7SVille Syrjälä }
367043f328d7SVille Syrjälä 
36716bcdb1c8SVille Syrjälä static void i8xx_irq_reset(struct drm_device *dev)
3672c2798b19SChris Wilson {
3673fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3674c2798b19SChris Wilson 
367544d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
367644d9241eSVille Syrjälä 
3677d420a50cSVille Syrjälä 	I915_WRITE16(HWSTAM, 0xffff);
3678d420a50cSVille Syrjälä 
3679e9e9848aSVille Syrjälä 	GEN2_IRQ_RESET();
3680c2798b19SChris Wilson }
3681c2798b19SChris Wilson 
3682c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
3683c2798b19SChris Wilson {
3684fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3685e9e9848aSVille Syrjälä 	u16 enable_mask;
3686c2798b19SChris Wilson 
3687045cebd2SVille Syrjälä 	I915_WRITE16(EMR, ~(I915_ERROR_PAGE_TABLE |
3688045cebd2SVille Syrjälä 			    I915_ERROR_MEMORY_REFRESH));
3689c2798b19SChris Wilson 
3690c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
3691c2798b19SChris Wilson 	dev_priv->irq_mask =
3692c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3693842ebf7aSVille Syrjälä 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT);
3694c2798b19SChris Wilson 
3695e9e9848aSVille Syrjälä 	enable_mask =
3696c2798b19SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3697c2798b19SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3698e9e9848aSVille Syrjälä 		I915_USER_INTERRUPT;
3699e9e9848aSVille Syrjälä 
3700e9e9848aSVille Syrjälä 	GEN2_IRQ_INIT(, dev_priv->irq_mask, enable_mask);
3701c2798b19SChris Wilson 
3702379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3703379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3704d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3705755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3706755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3707d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3708379ef82dSDaniel Vetter 
3709c2798b19SChris Wilson 	return 0;
3710c2798b19SChris Wilson }
3711c2798b19SChris Wilson 
3712ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3713c2798b19SChris Wilson {
371445a83f84SDaniel Vetter 	struct drm_device *dev = arg;
3715fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3716af722d28SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
3717c2798b19SChris Wilson 
37182dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
37192dd2a883SImre Deak 		return IRQ_NONE;
37202dd2a883SImre Deak 
37211f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
37221f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
37231f814dacSImre Deak 
3724af722d28SVille Syrjälä 	do {
3725af722d28SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
3726af722d28SVille Syrjälä 		u16 iir;
3727af722d28SVille Syrjälä 
3728c2798b19SChris Wilson 		iir = I915_READ16(IIR);
3729c2798b19SChris Wilson 		if (iir == 0)
3730af722d28SVille Syrjälä 			break;
3731c2798b19SChris Wilson 
3732af722d28SVille Syrjälä 		ret = IRQ_HANDLED;
3733c2798b19SChris Wilson 
3734eb64343cSVille Syrjälä 		/* Call regardless, as some status bits might not be
3735eb64343cSVille Syrjälä 		 * signalled in iir */
3736eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
3737c2798b19SChris Wilson 
3738fd3a4024SDaniel Vetter 		I915_WRITE16(IIR, iir);
3739c2798b19SChris Wilson 
3740c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
37413b3f1650SAkash Goel 			notify_ring(dev_priv->engine[RCS]);
3742c2798b19SChris Wilson 
3743af722d28SVille Syrjälä 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3744af722d28SVille Syrjälä 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
3745af722d28SVille Syrjälä 
3746eb64343cSVille Syrjälä 		i8xx_pipestat_irq_handler(dev_priv, iir, pipe_stats);
3747af722d28SVille Syrjälä 	} while (0);
3748c2798b19SChris Wilson 
37491f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
37501f814dacSImre Deak 
37511f814dacSImre Deak 	return ret;
3752c2798b19SChris Wilson }
3753c2798b19SChris Wilson 
37546bcdb1c8SVille Syrjälä static void i915_irq_reset(struct drm_device *dev)
3755a266c7d5SChris Wilson {
3756fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3757a266c7d5SChris Wilson 
375856b857a5STvrtko Ursulin 	if (I915_HAS_HOTPLUG(dev_priv)) {
37590706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
3760a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3761a266c7d5SChris Wilson 	}
3762a266c7d5SChris Wilson 
376344d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
376444d9241eSVille Syrjälä 
3765d420a50cSVille Syrjälä 	I915_WRITE(HWSTAM, 0xffffffff);
376644d9241eSVille Syrjälä 
3767ba7eb789SVille Syrjälä 	GEN3_IRQ_RESET();
3768a266c7d5SChris Wilson }
3769a266c7d5SChris Wilson 
3770a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
3771a266c7d5SChris Wilson {
3772fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
377338bde180SChris Wilson 	u32 enable_mask;
3774a266c7d5SChris Wilson 
3775045cebd2SVille Syrjälä 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE |
3776045cebd2SVille Syrjälä 			  I915_ERROR_MEMORY_REFRESH));
377738bde180SChris Wilson 
377838bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
377938bde180SChris Wilson 	dev_priv->irq_mask =
378038bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
378138bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3782842ebf7aSVille Syrjälä 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT);
378338bde180SChris Wilson 
378438bde180SChris Wilson 	enable_mask =
378538bde180SChris Wilson 		I915_ASLE_INTERRUPT |
378638bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
378738bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
378838bde180SChris Wilson 		I915_USER_INTERRUPT;
378938bde180SChris Wilson 
379056b857a5STvrtko Ursulin 	if (I915_HAS_HOTPLUG(dev_priv)) {
3791a266c7d5SChris Wilson 		/* Enable in IER... */
3792a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
3793a266c7d5SChris Wilson 		/* and unmask in IMR */
3794a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
3795a266c7d5SChris Wilson 	}
3796a266c7d5SChris Wilson 
3797ba7eb789SVille Syrjälä 	GEN3_IRQ_INIT(, dev_priv->irq_mask, enable_mask);
3798a266c7d5SChris Wilson 
3799379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3800379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3801d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3802755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3803755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3804d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3805379ef82dSDaniel Vetter 
3806c30bb1fdSVille Syrjälä 	i915_enable_asle_pipestat(dev_priv);
3807c30bb1fdSVille Syrjälä 
380820afbda2SDaniel Vetter 	return 0;
380920afbda2SDaniel Vetter }
381020afbda2SDaniel Vetter 
3811ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
3812a266c7d5SChris Wilson {
381345a83f84SDaniel Vetter 	struct drm_device *dev = arg;
3814fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3815af722d28SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
3816a266c7d5SChris Wilson 
38172dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
38182dd2a883SImre Deak 		return IRQ_NONE;
38192dd2a883SImre Deak 
38201f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
38211f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
38221f814dacSImre Deak 
382338bde180SChris Wilson 	do {
3824eb64343cSVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
3825af722d28SVille Syrjälä 		u32 hotplug_status = 0;
3826af722d28SVille Syrjälä 		u32 iir;
3827a266c7d5SChris Wilson 
3828af722d28SVille Syrjälä 		iir = I915_READ(IIR);
3829af722d28SVille Syrjälä 		if (iir == 0)
3830af722d28SVille Syrjälä 			break;
3831af722d28SVille Syrjälä 
3832af722d28SVille Syrjälä 		ret = IRQ_HANDLED;
3833af722d28SVille Syrjälä 
3834af722d28SVille Syrjälä 		if (I915_HAS_HOTPLUG(dev_priv) &&
3835af722d28SVille Syrjälä 		    iir & I915_DISPLAY_PORT_INTERRUPT)
3836af722d28SVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
3837a266c7d5SChris Wilson 
3838eb64343cSVille Syrjälä 		/* Call regardless, as some status bits might not be
3839eb64343cSVille Syrjälä 		 * signalled in iir */
3840eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
3841a266c7d5SChris Wilson 
3842fd3a4024SDaniel Vetter 		I915_WRITE(IIR, iir);
3843a266c7d5SChris Wilson 
3844a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
38453b3f1650SAkash Goel 			notify_ring(dev_priv->engine[RCS]);
3846a266c7d5SChris Wilson 
3847af722d28SVille Syrjälä 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3848af722d28SVille Syrjälä 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
3849a266c7d5SChris Wilson 
3850af722d28SVille Syrjälä 		if (hotplug_status)
3851af722d28SVille Syrjälä 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
3852af722d28SVille Syrjälä 
3853af722d28SVille Syrjälä 		i915_pipestat_irq_handler(dev_priv, iir, pipe_stats);
3854af722d28SVille Syrjälä 	} while (0);
3855a266c7d5SChris Wilson 
38561f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
38571f814dacSImre Deak 
3858a266c7d5SChris Wilson 	return ret;
3859a266c7d5SChris Wilson }
3860a266c7d5SChris Wilson 
38616bcdb1c8SVille Syrjälä static void i965_irq_reset(struct drm_device *dev)
3862a266c7d5SChris Wilson {
3863fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3864a266c7d5SChris Wilson 
38650706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
3866a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3867a266c7d5SChris Wilson 
386844d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
386944d9241eSVille Syrjälä 
3870d420a50cSVille Syrjälä 	I915_WRITE(HWSTAM, 0xffffffff);
387144d9241eSVille Syrjälä 
3872ba7eb789SVille Syrjälä 	GEN3_IRQ_RESET();
3873a266c7d5SChris Wilson }
3874a266c7d5SChris Wilson 
3875a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
3876a266c7d5SChris Wilson {
3877fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3878bbba0a97SChris Wilson 	u32 enable_mask;
3879a266c7d5SChris Wilson 	u32 error_mask;
3880a266c7d5SChris Wilson 
3881045cebd2SVille Syrjälä 	/*
3882045cebd2SVille Syrjälä 	 * Enable some error detection, note the instruction error mask
3883045cebd2SVille Syrjälä 	 * bit is reserved, so we leave it masked.
3884045cebd2SVille Syrjälä 	 */
3885045cebd2SVille Syrjälä 	if (IS_G4X(dev_priv)) {
3886045cebd2SVille Syrjälä 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
3887045cebd2SVille Syrjälä 			       GM45_ERROR_MEM_PRIV |
3888045cebd2SVille Syrjälä 			       GM45_ERROR_CP_PRIV |
3889045cebd2SVille Syrjälä 			       I915_ERROR_MEMORY_REFRESH);
3890045cebd2SVille Syrjälä 	} else {
3891045cebd2SVille Syrjälä 		error_mask = ~(I915_ERROR_PAGE_TABLE |
3892045cebd2SVille Syrjälä 			       I915_ERROR_MEMORY_REFRESH);
3893045cebd2SVille Syrjälä 	}
3894045cebd2SVille Syrjälä 	I915_WRITE(EMR, error_mask);
3895045cebd2SVille Syrjälä 
3896a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
3897c30bb1fdSVille Syrjälä 	dev_priv->irq_mask =
3898c30bb1fdSVille Syrjälä 		~(I915_ASLE_INTERRUPT |
3899adca4730SChris Wilson 		  I915_DISPLAY_PORT_INTERRUPT |
3900bbba0a97SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3901bbba0a97SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3902bbba0a97SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3903bbba0a97SChris Wilson 
3904c30bb1fdSVille Syrjälä 	enable_mask =
3905c30bb1fdSVille Syrjälä 		I915_ASLE_INTERRUPT |
3906c30bb1fdSVille Syrjälä 		I915_DISPLAY_PORT_INTERRUPT |
3907c30bb1fdSVille Syrjälä 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3908c30bb1fdSVille Syrjälä 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3909c30bb1fdSVille Syrjälä 		I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
3910c30bb1fdSVille Syrjälä 		I915_USER_INTERRUPT;
3911bbba0a97SChris Wilson 
391291d14251STvrtko Ursulin 	if (IS_G4X(dev_priv))
3913bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
3914a266c7d5SChris Wilson 
3915c30bb1fdSVille Syrjälä 	GEN3_IRQ_INIT(, dev_priv->irq_mask, enable_mask);
3916c30bb1fdSVille Syrjälä 
3917b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3918b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3919d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3920755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3921755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3922755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3923d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3924a266c7d5SChris Wilson 
392591d14251STvrtko Ursulin 	i915_enable_asle_pipestat(dev_priv);
392620afbda2SDaniel Vetter 
392720afbda2SDaniel Vetter 	return 0;
392820afbda2SDaniel Vetter }
392920afbda2SDaniel Vetter 
393091d14251STvrtko Ursulin static void i915_hpd_irq_setup(struct drm_i915_private *dev_priv)
393120afbda2SDaniel Vetter {
393220afbda2SDaniel Vetter 	u32 hotplug_en;
393320afbda2SDaniel Vetter 
393467520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
3935b5ea2d56SDaniel Vetter 
3936adca4730SChris Wilson 	/* Note HDMI and DP share hotplug bits */
3937e5868a31SEgbert Eich 	/* enable bits are the same for all generations */
393891d14251STvrtko Ursulin 	hotplug_en = intel_hpd_enabled_irqs(dev_priv, hpd_mask_i915);
3939a266c7d5SChris Wilson 	/* Programming the CRT detection parameters tends
3940a266c7d5SChris Wilson 	   to generate a spurious hotplug event about three
3941a266c7d5SChris Wilson 	   seconds later.  So just do it once.
3942a266c7d5SChris Wilson 	*/
394391d14251STvrtko Ursulin 	if (IS_G4X(dev_priv))
3944a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
3945a266c7d5SChris Wilson 	hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
3946a266c7d5SChris Wilson 
3947a266c7d5SChris Wilson 	/* Ignore TV since it's buggy */
39480706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv,
3949f9e3dc78SJani Nikula 					     HOTPLUG_INT_EN_MASK |
3950f9e3dc78SJani Nikula 					     CRT_HOTPLUG_VOLTAGE_COMPARE_MASK |
3951f9e3dc78SJani Nikula 					     CRT_HOTPLUG_ACTIVATION_PERIOD_64,
39520706f17cSEgbert Eich 					     hotplug_en);
3953a266c7d5SChris Wilson }
3954a266c7d5SChris Wilson 
3955ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
3956a266c7d5SChris Wilson {
395745a83f84SDaniel Vetter 	struct drm_device *dev = arg;
3958fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3959af722d28SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
3960a266c7d5SChris Wilson 
39612dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
39622dd2a883SImre Deak 		return IRQ_NONE;
39632dd2a883SImre Deak 
39641f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
39651f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
39661f814dacSImre Deak 
3967af722d28SVille Syrjälä 	do {
3968eb64343cSVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
3969af722d28SVille Syrjälä 		u32 hotplug_status = 0;
3970af722d28SVille Syrjälä 		u32 iir;
39712c8ba29fSChris Wilson 
3972af722d28SVille Syrjälä 		iir = I915_READ(IIR);
3973af722d28SVille Syrjälä 		if (iir == 0)
3974af722d28SVille Syrjälä 			break;
3975af722d28SVille Syrjälä 
3976af722d28SVille Syrjälä 		ret = IRQ_HANDLED;
3977af722d28SVille Syrjälä 
3978af722d28SVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
3979af722d28SVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
3980a266c7d5SChris Wilson 
3981eb64343cSVille Syrjälä 		/* Call regardless, as some status bits might not be
3982eb64343cSVille Syrjälä 		 * signalled in iir */
3983eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
3984a266c7d5SChris Wilson 
3985fd3a4024SDaniel Vetter 		I915_WRITE(IIR, iir);
3986a266c7d5SChris Wilson 
3987a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
39883b3f1650SAkash Goel 			notify_ring(dev_priv->engine[RCS]);
3989af722d28SVille Syrjälä 
3990a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
39913b3f1650SAkash Goel 			notify_ring(dev_priv->engine[VCS]);
3992a266c7d5SChris Wilson 
3993af722d28SVille Syrjälä 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3994af722d28SVille Syrjälä 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
3995515ac2bbSDaniel Vetter 
3996af722d28SVille Syrjälä 		if (hotplug_status)
3997af722d28SVille Syrjälä 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
3998af722d28SVille Syrjälä 
3999af722d28SVille Syrjälä 		i965_pipestat_irq_handler(dev_priv, iir, pipe_stats);
4000af722d28SVille Syrjälä 	} while (0);
4001a266c7d5SChris Wilson 
40021f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
40031f814dacSImre Deak 
4004a266c7d5SChris Wilson 	return ret;
4005a266c7d5SChris Wilson }
4006a266c7d5SChris Wilson 
4007fca52a55SDaniel Vetter /**
4008fca52a55SDaniel Vetter  * intel_irq_init - initializes irq support
4009fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4010fca52a55SDaniel Vetter  *
4011fca52a55SDaniel Vetter  * This function initializes all the irq support including work items, timers
4012fca52a55SDaniel Vetter  * and all the vtables. It does not setup the interrupt itself though.
4013fca52a55SDaniel Vetter  */
4014b963291cSDaniel Vetter void intel_irq_init(struct drm_i915_private *dev_priv)
4015f71d4af4SJesse Barnes {
401691c8a326SChris Wilson 	struct drm_device *dev = &dev_priv->drm;
4017cefcff8fSJoonas Lahtinen 	int i;
40188b2e326dSChris Wilson 
401977913b39SJani Nikula 	intel_hpd_init_work(dev_priv);
402077913b39SJani Nikula 
4021c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
4022cefcff8fSJoonas Lahtinen 
4023a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
4024cefcff8fSJoonas Lahtinen 	for (i = 0; i < MAX_L3_SLICES; ++i)
4025cefcff8fSJoonas Lahtinen 		dev_priv->l3_parity.remap_info[i] = NULL;
40268b2e326dSChris Wilson 
40274805fe82STvrtko Ursulin 	if (HAS_GUC_SCHED(dev_priv))
402826705e20SSagar Arun Kamble 		dev_priv->pm_guc_events = GEN9_GUC_TO_HOST_INT_EVENT;
402926705e20SSagar Arun Kamble 
4030a6706b45SDeepak S 	/* Let's track the enabled rps events */
4031666a4537SWayne Boyer 	if (IS_VALLEYVIEW(dev_priv))
40326c65a587SVille Syrjälä 		/* WaGsvRC0ResidencyMethod:vlv */
4033e0e8c7cbSChris Wilson 		dev_priv->pm_rps_events = GEN6_PM_RP_UP_EI_EXPIRED;
403431685c25SDeepak S 	else
4035a6706b45SDeepak S 		dev_priv->pm_rps_events = GEN6_PM_RPS_EVENTS;
4036a6706b45SDeepak S 
40375dd04556SSagar Arun Kamble 	dev_priv->rps.pm_intrmsk_mbz = 0;
40381800ad25SSagar Arun Kamble 
40391800ad25SSagar Arun Kamble 	/*
4040acf2dc22SMika Kuoppala 	 * SNB,IVB,HSW can while VLV,CHV may hard hang on looping batchbuffer
40411800ad25SSagar Arun Kamble 	 * if GEN6_PM_UP_EI_EXPIRED is masked.
40421800ad25SSagar Arun Kamble 	 *
40431800ad25SSagar Arun Kamble 	 * TODO: verify if this can be reproduced on VLV,CHV.
40441800ad25SSagar Arun Kamble 	 */
4045bca2bf2aSPandiyan, Dhinakaran 	if (INTEL_GEN(dev_priv) <= 7)
40465dd04556SSagar Arun Kamble 		dev_priv->rps.pm_intrmsk_mbz |= GEN6_PM_RP_UP_EI_EXPIRED;
40471800ad25SSagar Arun Kamble 
4048bca2bf2aSPandiyan, Dhinakaran 	if (INTEL_GEN(dev_priv) >= 8)
4049655d49efSChris Wilson 		dev_priv->rps.pm_intrmsk_mbz |= GEN8_PMINTR_DISABLE_REDIRECT_TO_GUC;
40501800ad25SSagar Arun Kamble 
4051b963291cSDaniel Vetter 	if (IS_GEN2(dev_priv)) {
40524194c088SRodrigo Vivi 		/* Gen2 doesn't have a hardware frame counter */
40534cdb83ecSVille Syrjälä 		dev->max_vblank_count = 0;
4054bca2bf2aSPandiyan, Dhinakaran 	} else if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5) {
4055f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
4056fd8f507cSVille Syrjälä 		dev->driver->get_vblank_counter = g4x_get_vblank_counter;
4057391f75e2SVille Syrjälä 	} else {
4058391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
4059391f75e2SVille Syrjälä 		dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
4060f71d4af4SJesse Barnes 	}
4061f71d4af4SJesse Barnes 
406221da2700SVille Syrjälä 	/*
406321da2700SVille Syrjälä 	 * Opt out of the vblank disable timer on everything except gen2.
406421da2700SVille Syrjälä 	 * Gen2 doesn't have a hardware frame counter and so depends on
406521da2700SVille Syrjälä 	 * vblank interrupts to produce sane vblank seuquence numbers.
406621da2700SVille Syrjälä 	 */
4067b963291cSDaniel Vetter 	if (!IS_GEN2(dev_priv))
406821da2700SVille Syrjälä 		dev->vblank_disable_immediate = true;
406921da2700SVille Syrjälä 
4070262fd485SChris Wilson 	/* Most platforms treat the display irq block as an always-on
4071262fd485SChris Wilson 	 * power domain. vlv/chv can disable it at runtime and need
4072262fd485SChris Wilson 	 * special care to avoid writing any of the display block registers
4073262fd485SChris Wilson 	 * outside of the power domain. We defer setting up the display irqs
4074262fd485SChris Wilson 	 * in this case to the runtime pm.
4075262fd485SChris Wilson 	 */
4076262fd485SChris Wilson 	dev_priv->display_irqs_enabled = true;
4077262fd485SChris Wilson 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4078262fd485SChris Wilson 		dev_priv->display_irqs_enabled = false;
4079262fd485SChris Wilson 
4080317eaa95SLyude 	dev_priv->hotplug.hpd_storm_threshold = HPD_STORM_DEFAULT_THRESHOLD;
4081317eaa95SLyude 
40821bf6ad62SDaniel Vetter 	dev->driver->get_vblank_timestamp = drm_calc_vbltimestamp_from_scanoutpos;
4083f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
4084f71d4af4SJesse Barnes 
4085b963291cSDaniel Vetter 	if (IS_CHERRYVIEW(dev_priv)) {
408643f328d7SVille Syrjälä 		dev->driver->irq_handler = cherryview_irq_handler;
40876bcdb1c8SVille Syrjälä 		dev->driver->irq_preinstall = cherryview_irq_reset;
408843f328d7SVille Syrjälä 		dev->driver->irq_postinstall = cherryview_irq_postinstall;
40896bcdb1c8SVille Syrjälä 		dev->driver->irq_uninstall = cherryview_irq_reset;
409086e83e35SChris Wilson 		dev->driver->enable_vblank = i965_enable_vblank;
409186e83e35SChris Wilson 		dev->driver->disable_vblank = i965_disable_vblank;
409243f328d7SVille Syrjälä 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4093b963291cSDaniel Vetter 	} else if (IS_VALLEYVIEW(dev_priv)) {
40947e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
40956bcdb1c8SVille Syrjälä 		dev->driver->irq_preinstall = valleyview_irq_reset;
40967e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
40976bcdb1c8SVille Syrjälä 		dev->driver->irq_uninstall = valleyview_irq_reset;
409886e83e35SChris Wilson 		dev->driver->enable_vblank = i965_enable_vblank;
409986e83e35SChris Wilson 		dev->driver->disable_vblank = i965_disable_vblank;
4100fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4101bca2bf2aSPandiyan, Dhinakaran 	} else if (INTEL_GEN(dev_priv) >= 8) {
4102abd58f01SBen Widawsky 		dev->driver->irq_handler = gen8_irq_handler;
4103723761b8SDaniel Vetter 		dev->driver->irq_preinstall = gen8_irq_reset;
4104abd58f01SBen Widawsky 		dev->driver->irq_postinstall = gen8_irq_postinstall;
41056bcdb1c8SVille Syrjälä 		dev->driver->irq_uninstall = gen8_irq_reset;
4106abd58f01SBen Widawsky 		dev->driver->enable_vblank = gen8_enable_vblank;
4107abd58f01SBen Widawsky 		dev->driver->disable_vblank = gen8_disable_vblank;
4108cc3f90f0SAnder Conselvan de Oliveira 		if (IS_GEN9_LP(dev_priv))
4109e0a20ad7SShashank Sharma 			dev_priv->display.hpd_irq_setup = bxt_hpd_irq_setup;
41107b22b8c4SRodrigo Vivi 		else if (HAS_PCH_SPT(dev_priv) || HAS_PCH_KBP(dev_priv) ||
41117b22b8c4SRodrigo Vivi 			 HAS_PCH_CNP(dev_priv))
41126dbf30ceSVille Syrjälä 			dev_priv->display.hpd_irq_setup = spt_hpd_irq_setup;
41136dbf30ceSVille Syrjälä 		else
41143a3b3c7dSVille Syrjälä 			dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
41156e266956STvrtko Ursulin 	} else if (HAS_PCH_SPLIT(dev_priv)) {
4116f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
4117723761b8SDaniel Vetter 		dev->driver->irq_preinstall = ironlake_irq_reset;
4118f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
41196bcdb1c8SVille Syrjälä 		dev->driver->irq_uninstall = ironlake_irq_reset;
4120f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
4121f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
4122e4ce95aaSVille Syrjälä 		dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
4123f71d4af4SJesse Barnes 	} else {
41247e22dbbbSTvrtko Ursulin 		if (IS_GEN2(dev_priv)) {
41256bcdb1c8SVille Syrjälä 			dev->driver->irq_preinstall = i8xx_irq_reset;
4126c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
4127c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
41286bcdb1c8SVille Syrjälä 			dev->driver->irq_uninstall = i8xx_irq_reset;
412986e83e35SChris Wilson 			dev->driver->enable_vblank = i8xx_enable_vblank;
413086e83e35SChris Wilson 			dev->driver->disable_vblank = i8xx_disable_vblank;
41317e22dbbbSTvrtko Ursulin 		} else if (IS_GEN3(dev_priv)) {
41326bcdb1c8SVille Syrjälä 			dev->driver->irq_preinstall = i915_irq_reset;
4133a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
41346bcdb1c8SVille Syrjälä 			dev->driver->irq_uninstall = i915_irq_reset;
4135a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
413686e83e35SChris Wilson 			dev->driver->enable_vblank = i8xx_enable_vblank;
413786e83e35SChris Wilson 			dev->driver->disable_vblank = i8xx_disable_vblank;
4138c2798b19SChris Wilson 		} else {
41396bcdb1c8SVille Syrjälä 			dev->driver->irq_preinstall = i965_irq_reset;
4140a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
41416bcdb1c8SVille Syrjälä 			dev->driver->irq_uninstall = i965_irq_reset;
4142a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
414386e83e35SChris Wilson 			dev->driver->enable_vblank = i965_enable_vblank;
414486e83e35SChris Wilson 			dev->driver->disable_vblank = i965_disable_vblank;
4145c2798b19SChris Wilson 		}
4146778eb334SVille Syrjälä 		if (I915_HAS_HOTPLUG(dev_priv))
4147778eb334SVille Syrjälä 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4148f71d4af4SJesse Barnes 	}
4149f71d4af4SJesse Barnes }
415020afbda2SDaniel Vetter 
4151fca52a55SDaniel Vetter /**
4152cefcff8fSJoonas Lahtinen  * intel_irq_fini - deinitializes IRQ support
4153cefcff8fSJoonas Lahtinen  * @i915: i915 device instance
4154cefcff8fSJoonas Lahtinen  *
4155cefcff8fSJoonas Lahtinen  * This function deinitializes all the IRQ support.
4156cefcff8fSJoonas Lahtinen  */
4157cefcff8fSJoonas Lahtinen void intel_irq_fini(struct drm_i915_private *i915)
4158cefcff8fSJoonas Lahtinen {
4159cefcff8fSJoonas Lahtinen 	int i;
4160cefcff8fSJoonas Lahtinen 
4161cefcff8fSJoonas Lahtinen 	for (i = 0; i < MAX_L3_SLICES; ++i)
4162cefcff8fSJoonas Lahtinen 		kfree(i915->l3_parity.remap_info[i]);
4163cefcff8fSJoonas Lahtinen }
4164cefcff8fSJoonas Lahtinen 
4165cefcff8fSJoonas Lahtinen /**
4166fca52a55SDaniel Vetter  * intel_irq_install - enables the hardware interrupt
4167fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4168fca52a55SDaniel Vetter  *
4169fca52a55SDaniel Vetter  * This function enables the hardware interrupt handling, but leaves the hotplug
4170fca52a55SDaniel Vetter  * handling still disabled. It is called after intel_irq_init().
4171fca52a55SDaniel Vetter  *
4172fca52a55SDaniel Vetter  * In the driver load and resume code we need working interrupts in a few places
4173fca52a55SDaniel Vetter  * but don't want to deal with the hassle of concurrent probe and hotplug
4174fca52a55SDaniel Vetter  * workers. Hence the split into this two-stage approach.
4175fca52a55SDaniel Vetter  */
41762aeb7d3aSDaniel Vetter int intel_irq_install(struct drm_i915_private *dev_priv)
41772aeb7d3aSDaniel Vetter {
41782aeb7d3aSDaniel Vetter 	/*
41792aeb7d3aSDaniel Vetter 	 * We enable some interrupt sources in our postinstall hooks, so mark
41802aeb7d3aSDaniel Vetter 	 * interrupts as enabled _before_ actually enabling them to avoid
41812aeb7d3aSDaniel Vetter 	 * special cases in our ordering checks.
41822aeb7d3aSDaniel Vetter 	 */
41832aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
41842aeb7d3aSDaniel Vetter 
418591c8a326SChris Wilson 	return drm_irq_install(&dev_priv->drm, dev_priv->drm.pdev->irq);
41862aeb7d3aSDaniel Vetter }
41872aeb7d3aSDaniel Vetter 
4188fca52a55SDaniel Vetter /**
4189fca52a55SDaniel Vetter  * intel_irq_uninstall - finilizes all irq handling
4190fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4191fca52a55SDaniel Vetter  *
4192fca52a55SDaniel Vetter  * This stops interrupt and hotplug handling and unregisters and frees all
4193fca52a55SDaniel Vetter  * resources acquired in the init functions.
4194fca52a55SDaniel Vetter  */
41952aeb7d3aSDaniel Vetter void intel_irq_uninstall(struct drm_i915_private *dev_priv)
41962aeb7d3aSDaniel Vetter {
419791c8a326SChris Wilson 	drm_irq_uninstall(&dev_priv->drm);
41982aeb7d3aSDaniel Vetter 	intel_hpd_cancel_work(dev_priv);
41992aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
42002aeb7d3aSDaniel Vetter }
42012aeb7d3aSDaniel Vetter 
4202fca52a55SDaniel Vetter /**
4203fca52a55SDaniel Vetter  * intel_runtime_pm_disable_interrupts - runtime interrupt disabling
4204fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4205fca52a55SDaniel Vetter  *
4206fca52a55SDaniel Vetter  * This function is used to disable interrupts at runtime, both in the runtime
4207fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4208fca52a55SDaniel Vetter  */
4209b963291cSDaniel Vetter void intel_runtime_pm_disable_interrupts(struct drm_i915_private *dev_priv)
4210c67a470bSPaulo Zanoni {
421191c8a326SChris Wilson 	dev_priv->drm.driver->irq_uninstall(&dev_priv->drm);
42122aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
421391c8a326SChris Wilson 	synchronize_irq(dev_priv->drm.irq);
4214c67a470bSPaulo Zanoni }
4215c67a470bSPaulo Zanoni 
4216fca52a55SDaniel Vetter /**
4217fca52a55SDaniel Vetter  * intel_runtime_pm_enable_interrupts - runtime interrupt enabling
4218fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4219fca52a55SDaniel Vetter  *
4220fca52a55SDaniel Vetter  * This function is used to enable interrupts at runtime, both in the runtime
4221fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4222fca52a55SDaniel Vetter  */
4223b963291cSDaniel Vetter void intel_runtime_pm_enable_interrupts(struct drm_i915_private *dev_priv)
4224c67a470bSPaulo Zanoni {
42252aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
422691c8a326SChris Wilson 	dev_priv->drm.driver->irq_preinstall(&dev_priv->drm);
422791c8a326SChris Wilson 	dev_priv->drm.driver->irq_postinstall(&dev_priv->drm);
4228c67a470bSPaulo Zanoni }
4229