xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision e9e9848a6bcfb635c94e1d5f0b3f60799cfe8955)
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 
139*e9e9848aSVille Syrjälä #define GEN2_IRQ_RESET(type) do { \
140*e9e9848aSVille Syrjälä 	I915_WRITE16(type##IMR, 0xffff); \
141*e9e9848aSVille Syrjälä 	POSTING_READ16(type##IMR); \
142*e9e9848aSVille Syrjälä 	I915_WRITE16(type##IER, 0); \
143*e9e9848aSVille Syrjälä 	I915_WRITE16(type##IIR, 0xffff); \
144*e9e9848aSVille Syrjälä 	POSTING_READ16(type##IIR); \
145*e9e9848aSVille Syrjälä 	I915_WRITE16(type##IIR, 0xffff); \
146*e9e9848aSVille Syrjälä 	POSTING_READ16(type##IIR); \
147*e9e9848aSVille Syrjälä } while (0)
148*e9e9848aSVille 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 
168*e9e9848aSVille Syrjälä static void gen2_assert_iir_is_zero(struct drm_i915_private *dev_priv,
169*e9e9848aSVille Syrjälä 				    i915_reg_t reg)
170*e9e9848aSVille Syrjälä {
171*e9e9848aSVille Syrjälä 	u16 val = I915_READ16(reg);
172*e9e9848aSVille Syrjälä 
173*e9e9848aSVille Syrjälä 	if (val == 0)
174*e9e9848aSVille Syrjälä 		return;
175*e9e9848aSVille Syrjälä 
176*e9e9848aSVille Syrjälä 	WARN(1, "Interrupt register 0x%x is not zero: 0x%08x\n",
177*e9e9848aSVille Syrjälä 	     i915_mmio_reg_offset(reg), val);
178*e9e9848aSVille Syrjälä 	I915_WRITE16(reg, 0xffff);
179*e9e9848aSVille Syrjälä 	POSTING_READ16(reg);
180*e9e9848aSVille Syrjälä 	I915_WRITE16(reg, 0xffff);
181*e9e9848aSVille Syrjälä 	POSTING_READ16(reg);
182*e9e9848aSVille Syrjälä }
183*e9e9848aSVille 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 
198*e9e9848aSVille Syrjälä #define GEN2_IRQ_INIT(type, imr_val, ier_val) do { \
199*e9e9848aSVille Syrjälä 	gen2_assert_iir_is_zero(dev_priv, type##IIR); \
200*e9e9848aSVille Syrjälä 	I915_WRITE16(type##IER, (ier_val)); \
201*e9e9848aSVille Syrjälä 	I915_WRITE16(type##IMR, (imr_val)); \
202*e9e9848aSVille Syrjälä 	POSTING_READ16(type##IMR); \
203*e9e9848aSVille Syrjälä } while (0)
204*e9e9848aSVille 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 
570b5ea642aSDaniel Vetter static void
571755e9019SImre Deak __i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
572755e9019SImre Deak 		       u32 enable_mask, u32 status_mask)
5737c463586SKeith Packard {
574f0f59a00SVille Syrjälä 	i915_reg_t reg = PIPESTAT(pipe);
575755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
5767c463586SKeith Packard 
57767520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
578d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
579b79480baSDaniel Vetter 
58004feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
58104feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
58204feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
58304feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
584755e9019SImre Deak 		return;
585755e9019SImre Deak 
586755e9019SImre Deak 	if ((pipestat & enable_mask) == enable_mask)
58746c06a30SVille Syrjälä 		return;
58846c06a30SVille Syrjälä 
58991d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] |= status_mask;
59091d181ddSImre Deak 
5917c463586SKeith Packard 	/* Enable the interrupt, clear any pending status */
592755e9019SImre Deak 	pipestat |= enable_mask | status_mask;
59346c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
5943143a2bfSChris Wilson 	POSTING_READ(reg);
5957c463586SKeith Packard }
5967c463586SKeith Packard 
597b5ea642aSDaniel Vetter static void
598755e9019SImre Deak __i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
599755e9019SImre Deak 		        u32 enable_mask, u32 status_mask)
6007c463586SKeith Packard {
601f0f59a00SVille Syrjälä 	i915_reg_t reg = PIPESTAT(pipe);
602755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
6037c463586SKeith Packard 
60467520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
605d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
606b79480baSDaniel Vetter 
60704feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
60804feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
60904feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
61004feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
61146c06a30SVille Syrjälä 		return;
61246c06a30SVille Syrjälä 
613755e9019SImre Deak 	if ((pipestat & enable_mask) == 0)
614755e9019SImre Deak 		return;
615755e9019SImre Deak 
61691d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] &= ~status_mask;
61791d181ddSImre Deak 
618755e9019SImre Deak 	pipestat &= ~enable_mask;
61946c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
6203143a2bfSChris Wilson 	POSTING_READ(reg);
6217c463586SKeith Packard }
6227c463586SKeith Packard 
62310c59c51SImre Deak static u32 vlv_get_pipestat_enable_mask(struct drm_device *dev, u32 status_mask)
62410c59c51SImre Deak {
62510c59c51SImre Deak 	u32 enable_mask = status_mask << 16;
62610c59c51SImre Deak 
62710c59c51SImre Deak 	/*
628724a6905SVille Syrjälä 	 * On pipe A we don't support the PSR interrupt yet,
629724a6905SVille Syrjälä 	 * on pipe B and C the same bit MBZ.
63010c59c51SImre Deak 	 */
63110c59c51SImre Deak 	if (WARN_ON_ONCE(status_mask & PIPE_A_PSR_STATUS_VLV))
63210c59c51SImre Deak 		return 0;
633724a6905SVille Syrjälä 	/*
634724a6905SVille Syrjälä 	 * On pipe B and C we don't support the PSR interrupt yet, on pipe
635724a6905SVille Syrjälä 	 * A the same bit is for perf counters which we don't use either.
636724a6905SVille Syrjälä 	 */
637724a6905SVille Syrjälä 	if (WARN_ON_ONCE(status_mask & PIPE_B_PSR_STATUS_VLV))
638724a6905SVille Syrjälä 		return 0;
63910c59c51SImre Deak 
64010c59c51SImre Deak 	enable_mask &= ~(PIPE_FIFO_UNDERRUN_STATUS |
64110c59c51SImre Deak 			 SPRITE0_FLIP_DONE_INT_EN_VLV |
64210c59c51SImre Deak 			 SPRITE1_FLIP_DONE_INT_EN_VLV);
64310c59c51SImre Deak 	if (status_mask & SPRITE0_FLIP_DONE_INT_STATUS_VLV)
64410c59c51SImre Deak 		enable_mask |= SPRITE0_FLIP_DONE_INT_EN_VLV;
64510c59c51SImre Deak 	if (status_mask & SPRITE1_FLIP_DONE_INT_STATUS_VLV)
64610c59c51SImre Deak 		enable_mask |= SPRITE1_FLIP_DONE_INT_EN_VLV;
64710c59c51SImre Deak 
64810c59c51SImre Deak 	return enable_mask;
64910c59c51SImre Deak }
65010c59c51SImre Deak 
651755e9019SImre Deak void
652755e9019SImre Deak i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
653755e9019SImre Deak 		     u32 status_mask)
654755e9019SImre Deak {
655755e9019SImre Deak 	u32 enable_mask;
656755e9019SImre Deak 
657666a4537SWayne Boyer 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
65891c8a326SChris Wilson 		enable_mask = vlv_get_pipestat_enable_mask(&dev_priv->drm,
65910c59c51SImre Deak 							   status_mask);
66010c59c51SImre Deak 	else
661755e9019SImre Deak 		enable_mask = status_mask << 16;
662755e9019SImre Deak 	__i915_enable_pipestat(dev_priv, pipe, enable_mask, status_mask);
663755e9019SImre Deak }
664755e9019SImre Deak 
665755e9019SImre Deak void
666755e9019SImre Deak i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
667755e9019SImre Deak 		      u32 status_mask)
668755e9019SImre Deak {
669755e9019SImre Deak 	u32 enable_mask;
670755e9019SImre Deak 
671666a4537SWayne Boyer 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
67291c8a326SChris Wilson 		enable_mask = vlv_get_pipestat_enable_mask(&dev_priv->drm,
67310c59c51SImre Deak 							   status_mask);
67410c59c51SImre Deak 	else
675755e9019SImre Deak 		enable_mask = status_mask << 16;
676755e9019SImre Deak 	__i915_disable_pipestat(dev_priv, pipe, enable_mask, status_mask);
677755e9019SImre Deak }
678755e9019SImre Deak 
679c0e09200SDave Airlie /**
680f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
68114bb2c11STvrtko Ursulin  * @dev_priv: i915 device private
68201c66889SZhao Yakui  */
68391d14251STvrtko Ursulin static void i915_enable_asle_pipestat(struct drm_i915_private *dev_priv)
68401c66889SZhao Yakui {
68591d14251STvrtko Ursulin 	if (!dev_priv->opregion.asle || !IS_MOBILE(dev_priv))
686f49e38ddSJani Nikula 		return;
687f49e38ddSJani Nikula 
68813321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
68901c66889SZhao Yakui 
690755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_STATUS);
69191d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 4)
6923b6c42e8SDaniel Vetter 		i915_enable_pipestat(dev_priv, PIPE_A,
693755e9019SImre Deak 				     PIPE_LEGACY_BLC_EVENT_STATUS);
6941ec14ad3SChris Wilson 
69513321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
69601c66889SZhao Yakui }
69701c66889SZhao Yakui 
698f75f3746SVille Syrjälä /*
699f75f3746SVille Syrjälä  * This timing diagram depicts the video signal in and
700f75f3746SVille Syrjälä  * around the vertical blanking period.
701f75f3746SVille Syrjälä  *
702f75f3746SVille Syrjälä  * Assumptions about the fictitious mode used in this example:
703f75f3746SVille Syrjälä  *  vblank_start >= 3
704f75f3746SVille Syrjälä  *  vsync_start = vblank_start + 1
705f75f3746SVille Syrjälä  *  vsync_end = vblank_start + 2
706f75f3746SVille Syrjälä  *  vtotal = vblank_start + 3
707f75f3746SVille Syrjälä  *
708f75f3746SVille Syrjälä  *           start of vblank:
709f75f3746SVille Syrjälä  *           latch double buffered registers
710f75f3746SVille Syrjälä  *           increment frame counter (ctg+)
711f75f3746SVille Syrjälä  *           generate start of vblank interrupt (gen4+)
712f75f3746SVille Syrjälä  *           |
713f75f3746SVille Syrjälä  *           |          frame start:
714f75f3746SVille Syrjälä  *           |          generate frame start interrupt (aka. vblank interrupt) (gmch)
715f75f3746SVille Syrjälä  *           |          may be shifted forward 1-3 extra lines via PIPECONF
716f75f3746SVille Syrjälä  *           |          |
717f75f3746SVille Syrjälä  *           |          |  start of vsync:
718f75f3746SVille Syrjälä  *           |          |  generate vsync interrupt
719f75f3746SVille Syrjälä  *           |          |  |
720f75f3746SVille Syrjälä  * ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx
721f75f3746SVille Syrjälä  *       .   \hs/   .      \hs/          \hs/          \hs/   .      \hs/
722f75f3746SVille Syrjälä  * ----va---> <-----------------vb--------------------> <--------va-------------
723f75f3746SVille Syrjälä  *       |          |       <----vs----->                     |
724f75f3746SVille Syrjälä  * -vbs-----> <---vbs+1---> <---vbs+2---> <-----0-----> <-----1-----> <-----2--- (scanline counter gen2)
725f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2---> <-----0--- (scanline counter gen3+)
726f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2- (scanline counter hsw+ hdmi)
727f75f3746SVille Syrjälä  *       |          |                                         |
728f75f3746SVille Syrjälä  *       last visible pixel                                   first visible pixel
729f75f3746SVille Syrjälä  *                  |                                         increment frame counter (gen3/4)
730f75f3746SVille Syrjälä  *                  pixel counter = vblank_start * htotal     pixel counter = 0 (gen3/4)
731f75f3746SVille Syrjälä  *
732f75f3746SVille Syrjälä  * x  = horizontal active
733f75f3746SVille Syrjälä  * _  = horizontal blanking
734f75f3746SVille Syrjälä  * hs = horizontal sync
735f75f3746SVille Syrjälä  * va = vertical active
736f75f3746SVille Syrjälä  * vb = vertical blanking
737f75f3746SVille Syrjälä  * vs = vertical sync
738f75f3746SVille Syrjälä  * vbs = vblank_start (number)
739f75f3746SVille Syrjälä  *
740f75f3746SVille Syrjälä  * Summary:
741f75f3746SVille Syrjälä  * - most events happen at the start of horizontal sync
742f75f3746SVille Syrjälä  * - frame start happens at the start of horizontal blank, 1-4 lines
743f75f3746SVille Syrjälä  *   (depending on PIPECONF settings) after the start of vblank
744f75f3746SVille Syrjälä  * - gen3/4 pixel and frame counter are synchronized with the start
745f75f3746SVille Syrjälä  *   of horizontal active on the first line of vertical active
746f75f3746SVille Syrjälä  */
747f75f3746SVille Syrjälä 
74842f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
74942f52ef8SKeith Packard  * we use as a pipe index
75042f52ef8SKeith Packard  */
75188e72717SThierry Reding static u32 i915_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
7520a3e67a4SJesse Barnes {
753fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
754f0f59a00SVille Syrjälä 	i915_reg_t high_frame, low_frame;
7550b2a8e09SVille Syrjälä 	u32 high1, high2, low, pixel, vbl_start, hsync_start, htotal;
7565caa0feaSDaniel Vetter 	const struct drm_display_mode *mode = &dev->vblank[pipe].hwmode;
757694e409dSVille Syrjälä 	unsigned long irqflags;
758391f75e2SVille Syrjälä 
7590b2a8e09SVille Syrjälä 	htotal = mode->crtc_htotal;
7600b2a8e09SVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
7610b2a8e09SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
7620b2a8e09SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
7630b2a8e09SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
764391f75e2SVille Syrjälä 
7650b2a8e09SVille Syrjälä 	/* Convert to pixel count */
7660b2a8e09SVille Syrjälä 	vbl_start *= htotal;
7670b2a8e09SVille Syrjälä 
7680b2a8e09SVille Syrjälä 	/* Start of vblank event occurs at start of hsync */
7690b2a8e09SVille Syrjälä 	vbl_start -= htotal - hsync_start;
7700b2a8e09SVille Syrjälä 
7719db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
7729db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
7735eddb70bSChris Wilson 
774694e409dSVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
775694e409dSVille Syrjälä 
7760a3e67a4SJesse Barnes 	/*
7770a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
7780a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
7790a3e67a4SJesse Barnes 	 * register.
7800a3e67a4SJesse Barnes 	 */
7810a3e67a4SJesse Barnes 	do {
782694e409dSVille Syrjälä 		high1 = I915_READ_FW(high_frame) & PIPE_FRAME_HIGH_MASK;
783694e409dSVille Syrjälä 		low   = I915_READ_FW(low_frame);
784694e409dSVille Syrjälä 		high2 = I915_READ_FW(high_frame) & PIPE_FRAME_HIGH_MASK;
7850a3e67a4SJesse Barnes 	} while (high1 != high2);
7860a3e67a4SJesse Barnes 
787694e409dSVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
788694e409dSVille Syrjälä 
7895eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
790391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
7915eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
792391f75e2SVille Syrjälä 
793391f75e2SVille Syrjälä 	/*
794391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
795391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
796391f75e2SVille Syrjälä 	 * counter against vblank start.
797391f75e2SVille Syrjälä 	 */
798edc08d0aSVille Syrjälä 	return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
7990a3e67a4SJesse Barnes }
8000a3e67a4SJesse Barnes 
801974e59baSDave Airlie static u32 g4x_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
8029880b7a5SJesse Barnes {
803fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
8049880b7a5SJesse Barnes 
805649636efSVille Syrjälä 	return I915_READ(PIPE_FRMCOUNT_G4X(pipe));
8069880b7a5SJesse Barnes }
8079880b7a5SJesse Barnes 
80875aa3f63SVille Syrjälä /* I915_READ_FW, only for fast reads of display block, no need for forcewake etc. */
809a225f079SVille Syrjälä static int __intel_get_crtc_scanline(struct intel_crtc *crtc)
810a225f079SVille Syrjälä {
811a225f079SVille Syrjälä 	struct drm_device *dev = crtc->base.dev;
812fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
8135caa0feaSDaniel Vetter 	const struct drm_display_mode *mode;
8145caa0feaSDaniel Vetter 	struct drm_vblank_crtc *vblank;
815a225f079SVille Syrjälä 	enum pipe pipe = crtc->pipe;
81680715b2fSVille Syrjälä 	int position, vtotal;
817a225f079SVille Syrjälä 
81872259536SVille Syrjälä 	if (!crtc->active)
81972259536SVille Syrjälä 		return -1;
82072259536SVille Syrjälä 
8215caa0feaSDaniel Vetter 	vblank = &crtc->base.dev->vblank[drm_crtc_index(&crtc->base)];
8225caa0feaSDaniel Vetter 	mode = &vblank->hwmode;
8235caa0feaSDaniel Vetter 
82480715b2fSVille Syrjälä 	vtotal = mode->crtc_vtotal;
825a225f079SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
826a225f079SVille Syrjälä 		vtotal /= 2;
827a225f079SVille Syrjälä 
82891d14251STvrtko Ursulin 	if (IS_GEN2(dev_priv))
82975aa3f63SVille Syrjälä 		position = I915_READ_FW(PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
830a225f079SVille Syrjälä 	else
83175aa3f63SVille Syrjälä 		position = I915_READ_FW(PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
832a225f079SVille Syrjälä 
833a225f079SVille Syrjälä 	/*
83441b578fbSJesse Barnes 	 * On HSW, the DSL reg (0x70000) appears to return 0 if we
83541b578fbSJesse Barnes 	 * read it just before the start of vblank.  So try it again
83641b578fbSJesse Barnes 	 * so we don't accidentally end up spanning a vblank frame
83741b578fbSJesse Barnes 	 * increment, causing the pipe_update_end() code to squak at us.
83841b578fbSJesse Barnes 	 *
83941b578fbSJesse Barnes 	 * The nature of this problem means we can't simply check the ISR
84041b578fbSJesse Barnes 	 * bit and return the vblank start value; nor can we use the scanline
84141b578fbSJesse Barnes 	 * debug register in the transcoder as it appears to have the same
84241b578fbSJesse Barnes 	 * problem.  We may need to extend this to include other platforms,
84341b578fbSJesse Barnes 	 * but so far testing only shows the problem on HSW.
84441b578fbSJesse Barnes 	 */
84591d14251STvrtko Ursulin 	if (HAS_DDI(dev_priv) && !position) {
84641b578fbSJesse Barnes 		int i, temp;
84741b578fbSJesse Barnes 
84841b578fbSJesse Barnes 		for (i = 0; i < 100; i++) {
84941b578fbSJesse Barnes 			udelay(1);
850707bdd3fSVille Syrjälä 			temp = I915_READ_FW(PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
85141b578fbSJesse Barnes 			if (temp != position) {
85241b578fbSJesse Barnes 				position = temp;
85341b578fbSJesse Barnes 				break;
85441b578fbSJesse Barnes 			}
85541b578fbSJesse Barnes 		}
85641b578fbSJesse Barnes 	}
85741b578fbSJesse Barnes 
85841b578fbSJesse Barnes 	/*
85980715b2fSVille Syrjälä 	 * See update_scanline_offset() for the details on the
86080715b2fSVille Syrjälä 	 * scanline_offset adjustment.
861a225f079SVille Syrjälä 	 */
86280715b2fSVille Syrjälä 	return (position + crtc->scanline_offset) % vtotal;
863a225f079SVille Syrjälä }
864a225f079SVille Syrjälä 
8651bf6ad62SDaniel Vetter static bool i915_get_crtc_scanoutpos(struct drm_device *dev, unsigned int pipe,
8661bf6ad62SDaniel Vetter 				     bool in_vblank_irq, int *vpos, int *hpos,
8673bb403bfSVille Syrjälä 				     ktime_t *stime, ktime_t *etime,
8683bb403bfSVille Syrjälä 				     const struct drm_display_mode *mode)
8690af7e4dfSMario Kleiner {
870fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
87198187836SVille Syrjälä 	struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
87298187836SVille Syrjälä 								pipe);
8733aa18df8SVille Syrjälä 	int position;
87478e8fc6bSVille Syrjälä 	int vbl_start, vbl_end, hsync_start, htotal, vtotal;
8750af7e4dfSMario Kleiner 	bool in_vbl = true;
876ad3543edSMario Kleiner 	unsigned long irqflags;
8770af7e4dfSMario Kleiner 
878fc467a22SMaarten Lankhorst 	if (WARN_ON(!mode->crtc_clock)) {
8790af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
8809db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
8811bf6ad62SDaniel Vetter 		return false;
8820af7e4dfSMario Kleiner 	}
8830af7e4dfSMario Kleiner 
884c2baf4b7SVille Syrjälä 	htotal = mode->crtc_htotal;
88578e8fc6bSVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
886c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
887c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
888c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
8890af7e4dfSMario Kleiner 
890d31faf65SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
891d31faf65SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
892d31faf65SVille Syrjälä 		vbl_end /= 2;
893d31faf65SVille Syrjälä 		vtotal /= 2;
894d31faf65SVille Syrjälä 	}
895d31faf65SVille Syrjälä 
896ad3543edSMario Kleiner 	/*
897ad3543edSMario Kleiner 	 * Lock uncore.lock, as we will do multiple timing critical raw
898ad3543edSMario Kleiner 	 * register reads, potentially with preemption disabled, so the
899ad3543edSMario Kleiner 	 * following code must not block on uncore.lock.
900ad3543edSMario Kleiner 	 */
901ad3543edSMario Kleiner 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
902ad3543edSMario Kleiner 
903ad3543edSMario Kleiner 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
904ad3543edSMario Kleiner 
905ad3543edSMario Kleiner 	/* Get optional system timestamp before query. */
906ad3543edSMario Kleiner 	if (stime)
907ad3543edSMario Kleiner 		*stime = ktime_get();
908ad3543edSMario Kleiner 
90991d14251STvrtko Ursulin 	if (IS_GEN2(dev_priv) || IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5) {
9100af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
9110af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
9120af7e4dfSMario Kleiner 		 */
913a225f079SVille Syrjälä 		position = __intel_get_crtc_scanline(intel_crtc);
9140af7e4dfSMario Kleiner 	} else {
9150af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
9160af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
9170af7e4dfSMario Kleiner 		 * scanout position.
9180af7e4dfSMario Kleiner 		 */
91975aa3f63SVille Syrjälä 		position = (I915_READ_FW(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
9200af7e4dfSMario Kleiner 
9213aa18df8SVille Syrjälä 		/* convert to pixel counts */
9223aa18df8SVille Syrjälä 		vbl_start *= htotal;
9233aa18df8SVille Syrjälä 		vbl_end *= htotal;
9243aa18df8SVille Syrjälä 		vtotal *= htotal;
92578e8fc6bSVille Syrjälä 
92678e8fc6bSVille Syrjälä 		/*
9277e78f1cbSVille Syrjälä 		 * In interlaced modes, the pixel counter counts all pixels,
9287e78f1cbSVille Syrjälä 		 * so one field will have htotal more pixels. In order to avoid
9297e78f1cbSVille Syrjälä 		 * the reported position from jumping backwards when the pixel
9307e78f1cbSVille Syrjälä 		 * counter is beyond the length of the shorter field, just
9317e78f1cbSVille Syrjälä 		 * clamp the position the length of the shorter field. This
9327e78f1cbSVille Syrjälä 		 * matches how the scanline counter based position works since
9337e78f1cbSVille Syrjälä 		 * the scanline counter doesn't count the two half lines.
9347e78f1cbSVille Syrjälä 		 */
9357e78f1cbSVille Syrjälä 		if (position >= vtotal)
9367e78f1cbSVille Syrjälä 			position = vtotal - 1;
9377e78f1cbSVille Syrjälä 
9387e78f1cbSVille Syrjälä 		/*
93978e8fc6bSVille Syrjälä 		 * Start of vblank interrupt is triggered at start of hsync,
94078e8fc6bSVille Syrjälä 		 * just prior to the first active line of vblank. However we
94178e8fc6bSVille Syrjälä 		 * consider lines to start at the leading edge of horizontal
94278e8fc6bSVille Syrjälä 		 * active. So, should we get here before we've crossed into
94378e8fc6bSVille Syrjälä 		 * the horizontal active of the first line in vblank, we would
94478e8fc6bSVille Syrjälä 		 * not set the DRM_SCANOUTPOS_INVBL flag. In order to fix that,
94578e8fc6bSVille Syrjälä 		 * always add htotal-hsync_start to the current pixel position.
94678e8fc6bSVille Syrjälä 		 */
94778e8fc6bSVille Syrjälä 		position = (position + htotal - hsync_start) % vtotal;
9483aa18df8SVille Syrjälä 	}
9493aa18df8SVille Syrjälä 
950ad3543edSMario Kleiner 	/* Get optional system timestamp after query. */
951ad3543edSMario Kleiner 	if (etime)
952ad3543edSMario Kleiner 		*etime = ktime_get();
953ad3543edSMario Kleiner 
954ad3543edSMario Kleiner 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
955ad3543edSMario Kleiner 
956ad3543edSMario Kleiner 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
957ad3543edSMario Kleiner 
9583aa18df8SVille Syrjälä 	in_vbl = position >= vbl_start && position < vbl_end;
9593aa18df8SVille Syrjälä 
9603aa18df8SVille Syrjälä 	/*
9613aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
9623aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
9633aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
9643aa18df8SVille Syrjälä 	 * up since vbl_end.
9653aa18df8SVille Syrjälä 	 */
9663aa18df8SVille Syrjälä 	if (position >= vbl_start)
9673aa18df8SVille Syrjälä 		position -= vbl_end;
9683aa18df8SVille Syrjälä 	else
9693aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
9703aa18df8SVille Syrjälä 
97191d14251STvrtko Ursulin 	if (IS_GEN2(dev_priv) || IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5) {
9723aa18df8SVille Syrjälä 		*vpos = position;
9733aa18df8SVille Syrjälä 		*hpos = 0;
9743aa18df8SVille Syrjälä 	} else {
9750af7e4dfSMario Kleiner 		*vpos = position / htotal;
9760af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
9770af7e4dfSMario Kleiner 	}
9780af7e4dfSMario Kleiner 
9791bf6ad62SDaniel Vetter 	return true;
9800af7e4dfSMario Kleiner }
9810af7e4dfSMario Kleiner 
982a225f079SVille Syrjälä int intel_get_crtc_scanline(struct intel_crtc *crtc)
983a225f079SVille Syrjälä {
984fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
985a225f079SVille Syrjälä 	unsigned long irqflags;
986a225f079SVille Syrjälä 	int position;
987a225f079SVille Syrjälä 
988a225f079SVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
989a225f079SVille Syrjälä 	position = __intel_get_crtc_scanline(crtc);
990a225f079SVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
991a225f079SVille Syrjälä 
992a225f079SVille Syrjälä 	return position;
993a225f079SVille Syrjälä }
994a225f079SVille Syrjälä 
99591d14251STvrtko Ursulin static void ironlake_rps_change_irq_handler(struct drm_i915_private *dev_priv)
996f97108d1SJesse Barnes {
997b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
9989270388eSDaniel Vetter 	u8 new_delay;
9999270388eSDaniel Vetter 
1000d0ecd7e2SDaniel Vetter 	spin_lock(&mchdev_lock);
1001f97108d1SJesse Barnes 
100273edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
100373edd18fSDaniel Vetter 
100420e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
10059270388eSDaniel Vetter 
10067648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
1007b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
1008b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
1009f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
1010f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
1011f97108d1SJesse Barnes 
1012f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
1013b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
101420e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
101520e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
101620e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
101720e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
1018b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
101920e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
102020e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
102120e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
102220e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
1023f97108d1SJesse Barnes 	}
1024f97108d1SJesse Barnes 
102591d14251STvrtko Ursulin 	if (ironlake_set_drps(dev_priv, new_delay))
102620e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
1027f97108d1SJesse Barnes 
1028d0ecd7e2SDaniel Vetter 	spin_unlock(&mchdev_lock);
10299270388eSDaniel Vetter 
1030f97108d1SJesse Barnes 	return;
1031f97108d1SJesse Barnes }
1032f97108d1SJesse Barnes 
10330bc40be8STvrtko Ursulin static void notify_ring(struct intel_engine_cs *engine)
1034549f7365SChris Wilson {
103556299fb7SChris Wilson 	struct drm_i915_gem_request *rq = NULL;
103656299fb7SChris Wilson 	struct intel_wait *wait;
1037dffabc8fSTvrtko Ursulin 
10382246bea6SChris Wilson 	atomic_inc(&engine->irq_count);
1039538b257dSChris Wilson 	set_bit(ENGINE_IRQ_BREADCRUMB, &engine->irq_posted);
104056299fb7SChris Wilson 
104161d3dc70SChris Wilson 	spin_lock(&engine->breadcrumbs.irq_lock);
104261d3dc70SChris Wilson 	wait = engine->breadcrumbs.irq_wait;
104356299fb7SChris Wilson 	if (wait) {
104456299fb7SChris Wilson 		/* We use a callback from the dma-fence to submit
104556299fb7SChris Wilson 		 * requests after waiting on our own requests. To
104656299fb7SChris Wilson 		 * ensure minimum delay in queuing the next request to
104756299fb7SChris Wilson 		 * hardware, signal the fence now rather than wait for
104856299fb7SChris Wilson 		 * the signaler to be woken up. We still wake up the
104956299fb7SChris Wilson 		 * waiter in order to handle the irq-seqno coherency
105056299fb7SChris Wilson 		 * issues (we may receive the interrupt before the
105156299fb7SChris Wilson 		 * seqno is written, see __i915_request_irq_complete())
105256299fb7SChris Wilson 		 * and to handle coalescing of multiple seqno updates
105356299fb7SChris Wilson 		 * and many waiters.
105456299fb7SChris Wilson 		 */
105556299fb7SChris Wilson 		if (i915_seqno_passed(intel_engine_get_seqno(engine),
1056db93991bSChris Wilson 				      wait->seqno) &&
1057db93991bSChris Wilson 		    !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
1058db93991bSChris Wilson 			      &wait->request->fence.flags))
105924754d75SChris Wilson 			rq = i915_gem_request_get(wait->request);
106056299fb7SChris Wilson 
106156299fb7SChris Wilson 		wake_up_process(wait->tsk);
106267b807a8SChris Wilson 	} else {
106367b807a8SChris Wilson 		__intel_engine_disarm_breadcrumbs(engine);
106456299fb7SChris Wilson 	}
106561d3dc70SChris Wilson 	spin_unlock(&engine->breadcrumbs.irq_lock);
106656299fb7SChris Wilson 
106724754d75SChris Wilson 	if (rq) {
106856299fb7SChris Wilson 		dma_fence_signal(&rq->fence);
106924754d75SChris Wilson 		i915_gem_request_put(rq);
107024754d75SChris Wilson 	}
107156299fb7SChris Wilson 
107256299fb7SChris Wilson 	trace_intel_engine_notify(engine, wait);
1073549f7365SChris Wilson }
1074549f7365SChris Wilson 
107543cf3bf0SChris Wilson static void vlv_c0_read(struct drm_i915_private *dev_priv,
107643cf3bf0SChris Wilson 			struct intel_rps_ei *ei)
107731685c25SDeepak S {
1078679cb6c1SMika Kuoppala 	ei->ktime = ktime_get_raw();
107943cf3bf0SChris Wilson 	ei->render_c0 = I915_READ(VLV_RENDER_C0_COUNT);
108043cf3bf0SChris Wilson 	ei->media_c0 = I915_READ(VLV_MEDIA_C0_COUNT);
108131685c25SDeepak S }
108231685c25SDeepak S 
108343cf3bf0SChris Wilson void gen6_rps_reset_ei(struct drm_i915_private *dev_priv)
108443cf3bf0SChris Wilson {
1085e0e8c7cbSChris Wilson 	memset(&dev_priv->rps.ei, 0, sizeof(dev_priv->rps.ei));
108643cf3bf0SChris Wilson }
108743cf3bf0SChris Wilson 
108843cf3bf0SChris Wilson static u32 vlv_wa_c0_ei(struct drm_i915_private *dev_priv, u32 pm_iir)
108943cf3bf0SChris Wilson {
1090e0e8c7cbSChris Wilson 	const struct intel_rps_ei *prev = &dev_priv->rps.ei;
109143cf3bf0SChris Wilson 	struct intel_rps_ei now;
109243cf3bf0SChris Wilson 	u32 events = 0;
109343cf3bf0SChris Wilson 
1094e0e8c7cbSChris Wilson 	if ((pm_iir & GEN6_PM_RP_UP_EI_EXPIRED) == 0)
109543cf3bf0SChris Wilson 		return 0;
109643cf3bf0SChris Wilson 
109743cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &now);
109831685c25SDeepak S 
1099679cb6c1SMika Kuoppala 	if (prev->ktime) {
1100e0e8c7cbSChris Wilson 		u64 time, c0;
1101569884e3SChris Wilson 		u32 render, media;
1102e0e8c7cbSChris Wilson 
1103679cb6c1SMika Kuoppala 		time = ktime_us_delta(now.ktime, prev->ktime);
11048f68d591SChris Wilson 
1105e0e8c7cbSChris Wilson 		time *= dev_priv->czclk_freq;
1106e0e8c7cbSChris Wilson 
1107e0e8c7cbSChris Wilson 		/* Workload can be split between render + media,
1108e0e8c7cbSChris Wilson 		 * e.g. SwapBuffers being blitted in X after being rendered in
1109e0e8c7cbSChris Wilson 		 * mesa. To account for this we need to combine both engines
1110e0e8c7cbSChris Wilson 		 * into our activity counter.
1111e0e8c7cbSChris Wilson 		 */
1112569884e3SChris Wilson 		render = now.render_c0 - prev->render_c0;
1113569884e3SChris Wilson 		media = now.media_c0 - prev->media_c0;
1114569884e3SChris Wilson 		c0 = max(render, media);
11156b7f6aa7SMika Kuoppala 		c0 *= 1000 * 100 << 8; /* to usecs and scale to threshold% */
1116e0e8c7cbSChris Wilson 
1117e0e8c7cbSChris Wilson 		if (c0 > time * dev_priv->rps.up_threshold)
1118e0e8c7cbSChris Wilson 			events = GEN6_PM_RP_UP_THRESHOLD;
1119e0e8c7cbSChris Wilson 		else if (c0 < time * dev_priv->rps.down_threshold)
1120e0e8c7cbSChris Wilson 			events = GEN6_PM_RP_DOWN_THRESHOLD;
112131685c25SDeepak S 	}
112231685c25SDeepak S 
1123e0e8c7cbSChris Wilson 	dev_priv->rps.ei = now;
112443cf3bf0SChris Wilson 	return events;
112531685c25SDeepak S }
112631685c25SDeepak S 
11274912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
11283b8d8d91SJesse Barnes {
11292d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
11302d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, rps.work);
11317c0a16adSChris Wilson 	bool client_boost = false;
11328d3afd7dSChris Wilson 	int new_delay, adj, min, max;
11337c0a16adSChris Wilson 	u32 pm_iir = 0;
11343b8d8d91SJesse Barnes 
113559cdb63dSDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
11367c0a16adSChris Wilson 	if (dev_priv->rps.interrupts_enabled) {
11377c0a16adSChris Wilson 		pm_iir = fetch_and_zero(&dev_priv->rps.pm_iir);
11387b92c1bdSChris Wilson 		client_boost = atomic_read(&dev_priv->rps.num_waiters);
1139d4d70aa5SImre Deak 	}
114059cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
11414912d041SBen Widawsky 
114260611c13SPaulo Zanoni 	/* Make sure we didn't queue anything we're not going to process. */
1143a6706b45SDeepak S 	WARN_ON(pm_iir & ~dev_priv->pm_rps_events);
11448d3afd7dSChris Wilson 	if ((pm_iir & dev_priv->pm_rps_events) == 0 && !client_boost)
11457c0a16adSChris Wilson 		goto out;
11463b8d8d91SJesse Barnes 
11474fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
11487b9e0ae6SChris Wilson 
114943cf3bf0SChris Wilson 	pm_iir |= vlv_wa_c0_ei(dev_priv, pm_iir);
115043cf3bf0SChris Wilson 
1151dd75fdc8SChris Wilson 	adj = dev_priv->rps.last_adj;
1152edcf284bSChris Wilson 	new_delay = dev_priv->rps.cur_freq;
11538d3afd7dSChris Wilson 	min = dev_priv->rps.min_freq_softlimit;
11548d3afd7dSChris Wilson 	max = dev_priv->rps.max_freq_softlimit;
11557b92c1bdSChris Wilson 	if (client_boost)
115629ecd78dSChris Wilson 		max = dev_priv->rps.max_freq;
115729ecd78dSChris Wilson 	if (client_boost && new_delay < dev_priv->rps.boost_freq) {
115829ecd78dSChris Wilson 		new_delay = dev_priv->rps.boost_freq;
11598d3afd7dSChris Wilson 		adj = 0;
11608d3afd7dSChris Wilson 	} else if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
1161dd75fdc8SChris Wilson 		if (adj > 0)
1162dd75fdc8SChris Wilson 			adj *= 2;
1163edcf284bSChris Wilson 		else /* CHV needs even encode values */
1164edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? 2 : 1;
11657e79a683SSagar Arun Kamble 
11667e79a683SSagar Arun Kamble 		if (new_delay >= dev_priv->rps.max_freq_softlimit)
11677e79a683SSagar Arun Kamble 			adj = 0;
11687b92c1bdSChris Wilson 	} else if (client_boost) {
1169f5a4c67dSChris Wilson 		adj = 0;
1170dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
1171b39fb297SBen Widawsky 		if (dev_priv->rps.cur_freq > dev_priv->rps.efficient_freq)
1172b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
117317136d54SChris Wilson 		else if (dev_priv->rps.cur_freq > dev_priv->rps.min_freq_softlimit)
1174b39fb297SBen Widawsky 			new_delay = dev_priv->rps.min_freq_softlimit;
1175dd75fdc8SChris Wilson 		adj = 0;
1176dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1177dd75fdc8SChris Wilson 		if (adj < 0)
1178dd75fdc8SChris Wilson 			adj *= 2;
1179edcf284bSChris Wilson 		else /* CHV needs even encode values */
1180edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? -2 : -1;
11817e79a683SSagar Arun Kamble 
11827e79a683SSagar Arun Kamble 		if (new_delay <= dev_priv->rps.min_freq_softlimit)
11837e79a683SSagar Arun Kamble 			adj = 0;
1184dd75fdc8SChris Wilson 	} else { /* unknown event */
1185edcf284bSChris Wilson 		adj = 0;
1186dd75fdc8SChris Wilson 	}
11873b8d8d91SJesse Barnes 
1188edcf284bSChris Wilson 	dev_priv->rps.last_adj = adj;
1189edcf284bSChris Wilson 
119079249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
119179249636SBen Widawsky 	 * interrupt
119279249636SBen Widawsky 	 */
1193edcf284bSChris Wilson 	new_delay += adj;
11948d3afd7dSChris Wilson 	new_delay = clamp_t(int, new_delay, min, max);
119527544369SDeepak S 
11969fcee2f7SChris Wilson 	if (intel_set_rps(dev_priv, new_delay)) {
11979fcee2f7SChris Wilson 		DRM_DEBUG_DRIVER("Failed to set new GPU frequency\n");
11989fcee2f7SChris Wilson 		dev_priv->rps.last_adj = 0;
11999fcee2f7SChris Wilson 	}
12003b8d8d91SJesse Barnes 
12014fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
12027c0a16adSChris Wilson 
12037c0a16adSChris Wilson out:
12047c0a16adSChris Wilson 	/* Make sure not to corrupt PMIMR state used by ringbuffer on GEN6 */
12057c0a16adSChris Wilson 	spin_lock_irq(&dev_priv->irq_lock);
12067c0a16adSChris Wilson 	if (dev_priv->rps.interrupts_enabled)
12077c0a16adSChris Wilson 		gen6_unmask_pm_irq(dev_priv, dev_priv->pm_rps_events);
12087c0a16adSChris Wilson 	spin_unlock_irq(&dev_priv->irq_lock);
12093b8d8d91SJesse Barnes }
12103b8d8d91SJesse Barnes 
1211e3689190SBen Widawsky 
1212e3689190SBen Widawsky /**
1213e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
1214e3689190SBen Widawsky  * occurred.
1215e3689190SBen Widawsky  * @work: workqueue struct
1216e3689190SBen Widawsky  *
1217e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
1218e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
1219e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
1220e3689190SBen Widawsky  */
1221e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
1222e3689190SBen Widawsky {
12232d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
1224cefcff8fSJoonas Lahtinen 		container_of(work, typeof(*dev_priv), l3_parity.error_work);
1225e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
122635a85ac6SBen Widawsky 	char *parity_event[6];
1227e3689190SBen Widawsky 	uint32_t misccpctl;
122835a85ac6SBen Widawsky 	uint8_t slice = 0;
1229e3689190SBen Widawsky 
1230e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
1231e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
1232e3689190SBen Widawsky 	 * any time we access those registers.
1233e3689190SBen Widawsky 	 */
123491c8a326SChris Wilson 	mutex_lock(&dev_priv->drm.struct_mutex);
1235e3689190SBen Widawsky 
123635a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
123735a85ac6SBen Widawsky 	if (WARN_ON(!dev_priv->l3_parity.which_slice))
123835a85ac6SBen Widawsky 		goto out;
123935a85ac6SBen Widawsky 
1240e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
1241e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1242e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
1243e3689190SBen Widawsky 
124435a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
1245f0f59a00SVille Syrjälä 		i915_reg_t reg;
124635a85ac6SBen Widawsky 
124735a85ac6SBen Widawsky 		slice--;
12482d1fe073SJoonas Lahtinen 		if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv)))
124935a85ac6SBen Widawsky 			break;
125035a85ac6SBen Widawsky 
125135a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
125235a85ac6SBen Widawsky 
12536fa1c5f1SVille Syrjälä 		reg = GEN7_L3CDERRST1(slice);
125435a85ac6SBen Widawsky 
125535a85ac6SBen Widawsky 		error_status = I915_READ(reg);
1256e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
1257e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
1258e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1259e3689190SBen Widawsky 
126035a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
126135a85ac6SBen Widawsky 		POSTING_READ(reg);
1262e3689190SBen Widawsky 
1263cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1264e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1265e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1266e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
126735a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
126835a85ac6SBen Widawsky 		parity_event[5] = NULL;
1269e3689190SBen Widawsky 
127091c8a326SChris Wilson 		kobject_uevent_env(&dev_priv->drm.primary->kdev->kobj,
1271e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
1272e3689190SBen Widawsky 
127335a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
127435a85ac6SBen Widawsky 			  slice, row, bank, subbank);
1275e3689190SBen Widawsky 
127635a85ac6SBen Widawsky 		kfree(parity_event[4]);
1277e3689190SBen Widawsky 		kfree(parity_event[3]);
1278e3689190SBen Widawsky 		kfree(parity_event[2]);
1279e3689190SBen Widawsky 		kfree(parity_event[1]);
1280e3689190SBen Widawsky 	}
1281e3689190SBen Widawsky 
128235a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
128335a85ac6SBen Widawsky 
128435a85ac6SBen Widawsky out:
128535a85ac6SBen Widawsky 	WARN_ON(dev_priv->l3_parity.which_slice);
12864cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
12872d1fe073SJoonas Lahtinen 	gen5_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv));
12884cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
128935a85ac6SBen Widawsky 
129091c8a326SChris Wilson 	mutex_unlock(&dev_priv->drm.struct_mutex);
129135a85ac6SBen Widawsky }
129235a85ac6SBen Widawsky 
1293261e40b8SVille Syrjälä static void ivybridge_parity_error_irq_handler(struct drm_i915_private *dev_priv,
1294261e40b8SVille Syrjälä 					       u32 iir)
1295e3689190SBen Widawsky {
1296261e40b8SVille Syrjälä 	if (!HAS_L3_DPF(dev_priv))
1297e3689190SBen Widawsky 		return;
1298e3689190SBen Widawsky 
1299d0ecd7e2SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1300261e40b8SVille Syrjälä 	gen5_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv));
1301d0ecd7e2SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
1302e3689190SBen Widawsky 
1303261e40b8SVille Syrjälä 	iir &= GT_PARITY_ERROR(dev_priv);
130435a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
130535a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 1;
130635a85ac6SBen Widawsky 
130735a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
130835a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 0;
130935a85ac6SBen Widawsky 
1310a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
1311e3689190SBen Widawsky }
1312e3689190SBen Widawsky 
1313261e40b8SVille Syrjälä static void ilk_gt_irq_handler(struct drm_i915_private *dev_priv,
1314f1af8fc1SPaulo Zanoni 			       u32 gt_iir)
1315f1af8fc1SPaulo Zanoni {
1316f8973c21SChris Wilson 	if (gt_iir & GT_RENDER_USER_INTERRUPT)
13173b3f1650SAkash Goel 		notify_ring(dev_priv->engine[RCS]);
1318f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
13193b3f1650SAkash Goel 		notify_ring(dev_priv->engine[VCS]);
1320f1af8fc1SPaulo Zanoni }
1321f1af8fc1SPaulo Zanoni 
1322261e40b8SVille Syrjälä static void snb_gt_irq_handler(struct drm_i915_private *dev_priv,
1323e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1324e7b4c6b1SDaniel Vetter {
1325f8973c21SChris Wilson 	if (gt_iir & GT_RENDER_USER_INTERRUPT)
13263b3f1650SAkash Goel 		notify_ring(dev_priv->engine[RCS]);
1327cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
13283b3f1650SAkash Goel 		notify_ring(dev_priv->engine[VCS]);
1329cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
13303b3f1650SAkash Goel 		notify_ring(dev_priv->engine[BCS]);
1331e7b4c6b1SDaniel Vetter 
1332cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1333cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
1334aaecdf61SDaniel Vetter 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT))
1335aaecdf61SDaniel Vetter 		DRM_DEBUG("Command parser error, gt_iir 0x%08x\n", gt_iir);
1336e3689190SBen Widawsky 
1337261e40b8SVille Syrjälä 	if (gt_iir & GT_PARITY_ERROR(dev_priv))
1338261e40b8SVille Syrjälä 		ivybridge_parity_error_irq_handler(dev_priv, gt_iir);
1339e7b4c6b1SDaniel Vetter }
1340e7b4c6b1SDaniel Vetter 
13415d3d69d5SChris Wilson static void
13420bc40be8STvrtko Ursulin gen8_cs_irq_handler(struct intel_engine_cs *engine, u32 iir, int test_shift)
1343fbcc1a0cSNick Hoath {
134431de7350SChris Wilson 	bool tasklet = false;
1345f747026cSChris Wilson 
1346f747026cSChris Wilson 	if (iir & (GT_CONTEXT_SWITCH_INTERRUPT << test_shift)) {
1347a4b2b015SChris Wilson 		if (port_count(&engine->execlist_port[0])) {
1348955a4b89SChris Wilson 			__set_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
134931de7350SChris Wilson 			tasklet = true;
1350f747026cSChris Wilson 		}
1351a4b2b015SChris Wilson 	}
135231de7350SChris Wilson 
135331de7350SChris Wilson 	if (iir & (GT_RENDER_USER_INTERRUPT << test_shift)) {
135431de7350SChris Wilson 		notify_ring(engine);
135531de7350SChris Wilson 		tasklet |= i915.enable_guc_submission;
135631de7350SChris Wilson 	}
135731de7350SChris Wilson 
135831de7350SChris Wilson 	if (tasklet)
135931de7350SChris Wilson 		tasklet_hi_schedule(&engine->irq_tasklet);
1360fbcc1a0cSNick Hoath }
1361fbcc1a0cSNick Hoath 
1362e30e251aSVille Syrjälä static irqreturn_t gen8_gt_irq_ack(struct drm_i915_private *dev_priv,
1363e30e251aSVille Syrjälä 				   u32 master_ctl,
1364e30e251aSVille Syrjälä 				   u32 gt_iir[4])
1365abd58f01SBen Widawsky {
1366abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
1367abd58f01SBen Widawsky 
1368abd58f01SBen Widawsky 	if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
1369e30e251aSVille Syrjälä 		gt_iir[0] = I915_READ_FW(GEN8_GT_IIR(0));
1370e30e251aSVille Syrjälä 		if (gt_iir[0]) {
1371e30e251aSVille Syrjälä 			I915_WRITE_FW(GEN8_GT_IIR(0), gt_iir[0]);
1372abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1373abd58f01SBen Widawsky 		} else
1374abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT0)!\n");
1375abd58f01SBen Widawsky 	}
1376abd58f01SBen Widawsky 
137785f9b5f9SZhao Yakui 	if (master_ctl & (GEN8_GT_VCS1_IRQ | GEN8_GT_VCS2_IRQ)) {
1378e30e251aSVille Syrjälä 		gt_iir[1] = I915_READ_FW(GEN8_GT_IIR(1));
1379e30e251aSVille Syrjälä 		if (gt_iir[1]) {
1380e30e251aSVille Syrjälä 			I915_WRITE_FW(GEN8_GT_IIR(1), gt_iir[1]);
1381abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1382abd58f01SBen Widawsky 		} else
1383abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT1)!\n");
1384abd58f01SBen Widawsky 	}
1385abd58f01SBen Widawsky 
138674cdb337SChris Wilson 	if (master_ctl & GEN8_GT_VECS_IRQ) {
1387e30e251aSVille Syrjälä 		gt_iir[3] = I915_READ_FW(GEN8_GT_IIR(3));
1388e30e251aSVille Syrjälä 		if (gt_iir[3]) {
1389e30e251aSVille Syrjälä 			I915_WRITE_FW(GEN8_GT_IIR(3), gt_iir[3]);
139074cdb337SChris Wilson 			ret = IRQ_HANDLED;
139174cdb337SChris Wilson 		} else
139274cdb337SChris Wilson 			DRM_ERROR("The master control interrupt lied (GT3)!\n");
139374cdb337SChris Wilson 	}
139474cdb337SChris Wilson 
139526705e20SSagar Arun Kamble 	if (master_ctl & (GEN8_GT_PM_IRQ | GEN8_GT_GUC_IRQ)) {
1396e30e251aSVille Syrjälä 		gt_iir[2] = I915_READ_FW(GEN8_GT_IIR(2));
139726705e20SSagar Arun Kamble 		if (gt_iir[2] & (dev_priv->pm_rps_events |
139826705e20SSagar Arun Kamble 				 dev_priv->pm_guc_events)) {
1399cb0d205eSChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(2),
140026705e20SSagar Arun Kamble 				      gt_iir[2] & (dev_priv->pm_rps_events |
140126705e20SSagar Arun Kamble 						   dev_priv->pm_guc_events));
140238cc46d7SOscar Mateo 			ret = IRQ_HANDLED;
14030961021aSBen Widawsky 		} else
14040961021aSBen Widawsky 			DRM_ERROR("The master control interrupt lied (PM)!\n");
14050961021aSBen Widawsky 	}
14060961021aSBen Widawsky 
1407abd58f01SBen Widawsky 	return ret;
1408abd58f01SBen Widawsky }
1409abd58f01SBen Widawsky 
1410e30e251aSVille Syrjälä static void gen8_gt_irq_handler(struct drm_i915_private *dev_priv,
1411e30e251aSVille Syrjälä 				u32 gt_iir[4])
1412e30e251aSVille Syrjälä {
1413e30e251aSVille Syrjälä 	if (gt_iir[0]) {
14143b3f1650SAkash Goel 		gen8_cs_irq_handler(dev_priv->engine[RCS],
1415e30e251aSVille Syrjälä 				    gt_iir[0], GEN8_RCS_IRQ_SHIFT);
14163b3f1650SAkash Goel 		gen8_cs_irq_handler(dev_priv->engine[BCS],
1417e30e251aSVille Syrjälä 				    gt_iir[0], GEN8_BCS_IRQ_SHIFT);
1418e30e251aSVille Syrjälä 	}
1419e30e251aSVille Syrjälä 
1420e30e251aSVille Syrjälä 	if (gt_iir[1]) {
14213b3f1650SAkash Goel 		gen8_cs_irq_handler(dev_priv->engine[VCS],
1422e30e251aSVille Syrjälä 				    gt_iir[1], GEN8_VCS1_IRQ_SHIFT);
14233b3f1650SAkash Goel 		gen8_cs_irq_handler(dev_priv->engine[VCS2],
1424e30e251aSVille Syrjälä 				    gt_iir[1], GEN8_VCS2_IRQ_SHIFT);
1425e30e251aSVille Syrjälä 	}
1426e30e251aSVille Syrjälä 
1427e30e251aSVille Syrjälä 	if (gt_iir[3])
14283b3f1650SAkash Goel 		gen8_cs_irq_handler(dev_priv->engine[VECS],
1429e30e251aSVille Syrjälä 				    gt_iir[3], GEN8_VECS_IRQ_SHIFT);
1430e30e251aSVille Syrjälä 
1431e30e251aSVille Syrjälä 	if (gt_iir[2] & dev_priv->pm_rps_events)
1432e30e251aSVille Syrjälä 		gen6_rps_irq_handler(dev_priv, gt_iir[2]);
143326705e20SSagar Arun Kamble 
143426705e20SSagar Arun Kamble 	if (gt_iir[2] & dev_priv->pm_guc_events)
143526705e20SSagar Arun Kamble 		gen9_guc_irq_handler(dev_priv, gt_iir[2]);
1436e30e251aSVille Syrjälä }
1437e30e251aSVille Syrjälä 
143863c88d22SImre Deak static bool bxt_port_hotplug_long_detect(enum port port, u32 val)
143963c88d22SImre Deak {
144063c88d22SImre Deak 	switch (port) {
144163c88d22SImre Deak 	case PORT_A:
1442195baa06SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
144363c88d22SImre Deak 	case PORT_B:
144463c88d22SImre Deak 		return val & PORTB_HOTPLUG_LONG_DETECT;
144563c88d22SImre Deak 	case PORT_C:
144663c88d22SImre Deak 		return val & PORTC_HOTPLUG_LONG_DETECT;
144763c88d22SImre Deak 	default:
144863c88d22SImre Deak 		return false;
144963c88d22SImre Deak 	}
145063c88d22SImre Deak }
145163c88d22SImre Deak 
14526dbf30ceSVille Syrjälä static bool spt_port_hotplug2_long_detect(enum port port, u32 val)
14536dbf30ceSVille Syrjälä {
14546dbf30ceSVille Syrjälä 	switch (port) {
14556dbf30ceSVille Syrjälä 	case PORT_E:
14566dbf30ceSVille Syrjälä 		return val & PORTE_HOTPLUG_LONG_DETECT;
14576dbf30ceSVille Syrjälä 	default:
14586dbf30ceSVille Syrjälä 		return false;
14596dbf30ceSVille Syrjälä 	}
14606dbf30ceSVille Syrjälä }
14616dbf30ceSVille Syrjälä 
146274c0b395SVille Syrjälä static bool spt_port_hotplug_long_detect(enum port port, u32 val)
146374c0b395SVille Syrjälä {
146474c0b395SVille Syrjälä 	switch (port) {
146574c0b395SVille Syrjälä 	case PORT_A:
146674c0b395SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
146774c0b395SVille Syrjälä 	case PORT_B:
146874c0b395SVille Syrjälä 		return val & PORTB_HOTPLUG_LONG_DETECT;
146974c0b395SVille Syrjälä 	case PORT_C:
147074c0b395SVille Syrjälä 		return val & PORTC_HOTPLUG_LONG_DETECT;
147174c0b395SVille Syrjälä 	case PORT_D:
147274c0b395SVille Syrjälä 		return val & PORTD_HOTPLUG_LONG_DETECT;
147374c0b395SVille Syrjälä 	default:
147474c0b395SVille Syrjälä 		return false;
147574c0b395SVille Syrjälä 	}
147674c0b395SVille Syrjälä }
147774c0b395SVille Syrjälä 
1478e4ce95aaSVille Syrjälä static bool ilk_port_hotplug_long_detect(enum port port, u32 val)
1479e4ce95aaSVille Syrjälä {
1480e4ce95aaSVille Syrjälä 	switch (port) {
1481e4ce95aaSVille Syrjälä 	case PORT_A:
1482e4ce95aaSVille Syrjälä 		return val & DIGITAL_PORTA_HOTPLUG_LONG_DETECT;
1483e4ce95aaSVille Syrjälä 	default:
1484e4ce95aaSVille Syrjälä 		return false;
1485e4ce95aaSVille Syrjälä 	}
1486e4ce95aaSVille Syrjälä }
1487e4ce95aaSVille Syrjälä 
1488676574dfSJani Nikula static bool pch_port_hotplug_long_detect(enum port port, u32 val)
148913cf5504SDave Airlie {
149013cf5504SDave Airlie 	switch (port) {
149113cf5504SDave Airlie 	case PORT_B:
1492676574dfSJani Nikula 		return val & PORTB_HOTPLUG_LONG_DETECT;
149313cf5504SDave Airlie 	case PORT_C:
1494676574dfSJani Nikula 		return val & PORTC_HOTPLUG_LONG_DETECT;
149513cf5504SDave Airlie 	case PORT_D:
1496676574dfSJani Nikula 		return val & PORTD_HOTPLUG_LONG_DETECT;
1497676574dfSJani Nikula 	default:
1498676574dfSJani Nikula 		return false;
149913cf5504SDave Airlie 	}
150013cf5504SDave Airlie }
150113cf5504SDave Airlie 
1502676574dfSJani Nikula static bool i9xx_port_hotplug_long_detect(enum port port, u32 val)
150313cf5504SDave Airlie {
150413cf5504SDave Airlie 	switch (port) {
150513cf5504SDave Airlie 	case PORT_B:
1506676574dfSJani Nikula 		return val & PORTB_HOTPLUG_INT_LONG_PULSE;
150713cf5504SDave Airlie 	case PORT_C:
1508676574dfSJani Nikula 		return val & PORTC_HOTPLUG_INT_LONG_PULSE;
150913cf5504SDave Airlie 	case PORT_D:
1510676574dfSJani Nikula 		return val & PORTD_HOTPLUG_INT_LONG_PULSE;
1511676574dfSJani Nikula 	default:
1512676574dfSJani Nikula 		return false;
151313cf5504SDave Airlie 	}
151413cf5504SDave Airlie }
151513cf5504SDave Airlie 
151642db67d6SVille Syrjälä /*
151742db67d6SVille Syrjälä  * Get a bit mask of pins that have triggered, and which ones may be long.
151842db67d6SVille Syrjälä  * This can be called multiple times with the same masks to accumulate
151942db67d6SVille Syrjälä  * hotplug detection results from several registers.
152042db67d6SVille Syrjälä  *
152142db67d6SVille Syrjälä  * Note that the caller is expected to zero out the masks initially.
152242db67d6SVille Syrjälä  */
1523fd63e2a9SImre Deak static void intel_get_hpd_pins(u32 *pin_mask, u32 *long_mask,
15248c841e57SJani Nikula 			     u32 hotplug_trigger, u32 dig_hotplug_reg,
1525fd63e2a9SImre Deak 			     const u32 hpd[HPD_NUM_PINS],
1526fd63e2a9SImre Deak 			     bool long_pulse_detect(enum port port, u32 val))
1527676574dfSJani Nikula {
15288c841e57SJani Nikula 	enum port port;
1529676574dfSJani Nikula 	int i;
1530676574dfSJani Nikula 
1531676574dfSJani Nikula 	for_each_hpd_pin(i) {
15328c841e57SJani Nikula 		if ((hpd[i] & hotplug_trigger) == 0)
15338c841e57SJani Nikula 			continue;
15348c841e57SJani Nikula 
1535676574dfSJani Nikula 		*pin_mask |= BIT(i);
1536676574dfSJani Nikula 
1537256cfddeSRodrigo Vivi 		port = intel_hpd_pin_to_port(i);
1538256cfddeSRodrigo Vivi 		if (port == PORT_NONE)
1539cc24fcdcSImre Deak 			continue;
1540cc24fcdcSImre Deak 
1541fd63e2a9SImre Deak 		if (long_pulse_detect(port, dig_hotplug_reg))
1542676574dfSJani Nikula 			*long_mask |= BIT(i);
1543676574dfSJani Nikula 	}
1544676574dfSJani Nikula 
1545676574dfSJani Nikula 	DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x, dig 0x%08x, pins 0x%08x\n",
1546676574dfSJani Nikula 			 hotplug_trigger, dig_hotplug_reg, *pin_mask);
1547676574dfSJani Nikula 
1548676574dfSJani Nikula }
1549676574dfSJani Nikula 
155091d14251STvrtko Ursulin static void gmbus_irq_handler(struct drm_i915_private *dev_priv)
1551515ac2bbSDaniel Vetter {
155228c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1553515ac2bbSDaniel Vetter }
1554515ac2bbSDaniel Vetter 
155591d14251STvrtko Ursulin static void dp_aux_irq_handler(struct drm_i915_private *dev_priv)
1556ce99c256SDaniel Vetter {
15579ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1558ce99c256SDaniel Vetter }
1559ce99c256SDaniel Vetter 
15608bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
156191d14251STvrtko Ursulin static void display_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
156291d14251STvrtko Ursulin 					 enum pipe pipe,
1563eba94eb9SDaniel Vetter 					 uint32_t crc0, uint32_t crc1,
1564eba94eb9SDaniel Vetter 					 uint32_t crc2, uint32_t crc3,
15658bc5e955SDaniel Vetter 					 uint32_t crc4)
15668bf1e9f1SShuang He {
15678bf1e9f1SShuang He 	struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
15688bf1e9f1SShuang He 	struct intel_pipe_crc_entry *entry;
15698c6b709dSTomeu Vizoso 	struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
15708c6b709dSTomeu Vizoso 	struct drm_driver *driver = dev_priv->drm.driver;
15718c6b709dSTomeu Vizoso 	uint32_t crcs[5];
1572ac2300d4SDamien Lespiau 	int head, tail;
1573b2c88f5bSDamien Lespiau 
1574d538bbdfSDamien Lespiau 	spin_lock(&pipe_crc->lock);
15758c6b709dSTomeu Vizoso 	if (pipe_crc->source) {
15760c912c79SDamien Lespiau 		if (!pipe_crc->entries) {
1577d538bbdfSDamien Lespiau 			spin_unlock(&pipe_crc->lock);
157834273620SDaniel Vetter 			DRM_DEBUG_KMS("spurious interrupt\n");
15790c912c79SDamien Lespiau 			return;
15800c912c79SDamien Lespiau 		}
15810c912c79SDamien Lespiau 
1582d538bbdfSDamien Lespiau 		head = pipe_crc->head;
1583d538bbdfSDamien Lespiau 		tail = pipe_crc->tail;
1584b2c88f5bSDamien Lespiau 
1585b2c88f5bSDamien Lespiau 		if (CIRC_SPACE(head, tail, INTEL_PIPE_CRC_ENTRIES_NR) < 1) {
1586d538bbdfSDamien Lespiau 			spin_unlock(&pipe_crc->lock);
1587b2c88f5bSDamien Lespiau 			DRM_ERROR("CRC buffer overflowing\n");
1588b2c88f5bSDamien Lespiau 			return;
1589b2c88f5bSDamien Lespiau 		}
1590b2c88f5bSDamien Lespiau 
1591b2c88f5bSDamien Lespiau 		entry = &pipe_crc->entries[head];
15928bf1e9f1SShuang He 
15938c6b709dSTomeu Vizoso 		entry->frame = driver->get_vblank_counter(&dev_priv->drm, pipe);
1594eba94eb9SDaniel Vetter 		entry->crc[0] = crc0;
1595eba94eb9SDaniel Vetter 		entry->crc[1] = crc1;
1596eba94eb9SDaniel Vetter 		entry->crc[2] = crc2;
1597eba94eb9SDaniel Vetter 		entry->crc[3] = crc3;
1598eba94eb9SDaniel Vetter 		entry->crc[4] = crc4;
1599b2c88f5bSDamien Lespiau 
1600b2c88f5bSDamien Lespiau 		head = (head + 1) & (INTEL_PIPE_CRC_ENTRIES_NR - 1);
1601d538bbdfSDamien Lespiau 		pipe_crc->head = head;
1602d538bbdfSDamien Lespiau 
1603d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
160407144428SDamien Lespiau 
160507144428SDamien Lespiau 		wake_up_interruptible(&pipe_crc->wq);
16068c6b709dSTomeu Vizoso 	} else {
16078c6b709dSTomeu Vizoso 		/*
16088c6b709dSTomeu Vizoso 		 * For some not yet identified reason, the first CRC is
16098c6b709dSTomeu Vizoso 		 * bonkers. So let's just wait for the next vblank and read
16108c6b709dSTomeu Vizoso 		 * out the buggy result.
16118c6b709dSTomeu Vizoso 		 *
16128c6b709dSTomeu Vizoso 		 * On CHV sometimes the second CRC is bonkers as well, so
16138c6b709dSTomeu Vizoso 		 * don't trust that one either.
16148c6b709dSTomeu Vizoso 		 */
16158c6b709dSTomeu Vizoso 		if (pipe_crc->skipped == 0 ||
16168c6b709dSTomeu Vizoso 		    (IS_CHERRYVIEW(dev_priv) && pipe_crc->skipped == 1)) {
16178c6b709dSTomeu Vizoso 			pipe_crc->skipped++;
16188c6b709dSTomeu Vizoso 			spin_unlock(&pipe_crc->lock);
16198c6b709dSTomeu Vizoso 			return;
16208c6b709dSTomeu Vizoso 		}
16218c6b709dSTomeu Vizoso 		spin_unlock(&pipe_crc->lock);
16228c6b709dSTomeu Vizoso 		crcs[0] = crc0;
16238c6b709dSTomeu Vizoso 		crcs[1] = crc1;
16248c6b709dSTomeu Vizoso 		crcs[2] = crc2;
16258c6b709dSTomeu Vizoso 		crcs[3] = crc3;
16268c6b709dSTomeu Vizoso 		crcs[4] = crc4;
1627246ee524STomeu Vizoso 		drm_crtc_add_crc_entry(&crtc->base, true,
1628ca814b25SDaniel Vetter 				       drm_crtc_accurate_vblank_count(&crtc->base),
1629246ee524STomeu Vizoso 				       crcs);
16308c6b709dSTomeu Vizoso 	}
16318bf1e9f1SShuang He }
1632277de95eSDaniel Vetter #else
1633277de95eSDaniel Vetter static inline void
163491d14251STvrtko Ursulin display_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
163591d14251STvrtko Ursulin 			     enum pipe pipe,
1636277de95eSDaniel Vetter 			     uint32_t crc0, uint32_t crc1,
1637277de95eSDaniel Vetter 			     uint32_t crc2, uint32_t crc3,
1638277de95eSDaniel Vetter 			     uint32_t crc4) {}
1639277de95eSDaniel Vetter #endif
1640eba94eb9SDaniel Vetter 
1641277de95eSDaniel Vetter 
164291d14251STvrtko Ursulin static void hsw_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
164391d14251STvrtko Ursulin 				     enum pipe pipe)
16445a69b89fSDaniel Vetter {
164591d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
16465a69b89fSDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
16475a69b89fSDaniel Vetter 				     0, 0, 0, 0);
16485a69b89fSDaniel Vetter }
16495a69b89fSDaniel Vetter 
165091d14251STvrtko Ursulin static void ivb_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
165191d14251STvrtko Ursulin 				     enum pipe pipe)
1652eba94eb9SDaniel Vetter {
165391d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
1654eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1655eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1656eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1657eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
16588bc5e955SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1659eba94eb9SDaniel Vetter }
16605b3a856bSDaniel Vetter 
166191d14251STvrtko Ursulin static void i9xx_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
166291d14251STvrtko Ursulin 				      enum pipe pipe)
16635b3a856bSDaniel Vetter {
16640b5c5ed0SDaniel Vetter 	uint32_t res1, res2;
16650b5c5ed0SDaniel Vetter 
166691d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 3)
16670b5c5ed0SDaniel Vetter 		res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
16680b5c5ed0SDaniel Vetter 	else
16690b5c5ed0SDaniel Vetter 		res1 = 0;
16700b5c5ed0SDaniel Vetter 
167191d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
16720b5c5ed0SDaniel Vetter 		res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
16730b5c5ed0SDaniel Vetter 	else
16740b5c5ed0SDaniel Vetter 		res2 = 0;
16755b3a856bSDaniel Vetter 
167691d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
16770b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_RED(pipe)),
16780b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_GREEN(pipe)),
16790b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_BLUE(pipe)),
16800b5c5ed0SDaniel Vetter 				     res1, res2);
16815b3a856bSDaniel Vetter }
16828bf1e9f1SShuang He 
16831403c0d4SPaulo Zanoni /* The RPS events need forcewake, so we add them to a work queue and mask their
16841403c0d4SPaulo Zanoni  * IMR bits until the work is done. Other interrupts can be processed without
16851403c0d4SPaulo Zanoni  * the work queue. */
16861403c0d4SPaulo Zanoni static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1687baf02a1fSBen Widawsky {
1688a6706b45SDeepak S 	if (pm_iir & dev_priv->pm_rps_events) {
168959cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
1690f4e9af4fSAkash Goel 		gen6_mask_pm_irq(dev_priv, pm_iir & dev_priv->pm_rps_events);
1691d4d70aa5SImre Deak 		if (dev_priv->rps.interrupts_enabled) {
1692d4d70aa5SImre Deak 			dev_priv->rps.pm_iir |= pm_iir & dev_priv->pm_rps_events;
1693c33d247dSChris Wilson 			schedule_work(&dev_priv->rps.work);
169441a05a3aSDaniel Vetter 		}
1695d4d70aa5SImre Deak 		spin_unlock(&dev_priv->irq_lock);
1696d4d70aa5SImre Deak 	}
1697baf02a1fSBen Widawsky 
1698bca2bf2aSPandiyan, Dhinakaran 	if (INTEL_GEN(dev_priv) >= 8)
1699c9a9a268SImre Deak 		return;
1700c9a9a268SImre Deak 
17012d1fe073SJoonas Lahtinen 	if (HAS_VEBOX(dev_priv)) {
170212638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
17033b3f1650SAkash Goel 			notify_ring(dev_priv->engine[VECS]);
170412638c57SBen Widawsky 
1705aaecdf61SDaniel Vetter 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT)
1706aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, pm_iir 0x%08x\n", pm_iir);
170712638c57SBen Widawsky 	}
17081403c0d4SPaulo Zanoni }
1709baf02a1fSBen Widawsky 
171026705e20SSagar Arun Kamble static void gen9_guc_irq_handler(struct drm_i915_private *dev_priv, u32 gt_iir)
171126705e20SSagar Arun Kamble {
171226705e20SSagar Arun Kamble 	if (gt_iir & GEN9_GUC_TO_HOST_INT_EVENT) {
17134100b2abSSagar Arun Kamble 		/* Sample the log buffer flush related bits & clear them out now
17144100b2abSSagar Arun Kamble 		 * itself from the message identity register to minimize the
17154100b2abSSagar Arun Kamble 		 * probability of losing a flush interrupt, when there are back
17164100b2abSSagar Arun Kamble 		 * to back flush interrupts.
17174100b2abSSagar Arun Kamble 		 * There can be a new flush interrupt, for different log buffer
17184100b2abSSagar Arun Kamble 		 * type (like for ISR), whilst Host is handling one (for DPC).
17194100b2abSSagar Arun Kamble 		 * Since same bit is used in message register for ISR & DPC, it
17204100b2abSSagar Arun Kamble 		 * could happen that GuC sets the bit for 2nd interrupt but Host
17214100b2abSSagar Arun Kamble 		 * clears out the bit on handling the 1st interrupt.
17224100b2abSSagar Arun Kamble 		 */
17234100b2abSSagar Arun Kamble 		u32 msg, flush;
17244100b2abSSagar Arun Kamble 
17254100b2abSSagar Arun Kamble 		msg = I915_READ(SOFT_SCRATCH(15));
1726a80bc45fSArkadiusz Hiler 		flush = msg & (INTEL_GUC_RECV_MSG_CRASH_DUMP_POSTED |
1727a80bc45fSArkadiusz Hiler 			       INTEL_GUC_RECV_MSG_FLUSH_LOG_BUFFER);
17284100b2abSSagar Arun Kamble 		if (flush) {
17294100b2abSSagar Arun Kamble 			/* Clear the message bits that are handled */
17304100b2abSSagar Arun Kamble 			I915_WRITE(SOFT_SCRATCH(15), msg & ~flush);
17314100b2abSSagar Arun Kamble 
17324100b2abSSagar Arun Kamble 			/* Handle flush interrupt in bottom half */
1733e7465473SOscar Mateo 			queue_work(dev_priv->guc.log.runtime.flush_wq,
1734e7465473SOscar Mateo 				   &dev_priv->guc.log.runtime.flush_work);
17355aa1ee4bSAkash Goel 
17365aa1ee4bSAkash Goel 			dev_priv->guc.log.flush_interrupt_count++;
17374100b2abSSagar Arun Kamble 		} else {
17384100b2abSSagar Arun Kamble 			/* Not clearing of unhandled event bits won't result in
17394100b2abSSagar Arun Kamble 			 * re-triggering of the interrupt.
17404100b2abSSagar Arun Kamble 			 */
17414100b2abSSagar Arun Kamble 		}
174226705e20SSagar Arun Kamble 	}
174326705e20SSagar Arun Kamble }
174426705e20SSagar Arun Kamble 
174544d9241eSVille Syrjälä static void i9xx_pipestat_irq_reset(struct drm_i915_private *dev_priv)
174644d9241eSVille Syrjälä {
174744d9241eSVille Syrjälä 	enum pipe pipe;
174844d9241eSVille Syrjälä 
174944d9241eSVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
175044d9241eSVille Syrjälä 		I915_WRITE(PIPESTAT(pipe),
175144d9241eSVille Syrjälä 			   PIPESTAT_INT_STATUS_MASK |
175244d9241eSVille Syrjälä 			   PIPE_FIFO_UNDERRUN_STATUS);
175344d9241eSVille Syrjälä 
175444d9241eSVille Syrjälä 		dev_priv->pipestat_irq_mask[pipe] = 0;
175544d9241eSVille Syrjälä 	}
175644d9241eSVille Syrjälä }
175744d9241eSVille Syrjälä 
175891d14251STvrtko Ursulin static void valleyview_pipestat_irq_ack(struct drm_i915_private *dev_priv,
175991d14251STvrtko Ursulin 					u32 iir, u32 pipe_stats[I915_MAX_PIPES])
17607e231dbeSJesse Barnes {
17617e231dbeSJesse Barnes 	int pipe;
17627e231dbeSJesse Barnes 
176358ead0d7SImre Deak 	spin_lock(&dev_priv->irq_lock);
17641ca993d2SVille Syrjälä 
17651ca993d2SVille Syrjälä 	if (!dev_priv->display_irqs_enabled) {
17661ca993d2SVille Syrjälä 		spin_unlock(&dev_priv->irq_lock);
17671ca993d2SVille Syrjälä 		return;
17681ca993d2SVille Syrjälä 	}
17691ca993d2SVille Syrjälä 
1770055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1771f0f59a00SVille Syrjälä 		i915_reg_t reg;
1772bbb5eebfSDaniel Vetter 		u32 mask, iir_bit = 0;
177391d181ddSImre Deak 
1774bbb5eebfSDaniel Vetter 		/*
1775bbb5eebfSDaniel Vetter 		 * PIPESTAT bits get signalled even when the interrupt is
1776bbb5eebfSDaniel Vetter 		 * disabled with the mask bits, and some of the status bits do
1777bbb5eebfSDaniel Vetter 		 * not generate interrupts at all (like the underrun bit). Hence
1778bbb5eebfSDaniel Vetter 		 * we need to be careful that we only handle what we want to
1779bbb5eebfSDaniel Vetter 		 * handle.
1780bbb5eebfSDaniel Vetter 		 */
17810f239f4cSDaniel Vetter 
17820f239f4cSDaniel Vetter 		/* fifo underruns are filterered in the underrun handler. */
17830f239f4cSDaniel Vetter 		mask = PIPE_FIFO_UNDERRUN_STATUS;
1784bbb5eebfSDaniel Vetter 
1785bbb5eebfSDaniel Vetter 		switch (pipe) {
1786bbb5eebfSDaniel Vetter 		case PIPE_A:
1787bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
1788bbb5eebfSDaniel Vetter 			break;
1789bbb5eebfSDaniel Vetter 		case PIPE_B:
1790bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
1791bbb5eebfSDaniel Vetter 			break;
17923278f67fSVille Syrjälä 		case PIPE_C:
17933278f67fSVille Syrjälä 			iir_bit = I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
17943278f67fSVille Syrjälä 			break;
1795bbb5eebfSDaniel Vetter 		}
1796bbb5eebfSDaniel Vetter 		if (iir & iir_bit)
1797bbb5eebfSDaniel Vetter 			mask |= dev_priv->pipestat_irq_mask[pipe];
1798bbb5eebfSDaniel Vetter 
1799bbb5eebfSDaniel Vetter 		if (!mask)
180091d181ddSImre Deak 			continue;
180191d181ddSImre Deak 
180291d181ddSImre Deak 		reg = PIPESTAT(pipe);
1803bbb5eebfSDaniel Vetter 		mask |= PIPESTAT_INT_ENABLE_MASK;
1804bbb5eebfSDaniel Vetter 		pipe_stats[pipe] = I915_READ(reg) & mask;
18057e231dbeSJesse Barnes 
18067e231dbeSJesse Barnes 		/*
18077e231dbeSJesse Barnes 		 * Clear the PIPE*STAT regs before the IIR
18087e231dbeSJesse Barnes 		 */
180991d181ddSImre Deak 		if (pipe_stats[pipe] & (PIPE_FIFO_UNDERRUN_STATUS |
181091d181ddSImre Deak 					PIPESTAT_INT_STATUS_MASK))
18117e231dbeSJesse Barnes 			I915_WRITE(reg, pipe_stats[pipe]);
18127e231dbeSJesse Barnes 	}
181358ead0d7SImre Deak 	spin_unlock(&dev_priv->irq_lock);
18142ecb8ca4SVille Syrjälä }
18152ecb8ca4SVille Syrjälä 
181691d14251STvrtko Ursulin static void valleyview_pipestat_irq_handler(struct drm_i915_private *dev_priv,
18172ecb8ca4SVille Syrjälä 					    u32 pipe_stats[I915_MAX_PIPES])
18182ecb8ca4SVille Syrjälä {
18192ecb8ca4SVille Syrjälä 	enum pipe pipe;
18207e231dbeSJesse Barnes 
1821055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1822fd3a4024SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS)
1823fd3a4024SDaniel Vetter 			drm_handle_vblank(&dev_priv->drm, pipe);
18244356d586SDaniel Vetter 
18254356d586SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
182691d14251STvrtko Ursulin 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
18272d9d2b0bSVille Syrjälä 
18281f7247c0SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
18291f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
183031acc7f5SJesse Barnes 	}
183131acc7f5SJesse Barnes 
1832c1874ed7SImre Deak 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
183391d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
1834c1874ed7SImre Deak }
1835c1874ed7SImre Deak 
18361ae3c34cSVille Syrjälä static u32 i9xx_hpd_irq_ack(struct drm_i915_private *dev_priv)
183716c6c56bSVille Syrjälä {
183816c6c56bSVille Syrjälä 	u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
183916c6c56bSVille Syrjälä 
18401ae3c34cSVille Syrjälä 	if (hotplug_status)
18413ff60f89SOscar Mateo 		I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
18421ae3c34cSVille Syrjälä 
18431ae3c34cSVille Syrjälä 	return hotplug_status;
18441ae3c34cSVille Syrjälä }
18451ae3c34cSVille Syrjälä 
184691d14251STvrtko Ursulin static void i9xx_hpd_irq_handler(struct drm_i915_private *dev_priv,
18471ae3c34cSVille Syrjälä 				 u32 hotplug_status)
18481ae3c34cSVille Syrjälä {
18491ae3c34cSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
18503ff60f89SOscar Mateo 
185191d14251STvrtko Ursulin 	if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
185291d14251STvrtko Ursulin 	    IS_CHERRYVIEW(dev_priv)) {
185316c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
185416c6c56bSVille Syrjälä 
185558f2cf24SVille Syrjälä 		if (hotplug_trigger) {
1856fd63e2a9SImre Deak 			intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
1857fd63e2a9SImre Deak 					   hotplug_trigger, hpd_status_g4x,
1858fd63e2a9SImre Deak 					   i9xx_port_hotplug_long_detect);
185958f2cf24SVille Syrjälä 
186091d14251STvrtko Ursulin 			intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
186158f2cf24SVille Syrjälä 		}
1862369712e8SJani Nikula 
1863369712e8SJani Nikula 		if (hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
186491d14251STvrtko Ursulin 			dp_aux_irq_handler(dev_priv);
186516c6c56bSVille Syrjälä 	} else {
186616c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
186716c6c56bSVille Syrjälä 
186858f2cf24SVille Syrjälä 		if (hotplug_trigger) {
1869fd63e2a9SImre Deak 			intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
18704e3d1e26SVille Syrjälä 					   hotplug_trigger, hpd_status_i915,
1871fd63e2a9SImre Deak 					   i9xx_port_hotplug_long_detect);
187291d14251STvrtko Ursulin 			intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
187316c6c56bSVille Syrjälä 		}
18743ff60f89SOscar Mateo 	}
187558f2cf24SVille Syrjälä }
187616c6c56bSVille Syrjälä 
1877c1874ed7SImre Deak static irqreturn_t valleyview_irq_handler(int irq, void *arg)
1878c1874ed7SImre Deak {
187945a83f84SDaniel Vetter 	struct drm_device *dev = arg;
1880fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
1881c1874ed7SImre Deak 	irqreturn_t ret = IRQ_NONE;
1882c1874ed7SImre Deak 
18832dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
18842dd2a883SImre Deak 		return IRQ_NONE;
18852dd2a883SImre Deak 
18861f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
18871f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
18881f814dacSImre Deak 
18891e1cace9SVille Syrjälä 	do {
18906e814800SVille Syrjälä 		u32 iir, gt_iir, pm_iir;
18912ecb8ca4SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
18921ae3c34cSVille Syrjälä 		u32 hotplug_status = 0;
1893a5e485a9SVille Syrjälä 		u32 ier = 0;
18943ff60f89SOscar Mateo 
1895c1874ed7SImre Deak 		gt_iir = I915_READ(GTIIR);
1896c1874ed7SImre Deak 		pm_iir = I915_READ(GEN6_PMIIR);
18973ff60f89SOscar Mateo 		iir = I915_READ(VLV_IIR);
1898c1874ed7SImre Deak 
1899c1874ed7SImre Deak 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
19001e1cace9SVille Syrjälä 			break;
1901c1874ed7SImre Deak 
1902c1874ed7SImre Deak 		ret = IRQ_HANDLED;
1903c1874ed7SImre Deak 
1904a5e485a9SVille Syrjälä 		/*
1905a5e485a9SVille Syrjälä 		 * Theory on interrupt generation, based on empirical evidence:
1906a5e485a9SVille Syrjälä 		 *
1907a5e485a9SVille Syrjälä 		 * x = ((VLV_IIR & VLV_IER) ||
1908a5e485a9SVille Syrjälä 		 *      (((GT_IIR & GT_IER) || (GEN6_PMIIR & GEN6_PMIER)) &&
1909a5e485a9SVille Syrjälä 		 *       (VLV_MASTER_IER & MASTER_INTERRUPT_ENABLE)));
1910a5e485a9SVille Syrjälä 		 *
1911a5e485a9SVille Syrjälä 		 * A CPU interrupt will only be raised when 'x' has a 0->1 edge.
1912a5e485a9SVille Syrjälä 		 * Hence we clear MASTER_INTERRUPT_ENABLE and VLV_IER to
1913a5e485a9SVille Syrjälä 		 * guarantee the CPU interrupt will be raised again even if we
1914a5e485a9SVille Syrjälä 		 * don't end up clearing all the VLV_IIR, GT_IIR, GEN6_PMIIR
1915a5e485a9SVille Syrjälä 		 * bits this time around.
1916a5e485a9SVille Syrjälä 		 */
19174a0a0202SVille Syrjälä 		I915_WRITE(VLV_MASTER_IER, 0);
1918a5e485a9SVille Syrjälä 		ier = I915_READ(VLV_IER);
1919a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, 0);
19204a0a0202SVille Syrjälä 
19214a0a0202SVille Syrjälä 		if (gt_iir)
19224a0a0202SVille Syrjälä 			I915_WRITE(GTIIR, gt_iir);
19234a0a0202SVille Syrjälä 		if (pm_iir)
19244a0a0202SVille Syrjälä 			I915_WRITE(GEN6_PMIIR, pm_iir);
19254a0a0202SVille Syrjälä 
19267ce4d1f2SVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
19271ae3c34cSVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
19287ce4d1f2SVille Syrjälä 
19293ff60f89SOscar Mateo 		/* Call regardless, as some status bits might not be
19303ff60f89SOscar Mateo 		 * signalled in iir */
193191d14251STvrtko Ursulin 		valleyview_pipestat_irq_ack(dev_priv, iir, pipe_stats);
19327ce4d1f2SVille Syrjälä 
1933eef57324SJerome Anand 		if (iir & (I915_LPE_PIPE_A_INTERRUPT |
1934eef57324SJerome Anand 			   I915_LPE_PIPE_B_INTERRUPT))
1935eef57324SJerome Anand 			intel_lpe_audio_irq_handler(dev_priv);
1936eef57324SJerome Anand 
19377ce4d1f2SVille Syrjälä 		/*
19387ce4d1f2SVille Syrjälä 		 * VLV_IIR is single buffered, and reflects the level
19397ce4d1f2SVille Syrjälä 		 * from PIPESTAT/PORT_HOTPLUG_STAT, hence clear it last.
19407ce4d1f2SVille Syrjälä 		 */
19417ce4d1f2SVille Syrjälä 		if (iir)
19427ce4d1f2SVille Syrjälä 			I915_WRITE(VLV_IIR, iir);
19434a0a0202SVille Syrjälä 
1944a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, ier);
19454a0a0202SVille Syrjälä 		I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
19464a0a0202SVille Syrjälä 		POSTING_READ(VLV_MASTER_IER);
19471ae3c34cSVille Syrjälä 
194852894874SVille Syrjälä 		if (gt_iir)
1949261e40b8SVille Syrjälä 			snb_gt_irq_handler(dev_priv, gt_iir);
195052894874SVille Syrjälä 		if (pm_iir)
195152894874SVille Syrjälä 			gen6_rps_irq_handler(dev_priv, pm_iir);
195252894874SVille Syrjälä 
19531ae3c34cSVille Syrjälä 		if (hotplug_status)
195491d14251STvrtko Ursulin 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
19552ecb8ca4SVille Syrjälä 
195691d14251STvrtko Ursulin 		valleyview_pipestat_irq_handler(dev_priv, pipe_stats);
19571e1cace9SVille Syrjälä 	} while (0);
19587e231dbeSJesse Barnes 
19591f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
19601f814dacSImre Deak 
19617e231dbeSJesse Barnes 	return ret;
19627e231dbeSJesse Barnes }
19637e231dbeSJesse Barnes 
196443f328d7SVille Syrjälä static irqreturn_t cherryview_irq_handler(int irq, void *arg)
196543f328d7SVille Syrjälä {
196645a83f84SDaniel Vetter 	struct drm_device *dev = arg;
1967fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
196843f328d7SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
196943f328d7SVille Syrjälä 
19702dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
19712dd2a883SImre Deak 		return IRQ_NONE;
19722dd2a883SImre Deak 
19731f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
19741f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
19751f814dacSImre Deak 
1976579de73bSChris Wilson 	do {
19776e814800SVille Syrjälä 		u32 master_ctl, iir;
1978e30e251aSVille Syrjälä 		u32 gt_iir[4] = {};
19792ecb8ca4SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
19801ae3c34cSVille Syrjälä 		u32 hotplug_status = 0;
1981a5e485a9SVille Syrjälä 		u32 ier = 0;
1982a5e485a9SVille Syrjälä 
19838e5fd599SVille Syrjälä 		master_ctl = I915_READ(GEN8_MASTER_IRQ) & ~GEN8_MASTER_IRQ_CONTROL;
19843278f67fSVille Syrjälä 		iir = I915_READ(VLV_IIR);
19853278f67fSVille Syrjälä 
19863278f67fSVille Syrjälä 		if (master_ctl == 0 && iir == 0)
19878e5fd599SVille Syrjälä 			break;
198843f328d7SVille Syrjälä 
198927b6c122SOscar Mateo 		ret = IRQ_HANDLED;
199027b6c122SOscar Mateo 
1991a5e485a9SVille Syrjälä 		/*
1992a5e485a9SVille Syrjälä 		 * Theory on interrupt generation, based on empirical evidence:
1993a5e485a9SVille Syrjälä 		 *
1994a5e485a9SVille Syrjälä 		 * x = ((VLV_IIR & VLV_IER) ||
1995a5e485a9SVille Syrjälä 		 *      ((GEN8_MASTER_IRQ & ~GEN8_MASTER_IRQ_CONTROL) &&
1996a5e485a9SVille Syrjälä 		 *       (GEN8_MASTER_IRQ & GEN8_MASTER_IRQ_CONTROL)));
1997a5e485a9SVille Syrjälä 		 *
1998a5e485a9SVille Syrjälä 		 * A CPU interrupt will only be raised when 'x' has a 0->1 edge.
1999a5e485a9SVille Syrjälä 		 * Hence we clear GEN8_MASTER_IRQ_CONTROL and VLV_IER to
2000a5e485a9SVille Syrjälä 		 * guarantee the CPU interrupt will be raised again even if we
2001a5e485a9SVille Syrjälä 		 * don't end up clearing all the VLV_IIR and GEN8_MASTER_IRQ_CONTROL
2002a5e485a9SVille Syrjälä 		 * bits this time around.
2003a5e485a9SVille Syrjälä 		 */
200443f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, 0);
2005a5e485a9SVille Syrjälä 		ier = I915_READ(VLV_IER);
2006a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, 0);
200743f328d7SVille Syrjälä 
2008e30e251aSVille Syrjälä 		gen8_gt_irq_ack(dev_priv, master_ctl, gt_iir);
200927b6c122SOscar Mateo 
201027b6c122SOscar Mateo 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
20111ae3c34cSVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
201243f328d7SVille Syrjälä 
201327b6c122SOscar Mateo 		/* Call regardless, as some status bits might not be
201427b6c122SOscar Mateo 		 * signalled in iir */
201591d14251STvrtko Ursulin 		valleyview_pipestat_irq_ack(dev_priv, iir, pipe_stats);
201643f328d7SVille Syrjälä 
2017eef57324SJerome Anand 		if (iir & (I915_LPE_PIPE_A_INTERRUPT |
2018eef57324SJerome Anand 			   I915_LPE_PIPE_B_INTERRUPT |
2019eef57324SJerome Anand 			   I915_LPE_PIPE_C_INTERRUPT))
2020eef57324SJerome Anand 			intel_lpe_audio_irq_handler(dev_priv);
2021eef57324SJerome Anand 
20227ce4d1f2SVille Syrjälä 		/*
20237ce4d1f2SVille Syrjälä 		 * VLV_IIR is single buffered, and reflects the level
20247ce4d1f2SVille Syrjälä 		 * from PIPESTAT/PORT_HOTPLUG_STAT, hence clear it last.
20257ce4d1f2SVille Syrjälä 		 */
20267ce4d1f2SVille Syrjälä 		if (iir)
20277ce4d1f2SVille Syrjälä 			I915_WRITE(VLV_IIR, iir);
20287ce4d1f2SVille Syrjälä 
2029a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, ier);
2030e5328c43SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
203143f328d7SVille Syrjälä 		POSTING_READ(GEN8_MASTER_IRQ);
20321ae3c34cSVille Syrjälä 
2033e30e251aSVille Syrjälä 		gen8_gt_irq_handler(dev_priv, gt_iir);
2034e30e251aSVille Syrjälä 
20351ae3c34cSVille Syrjälä 		if (hotplug_status)
203691d14251STvrtko Ursulin 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
20372ecb8ca4SVille Syrjälä 
203891d14251STvrtko Ursulin 		valleyview_pipestat_irq_handler(dev_priv, pipe_stats);
2039579de73bSChris Wilson 	} while (0);
20403278f67fSVille Syrjälä 
20411f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
20421f814dacSImre Deak 
204343f328d7SVille Syrjälä 	return ret;
204443f328d7SVille Syrjälä }
204543f328d7SVille Syrjälä 
204691d14251STvrtko Ursulin static void ibx_hpd_irq_handler(struct drm_i915_private *dev_priv,
204791d14251STvrtko Ursulin 				u32 hotplug_trigger,
204840e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2049776ad806SJesse Barnes {
205042db67d6SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2051776ad806SJesse Barnes 
20526a39d7c9SJani Nikula 	/*
20536a39d7c9SJani Nikula 	 * Somehow the PCH doesn't seem to really ack the interrupt to the CPU
20546a39d7c9SJani Nikula 	 * unless we touch the hotplug register, even if hotplug_trigger is
20556a39d7c9SJani Nikula 	 * zero. Not acking leads to "The master control interrupt lied (SDE)!"
20566a39d7c9SJani Nikula 	 * errors.
20576a39d7c9SJani Nikula 	 */
205813cf5504SDave Airlie 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
20596a39d7c9SJani Nikula 	if (!hotplug_trigger) {
20606a39d7c9SJani Nikula 		u32 mask = PORTA_HOTPLUG_STATUS_MASK |
20616a39d7c9SJani Nikula 			PORTD_HOTPLUG_STATUS_MASK |
20626a39d7c9SJani Nikula 			PORTC_HOTPLUG_STATUS_MASK |
20636a39d7c9SJani Nikula 			PORTB_HOTPLUG_STATUS_MASK;
20646a39d7c9SJani Nikula 		dig_hotplug_reg &= ~mask;
20656a39d7c9SJani Nikula 	}
20666a39d7c9SJani Nikula 
206713cf5504SDave Airlie 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
20686a39d7c9SJani Nikula 	if (!hotplug_trigger)
20696a39d7c9SJani Nikula 		return;
207013cf5504SDave Airlie 
2071fd63e2a9SImre Deak 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
207240e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2073fd63e2a9SImre Deak 			   pch_port_hotplug_long_detect);
207440e56410SVille Syrjälä 
207591d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2076aaf5ec2eSSonika Jindal }
207791d131d2SDaniel Vetter 
207891d14251STvrtko Ursulin static void ibx_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
207940e56410SVille Syrjälä {
208040e56410SVille Syrjälä 	int pipe;
208140e56410SVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
208240e56410SVille Syrjälä 
208391d14251STvrtko Ursulin 	ibx_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ibx);
208440e56410SVille Syrjälä 
2085cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
2086cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
2087776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
2088cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
2089cfc33bf7SVille Syrjälä 				 port_name(port));
2090cfc33bf7SVille Syrjälä 	}
2091776ad806SJesse Barnes 
2092ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
209391d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
2094ce99c256SDaniel Vetter 
2095776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
209691d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
2097776ad806SJesse Barnes 
2098776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
2099776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
2100776ad806SJesse Barnes 
2101776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
2102776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
2103776ad806SJesse Barnes 
2104776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
2105776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
2106776ad806SJesse Barnes 
21079db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
2108055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
21099db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
21109db4a9c7SJesse Barnes 					 pipe_name(pipe),
21119db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
2112776ad806SJesse Barnes 
2113776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
2114776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
2115776ad806SJesse Barnes 
2116776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
2117776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
2118776ad806SJesse Barnes 
2119776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
2120a2196033SMatthias Kaehlcke 		intel_pch_fifo_underrun_irq_handler(dev_priv, PIPE_A);
21218664281bSPaulo Zanoni 
21228664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
2123a2196033SMatthias Kaehlcke 		intel_pch_fifo_underrun_irq_handler(dev_priv, PIPE_B);
21248664281bSPaulo Zanoni }
21258664281bSPaulo Zanoni 
212691d14251STvrtko Ursulin static void ivb_err_int_handler(struct drm_i915_private *dev_priv)
21278664281bSPaulo Zanoni {
21288664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
21295a69b89fSDaniel Vetter 	enum pipe pipe;
21308664281bSPaulo Zanoni 
2131de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
2132de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
2133de032bf4SPaulo Zanoni 
2134055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
21351f7247c0SDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe))
21361f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
21378664281bSPaulo Zanoni 
21385a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
213991d14251STvrtko Ursulin 			if (IS_IVYBRIDGE(dev_priv))
214091d14251STvrtko Ursulin 				ivb_pipe_crc_irq_handler(dev_priv, pipe);
21415a69b89fSDaniel Vetter 			else
214291d14251STvrtko Ursulin 				hsw_pipe_crc_irq_handler(dev_priv, pipe);
21435a69b89fSDaniel Vetter 		}
21445a69b89fSDaniel Vetter 	}
21458bf1e9f1SShuang He 
21468664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
21478664281bSPaulo Zanoni }
21488664281bSPaulo Zanoni 
214991d14251STvrtko Ursulin static void cpt_serr_int_handler(struct drm_i915_private *dev_priv)
21508664281bSPaulo Zanoni {
21518664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
21528664281bSPaulo Zanoni 
2153de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
2154de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
2155de032bf4SPaulo Zanoni 
21568664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
2157a2196033SMatthias Kaehlcke 		intel_pch_fifo_underrun_irq_handler(dev_priv, PIPE_A);
21588664281bSPaulo Zanoni 
21598664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
2160a2196033SMatthias Kaehlcke 		intel_pch_fifo_underrun_irq_handler(dev_priv, PIPE_B);
21618664281bSPaulo Zanoni 
21628664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
2163a2196033SMatthias Kaehlcke 		intel_pch_fifo_underrun_irq_handler(dev_priv, PIPE_C);
21648664281bSPaulo Zanoni 
21658664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
2166776ad806SJesse Barnes }
2167776ad806SJesse Barnes 
216891d14251STvrtko Ursulin static void cpt_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
216923e81d69SAdam Jackson {
217023e81d69SAdam Jackson 	int pipe;
21716dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
2172aaf5ec2eSSonika Jindal 
217391d14251STvrtko Ursulin 	ibx_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_cpt);
217491d131d2SDaniel Vetter 
2175cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
2176cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
217723e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
2178cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
2179cfc33bf7SVille Syrjälä 				 port_name(port));
2180cfc33bf7SVille Syrjälä 	}
218123e81d69SAdam Jackson 
218223e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
218391d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
218423e81d69SAdam Jackson 
218523e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
218691d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
218723e81d69SAdam Jackson 
218823e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
218923e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
219023e81d69SAdam Jackson 
219123e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
219223e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
219323e81d69SAdam Jackson 
219423e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
2195055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
219623e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
219723e81d69SAdam Jackson 					 pipe_name(pipe),
219823e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
21998664281bSPaulo Zanoni 
22008664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
220191d14251STvrtko Ursulin 		cpt_serr_int_handler(dev_priv);
220223e81d69SAdam Jackson }
220323e81d69SAdam Jackson 
220491d14251STvrtko Ursulin static void spt_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
22056dbf30ceSVille Syrjälä {
22066dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_SPT &
22076dbf30ceSVille Syrjälä 		~SDE_PORTE_HOTPLUG_SPT;
22086dbf30ceSVille Syrjälä 	u32 hotplug2_trigger = pch_iir & SDE_PORTE_HOTPLUG_SPT;
22096dbf30ceSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
22106dbf30ceSVille Syrjälä 
22116dbf30ceSVille Syrjälä 	if (hotplug_trigger) {
22126dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
22136dbf30ceSVille Syrjälä 
22146dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
22156dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
22166dbf30ceSVille Syrjälä 
22176dbf30ceSVille Syrjälä 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
22186dbf30ceSVille Syrjälä 				   dig_hotplug_reg, hpd_spt,
221974c0b395SVille Syrjälä 				   spt_port_hotplug_long_detect);
22206dbf30ceSVille Syrjälä 	}
22216dbf30ceSVille Syrjälä 
22226dbf30ceSVille Syrjälä 	if (hotplug2_trigger) {
22236dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
22246dbf30ceSVille Syrjälä 
22256dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG2);
22266dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG2, dig_hotplug_reg);
22276dbf30ceSVille Syrjälä 
22286dbf30ceSVille Syrjälä 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug2_trigger,
22296dbf30ceSVille Syrjälä 				   dig_hotplug_reg, hpd_spt,
22306dbf30ceSVille Syrjälä 				   spt_port_hotplug2_long_detect);
22316dbf30ceSVille Syrjälä 	}
22326dbf30ceSVille Syrjälä 
22336dbf30ceSVille Syrjälä 	if (pin_mask)
223491d14251STvrtko Ursulin 		intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
22356dbf30ceSVille Syrjälä 
22366dbf30ceSVille Syrjälä 	if (pch_iir & SDE_GMBUS_CPT)
223791d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
22386dbf30ceSVille Syrjälä }
22396dbf30ceSVille Syrjälä 
224091d14251STvrtko Ursulin static void ilk_hpd_irq_handler(struct drm_i915_private *dev_priv,
224191d14251STvrtko Ursulin 				u32 hotplug_trigger,
224240e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2243c008bc6eSPaulo Zanoni {
2244e4ce95aaSVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2245e4ce95aaSVille Syrjälä 
2246e4ce95aaSVille Syrjälä 	dig_hotplug_reg = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
2247e4ce95aaSVille Syrjälä 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, dig_hotplug_reg);
2248e4ce95aaSVille Syrjälä 
2249e4ce95aaSVille Syrjälä 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
225040e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2251e4ce95aaSVille Syrjälä 			   ilk_port_hotplug_long_detect);
225240e56410SVille Syrjälä 
225391d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2254e4ce95aaSVille Syrjälä }
2255c008bc6eSPaulo Zanoni 
225691d14251STvrtko Ursulin static void ilk_display_irq_handler(struct drm_i915_private *dev_priv,
225791d14251STvrtko Ursulin 				    u32 de_iir)
225840e56410SVille Syrjälä {
225940e56410SVille Syrjälä 	enum pipe pipe;
226040e56410SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG;
226140e56410SVille Syrjälä 
226240e56410SVille Syrjälä 	if (hotplug_trigger)
226391d14251STvrtko Ursulin 		ilk_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ilk);
226440e56410SVille Syrjälä 
2265c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
226691d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
2267c008bc6eSPaulo Zanoni 
2268c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
226991d14251STvrtko Ursulin 		intel_opregion_asle_intr(dev_priv);
2270c008bc6eSPaulo Zanoni 
2271c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
2272c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
2273c008bc6eSPaulo Zanoni 
2274055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2275fd3a4024SDaniel Vetter 		if (de_iir & DE_PIPE_VBLANK(pipe))
2276fd3a4024SDaniel Vetter 			drm_handle_vblank(&dev_priv->drm, pipe);
2277c008bc6eSPaulo Zanoni 
227840da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
22791f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
2280c008bc6eSPaulo Zanoni 
228140da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
228291d14251STvrtko Ursulin 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
2283c008bc6eSPaulo Zanoni 	}
2284c008bc6eSPaulo Zanoni 
2285c008bc6eSPaulo Zanoni 	/* check event from PCH */
2286c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
2287c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
2288c008bc6eSPaulo Zanoni 
228991d14251STvrtko Ursulin 		if (HAS_PCH_CPT(dev_priv))
229091d14251STvrtko Ursulin 			cpt_irq_handler(dev_priv, pch_iir);
2291c008bc6eSPaulo Zanoni 		else
229291d14251STvrtko Ursulin 			ibx_irq_handler(dev_priv, pch_iir);
2293c008bc6eSPaulo Zanoni 
2294c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
2295c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
2296c008bc6eSPaulo Zanoni 	}
2297c008bc6eSPaulo Zanoni 
229891d14251STvrtko Ursulin 	if (IS_GEN5(dev_priv) && de_iir & DE_PCU_EVENT)
229991d14251STvrtko Ursulin 		ironlake_rps_change_irq_handler(dev_priv);
2300c008bc6eSPaulo Zanoni }
2301c008bc6eSPaulo Zanoni 
230291d14251STvrtko Ursulin static void ivb_display_irq_handler(struct drm_i915_private *dev_priv,
230391d14251STvrtko Ursulin 				    u32 de_iir)
23049719fb98SPaulo Zanoni {
230507d27e20SDamien Lespiau 	enum pipe pipe;
230623bb4cb5SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG_IVB;
230723bb4cb5SVille Syrjälä 
230840e56410SVille Syrjälä 	if (hotplug_trigger)
230991d14251STvrtko Ursulin 		ilk_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ivb);
23109719fb98SPaulo Zanoni 
23119719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
231291d14251STvrtko Ursulin 		ivb_err_int_handler(dev_priv);
23139719fb98SPaulo Zanoni 
23149719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
231591d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
23169719fb98SPaulo Zanoni 
23179719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
231891d14251STvrtko Ursulin 		intel_opregion_asle_intr(dev_priv);
23199719fb98SPaulo Zanoni 
2320055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2321fd3a4024SDaniel Vetter 		if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)))
2322fd3a4024SDaniel Vetter 			drm_handle_vblank(&dev_priv->drm, pipe);
23239719fb98SPaulo Zanoni 	}
23249719fb98SPaulo Zanoni 
23259719fb98SPaulo Zanoni 	/* check event from PCH */
232691d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv) && (de_iir & DE_PCH_EVENT_IVB)) {
23279719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
23289719fb98SPaulo Zanoni 
232991d14251STvrtko Ursulin 		cpt_irq_handler(dev_priv, pch_iir);
23309719fb98SPaulo Zanoni 
23319719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
23329719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
23339719fb98SPaulo Zanoni 	}
23349719fb98SPaulo Zanoni }
23359719fb98SPaulo Zanoni 
233672c90f62SOscar Mateo /*
233772c90f62SOscar Mateo  * To handle irqs with the minimum potential races with fresh interrupts, we:
233872c90f62SOscar Mateo  * 1 - Disable Master Interrupt Control.
233972c90f62SOscar Mateo  * 2 - Find the source(s) of the interrupt.
234072c90f62SOscar Mateo  * 3 - Clear the Interrupt Identity bits (IIR).
234172c90f62SOscar Mateo  * 4 - Process the interrupt(s) that had bits set in the IIRs.
234272c90f62SOscar Mateo  * 5 - Re-enable Master Interrupt Control.
234372c90f62SOscar Mateo  */
2344f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
2345b1f14ad0SJesse Barnes {
234645a83f84SDaniel Vetter 	struct drm_device *dev = arg;
2347fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2348f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
23490e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
2350b1f14ad0SJesse Barnes 
23512dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
23522dd2a883SImre Deak 		return IRQ_NONE;
23532dd2a883SImre Deak 
23541f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
23551f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
23561f814dacSImre Deak 
2357b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
2358b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
2359b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
236023a78516SPaulo Zanoni 	POSTING_READ(DEIER);
23610e43406bSChris Wilson 
236244498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
236344498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
236444498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
236544498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
236644498aeaSPaulo Zanoni 	 * due to its back queue). */
236791d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv)) {
236844498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
236944498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
237044498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2371ab5c608bSBen Widawsky 	}
237244498aeaSPaulo Zanoni 
237372c90f62SOscar Mateo 	/* Find, clear, then process each source of interrupt */
237472c90f62SOscar Mateo 
23750e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
23760e43406bSChris Wilson 	if (gt_iir) {
237772c90f62SOscar Mateo 		I915_WRITE(GTIIR, gt_iir);
237872c90f62SOscar Mateo 		ret = IRQ_HANDLED;
237991d14251STvrtko Ursulin 		if (INTEL_GEN(dev_priv) >= 6)
2380261e40b8SVille Syrjälä 			snb_gt_irq_handler(dev_priv, gt_iir);
2381d8fc8a47SPaulo Zanoni 		else
2382261e40b8SVille Syrjälä 			ilk_gt_irq_handler(dev_priv, gt_iir);
23830e43406bSChris Wilson 	}
2384b1f14ad0SJesse Barnes 
2385b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
23860e43406bSChris Wilson 	if (de_iir) {
238772c90f62SOscar Mateo 		I915_WRITE(DEIIR, de_iir);
238872c90f62SOscar Mateo 		ret = IRQ_HANDLED;
238991d14251STvrtko Ursulin 		if (INTEL_GEN(dev_priv) >= 7)
239091d14251STvrtko Ursulin 			ivb_display_irq_handler(dev_priv, de_iir);
2391f1af8fc1SPaulo Zanoni 		else
239291d14251STvrtko Ursulin 			ilk_display_irq_handler(dev_priv, de_iir);
23930e43406bSChris Wilson 	}
23940e43406bSChris Wilson 
239591d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 6) {
2396f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
23970e43406bSChris Wilson 		if (pm_iir) {
2398b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
23990e43406bSChris Wilson 			ret = IRQ_HANDLED;
240072c90f62SOscar Mateo 			gen6_rps_irq_handler(dev_priv, pm_iir);
24010e43406bSChris Wilson 		}
2402f1af8fc1SPaulo Zanoni 	}
2403b1f14ad0SJesse Barnes 
2404b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
2405b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
240691d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv)) {
240744498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
240844498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2409ab5c608bSBen Widawsky 	}
2410b1f14ad0SJesse Barnes 
24111f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
24121f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
24131f814dacSImre Deak 
2414b1f14ad0SJesse Barnes 	return ret;
2415b1f14ad0SJesse Barnes }
2416b1f14ad0SJesse Barnes 
241791d14251STvrtko Ursulin static void bxt_hpd_irq_handler(struct drm_i915_private *dev_priv,
241891d14251STvrtko Ursulin 				u32 hotplug_trigger,
241940e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2420d04a492dSShashank Sharma {
2421cebd87a0SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2422d04a492dSShashank Sharma 
2423a52bb15bSVille Syrjälä 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
2424a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
2425d04a492dSShashank Sharma 
2426cebd87a0SVille Syrjälä 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
242740e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2428cebd87a0SVille Syrjälä 			   bxt_port_hotplug_long_detect);
242940e56410SVille Syrjälä 
243091d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2431d04a492dSShashank Sharma }
2432d04a492dSShashank Sharma 
2433f11a0f46STvrtko Ursulin static irqreturn_t
2434f11a0f46STvrtko Ursulin gen8_de_irq_handler(struct drm_i915_private *dev_priv, u32 master_ctl)
2435abd58f01SBen Widawsky {
2436abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
2437f11a0f46STvrtko Ursulin 	u32 iir;
2438c42664ccSDaniel Vetter 	enum pipe pipe;
243988e04703SJesse Barnes 
2440abd58f01SBen Widawsky 	if (master_ctl & GEN8_DE_MISC_IRQ) {
2441e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_MISC_IIR);
2442e32192e1STvrtko Ursulin 		if (iir) {
2443e32192e1STvrtko Ursulin 			I915_WRITE(GEN8_DE_MISC_IIR, iir);
2444abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
2445e32192e1STvrtko Ursulin 			if (iir & GEN8_DE_MISC_GSE)
244691d14251STvrtko Ursulin 				intel_opregion_asle_intr(dev_priv);
244738cc46d7SOscar Mateo 			else
244838cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Misc interrupt\n");
2449abd58f01SBen Widawsky 		}
245038cc46d7SOscar Mateo 		else
245138cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
2452abd58f01SBen Widawsky 	}
2453abd58f01SBen Widawsky 
24546d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
2455e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_PORT_IIR);
2456e32192e1STvrtko Ursulin 		if (iir) {
2457e32192e1STvrtko Ursulin 			u32 tmp_mask;
2458d04a492dSShashank Sharma 			bool found = false;
2459cebd87a0SVille Syrjälä 
2460e32192e1STvrtko Ursulin 			I915_WRITE(GEN8_DE_PORT_IIR, iir);
24616d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
246288e04703SJesse Barnes 
2463e32192e1STvrtko Ursulin 			tmp_mask = GEN8_AUX_CHANNEL_A;
2464bca2bf2aSPandiyan, Dhinakaran 			if (INTEL_GEN(dev_priv) >= 9)
2465e32192e1STvrtko Ursulin 				tmp_mask |= GEN9_AUX_CHANNEL_B |
2466e32192e1STvrtko Ursulin 					    GEN9_AUX_CHANNEL_C |
2467e32192e1STvrtko Ursulin 					    GEN9_AUX_CHANNEL_D;
2468e32192e1STvrtko Ursulin 
2469e32192e1STvrtko Ursulin 			if (iir & tmp_mask) {
247091d14251STvrtko Ursulin 				dp_aux_irq_handler(dev_priv);
2471d04a492dSShashank Sharma 				found = true;
2472d04a492dSShashank Sharma 			}
2473d04a492dSShashank Sharma 
2474cc3f90f0SAnder Conselvan de Oliveira 			if (IS_GEN9_LP(dev_priv)) {
2475e32192e1STvrtko Ursulin 				tmp_mask = iir & BXT_DE_PORT_HOTPLUG_MASK;
2476e32192e1STvrtko Ursulin 				if (tmp_mask) {
247791d14251STvrtko Ursulin 					bxt_hpd_irq_handler(dev_priv, tmp_mask,
247891d14251STvrtko Ursulin 							    hpd_bxt);
2479d04a492dSShashank Sharma 					found = true;
2480d04a492dSShashank Sharma 				}
2481e32192e1STvrtko Ursulin 			} else if (IS_BROADWELL(dev_priv)) {
2482e32192e1STvrtko Ursulin 				tmp_mask = iir & GEN8_PORT_DP_A_HOTPLUG;
2483e32192e1STvrtko Ursulin 				if (tmp_mask) {
248491d14251STvrtko Ursulin 					ilk_hpd_irq_handler(dev_priv,
248591d14251STvrtko Ursulin 							    tmp_mask, hpd_bdw);
2486e32192e1STvrtko Ursulin 					found = true;
2487e32192e1STvrtko Ursulin 				}
2488e32192e1STvrtko Ursulin 			}
2489d04a492dSShashank Sharma 
2490cc3f90f0SAnder Conselvan de Oliveira 			if (IS_GEN9_LP(dev_priv) && (iir & BXT_DE_PORT_GMBUS)) {
249191d14251STvrtko Ursulin 				gmbus_irq_handler(dev_priv);
24929e63743eSShashank Sharma 				found = true;
24939e63743eSShashank Sharma 			}
24949e63743eSShashank Sharma 
2495d04a492dSShashank Sharma 			if (!found)
249638cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Port interrupt\n");
24976d766f02SDaniel Vetter 		}
249838cc46d7SOscar Mateo 		else
249938cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
25006d766f02SDaniel Vetter 	}
25016d766f02SDaniel Vetter 
2502055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2503fd3a4024SDaniel Vetter 		u32 fault_errors;
2504abd58f01SBen Widawsky 
2505c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2506c42664ccSDaniel Vetter 			continue;
2507c42664ccSDaniel Vetter 
2508e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
2509e32192e1STvrtko Ursulin 		if (!iir) {
2510e32192e1STvrtko Ursulin 			DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
2511e32192e1STvrtko Ursulin 			continue;
2512e32192e1STvrtko Ursulin 		}
2513770de83dSDamien Lespiau 
2514e32192e1STvrtko Ursulin 		ret = IRQ_HANDLED;
2515e32192e1STvrtko Ursulin 		I915_WRITE(GEN8_DE_PIPE_IIR(pipe), iir);
2516e32192e1STvrtko Ursulin 
2517fd3a4024SDaniel Vetter 		if (iir & GEN8_PIPE_VBLANK)
2518fd3a4024SDaniel Vetter 			drm_handle_vblank(&dev_priv->drm, pipe);
2519abd58f01SBen Widawsky 
2520e32192e1STvrtko Ursulin 		if (iir & GEN8_PIPE_CDCLK_CRC_DONE)
252191d14251STvrtko Ursulin 			hsw_pipe_crc_irq_handler(dev_priv, pipe);
25220fbe7870SDaniel Vetter 
2523e32192e1STvrtko Ursulin 		if (iir & GEN8_PIPE_FIFO_UNDERRUN)
2524e32192e1STvrtko Ursulin 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
252538d83c96SDaniel Vetter 
2526e32192e1STvrtko Ursulin 		fault_errors = iir;
2527bca2bf2aSPandiyan, Dhinakaran 		if (INTEL_GEN(dev_priv) >= 9)
2528e32192e1STvrtko Ursulin 			fault_errors &= GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
2529770de83dSDamien Lespiau 		else
2530e32192e1STvrtko Ursulin 			fault_errors &= GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
2531770de83dSDamien Lespiau 
2532770de83dSDamien Lespiau 		if (fault_errors)
25331353ec38STvrtko Ursulin 			DRM_ERROR("Fault errors on pipe %c: 0x%08x\n",
253430100f2bSDaniel Vetter 				  pipe_name(pipe),
2535e32192e1STvrtko Ursulin 				  fault_errors);
2536abd58f01SBen Widawsky 	}
2537abd58f01SBen Widawsky 
253891d14251STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv) && !HAS_PCH_NOP(dev_priv) &&
2539266ea3d9SShashank Sharma 	    master_ctl & GEN8_DE_PCH_IRQ) {
254092d03a80SDaniel Vetter 		/*
254192d03a80SDaniel Vetter 		 * FIXME(BDW): Assume for now that the new interrupt handling
254292d03a80SDaniel Vetter 		 * scheme also closed the SDE interrupt handling race we've seen
254392d03a80SDaniel Vetter 		 * on older pch-split platforms. But this needs testing.
254492d03a80SDaniel Vetter 		 */
2545e32192e1STvrtko Ursulin 		iir = I915_READ(SDEIIR);
2546e32192e1STvrtko Ursulin 		if (iir) {
2547e32192e1STvrtko Ursulin 			I915_WRITE(SDEIIR, iir);
254892d03a80SDaniel Vetter 			ret = IRQ_HANDLED;
25496dbf30ceSVille Syrjälä 
25507b22b8c4SRodrigo Vivi 			if (HAS_PCH_SPT(dev_priv) || HAS_PCH_KBP(dev_priv) ||
25517b22b8c4SRodrigo Vivi 			    HAS_PCH_CNP(dev_priv))
255291d14251STvrtko Ursulin 				spt_irq_handler(dev_priv, iir);
25536dbf30ceSVille Syrjälä 			else
255491d14251STvrtko Ursulin 				cpt_irq_handler(dev_priv, iir);
25552dfb0b81SJani Nikula 		} else {
25562dfb0b81SJani Nikula 			/*
25572dfb0b81SJani Nikula 			 * Like on previous PCH there seems to be something
25582dfb0b81SJani Nikula 			 * fishy going on with forwarding PCH interrupts.
25592dfb0b81SJani Nikula 			 */
25602dfb0b81SJani Nikula 			DRM_DEBUG_DRIVER("The master control interrupt lied (SDE)!\n");
25612dfb0b81SJani Nikula 		}
256292d03a80SDaniel Vetter 	}
256392d03a80SDaniel Vetter 
2564f11a0f46STvrtko Ursulin 	return ret;
2565f11a0f46STvrtko Ursulin }
2566f11a0f46STvrtko Ursulin 
2567f11a0f46STvrtko Ursulin static irqreturn_t gen8_irq_handler(int irq, void *arg)
2568f11a0f46STvrtko Ursulin {
2569f11a0f46STvrtko Ursulin 	struct drm_device *dev = arg;
2570fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2571f11a0f46STvrtko Ursulin 	u32 master_ctl;
2572e30e251aSVille Syrjälä 	u32 gt_iir[4] = {};
2573f11a0f46STvrtko Ursulin 	irqreturn_t ret;
2574f11a0f46STvrtko Ursulin 
2575f11a0f46STvrtko Ursulin 	if (!intel_irqs_enabled(dev_priv))
2576f11a0f46STvrtko Ursulin 		return IRQ_NONE;
2577f11a0f46STvrtko Ursulin 
2578f11a0f46STvrtko Ursulin 	master_ctl = I915_READ_FW(GEN8_MASTER_IRQ);
2579f11a0f46STvrtko Ursulin 	master_ctl &= ~GEN8_MASTER_IRQ_CONTROL;
2580f11a0f46STvrtko Ursulin 	if (!master_ctl)
2581f11a0f46STvrtko Ursulin 		return IRQ_NONE;
2582f11a0f46STvrtko Ursulin 
2583f11a0f46STvrtko Ursulin 	I915_WRITE_FW(GEN8_MASTER_IRQ, 0);
2584f11a0f46STvrtko Ursulin 
2585f11a0f46STvrtko Ursulin 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
2586f11a0f46STvrtko Ursulin 	disable_rpm_wakeref_asserts(dev_priv);
2587f11a0f46STvrtko Ursulin 
2588f11a0f46STvrtko Ursulin 	/* Find, clear, then process each source of interrupt */
2589e30e251aSVille Syrjälä 	ret = gen8_gt_irq_ack(dev_priv, master_ctl, gt_iir);
2590e30e251aSVille Syrjälä 	gen8_gt_irq_handler(dev_priv, gt_iir);
2591f11a0f46STvrtko Ursulin 	ret |= gen8_de_irq_handler(dev_priv, master_ctl);
2592f11a0f46STvrtko Ursulin 
2593cb0d205eSChris Wilson 	I915_WRITE_FW(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
2594cb0d205eSChris Wilson 	POSTING_READ_FW(GEN8_MASTER_IRQ);
2595abd58f01SBen Widawsky 
25961f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
25971f814dacSImre Deak 
2598abd58f01SBen Widawsky 	return ret;
2599abd58f01SBen Widawsky }
2600abd58f01SBen Widawsky 
260136703e79SChris Wilson struct wedge_me {
260236703e79SChris Wilson 	struct delayed_work work;
260336703e79SChris Wilson 	struct drm_i915_private *i915;
260436703e79SChris Wilson 	const char *name;
260536703e79SChris Wilson };
260636703e79SChris Wilson 
260736703e79SChris Wilson static void wedge_me(struct work_struct *work)
260836703e79SChris Wilson {
260936703e79SChris Wilson 	struct wedge_me *w = container_of(work, typeof(*w), work.work);
261036703e79SChris Wilson 
261136703e79SChris Wilson 	dev_err(w->i915->drm.dev,
261236703e79SChris Wilson 		"%s timed out, cancelling all in-flight rendering.\n",
261336703e79SChris Wilson 		w->name);
261436703e79SChris Wilson 	i915_gem_set_wedged(w->i915);
261536703e79SChris Wilson }
261636703e79SChris Wilson 
261736703e79SChris Wilson static void __init_wedge(struct wedge_me *w,
261836703e79SChris Wilson 			 struct drm_i915_private *i915,
261936703e79SChris Wilson 			 long timeout,
262036703e79SChris Wilson 			 const char *name)
262136703e79SChris Wilson {
262236703e79SChris Wilson 	w->i915 = i915;
262336703e79SChris Wilson 	w->name = name;
262436703e79SChris Wilson 
262536703e79SChris Wilson 	INIT_DELAYED_WORK_ONSTACK(&w->work, wedge_me);
262636703e79SChris Wilson 	schedule_delayed_work(&w->work, timeout);
262736703e79SChris Wilson }
262836703e79SChris Wilson 
262936703e79SChris Wilson static void __fini_wedge(struct wedge_me *w)
263036703e79SChris Wilson {
263136703e79SChris Wilson 	cancel_delayed_work_sync(&w->work);
263236703e79SChris Wilson 	destroy_delayed_work_on_stack(&w->work);
263336703e79SChris Wilson 	w->i915 = NULL;
263436703e79SChris Wilson }
263536703e79SChris Wilson 
263636703e79SChris Wilson #define i915_wedge_on_timeout(W, DEV, TIMEOUT)				\
263736703e79SChris Wilson 	for (__init_wedge((W), (DEV), (TIMEOUT), __func__);		\
263836703e79SChris Wilson 	     (W)->i915;							\
263936703e79SChris Wilson 	     __fini_wedge((W)))
264036703e79SChris Wilson 
26418a905236SJesse Barnes /**
2642d5367307SChris Wilson  * i915_reset_device - do process context error handling work
264314bb2c11STvrtko Ursulin  * @dev_priv: i915 device private
26448a905236SJesse Barnes  *
26458a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
26468a905236SJesse Barnes  * was detected.
26478a905236SJesse Barnes  */
2648d5367307SChris Wilson static void i915_reset_device(struct drm_i915_private *dev_priv)
26498a905236SJesse Barnes {
265091c8a326SChris Wilson 	struct kobject *kobj = &dev_priv->drm.primary->kdev->kobj;
2651cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
2652cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
2653cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
265436703e79SChris Wilson 	struct wedge_me w;
26558a905236SJesse Barnes 
2656c033666aSChris Wilson 	kobject_uevent_env(kobj, KOBJ_CHANGE, error_event);
26578a905236SJesse Barnes 
265844d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("resetting chip\n");
2659c033666aSChris Wilson 	kobject_uevent_env(kobj, KOBJ_CHANGE, reset_event);
26601f83fee0SDaniel Vetter 
266136703e79SChris Wilson 	/* Use a watchdog to ensure that our reset completes */
266236703e79SChris Wilson 	i915_wedge_on_timeout(&w, dev_priv, 5*HZ) {
2663c033666aSChris Wilson 		intel_prepare_reset(dev_priv);
26647514747dSVille Syrjälä 
266536703e79SChris Wilson 		/* Signal that locked waiters should reset the GPU */
26668c185ecaSChris Wilson 		set_bit(I915_RESET_HANDOFF, &dev_priv->gpu_error.flags);
26678c185ecaSChris Wilson 		wake_up_all(&dev_priv->gpu_error.wait_queue);
26688c185ecaSChris Wilson 
266936703e79SChris Wilson 		/* Wait for anyone holding the lock to wakeup, without
267036703e79SChris Wilson 		 * blocking indefinitely on struct_mutex.
267117e1df07SDaniel Vetter 		 */
267236703e79SChris Wilson 		do {
2673780f262aSChris Wilson 			if (mutex_trylock(&dev_priv->drm.struct_mutex)) {
2674535275d3SChris Wilson 				i915_reset(dev_priv, 0);
2675221fe799SChris Wilson 				mutex_unlock(&dev_priv->drm.struct_mutex);
2676780f262aSChris Wilson 			}
2677780f262aSChris Wilson 		} while (wait_on_bit_timeout(&dev_priv->gpu_error.flags,
26788c185ecaSChris Wilson 					     I915_RESET_HANDOFF,
2679780f262aSChris Wilson 					     TASK_UNINTERRUPTIBLE,
268036703e79SChris Wilson 					     1));
2681f69061beSDaniel Vetter 
2682c033666aSChris Wilson 		intel_finish_reset(dev_priv);
268336703e79SChris Wilson 	}
2684f454c694SImre Deak 
2685780f262aSChris Wilson 	if (!test_bit(I915_WEDGED, &dev_priv->gpu_error.flags))
2686c033666aSChris Wilson 		kobject_uevent_env(kobj,
2687f69061beSDaniel Vetter 				   KOBJ_CHANGE, reset_done_event);
2688f316a42cSBen Gamari }
26898a905236SJesse Barnes 
2690eaa14c24SChris Wilson static void i915_clear_error_registers(struct drm_i915_private *dev_priv)
2691c0e09200SDave Airlie {
2692eaa14c24SChris Wilson 	u32 eir;
269363eeaf38SJesse Barnes 
2694eaa14c24SChris Wilson 	if (!IS_GEN2(dev_priv))
2695eaa14c24SChris Wilson 		I915_WRITE(PGTBL_ER, I915_READ(PGTBL_ER));
269663eeaf38SJesse Barnes 
2697eaa14c24SChris Wilson 	if (INTEL_GEN(dev_priv) < 4)
2698eaa14c24SChris Wilson 		I915_WRITE(IPEIR, I915_READ(IPEIR));
2699eaa14c24SChris Wilson 	else
2700eaa14c24SChris Wilson 		I915_WRITE(IPEIR_I965, I915_READ(IPEIR_I965));
27018a905236SJesse Barnes 
2702eaa14c24SChris Wilson 	I915_WRITE(EIR, I915_READ(EIR));
270363eeaf38SJesse Barnes 	eir = I915_READ(EIR);
270463eeaf38SJesse Barnes 	if (eir) {
270563eeaf38SJesse Barnes 		/*
270663eeaf38SJesse Barnes 		 * some errors might have become stuck,
270763eeaf38SJesse Barnes 		 * mask them.
270863eeaf38SJesse Barnes 		 */
2709eaa14c24SChris Wilson 		DRM_DEBUG_DRIVER("EIR stuck: 0x%08x, masking\n", eir);
271063eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
271163eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
271263eeaf38SJesse Barnes 	}
271335aed2e6SChris Wilson }
271435aed2e6SChris Wilson 
271535aed2e6SChris Wilson /**
2716b8d24a06SMika Kuoppala  * i915_handle_error - handle a gpu error
271714bb2c11STvrtko Ursulin  * @dev_priv: i915 device private
271814b730fcSarun.siluvery@linux.intel.com  * @engine_mask: mask representing engines that are hung
271987c390b6SMichel Thierry  * @fmt: Error message format string
272087c390b6SMichel Thierry  *
2721aafd8581SJavier Martinez Canillas  * Do some basic checking of register state at error time and
272235aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
272335aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
272435aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
272535aed2e6SChris Wilson  * of a ring dump etc.).
272635aed2e6SChris Wilson  */
2727c033666aSChris Wilson void i915_handle_error(struct drm_i915_private *dev_priv,
2728c033666aSChris Wilson 		       u32 engine_mask,
272958174462SMika Kuoppala 		       const char *fmt, ...)
273035aed2e6SChris Wilson {
2731142bc7d9SMichel Thierry 	struct intel_engine_cs *engine;
2732142bc7d9SMichel Thierry 	unsigned int tmp;
273358174462SMika Kuoppala 	va_list args;
273458174462SMika Kuoppala 	char error_msg[80];
273535aed2e6SChris Wilson 
273658174462SMika Kuoppala 	va_start(args, fmt);
273758174462SMika Kuoppala 	vscnprintf(error_msg, sizeof(error_msg), fmt, args);
273858174462SMika Kuoppala 	va_end(args);
273958174462SMika Kuoppala 
27401604a86dSChris Wilson 	/*
27411604a86dSChris Wilson 	 * In most cases it's guaranteed that we get here with an RPM
27421604a86dSChris Wilson 	 * reference held, for example because there is a pending GPU
27431604a86dSChris Wilson 	 * request that won't finish until the reset is done. This
27441604a86dSChris Wilson 	 * isn't the case at least when we get here by doing a
27451604a86dSChris Wilson 	 * simulated reset via debugfs, so get an RPM reference.
27461604a86dSChris Wilson 	 */
27471604a86dSChris Wilson 	intel_runtime_pm_get(dev_priv);
27481604a86dSChris Wilson 
2749c033666aSChris Wilson 	i915_capture_error_state(dev_priv, engine_mask, error_msg);
2750eaa14c24SChris Wilson 	i915_clear_error_registers(dev_priv);
27518a905236SJesse Barnes 
2752142bc7d9SMichel Thierry 	/*
2753142bc7d9SMichel Thierry 	 * Try engine reset when available. We fall back to full reset if
2754142bc7d9SMichel Thierry 	 * single reset fails.
2755142bc7d9SMichel Thierry 	 */
2756142bc7d9SMichel Thierry 	if (intel_has_reset_engine(dev_priv)) {
2757142bc7d9SMichel Thierry 		for_each_engine_masked(engine, dev_priv, engine_mask, tmp) {
27589db529aaSDaniel Vetter 			BUILD_BUG_ON(I915_RESET_MODESET >= I915_RESET_ENGINE);
2759142bc7d9SMichel Thierry 			if (test_and_set_bit(I915_RESET_ENGINE + engine->id,
2760142bc7d9SMichel Thierry 					     &dev_priv->gpu_error.flags))
2761142bc7d9SMichel Thierry 				continue;
2762142bc7d9SMichel Thierry 
2763535275d3SChris Wilson 			if (i915_reset_engine(engine, 0) == 0)
2764142bc7d9SMichel Thierry 				engine_mask &= ~intel_engine_flag(engine);
2765142bc7d9SMichel Thierry 
2766142bc7d9SMichel Thierry 			clear_bit(I915_RESET_ENGINE + engine->id,
2767142bc7d9SMichel Thierry 				  &dev_priv->gpu_error.flags);
2768142bc7d9SMichel Thierry 			wake_up_bit(&dev_priv->gpu_error.flags,
2769142bc7d9SMichel Thierry 				    I915_RESET_ENGINE + engine->id);
2770142bc7d9SMichel Thierry 		}
2771142bc7d9SMichel Thierry 	}
2772142bc7d9SMichel Thierry 
27738af29b0cSChris Wilson 	if (!engine_mask)
27741604a86dSChris Wilson 		goto out;
27758af29b0cSChris Wilson 
2776142bc7d9SMichel Thierry 	/* Full reset needs the mutex, stop any other user trying to do so. */
2777d5367307SChris Wilson 	if (test_and_set_bit(I915_RESET_BACKOFF, &dev_priv->gpu_error.flags)) {
2778d5367307SChris Wilson 		wait_event(dev_priv->gpu_error.reset_queue,
2779d5367307SChris Wilson 			   !test_bit(I915_RESET_BACKOFF,
2780d5367307SChris Wilson 				     &dev_priv->gpu_error.flags));
27811604a86dSChris Wilson 		goto out;
2782d5367307SChris Wilson 	}
2783ba1234d1SBen Gamari 
2784142bc7d9SMichel Thierry 	/* Prevent any other reset-engine attempt. */
2785142bc7d9SMichel Thierry 	for_each_engine(engine, dev_priv, tmp) {
2786142bc7d9SMichel Thierry 		while (test_and_set_bit(I915_RESET_ENGINE + engine->id,
2787142bc7d9SMichel Thierry 					&dev_priv->gpu_error.flags))
2788142bc7d9SMichel Thierry 			wait_on_bit(&dev_priv->gpu_error.flags,
2789142bc7d9SMichel Thierry 				    I915_RESET_ENGINE + engine->id,
2790142bc7d9SMichel Thierry 				    TASK_UNINTERRUPTIBLE);
2791142bc7d9SMichel Thierry 	}
2792142bc7d9SMichel Thierry 
2793d5367307SChris Wilson 	i915_reset_device(dev_priv);
2794d5367307SChris Wilson 
2795142bc7d9SMichel Thierry 	for_each_engine(engine, dev_priv, tmp) {
2796142bc7d9SMichel Thierry 		clear_bit(I915_RESET_ENGINE + engine->id,
2797142bc7d9SMichel Thierry 			  &dev_priv->gpu_error.flags);
2798142bc7d9SMichel Thierry 	}
2799142bc7d9SMichel Thierry 
2800d5367307SChris Wilson 	clear_bit(I915_RESET_BACKOFF, &dev_priv->gpu_error.flags);
2801d5367307SChris Wilson 	wake_up_all(&dev_priv->gpu_error.reset_queue);
28021604a86dSChris Wilson 
28031604a86dSChris Wilson out:
28041604a86dSChris Wilson 	intel_runtime_pm_put(dev_priv);
28058a905236SJesse Barnes }
28068a905236SJesse Barnes 
280742f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
280842f52ef8SKeith Packard  * we use as a pipe index
280942f52ef8SKeith Packard  */
281086e83e35SChris Wilson static int i8xx_enable_vblank(struct drm_device *dev, unsigned int pipe)
28110a3e67a4SJesse Barnes {
2812fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2813e9d21d7fSKeith Packard 	unsigned long irqflags;
281471e0ffa5SJesse Barnes 
28151ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
281686e83e35SChris Wilson 	i915_enable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_STATUS);
281786e83e35SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
281886e83e35SChris Wilson 
281986e83e35SChris Wilson 	return 0;
282086e83e35SChris Wilson }
282186e83e35SChris Wilson 
282286e83e35SChris Wilson static int i965_enable_vblank(struct drm_device *dev, unsigned int pipe)
282386e83e35SChris Wilson {
282486e83e35SChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
282586e83e35SChris Wilson 	unsigned long irqflags;
282686e83e35SChris Wilson 
282786e83e35SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
28287c463586SKeith Packard 	i915_enable_pipestat(dev_priv, pipe,
2829755e9019SImre Deak 			     PIPE_START_VBLANK_INTERRUPT_STATUS);
28301ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
28318692d00eSChris Wilson 
28320a3e67a4SJesse Barnes 	return 0;
28330a3e67a4SJesse Barnes }
28340a3e67a4SJesse Barnes 
283588e72717SThierry Reding static int ironlake_enable_vblank(struct drm_device *dev, unsigned int pipe)
2836f796cf8fSJesse Barnes {
2837fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2838f796cf8fSJesse Barnes 	unsigned long irqflags;
283955b8f2a7STvrtko Ursulin 	uint32_t bit = INTEL_GEN(dev_priv) >= 7 ?
284086e83e35SChris Wilson 		DE_PIPE_VBLANK_IVB(pipe) : DE_PIPE_VBLANK(pipe);
2841f796cf8fSJesse Barnes 
2842f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2843fbdedaeaSVille Syrjälä 	ilk_enable_display_irq(dev_priv, bit);
2844b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2845b1f14ad0SJesse Barnes 
2846b1f14ad0SJesse Barnes 	return 0;
2847b1f14ad0SJesse Barnes }
2848b1f14ad0SJesse Barnes 
284988e72717SThierry Reding static int gen8_enable_vblank(struct drm_device *dev, unsigned int pipe)
2850abd58f01SBen Widawsky {
2851fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2852abd58f01SBen Widawsky 	unsigned long irqflags;
2853abd58f01SBen Widawsky 
2854abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2855013d3752SVille Syrjälä 	bdw_enable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
2856abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2857013d3752SVille Syrjälä 
2858abd58f01SBen Widawsky 	return 0;
2859abd58f01SBen Widawsky }
2860abd58f01SBen Widawsky 
286142f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
286242f52ef8SKeith Packard  * we use as a pipe index
286342f52ef8SKeith Packard  */
286486e83e35SChris Wilson static void i8xx_disable_vblank(struct drm_device *dev, unsigned int pipe)
286586e83e35SChris Wilson {
286686e83e35SChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
286786e83e35SChris Wilson 	unsigned long irqflags;
286886e83e35SChris Wilson 
286986e83e35SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
287086e83e35SChris Wilson 	i915_disable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_STATUS);
287186e83e35SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
287286e83e35SChris Wilson }
287386e83e35SChris Wilson 
287486e83e35SChris Wilson static void i965_disable_vblank(struct drm_device *dev, unsigned int pipe)
28750a3e67a4SJesse Barnes {
2876fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2877e9d21d7fSKeith Packard 	unsigned long irqflags;
28780a3e67a4SJesse Barnes 
28791ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
28807c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
2881755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
28821ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
28830a3e67a4SJesse Barnes }
28840a3e67a4SJesse Barnes 
288588e72717SThierry Reding static void ironlake_disable_vblank(struct drm_device *dev, unsigned int pipe)
2886f796cf8fSJesse Barnes {
2887fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2888f796cf8fSJesse Barnes 	unsigned long irqflags;
288955b8f2a7STvrtko Ursulin 	uint32_t bit = INTEL_GEN(dev_priv) >= 7 ?
289086e83e35SChris Wilson 		DE_PIPE_VBLANK_IVB(pipe) : DE_PIPE_VBLANK(pipe);
2891f796cf8fSJesse Barnes 
2892f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2893fbdedaeaSVille Syrjälä 	ilk_disable_display_irq(dev_priv, bit);
2894b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2895b1f14ad0SJesse Barnes }
2896b1f14ad0SJesse Barnes 
289788e72717SThierry Reding static void gen8_disable_vblank(struct drm_device *dev, unsigned int pipe)
2898abd58f01SBen Widawsky {
2899fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2900abd58f01SBen Widawsky 	unsigned long irqflags;
2901abd58f01SBen Widawsky 
2902abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2903013d3752SVille Syrjälä 	bdw_disable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
2904abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2905abd58f01SBen Widawsky }
2906abd58f01SBen Widawsky 
2907b243f530STvrtko Ursulin static void ibx_irq_reset(struct drm_i915_private *dev_priv)
290891738a95SPaulo Zanoni {
29096e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
291091738a95SPaulo Zanoni 		return;
291191738a95SPaulo Zanoni 
29123488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(SDE);
2913105b122eSPaulo Zanoni 
29146e266956STvrtko Ursulin 	if (HAS_PCH_CPT(dev_priv) || HAS_PCH_LPT(dev_priv))
2915105b122eSPaulo Zanoni 		I915_WRITE(SERR_INT, 0xffffffff);
2916622364b6SPaulo Zanoni }
2917105b122eSPaulo Zanoni 
291891738a95SPaulo Zanoni /*
2919622364b6SPaulo Zanoni  * SDEIER is also touched by the interrupt handler to work around missed PCH
2920622364b6SPaulo Zanoni  * interrupts. Hence we can't update it after the interrupt handler is enabled -
2921622364b6SPaulo Zanoni  * instead we unconditionally enable all PCH interrupt sources here, but then
2922622364b6SPaulo Zanoni  * only unmask them as needed with SDEIMR.
2923622364b6SPaulo Zanoni  *
2924622364b6SPaulo Zanoni  * This function needs to be called before interrupts are enabled.
292591738a95SPaulo Zanoni  */
2926622364b6SPaulo Zanoni static void ibx_irq_pre_postinstall(struct drm_device *dev)
2927622364b6SPaulo Zanoni {
2928fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2929622364b6SPaulo Zanoni 
29306e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
2931622364b6SPaulo Zanoni 		return;
2932622364b6SPaulo Zanoni 
2933622364b6SPaulo Zanoni 	WARN_ON(I915_READ(SDEIER) != 0);
293491738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
293591738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
293691738a95SPaulo Zanoni }
293791738a95SPaulo Zanoni 
2938b243f530STvrtko Ursulin static void gen5_gt_irq_reset(struct drm_i915_private *dev_priv)
2939d18ea1b5SDaniel Vetter {
29403488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(GT);
2941b243f530STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 6)
29423488d4ebSVille Syrjälä 		GEN3_IRQ_RESET(GEN6_PM);
2943d18ea1b5SDaniel Vetter }
2944d18ea1b5SDaniel Vetter 
294570591a41SVille Syrjälä static void vlv_display_irq_reset(struct drm_i915_private *dev_priv)
294670591a41SVille Syrjälä {
294771b8b41dSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
294871b8b41dSVille Syrjälä 		I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
294971b8b41dSVille Syrjälä 	else
295071b8b41dSVille Syrjälä 		I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
295171b8b41dSVille Syrjälä 
2952ad22d106SVille Syrjälä 	i915_hotplug_interrupt_update_locked(dev_priv, 0xffffffff, 0);
295370591a41SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
295470591a41SVille Syrjälä 
295544d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
295670591a41SVille Syrjälä 
29573488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(VLV_);
2958ad22d106SVille Syrjälä 	dev_priv->irq_mask = ~0;
295970591a41SVille Syrjälä }
296070591a41SVille Syrjälä 
29618bb61306SVille Syrjälä static void vlv_display_irq_postinstall(struct drm_i915_private *dev_priv)
29628bb61306SVille Syrjälä {
29638bb61306SVille Syrjälä 	u32 pipestat_mask;
29649ab981f2SVille Syrjälä 	u32 enable_mask;
29658bb61306SVille Syrjälä 	enum pipe pipe;
29668bb61306SVille Syrjälä 
2967842ebf7aSVille Syrjälä 	pipestat_mask = PIPE_CRC_DONE_INTERRUPT_STATUS;
29688bb61306SVille Syrjälä 
29698bb61306SVille Syrjälä 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
29708bb61306SVille Syrjälä 	for_each_pipe(dev_priv, pipe)
29718bb61306SVille Syrjälä 		i915_enable_pipestat(dev_priv, pipe, pipestat_mask);
29728bb61306SVille Syrjälä 
29739ab981f2SVille Syrjälä 	enable_mask = I915_DISPLAY_PORT_INTERRUPT |
29748bb61306SVille Syrjälä 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2975ebf5f921SVille Syrjälä 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2976ebf5f921SVille Syrjälä 		I915_LPE_PIPE_A_INTERRUPT |
2977ebf5f921SVille Syrjälä 		I915_LPE_PIPE_B_INTERRUPT;
2978ebf5f921SVille Syrjälä 
29798bb61306SVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
2980ebf5f921SVille Syrjälä 		enable_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT |
2981ebf5f921SVille Syrjälä 			I915_LPE_PIPE_C_INTERRUPT;
29826b7eafc1SVille Syrjälä 
29836b7eafc1SVille Syrjälä 	WARN_ON(dev_priv->irq_mask != ~0);
29846b7eafc1SVille Syrjälä 
29859ab981f2SVille Syrjälä 	dev_priv->irq_mask = ~enable_mask;
29868bb61306SVille Syrjälä 
29873488d4ebSVille Syrjälä 	GEN3_IRQ_INIT(VLV_, dev_priv->irq_mask, enable_mask);
29888bb61306SVille Syrjälä }
29898bb61306SVille Syrjälä 
29908bb61306SVille Syrjälä /* drm_dma.h hooks
29918bb61306SVille Syrjälä */
29928bb61306SVille Syrjälä static void ironlake_irq_reset(struct drm_device *dev)
29938bb61306SVille Syrjälä {
2994fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
29958bb61306SVille Syrjälä 
29968bb61306SVille Syrjälä 	I915_WRITE(HWSTAM, 0xffffffff);
29978bb61306SVille Syrjälä 
29983488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(DE);
29995db94019STvrtko Ursulin 	if (IS_GEN7(dev_priv))
30008bb61306SVille Syrjälä 		I915_WRITE(GEN7_ERR_INT, 0xffffffff);
30018bb61306SVille Syrjälä 
3002b243f530STvrtko Ursulin 	gen5_gt_irq_reset(dev_priv);
30038bb61306SVille Syrjälä 
3004b243f530STvrtko Ursulin 	ibx_irq_reset(dev_priv);
30058bb61306SVille Syrjälä }
30068bb61306SVille Syrjälä 
30077e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
30087e231dbeSJesse Barnes {
3009fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
30107e231dbeSJesse Barnes 
301134c7b8a7SVille Syrjälä 	I915_WRITE(VLV_MASTER_IER, 0);
301234c7b8a7SVille Syrjälä 	POSTING_READ(VLV_MASTER_IER);
301334c7b8a7SVille Syrjälä 
3014b243f530STvrtko Ursulin 	gen5_gt_irq_reset(dev_priv);
30157e231dbeSJesse Barnes 
3016ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
30179918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
301870591a41SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3019ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
30207e231dbeSJesse Barnes }
30217e231dbeSJesse Barnes 
3022d6e3cca3SDaniel Vetter static void gen8_gt_irq_reset(struct drm_i915_private *dev_priv)
3023d6e3cca3SDaniel Vetter {
3024d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 0);
3025d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 1);
3026d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 2);
3027d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 3);
3028d6e3cca3SDaniel Vetter }
3029d6e3cca3SDaniel Vetter 
3030823f6b38SPaulo Zanoni static void gen8_irq_reset(struct drm_device *dev)
3031abd58f01SBen Widawsky {
3032fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3033abd58f01SBen Widawsky 	int pipe;
3034abd58f01SBen Widawsky 
3035abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
3036abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3037abd58f01SBen Widawsky 
3038d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
3039abd58f01SBen Widawsky 
3040055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3041f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3042813bde43SPaulo Zanoni 						   POWER_DOMAIN_PIPE(pipe)))
3043f86f3fb0SPaulo Zanoni 			GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3044abd58f01SBen Widawsky 
30453488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(GEN8_DE_PORT_);
30463488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(GEN8_DE_MISC_);
30473488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(GEN8_PCU_);
3048abd58f01SBen Widawsky 
30496e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
3050b243f530STvrtko Ursulin 		ibx_irq_reset(dev_priv);
3051abd58f01SBen Widawsky }
3052abd58f01SBen Widawsky 
30534c6c03beSDamien Lespiau void gen8_irq_power_well_post_enable(struct drm_i915_private *dev_priv,
3054001bd2cbSImre Deak 				     u8 pipe_mask)
3055d49bdb0eSPaulo Zanoni {
30561180e206SPaulo Zanoni 	uint32_t extra_ier = GEN8_PIPE_VBLANK | GEN8_PIPE_FIFO_UNDERRUN;
30576831f3e3SVille Syrjälä 	enum pipe pipe;
3058d49bdb0eSPaulo Zanoni 
305913321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
30606831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
30616831f3e3SVille Syrjälä 		GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
30626831f3e3SVille Syrjälä 				  dev_priv->de_irq_mask[pipe],
30636831f3e3SVille Syrjälä 				  ~dev_priv->de_irq_mask[pipe] | extra_ier);
306413321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3065d49bdb0eSPaulo Zanoni }
3066d49bdb0eSPaulo Zanoni 
3067aae8ba84SVille Syrjälä void gen8_irq_power_well_pre_disable(struct drm_i915_private *dev_priv,
3068001bd2cbSImre Deak 				     u8 pipe_mask)
3069aae8ba84SVille Syrjälä {
30706831f3e3SVille Syrjälä 	enum pipe pipe;
30716831f3e3SVille Syrjälä 
3072aae8ba84SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
30736831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
30746831f3e3SVille Syrjälä 		GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3075aae8ba84SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3076aae8ba84SVille Syrjälä 
3077aae8ba84SVille Syrjälä 	/* make sure we're done processing display irqs */
307891c8a326SChris Wilson 	synchronize_irq(dev_priv->drm.irq);
3079aae8ba84SVille Syrjälä }
3080aae8ba84SVille Syrjälä 
308143f328d7SVille Syrjälä static void cherryview_irq_preinstall(struct drm_device *dev)
308243f328d7SVille Syrjälä {
3083fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
308443f328d7SVille Syrjälä 
308543f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
308643f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
308743f328d7SVille Syrjälä 
3088d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
308943f328d7SVille Syrjälä 
30903488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(GEN8_PCU_);
309143f328d7SVille Syrjälä 
3092ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
30939918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
309470591a41SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3095ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
309643f328d7SVille Syrjälä }
309743f328d7SVille Syrjälä 
309891d14251STvrtko Ursulin static u32 intel_hpd_enabled_irqs(struct drm_i915_private *dev_priv,
309987a02106SVille Syrjälä 				  const u32 hpd[HPD_NUM_PINS])
310087a02106SVille Syrjälä {
310187a02106SVille Syrjälä 	struct intel_encoder *encoder;
310287a02106SVille Syrjälä 	u32 enabled_irqs = 0;
310387a02106SVille Syrjälä 
310491c8a326SChris Wilson 	for_each_intel_encoder(&dev_priv->drm, encoder)
310587a02106SVille Syrjälä 		if (dev_priv->hotplug.stats[encoder->hpd_pin].state == HPD_ENABLED)
310687a02106SVille Syrjälä 			enabled_irqs |= hpd[encoder->hpd_pin];
310787a02106SVille Syrjälä 
310887a02106SVille Syrjälä 	return enabled_irqs;
310987a02106SVille Syrjälä }
311087a02106SVille Syrjälä 
31111a56b1a2SImre Deak static void ibx_hpd_detection_setup(struct drm_i915_private *dev_priv)
31121a56b1a2SImre Deak {
31131a56b1a2SImre Deak 	u32 hotplug;
31141a56b1a2SImre Deak 
31151a56b1a2SImre Deak 	/*
31161a56b1a2SImre Deak 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
31171a56b1a2SImre Deak 	 * duration to 2ms (which is the minimum in the Display Port spec).
31181a56b1a2SImre Deak 	 * The pulse duration bits are reserved on LPT+.
31191a56b1a2SImre Deak 	 */
31201a56b1a2SImre Deak 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
31211a56b1a2SImre Deak 	hotplug &= ~(PORTB_PULSE_DURATION_MASK |
31221a56b1a2SImre Deak 		     PORTC_PULSE_DURATION_MASK |
31231a56b1a2SImre Deak 		     PORTD_PULSE_DURATION_MASK);
31241a56b1a2SImre Deak 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
31251a56b1a2SImre Deak 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
31261a56b1a2SImre Deak 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
31271a56b1a2SImre Deak 	/*
31281a56b1a2SImre Deak 	 * When CPU and PCH are on the same package, port A
31291a56b1a2SImre Deak 	 * HPD must be enabled in both north and south.
31301a56b1a2SImre Deak 	 */
31311a56b1a2SImre Deak 	if (HAS_PCH_LPT_LP(dev_priv))
31321a56b1a2SImre Deak 		hotplug |= PORTA_HOTPLUG_ENABLE;
31331a56b1a2SImre Deak 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
31341a56b1a2SImre Deak }
31351a56b1a2SImre Deak 
313691d14251STvrtko Ursulin static void ibx_hpd_irq_setup(struct drm_i915_private *dev_priv)
313782a28bcfSDaniel Vetter {
31381a56b1a2SImre Deak 	u32 hotplug_irqs, enabled_irqs;
313982a28bcfSDaniel Vetter 
314091d14251STvrtko Ursulin 	if (HAS_PCH_IBX(dev_priv)) {
3141fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
314291d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ibx);
314382a28bcfSDaniel Vetter 	} else {
3144fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
314591d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_cpt);
314682a28bcfSDaniel Vetter 	}
314782a28bcfSDaniel Vetter 
3148fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
314982a28bcfSDaniel Vetter 
31501a56b1a2SImre Deak 	ibx_hpd_detection_setup(dev_priv);
31516dbf30ceSVille Syrjälä }
315226951cafSXiong Zhang 
31532a57d9ccSImre Deak static void spt_hpd_detection_setup(struct drm_i915_private *dev_priv)
31542a57d9ccSImre Deak {
31552a57d9ccSImre Deak 	u32 hotplug;
31562a57d9ccSImre Deak 
31572a57d9ccSImre Deak 	/* Enable digital hotplug on the PCH */
31582a57d9ccSImre Deak 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
31592a57d9ccSImre Deak 	hotplug |= PORTA_HOTPLUG_ENABLE |
31602a57d9ccSImre Deak 		   PORTB_HOTPLUG_ENABLE |
31612a57d9ccSImre Deak 		   PORTC_HOTPLUG_ENABLE |
31622a57d9ccSImre Deak 		   PORTD_HOTPLUG_ENABLE;
31632a57d9ccSImre Deak 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
31642a57d9ccSImre Deak 
31652a57d9ccSImre Deak 	hotplug = I915_READ(PCH_PORT_HOTPLUG2);
31662a57d9ccSImre Deak 	hotplug |= PORTE_HOTPLUG_ENABLE;
31672a57d9ccSImre Deak 	I915_WRITE(PCH_PORT_HOTPLUG2, hotplug);
31682a57d9ccSImre Deak }
31692a57d9ccSImre Deak 
317091d14251STvrtko Ursulin static void spt_hpd_irq_setup(struct drm_i915_private *dev_priv)
31716dbf30ceSVille Syrjälä {
31722a57d9ccSImre Deak 	u32 hotplug_irqs, enabled_irqs;
31736dbf30ceSVille Syrjälä 
31746dbf30ceSVille Syrjälä 	hotplug_irqs = SDE_HOTPLUG_MASK_SPT;
317591d14251STvrtko Ursulin 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_spt);
31766dbf30ceSVille Syrjälä 
31776dbf30ceSVille Syrjälä 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
31786dbf30ceSVille Syrjälä 
31792a57d9ccSImre Deak 	spt_hpd_detection_setup(dev_priv);
318026951cafSXiong Zhang }
31817fe0b973SKeith Packard 
31821a56b1a2SImre Deak static void ilk_hpd_detection_setup(struct drm_i915_private *dev_priv)
31831a56b1a2SImre Deak {
31841a56b1a2SImre Deak 	u32 hotplug;
31851a56b1a2SImre Deak 
31861a56b1a2SImre Deak 	/*
31871a56b1a2SImre Deak 	 * Enable digital hotplug on the CPU, and configure the DP short pulse
31881a56b1a2SImre Deak 	 * duration to 2ms (which is the minimum in the Display Port spec)
31891a56b1a2SImre Deak 	 * The pulse duration bits are reserved on HSW+.
31901a56b1a2SImre Deak 	 */
31911a56b1a2SImre Deak 	hotplug = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
31921a56b1a2SImre Deak 	hotplug &= ~DIGITAL_PORTA_PULSE_DURATION_MASK;
31931a56b1a2SImre Deak 	hotplug |= DIGITAL_PORTA_HOTPLUG_ENABLE |
31941a56b1a2SImre Deak 		   DIGITAL_PORTA_PULSE_DURATION_2ms;
31951a56b1a2SImre Deak 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, hotplug);
31961a56b1a2SImre Deak }
31971a56b1a2SImre Deak 
319891d14251STvrtko Ursulin static void ilk_hpd_irq_setup(struct drm_i915_private *dev_priv)
3199e4ce95aaSVille Syrjälä {
32001a56b1a2SImre Deak 	u32 hotplug_irqs, enabled_irqs;
3201e4ce95aaSVille Syrjälä 
320291d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 8) {
32033a3b3c7dSVille Syrjälä 		hotplug_irqs = GEN8_PORT_DP_A_HOTPLUG;
320491d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_bdw);
32053a3b3c7dSVille Syrjälä 
32063a3b3c7dSVille Syrjälä 		bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
320791d14251STvrtko Ursulin 	} else if (INTEL_GEN(dev_priv) >= 7) {
320823bb4cb5SVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG_IVB;
320991d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ivb);
32103a3b3c7dSVille Syrjälä 
32113a3b3c7dSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
321223bb4cb5SVille Syrjälä 	} else {
3213e4ce95aaSVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG;
321491d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ilk);
3215e4ce95aaSVille Syrjälä 
3216e4ce95aaSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
32173a3b3c7dSVille Syrjälä 	}
3218e4ce95aaSVille Syrjälä 
32191a56b1a2SImre Deak 	ilk_hpd_detection_setup(dev_priv);
3220e4ce95aaSVille Syrjälä 
322191d14251STvrtko Ursulin 	ibx_hpd_irq_setup(dev_priv);
3222e4ce95aaSVille Syrjälä }
3223e4ce95aaSVille Syrjälä 
32242a57d9ccSImre Deak static void __bxt_hpd_detection_setup(struct drm_i915_private *dev_priv,
32252a57d9ccSImre Deak 				      u32 enabled_irqs)
3226e0a20ad7SShashank Sharma {
32272a57d9ccSImre Deak 	u32 hotplug;
3228e0a20ad7SShashank Sharma 
3229a52bb15bSVille Syrjälä 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
32302a57d9ccSImre Deak 	hotplug |= PORTA_HOTPLUG_ENABLE |
32312a57d9ccSImre Deak 		   PORTB_HOTPLUG_ENABLE |
32322a57d9ccSImre Deak 		   PORTC_HOTPLUG_ENABLE;
3233d252bf68SShubhangi Shrivastava 
3234d252bf68SShubhangi Shrivastava 	DRM_DEBUG_KMS("Invert bit setting: hp_ctl:%x hp_port:%x\n",
3235d252bf68SShubhangi Shrivastava 		      hotplug, enabled_irqs);
3236d252bf68SShubhangi Shrivastava 	hotplug &= ~BXT_DDI_HPD_INVERT_MASK;
3237d252bf68SShubhangi Shrivastava 
3238d252bf68SShubhangi Shrivastava 	/*
3239d252bf68SShubhangi Shrivastava 	 * For BXT invert bit has to be set based on AOB design
3240d252bf68SShubhangi Shrivastava 	 * for HPD detection logic, update it based on VBT fields.
3241d252bf68SShubhangi Shrivastava 	 */
3242d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIA) &&
3243d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_A))
3244d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIA_HPD_INVERT;
3245d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIB) &&
3246d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_B))
3247d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIB_HPD_INVERT;
3248d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIC) &&
3249d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_C))
3250d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIC_HPD_INVERT;
3251d252bf68SShubhangi Shrivastava 
3252a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
3253e0a20ad7SShashank Sharma }
3254e0a20ad7SShashank Sharma 
32552a57d9ccSImre Deak static void bxt_hpd_detection_setup(struct drm_i915_private *dev_priv)
32562a57d9ccSImre Deak {
32572a57d9ccSImre Deak 	__bxt_hpd_detection_setup(dev_priv, BXT_DE_PORT_HOTPLUG_MASK);
32582a57d9ccSImre Deak }
32592a57d9ccSImre Deak 
32602a57d9ccSImre Deak static void bxt_hpd_irq_setup(struct drm_i915_private *dev_priv)
32612a57d9ccSImre Deak {
32622a57d9ccSImre Deak 	u32 hotplug_irqs, enabled_irqs;
32632a57d9ccSImre Deak 
32642a57d9ccSImre Deak 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_bxt);
32652a57d9ccSImre Deak 	hotplug_irqs = BXT_DE_PORT_HOTPLUG_MASK;
32662a57d9ccSImre Deak 
32672a57d9ccSImre Deak 	bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
32682a57d9ccSImre Deak 
32692a57d9ccSImre Deak 	__bxt_hpd_detection_setup(dev_priv, enabled_irqs);
32702a57d9ccSImre Deak }
32712a57d9ccSImre Deak 
3272d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
3273d46da437SPaulo Zanoni {
3274fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
327582a28bcfSDaniel Vetter 	u32 mask;
3276d46da437SPaulo Zanoni 
32776e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
3278692a04cfSDaniel Vetter 		return;
3279692a04cfSDaniel Vetter 
32806e266956STvrtko Ursulin 	if (HAS_PCH_IBX(dev_priv))
32815c673b60SDaniel Vetter 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
32824ebc6509SDhinakaran Pandiyan 	else if (HAS_PCH_CPT(dev_priv) || HAS_PCH_LPT(dev_priv))
32835c673b60SDaniel Vetter 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
32844ebc6509SDhinakaran Pandiyan 	else
32854ebc6509SDhinakaran Pandiyan 		mask = SDE_GMBUS_CPT;
32868664281bSPaulo Zanoni 
32873488d4ebSVille Syrjälä 	gen3_assert_iir_is_zero(dev_priv, SDEIIR);
3288d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
32892a57d9ccSImre Deak 
32902a57d9ccSImre Deak 	if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
32912a57d9ccSImre Deak 	    HAS_PCH_LPT(dev_priv))
32921a56b1a2SImre Deak 		ibx_hpd_detection_setup(dev_priv);
32932a57d9ccSImre Deak 	else
32942a57d9ccSImre Deak 		spt_hpd_detection_setup(dev_priv);
3295d46da437SPaulo Zanoni }
3296d46da437SPaulo Zanoni 
32970a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
32980a9a8c91SDaniel Vetter {
3299fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
33000a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
33010a9a8c91SDaniel Vetter 
33020a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
33030a9a8c91SDaniel Vetter 
33040a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
33053c9192bcSTvrtko Ursulin 	if (HAS_L3_DPF(dev_priv)) {
33060a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
3307772c2a51STvrtko Ursulin 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev_priv);
3308772c2a51STvrtko Ursulin 		gt_irqs |= GT_PARITY_ERROR(dev_priv);
33090a9a8c91SDaniel Vetter 	}
33100a9a8c91SDaniel Vetter 
33110a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
33125db94019STvrtko Ursulin 	if (IS_GEN5(dev_priv)) {
3313f8973c21SChris Wilson 		gt_irqs |= ILK_BSD_USER_INTERRUPT;
33140a9a8c91SDaniel Vetter 	} else {
33150a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
33160a9a8c91SDaniel Vetter 	}
33170a9a8c91SDaniel Vetter 
33183488d4ebSVille Syrjälä 	GEN3_IRQ_INIT(GT, dev_priv->gt_irq_mask, gt_irqs);
33190a9a8c91SDaniel Vetter 
3320b243f530STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 6) {
332178e68d36SImre Deak 		/*
332278e68d36SImre Deak 		 * RPS interrupts will get enabled/disabled on demand when RPS
332378e68d36SImre Deak 		 * itself is enabled/disabled.
332478e68d36SImre Deak 		 */
3325f4e9af4fSAkash Goel 		if (HAS_VEBOX(dev_priv)) {
33260a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
3327f4e9af4fSAkash Goel 			dev_priv->pm_ier |= PM_VEBOX_USER_INTERRUPT;
3328f4e9af4fSAkash Goel 		}
33290a9a8c91SDaniel Vetter 
3330f4e9af4fSAkash Goel 		dev_priv->pm_imr = 0xffffffff;
33313488d4ebSVille Syrjälä 		GEN3_IRQ_INIT(GEN6_PM, dev_priv->pm_imr, pm_irqs);
33320a9a8c91SDaniel Vetter 	}
33330a9a8c91SDaniel Vetter }
33340a9a8c91SDaniel Vetter 
3335f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
3336036a4a7dSZhenyu Wang {
3337fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
33388e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
33398e76f8dcSPaulo Zanoni 
3340b243f530STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 7) {
33418e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
3342842ebf7aSVille Syrjälä 				DE_PCH_EVENT_IVB | DE_AUX_CHANNEL_A_IVB);
33438e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
334423bb4cb5SVille Syrjälä 			      DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB |
334523bb4cb5SVille Syrjälä 			      DE_DP_A_HOTPLUG_IVB);
33468e76f8dcSPaulo Zanoni 	} else {
33478e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3348842ebf7aSVille Syrjälä 				DE_AUX_CHANNEL_A | DE_PIPEB_CRC_DONE |
3349842ebf7aSVille Syrjälä 				DE_PIPEA_CRC_DONE | DE_POISON);
3350e4ce95aaSVille Syrjälä 		extra_mask = (DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
3351e4ce95aaSVille Syrjälä 			      DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN |
3352e4ce95aaSVille Syrjälä 			      DE_DP_A_HOTPLUG);
33538e76f8dcSPaulo Zanoni 	}
3354036a4a7dSZhenyu Wang 
33551ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3356036a4a7dSZhenyu Wang 
33570c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xeffe);
33580c841212SPaulo Zanoni 
3359622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3360622364b6SPaulo Zanoni 
33613488d4ebSVille Syrjälä 	GEN3_IRQ_INIT(DE, dev_priv->irq_mask, display_mask | extra_mask);
3362036a4a7dSZhenyu Wang 
33630a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
3364036a4a7dSZhenyu Wang 
33651a56b1a2SImre Deak 	ilk_hpd_detection_setup(dev_priv);
33661a56b1a2SImre Deak 
3367d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
33687fe0b973SKeith Packard 
336950a0bc90STvrtko Ursulin 	if (IS_IRONLAKE_M(dev_priv)) {
33706005ce42SDaniel Vetter 		/* Enable PCU event interrupts
33716005ce42SDaniel Vetter 		 *
33726005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
33734bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
33744bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
3375d6207435SDaniel Vetter 		spin_lock_irq(&dev_priv->irq_lock);
3376fbdedaeaSVille Syrjälä 		ilk_enable_display_irq(dev_priv, DE_PCU_EVENT);
3377d6207435SDaniel Vetter 		spin_unlock_irq(&dev_priv->irq_lock);
3378f97108d1SJesse Barnes 	}
3379f97108d1SJesse Barnes 
3380036a4a7dSZhenyu Wang 	return 0;
3381036a4a7dSZhenyu Wang }
3382036a4a7dSZhenyu Wang 
3383f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3384f8b79e58SImre Deak {
338567520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
3386f8b79e58SImre Deak 
3387f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3388f8b79e58SImre Deak 		return;
3389f8b79e58SImre Deak 
3390f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3391f8b79e58SImre Deak 
3392d6c69803SVille Syrjälä 	if (intel_irqs_enabled(dev_priv)) {
3393d6c69803SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3394ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3395f8b79e58SImre Deak 	}
3396d6c69803SVille Syrjälä }
3397f8b79e58SImre Deak 
3398f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3399f8b79e58SImre Deak {
340067520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
3401f8b79e58SImre Deak 
3402f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3403f8b79e58SImre Deak 		return;
3404f8b79e58SImre Deak 
3405f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3406f8b79e58SImre Deak 
3407950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3408ad22d106SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3409f8b79e58SImre Deak }
3410f8b79e58SImre Deak 
34110e6c9a9eSVille Syrjälä 
34120e6c9a9eSVille Syrjälä static int valleyview_irq_postinstall(struct drm_device *dev)
34130e6c9a9eSVille Syrjälä {
3414fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
34150e6c9a9eSVille Syrjälä 
34160a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
34177e231dbeSJesse Barnes 
3418ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
34199918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3420ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3421ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3422ad22d106SVille Syrjälä 
34237e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
342434c7b8a7SVille Syrjälä 	POSTING_READ(VLV_MASTER_IER);
342520afbda2SDaniel Vetter 
342620afbda2SDaniel Vetter 	return 0;
342720afbda2SDaniel Vetter }
342820afbda2SDaniel Vetter 
3429abd58f01SBen Widawsky static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
3430abd58f01SBen Widawsky {
3431abd58f01SBen Widawsky 	/* These are interrupts we'll toggle with the ring mask register */
3432abd58f01SBen Widawsky 	uint32_t gt_interrupts[] = {
3433abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
343473d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
343573d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT |
343673d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT,
3437abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
343873d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
343973d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT |
344073d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT,
3441abd58f01SBen Widawsky 		0,
344273d477f6SOscar Mateo 		GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT |
344373d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT
3444abd58f01SBen Widawsky 		};
3445abd58f01SBen Widawsky 
344698735739STvrtko Ursulin 	if (HAS_L3_DPF(dev_priv))
344798735739STvrtko Ursulin 		gt_interrupts[0] |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
344898735739STvrtko Ursulin 
3449f4e9af4fSAkash Goel 	dev_priv->pm_ier = 0x0;
3450f4e9af4fSAkash Goel 	dev_priv->pm_imr = ~dev_priv->pm_ier;
34519a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 0, ~gt_interrupts[0], gt_interrupts[0]);
34529a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 1, ~gt_interrupts[1], gt_interrupts[1]);
345378e68d36SImre Deak 	/*
345478e68d36SImre Deak 	 * RPS interrupts will get enabled/disabled on demand when RPS itself
345526705e20SSagar Arun Kamble 	 * is enabled/disabled. Same wil be the case for GuC interrupts.
345678e68d36SImre Deak 	 */
3457f4e9af4fSAkash Goel 	GEN8_IRQ_INIT_NDX(GT, 2, dev_priv->pm_imr, dev_priv->pm_ier);
34589a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 3, ~gt_interrupts[3], gt_interrupts[3]);
3459abd58f01SBen Widawsky }
3460abd58f01SBen Widawsky 
3461abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3462abd58f01SBen Widawsky {
3463770de83dSDamien Lespiau 	uint32_t de_pipe_masked = GEN8_PIPE_CDCLK_CRC_DONE;
3464770de83dSDamien Lespiau 	uint32_t de_pipe_enables;
34653a3b3c7dSVille Syrjälä 	u32 de_port_masked = GEN8_AUX_CHANNEL_A;
34663a3b3c7dSVille Syrjälä 	u32 de_port_enables;
346711825b0dSVille Syrjälä 	u32 de_misc_masked = GEN8_DE_MISC_GSE;
34683a3b3c7dSVille Syrjälä 	enum pipe pipe;
3469770de83dSDamien Lespiau 
3470bca2bf2aSPandiyan, Dhinakaran 	if (INTEL_GEN(dev_priv) >= 9) {
3471842ebf7aSVille Syrjälä 		de_pipe_masked |= GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
34723a3b3c7dSVille Syrjälä 		de_port_masked |= GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
347388e04703SJesse Barnes 				  GEN9_AUX_CHANNEL_D;
3474cc3f90f0SAnder Conselvan de Oliveira 		if (IS_GEN9_LP(dev_priv))
34753a3b3c7dSVille Syrjälä 			de_port_masked |= BXT_DE_PORT_GMBUS;
34763a3b3c7dSVille Syrjälä 	} else {
3477842ebf7aSVille Syrjälä 		de_pipe_masked |= GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
34783a3b3c7dSVille Syrjälä 	}
3479770de83dSDamien Lespiau 
3480770de83dSDamien Lespiau 	de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
3481770de83dSDamien Lespiau 					   GEN8_PIPE_FIFO_UNDERRUN;
3482770de83dSDamien Lespiau 
34833a3b3c7dSVille Syrjälä 	de_port_enables = de_port_masked;
3484cc3f90f0SAnder Conselvan de Oliveira 	if (IS_GEN9_LP(dev_priv))
3485a52bb15bSVille Syrjälä 		de_port_enables |= BXT_DE_PORT_HOTPLUG_MASK;
3486a52bb15bSVille Syrjälä 	else if (IS_BROADWELL(dev_priv))
34873a3b3c7dSVille Syrjälä 		de_port_enables |= GEN8_PORT_DP_A_HOTPLUG;
34883a3b3c7dSVille Syrjälä 
348913b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_A] = ~de_pipe_masked;
349013b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_B] = ~de_pipe_masked;
349113b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_C] = ~de_pipe_masked;
3492abd58f01SBen Widawsky 
3493055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3494f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3495813bde43SPaulo Zanoni 				POWER_DOMAIN_PIPE(pipe)))
3496813bde43SPaulo Zanoni 			GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
3497813bde43SPaulo Zanoni 					  dev_priv->de_irq_mask[pipe],
349835079899SPaulo Zanoni 					  de_pipe_enables);
3499abd58f01SBen Widawsky 
35003488d4ebSVille Syrjälä 	GEN3_IRQ_INIT(GEN8_DE_PORT_, ~de_port_masked, de_port_enables);
35013488d4ebSVille Syrjälä 	GEN3_IRQ_INIT(GEN8_DE_MISC_, ~de_misc_masked, de_misc_masked);
35022a57d9ccSImre Deak 
35032a57d9ccSImre Deak 	if (IS_GEN9_LP(dev_priv))
35042a57d9ccSImre Deak 		bxt_hpd_detection_setup(dev_priv);
35051a56b1a2SImre Deak 	else if (IS_BROADWELL(dev_priv))
35061a56b1a2SImre Deak 		ilk_hpd_detection_setup(dev_priv);
3507abd58f01SBen Widawsky }
3508abd58f01SBen Widawsky 
3509abd58f01SBen Widawsky static int gen8_irq_postinstall(struct drm_device *dev)
3510abd58f01SBen Widawsky {
3511fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3512abd58f01SBen Widawsky 
35136e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
3514622364b6SPaulo Zanoni 		ibx_irq_pre_postinstall(dev);
3515622364b6SPaulo Zanoni 
3516abd58f01SBen Widawsky 	gen8_gt_irq_postinstall(dev_priv);
3517abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3518abd58f01SBen Widawsky 
35196e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
3520abd58f01SBen Widawsky 		ibx_irq_postinstall(dev);
3521abd58f01SBen Widawsky 
3522e5328c43SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
3523abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3524abd58f01SBen Widawsky 
3525abd58f01SBen Widawsky 	return 0;
3526abd58f01SBen Widawsky }
3527abd58f01SBen Widawsky 
352843f328d7SVille Syrjälä static int cherryview_irq_postinstall(struct drm_device *dev)
352943f328d7SVille Syrjälä {
3530fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
353143f328d7SVille Syrjälä 
353243f328d7SVille Syrjälä 	gen8_gt_irq_postinstall(dev_priv);
353343f328d7SVille Syrjälä 
3534ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
35359918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3536ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3537ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3538ad22d106SVille Syrjälä 
3539e5328c43SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
354043f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
354143f328d7SVille Syrjälä 
354243f328d7SVille Syrjälä 	return 0;
354343f328d7SVille Syrjälä }
354443f328d7SVille Syrjälä 
3545abd58f01SBen Widawsky static void gen8_irq_uninstall(struct drm_device *dev)
3546abd58f01SBen Widawsky {
3547fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3548abd58f01SBen Widawsky 
3549abd58f01SBen Widawsky 	if (!dev_priv)
3550abd58f01SBen Widawsky 		return;
3551abd58f01SBen Widawsky 
3552823f6b38SPaulo Zanoni 	gen8_irq_reset(dev);
3553abd58f01SBen Widawsky }
3554abd58f01SBen Widawsky 
35557e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
35567e231dbeSJesse Barnes {
3557fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
35587e231dbeSJesse Barnes 
35597e231dbeSJesse Barnes 	if (!dev_priv)
35607e231dbeSJesse Barnes 		return;
35617e231dbeSJesse Barnes 
3562843d0e7dSImre Deak 	I915_WRITE(VLV_MASTER_IER, 0);
356334c7b8a7SVille Syrjälä 	POSTING_READ(VLV_MASTER_IER);
3564843d0e7dSImre Deak 
3565b243f530STvrtko Ursulin 	gen5_gt_irq_reset(dev_priv);
3566893fce8eSVille Syrjälä 
35677e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
3568f8b79e58SImre Deak 
3569ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
35709918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3571ad22d106SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3572ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
35737e231dbeSJesse Barnes }
35747e231dbeSJesse Barnes 
357543f328d7SVille Syrjälä static void cherryview_irq_uninstall(struct drm_device *dev)
357643f328d7SVille Syrjälä {
3577fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
357843f328d7SVille Syrjälä 
357943f328d7SVille Syrjälä 	if (!dev_priv)
358043f328d7SVille Syrjälä 		return;
358143f328d7SVille Syrjälä 
358243f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
358343f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
358443f328d7SVille Syrjälä 
3585a2c30fbaSVille Syrjälä 	gen8_gt_irq_reset(dev_priv);
358643f328d7SVille Syrjälä 
35873488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(GEN8_PCU_);
358843f328d7SVille Syrjälä 
3589ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
35909918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3591ad22d106SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3592ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
359343f328d7SVille Syrjälä }
359443f328d7SVille Syrjälä 
3595f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
3596036a4a7dSZhenyu Wang {
3597fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
35984697995bSJesse Barnes 
35994697995bSJesse Barnes 	if (!dev_priv)
36004697995bSJesse Barnes 		return;
36014697995bSJesse Barnes 
3602be30b29fSPaulo Zanoni 	ironlake_irq_reset(dev);
3603036a4a7dSZhenyu Wang }
3604036a4a7dSZhenyu Wang 
3605c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
3606c2798b19SChris Wilson {
3607fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3608c2798b19SChris Wilson 
360944d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
361044d9241eSVille Syrjälä 
3611*e9e9848aSVille Syrjälä 	GEN2_IRQ_RESET();
3612c2798b19SChris Wilson }
3613c2798b19SChris Wilson 
3614c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
3615c2798b19SChris Wilson {
3616fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3617*e9e9848aSVille Syrjälä 	u16 enable_mask;
3618c2798b19SChris Wilson 
3619c2798b19SChris Wilson 	I915_WRITE16(EMR,
3620c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3621c2798b19SChris Wilson 
3622c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
3623c2798b19SChris Wilson 	dev_priv->irq_mask =
3624c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3625842ebf7aSVille Syrjälä 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT);
3626c2798b19SChris Wilson 
3627*e9e9848aSVille Syrjälä 	enable_mask =
3628c2798b19SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3629c2798b19SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3630*e9e9848aSVille Syrjälä 		I915_USER_INTERRUPT;
3631*e9e9848aSVille Syrjälä 
3632*e9e9848aSVille Syrjälä 	GEN2_IRQ_INIT(, dev_priv->irq_mask, enable_mask);
3633c2798b19SChris Wilson 
3634379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3635379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3636d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3637755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3638755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3639d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3640379ef82dSDaniel Vetter 
3641c2798b19SChris Wilson 	return 0;
3642c2798b19SChris Wilson }
3643c2798b19SChris Wilson 
3644ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3645c2798b19SChris Wilson {
364645a83f84SDaniel Vetter 	struct drm_device *dev = arg;
3647fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3648c2798b19SChris Wilson 	u16 iir, new_iir;
3649c2798b19SChris Wilson 	u32 pipe_stats[2];
3650c2798b19SChris Wilson 	int pipe;
36511f814dacSImre Deak 	irqreturn_t ret;
3652c2798b19SChris Wilson 
36532dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
36542dd2a883SImre Deak 		return IRQ_NONE;
36552dd2a883SImre Deak 
36561f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
36571f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
36581f814dacSImre Deak 
36591f814dacSImre Deak 	ret = IRQ_NONE;
3660c2798b19SChris Wilson 	iir = I915_READ16(IIR);
3661c2798b19SChris Wilson 	if (iir == 0)
36621f814dacSImre Deak 		goto out;
3663c2798b19SChris Wilson 
3664fd3a4024SDaniel Vetter 	while (iir) {
3665c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3666c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3667c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3668c2798b19SChris Wilson 		 * interrupts (for non-MSI).
3669c2798b19SChris Wilson 		 */
3670222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
3671c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3672aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
3673c2798b19SChris Wilson 
3674055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
3675f0f59a00SVille Syrjälä 			i915_reg_t reg = PIPESTAT(pipe);
3676c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3677c2798b19SChris Wilson 
3678c2798b19SChris Wilson 			/*
3679c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3680c2798b19SChris Wilson 			 */
36812d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & 0x8000ffff)
3682c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3683c2798b19SChris Wilson 		}
3684222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
3685c2798b19SChris Wilson 
3686fd3a4024SDaniel Vetter 		I915_WRITE16(IIR, iir);
3687c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
3688c2798b19SChris Wilson 
3689c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
36903b3f1650SAkash Goel 			notify_ring(dev_priv->engine[RCS]);
3691c2798b19SChris Wilson 
3692055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
36935a21b665SDaniel Vetter 			int plane = pipe;
36945a21b665SDaniel Vetter 			if (HAS_FBC(dev_priv))
36955a21b665SDaniel Vetter 				plane = !plane;
36965a21b665SDaniel Vetter 
3697fd3a4024SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
3698fd3a4024SDaniel Vetter 				drm_handle_vblank(&dev_priv->drm, pipe);
3699c2798b19SChris Wilson 
37004356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
370191d14251STvrtko Ursulin 				i9xx_pipe_crc_irq_handler(dev_priv, pipe);
37022d9d2b0bSVille Syrjälä 
37031f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
37041f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
37051f7247c0SDaniel Vetter 								    pipe);
37064356d586SDaniel Vetter 		}
3707c2798b19SChris Wilson 
3708c2798b19SChris Wilson 		iir = new_iir;
3709c2798b19SChris Wilson 	}
37101f814dacSImre Deak 	ret = IRQ_HANDLED;
3711c2798b19SChris Wilson 
37121f814dacSImre Deak out:
37131f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
37141f814dacSImre Deak 
37151f814dacSImre Deak 	return ret;
3716c2798b19SChris Wilson }
3717c2798b19SChris Wilson 
3718c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
3719c2798b19SChris Wilson {
3720fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3721c2798b19SChris Wilson 
372244d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
372344d9241eSVille Syrjälä 
3724*e9e9848aSVille Syrjälä 	GEN2_IRQ_RESET();
3725c2798b19SChris Wilson }
3726c2798b19SChris Wilson 
3727a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
3728a266c7d5SChris Wilson {
3729fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3730a266c7d5SChris Wilson 
373156b857a5STvrtko Ursulin 	if (I915_HAS_HOTPLUG(dev_priv)) {
37320706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
3733a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3734a266c7d5SChris Wilson 	}
3735a266c7d5SChris Wilson 
373644d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
373744d9241eSVille Syrjälä 
373800d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
373944d9241eSVille Syrjälä 
3740ba7eb789SVille Syrjälä 	GEN3_IRQ_RESET();
3741a266c7d5SChris Wilson }
3742a266c7d5SChris Wilson 
3743a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
3744a266c7d5SChris Wilson {
3745fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
374638bde180SChris Wilson 	u32 enable_mask;
3747a266c7d5SChris Wilson 
374838bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
374938bde180SChris Wilson 
375038bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
375138bde180SChris Wilson 	dev_priv->irq_mask =
375238bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
375338bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3754842ebf7aSVille Syrjälä 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT);
375538bde180SChris Wilson 
375638bde180SChris Wilson 	enable_mask =
375738bde180SChris Wilson 		I915_ASLE_INTERRUPT |
375838bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
375938bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
376038bde180SChris Wilson 		I915_USER_INTERRUPT;
376138bde180SChris Wilson 
376256b857a5STvrtko Ursulin 	if (I915_HAS_HOTPLUG(dev_priv)) {
37630706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
376420afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
376520afbda2SDaniel Vetter 
3766a266c7d5SChris Wilson 		/* Enable in IER... */
3767a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
3768a266c7d5SChris Wilson 		/* and unmask in IMR */
3769a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
3770a266c7d5SChris Wilson 	}
3771a266c7d5SChris Wilson 
3772ba7eb789SVille Syrjälä 	GEN3_IRQ_INIT(, dev_priv->irq_mask, enable_mask);
3773a266c7d5SChris Wilson 
377491d14251STvrtko Ursulin 	i915_enable_asle_pipestat(dev_priv);
377520afbda2SDaniel Vetter 
3776379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3777379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3778d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3779755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3780755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3781d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3782379ef82dSDaniel Vetter 
378320afbda2SDaniel Vetter 	return 0;
378420afbda2SDaniel Vetter }
378520afbda2SDaniel Vetter 
3786ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
3787a266c7d5SChris Wilson {
378845a83f84SDaniel Vetter 	struct drm_device *dev = arg;
3789fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
37908291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
379138bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
3792a266c7d5SChris Wilson 
37932dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
37942dd2a883SImre Deak 		return IRQ_NONE;
37952dd2a883SImre Deak 
37961f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
37971f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
37981f814dacSImre Deak 
3799a266c7d5SChris Wilson 	iir = I915_READ(IIR);
380038bde180SChris Wilson 	do {
3801fd3a4024SDaniel Vetter 		bool irq_received = (iir) != 0;
38028291ee90SChris Wilson 		bool blc_event = false;
3803a266c7d5SChris Wilson 
3804a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3805a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3806a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3807a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3808a266c7d5SChris Wilson 		 */
3809222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
3810a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3811aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
3812a266c7d5SChris Wilson 
3813055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
3814f0f59a00SVille Syrjälä 			i915_reg_t reg = PIPESTAT(pipe);
3815a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3816a266c7d5SChris Wilson 
381738bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
3818a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3819a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
382038bde180SChris Wilson 				irq_received = true;
3821a266c7d5SChris Wilson 			}
3822a266c7d5SChris Wilson 		}
3823222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
3824a266c7d5SChris Wilson 
3825a266c7d5SChris Wilson 		if (!irq_received)
3826a266c7d5SChris Wilson 			break;
3827a266c7d5SChris Wilson 
3828a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
382991d14251STvrtko Ursulin 		if (I915_HAS_HOTPLUG(dev_priv) &&
38301ae3c34cSVille Syrjälä 		    iir & I915_DISPLAY_PORT_INTERRUPT) {
38311ae3c34cSVille Syrjälä 			u32 hotplug_status = i9xx_hpd_irq_ack(dev_priv);
38321ae3c34cSVille Syrjälä 			if (hotplug_status)
383391d14251STvrtko Ursulin 				i9xx_hpd_irq_handler(dev_priv, hotplug_status);
38341ae3c34cSVille Syrjälä 		}
3835a266c7d5SChris Wilson 
3836fd3a4024SDaniel Vetter 		I915_WRITE(IIR, iir);
3837a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3838a266c7d5SChris Wilson 
3839a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
38403b3f1650SAkash Goel 			notify_ring(dev_priv->engine[RCS]);
3841a266c7d5SChris Wilson 
3842055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
38435a21b665SDaniel Vetter 			int plane = pipe;
38445a21b665SDaniel Vetter 			if (HAS_FBC(dev_priv))
38455a21b665SDaniel Vetter 				plane = !plane;
38465a21b665SDaniel Vetter 
3847fd3a4024SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
3848fd3a4024SDaniel Vetter 				drm_handle_vblank(&dev_priv->drm, pipe);
3849a266c7d5SChris Wilson 
3850a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3851a266c7d5SChris Wilson 				blc_event = true;
38524356d586SDaniel Vetter 
38534356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
385491d14251STvrtko Ursulin 				i9xx_pipe_crc_irq_handler(dev_priv, pipe);
38552d9d2b0bSVille Syrjälä 
38561f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
38571f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
38581f7247c0SDaniel Vetter 								    pipe);
3859a266c7d5SChris Wilson 		}
3860a266c7d5SChris Wilson 
3861a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
386291d14251STvrtko Ursulin 			intel_opregion_asle_intr(dev_priv);
3863a266c7d5SChris Wilson 
3864a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3865a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3866a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3867a266c7d5SChris Wilson 		 * we would never get another interrupt.
3868a266c7d5SChris Wilson 		 *
3869a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3870a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3871a266c7d5SChris Wilson 		 * another one.
3872a266c7d5SChris Wilson 		 *
3873a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3874a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3875a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3876a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3877a266c7d5SChris Wilson 		 * stray interrupts.
3878a266c7d5SChris Wilson 		 */
387938bde180SChris Wilson 		ret = IRQ_HANDLED;
3880a266c7d5SChris Wilson 		iir = new_iir;
3881fd3a4024SDaniel Vetter 	} while (iir);
3882a266c7d5SChris Wilson 
38831f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
38841f814dacSImre Deak 
3885a266c7d5SChris Wilson 	return ret;
3886a266c7d5SChris Wilson }
3887a266c7d5SChris Wilson 
3888a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
3889a266c7d5SChris Wilson {
3890fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3891a266c7d5SChris Wilson 
389256b857a5STvrtko Ursulin 	if (I915_HAS_HOTPLUG(dev_priv)) {
38930706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
3894a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3895a266c7d5SChris Wilson 	}
3896a266c7d5SChris Wilson 
389744d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
389844d9241eSVille Syrjälä 
389900d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
390044d9241eSVille Syrjälä 
3901ba7eb789SVille Syrjälä 	GEN3_IRQ_RESET();
3902a266c7d5SChris Wilson }
3903a266c7d5SChris Wilson 
3904a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
3905a266c7d5SChris Wilson {
3906fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3907a266c7d5SChris Wilson 
39080706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
3909a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3910a266c7d5SChris Wilson 
391144d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
391244d9241eSVille Syrjälä 
3913a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
391444d9241eSVille Syrjälä 
3915ba7eb789SVille Syrjälä 	GEN3_IRQ_RESET();
3916a266c7d5SChris Wilson }
3917a266c7d5SChris Wilson 
3918a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
3919a266c7d5SChris Wilson {
3920fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3921bbba0a97SChris Wilson 	u32 enable_mask;
3922a266c7d5SChris Wilson 	u32 error_mask;
3923a266c7d5SChris Wilson 
3924a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
3925bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
3926adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
3927bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3928bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3929bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3930bbba0a97SChris Wilson 
3931bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
3932bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
3933bbba0a97SChris Wilson 
393491d14251STvrtko Ursulin 	if (IS_G4X(dev_priv))
3935bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
3936a266c7d5SChris Wilson 
3937b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3938b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3939d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3940755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3941755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3942755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3943d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3944a266c7d5SChris Wilson 
3945a266c7d5SChris Wilson 	/*
3946a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
3947a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
3948a266c7d5SChris Wilson 	 */
394991d14251STvrtko Ursulin 	if (IS_G4X(dev_priv)) {
3950a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
3951a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
3952a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
3953a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
3954a266c7d5SChris Wilson 	} else {
3955a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
3956a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
3957a266c7d5SChris Wilson 	}
3958a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
3959a266c7d5SChris Wilson 
3960ba7eb789SVille Syrjälä 	GEN3_IRQ_INIT(, dev_priv->irq_mask, enable_mask);
3961a266c7d5SChris Wilson 
39620706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
396320afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
396420afbda2SDaniel Vetter 
396591d14251STvrtko Ursulin 	i915_enable_asle_pipestat(dev_priv);
396620afbda2SDaniel Vetter 
396720afbda2SDaniel Vetter 	return 0;
396820afbda2SDaniel Vetter }
396920afbda2SDaniel Vetter 
397091d14251STvrtko Ursulin static void i915_hpd_irq_setup(struct drm_i915_private *dev_priv)
397120afbda2SDaniel Vetter {
397220afbda2SDaniel Vetter 	u32 hotplug_en;
397320afbda2SDaniel Vetter 
397467520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
3975b5ea2d56SDaniel Vetter 
3976adca4730SChris Wilson 	/* Note HDMI and DP share hotplug bits */
3977e5868a31SEgbert Eich 	/* enable bits are the same for all generations */
397891d14251STvrtko Ursulin 	hotplug_en = intel_hpd_enabled_irqs(dev_priv, hpd_mask_i915);
3979a266c7d5SChris Wilson 	/* Programming the CRT detection parameters tends
3980a266c7d5SChris Wilson 	   to generate a spurious hotplug event about three
3981a266c7d5SChris Wilson 	   seconds later.  So just do it once.
3982a266c7d5SChris Wilson 	*/
398391d14251STvrtko Ursulin 	if (IS_G4X(dev_priv))
3984a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
3985a266c7d5SChris Wilson 	hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
3986a266c7d5SChris Wilson 
3987a266c7d5SChris Wilson 	/* Ignore TV since it's buggy */
39880706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv,
3989f9e3dc78SJani Nikula 					     HOTPLUG_INT_EN_MASK |
3990f9e3dc78SJani Nikula 					     CRT_HOTPLUG_VOLTAGE_COMPARE_MASK |
3991f9e3dc78SJani Nikula 					     CRT_HOTPLUG_ACTIVATION_PERIOD_64,
39920706f17cSEgbert Eich 					     hotplug_en);
3993a266c7d5SChris Wilson }
3994a266c7d5SChris Wilson 
3995ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
3996a266c7d5SChris Wilson {
399745a83f84SDaniel Vetter 	struct drm_device *dev = arg;
3998fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3999a266c7d5SChris Wilson 	u32 iir, new_iir;
4000a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
4001a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
4002a266c7d5SChris Wilson 
40032dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
40042dd2a883SImre Deak 		return IRQ_NONE;
40052dd2a883SImre Deak 
40061f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
40071f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
40081f814dacSImre Deak 
4009a266c7d5SChris Wilson 	iir = I915_READ(IIR);
4010a266c7d5SChris Wilson 
4011a266c7d5SChris Wilson 	for (;;) {
4012fd3a4024SDaniel Vetter 		bool irq_received = (iir) != 0;
40132c8ba29fSChris Wilson 		bool blc_event = false;
40142c8ba29fSChris Wilson 
4015a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4016a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4017a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4018a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
4019a266c7d5SChris Wilson 		 */
4020222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
4021a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4022aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4023a266c7d5SChris Wilson 
4024055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4025f0f59a00SVille Syrjälä 			i915_reg_t reg = PIPESTAT(pipe);
4026a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4027a266c7d5SChris Wilson 
4028a266c7d5SChris Wilson 			/*
4029a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
4030a266c7d5SChris Wilson 			 */
4031a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4032a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
4033501e01d7SVille Syrjälä 				irq_received = true;
4034a266c7d5SChris Wilson 			}
4035a266c7d5SChris Wilson 		}
4036222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4037a266c7d5SChris Wilson 
4038a266c7d5SChris Wilson 		if (!irq_received)
4039a266c7d5SChris Wilson 			break;
4040a266c7d5SChris Wilson 
4041a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
4042a266c7d5SChris Wilson 
4043a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
40441ae3c34cSVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
40451ae3c34cSVille Syrjälä 			u32 hotplug_status = i9xx_hpd_irq_ack(dev_priv);
40461ae3c34cSVille Syrjälä 			if (hotplug_status)
404791d14251STvrtko Ursulin 				i9xx_hpd_irq_handler(dev_priv, hotplug_status);
40481ae3c34cSVille Syrjälä 		}
4049a266c7d5SChris Wilson 
4050fd3a4024SDaniel Vetter 		I915_WRITE(IIR, iir);
4051a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4052a266c7d5SChris Wilson 
4053a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
40543b3f1650SAkash Goel 			notify_ring(dev_priv->engine[RCS]);
4055a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
40563b3f1650SAkash Goel 			notify_ring(dev_priv->engine[VCS]);
4057a266c7d5SChris Wilson 
4058055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4059fd3a4024SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS)
4060fd3a4024SDaniel Vetter 				drm_handle_vblank(&dev_priv->drm, pipe);
4061a266c7d5SChris Wilson 
4062a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4063a266c7d5SChris Wilson 				blc_event = true;
40644356d586SDaniel Vetter 
40654356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
406691d14251STvrtko Ursulin 				i9xx_pipe_crc_irq_handler(dev_priv, pipe);
4067a266c7d5SChris Wilson 
40681f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
40691f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
40702d9d2b0bSVille Syrjälä 		}
4071a266c7d5SChris Wilson 
4072a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
407391d14251STvrtko Ursulin 			intel_opregion_asle_intr(dev_priv);
4074a266c7d5SChris Wilson 
4075515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
407691d14251STvrtko Ursulin 			gmbus_irq_handler(dev_priv);
4077515ac2bbSDaniel Vetter 
4078a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4079a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4080a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4081a266c7d5SChris Wilson 		 * we would never get another interrupt.
4082a266c7d5SChris Wilson 		 *
4083a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4084a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4085a266c7d5SChris Wilson 		 * another one.
4086a266c7d5SChris Wilson 		 *
4087a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4088a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4089a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4090a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4091a266c7d5SChris Wilson 		 * stray interrupts.
4092a266c7d5SChris Wilson 		 */
4093a266c7d5SChris Wilson 		iir = new_iir;
4094a266c7d5SChris Wilson 	}
4095a266c7d5SChris Wilson 
40961f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
40971f814dacSImre Deak 
4098a266c7d5SChris Wilson 	return ret;
4099a266c7d5SChris Wilson }
4100a266c7d5SChris Wilson 
4101a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
4102a266c7d5SChris Wilson {
4103fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4104a266c7d5SChris Wilson 
4105a266c7d5SChris Wilson 	if (!dev_priv)
4106a266c7d5SChris Wilson 		return;
4107a266c7d5SChris Wilson 
41080706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4109a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4110a266c7d5SChris Wilson 
411144d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
411244d9241eSVille Syrjälä 
4113a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
411444d9241eSVille Syrjälä 
4115ba7eb789SVille Syrjälä 	GEN3_IRQ_RESET();
4116a266c7d5SChris Wilson }
4117a266c7d5SChris Wilson 
4118fca52a55SDaniel Vetter /**
4119fca52a55SDaniel Vetter  * intel_irq_init - initializes irq support
4120fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4121fca52a55SDaniel Vetter  *
4122fca52a55SDaniel Vetter  * This function initializes all the irq support including work items, timers
4123fca52a55SDaniel Vetter  * and all the vtables. It does not setup the interrupt itself though.
4124fca52a55SDaniel Vetter  */
4125b963291cSDaniel Vetter void intel_irq_init(struct drm_i915_private *dev_priv)
4126f71d4af4SJesse Barnes {
412791c8a326SChris Wilson 	struct drm_device *dev = &dev_priv->drm;
4128cefcff8fSJoonas Lahtinen 	int i;
41298b2e326dSChris Wilson 
413077913b39SJani Nikula 	intel_hpd_init_work(dev_priv);
413177913b39SJani Nikula 
4132c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
4133cefcff8fSJoonas Lahtinen 
4134a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
4135cefcff8fSJoonas Lahtinen 	for (i = 0; i < MAX_L3_SLICES; ++i)
4136cefcff8fSJoonas Lahtinen 		dev_priv->l3_parity.remap_info[i] = NULL;
41378b2e326dSChris Wilson 
41384805fe82STvrtko Ursulin 	if (HAS_GUC_SCHED(dev_priv))
413926705e20SSagar Arun Kamble 		dev_priv->pm_guc_events = GEN9_GUC_TO_HOST_INT_EVENT;
414026705e20SSagar Arun Kamble 
4141a6706b45SDeepak S 	/* Let's track the enabled rps events */
4142666a4537SWayne Boyer 	if (IS_VALLEYVIEW(dev_priv))
41436c65a587SVille Syrjälä 		/* WaGsvRC0ResidencyMethod:vlv */
4144e0e8c7cbSChris Wilson 		dev_priv->pm_rps_events = GEN6_PM_RP_UP_EI_EXPIRED;
414531685c25SDeepak S 	else
4146a6706b45SDeepak S 		dev_priv->pm_rps_events = GEN6_PM_RPS_EVENTS;
4147a6706b45SDeepak S 
41485dd04556SSagar Arun Kamble 	dev_priv->rps.pm_intrmsk_mbz = 0;
41491800ad25SSagar Arun Kamble 
41501800ad25SSagar Arun Kamble 	/*
4151acf2dc22SMika Kuoppala 	 * SNB,IVB,HSW can while VLV,CHV may hard hang on looping batchbuffer
41521800ad25SSagar Arun Kamble 	 * if GEN6_PM_UP_EI_EXPIRED is masked.
41531800ad25SSagar Arun Kamble 	 *
41541800ad25SSagar Arun Kamble 	 * TODO: verify if this can be reproduced on VLV,CHV.
41551800ad25SSagar Arun Kamble 	 */
4156bca2bf2aSPandiyan, Dhinakaran 	if (INTEL_GEN(dev_priv) <= 7)
41575dd04556SSagar Arun Kamble 		dev_priv->rps.pm_intrmsk_mbz |= GEN6_PM_RP_UP_EI_EXPIRED;
41581800ad25SSagar Arun Kamble 
4159bca2bf2aSPandiyan, Dhinakaran 	if (INTEL_GEN(dev_priv) >= 8)
4160655d49efSChris Wilson 		dev_priv->rps.pm_intrmsk_mbz |= GEN8_PMINTR_DISABLE_REDIRECT_TO_GUC;
41611800ad25SSagar Arun Kamble 
4162b963291cSDaniel Vetter 	if (IS_GEN2(dev_priv)) {
41634194c088SRodrigo Vivi 		/* Gen2 doesn't have a hardware frame counter */
41644cdb83ecSVille Syrjälä 		dev->max_vblank_count = 0;
4165bca2bf2aSPandiyan, Dhinakaran 	} else if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5) {
4166f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
4167fd8f507cSVille Syrjälä 		dev->driver->get_vblank_counter = g4x_get_vblank_counter;
4168391f75e2SVille Syrjälä 	} else {
4169391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
4170391f75e2SVille Syrjälä 		dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
4171f71d4af4SJesse Barnes 	}
4172f71d4af4SJesse Barnes 
417321da2700SVille Syrjälä 	/*
417421da2700SVille Syrjälä 	 * Opt out of the vblank disable timer on everything except gen2.
417521da2700SVille Syrjälä 	 * Gen2 doesn't have a hardware frame counter and so depends on
417621da2700SVille Syrjälä 	 * vblank interrupts to produce sane vblank seuquence numbers.
417721da2700SVille Syrjälä 	 */
4178b963291cSDaniel Vetter 	if (!IS_GEN2(dev_priv))
417921da2700SVille Syrjälä 		dev->vblank_disable_immediate = true;
418021da2700SVille Syrjälä 
4181262fd485SChris Wilson 	/* Most platforms treat the display irq block as an always-on
4182262fd485SChris Wilson 	 * power domain. vlv/chv can disable it at runtime and need
4183262fd485SChris Wilson 	 * special care to avoid writing any of the display block registers
4184262fd485SChris Wilson 	 * outside of the power domain. We defer setting up the display irqs
4185262fd485SChris Wilson 	 * in this case to the runtime pm.
4186262fd485SChris Wilson 	 */
4187262fd485SChris Wilson 	dev_priv->display_irqs_enabled = true;
4188262fd485SChris Wilson 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4189262fd485SChris Wilson 		dev_priv->display_irqs_enabled = false;
4190262fd485SChris Wilson 
4191317eaa95SLyude 	dev_priv->hotplug.hpd_storm_threshold = HPD_STORM_DEFAULT_THRESHOLD;
4192317eaa95SLyude 
41931bf6ad62SDaniel Vetter 	dev->driver->get_vblank_timestamp = drm_calc_vbltimestamp_from_scanoutpos;
4194f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
4195f71d4af4SJesse Barnes 
4196b963291cSDaniel Vetter 	if (IS_CHERRYVIEW(dev_priv)) {
419743f328d7SVille Syrjälä 		dev->driver->irq_handler = cherryview_irq_handler;
419843f328d7SVille Syrjälä 		dev->driver->irq_preinstall = cherryview_irq_preinstall;
419943f328d7SVille Syrjälä 		dev->driver->irq_postinstall = cherryview_irq_postinstall;
420043f328d7SVille Syrjälä 		dev->driver->irq_uninstall = cherryview_irq_uninstall;
420186e83e35SChris Wilson 		dev->driver->enable_vblank = i965_enable_vblank;
420286e83e35SChris Wilson 		dev->driver->disable_vblank = i965_disable_vblank;
420343f328d7SVille Syrjälä 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4204b963291cSDaniel Vetter 	} else if (IS_VALLEYVIEW(dev_priv)) {
42057e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
42067e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
42077e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
42087e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
420986e83e35SChris Wilson 		dev->driver->enable_vblank = i965_enable_vblank;
421086e83e35SChris Wilson 		dev->driver->disable_vblank = i965_disable_vblank;
4211fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4212bca2bf2aSPandiyan, Dhinakaran 	} else if (INTEL_GEN(dev_priv) >= 8) {
4213abd58f01SBen Widawsky 		dev->driver->irq_handler = gen8_irq_handler;
4214723761b8SDaniel Vetter 		dev->driver->irq_preinstall = gen8_irq_reset;
4215abd58f01SBen Widawsky 		dev->driver->irq_postinstall = gen8_irq_postinstall;
4216abd58f01SBen Widawsky 		dev->driver->irq_uninstall = gen8_irq_uninstall;
4217abd58f01SBen Widawsky 		dev->driver->enable_vblank = gen8_enable_vblank;
4218abd58f01SBen Widawsky 		dev->driver->disable_vblank = gen8_disable_vblank;
4219cc3f90f0SAnder Conselvan de Oliveira 		if (IS_GEN9_LP(dev_priv))
4220e0a20ad7SShashank Sharma 			dev_priv->display.hpd_irq_setup = bxt_hpd_irq_setup;
42217b22b8c4SRodrigo Vivi 		else if (HAS_PCH_SPT(dev_priv) || HAS_PCH_KBP(dev_priv) ||
42227b22b8c4SRodrigo Vivi 			 HAS_PCH_CNP(dev_priv))
42236dbf30ceSVille Syrjälä 			dev_priv->display.hpd_irq_setup = spt_hpd_irq_setup;
42246dbf30ceSVille Syrjälä 		else
42253a3b3c7dSVille Syrjälä 			dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
42266e266956STvrtko Ursulin 	} else if (HAS_PCH_SPLIT(dev_priv)) {
4227f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
4228723761b8SDaniel Vetter 		dev->driver->irq_preinstall = ironlake_irq_reset;
4229f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
4230f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
4231f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
4232f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
4233e4ce95aaSVille Syrjälä 		dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
4234f71d4af4SJesse Barnes 	} else {
42357e22dbbbSTvrtko Ursulin 		if (IS_GEN2(dev_priv)) {
4236c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
4237c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
4238c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
4239c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
424086e83e35SChris Wilson 			dev->driver->enable_vblank = i8xx_enable_vblank;
424186e83e35SChris Wilson 			dev->driver->disable_vblank = i8xx_disable_vblank;
42427e22dbbbSTvrtko Ursulin 		} else if (IS_GEN3(dev_priv)) {
4243a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
4244a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
4245a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
4246a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
424786e83e35SChris Wilson 			dev->driver->enable_vblank = i8xx_enable_vblank;
424886e83e35SChris Wilson 			dev->driver->disable_vblank = i8xx_disable_vblank;
4249c2798b19SChris Wilson 		} else {
4250a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
4251a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
4252a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
4253a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
425486e83e35SChris Wilson 			dev->driver->enable_vblank = i965_enable_vblank;
425586e83e35SChris Wilson 			dev->driver->disable_vblank = i965_disable_vblank;
4256c2798b19SChris Wilson 		}
4257778eb334SVille Syrjälä 		if (I915_HAS_HOTPLUG(dev_priv))
4258778eb334SVille Syrjälä 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4259f71d4af4SJesse Barnes 	}
4260f71d4af4SJesse Barnes }
426120afbda2SDaniel Vetter 
4262fca52a55SDaniel Vetter /**
4263cefcff8fSJoonas Lahtinen  * intel_irq_fini - deinitializes IRQ support
4264cefcff8fSJoonas Lahtinen  * @i915: i915 device instance
4265cefcff8fSJoonas Lahtinen  *
4266cefcff8fSJoonas Lahtinen  * This function deinitializes all the IRQ support.
4267cefcff8fSJoonas Lahtinen  */
4268cefcff8fSJoonas Lahtinen void intel_irq_fini(struct drm_i915_private *i915)
4269cefcff8fSJoonas Lahtinen {
4270cefcff8fSJoonas Lahtinen 	int i;
4271cefcff8fSJoonas Lahtinen 
4272cefcff8fSJoonas Lahtinen 	for (i = 0; i < MAX_L3_SLICES; ++i)
4273cefcff8fSJoonas Lahtinen 		kfree(i915->l3_parity.remap_info[i]);
4274cefcff8fSJoonas Lahtinen }
4275cefcff8fSJoonas Lahtinen 
4276cefcff8fSJoonas Lahtinen /**
4277fca52a55SDaniel Vetter  * intel_irq_install - enables the hardware interrupt
4278fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4279fca52a55SDaniel Vetter  *
4280fca52a55SDaniel Vetter  * This function enables the hardware interrupt handling, but leaves the hotplug
4281fca52a55SDaniel Vetter  * handling still disabled. It is called after intel_irq_init().
4282fca52a55SDaniel Vetter  *
4283fca52a55SDaniel Vetter  * In the driver load and resume code we need working interrupts in a few places
4284fca52a55SDaniel Vetter  * but don't want to deal with the hassle of concurrent probe and hotplug
4285fca52a55SDaniel Vetter  * workers. Hence the split into this two-stage approach.
4286fca52a55SDaniel Vetter  */
42872aeb7d3aSDaniel Vetter int intel_irq_install(struct drm_i915_private *dev_priv)
42882aeb7d3aSDaniel Vetter {
42892aeb7d3aSDaniel Vetter 	/*
42902aeb7d3aSDaniel Vetter 	 * We enable some interrupt sources in our postinstall hooks, so mark
42912aeb7d3aSDaniel Vetter 	 * interrupts as enabled _before_ actually enabling them to avoid
42922aeb7d3aSDaniel Vetter 	 * special cases in our ordering checks.
42932aeb7d3aSDaniel Vetter 	 */
42942aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
42952aeb7d3aSDaniel Vetter 
429691c8a326SChris Wilson 	return drm_irq_install(&dev_priv->drm, dev_priv->drm.pdev->irq);
42972aeb7d3aSDaniel Vetter }
42982aeb7d3aSDaniel Vetter 
4299fca52a55SDaniel Vetter /**
4300fca52a55SDaniel Vetter  * intel_irq_uninstall - finilizes all irq handling
4301fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4302fca52a55SDaniel Vetter  *
4303fca52a55SDaniel Vetter  * This stops interrupt and hotplug handling and unregisters and frees all
4304fca52a55SDaniel Vetter  * resources acquired in the init functions.
4305fca52a55SDaniel Vetter  */
43062aeb7d3aSDaniel Vetter void intel_irq_uninstall(struct drm_i915_private *dev_priv)
43072aeb7d3aSDaniel Vetter {
430891c8a326SChris Wilson 	drm_irq_uninstall(&dev_priv->drm);
43092aeb7d3aSDaniel Vetter 	intel_hpd_cancel_work(dev_priv);
43102aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
43112aeb7d3aSDaniel Vetter }
43122aeb7d3aSDaniel Vetter 
4313fca52a55SDaniel Vetter /**
4314fca52a55SDaniel Vetter  * intel_runtime_pm_disable_interrupts - runtime interrupt disabling
4315fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4316fca52a55SDaniel Vetter  *
4317fca52a55SDaniel Vetter  * This function is used to disable interrupts at runtime, both in the runtime
4318fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4319fca52a55SDaniel Vetter  */
4320b963291cSDaniel Vetter void intel_runtime_pm_disable_interrupts(struct drm_i915_private *dev_priv)
4321c67a470bSPaulo Zanoni {
432291c8a326SChris Wilson 	dev_priv->drm.driver->irq_uninstall(&dev_priv->drm);
43232aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
432491c8a326SChris Wilson 	synchronize_irq(dev_priv->drm.irq);
4325c67a470bSPaulo Zanoni }
4326c67a470bSPaulo Zanoni 
4327fca52a55SDaniel Vetter /**
4328fca52a55SDaniel Vetter  * intel_runtime_pm_enable_interrupts - runtime interrupt enabling
4329fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4330fca52a55SDaniel Vetter  *
4331fca52a55SDaniel Vetter  * This function is used to enable interrupts at runtime, both in the runtime
4332fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4333fca52a55SDaniel Vetter  */
4334b963291cSDaniel Vetter void intel_runtime_pm_enable_interrupts(struct drm_i915_private *dev_priv)
4335c67a470bSPaulo Zanoni {
43362aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
433791c8a326SChris Wilson 	dev_priv->drm.driver->irq_preinstall(&dev_priv->drm);
433891c8a326SChris Wilson 	dev_priv->drm.driver->irq_postinstall(&dev_priv->drm);
4339c67a470bSPaulo Zanoni }
4340