xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision ce80075470f6328e487389262c95af092d421ffc)
1c0e09200SDave Airlie /* i915_irq.c -- IRQ support for the I915 -*- linux-c -*-
2c0e09200SDave Airlie  */
3c0e09200SDave Airlie /*
4c0e09200SDave Airlie  * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
5c0e09200SDave Airlie  * All Rights Reserved.
6c0e09200SDave Airlie  *
7c0e09200SDave Airlie  * Permission is hereby granted, free of charge, to any person obtaining a
8c0e09200SDave Airlie  * copy of this software and associated documentation files (the
9c0e09200SDave Airlie  * "Software"), to deal in the Software without restriction, including
10c0e09200SDave Airlie  * without limitation the rights to use, copy, modify, merge, publish,
11c0e09200SDave Airlie  * distribute, sub license, and/or sell copies of the Software, and to
12c0e09200SDave Airlie  * permit persons to whom the Software is furnished to do so, subject to
13c0e09200SDave Airlie  * the following conditions:
14c0e09200SDave Airlie  *
15c0e09200SDave Airlie  * The above copyright notice and this permission notice (including the
16c0e09200SDave Airlie  * next paragraph) shall be included in all copies or substantial portions
17c0e09200SDave Airlie  * of the Software.
18c0e09200SDave Airlie  *
19c0e09200SDave Airlie  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20c0e09200SDave Airlie  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21c0e09200SDave Airlie  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22c0e09200SDave Airlie  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23c0e09200SDave Airlie  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24c0e09200SDave Airlie  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25c0e09200SDave Airlie  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26c0e09200SDave Airlie  *
27c0e09200SDave Airlie  */
28c0e09200SDave Airlie 
29a70491ccSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30a70491ccSJoe Perches 
3163eeaf38SJesse Barnes #include <linux/sysrq.h>
325a0e3ad6STejun Heo #include <linux/slab.h>
33b2c88f5bSDamien Lespiau #include <linux/circ_buf.h>
34760285e7SDavid Howells #include <drm/drmP.h>
35760285e7SDavid Howells #include <drm/i915_drm.h>
36c0e09200SDave Airlie #include "i915_drv.h"
371c5d22f7SChris Wilson #include "i915_trace.h"
3879e53945SJesse Barnes #include "intel_drv.h"
39c0e09200SDave Airlie 
40fca52a55SDaniel Vetter /**
41fca52a55SDaniel Vetter  * DOC: interrupt handling
42fca52a55SDaniel Vetter  *
43fca52a55SDaniel Vetter  * These functions provide the basic support for enabling and disabling the
44fca52a55SDaniel Vetter  * interrupt handling support. There's a lot more functionality in i915_irq.c
45fca52a55SDaniel Vetter  * and related files, but that will be described in separate chapters.
46fca52a55SDaniel Vetter  */
47fca52a55SDaniel Vetter 
48e4ce95aaSVille Syrjälä static const u32 hpd_ilk[HPD_NUM_PINS] = {
49e4ce95aaSVille Syrjälä 	[HPD_PORT_A] = DE_DP_A_HOTPLUG,
50e4ce95aaSVille Syrjälä };
51e4ce95aaSVille Syrjälä 
5223bb4cb5SVille Syrjälä static const u32 hpd_ivb[HPD_NUM_PINS] = {
5323bb4cb5SVille Syrjälä 	[HPD_PORT_A] = DE_DP_A_HOTPLUG_IVB,
5423bb4cb5SVille Syrjälä };
5523bb4cb5SVille Syrjälä 
563a3b3c7dSVille Syrjälä static const u32 hpd_bdw[HPD_NUM_PINS] = {
573a3b3c7dSVille Syrjälä 	[HPD_PORT_A] = GEN8_PORT_DP_A_HOTPLUG,
583a3b3c7dSVille Syrjälä };
593a3b3c7dSVille Syrjälä 
607c7e10dbSVille Syrjälä static const u32 hpd_ibx[HPD_NUM_PINS] = {
61e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG,
62e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG,
63e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG,
64e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG,
65e5868a31SEgbert Eich 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG
66e5868a31SEgbert Eich };
67e5868a31SEgbert Eich 
687c7e10dbSVille Syrjälä static const u32 hpd_cpt[HPD_NUM_PINS] = {
69e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG_CPT,
7073c352a2SDaniel Vetter 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG_CPT,
71e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
72e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
73e5868a31SEgbert Eich 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT
74e5868a31SEgbert Eich };
75e5868a31SEgbert Eich 
7626951cafSXiong Zhang static const u32 hpd_spt[HPD_NUM_PINS] = {
7774c0b395SVille Syrjälä 	[HPD_PORT_A] = SDE_PORTA_HOTPLUG_SPT,
7826951cafSXiong Zhang 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
7926951cafSXiong Zhang 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
8026951cafSXiong Zhang 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT,
8126951cafSXiong Zhang 	[HPD_PORT_E] = SDE_PORTE_HOTPLUG_SPT
8226951cafSXiong Zhang };
8326951cafSXiong Zhang 
847c7e10dbSVille Syrjälä static const u32 hpd_mask_i915[HPD_NUM_PINS] = {
85e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_EN,
86e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_EN,
87e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_EN,
88e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_EN,
89e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_EN,
90e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_EN
91e5868a31SEgbert Eich };
92e5868a31SEgbert Eich 
937c7e10dbSVille Syrjälä static const u32 hpd_status_g4x[HPD_NUM_PINS] = {
94e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
95e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_G4X,
96e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_G4X,
97e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
98e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
99e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
100e5868a31SEgbert Eich };
101e5868a31SEgbert Eich 
1024bca26d0SVille Syrjälä static const u32 hpd_status_i915[HPD_NUM_PINS] = {
103e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
104e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_I915,
105e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_I915,
106e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
107e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
108e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
109e5868a31SEgbert Eich };
110e5868a31SEgbert Eich 
111e0a20ad7SShashank Sharma /* BXT hpd list */
112e0a20ad7SShashank Sharma static const u32 hpd_bxt[HPD_NUM_PINS] = {
1137f3561beSSonika Jindal 	[HPD_PORT_A] = BXT_DE_PORT_HP_DDIA,
114e0a20ad7SShashank Sharma 	[HPD_PORT_B] = BXT_DE_PORT_HP_DDIB,
115e0a20ad7SShashank Sharma 	[HPD_PORT_C] = BXT_DE_PORT_HP_DDIC
116e0a20ad7SShashank Sharma };
117e0a20ad7SShashank Sharma 
1185c502442SPaulo Zanoni /* IIR can theoretically queue up two events. Be paranoid. */
119f86f3fb0SPaulo Zanoni #define GEN8_IRQ_RESET_NDX(type, which) do { \
1205c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IMR(which), 0xffffffff); \
1215c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IMR(which)); \
1225c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IER(which), 0); \
1235c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
1245c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IIR(which)); \
1255c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
1265c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IIR(which)); \
1275c502442SPaulo Zanoni } while (0)
1285c502442SPaulo Zanoni 
1293488d4ebSVille Syrjälä #define GEN3_IRQ_RESET(type) do { \
130a9d356a6SPaulo Zanoni 	I915_WRITE(type##IMR, 0xffffffff); \
1315c502442SPaulo Zanoni 	POSTING_READ(type##IMR); \
132a9d356a6SPaulo Zanoni 	I915_WRITE(type##IER, 0); \
1335c502442SPaulo Zanoni 	I915_WRITE(type##IIR, 0xffffffff); \
1345c502442SPaulo Zanoni 	POSTING_READ(type##IIR); \
1355c502442SPaulo Zanoni 	I915_WRITE(type##IIR, 0xffffffff); \
1365c502442SPaulo Zanoni 	POSTING_READ(type##IIR); \
137a9d356a6SPaulo Zanoni } while (0)
138a9d356a6SPaulo Zanoni 
139e9e9848aSVille Syrjälä #define GEN2_IRQ_RESET(type) do { \
140e9e9848aSVille Syrjälä 	I915_WRITE16(type##IMR, 0xffff); \
141e9e9848aSVille Syrjälä 	POSTING_READ16(type##IMR); \
142e9e9848aSVille Syrjälä 	I915_WRITE16(type##IER, 0); \
143e9e9848aSVille Syrjälä 	I915_WRITE16(type##IIR, 0xffff); \
144e9e9848aSVille Syrjälä 	POSTING_READ16(type##IIR); \
145e9e9848aSVille Syrjälä 	I915_WRITE16(type##IIR, 0xffff); \
146e9e9848aSVille Syrjälä 	POSTING_READ16(type##IIR); \
147e9e9848aSVille Syrjälä } while (0)
148e9e9848aSVille Syrjälä 
149337ba017SPaulo Zanoni /*
150337ba017SPaulo Zanoni  * We should clear IMR at preinstall/uninstall, and just check at postinstall.
151337ba017SPaulo Zanoni  */
1523488d4ebSVille Syrjälä static void gen3_assert_iir_is_zero(struct drm_i915_private *dev_priv,
153f0f59a00SVille Syrjälä 				    i915_reg_t reg)
154b51a2842SVille Syrjälä {
155b51a2842SVille Syrjälä 	u32 val = I915_READ(reg);
156b51a2842SVille Syrjälä 
157b51a2842SVille Syrjälä 	if (val == 0)
158b51a2842SVille Syrjälä 		return;
159b51a2842SVille Syrjälä 
160b51a2842SVille Syrjälä 	WARN(1, "Interrupt register 0x%x is not zero: 0x%08x\n",
161f0f59a00SVille Syrjälä 	     i915_mmio_reg_offset(reg), val);
162b51a2842SVille Syrjälä 	I915_WRITE(reg, 0xffffffff);
163b51a2842SVille Syrjälä 	POSTING_READ(reg);
164b51a2842SVille Syrjälä 	I915_WRITE(reg, 0xffffffff);
165b51a2842SVille Syrjälä 	POSTING_READ(reg);
166b51a2842SVille Syrjälä }
167337ba017SPaulo Zanoni 
168e9e9848aSVille Syrjälä static void gen2_assert_iir_is_zero(struct drm_i915_private *dev_priv,
169e9e9848aSVille Syrjälä 				    i915_reg_t reg)
170e9e9848aSVille Syrjälä {
171e9e9848aSVille Syrjälä 	u16 val = I915_READ16(reg);
172e9e9848aSVille Syrjälä 
173e9e9848aSVille Syrjälä 	if (val == 0)
174e9e9848aSVille Syrjälä 		return;
175e9e9848aSVille Syrjälä 
176e9e9848aSVille Syrjälä 	WARN(1, "Interrupt register 0x%x is not zero: 0x%08x\n",
177e9e9848aSVille Syrjälä 	     i915_mmio_reg_offset(reg), val);
178e9e9848aSVille Syrjälä 	I915_WRITE16(reg, 0xffff);
179e9e9848aSVille Syrjälä 	POSTING_READ16(reg);
180e9e9848aSVille Syrjälä 	I915_WRITE16(reg, 0xffff);
181e9e9848aSVille Syrjälä 	POSTING_READ16(reg);
182e9e9848aSVille Syrjälä }
183e9e9848aSVille Syrjälä 
18435079899SPaulo Zanoni #define GEN8_IRQ_INIT_NDX(type, which, imr_val, ier_val) do { \
1853488d4ebSVille Syrjälä 	gen3_assert_iir_is_zero(dev_priv, GEN8_##type##_IIR(which)); \
18635079899SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IER(which), (ier_val)); \
1877d1bd539SVille Syrjälä 	I915_WRITE(GEN8_##type##_IMR(which), (imr_val)); \
1887d1bd539SVille Syrjälä 	POSTING_READ(GEN8_##type##_IMR(which)); \
18935079899SPaulo Zanoni } while (0)
19035079899SPaulo Zanoni 
1913488d4ebSVille Syrjälä #define GEN3_IRQ_INIT(type, imr_val, ier_val) do { \
1923488d4ebSVille Syrjälä 	gen3_assert_iir_is_zero(dev_priv, type##IIR); \
19335079899SPaulo Zanoni 	I915_WRITE(type##IER, (ier_val)); \
1947d1bd539SVille Syrjälä 	I915_WRITE(type##IMR, (imr_val)); \
1957d1bd539SVille Syrjälä 	POSTING_READ(type##IMR); \
19635079899SPaulo Zanoni } while (0)
19735079899SPaulo Zanoni 
198e9e9848aSVille Syrjälä #define GEN2_IRQ_INIT(type, imr_val, ier_val) do { \
199e9e9848aSVille Syrjälä 	gen2_assert_iir_is_zero(dev_priv, type##IIR); \
200e9e9848aSVille Syrjälä 	I915_WRITE16(type##IER, (ier_val)); \
201e9e9848aSVille Syrjälä 	I915_WRITE16(type##IMR, (imr_val)); \
202e9e9848aSVille Syrjälä 	POSTING_READ16(type##IMR); \
203e9e9848aSVille Syrjälä } while (0)
204e9e9848aSVille Syrjälä 
205c9a9a268SImre Deak static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir);
20626705e20SSagar Arun Kamble static void gen9_guc_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir);
207c9a9a268SImre Deak 
2080706f17cSEgbert Eich /* For display hotplug interrupt */
2090706f17cSEgbert Eich static inline void
2100706f17cSEgbert Eich i915_hotplug_interrupt_update_locked(struct drm_i915_private *dev_priv,
2110706f17cSEgbert Eich 				     uint32_t mask,
2120706f17cSEgbert Eich 				     uint32_t bits)
2130706f17cSEgbert Eich {
2140706f17cSEgbert Eich 	uint32_t val;
2150706f17cSEgbert Eich 
21667520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
2170706f17cSEgbert Eich 	WARN_ON(bits & ~mask);
2180706f17cSEgbert Eich 
2190706f17cSEgbert Eich 	val = I915_READ(PORT_HOTPLUG_EN);
2200706f17cSEgbert Eich 	val &= ~mask;
2210706f17cSEgbert Eich 	val |= bits;
2220706f17cSEgbert Eich 	I915_WRITE(PORT_HOTPLUG_EN, val);
2230706f17cSEgbert Eich }
2240706f17cSEgbert Eich 
2250706f17cSEgbert Eich /**
2260706f17cSEgbert Eich  * i915_hotplug_interrupt_update - update hotplug interrupt enable
2270706f17cSEgbert Eich  * @dev_priv: driver private
2280706f17cSEgbert Eich  * @mask: bits to update
2290706f17cSEgbert Eich  * @bits: bits to enable
2300706f17cSEgbert Eich  * NOTE: the HPD enable bits are modified both inside and outside
2310706f17cSEgbert Eich  * of an interrupt context. To avoid that read-modify-write cycles
2320706f17cSEgbert Eich  * interfer, these bits are protected by a spinlock. Since this
2330706f17cSEgbert Eich  * function is usually not called from a context where the lock is
2340706f17cSEgbert Eich  * held already, this function acquires the lock itself. A non-locking
2350706f17cSEgbert Eich  * version is also available.
2360706f17cSEgbert Eich  */
2370706f17cSEgbert Eich void i915_hotplug_interrupt_update(struct drm_i915_private *dev_priv,
2380706f17cSEgbert Eich 				   uint32_t mask,
2390706f17cSEgbert Eich 				   uint32_t bits)
2400706f17cSEgbert Eich {
2410706f17cSEgbert Eich 	spin_lock_irq(&dev_priv->irq_lock);
2420706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv, mask, bits);
2430706f17cSEgbert Eich 	spin_unlock_irq(&dev_priv->irq_lock);
2440706f17cSEgbert Eich }
2450706f17cSEgbert Eich 
246d9dc34f1SVille Syrjälä /**
247d9dc34f1SVille Syrjälä  * ilk_update_display_irq - update DEIMR
248d9dc34f1SVille Syrjälä  * @dev_priv: driver private
249d9dc34f1SVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
250d9dc34f1SVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
251d9dc34f1SVille Syrjälä  */
252fbdedaeaSVille Syrjälä void ilk_update_display_irq(struct drm_i915_private *dev_priv,
253d9dc34f1SVille Syrjälä 			    uint32_t interrupt_mask,
254d9dc34f1SVille Syrjälä 			    uint32_t enabled_irq_mask)
255036a4a7dSZhenyu Wang {
256d9dc34f1SVille Syrjälä 	uint32_t new_val;
257d9dc34f1SVille Syrjälä 
25867520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
2594bc9d430SDaniel Vetter 
260d9dc34f1SVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
261d9dc34f1SVille Syrjälä 
2629df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
263c67a470bSPaulo Zanoni 		return;
264c67a470bSPaulo Zanoni 
265d9dc34f1SVille Syrjälä 	new_val = dev_priv->irq_mask;
266d9dc34f1SVille Syrjälä 	new_val &= ~interrupt_mask;
267d9dc34f1SVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
268d9dc34f1SVille Syrjälä 
269d9dc34f1SVille Syrjälä 	if (new_val != dev_priv->irq_mask) {
270d9dc34f1SVille Syrjälä 		dev_priv->irq_mask = new_val;
2711ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
2723143a2bfSChris Wilson 		POSTING_READ(DEIMR);
273036a4a7dSZhenyu Wang 	}
274036a4a7dSZhenyu Wang }
275036a4a7dSZhenyu Wang 
27643eaea13SPaulo Zanoni /**
27743eaea13SPaulo Zanoni  * ilk_update_gt_irq - update GTIMR
27843eaea13SPaulo Zanoni  * @dev_priv: driver private
27943eaea13SPaulo Zanoni  * @interrupt_mask: mask of interrupt bits to update
28043eaea13SPaulo Zanoni  * @enabled_irq_mask: mask of interrupt bits to enable
28143eaea13SPaulo Zanoni  */
28243eaea13SPaulo Zanoni static void ilk_update_gt_irq(struct drm_i915_private *dev_priv,
28343eaea13SPaulo Zanoni 			      uint32_t interrupt_mask,
28443eaea13SPaulo Zanoni 			      uint32_t enabled_irq_mask)
28543eaea13SPaulo Zanoni {
28667520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
28743eaea13SPaulo Zanoni 
28815a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
28915a17aaeSDaniel Vetter 
2909df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
291c67a470bSPaulo Zanoni 		return;
292c67a470bSPaulo Zanoni 
29343eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask &= ~interrupt_mask;
29443eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask |= (~enabled_irq_mask & interrupt_mask);
29543eaea13SPaulo Zanoni 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
29643eaea13SPaulo Zanoni }
29743eaea13SPaulo Zanoni 
298480c8033SDaniel Vetter void gen5_enable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
29943eaea13SPaulo Zanoni {
30043eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, mask);
30131bb59ccSChris Wilson 	POSTING_READ_FW(GTIMR);
30243eaea13SPaulo Zanoni }
30343eaea13SPaulo Zanoni 
304480c8033SDaniel Vetter void gen5_disable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
30543eaea13SPaulo Zanoni {
30643eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, 0);
30743eaea13SPaulo Zanoni }
30843eaea13SPaulo Zanoni 
309f0f59a00SVille Syrjälä static i915_reg_t gen6_pm_iir(struct drm_i915_private *dev_priv)
310b900b949SImre Deak {
311bca2bf2aSPandiyan, Dhinakaran 	return INTEL_GEN(dev_priv) >= 8 ? GEN8_GT_IIR(2) : GEN6_PMIIR;
312b900b949SImre Deak }
313b900b949SImre Deak 
314f0f59a00SVille Syrjälä static i915_reg_t gen6_pm_imr(struct drm_i915_private *dev_priv)
315a72fbc3aSImre Deak {
316bca2bf2aSPandiyan, Dhinakaran 	return INTEL_GEN(dev_priv) >= 8 ? GEN8_GT_IMR(2) : GEN6_PMIMR;
317a72fbc3aSImre Deak }
318a72fbc3aSImre Deak 
319f0f59a00SVille Syrjälä static i915_reg_t gen6_pm_ier(struct drm_i915_private *dev_priv)
320b900b949SImre Deak {
321bca2bf2aSPandiyan, Dhinakaran 	return INTEL_GEN(dev_priv) >= 8 ? GEN8_GT_IER(2) : GEN6_PMIER;
322b900b949SImre Deak }
323b900b949SImre Deak 
324edbfdb45SPaulo Zanoni /**
325edbfdb45SPaulo Zanoni  * snb_update_pm_irq - update GEN6_PMIMR
326edbfdb45SPaulo Zanoni  * @dev_priv: driver private
327edbfdb45SPaulo Zanoni  * @interrupt_mask: mask of interrupt bits to update
328edbfdb45SPaulo Zanoni  * @enabled_irq_mask: mask of interrupt bits to enable
329edbfdb45SPaulo Zanoni  */
330edbfdb45SPaulo Zanoni static void snb_update_pm_irq(struct drm_i915_private *dev_priv,
331edbfdb45SPaulo Zanoni 			      uint32_t interrupt_mask,
332edbfdb45SPaulo Zanoni 			      uint32_t enabled_irq_mask)
333edbfdb45SPaulo Zanoni {
334605cd25bSPaulo Zanoni 	uint32_t new_val;
335edbfdb45SPaulo Zanoni 
33615a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
33715a17aaeSDaniel Vetter 
33867520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
339edbfdb45SPaulo Zanoni 
340f4e9af4fSAkash Goel 	new_val = dev_priv->pm_imr;
341f52ecbcfSPaulo Zanoni 	new_val &= ~interrupt_mask;
342f52ecbcfSPaulo Zanoni 	new_val |= (~enabled_irq_mask & interrupt_mask);
343f52ecbcfSPaulo Zanoni 
344f4e9af4fSAkash Goel 	if (new_val != dev_priv->pm_imr) {
345f4e9af4fSAkash Goel 		dev_priv->pm_imr = new_val;
346f4e9af4fSAkash Goel 		I915_WRITE(gen6_pm_imr(dev_priv), dev_priv->pm_imr);
347a72fbc3aSImre Deak 		POSTING_READ(gen6_pm_imr(dev_priv));
348edbfdb45SPaulo Zanoni 	}
349f52ecbcfSPaulo Zanoni }
350edbfdb45SPaulo Zanoni 
351f4e9af4fSAkash Goel void gen6_unmask_pm_irq(struct drm_i915_private *dev_priv, u32 mask)
352edbfdb45SPaulo Zanoni {
3539939fba2SImre Deak 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
3549939fba2SImre Deak 		return;
3559939fba2SImre Deak 
356edbfdb45SPaulo Zanoni 	snb_update_pm_irq(dev_priv, mask, mask);
357edbfdb45SPaulo Zanoni }
358edbfdb45SPaulo Zanoni 
359f4e9af4fSAkash Goel static void __gen6_mask_pm_irq(struct drm_i915_private *dev_priv, u32 mask)
3609939fba2SImre Deak {
3619939fba2SImre Deak 	snb_update_pm_irq(dev_priv, mask, 0);
3629939fba2SImre Deak }
3639939fba2SImre Deak 
364f4e9af4fSAkash Goel void gen6_mask_pm_irq(struct drm_i915_private *dev_priv, u32 mask)
365edbfdb45SPaulo Zanoni {
3669939fba2SImre Deak 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
3679939fba2SImre Deak 		return;
3689939fba2SImre Deak 
369f4e9af4fSAkash Goel 	__gen6_mask_pm_irq(dev_priv, mask);
370f4e9af4fSAkash Goel }
371f4e9af4fSAkash Goel 
3723814fd77SOscar Mateo static void gen6_reset_pm_iir(struct drm_i915_private *dev_priv, u32 reset_mask)
373f4e9af4fSAkash Goel {
374f4e9af4fSAkash Goel 	i915_reg_t reg = gen6_pm_iir(dev_priv);
375f4e9af4fSAkash Goel 
37667520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
377f4e9af4fSAkash Goel 
378f4e9af4fSAkash Goel 	I915_WRITE(reg, reset_mask);
379f4e9af4fSAkash Goel 	I915_WRITE(reg, reset_mask);
380f4e9af4fSAkash Goel 	POSTING_READ(reg);
381f4e9af4fSAkash Goel }
382f4e9af4fSAkash Goel 
3833814fd77SOscar Mateo static void gen6_enable_pm_irq(struct drm_i915_private *dev_priv, u32 enable_mask)
384f4e9af4fSAkash Goel {
38567520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
386f4e9af4fSAkash Goel 
387f4e9af4fSAkash Goel 	dev_priv->pm_ier |= enable_mask;
388f4e9af4fSAkash Goel 	I915_WRITE(gen6_pm_ier(dev_priv), dev_priv->pm_ier);
389f4e9af4fSAkash Goel 	gen6_unmask_pm_irq(dev_priv, enable_mask);
390f4e9af4fSAkash Goel 	/* unmask_pm_irq provides an implicit barrier (POSTING_READ) */
391f4e9af4fSAkash Goel }
392f4e9af4fSAkash Goel 
3933814fd77SOscar Mateo static void gen6_disable_pm_irq(struct drm_i915_private *dev_priv, u32 disable_mask)
394f4e9af4fSAkash Goel {
39567520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
396f4e9af4fSAkash Goel 
397f4e9af4fSAkash Goel 	dev_priv->pm_ier &= ~disable_mask;
398f4e9af4fSAkash Goel 	__gen6_mask_pm_irq(dev_priv, disable_mask);
399f4e9af4fSAkash Goel 	I915_WRITE(gen6_pm_ier(dev_priv), dev_priv->pm_ier);
400f4e9af4fSAkash Goel 	/* though a barrier is missing here, but don't really need a one */
401edbfdb45SPaulo Zanoni }
402edbfdb45SPaulo Zanoni 
403dc97997aSChris Wilson void gen6_reset_rps_interrupts(struct drm_i915_private *dev_priv)
4043cc134e3SImre Deak {
4053cc134e3SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
406f4e9af4fSAkash Goel 	gen6_reset_pm_iir(dev_priv, dev_priv->pm_rps_events);
407562d9baeSSagar Arun Kamble 	dev_priv->gt_pm.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 {
413562d9baeSSagar Arun Kamble 	struct intel_rps *rps = &dev_priv->gt_pm.rps;
414562d9baeSSagar Arun Kamble 
415562d9baeSSagar Arun Kamble 	if (READ_ONCE(rps->interrupts_enabled))
416f2a91d1aSChris Wilson 		return;
417f2a91d1aSChris Wilson 
41851951ae7SMika Kuoppala 	if (WARN_ON_ONCE(IS_GEN11(dev_priv)))
41951951ae7SMika Kuoppala 		return;
42051951ae7SMika Kuoppala 
421b900b949SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
422562d9baeSSagar Arun Kamble 	WARN_ON_ONCE(rps->pm_iir);
423c33d247dSChris Wilson 	WARN_ON_ONCE(I915_READ(gen6_pm_iir(dev_priv)) & dev_priv->pm_rps_events);
424562d9baeSSagar Arun Kamble 	rps->interrupts_enabled = true;
425b900b949SImre Deak 	gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
42678e68d36SImre Deak 
427b900b949SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
428b900b949SImre Deak }
429b900b949SImre Deak 
43091d14251STvrtko Ursulin void gen6_disable_rps_interrupts(struct drm_i915_private *dev_priv)
431b900b949SImre Deak {
432562d9baeSSagar Arun Kamble 	struct intel_rps *rps = &dev_priv->gt_pm.rps;
433562d9baeSSagar Arun Kamble 
434562d9baeSSagar Arun Kamble 	if (!READ_ONCE(rps->interrupts_enabled))
435f2a91d1aSChris Wilson 		return;
436f2a91d1aSChris Wilson 
43751951ae7SMika Kuoppala 	if (WARN_ON_ONCE(IS_GEN11(dev_priv)))
43851951ae7SMika Kuoppala 		return;
43951951ae7SMika Kuoppala 
440d4d70aa5SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
441562d9baeSSagar Arun Kamble 	rps->interrupts_enabled = false;
4429939fba2SImre Deak 
443b20e3cfeSDave Gordon 	I915_WRITE(GEN6_PMINTRMSK, gen6_sanitize_rps_pm_mask(dev_priv, ~0u));
4449939fba2SImre Deak 
445f4e9af4fSAkash Goel 	gen6_disable_pm_irq(dev_priv, dev_priv->pm_rps_events);
44658072ccbSImre Deak 
44758072ccbSImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
44891c8a326SChris Wilson 	synchronize_irq(dev_priv->drm.irq);
449c33d247dSChris Wilson 
450c33d247dSChris Wilson 	/* Now that we will not be generating any more work, flush any
4513814fd77SOscar Mateo 	 * outstanding tasks. As we are called on the RPS idle path,
452c33d247dSChris Wilson 	 * we will reset the GPU to minimum frequencies, so the current
453c33d247dSChris Wilson 	 * state of the worker can be discarded.
454c33d247dSChris Wilson 	 */
455562d9baeSSagar Arun Kamble 	cancel_work_sync(&rps->work);
456c33d247dSChris Wilson 	gen6_reset_rps_interrupts(dev_priv);
457b900b949SImre Deak }
458b900b949SImre Deak 
45926705e20SSagar Arun Kamble void gen9_reset_guc_interrupts(struct drm_i915_private *dev_priv)
46026705e20SSagar Arun Kamble {
4611be333d3SSagar Arun Kamble 	assert_rpm_wakelock_held(dev_priv);
4621be333d3SSagar Arun Kamble 
46326705e20SSagar Arun Kamble 	spin_lock_irq(&dev_priv->irq_lock);
46426705e20SSagar Arun Kamble 	gen6_reset_pm_iir(dev_priv, dev_priv->pm_guc_events);
46526705e20SSagar Arun Kamble 	spin_unlock_irq(&dev_priv->irq_lock);
46626705e20SSagar Arun Kamble }
46726705e20SSagar Arun Kamble 
46826705e20SSagar Arun Kamble void gen9_enable_guc_interrupts(struct drm_i915_private *dev_priv)
46926705e20SSagar Arun Kamble {
4701be333d3SSagar Arun Kamble 	assert_rpm_wakelock_held(dev_priv);
4711be333d3SSagar Arun Kamble 
47226705e20SSagar Arun Kamble 	spin_lock_irq(&dev_priv->irq_lock);
47326705e20SSagar Arun Kamble 	if (!dev_priv->guc.interrupts_enabled) {
47426705e20SSagar Arun Kamble 		WARN_ON_ONCE(I915_READ(gen6_pm_iir(dev_priv)) &
47526705e20SSagar Arun Kamble 				       dev_priv->pm_guc_events);
47626705e20SSagar Arun Kamble 		dev_priv->guc.interrupts_enabled = true;
47726705e20SSagar Arun Kamble 		gen6_enable_pm_irq(dev_priv, dev_priv->pm_guc_events);
47826705e20SSagar Arun Kamble 	}
47926705e20SSagar Arun Kamble 	spin_unlock_irq(&dev_priv->irq_lock);
48026705e20SSagar Arun Kamble }
48126705e20SSagar Arun Kamble 
48226705e20SSagar Arun Kamble void gen9_disable_guc_interrupts(struct drm_i915_private *dev_priv)
48326705e20SSagar Arun Kamble {
4841be333d3SSagar Arun Kamble 	assert_rpm_wakelock_held(dev_priv);
4851be333d3SSagar Arun Kamble 
48626705e20SSagar Arun Kamble 	spin_lock_irq(&dev_priv->irq_lock);
48726705e20SSagar Arun Kamble 	dev_priv->guc.interrupts_enabled = false;
48826705e20SSagar Arun Kamble 
48926705e20SSagar Arun Kamble 	gen6_disable_pm_irq(dev_priv, dev_priv->pm_guc_events);
49026705e20SSagar Arun Kamble 
49126705e20SSagar Arun Kamble 	spin_unlock_irq(&dev_priv->irq_lock);
49226705e20SSagar Arun Kamble 	synchronize_irq(dev_priv->drm.irq);
49326705e20SSagar Arun Kamble 
49426705e20SSagar Arun Kamble 	gen9_reset_guc_interrupts(dev_priv);
49526705e20SSagar Arun Kamble }
49626705e20SSagar Arun Kamble 
4970961021aSBen Widawsky /**
4983a3b3c7dSVille Syrjälä  * bdw_update_port_irq - update DE port interrupt
4993a3b3c7dSVille Syrjälä  * @dev_priv: driver private
5003a3b3c7dSVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
5013a3b3c7dSVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
5023a3b3c7dSVille Syrjälä  */
5033a3b3c7dSVille Syrjälä static void bdw_update_port_irq(struct drm_i915_private *dev_priv,
5043a3b3c7dSVille Syrjälä 				uint32_t interrupt_mask,
5053a3b3c7dSVille Syrjälä 				uint32_t enabled_irq_mask)
5063a3b3c7dSVille Syrjälä {
5073a3b3c7dSVille Syrjälä 	uint32_t new_val;
5083a3b3c7dSVille Syrjälä 	uint32_t old_val;
5093a3b3c7dSVille Syrjälä 
51067520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
5113a3b3c7dSVille Syrjälä 
5123a3b3c7dSVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
5133a3b3c7dSVille Syrjälä 
5143a3b3c7dSVille Syrjälä 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
5153a3b3c7dSVille Syrjälä 		return;
5163a3b3c7dSVille Syrjälä 
5173a3b3c7dSVille Syrjälä 	old_val = I915_READ(GEN8_DE_PORT_IMR);
5183a3b3c7dSVille Syrjälä 
5193a3b3c7dSVille Syrjälä 	new_val = old_val;
5203a3b3c7dSVille Syrjälä 	new_val &= ~interrupt_mask;
5213a3b3c7dSVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
5223a3b3c7dSVille Syrjälä 
5233a3b3c7dSVille Syrjälä 	if (new_val != old_val) {
5243a3b3c7dSVille Syrjälä 		I915_WRITE(GEN8_DE_PORT_IMR, new_val);
5253a3b3c7dSVille Syrjälä 		POSTING_READ(GEN8_DE_PORT_IMR);
5263a3b3c7dSVille Syrjälä 	}
5273a3b3c7dSVille Syrjälä }
5283a3b3c7dSVille Syrjälä 
5293a3b3c7dSVille Syrjälä /**
530013d3752SVille Syrjälä  * bdw_update_pipe_irq - update DE pipe interrupt
531013d3752SVille Syrjälä  * @dev_priv: driver private
532013d3752SVille Syrjälä  * @pipe: pipe whose interrupt to update
533013d3752SVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
534013d3752SVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
535013d3752SVille Syrjälä  */
536013d3752SVille Syrjälä void bdw_update_pipe_irq(struct drm_i915_private *dev_priv,
537013d3752SVille Syrjälä 			 enum pipe pipe,
538013d3752SVille Syrjälä 			 uint32_t interrupt_mask,
539013d3752SVille Syrjälä 			 uint32_t enabled_irq_mask)
540013d3752SVille Syrjälä {
541013d3752SVille Syrjälä 	uint32_t new_val;
542013d3752SVille Syrjälä 
54367520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
544013d3752SVille Syrjälä 
545013d3752SVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
546013d3752SVille Syrjälä 
547013d3752SVille Syrjälä 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
548013d3752SVille Syrjälä 		return;
549013d3752SVille Syrjälä 
550013d3752SVille Syrjälä 	new_val = dev_priv->de_irq_mask[pipe];
551013d3752SVille Syrjälä 	new_val &= ~interrupt_mask;
552013d3752SVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
553013d3752SVille Syrjälä 
554013d3752SVille Syrjälä 	if (new_val != dev_priv->de_irq_mask[pipe]) {
555013d3752SVille Syrjälä 		dev_priv->de_irq_mask[pipe] = new_val;
556013d3752SVille Syrjälä 		I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
557013d3752SVille Syrjälä 		POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
558013d3752SVille Syrjälä 	}
559013d3752SVille Syrjälä }
560013d3752SVille Syrjälä 
561013d3752SVille Syrjälä /**
562fee884edSDaniel Vetter  * ibx_display_interrupt_update - update SDEIMR
563fee884edSDaniel Vetter  * @dev_priv: driver private
564fee884edSDaniel Vetter  * @interrupt_mask: mask of interrupt bits to update
565fee884edSDaniel Vetter  * @enabled_irq_mask: mask of interrupt bits to enable
566fee884edSDaniel Vetter  */
56747339cd9SDaniel Vetter void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
568fee884edSDaniel Vetter 				  uint32_t interrupt_mask,
569fee884edSDaniel Vetter 				  uint32_t enabled_irq_mask)
570fee884edSDaniel Vetter {
571fee884edSDaniel Vetter 	uint32_t sdeimr = I915_READ(SDEIMR);
572fee884edSDaniel Vetter 	sdeimr &= ~interrupt_mask;
573fee884edSDaniel Vetter 	sdeimr |= (~enabled_irq_mask & interrupt_mask);
574fee884edSDaniel Vetter 
57515a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
57615a17aaeSDaniel Vetter 
57767520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
578fee884edSDaniel Vetter 
5799df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
580c67a470bSPaulo Zanoni 		return;
581c67a470bSPaulo Zanoni 
582fee884edSDaniel Vetter 	I915_WRITE(SDEIMR, sdeimr);
583fee884edSDaniel Vetter 	POSTING_READ(SDEIMR);
584fee884edSDaniel Vetter }
5858664281bSPaulo Zanoni 
5866b12ca56SVille Syrjälä u32 i915_pipestat_enable_mask(struct drm_i915_private *dev_priv,
5876b12ca56SVille Syrjälä 			      enum pipe pipe)
5887c463586SKeith Packard {
5896b12ca56SVille Syrjälä 	u32 status_mask = dev_priv->pipestat_irq_mask[pipe];
59010c59c51SImre Deak 	u32 enable_mask = status_mask << 16;
59110c59c51SImre Deak 
5926b12ca56SVille Syrjälä 	lockdep_assert_held(&dev_priv->irq_lock);
5936b12ca56SVille Syrjälä 
5946b12ca56SVille Syrjälä 	if (INTEL_GEN(dev_priv) < 5)
5956b12ca56SVille Syrjälä 		goto out;
5966b12ca56SVille Syrjälä 
59710c59c51SImre Deak 	/*
598724a6905SVille Syrjälä 	 * On pipe A we don't support the PSR interrupt yet,
599724a6905SVille Syrjälä 	 * on pipe B and C the same bit MBZ.
60010c59c51SImre Deak 	 */
60110c59c51SImre Deak 	if (WARN_ON_ONCE(status_mask & PIPE_A_PSR_STATUS_VLV))
60210c59c51SImre Deak 		return 0;
603724a6905SVille Syrjälä 	/*
604724a6905SVille Syrjälä 	 * On pipe B and C we don't support the PSR interrupt yet, on pipe
605724a6905SVille Syrjälä 	 * A the same bit is for perf counters which we don't use either.
606724a6905SVille Syrjälä 	 */
607724a6905SVille Syrjälä 	if (WARN_ON_ONCE(status_mask & PIPE_B_PSR_STATUS_VLV))
608724a6905SVille Syrjälä 		return 0;
60910c59c51SImre Deak 
61010c59c51SImre Deak 	enable_mask &= ~(PIPE_FIFO_UNDERRUN_STATUS |
61110c59c51SImre Deak 			 SPRITE0_FLIP_DONE_INT_EN_VLV |
61210c59c51SImre Deak 			 SPRITE1_FLIP_DONE_INT_EN_VLV);
61310c59c51SImre Deak 	if (status_mask & SPRITE0_FLIP_DONE_INT_STATUS_VLV)
61410c59c51SImre Deak 		enable_mask |= SPRITE0_FLIP_DONE_INT_EN_VLV;
61510c59c51SImre Deak 	if (status_mask & SPRITE1_FLIP_DONE_INT_STATUS_VLV)
61610c59c51SImre Deak 		enable_mask |= SPRITE1_FLIP_DONE_INT_EN_VLV;
61710c59c51SImre Deak 
6186b12ca56SVille Syrjälä out:
6196b12ca56SVille Syrjälä 	WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
6206b12ca56SVille Syrjälä 		  status_mask & ~PIPESTAT_INT_STATUS_MASK,
6216b12ca56SVille Syrjälä 		  "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
6226b12ca56SVille Syrjälä 		  pipe_name(pipe), enable_mask, status_mask);
6236b12ca56SVille Syrjälä 
62410c59c51SImre Deak 	return enable_mask;
62510c59c51SImre Deak }
62610c59c51SImre Deak 
6276b12ca56SVille Syrjälä void i915_enable_pipestat(struct drm_i915_private *dev_priv,
6286b12ca56SVille Syrjälä 			  enum pipe pipe, u32 status_mask)
629755e9019SImre Deak {
6306b12ca56SVille Syrjälä 	i915_reg_t reg = PIPESTAT(pipe);
631755e9019SImre Deak 	u32 enable_mask;
632755e9019SImre Deak 
6336b12ca56SVille Syrjälä 	WARN_ONCE(status_mask & ~PIPESTAT_INT_STATUS_MASK,
6346b12ca56SVille Syrjälä 		  "pipe %c: status_mask=0x%x\n",
6356b12ca56SVille Syrjälä 		  pipe_name(pipe), status_mask);
6366b12ca56SVille Syrjälä 
6376b12ca56SVille Syrjälä 	lockdep_assert_held(&dev_priv->irq_lock);
6386b12ca56SVille Syrjälä 	WARN_ON(!intel_irqs_enabled(dev_priv));
6396b12ca56SVille Syrjälä 
6406b12ca56SVille Syrjälä 	if ((dev_priv->pipestat_irq_mask[pipe] & status_mask) == status_mask)
6416b12ca56SVille Syrjälä 		return;
6426b12ca56SVille Syrjälä 
6436b12ca56SVille Syrjälä 	dev_priv->pipestat_irq_mask[pipe] |= status_mask;
6446b12ca56SVille Syrjälä 	enable_mask = i915_pipestat_enable_mask(dev_priv, pipe);
6456b12ca56SVille Syrjälä 
6466b12ca56SVille Syrjälä 	I915_WRITE(reg, enable_mask | status_mask);
6476b12ca56SVille Syrjälä 	POSTING_READ(reg);
648755e9019SImre Deak }
649755e9019SImre Deak 
6506b12ca56SVille Syrjälä void i915_disable_pipestat(struct drm_i915_private *dev_priv,
6516b12ca56SVille Syrjälä 			   enum pipe pipe, u32 status_mask)
652755e9019SImre Deak {
6536b12ca56SVille Syrjälä 	i915_reg_t reg = PIPESTAT(pipe);
654755e9019SImre Deak 	u32 enable_mask;
655755e9019SImre Deak 
6566b12ca56SVille Syrjälä 	WARN_ONCE(status_mask & ~PIPESTAT_INT_STATUS_MASK,
6576b12ca56SVille Syrjälä 		  "pipe %c: status_mask=0x%x\n",
6586b12ca56SVille Syrjälä 		  pipe_name(pipe), status_mask);
6596b12ca56SVille Syrjälä 
6606b12ca56SVille Syrjälä 	lockdep_assert_held(&dev_priv->irq_lock);
6616b12ca56SVille Syrjälä 	WARN_ON(!intel_irqs_enabled(dev_priv));
6626b12ca56SVille Syrjälä 
6636b12ca56SVille Syrjälä 	if ((dev_priv->pipestat_irq_mask[pipe] & status_mask) == 0)
6646b12ca56SVille Syrjälä 		return;
6656b12ca56SVille Syrjälä 
6666b12ca56SVille Syrjälä 	dev_priv->pipestat_irq_mask[pipe] &= ~status_mask;
6676b12ca56SVille Syrjälä 	enable_mask = i915_pipestat_enable_mask(dev_priv, pipe);
6686b12ca56SVille Syrjälä 
6696b12ca56SVille Syrjälä 	I915_WRITE(reg, enable_mask | status_mask);
6706b12ca56SVille Syrjälä 	POSTING_READ(reg);
671755e9019SImre Deak }
672755e9019SImre Deak 
673c0e09200SDave Airlie /**
674f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
67514bb2c11STvrtko Ursulin  * @dev_priv: i915 device private
67601c66889SZhao Yakui  */
67791d14251STvrtko Ursulin static void i915_enable_asle_pipestat(struct drm_i915_private *dev_priv)
67801c66889SZhao Yakui {
67991d14251STvrtko Ursulin 	if (!dev_priv->opregion.asle || !IS_MOBILE(dev_priv))
680f49e38ddSJani Nikula 		return;
681f49e38ddSJani Nikula 
68213321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
68301c66889SZhao Yakui 
684755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_STATUS);
68591d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 4)
6863b6c42e8SDaniel Vetter 		i915_enable_pipestat(dev_priv, PIPE_A,
687755e9019SImre Deak 				     PIPE_LEGACY_BLC_EVENT_STATUS);
6881ec14ad3SChris Wilson 
68913321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
69001c66889SZhao Yakui }
69101c66889SZhao Yakui 
692f75f3746SVille Syrjälä /*
693f75f3746SVille Syrjälä  * This timing diagram depicts the video signal in and
694f75f3746SVille Syrjälä  * around the vertical blanking period.
695f75f3746SVille Syrjälä  *
696f75f3746SVille Syrjälä  * Assumptions about the fictitious mode used in this example:
697f75f3746SVille Syrjälä  *  vblank_start >= 3
698f75f3746SVille Syrjälä  *  vsync_start = vblank_start + 1
699f75f3746SVille Syrjälä  *  vsync_end = vblank_start + 2
700f75f3746SVille Syrjälä  *  vtotal = vblank_start + 3
701f75f3746SVille Syrjälä  *
702f75f3746SVille Syrjälä  *           start of vblank:
703f75f3746SVille Syrjälä  *           latch double buffered registers
704f75f3746SVille Syrjälä  *           increment frame counter (ctg+)
705f75f3746SVille Syrjälä  *           generate start of vblank interrupt (gen4+)
706f75f3746SVille Syrjälä  *           |
707f75f3746SVille Syrjälä  *           |          frame start:
708f75f3746SVille Syrjälä  *           |          generate frame start interrupt (aka. vblank interrupt) (gmch)
709f75f3746SVille Syrjälä  *           |          may be shifted forward 1-3 extra lines via PIPECONF
710f75f3746SVille Syrjälä  *           |          |
711f75f3746SVille Syrjälä  *           |          |  start of vsync:
712f75f3746SVille Syrjälä  *           |          |  generate vsync interrupt
713f75f3746SVille Syrjälä  *           |          |  |
714f75f3746SVille Syrjälä  * ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx
715f75f3746SVille Syrjälä  *       .   \hs/   .      \hs/          \hs/          \hs/   .      \hs/
716f75f3746SVille Syrjälä  * ----va---> <-----------------vb--------------------> <--------va-------------
717f75f3746SVille Syrjälä  *       |          |       <----vs----->                     |
718f75f3746SVille Syrjälä  * -vbs-----> <---vbs+1---> <---vbs+2---> <-----0-----> <-----1-----> <-----2--- (scanline counter gen2)
719f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2---> <-----0--- (scanline counter gen3+)
720f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2- (scanline counter hsw+ hdmi)
721f75f3746SVille Syrjälä  *       |          |                                         |
722f75f3746SVille Syrjälä  *       last visible pixel                                   first visible pixel
723f75f3746SVille Syrjälä  *                  |                                         increment frame counter (gen3/4)
724f75f3746SVille Syrjälä  *                  pixel counter = vblank_start * htotal     pixel counter = 0 (gen3/4)
725f75f3746SVille Syrjälä  *
726f75f3746SVille Syrjälä  * x  = horizontal active
727f75f3746SVille Syrjälä  * _  = horizontal blanking
728f75f3746SVille Syrjälä  * hs = horizontal sync
729f75f3746SVille Syrjälä  * va = vertical active
730f75f3746SVille Syrjälä  * vb = vertical blanking
731f75f3746SVille Syrjälä  * vs = vertical sync
732f75f3746SVille Syrjälä  * vbs = vblank_start (number)
733f75f3746SVille Syrjälä  *
734f75f3746SVille Syrjälä  * Summary:
735f75f3746SVille Syrjälä  * - most events happen at the start of horizontal sync
736f75f3746SVille Syrjälä  * - frame start happens at the start of horizontal blank, 1-4 lines
737f75f3746SVille Syrjälä  *   (depending on PIPECONF settings) after the start of vblank
738f75f3746SVille Syrjälä  * - gen3/4 pixel and frame counter are synchronized with the start
739f75f3746SVille Syrjälä  *   of horizontal active on the first line of vertical active
740f75f3746SVille Syrjälä  */
741f75f3746SVille Syrjälä 
74242f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
74342f52ef8SKeith Packard  * we use as a pipe index
74442f52ef8SKeith Packard  */
74588e72717SThierry Reding static u32 i915_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
7460a3e67a4SJesse Barnes {
747fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
748f0f59a00SVille Syrjälä 	i915_reg_t high_frame, low_frame;
7490b2a8e09SVille Syrjälä 	u32 high1, high2, low, pixel, vbl_start, hsync_start, htotal;
7505caa0feaSDaniel Vetter 	const struct drm_display_mode *mode = &dev->vblank[pipe].hwmode;
751694e409dSVille Syrjälä 	unsigned long irqflags;
752391f75e2SVille Syrjälä 
7530b2a8e09SVille Syrjälä 	htotal = mode->crtc_htotal;
7540b2a8e09SVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
7550b2a8e09SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
7560b2a8e09SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
7570b2a8e09SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
758391f75e2SVille Syrjälä 
7590b2a8e09SVille Syrjälä 	/* Convert to pixel count */
7600b2a8e09SVille Syrjälä 	vbl_start *= htotal;
7610b2a8e09SVille Syrjälä 
7620b2a8e09SVille Syrjälä 	/* Start of vblank event occurs at start of hsync */
7630b2a8e09SVille Syrjälä 	vbl_start -= htotal - hsync_start;
7640b2a8e09SVille Syrjälä 
7659db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
7669db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
7675eddb70bSChris Wilson 
768694e409dSVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
769694e409dSVille Syrjälä 
7700a3e67a4SJesse Barnes 	/*
7710a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
7720a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
7730a3e67a4SJesse Barnes 	 * register.
7740a3e67a4SJesse Barnes 	 */
7750a3e67a4SJesse Barnes 	do {
776694e409dSVille Syrjälä 		high1 = I915_READ_FW(high_frame) & PIPE_FRAME_HIGH_MASK;
777694e409dSVille Syrjälä 		low   = I915_READ_FW(low_frame);
778694e409dSVille Syrjälä 		high2 = I915_READ_FW(high_frame) & PIPE_FRAME_HIGH_MASK;
7790a3e67a4SJesse Barnes 	} while (high1 != high2);
7800a3e67a4SJesse Barnes 
781694e409dSVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
782694e409dSVille Syrjälä 
7835eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
784391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
7855eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
786391f75e2SVille Syrjälä 
787391f75e2SVille Syrjälä 	/*
788391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
789391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
790391f75e2SVille Syrjälä 	 * counter against vblank start.
791391f75e2SVille Syrjälä 	 */
792edc08d0aSVille Syrjälä 	return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
7930a3e67a4SJesse Barnes }
7940a3e67a4SJesse Barnes 
795974e59baSDave Airlie static u32 g4x_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
7969880b7a5SJesse Barnes {
797fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
7989880b7a5SJesse Barnes 
799649636efSVille Syrjälä 	return I915_READ(PIPE_FRMCOUNT_G4X(pipe));
8009880b7a5SJesse Barnes }
8019880b7a5SJesse Barnes 
802aec0246fSUma Shankar /*
803aec0246fSUma Shankar  * On certain encoders on certain platforms, pipe
804aec0246fSUma Shankar  * scanline register will not work to get the scanline,
805aec0246fSUma Shankar  * since the timings are driven from the PORT or issues
806aec0246fSUma Shankar  * with scanline register updates.
807aec0246fSUma Shankar  * This function will use Framestamp and current
808aec0246fSUma Shankar  * timestamp registers to calculate the scanline.
809aec0246fSUma Shankar  */
810aec0246fSUma Shankar static u32 __intel_get_crtc_scanline_from_timestamp(struct intel_crtc *crtc)
811aec0246fSUma Shankar {
812aec0246fSUma Shankar 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
813aec0246fSUma Shankar 	struct drm_vblank_crtc *vblank =
814aec0246fSUma Shankar 		&crtc->base.dev->vblank[drm_crtc_index(&crtc->base)];
815aec0246fSUma Shankar 	const struct drm_display_mode *mode = &vblank->hwmode;
816aec0246fSUma Shankar 	u32 vblank_start = mode->crtc_vblank_start;
817aec0246fSUma Shankar 	u32 vtotal = mode->crtc_vtotal;
818aec0246fSUma Shankar 	u32 htotal = mode->crtc_htotal;
819aec0246fSUma Shankar 	u32 clock = mode->crtc_clock;
820aec0246fSUma Shankar 	u32 scanline, scan_prev_time, scan_curr_time, scan_post_time;
821aec0246fSUma Shankar 
822aec0246fSUma Shankar 	/*
823aec0246fSUma Shankar 	 * To avoid the race condition where we might cross into the
824aec0246fSUma Shankar 	 * next vblank just between the PIPE_FRMTMSTMP and TIMESTAMP_CTR
825aec0246fSUma Shankar 	 * reads. We make sure we read PIPE_FRMTMSTMP and TIMESTAMP_CTR
826aec0246fSUma Shankar 	 * during the same frame.
827aec0246fSUma Shankar 	 */
828aec0246fSUma Shankar 	do {
829aec0246fSUma Shankar 		/*
830aec0246fSUma Shankar 		 * This field provides read back of the display
831aec0246fSUma Shankar 		 * pipe frame time stamp. The time stamp value
832aec0246fSUma Shankar 		 * is sampled at every start of vertical blank.
833aec0246fSUma Shankar 		 */
834aec0246fSUma Shankar 		scan_prev_time = I915_READ_FW(PIPE_FRMTMSTMP(crtc->pipe));
835aec0246fSUma Shankar 
836aec0246fSUma Shankar 		/*
837aec0246fSUma Shankar 		 * The TIMESTAMP_CTR register has the current
838aec0246fSUma Shankar 		 * time stamp value.
839aec0246fSUma Shankar 		 */
840aec0246fSUma Shankar 		scan_curr_time = I915_READ_FW(IVB_TIMESTAMP_CTR);
841aec0246fSUma Shankar 
842aec0246fSUma Shankar 		scan_post_time = I915_READ_FW(PIPE_FRMTMSTMP(crtc->pipe));
843aec0246fSUma Shankar 	} while (scan_post_time != scan_prev_time);
844aec0246fSUma Shankar 
845aec0246fSUma Shankar 	scanline = div_u64(mul_u32_u32(scan_curr_time - scan_prev_time,
846aec0246fSUma Shankar 					clock), 1000 * htotal);
847aec0246fSUma Shankar 	scanline = min(scanline, vtotal - 1);
848aec0246fSUma Shankar 	scanline = (scanline + vblank_start) % vtotal;
849aec0246fSUma Shankar 
850aec0246fSUma Shankar 	return scanline;
851aec0246fSUma Shankar }
852aec0246fSUma Shankar 
85375aa3f63SVille Syrjälä /* I915_READ_FW, only for fast reads of display block, no need for forcewake etc. */
854a225f079SVille Syrjälä static int __intel_get_crtc_scanline(struct intel_crtc *crtc)
855a225f079SVille Syrjälä {
856a225f079SVille Syrjälä 	struct drm_device *dev = crtc->base.dev;
857fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
8585caa0feaSDaniel Vetter 	const struct drm_display_mode *mode;
8595caa0feaSDaniel Vetter 	struct drm_vblank_crtc *vblank;
860a225f079SVille Syrjälä 	enum pipe pipe = crtc->pipe;
86180715b2fSVille Syrjälä 	int position, vtotal;
862a225f079SVille Syrjälä 
86372259536SVille Syrjälä 	if (!crtc->active)
86472259536SVille Syrjälä 		return -1;
86572259536SVille Syrjälä 
8665caa0feaSDaniel Vetter 	vblank = &crtc->base.dev->vblank[drm_crtc_index(&crtc->base)];
8675caa0feaSDaniel Vetter 	mode = &vblank->hwmode;
8685caa0feaSDaniel Vetter 
869aec0246fSUma Shankar 	if (mode->private_flags & I915_MODE_FLAG_GET_SCANLINE_FROM_TIMESTAMP)
870aec0246fSUma Shankar 		return __intel_get_crtc_scanline_from_timestamp(crtc);
871aec0246fSUma Shankar 
87280715b2fSVille Syrjälä 	vtotal = mode->crtc_vtotal;
873a225f079SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
874a225f079SVille Syrjälä 		vtotal /= 2;
875a225f079SVille Syrjälä 
87691d14251STvrtko Ursulin 	if (IS_GEN2(dev_priv))
87775aa3f63SVille Syrjälä 		position = I915_READ_FW(PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
878a225f079SVille Syrjälä 	else
87975aa3f63SVille Syrjälä 		position = I915_READ_FW(PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
880a225f079SVille Syrjälä 
881a225f079SVille Syrjälä 	/*
88241b578fbSJesse Barnes 	 * On HSW, the DSL reg (0x70000) appears to return 0 if we
88341b578fbSJesse Barnes 	 * read it just before the start of vblank.  So try it again
88441b578fbSJesse Barnes 	 * so we don't accidentally end up spanning a vblank frame
88541b578fbSJesse Barnes 	 * increment, causing the pipe_update_end() code to squak at us.
88641b578fbSJesse Barnes 	 *
88741b578fbSJesse Barnes 	 * The nature of this problem means we can't simply check the ISR
88841b578fbSJesse Barnes 	 * bit and return the vblank start value; nor can we use the scanline
88941b578fbSJesse Barnes 	 * debug register in the transcoder as it appears to have the same
89041b578fbSJesse Barnes 	 * problem.  We may need to extend this to include other platforms,
89141b578fbSJesse Barnes 	 * but so far testing only shows the problem on HSW.
89241b578fbSJesse Barnes 	 */
89391d14251STvrtko Ursulin 	if (HAS_DDI(dev_priv) && !position) {
89441b578fbSJesse Barnes 		int i, temp;
89541b578fbSJesse Barnes 
89641b578fbSJesse Barnes 		for (i = 0; i < 100; i++) {
89741b578fbSJesse Barnes 			udelay(1);
898707bdd3fSVille Syrjälä 			temp = I915_READ_FW(PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
89941b578fbSJesse Barnes 			if (temp != position) {
90041b578fbSJesse Barnes 				position = temp;
90141b578fbSJesse Barnes 				break;
90241b578fbSJesse Barnes 			}
90341b578fbSJesse Barnes 		}
90441b578fbSJesse Barnes 	}
90541b578fbSJesse Barnes 
90641b578fbSJesse Barnes 	/*
90780715b2fSVille Syrjälä 	 * See update_scanline_offset() for the details on the
90880715b2fSVille Syrjälä 	 * scanline_offset adjustment.
909a225f079SVille Syrjälä 	 */
91080715b2fSVille Syrjälä 	return (position + crtc->scanline_offset) % vtotal;
911a225f079SVille Syrjälä }
912a225f079SVille Syrjälä 
9131bf6ad62SDaniel Vetter static bool i915_get_crtc_scanoutpos(struct drm_device *dev, unsigned int pipe,
9141bf6ad62SDaniel Vetter 				     bool in_vblank_irq, int *vpos, int *hpos,
9153bb403bfSVille Syrjälä 				     ktime_t *stime, ktime_t *etime,
9163bb403bfSVille Syrjälä 				     const struct drm_display_mode *mode)
9170af7e4dfSMario Kleiner {
918fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
91998187836SVille Syrjälä 	struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
92098187836SVille Syrjälä 								pipe);
9213aa18df8SVille Syrjälä 	int position;
92278e8fc6bSVille Syrjälä 	int vbl_start, vbl_end, hsync_start, htotal, vtotal;
923ad3543edSMario Kleiner 	unsigned long irqflags;
9240af7e4dfSMario Kleiner 
925fc467a22SMaarten Lankhorst 	if (WARN_ON(!mode->crtc_clock)) {
9260af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
9279db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
9281bf6ad62SDaniel Vetter 		return false;
9290af7e4dfSMario Kleiner 	}
9300af7e4dfSMario Kleiner 
931c2baf4b7SVille Syrjälä 	htotal = mode->crtc_htotal;
93278e8fc6bSVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
933c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
934c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
935c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
9360af7e4dfSMario Kleiner 
937d31faf65SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
938d31faf65SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
939d31faf65SVille Syrjälä 		vbl_end /= 2;
940d31faf65SVille Syrjälä 		vtotal /= 2;
941d31faf65SVille Syrjälä 	}
942d31faf65SVille Syrjälä 
943ad3543edSMario Kleiner 	/*
944ad3543edSMario Kleiner 	 * Lock uncore.lock, as we will do multiple timing critical raw
945ad3543edSMario Kleiner 	 * register reads, potentially with preemption disabled, so the
946ad3543edSMario Kleiner 	 * following code must not block on uncore.lock.
947ad3543edSMario Kleiner 	 */
948ad3543edSMario Kleiner 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
949ad3543edSMario Kleiner 
950ad3543edSMario Kleiner 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
951ad3543edSMario Kleiner 
952ad3543edSMario Kleiner 	/* Get optional system timestamp before query. */
953ad3543edSMario Kleiner 	if (stime)
954ad3543edSMario Kleiner 		*stime = ktime_get();
955ad3543edSMario Kleiner 
95691d14251STvrtko Ursulin 	if (IS_GEN2(dev_priv) || IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5) {
9570af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
9580af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
9590af7e4dfSMario Kleiner 		 */
960a225f079SVille Syrjälä 		position = __intel_get_crtc_scanline(intel_crtc);
9610af7e4dfSMario Kleiner 	} else {
9620af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
9630af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
9640af7e4dfSMario Kleiner 		 * scanout position.
9650af7e4dfSMario Kleiner 		 */
96675aa3f63SVille Syrjälä 		position = (I915_READ_FW(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
9670af7e4dfSMario Kleiner 
9683aa18df8SVille Syrjälä 		/* convert to pixel counts */
9693aa18df8SVille Syrjälä 		vbl_start *= htotal;
9703aa18df8SVille Syrjälä 		vbl_end *= htotal;
9713aa18df8SVille Syrjälä 		vtotal *= htotal;
97278e8fc6bSVille Syrjälä 
97378e8fc6bSVille Syrjälä 		/*
9747e78f1cbSVille Syrjälä 		 * In interlaced modes, the pixel counter counts all pixels,
9757e78f1cbSVille Syrjälä 		 * so one field will have htotal more pixels. In order to avoid
9767e78f1cbSVille Syrjälä 		 * the reported position from jumping backwards when the pixel
9777e78f1cbSVille Syrjälä 		 * counter is beyond the length of the shorter field, just
9787e78f1cbSVille Syrjälä 		 * clamp the position the length of the shorter field. This
9797e78f1cbSVille Syrjälä 		 * matches how the scanline counter based position works since
9807e78f1cbSVille Syrjälä 		 * the scanline counter doesn't count the two half lines.
9817e78f1cbSVille Syrjälä 		 */
9827e78f1cbSVille Syrjälä 		if (position >= vtotal)
9837e78f1cbSVille Syrjälä 			position = vtotal - 1;
9847e78f1cbSVille Syrjälä 
9857e78f1cbSVille Syrjälä 		/*
98678e8fc6bSVille Syrjälä 		 * Start of vblank interrupt is triggered at start of hsync,
98778e8fc6bSVille Syrjälä 		 * just prior to the first active line of vblank. However we
98878e8fc6bSVille Syrjälä 		 * consider lines to start at the leading edge of horizontal
98978e8fc6bSVille Syrjälä 		 * active. So, should we get here before we've crossed into
99078e8fc6bSVille Syrjälä 		 * the horizontal active of the first line in vblank, we would
99178e8fc6bSVille Syrjälä 		 * not set the DRM_SCANOUTPOS_INVBL flag. In order to fix that,
99278e8fc6bSVille Syrjälä 		 * always add htotal-hsync_start to the current pixel position.
99378e8fc6bSVille Syrjälä 		 */
99478e8fc6bSVille Syrjälä 		position = (position + htotal - hsync_start) % vtotal;
9953aa18df8SVille Syrjälä 	}
9963aa18df8SVille Syrjälä 
997ad3543edSMario Kleiner 	/* Get optional system timestamp after query. */
998ad3543edSMario Kleiner 	if (etime)
999ad3543edSMario Kleiner 		*etime = ktime_get();
1000ad3543edSMario Kleiner 
1001ad3543edSMario Kleiner 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
1002ad3543edSMario Kleiner 
1003ad3543edSMario Kleiner 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
1004ad3543edSMario Kleiner 
10053aa18df8SVille Syrjälä 	/*
10063aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
10073aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
10083aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
10093aa18df8SVille Syrjälä 	 * up since vbl_end.
10103aa18df8SVille Syrjälä 	 */
10113aa18df8SVille Syrjälä 	if (position >= vbl_start)
10123aa18df8SVille Syrjälä 		position -= vbl_end;
10133aa18df8SVille Syrjälä 	else
10143aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
10153aa18df8SVille Syrjälä 
101691d14251STvrtko Ursulin 	if (IS_GEN2(dev_priv) || IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5) {
10173aa18df8SVille Syrjälä 		*vpos = position;
10183aa18df8SVille Syrjälä 		*hpos = 0;
10193aa18df8SVille Syrjälä 	} else {
10200af7e4dfSMario Kleiner 		*vpos = position / htotal;
10210af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
10220af7e4dfSMario Kleiner 	}
10230af7e4dfSMario Kleiner 
10241bf6ad62SDaniel Vetter 	return true;
10250af7e4dfSMario Kleiner }
10260af7e4dfSMario Kleiner 
1027a225f079SVille Syrjälä int intel_get_crtc_scanline(struct intel_crtc *crtc)
1028a225f079SVille Syrjälä {
1029fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1030a225f079SVille Syrjälä 	unsigned long irqflags;
1031a225f079SVille Syrjälä 	int position;
1032a225f079SVille Syrjälä 
1033a225f079SVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
1034a225f079SVille Syrjälä 	position = __intel_get_crtc_scanline(crtc);
1035a225f079SVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
1036a225f079SVille Syrjälä 
1037a225f079SVille Syrjälä 	return position;
1038a225f079SVille Syrjälä }
1039a225f079SVille Syrjälä 
104091d14251STvrtko Ursulin static void ironlake_rps_change_irq_handler(struct drm_i915_private *dev_priv)
1041f97108d1SJesse Barnes {
1042b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
10439270388eSDaniel Vetter 	u8 new_delay;
10449270388eSDaniel Vetter 
1045d0ecd7e2SDaniel Vetter 	spin_lock(&mchdev_lock);
1046f97108d1SJesse Barnes 
104773edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
104873edd18fSDaniel Vetter 
104920e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
10509270388eSDaniel Vetter 
10517648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
1052b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
1053b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
1054f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
1055f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
1056f97108d1SJesse Barnes 
1057f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
1058b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
105920e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
106020e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
106120e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
106220e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
1063b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
106420e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
106520e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
106620e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
106720e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
1068f97108d1SJesse Barnes 	}
1069f97108d1SJesse Barnes 
107091d14251STvrtko Ursulin 	if (ironlake_set_drps(dev_priv, new_delay))
107120e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
1072f97108d1SJesse Barnes 
1073d0ecd7e2SDaniel Vetter 	spin_unlock(&mchdev_lock);
10749270388eSDaniel Vetter 
1075f97108d1SJesse Barnes 	return;
1076f97108d1SJesse Barnes }
1077f97108d1SJesse Barnes 
10780bc40be8STvrtko Ursulin static void notify_ring(struct intel_engine_cs *engine)
1079549f7365SChris Wilson {
1080e61e0f51SChris Wilson 	struct i915_request *rq = NULL;
108156299fb7SChris Wilson 	struct intel_wait *wait;
1082dffabc8fSTvrtko Ursulin 
1083bcbd5c33SChris Wilson 	if (!engine->breadcrumbs.irq_armed)
1084bcbd5c33SChris Wilson 		return;
1085bcbd5c33SChris Wilson 
10862246bea6SChris Wilson 	atomic_inc(&engine->irq_count);
1087538b257dSChris Wilson 	set_bit(ENGINE_IRQ_BREADCRUMB, &engine->irq_posted);
108856299fb7SChris Wilson 
108961d3dc70SChris Wilson 	spin_lock(&engine->breadcrumbs.irq_lock);
109061d3dc70SChris Wilson 	wait = engine->breadcrumbs.irq_wait;
109156299fb7SChris Wilson 	if (wait) {
109217b51ad8SChris Wilson 		bool wakeup = engine->irq_seqno_barrier;
109317b51ad8SChris Wilson 
109456299fb7SChris Wilson 		/* We use a callback from the dma-fence to submit
109556299fb7SChris Wilson 		 * requests after waiting on our own requests. To
109656299fb7SChris Wilson 		 * ensure minimum delay in queuing the next request to
109756299fb7SChris Wilson 		 * hardware, signal the fence now rather than wait for
109856299fb7SChris Wilson 		 * the signaler to be woken up. We still wake up the
109956299fb7SChris Wilson 		 * waiter in order to handle the irq-seqno coherency
110056299fb7SChris Wilson 		 * issues (we may receive the interrupt before the
110156299fb7SChris Wilson 		 * seqno is written, see __i915_request_irq_complete())
110256299fb7SChris Wilson 		 * and to handle coalescing of multiple seqno updates
110356299fb7SChris Wilson 		 * and many waiters.
110456299fb7SChris Wilson 		 */
110556299fb7SChris Wilson 		if (i915_seqno_passed(intel_engine_get_seqno(engine),
110617b51ad8SChris Wilson 				      wait->seqno)) {
1107e61e0f51SChris Wilson 			struct i915_request *waiter = wait->request;
1108de4d2106SChris Wilson 
110917b51ad8SChris Wilson 			wakeup = true;
111017b51ad8SChris Wilson 			if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
1111de4d2106SChris Wilson 				      &waiter->fence.flags) &&
1112de4d2106SChris Wilson 			    intel_wait_check_request(wait, waiter))
1113e61e0f51SChris Wilson 				rq = i915_request_get(waiter);
111417b51ad8SChris Wilson 		}
111556299fb7SChris Wilson 
111617b51ad8SChris Wilson 		if (wakeup)
111756299fb7SChris Wilson 			wake_up_process(wait->tsk);
111867b807a8SChris Wilson 	} else {
1119bcbd5c33SChris Wilson 		if (engine->breadcrumbs.irq_armed)
112067b807a8SChris Wilson 			__intel_engine_disarm_breadcrumbs(engine);
112156299fb7SChris Wilson 	}
112261d3dc70SChris Wilson 	spin_unlock(&engine->breadcrumbs.irq_lock);
112356299fb7SChris Wilson 
112424754d75SChris Wilson 	if (rq) {
112556299fb7SChris Wilson 		dma_fence_signal(&rq->fence);
11264e9a8befSChris Wilson 		GEM_BUG_ON(!i915_request_completed(rq));
1127e61e0f51SChris Wilson 		i915_request_put(rq);
112824754d75SChris Wilson 	}
112956299fb7SChris Wilson 
113056299fb7SChris Wilson 	trace_intel_engine_notify(engine, wait);
1131549f7365SChris Wilson }
1132549f7365SChris Wilson 
113343cf3bf0SChris Wilson static void vlv_c0_read(struct drm_i915_private *dev_priv,
113443cf3bf0SChris Wilson 			struct intel_rps_ei *ei)
113531685c25SDeepak S {
1136679cb6c1SMika Kuoppala 	ei->ktime = ktime_get_raw();
113743cf3bf0SChris Wilson 	ei->render_c0 = I915_READ(VLV_RENDER_C0_COUNT);
113843cf3bf0SChris Wilson 	ei->media_c0 = I915_READ(VLV_MEDIA_C0_COUNT);
113931685c25SDeepak S }
114031685c25SDeepak S 
114143cf3bf0SChris Wilson void gen6_rps_reset_ei(struct drm_i915_private *dev_priv)
114243cf3bf0SChris Wilson {
1143562d9baeSSagar Arun Kamble 	memset(&dev_priv->gt_pm.rps.ei, 0, sizeof(dev_priv->gt_pm.rps.ei));
114443cf3bf0SChris Wilson }
114543cf3bf0SChris Wilson 
114643cf3bf0SChris Wilson static u32 vlv_wa_c0_ei(struct drm_i915_private *dev_priv, u32 pm_iir)
114743cf3bf0SChris Wilson {
1148562d9baeSSagar Arun Kamble 	struct intel_rps *rps = &dev_priv->gt_pm.rps;
1149562d9baeSSagar Arun Kamble 	const struct intel_rps_ei *prev = &rps->ei;
115043cf3bf0SChris Wilson 	struct intel_rps_ei now;
115143cf3bf0SChris Wilson 	u32 events = 0;
115243cf3bf0SChris Wilson 
1153e0e8c7cbSChris Wilson 	if ((pm_iir & GEN6_PM_RP_UP_EI_EXPIRED) == 0)
115443cf3bf0SChris Wilson 		return 0;
115543cf3bf0SChris Wilson 
115643cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &now);
115731685c25SDeepak S 
1158679cb6c1SMika Kuoppala 	if (prev->ktime) {
1159e0e8c7cbSChris Wilson 		u64 time, c0;
1160569884e3SChris Wilson 		u32 render, media;
1161e0e8c7cbSChris Wilson 
1162679cb6c1SMika Kuoppala 		time = ktime_us_delta(now.ktime, prev->ktime);
11638f68d591SChris Wilson 
1164e0e8c7cbSChris Wilson 		time *= dev_priv->czclk_freq;
1165e0e8c7cbSChris Wilson 
1166e0e8c7cbSChris Wilson 		/* Workload can be split between render + media,
1167e0e8c7cbSChris Wilson 		 * e.g. SwapBuffers being blitted in X after being rendered in
1168e0e8c7cbSChris Wilson 		 * mesa. To account for this we need to combine both engines
1169e0e8c7cbSChris Wilson 		 * into our activity counter.
1170e0e8c7cbSChris Wilson 		 */
1171569884e3SChris Wilson 		render = now.render_c0 - prev->render_c0;
1172569884e3SChris Wilson 		media = now.media_c0 - prev->media_c0;
1173569884e3SChris Wilson 		c0 = max(render, media);
11746b7f6aa7SMika Kuoppala 		c0 *= 1000 * 100 << 8; /* to usecs and scale to threshold% */
1175e0e8c7cbSChris Wilson 
1176562d9baeSSagar Arun Kamble 		if (c0 > time * rps->up_threshold)
1177e0e8c7cbSChris Wilson 			events = GEN6_PM_RP_UP_THRESHOLD;
1178562d9baeSSagar Arun Kamble 		else if (c0 < time * rps->down_threshold)
1179e0e8c7cbSChris Wilson 			events = GEN6_PM_RP_DOWN_THRESHOLD;
118031685c25SDeepak S 	}
118131685c25SDeepak S 
1182562d9baeSSagar Arun Kamble 	rps->ei = now;
118343cf3bf0SChris Wilson 	return events;
118431685c25SDeepak S }
118531685c25SDeepak S 
11864912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
11873b8d8d91SJesse Barnes {
11882d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
1189562d9baeSSagar Arun Kamble 		container_of(work, struct drm_i915_private, gt_pm.rps.work);
1190562d9baeSSagar Arun Kamble 	struct intel_rps *rps = &dev_priv->gt_pm.rps;
11917c0a16adSChris Wilson 	bool client_boost = false;
11928d3afd7dSChris Wilson 	int new_delay, adj, min, max;
11937c0a16adSChris Wilson 	u32 pm_iir = 0;
11943b8d8d91SJesse Barnes 
119559cdb63dSDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
1196562d9baeSSagar Arun Kamble 	if (rps->interrupts_enabled) {
1197562d9baeSSagar Arun Kamble 		pm_iir = fetch_and_zero(&rps->pm_iir);
1198562d9baeSSagar Arun Kamble 		client_boost = atomic_read(&rps->num_waiters);
1199d4d70aa5SImre Deak 	}
120059cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
12014912d041SBen Widawsky 
120260611c13SPaulo Zanoni 	/* Make sure we didn't queue anything we're not going to process. */
1203a6706b45SDeepak S 	WARN_ON(pm_iir & ~dev_priv->pm_rps_events);
12048d3afd7dSChris Wilson 	if ((pm_iir & dev_priv->pm_rps_events) == 0 && !client_boost)
12057c0a16adSChris Wilson 		goto out;
12063b8d8d91SJesse Barnes 
12079f817501SSagar Arun Kamble 	mutex_lock(&dev_priv->pcu_lock);
12087b9e0ae6SChris Wilson 
120943cf3bf0SChris Wilson 	pm_iir |= vlv_wa_c0_ei(dev_priv, pm_iir);
121043cf3bf0SChris Wilson 
1211562d9baeSSagar Arun Kamble 	adj = rps->last_adj;
1212562d9baeSSagar Arun Kamble 	new_delay = rps->cur_freq;
1213562d9baeSSagar Arun Kamble 	min = rps->min_freq_softlimit;
1214562d9baeSSagar Arun Kamble 	max = rps->max_freq_softlimit;
12157b92c1bdSChris Wilson 	if (client_boost)
1216562d9baeSSagar Arun Kamble 		max = rps->max_freq;
1217562d9baeSSagar Arun Kamble 	if (client_boost && new_delay < rps->boost_freq) {
1218562d9baeSSagar Arun Kamble 		new_delay = rps->boost_freq;
12198d3afd7dSChris Wilson 		adj = 0;
12208d3afd7dSChris Wilson 	} else if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
1221dd75fdc8SChris Wilson 		if (adj > 0)
1222dd75fdc8SChris Wilson 			adj *= 2;
1223edcf284bSChris Wilson 		else /* CHV needs even encode values */
1224edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? 2 : 1;
12257e79a683SSagar Arun Kamble 
1226562d9baeSSagar Arun Kamble 		if (new_delay >= rps->max_freq_softlimit)
12277e79a683SSagar Arun Kamble 			adj = 0;
12287b92c1bdSChris Wilson 	} else if (client_boost) {
1229f5a4c67dSChris Wilson 		adj = 0;
1230dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
1231562d9baeSSagar Arun Kamble 		if (rps->cur_freq > rps->efficient_freq)
1232562d9baeSSagar Arun Kamble 			new_delay = rps->efficient_freq;
1233562d9baeSSagar Arun Kamble 		else if (rps->cur_freq > rps->min_freq_softlimit)
1234562d9baeSSagar Arun Kamble 			new_delay = rps->min_freq_softlimit;
1235dd75fdc8SChris Wilson 		adj = 0;
1236dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1237dd75fdc8SChris Wilson 		if (adj < 0)
1238dd75fdc8SChris Wilson 			adj *= 2;
1239edcf284bSChris Wilson 		else /* CHV needs even encode values */
1240edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? -2 : -1;
12417e79a683SSagar Arun Kamble 
1242562d9baeSSagar Arun Kamble 		if (new_delay <= rps->min_freq_softlimit)
12437e79a683SSagar Arun Kamble 			adj = 0;
1244dd75fdc8SChris Wilson 	} else { /* unknown event */
1245edcf284bSChris Wilson 		adj = 0;
1246dd75fdc8SChris Wilson 	}
12473b8d8d91SJesse Barnes 
1248562d9baeSSagar Arun Kamble 	rps->last_adj = adj;
1249edcf284bSChris Wilson 
125079249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
125179249636SBen Widawsky 	 * interrupt
125279249636SBen Widawsky 	 */
1253edcf284bSChris Wilson 	new_delay += adj;
12548d3afd7dSChris Wilson 	new_delay = clamp_t(int, new_delay, min, max);
125527544369SDeepak S 
12569fcee2f7SChris Wilson 	if (intel_set_rps(dev_priv, new_delay)) {
12579fcee2f7SChris Wilson 		DRM_DEBUG_DRIVER("Failed to set new GPU frequency\n");
1258562d9baeSSagar Arun Kamble 		rps->last_adj = 0;
12599fcee2f7SChris Wilson 	}
12603b8d8d91SJesse Barnes 
12619f817501SSagar Arun Kamble 	mutex_unlock(&dev_priv->pcu_lock);
12627c0a16adSChris Wilson 
12637c0a16adSChris Wilson out:
12647c0a16adSChris Wilson 	/* Make sure not to corrupt PMIMR state used by ringbuffer on GEN6 */
12657c0a16adSChris Wilson 	spin_lock_irq(&dev_priv->irq_lock);
1266562d9baeSSagar Arun Kamble 	if (rps->interrupts_enabled)
12677c0a16adSChris Wilson 		gen6_unmask_pm_irq(dev_priv, dev_priv->pm_rps_events);
12687c0a16adSChris Wilson 	spin_unlock_irq(&dev_priv->irq_lock);
12693b8d8d91SJesse Barnes }
12703b8d8d91SJesse Barnes 
1271e3689190SBen Widawsky 
1272e3689190SBen Widawsky /**
1273e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
1274e3689190SBen Widawsky  * occurred.
1275e3689190SBen Widawsky  * @work: workqueue struct
1276e3689190SBen Widawsky  *
1277e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
1278e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
1279e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
1280e3689190SBen Widawsky  */
1281e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
1282e3689190SBen Widawsky {
12832d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
1284cefcff8fSJoonas Lahtinen 		container_of(work, typeof(*dev_priv), l3_parity.error_work);
1285e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
128635a85ac6SBen Widawsky 	char *parity_event[6];
1287e3689190SBen Widawsky 	uint32_t misccpctl;
128835a85ac6SBen Widawsky 	uint8_t slice = 0;
1289e3689190SBen Widawsky 
1290e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
1291e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
1292e3689190SBen Widawsky 	 * any time we access those registers.
1293e3689190SBen Widawsky 	 */
129491c8a326SChris Wilson 	mutex_lock(&dev_priv->drm.struct_mutex);
1295e3689190SBen Widawsky 
129635a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
129735a85ac6SBen Widawsky 	if (WARN_ON(!dev_priv->l3_parity.which_slice))
129835a85ac6SBen Widawsky 		goto out;
129935a85ac6SBen Widawsky 
1300e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
1301e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1302e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
1303e3689190SBen Widawsky 
130435a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
1305f0f59a00SVille Syrjälä 		i915_reg_t reg;
130635a85ac6SBen Widawsky 
130735a85ac6SBen Widawsky 		slice--;
13082d1fe073SJoonas Lahtinen 		if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv)))
130935a85ac6SBen Widawsky 			break;
131035a85ac6SBen Widawsky 
131135a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
131235a85ac6SBen Widawsky 
13136fa1c5f1SVille Syrjälä 		reg = GEN7_L3CDERRST1(slice);
131435a85ac6SBen Widawsky 
131535a85ac6SBen Widawsky 		error_status = I915_READ(reg);
1316e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
1317e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
1318e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1319e3689190SBen Widawsky 
132035a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
132135a85ac6SBen Widawsky 		POSTING_READ(reg);
1322e3689190SBen Widawsky 
1323cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1324e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1325e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1326e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
132735a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
132835a85ac6SBen Widawsky 		parity_event[5] = NULL;
1329e3689190SBen Widawsky 
133091c8a326SChris Wilson 		kobject_uevent_env(&dev_priv->drm.primary->kdev->kobj,
1331e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
1332e3689190SBen Widawsky 
133335a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
133435a85ac6SBen Widawsky 			  slice, row, bank, subbank);
1335e3689190SBen Widawsky 
133635a85ac6SBen Widawsky 		kfree(parity_event[4]);
1337e3689190SBen Widawsky 		kfree(parity_event[3]);
1338e3689190SBen Widawsky 		kfree(parity_event[2]);
1339e3689190SBen Widawsky 		kfree(parity_event[1]);
1340e3689190SBen Widawsky 	}
1341e3689190SBen Widawsky 
134235a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
134335a85ac6SBen Widawsky 
134435a85ac6SBen Widawsky out:
134535a85ac6SBen Widawsky 	WARN_ON(dev_priv->l3_parity.which_slice);
13464cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
13472d1fe073SJoonas Lahtinen 	gen5_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv));
13484cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
134935a85ac6SBen Widawsky 
135091c8a326SChris Wilson 	mutex_unlock(&dev_priv->drm.struct_mutex);
135135a85ac6SBen Widawsky }
135235a85ac6SBen Widawsky 
1353261e40b8SVille Syrjälä static void ivybridge_parity_error_irq_handler(struct drm_i915_private *dev_priv,
1354261e40b8SVille Syrjälä 					       u32 iir)
1355e3689190SBen Widawsky {
1356261e40b8SVille Syrjälä 	if (!HAS_L3_DPF(dev_priv))
1357e3689190SBen Widawsky 		return;
1358e3689190SBen Widawsky 
1359d0ecd7e2SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1360261e40b8SVille Syrjälä 	gen5_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv));
1361d0ecd7e2SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
1362e3689190SBen Widawsky 
1363261e40b8SVille Syrjälä 	iir &= GT_PARITY_ERROR(dev_priv);
136435a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
136535a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 1;
136635a85ac6SBen Widawsky 
136735a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
136835a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 0;
136935a85ac6SBen Widawsky 
1370a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
1371e3689190SBen Widawsky }
1372e3689190SBen Widawsky 
1373261e40b8SVille Syrjälä static void ilk_gt_irq_handler(struct drm_i915_private *dev_priv,
1374f1af8fc1SPaulo Zanoni 			       u32 gt_iir)
1375f1af8fc1SPaulo Zanoni {
1376f8973c21SChris Wilson 	if (gt_iir & GT_RENDER_USER_INTERRUPT)
13773b3f1650SAkash Goel 		notify_ring(dev_priv->engine[RCS]);
1378f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
13793b3f1650SAkash Goel 		notify_ring(dev_priv->engine[VCS]);
1380f1af8fc1SPaulo Zanoni }
1381f1af8fc1SPaulo Zanoni 
1382261e40b8SVille Syrjälä static void snb_gt_irq_handler(struct drm_i915_private *dev_priv,
1383e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1384e7b4c6b1SDaniel Vetter {
1385f8973c21SChris Wilson 	if (gt_iir & GT_RENDER_USER_INTERRUPT)
13863b3f1650SAkash Goel 		notify_ring(dev_priv->engine[RCS]);
1387cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
13883b3f1650SAkash Goel 		notify_ring(dev_priv->engine[VCS]);
1389cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
13903b3f1650SAkash Goel 		notify_ring(dev_priv->engine[BCS]);
1391e7b4c6b1SDaniel Vetter 
1392cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1393cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
1394aaecdf61SDaniel Vetter 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT))
1395aaecdf61SDaniel Vetter 		DRM_DEBUG("Command parser error, gt_iir 0x%08x\n", gt_iir);
1396e3689190SBen Widawsky 
1397261e40b8SVille Syrjälä 	if (gt_iir & GT_PARITY_ERROR(dev_priv))
1398261e40b8SVille Syrjälä 		ivybridge_parity_error_irq_handler(dev_priv, gt_iir);
1399e7b4c6b1SDaniel Vetter }
1400e7b4c6b1SDaniel Vetter 
14015d3d69d5SChris Wilson static void
140251f6b0f9SChris Wilson gen8_cs_irq_handler(struct intel_engine_cs *engine, u32 iir)
1403fbcc1a0cSNick Hoath {
1404b620e870SMika Kuoppala 	struct intel_engine_execlists * const execlists = &engine->execlists;
140531de7350SChris Wilson 	bool tasklet = false;
1406f747026cSChris Wilson 
140751f6b0f9SChris Wilson 	if (iir & GT_CONTEXT_SWITCH_INTERRUPT) {
14084a118ecbSChris Wilson 		if (READ_ONCE(engine->execlists.active)) {
1409955a4b89SChris Wilson 			__set_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
141031de7350SChris Wilson 			tasklet = true;
1411f747026cSChris Wilson 		}
14124a118ecbSChris Wilson 	}
141331de7350SChris Wilson 
141451f6b0f9SChris Wilson 	if (iir & GT_RENDER_USER_INTERRUPT) {
141531de7350SChris Wilson 		notify_ring(engine);
141693ffbe8eSMichal Wajdeczko 		tasklet |= USES_GUC_SUBMISSION(engine->i915);
141731de7350SChris Wilson 	}
141831de7350SChris Wilson 
141931de7350SChris Wilson 	if (tasklet)
1420c6dce8f1SSagar Arun Kamble 		tasklet_hi_schedule(&execlists->tasklet);
1421fbcc1a0cSNick Hoath }
1422fbcc1a0cSNick Hoath 
14232e4a5b25SChris Wilson static void gen8_gt_irq_ack(struct drm_i915_private *i915,
142455ef72f2SChris Wilson 			    u32 master_ctl, u32 gt_iir[4])
1425abd58f01SBen Widawsky {
14262e4a5b25SChris Wilson 	void __iomem * const regs = i915->regs;
14272e4a5b25SChris Wilson 
1428f0fd96f5SChris Wilson #define GEN8_GT_IRQS (GEN8_GT_RCS_IRQ | \
1429f0fd96f5SChris Wilson 		      GEN8_GT_BCS_IRQ | \
1430f0fd96f5SChris Wilson 		      GEN8_GT_VCS1_IRQ | \
1431f0fd96f5SChris Wilson 		      GEN8_GT_VCS2_IRQ | \
1432f0fd96f5SChris Wilson 		      GEN8_GT_VECS_IRQ | \
1433f0fd96f5SChris Wilson 		      GEN8_GT_PM_IRQ | \
1434f0fd96f5SChris Wilson 		      GEN8_GT_GUC_IRQ)
1435f0fd96f5SChris Wilson 
1436abd58f01SBen Widawsky 	if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
14372e4a5b25SChris Wilson 		gt_iir[0] = raw_reg_read(regs, GEN8_GT_IIR(0));
14382e4a5b25SChris Wilson 		if (likely(gt_iir[0]))
14392e4a5b25SChris Wilson 			raw_reg_write(regs, GEN8_GT_IIR(0), gt_iir[0]);
1440abd58f01SBen Widawsky 	}
1441abd58f01SBen Widawsky 
144285f9b5f9SZhao Yakui 	if (master_ctl & (GEN8_GT_VCS1_IRQ | GEN8_GT_VCS2_IRQ)) {
14432e4a5b25SChris Wilson 		gt_iir[1] = raw_reg_read(regs, GEN8_GT_IIR(1));
14442e4a5b25SChris Wilson 		if (likely(gt_iir[1]))
14452e4a5b25SChris Wilson 			raw_reg_write(regs, GEN8_GT_IIR(1), gt_iir[1]);
144674cdb337SChris Wilson 	}
144774cdb337SChris Wilson 
144826705e20SSagar Arun Kamble 	if (master_ctl & (GEN8_GT_PM_IRQ | GEN8_GT_GUC_IRQ)) {
14492e4a5b25SChris Wilson 		gt_iir[2] = raw_reg_read(regs, GEN8_GT_IIR(2));
14502e4a5b25SChris Wilson 		if (likely(gt_iir[2] & (i915->pm_rps_events |
14512e4a5b25SChris Wilson 					i915->pm_guc_events)))
14522e4a5b25SChris Wilson 			raw_reg_write(regs, GEN8_GT_IIR(2),
14532e4a5b25SChris Wilson 				      gt_iir[2] & (i915->pm_rps_events |
14542e4a5b25SChris Wilson 						   i915->pm_guc_events));
14550961021aSBen Widawsky 	}
14562e4a5b25SChris Wilson 
14572e4a5b25SChris Wilson 	if (master_ctl & GEN8_GT_VECS_IRQ) {
14582e4a5b25SChris Wilson 		gt_iir[3] = raw_reg_read(regs, GEN8_GT_IIR(3));
14592e4a5b25SChris Wilson 		if (likely(gt_iir[3]))
14602e4a5b25SChris Wilson 			raw_reg_write(regs, GEN8_GT_IIR(3), gt_iir[3]);
146155ef72f2SChris Wilson 	}
1462abd58f01SBen Widawsky }
1463abd58f01SBen Widawsky 
14642e4a5b25SChris Wilson static void gen8_gt_irq_handler(struct drm_i915_private *i915,
1465f0fd96f5SChris Wilson 				u32 master_ctl, u32 gt_iir[4])
1466e30e251aSVille Syrjälä {
1467f0fd96f5SChris Wilson 	if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
14682e4a5b25SChris Wilson 		gen8_cs_irq_handler(i915->engine[RCS],
146951f6b0f9SChris Wilson 				    gt_iir[0] >> GEN8_RCS_IRQ_SHIFT);
14702e4a5b25SChris Wilson 		gen8_cs_irq_handler(i915->engine[BCS],
147151f6b0f9SChris Wilson 				    gt_iir[0] >> GEN8_BCS_IRQ_SHIFT);
1472e30e251aSVille Syrjälä 	}
1473e30e251aSVille Syrjälä 
1474f0fd96f5SChris Wilson 	if (master_ctl & (GEN8_GT_VCS1_IRQ | GEN8_GT_VCS2_IRQ)) {
14752e4a5b25SChris Wilson 		gen8_cs_irq_handler(i915->engine[VCS],
147651f6b0f9SChris Wilson 				    gt_iir[1] >> GEN8_VCS1_IRQ_SHIFT);
14772e4a5b25SChris Wilson 		gen8_cs_irq_handler(i915->engine[VCS2],
147851f6b0f9SChris Wilson 				    gt_iir[1] >> GEN8_VCS2_IRQ_SHIFT);
1479e30e251aSVille Syrjälä 	}
1480e30e251aSVille Syrjälä 
1481f0fd96f5SChris Wilson 	if (master_ctl & GEN8_GT_VECS_IRQ) {
14822e4a5b25SChris Wilson 		gen8_cs_irq_handler(i915->engine[VECS],
148351f6b0f9SChris Wilson 				    gt_iir[3] >> GEN8_VECS_IRQ_SHIFT);
1484f0fd96f5SChris Wilson 	}
1485e30e251aSVille Syrjälä 
1486f0fd96f5SChris Wilson 	if (master_ctl & (GEN8_GT_PM_IRQ | GEN8_GT_GUC_IRQ)) {
14872e4a5b25SChris Wilson 		gen6_rps_irq_handler(i915, gt_iir[2]);
14882e4a5b25SChris Wilson 		gen9_guc_irq_handler(i915, gt_iir[2]);
1489e30e251aSVille Syrjälä 	}
1490f0fd96f5SChris Wilson }
1491e30e251aSVille Syrjälä 
149263c88d22SImre Deak static bool bxt_port_hotplug_long_detect(enum port port, u32 val)
149363c88d22SImre Deak {
149463c88d22SImre Deak 	switch (port) {
149563c88d22SImre Deak 	case PORT_A:
1496195baa06SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
149763c88d22SImre Deak 	case PORT_B:
149863c88d22SImre Deak 		return val & PORTB_HOTPLUG_LONG_DETECT;
149963c88d22SImre Deak 	case PORT_C:
150063c88d22SImre Deak 		return val & PORTC_HOTPLUG_LONG_DETECT;
150163c88d22SImre Deak 	default:
150263c88d22SImre Deak 		return false;
150363c88d22SImre Deak 	}
150463c88d22SImre Deak }
150563c88d22SImre Deak 
15066dbf30ceSVille Syrjälä static bool spt_port_hotplug2_long_detect(enum port port, u32 val)
15076dbf30ceSVille Syrjälä {
15086dbf30ceSVille Syrjälä 	switch (port) {
15096dbf30ceSVille Syrjälä 	case PORT_E:
15106dbf30ceSVille Syrjälä 		return val & PORTE_HOTPLUG_LONG_DETECT;
15116dbf30ceSVille Syrjälä 	default:
15126dbf30ceSVille Syrjälä 		return false;
15136dbf30ceSVille Syrjälä 	}
15146dbf30ceSVille Syrjälä }
15156dbf30ceSVille Syrjälä 
151674c0b395SVille Syrjälä static bool spt_port_hotplug_long_detect(enum port port, u32 val)
151774c0b395SVille Syrjälä {
151874c0b395SVille Syrjälä 	switch (port) {
151974c0b395SVille Syrjälä 	case PORT_A:
152074c0b395SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
152174c0b395SVille Syrjälä 	case PORT_B:
152274c0b395SVille Syrjälä 		return val & PORTB_HOTPLUG_LONG_DETECT;
152374c0b395SVille Syrjälä 	case PORT_C:
152474c0b395SVille Syrjälä 		return val & PORTC_HOTPLUG_LONG_DETECT;
152574c0b395SVille Syrjälä 	case PORT_D:
152674c0b395SVille Syrjälä 		return val & PORTD_HOTPLUG_LONG_DETECT;
152774c0b395SVille Syrjälä 	default:
152874c0b395SVille Syrjälä 		return false;
152974c0b395SVille Syrjälä 	}
153074c0b395SVille Syrjälä }
153174c0b395SVille Syrjälä 
1532e4ce95aaSVille Syrjälä static bool ilk_port_hotplug_long_detect(enum port port, u32 val)
1533e4ce95aaSVille Syrjälä {
1534e4ce95aaSVille Syrjälä 	switch (port) {
1535e4ce95aaSVille Syrjälä 	case PORT_A:
1536e4ce95aaSVille Syrjälä 		return val & DIGITAL_PORTA_HOTPLUG_LONG_DETECT;
1537e4ce95aaSVille Syrjälä 	default:
1538e4ce95aaSVille Syrjälä 		return false;
1539e4ce95aaSVille Syrjälä 	}
1540e4ce95aaSVille Syrjälä }
1541e4ce95aaSVille Syrjälä 
1542676574dfSJani Nikula static bool pch_port_hotplug_long_detect(enum port port, u32 val)
154313cf5504SDave Airlie {
154413cf5504SDave Airlie 	switch (port) {
154513cf5504SDave Airlie 	case PORT_B:
1546676574dfSJani Nikula 		return val & PORTB_HOTPLUG_LONG_DETECT;
154713cf5504SDave Airlie 	case PORT_C:
1548676574dfSJani Nikula 		return val & PORTC_HOTPLUG_LONG_DETECT;
154913cf5504SDave Airlie 	case PORT_D:
1550676574dfSJani Nikula 		return val & PORTD_HOTPLUG_LONG_DETECT;
1551676574dfSJani Nikula 	default:
1552676574dfSJani Nikula 		return false;
155313cf5504SDave Airlie 	}
155413cf5504SDave Airlie }
155513cf5504SDave Airlie 
1556676574dfSJani Nikula static bool i9xx_port_hotplug_long_detect(enum port port, u32 val)
155713cf5504SDave Airlie {
155813cf5504SDave Airlie 	switch (port) {
155913cf5504SDave Airlie 	case PORT_B:
1560676574dfSJani Nikula 		return val & PORTB_HOTPLUG_INT_LONG_PULSE;
156113cf5504SDave Airlie 	case PORT_C:
1562676574dfSJani Nikula 		return val & PORTC_HOTPLUG_INT_LONG_PULSE;
156313cf5504SDave Airlie 	case PORT_D:
1564676574dfSJani Nikula 		return val & PORTD_HOTPLUG_INT_LONG_PULSE;
1565676574dfSJani Nikula 	default:
1566676574dfSJani Nikula 		return false;
156713cf5504SDave Airlie 	}
156813cf5504SDave Airlie }
156913cf5504SDave Airlie 
157042db67d6SVille Syrjälä /*
157142db67d6SVille Syrjälä  * Get a bit mask of pins that have triggered, and which ones may be long.
157242db67d6SVille Syrjälä  * This can be called multiple times with the same masks to accumulate
157342db67d6SVille Syrjälä  * hotplug detection results from several registers.
157442db67d6SVille Syrjälä  *
157542db67d6SVille Syrjälä  * Note that the caller is expected to zero out the masks initially.
157642db67d6SVille Syrjälä  */
1577cf53902fSRodrigo Vivi static void intel_get_hpd_pins(struct drm_i915_private *dev_priv,
1578cf53902fSRodrigo Vivi 			       u32 *pin_mask, u32 *long_mask,
15798c841e57SJani Nikula 			       u32 hotplug_trigger, u32 dig_hotplug_reg,
1580fd63e2a9SImre Deak 			       const u32 hpd[HPD_NUM_PINS],
1581fd63e2a9SImre Deak 			       bool long_pulse_detect(enum port port, u32 val))
1582676574dfSJani Nikula {
15838c841e57SJani Nikula 	enum port port;
1584676574dfSJani Nikula 	int i;
1585676574dfSJani Nikula 
1586676574dfSJani Nikula 	for_each_hpd_pin(i) {
15878c841e57SJani Nikula 		if ((hpd[i] & hotplug_trigger) == 0)
15888c841e57SJani Nikula 			continue;
15898c841e57SJani Nikula 
1590676574dfSJani Nikula 		*pin_mask |= BIT(i);
1591676574dfSJani Nikula 
1592cf53902fSRodrigo Vivi 		port = intel_hpd_pin_to_port(dev_priv, i);
1593256cfddeSRodrigo Vivi 		if (port == PORT_NONE)
1594cc24fcdcSImre Deak 			continue;
1595cc24fcdcSImre Deak 
1596fd63e2a9SImre Deak 		if (long_pulse_detect(port, dig_hotplug_reg))
1597676574dfSJani Nikula 			*long_mask |= BIT(i);
1598676574dfSJani Nikula 	}
1599676574dfSJani Nikula 
1600676574dfSJani Nikula 	DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x, dig 0x%08x, pins 0x%08x\n",
1601676574dfSJani Nikula 			 hotplug_trigger, dig_hotplug_reg, *pin_mask);
1602676574dfSJani Nikula 
1603676574dfSJani Nikula }
1604676574dfSJani Nikula 
160591d14251STvrtko Ursulin static void gmbus_irq_handler(struct drm_i915_private *dev_priv)
1606515ac2bbSDaniel Vetter {
160728c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1608515ac2bbSDaniel Vetter }
1609515ac2bbSDaniel Vetter 
161091d14251STvrtko Ursulin static void dp_aux_irq_handler(struct drm_i915_private *dev_priv)
1611ce99c256SDaniel Vetter {
16129ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1613ce99c256SDaniel Vetter }
1614ce99c256SDaniel Vetter 
16158bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
161691d14251STvrtko Ursulin static void display_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
161791d14251STvrtko Ursulin 					 enum pipe pipe,
1618eba94eb9SDaniel Vetter 					 uint32_t crc0, uint32_t crc1,
1619eba94eb9SDaniel Vetter 					 uint32_t crc2, uint32_t crc3,
16208bc5e955SDaniel Vetter 					 uint32_t crc4)
16218bf1e9f1SShuang He {
16228bf1e9f1SShuang He 	struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
16238bf1e9f1SShuang He 	struct intel_pipe_crc_entry *entry;
16248c6b709dSTomeu Vizoso 	struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
16258c6b709dSTomeu Vizoso 	struct drm_driver *driver = dev_priv->drm.driver;
16268c6b709dSTomeu Vizoso 	uint32_t crcs[5];
1627ac2300d4SDamien Lespiau 	int head, tail;
1628b2c88f5bSDamien Lespiau 
1629d538bbdfSDamien Lespiau 	spin_lock(&pipe_crc->lock);
1630033b7a23SMaarten Lankhorst 	if (pipe_crc->source && !crtc->base.crc.opened) {
16310c912c79SDamien Lespiau 		if (!pipe_crc->entries) {
1632d538bbdfSDamien Lespiau 			spin_unlock(&pipe_crc->lock);
163334273620SDaniel Vetter 			DRM_DEBUG_KMS("spurious interrupt\n");
16340c912c79SDamien Lespiau 			return;
16350c912c79SDamien Lespiau 		}
16360c912c79SDamien Lespiau 
1637d538bbdfSDamien Lespiau 		head = pipe_crc->head;
1638d538bbdfSDamien Lespiau 		tail = pipe_crc->tail;
1639b2c88f5bSDamien Lespiau 
1640b2c88f5bSDamien Lespiau 		if (CIRC_SPACE(head, tail, INTEL_PIPE_CRC_ENTRIES_NR) < 1) {
1641d538bbdfSDamien Lespiau 			spin_unlock(&pipe_crc->lock);
1642b2c88f5bSDamien Lespiau 			DRM_ERROR("CRC buffer overflowing\n");
1643b2c88f5bSDamien Lespiau 			return;
1644b2c88f5bSDamien Lespiau 		}
1645b2c88f5bSDamien Lespiau 
1646b2c88f5bSDamien Lespiau 		entry = &pipe_crc->entries[head];
16478bf1e9f1SShuang He 
16488c6b709dSTomeu Vizoso 		entry->frame = driver->get_vblank_counter(&dev_priv->drm, pipe);
1649eba94eb9SDaniel Vetter 		entry->crc[0] = crc0;
1650eba94eb9SDaniel Vetter 		entry->crc[1] = crc1;
1651eba94eb9SDaniel Vetter 		entry->crc[2] = crc2;
1652eba94eb9SDaniel Vetter 		entry->crc[3] = crc3;
1653eba94eb9SDaniel Vetter 		entry->crc[4] = crc4;
1654b2c88f5bSDamien Lespiau 
1655b2c88f5bSDamien Lespiau 		head = (head + 1) & (INTEL_PIPE_CRC_ENTRIES_NR - 1);
1656d538bbdfSDamien Lespiau 		pipe_crc->head = head;
1657d538bbdfSDamien Lespiau 
1658d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
165907144428SDamien Lespiau 
166007144428SDamien Lespiau 		wake_up_interruptible(&pipe_crc->wq);
16618c6b709dSTomeu Vizoso 	} else {
16628c6b709dSTomeu Vizoso 		/*
16638c6b709dSTomeu Vizoso 		 * For some not yet identified reason, the first CRC is
16648c6b709dSTomeu Vizoso 		 * bonkers. So let's just wait for the next vblank and read
16658c6b709dSTomeu Vizoso 		 * out the buggy result.
16668c6b709dSTomeu Vizoso 		 *
1667163e8aecSRodrigo Vivi 		 * On GEN8+ sometimes the second CRC is bonkers as well, so
16688c6b709dSTomeu Vizoso 		 * don't trust that one either.
16698c6b709dSTomeu Vizoso 		 */
1670033b7a23SMaarten Lankhorst 		if (pipe_crc->skipped <= 0 ||
1671163e8aecSRodrigo Vivi 		    (INTEL_GEN(dev_priv) >= 8 && pipe_crc->skipped == 1)) {
16728c6b709dSTomeu Vizoso 			pipe_crc->skipped++;
16738c6b709dSTomeu Vizoso 			spin_unlock(&pipe_crc->lock);
16748c6b709dSTomeu Vizoso 			return;
16758c6b709dSTomeu Vizoso 		}
16768c6b709dSTomeu Vizoso 		spin_unlock(&pipe_crc->lock);
16778c6b709dSTomeu Vizoso 		crcs[0] = crc0;
16788c6b709dSTomeu Vizoso 		crcs[1] = crc1;
16798c6b709dSTomeu Vizoso 		crcs[2] = crc2;
16808c6b709dSTomeu Vizoso 		crcs[3] = crc3;
16818c6b709dSTomeu Vizoso 		crcs[4] = crc4;
1682246ee524STomeu Vizoso 		drm_crtc_add_crc_entry(&crtc->base, true,
1683ca814b25SDaniel Vetter 				       drm_crtc_accurate_vblank_count(&crtc->base),
1684246ee524STomeu Vizoso 				       crcs);
16858c6b709dSTomeu Vizoso 	}
16868bf1e9f1SShuang He }
1687277de95eSDaniel Vetter #else
1688277de95eSDaniel Vetter static inline void
168991d14251STvrtko Ursulin display_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
169091d14251STvrtko Ursulin 			     enum pipe pipe,
1691277de95eSDaniel Vetter 			     uint32_t crc0, uint32_t crc1,
1692277de95eSDaniel Vetter 			     uint32_t crc2, uint32_t crc3,
1693277de95eSDaniel Vetter 			     uint32_t crc4) {}
1694277de95eSDaniel Vetter #endif
1695eba94eb9SDaniel Vetter 
1696277de95eSDaniel Vetter 
169791d14251STvrtko Ursulin static void hsw_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
169891d14251STvrtko Ursulin 				     enum pipe pipe)
16995a69b89fSDaniel Vetter {
170091d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
17015a69b89fSDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
17025a69b89fSDaniel Vetter 				     0, 0, 0, 0);
17035a69b89fSDaniel Vetter }
17045a69b89fSDaniel Vetter 
170591d14251STvrtko Ursulin static void ivb_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
170691d14251STvrtko Ursulin 				     enum pipe pipe)
1707eba94eb9SDaniel Vetter {
170891d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
1709eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1710eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1711eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1712eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
17138bc5e955SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1714eba94eb9SDaniel Vetter }
17155b3a856bSDaniel Vetter 
171691d14251STvrtko Ursulin static void i9xx_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
171791d14251STvrtko Ursulin 				      enum pipe pipe)
17185b3a856bSDaniel Vetter {
17190b5c5ed0SDaniel Vetter 	uint32_t res1, res2;
17200b5c5ed0SDaniel Vetter 
172191d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 3)
17220b5c5ed0SDaniel Vetter 		res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
17230b5c5ed0SDaniel Vetter 	else
17240b5c5ed0SDaniel Vetter 		res1 = 0;
17250b5c5ed0SDaniel Vetter 
172691d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
17270b5c5ed0SDaniel Vetter 		res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
17280b5c5ed0SDaniel Vetter 	else
17290b5c5ed0SDaniel Vetter 		res2 = 0;
17305b3a856bSDaniel Vetter 
173191d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
17320b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_RED(pipe)),
17330b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_GREEN(pipe)),
17340b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_BLUE(pipe)),
17350b5c5ed0SDaniel Vetter 				     res1, res2);
17365b3a856bSDaniel Vetter }
17378bf1e9f1SShuang He 
17381403c0d4SPaulo Zanoni /* The RPS events need forcewake, so we add them to a work queue and mask their
17391403c0d4SPaulo Zanoni  * IMR bits until the work is done. Other interrupts can be processed without
17401403c0d4SPaulo Zanoni  * the work queue. */
17411403c0d4SPaulo Zanoni static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1742baf02a1fSBen Widawsky {
1743562d9baeSSagar Arun Kamble 	struct intel_rps *rps = &dev_priv->gt_pm.rps;
1744562d9baeSSagar Arun Kamble 
1745a6706b45SDeepak S 	if (pm_iir & dev_priv->pm_rps_events) {
174659cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
1747f4e9af4fSAkash Goel 		gen6_mask_pm_irq(dev_priv, pm_iir & dev_priv->pm_rps_events);
1748562d9baeSSagar Arun Kamble 		if (rps->interrupts_enabled) {
1749562d9baeSSagar Arun Kamble 			rps->pm_iir |= pm_iir & dev_priv->pm_rps_events;
1750562d9baeSSagar Arun Kamble 			schedule_work(&rps->work);
175141a05a3aSDaniel Vetter 		}
1752d4d70aa5SImre Deak 		spin_unlock(&dev_priv->irq_lock);
1753d4d70aa5SImre Deak 	}
1754baf02a1fSBen Widawsky 
1755bca2bf2aSPandiyan, Dhinakaran 	if (INTEL_GEN(dev_priv) >= 8)
1756c9a9a268SImre Deak 		return;
1757c9a9a268SImre Deak 
17582d1fe073SJoonas Lahtinen 	if (HAS_VEBOX(dev_priv)) {
175912638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
17603b3f1650SAkash Goel 			notify_ring(dev_priv->engine[VECS]);
176112638c57SBen Widawsky 
1762aaecdf61SDaniel Vetter 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT)
1763aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, pm_iir 0x%08x\n", pm_iir);
176412638c57SBen Widawsky 	}
17651403c0d4SPaulo Zanoni }
1766baf02a1fSBen Widawsky 
176726705e20SSagar Arun Kamble static void gen9_guc_irq_handler(struct drm_i915_private *dev_priv, u32 gt_iir)
176826705e20SSagar Arun Kamble {
176993bf8096SMichal Wajdeczko 	if (gt_iir & GEN9_GUC_TO_HOST_INT_EVENT)
177093bf8096SMichal Wajdeczko 		intel_guc_to_host_event_handler(&dev_priv->guc);
177126705e20SSagar Arun Kamble }
177226705e20SSagar Arun Kamble 
177344d9241eSVille Syrjälä static void i9xx_pipestat_irq_reset(struct drm_i915_private *dev_priv)
177444d9241eSVille Syrjälä {
177544d9241eSVille Syrjälä 	enum pipe pipe;
177644d9241eSVille Syrjälä 
177744d9241eSVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
177844d9241eSVille Syrjälä 		I915_WRITE(PIPESTAT(pipe),
177944d9241eSVille Syrjälä 			   PIPESTAT_INT_STATUS_MASK |
178044d9241eSVille Syrjälä 			   PIPE_FIFO_UNDERRUN_STATUS);
178144d9241eSVille Syrjälä 
178244d9241eSVille Syrjälä 		dev_priv->pipestat_irq_mask[pipe] = 0;
178344d9241eSVille Syrjälä 	}
178444d9241eSVille Syrjälä }
178544d9241eSVille Syrjälä 
1786eb64343cSVille Syrjälä static void i9xx_pipestat_irq_ack(struct drm_i915_private *dev_priv,
178791d14251STvrtko Ursulin 				  u32 iir, u32 pipe_stats[I915_MAX_PIPES])
17887e231dbeSJesse Barnes {
17897e231dbeSJesse Barnes 	int pipe;
17907e231dbeSJesse Barnes 
179158ead0d7SImre Deak 	spin_lock(&dev_priv->irq_lock);
17921ca993d2SVille Syrjälä 
17931ca993d2SVille Syrjälä 	if (!dev_priv->display_irqs_enabled) {
17941ca993d2SVille Syrjälä 		spin_unlock(&dev_priv->irq_lock);
17951ca993d2SVille Syrjälä 		return;
17961ca993d2SVille Syrjälä 	}
17971ca993d2SVille Syrjälä 
1798055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1799f0f59a00SVille Syrjälä 		i915_reg_t reg;
18006b12ca56SVille Syrjälä 		u32 status_mask, enable_mask, iir_bit = 0;
180191d181ddSImre Deak 
1802bbb5eebfSDaniel Vetter 		/*
1803bbb5eebfSDaniel Vetter 		 * PIPESTAT bits get signalled even when the interrupt is
1804bbb5eebfSDaniel Vetter 		 * disabled with the mask bits, and some of the status bits do
1805bbb5eebfSDaniel Vetter 		 * not generate interrupts at all (like the underrun bit). Hence
1806bbb5eebfSDaniel Vetter 		 * we need to be careful that we only handle what we want to
1807bbb5eebfSDaniel Vetter 		 * handle.
1808bbb5eebfSDaniel Vetter 		 */
18090f239f4cSDaniel Vetter 
18100f239f4cSDaniel Vetter 		/* fifo underruns are filterered in the underrun handler. */
18116b12ca56SVille Syrjälä 		status_mask = PIPE_FIFO_UNDERRUN_STATUS;
1812bbb5eebfSDaniel Vetter 
1813bbb5eebfSDaniel Vetter 		switch (pipe) {
1814bbb5eebfSDaniel Vetter 		case PIPE_A:
1815bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
1816bbb5eebfSDaniel Vetter 			break;
1817bbb5eebfSDaniel Vetter 		case PIPE_B:
1818bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
1819bbb5eebfSDaniel Vetter 			break;
18203278f67fSVille Syrjälä 		case PIPE_C:
18213278f67fSVille Syrjälä 			iir_bit = I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
18223278f67fSVille Syrjälä 			break;
1823bbb5eebfSDaniel Vetter 		}
1824bbb5eebfSDaniel Vetter 		if (iir & iir_bit)
18256b12ca56SVille Syrjälä 			status_mask |= dev_priv->pipestat_irq_mask[pipe];
1826bbb5eebfSDaniel Vetter 
18276b12ca56SVille Syrjälä 		if (!status_mask)
182891d181ddSImre Deak 			continue;
182991d181ddSImre Deak 
183091d181ddSImre Deak 		reg = PIPESTAT(pipe);
18316b12ca56SVille Syrjälä 		pipe_stats[pipe] = I915_READ(reg) & status_mask;
18326b12ca56SVille Syrjälä 		enable_mask = i915_pipestat_enable_mask(dev_priv, pipe);
18337e231dbeSJesse Barnes 
18347e231dbeSJesse Barnes 		/*
18357e231dbeSJesse Barnes 		 * Clear the PIPE*STAT regs before the IIR
18367e231dbeSJesse Barnes 		 */
18376b12ca56SVille Syrjälä 		if (pipe_stats[pipe])
18386b12ca56SVille Syrjälä 			I915_WRITE(reg, enable_mask | pipe_stats[pipe]);
18397e231dbeSJesse Barnes 	}
184058ead0d7SImre Deak 	spin_unlock(&dev_priv->irq_lock);
18412ecb8ca4SVille Syrjälä }
18422ecb8ca4SVille Syrjälä 
1843eb64343cSVille Syrjälä static void i8xx_pipestat_irq_handler(struct drm_i915_private *dev_priv,
1844eb64343cSVille Syrjälä 				      u16 iir, u32 pipe_stats[I915_MAX_PIPES])
1845eb64343cSVille Syrjälä {
1846eb64343cSVille Syrjälä 	enum pipe pipe;
1847eb64343cSVille Syrjälä 
1848eb64343cSVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
1849eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
1850eb64343cSVille Syrjälä 			drm_handle_vblank(&dev_priv->drm, pipe);
1851eb64343cSVille Syrjälä 
1852eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1853eb64343cSVille Syrjälä 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
1854eb64343cSVille Syrjälä 
1855eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
1856eb64343cSVille Syrjälä 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
1857eb64343cSVille Syrjälä 	}
1858eb64343cSVille Syrjälä }
1859eb64343cSVille Syrjälä 
1860eb64343cSVille Syrjälä static void i915_pipestat_irq_handler(struct drm_i915_private *dev_priv,
1861eb64343cSVille Syrjälä 				      u32 iir, u32 pipe_stats[I915_MAX_PIPES])
1862eb64343cSVille Syrjälä {
1863eb64343cSVille Syrjälä 	bool blc_event = false;
1864eb64343cSVille Syrjälä 	enum pipe pipe;
1865eb64343cSVille Syrjälä 
1866eb64343cSVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
1867eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
1868eb64343cSVille Syrjälä 			drm_handle_vblank(&dev_priv->drm, pipe);
1869eb64343cSVille Syrjälä 
1870eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
1871eb64343cSVille Syrjälä 			blc_event = true;
1872eb64343cSVille Syrjälä 
1873eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1874eb64343cSVille Syrjälä 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
1875eb64343cSVille Syrjälä 
1876eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
1877eb64343cSVille Syrjälä 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
1878eb64343cSVille Syrjälä 	}
1879eb64343cSVille Syrjälä 
1880eb64343cSVille Syrjälä 	if (blc_event || (iir & I915_ASLE_INTERRUPT))
1881eb64343cSVille Syrjälä 		intel_opregion_asle_intr(dev_priv);
1882eb64343cSVille Syrjälä }
1883eb64343cSVille Syrjälä 
1884eb64343cSVille Syrjälä static void i965_pipestat_irq_handler(struct drm_i915_private *dev_priv,
1885eb64343cSVille Syrjälä 				      u32 iir, u32 pipe_stats[I915_MAX_PIPES])
1886eb64343cSVille Syrjälä {
1887eb64343cSVille Syrjälä 	bool blc_event = false;
1888eb64343cSVille Syrjälä 	enum pipe pipe;
1889eb64343cSVille Syrjälä 
1890eb64343cSVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
1891eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS)
1892eb64343cSVille Syrjälä 			drm_handle_vblank(&dev_priv->drm, pipe);
1893eb64343cSVille Syrjälä 
1894eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
1895eb64343cSVille Syrjälä 			blc_event = true;
1896eb64343cSVille Syrjälä 
1897eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1898eb64343cSVille Syrjälä 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
1899eb64343cSVille Syrjälä 
1900eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
1901eb64343cSVille Syrjälä 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
1902eb64343cSVille Syrjälä 	}
1903eb64343cSVille Syrjälä 
1904eb64343cSVille Syrjälä 	if (blc_event || (iir & I915_ASLE_INTERRUPT))
1905eb64343cSVille Syrjälä 		intel_opregion_asle_intr(dev_priv);
1906eb64343cSVille Syrjälä 
1907eb64343cSVille Syrjälä 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1908eb64343cSVille Syrjälä 		gmbus_irq_handler(dev_priv);
1909eb64343cSVille Syrjälä }
1910eb64343cSVille Syrjälä 
191191d14251STvrtko Ursulin static void valleyview_pipestat_irq_handler(struct drm_i915_private *dev_priv,
19122ecb8ca4SVille Syrjälä 					    u32 pipe_stats[I915_MAX_PIPES])
19132ecb8ca4SVille Syrjälä {
19142ecb8ca4SVille Syrjälä 	enum pipe pipe;
19157e231dbeSJesse Barnes 
1916055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1917fd3a4024SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS)
1918fd3a4024SDaniel Vetter 			drm_handle_vblank(&dev_priv->drm, pipe);
19194356d586SDaniel Vetter 
19204356d586SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
192191d14251STvrtko Ursulin 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
19222d9d2b0bSVille Syrjälä 
19231f7247c0SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
19241f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
192531acc7f5SJesse Barnes 	}
192631acc7f5SJesse Barnes 
1927c1874ed7SImre Deak 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
192891d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
1929c1874ed7SImre Deak }
1930c1874ed7SImre Deak 
19311ae3c34cSVille Syrjälä static u32 i9xx_hpd_irq_ack(struct drm_i915_private *dev_priv)
193216c6c56bSVille Syrjälä {
193316c6c56bSVille Syrjälä 	u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
193416c6c56bSVille Syrjälä 
19351ae3c34cSVille Syrjälä 	if (hotplug_status)
19363ff60f89SOscar Mateo 		I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
19371ae3c34cSVille Syrjälä 
19381ae3c34cSVille Syrjälä 	return hotplug_status;
19391ae3c34cSVille Syrjälä }
19401ae3c34cSVille Syrjälä 
194191d14251STvrtko Ursulin static void i9xx_hpd_irq_handler(struct drm_i915_private *dev_priv,
19421ae3c34cSVille Syrjälä 				 u32 hotplug_status)
19431ae3c34cSVille Syrjälä {
19441ae3c34cSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
19453ff60f89SOscar Mateo 
194691d14251STvrtko Ursulin 	if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
194791d14251STvrtko Ursulin 	    IS_CHERRYVIEW(dev_priv)) {
194816c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
194916c6c56bSVille Syrjälä 
195058f2cf24SVille Syrjälä 		if (hotplug_trigger) {
1951cf53902fSRodrigo Vivi 			intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
1952cf53902fSRodrigo Vivi 					   hotplug_trigger, hotplug_trigger,
1953cf53902fSRodrigo Vivi 					   hpd_status_g4x,
1954fd63e2a9SImre Deak 					   i9xx_port_hotplug_long_detect);
195558f2cf24SVille Syrjälä 
195691d14251STvrtko Ursulin 			intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
195758f2cf24SVille Syrjälä 		}
1958369712e8SJani Nikula 
1959369712e8SJani Nikula 		if (hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
196091d14251STvrtko Ursulin 			dp_aux_irq_handler(dev_priv);
196116c6c56bSVille Syrjälä 	} else {
196216c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
196316c6c56bSVille Syrjälä 
196458f2cf24SVille Syrjälä 		if (hotplug_trigger) {
1965cf53902fSRodrigo Vivi 			intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
1966cf53902fSRodrigo Vivi 					   hotplug_trigger, hotplug_trigger,
1967cf53902fSRodrigo Vivi 					   hpd_status_i915,
1968fd63e2a9SImre Deak 					   i9xx_port_hotplug_long_detect);
196991d14251STvrtko Ursulin 			intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
197016c6c56bSVille Syrjälä 		}
19713ff60f89SOscar Mateo 	}
197258f2cf24SVille Syrjälä }
197316c6c56bSVille Syrjälä 
1974c1874ed7SImre Deak static irqreturn_t valleyview_irq_handler(int irq, void *arg)
1975c1874ed7SImre Deak {
197645a83f84SDaniel Vetter 	struct drm_device *dev = arg;
1977fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
1978c1874ed7SImre Deak 	irqreturn_t ret = IRQ_NONE;
1979c1874ed7SImre Deak 
19802dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
19812dd2a883SImre Deak 		return IRQ_NONE;
19822dd2a883SImre Deak 
19831f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
19841f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
19851f814dacSImre Deak 
19861e1cace9SVille Syrjälä 	do {
19876e814800SVille Syrjälä 		u32 iir, gt_iir, pm_iir;
19882ecb8ca4SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
19891ae3c34cSVille Syrjälä 		u32 hotplug_status = 0;
1990a5e485a9SVille Syrjälä 		u32 ier = 0;
19913ff60f89SOscar Mateo 
1992c1874ed7SImre Deak 		gt_iir = I915_READ(GTIIR);
1993c1874ed7SImre Deak 		pm_iir = I915_READ(GEN6_PMIIR);
19943ff60f89SOscar Mateo 		iir = I915_READ(VLV_IIR);
1995c1874ed7SImre Deak 
1996c1874ed7SImre Deak 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
19971e1cace9SVille Syrjälä 			break;
1998c1874ed7SImre Deak 
1999c1874ed7SImre Deak 		ret = IRQ_HANDLED;
2000c1874ed7SImre Deak 
2001a5e485a9SVille Syrjälä 		/*
2002a5e485a9SVille Syrjälä 		 * Theory on interrupt generation, based on empirical evidence:
2003a5e485a9SVille Syrjälä 		 *
2004a5e485a9SVille Syrjälä 		 * x = ((VLV_IIR & VLV_IER) ||
2005a5e485a9SVille Syrjälä 		 *      (((GT_IIR & GT_IER) || (GEN6_PMIIR & GEN6_PMIER)) &&
2006a5e485a9SVille Syrjälä 		 *       (VLV_MASTER_IER & MASTER_INTERRUPT_ENABLE)));
2007a5e485a9SVille Syrjälä 		 *
2008a5e485a9SVille Syrjälä 		 * A CPU interrupt will only be raised when 'x' has a 0->1 edge.
2009a5e485a9SVille Syrjälä 		 * Hence we clear MASTER_INTERRUPT_ENABLE and VLV_IER to
2010a5e485a9SVille Syrjälä 		 * guarantee the CPU interrupt will be raised again even if we
2011a5e485a9SVille Syrjälä 		 * don't end up clearing all the VLV_IIR, GT_IIR, GEN6_PMIIR
2012a5e485a9SVille Syrjälä 		 * bits this time around.
2013a5e485a9SVille Syrjälä 		 */
20144a0a0202SVille Syrjälä 		I915_WRITE(VLV_MASTER_IER, 0);
2015a5e485a9SVille Syrjälä 		ier = I915_READ(VLV_IER);
2016a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, 0);
20174a0a0202SVille Syrjälä 
20184a0a0202SVille Syrjälä 		if (gt_iir)
20194a0a0202SVille Syrjälä 			I915_WRITE(GTIIR, gt_iir);
20204a0a0202SVille Syrjälä 		if (pm_iir)
20214a0a0202SVille Syrjälä 			I915_WRITE(GEN6_PMIIR, pm_iir);
20224a0a0202SVille Syrjälä 
20237ce4d1f2SVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
20241ae3c34cSVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
20257ce4d1f2SVille Syrjälä 
20263ff60f89SOscar Mateo 		/* Call regardless, as some status bits might not be
20273ff60f89SOscar Mateo 		 * signalled in iir */
2028eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
20297ce4d1f2SVille Syrjälä 
2030eef57324SJerome Anand 		if (iir & (I915_LPE_PIPE_A_INTERRUPT |
2031eef57324SJerome Anand 			   I915_LPE_PIPE_B_INTERRUPT))
2032eef57324SJerome Anand 			intel_lpe_audio_irq_handler(dev_priv);
2033eef57324SJerome Anand 
20347ce4d1f2SVille Syrjälä 		/*
20357ce4d1f2SVille Syrjälä 		 * VLV_IIR is single buffered, and reflects the level
20367ce4d1f2SVille Syrjälä 		 * from PIPESTAT/PORT_HOTPLUG_STAT, hence clear it last.
20377ce4d1f2SVille Syrjälä 		 */
20387ce4d1f2SVille Syrjälä 		if (iir)
20397ce4d1f2SVille Syrjälä 			I915_WRITE(VLV_IIR, iir);
20404a0a0202SVille Syrjälä 
2041a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, ier);
20424a0a0202SVille Syrjälä 		I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
20434a0a0202SVille Syrjälä 		POSTING_READ(VLV_MASTER_IER);
20441ae3c34cSVille Syrjälä 
204552894874SVille Syrjälä 		if (gt_iir)
2046261e40b8SVille Syrjälä 			snb_gt_irq_handler(dev_priv, gt_iir);
204752894874SVille Syrjälä 		if (pm_iir)
204852894874SVille Syrjälä 			gen6_rps_irq_handler(dev_priv, pm_iir);
204952894874SVille Syrjälä 
20501ae3c34cSVille Syrjälä 		if (hotplug_status)
205191d14251STvrtko Ursulin 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
20522ecb8ca4SVille Syrjälä 
205391d14251STvrtko Ursulin 		valleyview_pipestat_irq_handler(dev_priv, pipe_stats);
20541e1cace9SVille Syrjälä 	} while (0);
20557e231dbeSJesse Barnes 
20561f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
20571f814dacSImre Deak 
20587e231dbeSJesse Barnes 	return ret;
20597e231dbeSJesse Barnes }
20607e231dbeSJesse Barnes 
206143f328d7SVille Syrjälä static irqreturn_t cherryview_irq_handler(int irq, void *arg)
206243f328d7SVille Syrjälä {
206345a83f84SDaniel Vetter 	struct drm_device *dev = arg;
2064fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
206543f328d7SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
206643f328d7SVille Syrjälä 
20672dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
20682dd2a883SImre Deak 		return IRQ_NONE;
20692dd2a883SImre Deak 
20701f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
20711f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
20721f814dacSImre Deak 
2073579de73bSChris Wilson 	do {
20746e814800SVille Syrjälä 		u32 master_ctl, iir;
20752ecb8ca4SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
20761ae3c34cSVille Syrjälä 		u32 hotplug_status = 0;
2077f0fd96f5SChris Wilson 		u32 gt_iir[4];
2078a5e485a9SVille Syrjälä 		u32 ier = 0;
2079a5e485a9SVille Syrjälä 
20808e5fd599SVille Syrjälä 		master_ctl = I915_READ(GEN8_MASTER_IRQ) & ~GEN8_MASTER_IRQ_CONTROL;
20813278f67fSVille Syrjälä 		iir = I915_READ(VLV_IIR);
20823278f67fSVille Syrjälä 
20833278f67fSVille Syrjälä 		if (master_ctl == 0 && iir == 0)
20848e5fd599SVille Syrjälä 			break;
208543f328d7SVille Syrjälä 
208627b6c122SOscar Mateo 		ret = IRQ_HANDLED;
208727b6c122SOscar Mateo 
2088a5e485a9SVille Syrjälä 		/*
2089a5e485a9SVille Syrjälä 		 * Theory on interrupt generation, based on empirical evidence:
2090a5e485a9SVille Syrjälä 		 *
2091a5e485a9SVille Syrjälä 		 * x = ((VLV_IIR & VLV_IER) ||
2092a5e485a9SVille Syrjälä 		 *      ((GEN8_MASTER_IRQ & ~GEN8_MASTER_IRQ_CONTROL) &&
2093a5e485a9SVille Syrjälä 		 *       (GEN8_MASTER_IRQ & GEN8_MASTER_IRQ_CONTROL)));
2094a5e485a9SVille Syrjälä 		 *
2095a5e485a9SVille Syrjälä 		 * A CPU interrupt will only be raised when 'x' has a 0->1 edge.
2096a5e485a9SVille Syrjälä 		 * Hence we clear GEN8_MASTER_IRQ_CONTROL and VLV_IER to
2097a5e485a9SVille Syrjälä 		 * guarantee the CPU interrupt will be raised again even if we
2098a5e485a9SVille Syrjälä 		 * don't end up clearing all the VLV_IIR and GEN8_MASTER_IRQ_CONTROL
2099a5e485a9SVille Syrjälä 		 * bits this time around.
2100a5e485a9SVille Syrjälä 		 */
210143f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, 0);
2102a5e485a9SVille Syrjälä 		ier = I915_READ(VLV_IER);
2103a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, 0);
210443f328d7SVille Syrjälä 
2105e30e251aSVille Syrjälä 		gen8_gt_irq_ack(dev_priv, master_ctl, gt_iir);
210627b6c122SOscar Mateo 
210727b6c122SOscar Mateo 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
21081ae3c34cSVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
210943f328d7SVille Syrjälä 
211027b6c122SOscar Mateo 		/* Call regardless, as some status bits might not be
211127b6c122SOscar Mateo 		 * signalled in iir */
2112eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
211343f328d7SVille Syrjälä 
2114eef57324SJerome Anand 		if (iir & (I915_LPE_PIPE_A_INTERRUPT |
2115eef57324SJerome Anand 			   I915_LPE_PIPE_B_INTERRUPT |
2116eef57324SJerome Anand 			   I915_LPE_PIPE_C_INTERRUPT))
2117eef57324SJerome Anand 			intel_lpe_audio_irq_handler(dev_priv);
2118eef57324SJerome Anand 
21197ce4d1f2SVille Syrjälä 		/*
21207ce4d1f2SVille Syrjälä 		 * VLV_IIR is single buffered, and reflects the level
21217ce4d1f2SVille Syrjälä 		 * from PIPESTAT/PORT_HOTPLUG_STAT, hence clear it last.
21227ce4d1f2SVille Syrjälä 		 */
21237ce4d1f2SVille Syrjälä 		if (iir)
21247ce4d1f2SVille Syrjälä 			I915_WRITE(VLV_IIR, iir);
21257ce4d1f2SVille Syrjälä 
2126a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, ier);
2127e5328c43SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
212843f328d7SVille Syrjälä 		POSTING_READ(GEN8_MASTER_IRQ);
21291ae3c34cSVille Syrjälä 
2130f0fd96f5SChris Wilson 		gen8_gt_irq_handler(dev_priv, master_ctl, gt_iir);
2131e30e251aSVille Syrjälä 
21321ae3c34cSVille Syrjälä 		if (hotplug_status)
213391d14251STvrtko Ursulin 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
21342ecb8ca4SVille Syrjälä 
213591d14251STvrtko Ursulin 		valleyview_pipestat_irq_handler(dev_priv, pipe_stats);
2136579de73bSChris Wilson 	} while (0);
21373278f67fSVille Syrjälä 
21381f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
21391f814dacSImre Deak 
214043f328d7SVille Syrjälä 	return ret;
214143f328d7SVille Syrjälä }
214243f328d7SVille Syrjälä 
214391d14251STvrtko Ursulin static void ibx_hpd_irq_handler(struct drm_i915_private *dev_priv,
214491d14251STvrtko Ursulin 				u32 hotplug_trigger,
214540e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2146776ad806SJesse Barnes {
214742db67d6SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2148776ad806SJesse Barnes 
21496a39d7c9SJani Nikula 	/*
21506a39d7c9SJani Nikula 	 * Somehow the PCH doesn't seem to really ack the interrupt to the CPU
21516a39d7c9SJani Nikula 	 * unless we touch the hotplug register, even if hotplug_trigger is
21526a39d7c9SJani Nikula 	 * zero. Not acking leads to "The master control interrupt lied (SDE)!"
21536a39d7c9SJani Nikula 	 * errors.
21546a39d7c9SJani Nikula 	 */
215513cf5504SDave Airlie 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
21566a39d7c9SJani Nikula 	if (!hotplug_trigger) {
21576a39d7c9SJani Nikula 		u32 mask = PORTA_HOTPLUG_STATUS_MASK |
21586a39d7c9SJani Nikula 			PORTD_HOTPLUG_STATUS_MASK |
21596a39d7c9SJani Nikula 			PORTC_HOTPLUG_STATUS_MASK |
21606a39d7c9SJani Nikula 			PORTB_HOTPLUG_STATUS_MASK;
21616a39d7c9SJani Nikula 		dig_hotplug_reg &= ~mask;
21626a39d7c9SJani Nikula 	}
21636a39d7c9SJani Nikula 
216413cf5504SDave Airlie 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
21656a39d7c9SJani Nikula 	if (!hotplug_trigger)
21666a39d7c9SJani Nikula 		return;
216713cf5504SDave Airlie 
2168cf53902fSRodrigo Vivi 	intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask, hotplug_trigger,
216940e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2170fd63e2a9SImre Deak 			   pch_port_hotplug_long_detect);
217140e56410SVille Syrjälä 
217291d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2173aaf5ec2eSSonika Jindal }
217491d131d2SDaniel Vetter 
217591d14251STvrtko Ursulin static void ibx_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
217640e56410SVille Syrjälä {
217740e56410SVille Syrjälä 	int pipe;
217840e56410SVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
217940e56410SVille Syrjälä 
218091d14251STvrtko Ursulin 	ibx_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ibx);
218140e56410SVille Syrjälä 
2182cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
2183cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
2184776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
2185cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
2186cfc33bf7SVille Syrjälä 				 port_name(port));
2187cfc33bf7SVille Syrjälä 	}
2188776ad806SJesse Barnes 
2189ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
219091d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
2191ce99c256SDaniel Vetter 
2192776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
219391d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
2194776ad806SJesse Barnes 
2195776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
2196776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
2197776ad806SJesse Barnes 
2198776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
2199776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
2200776ad806SJesse Barnes 
2201776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
2202776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
2203776ad806SJesse Barnes 
22049db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
2205055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
22069db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
22079db4a9c7SJesse Barnes 					 pipe_name(pipe),
22089db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
2209776ad806SJesse Barnes 
2210776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
2211776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
2212776ad806SJesse Barnes 
2213776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
2214776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
2215776ad806SJesse Barnes 
2216776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
2217a2196033SMatthias Kaehlcke 		intel_pch_fifo_underrun_irq_handler(dev_priv, PIPE_A);
22188664281bSPaulo Zanoni 
22198664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
2220a2196033SMatthias Kaehlcke 		intel_pch_fifo_underrun_irq_handler(dev_priv, PIPE_B);
22218664281bSPaulo Zanoni }
22228664281bSPaulo Zanoni 
222391d14251STvrtko Ursulin static void ivb_err_int_handler(struct drm_i915_private *dev_priv)
22248664281bSPaulo Zanoni {
22258664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
22265a69b89fSDaniel Vetter 	enum pipe pipe;
22278664281bSPaulo Zanoni 
2228de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
2229de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
2230de032bf4SPaulo Zanoni 
2231055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
22321f7247c0SDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe))
22331f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
22348664281bSPaulo Zanoni 
22355a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
223691d14251STvrtko Ursulin 			if (IS_IVYBRIDGE(dev_priv))
223791d14251STvrtko Ursulin 				ivb_pipe_crc_irq_handler(dev_priv, pipe);
22385a69b89fSDaniel Vetter 			else
223991d14251STvrtko Ursulin 				hsw_pipe_crc_irq_handler(dev_priv, pipe);
22405a69b89fSDaniel Vetter 		}
22415a69b89fSDaniel Vetter 	}
22428bf1e9f1SShuang He 
22438664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
22448664281bSPaulo Zanoni }
22458664281bSPaulo Zanoni 
224691d14251STvrtko Ursulin static void cpt_serr_int_handler(struct drm_i915_private *dev_priv)
22478664281bSPaulo Zanoni {
22488664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
224945c1cd87SMika Kahola 	enum pipe pipe;
22508664281bSPaulo Zanoni 
2251de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
2252de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
2253de032bf4SPaulo Zanoni 
225445c1cd87SMika Kahola 	for_each_pipe(dev_priv, pipe)
225545c1cd87SMika Kahola 		if (serr_int & SERR_INT_TRANS_FIFO_UNDERRUN(pipe))
225645c1cd87SMika Kahola 			intel_pch_fifo_underrun_irq_handler(dev_priv, pipe);
22578664281bSPaulo Zanoni 
22588664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
2259776ad806SJesse Barnes }
2260776ad806SJesse Barnes 
226191d14251STvrtko Ursulin static void cpt_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
226223e81d69SAdam Jackson {
226323e81d69SAdam Jackson 	int pipe;
22646dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
2265aaf5ec2eSSonika Jindal 
226691d14251STvrtko Ursulin 	ibx_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_cpt);
226791d131d2SDaniel Vetter 
2268cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
2269cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
227023e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
2271cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
2272cfc33bf7SVille Syrjälä 				 port_name(port));
2273cfc33bf7SVille Syrjälä 	}
227423e81d69SAdam Jackson 
227523e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
227691d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
227723e81d69SAdam Jackson 
227823e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
227991d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
228023e81d69SAdam Jackson 
228123e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
228223e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
228323e81d69SAdam Jackson 
228423e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
228523e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
228623e81d69SAdam Jackson 
228723e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
2288055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
228923e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
229023e81d69SAdam Jackson 					 pipe_name(pipe),
229123e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
22928664281bSPaulo Zanoni 
22938664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
229491d14251STvrtko Ursulin 		cpt_serr_int_handler(dev_priv);
229523e81d69SAdam Jackson }
229623e81d69SAdam Jackson 
229791d14251STvrtko Ursulin static void spt_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
22986dbf30ceSVille Syrjälä {
22996dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_SPT &
23006dbf30ceSVille Syrjälä 		~SDE_PORTE_HOTPLUG_SPT;
23016dbf30ceSVille Syrjälä 	u32 hotplug2_trigger = pch_iir & SDE_PORTE_HOTPLUG_SPT;
23026dbf30ceSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
23036dbf30ceSVille Syrjälä 
23046dbf30ceSVille Syrjälä 	if (hotplug_trigger) {
23056dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
23066dbf30ceSVille Syrjälä 
23076dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
23086dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
23096dbf30ceSVille Syrjälä 
2310cf53902fSRodrigo Vivi 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
2311cf53902fSRodrigo Vivi 				   hotplug_trigger, dig_hotplug_reg, hpd_spt,
231274c0b395SVille Syrjälä 				   spt_port_hotplug_long_detect);
23136dbf30ceSVille Syrjälä 	}
23146dbf30ceSVille Syrjälä 
23156dbf30ceSVille Syrjälä 	if (hotplug2_trigger) {
23166dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
23176dbf30ceSVille Syrjälä 
23186dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG2);
23196dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG2, dig_hotplug_reg);
23206dbf30ceSVille Syrjälä 
2321cf53902fSRodrigo Vivi 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
2322cf53902fSRodrigo Vivi 				   hotplug2_trigger, dig_hotplug_reg, hpd_spt,
23236dbf30ceSVille Syrjälä 				   spt_port_hotplug2_long_detect);
23246dbf30ceSVille Syrjälä 	}
23256dbf30ceSVille Syrjälä 
23266dbf30ceSVille Syrjälä 	if (pin_mask)
232791d14251STvrtko Ursulin 		intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
23286dbf30ceSVille Syrjälä 
23296dbf30ceSVille Syrjälä 	if (pch_iir & SDE_GMBUS_CPT)
233091d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
23316dbf30ceSVille Syrjälä }
23326dbf30ceSVille Syrjälä 
233391d14251STvrtko Ursulin static void ilk_hpd_irq_handler(struct drm_i915_private *dev_priv,
233491d14251STvrtko Ursulin 				u32 hotplug_trigger,
233540e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2336c008bc6eSPaulo Zanoni {
2337e4ce95aaSVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2338e4ce95aaSVille Syrjälä 
2339e4ce95aaSVille Syrjälä 	dig_hotplug_reg = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
2340e4ce95aaSVille Syrjälä 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, dig_hotplug_reg);
2341e4ce95aaSVille Syrjälä 
2342cf53902fSRodrigo Vivi 	intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask, hotplug_trigger,
234340e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2344e4ce95aaSVille Syrjälä 			   ilk_port_hotplug_long_detect);
234540e56410SVille Syrjälä 
234691d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2347e4ce95aaSVille Syrjälä }
2348c008bc6eSPaulo Zanoni 
234991d14251STvrtko Ursulin static void ilk_display_irq_handler(struct drm_i915_private *dev_priv,
235091d14251STvrtko Ursulin 				    u32 de_iir)
235140e56410SVille Syrjälä {
235240e56410SVille Syrjälä 	enum pipe pipe;
235340e56410SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG;
235440e56410SVille Syrjälä 
235540e56410SVille Syrjälä 	if (hotplug_trigger)
235691d14251STvrtko Ursulin 		ilk_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ilk);
235740e56410SVille Syrjälä 
2358c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
235991d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
2360c008bc6eSPaulo Zanoni 
2361c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
236291d14251STvrtko Ursulin 		intel_opregion_asle_intr(dev_priv);
2363c008bc6eSPaulo Zanoni 
2364c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
2365c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
2366c008bc6eSPaulo Zanoni 
2367055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2368fd3a4024SDaniel Vetter 		if (de_iir & DE_PIPE_VBLANK(pipe))
2369fd3a4024SDaniel Vetter 			drm_handle_vblank(&dev_priv->drm, pipe);
2370c008bc6eSPaulo Zanoni 
237140da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
23721f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
2373c008bc6eSPaulo Zanoni 
237440da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
237591d14251STvrtko Ursulin 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
2376c008bc6eSPaulo Zanoni 	}
2377c008bc6eSPaulo Zanoni 
2378c008bc6eSPaulo Zanoni 	/* check event from PCH */
2379c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
2380c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
2381c008bc6eSPaulo Zanoni 
238291d14251STvrtko Ursulin 		if (HAS_PCH_CPT(dev_priv))
238391d14251STvrtko Ursulin 			cpt_irq_handler(dev_priv, pch_iir);
2384c008bc6eSPaulo Zanoni 		else
238591d14251STvrtko Ursulin 			ibx_irq_handler(dev_priv, pch_iir);
2386c008bc6eSPaulo Zanoni 
2387c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
2388c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
2389c008bc6eSPaulo Zanoni 	}
2390c008bc6eSPaulo Zanoni 
239191d14251STvrtko Ursulin 	if (IS_GEN5(dev_priv) && de_iir & DE_PCU_EVENT)
239291d14251STvrtko Ursulin 		ironlake_rps_change_irq_handler(dev_priv);
2393c008bc6eSPaulo Zanoni }
2394c008bc6eSPaulo Zanoni 
239591d14251STvrtko Ursulin static void ivb_display_irq_handler(struct drm_i915_private *dev_priv,
239691d14251STvrtko Ursulin 				    u32 de_iir)
23979719fb98SPaulo Zanoni {
239807d27e20SDamien Lespiau 	enum pipe pipe;
239923bb4cb5SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG_IVB;
240023bb4cb5SVille Syrjälä 
240140e56410SVille Syrjälä 	if (hotplug_trigger)
240291d14251STvrtko Ursulin 		ilk_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ivb);
24039719fb98SPaulo Zanoni 
24049719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
240591d14251STvrtko Ursulin 		ivb_err_int_handler(dev_priv);
24069719fb98SPaulo Zanoni 
24079719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
240891d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
24099719fb98SPaulo Zanoni 
24109719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
241191d14251STvrtko Ursulin 		intel_opregion_asle_intr(dev_priv);
24129719fb98SPaulo Zanoni 
2413055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2414fd3a4024SDaniel Vetter 		if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)))
2415fd3a4024SDaniel Vetter 			drm_handle_vblank(&dev_priv->drm, pipe);
24169719fb98SPaulo Zanoni 	}
24179719fb98SPaulo Zanoni 
24189719fb98SPaulo Zanoni 	/* check event from PCH */
241991d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv) && (de_iir & DE_PCH_EVENT_IVB)) {
24209719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
24219719fb98SPaulo Zanoni 
242291d14251STvrtko Ursulin 		cpt_irq_handler(dev_priv, pch_iir);
24239719fb98SPaulo Zanoni 
24249719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
24259719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
24269719fb98SPaulo Zanoni 	}
24279719fb98SPaulo Zanoni }
24289719fb98SPaulo Zanoni 
242972c90f62SOscar Mateo /*
243072c90f62SOscar Mateo  * To handle irqs with the minimum potential races with fresh interrupts, we:
243172c90f62SOscar Mateo  * 1 - Disable Master Interrupt Control.
243272c90f62SOscar Mateo  * 2 - Find the source(s) of the interrupt.
243372c90f62SOscar Mateo  * 3 - Clear the Interrupt Identity bits (IIR).
243472c90f62SOscar Mateo  * 4 - Process the interrupt(s) that had bits set in the IIRs.
243572c90f62SOscar Mateo  * 5 - Re-enable Master Interrupt Control.
243672c90f62SOscar Mateo  */
2437f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
2438b1f14ad0SJesse Barnes {
243945a83f84SDaniel Vetter 	struct drm_device *dev = arg;
2440fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2441f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
24420e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
2443b1f14ad0SJesse Barnes 
24442dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
24452dd2a883SImre Deak 		return IRQ_NONE;
24462dd2a883SImre Deak 
24471f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
24481f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
24491f814dacSImre Deak 
2450b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
2451b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
2452b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
245323a78516SPaulo Zanoni 	POSTING_READ(DEIER);
24540e43406bSChris Wilson 
245544498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
245644498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
245744498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
245844498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
245944498aeaSPaulo Zanoni 	 * due to its back queue). */
246091d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv)) {
246144498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
246244498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
246344498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2464ab5c608bSBen Widawsky 	}
246544498aeaSPaulo Zanoni 
246672c90f62SOscar Mateo 	/* Find, clear, then process each source of interrupt */
246772c90f62SOscar Mateo 
24680e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
24690e43406bSChris Wilson 	if (gt_iir) {
247072c90f62SOscar Mateo 		I915_WRITE(GTIIR, gt_iir);
247172c90f62SOscar Mateo 		ret = IRQ_HANDLED;
247291d14251STvrtko Ursulin 		if (INTEL_GEN(dev_priv) >= 6)
2473261e40b8SVille Syrjälä 			snb_gt_irq_handler(dev_priv, gt_iir);
2474d8fc8a47SPaulo Zanoni 		else
2475261e40b8SVille Syrjälä 			ilk_gt_irq_handler(dev_priv, gt_iir);
24760e43406bSChris Wilson 	}
2477b1f14ad0SJesse Barnes 
2478b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
24790e43406bSChris Wilson 	if (de_iir) {
248072c90f62SOscar Mateo 		I915_WRITE(DEIIR, de_iir);
248172c90f62SOscar Mateo 		ret = IRQ_HANDLED;
248291d14251STvrtko Ursulin 		if (INTEL_GEN(dev_priv) >= 7)
248391d14251STvrtko Ursulin 			ivb_display_irq_handler(dev_priv, de_iir);
2484f1af8fc1SPaulo Zanoni 		else
248591d14251STvrtko Ursulin 			ilk_display_irq_handler(dev_priv, de_iir);
24860e43406bSChris Wilson 	}
24870e43406bSChris Wilson 
248891d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 6) {
2489f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
24900e43406bSChris Wilson 		if (pm_iir) {
2491b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
24920e43406bSChris Wilson 			ret = IRQ_HANDLED;
249372c90f62SOscar Mateo 			gen6_rps_irq_handler(dev_priv, pm_iir);
24940e43406bSChris Wilson 		}
2495f1af8fc1SPaulo Zanoni 	}
2496b1f14ad0SJesse Barnes 
2497b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
2498b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
249991d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv)) {
250044498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
250144498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2502ab5c608bSBen Widawsky 	}
2503b1f14ad0SJesse Barnes 
25041f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
25051f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
25061f814dacSImre Deak 
2507b1f14ad0SJesse Barnes 	return ret;
2508b1f14ad0SJesse Barnes }
2509b1f14ad0SJesse Barnes 
251091d14251STvrtko Ursulin static void bxt_hpd_irq_handler(struct drm_i915_private *dev_priv,
251191d14251STvrtko Ursulin 				u32 hotplug_trigger,
251240e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2513d04a492dSShashank Sharma {
2514cebd87a0SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2515d04a492dSShashank Sharma 
2516a52bb15bSVille Syrjälä 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
2517a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
2518d04a492dSShashank Sharma 
2519cf53902fSRodrigo Vivi 	intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask, hotplug_trigger,
252040e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2521cebd87a0SVille Syrjälä 			   bxt_port_hotplug_long_detect);
252240e56410SVille Syrjälä 
252391d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2524d04a492dSShashank Sharma }
2525d04a492dSShashank Sharma 
2526f11a0f46STvrtko Ursulin static irqreturn_t
2527f11a0f46STvrtko Ursulin gen8_de_irq_handler(struct drm_i915_private *dev_priv, u32 master_ctl)
2528abd58f01SBen Widawsky {
2529abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
2530f11a0f46STvrtko Ursulin 	u32 iir;
2531c42664ccSDaniel Vetter 	enum pipe pipe;
253288e04703SJesse Barnes 
2533abd58f01SBen Widawsky 	if (master_ctl & GEN8_DE_MISC_IRQ) {
2534e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_MISC_IIR);
2535e32192e1STvrtko Ursulin 		if (iir) {
2536e32192e1STvrtko Ursulin 			I915_WRITE(GEN8_DE_MISC_IIR, iir);
2537abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
2538e32192e1STvrtko Ursulin 			if (iir & GEN8_DE_MISC_GSE)
253991d14251STvrtko Ursulin 				intel_opregion_asle_intr(dev_priv);
254038cc46d7SOscar Mateo 			else
254138cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Misc interrupt\n");
2542abd58f01SBen Widawsky 		}
254338cc46d7SOscar Mateo 		else
254438cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
2545abd58f01SBen Widawsky 	}
2546abd58f01SBen Widawsky 
25476d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
2548e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_PORT_IIR);
2549e32192e1STvrtko Ursulin 		if (iir) {
2550e32192e1STvrtko Ursulin 			u32 tmp_mask;
2551d04a492dSShashank Sharma 			bool found = false;
2552cebd87a0SVille Syrjälä 
2553e32192e1STvrtko Ursulin 			I915_WRITE(GEN8_DE_PORT_IIR, iir);
25546d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
255588e04703SJesse Barnes 
2556e32192e1STvrtko Ursulin 			tmp_mask = GEN8_AUX_CHANNEL_A;
2557bca2bf2aSPandiyan, Dhinakaran 			if (INTEL_GEN(dev_priv) >= 9)
2558e32192e1STvrtko Ursulin 				tmp_mask |= GEN9_AUX_CHANNEL_B |
2559e32192e1STvrtko Ursulin 					    GEN9_AUX_CHANNEL_C |
2560e32192e1STvrtko Ursulin 					    GEN9_AUX_CHANNEL_D;
2561e32192e1STvrtko Ursulin 
2562a324fcacSRodrigo Vivi 			if (IS_CNL_WITH_PORT_F(dev_priv))
2563a324fcacSRodrigo Vivi 				tmp_mask |= CNL_AUX_CHANNEL_F;
2564a324fcacSRodrigo Vivi 
2565e32192e1STvrtko Ursulin 			if (iir & tmp_mask) {
256691d14251STvrtko Ursulin 				dp_aux_irq_handler(dev_priv);
2567d04a492dSShashank Sharma 				found = true;
2568d04a492dSShashank Sharma 			}
2569d04a492dSShashank Sharma 
2570cc3f90f0SAnder Conselvan de Oliveira 			if (IS_GEN9_LP(dev_priv)) {
2571e32192e1STvrtko Ursulin 				tmp_mask = iir & BXT_DE_PORT_HOTPLUG_MASK;
2572e32192e1STvrtko Ursulin 				if (tmp_mask) {
257391d14251STvrtko Ursulin 					bxt_hpd_irq_handler(dev_priv, tmp_mask,
257491d14251STvrtko Ursulin 							    hpd_bxt);
2575d04a492dSShashank Sharma 					found = true;
2576d04a492dSShashank Sharma 				}
2577e32192e1STvrtko Ursulin 			} else if (IS_BROADWELL(dev_priv)) {
2578e32192e1STvrtko Ursulin 				tmp_mask = iir & GEN8_PORT_DP_A_HOTPLUG;
2579e32192e1STvrtko Ursulin 				if (tmp_mask) {
258091d14251STvrtko Ursulin 					ilk_hpd_irq_handler(dev_priv,
258191d14251STvrtko Ursulin 							    tmp_mask, hpd_bdw);
2582e32192e1STvrtko Ursulin 					found = true;
2583e32192e1STvrtko Ursulin 				}
2584e32192e1STvrtko Ursulin 			}
2585d04a492dSShashank Sharma 
2586cc3f90f0SAnder Conselvan de Oliveira 			if (IS_GEN9_LP(dev_priv) && (iir & BXT_DE_PORT_GMBUS)) {
258791d14251STvrtko Ursulin 				gmbus_irq_handler(dev_priv);
25889e63743eSShashank Sharma 				found = true;
25899e63743eSShashank Sharma 			}
25909e63743eSShashank Sharma 
2591d04a492dSShashank Sharma 			if (!found)
259238cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Port interrupt\n");
25936d766f02SDaniel Vetter 		}
259438cc46d7SOscar Mateo 		else
259538cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
25966d766f02SDaniel Vetter 	}
25976d766f02SDaniel Vetter 
2598055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2599fd3a4024SDaniel Vetter 		u32 fault_errors;
2600abd58f01SBen Widawsky 
2601c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2602c42664ccSDaniel Vetter 			continue;
2603c42664ccSDaniel Vetter 
2604e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
2605e32192e1STvrtko Ursulin 		if (!iir) {
2606e32192e1STvrtko Ursulin 			DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
2607e32192e1STvrtko Ursulin 			continue;
2608e32192e1STvrtko Ursulin 		}
2609770de83dSDamien Lespiau 
2610e32192e1STvrtko Ursulin 		ret = IRQ_HANDLED;
2611e32192e1STvrtko Ursulin 		I915_WRITE(GEN8_DE_PIPE_IIR(pipe), iir);
2612e32192e1STvrtko Ursulin 
2613fd3a4024SDaniel Vetter 		if (iir & GEN8_PIPE_VBLANK)
2614fd3a4024SDaniel Vetter 			drm_handle_vblank(&dev_priv->drm, pipe);
2615abd58f01SBen Widawsky 
2616e32192e1STvrtko Ursulin 		if (iir & GEN8_PIPE_CDCLK_CRC_DONE)
261791d14251STvrtko Ursulin 			hsw_pipe_crc_irq_handler(dev_priv, pipe);
26180fbe7870SDaniel Vetter 
2619e32192e1STvrtko Ursulin 		if (iir & GEN8_PIPE_FIFO_UNDERRUN)
2620e32192e1STvrtko Ursulin 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
262138d83c96SDaniel Vetter 
2622e32192e1STvrtko Ursulin 		fault_errors = iir;
2623bca2bf2aSPandiyan, Dhinakaran 		if (INTEL_GEN(dev_priv) >= 9)
2624e32192e1STvrtko Ursulin 			fault_errors &= GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
2625770de83dSDamien Lespiau 		else
2626e32192e1STvrtko Ursulin 			fault_errors &= GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
2627770de83dSDamien Lespiau 
2628770de83dSDamien Lespiau 		if (fault_errors)
26291353ec38STvrtko Ursulin 			DRM_ERROR("Fault errors on pipe %c: 0x%08x\n",
263030100f2bSDaniel Vetter 				  pipe_name(pipe),
2631e32192e1STvrtko Ursulin 				  fault_errors);
2632abd58f01SBen Widawsky 	}
2633abd58f01SBen Widawsky 
263491d14251STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv) && !HAS_PCH_NOP(dev_priv) &&
2635266ea3d9SShashank Sharma 	    master_ctl & GEN8_DE_PCH_IRQ) {
263692d03a80SDaniel Vetter 		/*
263792d03a80SDaniel Vetter 		 * FIXME(BDW): Assume for now that the new interrupt handling
263892d03a80SDaniel Vetter 		 * scheme also closed the SDE interrupt handling race we've seen
263992d03a80SDaniel Vetter 		 * on older pch-split platforms. But this needs testing.
264092d03a80SDaniel Vetter 		 */
2641e32192e1STvrtko Ursulin 		iir = I915_READ(SDEIIR);
2642e32192e1STvrtko Ursulin 		if (iir) {
2643e32192e1STvrtko Ursulin 			I915_WRITE(SDEIIR, iir);
264492d03a80SDaniel Vetter 			ret = IRQ_HANDLED;
26456dbf30ceSVille Syrjälä 
26467b22b8c4SRodrigo Vivi 			if (HAS_PCH_SPT(dev_priv) || HAS_PCH_KBP(dev_priv) ||
26477b22b8c4SRodrigo Vivi 			    HAS_PCH_CNP(dev_priv))
264891d14251STvrtko Ursulin 				spt_irq_handler(dev_priv, iir);
26496dbf30ceSVille Syrjälä 			else
265091d14251STvrtko Ursulin 				cpt_irq_handler(dev_priv, iir);
26512dfb0b81SJani Nikula 		} else {
26522dfb0b81SJani Nikula 			/*
26532dfb0b81SJani Nikula 			 * Like on previous PCH there seems to be something
26542dfb0b81SJani Nikula 			 * fishy going on with forwarding PCH interrupts.
26552dfb0b81SJani Nikula 			 */
26562dfb0b81SJani Nikula 			DRM_DEBUG_DRIVER("The master control interrupt lied (SDE)!\n");
26572dfb0b81SJani Nikula 		}
265892d03a80SDaniel Vetter 	}
265992d03a80SDaniel Vetter 
2660f11a0f46STvrtko Ursulin 	return ret;
2661f11a0f46STvrtko Ursulin }
2662f11a0f46STvrtko Ursulin 
2663f11a0f46STvrtko Ursulin static irqreturn_t gen8_irq_handler(int irq, void *arg)
2664f11a0f46STvrtko Ursulin {
2665f0fd96f5SChris Wilson 	struct drm_i915_private *dev_priv = to_i915(arg);
2666f11a0f46STvrtko Ursulin 	u32 master_ctl;
2667f0fd96f5SChris Wilson 	u32 gt_iir[4];
2668f11a0f46STvrtko Ursulin 
2669f11a0f46STvrtko Ursulin 	if (!intel_irqs_enabled(dev_priv))
2670f11a0f46STvrtko Ursulin 		return IRQ_NONE;
2671f11a0f46STvrtko Ursulin 
2672f11a0f46STvrtko Ursulin 	master_ctl = I915_READ_FW(GEN8_MASTER_IRQ);
2673f11a0f46STvrtko Ursulin 	master_ctl &= ~GEN8_MASTER_IRQ_CONTROL;
2674f11a0f46STvrtko Ursulin 	if (!master_ctl)
2675f11a0f46STvrtko Ursulin 		return IRQ_NONE;
2676f11a0f46STvrtko Ursulin 
2677f11a0f46STvrtko Ursulin 	I915_WRITE_FW(GEN8_MASTER_IRQ, 0);
2678f11a0f46STvrtko Ursulin 
2679f11a0f46STvrtko Ursulin 	/* Find, clear, then process each source of interrupt */
268055ef72f2SChris Wilson 	gen8_gt_irq_ack(dev_priv, master_ctl, gt_iir);
2681f0fd96f5SChris Wilson 
2682f0fd96f5SChris Wilson 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
2683f0fd96f5SChris Wilson 	if (master_ctl & ~GEN8_GT_IRQS) {
2684f0fd96f5SChris Wilson 		disable_rpm_wakeref_asserts(dev_priv);
268555ef72f2SChris Wilson 		gen8_de_irq_handler(dev_priv, master_ctl);
2686f0fd96f5SChris Wilson 		enable_rpm_wakeref_asserts(dev_priv);
2687f0fd96f5SChris Wilson 	}
2688f11a0f46STvrtko Ursulin 
2689cb0d205eSChris Wilson 	I915_WRITE_FW(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
2690abd58f01SBen Widawsky 
2691f0fd96f5SChris Wilson 	gen8_gt_irq_handler(dev_priv, master_ctl, gt_iir);
26921f814dacSImre Deak 
269355ef72f2SChris Wilson 	return IRQ_HANDLED;
2694abd58f01SBen Widawsky }
2695abd58f01SBen Widawsky 
269636703e79SChris Wilson struct wedge_me {
269736703e79SChris Wilson 	struct delayed_work work;
269836703e79SChris Wilson 	struct drm_i915_private *i915;
269936703e79SChris Wilson 	const char *name;
270036703e79SChris Wilson };
270136703e79SChris Wilson 
270236703e79SChris Wilson static void wedge_me(struct work_struct *work)
270336703e79SChris Wilson {
270436703e79SChris Wilson 	struct wedge_me *w = container_of(work, typeof(*w), work.work);
270536703e79SChris Wilson 
270636703e79SChris Wilson 	dev_err(w->i915->drm.dev,
270736703e79SChris Wilson 		"%s timed out, cancelling all in-flight rendering.\n",
270836703e79SChris Wilson 		w->name);
270936703e79SChris Wilson 	i915_gem_set_wedged(w->i915);
271036703e79SChris Wilson }
271136703e79SChris Wilson 
271236703e79SChris Wilson static void __init_wedge(struct wedge_me *w,
271336703e79SChris Wilson 			 struct drm_i915_private *i915,
271436703e79SChris Wilson 			 long timeout,
271536703e79SChris Wilson 			 const char *name)
271636703e79SChris Wilson {
271736703e79SChris Wilson 	w->i915 = i915;
271836703e79SChris Wilson 	w->name = name;
271936703e79SChris Wilson 
272036703e79SChris Wilson 	INIT_DELAYED_WORK_ONSTACK(&w->work, wedge_me);
272136703e79SChris Wilson 	schedule_delayed_work(&w->work, timeout);
272236703e79SChris Wilson }
272336703e79SChris Wilson 
272436703e79SChris Wilson static void __fini_wedge(struct wedge_me *w)
272536703e79SChris Wilson {
272636703e79SChris Wilson 	cancel_delayed_work_sync(&w->work);
272736703e79SChris Wilson 	destroy_delayed_work_on_stack(&w->work);
272836703e79SChris Wilson 	w->i915 = NULL;
272936703e79SChris Wilson }
273036703e79SChris Wilson 
273136703e79SChris Wilson #define i915_wedge_on_timeout(W, DEV, TIMEOUT)				\
273236703e79SChris Wilson 	for (__init_wedge((W), (DEV), (TIMEOUT), __func__);		\
273336703e79SChris Wilson 	     (W)->i915;							\
273436703e79SChris Wilson 	     __fini_wedge((W)))
273536703e79SChris Wilson 
273651951ae7SMika Kuoppala static void
273751951ae7SMika Kuoppala gen11_gt_engine_irq_handler(struct drm_i915_private * const i915,
273851951ae7SMika Kuoppala 			    const unsigned int bank,
273951951ae7SMika Kuoppala 			    const unsigned int engine_n,
274051951ae7SMika Kuoppala 			    const u16 iir)
274151951ae7SMika Kuoppala {
274251951ae7SMika Kuoppala 	struct intel_engine_cs ** const engine = i915->engine;
274351951ae7SMika Kuoppala 
274451951ae7SMika Kuoppala 	switch (bank) {
274551951ae7SMika Kuoppala 	case 0:
274651951ae7SMika Kuoppala 		switch (engine_n) {
274751951ae7SMika Kuoppala 
274851951ae7SMika Kuoppala 		case GEN11_RCS0:
274951f6b0f9SChris Wilson 			return gen8_cs_irq_handler(engine[RCS], iir);
275051951ae7SMika Kuoppala 
275151951ae7SMika Kuoppala 		case GEN11_BCS:
275251f6b0f9SChris Wilson 			return gen8_cs_irq_handler(engine[BCS], iir);
275351951ae7SMika Kuoppala 		}
275451951ae7SMika Kuoppala 	case 1:
275551951ae7SMika Kuoppala 		switch (engine_n) {
275651951ae7SMika Kuoppala 
275751951ae7SMika Kuoppala 		case GEN11_VCS(0):
275851f6b0f9SChris Wilson 			return gen8_cs_irq_handler(engine[_VCS(0)], iir);
275951951ae7SMika Kuoppala 		case GEN11_VCS(1):
276051f6b0f9SChris Wilson 			return gen8_cs_irq_handler(engine[_VCS(1)], iir);
276151951ae7SMika Kuoppala 		case GEN11_VCS(2):
276251f6b0f9SChris Wilson 			return gen8_cs_irq_handler(engine[_VCS(2)], iir);
276351951ae7SMika Kuoppala 		case GEN11_VCS(3):
276451f6b0f9SChris Wilson 			return gen8_cs_irq_handler(engine[_VCS(3)], iir);
276551951ae7SMika Kuoppala 
276651951ae7SMika Kuoppala 		case GEN11_VECS(0):
276751f6b0f9SChris Wilson 			return gen8_cs_irq_handler(engine[_VECS(0)], iir);
276851951ae7SMika Kuoppala 		case GEN11_VECS(1):
276951f6b0f9SChris Wilson 			return gen8_cs_irq_handler(engine[_VECS(1)], iir);
277051951ae7SMika Kuoppala 		}
277151951ae7SMika Kuoppala 	}
277251951ae7SMika Kuoppala }
277351951ae7SMika Kuoppala 
277451951ae7SMika Kuoppala static u32
277551951ae7SMika Kuoppala gen11_gt_engine_intr(struct drm_i915_private * const i915,
277651951ae7SMika Kuoppala 		     const unsigned int bank, const unsigned int bit)
277751951ae7SMika Kuoppala {
277851951ae7SMika Kuoppala 	void __iomem * const regs = i915->regs;
277951951ae7SMika Kuoppala 	u32 timeout_ts;
278051951ae7SMika Kuoppala 	u32 ident;
278151951ae7SMika Kuoppala 
278251951ae7SMika Kuoppala 	raw_reg_write(regs, GEN11_IIR_REG_SELECTOR(bank), BIT(bit));
278351951ae7SMika Kuoppala 
278451951ae7SMika Kuoppala 	/*
278551951ae7SMika Kuoppala 	 * NB: Specs do not specify how long to spin wait,
278651951ae7SMika Kuoppala 	 * so we do ~100us as an educated guess.
278751951ae7SMika Kuoppala 	 */
278851951ae7SMika Kuoppala 	timeout_ts = (local_clock() >> 10) + 100;
278951951ae7SMika Kuoppala 	do {
279051951ae7SMika Kuoppala 		ident = raw_reg_read(regs, GEN11_INTR_IDENTITY_REG(bank));
279151951ae7SMika Kuoppala 	} while (!(ident & GEN11_INTR_DATA_VALID) &&
279251951ae7SMika Kuoppala 		 !time_after32(local_clock() >> 10, timeout_ts));
279351951ae7SMika Kuoppala 
279451951ae7SMika Kuoppala 	if (unlikely(!(ident & GEN11_INTR_DATA_VALID))) {
279551951ae7SMika Kuoppala 		DRM_ERROR("INTR_IDENTITY_REG%u:%u 0x%08x not valid!\n",
279651951ae7SMika Kuoppala 			  bank, bit, ident);
279751951ae7SMika Kuoppala 		return 0;
279851951ae7SMika Kuoppala 	}
279951951ae7SMika Kuoppala 
280051951ae7SMika Kuoppala 	raw_reg_write(regs, GEN11_INTR_IDENTITY_REG(bank),
280151951ae7SMika Kuoppala 		      GEN11_INTR_DATA_VALID);
280251951ae7SMika Kuoppala 
280351951ae7SMika Kuoppala 	return ident & GEN11_INTR_ENGINE_MASK;
280451951ae7SMika Kuoppala }
280551951ae7SMika Kuoppala 
280651951ae7SMika Kuoppala static void
280751951ae7SMika Kuoppala gen11_gt_irq_handler(struct drm_i915_private * const i915,
280851951ae7SMika Kuoppala 		     const u32 master_ctl)
280951951ae7SMika Kuoppala {
281051951ae7SMika Kuoppala 	void __iomem * const regs = i915->regs;
281151951ae7SMika Kuoppala 	unsigned int bank;
281251951ae7SMika Kuoppala 
281351951ae7SMika Kuoppala 	for (bank = 0; bank < 2; bank++) {
281451951ae7SMika Kuoppala 		unsigned long intr_dw;
281551951ae7SMika Kuoppala 		unsigned int bit;
281651951ae7SMika Kuoppala 
281751951ae7SMika Kuoppala 		if (!(master_ctl & GEN11_GT_DW_IRQ(bank)))
281851951ae7SMika Kuoppala 			continue;
281951951ae7SMika Kuoppala 
282051951ae7SMika Kuoppala 		intr_dw = raw_reg_read(regs, GEN11_GT_INTR_DW(bank));
282151951ae7SMika Kuoppala 
282251951ae7SMika Kuoppala 		if (unlikely(!intr_dw)) {
282351951ae7SMika Kuoppala 			DRM_ERROR("GT_INTR_DW%u blank!\n", bank);
282451951ae7SMika Kuoppala 			continue;
282551951ae7SMika Kuoppala 		}
282651951ae7SMika Kuoppala 
282751951ae7SMika Kuoppala 		for_each_set_bit(bit, &intr_dw, 32) {
282851951ae7SMika Kuoppala 			const u16 iir = gen11_gt_engine_intr(i915, bank, bit);
282951951ae7SMika Kuoppala 
283051951ae7SMika Kuoppala 			if (unlikely(!iir))
283151951ae7SMika Kuoppala 				continue;
283251951ae7SMika Kuoppala 
283351951ae7SMika Kuoppala 			gen11_gt_engine_irq_handler(i915, bank, bit, iir);
283451951ae7SMika Kuoppala 		}
283551951ae7SMika Kuoppala 
283651951ae7SMika Kuoppala 		/* Clear must be after shared has been served for engine */
283751951ae7SMika Kuoppala 		raw_reg_write(regs, GEN11_GT_INTR_DW(bank), intr_dw);
283851951ae7SMika Kuoppala 	}
283951951ae7SMika Kuoppala }
284051951ae7SMika Kuoppala 
284151951ae7SMika Kuoppala static irqreturn_t gen11_irq_handler(int irq, void *arg)
284251951ae7SMika Kuoppala {
284351951ae7SMika Kuoppala 	struct drm_i915_private * const i915 = to_i915(arg);
284451951ae7SMika Kuoppala 	void __iomem * const regs = i915->regs;
284551951ae7SMika Kuoppala 	u32 master_ctl;
284651951ae7SMika Kuoppala 
284751951ae7SMika Kuoppala 	if (!intel_irqs_enabled(i915))
284851951ae7SMika Kuoppala 		return IRQ_NONE;
284951951ae7SMika Kuoppala 
285051951ae7SMika Kuoppala 	master_ctl = raw_reg_read(regs, GEN11_GFX_MSTR_IRQ);
285151951ae7SMika Kuoppala 	master_ctl &= ~GEN11_MASTER_IRQ;
285251951ae7SMika Kuoppala 	if (!master_ctl)
285351951ae7SMika Kuoppala 		return IRQ_NONE;
285451951ae7SMika Kuoppala 
285551951ae7SMika Kuoppala 	/* Disable interrupts. */
285651951ae7SMika Kuoppala 	raw_reg_write(regs, GEN11_GFX_MSTR_IRQ, 0);
285751951ae7SMika Kuoppala 
285851951ae7SMika Kuoppala 	/* Find, clear, then process each source of interrupt. */
285951951ae7SMika Kuoppala 	gen11_gt_irq_handler(i915, master_ctl);
286051951ae7SMika Kuoppala 
286151951ae7SMika Kuoppala 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
286251951ae7SMika Kuoppala 	if (master_ctl & GEN11_DISPLAY_IRQ) {
286351951ae7SMika Kuoppala 		const u32 disp_ctl = raw_reg_read(regs, GEN11_DISPLAY_INT_CTL);
286451951ae7SMika Kuoppala 
286551951ae7SMika Kuoppala 		disable_rpm_wakeref_asserts(i915);
286651951ae7SMika Kuoppala 		/*
286751951ae7SMika Kuoppala 		 * GEN11_DISPLAY_INT_CTL has same format as GEN8_MASTER_IRQ
286851951ae7SMika Kuoppala 		 * for the display related bits.
286951951ae7SMika Kuoppala 		 */
287051951ae7SMika Kuoppala 		gen8_de_irq_handler(i915, disp_ctl);
287151951ae7SMika Kuoppala 		enable_rpm_wakeref_asserts(i915);
287251951ae7SMika Kuoppala 	}
287351951ae7SMika Kuoppala 
287451951ae7SMika Kuoppala 	/* Acknowledge and enable interrupts. */
287551951ae7SMika Kuoppala 	raw_reg_write(regs, GEN11_GFX_MSTR_IRQ, GEN11_MASTER_IRQ | master_ctl);
287651951ae7SMika Kuoppala 
287751951ae7SMika Kuoppala 	return IRQ_HANDLED;
287851951ae7SMika Kuoppala }
287951951ae7SMika Kuoppala 
2880*ce800754SChris Wilson static void i915_reset_device(struct drm_i915_private *dev_priv,
2881*ce800754SChris Wilson 			      const char *msg)
28828a905236SJesse Barnes {
2883*ce800754SChris Wilson 	struct i915_gpu_error *error = &dev_priv->gpu_error;
288491c8a326SChris Wilson 	struct kobject *kobj = &dev_priv->drm.primary->kdev->kobj;
2885cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
2886cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
2887cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
288836703e79SChris Wilson 	struct wedge_me w;
28898a905236SJesse Barnes 
2890c033666aSChris Wilson 	kobject_uevent_env(kobj, KOBJ_CHANGE, error_event);
28918a905236SJesse Barnes 
289244d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("resetting chip\n");
2893c033666aSChris Wilson 	kobject_uevent_env(kobj, KOBJ_CHANGE, reset_event);
28941f83fee0SDaniel Vetter 
289536703e79SChris Wilson 	/* Use a watchdog to ensure that our reset completes */
289636703e79SChris Wilson 	i915_wedge_on_timeout(&w, dev_priv, 5*HZ) {
2897c033666aSChris Wilson 		intel_prepare_reset(dev_priv);
28987514747dSVille Syrjälä 
2899*ce800754SChris Wilson 		error->reason = msg;
2900*ce800754SChris Wilson 
290136703e79SChris Wilson 		/* Signal that locked waiters should reset the GPU */
2902*ce800754SChris Wilson 		set_bit(I915_RESET_HANDOFF, &error->flags);
2903*ce800754SChris Wilson 		wake_up_all(&error->wait_queue);
29048c185ecaSChris Wilson 
290536703e79SChris Wilson 		/* Wait for anyone holding the lock to wakeup, without
290636703e79SChris Wilson 		 * blocking indefinitely on struct_mutex.
290717e1df07SDaniel Vetter 		 */
290836703e79SChris Wilson 		do {
2909780f262aSChris Wilson 			if (mutex_trylock(&dev_priv->drm.struct_mutex)) {
2910*ce800754SChris Wilson 				i915_reset(dev_priv);
2911221fe799SChris Wilson 				mutex_unlock(&dev_priv->drm.struct_mutex);
2912780f262aSChris Wilson 			}
2913*ce800754SChris Wilson 		} while (wait_on_bit_timeout(&error->flags,
29148c185ecaSChris Wilson 					     I915_RESET_HANDOFF,
2915780f262aSChris Wilson 					     TASK_UNINTERRUPTIBLE,
291636703e79SChris Wilson 					     1));
2917f69061beSDaniel Vetter 
2918*ce800754SChris Wilson 		error->reason = NULL;
2919*ce800754SChris Wilson 
2920c033666aSChris Wilson 		intel_finish_reset(dev_priv);
292136703e79SChris Wilson 	}
2922f454c694SImre Deak 
2923*ce800754SChris Wilson 	if (!test_bit(I915_WEDGED, &error->flags))
2924*ce800754SChris Wilson 		kobject_uevent_env(kobj, KOBJ_CHANGE, reset_done_event);
2925f316a42cSBen Gamari }
29268a905236SJesse Barnes 
2927eaa14c24SChris Wilson static void i915_clear_error_registers(struct drm_i915_private *dev_priv)
2928c0e09200SDave Airlie {
2929eaa14c24SChris Wilson 	u32 eir;
293063eeaf38SJesse Barnes 
2931eaa14c24SChris Wilson 	if (!IS_GEN2(dev_priv))
2932eaa14c24SChris Wilson 		I915_WRITE(PGTBL_ER, I915_READ(PGTBL_ER));
293363eeaf38SJesse Barnes 
2934eaa14c24SChris Wilson 	if (INTEL_GEN(dev_priv) < 4)
2935eaa14c24SChris Wilson 		I915_WRITE(IPEIR, I915_READ(IPEIR));
2936eaa14c24SChris Wilson 	else
2937eaa14c24SChris Wilson 		I915_WRITE(IPEIR_I965, I915_READ(IPEIR_I965));
29388a905236SJesse Barnes 
2939eaa14c24SChris Wilson 	I915_WRITE(EIR, I915_READ(EIR));
294063eeaf38SJesse Barnes 	eir = I915_READ(EIR);
294163eeaf38SJesse Barnes 	if (eir) {
294263eeaf38SJesse Barnes 		/*
294363eeaf38SJesse Barnes 		 * some errors might have become stuck,
294463eeaf38SJesse Barnes 		 * mask them.
294563eeaf38SJesse Barnes 		 */
2946eaa14c24SChris Wilson 		DRM_DEBUG_DRIVER("EIR stuck: 0x%08x, masking\n", eir);
294763eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
294863eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
294963eeaf38SJesse Barnes 	}
295035aed2e6SChris Wilson }
295135aed2e6SChris Wilson 
295235aed2e6SChris Wilson /**
2953b8d24a06SMika Kuoppala  * i915_handle_error - handle a gpu error
295414bb2c11STvrtko Ursulin  * @dev_priv: i915 device private
295514b730fcSarun.siluvery@linux.intel.com  * @engine_mask: mask representing engines that are hung
2956*ce800754SChris Wilson  * @flags: control flags
295787c390b6SMichel Thierry  * @fmt: Error message format string
295887c390b6SMichel Thierry  *
2959aafd8581SJavier Martinez Canillas  * Do some basic checking of register state at error time and
296035aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
296135aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
296235aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
296335aed2e6SChris Wilson  * of a ring dump etc.).
296435aed2e6SChris Wilson  */
2965c033666aSChris Wilson void i915_handle_error(struct drm_i915_private *dev_priv,
2966c033666aSChris Wilson 		       u32 engine_mask,
2967*ce800754SChris Wilson 		       unsigned long flags,
296858174462SMika Kuoppala 		       const char *fmt, ...)
296935aed2e6SChris Wilson {
2970142bc7d9SMichel Thierry 	struct intel_engine_cs *engine;
2971142bc7d9SMichel Thierry 	unsigned int tmp;
297258174462SMika Kuoppala 	char error_msg[80];
2973*ce800754SChris Wilson 	char *msg = NULL;
2974*ce800754SChris Wilson 
2975*ce800754SChris Wilson 	if (fmt) {
2976*ce800754SChris Wilson 		va_list args;
297735aed2e6SChris Wilson 
297858174462SMika Kuoppala 		va_start(args, fmt);
297958174462SMika Kuoppala 		vscnprintf(error_msg, sizeof(error_msg), fmt, args);
298058174462SMika Kuoppala 		va_end(args);
298158174462SMika Kuoppala 
2982*ce800754SChris Wilson 		msg = error_msg;
2983*ce800754SChris Wilson 	}
2984*ce800754SChris Wilson 
29851604a86dSChris Wilson 	/*
29861604a86dSChris Wilson 	 * In most cases it's guaranteed that we get here with an RPM
29871604a86dSChris Wilson 	 * reference held, for example because there is a pending GPU
29881604a86dSChris Wilson 	 * request that won't finish until the reset is done. This
29891604a86dSChris Wilson 	 * isn't the case at least when we get here by doing a
29901604a86dSChris Wilson 	 * simulated reset via debugfs, so get an RPM reference.
29911604a86dSChris Wilson 	 */
29921604a86dSChris Wilson 	intel_runtime_pm_get(dev_priv);
29931604a86dSChris Wilson 
2994873d66fbSChris Wilson 	engine_mask &= INTEL_INFO(dev_priv)->ring_mask;
2995*ce800754SChris Wilson 
2996*ce800754SChris Wilson 	if (flags & I915_ERROR_CAPTURE) {
2997*ce800754SChris Wilson 		i915_capture_error_state(dev_priv, engine_mask, msg);
2998eaa14c24SChris Wilson 		i915_clear_error_registers(dev_priv);
2999*ce800754SChris Wilson 	}
30008a905236SJesse Barnes 
3001142bc7d9SMichel Thierry 	/*
3002142bc7d9SMichel Thierry 	 * Try engine reset when available. We fall back to full reset if
3003142bc7d9SMichel Thierry 	 * single reset fails.
3004142bc7d9SMichel Thierry 	 */
3005142bc7d9SMichel Thierry 	if (intel_has_reset_engine(dev_priv)) {
3006142bc7d9SMichel Thierry 		for_each_engine_masked(engine, dev_priv, engine_mask, tmp) {
30079db529aaSDaniel Vetter 			BUILD_BUG_ON(I915_RESET_MODESET >= I915_RESET_ENGINE);
3008142bc7d9SMichel Thierry 			if (test_and_set_bit(I915_RESET_ENGINE + engine->id,
3009142bc7d9SMichel Thierry 					     &dev_priv->gpu_error.flags))
3010142bc7d9SMichel Thierry 				continue;
3011142bc7d9SMichel Thierry 
3012*ce800754SChris Wilson 			if (i915_reset_engine(engine, msg) == 0)
3013142bc7d9SMichel Thierry 				engine_mask &= ~intel_engine_flag(engine);
3014142bc7d9SMichel Thierry 
3015142bc7d9SMichel Thierry 			clear_bit(I915_RESET_ENGINE + engine->id,
3016142bc7d9SMichel Thierry 				  &dev_priv->gpu_error.flags);
3017142bc7d9SMichel Thierry 			wake_up_bit(&dev_priv->gpu_error.flags,
3018142bc7d9SMichel Thierry 				    I915_RESET_ENGINE + engine->id);
3019142bc7d9SMichel Thierry 		}
3020142bc7d9SMichel Thierry 	}
3021142bc7d9SMichel Thierry 
30228af29b0cSChris Wilson 	if (!engine_mask)
30231604a86dSChris Wilson 		goto out;
30248af29b0cSChris Wilson 
3025142bc7d9SMichel Thierry 	/* Full reset needs the mutex, stop any other user trying to do so. */
3026d5367307SChris Wilson 	if (test_and_set_bit(I915_RESET_BACKOFF, &dev_priv->gpu_error.flags)) {
3027d5367307SChris Wilson 		wait_event(dev_priv->gpu_error.reset_queue,
3028d5367307SChris Wilson 			   !test_bit(I915_RESET_BACKOFF,
3029d5367307SChris Wilson 				     &dev_priv->gpu_error.flags));
30301604a86dSChris Wilson 		goto out;
3031d5367307SChris Wilson 	}
3032ba1234d1SBen Gamari 
3033142bc7d9SMichel Thierry 	/* Prevent any other reset-engine attempt. */
3034142bc7d9SMichel Thierry 	for_each_engine(engine, dev_priv, tmp) {
3035142bc7d9SMichel Thierry 		while (test_and_set_bit(I915_RESET_ENGINE + engine->id,
3036142bc7d9SMichel Thierry 					&dev_priv->gpu_error.flags))
3037142bc7d9SMichel Thierry 			wait_on_bit(&dev_priv->gpu_error.flags,
3038142bc7d9SMichel Thierry 				    I915_RESET_ENGINE + engine->id,
3039142bc7d9SMichel Thierry 				    TASK_UNINTERRUPTIBLE);
3040142bc7d9SMichel Thierry 	}
3041142bc7d9SMichel Thierry 
3042*ce800754SChris Wilson 	i915_reset_device(dev_priv, msg);
3043d5367307SChris Wilson 
3044142bc7d9SMichel Thierry 	for_each_engine(engine, dev_priv, tmp) {
3045142bc7d9SMichel Thierry 		clear_bit(I915_RESET_ENGINE + engine->id,
3046142bc7d9SMichel Thierry 			  &dev_priv->gpu_error.flags);
3047142bc7d9SMichel Thierry 	}
3048142bc7d9SMichel Thierry 
3049d5367307SChris Wilson 	clear_bit(I915_RESET_BACKOFF, &dev_priv->gpu_error.flags);
3050d5367307SChris Wilson 	wake_up_all(&dev_priv->gpu_error.reset_queue);
30511604a86dSChris Wilson 
30521604a86dSChris Wilson out:
30531604a86dSChris Wilson 	intel_runtime_pm_put(dev_priv);
30548a905236SJesse Barnes }
30558a905236SJesse Barnes 
305642f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
305742f52ef8SKeith Packard  * we use as a pipe index
305842f52ef8SKeith Packard  */
305986e83e35SChris Wilson static int i8xx_enable_vblank(struct drm_device *dev, unsigned int pipe)
30600a3e67a4SJesse Barnes {
3061fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3062e9d21d7fSKeith Packard 	unsigned long irqflags;
306371e0ffa5SJesse Barnes 
30641ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
306586e83e35SChris Wilson 	i915_enable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_STATUS);
306686e83e35SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
306786e83e35SChris Wilson 
306886e83e35SChris Wilson 	return 0;
306986e83e35SChris Wilson }
307086e83e35SChris Wilson 
307186e83e35SChris Wilson static int i965_enable_vblank(struct drm_device *dev, unsigned int pipe)
307286e83e35SChris Wilson {
307386e83e35SChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
307486e83e35SChris Wilson 	unsigned long irqflags;
307586e83e35SChris Wilson 
307686e83e35SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
30777c463586SKeith Packard 	i915_enable_pipestat(dev_priv, pipe,
3078755e9019SImre Deak 			     PIPE_START_VBLANK_INTERRUPT_STATUS);
30791ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
30808692d00eSChris Wilson 
30810a3e67a4SJesse Barnes 	return 0;
30820a3e67a4SJesse Barnes }
30830a3e67a4SJesse Barnes 
308488e72717SThierry Reding static int ironlake_enable_vblank(struct drm_device *dev, unsigned int pipe)
3085f796cf8fSJesse Barnes {
3086fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3087f796cf8fSJesse Barnes 	unsigned long irqflags;
308855b8f2a7STvrtko Ursulin 	uint32_t bit = INTEL_GEN(dev_priv) >= 7 ?
308986e83e35SChris Wilson 		DE_PIPE_VBLANK_IVB(pipe) : DE_PIPE_VBLANK(pipe);
3090f796cf8fSJesse Barnes 
3091f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3092fbdedaeaSVille Syrjälä 	ilk_enable_display_irq(dev_priv, bit);
3093b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3094b1f14ad0SJesse Barnes 
30952e8bf223SDhinakaran Pandiyan 	/* Even though there is no DMC, frame counter can get stuck when
30962e8bf223SDhinakaran Pandiyan 	 * PSR is active as no frames are generated.
30972e8bf223SDhinakaran Pandiyan 	 */
30982e8bf223SDhinakaran Pandiyan 	if (HAS_PSR(dev_priv))
30992e8bf223SDhinakaran Pandiyan 		drm_vblank_restore(dev, pipe);
31002e8bf223SDhinakaran Pandiyan 
3101b1f14ad0SJesse Barnes 	return 0;
3102b1f14ad0SJesse Barnes }
3103b1f14ad0SJesse Barnes 
310488e72717SThierry Reding static int gen8_enable_vblank(struct drm_device *dev, unsigned int pipe)
3105abd58f01SBen Widawsky {
3106fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3107abd58f01SBen Widawsky 	unsigned long irqflags;
3108abd58f01SBen Widawsky 
3109abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3110013d3752SVille Syrjälä 	bdw_enable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
3111abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3112013d3752SVille Syrjälä 
31132e8bf223SDhinakaran Pandiyan 	/* Even if there is no DMC, frame counter can get stuck when
31142e8bf223SDhinakaran Pandiyan 	 * PSR is active as no frames are generated, so check only for PSR.
31152e8bf223SDhinakaran Pandiyan 	 */
31162e8bf223SDhinakaran Pandiyan 	if (HAS_PSR(dev_priv))
31172e8bf223SDhinakaran Pandiyan 		drm_vblank_restore(dev, pipe);
31182e8bf223SDhinakaran Pandiyan 
3119abd58f01SBen Widawsky 	return 0;
3120abd58f01SBen Widawsky }
3121abd58f01SBen Widawsky 
312242f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
312342f52ef8SKeith Packard  * we use as a pipe index
312442f52ef8SKeith Packard  */
312586e83e35SChris Wilson static void i8xx_disable_vblank(struct drm_device *dev, unsigned int pipe)
312686e83e35SChris Wilson {
312786e83e35SChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
312886e83e35SChris Wilson 	unsigned long irqflags;
312986e83e35SChris Wilson 
313086e83e35SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
313186e83e35SChris Wilson 	i915_disable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_STATUS);
313286e83e35SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
313386e83e35SChris Wilson }
313486e83e35SChris Wilson 
313586e83e35SChris Wilson static void i965_disable_vblank(struct drm_device *dev, unsigned int pipe)
31360a3e67a4SJesse Barnes {
3137fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3138e9d21d7fSKeith Packard 	unsigned long irqflags;
31390a3e67a4SJesse Barnes 
31401ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
31417c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
3142755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
31431ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
31440a3e67a4SJesse Barnes }
31450a3e67a4SJesse Barnes 
314688e72717SThierry Reding static void ironlake_disable_vblank(struct drm_device *dev, unsigned int pipe)
3147f796cf8fSJesse Barnes {
3148fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3149f796cf8fSJesse Barnes 	unsigned long irqflags;
315055b8f2a7STvrtko Ursulin 	uint32_t bit = INTEL_GEN(dev_priv) >= 7 ?
315186e83e35SChris Wilson 		DE_PIPE_VBLANK_IVB(pipe) : DE_PIPE_VBLANK(pipe);
3152f796cf8fSJesse Barnes 
3153f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3154fbdedaeaSVille Syrjälä 	ilk_disable_display_irq(dev_priv, bit);
3155b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3156b1f14ad0SJesse Barnes }
3157b1f14ad0SJesse Barnes 
315888e72717SThierry Reding static void gen8_disable_vblank(struct drm_device *dev, unsigned int pipe)
3159abd58f01SBen Widawsky {
3160fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3161abd58f01SBen Widawsky 	unsigned long irqflags;
3162abd58f01SBen Widawsky 
3163abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3164013d3752SVille Syrjälä 	bdw_disable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
3165abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3166abd58f01SBen Widawsky }
3167abd58f01SBen Widawsky 
3168b243f530STvrtko Ursulin static void ibx_irq_reset(struct drm_i915_private *dev_priv)
316991738a95SPaulo Zanoni {
31706e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
317191738a95SPaulo Zanoni 		return;
317291738a95SPaulo Zanoni 
31733488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(SDE);
3174105b122eSPaulo Zanoni 
31756e266956STvrtko Ursulin 	if (HAS_PCH_CPT(dev_priv) || HAS_PCH_LPT(dev_priv))
3176105b122eSPaulo Zanoni 		I915_WRITE(SERR_INT, 0xffffffff);
3177622364b6SPaulo Zanoni }
3178105b122eSPaulo Zanoni 
317991738a95SPaulo Zanoni /*
3180622364b6SPaulo Zanoni  * SDEIER is also touched by the interrupt handler to work around missed PCH
3181622364b6SPaulo Zanoni  * interrupts. Hence we can't update it after the interrupt handler is enabled -
3182622364b6SPaulo Zanoni  * instead we unconditionally enable all PCH interrupt sources here, but then
3183622364b6SPaulo Zanoni  * only unmask them as needed with SDEIMR.
3184622364b6SPaulo Zanoni  *
3185622364b6SPaulo Zanoni  * This function needs to be called before interrupts are enabled.
318691738a95SPaulo Zanoni  */
3187622364b6SPaulo Zanoni static void ibx_irq_pre_postinstall(struct drm_device *dev)
3188622364b6SPaulo Zanoni {
3189fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3190622364b6SPaulo Zanoni 
31916e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
3192622364b6SPaulo Zanoni 		return;
3193622364b6SPaulo Zanoni 
3194622364b6SPaulo Zanoni 	WARN_ON(I915_READ(SDEIER) != 0);
319591738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
319691738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
319791738a95SPaulo Zanoni }
319891738a95SPaulo Zanoni 
3199b243f530STvrtko Ursulin static void gen5_gt_irq_reset(struct drm_i915_private *dev_priv)
3200d18ea1b5SDaniel Vetter {
32013488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(GT);
3202b243f530STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 6)
32033488d4ebSVille Syrjälä 		GEN3_IRQ_RESET(GEN6_PM);
3204d18ea1b5SDaniel Vetter }
3205d18ea1b5SDaniel Vetter 
320670591a41SVille Syrjälä static void vlv_display_irq_reset(struct drm_i915_private *dev_priv)
320770591a41SVille Syrjälä {
320871b8b41dSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
320971b8b41dSVille Syrjälä 		I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
321071b8b41dSVille Syrjälä 	else
321171b8b41dSVille Syrjälä 		I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
321271b8b41dSVille Syrjälä 
3213ad22d106SVille Syrjälä 	i915_hotplug_interrupt_update_locked(dev_priv, 0xffffffff, 0);
321470591a41SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
321570591a41SVille Syrjälä 
321644d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
321770591a41SVille Syrjälä 
32183488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(VLV_);
32198bd099a7SChris Wilson 	dev_priv->irq_mask = ~0u;
322070591a41SVille Syrjälä }
322170591a41SVille Syrjälä 
32228bb61306SVille Syrjälä static void vlv_display_irq_postinstall(struct drm_i915_private *dev_priv)
32238bb61306SVille Syrjälä {
32248bb61306SVille Syrjälä 	u32 pipestat_mask;
32259ab981f2SVille Syrjälä 	u32 enable_mask;
32268bb61306SVille Syrjälä 	enum pipe pipe;
32278bb61306SVille Syrjälä 
3228842ebf7aSVille Syrjälä 	pipestat_mask = PIPE_CRC_DONE_INTERRUPT_STATUS;
32298bb61306SVille Syrjälä 
32308bb61306SVille Syrjälä 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
32318bb61306SVille Syrjälä 	for_each_pipe(dev_priv, pipe)
32328bb61306SVille Syrjälä 		i915_enable_pipestat(dev_priv, pipe, pipestat_mask);
32338bb61306SVille Syrjälä 
32349ab981f2SVille Syrjälä 	enable_mask = I915_DISPLAY_PORT_INTERRUPT |
32358bb61306SVille Syrjälä 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3236ebf5f921SVille Syrjälä 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3237ebf5f921SVille Syrjälä 		I915_LPE_PIPE_A_INTERRUPT |
3238ebf5f921SVille Syrjälä 		I915_LPE_PIPE_B_INTERRUPT;
3239ebf5f921SVille Syrjälä 
32408bb61306SVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
3241ebf5f921SVille Syrjälä 		enable_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT |
3242ebf5f921SVille Syrjälä 			I915_LPE_PIPE_C_INTERRUPT;
32436b7eafc1SVille Syrjälä 
32448bd099a7SChris Wilson 	WARN_ON(dev_priv->irq_mask != ~0u);
32456b7eafc1SVille Syrjälä 
32469ab981f2SVille Syrjälä 	dev_priv->irq_mask = ~enable_mask;
32478bb61306SVille Syrjälä 
32483488d4ebSVille Syrjälä 	GEN3_IRQ_INIT(VLV_, dev_priv->irq_mask, enable_mask);
32498bb61306SVille Syrjälä }
32508bb61306SVille Syrjälä 
32518bb61306SVille Syrjälä /* drm_dma.h hooks
32528bb61306SVille Syrjälä */
32538bb61306SVille Syrjälä static void ironlake_irq_reset(struct drm_device *dev)
32548bb61306SVille Syrjälä {
3255fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
32568bb61306SVille Syrjälä 
3257d420a50cSVille Syrjälä 	if (IS_GEN5(dev_priv))
32588bb61306SVille Syrjälä 		I915_WRITE(HWSTAM, 0xffffffff);
32598bb61306SVille Syrjälä 
32603488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(DE);
32615db94019STvrtko Ursulin 	if (IS_GEN7(dev_priv))
32628bb61306SVille Syrjälä 		I915_WRITE(GEN7_ERR_INT, 0xffffffff);
32638bb61306SVille Syrjälä 
3264b243f530STvrtko Ursulin 	gen5_gt_irq_reset(dev_priv);
32658bb61306SVille Syrjälä 
3266b243f530STvrtko Ursulin 	ibx_irq_reset(dev_priv);
32678bb61306SVille Syrjälä }
32688bb61306SVille Syrjälä 
32696bcdb1c8SVille Syrjälä static void valleyview_irq_reset(struct drm_device *dev)
32707e231dbeSJesse Barnes {
3271fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
32727e231dbeSJesse Barnes 
327334c7b8a7SVille Syrjälä 	I915_WRITE(VLV_MASTER_IER, 0);
327434c7b8a7SVille Syrjälä 	POSTING_READ(VLV_MASTER_IER);
327534c7b8a7SVille Syrjälä 
3276b243f530STvrtko Ursulin 	gen5_gt_irq_reset(dev_priv);
32777e231dbeSJesse Barnes 
3278ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
32799918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
328070591a41SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3281ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
32827e231dbeSJesse Barnes }
32837e231dbeSJesse Barnes 
3284d6e3cca3SDaniel Vetter static void gen8_gt_irq_reset(struct drm_i915_private *dev_priv)
3285d6e3cca3SDaniel Vetter {
3286d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 0);
3287d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 1);
3288d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 2);
3289d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 3);
3290d6e3cca3SDaniel Vetter }
3291d6e3cca3SDaniel Vetter 
3292823f6b38SPaulo Zanoni static void gen8_irq_reset(struct drm_device *dev)
3293abd58f01SBen Widawsky {
3294fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3295abd58f01SBen Widawsky 	int pipe;
3296abd58f01SBen Widawsky 
3297abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
3298abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3299abd58f01SBen Widawsky 
3300d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
3301abd58f01SBen Widawsky 
3302055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3303f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3304813bde43SPaulo Zanoni 						   POWER_DOMAIN_PIPE(pipe)))
3305f86f3fb0SPaulo Zanoni 			GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3306abd58f01SBen Widawsky 
33073488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(GEN8_DE_PORT_);
33083488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(GEN8_DE_MISC_);
33093488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(GEN8_PCU_);
3310abd58f01SBen Widawsky 
33116e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
3312b243f530STvrtko Ursulin 		ibx_irq_reset(dev_priv);
3313abd58f01SBen Widawsky }
3314abd58f01SBen Widawsky 
331551951ae7SMika Kuoppala static void gen11_gt_irq_reset(struct drm_i915_private *dev_priv)
331651951ae7SMika Kuoppala {
331751951ae7SMika Kuoppala 	/* Disable RCS, BCS, VCS and VECS class engines. */
331851951ae7SMika Kuoppala 	I915_WRITE(GEN11_RENDER_COPY_INTR_ENABLE, 0);
331951951ae7SMika Kuoppala 	I915_WRITE(GEN11_VCS_VECS_INTR_ENABLE,	  0);
332051951ae7SMika Kuoppala 
332151951ae7SMika Kuoppala 	/* Restore masks irqs on RCS, BCS, VCS and VECS engines. */
332251951ae7SMika Kuoppala 	I915_WRITE(GEN11_RCS0_RSVD_INTR_MASK,	~0);
332351951ae7SMika Kuoppala 	I915_WRITE(GEN11_BCS_RSVD_INTR_MASK,	~0);
332451951ae7SMika Kuoppala 	I915_WRITE(GEN11_VCS0_VCS1_INTR_MASK,	~0);
332551951ae7SMika Kuoppala 	I915_WRITE(GEN11_VCS2_VCS3_INTR_MASK,	~0);
332651951ae7SMika Kuoppala 	I915_WRITE(GEN11_VECS0_VECS1_INTR_MASK,	~0);
332751951ae7SMika Kuoppala }
332851951ae7SMika Kuoppala 
332951951ae7SMika Kuoppala static void gen11_irq_reset(struct drm_device *dev)
333051951ae7SMika Kuoppala {
333151951ae7SMika Kuoppala 	struct drm_i915_private *dev_priv = dev->dev_private;
333251951ae7SMika Kuoppala 	int pipe;
333351951ae7SMika Kuoppala 
333451951ae7SMika Kuoppala 	I915_WRITE(GEN11_GFX_MSTR_IRQ, 0);
333551951ae7SMika Kuoppala 	POSTING_READ(GEN11_GFX_MSTR_IRQ);
333651951ae7SMika Kuoppala 
333751951ae7SMika Kuoppala 	gen11_gt_irq_reset(dev_priv);
333851951ae7SMika Kuoppala 
333951951ae7SMika Kuoppala 	I915_WRITE(GEN11_DISPLAY_INT_CTL, 0);
334051951ae7SMika Kuoppala 
334151951ae7SMika Kuoppala 	for_each_pipe(dev_priv, pipe)
334251951ae7SMika Kuoppala 		if (intel_display_power_is_enabled(dev_priv,
334351951ae7SMika Kuoppala 						   POWER_DOMAIN_PIPE(pipe)))
334451951ae7SMika Kuoppala 			GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
334551951ae7SMika Kuoppala 
334651951ae7SMika Kuoppala 	GEN3_IRQ_RESET(GEN8_DE_PORT_);
334751951ae7SMika Kuoppala 	GEN3_IRQ_RESET(GEN8_DE_MISC_);
334851951ae7SMika Kuoppala 	GEN3_IRQ_RESET(GEN8_PCU_);
334951951ae7SMika Kuoppala }
335051951ae7SMika Kuoppala 
33514c6c03beSDamien Lespiau void gen8_irq_power_well_post_enable(struct drm_i915_private *dev_priv,
3352001bd2cbSImre Deak 				     u8 pipe_mask)
3353d49bdb0eSPaulo Zanoni {
33541180e206SPaulo Zanoni 	uint32_t extra_ier = GEN8_PIPE_VBLANK | GEN8_PIPE_FIFO_UNDERRUN;
33556831f3e3SVille Syrjälä 	enum pipe pipe;
3356d49bdb0eSPaulo Zanoni 
335713321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
33589dfe2e3aSImre Deak 
33599dfe2e3aSImre Deak 	if (!intel_irqs_enabled(dev_priv)) {
33609dfe2e3aSImre Deak 		spin_unlock_irq(&dev_priv->irq_lock);
33619dfe2e3aSImre Deak 		return;
33629dfe2e3aSImre Deak 	}
33639dfe2e3aSImre Deak 
33646831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
33656831f3e3SVille Syrjälä 		GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
33666831f3e3SVille Syrjälä 				  dev_priv->de_irq_mask[pipe],
33676831f3e3SVille Syrjälä 				  ~dev_priv->de_irq_mask[pipe] | extra_ier);
33689dfe2e3aSImre Deak 
336913321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3370d49bdb0eSPaulo Zanoni }
3371d49bdb0eSPaulo Zanoni 
3372aae8ba84SVille Syrjälä void gen8_irq_power_well_pre_disable(struct drm_i915_private *dev_priv,
3373001bd2cbSImre Deak 				     u8 pipe_mask)
3374aae8ba84SVille Syrjälä {
33756831f3e3SVille Syrjälä 	enum pipe pipe;
33766831f3e3SVille Syrjälä 
3377aae8ba84SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
33789dfe2e3aSImre Deak 
33799dfe2e3aSImre Deak 	if (!intel_irqs_enabled(dev_priv)) {
33809dfe2e3aSImre Deak 		spin_unlock_irq(&dev_priv->irq_lock);
33819dfe2e3aSImre Deak 		return;
33829dfe2e3aSImre Deak 	}
33839dfe2e3aSImre Deak 
33846831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
33856831f3e3SVille Syrjälä 		GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
33869dfe2e3aSImre Deak 
3387aae8ba84SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3388aae8ba84SVille Syrjälä 
3389aae8ba84SVille Syrjälä 	/* make sure we're done processing display irqs */
339091c8a326SChris Wilson 	synchronize_irq(dev_priv->drm.irq);
3391aae8ba84SVille Syrjälä }
3392aae8ba84SVille Syrjälä 
33936bcdb1c8SVille Syrjälä static void cherryview_irq_reset(struct drm_device *dev)
339443f328d7SVille Syrjälä {
3395fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
339643f328d7SVille Syrjälä 
339743f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
339843f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
339943f328d7SVille Syrjälä 
3400d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
340143f328d7SVille Syrjälä 
34023488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(GEN8_PCU_);
340343f328d7SVille Syrjälä 
3404ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
34059918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
340670591a41SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3407ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
340843f328d7SVille Syrjälä }
340943f328d7SVille Syrjälä 
341091d14251STvrtko Ursulin static u32 intel_hpd_enabled_irqs(struct drm_i915_private *dev_priv,
341187a02106SVille Syrjälä 				  const u32 hpd[HPD_NUM_PINS])
341287a02106SVille Syrjälä {
341387a02106SVille Syrjälä 	struct intel_encoder *encoder;
341487a02106SVille Syrjälä 	u32 enabled_irqs = 0;
341587a02106SVille Syrjälä 
341691c8a326SChris Wilson 	for_each_intel_encoder(&dev_priv->drm, encoder)
341787a02106SVille Syrjälä 		if (dev_priv->hotplug.stats[encoder->hpd_pin].state == HPD_ENABLED)
341887a02106SVille Syrjälä 			enabled_irqs |= hpd[encoder->hpd_pin];
341987a02106SVille Syrjälä 
342087a02106SVille Syrjälä 	return enabled_irqs;
342187a02106SVille Syrjälä }
342287a02106SVille Syrjälä 
34231a56b1a2SImre Deak static void ibx_hpd_detection_setup(struct drm_i915_private *dev_priv)
34241a56b1a2SImre Deak {
34251a56b1a2SImre Deak 	u32 hotplug;
34261a56b1a2SImre Deak 
34271a56b1a2SImre Deak 	/*
34281a56b1a2SImre Deak 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
34291a56b1a2SImre Deak 	 * duration to 2ms (which is the minimum in the Display Port spec).
34301a56b1a2SImre Deak 	 * The pulse duration bits are reserved on LPT+.
34311a56b1a2SImre Deak 	 */
34321a56b1a2SImre Deak 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
34331a56b1a2SImre Deak 	hotplug &= ~(PORTB_PULSE_DURATION_MASK |
34341a56b1a2SImre Deak 		     PORTC_PULSE_DURATION_MASK |
34351a56b1a2SImre Deak 		     PORTD_PULSE_DURATION_MASK);
34361a56b1a2SImre Deak 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
34371a56b1a2SImre Deak 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
34381a56b1a2SImre Deak 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
34391a56b1a2SImre Deak 	/*
34401a56b1a2SImre Deak 	 * When CPU and PCH are on the same package, port A
34411a56b1a2SImre Deak 	 * HPD must be enabled in both north and south.
34421a56b1a2SImre Deak 	 */
34431a56b1a2SImre Deak 	if (HAS_PCH_LPT_LP(dev_priv))
34441a56b1a2SImre Deak 		hotplug |= PORTA_HOTPLUG_ENABLE;
34451a56b1a2SImre Deak 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
34461a56b1a2SImre Deak }
34471a56b1a2SImre Deak 
344891d14251STvrtko Ursulin static void ibx_hpd_irq_setup(struct drm_i915_private *dev_priv)
344982a28bcfSDaniel Vetter {
34501a56b1a2SImre Deak 	u32 hotplug_irqs, enabled_irqs;
345182a28bcfSDaniel Vetter 
345291d14251STvrtko Ursulin 	if (HAS_PCH_IBX(dev_priv)) {
3453fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
345491d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ibx);
345582a28bcfSDaniel Vetter 	} else {
3456fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
345791d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_cpt);
345882a28bcfSDaniel Vetter 	}
345982a28bcfSDaniel Vetter 
3460fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
346182a28bcfSDaniel Vetter 
34621a56b1a2SImre Deak 	ibx_hpd_detection_setup(dev_priv);
34636dbf30ceSVille Syrjälä }
346426951cafSXiong Zhang 
34652a57d9ccSImre Deak static void spt_hpd_detection_setup(struct drm_i915_private *dev_priv)
34662a57d9ccSImre Deak {
34673b92e263SRodrigo Vivi 	u32 val, hotplug;
34683b92e263SRodrigo Vivi 
34693b92e263SRodrigo Vivi 	/* Display WA #1179 WaHardHangonHotPlug: cnp */
34703b92e263SRodrigo Vivi 	if (HAS_PCH_CNP(dev_priv)) {
34713b92e263SRodrigo Vivi 		val = I915_READ(SOUTH_CHICKEN1);
34723b92e263SRodrigo Vivi 		val &= ~CHASSIS_CLK_REQ_DURATION_MASK;
34733b92e263SRodrigo Vivi 		val |= CHASSIS_CLK_REQ_DURATION(0xf);
34743b92e263SRodrigo Vivi 		I915_WRITE(SOUTH_CHICKEN1, val);
34753b92e263SRodrigo Vivi 	}
34762a57d9ccSImre Deak 
34772a57d9ccSImre Deak 	/* Enable digital hotplug on the PCH */
34782a57d9ccSImre Deak 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
34792a57d9ccSImre Deak 	hotplug |= PORTA_HOTPLUG_ENABLE |
34802a57d9ccSImre Deak 		   PORTB_HOTPLUG_ENABLE |
34812a57d9ccSImre Deak 		   PORTC_HOTPLUG_ENABLE |
34822a57d9ccSImre Deak 		   PORTD_HOTPLUG_ENABLE;
34832a57d9ccSImre Deak 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
34842a57d9ccSImre Deak 
34852a57d9ccSImre Deak 	hotplug = I915_READ(PCH_PORT_HOTPLUG2);
34862a57d9ccSImre Deak 	hotplug |= PORTE_HOTPLUG_ENABLE;
34872a57d9ccSImre Deak 	I915_WRITE(PCH_PORT_HOTPLUG2, hotplug);
34882a57d9ccSImre Deak }
34892a57d9ccSImre Deak 
349091d14251STvrtko Ursulin static void spt_hpd_irq_setup(struct drm_i915_private *dev_priv)
34916dbf30ceSVille Syrjälä {
34922a57d9ccSImre Deak 	u32 hotplug_irqs, enabled_irqs;
34936dbf30ceSVille Syrjälä 
34946dbf30ceSVille Syrjälä 	hotplug_irqs = SDE_HOTPLUG_MASK_SPT;
349591d14251STvrtko Ursulin 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_spt);
34966dbf30ceSVille Syrjälä 
34976dbf30ceSVille Syrjälä 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
34986dbf30ceSVille Syrjälä 
34992a57d9ccSImre Deak 	spt_hpd_detection_setup(dev_priv);
350026951cafSXiong Zhang }
35017fe0b973SKeith Packard 
35021a56b1a2SImre Deak static void ilk_hpd_detection_setup(struct drm_i915_private *dev_priv)
35031a56b1a2SImre Deak {
35041a56b1a2SImre Deak 	u32 hotplug;
35051a56b1a2SImre Deak 
35061a56b1a2SImre Deak 	/*
35071a56b1a2SImre Deak 	 * Enable digital hotplug on the CPU, and configure the DP short pulse
35081a56b1a2SImre Deak 	 * duration to 2ms (which is the minimum in the Display Port spec)
35091a56b1a2SImre Deak 	 * The pulse duration bits are reserved on HSW+.
35101a56b1a2SImre Deak 	 */
35111a56b1a2SImre Deak 	hotplug = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
35121a56b1a2SImre Deak 	hotplug &= ~DIGITAL_PORTA_PULSE_DURATION_MASK;
35131a56b1a2SImre Deak 	hotplug |= DIGITAL_PORTA_HOTPLUG_ENABLE |
35141a56b1a2SImre Deak 		   DIGITAL_PORTA_PULSE_DURATION_2ms;
35151a56b1a2SImre Deak 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, hotplug);
35161a56b1a2SImre Deak }
35171a56b1a2SImre Deak 
351891d14251STvrtko Ursulin static void ilk_hpd_irq_setup(struct drm_i915_private *dev_priv)
3519e4ce95aaSVille Syrjälä {
35201a56b1a2SImre Deak 	u32 hotplug_irqs, enabled_irqs;
3521e4ce95aaSVille Syrjälä 
352291d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 8) {
35233a3b3c7dSVille Syrjälä 		hotplug_irqs = GEN8_PORT_DP_A_HOTPLUG;
352491d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_bdw);
35253a3b3c7dSVille Syrjälä 
35263a3b3c7dSVille Syrjälä 		bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
352791d14251STvrtko Ursulin 	} else if (INTEL_GEN(dev_priv) >= 7) {
352823bb4cb5SVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG_IVB;
352991d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ivb);
35303a3b3c7dSVille Syrjälä 
35313a3b3c7dSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
353223bb4cb5SVille Syrjälä 	} else {
3533e4ce95aaSVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG;
353491d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ilk);
3535e4ce95aaSVille Syrjälä 
3536e4ce95aaSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
35373a3b3c7dSVille Syrjälä 	}
3538e4ce95aaSVille Syrjälä 
35391a56b1a2SImre Deak 	ilk_hpd_detection_setup(dev_priv);
3540e4ce95aaSVille Syrjälä 
354191d14251STvrtko Ursulin 	ibx_hpd_irq_setup(dev_priv);
3542e4ce95aaSVille Syrjälä }
3543e4ce95aaSVille Syrjälä 
35442a57d9ccSImre Deak static void __bxt_hpd_detection_setup(struct drm_i915_private *dev_priv,
35452a57d9ccSImre Deak 				      u32 enabled_irqs)
3546e0a20ad7SShashank Sharma {
35472a57d9ccSImre Deak 	u32 hotplug;
3548e0a20ad7SShashank Sharma 
3549a52bb15bSVille Syrjälä 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
35502a57d9ccSImre Deak 	hotplug |= PORTA_HOTPLUG_ENABLE |
35512a57d9ccSImre Deak 		   PORTB_HOTPLUG_ENABLE |
35522a57d9ccSImre Deak 		   PORTC_HOTPLUG_ENABLE;
3553d252bf68SShubhangi Shrivastava 
3554d252bf68SShubhangi Shrivastava 	DRM_DEBUG_KMS("Invert bit setting: hp_ctl:%x hp_port:%x\n",
3555d252bf68SShubhangi Shrivastava 		      hotplug, enabled_irqs);
3556d252bf68SShubhangi Shrivastava 	hotplug &= ~BXT_DDI_HPD_INVERT_MASK;
3557d252bf68SShubhangi Shrivastava 
3558d252bf68SShubhangi Shrivastava 	/*
3559d252bf68SShubhangi Shrivastava 	 * For BXT invert bit has to be set based on AOB design
3560d252bf68SShubhangi Shrivastava 	 * for HPD detection logic, update it based on VBT fields.
3561d252bf68SShubhangi Shrivastava 	 */
3562d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIA) &&
3563d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_A))
3564d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIA_HPD_INVERT;
3565d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIB) &&
3566d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_B))
3567d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIB_HPD_INVERT;
3568d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIC) &&
3569d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_C))
3570d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIC_HPD_INVERT;
3571d252bf68SShubhangi Shrivastava 
3572a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
3573e0a20ad7SShashank Sharma }
3574e0a20ad7SShashank Sharma 
35752a57d9ccSImre Deak static void bxt_hpd_detection_setup(struct drm_i915_private *dev_priv)
35762a57d9ccSImre Deak {
35772a57d9ccSImre Deak 	__bxt_hpd_detection_setup(dev_priv, BXT_DE_PORT_HOTPLUG_MASK);
35782a57d9ccSImre Deak }
35792a57d9ccSImre Deak 
35802a57d9ccSImre Deak static void bxt_hpd_irq_setup(struct drm_i915_private *dev_priv)
35812a57d9ccSImre Deak {
35822a57d9ccSImre Deak 	u32 hotplug_irqs, enabled_irqs;
35832a57d9ccSImre Deak 
35842a57d9ccSImre Deak 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_bxt);
35852a57d9ccSImre Deak 	hotplug_irqs = BXT_DE_PORT_HOTPLUG_MASK;
35862a57d9ccSImre Deak 
35872a57d9ccSImre Deak 	bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
35882a57d9ccSImre Deak 
35892a57d9ccSImre Deak 	__bxt_hpd_detection_setup(dev_priv, enabled_irqs);
35902a57d9ccSImre Deak }
35912a57d9ccSImre Deak 
3592d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
3593d46da437SPaulo Zanoni {
3594fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
359582a28bcfSDaniel Vetter 	u32 mask;
3596d46da437SPaulo Zanoni 
35976e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
3598692a04cfSDaniel Vetter 		return;
3599692a04cfSDaniel Vetter 
36006e266956STvrtko Ursulin 	if (HAS_PCH_IBX(dev_priv))
36015c673b60SDaniel Vetter 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
36024ebc6509SDhinakaran Pandiyan 	else if (HAS_PCH_CPT(dev_priv) || HAS_PCH_LPT(dev_priv))
36035c673b60SDaniel Vetter 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
36044ebc6509SDhinakaran Pandiyan 	else
36054ebc6509SDhinakaran Pandiyan 		mask = SDE_GMBUS_CPT;
36068664281bSPaulo Zanoni 
36073488d4ebSVille Syrjälä 	gen3_assert_iir_is_zero(dev_priv, SDEIIR);
3608d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
36092a57d9ccSImre Deak 
36102a57d9ccSImre Deak 	if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
36112a57d9ccSImre Deak 	    HAS_PCH_LPT(dev_priv))
36121a56b1a2SImre Deak 		ibx_hpd_detection_setup(dev_priv);
36132a57d9ccSImre Deak 	else
36142a57d9ccSImre Deak 		spt_hpd_detection_setup(dev_priv);
3615d46da437SPaulo Zanoni }
3616d46da437SPaulo Zanoni 
36170a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
36180a9a8c91SDaniel Vetter {
3619fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
36200a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
36210a9a8c91SDaniel Vetter 
36220a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
36230a9a8c91SDaniel Vetter 
36240a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
36253c9192bcSTvrtko Ursulin 	if (HAS_L3_DPF(dev_priv)) {
36260a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
3627772c2a51STvrtko Ursulin 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev_priv);
3628772c2a51STvrtko Ursulin 		gt_irqs |= GT_PARITY_ERROR(dev_priv);
36290a9a8c91SDaniel Vetter 	}
36300a9a8c91SDaniel Vetter 
36310a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
36325db94019STvrtko Ursulin 	if (IS_GEN5(dev_priv)) {
3633f8973c21SChris Wilson 		gt_irqs |= ILK_BSD_USER_INTERRUPT;
36340a9a8c91SDaniel Vetter 	} else {
36350a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
36360a9a8c91SDaniel Vetter 	}
36370a9a8c91SDaniel Vetter 
36383488d4ebSVille Syrjälä 	GEN3_IRQ_INIT(GT, dev_priv->gt_irq_mask, gt_irqs);
36390a9a8c91SDaniel Vetter 
3640b243f530STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 6) {
364178e68d36SImre Deak 		/*
364278e68d36SImre Deak 		 * RPS interrupts will get enabled/disabled on demand when RPS
364378e68d36SImre Deak 		 * itself is enabled/disabled.
364478e68d36SImre Deak 		 */
3645f4e9af4fSAkash Goel 		if (HAS_VEBOX(dev_priv)) {
36460a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
3647f4e9af4fSAkash Goel 			dev_priv->pm_ier |= PM_VEBOX_USER_INTERRUPT;
3648f4e9af4fSAkash Goel 		}
36490a9a8c91SDaniel Vetter 
3650f4e9af4fSAkash Goel 		dev_priv->pm_imr = 0xffffffff;
36513488d4ebSVille Syrjälä 		GEN3_IRQ_INIT(GEN6_PM, dev_priv->pm_imr, pm_irqs);
36520a9a8c91SDaniel Vetter 	}
36530a9a8c91SDaniel Vetter }
36540a9a8c91SDaniel Vetter 
3655f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
3656036a4a7dSZhenyu Wang {
3657fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
36588e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
36598e76f8dcSPaulo Zanoni 
3660b243f530STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 7) {
36618e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
3662842ebf7aSVille Syrjälä 				DE_PCH_EVENT_IVB | DE_AUX_CHANNEL_A_IVB);
36638e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
366423bb4cb5SVille Syrjälä 			      DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB |
366523bb4cb5SVille Syrjälä 			      DE_DP_A_HOTPLUG_IVB);
36668e76f8dcSPaulo Zanoni 	} else {
36678e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3668842ebf7aSVille Syrjälä 				DE_AUX_CHANNEL_A | DE_PIPEB_CRC_DONE |
3669842ebf7aSVille Syrjälä 				DE_PIPEA_CRC_DONE | DE_POISON);
3670e4ce95aaSVille Syrjälä 		extra_mask = (DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
3671e4ce95aaSVille Syrjälä 			      DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN |
3672e4ce95aaSVille Syrjälä 			      DE_DP_A_HOTPLUG);
36738e76f8dcSPaulo Zanoni 	}
3674036a4a7dSZhenyu Wang 
36751ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3676036a4a7dSZhenyu Wang 
3677622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3678622364b6SPaulo Zanoni 
36793488d4ebSVille Syrjälä 	GEN3_IRQ_INIT(DE, dev_priv->irq_mask, display_mask | extra_mask);
3680036a4a7dSZhenyu Wang 
36810a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
3682036a4a7dSZhenyu Wang 
36831a56b1a2SImre Deak 	ilk_hpd_detection_setup(dev_priv);
36841a56b1a2SImre Deak 
3685d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
36867fe0b973SKeith Packard 
368750a0bc90STvrtko Ursulin 	if (IS_IRONLAKE_M(dev_priv)) {
36886005ce42SDaniel Vetter 		/* Enable PCU event interrupts
36896005ce42SDaniel Vetter 		 *
36906005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
36914bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
36924bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
3693d6207435SDaniel Vetter 		spin_lock_irq(&dev_priv->irq_lock);
3694fbdedaeaSVille Syrjälä 		ilk_enable_display_irq(dev_priv, DE_PCU_EVENT);
3695d6207435SDaniel Vetter 		spin_unlock_irq(&dev_priv->irq_lock);
3696f97108d1SJesse Barnes 	}
3697f97108d1SJesse Barnes 
3698036a4a7dSZhenyu Wang 	return 0;
3699036a4a7dSZhenyu Wang }
3700036a4a7dSZhenyu Wang 
3701f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3702f8b79e58SImre Deak {
370367520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
3704f8b79e58SImre Deak 
3705f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3706f8b79e58SImre Deak 		return;
3707f8b79e58SImre Deak 
3708f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3709f8b79e58SImre Deak 
3710d6c69803SVille Syrjälä 	if (intel_irqs_enabled(dev_priv)) {
3711d6c69803SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3712ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3713f8b79e58SImre Deak 	}
3714d6c69803SVille Syrjälä }
3715f8b79e58SImre Deak 
3716f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3717f8b79e58SImre Deak {
371867520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
3719f8b79e58SImre Deak 
3720f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3721f8b79e58SImre Deak 		return;
3722f8b79e58SImre Deak 
3723f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3724f8b79e58SImre Deak 
3725950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3726ad22d106SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3727f8b79e58SImre Deak }
3728f8b79e58SImre Deak 
37290e6c9a9eSVille Syrjälä 
37300e6c9a9eSVille Syrjälä static int valleyview_irq_postinstall(struct drm_device *dev)
37310e6c9a9eSVille Syrjälä {
3732fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
37330e6c9a9eSVille Syrjälä 
37340a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
37357e231dbeSJesse Barnes 
3736ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
37379918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3738ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3739ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3740ad22d106SVille Syrjälä 
37417e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
374234c7b8a7SVille Syrjälä 	POSTING_READ(VLV_MASTER_IER);
374320afbda2SDaniel Vetter 
374420afbda2SDaniel Vetter 	return 0;
374520afbda2SDaniel Vetter }
374620afbda2SDaniel Vetter 
3747abd58f01SBen Widawsky static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
3748abd58f01SBen Widawsky {
3749abd58f01SBen Widawsky 	/* These are interrupts we'll toggle with the ring mask register */
3750abd58f01SBen Widawsky 	uint32_t gt_interrupts[] = {
3751abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
375273d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
375373d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT |
375473d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT,
3755abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
375673d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
375773d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT |
375873d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT,
3759abd58f01SBen Widawsky 		0,
376073d477f6SOscar Mateo 		GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT |
376173d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT
3762abd58f01SBen Widawsky 		};
3763abd58f01SBen Widawsky 
376498735739STvrtko Ursulin 	if (HAS_L3_DPF(dev_priv))
376598735739STvrtko Ursulin 		gt_interrupts[0] |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
376698735739STvrtko Ursulin 
3767f4e9af4fSAkash Goel 	dev_priv->pm_ier = 0x0;
3768f4e9af4fSAkash Goel 	dev_priv->pm_imr = ~dev_priv->pm_ier;
37699a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 0, ~gt_interrupts[0], gt_interrupts[0]);
37709a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 1, ~gt_interrupts[1], gt_interrupts[1]);
377178e68d36SImre Deak 	/*
377278e68d36SImre Deak 	 * RPS interrupts will get enabled/disabled on demand when RPS itself
377326705e20SSagar Arun Kamble 	 * is enabled/disabled. Same wil be the case for GuC interrupts.
377478e68d36SImre Deak 	 */
3775f4e9af4fSAkash Goel 	GEN8_IRQ_INIT_NDX(GT, 2, dev_priv->pm_imr, dev_priv->pm_ier);
37769a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 3, ~gt_interrupts[3], gt_interrupts[3]);
3777abd58f01SBen Widawsky }
3778abd58f01SBen Widawsky 
3779abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3780abd58f01SBen Widawsky {
3781770de83dSDamien Lespiau 	uint32_t de_pipe_masked = GEN8_PIPE_CDCLK_CRC_DONE;
3782770de83dSDamien Lespiau 	uint32_t de_pipe_enables;
37833a3b3c7dSVille Syrjälä 	u32 de_port_masked = GEN8_AUX_CHANNEL_A;
37843a3b3c7dSVille Syrjälä 	u32 de_port_enables;
378511825b0dSVille Syrjälä 	u32 de_misc_masked = GEN8_DE_MISC_GSE;
37863a3b3c7dSVille Syrjälä 	enum pipe pipe;
3787770de83dSDamien Lespiau 
3788bca2bf2aSPandiyan, Dhinakaran 	if (INTEL_GEN(dev_priv) >= 9) {
3789842ebf7aSVille Syrjälä 		de_pipe_masked |= GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
37903a3b3c7dSVille Syrjälä 		de_port_masked |= GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
379188e04703SJesse Barnes 				  GEN9_AUX_CHANNEL_D;
3792cc3f90f0SAnder Conselvan de Oliveira 		if (IS_GEN9_LP(dev_priv))
37933a3b3c7dSVille Syrjälä 			de_port_masked |= BXT_DE_PORT_GMBUS;
37943a3b3c7dSVille Syrjälä 	} else {
3795842ebf7aSVille Syrjälä 		de_pipe_masked |= GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
37963a3b3c7dSVille Syrjälä 	}
3797770de83dSDamien Lespiau 
3798a324fcacSRodrigo Vivi 	if (IS_CNL_WITH_PORT_F(dev_priv))
3799a324fcacSRodrigo Vivi 		de_port_masked |= CNL_AUX_CHANNEL_F;
3800a324fcacSRodrigo Vivi 
3801770de83dSDamien Lespiau 	de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
3802770de83dSDamien Lespiau 					   GEN8_PIPE_FIFO_UNDERRUN;
3803770de83dSDamien Lespiau 
38043a3b3c7dSVille Syrjälä 	de_port_enables = de_port_masked;
3805cc3f90f0SAnder Conselvan de Oliveira 	if (IS_GEN9_LP(dev_priv))
3806a52bb15bSVille Syrjälä 		de_port_enables |= BXT_DE_PORT_HOTPLUG_MASK;
3807a52bb15bSVille Syrjälä 	else if (IS_BROADWELL(dev_priv))
38083a3b3c7dSVille Syrjälä 		de_port_enables |= GEN8_PORT_DP_A_HOTPLUG;
38093a3b3c7dSVille Syrjälä 
38100a195c02SMika Kahola 	for_each_pipe(dev_priv, pipe) {
38110a195c02SMika Kahola 		dev_priv->de_irq_mask[pipe] = ~de_pipe_masked;
3812abd58f01SBen Widawsky 
3813f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3814813bde43SPaulo Zanoni 				POWER_DOMAIN_PIPE(pipe)))
3815813bde43SPaulo Zanoni 			GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
3816813bde43SPaulo Zanoni 					  dev_priv->de_irq_mask[pipe],
381735079899SPaulo Zanoni 					  de_pipe_enables);
38180a195c02SMika Kahola 	}
3819abd58f01SBen Widawsky 
38203488d4ebSVille Syrjälä 	GEN3_IRQ_INIT(GEN8_DE_PORT_, ~de_port_masked, de_port_enables);
38213488d4ebSVille Syrjälä 	GEN3_IRQ_INIT(GEN8_DE_MISC_, ~de_misc_masked, de_misc_masked);
38222a57d9ccSImre Deak 
38232a57d9ccSImre Deak 	if (IS_GEN9_LP(dev_priv))
38242a57d9ccSImre Deak 		bxt_hpd_detection_setup(dev_priv);
38251a56b1a2SImre Deak 	else if (IS_BROADWELL(dev_priv))
38261a56b1a2SImre Deak 		ilk_hpd_detection_setup(dev_priv);
3827abd58f01SBen Widawsky }
3828abd58f01SBen Widawsky 
3829abd58f01SBen Widawsky static int gen8_irq_postinstall(struct drm_device *dev)
3830abd58f01SBen Widawsky {
3831fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3832abd58f01SBen Widawsky 
38336e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
3834622364b6SPaulo Zanoni 		ibx_irq_pre_postinstall(dev);
3835622364b6SPaulo Zanoni 
3836abd58f01SBen Widawsky 	gen8_gt_irq_postinstall(dev_priv);
3837abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3838abd58f01SBen Widawsky 
38396e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
3840abd58f01SBen Widawsky 		ibx_irq_postinstall(dev);
3841abd58f01SBen Widawsky 
3842e5328c43SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
3843abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3844abd58f01SBen Widawsky 
3845abd58f01SBen Widawsky 	return 0;
3846abd58f01SBen Widawsky }
3847abd58f01SBen Widawsky 
384851951ae7SMika Kuoppala static void gen11_gt_irq_postinstall(struct drm_i915_private *dev_priv)
384951951ae7SMika Kuoppala {
385051951ae7SMika Kuoppala 	const u32 irqs = GT_RENDER_USER_INTERRUPT | GT_CONTEXT_SWITCH_INTERRUPT;
385151951ae7SMika Kuoppala 
385251951ae7SMika Kuoppala 	BUILD_BUG_ON(irqs & 0xffff0000);
385351951ae7SMika Kuoppala 
385451951ae7SMika Kuoppala 	/* Enable RCS, BCS, VCS and VECS class interrupts. */
385551951ae7SMika Kuoppala 	I915_WRITE(GEN11_RENDER_COPY_INTR_ENABLE, irqs << 16 | irqs);
385651951ae7SMika Kuoppala 	I915_WRITE(GEN11_VCS_VECS_INTR_ENABLE,	  irqs << 16 | irqs);
385751951ae7SMika Kuoppala 
385851951ae7SMika Kuoppala 	/* Unmask irqs on RCS, BCS, VCS and VECS engines. */
385951951ae7SMika Kuoppala 	I915_WRITE(GEN11_RCS0_RSVD_INTR_MASK,	~(irqs << 16));
386051951ae7SMika Kuoppala 	I915_WRITE(GEN11_BCS_RSVD_INTR_MASK,	~(irqs << 16));
386151951ae7SMika Kuoppala 	I915_WRITE(GEN11_VCS0_VCS1_INTR_MASK,	~(irqs | irqs << 16));
386251951ae7SMika Kuoppala 	I915_WRITE(GEN11_VCS2_VCS3_INTR_MASK,	~(irqs | irqs << 16));
386351951ae7SMika Kuoppala 	I915_WRITE(GEN11_VECS0_VECS1_INTR_MASK,	~(irqs | irqs << 16));
386451951ae7SMika Kuoppala 
386551951ae7SMika Kuoppala 	dev_priv->pm_imr = 0xffffffff; /* TODO */
386651951ae7SMika Kuoppala }
386751951ae7SMika Kuoppala 
386851951ae7SMika Kuoppala static int gen11_irq_postinstall(struct drm_device *dev)
386951951ae7SMika Kuoppala {
387051951ae7SMika Kuoppala 	struct drm_i915_private *dev_priv = dev->dev_private;
387151951ae7SMika Kuoppala 
387251951ae7SMika Kuoppala 	gen11_gt_irq_postinstall(dev_priv);
387351951ae7SMika Kuoppala 	gen8_de_irq_postinstall(dev_priv);
387451951ae7SMika Kuoppala 
387551951ae7SMika Kuoppala 	I915_WRITE(GEN11_DISPLAY_INT_CTL, GEN11_DISPLAY_IRQ_ENABLE);
387651951ae7SMika Kuoppala 
387751951ae7SMika Kuoppala 	I915_WRITE(GEN11_GFX_MSTR_IRQ, GEN11_MASTER_IRQ);
387851951ae7SMika Kuoppala 	POSTING_READ(GEN11_GFX_MSTR_IRQ);
387951951ae7SMika Kuoppala 
388051951ae7SMika Kuoppala 	return 0;
388151951ae7SMika Kuoppala }
388251951ae7SMika Kuoppala 
388343f328d7SVille Syrjälä static int cherryview_irq_postinstall(struct drm_device *dev)
388443f328d7SVille Syrjälä {
3885fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
388643f328d7SVille Syrjälä 
388743f328d7SVille Syrjälä 	gen8_gt_irq_postinstall(dev_priv);
388843f328d7SVille Syrjälä 
3889ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
38909918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3891ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3892ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3893ad22d106SVille Syrjälä 
3894e5328c43SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
389543f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
389643f328d7SVille Syrjälä 
389743f328d7SVille Syrjälä 	return 0;
389843f328d7SVille Syrjälä }
389943f328d7SVille Syrjälä 
39006bcdb1c8SVille Syrjälä static void i8xx_irq_reset(struct drm_device *dev)
3901c2798b19SChris Wilson {
3902fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3903c2798b19SChris Wilson 
390444d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
390544d9241eSVille Syrjälä 
3906d420a50cSVille Syrjälä 	I915_WRITE16(HWSTAM, 0xffff);
3907d420a50cSVille Syrjälä 
3908e9e9848aSVille Syrjälä 	GEN2_IRQ_RESET();
3909c2798b19SChris Wilson }
3910c2798b19SChris Wilson 
3911c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
3912c2798b19SChris Wilson {
3913fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3914e9e9848aSVille Syrjälä 	u16 enable_mask;
3915c2798b19SChris Wilson 
3916045cebd2SVille Syrjälä 	I915_WRITE16(EMR, ~(I915_ERROR_PAGE_TABLE |
3917045cebd2SVille Syrjälä 			    I915_ERROR_MEMORY_REFRESH));
3918c2798b19SChris Wilson 
3919c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
3920c2798b19SChris Wilson 	dev_priv->irq_mask =
3921c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3922842ebf7aSVille Syrjälä 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT);
3923c2798b19SChris Wilson 
3924e9e9848aSVille Syrjälä 	enable_mask =
3925c2798b19SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3926c2798b19SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3927e9e9848aSVille Syrjälä 		I915_USER_INTERRUPT;
3928e9e9848aSVille Syrjälä 
3929e9e9848aSVille Syrjälä 	GEN2_IRQ_INIT(, dev_priv->irq_mask, enable_mask);
3930c2798b19SChris Wilson 
3931379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3932379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3933d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3934755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3935755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3936d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3937379ef82dSDaniel Vetter 
3938c2798b19SChris Wilson 	return 0;
3939c2798b19SChris Wilson }
3940c2798b19SChris Wilson 
3941ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3942c2798b19SChris Wilson {
394345a83f84SDaniel Vetter 	struct drm_device *dev = arg;
3944fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3945af722d28SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
3946c2798b19SChris Wilson 
39472dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
39482dd2a883SImre Deak 		return IRQ_NONE;
39492dd2a883SImre Deak 
39501f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
39511f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
39521f814dacSImre Deak 
3953af722d28SVille Syrjälä 	do {
3954af722d28SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
3955af722d28SVille Syrjälä 		u16 iir;
3956af722d28SVille Syrjälä 
3957c2798b19SChris Wilson 		iir = I915_READ16(IIR);
3958c2798b19SChris Wilson 		if (iir == 0)
3959af722d28SVille Syrjälä 			break;
3960c2798b19SChris Wilson 
3961af722d28SVille Syrjälä 		ret = IRQ_HANDLED;
3962c2798b19SChris Wilson 
3963eb64343cSVille Syrjälä 		/* Call regardless, as some status bits might not be
3964eb64343cSVille Syrjälä 		 * signalled in iir */
3965eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
3966c2798b19SChris Wilson 
3967fd3a4024SDaniel Vetter 		I915_WRITE16(IIR, iir);
3968c2798b19SChris Wilson 
3969c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
39703b3f1650SAkash Goel 			notify_ring(dev_priv->engine[RCS]);
3971c2798b19SChris Wilson 
3972af722d28SVille Syrjälä 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3973af722d28SVille Syrjälä 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
3974af722d28SVille Syrjälä 
3975eb64343cSVille Syrjälä 		i8xx_pipestat_irq_handler(dev_priv, iir, pipe_stats);
3976af722d28SVille Syrjälä 	} while (0);
3977c2798b19SChris Wilson 
39781f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
39791f814dacSImre Deak 
39801f814dacSImre Deak 	return ret;
3981c2798b19SChris Wilson }
3982c2798b19SChris Wilson 
39836bcdb1c8SVille Syrjälä static void i915_irq_reset(struct drm_device *dev)
3984a266c7d5SChris Wilson {
3985fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3986a266c7d5SChris Wilson 
398756b857a5STvrtko Ursulin 	if (I915_HAS_HOTPLUG(dev_priv)) {
39880706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
3989a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3990a266c7d5SChris Wilson 	}
3991a266c7d5SChris Wilson 
399244d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
399344d9241eSVille Syrjälä 
3994d420a50cSVille Syrjälä 	I915_WRITE(HWSTAM, 0xffffffff);
399544d9241eSVille Syrjälä 
3996ba7eb789SVille Syrjälä 	GEN3_IRQ_RESET();
3997a266c7d5SChris Wilson }
3998a266c7d5SChris Wilson 
3999a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
4000a266c7d5SChris Wilson {
4001fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
400238bde180SChris Wilson 	u32 enable_mask;
4003a266c7d5SChris Wilson 
4004045cebd2SVille Syrjälä 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE |
4005045cebd2SVille Syrjälä 			  I915_ERROR_MEMORY_REFRESH));
400638bde180SChris Wilson 
400738bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
400838bde180SChris Wilson 	dev_priv->irq_mask =
400938bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
401038bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4011842ebf7aSVille Syrjälä 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT);
401238bde180SChris Wilson 
401338bde180SChris Wilson 	enable_mask =
401438bde180SChris Wilson 		I915_ASLE_INTERRUPT |
401538bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
401638bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
401738bde180SChris Wilson 		I915_USER_INTERRUPT;
401838bde180SChris Wilson 
401956b857a5STvrtko Ursulin 	if (I915_HAS_HOTPLUG(dev_priv)) {
4020a266c7d5SChris Wilson 		/* Enable in IER... */
4021a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
4022a266c7d5SChris Wilson 		/* and unmask in IMR */
4023a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
4024a266c7d5SChris Wilson 	}
4025a266c7d5SChris Wilson 
4026ba7eb789SVille Syrjälä 	GEN3_IRQ_INIT(, dev_priv->irq_mask, enable_mask);
4027a266c7d5SChris Wilson 
4028379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4029379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4030d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4031755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4032755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4033d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4034379ef82dSDaniel Vetter 
4035c30bb1fdSVille Syrjälä 	i915_enable_asle_pipestat(dev_priv);
4036c30bb1fdSVille Syrjälä 
403720afbda2SDaniel Vetter 	return 0;
403820afbda2SDaniel Vetter }
403920afbda2SDaniel Vetter 
4040ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
4041a266c7d5SChris Wilson {
404245a83f84SDaniel Vetter 	struct drm_device *dev = arg;
4043fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4044af722d28SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
4045a266c7d5SChris Wilson 
40462dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
40472dd2a883SImre Deak 		return IRQ_NONE;
40482dd2a883SImre Deak 
40491f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
40501f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
40511f814dacSImre Deak 
405238bde180SChris Wilson 	do {
4053eb64343cSVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
4054af722d28SVille Syrjälä 		u32 hotplug_status = 0;
4055af722d28SVille Syrjälä 		u32 iir;
4056a266c7d5SChris Wilson 
4057af722d28SVille Syrjälä 		iir = I915_READ(IIR);
4058af722d28SVille Syrjälä 		if (iir == 0)
4059af722d28SVille Syrjälä 			break;
4060af722d28SVille Syrjälä 
4061af722d28SVille Syrjälä 		ret = IRQ_HANDLED;
4062af722d28SVille Syrjälä 
4063af722d28SVille Syrjälä 		if (I915_HAS_HOTPLUG(dev_priv) &&
4064af722d28SVille Syrjälä 		    iir & I915_DISPLAY_PORT_INTERRUPT)
4065af722d28SVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
4066a266c7d5SChris Wilson 
4067eb64343cSVille Syrjälä 		/* Call regardless, as some status bits might not be
4068eb64343cSVille Syrjälä 		 * signalled in iir */
4069eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
4070a266c7d5SChris Wilson 
4071fd3a4024SDaniel Vetter 		I915_WRITE(IIR, iir);
4072a266c7d5SChris Wilson 
4073a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
40743b3f1650SAkash Goel 			notify_ring(dev_priv->engine[RCS]);
4075a266c7d5SChris Wilson 
4076af722d28SVille Syrjälä 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4077af722d28SVille Syrjälä 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4078a266c7d5SChris Wilson 
4079af722d28SVille Syrjälä 		if (hotplug_status)
4080af722d28SVille Syrjälä 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
4081af722d28SVille Syrjälä 
4082af722d28SVille Syrjälä 		i915_pipestat_irq_handler(dev_priv, iir, pipe_stats);
4083af722d28SVille Syrjälä 	} while (0);
4084a266c7d5SChris Wilson 
40851f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
40861f814dacSImre Deak 
4087a266c7d5SChris Wilson 	return ret;
4088a266c7d5SChris Wilson }
4089a266c7d5SChris Wilson 
40906bcdb1c8SVille Syrjälä static void i965_irq_reset(struct drm_device *dev)
4091a266c7d5SChris Wilson {
4092fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4093a266c7d5SChris Wilson 
40940706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4095a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4096a266c7d5SChris Wilson 
409744d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
409844d9241eSVille Syrjälä 
4099d420a50cSVille Syrjälä 	I915_WRITE(HWSTAM, 0xffffffff);
410044d9241eSVille Syrjälä 
4101ba7eb789SVille Syrjälä 	GEN3_IRQ_RESET();
4102a266c7d5SChris Wilson }
4103a266c7d5SChris Wilson 
4104a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
4105a266c7d5SChris Wilson {
4106fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4107bbba0a97SChris Wilson 	u32 enable_mask;
4108a266c7d5SChris Wilson 	u32 error_mask;
4109a266c7d5SChris Wilson 
4110045cebd2SVille Syrjälä 	/*
4111045cebd2SVille Syrjälä 	 * Enable some error detection, note the instruction error mask
4112045cebd2SVille Syrjälä 	 * bit is reserved, so we leave it masked.
4113045cebd2SVille Syrjälä 	 */
4114045cebd2SVille Syrjälä 	if (IS_G4X(dev_priv)) {
4115045cebd2SVille Syrjälä 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
4116045cebd2SVille Syrjälä 			       GM45_ERROR_MEM_PRIV |
4117045cebd2SVille Syrjälä 			       GM45_ERROR_CP_PRIV |
4118045cebd2SVille Syrjälä 			       I915_ERROR_MEMORY_REFRESH);
4119045cebd2SVille Syrjälä 	} else {
4120045cebd2SVille Syrjälä 		error_mask = ~(I915_ERROR_PAGE_TABLE |
4121045cebd2SVille Syrjälä 			       I915_ERROR_MEMORY_REFRESH);
4122045cebd2SVille Syrjälä 	}
4123045cebd2SVille Syrjälä 	I915_WRITE(EMR, error_mask);
4124045cebd2SVille Syrjälä 
4125a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
4126c30bb1fdSVille Syrjälä 	dev_priv->irq_mask =
4127c30bb1fdSVille Syrjälä 		~(I915_ASLE_INTERRUPT |
4128adca4730SChris Wilson 		  I915_DISPLAY_PORT_INTERRUPT |
4129bbba0a97SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4130bbba0a97SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
4131bbba0a97SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
4132bbba0a97SChris Wilson 
4133c30bb1fdSVille Syrjälä 	enable_mask =
4134c30bb1fdSVille Syrjälä 		I915_ASLE_INTERRUPT |
4135c30bb1fdSVille Syrjälä 		I915_DISPLAY_PORT_INTERRUPT |
4136c30bb1fdSVille Syrjälä 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4137c30bb1fdSVille Syrjälä 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
4138c30bb1fdSVille Syrjälä 		I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
4139c30bb1fdSVille Syrjälä 		I915_USER_INTERRUPT;
4140bbba0a97SChris Wilson 
414191d14251STvrtko Ursulin 	if (IS_G4X(dev_priv))
4142bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
4143a266c7d5SChris Wilson 
4144c30bb1fdSVille Syrjälä 	GEN3_IRQ_INIT(, dev_priv->irq_mask, enable_mask);
4145c30bb1fdSVille Syrjälä 
4146b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4147b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4148d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4149755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
4150755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4151755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4152d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4153a266c7d5SChris Wilson 
415491d14251STvrtko Ursulin 	i915_enable_asle_pipestat(dev_priv);
415520afbda2SDaniel Vetter 
415620afbda2SDaniel Vetter 	return 0;
415720afbda2SDaniel Vetter }
415820afbda2SDaniel Vetter 
415991d14251STvrtko Ursulin static void i915_hpd_irq_setup(struct drm_i915_private *dev_priv)
416020afbda2SDaniel Vetter {
416120afbda2SDaniel Vetter 	u32 hotplug_en;
416220afbda2SDaniel Vetter 
416367520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
4164b5ea2d56SDaniel Vetter 
4165adca4730SChris Wilson 	/* Note HDMI and DP share hotplug bits */
4166e5868a31SEgbert Eich 	/* enable bits are the same for all generations */
416791d14251STvrtko Ursulin 	hotplug_en = intel_hpd_enabled_irqs(dev_priv, hpd_mask_i915);
4168a266c7d5SChris Wilson 	/* Programming the CRT detection parameters tends
4169a266c7d5SChris Wilson 	   to generate a spurious hotplug event about three
4170a266c7d5SChris Wilson 	   seconds later.  So just do it once.
4171a266c7d5SChris Wilson 	*/
417291d14251STvrtko Ursulin 	if (IS_G4X(dev_priv))
4173a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
4174a266c7d5SChris Wilson 	hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
4175a266c7d5SChris Wilson 
4176a266c7d5SChris Wilson 	/* Ignore TV since it's buggy */
41770706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv,
4178f9e3dc78SJani Nikula 					     HOTPLUG_INT_EN_MASK |
4179f9e3dc78SJani Nikula 					     CRT_HOTPLUG_VOLTAGE_COMPARE_MASK |
4180f9e3dc78SJani Nikula 					     CRT_HOTPLUG_ACTIVATION_PERIOD_64,
41810706f17cSEgbert Eich 					     hotplug_en);
4182a266c7d5SChris Wilson }
4183a266c7d5SChris Wilson 
4184ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
4185a266c7d5SChris Wilson {
418645a83f84SDaniel Vetter 	struct drm_device *dev = arg;
4187fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4188af722d28SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
4189a266c7d5SChris Wilson 
41902dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
41912dd2a883SImre Deak 		return IRQ_NONE;
41922dd2a883SImre Deak 
41931f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
41941f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
41951f814dacSImre Deak 
4196af722d28SVille Syrjälä 	do {
4197eb64343cSVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
4198af722d28SVille Syrjälä 		u32 hotplug_status = 0;
4199af722d28SVille Syrjälä 		u32 iir;
42002c8ba29fSChris Wilson 
4201af722d28SVille Syrjälä 		iir = I915_READ(IIR);
4202af722d28SVille Syrjälä 		if (iir == 0)
4203af722d28SVille Syrjälä 			break;
4204af722d28SVille Syrjälä 
4205af722d28SVille Syrjälä 		ret = IRQ_HANDLED;
4206af722d28SVille Syrjälä 
4207af722d28SVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
4208af722d28SVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
4209a266c7d5SChris Wilson 
4210eb64343cSVille Syrjälä 		/* Call regardless, as some status bits might not be
4211eb64343cSVille Syrjälä 		 * signalled in iir */
4212eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
4213a266c7d5SChris Wilson 
4214fd3a4024SDaniel Vetter 		I915_WRITE(IIR, iir);
4215a266c7d5SChris Wilson 
4216a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
42173b3f1650SAkash Goel 			notify_ring(dev_priv->engine[RCS]);
4218af722d28SVille Syrjälä 
4219a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
42203b3f1650SAkash Goel 			notify_ring(dev_priv->engine[VCS]);
4221a266c7d5SChris Wilson 
4222af722d28SVille Syrjälä 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4223af722d28SVille Syrjälä 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4224515ac2bbSDaniel Vetter 
4225af722d28SVille Syrjälä 		if (hotplug_status)
4226af722d28SVille Syrjälä 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
4227af722d28SVille Syrjälä 
4228af722d28SVille Syrjälä 		i965_pipestat_irq_handler(dev_priv, iir, pipe_stats);
4229af722d28SVille Syrjälä 	} while (0);
4230a266c7d5SChris Wilson 
42311f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
42321f814dacSImre Deak 
4233a266c7d5SChris Wilson 	return ret;
4234a266c7d5SChris Wilson }
4235a266c7d5SChris Wilson 
4236fca52a55SDaniel Vetter /**
4237fca52a55SDaniel Vetter  * intel_irq_init - initializes irq support
4238fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4239fca52a55SDaniel Vetter  *
4240fca52a55SDaniel Vetter  * This function initializes all the irq support including work items, timers
4241fca52a55SDaniel Vetter  * and all the vtables. It does not setup the interrupt itself though.
4242fca52a55SDaniel Vetter  */
4243b963291cSDaniel Vetter void intel_irq_init(struct drm_i915_private *dev_priv)
4244f71d4af4SJesse Barnes {
424591c8a326SChris Wilson 	struct drm_device *dev = &dev_priv->drm;
4246562d9baeSSagar Arun Kamble 	struct intel_rps *rps = &dev_priv->gt_pm.rps;
4247cefcff8fSJoonas Lahtinen 	int i;
42488b2e326dSChris Wilson 
424977913b39SJani Nikula 	intel_hpd_init_work(dev_priv);
425077913b39SJani Nikula 
4251562d9baeSSagar Arun Kamble 	INIT_WORK(&rps->work, gen6_pm_rps_work);
4252cefcff8fSJoonas Lahtinen 
4253a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
4254cefcff8fSJoonas Lahtinen 	for (i = 0; i < MAX_L3_SLICES; ++i)
4255cefcff8fSJoonas Lahtinen 		dev_priv->l3_parity.remap_info[i] = NULL;
42568b2e326dSChris Wilson 
42574805fe82STvrtko Ursulin 	if (HAS_GUC_SCHED(dev_priv))
425826705e20SSagar Arun Kamble 		dev_priv->pm_guc_events = GEN9_GUC_TO_HOST_INT_EVENT;
425926705e20SSagar Arun Kamble 
4260a6706b45SDeepak S 	/* Let's track the enabled rps events */
4261666a4537SWayne Boyer 	if (IS_VALLEYVIEW(dev_priv))
42626c65a587SVille Syrjälä 		/* WaGsvRC0ResidencyMethod:vlv */
4263e0e8c7cbSChris Wilson 		dev_priv->pm_rps_events = GEN6_PM_RP_UP_EI_EXPIRED;
426431685c25SDeepak S 	else
4265a6706b45SDeepak S 		dev_priv->pm_rps_events = GEN6_PM_RPS_EVENTS;
4266a6706b45SDeepak S 
4267562d9baeSSagar Arun Kamble 	rps->pm_intrmsk_mbz = 0;
42681800ad25SSagar Arun Kamble 
42691800ad25SSagar Arun Kamble 	/*
4270acf2dc22SMika Kuoppala 	 * SNB,IVB,HSW can while VLV,CHV may hard hang on looping batchbuffer
42711800ad25SSagar Arun Kamble 	 * if GEN6_PM_UP_EI_EXPIRED is masked.
42721800ad25SSagar Arun Kamble 	 *
42731800ad25SSagar Arun Kamble 	 * TODO: verify if this can be reproduced on VLV,CHV.
42741800ad25SSagar Arun Kamble 	 */
4275bca2bf2aSPandiyan, Dhinakaran 	if (INTEL_GEN(dev_priv) <= 7)
4276562d9baeSSagar Arun Kamble 		rps->pm_intrmsk_mbz |= GEN6_PM_RP_UP_EI_EXPIRED;
42771800ad25SSagar Arun Kamble 
4278bca2bf2aSPandiyan, Dhinakaran 	if (INTEL_GEN(dev_priv) >= 8)
4279562d9baeSSagar Arun Kamble 		rps->pm_intrmsk_mbz |= GEN8_PMINTR_DISABLE_REDIRECT_TO_GUC;
42801800ad25SSagar Arun Kamble 
4281b963291cSDaniel Vetter 	if (IS_GEN2(dev_priv)) {
42824194c088SRodrigo Vivi 		/* Gen2 doesn't have a hardware frame counter */
42834cdb83ecSVille Syrjälä 		dev->max_vblank_count = 0;
4284bca2bf2aSPandiyan, Dhinakaran 	} else if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5) {
4285f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
4286fd8f507cSVille Syrjälä 		dev->driver->get_vblank_counter = g4x_get_vblank_counter;
4287391f75e2SVille Syrjälä 	} else {
4288391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
4289391f75e2SVille Syrjälä 		dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
4290f71d4af4SJesse Barnes 	}
4291f71d4af4SJesse Barnes 
429221da2700SVille Syrjälä 	/*
429321da2700SVille Syrjälä 	 * Opt out of the vblank disable timer on everything except gen2.
429421da2700SVille Syrjälä 	 * Gen2 doesn't have a hardware frame counter and so depends on
429521da2700SVille Syrjälä 	 * vblank interrupts to produce sane vblank seuquence numbers.
429621da2700SVille Syrjälä 	 */
4297b963291cSDaniel Vetter 	if (!IS_GEN2(dev_priv))
429821da2700SVille Syrjälä 		dev->vblank_disable_immediate = true;
429921da2700SVille Syrjälä 
4300262fd485SChris Wilson 	/* Most platforms treat the display irq block as an always-on
4301262fd485SChris Wilson 	 * power domain. vlv/chv can disable it at runtime and need
4302262fd485SChris Wilson 	 * special care to avoid writing any of the display block registers
4303262fd485SChris Wilson 	 * outside of the power domain. We defer setting up the display irqs
4304262fd485SChris Wilson 	 * in this case to the runtime pm.
4305262fd485SChris Wilson 	 */
4306262fd485SChris Wilson 	dev_priv->display_irqs_enabled = true;
4307262fd485SChris Wilson 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4308262fd485SChris Wilson 		dev_priv->display_irqs_enabled = false;
4309262fd485SChris Wilson 
4310317eaa95SLyude 	dev_priv->hotplug.hpd_storm_threshold = HPD_STORM_DEFAULT_THRESHOLD;
4311317eaa95SLyude 
43121bf6ad62SDaniel Vetter 	dev->driver->get_vblank_timestamp = drm_calc_vbltimestamp_from_scanoutpos;
4313f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
4314f71d4af4SJesse Barnes 
4315b963291cSDaniel Vetter 	if (IS_CHERRYVIEW(dev_priv)) {
431643f328d7SVille Syrjälä 		dev->driver->irq_handler = cherryview_irq_handler;
43176bcdb1c8SVille Syrjälä 		dev->driver->irq_preinstall = cherryview_irq_reset;
431843f328d7SVille Syrjälä 		dev->driver->irq_postinstall = cherryview_irq_postinstall;
43196bcdb1c8SVille Syrjälä 		dev->driver->irq_uninstall = cherryview_irq_reset;
432086e83e35SChris Wilson 		dev->driver->enable_vblank = i965_enable_vblank;
432186e83e35SChris Wilson 		dev->driver->disable_vblank = i965_disable_vblank;
432243f328d7SVille Syrjälä 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4323b963291cSDaniel Vetter 	} else if (IS_VALLEYVIEW(dev_priv)) {
43247e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
43256bcdb1c8SVille Syrjälä 		dev->driver->irq_preinstall = valleyview_irq_reset;
43267e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
43276bcdb1c8SVille Syrjälä 		dev->driver->irq_uninstall = valleyview_irq_reset;
432886e83e35SChris Wilson 		dev->driver->enable_vblank = i965_enable_vblank;
432986e83e35SChris Wilson 		dev->driver->disable_vblank = i965_disable_vblank;
4330fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
433151951ae7SMika Kuoppala 	} else if (INTEL_GEN(dev_priv) >= 11) {
433251951ae7SMika Kuoppala 		dev->driver->irq_handler = gen11_irq_handler;
433351951ae7SMika Kuoppala 		dev->driver->irq_preinstall = gen11_irq_reset;
433451951ae7SMika Kuoppala 		dev->driver->irq_postinstall = gen11_irq_postinstall;
433551951ae7SMika Kuoppala 		dev->driver->irq_uninstall = gen11_irq_reset;
433651951ae7SMika Kuoppala 		dev->driver->enable_vblank = gen8_enable_vblank;
433751951ae7SMika Kuoppala 		dev->driver->disable_vblank = gen8_disable_vblank;
433851951ae7SMika Kuoppala 		dev_priv->display.hpd_irq_setup = spt_hpd_irq_setup;
4339bca2bf2aSPandiyan, Dhinakaran 	} else if (INTEL_GEN(dev_priv) >= 8) {
4340abd58f01SBen Widawsky 		dev->driver->irq_handler = gen8_irq_handler;
4341723761b8SDaniel Vetter 		dev->driver->irq_preinstall = gen8_irq_reset;
4342abd58f01SBen Widawsky 		dev->driver->irq_postinstall = gen8_irq_postinstall;
43436bcdb1c8SVille Syrjälä 		dev->driver->irq_uninstall = gen8_irq_reset;
4344abd58f01SBen Widawsky 		dev->driver->enable_vblank = gen8_enable_vblank;
4345abd58f01SBen Widawsky 		dev->driver->disable_vblank = gen8_disable_vblank;
4346cc3f90f0SAnder Conselvan de Oliveira 		if (IS_GEN9_LP(dev_priv))
4347e0a20ad7SShashank Sharma 			dev_priv->display.hpd_irq_setup = bxt_hpd_irq_setup;
43487b22b8c4SRodrigo Vivi 		else if (HAS_PCH_SPT(dev_priv) || HAS_PCH_KBP(dev_priv) ||
43497b22b8c4SRodrigo Vivi 			 HAS_PCH_CNP(dev_priv))
43506dbf30ceSVille Syrjälä 			dev_priv->display.hpd_irq_setup = spt_hpd_irq_setup;
43516dbf30ceSVille Syrjälä 		else
43523a3b3c7dSVille Syrjälä 			dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
43536e266956STvrtko Ursulin 	} else if (HAS_PCH_SPLIT(dev_priv)) {
4354f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
4355723761b8SDaniel Vetter 		dev->driver->irq_preinstall = ironlake_irq_reset;
4356f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
43576bcdb1c8SVille Syrjälä 		dev->driver->irq_uninstall = ironlake_irq_reset;
4358f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
4359f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
4360e4ce95aaSVille Syrjälä 		dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
4361f71d4af4SJesse Barnes 	} else {
43627e22dbbbSTvrtko Ursulin 		if (IS_GEN2(dev_priv)) {
43636bcdb1c8SVille Syrjälä 			dev->driver->irq_preinstall = i8xx_irq_reset;
4364c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
4365c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
43666bcdb1c8SVille Syrjälä 			dev->driver->irq_uninstall = i8xx_irq_reset;
436786e83e35SChris Wilson 			dev->driver->enable_vblank = i8xx_enable_vblank;
436886e83e35SChris Wilson 			dev->driver->disable_vblank = i8xx_disable_vblank;
43697e22dbbbSTvrtko Ursulin 		} else if (IS_GEN3(dev_priv)) {
43706bcdb1c8SVille Syrjälä 			dev->driver->irq_preinstall = i915_irq_reset;
4371a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
43726bcdb1c8SVille Syrjälä 			dev->driver->irq_uninstall = i915_irq_reset;
4373a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
437486e83e35SChris Wilson 			dev->driver->enable_vblank = i8xx_enable_vblank;
437586e83e35SChris Wilson 			dev->driver->disable_vblank = i8xx_disable_vblank;
4376c2798b19SChris Wilson 		} else {
43776bcdb1c8SVille Syrjälä 			dev->driver->irq_preinstall = i965_irq_reset;
4378a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
43796bcdb1c8SVille Syrjälä 			dev->driver->irq_uninstall = i965_irq_reset;
4380a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
438186e83e35SChris Wilson 			dev->driver->enable_vblank = i965_enable_vblank;
438286e83e35SChris Wilson 			dev->driver->disable_vblank = i965_disable_vblank;
4383c2798b19SChris Wilson 		}
4384778eb334SVille Syrjälä 		if (I915_HAS_HOTPLUG(dev_priv))
4385778eb334SVille Syrjälä 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4386f71d4af4SJesse Barnes 	}
4387f71d4af4SJesse Barnes }
438820afbda2SDaniel Vetter 
4389fca52a55SDaniel Vetter /**
4390cefcff8fSJoonas Lahtinen  * intel_irq_fini - deinitializes IRQ support
4391cefcff8fSJoonas Lahtinen  * @i915: i915 device instance
4392cefcff8fSJoonas Lahtinen  *
4393cefcff8fSJoonas Lahtinen  * This function deinitializes all the IRQ support.
4394cefcff8fSJoonas Lahtinen  */
4395cefcff8fSJoonas Lahtinen void intel_irq_fini(struct drm_i915_private *i915)
4396cefcff8fSJoonas Lahtinen {
4397cefcff8fSJoonas Lahtinen 	int i;
4398cefcff8fSJoonas Lahtinen 
4399cefcff8fSJoonas Lahtinen 	for (i = 0; i < MAX_L3_SLICES; ++i)
4400cefcff8fSJoonas Lahtinen 		kfree(i915->l3_parity.remap_info[i]);
4401cefcff8fSJoonas Lahtinen }
4402cefcff8fSJoonas Lahtinen 
4403cefcff8fSJoonas Lahtinen /**
4404fca52a55SDaniel Vetter  * intel_irq_install - enables the hardware interrupt
4405fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4406fca52a55SDaniel Vetter  *
4407fca52a55SDaniel Vetter  * This function enables the hardware interrupt handling, but leaves the hotplug
4408fca52a55SDaniel Vetter  * handling still disabled. It is called after intel_irq_init().
4409fca52a55SDaniel Vetter  *
4410fca52a55SDaniel Vetter  * In the driver load and resume code we need working interrupts in a few places
4411fca52a55SDaniel Vetter  * but don't want to deal with the hassle of concurrent probe and hotplug
4412fca52a55SDaniel Vetter  * workers. Hence the split into this two-stage approach.
4413fca52a55SDaniel Vetter  */
44142aeb7d3aSDaniel Vetter int intel_irq_install(struct drm_i915_private *dev_priv)
44152aeb7d3aSDaniel Vetter {
44162aeb7d3aSDaniel Vetter 	/*
44172aeb7d3aSDaniel Vetter 	 * We enable some interrupt sources in our postinstall hooks, so mark
44182aeb7d3aSDaniel Vetter 	 * interrupts as enabled _before_ actually enabling them to avoid
44192aeb7d3aSDaniel Vetter 	 * special cases in our ordering checks.
44202aeb7d3aSDaniel Vetter 	 */
4421ad1443f0SSagar Arun Kamble 	dev_priv->runtime_pm.irqs_enabled = true;
44222aeb7d3aSDaniel Vetter 
442391c8a326SChris Wilson 	return drm_irq_install(&dev_priv->drm, dev_priv->drm.pdev->irq);
44242aeb7d3aSDaniel Vetter }
44252aeb7d3aSDaniel Vetter 
4426fca52a55SDaniel Vetter /**
4427fca52a55SDaniel Vetter  * intel_irq_uninstall - finilizes all irq handling
4428fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4429fca52a55SDaniel Vetter  *
4430fca52a55SDaniel Vetter  * This stops interrupt and hotplug handling and unregisters and frees all
4431fca52a55SDaniel Vetter  * resources acquired in the init functions.
4432fca52a55SDaniel Vetter  */
44332aeb7d3aSDaniel Vetter void intel_irq_uninstall(struct drm_i915_private *dev_priv)
44342aeb7d3aSDaniel Vetter {
443591c8a326SChris Wilson 	drm_irq_uninstall(&dev_priv->drm);
44362aeb7d3aSDaniel Vetter 	intel_hpd_cancel_work(dev_priv);
4437ad1443f0SSagar Arun Kamble 	dev_priv->runtime_pm.irqs_enabled = false;
44382aeb7d3aSDaniel Vetter }
44392aeb7d3aSDaniel Vetter 
4440fca52a55SDaniel Vetter /**
4441fca52a55SDaniel Vetter  * intel_runtime_pm_disable_interrupts - runtime interrupt disabling
4442fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4443fca52a55SDaniel Vetter  *
4444fca52a55SDaniel Vetter  * This function is used to disable interrupts at runtime, both in the runtime
4445fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4446fca52a55SDaniel Vetter  */
4447b963291cSDaniel Vetter void intel_runtime_pm_disable_interrupts(struct drm_i915_private *dev_priv)
4448c67a470bSPaulo Zanoni {
444991c8a326SChris Wilson 	dev_priv->drm.driver->irq_uninstall(&dev_priv->drm);
4450ad1443f0SSagar Arun Kamble 	dev_priv->runtime_pm.irqs_enabled = false;
445191c8a326SChris Wilson 	synchronize_irq(dev_priv->drm.irq);
4452c67a470bSPaulo Zanoni }
4453c67a470bSPaulo Zanoni 
4454fca52a55SDaniel Vetter /**
4455fca52a55SDaniel Vetter  * intel_runtime_pm_enable_interrupts - runtime interrupt enabling
4456fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4457fca52a55SDaniel Vetter  *
4458fca52a55SDaniel Vetter  * This function is used to enable interrupts at runtime, both in the runtime
4459fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4460fca52a55SDaniel Vetter  */
4461b963291cSDaniel Vetter void intel_runtime_pm_enable_interrupts(struct drm_i915_private *dev_priv)
4462c67a470bSPaulo Zanoni {
4463ad1443f0SSagar Arun Kamble 	dev_priv->runtime_pm.irqs_enabled = true;
446491c8a326SChris Wilson 	dev_priv->drm.driver->irq_preinstall(&dev_priv->drm);
446591c8a326SChris Wilson 	dev_priv->drm.driver->irq_postinstall(&dev_priv->drm);
4466c67a470bSPaulo Zanoni }
4467