xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision f3e304852242f5a5da6ab47798f4aca044786885)
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>
34fcd70cd3SDaniel Vetter #include <drm/drm_irq.h>
35fcd70cd3SDaniel Vetter #include <drm/drm_drv.h>
36760285e7SDavid Howells #include <drm/i915_drm.h>
37c0e09200SDave Airlie #include "i915_drv.h"
381c5d22f7SChris Wilson #include "i915_trace.h"
3979e53945SJesse Barnes #include "intel_drv.h"
40c0e09200SDave Airlie 
41fca52a55SDaniel Vetter /**
42fca52a55SDaniel Vetter  * DOC: interrupt handling
43fca52a55SDaniel Vetter  *
44fca52a55SDaniel Vetter  * These functions provide the basic support for enabling and disabling the
45fca52a55SDaniel Vetter  * interrupt handling support. There's a lot more functionality in i915_irq.c
46fca52a55SDaniel Vetter  * and related files, but that will be described in separate chapters.
47fca52a55SDaniel Vetter  */
48fca52a55SDaniel Vetter 
49e4ce95aaSVille Syrjälä static const u32 hpd_ilk[HPD_NUM_PINS] = {
50e4ce95aaSVille Syrjälä 	[HPD_PORT_A] = DE_DP_A_HOTPLUG,
51e4ce95aaSVille Syrjälä };
52e4ce95aaSVille Syrjälä 
5323bb4cb5SVille Syrjälä static const u32 hpd_ivb[HPD_NUM_PINS] = {
5423bb4cb5SVille Syrjälä 	[HPD_PORT_A] = DE_DP_A_HOTPLUG_IVB,
5523bb4cb5SVille Syrjälä };
5623bb4cb5SVille Syrjälä 
573a3b3c7dSVille Syrjälä static const u32 hpd_bdw[HPD_NUM_PINS] = {
583a3b3c7dSVille Syrjälä 	[HPD_PORT_A] = GEN8_PORT_DP_A_HOTPLUG,
593a3b3c7dSVille Syrjälä };
603a3b3c7dSVille Syrjälä 
617c7e10dbSVille Syrjälä static const u32 hpd_ibx[HPD_NUM_PINS] = {
62e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG,
63e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG,
64e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG,
65e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG,
66e5868a31SEgbert Eich 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG
67e5868a31SEgbert Eich };
68e5868a31SEgbert Eich 
697c7e10dbSVille Syrjälä static const u32 hpd_cpt[HPD_NUM_PINS] = {
70e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG_CPT,
7173c352a2SDaniel Vetter 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG_CPT,
72e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
73e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
74e5868a31SEgbert Eich 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT
75e5868a31SEgbert Eich };
76e5868a31SEgbert Eich 
7726951cafSXiong Zhang static const u32 hpd_spt[HPD_NUM_PINS] = {
7874c0b395SVille Syrjälä 	[HPD_PORT_A] = SDE_PORTA_HOTPLUG_SPT,
7926951cafSXiong Zhang 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
8026951cafSXiong Zhang 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
8126951cafSXiong Zhang 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT,
8226951cafSXiong Zhang 	[HPD_PORT_E] = SDE_PORTE_HOTPLUG_SPT
8326951cafSXiong Zhang };
8426951cafSXiong Zhang 
857c7e10dbSVille Syrjälä static const u32 hpd_mask_i915[HPD_NUM_PINS] = {
86e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_EN,
87e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_EN,
88e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_EN,
89e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_EN,
90e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_EN,
91e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_EN
92e5868a31SEgbert Eich };
93e5868a31SEgbert Eich 
947c7e10dbSVille Syrjälä static const u32 hpd_status_g4x[HPD_NUM_PINS] = {
95e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
96e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_G4X,
97e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_G4X,
98e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
99e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
100e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
101e5868a31SEgbert Eich };
102e5868a31SEgbert Eich 
1034bca26d0SVille Syrjälä static const u32 hpd_status_i915[HPD_NUM_PINS] = {
104e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
105e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_I915,
106e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_I915,
107e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
108e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
109e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
110e5868a31SEgbert Eich };
111e5868a31SEgbert Eich 
112e0a20ad7SShashank Sharma /* BXT hpd list */
113e0a20ad7SShashank Sharma static const u32 hpd_bxt[HPD_NUM_PINS] = {
1147f3561beSSonika Jindal 	[HPD_PORT_A] = BXT_DE_PORT_HP_DDIA,
115e0a20ad7SShashank Sharma 	[HPD_PORT_B] = BXT_DE_PORT_HP_DDIB,
116e0a20ad7SShashank Sharma 	[HPD_PORT_C] = BXT_DE_PORT_HP_DDIC
117e0a20ad7SShashank Sharma };
118e0a20ad7SShashank Sharma 
119b796b971SDhinakaran Pandiyan static const u32 hpd_gen11[HPD_NUM_PINS] = {
120b796b971SDhinakaran Pandiyan 	[HPD_PORT_C] = GEN11_TC1_HOTPLUG | GEN11_TBT1_HOTPLUG,
121b796b971SDhinakaran Pandiyan 	[HPD_PORT_D] = GEN11_TC2_HOTPLUG | GEN11_TBT2_HOTPLUG,
122b796b971SDhinakaran Pandiyan 	[HPD_PORT_E] = GEN11_TC3_HOTPLUG | GEN11_TBT3_HOTPLUG,
123b796b971SDhinakaran Pandiyan 	[HPD_PORT_F] = GEN11_TC4_HOTPLUG | GEN11_TBT4_HOTPLUG
124121e758eSDhinakaran Pandiyan };
125121e758eSDhinakaran Pandiyan 
12631604222SAnusha Srivatsa static const u32 hpd_icp[HPD_NUM_PINS] = {
12731604222SAnusha Srivatsa 	[HPD_PORT_A] = SDE_DDIA_HOTPLUG_ICP,
12831604222SAnusha Srivatsa 	[HPD_PORT_B] = SDE_DDIB_HOTPLUG_ICP,
12931604222SAnusha Srivatsa 	[HPD_PORT_C] = SDE_TC1_HOTPLUG_ICP,
13031604222SAnusha Srivatsa 	[HPD_PORT_D] = SDE_TC2_HOTPLUG_ICP,
13131604222SAnusha Srivatsa 	[HPD_PORT_E] = SDE_TC3_HOTPLUG_ICP,
13231604222SAnusha Srivatsa 	[HPD_PORT_F] = SDE_TC4_HOTPLUG_ICP
13331604222SAnusha Srivatsa };
13431604222SAnusha Srivatsa 
1355c502442SPaulo Zanoni /* IIR can theoretically queue up two events. Be paranoid. */
136f86f3fb0SPaulo Zanoni #define GEN8_IRQ_RESET_NDX(type, which) do { \
1375c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IMR(which), 0xffffffff); \
1385c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IMR(which)); \
1395c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IER(which), 0); \
1405c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
1415c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IIR(which)); \
1425c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
1435c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IIR(which)); \
1445c502442SPaulo Zanoni } while (0)
1455c502442SPaulo Zanoni 
1463488d4ebSVille Syrjälä #define GEN3_IRQ_RESET(type) do { \
147a9d356a6SPaulo Zanoni 	I915_WRITE(type##IMR, 0xffffffff); \
1485c502442SPaulo Zanoni 	POSTING_READ(type##IMR); \
149a9d356a6SPaulo Zanoni 	I915_WRITE(type##IER, 0); \
1505c502442SPaulo Zanoni 	I915_WRITE(type##IIR, 0xffffffff); \
1515c502442SPaulo Zanoni 	POSTING_READ(type##IIR); \
1525c502442SPaulo Zanoni 	I915_WRITE(type##IIR, 0xffffffff); \
1535c502442SPaulo Zanoni 	POSTING_READ(type##IIR); \
154a9d356a6SPaulo Zanoni } while (0)
155a9d356a6SPaulo Zanoni 
156e9e9848aSVille Syrjälä #define GEN2_IRQ_RESET(type) do { \
157e9e9848aSVille Syrjälä 	I915_WRITE16(type##IMR, 0xffff); \
158e9e9848aSVille Syrjälä 	POSTING_READ16(type##IMR); \
159e9e9848aSVille Syrjälä 	I915_WRITE16(type##IER, 0); \
160e9e9848aSVille Syrjälä 	I915_WRITE16(type##IIR, 0xffff); \
161e9e9848aSVille Syrjälä 	POSTING_READ16(type##IIR); \
162e9e9848aSVille Syrjälä 	I915_WRITE16(type##IIR, 0xffff); \
163e9e9848aSVille Syrjälä 	POSTING_READ16(type##IIR); \
164e9e9848aSVille Syrjälä } while (0)
165e9e9848aSVille Syrjälä 
166337ba017SPaulo Zanoni /*
167337ba017SPaulo Zanoni  * We should clear IMR at preinstall/uninstall, and just check at postinstall.
168337ba017SPaulo Zanoni  */
1693488d4ebSVille Syrjälä static void gen3_assert_iir_is_zero(struct drm_i915_private *dev_priv,
170f0f59a00SVille Syrjälä 				    i915_reg_t reg)
171b51a2842SVille Syrjälä {
172b51a2842SVille Syrjälä 	u32 val = I915_READ(reg);
173b51a2842SVille Syrjälä 
174b51a2842SVille Syrjälä 	if (val == 0)
175b51a2842SVille Syrjälä 		return;
176b51a2842SVille Syrjälä 
177b51a2842SVille Syrjälä 	WARN(1, "Interrupt register 0x%x is not zero: 0x%08x\n",
178f0f59a00SVille Syrjälä 	     i915_mmio_reg_offset(reg), val);
179b51a2842SVille Syrjälä 	I915_WRITE(reg, 0xffffffff);
180b51a2842SVille Syrjälä 	POSTING_READ(reg);
181b51a2842SVille Syrjälä 	I915_WRITE(reg, 0xffffffff);
182b51a2842SVille Syrjälä 	POSTING_READ(reg);
183b51a2842SVille Syrjälä }
184337ba017SPaulo Zanoni 
185e9e9848aSVille Syrjälä static void gen2_assert_iir_is_zero(struct drm_i915_private *dev_priv,
186e9e9848aSVille Syrjälä 				    i915_reg_t reg)
187e9e9848aSVille Syrjälä {
188e9e9848aSVille Syrjälä 	u16 val = I915_READ16(reg);
189e9e9848aSVille Syrjälä 
190e9e9848aSVille Syrjälä 	if (val == 0)
191e9e9848aSVille Syrjälä 		return;
192e9e9848aSVille Syrjälä 
193e9e9848aSVille Syrjälä 	WARN(1, "Interrupt register 0x%x is not zero: 0x%08x\n",
194e9e9848aSVille Syrjälä 	     i915_mmio_reg_offset(reg), val);
195e9e9848aSVille Syrjälä 	I915_WRITE16(reg, 0xffff);
196e9e9848aSVille Syrjälä 	POSTING_READ16(reg);
197e9e9848aSVille Syrjälä 	I915_WRITE16(reg, 0xffff);
198e9e9848aSVille Syrjälä 	POSTING_READ16(reg);
199e9e9848aSVille Syrjälä }
200e9e9848aSVille Syrjälä 
20135079899SPaulo Zanoni #define GEN8_IRQ_INIT_NDX(type, which, imr_val, ier_val) do { \
2023488d4ebSVille Syrjälä 	gen3_assert_iir_is_zero(dev_priv, GEN8_##type##_IIR(which)); \
20335079899SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IER(which), (ier_val)); \
2047d1bd539SVille Syrjälä 	I915_WRITE(GEN8_##type##_IMR(which), (imr_val)); \
2057d1bd539SVille Syrjälä 	POSTING_READ(GEN8_##type##_IMR(which)); \
20635079899SPaulo Zanoni } while (0)
20735079899SPaulo Zanoni 
2083488d4ebSVille Syrjälä #define GEN3_IRQ_INIT(type, imr_val, ier_val) do { \
2093488d4ebSVille Syrjälä 	gen3_assert_iir_is_zero(dev_priv, type##IIR); \
21035079899SPaulo Zanoni 	I915_WRITE(type##IER, (ier_val)); \
2117d1bd539SVille Syrjälä 	I915_WRITE(type##IMR, (imr_val)); \
2127d1bd539SVille Syrjälä 	POSTING_READ(type##IMR); \
21335079899SPaulo Zanoni } while (0)
21435079899SPaulo Zanoni 
215e9e9848aSVille Syrjälä #define GEN2_IRQ_INIT(type, imr_val, ier_val) do { \
216e9e9848aSVille Syrjälä 	gen2_assert_iir_is_zero(dev_priv, type##IIR); \
217e9e9848aSVille Syrjälä 	I915_WRITE16(type##IER, (ier_val)); \
218e9e9848aSVille Syrjälä 	I915_WRITE16(type##IMR, (imr_val)); \
219e9e9848aSVille Syrjälä 	POSTING_READ16(type##IMR); \
220e9e9848aSVille Syrjälä } while (0)
221e9e9848aSVille Syrjälä 
222c9a9a268SImre Deak static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir);
22326705e20SSagar Arun Kamble static void gen9_guc_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir);
224c9a9a268SImre Deak 
2250706f17cSEgbert Eich /* For display hotplug interrupt */
2260706f17cSEgbert Eich static inline void
2270706f17cSEgbert Eich i915_hotplug_interrupt_update_locked(struct drm_i915_private *dev_priv,
228a9c287c9SJani Nikula 				     u32 mask,
229a9c287c9SJani Nikula 				     u32 bits)
2300706f17cSEgbert Eich {
231a9c287c9SJani Nikula 	u32 val;
2320706f17cSEgbert Eich 
23367520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
2340706f17cSEgbert Eich 	WARN_ON(bits & ~mask);
2350706f17cSEgbert Eich 
2360706f17cSEgbert Eich 	val = I915_READ(PORT_HOTPLUG_EN);
2370706f17cSEgbert Eich 	val &= ~mask;
2380706f17cSEgbert Eich 	val |= bits;
2390706f17cSEgbert Eich 	I915_WRITE(PORT_HOTPLUG_EN, val);
2400706f17cSEgbert Eich }
2410706f17cSEgbert Eich 
2420706f17cSEgbert Eich /**
2430706f17cSEgbert Eich  * i915_hotplug_interrupt_update - update hotplug interrupt enable
2440706f17cSEgbert Eich  * @dev_priv: driver private
2450706f17cSEgbert Eich  * @mask: bits to update
2460706f17cSEgbert Eich  * @bits: bits to enable
2470706f17cSEgbert Eich  * NOTE: the HPD enable bits are modified both inside and outside
2480706f17cSEgbert Eich  * of an interrupt context. To avoid that read-modify-write cycles
2490706f17cSEgbert Eich  * interfer, these bits are protected by a spinlock. Since this
2500706f17cSEgbert Eich  * function is usually not called from a context where the lock is
2510706f17cSEgbert Eich  * held already, this function acquires the lock itself. A non-locking
2520706f17cSEgbert Eich  * version is also available.
2530706f17cSEgbert Eich  */
2540706f17cSEgbert Eich void i915_hotplug_interrupt_update(struct drm_i915_private *dev_priv,
255a9c287c9SJani Nikula 				   u32 mask,
256a9c287c9SJani Nikula 				   u32 bits)
2570706f17cSEgbert Eich {
2580706f17cSEgbert Eich 	spin_lock_irq(&dev_priv->irq_lock);
2590706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv, mask, bits);
2600706f17cSEgbert Eich 	spin_unlock_irq(&dev_priv->irq_lock);
2610706f17cSEgbert Eich }
2620706f17cSEgbert Eich 
26396606f3bSOscar Mateo static u32
26496606f3bSOscar Mateo gen11_gt_engine_identity(struct drm_i915_private * const i915,
26596606f3bSOscar Mateo 			 const unsigned int bank, const unsigned int bit);
26696606f3bSOscar Mateo 
26760a94324SChris Wilson static bool gen11_reset_one_iir(struct drm_i915_private * const i915,
26896606f3bSOscar Mateo 				const unsigned int bank,
26996606f3bSOscar Mateo 				const unsigned int bit)
27096606f3bSOscar Mateo {
27196606f3bSOscar Mateo 	void __iomem * const regs = i915->regs;
27296606f3bSOscar Mateo 	u32 dw;
27396606f3bSOscar Mateo 
27496606f3bSOscar Mateo 	lockdep_assert_held(&i915->irq_lock);
27596606f3bSOscar Mateo 
27696606f3bSOscar Mateo 	dw = raw_reg_read(regs, GEN11_GT_INTR_DW(bank));
27796606f3bSOscar Mateo 	if (dw & BIT(bit)) {
27896606f3bSOscar Mateo 		/*
27996606f3bSOscar Mateo 		 * According to the BSpec, DW_IIR bits cannot be cleared without
28096606f3bSOscar Mateo 		 * first servicing the Selector & Shared IIR registers.
28196606f3bSOscar Mateo 		 */
28296606f3bSOscar Mateo 		gen11_gt_engine_identity(i915, bank, bit);
28396606f3bSOscar Mateo 
28496606f3bSOscar Mateo 		/*
28596606f3bSOscar Mateo 		 * We locked GT INT DW by reading it. If we want to (try
28696606f3bSOscar Mateo 		 * to) recover from this succesfully, we need to clear
28796606f3bSOscar Mateo 		 * our bit, otherwise we are locking the register for
28896606f3bSOscar Mateo 		 * everybody.
28996606f3bSOscar Mateo 		 */
29096606f3bSOscar Mateo 		raw_reg_write(regs, GEN11_GT_INTR_DW(bank), BIT(bit));
29196606f3bSOscar Mateo 
29296606f3bSOscar Mateo 		return true;
29396606f3bSOscar Mateo 	}
29496606f3bSOscar Mateo 
29596606f3bSOscar Mateo 	return false;
29696606f3bSOscar Mateo }
29796606f3bSOscar Mateo 
298d9dc34f1SVille Syrjälä /**
299d9dc34f1SVille Syrjälä  * ilk_update_display_irq - update DEIMR
300d9dc34f1SVille Syrjälä  * @dev_priv: driver private
301d9dc34f1SVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
302d9dc34f1SVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
303d9dc34f1SVille Syrjälä  */
304fbdedaeaSVille Syrjälä void ilk_update_display_irq(struct drm_i915_private *dev_priv,
305a9c287c9SJani Nikula 			    u32 interrupt_mask,
306a9c287c9SJani Nikula 			    u32 enabled_irq_mask)
307036a4a7dSZhenyu Wang {
308a9c287c9SJani Nikula 	u32 new_val;
309d9dc34f1SVille Syrjälä 
31067520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
3114bc9d430SDaniel Vetter 
312d9dc34f1SVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
313d9dc34f1SVille Syrjälä 
3149df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
315c67a470bSPaulo Zanoni 		return;
316c67a470bSPaulo Zanoni 
317d9dc34f1SVille Syrjälä 	new_val = dev_priv->irq_mask;
318d9dc34f1SVille Syrjälä 	new_val &= ~interrupt_mask;
319d9dc34f1SVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
320d9dc34f1SVille Syrjälä 
321d9dc34f1SVille Syrjälä 	if (new_val != dev_priv->irq_mask) {
322d9dc34f1SVille Syrjälä 		dev_priv->irq_mask = new_val;
3231ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
3243143a2bfSChris Wilson 		POSTING_READ(DEIMR);
325036a4a7dSZhenyu Wang 	}
326036a4a7dSZhenyu Wang }
327036a4a7dSZhenyu Wang 
32843eaea13SPaulo Zanoni /**
32943eaea13SPaulo Zanoni  * ilk_update_gt_irq - update GTIMR
33043eaea13SPaulo Zanoni  * @dev_priv: driver private
33143eaea13SPaulo Zanoni  * @interrupt_mask: mask of interrupt bits to update
33243eaea13SPaulo Zanoni  * @enabled_irq_mask: mask of interrupt bits to enable
33343eaea13SPaulo Zanoni  */
33443eaea13SPaulo Zanoni static void ilk_update_gt_irq(struct drm_i915_private *dev_priv,
335a9c287c9SJani Nikula 			      u32 interrupt_mask,
336a9c287c9SJani Nikula 			      u32 enabled_irq_mask)
33743eaea13SPaulo Zanoni {
33867520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
33943eaea13SPaulo Zanoni 
34015a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
34115a17aaeSDaniel Vetter 
3429df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
343c67a470bSPaulo Zanoni 		return;
344c67a470bSPaulo Zanoni 
34543eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask &= ~interrupt_mask;
34643eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask |= (~enabled_irq_mask & interrupt_mask);
34743eaea13SPaulo Zanoni 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
34843eaea13SPaulo Zanoni }
34943eaea13SPaulo Zanoni 
350a9c287c9SJani Nikula void gen5_enable_gt_irq(struct drm_i915_private *dev_priv, u32 mask)
35143eaea13SPaulo Zanoni {
35243eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, mask);
35331bb59ccSChris Wilson 	POSTING_READ_FW(GTIMR);
35443eaea13SPaulo Zanoni }
35543eaea13SPaulo Zanoni 
356a9c287c9SJani Nikula void gen5_disable_gt_irq(struct drm_i915_private *dev_priv, u32 mask)
35743eaea13SPaulo Zanoni {
35843eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, 0);
35943eaea13SPaulo Zanoni }
36043eaea13SPaulo Zanoni 
361f0f59a00SVille Syrjälä static i915_reg_t gen6_pm_iir(struct drm_i915_private *dev_priv)
362b900b949SImre Deak {
363d02b98b8SOscar Mateo 	WARN_ON_ONCE(INTEL_GEN(dev_priv) >= 11);
364d02b98b8SOscar Mateo 
365bca2bf2aSPandiyan, Dhinakaran 	return INTEL_GEN(dev_priv) >= 8 ? GEN8_GT_IIR(2) : GEN6_PMIIR;
366b900b949SImre Deak }
367b900b949SImre Deak 
368f0f59a00SVille Syrjälä static i915_reg_t gen6_pm_imr(struct drm_i915_private *dev_priv)
369a72fbc3aSImre Deak {
370d02b98b8SOscar Mateo 	if (INTEL_GEN(dev_priv) >= 11)
371d02b98b8SOscar Mateo 		return GEN11_GPM_WGBOXPERF_INTR_MASK;
372d02b98b8SOscar Mateo 	else if (INTEL_GEN(dev_priv) >= 8)
373d02b98b8SOscar Mateo 		return GEN8_GT_IMR(2);
374d02b98b8SOscar Mateo 	else
375d02b98b8SOscar Mateo 		return GEN6_PMIMR;
376a72fbc3aSImre Deak }
377a72fbc3aSImre Deak 
378f0f59a00SVille Syrjälä static i915_reg_t gen6_pm_ier(struct drm_i915_private *dev_priv)
379b900b949SImre Deak {
380d02b98b8SOscar Mateo 	if (INTEL_GEN(dev_priv) >= 11)
381d02b98b8SOscar Mateo 		return GEN11_GPM_WGBOXPERF_INTR_ENABLE;
382d02b98b8SOscar Mateo 	else if (INTEL_GEN(dev_priv) >= 8)
383d02b98b8SOscar Mateo 		return GEN8_GT_IER(2);
384d02b98b8SOscar Mateo 	else
385d02b98b8SOscar Mateo 		return GEN6_PMIER;
386b900b949SImre Deak }
387b900b949SImre Deak 
388edbfdb45SPaulo Zanoni /**
389edbfdb45SPaulo Zanoni  * snb_update_pm_irq - update GEN6_PMIMR
390edbfdb45SPaulo Zanoni  * @dev_priv: driver private
391edbfdb45SPaulo Zanoni  * @interrupt_mask: mask of interrupt bits to update
392edbfdb45SPaulo Zanoni  * @enabled_irq_mask: mask of interrupt bits to enable
393edbfdb45SPaulo Zanoni  */
394edbfdb45SPaulo Zanoni static void snb_update_pm_irq(struct drm_i915_private *dev_priv,
395a9c287c9SJani Nikula 			      u32 interrupt_mask,
396a9c287c9SJani Nikula 			      u32 enabled_irq_mask)
397edbfdb45SPaulo Zanoni {
398a9c287c9SJani Nikula 	u32 new_val;
399edbfdb45SPaulo Zanoni 
40015a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
40115a17aaeSDaniel Vetter 
40267520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
403edbfdb45SPaulo Zanoni 
404f4e9af4fSAkash Goel 	new_val = dev_priv->pm_imr;
405f52ecbcfSPaulo Zanoni 	new_val &= ~interrupt_mask;
406f52ecbcfSPaulo Zanoni 	new_val |= (~enabled_irq_mask & interrupt_mask);
407f52ecbcfSPaulo Zanoni 
408f4e9af4fSAkash Goel 	if (new_val != dev_priv->pm_imr) {
409f4e9af4fSAkash Goel 		dev_priv->pm_imr = new_val;
410f4e9af4fSAkash Goel 		I915_WRITE(gen6_pm_imr(dev_priv), dev_priv->pm_imr);
411a72fbc3aSImre Deak 		POSTING_READ(gen6_pm_imr(dev_priv));
412edbfdb45SPaulo Zanoni 	}
413f52ecbcfSPaulo Zanoni }
414edbfdb45SPaulo Zanoni 
415f4e9af4fSAkash Goel void gen6_unmask_pm_irq(struct drm_i915_private *dev_priv, u32 mask)
416edbfdb45SPaulo Zanoni {
4179939fba2SImre Deak 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
4189939fba2SImre Deak 		return;
4199939fba2SImre Deak 
420edbfdb45SPaulo Zanoni 	snb_update_pm_irq(dev_priv, mask, mask);
421edbfdb45SPaulo Zanoni }
422edbfdb45SPaulo Zanoni 
423f4e9af4fSAkash Goel static void __gen6_mask_pm_irq(struct drm_i915_private *dev_priv, u32 mask)
4249939fba2SImre Deak {
4259939fba2SImre Deak 	snb_update_pm_irq(dev_priv, mask, 0);
4269939fba2SImre Deak }
4279939fba2SImre Deak 
428f4e9af4fSAkash Goel void gen6_mask_pm_irq(struct drm_i915_private *dev_priv, u32 mask)
429edbfdb45SPaulo Zanoni {
4309939fba2SImre Deak 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
4319939fba2SImre Deak 		return;
4329939fba2SImre Deak 
433f4e9af4fSAkash Goel 	__gen6_mask_pm_irq(dev_priv, mask);
434f4e9af4fSAkash Goel }
435f4e9af4fSAkash Goel 
4363814fd77SOscar Mateo static void gen6_reset_pm_iir(struct drm_i915_private *dev_priv, u32 reset_mask)
437f4e9af4fSAkash Goel {
438f4e9af4fSAkash Goel 	i915_reg_t reg = gen6_pm_iir(dev_priv);
439f4e9af4fSAkash Goel 
44067520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
441f4e9af4fSAkash Goel 
442f4e9af4fSAkash Goel 	I915_WRITE(reg, reset_mask);
443f4e9af4fSAkash Goel 	I915_WRITE(reg, reset_mask);
444f4e9af4fSAkash Goel 	POSTING_READ(reg);
445f4e9af4fSAkash Goel }
446f4e9af4fSAkash Goel 
4473814fd77SOscar Mateo static void gen6_enable_pm_irq(struct drm_i915_private *dev_priv, u32 enable_mask)
448f4e9af4fSAkash Goel {
44967520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
450f4e9af4fSAkash Goel 
451f4e9af4fSAkash Goel 	dev_priv->pm_ier |= enable_mask;
452f4e9af4fSAkash Goel 	I915_WRITE(gen6_pm_ier(dev_priv), dev_priv->pm_ier);
453f4e9af4fSAkash Goel 	gen6_unmask_pm_irq(dev_priv, enable_mask);
454f4e9af4fSAkash Goel 	/* unmask_pm_irq provides an implicit barrier (POSTING_READ) */
455f4e9af4fSAkash Goel }
456f4e9af4fSAkash Goel 
4573814fd77SOscar Mateo static void gen6_disable_pm_irq(struct drm_i915_private *dev_priv, u32 disable_mask)
458f4e9af4fSAkash Goel {
45967520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
460f4e9af4fSAkash Goel 
461f4e9af4fSAkash Goel 	dev_priv->pm_ier &= ~disable_mask;
462f4e9af4fSAkash Goel 	__gen6_mask_pm_irq(dev_priv, disable_mask);
463f4e9af4fSAkash Goel 	I915_WRITE(gen6_pm_ier(dev_priv), dev_priv->pm_ier);
464f4e9af4fSAkash Goel 	/* though a barrier is missing here, but don't really need a one */
465edbfdb45SPaulo Zanoni }
466edbfdb45SPaulo Zanoni 
467d02b98b8SOscar Mateo void gen11_reset_rps_interrupts(struct drm_i915_private *dev_priv)
468d02b98b8SOscar Mateo {
469d02b98b8SOscar Mateo 	spin_lock_irq(&dev_priv->irq_lock);
470d02b98b8SOscar Mateo 
47196606f3bSOscar Mateo 	while (gen11_reset_one_iir(dev_priv, 0, GEN11_GTPM))
47296606f3bSOscar Mateo 		;
473d02b98b8SOscar Mateo 
474d02b98b8SOscar Mateo 	dev_priv->gt_pm.rps.pm_iir = 0;
475d02b98b8SOscar Mateo 
476d02b98b8SOscar Mateo 	spin_unlock_irq(&dev_priv->irq_lock);
477d02b98b8SOscar Mateo }
478d02b98b8SOscar Mateo 
479dc97997aSChris Wilson void gen6_reset_rps_interrupts(struct drm_i915_private *dev_priv)
4803cc134e3SImre Deak {
4813cc134e3SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
4824668f695SChris Wilson 	gen6_reset_pm_iir(dev_priv, GEN6_PM_RPS_EVENTS);
483562d9baeSSagar Arun Kamble 	dev_priv->gt_pm.rps.pm_iir = 0;
4843cc134e3SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
4853cc134e3SImre Deak }
4863cc134e3SImre Deak 
48791d14251STvrtko Ursulin void gen6_enable_rps_interrupts(struct drm_i915_private *dev_priv)
488b900b949SImre Deak {
489562d9baeSSagar Arun Kamble 	struct intel_rps *rps = &dev_priv->gt_pm.rps;
490562d9baeSSagar Arun Kamble 
491562d9baeSSagar Arun Kamble 	if (READ_ONCE(rps->interrupts_enabled))
492f2a91d1aSChris Wilson 		return;
493f2a91d1aSChris Wilson 
494b900b949SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
495562d9baeSSagar Arun Kamble 	WARN_ON_ONCE(rps->pm_iir);
49696606f3bSOscar Mateo 
497d02b98b8SOscar Mateo 	if (INTEL_GEN(dev_priv) >= 11)
49896606f3bSOscar Mateo 		WARN_ON_ONCE(gen11_reset_one_iir(dev_priv, 0, GEN11_GTPM));
499d02b98b8SOscar Mateo 	else
500c33d247dSChris Wilson 		WARN_ON_ONCE(I915_READ(gen6_pm_iir(dev_priv)) & dev_priv->pm_rps_events);
50196606f3bSOscar Mateo 
502562d9baeSSagar Arun Kamble 	rps->interrupts_enabled = true;
503b900b949SImre Deak 	gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
50478e68d36SImre Deak 
505b900b949SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
506b900b949SImre Deak }
507b900b949SImre Deak 
50891d14251STvrtko Ursulin void gen6_disable_rps_interrupts(struct drm_i915_private *dev_priv)
509b900b949SImre Deak {
510562d9baeSSagar Arun Kamble 	struct intel_rps *rps = &dev_priv->gt_pm.rps;
511562d9baeSSagar Arun Kamble 
512562d9baeSSagar Arun Kamble 	if (!READ_ONCE(rps->interrupts_enabled))
513f2a91d1aSChris Wilson 		return;
514f2a91d1aSChris Wilson 
515d4d70aa5SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
516562d9baeSSagar Arun Kamble 	rps->interrupts_enabled = false;
5179939fba2SImre Deak 
518b20e3cfeSDave Gordon 	I915_WRITE(GEN6_PMINTRMSK, gen6_sanitize_rps_pm_mask(dev_priv, ~0u));
5199939fba2SImre Deak 
5204668f695SChris Wilson 	gen6_disable_pm_irq(dev_priv, GEN6_PM_RPS_EVENTS);
52158072ccbSImre Deak 
52258072ccbSImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
52391c8a326SChris Wilson 	synchronize_irq(dev_priv->drm.irq);
524c33d247dSChris Wilson 
525c33d247dSChris Wilson 	/* Now that we will not be generating any more work, flush any
5263814fd77SOscar Mateo 	 * outstanding tasks. As we are called on the RPS idle path,
527c33d247dSChris Wilson 	 * we will reset the GPU to minimum frequencies, so the current
528c33d247dSChris Wilson 	 * state of the worker can be discarded.
529c33d247dSChris Wilson 	 */
530562d9baeSSagar Arun Kamble 	cancel_work_sync(&rps->work);
531d02b98b8SOscar Mateo 	if (INTEL_GEN(dev_priv) >= 11)
532d02b98b8SOscar Mateo 		gen11_reset_rps_interrupts(dev_priv);
533d02b98b8SOscar Mateo 	else
534c33d247dSChris Wilson 		gen6_reset_rps_interrupts(dev_priv);
535b900b949SImre Deak }
536b900b949SImre Deak 
53726705e20SSagar Arun Kamble void gen9_reset_guc_interrupts(struct drm_i915_private *dev_priv)
53826705e20SSagar Arun Kamble {
5391be333d3SSagar Arun Kamble 	assert_rpm_wakelock_held(dev_priv);
5401be333d3SSagar Arun Kamble 
54126705e20SSagar Arun Kamble 	spin_lock_irq(&dev_priv->irq_lock);
54226705e20SSagar Arun Kamble 	gen6_reset_pm_iir(dev_priv, dev_priv->pm_guc_events);
54326705e20SSagar Arun Kamble 	spin_unlock_irq(&dev_priv->irq_lock);
54426705e20SSagar Arun Kamble }
54526705e20SSagar Arun Kamble 
54626705e20SSagar Arun Kamble void gen9_enable_guc_interrupts(struct drm_i915_private *dev_priv)
54726705e20SSagar Arun Kamble {
5481be333d3SSagar Arun Kamble 	assert_rpm_wakelock_held(dev_priv);
5491be333d3SSagar Arun Kamble 
55026705e20SSagar Arun Kamble 	spin_lock_irq(&dev_priv->irq_lock);
55126705e20SSagar Arun Kamble 	if (!dev_priv->guc.interrupts_enabled) {
55226705e20SSagar Arun Kamble 		WARN_ON_ONCE(I915_READ(gen6_pm_iir(dev_priv)) &
55326705e20SSagar Arun Kamble 				       dev_priv->pm_guc_events);
55426705e20SSagar Arun Kamble 		dev_priv->guc.interrupts_enabled = true;
55526705e20SSagar Arun Kamble 		gen6_enable_pm_irq(dev_priv, dev_priv->pm_guc_events);
55626705e20SSagar Arun Kamble 	}
55726705e20SSagar Arun Kamble 	spin_unlock_irq(&dev_priv->irq_lock);
55826705e20SSagar Arun Kamble }
55926705e20SSagar Arun Kamble 
56026705e20SSagar Arun Kamble void gen9_disable_guc_interrupts(struct drm_i915_private *dev_priv)
56126705e20SSagar Arun Kamble {
5621be333d3SSagar Arun Kamble 	assert_rpm_wakelock_held(dev_priv);
5631be333d3SSagar Arun Kamble 
56426705e20SSagar Arun Kamble 	spin_lock_irq(&dev_priv->irq_lock);
56526705e20SSagar Arun Kamble 	dev_priv->guc.interrupts_enabled = false;
56626705e20SSagar Arun Kamble 
56726705e20SSagar Arun Kamble 	gen6_disable_pm_irq(dev_priv, dev_priv->pm_guc_events);
56826705e20SSagar Arun Kamble 
56926705e20SSagar Arun Kamble 	spin_unlock_irq(&dev_priv->irq_lock);
57026705e20SSagar Arun Kamble 	synchronize_irq(dev_priv->drm.irq);
57126705e20SSagar Arun Kamble 
57226705e20SSagar Arun Kamble 	gen9_reset_guc_interrupts(dev_priv);
57326705e20SSagar Arun Kamble }
57426705e20SSagar Arun Kamble 
5750961021aSBen Widawsky /**
5763a3b3c7dSVille Syrjälä  * bdw_update_port_irq - update DE port interrupt
5773a3b3c7dSVille Syrjälä  * @dev_priv: driver private
5783a3b3c7dSVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
5793a3b3c7dSVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
5803a3b3c7dSVille Syrjälä  */
5813a3b3c7dSVille Syrjälä static void bdw_update_port_irq(struct drm_i915_private *dev_priv,
582a9c287c9SJani Nikula 				u32 interrupt_mask,
583a9c287c9SJani Nikula 				u32 enabled_irq_mask)
5843a3b3c7dSVille Syrjälä {
585a9c287c9SJani Nikula 	u32 new_val;
586a9c287c9SJani Nikula 	u32 old_val;
5873a3b3c7dSVille Syrjälä 
58867520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
5893a3b3c7dSVille Syrjälä 
5903a3b3c7dSVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
5913a3b3c7dSVille Syrjälä 
5923a3b3c7dSVille Syrjälä 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
5933a3b3c7dSVille Syrjälä 		return;
5943a3b3c7dSVille Syrjälä 
5953a3b3c7dSVille Syrjälä 	old_val = I915_READ(GEN8_DE_PORT_IMR);
5963a3b3c7dSVille Syrjälä 
5973a3b3c7dSVille Syrjälä 	new_val = old_val;
5983a3b3c7dSVille Syrjälä 	new_val &= ~interrupt_mask;
5993a3b3c7dSVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
6003a3b3c7dSVille Syrjälä 
6013a3b3c7dSVille Syrjälä 	if (new_val != old_val) {
6023a3b3c7dSVille Syrjälä 		I915_WRITE(GEN8_DE_PORT_IMR, new_val);
6033a3b3c7dSVille Syrjälä 		POSTING_READ(GEN8_DE_PORT_IMR);
6043a3b3c7dSVille Syrjälä 	}
6053a3b3c7dSVille Syrjälä }
6063a3b3c7dSVille Syrjälä 
6073a3b3c7dSVille Syrjälä /**
608013d3752SVille Syrjälä  * bdw_update_pipe_irq - update DE pipe interrupt
609013d3752SVille Syrjälä  * @dev_priv: driver private
610013d3752SVille Syrjälä  * @pipe: pipe whose interrupt to update
611013d3752SVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
612013d3752SVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
613013d3752SVille Syrjälä  */
614013d3752SVille Syrjälä void bdw_update_pipe_irq(struct drm_i915_private *dev_priv,
615013d3752SVille Syrjälä 			 enum pipe pipe,
616a9c287c9SJani Nikula 			 u32 interrupt_mask,
617a9c287c9SJani Nikula 			 u32 enabled_irq_mask)
618013d3752SVille Syrjälä {
619a9c287c9SJani Nikula 	u32 new_val;
620013d3752SVille Syrjälä 
62167520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
622013d3752SVille Syrjälä 
623013d3752SVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
624013d3752SVille Syrjälä 
625013d3752SVille Syrjälä 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
626013d3752SVille Syrjälä 		return;
627013d3752SVille Syrjälä 
628013d3752SVille Syrjälä 	new_val = dev_priv->de_irq_mask[pipe];
629013d3752SVille Syrjälä 	new_val &= ~interrupt_mask;
630013d3752SVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
631013d3752SVille Syrjälä 
632013d3752SVille Syrjälä 	if (new_val != dev_priv->de_irq_mask[pipe]) {
633013d3752SVille Syrjälä 		dev_priv->de_irq_mask[pipe] = new_val;
634013d3752SVille Syrjälä 		I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
635013d3752SVille Syrjälä 		POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
636013d3752SVille Syrjälä 	}
637013d3752SVille Syrjälä }
638013d3752SVille Syrjälä 
639013d3752SVille Syrjälä /**
640fee884edSDaniel Vetter  * ibx_display_interrupt_update - update SDEIMR
641fee884edSDaniel Vetter  * @dev_priv: driver private
642fee884edSDaniel Vetter  * @interrupt_mask: mask of interrupt bits to update
643fee884edSDaniel Vetter  * @enabled_irq_mask: mask of interrupt bits to enable
644fee884edSDaniel Vetter  */
64547339cd9SDaniel Vetter void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
646a9c287c9SJani Nikula 				  u32 interrupt_mask,
647a9c287c9SJani Nikula 				  u32 enabled_irq_mask)
648fee884edSDaniel Vetter {
649a9c287c9SJani Nikula 	u32 sdeimr = I915_READ(SDEIMR);
650fee884edSDaniel Vetter 	sdeimr &= ~interrupt_mask;
651fee884edSDaniel Vetter 	sdeimr |= (~enabled_irq_mask & interrupt_mask);
652fee884edSDaniel Vetter 
65315a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
65415a17aaeSDaniel Vetter 
65567520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
656fee884edSDaniel Vetter 
6579df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
658c67a470bSPaulo Zanoni 		return;
659c67a470bSPaulo Zanoni 
660fee884edSDaniel Vetter 	I915_WRITE(SDEIMR, sdeimr);
661fee884edSDaniel Vetter 	POSTING_READ(SDEIMR);
662fee884edSDaniel Vetter }
6638664281bSPaulo Zanoni 
6646b12ca56SVille Syrjälä u32 i915_pipestat_enable_mask(struct drm_i915_private *dev_priv,
6656b12ca56SVille Syrjälä 			      enum pipe pipe)
6667c463586SKeith Packard {
6676b12ca56SVille Syrjälä 	u32 status_mask = dev_priv->pipestat_irq_mask[pipe];
66810c59c51SImre Deak 	u32 enable_mask = status_mask << 16;
66910c59c51SImre Deak 
6706b12ca56SVille Syrjälä 	lockdep_assert_held(&dev_priv->irq_lock);
6716b12ca56SVille Syrjälä 
6726b12ca56SVille Syrjälä 	if (INTEL_GEN(dev_priv) < 5)
6736b12ca56SVille Syrjälä 		goto out;
6746b12ca56SVille Syrjälä 
67510c59c51SImre Deak 	/*
676724a6905SVille Syrjälä 	 * On pipe A we don't support the PSR interrupt yet,
677724a6905SVille Syrjälä 	 * on pipe B and C the same bit MBZ.
67810c59c51SImre Deak 	 */
67910c59c51SImre Deak 	if (WARN_ON_ONCE(status_mask & PIPE_A_PSR_STATUS_VLV))
68010c59c51SImre Deak 		return 0;
681724a6905SVille Syrjälä 	/*
682724a6905SVille Syrjälä 	 * On pipe B and C we don't support the PSR interrupt yet, on pipe
683724a6905SVille Syrjälä 	 * A the same bit is for perf counters which we don't use either.
684724a6905SVille Syrjälä 	 */
685724a6905SVille Syrjälä 	if (WARN_ON_ONCE(status_mask & PIPE_B_PSR_STATUS_VLV))
686724a6905SVille Syrjälä 		return 0;
68710c59c51SImre Deak 
68810c59c51SImre Deak 	enable_mask &= ~(PIPE_FIFO_UNDERRUN_STATUS |
68910c59c51SImre Deak 			 SPRITE0_FLIP_DONE_INT_EN_VLV |
69010c59c51SImre Deak 			 SPRITE1_FLIP_DONE_INT_EN_VLV);
69110c59c51SImre Deak 	if (status_mask & SPRITE0_FLIP_DONE_INT_STATUS_VLV)
69210c59c51SImre Deak 		enable_mask |= SPRITE0_FLIP_DONE_INT_EN_VLV;
69310c59c51SImre Deak 	if (status_mask & SPRITE1_FLIP_DONE_INT_STATUS_VLV)
69410c59c51SImre Deak 		enable_mask |= SPRITE1_FLIP_DONE_INT_EN_VLV;
69510c59c51SImre Deak 
6966b12ca56SVille Syrjälä out:
6976b12ca56SVille Syrjälä 	WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
6986b12ca56SVille Syrjälä 		  status_mask & ~PIPESTAT_INT_STATUS_MASK,
6996b12ca56SVille Syrjälä 		  "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
7006b12ca56SVille Syrjälä 		  pipe_name(pipe), enable_mask, status_mask);
7016b12ca56SVille Syrjälä 
70210c59c51SImre Deak 	return enable_mask;
70310c59c51SImre Deak }
70410c59c51SImre Deak 
7056b12ca56SVille Syrjälä void i915_enable_pipestat(struct drm_i915_private *dev_priv,
7066b12ca56SVille Syrjälä 			  enum pipe pipe, u32 status_mask)
707755e9019SImre Deak {
7086b12ca56SVille Syrjälä 	i915_reg_t reg = PIPESTAT(pipe);
709755e9019SImre Deak 	u32 enable_mask;
710755e9019SImre Deak 
7116b12ca56SVille Syrjälä 	WARN_ONCE(status_mask & ~PIPESTAT_INT_STATUS_MASK,
7126b12ca56SVille Syrjälä 		  "pipe %c: status_mask=0x%x\n",
7136b12ca56SVille Syrjälä 		  pipe_name(pipe), status_mask);
7146b12ca56SVille Syrjälä 
7156b12ca56SVille Syrjälä 	lockdep_assert_held(&dev_priv->irq_lock);
7166b12ca56SVille Syrjälä 	WARN_ON(!intel_irqs_enabled(dev_priv));
7176b12ca56SVille Syrjälä 
7186b12ca56SVille Syrjälä 	if ((dev_priv->pipestat_irq_mask[pipe] & status_mask) == status_mask)
7196b12ca56SVille Syrjälä 		return;
7206b12ca56SVille Syrjälä 
7216b12ca56SVille Syrjälä 	dev_priv->pipestat_irq_mask[pipe] |= status_mask;
7226b12ca56SVille Syrjälä 	enable_mask = i915_pipestat_enable_mask(dev_priv, pipe);
7236b12ca56SVille Syrjälä 
7246b12ca56SVille Syrjälä 	I915_WRITE(reg, enable_mask | status_mask);
7256b12ca56SVille Syrjälä 	POSTING_READ(reg);
726755e9019SImre Deak }
727755e9019SImre Deak 
7286b12ca56SVille Syrjälä void i915_disable_pipestat(struct drm_i915_private *dev_priv,
7296b12ca56SVille Syrjälä 			   enum pipe pipe, u32 status_mask)
730755e9019SImre Deak {
7316b12ca56SVille Syrjälä 	i915_reg_t reg = PIPESTAT(pipe);
732755e9019SImre Deak 	u32 enable_mask;
733755e9019SImre Deak 
7346b12ca56SVille Syrjälä 	WARN_ONCE(status_mask & ~PIPESTAT_INT_STATUS_MASK,
7356b12ca56SVille Syrjälä 		  "pipe %c: status_mask=0x%x\n",
7366b12ca56SVille Syrjälä 		  pipe_name(pipe), status_mask);
7376b12ca56SVille Syrjälä 
7386b12ca56SVille Syrjälä 	lockdep_assert_held(&dev_priv->irq_lock);
7396b12ca56SVille Syrjälä 	WARN_ON(!intel_irqs_enabled(dev_priv));
7406b12ca56SVille Syrjälä 
7416b12ca56SVille Syrjälä 	if ((dev_priv->pipestat_irq_mask[pipe] & status_mask) == 0)
7426b12ca56SVille Syrjälä 		return;
7436b12ca56SVille Syrjälä 
7446b12ca56SVille Syrjälä 	dev_priv->pipestat_irq_mask[pipe] &= ~status_mask;
7456b12ca56SVille Syrjälä 	enable_mask = i915_pipestat_enable_mask(dev_priv, pipe);
7466b12ca56SVille Syrjälä 
7476b12ca56SVille Syrjälä 	I915_WRITE(reg, enable_mask | status_mask);
7486b12ca56SVille Syrjälä 	POSTING_READ(reg);
749755e9019SImre Deak }
750755e9019SImre Deak 
751*f3e30485SVille Syrjälä static bool i915_has_asle(struct drm_i915_private *dev_priv)
752*f3e30485SVille Syrjälä {
753*f3e30485SVille Syrjälä 	if (!dev_priv->opregion.asle)
754*f3e30485SVille Syrjälä 		return false;
755*f3e30485SVille Syrjälä 
756*f3e30485SVille Syrjälä 	return IS_PINEVIEW(dev_priv) || IS_MOBILE(dev_priv);
757*f3e30485SVille Syrjälä }
758*f3e30485SVille Syrjälä 
759c0e09200SDave Airlie /**
760f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
76114bb2c11STvrtko Ursulin  * @dev_priv: i915 device private
76201c66889SZhao Yakui  */
76391d14251STvrtko Ursulin static void i915_enable_asle_pipestat(struct drm_i915_private *dev_priv)
76401c66889SZhao Yakui {
765*f3e30485SVille Syrjälä 	if (!i915_has_asle(dev_priv))
766f49e38ddSJani Nikula 		return;
767f49e38ddSJani Nikula 
76813321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
76901c66889SZhao Yakui 
770755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_STATUS);
77191d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 4)
7723b6c42e8SDaniel Vetter 		i915_enable_pipestat(dev_priv, PIPE_A,
773755e9019SImre Deak 				     PIPE_LEGACY_BLC_EVENT_STATUS);
7741ec14ad3SChris Wilson 
77513321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
77601c66889SZhao Yakui }
77701c66889SZhao Yakui 
778f75f3746SVille Syrjälä /*
779f75f3746SVille Syrjälä  * This timing diagram depicts the video signal in and
780f75f3746SVille Syrjälä  * around the vertical blanking period.
781f75f3746SVille Syrjälä  *
782f75f3746SVille Syrjälä  * Assumptions about the fictitious mode used in this example:
783f75f3746SVille Syrjälä  *  vblank_start >= 3
784f75f3746SVille Syrjälä  *  vsync_start = vblank_start + 1
785f75f3746SVille Syrjälä  *  vsync_end = vblank_start + 2
786f75f3746SVille Syrjälä  *  vtotal = vblank_start + 3
787f75f3746SVille Syrjälä  *
788f75f3746SVille Syrjälä  *           start of vblank:
789f75f3746SVille Syrjälä  *           latch double buffered registers
790f75f3746SVille Syrjälä  *           increment frame counter (ctg+)
791f75f3746SVille Syrjälä  *           generate start of vblank interrupt (gen4+)
792f75f3746SVille Syrjälä  *           |
793f75f3746SVille Syrjälä  *           |          frame start:
794f75f3746SVille Syrjälä  *           |          generate frame start interrupt (aka. vblank interrupt) (gmch)
795f75f3746SVille Syrjälä  *           |          may be shifted forward 1-3 extra lines via PIPECONF
796f75f3746SVille Syrjälä  *           |          |
797f75f3746SVille Syrjälä  *           |          |  start of vsync:
798f75f3746SVille Syrjälä  *           |          |  generate vsync interrupt
799f75f3746SVille Syrjälä  *           |          |  |
800f75f3746SVille Syrjälä  * ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx
801f75f3746SVille Syrjälä  *       .   \hs/   .      \hs/          \hs/          \hs/   .      \hs/
802f75f3746SVille Syrjälä  * ----va---> <-----------------vb--------------------> <--------va-------------
803f75f3746SVille Syrjälä  *       |          |       <----vs----->                     |
804f75f3746SVille Syrjälä  * -vbs-----> <---vbs+1---> <---vbs+2---> <-----0-----> <-----1-----> <-----2--- (scanline counter gen2)
805f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2---> <-----0--- (scanline counter gen3+)
806f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2- (scanline counter hsw+ hdmi)
807f75f3746SVille Syrjälä  *       |          |                                         |
808f75f3746SVille Syrjälä  *       last visible pixel                                   first visible pixel
809f75f3746SVille Syrjälä  *                  |                                         increment frame counter (gen3/4)
810f75f3746SVille Syrjälä  *                  pixel counter = vblank_start * htotal     pixel counter = 0 (gen3/4)
811f75f3746SVille Syrjälä  *
812f75f3746SVille Syrjälä  * x  = horizontal active
813f75f3746SVille Syrjälä  * _  = horizontal blanking
814f75f3746SVille Syrjälä  * hs = horizontal sync
815f75f3746SVille Syrjälä  * va = vertical active
816f75f3746SVille Syrjälä  * vb = vertical blanking
817f75f3746SVille Syrjälä  * vs = vertical sync
818f75f3746SVille Syrjälä  * vbs = vblank_start (number)
819f75f3746SVille Syrjälä  *
820f75f3746SVille Syrjälä  * Summary:
821f75f3746SVille Syrjälä  * - most events happen at the start of horizontal sync
822f75f3746SVille Syrjälä  * - frame start happens at the start of horizontal blank, 1-4 lines
823f75f3746SVille Syrjälä  *   (depending on PIPECONF settings) after the start of vblank
824f75f3746SVille Syrjälä  * - gen3/4 pixel and frame counter are synchronized with the start
825f75f3746SVille Syrjälä  *   of horizontal active on the first line of vertical active
826f75f3746SVille Syrjälä  */
827f75f3746SVille Syrjälä 
82842f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
82942f52ef8SKeith Packard  * we use as a pipe index
83042f52ef8SKeith Packard  */
83188e72717SThierry Reding static u32 i915_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
8320a3e67a4SJesse Barnes {
833fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
83432db0b65SVille Syrjälä 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
83532db0b65SVille Syrjälä 	const struct drm_display_mode *mode = &vblank->hwmode;
836f0f59a00SVille Syrjälä 	i915_reg_t high_frame, low_frame;
8370b2a8e09SVille Syrjälä 	u32 high1, high2, low, pixel, vbl_start, hsync_start, htotal;
838694e409dSVille Syrjälä 	unsigned long irqflags;
839391f75e2SVille Syrjälä 
84032db0b65SVille Syrjälä 	/*
84132db0b65SVille Syrjälä 	 * On i965gm TV output the frame counter only works up to
84232db0b65SVille Syrjälä 	 * the point when we enable the TV encoder. After that the
84332db0b65SVille Syrjälä 	 * frame counter ceases to work and reads zero. We need a
84432db0b65SVille Syrjälä 	 * vblank wait before enabling the TV encoder and so we
84532db0b65SVille Syrjälä 	 * have to enable vblank interrupts while the frame counter
84632db0b65SVille Syrjälä 	 * is still in a working state. However the core vblank code
84732db0b65SVille Syrjälä 	 * does not like us returning non-zero frame counter values
84832db0b65SVille Syrjälä 	 * when we've told it that we don't have a working frame
84932db0b65SVille Syrjälä 	 * counter. Thus we must stop non-zero values leaking out.
85032db0b65SVille Syrjälä 	 */
85132db0b65SVille Syrjälä 	if (!vblank->max_vblank_count)
85232db0b65SVille Syrjälä 		return 0;
85332db0b65SVille Syrjälä 
8540b2a8e09SVille Syrjälä 	htotal = mode->crtc_htotal;
8550b2a8e09SVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
8560b2a8e09SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
8570b2a8e09SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
8580b2a8e09SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
859391f75e2SVille Syrjälä 
8600b2a8e09SVille Syrjälä 	/* Convert to pixel count */
8610b2a8e09SVille Syrjälä 	vbl_start *= htotal;
8620b2a8e09SVille Syrjälä 
8630b2a8e09SVille Syrjälä 	/* Start of vblank event occurs at start of hsync */
8640b2a8e09SVille Syrjälä 	vbl_start -= htotal - hsync_start;
8650b2a8e09SVille Syrjälä 
8669db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
8679db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
8685eddb70bSChris Wilson 
869694e409dSVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
870694e409dSVille Syrjälä 
8710a3e67a4SJesse Barnes 	/*
8720a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
8730a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
8740a3e67a4SJesse Barnes 	 * register.
8750a3e67a4SJesse Barnes 	 */
8760a3e67a4SJesse Barnes 	do {
877694e409dSVille Syrjälä 		high1 = I915_READ_FW(high_frame) & PIPE_FRAME_HIGH_MASK;
878694e409dSVille Syrjälä 		low   = I915_READ_FW(low_frame);
879694e409dSVille Syrjälä 		high2 = I915_READ_FW(high_frame) & PIPE_FRAME_HIGH_MASK;
8800a3e67a4SJesse Barnes 	} while (high1 != high2);
8810a3e67a4SJesse Barnes 
882694e409dSVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
883694e409dSVille Syrjälä 
8845eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
885391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
8865eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
887391f75e2SVille Syrjälä 
888391f75e2SVille Syrjälä 	/*
889391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
890391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
891391f75e2SVille Syrjälä 	 * counter against vblank start.
892391f75e2SVille Syrjälä 	 */
893edc08d0aSVille Syrjälä 	return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
8940a3e67a4SJesse Barnes }
8950a3e67a4SJesse Barnes 
896974e59baSDave Airlie static u32 g4x_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
8979880b7a5SJesse Barnes {
898fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
8999880b7a5SJesse Barnes 
900649636efSVille Syrjälä 	return I915_READ(PIPE_FRMCOUNT_G4X(pipe));
9019880b7a5SJesse Barnes }
9029880b7a5SJesse Barnes 
903aec0246fSUma Shankar /*
904aec0246fSUma Shankar  * On certain encoders on certain platforms, pipe
905aec0246fSUma Shankar  * scanline register will not work to get the scanline,
906aec0246fSUma Shankar  * since the timings are driven from the PORT or issues
907aec0246fSUma Shankar  * with scanline register updates.
908aec0246fSUma Shankar  * This function will use Framestamp and current
909aec0246fSUma Shankar  * timestamp registers to calculate the scanline.
910aec0246fSUma Shankar  */
911aec0246fSUma Shankar static u32 __intel_get_crtc_scanline_from_timestamp(struct intel_crtc *crtc)
912aec0246fSUma Shankar {
913aec0246fSUma Shankar 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
914aec0246fSUma Shankar 	struct drm_vblank_crtc *vblank =
915aec0246fSUma Shankar 		&crtc->base.dev->vblank[drm_crtc_index(&crtc->base)];
916aec0246fSUma Shankar 	const struct drm_display_mode *mode = &vblank->hwmode;
917aec0246fSUma Shankar 	u32 vblank_start = mode->crtc_vblank_start;
918aec0246fSUma Shankar 	u32 vtotal = mode->crtc_vtotal;
919aec0246fSUma Shankar 	u32 htotal = mode->crtc_htotal;
920aec0246fSUma Shankar 	u32 clock = mode->crtc_clock;
921aec0246fSUma Shankar 	u32 scanline, scan_prev_time, scan_curr_time, scan_post_time;
922aec0246fSUma Shankar 
923aec0246fSUma Shankar 	/*
924aec0246fSUma Shankar 	 * To avoid the race condition where we might cross into the
925aec0246fSUma Shankar 	 * next vblank just between the PIPE_FRMTMSTMP and TIMESTAMP_CTR
926aec0246fSUma Shankar 	 * reads. We make sure we read PIPE_FRMTMSTMP and TIMESTAMP_CTR
927aec0246fSUma Shankar 	 * during the same frame.
928aec0246fSUma Shankar 	 */
929aec0246fSUma Shankar 	do {
930aec0246fSUma Shankar 		/*
931aec0246fSUma Shankar 		 * This field provides read back of the display
932aec0246fSUma Shankar 		 * pipe frame time stamp. The time stamp value
933aec0246fSUma Shankar 		 * is sampled at every start of vertical blank.
934aec0246fSUma Shankar 		 */
935aec0246fSUma Shankar 		scan_prev_time = I915_READ_FW(PIPE_FRMTMSTMP(crtc->pipe));
936aec0246fSUma Shankar 
937aec0246fSUma Shankar 		/*
938aec0246fSUma Shankar 		 * The TIMESTAMP_CTR register has the current
939aec0246fSUma Shankar 		 * time stamp value.
940aec0246fSUma Shankar 		 */
941aec0246fSUma Shankar 		scan_curr_time = I915_READ_FW(IVB_TIMESTAMP_CTR);
942aec0246fSUma Shankar 
943aec0246fSUma Shankar 		scan_post_time = I915_READ_FW(PIPE_FRMTMSTMP(crtc->pipe));
944aec0246fSUma Shankar 	} while (scan_post_time != scan_prev_time);
945aec0246fSUma Shankar 
946aec0246fSUma Shankar 	scanline = div_u64(mul_u32_u32(scan_curr_time - scan_prev_time,
947aec0246fSUma Shankar 					clock), 1000 * htotal);
948aec0246fSUma Shankar 	scanline = min(scanline, vtotal - 1);
949aec0246fSUma Shankar 	scanline = (scanline + vblank_start) % vtotal;
950aec0246fSUma Shankar 
951aec0246fSUma Shankar 	return scanline;
952aec0246fSUma Shankar }
953aec0246fSUma Shankar 
95475aa3f63SVille Syrjälä /* I915_READ_FW, only for fast reads of display block, no need for forcewake etc. */
955a225f079SVille Syrjälä static int __intel_get_crtc_scanline(struct intel_crtc *crtc)
956a225f079SVille Syrjälä {
957a225f079SVille Syrjälä 	struct drm_device *dev = crtc->base.dev;
958fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
9595caa0feaSDaniel Vetter 	const struct drm_display_mode *mode;
9605caa0feaSDaniel Vetter 	struct drm_vblank_crtc *vblank;
961a225f079SVille Syrjälä 	enum pipe pipe = crtc->pipe;
96280715b2fSVille Syrjälä 	int position, vtotal;
963a225f079SVille Syrjälä 
96472259536SVille Syrjälä 	if (!crtc->active)
96572259536SVille Syrjälä 		return -1;
96672259536SVille Syrjälä 
9675caa0feaSDaniel Vetter 	vblank = &crtc->base.dev->vblank[drm_crtc_index(&crtc->base)];
9685caa0feaSDaniel Vetter 	mode = &vblank->hwmode;
9695caa0feaSDaniel Vetter 
970aec0246fSUma Shankar 	if (mode->private_flags & I915_MODE_FLAG_GET_SCANLINE_FROM_TIMESTAMP)
971aec0246fSUma Shankar 		return __intel_get_crtc_scanline_from_timestamp(crtc);
972aec0246fSUma Shankar 
97380715b2fSVille Syrjälä 	vtotal = mode->crtc_vtotal;
974a225f079SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
975a225f079SVille Syrjälä 		vtotal /= 2;
976a225f079SVille Syrjälä 
977cf819effSLucas De Marchi 	if (IS_GEN(dev_priv, 2))
97875aa3f63SVille Syrjälä 		position = I915_READ_FW(PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
979a225f079SVille Syrjälä 	else
98075aa3f63SVille Syrjälä 		position = I915_READ_FW(PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
981a225f079SVille Syrjälä 
982a225f079SVille Syrjälä 	/*
98341b578fbSJesse Barnes 	 * On HSW, the DSL reg (0x70000) appears to return 0 if we
98441b578fbSJesse Barnes 	 * read it just before the start of vblank.  So try it again
98541b578fbSJesse Barnes 	 * so we don't accidentally end up spanning a vblank frame
98641b578fbSJesse Barnes 	 * increment, causing the pipe_update_end() code to squak at us.
98741b578fbSJesse Barnes 	 *
98841b578fbSJesse Barnes 	 * The nature of this problem means we can't simply check the ISR
98941b578fbSJesse Barnes 	 * bit and return the vblank start value; nor can we use the scanline
99041b578fbSJesse Barnes 	 * debug register in the transcoder as it appears to have the same
99141b578fbSJesse Barnes 	 * problem.  We may need to extend this to include other platforms,
99241b578fbSJesse Barnes 	 * but so far testing only shows the problem on HSW.
99341b578fbSJesse Barnes 	 */
99491d14251STvrtko Ursulin 	if (HAS_DDI(dev_priv) && !position) {
99541b578fbSJesse Barnes 		int i, temp;
99641b578fbSJesse Barnes 
99741b578fbSJesse Barnes 		for (i = 0; i < 100; i++) {
99841b578fbSJesse Barnes 			udelay(1);
999707bdd3fSVille Syrjälä 			temp = I915_READ_FW(PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
100041b578fbSJesse Barnes 			if (temp != position) {
100141b578fbSJesse Barnes 				position = temp;
100241b578fbSJesse Barnes 				break;
100341b578fbSJesse Barnes 			}
100441b578fbSJesse Barnes 		}
100541b578fbSJesse Barnes 	}
100641b578fbSJesse Barnes 
100741b578fbSJesse Barnes 	/*
100880715b2fSVille Syrjälä 	 * See update_scanline_offset() for the details on the
100980715b2fSVille Syrjälä 	 * scanline_offset adjustment.
1010a225f079SVille Syrjälä 	 */
101180715b2fSVille Syrjälä 	return (position + crtc->scanline_offset) % vtotal;
1012a225f079SVille Syrjälä }
1013a225f079SVille Syrjälä 
10141bf6ad62SDaniel Vetter static bool i915_get_crtc_scanoutpos(struct drm_device *dev, unsigned int pipe,
10151bf6ad62SDaniel Vetter 				     bool in_vblank_irq, int *vpos, int *hpos,
10163bb403bfSVille Syrjälä 				     ktime_t *stime, ktime_t *etime,
10173bb403bfSVille Syrjälä 				     const struct drm_display_mode *mode)
10180af7e4dfSMario Kleiner {
1019fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
102098187836SVille Syrjälä 	struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
102198187836SVille Syrjälä 								pipe);
10223aa18df8SVille Syrjälä 	int position;
102378e8fc6bSVille Syrjälä 	int vbl_start, vbl_end, hsync_start, htotal, vtotal;
1024ad3543edSMario Kleiner 	unsigned long irqflags;
10258a920e24SVille Syrjälä 	bool use_scanline_counter = INTEL_GEN(dev_priv) >= 5 ||
10268a920e24SVille Syrjälä 		IS_G4X(dev_priv) || IS_GEN(dev_priv, 2) ||
10278a920e24SVille Syrjälä 		mode->private_flags & I915_MODE_FLAG_USE_SCANLINE_COUNTER;
10280af7e4dfSMario Kleiner 
1029fc467a22SMaarten Lankhorst 	if (WARN_ON(!mode->crtc_clock)) {
10300af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
10319db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
10321bf6ad62SDaniel Vetter 		return false;
10330af7e4dfSMario Kleiner 	}
10340af7e4dfSMario Kleiner 
1035c2baf4b7SVille Syrjälä 	htotal = mode->crtc_htotal;
103678e8fc6bSVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
1037c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
1038c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
1039c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
10400af7e4dfSMario Kleiner 
1041d31faf65SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
1042d31faf65SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
1043d31faf65SVille Syrjälä 		vbl_end /= 2;
1044d31faf65SVille Syrjälä 		vtotal /= 2;
1045d31faf65SVille Syrjälä 	}
1046d31faf65SVille Syrjälä 
1047ad3543edSMario Kleiner 	/*
1048ad3543edSMario Kleiner 	 * Lock uncore.lock, as we will do multiple timing critical raw
1049ad3543edSMario Kleiner 	 * register reads, potentially with preemption disabled, so the
1050ad3543edSMario Kleiner 	 * following code must not block on uncore.lock.
1051ad3543edSMario Kleiner 	 */
1052ad3543edSMario Kleiner 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
1053ad3543edSMario Kleiner 
1054ad3543edSMario Kleiner 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
1055ad3543edSMario Kleiner 
1056ad3543edSMario Kleiner 	/* Get optional system timestamp before query. */
1057ad3543edSMario Kleiner 	if (stime)
1058ad3543edSMario Kleiner 		*stime = ktime_get();
1059ad3543edSMario Kleiner 
10608a920e24SVille Syrjälä 	if (use_scanline_counter) {
10610af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
10620af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
10630af7e4dfSMario Kleiner 		 */
1064a225f079SVille Syrjälä 		position = __intel_get_crtc_scanline(intel_crtc);
10650af7e4dfSMario Kleiner 	} else {
10660af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
10670af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
10680af7e4dfSMario Kleiner 		 * scanout position.
10690af7e4dfSMario Kleiner 		 */
107075aa3f63SVille Syrjälä 		position = (I915_READ_FW(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
10710af7e4dfSMario Kleiner 
10723aa18df8SVille Syrjälä 		/* convert to pixel counts */
10733aa18df8SVille Syrjälä 		vbl_start *= htotal;
10743aa18df8SVille Syrjälä 		vbl_end *= htotal;
10753aa18df8SVille Syrjälä 		vtotal *= htotal;
107678e8fc6bSVille Syrjälä 
107778e8fc6bSVille Syrjälä 		/*
10787e78f1cbSVille Syrjälä 		 * In interlaced modes, the pixel counter counts all pixels,
10797e78f1cbSVille Syrjälä 		 * so one field will have htotal more pixels. In order to avoid
10807e78f1cbSVille Syrjälä 		 * the reported position from jumping backwards when the pixel
10817e78f1cbSVille Syrjälä 		 * counter is beyond the length of the shorter field, just
10827e78f1cbSVille Syrjälä 		 * clamp the position the length of the shorter field. This
10837e78f1cbSVille Syrjälä 		 * matches how the scanline counter based position works since
10847e78f1cbSVille Syrjälä 		 * the scanline counter doesn't count the two half lines.
10857e78f1cbSVille Syrjälä 		 */
10867e78f1cbSVille Syrjälä 		if (position >= vtotal)
10877e78f1cbSVille Syrjälä 			position = vtotal - 1;
10887e78f1cbSVille Syrjälä 
10897e78f1cbSVille Syrjälä 		/*
109078e8fc6bSVille Syrjälä 		 * Start of vblank interrupt is triggered at start of hsync,
109178e8fc6bSVille Syrjälä 		 * just prior to the first active line of vblank. However we
109278e8fc6bSVille Syrjälä 		 * consider lines to start at the leading edge of horizontal
109378e8fc6bSVille Syrjälä 		 * active. So, should we get here before we've crossed into
109478e8fc6bSVille Syrjälä 		 * the horizontal active of the first line in vblank, we would
109578e8fc6bSVille Syrjälä 		 * not set the DRM_SCANOUTPOS_INVBL flag. In order to fix that,
109678e8fc6bSVille Syrjälä 		 * always add htotal-hsync_start to the current pixel position.
109778e8fc6bSVille Syrjälä 		 */
109878e8fc6bSVille Syrjälä 		position = (position + htotal - hsync_start) % vtotal;
10993aa18df8SVille Syrjälä 	}
11003aa18df8SVille Syrjälä 
1101ad3543edSMario Kleiner 	/* Get optional system timestamp after query. */
1102ad3543edSMario Kleiner 	if (etime)
1103ad3543edSMario Kleiner 		*etime = ktime_get();
1104ad3543edSMario Kleiner 
1105ad3543edSMario Kleiner 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
1106ad3543edSMario Kleiner 
1107ad3543edSMario Kleiner 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
1108ad3543edSMario Kleiner 
11093aa18df8SVille Syrjälä 	/*
11103aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
11113aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
11123aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
11133aa18df8SVille Syrjälä 	 * up since vbl_end.
11143aa18df8SVille Syrjälä 	 */
11153aa18df8SVille Syrjälä 	if (position >= vbl_start)
11163aa18df8SVille Syrjälä 		position -= vbl_end;
11173aa18df8SVille Syrjälä 	else
11183aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
11193aa18df8SVille Syrjälä 
11208a920e24SVille Syrjälä 	if (use_scanline_counter) {
11213aa18df8SVille Syrjälä 		*vpos = position;
11223aa18df8SVille Syrjälä 		*hpos = 0;
11233aa18df8SVille Syrjälä 	} else {
11240af7e4dfSMario Kleiner 		*vpos = position / htotal;
11250af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
11260af7e4dfSMario Kleiner 	}
11270af7e4dfSMario Kleiner 
11281bf6ad62SDaniel Vetter 	return true;
11290af7e4dfSMario Kleiner }
11300af7e4dfSMario Kleiner 
1131a225f079SVille Syrjälä int intel_get_crtc_scanline(struct intel_crtc *crtc)
1132a225f079SVille Syrjälä {
1133fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1134a225f079SVille Syrjälä 	unsigned long irqflags;
1135a225f079SVille Syrjälä 	int position;
1136a225f079SVille Syrjälä 
1137a225f079SVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
1138a225f079SVille Syrjälä 	position = __intel_get_crtc_scanline(crtc);
1139a225f079SVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
1140a225f079SVille Syrjälä 
1141a225f079SVille Syrjälä 	return position;
1142a225f079SVille Syrjälä }
1143a225f079SVille Syrjälä 
114491d14251STvrtko Ursulin static void ironlake_rps_change_irq_handler(struct drm_i915_private *dev_priv)
1145f97108d1SJesse Barnes {
1146b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
11479270388eSDaniel Vetter 	u8 new_delay;
11489270388eSDaniel Vetter 
1149d0ecd7e2SDaniel Vetter 	spin_lock(&mchdev_lock);
1150f97108d1SJesse Barnes 
115173edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
115273edd18fSDaniel Vetter 
115320e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
11549270388eSDaniel Vetter 
11557648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
1156b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
1157b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
1158f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
1159f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
1160f97108d1SJesse Barnes 
1161f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
1162b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
116320e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
116420e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
116520e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
116620e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
1167b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
116820e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
116920e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
117020e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
117120e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
1172f97108d1SJesse Barnes 	}
1173f97108d1SJesse Barnes 
117491d14251STvrtko Ursulin 	if (ironlake_set_drps(dev_priv, new_delay))
117520e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
1176f97108d1SJesse Barnes 
1177d0ecd7e2SDaniel Vetter 	spin_unlock(&mchdev_lock);
11789270388eSDaniel Vetter 
1179f97108d1SJesse Barnes 	return;
1180f97108d1SJesse Barnes }
1181f97108d1SJesse Barnes 
118243cf3bf0SChris Wilson static void vlv_c0_read(struct drm_i915_private *dev_priv,
118343cf3bf0SChris Wilson 			struct intel_rps_ei *ei)
118431685c25SDeepak S {
1185679cb6c1SMika Kuoppala 	ei->ktime = ktime_get_raw();
118643cf3bf0SChris Wilson 	ei->render_c0 = I915_READ(VLV_RENDER_C0_COUNT);
118743cf3bf0SChris Wilson 	ei->media_c0 = I915_READ(VLV_MEDIA_C0_COUNT);
118831685c25SDeepak S }
118931685c25SDeepak S 
119043cf3bf0SChris Wilson void gen6_rps_reset_ei(struct drm_i915_private *dev_priv)
119143cf3bf0SChris Wilson {
1192562d9baeSSagar Arun Kamble 	memset(&dev_priv->gt_pm.rps.ei, 0, sizeof(dev_priv->gt_pm.rps.ei));
119343cf3bf0SChris Wilson }
119443cf3bf0SChris Wilson 
119543cf3bf0SChris Wilson static u32 vlv_wa_c0_ei(struct drm_i915_private *dev_priv, u32 pm_iir)
119643cf3bf0SChris Wilson {
1197562d9baeSSagar Arun Kamble 	struct intel_rps *rps = &dev_priv->gt_pm.rps;
1198562d9baeSSagar Arun Kamble 	const struct intel_rps_ei *prev = &rps->ei;
119943cf3bf0SChris Wilson 	struct intel_rps_ei now;
120043cf3bf0SChris Wilson 	u32 events = 0;
120143cf3bf0SChris Wilson 
1202e0e8c7cbSChris Wilson 	if ((pm_iir & GEN6_PM_RP_UP_EI_EXPIRED) == 0)
120343cf3bf0SChris Wilson 		return 0;
120443cf3bf0SChris Wilson 
120543cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &now);
120631685c25SDeepak S 
1207679cb6c1SMika Kuoppala 	if (prev->ktime) {
1208e0e8c7cbSChris Wilson 		u64 time, c0;
1209569884e3SChris Wilson 		u32 render, media;
1210e0e8c7cbSChris Wilson 
1211679cb6c1SMika Kuoppala 		time = ktime_us_delta(now.ktime, prev->ktime);
12128f68d591SChris Wilson 
1213e0e8c7cbSChris Wilson 		time *= dev_priv->czclk_freq;
1214e0e8c7cbSChris Wilson 
1215e0e8c7cbSChris Wilson 		/* Workload can be split between render + media,
1216e0e8c7cbSChris Wilson 		 * e.g. SwapBuffers being blitted in X after being rendered in
1217e0e8c7cbSChris Wilson 		 * mesa. To account for this we need to combine both engines
1218e0e8c7cbSChris Wilson 		 * into our activity counter.
1219e0e8c7cbSChris Wilson 		 */
1220569884e3SChris Wilson 		render = now.render_c0 - prev->render_c0;
1221569884e3SChris Wilson 		media = now.media_c0 - prev->media_c0;
1222569884e3SChris Wilson 		c0 = max(render, media);
12236b7f6aa7SMika Kuoppala 		c0 *= 1000 * 100 << 8; /* to usecs and scale to threshold% */
1224e0e8c7cbSChris Wilson 
122560548c55SChris Wilson 		if (c0 > time * rps->power.up_threshold)
1226e0e8c7cbSChris Wilson 			events = GEN6_PM_RP_UP_THRESHOLD;
122760548c55SChris Wilson 		else if (c0 < time * rps->power.down_threshold)
1228e0e8c7cbSChris Wilson 			events = GEN6_PM_RP_DOWN_THRESHOLD;
122931685c25SDeepak S 	}
123031685c25SDeepak S 
1231562d9baeSSagar Arun Kamble 	rps->ei = now;
123243cf3bf0SChris Wilson 	return events;
123331685c25SDeepak S }
123431685c25SDeepak S 
12354912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
12363b8d8d91SJesse Barnes {
12372d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
1238562d9baeSSagar Arun Kamble 		container_of(work, struct drm_i915_private, gt_pm.rps.work);
1239562d9baeSSagar Arun Kamble 	struct intel_rps *rps = &dev_priv->gt_pm.rps;
12407c0a16adSChris Wilson 	bool client_boost = false;
12418d3afd7dSChris Wilson 	int new_delay, adj, min, max;
12427c0a16adSChris Wilson 	u32 pm_iir = 0;
12433b8d8d91SJesse Barnes 
124459cdb63dSDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
1245562d9baeSSagar Arun Kamble 	if (rps->interrupts_enabled) {
1246562d9baeSSagar Arun Kamble 		pm_iir = fetch_and_zero(&rps->pm_iir);
1247562d9baeSSagar Arun Kamble 		client_boost = atomic_read(&rps->num_waiters);
1248d4d70aa5SImre Deak 	}
124959cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
12504912d041SBen Widawsky 
125160611c13SPaulo Zanoni 	/* Make sure we didn't queue anything we're not going to process. */
1252a6706b45SDeepak S 	WARN_ON(pm_iir & ~dev_priv->pm_rps_events);
12538d3afd7dSChris Wilson 	if ((pm_iir & dev_priv->pm_rps_events) == 0 && !client_boost)
12547c0a16adSChris Wilson 		goto out;
12553b8d8d91SJesse Barnes 
12569f817501SSagar Arun Kamble 	mutex_lock(&dev_priv->pcu_lock);
12577b9e0ae6SChris Wilson 
125843cf3bf0SChris Wilson 	pm_iir |= vlv_wa_c0_ei(dev_priv, pm_iir);
125943cf3bf0SChris Wilson 
1260562d9baeSSagar Arun Kamble 	adj = rps->last_adj;
1261562d9baeSSagar Arun Kamble 	new_delay = rps->cur_freq;
1262562d9baeSSagar Arun Kamble 	min = rps->min_freq_softlimit;
1263562d9baeSSagar Arun Kamble 	max = rps->max_freq_softlimit;
12647b92c1bdSChris Wilson 	if (client_boost)
1265562d9baeSSagar Arun Kamble 		max = rps->max_freq;
1266562d9baeSSagar Arun Kamble 	if (client_boost && new_delay < rps->boost_freq) {
1267562d9baeSSagar Arun Kamble 		new_delay = rps->boost_freq;
12688d3afd7dSChris Wilson 		adj = 0;
12698d3afd7dSChris Wilson 	} else if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
1270dd75fdc8SChris Wilson 		if (adj > 0)
1271dd75fdc8SChris Wilson 			adj *= 2;
1272edcf284bSChris Wilson 		else /* CHV needs even encode values */
1273edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? 2 : 1;
12747e79a683SSagar Arun Kamble 
1275562d9baeSSagar Arun Kamble 		if (new_delay >= rps->max_freq_softlimit)
12767e79a683SSagar Arun Kamble 			adj = 0;
12777b92c1bdSChris Wilson 	} else if (client_boost) {
1278f5a4c67dSChris Wilson 		adj = 0;
1279dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
1280562d9baeSSagar Arun Kamble 		if (rps->cur_freq > rps->efficient_freq)
1281562d9baeSSagar Arun Kamble 			new_delay = rps->efficient_freq;
1282562d9baeSSagar Arun Kamble 		else if (rps->cur_freq > rps->min_freq_softlimit)
1283562d9baeSSagar Arun Kamble 			new_delay = rps->min_freq_softlimit;
1284dd75fdc8SChris Wilson 		adj = 0;
1285dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1286dd75fdc8SChris Wilson 		if (adj < 0)
1287dd75fdc8SChris Wilson 			adj *= 2;
1288edcf284bSChris Wilson 		else /* CHV needs even encode values */
1289edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? -2 : -1;
12907e79a683SSagar Arun Kamble 
1291562d9baeSSagar Arun Kamble 		if (new_delay <= rps->min_freq_softlimit)
12927e79a683SSagar Arun Kamble 			adj = 0;
1293dd75fdc8SChris Wilson 	} else { /* unknown event */
1294edcf284bSChris Wilson 		adj = 0;
1295dd75fdc8SChris Wilson 	}
12963b8d8d91SJesse Barnes 
1297562d9baeSSagar Arun Kamble 	rps->last_adj = adj;
1298edcf284bSChris Wilson 
12992a8862d2SChris Wilson 	/*
13002a8862d2SChris Wilson 	 * Limit deboosting and boosting to keep ourselves at the extremes
13012a8862d2SChris Wilson 	 * when in the respective power modes (i.e. slowly decrease frequencies
13022a8862d2SChris Wilson 	 * while in the HIGH_POWER zone and slowly increase frequencies while
13032a8862d2SChris Wilson 	 * in the LOW_POWER zone). On idle, we will hit the timeout and drop
13042a8862d2SChris Wilson 	 * to the next level quickly, and conversely if busy we expect to
13052a8862d2SChris Wilson 	 * hit a waitboost and rapidly switch into max power.
13062a8862d2SChris Wilson 	 */
13072a8862d2SChris Wilson 	if ((adj < 0 && rps->power.mode == HIGH_POWER) ||
13082a8862d2SChris Wilson 	    (adj > 0 && rps->power.mode == LOW_POWER))
13092a8862d2SChris Wilson 		rps->last_adj = 0;
13102a8862d2SChris Wilson 
131179249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
131279249636SBen Widawsky 	 * interrupt
131379249636SBen Widawsky 	 */
1314edcf284bSChris Wilson 	new_delay += adj;
13158d3afd7dSChris Wilson 	new_delay = clamp_t(int, new_delay, min, max);
131627544369SDeepak S 
13179fcee2f7SChris Wilson 	if (intel_set_rps(dev_priv, new_delay)) {
13189fcee2f7SChris Wilson 		DRM_DEBUG_DRIVER("Failed to set new GPU frequency\n");
1319562d9baeSSagar Arun Kamble 		rps->last_adj = 0;
13209fcee2f7SChris Wilson 	}
13213b8d8d91SJesse Barnes 
13229f817501SSagar Arun Kamble 	mutex_unlock(&dev_priv->pcu_lock);
13237c0a16adSChris Wilson 
13247c0a16adSChris Wilson out:
13257c0a16adSChris Wilson 	/* Make sure not to corrupt PMIMR state used by ringbuffer on GEN6 */
13267c0a16adSChris Wilson 	spin_lock_irq(&dev_priv->irq_lock);
1327562d9baeSSagar Arun Kamble 	if (rps->interrupts_enabled)
13287c0a16adSChris Wilson 		gen6_unmask_pm_irq(dev_priv, dev_priv->pm_rps_events);
13297c0a16adSChris Wilson 	spin_unlock_irq(&dev_priv->irq_lock);
13303b8d8d91SJesse Barnes }
13313b8d8d91SJesse Barnes 
1332e3689190SBen Widawsky 
1333e3689190SBen Widawsky /**
1334e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
1335e3689190SBen Widawsky  * occurred.
1336e3689190SBen Widawsky  * @work: workqueue struct
1337e3689190SBen Widawsky  *
1338e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
1339e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
1340e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
1341e3689190SBen Widawsky  */
1342e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
1343e3689190SBen Widawsky {
13442d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
1345cefcff8fSJoonas Lahtinen 		container_of(work, typeof(*dev_priv), l3_parity.error_work);
1346e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
134735a85ac6SBen Widawsky 	char *parity_event[6];
1348a9c287c9SJani Nikula 	u32 misccpctl;
1349a9c287c9SJani Nikula 	u8 slice = 0;
1350e3689190SBen Widawsky 
1351e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
1352e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
1353e3689190SBen Widawsky 	 * any time we access those registers.
1354e3689190SBen Widawsky 	 */
135591c8a326SChris Wilson 	mutex_lock(&dev_priv->drm.struct_mutex);
1356e3689190SBen Widawsky 
135735a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
135835a85ac6SBen Widawsky 	if (WARN_ON(!dev_priv->l3_parity.which_slice))
135935a85ac6SBen Widawsky 		goto out;
136035a85ac6SBen Widawsky 
1361e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
1362e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1363e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
1364e3689190SBen Widawsky 
136535a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
1366f0f59a00SVille Syrjälä 		i915_reg_t reg;
136735a85ac6SBen Widawsky 
136835a85ac6SBen Widawsky 		slice--;
13692d1fe073SJoonas Lahtinen 		if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv)))
137035a85ac6SBen Widawsky 			break;
137135a85ac6SBen Widawsky 
137235a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
137335a85ac6SBen Widawsky 
13746fa1c5f1SVille Syrjälä 		reg = GEN7_L3CDERRST1(slice);
137535a85ac6SBen Widawsky 
137635a85ac6SBen Widawsky 		error_status = I915_READ(reg);
1377e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
1378e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
1379e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1380e3689190SBen Widawsky 
138135a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
138235a85ac6SBen Widawsky 		POSTING_READ(reg);
1383e3689190SBen Widawsky 
1384cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1385e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1386e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1387e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
138835a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
138935a85ac6SBen Widawsky 		parity_event[5] = NULL;
1390e3689190SBen Widawsky 
139191c8a326SChris Wilson 		kobject_uevent_env(&dev_priv->drm.primary->kdev->kobj,
1392e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
1393e3689190SBen Widawsky 
139435a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
139535a85ac6SBen Widawsky 			  slice, row, bank, subbank);
1396e3689190SBen Widawsky 
139735a85ac6SBen Widawsky 		kfree(parity_event[4]);
1398e3689190SBen Widawsky 		kfree(parity_event[3]);
1399e3689190SBen Widawsky 		kfree(parity_event[2]);
1400e3689190SBen Widawsky 		kfree(parity_event[1]);
1401e3689190SBen Widawsky 	}
1402e3689190SBen Widawsky 
140335a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
140435a85ac6SBen Widawsky 
140535a85ac6SBen Widawsky out:
140635a85ac6SBen Widawsky 	WARN_ON(dev_priv->l3_parity.which_slice);
14074cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
14082d1fe073SJoonas Lahtinen 	gen5_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv));
14094cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
141035a85ac6SBen Widawsky 
141191c8a326SChris Wilson 	mutex_unlock(&dev_priv->drm.struct_mutex);
141235a85ac6SBen Widawsky }
141335a85ac6SBen Widawsky 
1414261e40b8SVille Syrjälä static void ivybridge_parity_error_irq_handler(struct drm_i915_private *dev_priv,
1415261e40b8SVille Syrjälä 					       u32 iir)
1416e3689190SBen Widawsky {
1417261e40b8SVille Syrjälä 	if (!HAS_L3_DPF(dev_priv))
1418e3689190SBen Widawsky 		return;
1419e3689190SBen Widawsky 
1420d0ecd7e2SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1421261e40b8SVille Syrjälä 	gen5_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv));
1422d0ecd7e2SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
1423e3689190SBen Widawsky 
1424261e40b8SVille Syrjälä 	iir &= GT_PARITY_ERROR(dev_priv);
142535a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
142635a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 1;
142735a85ac6SBen Widawsky 
142835a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
142935a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 0;
143035a85ac6SBen Widawsky 
1431a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
1432e3689190SBen Widawsky }
1433e3689190SBen Widawsky 
1434261e40b8SVille Syrjälä static void ilk_gt_irq_handler(struct drm_i915_private *dev_priv,
1435f1af8fc1SPaulo Zanoni 			       u32 gt_iir)
1436f1af8fc1SPaulo Zanoni {
1437f8973c21SChris Wilson 	if (gt_iir & GT_RENDER_USER_INTERRUPT)
14388a68d464SChris Wilson 		intel_engine_breadcrumbs_irq(dev_priv->engine[RCS0]);
1439f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
14408a68d464SChris Wilson 		intel_engine_breadcrumbs_irq(dev_priv->engine[VCS0]);
1441f1af8fc1SPaulo Zanoni }
1442f1af8fc1SPaulo Zanoni 
1443261e40b8SVille Syrjälä static void snb_gt_irq_handler(struct drm_i915_private *dev_priv,
1444e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1445e7b4c6b1SDaniel Vetter {
1446f8973c21SChris Wilson 	if (gt_iir & GT_RENDER_USER_INTERRUPT)
14478a68d464SChris Wilson 		intel_engine_breadcrumbs_irq(dev_priv->engine[RCS0]);
1448cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
14498a68d464SChris Wilson 		intel_engine_breadcrumbs_irq(dev_priv->engine[VCS0]);
1450cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
14518a68d464SChris Wilson 		intel_engine_breadcrumbs_irq(dev_priv->engine[BCS0]);
1452e7b4c6b1SDaniel Vetter 
1453cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1454cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
1455aaecdf61SDaniel Vetter 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT))
1456aaecdf61SDaniel Vetter 		DRM_DEBUG("Command parser error, gt_iir 0x%08x\n", gt_iir);
1457e3689190SBen Widawsky 
1458261e40b8SVille Syrjälä 	if (gt_iir & GT_PARITY_ERROR(dev_priv))
1459261e40b8SVille Syrjälä 		ivybridge_parity_error_irq_handler(dev_priv, gt_iir);
1460e7b4c6b1SDaniel Vetter }
1461e7b4c6b1SDaniel Vetter 
14625d3d69d5SChris Wilson static void
146351f6b0f9SChris Wilson gen8_cs_irq_handler(struct intel_engine_cs *engine, u32 iir)
1464fbcc1a0cSNick Hoath {
146531de7350SChris Wilson 	bool tasklet = false;
1466f747026cSChris Wilson 
1467fd8526e5SChris Wilson 	if (iir & GT_CONTEXT_SWITCH_INTERRUPT)
14688ea397faSChris Wilson 		tasklet = true;
146931de7350SChris Wilson 
147051f6b0f9SChris Wilson 	if (iir & GT_RENDER_USER_INTERRUPT) {
147152c0fdb2SChris Wilson 		intel_engine_breadcrumbs_irq(engine);
147293ffbe8eSMichal Wajdeczko 		tasklet |= USES_GUC_SUBMISSION(engine->i915);
147331de7350SChris Wilson 	}
147431de7350SChris Wilson 
147531de7350SChris Wilson 	if (tasklet)
1476fd8526e5SChris Wilson 		tasklet_hi_schedule(&engine->execlists.tasklet);
1477fbcc1a0cSNick Hoath }
1478fbcc1a0cSNick Hoath 
14792e4a5b25SChris Wilson static void gen8_gt_irq_ack(struct drm_i915_private *i915,
148055ef72f2SChris Wilson 			    u32 master_ctl, u32 gt_iir[4])
1481abd58f01SBen Widawsky {
14822e4a5b25SChris Wilson 	void __iomem * const regs = i915->regs;
14832e4a5b25SChris Wilson 
1484f0fd96f5SChris Wilson #define GEN8_GT_IRQS (GEN8_GT_RCS_IRQ | \
1485f0fd96f5SChris Wilson 		      GEN8_GT_BCS_IRQ | \
14868a68d464SChris Wilson 		      GEN8_GT_VCS0_IRQ | \
1487f0fd96f5SChris Wilson 		      GEN8_GT_VCS1_IRQ | \
1488f0fd96f5SChris Wilson 		      GEN8_GT_VECS_IRQ | \
1489f0fd96f5SChris Wilson 		      GEN8_GT_PM_IRQ | \
1490f0fd96f5SChris Wilson 		      GEN8_GT_GUC_IRQ)
1491f0fd96f5SChris Wilson 
1492abd58f01SBen Widawsky 	if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
14932e4a5b25SChris Wilson 		gt_iir[0] = raw_reg_read(regs, GEN8_GT_IIR(0));
14942e4a5b25SChris Wilson 		if (likely(gt_iir[0]))
14952e4a5b25SChris Wilson 			raw_reg_write(regs, GEN8_GT_IIR(0), gt_iir[0]);
1496abd58f01SBen Widawsky 	}
1497abd58f01SBen Widawsky 
14988a68d464SChris Wilson 	if (master_ctl & (GEN8_GT_VCS0_IRQ | GEN8_GT_VCS1_IRQ)) {
14992e4a5b25SChris Wilson 		gt_iir[1] = raw_reg_read(regs, GEN8_GT_IIR(1));
15002e4a5b25SChris Wilson 		if (likely(gt_iir[1]))
15012e4a5b25SChris Wilson 			raw_reg_write(regs, GEN8_GT_IIR(1), gt_iir[1]);
150274cdb337SChris Wilson 	}
150374cdb337SChris Wilson 
150426705e20SSagar Arun Kamble 	if (master_ctl & (GEN8_GT_PM_IRQ | GEN8_GT_GUC_IRQ)) {
15052e4a5b25SChris Wilson 		gt_iir[2] = raw_reg_read(regs, GEN8_GT_IIR(2));
1506f4de7794SChris Wilson 		if (likely(gt_iir[2]))
1507f4de7794SChris Wilson 			raw_reg_write(regs, GEN8_GT_IIR(2), gt_iir[2]);
15080961021aSBen Widawsky 	}
15092e4a5b25SChris Wilson 
15102e4a5b25SChris Wilson 	if (master_ctl & GEN8_GT_VECS_IRQ) {
15112e4a5b25SChris Wilson 		gt_iir[3] = raw_reg_read(regs, GEN8_GT_IIR(3));
15122e4a5b25SChris Wilson 		if (likely(gt_iir[3]))
15132e4a5b25SChris Wilson 			raw_reg_write(regs, GEN8_GT_IIR(3), gt_iir[3]);
151455ef72f2SChris Wilson 	}
1515abd58f01SBen Widawsky }
1516abd58f01SBen Widawsky 
15172e4a5b25SChris Wilson static void gen8_gt_irq_handler(struct drm_i915_private *i915,
1518f0fd96f5SChris Wilson 				u32 master_ctl, u32 gt_iir[4])
1519e30e251aSVille Syrjälä {
1520f0fd96f5SChris Wilson 	if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
15218a68d464SChris Wilson 		gen8_cs_irq_handler(i915->engine[RCS0],
152251f6b0f9SChris Wilson 				    gt_iir[0] >> GEN8_RCS_IRQ_SHIFT);
15238a68d464SChris Wilson 		gen8_cs_irq_handler(i915->engine[BCS0],
152451f6b0f9SChris Wilson 				    gt_iir[0] >> GEN8_BCS_IRQ_SHIFT);
1525e30e251aSVille Syrjälä 	}
1526e30e251aSVille Syrjälä 
15278a68d464SChris Wilson 	if (master_ctl & (GEN8_GT_VCS0_IRQ | GEN8_GT_VCS1_IRQ)) {
15288a68d464SChris Wilson 		gen8_cs_irq_handler(i915->engine[VCS0],
15298a68d464SChris Wilson 				    gt_iir[1] >> GEN8_VCS0_IRQ_SHIFT);
15308a68d464SChris Wilson 		gen8_cs_irq_handler(i915->engine[VCS1],
153151f6b0f9SChris Wilson 				    gt_iir[1] >> GEN8_VCS1_IRQ_SHIFT);
1532e30e251aSVille Syrjälä 	}
1533e30e251aSVille Syrjälä 
1534f0fd96f5SChris Wilson 	if (master_ctl & GEN8_GT_VECS_IRQ) {
15358a68d464SChris Wilson 		gen8_cs_irq_handler(i915->engine[VECS0],
153651f6b0f9SChris Wilson 				    gt_iir[3] >> GEN8_VECS_IRQ_SHIFT);
1537f0fd96f5SChris Wilson 	}
1538e30e251aSVille Syrjälä 
1539f0fd96f5SChris Wilson 	if (master_ctl & (GEN8_GT_PM_IRQ | GEN8_GT_GUC_IRQ)) {
15402e4a5b25SChris Wilson 		gen6_rps_irq_handler(i915, gt_iir[2]);
15412e4a5b25SChris Wilson 		gen9_guc_irq_handler(i915, gt_iir[2]);
1542e30e251aSVille Syrjälä 	}
1543f0fd96f5SChris Wilson }
1544e30e251aSVille Syrjälä 
1545af92058fSVille Syrjälä static bool gen11_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
1546121e758eSDhinakaran Pandiyan {
1547af92058fSVille Syrjälä 	switch (pin) {
1548af92058fSVille Syrjälä 	case HPD_PORT_C:
1549121e758eSDhinakaran Pandiyan 		return val & GEN11_HOTPLUG_CTL_LONG_DETECT(PORT_TC1);
1550af92058fSVille Syrjälä 	case HPD_PORT_D:
1551121e758eSDhinakaran Pandiyan 		return val & GEN11_HOTPLUG_CTL_LONG_DETECT(PORT_TC2);
1552af92058fSVille Syrjälä 	case HPD_PORT_E:
1553121e758eSDhinakaran Pandiyan 		return val & GEN11_HOTPLUG_CTL_LONG_DETECT(PORT_TC3);
1554af92058fSVille Syrjälä 	case HPD_PORT_F:
1555121e758eSDhinakaran Pandiyan 		return val & GEN11_HOTPLUG_CTL_LONG_DETECT(PORT_TC4);
1556121e758eSDhinakaran Pandiyan 	default:
1557121e758eSDhinakaran Pandiyan 		return false;
1558121e758eSDhinakaran Pandiyan 	}
1559121e758eSDhinakaran Pandiyan }
1560121e758eSDhinakaran Pandiyan 
1561af92058fSVille Syrjälä static bool bxt_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
156263c88d22SImre Deak {
1563af92058fSVille Syrjälä 	switch (pin) {
1564af92058fSVille Syrjälä 	case HPD_PORT_A:
1565195baa06SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
1566af92058fSVille Syrjälä 	case HPD_PORT_B:
156763c88d22SImre Deak 		return val & PORTB_HOTPLUG_LONG_DETECT;
1568af92058fSVille Syrjälä 	case HPD_PORT_C:
156963c88d22SImre Deak 		return val & PORTC_HOTPLUG_LONG_DETECT;
157063c88d22SImre Deak 	default:
157163c88d22SImre Deak 		return false;
157263c88d22SImre Deak 	}
157363c88d22SImre Deak }
157463c88d22SImre Deak 
1575af92058fSVille Syrjälä static bool icp_ddi_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
157631604222SAnusha Srivatsa {
1577af92058fSVille Syrjälä 	switch (pin) {
1578af92058fSVille Syrjälä 	case HPD_PORT_A:
157931604222SAnusha Srivatsa 		return val & ICP_DDIA_HPD_LONG_DETECT;
1580af92058fSVille Syrjälä 	case HPD_PORT_B:
158131604222SAnusha Srivatsa 		return val & ICP_DDIB_HPD_LONG_DETECT;
158231604222SAnusha Srivatsa 	default:
158331604222SAnusha Srivatsa 		return false;
158431604222SAnusha Srivatsa 	}
158531604222SAnusha Srivatsa }
158631604222SAnusha Srivatsa 
1587af92058fSVille Syrjälä static bool icp_tc_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
158831604222SAnusha Srivatsa {
1589af92058fSVille Syrjälä 	switch (pin) {
1590af92058fSVille Syrjälä 	case HPD_PORT_C:
159131604222SAnusha Srivatsa 		return val & ICP_TC_HPD_LONG_DETECT(PORT_TC1);
1592af92058fSVille Syrjälä 	case HPD_PORT_D:
159331604222SAnusha Srivatsa 		return val & ICP_TC_HPD_LONG_DETECT(PORT_TC2);
1594af92058fSVille Syrjälä 	case HPD_PORT_E:
159531604222SAnusha Srivatsa 		return val & ICP_TC_HPD_LONG_DETECT(PORT_TC3);
1596af92058fSVille Syrjälä 	case HPD_PORT_F:
159731604222SAnusha Srivatsa 		return val & ICP_TC_HPD_LONG_DETECT(PORT_TC4);
159831604222SAnusha Srivatsa 	default:
159931604222SAnusha Srivatsa 		return false;
160031604222SAnusha Srivatsa 	}
160131604222SAnusha Srivatsa }
160231604222SAnusha Srivatsa 
1603af92058fSVille Syrjälä static bool spt_port_hotplug2_long_detect(enum hpd_pin pin, u32 val)
16046dbf30ceSVille Syrjälä {
1605af92058fSVille Syrjälä 	switch (pin) {
1606af92058fSVille Syrjälä 	case HPD_PORT_E:
16076dbf30ceSVille Syrjälä 		return val & PORTE_HOTPLUG_LONG_DETECT;
16086dbf30ceSVille Syrjälä 	default:
16096dbf30ceSVille Syrjälä 		return false;
16106dbf30ceSVille Syrjälä 	}
16116dbf30ceSVille Syrjälä }
16126dbf30ceSVille Syrjälä 
1613af92058fSVille Syrjälä static bool spt_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
161474c0b395SVille Syrjälä {
1615af92058fSVille Syrjälä 	switch (pin) {
1616af92058fSVille Syrjälä 	case HPD_PORT_A:
161774c0b395SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
1618af92058fSVille Syrjälä 	case HPD_PORT_B:
161974c0b395SVille Syrjälä 		return val & PORTB_HOTPLUG_LONG_DETECT;
1620af92058fSVille Syrjälä 	case HPD_PORT_C:
162174c0b395SVille Syrjälä 		return val & PORTC_HOTPLUG_LONG_DETECT;
1622af92058fSVille Syrjälä 	case HPD_PORT_D:
162374c0b395SVille Syrjälä 		return val & PORTD_HOTPLUG_LONG_DETECT;
162474c0b395SVille Syrjälä 	default:
162574c0b395SVille Syrjälä 		return false;
162674c0b395SVille Syrjälä 	}
162774c0b395SVille Syrjälä }
162874c0b395SVille Syrjälä 
1629af92058fSVille Syrjälä static bool ilk_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
1630e4ce95aaSVille Syrjälä {
1631af92058fSVille Syrjälä 	switch (pin) {
1632af92058fSVille Syrjälä 	case HPD_PORT_A:
1633e4ce95aaSVille Syrjälä 		return val & DIGITAL_PORTA_HOTPLUG_LONG_DETECT;
1634e4ce95aaSVille Syrjälä 	default:
1635e4ce95aaSVille Syrjälä 		return false;
1636e4ce95aaSVille Syrjälä 	}
1637e4ce95aaSVille Syrjälä }
1638e4ce95aaSVille Syrjälä 
1639af92058fSVille Syrjälä static bool pch_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
164013cf5504SDave Airlie {
1641af92058fSVille Syrjälä 	switch (pin) {
1642af92058fSVille Syrjälä 	case HPD_PORT_B:
1643676574dfSJani Nikula 		return val & PORTB_HOTPLUG_LONG_DETECT;
1644af92058fSVille Syrjälä 	case HPD_PORT_C:
1645676574dfSJani Nikula 		return val & PORTC_HOTPLUG_LONG_DETECT;
1646af92058fSVille Syrjälä 	case HPD_PORT_D:
1647676574dfSJani Nikula 		return val & PORTD_HOTPLUG_LONG_DETECT;
1648676574dfSJani Nikula 	default:
1649676574dfSJani Nikula 		return false;
165013cf5504SDave Airlie 	}
165113cf5504SDave Airlie }
165213cf5504SDave Airlie 
1653af92058fSVille Syrjälä static bool i9xx_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
165413cf5504SDave Airlie {
1655af92058fSVille Syrjälä 	switch (pin) {
1656af92058fSVille Syrjälä 	case HPD_PORT_B:
1657676574dfSJani Nikula 		return val & PORTB_HOTPLUG_INT_LONG_PULSE;
1658af92058fSVille Syrjälä 	case HPD_PORT_C:
1659676574dfSJani Nikula 		return val & PORTC_HOTPLUG_INT_LONG_PULSE;
1660af92058fSVille Syrjälä 	case HPD_PORT_D:
1661676574dfSJani Nikula 		return val & PORTD_HOTPLUG_INT_LONG_PULSE;
1662676574dfSJani Nikula 	default:
1663676574dfSJani Nikula 		return false;
166413cf5504SDave Airlie 	}
166513cf5504SDave Airlie }
166613cf5504SDave Airlie 
166742db67d6SVille Syrjälä /*
166842db67d6SVille Syrjälä  * Get a bit mask of pins that have triggered, and which ones may be long.
166942db67d6SVille Syrjälä  * This can be called multiple times with the same masks to accumulate
167042db67d6SVille Syrjälä  * hotplug detection results from several registers.
167142db67d6SVille Syrjälä  *
167242db67d6SVille Syrjälä  * Note that the caller is expected to zero out the masks initially.
167342db67d6SVille Syrjälä  */
1674cf53902fSRodrigo Vivi static void intel_get_hpd_pins(struct drm_i915_private *dev_priv,
1675cf53902fSRodrigo Vivi 			       u32 *pin_mask, u32 *long_mask,
16768c841e57SJani Nikula 			       u32 hotplug_trigger, u32 dig_hotplug_reg,
1677fd63e2a9SImre Deak 			       const u32 hpd[HPD_NUM_PINS],
1678af92058fSVille Syrjälä 			       bool long_pulse_detect(enum hpd_pin pin, u32 val))
1679676574dfSJani Nikula {
1680e9be2850SVille Syrjälä 	enum hpd_pin pin;
1681676574dfSJani Nikula 
1682e9be2850SVille Syrjälä 	for_each_hpd_pin(pin) {
1683e9be2850SVille Syrjälä 		if ((hpd[pin] & hotplug_trigger) == 0)
16848c841e57SJani Nikula 			continue;
16858c841e57SJani Nikula 
1686e9be2850SVille Syrjälä 		*pin_mask |= BIT(pin);
1687676574dfSJani Nikula 
1688af92058fSVille Syrjälä 		if (long_pulse_detect(pin, dig_hotplug_reg))
1689e9be2850SVille Syrjälä 			*long_mask |= BIT(pin);
1690676574dfSJani Nikula 	}
1691676574dfSJani Nikula 
1692f88f0478SVille Syrjälä 	DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x, dig 0x%08x, pins 0x%08x, long 0x%08x\n",
1693f88f0478SVille Syrjälä 			 hotplug_trigger, dig_hotplug_reg, *pin_mask, *long_mask);
1694676574dfSJani Nikula 
1695676574dfSJani Nikula }
1696676574dfSJani Nikula 
169791d14251STvrtko Ursulin static void gmbus_irq_handler(struct drm_i915_private *dev_priv)
1698515ac2bbSDaniel Vetter {
169928c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1700515ac2bbSDaniel Vetter }
1701515ac2bbSDaniel Vetter 
170291d14251STvrtko Ursulin static void dp_aux_irq_handler(struct drm_i915_private *dev_priv)
1703ce99c256SDaniel Vetter {
17049ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1705ce99c256SDaniel Vetter }
1706ce99c256SDaniel Vetter 
17078bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
170891d14251STvrtko Ursulin static void display_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
170991d14251STvrtko Ursulin 					 enum pipe pipe,
1710a9c287c9SJani Nikula 					 u32 crc0, u32 crc1,
1711a9c287c9SJani Nikula 					 u32 crc2, u32 crc3,
1712a9c287c9SJani Nikula 					 u32 crc4)
17138bf1e9f1SShuang He {
17148bf1e9f1SShuang He 	struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
17158c6b709dSTomeu Vizoso 	struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
17165cee6c45SVille Syrjälä 	u32 crcs[5] = { crc0, crc1, crc2, crc3, crc4 };
17175cee6c45SVille Syrjälä 
17185cee6c45SVille Syrjälä 	trace_intel_pipe_crc(crtc, crcs);
1719b2c88f5bSDamien Lespiau 
1720d538bbdfSDamien Lespiau 	spin_lock(&pipe_crc->lock);
17218c6b709dSTomeu Vizoso 	/*
17228c6b709dSTomeu Vizoso 	 * For some not yet identified reason, the first CRC is
17238c6b709dSTomeu Vizoso 	 * bonkers. So let's just wait for the next vblank and read
17248c6b709dSTomeu Vizoso 	 * out the buggy result.
17258c6b709dSTomeu Vizoso 	 *
1726163e8aecSRodrigo Vivi 	 * On GEN8+ sometimes the second CRC is bonkers as well, so
17278c6b709dSTomeu Vizoso 	 * don't trust that one either.
17288c6b709dSTomeu Vizoso 	 */
1729033b7a23SMaarten Lankhorst 	if (pipe_crc->skipped <= 0 ||
1730163e8aecSRodrigo Vivi 	    (INTEL_GEN(dev_priv) >= 8 && pipe_crc->skipped == 1)) {
17318c6b709dSTomeu Vizoso 		pipe_crc->skipped++;
17328c6b709dSTomeu Vizoso 		spin_unlock(&pipe_crc->lock);
17338c6b709dSTomeu Vizoso 		return;
17348c6b709dSTomeu Vizoso 	}
17358c6b709dSTomeu Vizoso 	spin_unlock(&pipe_crc->lock);
17366cc42152SMaarten Lankhorst 
1737246ee524STomeu Vizoso 	drm_crtc_add_crc_entry(&crtc->base, true,
1738ca814b25SDaniel Vetter 				drm_crtc_accurate_vblank_count(&crtc->base),
1739246ee524STomeu Vizoso 				crcs);
17408c6b709dSTomeu Vizoso }
1741277de95eSDaniel Vetter #else
1742277de95eSDaniel Vetter static inline void
174391d14251STvrtko Ursulin display_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
174491d14251STvrtko Ursulin 			     enum pipe pipe,
1745a9c287c9SJani Nikula 			     u32 crc0, u32 crc1,
1746a9c287c9SJani Nikula 			     u32 crc2, u32 crc3,
1747a9c287c9SJani Nikula 			     u32 crc4) {}
1748277de95eSDaniel Vetter #endif
1749eba94eb9SDaniel Vetter 
1750277de95eSDaniel Vetter 
175191d14251STvrtko Ursulin static void hsw_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
175291d14251STvrtko Ursulin 				     enum pipe pipe)
17535a69b89fSDaniel Vetter {
175491d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
17555a69b89fSDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
17565a69b89fSDaniel Vetter 				     0, 0, 0, 0);
17575a69b89fSDaniel Vetter }
17585a69b89fSDaniel Vetter 
175991d14251STvrtko Ursulin static void ivb_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
176091d14251STvrtko Ursulin 				     enum pipe pipe)
1761eba94eb9SDaniel Vetter {
176291d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
1763eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1764eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1765eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1766eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
17678bc5e955SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1768eba94eb9SDaniel Vetter }
17695b3a856bSDaniel Vetter 
177091d14251STvrtko Ursulin static void i9xx_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
177191d14251STvrtko Ursulin 				      enum pipe pipe)
17725b3a856bSDaniel Vetter {
1773a9c287c9SJani Nikula 	u32 res1, res2;
17740b5c5ed0SDaniel Vetter 
177591d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 3)
17760b5c5ed0SDaniel Vetter 		res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
17770b5c5ed0SDaniel Vetter 	else
17780b5c5ed0SDaniel Vetter 		res1 = 0;
17790b5c5ed0SDaniel Vetter 
178091d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
17810b5c5ed0SDaniel Vetter 		res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
17820b5c5ed0SDaniel Vetter 	else
17830b5c5ed0SDaniel Vetter 		res2 = 0;
17845b3a856bSDaniel Vetter 
178591d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
17860b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_RED(pipe)),
17870b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_GREEN(pipe)),
17880b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_BLUE(pipe)),
17890b5c5ed0SDaniel Vetter 				     res1, res2);
17905b3a856bSDaniel Vetter }
17918bf1e9f1SShuang He 
17921403c0d4SPaulo Zanoni /* The RPS events need forcewake, so we add them to a work queue and mask their
17931403c0d4SPaulo Zanoni  * IMR bits until the work is done. Other interrupts can be processed without
17941403c0d4SPaulo Zanoni  * the work queue. */
17951403c0d4SPaulo Zanoni static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1796baf02a1fSBen Widawsky {
1797562d9baeSSagar Arun Kamble 	struct intel_rps *rps = &dev_priv->gt_pm.rps;
1798562d9baeSSagar Arun Kamble 
1799a6706b45SDeepak S 	if (pm_iir & dev_priv->pm_rps_events) {
180059cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
1801f4e9af4fSAkash Goel 		gen6_mask_pm_irq(dev_priv, pm_iir & dev_priv->pm_rps_events);
1802562d9baeSSagar Arun Kamble 		if (rps->interrupts_enabled) {
1803562d9baeSSagar Arun Kamble 			rps->pm_iir |= pm_iir & dev_priv->pm_rps_events;
1804562d9baeSSagar Arun Kamble 			schedule_work(&rps->work);
180541a05a3aSDaniel Vetter 		}
1806d4d70aa5SImre Deak 		spin_unlock(&dev_priv->irq_lock);
1807d4d70aa5SImre Deak 	}
1808baf02a1fSBen Widawsky 
1809bca2bf2aSPandiyan, Dhinakaran 	if (INTEL_GEN(dev_priv) >= 8)
1810c9a9a268SImre Deak 		return;
1811c9a9a268SImre Deak 
181212638c57SBen Widawsky 	if (pm_iir & PM_VEBOX_USER_INTERRUPT)
18138a68d464SChris Wilson 		intel_engine_breadcrumbs_irq(dev_priv->engine[VECS0]);
181412638c57SBen Widawsky 
1815aaecdf61SDaniel Vetter 	if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT)
1816aaecdf61SDaniel Vetter 		DRM_DEBUG("Command parser error, pm_iir 0x%08x\n", pm_iir);
181712638c57SBen Widawsky }
1818baf02a1fSBen Widawsky 
181926705e20SSagar Arun Kamble static void gen9_guc_irq_handler(struct drm_i915_private *dev_priv, u32 gt_iir)
182026705e20SSagar Arun Kamble {
182193bf8096SMichal Wajdeczko 	if (gt_iir & GEN9_GUC_TO_HOST_INT_EVENT)
182293bf8096SMichal Wajdeczko 		intel_guc_to_host_event_handler(&dev_priv->guc);
182326705e20SSagar Arun Kamble }
182426705e20SSagar Arun Kamble 
182544d9241eSVille Syrjälä static void i9xx_pipestat_irq_reset(struct drm_i915_private *dev_priv)
182644d9241eSVille Syrjälä {
182744d9241eSVille Syrjälä 	enum pipe pipe;
182844d9241eSVille Syrjälä 
182944d9241eSVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
183044d9241eSVille Syrjälä 		I915_WRITE(PIPESTAT(pipe),
183144d9241eSVille Syrjälä 			   PIPESTAT_INT_STATUS_MASK |
183244d9241eSVille Syrjälä 			   PIPE_FIFO_UNDERRUN_STATUS);
183344d9241eSVille Syrjälä 
183444d9241eSVille Syrjälä 		dev_priv->pipestat_irq_mask[pipe] = 0;
183544d9241eSVille Syrjälä 	}
183644d9241eSVille Syrjälä }
183744d9241eSVille Syrjälä 
1838eb64343cSVille Syrjälä static void i9xx_pipestat_irq_ack(struct drm_i915_private *dev_priv,
183991d14251STvrtko Ursulin 				  u32 iir, u32 pipe_stats[I915_MAX_PIPES])
18407e231dbeSJesse Barnes {
18417e231dbeSJesse Barnes 	int pipe;
18427e231dbeSJesse Barnes 
184358ead0d7SImre Deak 	spin_lock(&dev_priv->irq_lock);
18441ca993d2SVille Syrjälä 
18451ca993d2SVille Syrjälä 	if (!dev_priv->display_irqs_enabled) {
18461ca993d2SVille Syrjälä 		spin_unlock(&dev_priv->irq_lock);
18471ca993d2SVille Syrjälä 		return;
18481ca993d2SVille Syrjälä 	}
18491ca993d2SVille Syrjälä 
1850055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1851f0f59a00SVille Syrjälä 		i915_reg_t reg;
18526b12ca56SVille Syrjälä 		u32 status_mask, enable_mask, iir_bit = 0;
185391d181ddSImre Deak 
1854bbb5eebfSDaniel Vetter 		/*
1855bbb5eebfSDaniel Vetter 		 * PIPESTAT bits get signalled even when the interrupt is
1856bbb5eebfSDaniel Vetter 		 * disabled with the mask bits, and some of the status bits do
1857bbb5eebfSDaniel Vetter 		 * not generate interrupts at all (like the underrun bit). Hence
1858bbb5eebfSDaniel Vetter 		 * we need to be careful that we only handle what we want to
1859bbb5eebfSDaniel Vetter 		 * handle.
1860bbb5eebfSDaniel Vetter 		 */
18610f239f4cSDaniel Vetter 
18620f239f4cSDaniel Vetter 		/* fifo underruns are filterered in the underrun handler. */
18636b12ca56SVille Syrjälä 		status_mask = PIPE_FIFO_UNDERRUN_STATUS;
1864bbb5eebfSDaniel Vetter 
1865bbb5eebfSDaniel Vetter 		switch (pipe) {
1866bbb5eebfSDaniel Vetter 		case PIPE_A:
1867bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
1868bbb5eebfSDaniel Vetter 			break;
1869bbb5eebfSDaniel Vetter 		case PIPE_B:
1870bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
1871bbb5eebfSDaniel Vetter 			break;
18723278f67fSVille Syrjälä 		case PIPE_C:
18733278f67fSVille Syrjälä 			iir_bit = I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
18743278f67fSVille Syrjälä 			break;
1875bbb5eebfSDaniel Vetter 		}
1876bbb5eebfSDaniel Vetter 		if (iir & iir_bit)
18776b12ca56SVille Syrjälä 			status_mask |= dev_priv->pipestat_irq_mask[pipe];
1878bbb5eebfSDaniel Vetter 
18796b12ca56SVille Syrjälä 		if (!status_mask)
188091d181ddSImre Deak 			continue;
188191d181ddSImre Deak 
188291d181ddSImre Deak 		reg = PIPESTAT(pipe);
18836b12ca56SVille Syrjälä 		pipe_stats[pipe] = I915_READ(reg) & status_mask;
18846b12ca56SVille Syrjälä 		enable_mask = i915_pipestat_enable_mask(dev_priv, pipe);
18857e231dbeSJesse Barnes 
18867e231dbeSJesse Barnes 		/*
18877e231dbeSJesse Barnes 		 * Clear the PIPE*STAT regs before the IIR
1888132c27c9SVille Syrjälä 		 *
1889132c27c9SVille Syrjälä 		 * Toggle the enable bits to make sure we get an
1890132c27c9SVille Syrjälä 		 * edge in the ISR pipe event bit if we don't clear
1891132c27c9SVille Syrjälä 		 * all the enabled status bits. Otherwise the edge
1892132c27c9SVille Syrjälä 		 * triggered IIR on i965/g4x wouldn't notice that
1893132c27c9SVille Syrjälä 		 * an interrupt is still pending.
18947e231dbeSJesse Barnes 		 */
1895132c27c9SVille Syrjälä 		if (pipe_stats[pipe]) {
1896132c27c9SVille Syrjälä 			I915_WRITE(reg, pipe_stats[pipe]);
1897132c27c9SVille Syrjälä 			I915_WRITE(reg, enable_mask);
1898132c27c9SVille Syrjälä 		}
18997e231dbeSJesse Barnes 	}
190058ead0d7SImre Deak 	spin_unlock(&dev_priv->irq_lock);
19012ecb8ca4SVille Syrjälä }
19022ecb8ca4SVille Syrjälä 
1903eb64343cSVille Syrjälä static void i8xx_pipestat_irq_handler(struct drm_i915_private *dev_priv,
1904eb64343cSVille Syrjälä 				      u16 iir, u32 pipe_stats[I915_MAX_PIPES])
1905eb64343cSVille Syrjälä {
1906eb64343cSVille Syrjälä 	enum pipe pipe;
1907eb64343cSVille Syrjälä 
1908eb64343cSVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
1909eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
1910eb64343cSVille Syrjälä 			drm_handle_vblank(&dev_priv->drm, pipe);
1911eb64343cSVille Syrjälä 
1912eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1913eb64343cSVille Syrjälä 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
1914eb64343cSVille Syrjälä 
1915eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
1916eb64343cSVille Syrjälä 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
1917eb64343cSVille Syrjälä 	}
1918eb64343cSVille Syrjälä }
1919eb64343cSVille Syrjälä 
1920eb64343cSVille Syrjälä static void i915_pipestat_irq_handler(struct drm_i915_private *dev_priv,
1921eb64343cSVille Syrjälä 				      u32 iir, u32 pipe_stats[I915_MAX_PIPES])
1922eb64343cSVille Syrjälä {
1923eb64343cSVille Syrjälä 	bool blc_event = false;
1924eb64343cSVille Syrjälä 	enum pipe pipe;
1925eb64343cSVille Syrjälä 
1926eb64343cSVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
1927eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
1928eb64343cSVille Syrjälä 			drm_handle_vblank(&dev_priv->drm, pipe);
1929eb64343cSVille Syrjälä 
1930eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
1931eb64343cSVille Syrjälä 			blc_event = true;
1932eb64343cSVille Syrjälä 
1933eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1934eb64343cSVille Syrjälä 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
1935eb64343cSVille Syrjälä 
1936eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
1937eb64343cSVille Syrjälä 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
1938eb64343cSVille Syrjälä 	}
1939eb64343cSVille Syrjälä 
1940eb64343cSVille Syrjälä 	if (blc_event || (iir & I915_ASLE_INTERRUPT))
1941eb64343cSVille Syrjälä 		intel_opregion_asle_intr(dev_priv);
1942eb64343cSVille Syrjälä }
1943eb64343cSVille Syrjälä 
1944eb64343cSVille Syrjälä static void i965_pipestat_irq_handler(struct drm_i915_private *dev_priv,
1945eb64343cSVille Syrjälä 				      u32 iir, u32 pipe_stats[I915_MAX_PIPES])
1946eb64343cSVille Syrjälä {
1947eb64343cSVille Syrjälä 	bool blc_event = false;
1948eb64343cSVille Syrjälä 	enum pipe pipe;
1949eb64343cSVille Syrjälä 
1950eb64343cSVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
1951eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS)
1952eb64343cSVille Syrjälä 			drm_handle_vblank(&dev_priv->drm, pipe);
1953eb64343cSVille Syrjälä 
1954eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
1955eb64343cSVille Syrjälä 			blc_event = true;
1956eb64343cSVille Syrjälä 
1957eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1958eb64343cSVille Syrjälä 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
1959eb64343cSVille Syrjälä 
1960eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
1961eb64343cSVille Syrjälä 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
1962eb64343cSVille Syrjälä 	}
1963eb64343cSVille Syrjälä 
1964eb64343cSVille Syrjälä 	if (blc_event || (iir & I915_ASLE_INTERRUPT))
1965eb64343cSVille Syrjälä 		intel_opregion_asle_intr(dev_priv);
1966eb64343cSVille Syrjälä 
1967eb64343cSVille Syrjälä 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1968eb64343cSVille Syrjälä 		gmbus_irq_handler(dev_priv);
1969eb64343cSVille Syrjälä }
1970eb64343cSVille Syrjälä 
197191d14251STvrtko Ursulin static void valleyview_pipestat_irq_handler(struct drm_i915_private *dev_priv,
19722ecb8ca4SVille Syrjälä 					    u32 pipe_stats[I915_MAX_PIPES])
19732ecb8ca4SVille Syrjälä {
19742ecb8ca4SVille Syrjälä 	enum pipe pipe;
19757e231dbeSJesse Barnes 
1976055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1977fd3a4024SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS)
1978fd3a4024SDaniel Vetter 			drm_handle_vblank(&dev_priv->drm, pipe);
19794356d586SDaniel Vetter 
19804356d586SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
198191d14251STvrtko Ursulin 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
19822d9d2b0bSVille Syrjälä 
19831f7247c0SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
19841f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
198531acc7f5SJesse Barnes 	}
198631acc7f5SJesse Barnes 
1987c1874ed7SImre Deak 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
198891d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
1989c1874ed7SImre Deak }
1990c1874ed7SImre Deak 
19911ae3c34cSVille Syrjälä static u32 i9xx_hpd_irq_ack(struct drm_i915_private *dev_priv)
199216c6c56bSVille Syrjälä {
19930ba7c51aSVille Syrjälä 	u32 hotplug_status = 0, hotplug_status_mask;
19940ba7c51aSVille Syrjälä 	int i;
199516c6c56bSVille Syrjälä 
19960ba7c51aSVille Syrjälä 	if (IS_G4X(dev_priv) ||
19970ba7c51aSVille Syrjälä 	    IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
19980ba7c51aSVille Syrjälä 		hotplug_status_mask = HOTPLUG_INT_STATUS_G4X |
19990ba7c51aSVille Syrjälä 			DP_AUX_CHANNEL_MASK_INT_STATUS_G4X;
20000ba7c51aSVille Syrjälä 	else
20010ba7c51aSVille Syrjälä 		hotplug_status_mask = HOTPLUG_INT_STATUS_I915;
20020ba7c51aSVille Syrjälä 
20030ba7c51aSVille Syrjälä 	/*
20040ba7c51aSVille Syrjälä 	 * We absolutely have to clear all the pending interrupt
20050ba7c51aSVille Syrjälä 	 * bits in PORT_HOTPLUG_STAT. Otherwise the ISR port
20060ba7c51aSVille Syrjälä 	 * interrupt bit won't have an edge, and the i965/g4x
20070ba7c51aSVille Syrjälä 	 * edge triggered IIR will not notice that an interrupt
20080ba7c51aSVille Syrjälä 	 * is still pending. We can't use PORT_HOTPLUG_EN to
20090ba7c51aSVille Syrjälä 	 * guarantee the edge as the act of toggling the enable
20100ba7c51aSVille Syrjälä 	 * bits can itself generate a new hotplug interrupt :(
20110ba7c51aSVille Syrjälä 	 */
20120ba7c51aSVille Syrjälä 	for (i = 0; i < 10; i++) {
20130ba7c51aSVille Syrjälä 		u32 tmp = I915_READ(PORT_HOTPLUG_STAT) & hotplug_status_mask;
20140ba7c51aSVille Syrjälä 
20150ba7c51aSVille Syrjälä 		if (tmp == 0)
20160ba7c51aSVille Syrjälä 			return hotplug_status;
20170ba7c51aSVille Syrjälä 
20180ba7c51aSVille Syrjälä 		hotplug_status |= tmp;
20193ff60f89SOscar Mateo 		I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
20200ba7c51aSVille Syrjälä 	}
20210ba7c51aSVille Syrjälä 
20220ba7c51aSVille Syrjälä 	WARN_ONCE(1,
20230ba7c51aSVille Syrjälä 		  "PORT_HOTPLUG_STAT did not clear (0x%08x)\n",
20240ba7c51aSVille Syrjälä 		  I915_READ(PORT_HOTPLUG_STAT));
20251ae3c34cSVille Syrjälä 
20261ae3c34cSVille Syrjälä 	return hotplug_status;
20271ae3c34cSVille Syrjälä }
20281ae3c34cSVille Syrjälä 
202991d14251STvrtko Ursulin static void i9xx_hpd_irq_handler(struct drm_i915_private *dev_priv,
20301ae3c34cSVille Syrjälä 				 u32 hotplug_status)
20311ae3c34cSVille Syrjälä {
20321ae3c34cSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
20333ff60f89SOscar Mateo 
203491d14251STvrtko Ursulin 	if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
203591d14251STvrtko Ursulin 	    IS_CHERRYVIEW(dev_priv)) {
203616c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
203716c6c56bSVille Syrjälä 
203858f2cf24SVille Syrjälä 		if (hotplug_trigger) {
2039cf53902fSRodrigo Vivi 			intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
2040cf53902fSRodrigo Vivi 					   hotplug_trigger, hotplug_trigger,
2041cf53902fSRodrigo Vivi 					   hpd_status_g4x,
2042fd63e2a9SImre Deak 					   i9xx_port_hotplug_long_detect);
204358f2cf24SVille Syrjälä 
204491d14251STvrtko Ursulin 			intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
204558f2cf24SVille Syrjälä 		}
2046369712e8SJani Nikula 
2047369712e8SJani Nikula 		if (hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
204891d14251STvrtko Ursulin 			dp_aux_irq_handler(dev_priv);
204916c6c56bSVille Syrjälä 	} else {
205016c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
205116c6c56bSVille Syrjälä 
205258f2cf24SVille Syrjälä 		if (hotplug_trigger) {
2053cf53902fSRodrigo Vivi 			intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
2054cf53902fSRodrigo Vivi 					   hotplug_trigger, hotplug_trigger,
2055cf53902fSRodrigo Vivi 					   hpd_status_i915,
2056fd63e2a9SImre Deak 					   i9xx_port_hotplug_long_detect);
205791d14251STvrtko Ursulin 			intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
205816c6c56bSVille Syrjälä 		}
20593ff60f89SOscar Mateo 	}
206058f2cf24SVille Syrjälä }
206116c6c56bSVille Syrjälä 
2062c1874ed7SImre Deak static irqreturn_t valleyview_irq_handler(int irq, void *arg)
2063c1874ed7SImre Deak {
206445a83f84SDaniel Vetter 	struct drm_device *dev = arg;
2065fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2066c1874ed7SImre Deak 	irqreturn_t ret = IRQ_NONE;
2067c1874ed7SImre Deak 
20682dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
20692dd2a883SImre Deak 		return IRQ_NONE;
20702dd2a883SImre Deak 
20711f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
20721f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
20731f814dacSImre Deak 
20741e1cace9SVille Syrjälä 	do {
20756e814800SVille Syrjälä 		u32 iir, gt_iir, pm_iir;
20762ecb8ca4SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
20771ae3c34cSVille Syrjälä 		u32 hotplug_status = 0;
2078a5e485a9SVille Syrjälä 		u32 ier = 0;
20793ff60f89SOscar Mateo 
2080c1874ed7SImre Deak 		gt_iir = I915_READ(GTIIR);
2081c1874ed7SImre Deak 		pm_iir = I915_READ(GEN6_PMIIR);
20823ff60f89SOscar Mateo 		iir = I915_READ(VLV_IIR);
2083c1874ed7SImre Deak 
2084c1874ed7SImre Deak 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
20851e1cace9SVille Syrjälä 			break;
2086c1874ed7SImre Deak 
2087c1874ed7SImre Deak 		ret = IRQ_HANDLED;
2088c1874ed7SImre Deak 
2089a5e485a9SVille Syrjälä 		/*
2090a5e485a9SVille Syrjälä 		 * Theory on interrupt generation, based on empirical evidence:
2091a5e485a9SVille Syrjälä 		 *
2092a5e485a9SVille Syrjälä 		 * x = ((VLV_IIR & VLV_IER) ||
2093a5e485a9SVille Syrjälä 		 *      (((GT_IIR & GT_IER) || (GEN6_PMIIR & GEN6_PMIER)) &&
2094a5e485a9SVille Syrjälä 		 *       (VLV_MASTER_IER & MASTER_INTERRUPT_ENABLE)));
2095a5e485a9SVille Syrjälä 		 *
2096a5e485a9SVille Syrjälä 		 * A CPU interrupt will only be raised when 'x' has a 0->1 edge.
2097a5e485a9SVille Syrjälä 		 * Hence we clear MASTER_INTERRUPT_ENABLE and VLV_IER to
2098a5e485a9SVille Syrjälä 		 * guarantee the CPU interrupt will be raised again even if we
2099a5e485a9SVille Syrjälä 		 * don't end up clearing all the VLV_IIR, GT_IIR, GEN6_PMIIR
2100a5e485a9SVille Syrjälä 		 * bits this time around.
2101a5e485a9SVille Syrjälä 		 */
21024a0a0202SVille Syrjälä 		I915_WRITE(VLV_MASTER_IER, 0);
2103a5e485a9SVille Syrjälä 		ier = I915_READ(VLV_IER);
2104a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, 0);
21054a0a0202SVille Syrjälä 
21064a0a0202SVille Syrjälä 		if (gt_iir)
21074a0a0202SVille Syrjälä 			I915_WRITE(GTIIR, gt_iir);
21084a0a0202SVille Syrjälä 		if (pm_iir)
21094a0a0202SVille Syrjälä 			I915_WRITE(GEN6_PMIIR, pm_iir);
21104a0a0202SVille Syrjälä 
21117ce4d1f2SVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
21121ae3c34cSVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
21137ce4d1f2SVille Syrjälä 
21143ff60f89SOscar Mateo 		/* Call regardless, as some status bits might not be
21153ff60f89SOscar Mateo 		 * signalled in iir */
2116eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
21177ce4d1f2SVille Syrjälä 
2118eef57324SJerome Anand 		if (iir & (I915_LPE_PIPE_A_INTERRUPT |
2119eef57324SJerome Anand 			   I915_LPE_PIPE_B_INTERRUPT))
2120eef57324SJerome Anand 			intel_lpe_audio_irq_handler(dev_priv);
2121eef57324SJerome Anand 
21227ce4d1f2SVille Syrjälä 		/*
21237ce4d1f2SVille Syrjälä 		 * VLV_IIR is single buffered, and reflects the level
21247ce4d1f2SVille Syrjälä 		 * from PIPESTAT/PORT_HOTPLUG_STAT, hence clear it last.
21257ce4d1f2SVille Syrjälä 		 */
21267ce4d1f2SVille Syrjälä 		if (iir)
21277ce4d1f2SVille Syrjälä 			I915_WRITE(VLV_IIR, iir);
21284a0a0202SVille Syrjälä 
2129a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, ier);
21304a0a0202SVille Syrjälä 		I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
21311ae3c34cSVille Syrjälä 
213252894874SVille Syrjälä 		if (gt_iir)
2133261e40b8SVille Syrjälä 			snb_gt_irq_handler(dev_priv, gt_iir);
213452894874SVille Syrjälä 		if (pm_iir)
213552894874SVille Syrjälä 			gen6_rps_irq_handler(dev_priv, pm_iir);
213652894874SVille Syrjälä 
21371ae3c34cSVille Syrjälä 		if (hotplug_status)
213891d14251STvrtko Ursulin 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
21392ecb8ca4SVille Syrjälä 
214091d14251STvrtko Ursulin 		valleyview_pipestat_irq_handler(dev_priv, pipe_stats);
21411e1cace9SVille Syrjälä 	} while (0);
21427e231dbeSJesse Barnes 
21431f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
21441f814dacSImre Deak 
21457e231dbeSJesse Barnes 	return ret;
21467e231dbeSJesse Barnes }
21477e231dbeSJesse Barnes 
214843f328d7SVille Syrjälä static irqreturn_t cherryview_irq_handler(int irq, void *arg)
214943f328d7SVille Syrjälä {
215045a83f84SDaniel Vetter 	struct drm_device *dev = arg;
2151fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
215243f328d7SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
215343f328d7SVille Syrjälä 
21542dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
21552dd2a883SImre Deak 		return IRQ_NONE;
21562dd2a883SImre Deak 
21571f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
21581f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
21591f814dacSImre Deak 
2160579de73bSChris Wilson 	do {
21616e814800SVille Syrjälä 		u32 master_ctl, iir;
21622ecb8ca4SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
21631ae3c34cSVille Syrjälä 		u32 hotplug_status = 0;
2164f0fd96f5SChris Wilson 		u32 gt_iir[4];
2165a5e485a9SVille Syrjälä 		u32 ier = 0;
2166a5e485a9SVille Syrjälä 
21678e5fd599SVille Syrjälä 		master_ctl = I915_READ(GEN8_MASTER_IRQ) & ~GEN8_MASTER_IRQ_CONTROL;
21683278f67fSVille Syrjälä 		iir = I915_READ(VLV_IIR);
21693278f67fSVille Syrjälä 
21703278f67fSVille Syrjälä 		if (master_ctl == 0 && iir == 0)
21718e5fd599SVille Syrjälä 			break;
217243f328d7SVille Syrjälä 
217327b6c122SOscar Mateo 		ret = IRQ_HANDLED;
217427b6c122SOscar Mateo 
2175a5e485a9SVille Syrjälä 		/*
2176a5e485a9SVille Syrjälä 		 * Theory on interrupt generation, based on empirical evidence:
2177a5e485a9SVille Syrjälä 		 *
2178a5e485a9SVille Syrjälä 		 * x = ((VLV_IIR & VLV_IER) ||
2179a5e485a9SVille Syrjälä 		 *      ((GEN8_MASTER_IRQ & ~GEN8_MASTER_IRQ_CONTROL) &&
2180a5e485a9SVille Syrjälä 		 *       (GEN8_MASTER_IRQ & GEN8_MASTER_IRQ_CONTROL)));
2181a5e485a9SVille Syrjälä 		 *
2182a5e485a9SVille Syrjälä 		 * A CPU interrupt will only be raised when 'x' has a 0->1 edge.
2183a5e485a9SVille Syrjälä 		 * Hence we clear GEN8_MASTER_IRQ_CONTROL and VLV_IER to
2184a5e485a9SVille Syrjälä 		 * guarantee the CPU interrupt will be raised again even if we
2185a5e485a9SVille Syrjälä 		 * don't end up clearing all the VLV_IIR and GEN8_MASTER_IRQ_CONTROL
2186a5e485a9SVille Syrjälä 		 * bits this time around.
2187a5e485a9SVille Syrjälä 		 */
218843f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, 0);
2189a5e485a9SVille Syrjälä 		ier = I915_READ(VLV_IER);
2190a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, 0);
219143f328d7SVille Syrjälä 
2192e30e251aSVille Syrjälä 		gen8_gt_irq_ack(dev_priv, master_ctl, gt_iir);
219327b6c122SOscar Mateo 
219427b6c122SOscar Mateo 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
21951ae3c34cSVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
219643f328d7SVille Syrjälä 
219727b6c122SOscar Mateo 		/* Call regardless, as some status bits might not be
219827b6c122SOscar Mateo 		 * signalled in iir */
2199eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
220043f328d7SVille Syrjälä 
2201eef57324SJerome Anand 		if (iir & (I915_LPE_PIPE_A_INTERRUPT |
2202eef57324SJerome Anand 			   I915_LPE_PIPE_B_INTERRUPT |
2203eef57324SJerome Anand 			   I915_LPE_PIPE_C_INTERRUPT))
2204eef57324SJerome Anand 			intel_lpe_audio_irq_handler(dev_priv);
2205eef57324SJerome Anand 
22067ce4d1f2SVille Syrjälä 		/*
22077ce4d1f2SVille Syrjälä 		 * VLV_IIR is single buffered, and reflects the level
22087ce4d1f2SVille Syrjälä 		 * from PIPESTAT/PORT_HOTPLUG_STAT, hence clear it last.
22097ce4d1f2SVille Syrjälä 		 */
22107ce4d1f2SVille Syrjälä 		if (iir)
22117ce4d1f2SVille Syrjälä 			I915_WRITE(VLV_IIR, iir);
22127ce4d1f2SVille Syrjälä 
2213a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, ier);
2214e5328c43SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
22151ae3c34cSVille Syrjälä 
2216f0fd96f5SChris Wilson 		gen8_gt_irq_handler(dev_priv, master_ctl, gt_iir);
2217e30e251aSVille Syrjälä 
22181ae3c34cSVille Syrjälä 		if (hotplug_status)
221991d14251STvrtko Ursulin 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
22202ecb8ca4SVille Syrjälä 
222191d14251STvrtko Ursulin 		valleyview_pipestat_irq_handler(dev_priv, pipe_stats);
2222579de73bSChris Wilson 	} while (0);
22233278f67fSVille Syrjälä 
22241f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
22251f814dacSImre Deak 
222643f328d7SVille Syrjälä 	return ret;
222743f328d7SVille Syrjälä }
222843f328d7SVille Syrjälä 
222991d14251STvrtko Ursulin static void ibx_hpd_irq_handler(struct drm_i915_private *dev_priv,
223091d14251STvrtko Ursulin 				u32 hotplug_trigger,
223140e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2232776ad806SJesse Barnes {
223342db67d6SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2234776ad806SJesse Barnes 
22356a39d7c9SJani Nikula 	/*
22366a39d7c9SJani Nikula 	 * Somehow the PCH doesn't seem to really ack the interrupt to the CPU
22376a39d7c9SJani Nikula 	 * unless we touch the hotplug register, even if hotplug_trigger is
22386a39d7c9SJani Nikula 	 * zero. Not acking leads to "The master control interrupt lied (SDE)!"
22396a39d7c9SJani Nikula 	 * errors.
22406a39d7c9SJani Nikula 	 */
224113cf5504SDave Airlie 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
22426a39d7c9SJani Nikula 	if (!hotplug_trigger) {
22436a39d7c9SJani Nikula 		u32 mask = PORTA_HOTPLUG_STATUS_MASK |
22446a39d7c9SJani Nikula 			PORTD_HOTPLUG_STATUS_MASK |
22456a39d7c9SJani Nikula 			PORTC_HOTPLUG_STATUS_MASK |
22466a39d7c9SJani Nikula 			PORTB_HOTPLUG_STATUS_MASK;
22476a39d7c9SJani Nikula 		dig_hotplug_reg &= ~mask;
22486a39d7c9SJani Nikula 	}
22496a39d7c9SJani Nikula 
225013cf5504SDave Airlie 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
22516a39d7c9SJani Nikula 	if (!hotplug_trigger)
22526a39d7c9SJani Nikula 		return;
225313cf5504SDave Airlie 
2254cf53902fSRodrigo Vivi 	intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask, hotplug_trigger,
225540e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2256fd63e2a9SImre Deak 			   pch_port_hotplug_long_detect);
225740e56410SVille Syrjälä 
225891d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2259aaf5ec2eSSonika Jindal }
226091d131d2SDaniel Vetter 
226191d14251STvrtko Ursulin static void ibx_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
226240e56410SVille Syrjälä {
226340e56410SVille Syrjälä 	int pipe;
226440e56410SVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
226540e56410SVille Syrjälä 
226691d14251STvrtko Ursulin 	ibx_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ibx);
226740e56410SVille Syrjälä 
2268cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
2269cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
2270776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
2271cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
2272cfc33bf7SVille Syrjälä 				 port_name(port));
2273cfc33bf7SVille Syrjälä 	}
2274776ad806SJesse Barnes 
2275ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
227691d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
2277ce99c256SDaniel Vetter 
2278776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
227991d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
2280776ad806SJesse Barnes 
2281776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
2282776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
2283776ad806SJesse Barnes 
2284776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
2285776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
2286776ad806SJesse Barnes 
2287776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
2288776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
2289776ad806SJesse Barnes 
22909db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
2291055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
22929db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
22939db4a9c7SJesse Barnes 					 pipe_name(pipe),
22949db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
2295776ad806SJesse Barnes 
2296776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
2297776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
2298776ad806SJesse Barnes 
2299776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
2300776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
2301776ad806SJesse Barnes 
2302776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
2303a2196033SMatthias Kaehlcke 		intel_pch_fifo_underrun_irq_handler(dev_priv, PIPE_A);
23048664281bSPaulo Zanoni 
23058664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
2306a2196033SMatthias Kaehlcke 		intel_pch_fifo_underrun_irq_handler(dev_priv, PIPE_B);
23078664281bSPaulo Zanoni }
23088664281bSPaulo Zanoni 
230991d14251STvrtko Ursulin static void ivb_err_int_handler(struct drm_i915_private *dev_priv)
23108664281bSPaulo Zanoni {
23118664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
23125a69b89fSDaniel Vetter 	enum pipe pipe;
23138664281bSPaulo Zanoni 
2314de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
2315de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
2316de032bf4SPaulo Zanoni 
2317055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
23181f7247c0SDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe))
23191f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
23208664281bSPaulo Zanoni 
23215a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
232291d14251STvrtko Ursulin 			if (IS_IVYBRIDGE(dev_priv))
232391d14251STvrtko Ursulin 				ivb_pipe_crc_irq_handler(dev_priv, pipe);
23245a69b89fSDaniel Vetter 			else
232591d14251STvrtko Ursulin 				hsw_pipe_crc_irq_handler(dev_priv, pipe);
23265a69b89fSDaniel Vetter 		}
23275a69b89fSDaniel Vetter 	}
23288bf1e9f1SShuang He 
23298664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
23308664281bSPaulo Zanoni }
23318664281bSPaulo Zanoni 
233291d14251STvrtko Ursulin static void cpt_serr_int_handler(struct drm_i915_private *dev_priv)
23338664281bSPaulo Zanoni {
23348664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
233545c1cd87SMika Kahola 	enum pipe pipe;
23368664281bSPaulo Zanoni 
2337de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
2338de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
2339de032bf4SPaulo Zanoni 
234045c1cd87SMika Kahola 	for_each_pipe(dev_priv, pipe)
234145c1cd87SMika Kahola 		if (serr_int & SERR_INT_TRANS_FIFO_UNDERRUN(pipe))
234245c1cd87SMika Kahola 			intel_pch_fifo_underrun_irq_handler(dev_priv, pipe);
23438664281bSPaulo Zanoni 
23448664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
2345776ad806SJesse Barnes }
2346776ad806SJesse Barnes 
234791d14251STvrtko Ursulin static void cpt_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
234823e81d69SAdam Jackson {
234923e81d69SAdam Jackson 	int pipe;
23506dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
2351aaf5ec2eSSonika Jindal 
235291d14251STvrtko Ursulin 	ibx_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_cpt);
235391d131d2SDaniel Vetter 
2354cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
2355cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
235623e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
2357cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
2358cfc33bf7SVille Syrjälä 				 port_name(port));
2359cfc33bf7SVille Syrjälä 	}
236023e81d69SAdam Jackson 
236123e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
236291d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
236323e81d69SAdam Jackson 
236423e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
236591d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
236623e81d69SAdam Jackson 
236723e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
236823e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
236923e81d69SAdam Jackson 
237023e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
237123e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
237223e81d69SAdam Jackson 
237323e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
2374055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
237523e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
237623e81d69SAdam Jackson 					 pipe_name(pipe),
237723e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
23788664281bSPaulo Zanoni 
23798664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
238091d14251STvrtko Ursulin 		cpt_serr_int_handler(dev_priv);
238123e81d69SAdam Jackson }
238223e81d69SAdam Jackson 
238331604222SAnusha Srivatsa static void icp_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
238431604222SAnusha Srivatsa {
238531604222SAnusha Srivatsa 	u32 ddi_hotplug_trigger = pch_iir & SDE_DDI_MASK_ICP;
238631604222SAnusha Srivatsa 	u32 tc_hotplug_trigger = pch_iir & SDE_TC_MASK_ICP;
238731604222SAnusha Srivatsa 	u32 pin_mask = 0, long_mask = 0;
238831604222SAnusha Srivatsa 
238931604222SAnusha Srivatsa 	if (ddi_hotplug_trigger) {
239031604222SAnusha Srivatsa 		u32 dig_hotplug_reg;
239131604222SAnusha Srivatsa 
239231604222SAnusha Srivatsa 		dig_hotplug_reg = I915_READ(SHOTPLUG_CTL_DDI);
239331604222SAnusha Srivatsa 		I915_WRITE(SHOTPLUG_CTL_DDI, dig_hotplug_reg);
239431604222SAnusha Srivatsa 
239531604222SAnusha Srivatsa 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
239631604222SAnusha Srivatsa 				   ddi_hotplug_trigger,
239731604222SAnusha Srivatsa 				   dig_hotplug_reg, hpd_icp,
239831604222SAnusha Srivatsa 				   icp_ddi_port_hotplug_long_detect);
239931604222SAnusha Srivatsa 	}
240031604222SAnusha Srivatsa 
240131604222SAnusha Srivatsa 	if (tc_hotplug_trigger) {
240231604222SAnusha Srivatsa 		u32 dig_hotplug_reg;
240331604222SAnusha Srivatsa 
240431604222SAnusha Srivatsa 		dig_hotplug_reg = I915_READ(SHOTPLUG_CTL_TC);
240531604222SAnusha Srivatsa 		I915_WRITE(SHOTPLUG_CTL_TC, dig_hotplug_reg);
240631604222SAnusha Srivatsa 
240731604222SAnusha Srivatsa 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
240831604222SAnusha Srivatsa 				   tc_hotplug_trigger,
240931604222SAnusha Srivatsa 				   dig_hotplug_reg, hpd_icp,
241031604222SAnusha Srivatsa 				   icp_tc_port_hotplug_long_detect);
241131604222SAnusha Srivatsa 	}
241231604222SAnusha Srivatsa 
241331604222SAnusha Srivatsa 	if (pin_mask)
241431604222SAnusha Srivatsa 		intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
241531604222SAnusha Srivatsa 
241631604222SAnusha Srivatsa 	if (pch_iir & SDE_GMBUS_ICP)
241731604222SAnusha Srivatsa 		gmbus_irq_handler(dev_priv);
241831604222SAnusha Srivatsa }
241931604222SAnusha Srivatsa 
242091d14251STvrtko Ursulin static void spt_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
24216dbf30ceSVille Syrjälä {
24226dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_SPT &
24236dbf30ceSVille Syrjälä 		~SDE_PORTE_HOTPLUG_SPT;
24246dbf30ceSVille Syrjälä 	u32 hotplug2_trigger = pch_iir & SDE_PORTE_HOTPLUG_SPT;
24256dbf30ceSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
24266dbf30ceSVille Syrjälä 
24276dbf30ceSVille Syrjälä 	if (hotplug_trigger) {
24286dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
24296dbf30ceSVille Syrjälä 
24306dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
24316dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
24326dbf30ceSVille Syrjälä 
2433cf53902fSRodrigo Vivi 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
2434cf53902fSRodrigo Vivi 				   hotplug_trigger, dig_hotplug_reg, hpd_spt,
243574c0b395SVille Syrjälä 				   spt_port_hotplug_long_detect);
24366dbf30ceSVille Syrjälä 	}
24376dbf30ceSVille Syrjälä 
24386dbf30ceSVille Syrjälä 	if (hotplug2_trigger) {
24396dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
24406dbf30ceSVille Syrjälä 
24416dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG2);
24426dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG2, dig_hotplug_reg);
24436dbf30ceSVille Syrjälä 
2444cf53902fSRodrigo Vivi 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
2445cf53902fSRodrigo Vivi 				   hotplug2_trigger, dig_hotplug_reg, hpd_spt,
24466dbf30ceSVille Syrjälä 				   spt_port_hotplug2_long_detect);
24476dbf30ceSVille Syrjälä 	}
24486dbf30ceSVille Syrjälä 
24496dbf30ceSVille Syrjälä 	if (pin_mask)
245091d14251STvrtko Ursulin 		intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
24516dbf30ceSVille Syrjälä 
24526dbf30ceSVille Syrjälä 	if (pch_iir & SDE_GMBUS_CPT)
245391d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
24546dbf30ceSVille Syrjälä }
24556dbf30ceSVille Syrjälä 
245691d14251STvrtko Ursulin static void ilk_hpd_irq_handler(struct drm_i915_private *dev_priv,
245791d14251STvrtko Ursulin 				u32 hotplug_trigger,
245840e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2459c008bc6eSPaulo Zanoni {
2460e4ce95aaSVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2461e4ce95aaSVille Syrjälä 
2462e4ce95aaSVille Syrjälä 	dig_hotplug_reg = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
2463e4ce95aaSVille Syrjälä 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, dig_hotplug_reg);
2464e4ce95aaSVille Syrjälä 
2465cf53902fSRodrigo Vivi 	intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask, hotplug_trigger,
246640e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2467e4ce95aaSVille Syrjälä 			   ilk_port_hotplug_long_detect);
246840e56410SVille Syrjälä 
246991d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2470e4ce95aaSVille Syrjälä }
2471c008bc6eSPaulo Zanoni 
247291d14251STvrtko Ursulin static void ilk_display_irq_handler(struct drm_i915_private *dev_priv,
247391d14251STvrtko Ursulin 				    u32 de_iir)
247440e56410SVille Syrjälä {
247540e56410SVille Syrjälä 	enum pipe pipe;
247640e56410SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG;
247740e56410SVille Syrjälä 
247840e56410SVille Syrjälä 	if (hotplug_trigger)
247991d14251STvrtko Ursulin 		ilk_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ilk);
248040e56410SVille Syrjälä 
2481c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
248291d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
2483c008bc6eSPaulo Zanoni 
2484c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
248591d14251STvrtko Ursulin 		intel_opregion_asle_intr(dev_priv);
2486c008bc6eSPaulo Zanoni 
2487c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
2488c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
2489c008bc6eSPaulo Zanoni 
2490055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2491fd3a4024SDaniel Vetter 		if (de_iir & DE_PIPE_VBLANK(pipe))
2492fd3a4024SDaniel Vetter 			drm_handle_vblank(&dev_priv->drm, pipe);
2493c008bc6eSPaulo Zanoni 
249440da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
24951f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
2496c008bc6eSPaulo Zanoni 
249740da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
249891d14251STvrtko Ursulin 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
2499c008bc6eSPaulo Zanoni 	}
2500c008bc6eSPaulo Zanoni 
2501c008bc6eSPaulo Zanoni 	/* check event from PCH */
2502c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
2503c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
2504c008bc6eSPaulo Zanoni 
250591d14251STvrtko Ursulin 		if (HAS_PCH_CPT(dev_priv))
250691d14251STvrtko Ursulin 			cpt_irq_handler(dev_priv, pch_iir);
2507c008bc6eSPaulo Zanoni 		else
250891d14251STvrtko Ursulin 			ibx_irq_handler(dev_priv, pch_iir);
2509c008bc6eSPaulo Zanoni 
2510c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
2511c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
2512c008bc6eSPaulo Zanoni 	}
2513c008bc6eSPaulo Zanoni 
2514cf819effSLucas De Marchi 	if (IS_GEN(dev_priv, 5) && de_iir & DE_PCU_EVENT)
251591d14251STvrtko Ursulin 		ironlake_rps_change_irq_handler(dev_priv);
2516c008bc6eSPaulo Zanoni }
2517c008bc6eSPaulo Zanoni 
251891d14251STvrtko Ursulin static void ivb_display_irq_handler(struct drm_i915_private *dev_priv,
251991d14251STvrtko Ursulin 				    u32 de_iir)
25209719fb98SPaulo Zanoni {
252107d27e20SDamien Lespiau 	enum pipe pipe;
252223bb4cb5SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG_IVB;
252323bb4cb5SVille Syrjälä 
252440e56410SVille Syrjälä 	if (hotplug_trigger)
252591d14251STvrtko Ursulin 		ilk_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ivb);
25269719fb98SPaulo Zanoni 
25279719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
252891d14251STvrtko Ursulin 		ivb_err_int_handler(dev_priv);
25299719fb98SPaulo Zanoni 
253054fd3149SDhinakaran Pandiyan 	if (de_iir & DE_EDP_PSR_INT_HSW) {
253154fd3149SDhinakaran Pandiyan 		u32 psr_iir = I915_READ(EDP_PSR_IIR);
253254fd3149SDhinakaran Pandiyan 
253354fd3149SDhinakaran Pandiyan 		intel_psr_irq_handler(dev_priv, psr_iir);
253454fd3149SDhinakaran Pandiyan 		I915_WRITE(EDP_PSR_IIR, psr_iir);
253554fd3149SDhinakaran Pandiyan 	}
2536fc340442SDaniel Vetter 
25379719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
253891d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
25399719fb98SPaulo Zanoni 
25409719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
254191d14251STvrtko Ursulin 		intel_opregion_asle_intr(dev_priv);
25429719fb98SPaulo Zanoni 
2543055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2544fd3a4024SDaniel Vetter 		if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)))
2545fd3a4024SDaniel Vetter 			drm_handle_vblank(&dev_priv->drm, pipe);
25469719fb98SPaulo Zanoni 	}
25479719fb98SPaulo Zanoni 
25489719fb98SPaulo Zanoni 	/* check event from PCH */
254991d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv) && (de_iir & DE_PCH_EVENT_IVB)) {
25509719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
25519719fb98SPaulo Zanoni 
255291d14251STvrtko Ursulin 		cpt_irq_handler(dev_priv, pch_iir);
25539719fb98SPaulo Zanoni 
25549719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
25559719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
25569719fb98SPaulo Zanoni 	}
25579719fb98SPaulo Zanoni }
25589719fb98SPaulo Zanoni 
255972c90f62SOscar Mateo /*
256072c90f62SOscar Mateo  * To handle irqs with the minimum potential races with fresh interrupts, we:
256172c90f62SOscar Mateo  * 1 - Disable Master Interrupt Control.
256272c90f62SOscar Mateo  * 2 - Find the source(s) of the interrupt.
256372c90f62SOscar Mateo  * 3 - Clear the Interrupt Identity bits (IIR).
256472c90f62SOscar Mateo  * 4 - Process the interrupt(s) that had bits set in the IIRs.
256572c90f62SOscar Mateo  * 5 - Re-enable Master Interrupt Control.
256672c90f62SOscar Mateo  */
2567f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
2568b1f14ad0SJesse Barnes {
256945a83f84SDaniel Vetter 	struct drm_device *dev = arg;
2570fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2571f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
25720e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
2573b1f14ad0SJesse Barnes 
25742dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
25752dd2a883SImre Deak 		return IRQ_NONE;
25762dd2a883SImre Deak 
25771f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
25781f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
25791f814dacSImre Deak 
2580b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
2581b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
2582b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
25830e43406bSChris Wilson 
258444498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
258544498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
258644498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
258744498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
258844498aeaSPaulo Zanoni 	 * due to its back queue). */
258991d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv)) {
259044498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
259144498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
2592ab5c608bSBen Widawsky 	}
259344498aeaSPaulo Zanoni 
259472c90f62SOscar Mateo 	/* Find, clear, then process each source of interrupt */
259572c90f62SOscar Mateo 
25960e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
25970e43406bSChris Wilson 	if (gt_iir) {
259872c90f62SOscar Mateo 		I915_WRITE(GTIIR, gt_iir);
259972c90f62SOscar Mateo 		ret = IRQ_HANDLED;
260091d14251STvrtko Ursulin 		if (INTEL_GEN(dev_priv) >= 6)
2601261e40b8SVille Syrjälä 			snb_gt_irq_handler(dev_priv, gt_iir);
2602d8fc8a47SPaulo Zanoni 		else
2603261e40b8SVille Syrjälä 			ilk_gt_irq_handler(dev_priv, gt_iir);
26040e43406bSChris Wilson 	}
2605b1f14ad0SJesse Barnes 
2606b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
26070e43406bSChris Wilson 	if (de_iir) {
260872c90f62SOscar Mateo 		I915_WRITE(DEIIR, de_iir);
260972c90f62SOscar Mateo 		ret = IRQ_HANDLED;
261091d14251STvrtko Ursulin 		if (INTEL_GEN(dev_priv) >= 7)
261191d14251STvrtko Ursulin 			ivb_display_irq_handler(dev_priv, de_iir);
2612f1af8fc1SPaulo Zanoni 		else
261391d14251STvrtko Ursulin 			ilk_display_irq_handler(dev_priv, de_iir);
26140e43406bSChris Wilson 	}
26150e43406bSChris Wilson 
261691d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 6) {
2617f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
26180e43406bSChris Wilson 		if (pm_iir) {
2619b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
26200e43406bSChris Wilson 			ret = IRQ_HANDLED;
262172c90f62SOscar Mateo 			gen6_rps_irq_handler(dev_priv, pm_iir);
26220e43406bSChris Wilson 		}
2623f1af8fc1SPaulo Zanoni 	}
2624b1f14ad0SJesse Barnes 
2625b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
262674093f3eSChris Wilson 	if (!HAS_PCH_NOP(dev_priv))
262744498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
2628b1f14ad0SJesse Barnes 
26291f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
26301f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
26311f814dacSImre Deak 
2632b1f14ad0SJesse Barnes 	return ret;
2633b1f14ad0SJesse Barnes }
2634b1f14ad0SJesse Barnes 
263591d14251STvrtko Ursulin static void bxt_hpd_irq_handler(struct drm_i915_private *dev_priv,
263691d14251STvrtko Ursulin 				u32 hotplug_trigger,
263740e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2638d04a492dSShashank Sharma {
2639cebd87a0SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2640d04a492dSShashank Sharma 
2641a52bb15bSVille Syrjälä 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
2642a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
2643d04a492dSShashank Sharma 
2644cf53902fSRodrigo Vivi 	intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask, hotplug_trigger,
264540e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2646cebd87a0SVille Syrjälä 			   bxt_port_hotplug_long_detect);
264740e56410SVille Syrjälä 
264891d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2649d04a492dSShashank Sharma }
2650d04a492dSShashank Sharma 
2651121e758eSDhinakaran Pandiyan static void gen11_hpd_irq_handler(struct drm_i915_private *dev_priv, u32 iir)
2652121e758eSDhinakaran Pandiyan {
2653121e758eSDhinakaran Pandiyan 	u32 pin_mask = 0, long_mask = 0;
2654b796b971SDhinakaran Pandiyan 	u32 trigger_tc = iir & GEN11_DE_TC_HOTPLUG_MASK;
2655b796b971SDhinakaran Pandiyan 	u32 trigger_tbt = iir & GEN11_DE_TBT_HOTPLUG_MASK;
2656121e758eSDhinakaran Pandiyan 
2657121e758eSDhinakaran Pandiyan 	if (trigger_tc) {
2658b796b971SDhinakaran Pandiyan 		u32 dig_hotplug_reg;
2659b796b971SDhinakaran Pandiyan 
2660121e758eSDhinakaran Pandiyan 		dig_hotplug_reg = I915_READ(GEN11_TC_HOTPLUG_CTL);
2661121e758eSDhinakaran Pandiyan 		I915_WRITE(GEN11_TC_HOTPLUG_CTL, dig_hotplug_reg);
2662121e758eSDhinakaran Pandiyan 
2663121e758eSDhinakaran Pandiyan 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask, trigger_tc,
2664b796b971SDhinakaran Pandiyan 				   dig_hotplug_reg, hpd_gen11,
2665121e758eSDhinakaran Pandiyan 				   gen11_port_hotplug_long_detect);
2666121e758eSDhinakaran Pandiyan 	}
2667b796b971SDhinakaran Pandiyan 
2668b796b971SDhinakaran Pandiyan 	if (trigger_tbt) {
2669b796b971SDhinakaran Pandiyan 		u32 dig_hotplug_reg;
2670b796b971SDhinakaran Pandiyan 
2671b796b971SDhinakaran Pandiyan 		dig_hotplug_reg = I915_READ(GEN11_TBT_HOTPLUG_CTL);
2672b796b971SDhinakaran Pandiyan 		I915_WRITE(GEN11_TBT_HOTPLUG_CTL, dig_hotplug_reg);
2673b796b971SDhinakaran Pandiyan 
2674b796b971SDhinakaran Pandiyan 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask, trigger_tbt,
2675b796b971SDhinakaran Pandiyan 				   dig_hotplug_reg, hpd_gen11,
2676b796b971SDhinakaran Pandiyan 				   gen11_port_hotplug_long_detect);
2677b796b971SDhinakaran Pandiyan 	}
2678b796b971SDhinakaran Pandiyan 
2679b796b971SDhinakaran Pandiyan 	if (pin_mask)
2680b796b971SDhinakaran Pandiyan 		intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2681b796b971SDhinakaran Pandiyan 	else
2682b796b971SDhinakaran Pandiyan 		DRM_ERROR("Unexpected DE HPD interrupt 0x%08x\n", iir);
2683121e758eSDhinakaran Pandiyan }
2684121e758eSDhinakaran Pandiyan 
26859d17210fSLucas De Marchi static u32 gen8_de_port_aux_mask(struct drm_i915_private *dev_priv)
26869d17210fSLucas De Marchi {
26879d17210fSLucas De Marchi 	u32 mask = GEN8_AUX_CHANNEL_A;
26889d17210fSLucas De Marchi 
26899d17210fSLucas De Marchi 	if (INTEL_GEN(dev_priv) >= 9)
26909d17210fSLucas De Marchi 		mask |= GEN9_AUX_CHANNEL_B |
26919d17210fSLucas De Marchi 			GEN9_AUX_CHANNEL_C |
26929d17210fSLucas De Marchi 			GEN9_AUX_CHANNEL_D;
26939d17210fSLucas De Marchi 
26949d17210fSLucas De Marchi 	if (IS_CNL_WITH_PORT_F(dev_priv))
26959d17210fSLucas De Marchi 		mask |= CNL_AUX_CHANNEL_F;
26969d17210fSLucas De Marchi 
26979d17210fSLucas De Marchi 	if (INTEL_GEN(dev_priv) >= 11)
26989d17210fSLucas De Marchi 		mask |= ICL_AUX_CHANNEL_E |
26999d17210fSLucas De Marchi 			CNL_AUX_CHANNEL_F;
27009d17210fSLucas De Marchi 
27019d17210fSLucas De Marchi 	return mask;
27029d17210fSLucas De Marchi }
27039d17210fSLucas De Marchi 
2704f11a0f46STvrtko Ursulin static irqreturn_t
2705f11a0f46STvrtko Ursulin gen8_de_irq_handler(struct drm_i915_private *dev_priv, u32 master_ctl)
2706abd58f01SBen Widawsky {
2707abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
2708f11a0f46STvrtko Ursulin 	u32 iir;
2709c42664ccSDaniel Vetter 	enum pipe pipe;
271088e04703SJesse Barnes 
2711abd58f01SBen Widawsky 	if (master_ctl & GEN8_DE_MISC_IRQ) {
2712e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_MISC_IIR);
2713e32192e1STvrtko Ursulin 		if (iir) {
2714e04f7eceSVille Syrjälä 			bool found = false;
2715e04f7eceSVille Syrjälä 
2716e32192e1STvrtko Ursulin 			I915_WRITE(GEN8_DE_MISC_IIR, iir);
2717abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
2718e04f7eceSVille Syrjälä 
2719e04f7eceSVille Syrjälä 			if (iir & GEN8_DE_MISC_GSE) {
272091d14251STvrtko Ursulin 				intel_opregion_asle_intr(dev_priv);
2721e04f7eceSVille Syrjälä 				found = true;
2722e04f7eceSVille Syrjälä 			}
2723e04f7eceSVille Syrjälä 
2724e04f7eceSVille Syrjälä 			if (iir & GEN8_DE_EDP_PSR) {
272554fd3149SDhinakaran Pandiyan 				u32 psr_iir = I915_READ(EDP_PSR_IIR);
272654fd3149SDhinakaran Pandiyan 
272754fd3149SDhinakaran Pandiyan 				intel_psr_irq_handler(dev_priv, psr_iir);
272854fd3149SDhinakaran Pandiyan 				I915_WRITE(EDP_PSR_IIR, psr_iir);
2729e04f7eceSVille Syrjälä 				found = true;
2730e04f7eceSVille Syrjälä 			}
2731e04f7eceSVille Syrjälä 
2732e04f7eceSVille Syrjälä 			if (!found)
273338cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Misc interrupt\n");
2734abd58f01SBen Widawsky 		}
273538cc46d7SOscar Mateo 		else
273638cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
2737abd58f01SBen Widawsky 	}
2738abd58f01SBen Widawsky 
2739121e758eSDhinakaran Pandiyan 	if (INTEL_GEN(dev_priv) >= 11 && (master_ctl & GEN11_DE_HPD_IRQ)) {
2740121e758eSDhinakaran Pandiyan 		iir = I915_READ(GEN11_DE_HPD_IIR);
2741121e758eSDhinakaran Pandiyan 		if (iir) {
2742121e758eSDhinakaran Pandiyan 			I915_WRITE(GEN11_DE_HPD_IIR, iir);
2743121e758eSDhinakaran Pandiyan 			ret = IRQ_HANDLED;
2744121e758eSDhinakaran Pandiyan 			gen11_hpd_irq_handler(dev_priv, iir);
2745121e758eSDhinakaran Pandiyan 		} else {
2746121e758eSDhinakaran Pandiyan 			DRM_ERROR("The master control interrupt lied, (DE HPD)!\n");
2747121e758eSDhinakaran Pandiyan 		}
2748121e758eSDhinakaran Pandiyan 	}
2749121e758eSDhinakaran Pandiyan 
27506d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
2751e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_PORT_IIR);
2752e32192e1STvrtko Ursulin 		if (iir) {
2753e32192e1STvrtko Ursulin 			u32 tmp_mask;
2754d04a492dSShashank Sharma 			bool found = false;
2755cebd87a0SVille Syrjälä 
2756e32192e1STvrtko Ursulin 			I915_WRITE(GEN8_DE_PORT_IIR, iir);
27576d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
275888e04703SJesse Barnes 
27599d17210fSLucas De Marchi 			if (iir & gen8_de_port_aux_mask(dev_priv)) {
276091d14251STvrtko Ursulin 				dp_aux_irq_handler(dev_priv);
2761d04a492dSShashank Sharma 				found = true;
2762d04a492dSShashank Sharma 			}
2763d04a492dSShashank Sharma 
2764cc3f90f0SAnder Conselvan de Oliveira 			if (IS_GEN9_LP(dev_priv)) {
2765e32192e1STvrtko Ursulin 				tmp_mask = iir & BXT_DE_PORT_HOTPLUG_MASK;
2766e32192e1STvrtko Ursulin 				if (tmp_mask) {
276791d14251STvrtko Ursulin 					bxt_hpd_irq_handler(dev_priv, tmp_mask,
276891d14251STvrtko Ursulin 							    hpd_bxt);
2769d04a492dSShashank Sharma 					found = true;
2770d04a492dSShashank Sharma 				}
2771e32192e1STvrtko Ursulin 			} else if (IS_BROADWELL(dev_priv)) {
2772e32192e1STvrtko Ursulin 				tmp_mask = iir & GEN8_PORT_DP_A_HOTPLUG;
2773e32192e1STvrtko Ursulin 				if (tmp_mask) {
277491d14251STvrtko Ursulin 					ilk_hpd_irq_handler(dev_priv,
277591d14251STvrtko Ursulin 							    tmp_mask, hpd_bdw);
2776e32192e1STvrtko Ursulin 					found = true;
2777e32192e1STvrtko Ursulin 				}
2778e32192e1STvrtko Ursulin 			}
2779d04a492dSShashank Sharma 
2780cc3f90f0SAnder Conselvan de Oliveira 			if (IS_GEN9_LP(dev_priv) && (iir & BXT_DE_PORT_GMBUS)) {
278191d14251STvrtko Ursulin 				gmbus_irq_handler(dev_priv);
27829e63743eSShashank Sharma 				found = true;
27839e63743eSShashank Sharma 			}
27849e63743eSShashank Sharma 
2785d04a492dSShashank Sharma 			if (!found)
278638cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Port interrupt\n");
27876d766f02SDaniel Vetter 		}
278838cc46d7SOscar Mateo 		else
278938cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
27906d766f02SDaniel Vetter 	}
27916d766f02SDaniel Vetter 
2792055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2793fd3a4024SDaniel Vetter 		u32 fault_errors;
2794abd58f01SBen Widawsky 
2795c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2796c42664ccSDaniel Vetter 			continue;
2797c42664ccSDaniel Vetter 
2798e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
2799e32192e1STvrtko Ursulin 		if (!iir) {
2800e32192e1STvrtko Ursulin 			DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
2801e32192e1STvrtko Ursulin 			continue;
2802e32192e1STvrtko Ursulin 		}
2803770de83dSDamien Lespiau 
2804e32192e1STvrtko Ursulin 		ret = IRQ_HANDLED;
2805e32192e1STvrtko Ursulin 		I915_WRITE(GEN8_DE_PIPE_IIR(pipe), iir);
2806e32192e1STvrtko Ursulin 
2807fd3a4024SDaniel Vetter 		if (iir & GEN8_PIPE_VBLANK)
2808fd3a4024SDaniel Vetter 			drm_handle_vblank(&dev_priv->drm, pipe);
2809abd58f01SBen Widawsky 
2810e32192e1STvrtko Ursulin 		if (iir & GEN8_PIPE_CDCLK_CRC_DONE)
281191d14251STvrtko Ursulin 			hsw_pipe_crc_irq_handler(dev_priv, pipe);
28120fbe7870SDaniel Vetter 
2813e32192e1STvrtko Ursulin 		if (iir & GEN8_PIPE_FIFO_UNDERRUN)
2814e32192e1STvrtko Ursulin 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
281538d83c96SDaniel Vetter 
2816e32192e1STvrtko Ursulin 		fault_errors = iir;
2817bca2bf2aSPandiyan, Dhinakaran 		if (INTEL_GEN(dev_priv) >= 9)
2818e32192e1STvrtko Ursulin 			fault_errors &= GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
2819770de83dSDamien Lespiau 		else
2820e32192e1STvrtko Ursulin 			fault_errors &= GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
2821770de83dSDamien Lespiau 
2822770de83dSDamien Lespiau 		if (fault_errors)
28231353ec38STvrtko Ursulin 			DRM_ERROR("Fault errors on pipe %c: 0x%08x\n",
282430100f2bSDaniel Vetter 				  pipe_name(pipe),
2825e32192e1STvrtko Ursulin 				  fault_errors);
2826abd58f01SBen Widawsky 	}
2827abd58f01SBen Widawsky 
282891d14251STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv) && !HAS_PCH_NOP(dev_priv) &&
2829266ea3d9SShashank Sharma 	    master_ctl & GEN8_DE_PCH_IRQ) {
283092d03a80SDaniel Vetter 		/*
283192d03a80SDaniel Vetter 		 * FIXME(BDW): Assume for now that the new interrupt handling
283292d03a80SDaniel Vetter 		 * scheme also closed the SDE interrupt handling race we've seen
283392d03a80SDaniel Vetter 		 * on older pch-split platforms. But this needs testing.
283492d03a80SDaniel Vetter 		 */
2835e32192e1STvrtko Ursulin 		iir = I915_READ(SDEIIR);
2836e32192e1STvrtko Ursulin 		if (iir) {
2837e32192e1STvrtko Ursulin 			I915_WRITE(SDEIIR, iir);
283892d03a80SDaniel Vetter 			ret = IRQ_HANDLED;
28396dbf30ceSVille Syrjälä 
284029b43ae2SRodrigo Vivi 			if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
284131604222SAnusha Srivatsa 				icp_irq_handler(dev_priv, iir);
2842c6c30b91SRodrigo Vivi 			else if (INTEL_PCH_TYPE(dev_priv) >= PCH_SPT)
284391d14251STvrtko Ursulin 				spt_irq_handler(dev_priv, iir);
28446dbf30ceSVille Syrjälä 			else
284591d14251STvrtko Ursulin 				cpt_irq_handler(dev_priv, iir);
28462dfb0b81SJani Nikula 		} else {
28472dfb0b81SJani Nikula 			/*
28482dfb0b81SJani Nikula 			 * Like on previous PCH there seems to be something
28492dfb0b81SJani Nikula 			 * fishy going on with forwarding PCH interrupts.
28502dfb0b81SJani Nikula 			 */
28512dfb0b81SJani Nikula 			DRM_DEBUG_DRIVER("The master control interrupt lied (SDE)!\n");
28522dfb0b81SJani Nikula 		}
285392d03a80SDaniel Vetter 	}
285492d03a80SDaniel Vetter 
2855f11a0f46STvrtko Ursulin 	return ret;
2856f11a0f46STvrtko Ursulin }
2857f11a0f46STvrtko Ursulin 
28584376b9c9SMika Kuoppala static inline u32 gen8_master_intr_disable(void __iomem * const regs)
28594376b9c9SMika Kuoppala {
28604376b9c9SMika Kuoppala 	raw_reg_write(regs, GEN8_MASTER_IRQ, 0);
28614376b9c9SMika Kuoppala 
28624376b9c9SMika Kuoppala 	/*
28634376b9c9SMika Kuoppala 	 * Now with master disabled, get a sample of level indications
28644376b9c9SMika Kuoppala 	 * for this interrupt. Indications will be cleared on related acks.
28654376b9c9SMika Kuoppala 	 * New indications can and will light up during processing,
28664376b9c9SMika Kuoppala 	 * and will generate new interrupt after enabling master.
28674376b9c9SMika Kuoppala 	 */
28684376b9c9SMika Kuoppala 	return raw_reg_read(regs, GEN8_MASTER_IRQ);
28694376b9c9SMika Kuoppala }
28704376b9c9SMika Kuoppala 
28714376b9c9SMika Kuoppala static inline void gen8_master_intr_enable(void __iomem * const regs)
28724376b9c9SMika Kuoppala {
28734376b9c9SMika Kuoppala 	raw_reg_write(regs, GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
28744376b9c9SMika Kuoppala }
28754376b9c9SMika Kuoppala 
2876f11a0f46STvrtko Ursulin static irqreturn_t gen8_irq_handler(int irq, void *arg)
2877f11a0f46STvrtko Ursulin {
2878f0fd96f5SChris Wilson 	struct drm_i915_private *dev_priv = to_i915(arg);
28794376b9c9SMika Kuoppala 	void __iomem * const regs = dev_priv->regs;
2880f11a0f46STvrtko Ursulin 	u32 master_ctl;
2881f0fd96f5SChris Wilson 	u32 gt_iir[4];
2882f11a0f46STvrtko Ursulin 
2883f11a0f46STvrtko Ursulin 	if (!intel_irqs_enabled(dev_priv))
2884f11a0f46STvrtko Ursulin 		return IRQ_NONE;
2885f11a0f46STvrtko Ursulin 
28864376b9c9SMika Kuoppala 	master_ctl = gen8_master_intr_disable(regs);
28874376b9c9SMika Kuoppala 	if (!master_ctl) {
28884376b9c9SMika Kuoppala 		gen8_master_intr_enable(regs);
2889f11a0f46STvrtko Ursulin 		return IRQ_NONE;
28904376b9c9SMika Kuoppala 	}
2891f11a0f46STvrtko Ursulin 
2892f11a0f46STvrtko Ursulin 	/* Find, clear, then process each source of interrupt */
289355ef72f2SChris Wilson 	gen8_gt_irq_ack(dev_priv, master_ctl, gt_iir);
2894f0fd96f5SChris Wilson 
2895f0fd96f5SChris Wilson 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
2896f0fd96f5SChris Wilson 	if (master_ctl & ~GEN8_GT_IRQS) {
2897f0fd96f5SChris Wilson 		disable_rpm_wakeref_asserts(dev_priv);
289855ef72f2SChris Wilson 		gen8_de_irq_handler(dev_priv, master_ctl);
2899f0fd96f5SChris Wilson 		enable_rpm_wakeref_asserts(dev_priv);
2900f0fd96f5SChris Wilson 	}
2901f11a0f46STvrtko Ursulin 
29024376b9c9SMika Kuoppala 	gen8_master_intr_enable(regs);
2903abd58f01SBen Widawsky 
2904f0fd96f5SChris Wilson 	gen8_gt_irq_handler(dev_priv, master_ctl, gt_iir);
29051f814dacSImre Deak 
290655ef72f2SChris Wilson 	return IRQ_HANDLED;
2907abd58f01SBen Widawsky }
2908abd58f01SBen Widawsky 
290951951ae7SMika Kuoppala static u32
2910f744dbc2SMika Kuoppala gen11_gt_engine_identity(struct drm_i915_private * const i915,
291151951ae7SMika Kuoppala 			 const unsigned int bank, const unsigned int bit)
291251951ae7SMika Kuoppala {
291351951ae7SMika Kuoppala 	void __iomem * const regs = i915->regs;
291451951ae7SMika Kuoppala 	u32 timeout_ts;
291551951ae7SMika Kuoppala 	u32 ident;
291651951ae7SMika Kuoppala 
291796606f3bSOscar Mateo 	lockdep_assert_held(&i915->irq_lock);
291896606f3bSOscar Mateo 
291951951ae7SMika Kuoppala 	raw_reg_write(regs, GEN11_IIR_REG_SELECTOR(bank), BIT(bit));
292051951ae7SMika Kuoppala 
292151951ae7SMika Kuoppala 	/*
292251951ae7SMika Kuoppala 	 * NB: Specs do not specify how long to spin wait,
292351951ae7SMika Kuoppala 	 * so we do ~100us as an educated guess.
292451951ae7SMika Kuoppala 	 */
292551951ae7SMika Kuoppala 	timeout_ts = (local_clock() >> 10) + 100;
292651951ae7SMika Kuoppala 	do {
292751951ae7SMika Kuoppala 		ident = raw_reg_read(regs, GEN11_INTR_IDENTITY_REG(bank));
292851951ae7SMika Kuoppala 	} while (!(ident & GEN11_INTR_DATA_VALID) &&
292951951ae7SMika Kuoppala 		 !time_after32(local_clock() >> 10, timeout_ts));
293051951ae7SMika Kuoppala 
293151951ae7SMika Kuoppala 	if (unlikely(!(ident & GEN11_INTR_DATA_VALID))) {
293251951ae7SMika Kuoppala 		DRM_ERROR("INTR_IDENTITY_REG%u:%u 0x%08x not valid!\n",
293351951ae7SMika Kuoppala 			  bank, bit, ident);
293451951ae7SMika Kuoppala 		return 0;
293551951ae7SMika Kuoppala 	}
293651951ae7SMika Kuoppala 
293751951ae7SMika Kuoppala 	raw_reg_write(regs, GEN11_INTR_IDENTITY_REG(bank),
293851951ae7SMika Kuoppala 		      GEN11_INTR_DATA_VALID);
293951951ae7SMika Kuoppala 
2940f744dbc2SMika Kuoppala 	return ident;
2941f744dbc2SMika Kuoppala }
2942f744dbc2SMika Kuoppala 
2943f744dbc2SMika Kuoppala static void
2944f744dbc2SMika Kuoppala gen11_other_irq_handler(struct drm_i915_private * const i915,
2945f744dbc2SMika Kuoppala 			const u8 instance, const u16 iir)
2946f744dbc2SMika Kuoppala {
2947d02b98b8SOscar Mateo 	if (instance == OTHER_GTPM_INSTANCE)
2948d02b98b8SOscar Mateo 		return gen6_rps_irq_handler(i915, iir);
2949d02b98b8SOscar Mateo 
2950f744dbc2SMika Kuoppala 	WARN_ONCE(1, "unhandled other interrupt instance=0x%x, iir=0x%x\n",
2951f744dbc2SMika Kuoppala 		  instance, iir);
2952f744dbc2SMika Kuoppala }
2953f744dbc2SMika Kuoppala 
2954f744dbc2SMika Kuoppala static void
2955f744dbc2SMika Kuoppala gen11_engine_irq_handler(struct drm_i915_private * const i915,
2956f744dbc2SMika Kuoppala 			 const u8 class, const u8 instance, const u16 iir)
2957f744dbc2SMika Kuoppala {
2958f744dbc2SMika Kuoppala 	struct intel_engine_cs *engine;
2959f744dbc2SMika Kuoppala 
2960f744dbc2SMika Kuoppala 	if (instance <= MAX_ENGINE_INSTANCE)
2961f744dbc2SMika Kuoppala 		engine = i915->engine_class[class][instance];
2962f744dbc2SMika Kuoppala 	else
2963f744dbc2SMika Kuoppala 		engine = NULL;
2964f744dbc2SMika Kuoppala 
2965f744dbc2SMika Kuoppala 	if (likely(engine))
2966f744dbc2SMika Kuoppala 		return gen8_cs_irq_handler(engine, iir);
2967f744dbc2SMika Kuoppala 
2968f744dbc2SMika Kuoppala 	WARN_ONCE(1, "unhandled engine interrupt class=0x%x, instance=0x%x\n",
2969f744dbc2SMika Kuoppala 		  class, instance);
2970f744dbc2SMika Kuoppala }
2971f744dbc2SMika Kuoppala 
2972f744dbc2SMika Kuoppala static void
2973f744dbc2SMika Kuoppala gen11_gt_identity_handler(struct drm_i915_private * const i915,
2974f744dbc2SMika Kuoppala 			  const u32 identity)
2975f744dbc2SMika Kuoppala {
2976f744dbc2SMika Kuoppala 	const u8 class = GEN11_INTR_ENGINE_CLASS(identity);
2977f744dbc2SMika Kuoppala 	const u8 instance = GEN11_INTR_ENGINE_INSTANCE(identity);
2978f744dbc2SMika Kuoppala 	const u16 intr = GEN11_INTR_ENGINE_INTR(identity);
2979f744dbc2SMika Kuoppala 
2980f744dbc2SMika Kuoppala 	if (unlikely(!intr))
2981f744dbc2SMika Kuoppala 		return;
2982f744dbc2SMika Kuoppala 
2983f744dbc2SMika Kuoppala 	if (class <= COPY_ENGINE_CLASS)
2984f744dbc2SMika Kuoppala 		return gen11_engine_irq_handler(i915, class, instance, intr);
2985f744dbc2SMika Kuoppala 
2986f744dbc2SMika Kuoppala 	if (class == OTHER_CLASS)
2987f744dbc2SMika Kuoppala 		return gen11_other_irq_handler(i915, instance, intr);
2988f744dbc2SMika Kuoppala 
2989f744dbc2SMika Kuoppala 	WARN_ONCE(1, "unknown interrupt class=0x%x, instance=0x%x, intr=0x%x\n",
2990f744dbc2SMika Kuoppala 		  class, instance, intr);
299151951ae7SMika Kuoppala }
299251951ae7SMika Kuoppala 
299351951ae7SMika Kuoppala static void
299496606f3bSOscar Mateo gen11_gt_bank_handler(struct drm_i915_private * const i915,
299596606f3bSOscar Mateo 		      const unsigned int bank)
299651951ae7SMika Kuoppala {
299751951ae7SMika Kuoppala 	void __iomem * const regs = i915->regs;
299851951ae7SMika Kuoppala 	unsigned long intr_dw;
299951951ae7SMika Kuoppala 	unsigned int bit;
300051951ae7SMika Kuoppala 
300196606f3bSOscar Mateo 	lockdep_assert_held(&i915->irq_lock);
300251951ae7SMika Kuoppala 
300351951ae7SMika Kuoppala 	intr_dw = raw_reg_read(regs, GEN11_GT_INTR_DW(bank));
300451951ae7SMika Kuoppala 
300551951ae7SMika Kuoppala 	if (unlikely(!intr_dw)) {
300651951ae7SMika Kuoppala 		DRM_ERROR("GT_INTR_DW%u blank!\n", bank);
300796606f3bSOscar Mateo 		return;
300851951ae7SMika Kuoppala 	}
300951951ae7SMika Kuoppala 
301051951ae7SMika Kuoppala 	for_each_set_bit(bit, &intr_dw, 32) {
3011f744dbc2SMika Kuoppala 		const u32 ident = gen11_gt_engine_identity(i915,
3012f744dbc2SMika Kuoppala 							   bank, bit);
301351951ae7SMika Kuoppala 
3014f744dbc2SMika Kuoppala 		gen11_gt_identity_handler(i915, ident);
301551951ae7SMika Kuoppala 	}
301651951ae7SMika Kuoppala 
301751951ae7SMika Kuoppala 	/* Clear must be after shared has been served for engine */
301851951ae7SMika Kuoppala 	raw_reg_write(regs, GEN11_GT_INTR_DW(bank), intr_dw);
301951951ae7SMika Kuoppala }
302096606f3bSOscar Mateo 
302196606f3bSOscar Mateo static void
302296606f3bSOscar Mateo gen11_gt_irq_handler(struct drm_i915_private * const i915,
302396606f3bSOscar Mateo 		     const u32 master_ctl)
302496606f3bSOscar Mateo {
302596606f3bSOscar Mateo 	unsigned int bank;
302696606f3bSOscar Mateo 
302796606f3bSOscar Mateo 	spin_lock(&i915->irq_lock);
302896606f3bSOscar Mateo 
302996606f3bSOscar Mateo 	for (bank = 0; bank < 2; bank++) {
303096606f3bSOscar Mateo 		if (master_ctl & GEN11_GT_DW_IRQ(bank))
303196606f3bSOscar Mateo 			gen11_gt_bank_handler(i915, bank);
303296606f3bSOscar Mateo 	}
303396606f3bSOscar Mateo 
303496606f3bSOscar Mateo 	spin_unlock(&i915->irq_lock);
303551951ae7SMika Kuoppala }
303651951ae7SMika Kuoppala 
30377a909383SChris Wilson static u32
30387a909383SChris Wilson gen11_gu_misc_irq_ack(struct drm_i915_private *dev_priv, const u32 master_ctl)
3039df0d28c1SDhinakaran Pandiyan {
3040df0d28c1SDhinakaran Pandiyan 	void __iomem * const regs = dev_priv->regs;
30417a909383SChris Wilson 	u32 iir;
3042df0d28c1SDhinakaran Pandiyan 
3043df0d28c1SDhinakaran Pandiyan 	if (!(master_ctl & GEN11_GU_MISC_IRQ))
30447a909383SChris Wilson 		return 0;
3045df0d28c1SDhinakaran Pandiyan 
30467a909383SChris Wilson 	iir = raw_reg_read(regs, GEN11_GU_MISC_IIR);
30477a909383SChris Wilson 	if (likely(iir))
30487a909383SChris Wilson 		raw_reg_write(regs, GEN11_GU_MISC_IIR, iir);
30497a909383SChris Wilson 
30507a909383SChris Wilson 	return iir;
3051df0d28c1SDhinakaran Pandiyan }
3052df0d28c1SDhinakaran Pandiyan 
3053df0d28c1SDhinakaran Pandiyan static void
30547a909383SChris Wilson gen11_gu_misc_irq_handler(struct drm_i915_private *dev_priv, const u32 iir)
3055df0d28c1SDhinakaran Pandiyan {
3056df0d28c1SDhinakaran Pandiyan 	if (iir & GEN11_GU_MISC_GSE)
3057df0d28c1SDhinakaran Pandiyan 		intel_opregion_asle_intr(dev_priv);
3058df0d28c1SDhinakaran Pandiyan }
3059df0d28c1SDhinakaran Pandiyan 
306081067b71SMika Kuoppala static inline u32 gen11_master_intr_disable(void __iomem * const regs)
306181067b71SMika Kuoppala {
306281067b71SMika Kuoppala 	raw_reg_write(regs, GEN11_GFX_MSTR_IRQ, 0);
306381067b71SMika Kuoppala 
306481067b71SMika Kuoppala 	/*
306581067b71SMika Kuoppala 	 * Now with master disabled, get a sample of level indications
306681067b71SMika Kuoppala 	 * for this interrupt. Indications will be cleared on related acks.
306781067b71SMika Kuoppala 	 * New indications can and will light up during processing,
306881067b71SMika Kuoppala 	 * and will generate new interrupt after enabling master.
306981067b71SMika Kuoppala 	 */
307081067b71SMika Kuoppala 	return raw_reg_read(regs, GEN11_GFX_MSTR_IRQ);
307181067b71SMika Kuoppala }
307281067b71SMika Kuoppala 
307381067b71SMika Kuoppala static inline void gen11_master_intr_enable(void __iomem * const regs)
307481067b71SMika Kuoppala {
307581067b71SMika Kuoppala 	raw_reg_write(regs, GEN11_GFX_MSTR_IRQ, GEN11_MASTER_IRQ);
307681067b71SMika Kuoppala }
307781067b71SMika Kuoppala 
307851951ae7SMika Kuoppala static irqreturn_t gen11_irq_handler(int irq, void *arg)
307951951ae7SMika Kuoppala {
308051951ae7SMika Kuoppala 	struct drm_i915_private * const i915 = to_i915(arg);
308151951ae7SMika Kuoppala 	void __iomem * const regs = i915->regs;
308251951ae7SMika Kuoppala 	u32 master_ctl;
3083df0d28c1SDhinakaran Pandiyan 	u32 gu_misc_iir;
308451951ae7SMika Kuoppala 
308551951ae7SMika Kuoppala 	if (!intel_irqs_enabled(i915))
308651951ae7SMika Kuoppala 		return IRQ_NONE;
308751951ae7SMika Kuoppala 
308881067b71SMika Kuoppala 	master_ctl = gen11_master_intr_disable(regs);
308981067b71SMika Kuoppala 	if (!master_ctl) {
309081067b71SMika Kuoppala 		gen11_master_intr_enable(regs);
309151951ae7SMika Kuoppala 		return IRQ_NONE;
309281067b71SMika Kuoppala 	}
309351951ae7SMika Kuoppala 
309451951ae7SMika Kuoppala 	/* Find, clear, then process each source of interrupt. */
309551951ae7SMika Kuoppala 	gen11_gt_irq_handler(i915, master_ctl);
309651951ae7SMika Kuoppala 
309751951ae7SMika Kuoppala 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
309851951ae7SMika Kuoppala 	if (master_ctl & GEN11_DISPLAY_IRQ) {
309951951ae7SMika Kuoppala 		const u32 disp_ctl = raw_reg_read(regs, GEN11_DISPLAY_INT_CTL);
310051951ae7SMika Kuoppala 
310151951ae7SMika Kuoppala 		disable_rpm_wakeref_asserts(i915);
310251951ae7SMika Kuoppala 		/*
310351951ae7SMika Kuoppala 		 * GEN11_DISPLAY_INT_CTL has same format as GEN8_MASTER_IRQ
310451951ae7SMika Kuoppala 		 * for the display related bits.
310551951ae7SMika Kuoppala 		 */
310651951ae7SMika Kuoppala 		gen8_de_irq_handler(i915, disp_ctl);
310751951ae7SMika Kuoppala 		enable_rpm_wakeref_asserts(i915);
310851951ae7SMika Kuoppala 	}
310951951ae7SMika Kuoppala 
31107a909383SChris Wilson 	gu_misc_iir = gen11_gu_misc_irq_ack(i915, master_ctl);
3111df0d28c1SDhinakaran Pandiyan 
311281067b71SMika Kuoppala 	gen11_master_intr_enable(regs);
311351951ae7SMika Kuoppala 
31147a909383SChris Wilson 	gen11_gu_misc_irq_handler(i915, gu_misc_iir);
3115df0d28c1SDhinakaran Pandiyan 
311651951ae7SMika Kuoppala 	return IRQ_HANDLED;
311751951ae7SMika Kuoppala }
311851951ae7SMika Kuoppala 
311942f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
312042f52ef8SKeith Packard  * we use as a pipe index
312142f52ef8SKeith Packard  */
312286e83e35SChris Wilson static int i8xx_enable_vblank(struct drm_device *dev, unsigned int pipe)
31230a3e67a4SJesse Barnes {
3124fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3125e9d21d7fSKeith Packard 	unsigned long irqflags;
312671e0ffa5SJesse Barnes 
31271ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
312886e83e35SChris Wilson 	i915_enable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_STATUS);
312986e83e35SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
313086e83e35SChris Wilson 
313186e83e35SChris Wilson 	return 0;
313286e83e35SChris Wilson }
313386e83e35SChris Wilson 
313486e83e35SChris Wilson static int i965_enable_vblank(struct drm_device *dev, unsigned int pipe)
313586e83e35SChris Wilson {
313686e83e35SChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
313786e83e35SChris Wilson 	unsigned long irqflags;
313886e83e35SChris Wilson 
313986e83e35SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
31407c463586SKeith Packard 	i915_enable_pipestat(dev_priv, pipe,
3141755e9019SImre Deak 			     PIPE_START_VBLANK_INTERRUPT_STATUS);
31421ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
31438692d00eSChris Wilson 
31440a3e67a4SJesse Barnes 	return 0;
31450a3e67a4SJesse Barnes }
31460a3e67a4SJesse Barnes 
314788e72717SThierry Reding static int ironlake_enable_vblank(struct drm_device *dev, unsigned int pipe)
3148f796cf8fSJesse Barnes {
3149fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3150f796cf8fSJesse Barnes 	unsigned long irqflags;
3151a9c287c9SJani Nikula 	u32 bit = INTEL_GEN(dev_priv) >= 7 ?
315286e83e35SChris Wilson 		DE_PIPE_VBLANK_IVB(pipe) : DE_PIPE_VBLANK(pipe);
3153f796cf8fSJesse Barnes 
3154f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3155fbdedaeaSVille Syrjälä 	ilk_enable_display_irq(dev_priv, bit);
3156b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3157b1f14ad0SJesse Barnes 
31582e8bf223SDhinakaran Pandiyan 	/* Even though there is no DMC, frame counter can get stuck when
31592e8bf223SDhinakaran Pandiyan 	 * PSR is active as no frames are generated.
31602e8bf223SDhinakaran Pandiyan 	 */
31612e8bf223SDhinakaran Pandiyan 	if (HAS_PSR(dev_priv))
31622e8bf223SDhinakaran Pandiyan 		drm_vblank_restore(dev, pipe);
31632e8bf223SDhinakaran Pandiyan 
3164b1f14ad0SJesse Barnes 	return 0;
3165b1f14ad0SJesse Barnes }
3166b1f14ad0SJesse Barnes 
316788e72717SThierry Reding static int gen8_enable_vblank(struct drm_device *dev, unsigned int pipe)
3168abd58f01SBen Widawsky {
3169fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3170abd58f01SBen Widawsky 	unsigned long irqflags;
3171abd58f01SBen Widawsky 
3172abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3173013d3752SVille Syrjälä 	bdw_enable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
3174abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3175013d3752SVille Syrjälä 
31762e8bf223SDhinakaran Pandiyan 	/* Even if there is no DMC, frame counter can get stuck when
31772e8bf223SDhinakaran Pandiyan 	 * PSR is active as no frames are generated, so check only for PSR.
31782e8bf223SDhinakaran Pandiyan 	 */
31792e8bf223SDhinakaran Pandiyan 	if (HAS_PSR(dev_priv))
31802e8bf223SDhinakaran Pandiyan 		drm_vblank_restore(dev, pipe);
31812e8bf223SDhinakaran Pandiyan 
3182abd58f01SBen Widawsky 	return 0;
3183abd58f01SBen Widawsky }
3184abd58f01SBen Widawsky 
318542f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
318642f52ef8SKeith Packard  * we use as a pipe index
318742f52ef8SKeith Packard  */
318886e83e35SChris Wilson static void i8xx_disable_vblank(struct drm_device *dev, unsigned int pipe)
318986e83e35SChris Wilson {
319086e83e35SChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
319186e83e35SChris Wilson 	unsigned long irqflags;
319286e83e35SChris Wilson 
319386e83e35SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
319486e83e35SChris Wilson 	i915_disable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_STATUS);
319586e83e35SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
319686e83e35SChris Wilson }
319786e83e35SChris Wilson 
319886e83e35SChris Wilson static void i965_disable_vblank(struct drm_device *dev, unsigned int pipe)
31990a3e67a4SJesse Barnes {
3200fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3201e9d21d7fSKeith Packard 	unsigned long irqflags;
32020a3e67a4SJesse Barnes 
32031ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
32047c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
3205755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
32061ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
32070a3e67a4SJesse Barnes }
32080a3e67a4SJesse Barnes 
320988e72717SThierry Reding static void ironlake_disable_vblank(struct drm_device *dev, unsigned int pipe)
3210f796cf8fSJesse Barnes {
3211fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3212f796cf8fSJesse Barnes 	unsigned long irqflags;
3213a9c287c9SJani Nikula 	u32 bit = INTEL_GEN(dev_priv) >= 7 ?
321486e83e35SChris Wilson 		DE_PIPE_VBLANK_IVB(pipe) : DE_PIPE_VBLANK(pipe);
3215f796cf8fSJesse Barnes 
3216f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3217fbdedaeaSVille Syrjälä 	ilk_disable_display_irq(dev_priv, bit);
3218b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3219b1f14ad0SJesse Barnes }
3220b1f14ad0SJesse Barnes 
322188e72717SThierry Reding static void gen8_disable_vblank(struct drm_device *dev, unsigned int pipe)
3222abd58f01SBen Widawsky {
3223fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3224abd58f01SBen Widawsky 	unsigned long irqflags;
3225abd58f01SBen Widawsky 
3226abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3227013d3752SVille Syrjälä 	bdw_disable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
3228abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3229abd58f01SBen Widawsky }
3230abd58f01SBen Widawsky 
3231b243f530STvrtko Ursulin static void ibx_irq_reset(struct drm_i915_private *dev_priv)
323291738a95SPaulo Zanoni {
32336e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
323491738a95SPaulo Zanoni 		return;
323591738a95SPaulo Zanoni 
32363488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(SDE);
3237105b122eSPaulo Zanoni 
32386e266956STvrtko Ursulin 	if (HAS_PCH_CPT(dev_priv) || HAS_PCH_LPT(dev_priv))
3239105b122eSPaulo Zanoni 		I915_WRITE(SERR_INT, 0xffffffff);
3240622364b6SPaulo Zanoni }
3241105b122eSPaulo Zanoni 
324291738a95SPaulo Zanoni /*
3243622364b6SPaulo Zanoni  * SDEIER is also touched by the interrupt handler to work around missed PCH
3244622364b6SPaulo Zanoni  * interrupts. Hence we can't update it after the interrupt handler is enabled -
3245622364b6SPaulo Zanoni  * instead we unconditionally enable all PCH interrupt sources here, but then
3246622364b6SPaulo Zanoni  * only unmask them as needed with SDEIMR.
3247622364b6SPaulo Zanoni  *
3248622364b6SPaulo Zanoni  * This function needs to be called before interrupts are enabled.
324991738a95SPaulo Zanoni  */
3250622364b6SPaulo Zanoni static void ibx_irq_pre_postinstall(struct drm_device *dev)
3251622364b6SPaulo Zanoni {
3252fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3253622364b6SPaulo Zanoni 
32546e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
3255622364b6SPaulo Zanoni 		return;
3256622364b6SPaulo Zanoni 
3257622364b6SPaulo Zanoni 	WARN_ON(I915_READ(SDEIER) != 0);
325891738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
325991738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
326091738a95SPaulo Zanoni }
326191738a95SPaulo Zanoni 
3262b243f530STvrtko Ursulin static void gen5_gt_irq_reset(struct drm_i915_private *dev_priv)
3263d18ea1b5SDaniel Vetter {
32643488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(GT);
3265b243f530STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 6)
32663488d4ebSVille Syrjälä 		GEN3_IRQ_RESET(GEN6_PM);
3267d18ea1b5SDaniel Vetter }
3268d18ea1b5SDaniel Vetter 
326970591a41SVille Syrjälä static void vlv_display_irq_reset(struct drm_i915_private *dev_priv)
327070591a41SVille Syrjälä {
327171b8b41dSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
327271b8b41dSVille Syrjälä 		I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
327371b8b41dSVille Syrjälä 	else
327471b8b41dSVille Syrjälä 		I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
327571b8b41dSVille Syrjälä 
3276ad22d106SVille Syrjälä 	i915_hotplug_interrupt_update_locked(dev_priv, 0xffffffff, 0);
327770591a41SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
327870591a41SVille Syrjälä 
327944d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
328070591a41SVille Syrjälä 
32813488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(VLV_);
32828bd099a7SChris Wilson 	dev_priv->irq_mask = ~0u;
328370591a41SVille Syrjälä }
328470591a41SVille Syrjälä 
32858bb61306SVille Syrjälä static void vlv_display_irq_postinstall(struct drm_i915_private *dev_priv)
32868bb61306SVille Syrjälä {
32878bb61306SVille Syrjälä 	u32 pipestat_mask;
32889ab981f2SVille Syrjälä 	u32 enable_mask;
32898bb61306SVille Syrjälä 	enum pipe pipe;
32908bb61306SVille Syrjälä 
3291842ebf7aSVille Syrjälä 	pipestat_mask = PIPE_CRC_DONE_INTERRUPT_STATUS;
32928bb61306SVille Syrjälä 
32938bb61306SVille Syrjälä 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
32948bb61306SVille Syrjälä 	for_each_pipe(dev_priv, pipe)
32958bb61306SVille Syrjälä 		i915_enable_pipestat(dev_priv, pipe, pipestat_mask);
32968bb61306SVille Syrjälä 
32979ab981f2SVille Syrjälä 	enable_mask = I915_DISPLAY_PORT_INTERRUPT |
32988bb61306SVille Syrjälä 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3299ebf5f921SVille Syrjälä 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3300ebf5f921SVille Syrjälä 		I915_LPE_PIPE_A_INTERRUPT |
3301ebf5f921SVille Syrjälä 		I915_LPE_PIPE_B_INTERRUPT;
3302ebf5f921SVille Syrjälä 
33038bb61306SVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
3304ebf5f921SVille Syrjälä 		enable_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT |
3305ebf5f921SVille Syrjälä 			I915_LPE_PIPE_C_INTERRUPT;
33066b7eafc1SVille Syrjälä 
33078bd099a7SChris Wilson 	WARN_ON(dev_priv->irq_mask != ~0u);
33086b7eafc1SVille Syrjälä 
33099ab981f2SVille Syrjälä 	dev_priv->irq_mask = ~enable_mask;
33108bb61306SVille Syrjälä 
33113488d4ebSVille Syrjälä 	GEN3_IRQ_INIT(VLV_, dev_priv->irq_mask, enable_mask);
33128bb61306SVille Syrjälä }
33138bb61306SVille Syrjälä 
33148bb61306SVille Syrjälä /* drm_dma.h hooks
33158bb61306SVille Syrjälä */
33168bb61306SVille Syrjälä static void ironlake_irq_reset(struct drm_device *dev)
33178bb61306SVille Syrjälä {
3318fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
33198bb61306SVille Syrjälä 
33203488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(DE);
3321cf819effSLucas De Marchi 	if (IS_GEN(dev_priv, 7))
33228bb61306SVille Syrjälä 		I915_WRITE(GEN7_ERR_INT, 0xffffffff);
33238bb61306SVille Syrjälä 
3324fc340442SDaniel Vetter 	if (IS_HASWELL(dev_priv)) {
3325fc340442SDaniel Vetter 		I915_WRITE(EDP_PSR_IMR, 0xffffffff);
3326fc340442SDaniel Vetter 		I915_WRITE(EDP_PSR_IIR, 0xffffffff);
3327fc340442SDaniel Vetter 	}
3328fc340442SDaniel Vetter 
3329b243f530STvrtko Ursulin 	gen5_gt_irq_reset(dev_priv);
33308bb61306SVille Syrjälä 
3331b243f530STvrtko Ursulin 	ibx_irq_reset(dev_priv);
33328bb61306SVille Syrjälä }
33338bb61306SVille Syrjälä 
33346bcdb1c8SVille Syrjälä static void valleyview_irq_reset(struct drm_device *dev)
33357e231dbeSJesse Barnes {
3336fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
33377e231dbeSJesse Barnes 
333834c7b8a7SVille Syrjälä 	I915_WRITE(VLV_MASTER_IER, 0);
333934c7b8a7SVille Syrjälä 	POSTING_READ(VLV_MASTER_IER);
334034c7b8a7SVille Syrjälä 
3341b243f530STvrtko Ursulin 	gen5_gt_irq_reset(dev_priv);
33427e231dbeSJesse Barnes 
3343ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
33449918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
334570591a41SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3346ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
33477e231dbeSJesse Barnes }
33487e231dbeSJesse Barnes 
3349d6e3cca3SDaniel Vetter static void gen8_gt_irq_reset(struct drm_i915_private *dev_priv)
3350d6e3cca3SDaniel Vetter {
3351d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 0);
3352d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 1);
3353d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 2);
3354d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 3);
3355d6e3cca3SDaniel Vetter }
3356d6e3cca3SDaniel Vetter 
3357823f6b38SPaulo Zanoni static void gen8_irq_reset(struct drm_device *dev)
3358abd58f01SBen Widawsky {
3359fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3360abd58f01SBen Widawsky 	int pipe;
3361abd58f01SBen Widawsky 
33624376b9c9SMika Kuoppala 	gen8_master_intr_disable(dev_priv->regs);
3363abd58f01SBen Widawsky 
3364d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
3365abd58f01SBen Widawsky 
3366e04f7eceSVille Syrjälä 	I915_WRITE(EDP_PSR_IMR, 0xffffffff);
3367e04f7eceSVille Syrjälä 	I915_WRITE(EDP_PSR_IIR, 0xffffffff);
3368e04f7eceSVille Syrjälä 
3369055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3370f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3371813bde43SPaulo Zanoni 						   POWER_DOMAIN_PIPE(pipe)))
3372f86f3fb0SPaulo Zanoni 			GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3373abd58f01SBen Widawsky 
33743488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(GEN8_DE_PORT_);
33753488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(GEN8_DE_MISC_);
33763488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(GEN8_PCU_);
3377abd58f01SBen Widawsky 
33786e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
3379b243f530STvrtko Ursulin 		ibx_irq_reset(dev_priv);
3380abd58f01SBen Widawsky }
3381abd58f01SBen Widawsky 
338251951ae7SMika Kuoppala static void gen11_gt_irq_reset(struct drm_i915_private *dev_priv)
338351951ae7SMika Kuoppala {
338451951ae7SMika Kuoppala 	/* Disable RCS, BCS, VCS and VECS class engines. */
338551951ae7SMika Kuoppala 	I915_WRITE(GEN11_RENDER_COPY_INTR_ENABLE, 0);
338651951ae7SMika Kuoppala 	I915_WRITE(GEN11_VCS_VECS_INTR_ENABLE,	  0);
338751951ae7SMika Kuoppala 
338851951ae7SMika Kuoppala 	/* Restore masks irqs on RCS, BCS, VCS and VECS engines. */
338951951ae7SMika Kuoppala 	I915_WRITE(GEN11_RCS0_RSVD_INTR_MASK,	~0);
339051951ae7SMika Kuoppala 	I915_WRITE(GEN11_BCS_RSVD_INTR_MASK,	~0);
339151951ae7SMika Kuoppala 	I915_WRITE(GEN11_VCS0_VCS1_INTR_MASK,	~0);
339251951ae7SMika Kuoppala 	I915_WRITE(GEN11_VCS2_VCS3_INTR_MASK,	~0);
339351951ae7SMika Kuoppala 	I915_WRITE(GEN11_VECS0_VECS1_INTR_MASK,	~0);
3394d02b98b8SOscar Mateo 
3395d02b98b8SOscar Mateo 	I915_WRITE(GEN11_GPM_WGBOXPERF_INTR_ENABLE, 0);
3396d02b98b8SOscar Mateo 	I915_WRITE(GEN11_GPM_WGBOXPERF_INTR_MASK,  ~0);
339751951ae7SMika Kuoppala }
339851951ae7SMika Kuoppala 
339951951ae7SMika Kuoppala static void gen11_irq_reset(struct drm_device *dev)
340051951ae7SMika Kuoppala {
340151951ae7SMika Kuoppala 	struct drm_i915_private *dev_priv = dev->dev_private;
340251951ae7SMika Kuoppala 	int pipe;
340351951ae7SMika Kuoppala 
340481067b71SMika Kuoppala 	gen11_master_intr_disable(dev_priv->regs);
340551951ae7SMika Kuoppala 
340651951ae7SMika Kuoppala 	gen11_gt_irq_reset(dev_priv);
340751951ae7SMika Kuoppala 
340851951ae7SMika Kuoppala 	I915_WRITE(GEN11_DISPLAY_INT_CTL, 0);
340951951ae7SMika Kuoppala 
341062819dfdSJosé Roberto de Souza 	I915_WRITE(EDP_PSR_IMR, 0xffffffff);
341162819dfdSJosé Roberto de Souza 	I915_WRITE(EDP_PSR_IIR, 0xffffffff);
341262819dfdSJosé Roberto de Souza 
341351951ae7SMika Kuoppala 	for_each_pipe(dev_priv, pipe)
341451951ae7SMika Kuoppala 		if (intel_display_power_is_enabled(dev_priv,
341551951ae7SMika Kuoppala 						   POWER_DOMAIN_PIPE(pipe)))
341651951ae7SMika Kuoppala 			GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
341751951ae7SMika Kuoppala 
341851951ae7SMika Kuoppala 	GEN3_IRQ_RESET(GEN8_DE_PORT_);
341951951ae7SMika Kuoppala 	GEN3_IRQ_RESET(GEN8_DE_MISC_);
3420121e758eSDhinakaran Pandiyan 	GEN3_IRQ_RESET(GEN11_DE_HPD_);
3421df0d28c1SDhinakaran Pandiyan 	GEN3_IRQ_RESET(GEN11_GU_MISC_);
342251951ae7SMika Kuoppala 	GEN3_IRQ_RESET(GEN8_PCU_);
342331604222SAnusha Srivatsa 
342429b43ae2SRodrigo Vivi 	if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
342531604222SAnusha Srivatsa 		GEN3_IRQ_RESET(SDE);
342651951ae7SMika Kuoppala }
342751951ae7SMika Kuoppala 
34284c6c03beSDamien Lespiau void gen8_irq_power_well_post_enable(struct drm_i915_private *dev_priv,
3429001bd2cbSImre Deak 				     u8 pipe_mask)
3430d49bdb0eSPaulo Zanoni {
3431a9c287c9SJani Nikula 	u32 extra_ier = GEN8_PIPE_VBLANK | GEN8_PIPE_FIFO_UNDERRUN;
34326831f3e3SVille Syrjälä 	enum pipe pipe;
3433d49bdb0eSPaulo Zanoni 
343413321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
34359dfe2e3aSImre Deak 
34369dfe2e3aSImre Deak 	if (!intel_irqs_enabled(dev_priv)) {
34379dfe2e3aSImre Deak 		spin_unlock_irq(&dev_priv->irq_lock);
34389dfe2e3aSImre Deak 		return;
34399dfe2e3aSImre Deak 	}
34409dfe2e3aSImre Deak 
34416831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
34426831f3e3SVille Syrjälä 		GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
34436831f3e3SVille Syrjälä 				  dev_priv->de_irq_mask[pipe],
34446831f3e3SVille Syrjälä 				  ~dev_priv->de_irq_mask[pipe] | extra_ier);
34459dfe2e3aSImre Deak 
344613321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3447d49bdb0eSPaulo Zanoni }
3448d49bdb0eSPaulo Zanoni 
3449aae8ba84SVille Syrjälä void gen8_irq_power_well_pre_disable(struct drm_i915_private *dev_priv,
3450001bd2cbSImre Deak 				     u8 pipe_mask)
3451aae8ba84SVille Syrjälä {
34526831f3e3SVille Syrjälä 	enum pipe pipe;
34536831f3e3SVille Syrjälä 
3454aae8ba84SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
34559dfe2e3aSImre Deak 
34569dfe2e3aSImre Deak 	if (!intel_irqs_enabled(dev_priv)) {
34579dfe2e3aSImre Deak 		spin_unlock_irq(&dev_priv->irq_lock);
34589dfe2e3aSImre Deak 		return;
34599dfe2e3aSImre Deak 	}
34609dfe2e3aSImre Deak 
34616831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
34626831f3e3SVille Syrjälä 		GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
34639dfe2e3aSImre Deak 
3464aae8ba84SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3465aae8ba84SVille Syrjälä 
3466aae8ba84SVille Syrjälä 	/* make sure we're done processing display irqs */
346791c8a326SChris Wilson 	synchronize_irq(dev_priv->drm.irq);
3468aae8ba84SVille Syrjälä }
3469aae8ba84SVille Syrjälä 
34706bcdb1c8SVille Syrjälä static void cherryview_irq_reset(struct drm_device *dev)
347143f328d7SVille Syrjälä {
3472fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
347343f328d7SVille Syrjälä 
347443f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
347543f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
347643f328d7SVille Syrjälä 
3477d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
347843f328d7SVille Syrjälä 
34793488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(GEN8_PCU_);
348043f328d7SVille Syrjälä 
3481ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
34829918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
348370591a41SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3484ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
348543f328d7SVille Syrjälä }
348643f328d7SVille Syrjälä 
348791d14251STvrtko Ursulin static u32 intel_hpd_enabled_irqs(struct drm_i915_private *dev_priv,
348887a02106SVille Syrjälä 				  const u32 hpd[HPD_NUM_PINS])
348987a02106SVille Syrjälä {
349087a02106SVille Syrjälä 	struct intel_encoder *encoder;
349187a02106SVille Syrjälä 	u32 enabled_irqs = 0;
349287a02106SVille Syrjälä 
349391c8a326SChris Wilson 	for_each_intel_encoder(&dev_priv->drm, encoder)
349487a02106SVille Syrjälä 		if (dev_priv->hotplug.stats[encoder->hpd_pin].state == HPD_ENABLED)
349587a02106SVille Syrjälä 			enabled_irqs |= hpd[encoder->hpd_pin];
349687a02106SVille Syrjälä 
349787a02106SVille Syrjälä 	return enabled_irqs;
349887a02106SVille Syrjälä }
349987a02106SVille Syrjälä 
35001a56b1a2SImre Deak static void ibx_hpd_detection_setup(struct drm_i915_private *dev_priv)
35011a56b1a2SImre Deak {
35021a56b1a2SImre Deak 	u32 hotplug;
35031a56b1a2SImre Deak 
35041a56b1a2SImre Deak 	/*
35051a56b1a2SImre Deak 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
35061a56b1a2SImre Deak 	 * duration to 2ms (which is the minimum in the Display Port spec).
35071a56b1a2SImre Deak 	 * The pulse duration bits are reserved on LPT+.
35081a56b1a2SImre Deak 	 */
35091a56b1a2SImre Deak 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
35101a56b1a2SImre Deak 	hotplug &= ~(PORTB_PULSE_DURATION_MASK |
35111a56b1a2SImre Deak 		     PORTC_PULSE_DURATION_MASK |
35121a56b1a2SImre Deak 		     PORTD_PULSE_DURATION_MASK);
35131a56b1a2SImre Deak 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
35141a56b1a2SImre Deak 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
35151a56b1a2SImre Deak 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
35161a56b1a2SImre Deak 	/*
35171a56b1a2SImre Deak 	 * When CPU and PCH are on the same package, port A
35181a56b1a2SImre Deak 	 * HPD must be enabled in both north and south.
35191a56b1a2SImre Deak 	 */
35201a56b1a2SImre Deak 	if (HAS_PCH_LPT_LP(dev_priv))
35211a56b1a2SImre Deak 		hotplug |= PORTA_HOTPLUG_ENABLE;
35221a56b1a2SImre Deak 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
35231a56b1a2SImre Deak }
35241a56b1a2SImre Deak 
352591d14251STvrtko Ursulin static void ibx_hpd_irq_setup(struct drm_i915_private *dev_priv)
352682a28bcfSDaniel Vetter {
35271a56b1a2SImre Deak 	u32 hotplug_irqs, enabled_irqs;
352882a28bcfSDaniel Vetter 
352991d14251STvrtko Ursulin 	if (HAS_PCH_IBX(dev_priv)) {
3530fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
353191d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ibx);
353282a28bcfSDaniel Vetter 	} else {
3533fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
353491d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_cpt);
353582a28bcfSDaniel Vetter 	}
353682a28bcfSDaniel Vetter 
3537fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
353882a28bcfSDaniel Vetter 
35391a56b1a2SImre Deak 	ibx_hpd_detection_setup(dev_priv);
35406dbf30ceSVille Syrjälä }
354126951cafSXiong Zhang 
354231604222SAnusha Srivatsa static void icp_hpd_detection_setup(struct drm_i915_private *dev_priv)
354331604222SAnusha Srivatsa {
354431604222SAnusha Srivatsa 	u32 hotplug;
354531604222SAnusha Srivatsa 
354631604222SAnusha Srivatsa 	hotplug = I915_READ(SHOTPLUG_CTL_DDI);
354731604222SAnusha Srivatsa 	hotplug |= ICP_DDIA_HPD_ENABLE |
354831604222SAnusha Srivatsa 		   ICP_DDIB_HPD_ENABLE;
354931604222SAnusha Srivatsa 	I915_WRITE(SHOTPLUG_CTL_DDI, hotplug);
355031604222SAnusha Srivatsa 
355131604222SAnusha Srivatsa 	hotplug = I915_READ(SHOTPLUG_CTL_TC);
355231604222SAnusha Srivatsa 	hotplug |= ICP_TC_HPD_ENABLE(PORT_TC1) |
355331604222SAnusha Srivatsa 		   ICP_TC_HPD_ENABLE(PORT_TC2) |
355431604222SAnusha Srivatsa 		   ICP_TC_HPD_ENABLE(PORT_TC3) |
355531604222SAnusha Srivatsa 		   ICP_TC_HPD_ENABLE(PORT_TC4);
355631604222SAnusha Srivatsa 	I915_WRITE(SHOTPLUG_CTL_TC, hotplug);
355731604222SAnusha Srivatsa }
355831604222SAnusha Srivatsa 
355931604222SAnusha Srivatsa static void icp_hpd_irq_setup(struct drm_i915_private *dev_priv)
356031604222SAnusha Srivatsa {
356131604222SAnusha Srivatsa 	u32 hotplug_irqs, enabled_irqs;
356231604222SAnusha Srivatsa 
356331604222SAnusha Srivatsa 	hotplug_irqs = SDE_DDI_MASK_ICP | SDE_TC_MASK_ICP;
356431604222SAnusha Srivatsa 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_icp);
356531604222SAnusha Srivatsa 
356631604222SAnusha Srivatsa 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
356731604222SAnusha Srivatsa 
356831604222SAnusha Srivatsa 	icp_hpd_detection_setup(dev_priv);
356931604222SAnusha Srivatsa }
357031604222SAnusha Srivatsa 
3571121e758eSDhinakaran Pandiyan static void gen11_hpd_detection_setup(struct drm_i915_private *dev_priv)
3572121e758eSDhinakaran Pandiyan {
3573121e758eSDhinakaran Pandiyan 	u32 hotplug;
3574121e758eSDhinakaran Pandiyan 
3575121e758eSDhinakaran Pandiyan 	hotplug = I915_READ(GEN11_TC_HOTPLUG_CTL);
3576121e758eSDhinakaran Pandiyan 	hotplug |= GEN11_HOTPLUG_CTL_ENABLE(PORT_TC1) |
3577121e758eSDhinakaran Pandiyan 		   GEN11_HOTPLUG_CTL_ENABLE(PORT_TC2) |
3578121e758eSDhinakaran Pandiyan 		   GEN11_HOTPLUG_CTL_ENABLE(PORT_TC3) |
3579121e758eSDhinakaran Pandiyan 		   GEN11_HOTPLUG_CTL_ENABLE(PORT_TC4);
3580121e758eSDhinakaran Pandiyan 	I915_WRITE(GEN11_TC_HOTPLUG_CTL, hotplug);
3581b796b971SDhinakaran Pandiyan 
3582b796b971SDhinakaran Pandiyan 	hotplug = I915_READ(GEN11_TBT_HOTPLUG_CTL);
3583b796b971SDhinakaran Pandiyan 	hotplug |= GEN11_HOTPLUG_CTL_ENABLE(PORT_TC1) |
3584b796b971SDhinakaran Pandiyan 		   GEN11_HOTPLUG_CTL_ENABLE(PORT_TC2) |
3585b796b971SDhinakaran Pandiyan 		   GEN11_HOTPLUG_CTL_ENABLE(PORT_TC3) |
3586b796b971SDhinakaran Pandiyan 		   GEN11_HOTPLUG_CTL_ENABLE(PORT_TC4);
3587b796b971SDhinakaran Pandiyan 	I915_WRITE(GEN11_TBT_HOTPLUG_CTL, hotplug);
3588121e758eSDhinakaran Pandiyan }
3589121e758eSDhinakaran Pandiyan 
3590121e758eSDhinakaran Pandiyan static void gen11_hpd_irq_setup(struct drm_i915_private *dev_priv)
3591121e758eSDhinakaran Pandiyan {
3592121e758eSDhinakaran Pandiyan 	u32 hotplug_irqs, enabled_irqs;
3593121e758eSDhinakaran Pandiyan 	u32 val;
3594121e758eSDhinakaran Pandiyan 
3595b796b971SDhinakaran Pandiyan 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_gen11);
3596b796b971SDhinakaran Pandiyan 	hotplug_irqs = GEN11_DE_TC_HOTPLUG_MASK | GEN11_DE_TBT_HOTPLUG_MASK;
3597121e758eSDhinakaran Pandiyan 
3598121e758eSDhinakaran Pandiyan 	val = I915_READ(GEN11_DE_HPD_IMR);
3599121e758eSDhinakaran Pandiyan 	val &= ~hotplug_irqs;
3600121e758eSDhinakaran Pandiyan 	I915_WRITE(GEN11_DE_HPD_IMR, val);
3601121e758eSDhinakaran Pandiyan 	POSTING_READ(GEN11_DE_HPD_IMR);
3602121e758eSDhinakaran Pandiyan 
3603121e758eSDhinakaran Pandiyan 	gen11_hpd_detection_setup(dev_priv);
360431604222SAnusha Srivatsa 
360529b43ae2SRodrigo Vivi 	if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
360631604222SAnusha Srivatsa 		icp_hpd_irq_setup(dev_priv);
3607121e758eSDhinakaran Pandiyan }
3608121e758eSDhinakaran Pandiyan 
36092a57d9ccSImre Deak static void spt_hpd_detection_setup(struct drm_i915_private *dev_priv)
36102a57d9ccSImre Deak {
36113b92e263SRodrigo Vivi 	u32 val, hotplug;
36123b92e263SRodrigo Vivi 
36133b92e263SRodrigo Vivi 	/* Display WA #1179 WaHardHangonHotPlug: cnp */
36143b92e263SRodrigo Vivi 	if (HAS_PCH_CNP(dev_priv)) {
36153b92e263SRodrigo Vivi 		val = I915_READ(SOUTH_CHICKEN1);
36163b92e263SRodrigo Vivi 		val &= ~CHASSIS_CLK_REQ_DURATION_MASK;
36173b92e263SRodrigo Vivi 		val |= CHASSIS_CLK_REQ_DURATION(0xf);
36183b92e263SRodrigo Vivi 		I915_WRITE(SOUTH_CHICKEN1, val);
36193b92e263SRodrigo Vivi 	}
36202a57d9ccSImre Deak 
36212a57d9ccSImre Deak 	/* Enable digital hotplug on the PCH */
36222a57d9ccSImre Deak 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
36232a57d9ccSImre Deak 	hotplug |= PORTA_HOTPLUG_ENABLE |
36242a57d9ccSImre Deak 		   PORTB_HOTPLUG_ENABLE |
36252a57d9ccSImre Deak 		   PORTC_HOTPLUG_ENABLE |
36262a57d9ccSImre Deak 		   PORTD_HOTPLUG_ENABLE;
36272a57d9ccSImre Deak 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
36282a57d9ccSImre Deak 
36292a57d9ccSImre Deak 	hotplug = I915_READ(PCH_PORT_HOTPLUG2);
36302a57d9ccSImre Deak 	hotplug |= PORTE_HOTPLUG_ENABLE;
36312a57d9ccSImre Deak 	I915_WRITE(PCH_PORT_HOTPLUG2, hotplug);
36322a57d9ccSImre Deak }
36332a57d9ccSImre Deak 
363491d14251STvrtko Ursulin static void spt_hpd_irq_setup(struct drm_i915_private *dev_priv)
36356dbf30ceSVille Syrjälä {
36362a57d9ccSImre Deak 	u32 hotplug_irqs, enabled_irqs;
36376dbf30ceSVille Syrjälä 
36386dbf30ceSVille Syrjälä 	hotplug_irqs = SDE_HOTPLUG_MASK_SPT;
363991d14251STvrtko Ursulin 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_spt);
36406dbf30ceSVille Syrjälä 
36416dbf30ceSVille Syrjälä 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
36426dbf30ceSVille Syrjälä 
36432a57d9ccSImre Deak 	spt_hpd_detection_setup(dev_priv);
364426951cafSXiong Zhang }
36457fe0b973SKeith Packard 
36461a56b1a2SImre Deak static void ilk_hpd_detection_setup(struct drm_i915_private *dev_priv)
36471a56b1a2SImre Deak {
36481a56b1a2SImre Deak 	u32 hotplug;
36491a56b1a2SImre Deak 
36501a56b1a2SImre Deak 	/*
36511a56b1a2SImre Deak 	 * Enable digital hotplug on the CPU, and configure the DP short pulse
36521a56b1a2SImre Deak 	 * duration to 2ms (which is the minimum in the Display Port spec)
36531a56b1a2SImre Deak 	 * The pulse duration bits are reserved on HSW+.
36541a56b1a2SImre Deak 	 */
36551a56b1a2SImre Deak 	hotplug = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
36561a56b1a2SImre Deak 	hotplug &= ~DIGITAL_PORTA_PULSE_DURATION_MASK;
36571a56b1a2SImre Deak 	hotplug |= DIGITAL_PORTA_HOTPLUG_ENABLE |
36581a56b1a2SImre Deak 		   DIGITAL_PORTA_PULSE_DURATION_2ms;
36591a56b1a2SImre Deak 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, hotplug);
36601a56b1a2SImre Deak }
36611a56b1a2SImre Deak 
366291d14251STvrtko Ursulin static void ilk_hpd_irq_setup(struct drm_i915_private *dev_priv)
3663e4ce95aaSVille Syrjälä {
36641a56b1a2SImre Deak 	u32 hotplug_irqs, enabled_irqs;
3665e4ce95aaSVille Syrjälä 
366691d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 8) {
36673a3b3c7dSVille Syrjälä 		hotplug_irqs = GEN8_PORT_DP_A_HOTPLUG;
366891d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_bdw);
36693a3b3c7dSVille Syrjälä 
36703a3b3c7dSVille Syrjälä 		bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
367191d14251STvrtko Ursulin 	} else if (INTEL_GEN(dev_priv) >= 7) {
367223bb4cb5SVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG_IVB;
367391d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ivb);
36743a3b3c7dSVille Syrjälä 
36753a3b3c7dSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
367623bb4cb5SVille Syrjälä 	} else {
3677e4ce95aaSVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG;
367891d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ilk);
3679e4ce95aaSVille Syrjälä 
3680e4ce95aaSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
36813a3b3c7dSVille Syrjälä 	}
3682e4ce95aaSVille Syrjälä 
36831a56b1a2SImre Deak 	ilk_hpd_detection_setup(dev_priv);
3684e4ce95aaSVille Syrjälä 
368591d14251STvrtko Ursulin 	ibx_hpd_irq_setup(dev_priv);
3686e4ce95aaSVille Syrjälä }
3687e4ce95aaSVille Syrjälä 
36882a57d9ccSImre Deak static void __bxt_hpd_detection_setup(struct drm_i915_private *dev_priv,
36892a57d9ccSImre Deak 				      u32 enabled_irqs)
3690e0a20ad7SShashank Sharma {
36912a57d9ccSImre Deak 	u32 hotplug;
3692e0a20ad7SShashank Sharma 
3693a52bb15bSVille Syrjälä 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
36942a57d9ccSImre Deak 	hotplug |= PORTA_HOTPLUG_ENABLE |
36952a57d9ccSImre Deak 		   PORTB_HOTPLUG_ENABLE |
36962a57d9ccSImre Deak 		   PORTC_HOTPLUG_ENABLE;
3697d252bf68SShubhangi Shrivastava 
3698d252bf68SShubhangi Shrivastava 	DRM_DEBUG_KMS("Invert bit setting: hp_ctl:%x hp_port:%x\n",
3699d252bf68SShubhangi Shrivastava 		      hotplug, enabled_irqs);
3700d252bf68SShubhangi Shrivastava 	hotplug &= ~BXT_DDI_HPD_INVERT_MASK;
3701d252bf68SShubhangi Shrivastava 
3702d252bf68SShubhangi Shrivastava 	/*
3703d252bf68SShubhangi Shrivastava 	 * For BXT invert bit has to be set based on AOB design
3704d252bf68SShubhangi Shrivastava 	 * for HPD detection logic, update it based on VBT fields.
3705d252bf68SShubhangi Shrivastava 	 */
3706d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIA) &&
3707d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_A))
3708d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIA_HPD_INVERT;
3709d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIB) &&
3710d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_B))
3711d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIB_HPD_INVERT;
3712d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIC) &&
3713d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_C))
3714d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIC_HPD_INVERT;
3715d252bf68SShubhangi Shrivastava 
3716a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
3717e0a20ad7SShashank Sharma }
3718e0a20ad7SShashank Sharma 
37192a57d9ccSImre Deak static void bxt_hpd_detection_setup(struct drm_i915_private *dev_priv)
37202a57d9ccSImre Deak {
37212a57d9ccSImre Deak 	__bxt_hpd_detection_setup(dev_priv, BXT_DE_PORT_HOTPLUG_MASK);
37222a57d9ccSImre Deak }
37232a57d9ccSImre Deak 
37242a57d9ccSImre Deak static void bxt_hpd_irq_setup(struct drm_i915_private *dev_priv)
37252a57d9ccSImre Deak {
37262a57d9ccSImre Deak 	u32 hotplug_irqs, enabled_irqs;
37272a57d9ccSImre Deak 
37282a57d9ccSImre Deak 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_bxt);
37292a57d9ccSImre Deak 	hotplug_irqs = BXT_DE_PORT_HOTPLUG_MASK;
37302a57d9ccSImre Deak 
37312a57d9ccSImre Deak 	bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
37322a57d9ccSImre Deak 
37332a57d9ccSImre Deak 	__bxt_hpd_detection_setup(dev_priv, enabled_irqs);
37342a57d9ccSImre Deak }
37352a57d9ccSImre Deak 
3736d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
3737d46da437SPaulo Zanoni {
3738fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
373982a28bcfSDaniel Vetter 	u32 mask;
3740d46da437SPaulo Zanoni 
37416e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
3742692a04cfSDaniel Vetter 		return;
3743692a04cfSDaniel Vetter 
37446e266956STvrtko Ursulin 	if (HAS_PCH_IBX(dev_priv))
37455c673b60SDaniel Vetter 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
37464ebc6509SDhinakaran Pandiyan 	else if (HAS_PCH_CPT(dev_priv) || HAS_PCH_LPT(dev_priv))
37475c673b60SDaniel Vetter 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
37484ebc6509SDhinakaran Pandiyan 	else
37494ebc6509SDhinakaran Pandiyan 		mask = SDE_GMBUS_CPT;
37508664281bSPaulo Zanoni 
37513488d4ebSVille Syrjälä 	gen3_assert_iir_is_zero(dev_priv, SDEIIR);
3752d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
37532a57d9ccSImre Deak 
37542a57d9ccSImre Deak 	if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
37552a57d9ccSImre Deak 	    HAS_PCH_LPT(dev_priv))
37561a56b1a2SImre Deak 		ibx_hpd_detection_setup(dev_priv);
37572a57d9ccSImre Deak 	else
37582a57d9ccSImre Deak 		spt_hpd_detection_setup(dev_priv);
3759d46da437SPaulo Zanoni }
3760d46da437SPaulo Zanoni 
37610a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
37620a9a8c91SDaniel Vetter {
3763fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
37640a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
37650a9a8c91SDaniel Vetter 
37660a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
37670a9a8c91SDaniel Vetter 
37680a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
37693c9192bcSTvrtko Ursulin 	if (HAS_L3_DPF(dev_priv)) {
37700a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
3771772c2a51STvrtko Ursulin 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev_priv);
3772772c2a51STvrtko Ursulin 		gt_irqs |= GT_PARITY_ERROR(dev_priv);
37730a9a8c91SDaniel Vetter 	}
37740a9a8c91SDaniel Vetter 
37750a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
3776cf819effSLucas De Marchi 	if (IS_GEN(dev_priv, 5)) {
3777f8973c21SChris Wilson 		gt_irqs |= ILK_BSD_USER_INTERRUPT;
37780a9a8c91SDaniel Vetter 	} else {
37790a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
37800a9a8c91SDaniel Vetter 	}
37810a9a8c91SDaniel Vetter 
37823488d4ebSVille Syrjälä 	GEN3_IRQ_INIT(GT, dev_priv->gt_irq_mask, gt_irqs);
37830a9a8c91SDaniel Vetter 
3784b243f530STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 6) {
378578e68d36SImre Deak 		/*
378678e68d36SImre Deak 		 * RPS interrupts will get enabled/disabled on demand when RPS
378778e68d36SImre Deak 		 * itself is enabled/disabled.
378878e68d36SImre Deak 		 */
37898a68d464SChris Wilson 		if (HAS_ENGINE(dev_priv, VECS0)) {
37900a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
3791f4e9af4fSAkash Goel 			dev_priv->pm_ier |= PM_VEBOX_USER_INTERRUPT;
3792f4e9af4fSAkash Goel 		}
37930a9a8c91SDaniel Vetter 
3794f4e9af4fSAkash Goel 		dev_priv->pm_imr = 0xffffffff;
37953488d4ebSVille Syrjälä 		GEN3_IRQ_INIT(GEN6_PM, dev_priv->pm_imr, pm_irqs);
37960a9a8c91SDaniel Vetter 	}
37970a9a8c91SDaniel Vetter }
37980a9a8c91SDaniel Vetter 
3799f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
3800036a4a7dSZhenyu Wang {
3801fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
38028e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
38038e76f8dcSPaulo Zanoni 
3804b243f530STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 7) {
38058e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
3806842ebf7aSVille Syrjälä 				DE_PCH_EVENT_IVB | DE_AUX_CHANNEL_A_IVB);
38078e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
380823bb4cb5SVille Syrjälä 			      DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB |
380923bb4cb5SVille Syrjälä 			      DE_DP_A_HOTPLUG_IVB);
38108e76f8dcSPaulo Zanoni 	} else {
38118e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3812842ebf7aSVille Syrjälä 				DE_AUX_CHANNEL_A | DE_PIPEB_CRC_DONE |
3813842ebf7aSVille Syrjälä 				DE_PIPEA_CRC_DONE | DE_POISON);
3814e4ce95aaSVille Syrjälä 		extra_mask = (DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
3815e4ce95aaSVille Syrjälä 			      DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN |
3816e4ce95aaSVille Syrjälä 			      DE_DP_A_HOTPLUG);
38178e76f8dcSPaulo Zanoni 	}
3818036a4a7dSZhenyu Wang 
3819fc340442SDaniel Vetter 	if (IS_HASWELL(dev_priv)) {
3820fc340442SDaniel Vetter 		gen3_assert_iir_is_zero(dev_priv, EDP_PSR_IIR);
38211aeb1b5fSDhinakaran Pandiyan 		intel_psr_irq_control(dev_priv, dev_priv->psr.debug);
3822fc340442SDaniel Vetter 		display_mask |= DE_EDP_PSR_INT_HSW;
3823fc340442SDaniel Vetter 	}
3824fc340442SDaniel Vetter 
38251ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3826036a4a7dSZhenyu Wang 
3827622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3828622364b6SPaulo Zanoni 
38293488d4ebSVille Syrjälä 	GEN3_IRQ_INIT(DE, dev_priv->irq_mask, display_mask | extra_mask);
3830036a4a7dSZhenyu Wang 
38310a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
3832036a4a7dSZhenyu Wang 
38331a56b1a2SImre Deak 	ilk_hpd_detection_setup(dev_priv);
38341a56b1a2SImre Deak 
3835d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
38367fe0b973SKeith Packard 
383750a0bc90STvrtko Ursulin 	if (IS_IRONLAKE_M(dev_priv)) {
38386005ce42SDaniel Vetter 		/* Enable PCU event interrupts
38396005ce42SDaniel Vetter 		 *
38406005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
38414bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
38424bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
3843d6207435SDaniel Vetter 		spin_lock_irq(&dev_priv->irq_lock);
3844fbdedaeaSVille Syrjälä 		ilk_enable_display_irq(dev_priv, DE_PCU_EVENT);
3845d6207435SDaniel Vetter 		spin_unlock_irq(&dev_priv->irq_lock);
3846f97108d1SJesse Barnes 	}
3847f97108d1SJesse Barnes 
3848036a4a7dSZhenyu Wang 	return 0;
3849036a4a7dSZhenyu Wang }
3850036a4a7dSZhenyu Wang 
3851f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3852f8b79e58SImre Deak {
385367520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
3854f8b79e58SImre Deak 
3855f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3856f8b79e58SImre Deak 		return;
3857f8b79e58SImre Deak 
3858f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3859f8b79e58SImre Deak 
3860d6c69803SVille Syrjälä 	if (intel_irqs_enabled(dev_priv)) {
3861d6c69803SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3862ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3863f8b79e58SImre Deak 	}
3864d6c69803SVille Syrjälä }
3865f8b79e58SImre Deak 
3866f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3867f8b79e58SImre Deak {
386867520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
3869f8b79e58SImre Deak 
3870f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3871f8b79e58SImre Deak 		return;
3872f8b79e58SImre Deak 
3873f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3874f8b79e58SImre Deak 
3875950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3876ad22d106SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3877f8b79e58SImre Deak }
3878f8b79e58SImre Deak 
38790e6c9a9eSVille Syrjälä 
38800e6c9a9eSVille Syrjälä static int valleyview_irq_postinstall(struct drm_device *dev)
38810e6c9a9eSVille Syrjälä {
3882fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
38830e6c9a9eSVille Syrjälä 
38840a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
38857e231dbeSJesse Barnes 
3886ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
38879918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3888ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3889ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3890ad22d106SVille Syrjälä 
38917e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
389234c7b8a7SVille Syrjälä 	POSTING_READ(VLV_MASTER_IER);
389320afbda2SDaniel Vetter 
389420afbda2SDaniel Vetter 	return 0;
389520afbda2SDaniel Vetter }
389620afbda2SDaniel Vetter 
3897abd58f01SBen Widawsky static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
3898abd58f01SBen Widawsky {
3899abd58f01SBen Widawsky 	/* These are interrupts we'll toggle with the ring mask register */
3900a9c287c9SJani Nikula 	u32 gt_interrupts[] = {
39018a68d464SChris Wilson 		(GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
390273d477f6SOscar Mateo 		 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
390373d477f6SOscar Mateo 		 GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT |
39048a68d464SChris Wilson 		 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT),
39058a68d464SChris Wilson 
39068a68d464SChris Wilson 		(GT_RENDER_USER_INTERRUPT << GEN8_VCS0_IRQ_SHIFT |
39078a68d464SChris Wilson 		 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS0_IRQ_SHIFT |
3908abd58f01SBen Widawsky 		 GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
39098a68d464SChris Wilson 		 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT),
39108a68d464SChris Wilson 
3911abd58f01SBen Widawsky 		0,
39128a68d464SChris Wilson 
39138a68d464SChris Wilson 		(GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT |
39148a68d464SChris Wilson 		 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT)
3915abd58f01SBen Widawsky 	};
3916abd58f01SBen Widawsky 
3917f4e9af4fSAkash Goel 	dev_priv->pm_ier = 0x0;
3918f4e9af4fSAkash Goel 	dev_priv->pm_imr = ~dev_priv->pm_ier;
39199a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 0, ~gt_interrupts[0], gt_interrupts[0]);
39209a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 1, ~gt_interrupts[1], gt_interrupts[1]);
392178e68d36SImre Deak 	/*
392278e68d36SImre Deak 	 * RPS interrupts will get enabled/disabled on demand when RPS itself
392326705e20SSagar Arun Kamble 	 * is enabled/disabled. Same wil be the case for GuC interrupts.
392478e68d36SImre Deak 	 */
3925f4e9af4fSAkash Goel 	GEN8_IRQ_INIT_NDX(GT, 2, dev_priv->pm_imr, dev_priv->pm_ier);
39269a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 3, ~gt_interrupts[3], gt_interrupts[3]);
3927abd58f01SBen Widawsky }
3928abd58f01SBen Widawsky 
3929abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3930abd58f01SBen Widawsky {
3931a9c287c9SJani Nikula 	u32 de_pipe_masked = GEN8_PIPE_CDCLK_CRC_DONE;
3932a9c287c9SJani Nikula 	u32 de_pipe_enables;
39333a3b3c7dSVille Syrjälä 	u32 de_port_masked = GEN8_AUX_CHANNEL_A;
39343a3b3c7dSVille Syrjälä 	u32 de_port_enables;
3935df0d28c1SDhinakaran Pandiyan 	u32 de_misc_masked = GEN8_DE_EDP_PSR;
39363a3b3c7dSVille Syrjälä 	enum pipe pipe;
3937770de83dSDamien Lespiau 
3938df0d28c1SDhinakaran Pandiyan 	if (INTEL_GEN(dev_priv) <= 10)
3939df0d28c1SDhinakaran Pandiyan 		de_misc_masked |= GEN8_DE_MISC_GSE;
3940df0d28c1SDhinakaran Pandiyan 
3941bca2bf2aSPandiyan, Dhinakaran 	if (INTEL_GEN(dev_priv) >= 9) {
3942842ebf7aSVille Syrjälä 		de_pipe_masked |= GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
39433a3b3c7dSVille Syrjälä 		de_port_masked |= GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
394488e04703SJesse Barnes 				  GEN9_AUX_CHANNEL_D;
3945cc3f90f0SAnder Conselvan de Oliveira 		if (IS_GEN9_LP(dev_priv))
39463a3b3c7dSVille Syrjälä 			de_port_masked |= BXT_DE_PORT_GMBUS;
39473a3b3c7dSVille Syrjälä 	} else {
3948842ebf7aSVille Syrjälä 		de_pipe_masked |= GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
39493a3b3c7dSVille Syrjälä 	}
3950770de83dSDamien Lespiau 
3951bb187e93SJames Ausmus 	if (INTEL_GEN(dev_priv) >= 11)
3952bb187e93SJames Ausmus 		de_port_masked |= ICL_AUX_CHANNEL_E;
3953bb187e93SJames Ausmus 
39549bb635d9SDhinakaran Pandiyan 	if (IS_CNL_WITH_PORT_F(dev_priv) || INTEL_GEN(dev_priv) >= 11)
3955a324fcacSRodrigo Vivi 		de_port_masked |= CNL_AUX_CHANNEL_F;
3956a324fcacSRodrigo Vivi 
3957770de83dSDamien Lespiau 	de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
3958770de83dSDamien Lespiau 					   GEN8_PIPE_FIFO_UNDERRUN;
3959770de83dSDamien Lespiau 
39603a3b3c7dSVille Syrjälä 	de_port_enables = de_port_masked;
3961cc3f90f0SAnder Conselvan de Oliveira 	if (IS_GEN9_LP(dev_priv))
3962a52bb15bSVille Syrjälä 		de_port_enables |= BXT_DE_PORT_HOTPLUG_MASK;
3963a52bb15bSVille Syrjälä 	else if (IS_BROADWELL(dev_priv))
39643a3b3c7dSVille Syrjälä 		de_port_enables |= GEN8_PORT_DP_A_HOTPLUG;
39653a3b3c7dSVille Syrjälä 
3966e04f7eceSVille Syrjälä 	gen3_assert_iir_is_zero(dev_priv, EDP_PSR_IIR);
396754fd3149SDhinakaran Pandiyan 	intel_psr_irq_control(dev_priv, dev_priv->psr.debug);
3968e04f7eceSVille Syrjälä 
39690a195c02SMika Kahola 	for_each_pipe(dev_priv, pipe) {
39700a195c02SMika Kahola 		dev_priv->de_irq_mask[pipe] = ~de_pipe_masked;
3971abd58f01SBen Widawsky 
3972f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3973813bde43SPaulo Zanoni 				POWER_DOMAIN_PIPE(pipe)))
3974813bde43SPaulo Zanoni 			GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
3975813bde43SPaulo Zanoni 					  dev_priv->de_irq_mask[pipe],
397635079899SPaulo Zanoni 					  de_pipe_enables);
39770a195c02SMika Kahola 	}
3978abd58f01SBen Widawsky 
39793488d4ebSVille Syrjälä 	GEN3_IRQ_INIT(GEN8_DE_PORT_, ~de_port_masked, de_port_enables);
39803488d4ebSVille Syrjälä 	GEN3_IRQ_INIT(GEN8_DE_MISC_, ~de_misc_masked, de_misc_masked);
39812a57d9ccSImre Deak 
3982121e758eSDhinakaran Pandiyan 	if (INTEL_GEN(dev_priv) >= 11) {
3983121e758eSDhinakaran Pandiyan 		u32 de_hpd_masked = 0;
3984b796b971SDhinakaran Pandiyan 		u32 de_hpd_enables = GEN11_DE_TC_HOTPLUG_MASK |
3985b796b971SDhinakaran Pandiyan 				     GEN11_DE_TBT_HOTPLUG_MASK;
3986121e758eSDhinakaran Pandiyan 
3987121e758eSDhinakaran Pandiyan 		GEN3_IRQ_INIT(GEN11_DE_HPD_, ~de_hpd_masked, de_hpd_enables);
3988121e758eSDhinakaran Pandiyan 		gen11_hpd_detection_setup(dev_priv);
3989121e758eSDhinakaran Pandiyan 	} else if (IS_GEN9_LP(dev_priv)) {
39902a57d9ccSImre Deak 		bxt_hpd_detection_setup(dev_priv);
3991121e758eSDhinakaran Pandiyan 	} else if (IS_BROADWELL(dev_priv)) {
39921a56b1a2SImre Deak 		ilk_hpd_detection_setup(dev_priv);
3993abd58f01SBen Widawsky 	}
3994121e758eSDhinakaran Pandiyan }
3995abd58f01SBen Widawsky 
3996abd58f01SBen Widawsky static int gen8_irq_postinstall(struct drm_device *dev)
3997abd58f01SBen Widawsky {
3998fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3999abd58f01SBen Widawsky 
40006e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
4001622364b6SPaulo Zanoni 		ibx_irq_pre_postinstall(dev);
4002622364b6SPaulo Zanoni 
4003abd58f01SBen Widawsky 	gen8_gt_irq_postinstall(dev_priv);
4004abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
4005abd58f01SBen Widawsky 
40066e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
4007abd58f01SBen Widawsky 		ibx_irq_postinstall(dev);
4008abd58f01SBen Widawsky 
40094376b9c9SMika Kuoppala 	gen8_master_intr_enable(dev_priv->regs);
4010abd58f01SBen Widawsky 
4011abd58f01SBen Widawsky 	return 0;
4012abd58f01SBen Widawsky }
4013abd58f01SBen Widawsky 
401451951ae7SMika Kuoppala static void gen11_gt_irq_postinstall(struct drm_i915_private *dev_priv)
401551951ae7SMika Kuoppala {
401651951ae7SMika Kuoppala 	const u32 irqs = GT_RENDER_USER_INTERRUPT | GT_CONTEXT_SWITCH_INTERRUPT;
401751951ae7SMika Kuoppala 
401851951ae7SMika Kuoppala 	BUILD_BUG_ON(irqs & 0xffff0000);
401951951ae7SMika Kuoppala 
402051951ae7SMika Kuoppala 	/* Enable RCS, BCS, VCS and VECS class interrupts. */
402151951ae7SMika Kuoppala 	I915_WRITE(GEN11_RENDER_COPY_INTR_ENABLE, irqs << 16 | irqs);
402251951ae7SMika Kuoppala 	I915_WRITE(GEN11_VCS_VECS_INTR_ENABLE,	  irqs << 16 | irqs);
402351951ae7SMika Kuoppala 
402451951ae7SMika Kuoppala 	/* Unmask irqs on RCS, BCS, VCS and VECS engines. */
402551951ae7SMika Kuoppala 	I915_WRITE(GEN11_RCS0_RSVD_INTR_MASK,	~(irqs << 16));
402651951ae7SMika Kuoppala 	I915_WRITE(GEN11_BCS_RSVD_INTR_MASK,	~(irqs << 16));
402751951ae7SMika Kuoppala 	I915_WRITE(GEN11_VCS0_VCS1_INTR_MASK,	~(irqs | irqs << 16));
402851951ae7SMika Kuoppala 	I915_WRITE(GEN11_VCS2_VCS3_INTR_MASK,	~(irqs | irqs << 16));
402951951ae7SMika Kuoppala 	I915_WRITE(GEN11_VECS0_VECS1_INTR_MASK,	~(irqs | irqs << 16));
403051951ae7SMika Kuoppala 
4031d02b98b8SOscar Mateo 	/*
4032d02b98b8SOscar Mateo 	 * RPS interrupts will get enabled/disabled on demand when RPS itself
4033d02b98b8SOscar Mateo 	 * is enabled/disabled.
4034d02b98b8SOscar Mateo 	 */
4035d02b98b8SOscar Mateo 	dev_priv->pm_ier = 0x0;
4036d02b98b8SOscar Mateo 	dev_priv->pm_imr = ~dev_priv->pm_ier;
4037d02b98b8SOscar Mateo 	I915_WRITE(GEN11_GPM_WGBOXPERF_INTR_ENABLE, 0);
4038d02b98b8SOscar Mateo 	I915_WRITE(GEN11_GPM_WGBOXPERF_INTR_MASK,  ~0);
403951951ae7SMika Kuoppala }
404051951ae7SMika Kuoppala 
404131604222SAnusha Srivatsa static void icp_irq_postinstall(struct drm_device *dev)
404231604222SAnusha Srivatsa {
404331604222SAnusha Srivatsa 	struct drm_i915_private *dev_priv = to_i915(dev);
404431604222SAnusha Srivatsa 	u32 mask = SDE_GMBUS_ICP;
404531604222SAnusha Srivatsa 
404631604222SAnusha Srivatsa 	WARN_ON(I915_READ(SDEIER) != 0);
404731604222SAnusha Srivatsa 	I915_WRITE(SDEIER, 0xffffffff);
404831604222SAnusha Srivatsa 	POSTING_READ(SDEIER);
404931604222SAnusha Srivatsa 
405031604222SAnusha Srivatsa 	gen3_assert_iir_is_zero(dev_priv, SDEIIR);
405131604222SAnusha Srivatsa 	I915_WRITE(SDEIMR, ~mask);
405231604222SAnusha Srivatsa 
405331604222SAnusha Srivatsa 	icp_hpd_detection_setup(dev_priv);
405431604222SAnusha Srivatsa }
405531604222SAnusha Srivatsa 
405651951ae7SMika Kuoppala static int gen11_irq_postinstall(struct drm_device *dev)
405751951ae7SMika Kuoppala {
405851951ae7SMika Kuoppala 	struct drm_i915_private *dev_priv = dev->dev_private;
4059df0d28c1SDhinakaran Pandiyan 	u32 gu_misc_masked = GEN11_GU_MISC_GSE;
406051951ae7SMika Kuoppala 
406129b43ae2SRodrigo Vivi 	if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
406231604222SAnusha Srivatsa 		icp_irq_postinstall(dev);
406331604222SAnusha Srivatsa 
406451951ae7SMika Kuoppala 	gen11_gt_irq_postinstall(dev_priv);
406551951ae7SMika Kuoppala 	gen8_de_irq_postinstall(dev_priv);
406651951ae7SMika Kuoppala 
4067df0d28c1SDhinakaran Pandiyan 	GEN3_IRQ_INIT(GEN11_GU_MISC_, ~gu_misc_masked, gu_misc_masked);
4068df0d28c1SDhinakaran Pandiyan 
406951951ae7SMika Kuoppala 	I915_WRITE(GEN11_DISPLAY_INT_CTL, GEN11_DISPLAY_IRQ_ENABLE);
407051951ae7SMika Kuoppala 
407181067b71SMika Kuoppala 	gen11_master_intr_enable(dev_priv->regs);
4072c25f0c6aSDaniele Ceraolo Spurio 	POSTING_READ(GEN11_GFX_MSTR_IRQ);
407351951ae7SMika Kuoppala 
407451951ae7SMika Kuoppala 	return 0;
407551951ae7SMika Kuoppala }
407651951ae7SMika Kuoppala 
407743f328d7SVille Syrjälä static int cherryview_irq_postinstall(struct drm_device *dev)
407843f328d7SVille Syrjälä {
4079fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
408043f328d7SVille Syrjälä 
408143f328d7SVille Syrjälä 	gen8_gt_irq_postinstall(dev_priv);
408243f328d7SVille Syrjälä 
4083ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
40849918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
4085ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
4086ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
4087ad22d106SVille Syrjälä 
4088e5328c43SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
408943f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
409043f328d7SVille Syrjälä 
409143f328d7SVille Syrjälä 	return 0;
409243f328d7SVille Syrjälä }
409343f328d7SVille Syrjälä 
40946bcdb1c8SVille Syrjälä static void i8xx_irq_reset(struct drm_device *dev)
4095c2798b19SChris Wilson {
4096fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4097c2798b19SChris Wilson 
409844d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
409944d9241eSVille Syrjälä 
4100e9e9848aSVille Syrjälä 	GEN2_IRQ_RESET();
4101c2798b19SChris Wilson }
4102c2798b19SChris Wilson 
4103c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
4104c2798b19SChris Wilson {
4105fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4106e9e9848aSVille Syrjälä 	u16 enable_mask;
4107c2798b19SChris Wilson 
4108045cebd2SVille Syrjälä 	I915_WRITE16(EMR, ~(I915_ERROR_PAGE_TABLE |
4109045cebd2SVille Syrjälä 			    I915_ERROR_MEMORY_REFRESH));
4110c2798b19SChris Wilson 
4111c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
4112c2798b19SChris Wilson 	dev_priv->irq_mask =
4113c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
411416659bc5SVille Syrjälä 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
411516659bc5SVille Syrjälä 		  I915_MASTER_ERROR_INTERRUPT);
4116c2798b19SChris Wilson 
4117e9e9848aSVille Syrjälä 	enable_mask =
4118c2798b19SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4119c2798b19SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
412016659bc5SVille Syrjälä 		I915_MASTER_ERROR_INTERRUPT |
4121e9e9848aSVille Syrjälä 		I915_USER_INTERRUPT;
4122e9e9848aSVille Syrjälä 
4123e9e9848aSVille Syrjälä 	GEN2_IRQ_INIT(, dev_priv->irq_mask, enable_mask);
4124c2798b19SChris Wilson 
4125379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4126379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4127d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4128755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4129755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4130d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4131379ef82dSDaniel Vetter 
4132c2798b19SChris Wilson 	return 0;
4133c2798b19SChris Wilson }
4134c2798b19SChris Wilson 
413578c357ddSVille Syrjälä static void i8xx_error_irq_ack(struct drm_i915_private *dev_priv,
413678c357ddSVille Syrjälä 			       u16 *eir, u16 *eir_stuck)
413778c357ddSVille Syrjälä {
413878c357ddSVille Syrjälä 	u16 emr;
413978c357ddSVille Syrjälä 
414078c357ddSVille Syrjälä 	*eir = I915_READ16(EIR);
414178c357ddSVille Syrjälä 
414278c357ddSVille Syrjälä 	if (*eir)
414378c357ddSVille Syrjälä 		I915_WRITE16(EIR, *eir);
414478c357ddSVille Syrjälä 
414578c357ddSVille Syrjälä 	*eir_stuck = I915_READ16(EIR);
414678c357ddSVille Syrjälä 	if (*eir_stuck == 0)
414778c357ddSVille Syrjälä 		return;
414878c357ddSVille Syrjälä 
414978c357ddSVille Syrjälä 	/*
415078c357ddSVille Syrjälä 	 * Toggle all EMR bits to make sure we get an edge
415178c357ddSVille Syrjälä 	 * in the ISR master error bit if we don't clear
415278c357ddSVille Syrjälä 	 * all the EIR bits. Otherwise the edge triggered
415378c357ddSVille Syrjälä 	 * IIR on i965/g4x wouldn't notice that an interrupt
415478c357ddSVille Syrjälä 	 * is still pending. Also some EIR bits can't be
415578c357ddSVille Syrjälä 	 * cleared except by handling the underlying error
415678c357ddSVille Syrjälä 	 * (or by a GPU reset) so we mask any bit that
415778c357ddSVille Syrjälä 	 * remains set.
415878c357ddSVille Syrjälä 	 */
415978c357ddSVille Syrjälä 	emr = I915_READ16(EMR);
416078c357ddSVille Syrjälä 	I915_WRITE16(EMR, 0xffff);
416178c357ddSVille Syrjälä 	I915_WRITE16(EMR, emr | *eir_stuck);
416278c357ddSVille Syrjälä }
416378c357ddSVille Syrjälä 
416478c357ddSVille Syrjälä static void i8xx_error_irq_handler(struct drm_i915_private *dev_priv,
416578c357ddSVille Syrjälä 				   u16 eir, u16 eir_stuck)
416678c357ddSVille Syrjälä {
416778c357ddSVille Syrjälä 	DRM_DEBUG("Master Error: EIR 0x%04x\n", eir);
416878c357ddSVille Syrjälä 
416978c357ddSVille Syrjälä 	if (eir_stuck)
417078c357ddSVille Syrjälä 		DRM_DEBUG_DRIVER("EIR stuck: 0x%04x, masked\n", eir_stuck);
417178c357ddSVille Syrjälä }
417278c357ddSVille Syrjälä 
417378c357ddSVille Syrjälä static void i9xx_error_irq_ack(struct drm_i915_private *dev_priv,
417478c357ddSVille Syrjälä 			       u32 *eir, u32 *eir_stuck)
417578c357ddSVille Syrjälä {
417678c357ddSVille Syrjälä 	u32 emr;
417778c357ddSVille Syrjälä 
417878c357ddSVille Syrjälä 	*eir = I915_READ(EIR);
417978c357ddSVille Syrjälä 
418078c357ddSVille Syrjälä 	I915_WRITE(EIR, *eir);
418178c357ddSVille Syrjälä 
418278c357ddSVille Syrjälä 	*eir_stuck = I915_READ(EIR);
418378c357ddSVille Syrjälä 	if (*eir_stuck == 0)
418478c357ddSVille Syrjälä 		return;
418578c357ddSVille Syrjälä 
418678c357ddSVille Syrjälä 	/*
418778c357ddSVille Syrjälä 	 * Toggle all EMR bits to make sure we get an edge
418878c357ddSVille Syrjälä 	 * in the ISR master error bit if we don't clear
418978c357ddSVille Syrjälä 	 * all the EIR bits. Otherwise the edge triggered
419078c357ddSVille Syrjälä 	 * IIR on i965/g4x wouldn't notice that an interrupt
419178c357ddSVille Syrjälä 	 * is still pending. Also some EIR bits can't be
419278c357ddSVille Syrjälä 	 * cleared except by handling the underlying error
419378c357ddSVille Syrjälä 	 * (or by a GPU reset) so we mask any bit that
419478c357ddSVille Syrjälä 	 * remains set.
419578c357ddSVille Syrjälä 	 */
419678c357ddSVille Syrjälä 	emr = I915_READ(EMR);
419778c357ddSVille Syrjälä 	I915_WRITE(EMR, 0xffffffff);
419878c357ddSVille Syrjälä 	I915_WRITE(EMR, emr | *eir_stuck);
419978c357ddSVille Syrjälä }
420078c357ddSVille Syrjälä 
420178c357ddSVille Syrjälä static void i9xx_error_irq_handler(struct drm_i915_private *dev_priv,
420278c357ddSVille Syrjälä 				   u32 eir, u32 eir_stuck)
420378c357ddSVille Syrjälä {
420478c357ddSVille Syrjälä 	DRM_DEBUG("Master Error, EIR 0x%08x\n", eir);
420578c357ddSVille Syrjälä 
420678c357ddSVille Syrjälä 	if (eir_stuck)
420778c357ddSVille Syrjälä 		DRM_DEBUG_DRIVER("EIR stuck: 0x%08x, masked\n", eir_stuck);
420878c357ddSVille Syrjälä }
420978c357ddSVille Syrjälä 
4210ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
4211c2798b19SChris Wilson {
421245a83f84SDaniel Vetter 	struct drm_device *dev = arg;
4213fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4214af722d28SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
4215c2798b19SChris Wilson 
42162dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
42172dd2a883SImre Deak 		return IRQ_NONE;
42182dd2a883SImre Deak 
42191f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
42201f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
42211f814dacSImre Deak 
4222af722d28SVille Syrjälä 	do {
4223af722d28SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
422478c357ddSVille Syrjälä 		u16 eir = 0, eir_stuck = 0;
4225af722d28SVille Syrjälä 		u16 iir;
4226af722d28SVille Syrjälä 
4227c2798b19SChris Wilson 		iir = I915_READ16(IIR);
4228c2798b19SChris Wilson 		if (iir == 0)
4229af722d28SVille Syrjälä 			break;
4230c2798b19SChris Wilson 
4231af722d28SVille Syrjälä 		ret = IRQ_HANDLED;
4232c2798b19SChris Wilson 
4233eb64343cSVille Syrjälä 		/* Call regardless, as some status bits might not be
4234eb64343cSVille Syrjälä 		 * signalled in iir */
4235eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
4236c2798b19SChris Wilson 
423778c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
423878c357ddSVille Syrjälä 			i8xx_error_irq_ack(dev_priv, &eir, &eir_stuck);
423978c357ddSVille Syrjälä 
4240fd3a4024SDaniel Vetter 		I915_WRITE16(IIR, iir);
4241c2798b19SChris Wilson 
4242c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
42438a68d464SChris Wilson 			intel_engine_breadcrumbs_irq(dev_priv->engine[RCS0]);
4244c2798b19SChris Wilson 
424578c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
424678c357ddSVille Syrjälä 			i8xx_error_irq_handler(dev_priv, eir, eir_stuck);
4247af722d28SVille Syrjälä 
4248eb64343cSVille Syrjälä 		i8xx_pipestat_irq_handler(dev_priv, iir, pipe_stats);
4249af722d28SVille Syrjälä 	} while (0);
4250c2798b19SChris Wilson 
42511f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
42521f814dacSImre Deak 
42531f814dacSImre Deak 	return ret;
4254c2798b19SChris Wilson }
4255c2798b19SChris Wilson 
42566bcdb1c8SVille Syrjälä static void i915_irq_reset(struct drm_device *dev)
4257a266c7d5SChris Wilson {
4258fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4259a266c7d5SChris Wilson 
426056b857a5STvrtko Ursulin 	if (I915_HAS_HOTPLUG(dev_priv)) {
42610706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4262a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4263a266c7d5SChris Wilson 	}
4264a266c7d5SChris Wilson 
426544d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
426644d9241eSVille Syrjälä 
4267ba7eb789SVille Syrjälä 	GEN3_IRQ_RESET();
4268a266c7d5SChris Wilson }
4269a266c7d5SChris Wilson 
4270a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
4271a266c7d5SChris Wilson {
4272fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
427338bde180SChris Wilson 	u32 enable_mask;
4274a266c7d5SChris Wilson 
4275045cebd2SVille Syrjälä 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE |
4276045cebd2SVille Syrjälä 			  I915_ERROR_MEMORY_REFRESH));
427738bde180SChris Wilson 
427838bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
427938bde180SChris Wilson 	dev_priv->irq_mask =
428038bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
428138bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
428216659bc5SVille Syrjälä 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
428316659bc5SVille Syrjälä 		  I915_MASTER_ERROR_INTERRUPT);
428438bde180SChris Wilson 
428538bde180SChris Wilson 	enable_mask =
428638bde180SChris Wilson 		I915_ASLE_INTERRUPT |
428738bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
428838bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
428916659bc5SVille Syrjälä 		I915_MASTER_ERROR_INTERRUPT |
429038bde180SChris Wilson 		I915_USER_INTERRUPT;
429138bde180SChris Wilson 
429256b857a5STvrtko Ursulin 	if (I915_HAS_HOTPLUG(dev_priv)) {
4293a266c7d5SChris Wilson 		/* Enable in IER... */
4294a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
4295a266c7d5SChris Wilson 		/* and unmask in IMR */
4296a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
4297a266c7d5SChris Wilson 	}
4298a266c7d5SChris Wilson 
4299ba7eb789SVille Syrjälä 	GEN3_IRQ_INIT(, dev_priv->irq_mask, enable_mask);
4300a266c7d5SChris Wilson 
4301379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4302379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4303d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4304755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4305755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4306d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4307379ef82dSDaniel Vetter 
4308c30bb1fdSVille Syrjälä 	i915_enable_asle_pipestat(dev_priv);
4309c30bb1fdSVille Syrjälä 
431020afbda2SDaniel Vetter 	return 0;
431120afbda2SDaniel Vetter }
431220afbda2SDaniel Vetter 
4313ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
4314a266c7d5SChris Wilson {
431545a83f84SDaniel Vetter 	struct drm_device *dev = arg;
4316fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4317af722d28SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
4318a266c7d5SChris Wilson 
43192dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
43202dd2a883SImre Deak 		return IRQ_NONE;
43212dd2a883SImre Deak 
43221f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
43231f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
43241f814dacSImre Deak 
432538bde180SChris Wilson 	do {
4326eb64343cSVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
432778c357ddSVille Syrjälä 		u32 eir = 0, eir_stuck = 0;
4328af722d28SVille Syrjälä 		u32 hotplug_status = 0;
4329af722d28SVille Syrjälä 		u32 iir;
4330a266c7d5SChris Wilson 
4331af722d28SVille Syrjälä 		iir = I915_READ(IIR);
4332af722d28SVille Syrjälä 		if (iir == 0)
4333af722d28SVille Syrjälä 			break;
4334af722d28SVille Syrjälä 
4335af722d28SVille Syrjälä 		ret = IRQ_HANDLED;
4336af722d28SVille Syrjälä 
4337af722d28SVille Syrjälä 		if (I915_HAS_HOTPLUG(dev_priv) &&
4338af722d28SVille Syrjälä 		    iir & I915_DISPLAY_PORT_INTERRUPT)
4339af722d28SVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
4340a266c7d5SChris Wilson 
4341eb64343cSVille Syrjälä 		/* Call regardless, as some status bits might not be
4342eb64343cSVille Syrjälä 		 * signalled in iir */
4343eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
4344a266c7d5SChris Wilson 
434578c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
434678c357ddSVille Syrjälä 			i9xx_error_irq_ack(dev_priv, &eir, &eir_stuck);
434778c357ddSVille Syrjälä 
4348fd3a4024SDaniel Vetter 		I915_WRITE(IIR, iir);
4349a266c7d5SChris Wilson 
4350a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
43518a68d464SChris Wilson 			intel_engine_breadcrumbs_irq(dev_priv->engine[RCS0]);
4352a266c7d5SChris Wilson 
435378c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
435478c357ddSVille Syrjälä 			i9xx_error_irq_handler(dev_priv, eir, eir_stuck);
4355a266c7d5SChris Wilson 
4356af722d28SVille Syrjälä 		if (hotplug_status)
4357af722d28SVille Syrjälä 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
4358af722d28SVille Syrjälä 
4359af722d28SVille Syrjälä 		i915_pipestat_irq_handler(dev_priv, iir, pipe_stats);
4360af722d28SVille Syrjälä 	} while (0);
4361a266c7d5SChris Wilson 
43621f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
43631f814dacSImre Deak 
4364a266c7d5SChris Wilson 	return ret;
4365a266c7d5SChris Wilson }
4366a266c7d5SChris Wilson 
43676bcdb1c8SVille Syrjälä static void i965_irq_reset(struct drm_device *dev)
4368a266c7d5SChris Wilson {
4369fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4370a266c7d5SChris Wilson 
43710706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4372a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4373a266c7d5SChris Wilson 
437444d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
437544d9241eSVille Syrjälä 
4376ba7eb789SVille Syrjälä 	GEN3_IRQ_RESET();
4377a266c7d5SChris Wilson }
4378a266c7d5SChris Wilson 
4379a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
4380a266c7d5SChris Wilson {
4381fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4382bbba0a97SChris Wilson 	u32 enable_mask;
4383a266c7d5SChris Wilson 	u32 error_mask;
4384a266c7d5SChris Wilson 
4385045cebd2SVille Syrjälä 	/*
4386045cebd2SVille Syrjälä 	 * Enable some error detection, note the instruction error mask
4387045cebd2SVille Syrjälä 	 * bit is reserved, so we leave it masked.
4388045cebd2SVille Syrjälä 	 */
4389045cebd2SVille Syrjälä 	if (IS_G4X(dev_priv)) {
4390045cebd2SVille Syrjälä 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
4391045cebd2SVille Syrjälä 			       GM45_ERROR_MEM_PRIV |
4392045cebd2SVille Syrjälä 			       GM45_ERROR_CP_PRIV |
4393045cebd2SVille Syrjälä 			       I915_ERROR_MEMORY_REFRESH);
4394045cebd2SVille Syrjälä 	} else {
4395045cebd2SVille Syrjälä 		error_mask = ~(I915_ERROR_PAGE_TABLE |
4396045cebd2SVille Syrjälä 			       I915_ERROR_MEMORY_REFRESH);
4397045cebd2SVille Syrjälä 	}
4398045cebd2SVille Syrjälä 	I915_WRITE(EMR, error_mask);
4399045cebd2SVille Syrjälä 
4400a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
4401c30bb1fdSVille Syrjälä 	dev_priv->irq_mask =
4402c30bb1fdSVille Syrjälä 		~(I915_ASLE_INTERRUPT |
4403adca4730SChris Wilson 		  I915_DISPLAY_PORT_INTERRUPT |
4404bbba0a97SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4405bbba0a97SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
440678c357ddSVille Syrjälä 		  I915_MASTER_ERROR_INTERRUPT);
4407bbba0a97SChris Wilson 
4408c30bb1fdSVille Syrjälä 	enable_mask =
4409c30bb1fdSVille Syrjälä 		I915_ASLE_INTERRUPT |
4410c30bb1fdSVille Syrjälä 		I915_DISPLAY_PORT_INTERRUPT |
4411c30bb1fdSVille Syrjälä 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4412c30bb1fdSVille Syrjälä 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
441378c357ddSVille Syrjälä 		I915_MASTER_ERROR_INTERRUPT |
4414c30bb1fdSVille Syrjälä 		I915_USER_INTERRUPT;
4415bbba0a97SChris Wilson 
441691d14251STvrtko Ursulin 	if (IS_G4X(dev_priv))
4417bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
4418a266c7d5SChris Wilson 
4419c30bb1fdSVille Syrjälä 	GEN3_IRQ_INIT(, dev_priv->irq_mask, enable_mask);
4420c30bb1fdSVille Syrjälä 
4421b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4422b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4423d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4424755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
4425755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4426755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4427d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4428a266c7d5SChris Wilson 
442991d14251STvrtko Ursulin 	i915_enable_asle_pipestat(dev_priv);
443020afbda2SDaniel Vetter 
443120afbda2SDaniel Vetter 	return 0;
443220afbda2SDaniel Vetter }
443320afbda2SDaniel Vetter 
443491d14251STvrtko Ursulin static void i915_hpd_irq_setup(struct drm_i915_private *dev_priv)
443520afbda2SDaniel Vetter {
443620afbda2SDaniel Vetter 	u32 hotplug_en;
443720afbda2SDaniel Vetter 
443867520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
4439b5ea2d56SDaniel Vetter 
4440adca4730SChris Wilson 	/* Note HDMI and DP share hotplug bits */
4441e5868a31SEgbert Eich 	/* enable bits are the same for all generations */
444291d14251STvrtko Ursulin 	hotplug_en = intel_hpd_enabled_irqs(dev_priv, hpd_mask_i915);
4443a266c7d5SChris Wilson 	/* Programming the CRT detection parameters tends
4444a266c7d5SChris Wilson 	   to generate a spurious hotplug event about three
4445a266c7d5SChris Wilson 	   seconds later.  So just do it once.
4446a266c7d5SChris Wilson 	*/
444791d14251STvrtko Ursulin 	if (IS_G4X(dev_priv))
4448a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
4449a266c7d5SChris Wilson 	hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
4450a266c7d5SChris Wilson 
4451a266c7d5SChris Wilson 	/* Ignore TV since it's buggy */
44520706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv,
4453f9e3dc78SJani Nikula 					     HOTPLUG_INT_EN_MASK |
4454f9e3dc78SJani Nikula 					     CRT_HOTPLUG_VOLTAGE_COMPARE_MASK |
4455f9e3dc78SJani Nikula 					     CRT_HOTPLUG_ACTIVATION_PERIOD_64,
44560706f17cSEgbert Eich 					     hotplug_en);
4457a266c7d5SChris Wilson }
4458a266c7d5SChris Wilson 
4459ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
4460a266c7d5SChris Wilson {
446145a83f84SDaniel Vetter 	struct drm_device *dev = arg;
4462fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4463af722d28SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
4464a266c7d5SChris Wilson 
44652dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
44662dd2a883SImre Deak 		return IRQ_NONE;
44672dd2a883SImre Deak 
44681f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
44691f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
44701f814dacSImre Deak 
4471af722d28SVille Syrjälä 	do {
4472eb64343cSVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
447378c357ddSVille Syrjälä 		u32 eir = 0, eir_stuck = 0;
4474af722d28SVille Syrjälä 		u32 hotplug_status = 0;
4475af722d28SVille Syrjälä 		u32 iir;
44762c8ba29fSChris Wilson 
4477af722d28SVille Syrjälä 		iir = I915_READ(IIR);
4478af722d28SVille Syrjälä 		if (iir == 0)
4479af722d28SVille Syrjälä 			break;
4480af722d28SVille Syrjälä 
4481af722d28SVille Syrjälä 		ret = IRQ_HANDLED;
4482af722d28SVille Syrjälä 
4483af722d28SVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
4484af722d28SVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
4485a266c7d5SChris Wilson 
4486eb64343cSVille Syrjälä 		/* Call regardless, as some status bits might not be
4487eb64343cSVille Syrjälä 		 * signalled in iir */
4488eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
4489a266c7d5SChris Wilson 
449078c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
449178c357ddSVille Syrjälä 			i9xx_error_irq_ack(dev_priv, &eir, &eir_stuck);
449278c357ddSVille Syrjälä 
4493fd3a4024SDaniel Vetter 		I915_WRITE(IIR, iir);
4494a266c7d5SChris Wilson 
4495a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
44968a68d464SChris Wilson 			intel_engine_breadcrumbs_irq(dev_priv->engine[RCS0]);
4497af722d28SVille Syrjälä 
4498a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
44998a68d464SChris Wilson 			intel_engine_breadcrumbs_irq(dev_priv->engine[VCS0]);
4500a266c7d5SChris Wilson 
450178c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
450278c357ddSVille Syrjälä 			i9xx_error_irq_handler(dev_priv, eir, eir_stuck);
4503515ac2bbSDaniel Vetter 
4504af722d28SVille Syrjälä 		if (hotplug_status)
4505af722d28SVille Syrjälä 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
4506af722d28SVille Syrjälä 
4507af722d28SVille Syrjälä 		i965_pipestat_irq_handler(dev_priv, iir, pipe_stats);
4508af722d28SVille Syrjälä 	} while (0);
4509a266c7d5SChris Wilson 
45101f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
45111f814dacSImre Deak 
4512a266c7d5SChris Wilson 	return ret;
4513a266c7d5SChris Wilson }
4514a266c7d5SChris Wilson 
4515fca52a55SDaniel Vetter /**
4516fca52a55SDaniel Vetter  * intel_irq_init - initializes irq support
4517fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4518fca52a55SDaniel Vetter  *
4519fca52a55SDaniel Vetter  * This function initializes all the irq support including work items, timers
4520fca52a55SDaniel Vetter  * and all the vtables. It does not setup the interrupt itself though.
4521fca52a55SDaniel Vetter  */
4522b963291cSDaniel Vetter void intel_irq_init(struct drm_i915_private *dev_priv)
4523f71d4af4SJesse Barnes {
452491c8a326SChris Wilson 	struct drm_device *dev = &dev_priv->drm;
4525562d9baeSSagar Arun Kamble 	struct intel_rps *rps = &dev_priv->gt_pm.rps;
4526cefcff8fSJoonas Lahtinen 	int i;
45278b2e326dSChris Wilson 
452877913b39SJani Nikula 	intel_hpd_init_work(dev_priv);
452977913b39SJani Nikula 
4530562d9baeSSagar Arun Kamble 	INIT_WORK(&rps->work, gen6_pm_rps_work);
4531cefcff8fSJoonas Lahtinen 
4532a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
4533cefcff8fSJoonas Lahtinen 	for (i = 0; i < MAX_L3_SLICES; ++i)
4534cefcff8fSJoonas Lahtinen 		dev_priv->l3_parity.remap_info[i] = NULL;
45358b2e326dSChris Wilson 
45364805fe82STvrtko Ursulin 	if (HAS_GUC_SCHED(dev_priv))
453726705e20SSagar Arun Kamble 		dev_priv->pm_guc_events = GEN9_GUC_TO_HOST_INT_EVENT;
453826705e20SSagar Arun Kamble 
4539a6706b45SDeepak S 	/* Let's track the enabled rps events */
4540666a4537SWayne Boyer 	if (IS_VALLEYVIEW(dev_priv))
45416c65a587SVille Syrjälä 		/* WaGsvRC0ResidencyMethod:vlv */
4542e0e8c7cbSChris Wilson 		dev_priv->pm_rps_events = GEN6_PM_RP_UP_EI_EXPIRED;
454331685c25SDeepak S 	else
45444668f695SChris Wilson 		dev_priv->pm_rps_events = (GEN6_PM_RP_UP_THRESHOLD |
45454668f695SChris Wilson 					   GEN6_PM_RP_DOWN_THRESHOLD |
45464668f695SChris Wilson 					   GEN6_PM_RP_DOWN_TIMEOUT);
4547a6706b45SDeepak S 
4548562d9baeSSagar Arun Kamble 	rps->pm_intrmsk_mbz = 0;
45491800ad25SSagar Arun Kamble 
45501800ad25SSagar Arun Kamble 	/*
4551acf2dc22SMika Kuoppala 	 * SNB,IVB,HSW can while VLV,CHV may hard hang on looping batchbuffer
45521800ad25SSagar Arun Kamble 	 * if GEN6_PM_UP_EI_EXPIRED is masked.
45531800ad25SSagar Arun Kamble 	 *
45541800ad25SSagar Arun Kamble 	 * TODO: verify if this can be reproduced on VLV,CHV.
45551800ad25SSagar Arun Kamble 	 */
4556bca2bf2aSPandiyan, Dhinakaran 	if (INTEL_GEN(dev_priv) <= 7)
4557562d9baeSSagar Arun Kamble 		rps->pm_intrmsk_mbz |= GEN6_PM_RP_UP_EI_EXPIRED;
45581800ad25SSagar Arun Kamble 
4559bca2bf2aSPandiyan, Dhinakaran 	if (INTEL_GEN(dev_priv) >= 8)
4560562d9baeSSagar Arun Kamble 		rps->pm_intrmsk_mbz |= GEN8_PMINTR_DISABLE_REDIRECT_TO_GUC;
45611800ad25SSagar Arun Kamble 
456232db0b65SVille Syrjälä 	if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
4563fd8f507cSVille Syrjälä 		dev->driver->get_vblank_counter = g4x_get_vblank_counter;
456432db0b65SVille Syrjälä 	else if (INTEL_GEN(dev_priv) >= 3)
4565391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
4566f71d4af4SJesse Barnes 
456721da2700SVille Syrjälä 	/*
456821da2700SVille Syrjälä 	 * Opt out of the vblank disable timer on everything except gen2.
456921da2700SVille Syrjälä 	 * Gen2 doesn't have a hardware frame counter and so depends on
457021da2700SVille Syrjälä 	 * vblank interrupts to produce sane vblank seuquence numbers.
457121da2700SVille Syrjälä 	 */
4572cf819effSLucas De Marchi 	if (!IS_GEN(dev_priv, 2))
457321da2700SVille Syrjälä 		dev->vblank_disable_immediate = true;
457421da2700SVille Syrjälä 
4575262fd485SChris Wilson 	/* Most platforms treat the display irq block as an always-on
4576262fd485SChris Wilson 	 * power domain. vlv/chv can disable it at runtime and need
4577262fd485SChris Wilson 	 * special care to avoid writing any of the display block registers
4578262fd485SChris Wilson 	 * outside of the power domain. We defer setting up the display irqs
4579262fd485SChris Wilson 	 * in this case to the runtime pm.
4580262fd485SChris Wilson 	 */
4581262fd485SChris Wilson 	dev_priv->display_irqs_enabled = true;
4582262fd485SChris Wilson 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4583262fd485SChris Wilson 		dev_priv->display_irqs_enabled = false;
4584262fd485SChris Wilson 
4585317eaa95SLyude 	dev_priv->hotplug.hpd_storm_threshold = HPD_STORM_DEFAULT_THRESHOLD;
45869a64c650SLyude Paul 	/* If we have MST support, we want to avoid doing short HPD IRQ storm
45879a64c650SLyude Paul 	 * detection, as short HPD storms will occur as a natural part of
45889a64c650SLyude Paul 	 * sideband messaging with MST.
45899a64c650SLyude Paul 	 * On older platforms however, IRQ storms can occur with both long and
45909a64c650SLyude Paul 	 * short pulses, as seen on some G4x systems.
45919a64c650SLyude Paul 	 */
45929a64c650SLyude Paul 	dev_priv->hotplug.hpd_short_storm_enabled = !HAS_DP_MST(dev_priv);
4593317eaa95SLyude 
45941bf6ad62SDaniel Vetter 	dev->driver->get_vblank_timestamp = drm_calc_vbltimestamp_from_scanoutpos;
4595f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
4596f71d4af4SJesse Barnes 
4597b963291cSDaniel Vetter 	if (IS_CHERRYVIEW(dev_priv)) {
459843f328d7SVille Syrjälä 		dev->driver->irq_handler = cherryview_irq_handler;
45996bcdb1c8SVille Syrjälä 		dev->driver->irq_preinstall = cherryview_irq_reset;
460043f328d7SVille Syrjälä 		dev->driver->irq_postinstall = cherryview_irq_postinstall;
46016bcdb1c8SVille Syrjälä 		dev->driver->irq_uninstall = cherryview_irq_reset;
460286e83e35SChris Wilson 		dev->driver->enable_vblank = i965_enable_vblank;
460386e83e35SChris Wilson 		dev->driver->disable_vblank = i965_disable_vblank;
460443f328d7SVille Syrjälä 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4605b963291cSDaniel Vetter 	} else if (IS_VALLEYVIEW(dev_priv)) {
46067e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
46076bcdb1c8SVille Syrjälä 		dev->driver->irq_preinstall = valleyview_irq_reset;
46087e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
46096bcdb1c8SVille Syrjälä 		dev->driver->irq_uninstall = valleyview_irq_reset;
461086e83e35SChris Wilson 		dev->driver->enable_vblank = i965_enable_vblank;
461186e83e35SChris Wilson 		dev->driver->disable_vblank = i965_disable_vblank;
4612fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
461351951ae7SMika Kuoppala 	} else if (INTEL_GEN(dev_priv) >= 11) {
461451951ae7SMika Kuoppala 		dev->driver->irq_handler = gen11_irq_handler;
461551951ae7SMika Kuoppala 		dev->driver->irq_preinstall = gen11_irq_reset;
461651951ae7SMika Kuoppala 		dev->driver->irq_postinstall = gen11_irq_postinstall;
461751951ae7SMika Kuoppala 		dev->driver->irq_uninstall = gen11_irq_reset;
461851951ae7SMika Kuoppala 		dev->driver->enable_vblank = gen8_enable_vblank;
461951951ae7SMika Kuoppala 		dev->driver->disable_vblank = gen8_disable_vblank;
4620121e758eSDhinakaran Pandiyan 		dev_priv->display.hpd_irq_setup = gen11_hpd_irq_setup;
4621bca2bf2aSPandiyan, Dhinakaran 	} else if (INTEL_GEN(dev_priv) >= 8) {
4622abd58f01SBen Widawsky 		dev->driver->irq_handler = gen8_irq_handler;
4623723761b8SDaniel Vetter 		dev->driver->irq_preinstall = gen8_irq_reset;
4624abd58f01SBen Widawsky 		dev->driver->irq_postinstall = gen8_irq_postinstall;
46256bcdb1c8SVille Syrjälä 		dev->driver->irq_uninstall = gen8_irq_reset;
4626abd58f01SBen Widawsky 		dev->driver->enable_vblank = gen8_enable_vblank;
4627abd58f01SBen Widawsky 		dev->driver->disable_vblank = gen8_disable_vblank;
4628cc3f90f0SAnder Conselvan de Oliveira 		if (IS_GEN9_LP(dev_priv))
4629e0a20ad7SShashank Sharma 			dev_priv->display.hpd_irq_setup = bxt_hpd_irq_setup;
4630c6c30b91SRodrigo Vivi 		else if (INTEL_PCH_TYPE(dev_priv) >= PCH_SPT)
46316dbf30ceSVille Syrjälä 			dev_priv->display.hpd_irq_setup = spt_hpd_irq_setup;
46326dbf30ceSVille Syrjälä 		else
46333a3b3c7dSVille Syrjälä 			dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
46346e266956STvrtko Ursulin 	} else if (HAS_PCH_SPLIT(dev_priv)) {
4635f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
4636723761b8SDaniel Vetter 		dev->driver->irq_preinstall = ironlake_irq_reset;
4637f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
46386bcdb1c8SVille Syrjälä 		dev->driver->irq_uninstall = ironlake_irq_reset;
4639f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
4640f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
4641e4ce95aaSVille Syrjälä 		dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
4642f71d4af4SJesse Barnes 	} else {
4643cf819effSLucas De Marchi 		if (IS_GEN(dev_priv, 2)) {
46446bcdb1c8SVille Syrjälä 			dev->driver->irq_preinstall = i8xx_irq_reset;
4645c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
4646c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
46476bcdb1c8SVille Syrjälä 			dev->driver->irq_uninstall = i8xx_irq_reset;
464886e83e35SChris Wilson 			dev->driver->enable_vblank = i8xx_enable_vblank;
464986e83e35SChris Wilson 			dev->driver->disable_vblank = i8xx_disable_vblank;
4650cf819effSLucas De Marchi 		} else if (IS_GEN(dev_priv, 3)) {
46516bcdb1c8SVille Syrjälä 			dev->driver->irq_preinstall = i915_irq_reset;
4652a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
46536bcdb1c8SVille Syrjälä 			dev->driver->irq_uninstall = i915_irq_reset;
4654a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
465586e83e35SChris Wilson 			dev->driver->enable_vblank = i8xx_enable_vblank;
465686e83e35SChris Wilson 			dev->driver->disable_vblank = i8xx_disable_vblank;
4657c2798b19SChris Wilson 		} else {
46586bcdb1c8SVille Syrjälä 			dev->driver->irq_preinstall = i965_irq_reset;
4659a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
46606bcdb1c8SVille Syrjälä 			dev->driver->irq_uninstall = i965_irq_reset;
4661a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
466286e83e35SChris Wilson 			dev->driver->enable_vblank = i965_enable_vblank;
466386e83e35SChris Wilson 			dev->driver->disable_vblank = i965_disable_vblank;
4664c2798b19SChris Wilson 		}
4665778eb334SVille Syrjälä 		if (I915_HAS_HOTPLUG(dev_priv))
4666778eb334SVille Syrjälä 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4667f71d4af4SJesse Barnes 	}
4668f71d4af4SJesse Barnes }
466920afbda2SDaniel Vetter 
4670fca52a55SDaniel Vetter /**
4671cefcff8fSJoonas Lahtinen  * intel_irq_fini - deinitializes IRQ support
4672cefcff8fSJoonas Lahtinen  * @i915: i915 device instance
4673cefcff8fSJoonas Lahtinen  *
4674cefcff8fSJoonas Lahtinen  * This function deinitializes all the IRQ support.
4675cefcff8fSJoonas Lahtinen  */
4676cefcff8fSJoonas Lahtinen void intel_irq_fini(struct drm_i915_private *i915)
4677cefcff8fSJoonas Lahtinen {
4678cefcff8fSJoonas Lahtinen 	int i;
4679cefcff8fSJoonas Lahtinen 
4680cefcff8fSJoonas Lahtinen 	for (i = 0; i < MAX_L3_SLICES; ++i)
4681cefcff8fSJoonas Lahtinen 		kfree(i915->l3_parity.remap_info[i]);
4682cefcff8fSJoonas Lahtinen }
4683cefcff8fSJoonas Lahtinen 
4684cefcff8fSJoonas Lahtinen /**
4685fca52a55SDaniel Vetter  * intel_irq_install - enables the hardware interrupt
4686fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4687fca52a55SDaniel Vetter  *
4688fca52a55SDaniel Vetter  * This function enables the hardware interrupt handling, but leaves the hotplug
4689fca52a55SDaniel Vetter  * handling still disabled. It is called after intel_irq_init().
4690fca52a55SDaniel Vetter  *
4691fca52a55SDaniel Vetter  * In the driver load and resume code we need working interrupts in a few places
4692fca52a55SDaniel Vetter  * but don't want to deal with the hassle of concurrent probe and hotplug
4693fca52a55SDaniel Vetter  * workers. Hence the split into this two-stage approach.
4694fca52a55SDaniel Vetter  */
46952aeb7d3aSDaniel Vetter int intel_irq_install(struct drm_i915_private *dev_priv)
46962aeb7d3aSDaniel Vetter {
46972aeb7d3aSDaniel Vetter 	/*
46982aeb7d3aSDaniel Vetter 	 * We enable some interrupt sources in our postinstall hooks, so mark
46992aeb7d3aSDaniel Vetter 	 * interrupts as enabled _before_ actually enabling them to avoid
47002aeb7d3aSDaniel Vetter 	 * special cases in our ordering checks.
47012aeb7d3aSDaniel Vetter 	 */
4702ad1443f0SSagar Arun Kamble 	dev_priv->runtime_pm.irqs_enabled = true;
47032aeb7d3aSDaniel Vetter 
470491c8a326SChris Wilson 	return drm_irq_install(&dev_priv->drm, dev_priv->drm.pdev->irq);
47052aeb7d3aSDaniel Vetter }
47062aeb7d3aSDaniel Vetter 
4707fca52a55SDaniel Vetter /**
4708fca52a55SDaniel Vetter  * intel_irq_uninstall - finilizes all irq handling
4709fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4710fca52a55SDaniel Vetter  *
4711fca52a55SDaniel Vetter  * This stops interrupt and hotplug handling and unregisters and frees all
4712fca52a55SDaniel Vetter  * resources acquired in the init functions.
4713fca52a55SDaniel Vetter  */
47142aeb7d3aSDaniel Vetter void intel_irq_uninstall(struct drm_i915_private *dev_priv)
47152aeb7d3aSDaniel Vetter {
471691c8a326SChris Wilson 	drm_irq_uninstall(&dev_priv->drm);
47172aeb7d3aSDaniel Vetter 	intel_hpd_cancel_work(dev_priv);
4718ad1443f0SSagar Arun Kamble 	dev_priv->runtime_pm.irqs_enabled = false;
47192aeb7d3aSDaniel Vetter }
47202aeb7d3aSDaniel Vetter 
4721fca52a55SDaniel Vetter /**
4722fca52a55SDaniel Vetter  * intel_runtime_pm_disable_interrupts - runtime interrupt disabling
4723fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4724fca52a55SDaniel Vetter  *
4725fca52a55SDaniel Vetter  * This function is used to disable interrupts at runtime, both in the runtime
4726fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4727fca52a55SDaniel Vetter  */
4728b963291cSDaniel Vetter void intel_runtime_pm_disable_interrupts(struct drm_i915_private *dev_priv)
4729c67a470bSPaulo Zanoni {
473091c8a326SChris Wilson 	dev_priv->drm.driver->irq_uninstall(&dev_priv->drm);
4731ad1443f0SSagar Arun Kamble 	dev_priv->runtime_pm.irqs_enabled = false;
473291c8a326SChris Wilson 	synchronize_irq(dev_priv->drm.irq);
4733c67a470bSPaulo Zanoni }
4734c67a470bSPaulo Zanoni 
4735fca52a55SDaniel Vetter /**
4736fca52a55SDaniel Vetter  * intel_runtime_pm_enable_interrupts - runtime interrupt enabling
4737fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4738fca52a55SDaniel Vetter  *
4739fca52a55SDaniel Vetter  * This function is used to enable interrupts at runtime, both in the runtime
4740fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4741fca52a55SDaniel Vetter  */
4742b963291cSDaniel Vetter void intel_runtime_pm_enable_interrupts(struct drm_i915_private *dev_priv)
4743c67a470bSPaulo Zanoni {
4744ad1443f0SSagar Arun Kamble 	dev_priv->runtime_pm.irqs_enabled = true;
474591c8a326SChris Wilson 	dev_priv->drm.driver->irq_preinstall(&dev_priv->drm);
474691c8a326SChris Wilson 	dev_priv->drm.driver->irq_postinstall(&dev_priv->drm);
4747c67a470bSPaulo Zanoni }
4748