xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 8a68d464366efb5b294fa11ccf23b51306cc2695)
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 
751c0e09200SDave Airlie /**
752f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
75314bb2c11STvrtko Ursulin  * @dev_priv: i915 device private
75401c66889SZhao Yakui  */
75591d14251STvrtko Ursulin static void i915_enable_asle_pipestat(struct drm_i915_private *dev_priv)
75601c66889SZhao Yakui {
75791d14251STvrtko Ursulin 	if (!dev_priv->opregion.asle || !IS_MOBILE(dev_priv))
758f49e38ddSJani Nikula 		return;
759f49e38ddSJani Nikula 
76013321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
76101c66889SZhao Yakui 
762755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_STATUS);
76391d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 4)
7643b6c42e8SDaniel Vetter 		i915_enable_pipestat(dev_priv, PIPE_A,
765755e9019SImre Deak 				     PIPE_LEGACY_BLC_EVENT_STATUS);
7661ec14ad3SChris Wilson 
76713321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
76801c66889SZhao Yakui }
76901c66889SZhao Yakui 
770f75f3746SVille Syrjälä /*
771f75f3746SVille Syrjälä  * This timing diagram depicts the video signal in and
772f75f3746SVille Syrjälä  * around the vertical blanking period.
773f75f3746SVille Syrjälä  *
774f75f3746SVille Syrjälä  * Assumptions about the fictitious mode used in this example:
775f75f3746SVille Syrjälä  *  vblank_start >= 3
776f75f3746SVille Syrjälä  *  vsync_start = vblank_start + 1
777f75f3746SVille Syrjälä  *  vsync_end = vblank_start + 2
778f75f3746SVille Syrjälä  *  vtotal = vblank_start + 3
779f75f3746SVille Syrjälä  *
780f75f3746SVille Syrjälä  *           start of vblank:
781f75f3746SVille Syrjälä  *           latch double buffered registers
782f75f3746SVille Syrjälä  *           increment frame counter (ctg+)
783f75f3746SVille Syrjälä  *           generate start of vblank interrupt (gen4+)
784f75f3746SVille Syrjälä  *           |
785f75f3746SVille Syrjälä  *           |          frame start:
786f75f3746SVille Syrjälä  *           |          generate frame start interrupt (aka. vblank interrupt) (gmch)
787f75f3746SVille Syrjälä  *           |          may be shifted forward 1-3 extra lines via PIPECONF
788f75f3746SVille Syrjälä  *           |          |
789f75f3746SVille Syrjälä  *           |          |  start of vsync:
790f75f3746SVille Syrjälä  *           |          |  generate vsync interrupt
791f75f3746SVille Syrjälä  *           |          |  |
792f75f3746SVille Syrjälä  * ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx
793f75f3746SVille Syrjälä  *       .   \hs/   .      \hs/          \hs/          \hs/   .      \hs/
794f75f3746SVille Syrjälä  * ----va---> <-----------------vb--------------------> <--------va-------------
795f75f3746SVille Syrjälä  *       |          |       <----vs----->                     |
796f75f3746SVille Syrjälä  * -vbs-----> <---vbs+1---> <---vbs+2---> <-----0-----> <-----1-----> <-----2--- (scanline counter gen2)
797f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2---> <-----0--- (scanline counter gen3+)
798f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2- (scanline counter hsw+ hdmi)
799f75f3746SVille Syrjälä  *       |          |                                         |
800f75f3746SVille Syrjälä  *       last visible pixel                                   first visible pixel
801f75f3746SVille Syrjälä  *                  |                                         increment frame counter (gen3/4)
802f75f3746SVille Syrjälä  *                  pixel counter = vblank_start * htotal     pixel counter = 0 (gen3/4)
803f75f3746SVille Syrjälä  *
804f75f3746SVille Syrjälä  * x  = horizontal active
805f75f3746SVille Syrjälä  * _  = horizontal blanking
806f75f3746SVille Syrjälä  * hs = horizontal sync
807f75f3746SVille Syrjälä  * va = vertical active
808f75f3746SVille Syrjälä  * vb = vertical blanking
809f75f3746SVille Syrjälä  * vs = vertical sync
810f75f3746SVille Syrjälä  * vbs = vblank_start (number)
811f75f3746SVille Syrjälä  *
812f75f3746SVille Syrjälä  * Summary:
813f75f3746SVille Syrjälä  * - most events happen at the start of horizontal sync
814f75f3746SVille Syrjälä  * - frame start happens at the start of horizontal blank, 1-4 lines
815f75f3746SVille Syrjälä  *   (depending on PIPECONF settings) after the start of vblank
816f75f3746SVille Syrjälä  * - gen3/4 pixel and frame counter are synchronized with the start
817f75f3746SVille Syrjälä  *   of horizontal active on the first line of vertical active
818f75f3746SVille Syrjälä  */
819f75f3746SVille Syrjälä 
82042f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
82142f52ef8SKeith Packard  * we use as a pipe index
82242f52ef8SKeith Packard  */
82388e72717SThierry Reding static u32 i915_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
8240a3e67a4SJesse Barnes {
825fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
82632db0b65SVille Syrjälä 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
82732db0b65SVille Syrjälä 	const struct drm_display_mode *mode = &vblank->hwmode;
828f0f59a00SVille Syrjälä 	i915_reg_t high_frame, low_frame;
8290b2a8e09SVille Syrjälä 	u32 high1, high2, low, pixel, vbl_start, hsync_start, htotal;
830694e409dSVille Syrjälä 	unsigned long irqflags;
831391f75e2SVille Syrjälä 
83232db0b65SVille Syrjälä 	/*
83332db0b65SVille Syrjälä 	 * On i965gm TV output the frame counter only works up to
83432db0b65SVille Syrjälä 	 * the point when we enable the TV encoder. After that the
83532db0b65SVille Syrjälä 	 * frame counter ceases to work and reads zero. We need a
83632db0b65SVille Syrjälä 	 * vblank wait before enabling the TV encoder and so we
83732db0b65SVille Syrjälä 	 * have to enable vblank interrupts while the frame counter
83832db0b65SVille Syrjälä 	 * is still in a working state. However the core vblank code
83932db0b65SVille Syrjälä 	 * does not like us returning non-zero frame counter values
84032db0b65SVille Syrjälä 	 * when we've told it that we don't have a working frame
84132db0b65SVille Syrjälä 	 * counter. Thus we must stop non-zero values leaking out.
84232db0b65SVille Syrjälä 	 */
84332db0b65SVille Syrjälä 	if (!vblank->max_vblank_count)
84432db0b65SVille Syrjälä 		return 0;
84532db0b65SVille Syrjälä 
8460b2a8e09SVille Syrjälä 	htotal = mode->crtc_htotal;
8470b2a8e09SVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
8480b2a8e09SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
8490b2a8e09SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
8500b2a8e09SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
851391f75e2SVille Syrjälä 
8520b2a8e09SVille Syrjälä 	/* Convert to pixel count */
8530b2a8e09SVille Syrjälä 	vbl_start *= htotal;
8540b2a8e09SVille Syrjälä 
8550b2a8e09SVille Syrjälä 	/* Start of vblank event occurs at start of hsync */
8560b2a8e09SVille Syrjälä 	vbl_start -= htotal - hsync_start;
8570b2a8e09SVille Syrjälä 
8589db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
8599db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
8605eddb70bSChris Wilson 
861694e409dSVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
862694e409dSVille Syrjälä 
8630a3e67a4SJesse Barnes 	/*
8640a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
8650a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
8660a3e67a4SJesse Barnes 	 * register.
8670a3e67a4SJesse Barnes 	 */
8680a3e67a4SJesse Barnes 	do {
869694e409dSVille Syrjälä 		high1 = I915_READ_FW(high_frame) & PIPE_FRAME_HIGH_MASK;
870694e409dSVille Syrjälä 		low   = I915_READ_FW(low_frame);
871694e409dSVille Syrjälä 		high2 = I915_READ_FW(high_frame) & PIPE_FRAME_HIGH_MASK;
8720a3e67a4SJesse Barnes 	} while (high1 != high2);
8730a3e67a4SJesse Barnes 
874694e409dSVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
875694e409dSVille Syrjälä 
8765eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
877391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
8785eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
879391f75e2SVille Syrjälä 
880391f75e2SVille Syrjälä 	/*
881391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
882391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
883391f75e2SVille Syrjälä 	 * counter against vblank start.
884391f75e2SVille Syrjälä 	 */
885edc08d0aSVille Syrjälä 	return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
8860a3e67a4SJesse Barnes }
8870a3e67a4SJesse Barnes 
888974e59baSDave Airlie static u32 g4x_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
8899880b7a5SJesse Barnes {
890fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
8919880b7a5SJesse Barnes 
892649636efSVille Syrjälä 	return I915_READ(PIPE_FRMCOUNT_G4X(pipe));
8939880b7a5SJesse Barnes }
8949880b7a5SJesse Barnes 
895aec0246fSUma Shankar /*
896aec0246fSUma Shankar  * On certain encoders on certain platforms, pipe
897aec0246fSUma Shankar  * scanline register will not work to get the scanline,
898aec0246fSUma Shankar  * since the timings are driven from the PORT or issues
899aec0246fSUma Shankar  * with scanline register updates.
900aec0246fSUma Shankar  * This function will use Framestamp and current
901aec0246fSUma Shankar  * timestamp registers to calculate the scanline.
902aec0246fSUma Shankar  */
903aec0246fSUma Shankar static u32 __intel_get_crtc_scanline_from_timestamp(struct intel_crtc *crtc)
904aec0246fSUma Shankar {
905aec0246fSUma Shankar 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
906aec0246fSUma Shankar 	struct drm_vblank_crtc *vblank =
907aec0246fSUma Shankar 		&crtc->base.dev->vblank[drm_crtc_index(&crtc->base)];
908aec0246fSUma Shankar 	const struct drm_display_mode *mode = &vblank->hwmode;
909aec0246fSUma Shankar 	u32 vblank_start = mode->crtc_vblank_start;
910aec0246fSUma Shankar 	u32 vtotal = mode->crtc_vtotal;
911aec0246fSUma Shankar 	u32 htotal = mode->crtc_htotal;
912aec0246fSUma Shankar 	u32 clock = mode->crtc_clock;
913aec0246fSUma Shankar 	u32 scanline, scan_prev_time, scan_curr_time, scan_post_time;
914aec0246fSUma Shankar 
915aec0246fSUma Shankar 	/*
916aec0246fSUma Shankar 	 * To avoid the race condition where we might cross into the
917aec0246fSUma Shankar 	 * next vblank just between the PIPE_FRMTMSTMP and TIMESTAMP_CTR
918aec0246fSUma Shankar 	 * reads. We make sure we read PIPE_FRMTMSTMP and TIMESTAMP_CTR
919aec0246fSUma Shankar 	 * during the same frame.
920aec0246fSUma Shankar 	 */
921aec0246fSUma Shankar 	do {
922aec0246fSUma Shankar 		/*
923aec0246fSUma Shankar 		 * This field provides read back of the display
924aec0246fSUma Shankar 		 * pipe frame time stamp. The time stamp value
925aec0246fSUma Shankar 		 * is sampled at every start of vertical blank.
926aec0246fSUma Shankar 		 */
927aec0246fSUma Shankar 		scan_prev_time = I915_READ_FW(PIPE_FRMTMSTMP(crtc->pipe));
928aec0246fSUma Shankar 
929aec0246fSUma Shankar 		/*
930aec0246fSUma Shankar 		 * The TIMESTAMP_CTR register has the current
931aec0246fSUma Shankar 		 * time stamp value.
932aec0246fSUma Shankar 		 */
933aec0246fSUma Shankar 		scan_curr_time = I915_READ_FW(IVB_TIMESTAMP_CTR);
934aec0246fSUma Shankar 
935aec0246fSUma Shankar 		scan_post_time = I915_READ_FW(PIPE_FRMTMSTMP(crtc->pipe));
936aec0246fSUma Shankar 	} while (scan_post_time != scan_prev_time);
937aec0246fSUma Shankar 
938aec0246fSUma Shankar 	scanline = div_u64(mul_u32_u32(scan_curr_time - scan_prev_time,
939aec0246fSUma Shankar 					clock), 1000 * htotal);
940aec0246fSUma Shankar 	scanline = min(scanline, vtotal - 1);
941aec0246fSUma Shankar 	scanline = (scanline + vblank_start) % vtotal;
942aec0246fSUma Shankar 
943aec0246fSUma Shankar 	return scanline;
944aec0246fSUma Shankar }
945aec0246fSUma Shankar 
94675aa3f63SVille Syrjälä /* I915_READ_FW, only for fast reads of display block, no need for forcewake etc. */
947a225f079SVille Syrjälä static int __intel_get_crtc_scanline(struct intel_crtc *crtc)
948a225f079SVille Syrjälä {
949a225f079SVille Syrjälä 	struct drm_device *dev = crtc->base.dev;
950fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
9515caa0feaSDaniel Vetter 	const struct drm_display_mode *mode;
9525caa0feaSDaniel Vetter 	struct drm_vblank_crtc *vblank;
953a225f079SVille Syrjälä 	enum pipe pipe = crtc->pipe;
95480715b2fSVille Syrjälä 	int position, vtotal;
955a225f079SVille Syrjälä 
95672259536SVille Syrjälä 	if (!crtc->active)
95772259536SVille Syrjälä 		return -1;
95872259536SVille Syrjälä 
9595caa0feaSDaniel Vetter 	vblank = &crtc->base.dev->vblank[drm_crtc_index(&crtc->base)];
9605caa0feaSDaniel Vetter 	mode = &vblank->hwmode;
9615caa0feaSDaniel Vetter 
962aec0246fSUma Shankar 	if (mode->private_flags & I915_MODE_FLAG_GET_SCANLINE_FROM_TIMESTAMP)
963aec0246fSUma Shankar 		return __intel_get_crtc_scanline_from_timestamp(crtc);
964aec0246fSUma Shankar 
96580715b2fSVille Syrjälä 	vtotal = mode->crtc_vtotal;
966a225f079SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
967a225f079SVille Syrjälä 		vtotal /= 2;
968a225f079SVille Syrjälä 
969cf819effSLucas De Marchi 	if (IS_GEN(dev_priv, 2))
97075aa3f63SVille Syrjälä 		position = I915_READ_FW(PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
971a225f079SVille Syrjälä 	else
97275aa3f63SVille Syrjälä 		position = I915_READ_FW(PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
973a225f079SVille Syrjälä 
974a225f079SVille Syrjälä 	/*
97541b578fbSJesse Barnes 	 * On HSW, the DSL reg (0x70000) appears to return 0 if we
97641b578fbSJesse Barnes 	 * read it just before the start of vblank.  So try it again
97741b578fbSJesse Barnes 	 * so we don't accidentally end up spanning a vblank frame
97841b578fbSJesse Barnes 	 * increment, causing the pipe_update_end() code to squak at us.
97941b578fbSJesse Barnes 	 *
98041b578fbSJesse Barnes 	 * The nature of this problem means we can't simply check the ISR
98141b578fbSJesse Barnes 	 * bit and return the vblank start value; nor can we use the scanline
98241b578fbSJesse Barnes 	 * debug register in the transcoder as it appears to have the same
98341b578fbSJesse Barnes 	 * problem.  We may need to extend this to include other platforms,
98441b578fbSJesse Barnes 	 * but so far testing only shows the problem on HSW.
98541b578fbSJesse Barnes 	 */
98691d14251STvrtko Ursulin 	if (HAS_DDI(dev_priv) && !position) {
98741b578fbSJesse Barnes 		int i, temp;
98841b578fbSJesse Barnes 
98941b578fbSJesse Barnes 		for (i = 0; i < 100; i++) {
99041b578fbSJesse Barnes 			udelay(1);
991707bdd3fSVille Syrjälä 			temp = I915_READ_FW(PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
99241b578fbSJesse Barnes 			if (temp != position) {
99341b578fbSJesse Barnes 				position = temp;
99441b578fbSJesse Barnes 				break;
99541b578fbSJesse Barnes 			}
99641b578fbSJesse Barnes 		}
99741b578fbSJesse Barnes 	}
99841b578fbSJesse Barnes 
99941b578fbSJesse Barnes 	/*
100080715b2fSVille Syrjälä 	 * See update_scanline_offset() for the details on the
100180715b2fSVille Syrjälä 	 * scanline_offset adjustment.
1002a225f079SVille Syrjälä 	 */
100380715b2fSVille Syrjälä 	return (position + crtc->scanline_offset) % vtotal;
1004a225f079SVille Syrjälä }
1005a225f079SVille Syrjälä 
10061bf6ad62SDaniel Vetter static bool i915_get_crtc_scanoutpos(struct drm_device *dev, unsigned int pipe,
10071bf6ad62SDaniel Vetter 				     bool in_vblank_irq, int *vpos, int *hpos,
10083bb403bfSVille Syrjälä 				     ktime_t *stime, ktime_t *etime,
10093bb403bfSVille Syrjälä 				     const struct drm_display_mode *mode)
10100af7e4dfSMario Kleiner {
1011fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
101298187836SVille Syrjälä 	struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
101398187836SVille Syrjälä 								pipe);
10143aa18df8SVille Syrjälä 	int position;
101578e8fc6bSVille Syrjälä 	int vbl_start, vbl_end, hsync_start, htotal, vtotal;
1016ad3543edSMario Kleiner 	unsigned long irqflags;
10178a920e24SVille Syrjälä 	bool use_scanline_counter = INTEL_GEN(dev_priv) >= 5 ||
10188a920e24SVille Syrjälä 		IS_G4X(dev_priv) || IS_GEN(dev_priv, 2) ||
10198a920e24SVille Syrjälä 		mode->private_flags & I915_MODE_FLAG_USE_SCANLINE_COUNTER;
10200af7e4dfSMario Kleiner 
1021fc467a22SMaarten Lankhorst 	if (WARN_ON(!mode->crtc_clock)) {
10220af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
10239db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
10241bf6ad62SDaniel Vetter 		return false;
10250af7e4dfSMario Kleiner 	}
10260af7e4dfSMario Kleiner 
1027c2baf4b7SVille Syrjälä 	htotal = mode->crtc_htotal;
102878e8fc6bSVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
1029c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
1030c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
1031c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
10320af7e4dfSMario Kleiner 
1033d31faf65SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
1034d31faf65SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
1035d31faf65SVille Syrjälä 		vbl_end /= 2;
1036d31faf65SVille Syrjälä 		vtotal /= 2;
1037d31faf65SVille Syrjälä 	}
1038d31faf65SVille Syrjälä 
1039ad3543edSMario Kleiner 	/*
1040ad3543edSMario Kleiner 	 * Lock uncore.lock, as we will do multiple timing critical raw
1041ad3543edSMario Kleiner 	 * register reads, potentially with preemption disabled, so the
1042ad3543edSMario Kleiner 	 * following code must not block on uncore.lock.
1043ad3543edSMario Kleiner 	 */
1044ad3543edSMario Kleiner 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
1045ad3543edSMario Kleiner 
1046ad3543edSMario Kleiner 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
1047ad3543edSMario Kleiner 
1048ad3543edSMario Kleiner 	/* Get optional system timestamp before query. */
1049ad3543edSMario Kleiner 	if (stime)
1050ad3543edSMario Kleiner 		*stime = ktime_get();
1051ad3543edSMario Kleiner 
10528a920e24SVille Syrjälä 	if (use_scanline_counter) {
10530af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
10540af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
10550af7e4dfSMario Kleiner 		 */
1056a225f079SVille Syrjälä 		position = __intel_get_crtc_scanline(intel_crtc);
10570af7e4dfSMario Kleiner 	} else {
10580af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
10590af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
10600af7e4dfSMario Kleiner 		 * scanout position.
10610af7e4dfSMario Kleiner 		 */
106275aa3f63SVille Syrjälä 		position = (I915_READ_FW(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
10630af7e4dfSMario Kleiner 
10643aa18df8SVille Syrjälä 		/* convert to pixel counts */
10653aa18df8SVille Syrjälä 		vbl_start *= htotal;
10663aa18df8SVille Syrjälä 		vbl_end *= htotal;
10673aa18df8SVille Syrjälä 		vtotal *= htotal;
106878e8fc6bSVille Syrjälä 
106978e8fc6bSVille Syrjälä 		/*
10707e78f1cbSVille Syrjälä 		 * In interlaced modes, the pixel counter counts all pixels,
10717e78f1cbSVille Syrjälä 		 * so one field will have htotal more pixels. In order to avoid
10727e78f1cbSVille Syrjälä 		 * the reported position from jumping backwards when the pixel
10737e78f1cbSVille Syrjälä 		 * counter is beyond the length of the shorter field, just
10747e78f1cbSVille Syrjälä 		 * clamp the position the length of the shorter field. This
10757e78f1cbSVille Syrjälä 		 * matches how the scanline counter based position works since
10767e78f1cbSVille Syrjälä 		 * the scanline counter doesn't count the two half lines.
10777e78f1cbSVille Syrjälä 		 */
10787e78f1cbSVille Syrjälä 		if (position >= vtotal)
10797e78f1cbSVille Syrjälä 			position = vtotal - 1;
10807e78f1cbSVille Syrjälä 
10817e78f1cbSVille Syrjälä 		/*
108278e8fc6bSVille Syrjälä 		 * Start of vblank interrupt is triggered at start of hsync,
108378e8fc6bSVille Syrjälä 		 * just prior to the first active line of vblank. However we
108478e8fc6bSVille Syrjälä 		 * consider lines to start at the leading edge of horizontal
108578e8fc6bSVille Syrjälä 		 * active. So, should we get here before we've crossed into
108678e8fc6bSVille Syrjälä 		 * the horizontal active of the first line in vblank, we would
108778e8fc6bSVille Syrjälä 		 * not set the DRM_SCANOUTPOS_INVBL flag. In order to fix that,
108878e8fc6bSVille Syrjälä 		 * always add htotal-hsync_start to the current pixel position.
108978e8fc6bSVille Syrjälä 		 */
109078e8fc6bSVille Syrjälä 		position = (position + htotal - hsync_start) % vtotal;
10913aa18df8SVille Syrjälä 	}
10923aa18df8SVille Syrjälä 
1093ad3543edSMario Kleiner 	/* Get optional system timestamp after query. */
1094ad3543edSMario Kleiner 	if (etime)
1095ad3543edSMario Kleiner 		*etime = ktime_get();
1096ad3543edSMario Kleiner 
1097ad3543edSMario Kleiner 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
1098ad3543edSMario Kleiner 
1099ad3543edSMario Kleiner 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
1100ad3543edSMario Kleiner 
11013aa18df8SVille Syrjälä 	/*
11023aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
11033aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
11043aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
11053aa18df8SVille Syrjälä 	 * up since vbl_end.
11063aa18df8SVille Syrjälä 	 */
11073aa18df8SVille Syrjälä 	if (position >= vbl_start)
11083aa18df8SVille Syrjälä 		position -= vbl_end;
11093aa18df8SVille Syrjälä 	else
11103aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
11113aa18df8SVille Syrjälä 
11128a920e24SVille Syrjälä 	if (use_scanline_counter) {
11133aa18df8SVille Syrjälä 		*vpos = position;
11143aa18df8SVille Syrjälä 		*hpos = 0;
11153aa18df8SVille Syrjälä 	} else {
11160af7e4dfSMario Kleiner 		*vpos = position / htotal;
11170af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
11180af7e4dfSMario Kleiner 	}
11190af7e4dfSMario Kleiner 
11201bf6ad62SDaniel Vetter 	return true;
11210af7e4dfSMario Kleiner }
11220af7e4dfSMario Kleiner 
1123a225f079SVille Syrjälä int intel_get_crtc_scanline(struct intel_crtc *crtc)
1124a225f079SVille Syrjälä {
1125fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1126a225f079SVille Syrjälä 	unsigned long irqflags;
1127a225f079SVille Syrjälä 	int position;
1128a225f079SVille Syrjälä 
1129a225f079SVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
1130a225f079SVille Syrjälä 	position = __intel_get_crtc_scanline(crtc);
1131a225f079SVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
1132a225f079SVille Syrjälä 
1133a225f079SVille Syrjälä 	return position;
1134a225f079SVille Syrjälä }
1135a225f079SVille Syrjälä 
113691d14251STvrtko Ursulin static void ironlake_rps_change_irq_handler(struct drm_i915_private *dev_priv)
1137f97108d1SJesse Barnes {
1138b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
11399270388eSDaniel Vetter 	u8 new_delay;
11409270388eSDaniel Vetter 
1141d0ecd7e2SDaniel Vetter 	spin_lock(&mchdev_lock);
1142f97108d1SJesse Barnes 
114373edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
114473edd18fSDaniel Vetter 
114520e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
11469270388eSDaniel Vetter 
11477648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
1148b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
1149b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
1150f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
1151f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
1152f97108d1SJesse Barnes 
1153f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
1154b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
115520e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
115620e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
115720e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
115820e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
1159b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
116020e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
116120e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
116220e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
116320e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
1164f97108d1SJesse Barnes 	}
1165f97108d1SJesse Barnes 
116691d14251STvrtko Ursulin 	if (ironlake_set_drps(dev_priv, new_delay))
116720e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
1168f97108d1SJesse Barnes 
1169d0ecd7e2SDaniel Vetter 	spin_unlock(&mchdev_lock);
11709270388eSDaniel Vetter 
1171f97108d1SJesse Barnes 	return;
1172f97108d1SJesse Barnes }
1173f97108d1SJesse Barnes 
117443cf3bf0SChris Wilson static void vlv_c0_read(struct drm_i915_private *dev_priv,
117543cf3bf0SChris Wilson 			struct intel_rps_ei *ei)
117631685c25SDeepak S {
1177679cb6c1SMika Kuoppala 	ei->ktime = ktime_get_raw();
117843cf3bf0SChris Wilson 	ei->render_c0 = I915_READ(VLV_RENDER_C0_COUNT);
117943cf3bf0SChris Wilson 	ei->media_c0 = I915_READ(VLV_MEDIA_C0_COUNT);
118031685c25SDeepak S }
118131685c25SDeepak S 
118243cf3bf0SChris Wilson void gen6_rps_reset_ei(struct drm_i915_private *dev_priv)
118343cf3bf0SChris Wilson {
1184562d9baeSSagar Arun Kamble 	memset(&dev_priv->gt_pm.rps.ei, 0, sizeof(dev_priv->gt_pm.rps.ei));
118543cf3bf0SChris Wilson }
118643cf3bf0SChris Wilson 
118743cf3bf0SChris Wilson static u32 vlv_wa_c0_ei(struct drm_i915_private *dev_priv, u32 pm_iir)
118843cf3bf0SChris Wilson {
1189562d9baeSSagar Arun Kamble 	struct intel_rps *rps = &dev_priv->gt_pm.rps;
1190562d9baeSSagar Arun Kamble 	const struct intel_rps_ei *prev = &rps->ei;
119143cf3bf0SChris Wilson 	struct intel_rps_ei now;
119243cf3bf0SChris Wilson 	u32 events = 0;
119343cf3bf0SChris Wilson 
1194e0e8c7cbSChris Wilson 	if ((pm_iir & GEN6_PM_RP_UP_EI_EXPIRED) == 0)
119543cf3bf0SChris Wilson 		return 0;
119643cf3bf0SChris Wilson 
119743cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &now);
119831685c25SDeepak S 
1199679cb6c1SMika Kuoppala 	if (prev->ktime) {
1200e0e8c7cbSChris Wilson 		u64 time, c0;
1201569884e3SChris Wilson 		u32 render, media;
1202e0e8c7cbSChris Wilson 
1203679cb6c1SMika Kuoppala 		time = ktime_us_delta(now.ktime, prev->ktime);
12048f68d591SChris Wilson 
1205e0e8c7cbSChris Wilson 		time *= dev_priv->czclk_freq;
1206e0e8c7cbSChris Wilson 
1207e0e8c7cbSChris Wilson 		/* Workload can be split between render + media,
1208e0e8c7cbSChris Wilson 		 * e.g. SwapBuffers being blitted in X after being rendered in
1209e0e8c7cbSChris Wilson 		 * mesa. To account for this we need to combine both engines
1210e0e8c7cbSChris Wilson 		 * into our activity counter.
1211e0e8c7cbSChris Wilson 		 */
1212569884e3SChris Wilson 		render = now.render_c0 - prev->render_c0;
1213569884e3SChris Wilson 		media = now.media_c0 - prev->media_c0;
1214569884e3SChris Wilson 		c0 = max(render, media);
12156b7f6aa7SMika Kuoppala 		c0 *= 1000 * 100 << 8; /* to usecs and scale to threshold% */
1216e0e8c7cbSChris Wilson 
121760548c55SChris Wilson 		if (c0 > time * rps->power.up_threshold)
1218e0e8c7cbSChris Wilson 			events = GEN6_PM_RP_UP_THRESHOLD;
121960548c55SChris Wilson 		else if (c0 < time * rps->power.down_threshold)
1220e0e8c7cbSChris Wilson 			events = GEN6_PM_RP_DOWN_THRESHOLD;
122131685c25SDeepak S 	}
122231685c25SDeepak S 
1223562d9baeSSagar Arun Kamble 	rps->ei = now;
122443cf3bf0SChris Wilson 	return events;
122531685c25SDeepak S }
122631685c25SDeepak S 
12274912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
12283b8d8d91SJesse Barnes {
12292d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
1230562d9baeSSagar Arun Kamble 		container_of(work, struct drm_i915_private, gt_pm.rps.work);
1231562d9baeSSagar Arun Kamble 	struct intel_rps *rps = &dev_priv->gt_pm.rps;
12327c0a16adSChris Wilson 	bool client_boost = false;
12338d3afd7dSChris Wilson 	int new_delay, adj, min, max;
12347c0a16adSChris Wilson 	u32 pm_iir = 0;
12353b8d8d91SJesse Barnes 
123659cdb63dSDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
1237562d9baeSSagar Arun Kamble 	if (rps->interrupts_enabled) {
1238562d9baeSSagar Arun Kamble 		pm_iir = fetch_and_zero(&rps->pm_iir);
1239562d9baeSSagar Arun Kamble 		client_boost = atomic_read(&rps->num_waiters);
1240d4d70aa5SImre Deak 	}
124159cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
12424912d041SBen Widawsky 
124360611c13SPaulo Zanoni 	/* Make sure we didn't queue anything we're not going to process. */
1244a6706b45SDeepak S 	WARN_ON(pm_iir & ~dev_priv->pm_rps_events);
12458d3afd7dSChris Wilson 	if ((pm_iir & dev_priv->pm_rps_events) == 0 && !client_boost)
12467c0a16adSChris Wilson 		goto out;
12473b8d8d91SJesse Barnes 
12489f817501SSagar Arun Kamble 	mutex_lock(&dev_priv->pcu_lock);
12497b9e0ae6SChris Wilson 
125043cf3bf0SChris Wilson 	pm_iir |= vlv_wa_c0_ei(dev_priv, pm_iir);
125143cf3bf0SChris Wilson 
1252562d9baeSSagar Arun Kamble 	adj = rps->last_adj;
1253562d9baeSSagar Arun Kamble 	new_delay = rps->cur_freq;
1254562d9baeSSagar Arun Kamble 	min = rps->min_freq_softlimit;
1255562d9baeSSagar Arun Kamble 	max = rps->max_freq_softlimit;
12567b92c1bdSChris Wilson 	if (client_boost)
1257562d9baeSSagar Arun Kamble 		max = rps->max_freq;
1258562d9baeSSagar Arun Kamble 	if (client_boost && new_delay < rps->boost_freq) {
1259562d9baeSSagar Arun Kamble 		new_delay = rps->boost_freq;
12608d3afd7dSChris Wilson 		adj = 0;
12618d3afd7dSChris Wilson 	} else if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
1262dd75fdc8SChris Wilson 		if (adj > 0)
1263dd75fdc8SChris Wilson 			adj *= 2;
1264edcf284bSChris Wilson 		else /* CHV needs even encode values */
1265edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? 2 : 1;
12667e79a683SSagar Arun Kamble 
1267562d9baeSSagar Arun Kamble 		if (new_delay >= rps->max_freq_softlimit)
12687e79a683SSagar Arun Kamble 			adj = 0;
12697b92c1bdSChris Wilson 	} else if (client_boost) {
1270f5a4c67dSChris Wilson 		adj = 0;
1271dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
1272562d9baeSSagar Arun Kamble 		if (rps->cur_freq > rps->efficient_freq)
1273562d9baeSSagar Arun Kamble 			new_delay = rps->efficient_freq;
1274562d9baeSSagar Arun Kamble 		else if (rps->cur_freq > rps->min_freq_softlimit)
1275562d9baeSSagar Arun Kamble 			new_delay = rps->min_freq_softlimit;
1276dd75fdc8SChris Wilson 		adj = 0;
1277dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1278dd75fdc8SChris Wilson 		if (adj < 0)
1279dd75fdc8SChris Wilson 			adj *= 2;
1280edcf284bSChris Wilson 		else /* CHV needs even encode values */
1281edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? -2 : -1;
12827e79a683SSagar Arun Kamble 
1283562d9baeSSagar Arun Kamble 		if (new_delay <= rps->min_freq_softlimit)
12847e79a683SSagar Arun Kamble 			adj = 0;
1285dd75fdc8SChris Wilson 	} else { /* unknown event */
1286edcf284bSChris Wilson 		adj = 0;
1287dd75fdc8SChris Wilson 	}
12883b8d8d91SJesse Barnes 
1289562d9baeSSagar Arun Kamble 	rps->last_adj = adj;
1290edcf284bSChris Wilson 
12912a8862d2SChris Wilson 	/*
12922a8862d2SChris Wilson 	 * Limit deboosting and boosting to keep ourselves at the extremes
12932a8862d2SChris Wilson 	 * when in the respective power modes (i.e. slowly decrease frequencies
12942a8862d2SChris Wilson 	 * while in the HIGH_POWER zone and slowly increase frequencies while
12952a8862d2SChris Wilson 	 * in the LOW_POWER zone). On idle, we will hit the timeout and drop
12962a8862d2SChris Wilson 	 * to the next level quickly, and conversely if busy we expect to
12972a8862d2SChris Wilson 	 * hit a waitboost and rapidly switch into max power.
12982a8862d2SChris Wilson 	 */
12992a8862d2SChris Wilson 	if ((adj < 0 && rps->power.mode == HIGH_POWER) ||
13002a8862d2SChris Wilson 	    (adj > 0 && rps->power.mode == LOW_POWER))
13012a8862d2SChris Wilson 		rps->last_adj = 0;
13022a8862d2SChris Wilson 
130379249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
130479249636SBen Widawsky 	 * interrupt
130579249636SBen Widawsky 	 */
1306edcf284bSChris Wilson 	new_delay += adj;
13078d3afd7dSChris Wilson 	new_delay = clamp_t(int, new_delay, min, max);
130827544369SDeepak S 
13099fcee2f7SChris Wilson 	if (intel_set_rps(dev_priv, new_delay)) {
13109fcee2f7SChris Wilson 		DRM_DEBUG_DRIVER("Failed to set new GPU frequency\n");
1311562d9baeSSagar Arun Kamble 		rps->last_adj = 0;
13129fcee2f7SChris Wilson 	}
13133b8d8d91SJesse Barnes 
13149f817501SSagar Arun Kamble 	mutex_unlock(&dev_priv->pcu_lock);
13157c0a16adSChris Wilson 
13167c0a16adSChris Wilson out:
13177c0a16adSChris Wilson 	/* Make sure not to corrupt PMIMR state used by ringbuffer on GEN6 */
13187c0a16adSChris Wilson 	spin_lock_irq(&dev_priv->irq_lock);
1319562d9baeSSagar Arun Kamble 	if (rps->interrupts_enabled)
13207c0a16adSChris Wilson 		gen6_unmask_pm_irq(dev_priv, dev_priv->pm_rps_events);
13217c0a16adSChris Wilson 	spin_unlock_irq(&dev_priv->irq_lock);
13223b8d8d91SJesse Barnes }
13233b8d8d91SJesse Barnes 
1324e3689190SBen Widawsky 
1325e3689190SBen Widawsky /**
1326e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
1327e3689190SBen Widawsky  * occurred.
1328e3689190SBen Widawsky  * @work: workqueue struct
1329e3689190SBen Widawsky  *
1330e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
1331e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
1332e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
1333e3689190SBen Widawsky  */
1334e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
1335e3689190SBen Widawsky {
13362d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
1337cefcff8fSJoonas Lahtinen 		container_of(work, typeof(*dev_priv), l3_parity.error_work);
1338e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
133935a85ac6SBen Widawsky 	char *parity_event[6];
1340a9c287c9SJani Nikula 	u32 misccpctl;
1341a9c287c9SJani Nikula 	u8 slice = 0;
1342e3689190SBen Widawsky 
1343e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
1344e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
1345e3689190SBen Widawsky 	 * any time we access those registers.
1346e3689190SBen Widawsky 	 */
134791c8a326SChris Wilson 	mutex_lock(&dev_priv->drm.struct_mutex);
1348e3689190SBen Widawsky 
134935a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
135035a85ac6SBen Widawsky 	if (WARN_ON(!dev_priv->l3_parity.which_slice))
135135a85ac6SBen Widawsky 		goto out;
135235a85ac6SBen Widawsky 
1353e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
1354e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1355e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
1356e3689190SBen Widawsky 
135735a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
1358f0f59a00SVille Syrjälä 		i915_reg_t reg;
135935a85ac6SBen Widawsky 
136035a85ac6SBen Widawsky 		slice--;
13612d1fe073SJoonas Lahtinen 		if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv)))
136235a85ac6SBen Widawsky 			break;
136335a85ac6SBen Widawsky 
136435a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
136535a85ac6SBen Widawsky 
13666fa1c5f1SVille Syrjälä 		reg = GEN7_L3CDERRST1(slice);
136735a85ac6SBen Widawsky 
136835a85ac6SBen Widawsky 		error_status = I915_READ(reg);
1369e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
1370e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
1371e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1372e3689190SBen Widawsky 
137335a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
137435a85ac6SBen Widawsky 		POSTING_READ(reg);
1375e3689190SBen Widawsky 
1376cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1377e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1378e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1379e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
138035a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
138135a85ac6SBen Widawsky 		parity_event[5] = NULL;
1382e3689190SBen Widawsky 
138391c8a326SChris Wilson 		kobject_uevent_env(&dev_priv->drm.primary->kdev->kobj,
1384e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
1385e3689190SBen Widawsky 
138635a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
138735a85ac6SBen Widawsky 			  slice, row, bank, subbank);
1388e3689190SBen Widawsky 
138935a85ac6SBen Widawsky 		kfree(parity_event[4]);
1390e3689190SBen Widawsky 		kfree(parity_event[3]);
1391e3689190SBen Widawsky 		kfree(parity_event[2]);
1392e3689190SBen Widawsky 		kfree(parity_event[1]);
1393e3689190SBen Widawsky 	}
1394e3689190SBen Widawsky 
139535a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
139635a85ac6SBen Widawsky 
139735a85ac6SBen Widawsky out:
139835a85ac6SBen Widawsky 	WARN_ON(dev_priv->l3_parity.which_slice);
13994cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
14002d1fe073SJoonas Lahtinen 	gen5_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv));
14014cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
140235a85ac6SBen Widawsky 
140391c8a326SChris Wilson 	mutex_unlock(&dev_priv->drm.struct_mutex);
140435a85ac6SBen Widawsky }
140535a85ac6SBen Widawsky 
1406261e40b8SVille Syrjälä static void ivybridge_parity_error_irq_handler(struct drm_i915_private *dev_priv,
1407261e40b8SVille Syrjälä 					       u32 iir)
1408e3689190SBen Widawsky {
1409261e40b8SVille Syrjälä 	if (!HAS_L3_DPF(dev_priv))
1410e3689190SBen Widawsky 		return;
1411e3689190SBen Widawsky 
1412d0ecd7e2SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1413261e40b8SVille Syrjälä 	gen5_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv));
1414d0ecd7e2SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
1415e3689190SBen Widawsky 
1416261e40b8SVille Syrjälä 	iir &= GT_PARITY_ERROR(dev_priv);
141735a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
141835a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 1;
141935a85ac6SBen Widawsky 
142035a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
142135a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 0;
142235a85ac6SBen Widawsky 
1423a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
1424e3689190SBen Widawsky }
1425e3689190SBen Widawsky 
1426261e40b8SVille Syrjälä static void ilk_gt_irq_handler(struct drm_i915_private *dev_priv,
1427f1af8fc1SPaulo Zanoni 			       u32 gt_iir)
1428f1af8fc1SPaulo Zanoni {
1429f8973c21SChris Wilson 	if (gt_iir & GT_RENDER_USER_INTERRUPT)
1430*8a68d464SChris Wilson 		intel_engine_breadcrumbs_irq(dev_priv->engine[RCS0]);
1431f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
1432*8a68d464SChris Wilson 		intel_engine_breadcrumbs_irq(dev_priv->engine[VCS0]);
1433f1af8fc1SPaulo Zanoni }
1434f1af8fc1SPaulo Zanoni 
1435261e40b8SVille Syrjälä static void snb_gt_irq_handler(struct drm_i915_private *dev_priv,
1436e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1437e7b4c6b1SDaniel Vetter {
1438f8973c21SChris Wilson 	if (gt_iir & GT_RENDER_USER_INTERRUPT)
1439*8a68d464SChris Wilson 		intel_engine_breadcrumbs_irq(dev_priv->engine[RCS0]);
1440cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
1441*8a68d464SChris Wilson 		intel_engine_breadcrumbs_irq(dev_priv->engine[VCS0]);
1442cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
1443*8a68d464SChris Wilson 		intel_engine_breadcrumbs_irq(dev_priv->engine[BCS0]);
1444e7b4c6b1SDaniel Vetter 
1445cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1446cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
1447aaecdf61SDaniel Vetter 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT))
1448aaecdf61SDaniel Vetter 		DRM_DEBUG("Command parser error, gt_iir 0x%08x\n", gt_iir);
1449e3689190SBen Widawsky 
1450261e40b8SVille Syrjälä 	if (gt_iir & GT_PARITY_ERROR(dev_priv))
1451261e40b8SVille Syrjälä 		ivybridge_parity_error_irq_handler(dev_priv, gt_iir);
1452e7b4c6b1SDaniel Vetter }
1453e7b4c6b1SDaniel Vetter 
14545d3d69d5SChris Wilson static void
145551f6b0f9SChris Wilson gen8_cs_irq_handler(struct intel_engine_cs *engine, u32 iir)
1456fbcc1a0cSNick Hoath {
145731de7350SChris Wilson 	bool tasklet = false;
1458f747026cSChris Wilson 
1459fd8526e5SChris Wilson 	if (iir & GT_CONTEXT_SWITCH_INTERRUPT)
14608ea397faSChris Wilson 		tasklet = true;
146131de7350SChris Wilson 
146251f6b0f9SChris Wilson 	if (iir & GT_RENDER_USER_INTERRUPT) {
146352c0fdb2SChris Wilson 		intel_engine_breadcrumbs_irq(engine);
146493ffbe8eSMichal Wajdeczko 		tasklet |= USES_GUC_SUBMISSION(engine->i915);
146531de7350SChris Wilson 	}
146631de7350SChris Wilson 
146731de7350SChris Wilson 	if (tasklet)
1468fd8526e5SChris Wilson 		tasklet_hi_schedule(&engine->execlists.tasklet);
1469fbcc1a0cSNick Hoath }
1470fbcc1a0cSNick Hoath 
14712e4a5b25SChris Wilson static void gen8_gt_irq_ack(struct drm_i915_private *i915,
147255ef72f2SChris Wilson 			    u32 master_ctl, u32 gt_iir[4])
1473abd58f01SBen Widawsky {
14742e4a5b25SChris Wilson 	void __iomem * const regs = i915->regs;
14752e4a5b25SChris Wilson 
1476f0fd96f5SChris Wilson #define GEN8_GT_IRQS (GEN8_GT_RCS_IRQ | \
1477f0fd96f5SChris Wilson 		      GEN8_GT_BCS_IRQ | \
1478*8a68d464SChris Wilson 		      GEN8_GT_VCS0_IRQ | \
1479f0fd96f5SChris Wilson 		      GEN8_GT_VCS1_IRQ | \
1480f0fd96f5SChris Wilson 		      GEN8_GT_VECS_IRQ | \
1481f0fd96f5SChris Wilson 		      GEN8_GT_PM_IRQ | \
1482f0fd96f5SChris Wilson 		      GEN8_GT_GUC_IRQ)
1483f0fd96f5SChris Wilson 
1484abd58f01SBen Widawsky 	if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
14852e4a5b25SChris Wilson 		gt_iir[0] = raw_reg_read(regs, GEN8_GT_IIR(0));
14862e4a5b25SChris Wilson 		if (likely(gt_iir[0]))
14872e4a5b25SChris Wilson 			raw_reg_write(regs, GEN8_GT_IIR(0), gt_iir[0]);
1488abd58f01SBen Widawsky 	}
1489abd58f01SBen Widawsky 
1490*8a68d464SChris Wilson 	if (master_ctl & (GEN8_GT_VCS0_IRQ | GEN8_GT_VCS1_IRQ)) {
14912e4a5b25SChris Wilson 		gt_iir[1] = raw_reg_read(regs, GEN8_GT_IIR(1));
14922e4a5b25SChris Wilson 		if (likely(gt_iir[1]))
14932e4a5b25SChris Wilson 			raw_reg_write(regs, GEN8_GT_IIR(1), gt_iir[1]);
149474cdb337SChris Wilson 	}
149574cdb337SChris Wilson 
149626705e20SSagar Arun Kamble 	if (master_ctl & (GEN8_GT_PM_IRQ | GEN8_GT_GUC_IRQ)) {
14972e4a5b25SChris Wilson 		gt_iir[2] = raw_reg_read(regs, GEN8_GT_IIR(2));
1498f4de7794SChris Wilson 		if (likely(gt_iir[2]))
1499f4de7794SChris Wilson 			raw_reg_write(regs, GEN8_GT_IIR(2), gt_iir[2]);
15000961021aSBen Widawsky 	}
15012e4a5b25SChris Wilson 
15022e4a5b25SChris Wilson 	if (master_ctl & GEN8_GT_VECS_IRQ) {
15032e4a5b25SChris Wilson 		gt_iir[3] = raw_reg_read(regs, GEN8_GT_IIR(3));
15042e4a5b25SChris Wilson 		if (likely(gt_iir[3]))
15052e4a5b25SChris Wilson 			raw_reg_write(regs, GEN8_GT_IIR(3), gt_iir[3]);
150655ef72f2SChris Wilson 	}
1507abd58f01SBen Widawsky }
1508abd58f01SBen Widawsky 
15092e4a5b25SChris Wilson static void gen8_gt_irq_handler(struct drm_i915_private *i915,
1510f0fd96f5SChris Wilson 				u32 master_ctl, u32 gt_iir[4])
1511e30e251aSVille Syrjälä {
1512f0fd96f5SChris Wilson 	if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
1513*8a68d464SChris Wilson 		gen8_cs_irq_handler(i915->engine[RCS0],
151451f6b0f9SChris Wilson 				    gt_iir[0] >> GEN8_RCS_IRQ_SHIFT);
1515*8a68d464SChris Wilson 		gen8_cs_irq_handler(i915->engine[BCS0],
151651f6b0f9SChris Wilson 				    gt_iir[0] >> GEN8_BCS_IRQ_SHIFT);
1517e30e251aSVille Syrjälä 	}
1518e30e251aSVille Syrjälä 
1519*8a68d464SChris Wilson 	if (master_ctl & (GEN8_GT_VCS0_IRQ | GEN8_GT_VCS1_IRQ)) {
1520*8a68d464SChris Wilson 		gen8_cs_irq_handler(i915->engine[VCS0],
1521*8a68d464SChris Wilson 				    gt_iir[1] >> GEN8_VCS0_IRQ_SHIFT);
1522*8a68d464SChris Wilson 		gen8_cs_irq_handler(i915->engine[VCS1],
152351f6b0f9SChris Wilson 				    gt_iir[1] >> GEN8_VCS1_IRQ_SHIFT);
1524e30e251aSVille Syrjälä 	}
1525e30e251aSVille Syrjälä 
1526f0fd96f5SChris Wilson 	if (master_ctl & GEN8_GT_VECS_IRQ) {
1527*8a68d464SChris Wilson 		gen8_cs_irq_handler(i915->engine[VECS0],
152851f6b0f9SChris Wilson 				    gt_iir[3] >> GEN8_VECS_IRQ_SHIFT);
1529f0fd96f5SChris Wilson 	}
1530e30e251aSVille Syrjälä 
1531f0fd96f5SChris Wilson 	if (master_ctl & (GEN8_GT_PM_IRQ | GEN8_GT_GUC_IRQ)) {
15322e4a5b25SChris Wilson 		gen6_rps_irq_handler(i915, gt_iir[2]);
15332e4a5b25SChris Wilson 		gen9_guc_irq_handler(i915, gt_iir[2]);
1534e30e251aSVille Syrjälä 	}
1535f0fd96f5SChris Wilson }
1536e30e251aSVille Syrjälä 
1537af92058fSVille Syrjälä static bool gen11_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
1538121e758eSDhinakaran Pandiyan {
1539af92058fSVille Syrjälä 	switch (pin) {
1540af92058fSVille Syrjälä 	case HPD_PORT_C:
1541121e758eSDhinakaran Pandiyan 		return val & GEN11_HOTPLUG_CTL_LONG_DETECT(PORT_TC1);
1542af92058fSVille Syrjälä 	case HPD_PORT_D:
1543121e758eSDhinakaran Pandiyan 		return val & GEN11_HOTPLUG_CTL_LONG_DETECT(PORT_TC2);
1544af92058fSVille Syrjälä 	case HPD_PORT_E:
1545121e758eSDhinakaran Pandiyan 		return val & GEN11_HOTPLUG_CTL_LONG_DETECT(PORT_TC3);
1546af92058fSVille Syrjälä 	case HPD_PORT_F:
1547121e758eSDhinakaran Pandiyan 		return val & GEN11_HOTPLUG_CTL_LONG_DETECT(PORT_TC4);
1548121e758eSDhinakaran Pandiyan 	default:
1549121e758eSDhinakaran Pandiyan 		return false;
1550121e758eSDhinakaran Pandiyan 	}
1551121e758eSDhinakaran Pandiyan }
1552121e758eSDhinakaran Pandiyan 
1553af92058fSVille Syrjälä static bool bxt_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
155463c88d22SImre Deak {
1555af92058fSVille Syrjälä 	switch (pin) {
1556af92058fSVille Syrjälä 	case HPD_PORT_A:
1557195baa06SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
1558af92058fSVille Syrjälä 	case HPD_PORT_B:
155963c88d22SImre Deak 		return val & PORTB_HOTPLUG_LONG_DETECT;
1560af92058fSVille Syrjälä 	case HPD_PORT_C:
156163c88d22SImre Deak 		return val & PORTC_HOTPLUG_LONG_DETECT;
156263c88d22SImre Deak 	default:
156363c88d22SImre Deak 		return false;
156463c88d22SImre Deak 	}
156563c88d22SImre Deak }
156663c88d22SImre Deak 
1567af92058fSVille Syrjälä static bool icp_ddi_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
156831604222SAnusha Srivatsa {
1569af92058fSVille Syrjälä 	switch (pin) {
1570af92058fSVille Syrjälä 	case HPD_PORT_A:
157131604222SAnusha Srivatsa 		return val & ICP_DDIA_HPD_LONG_DETECT;
1572af92058fSVille Syrjälä 	case HPD_PORT_B:
157331604222SAnusha Srivatsa 		return val & ICP_DDIB_HPD_LONG_DETECT;
157431604222SAnusha Srivatsa 	default:
157531604222SAnusha Srivatsa 		return false;
157631604222SAnusha Srivatsa 	}
157731604222SAnusha Srivatsa }
157831604222SAnusha Srivatsa 
1579af92058fSVille Syrjälä static bool icp_tc_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
158031604222SAnusha Srivatsa {
1581af92058fSVille Syrjälä 	switch (pin) {
1582af92058fSVille Syrjälä 	case HPD_PORT_C:
158331604222SAnusha Srivatsa 		return val & ICP_TC_HPD_LONG_DETECT(PORT_TC1);
1584af92058fSVille Syrjälä 	case HPD_PORT_D:
158531604222SAnusha Srivatsa 		return val & ICP_TC_HPD_LONG_DETECT(PORT_TC2);
1586af92058fSVille Syrjälä 	case HPD_PORT_E:
158731604222SAnusha Srivatsa 		return val & ICP_TC_HPD_LONG_DETECT(PORT_TC3);
1588af92058fSVille Syrjälä 	case HPD_PORT_F:
158931604222SAnusha Srivatsa 		return val & ICP_TC_HPD_LONG_DETECT(PORT_TC4);
159031604222SAnusha Srivatsa 	default:
159131604222SAnusha Srivatsa 		return false;
159231604222SAnusha Srivatsa 	}
159331604222SAnusha Srivatsa }
159431604222SAnusha Srivatsa 
1595af92058fSVille Syrjälä static bool spt_port_hotplug2_long_detect(enum hpd_pin pin, u32 val)
15966dbf30ceSVille Syrjälä {
1597af92058fSVille Syrjälä 	switch (pin) {
1598af92058fSVille Syrjälä 	case HPD_PORT_E:
15996dbf30ceSVille Syrjälä 		return val & PORTE_HOTPLUG_LONG_DETECT;
16006dbf30ceSVille Syrjälä 	default:
16016dbf30ceSVille Syrjälä 		return false;
16026dbf30ceSVille Syrjälä 	}
16036dbf30ceSVille Syrjälä }
16046dbf30ceSVille Syrjälä 
1605af92058fSVille Syrjälä static bool spt_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
160674c0b395SVille Syrjälä {
1607af92058fSVille Syrjälä 	switch (pin) {
1608af92058fSVille Syrjälä 	case HPD_PORT_A:
160974c0b395SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
1610af92058fSVille Syrjälä 	case HPD_PORT_B:
161174c0b395SVille Syrjälä 		return val & PORTB_HOTPLUG_LONG_DETECT;
1612af92058fSVille Syrjälä 	case HPD_PORT_C:
161374c0b395SVille Syrjälä 		return val & PORTC_HOTPLUG_LONG_DETECT;
1614af92058fSVille Syrjälä 	case HPD_PORT_D:
161574c0b395SVille Syrjälä 		return val & PORTD_HOTPLUG_LONG_DETECT;
161674c0b395SVille Syrjälä 	default:
161774c0b395SVille Syrjälä 		return false;
161874c0b395SVille Syrjälä 	}
161974c0b395SVille Syrjälä }
162074c0b395SVille Syrjälä 
1621af92058fSVille Syrjälä static bool ilk_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
1622e4ce95aaSVille Syrjälä {
1623af92058fSVille Syrjälä 	switch (pin) {
1624af92058fSVille Syrjälä 	case HPD_PORT_A:
1625e4ce95aaSVille Syrjälä 		return val & DIGITAL_PORTA_HOTPLUG_LONG_DETECT;
1626e4ce95aaSVille Syrjälä 	default:
1627e4ce95aaSVille Syrjälä 		return false;
1628e4ce95aaSVille Syrjälä 	}
1629e4ce95aaSVille Syrjälä }
1630e4ce95aaSVille Syrjälä 
1631af92058fSVille Syrjälä static bool pch_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
163213cf5504SDave Airlie {
1633af92058fSVille Syrjälä 	switch (pin) {
1634af92058fSVille Syrjälä 	case HPD_PORT_B:
1635676574dfSJani Nikula 		return val & PORTB_HOTPLUG_LONG_DETECT;
1636af92058fSVille Syrjälä 	case HPD_PORT_C:
1637676574dfSJani Nikula 		return val & PORTC_HOTPLUG_LONG_DETECT;
1638af92058fSVille Syrjälä 	case HPD_PORT_D:
1639676574dfSJani Nikula 		return val & PORTD_HOTPLUG_LONG_DETECT;
1640676574dfSJani Nikula 	default:
1641676574dfSJani Nikula 		return false;
164213cf5504SDave Airlie 	}
164313cf5504SDave Airlie }
164413cf5504SDave Airlie 
1645af92058fSVille Syrjälä static bool i9xx_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
164613cf5504SDave Airlie {
1647af92058fSVille Syrjälä 	switch (pin) {
1648af92058fSVille Syrjälä 	case HPD_PORT_B:
1649676574dfSJani Nikula 		return val & PORTB_HOTPLUG_INT_LONG_PULSE;
1650af92058fSVille Syrjälä 	case HPD_PORT_C:
1651676574dfSJani Nikula 		return val & PORTC_HOTPLUG_INT_LONG_PULSE;
1652af92058fSVille Syrjälä 	case HPD_PORT_D:
1653676574dfSJani Nikula 		return val & PORTD_HOTPLUG_INT_LONG_PULSE;
1654676574dfSJani Nikula 	default:
1655676574dfSJani Nikula 		return false;
165613cf5504SDave Airlie 	}
165713cf5504SDave Airlie }
165813cf5504SDave Airlie 
165942db67d6SVille Syrjälä /*
166042db67d6SVille Syrjälä  * Get a bit mask of pins that have triggered, and which ones may be long.
166142db67d6SVille Syrjälä  * This can be called multiple times with the same masks to accumulate
166242db67d6SVille Syrjälä  * hotplug detection results from several registers.
166342db67d6SVille Syrjälä  *
166442db67d6SVille Syrjälä  * Note that the caller is expected to zero out the masks initially.
166542db67d6SVille Syrjälä  */
1666cf53902fSRodrigo Vivi static void intel_get_hpd_pins(struct drm_i915_private *dev_priv,
1667cf53902fSRodrigo Vivi 			       u32 *pin_mask, u32 *long_mask,
16688c841e57SJani Nikula 			       u32 hotplug_trigger, u32 dig_hotplug_reg,
1669fd63e2a9SImre Deak 			       const u32 hpd[HPD_NUM_PINS],
1670af92058fSVille Syrjälä 			       bool long_pulse_detect(enum hpd_pin pin, u32 val))
1671676574dfSJani Nikula {
1672e9be2850SVille Syrjälä 	enum hpd_pin pin;
1673676574dfSJani Nikula 
1674e9be2850SVille Syrjälä 	for_each_hpd_pin(pin) {
1675e9be2850SVille Syrjälä 		if ((hpd[pin] & hotplug_trigger) == 0)
16768c841e57SJani Nikula 			continue;
16778c841e57SJani Nikula 
1678e9be2850SVille Syrjälä 		*pin_mask |= BIT(pin);
1679676574dfSJani Nikula 
1680af92058fSVille Syrjälä 		if (long_pulse_detect(pin, dig_hotplug_reg))
1681e9be2850SVille Syrjälä 			*long_mask |= BIT(pin);
1682676574dfSJani Nikula 	}
1683676574dfSJani Nikula 
1684f88f0478SVille Syrjälä 	DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x, dig 0x%08x, pins 0x%08x, long 0x%08x\n",
1685f88f0478SVille Syrjälä 			 hotplug_trigger, dig_hotplug_reg, *pin_mask, *long_mask);
1686676574dfSJani Nikula 
1687676574dfSJani Nikula }
1688676574dfSJani Nikula 
168991d14251STvrtko Ursulin static void gmbus_irq_handler(struct drm_i915_private *dev_priv)
1690515ac2bbSDaniel Vetter {
169128c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1692515ac2bbSDaniel Vetter }
1693515ac2bbSDaniel Vetter 
169491d14251STvrtko Ursulin static void dp_aux_irq_handler(struct drm_i915_private *dev_priv)
1695ce99c256SDaniel Vetter {
16969ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1697ce99c256SDaniel Vetter }
1698ce99c256SDaniel Vetter 
16998bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
170091d14251STvrtko Ursulin static void display_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
170191d14251STvrtko Ursulin 					 enum pipe pipe,
1702a9c287c9SJani Nikula 					 u32 crc0, u32 crc1,
1703a9c287c9SJani Nikula 					 u32 crc2, u32 crc3,
1704a9c287c9SJani Nikula 					 u32 crc4)
17058bf1e9f1SShuang He {
17068bf1e9f1SShuang He 	struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
17078c6b709dSTomeu Vizoso 	struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
17085cee6c45SVille Syrjälä 	u32 crcs[5] = { crc0, crc1, crc2, crc3, crc4 };
17095cee6c45SVille Syrjälä 
17105cee6c45SVille Syrjälä 	trace_intel_pipe_crc(crtc, crcs);
1711b2c88f5bSDamien Lespiau 
1712d538bbdfSDamien Lespiau 	spin_lock(&pipe_crc->lock);
17138c6b709dSTomeu Vizoso 	/*
17148c6b709dSTomeu Vizoso 	 * For some not yet identified reason, the first CRC is
17158c6b709dSTomeu Vizoso 	 * bonkers. So let's just wait for the next vblank and read
17168c6b709dSTomeu Vizoso 	 * out the buggy result.
17178c6b709dSTomeu Vizoso 	 *
1718163e8aecSRodrigo Vivi 	 * On GEN8+ sometimes the second CRC is bonkers as well, so
17198c6b709dSTomeu Vizoso 	 * don't trust that one either.
17208c6b709dSTomeu Vizoso 	 */
1721033b7a23SMaarten Lankhorst 	if (pipe_crc->skipped <= 0 ||
1722163e8aecSRodrigo Vivi 	    (INTEL_GEN(dev_priv) >= 8 && pipe_crc->skipped == 1)) {
17238c6b709dSTomeu Vizoso 		pipe_crc->skipped++;
17248c6b709dSTomeu Vizoso 		spin_unlock(&pipe_crc->lock);
17258c6b709dSTomeu Vizoso 		return;
17268c6b709dSTomeu Vizoso 	}
17278c6b709dSTomeu Vizoso 	spin_unlock(&pipe_crc->lock);
17286cc42152SMaarten Lankhorst 
1729246ee524STomeu Vizoso 	drm_crtc_add_crc_entry(&crtc->base, true,
1730ca814b25SDaniel Vetter 				drm_crtc_accurate_vblank_count(&crtc->base),
1731246ee524STomeu Vizoso 				crcs);
17328c6b709dSTomeu Vizoso }
1733277de95eSDaniel Vetter #else
1734277de95eSDaniel Vetter static inline void
173591d14251STvrtko Ursulin display_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
173691d14251STvrtko Ursulin 			     enum pipe pipe,
1737a9c287c9SJani Nikula 			     u32 crc0, u32 crc1,
1738a9c287c9SJani Nikula 			     u32 crc2, u32 crc3,
1739a9c287c9SJani Nikula 			     u32 crc4) {}
1740277de95eSDaniel Vetter #endif
1741eba94eb9SDaniel Vetter 
1742277de95eSDaniel Vetter 
174391d14251STvrtko Ursulin static void hsw_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
174491d14251STvrtko Ursulin 				     enum pipe pipe)
17455a69b89fSDaniel Vetter {
174691d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
17475a69b89fSDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
17485a69b89fSDaniel Vetter 				     0, 0, 0, 0);
17495a69b89fSDaniel Vetter }
17505a69b89fSDaniel Vetter 
175191d14251STvrtko Ursulin static void ivb_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
175291d14251STvrtko Ursulin 				     enum pipe pipe)
1753eba94eb9SDaniel Vetter {
175491d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
1755eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1756eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1757eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1758eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
17598bc5e955SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1760eba94eb9SDaniel Vetter }
17615b3a856bSDaniel Vetter 
176291d14251STvrtko Ursulin static void i9xx_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
176391d14251STvrtko Ursulin 				      enum pipe pipe)
17645b3a856bSDaniel Vetter {
1765a9c287c9SJani Nikula 	u32 res1, res2;
17660b5c5ed0SDaniel Vetter 
176791d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 3)
17680b5c5ed0SDaniel Vetter 		res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
17690b5c5ed0SDaniel Vetter 	else
17700b5c5ed0SDaniel Vetter 		res1 = 0;
17710b5c5ed0SDaniel Vetter 
177291d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
17730b5c5ed0SDaniel Vetter 		res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
17740b5c5ed0SDaniel Vetter 	else
17750b5c5ed0SDaniel Vetter 		res2 = 0;
17765b3a856bSDaniel Vetter 
177791d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
17780b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_RED(pipe)),
17790b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_GREEN(pipe)),
17800b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_BLUE(pipe)),
17810b5c5ed0SDaniel Vetter 				     res1, res2);
17825b3a856bSDaniel Vetter }
17838bf1e9f1SShuang He 
17841403c0d4SPaulo Zanoni /* The RPS events need forcewake, so we add them to a work queue and mask their
17851403c0d4SPaulo Zanoni  * IMR bits until the work is done. Other interrupts can be processed without
17861403c0d4SPaulo Zanoni  * the work queue. */
17871403c0d4SPaulo Zanoni static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1788baf02a1fSBen Widawsky {
1789562d9baeSSagar Arun Kamble 	struct intel_rps *rps = &dev_priv->gt_pm.rps;
1790562d9baeSSagar Arun Kamble 
1791a6706b45SDeepak S 	if (pm_iir & dev_priv->pm_rps_events) {
179259cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
1793f4e9af4fSAkash Goel 		gen6_mask_pm_irq(dev_priv, pm_iir & dev_priv->pm_rps_events);
1794562d9baeSSagar Arun Kamble 		if (rps->interrupts_enabled) {
1795562d9baeSSagar Arun Kamble 			rps->pm_iir |= pm_iir & dev_priv->pm_rps_events;
1796562d9baeSSagar Arun Kamble 			schedule_work(&rps->work);
179741a05a3aSDaniel Vetter 		}
1798d4d70aa5SImre Deak 		spin_unlock(&dev_priv->irq_lock);
1799d4d70aa5SImre Deak 	}
1800baf02a1fSBen Widawsky 
1801bca2bf2aSPandiyan, Dhinakaran 	if (INTEL_GEN(dev_priv) >= 8)
1802c9a9a268SImre Deak 		return;
1803c9a9a268SImre Deak 
180412638c57SBen Widawsky 	if (pm_iir & PM_VEBOX_USER_INTERRUPT)
1805*8a68d464SChris Wilson 		intel_engine_breadcrumbs_irq(dev_priv->engine[VECS0]);
180612638c57SBen Widawsky 
1807aaecdf61SDaniel Vetter 	if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT)
1808aaecdf61SDaniel Vetter 		DRM_DEBUG("Command parser error, pm_iir 0x%08x\n", pm_iir);
180912638c57SBen Widawsky }
1810baf02a1fSBen Widawsky 
181126705e20SSagar Arun Kamble static void gen9_guc_irq_handler(struct drm_i915_private *dev_priv, u32 gt_iir)
181226705e20SSagar Arun Kamble {
181393bf8096SMichal Wajdeczko 	if (gt_iir & GEN9_GUC_TO_HOST_INT_EVENT)
181493bf8096SMichal Wajdeczko 		intel_guc_to_host_event_handler(&dev_priv->guc);
181526705e20SSagar Arun Kamble }
181626705e20SSagar Arun Kamble 
181744d9241eSVille Syrjälä static void i9xx_pipestat_irq_reset(struct drm_i915_private *dev_priv)
181844d9241eSVille Syrjälä {
181944d9241eSVille Syrjälä 	enum pipe pipe;
182044d9241eSVille Syrjälä 
182144d9241eSVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
182244d9241eSVille Syrjälä 		I915_WRITE(PIPESTAT(pipe),
182344d9241eSVille Syrjälä 			   PIPESTAT_INT_STATUS_MASK |
182444d9241eSVille Syrjälä 			   PIPE_FIFO_UNDERRUN_STATUS);
182544d9241eSVille Syrjälä 
182644d9241eSVille Syrjälä 		dev_priv->pipestat_irq_mask[pipe] = 0;
182744d9241eSVille Syrjälä 	}
182844d9241eSVille Syrjälä }
182944d9241eSVille Syrjälä 
1830eb64343cSVille Syrjälä static void i9xx_pipestat_irq_ack(struct drm_i915_private *dev_priv,
183191d14251STvrtko Ursulin 				  u32 iir, u32 pipe_stats[I915_MAX_PIPES])
18327e231dbeSJesse Barnes {
18337e231dbeSJesse Barnes 	int pipe;
18347e231dbeSJesse Barnes 
183558ead0d7SImre Deak 	spin_lock(&dev_priv->irq_lock);
18361ca993d2SVille Syrjälä 
18371ca993d2SVille Syrjälä 	if (!dev_priv->display_irqs_enabled) {
18381ca993d2SVille Syrjälä 		spin_unlock(&dev_priv->irq_lock);
18391ca993d2SVille Syrjälä 		return;
18401ca993d2SVille Syrjälä 	}
18411ca993d2SVille Syrjälä 
1842055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1843f0f59a00SVille Syrjälä 		i915_reg_t reg;
18446b12ca56SVille Syrjälä 		u32 status_mask, enable_mask, iir_bit = 0;
184591d181ddSImre Deak 
1846bbb5eebfSDaniel Vetter 		/*
1847bbb5eebfSDaniel Vetter 		 * PIPESTAT bits get signalled even when the interrupt is
1848bbb5eebfSDaniel Vetter 		 * disabled with the mask bits, and some of the status bits do
1849bbb5eebfSDaniel Vetter 		 * not generate interrupts at all (like the underrun bit). Hence
1850bbb5eebfSDaniel Vetter 		 * we need to be careful that we only handle what we want to
1851bbb5eebfSDaniel Vetter 		 * handle.
1852bbb5eebfSDaniel Vetter 		 */
18530f239f4cSDaniel Vetter 
18540f239f4cSDaniel Vetter 		/* fifo underruns are filterered in the underrun handler. */
18556b12ca56SVille Syrjälä 		status_mask = PIPE_FIFO_UNDERRUN_STATUS;
1856bbb5eebfSDaniel Vetter 
1857bbb5eebfSDaniel Vetter 		switch (pipe) {
1858bbb5eebfSDaniel Vetter 		case PIPE_A:
1859bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
1860bbb5eebfSDaniel Vetter 			break;
1861bbb5eebfSDaniel Vetter 		case PIPE_B:
1862bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
1863bbb5eebfSDaniel Vetter 			break;
18643278f67fSVille Syrjälä 		case PIPE_C:
18653278f67fSVille Syrjälä 			iir_bit = I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
18663278f67fSVille Syrjälä 			break;
1867bbb5eebfSDaniel Vetter 		}
1868bbb5eebfSDaniel Vetter 		if (iir & iir_bit)
18696b12ca56SVille Syrjälä 			status_mask |= dev_priv->pipestat_irq_mask[pipe];
1870bbb5eebfSDaniel Vetter 
18716b12ca56SVille Syrjälä 		if (!status_mask)
187291d181ddSImre Deak 			continue;
187391d181ddSImre Deak 
187491d181ddSImre Deak 		reg = PIPESTAT(pipe);
18756b12ca56SVille Syrjälä 		pipe_stats[pipe] = I915_READ(reg) & status_mask;
18766b12ca56SVille Syrjälä 		enable_mask = i915_pipestat_enable_mask(dev_priv, pipe);
18777e231dbeSJesse Barnes 
18787e231dbeSJesse Barnes 		/*
18797e231dbeSJesse Barnes 		 * Clear the PIPE*STAT regs before the IIR
1880132c27c9SVille Syrjälä 		 *
1881132c27c9SVille Syrjälä 		 * Toggle the enable bits to make sure we get an
1882132c27c9SVille Syrjälä 		 * edge in the ISR pipe event bit if we don't clear
1883132c27c9SVille Syrjälä 		 * all the enabled status bits. Otherwise the edge
1884132c27c9SVille Syrjälä 		 * triggered IIR on i965/g4x wouldn't notice that
1885132c27c9SVille Syrjälä 		 * an interrupt is still pending.
18867e231dbeSJesse Barnes 		 */
1887132c27c9SVille Syrjälä 		if (pipe_stats[pipe]) {
1888132c27c9SVille Syrjälä 			I915_WRITE(reg, pipe_stats[pipe]);
1889132c27c9SVille Syrjälä 			I915_WRITE(reg, enable_mask);
1890132c27c9SVille Syrjälä 		}
18917e231dbeSJesse Barnes 	}
189258ead0d7SImre Deak 	spin_unlock(&dev_priv->irq_lock);
18932ecb8ca4SVille Syrjälä }
18942ecb8ca4SVille Syrjälä 
1895eb64343cSVille Syrjälä static void i8xx_pipestat_irq_handler(struct drm_i915_private *dev_priv,
1896eb64343cSVille Syrjälä 				      u16 iir, u32 pipe_stats[I915_MAX_PIPES])
1897eb64343cSVille Syrjälä {
1898eb64343cSVille Syrjälä 	enum pipe pipe;
1899eb64343cSVille Syrjälä 
1900eb64343cSVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
1901eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
1902eb64343cSVille Syrjälä 			drm_handle_vblank(&dev_priv->drm, pipe);
1903eb64343cSVille Syrjälä 
1904eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1905eb64343cSVille Syrjälä 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
1906eb64343cSVille Syrjälä 
1907eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
1908eb64343cSVille Syrjälä 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
1909eb64343cSVille Syrjälä 	}
1910eb64343cSVille Syrjälä }
1911eb64343cSVille Syrjälä 
1912eb64343cSVille Syrjälä static void i915_pipestat_irq_handler(struct drm_i915_private *dev_priv,
1913eb64343cSVille Syrjälä 				      u32 iir, u32 pipe_stats[I915_MAX_PIPES])
1914eb64343cSVille Syrjälä {
1915eb64343cSVille Syrjälä 	bool blc_event = false;
1916eb64343cSVille Syrjälä 	enum pipe pipe;
1917eb64343cSVille Syrjälä 
1918eb64343cSVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
1919eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
1920eb64343cSVille Syrjälä 			drm_handle_vblank(&dev_priv->drm, pipe);
1921eb64343cSVille Syrjälä 
1922eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
1923eb64343cSVille Syrjälä 			blc_event = true;
1924eb64343cSVille Syrjälä 
1925eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1926eb64343cSVille Syrjälä 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
1927eb64343cSVille Syrjälä 
1928eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
1929eb64343cSVille Syrjälä 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
1930eb64343cSVille Syrjälä 	}
1931eb64343cSVille Syrjälä 
1932eb64343cSVille Syrjälä 	if (blc_event || (iir & I915_ASLE_INTERRUPT))
1933eb64343cSVille Syrjälä 		intel_opregion_asle_intr(dev_priv);
1934eb64343cSVille Syrjälä }
1935eb64343cSVille Syrjälä 
1936eb64343cSVille Syrjälä static void i965_pipestat_irq_handler(struct drm_i915_private *dev_priv,
1937eb64343cSVille Syrjälä 				      u32 iir, u32 pipe_stats[I915_MAX_PIPES])
1938eb64343cSVille Syrjälä {
1939eb64343cSVille Syrjälä 	bool blc_event = false;
1940eb64343cSVille Syrjälä 	enum pipe pipe;
1941eb64343cSVille Syrjälä 
1942eb64343cSVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
1943eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS)
1944eb64343cSVille Syrjälä 			drm_handle_vblank(&dev_priv->drm, pipe);
1945eb64343cSVille Syrjälä 
1946eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
1947eb64343cSVille Syrjälä 			blc_event = true;
1948eb64343cSVille Syrjälä 
1949eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1950eb64343cSVille Syrjälä 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
1951eb64343cSVille Syrjälä 
1952eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
1953eb64343cSVille Syrjälä 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
1954eb64343cSVille Syrjälä 	}
1955eb64343cSVille Syrjälä 
1956eb64343cSVille Syrjälä 	if (blc_event || (iir & I915_ASLE_INTERRUPT))
1957eb64343cSVille Syrjälä 		intel_opregion_asle_intr(dev_priv);
1958eb64343cSVille Syrjälä 
1959eb64343cSVille Syrjälä 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1960eb64343cSVille Syrjälä 		gmbus_irq_handler(dev_priv);
1961eb64343cSVille Syrjälä }
1962eb64343cSVille Syrjälä 
196391d14251STvrtko Ursulin static void valleyview_pipestat_irq_handler(struct drm_i915_private *dev_priv,
19642ecb8ca4SVille Syrjälä 					    u32 pipe_stats[I915_MAX_PIPES])
19652ecb8ca4SVille Syrjälä {
19662ecb8ca4SVille Syrjälä 	enum pipe pipe;
19677e231dbeSJesse Barnes 
1968055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1969fd3a4024SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS)
1970fd3a4024SDaniel Vetter 			drm_handle_vblank(&dev_priv->drm, pipe);
19714356d586SDaniel Vetter 
19724356d586SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
197391d14251STvrtko Ursulin 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
19742d9d2b0bSVille Syrjälä 
19751f7247c0SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
19761f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
197731acc7f5SJesse Barnes 	}
197831acc7f5SJesse Barnes 
1979c1874ed7SImre Deak 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
198091d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
1981c1874ed7SImre Deak }
1982c1874ed7SImre Deak 
19831ae3c34cSVille Syrjälä static u32 i9xx_hpd_irq_ack(struct drm_i915_private *dev_priv)
198416c6c56bSVille Syrjälä {
19850ba7c51aSVille Syrjälä 	u32 hotplug_status = 0, hotplug_status_mask;
19860ba7c51aSVille Syrjälä 	int i;
198716c6c56bSVille Syrjälä 
19880ba7c51aSVille Syrjälä 	if (IS_G4X(dev_priv) ||
19890ba7c51aSVille Syrjälä 	    IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
19900ba7c51aSVille Syrjälä 		hotplug_status_mask = HOTPLUG_INT_STATUS_G4X |
19910ba7c51aSVille Syrjälä 			DP_AUX_CHANNEL_MASK_INT_STATUS_G4X;
19920ba7c51aSVille Syrjälä 	else
19930ba7c51aSVille Syrjälä 		hotplug_status_mask = HOTPLUG_INT_STATUS_I915;
19940ba7c51aSVille Syrjälä 
19950ba7c51aSVille Syrjälä 	/*
19960ba7c51aSVille Syrjälä 	 * We absolutely have to clear all the pending interrupt
19970ba7c51aSVille Syrjälä 	 * bits in PORT_HOTPLUG_STAT. Otherwise the ISR port
19980ba7c51aSVille Syrjälä 	 * interrupt bit won't have an edge, and the i965/g4x
19990ba7c51aSVille Syrjälä 	 * edge triggered IIR will not notice that an interrupt
20000ba7c51aSVille Syrjälä 	 * is still pending. We can't use PORT_HOTPLUG_EN to
20010ba7c51aSVille Syrjälä 	 * guarantee the edge as the act of toggling the enable
20020ba7c51aSVille Syrjälä 	 * bits can itself generate a new hotplug interrupt :(
20030ba7c51aSVille Syrjälä 	 */
20040ba7c51aSVille Syrjälä 	for (i = 0; i < 10; i++) {
20050ba7c51aSVille Syrjälä 		u32 tmp = I915_READ(PORT_HOTPLUG_STAT) & hotplug_status_mask;
20060ba7c51aSVille Syrjälä 
20070ba7c51aSVille Syrjälä 		if (tmp == 0)
20080ba7c51aSVille Syrjälä 			return hotplug_status;
20090ba7c51aSVille Syrjälä 
20100ba7c51aSVille Syrjälä 		hotplug_status |= tmp;
20113ff60f89SOscar Mateo 		I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
20120ba7c51aSVille Syrjälä 	}
20130ba7c51aSVille Syrjälä 
20140ba7c51aSVille Syrjälä 	WARN_ONCE(1,
20150ba7c51aSVille Syrjälä 		  "PORT_HOTPLUG_STAT did not clear (0x%08x)\n",
20160ba7c51aSVille Syrjälä 		  I915_READ(PORT_HOTPLUG_STAT));
20171ae3c34cSVille Syrjälä 
20181ae3c34cSVille Syrjälä 	return hotplug_status;
20191ae3c34cSVille Syrjälä }
20201ae3c34cSVille Syrjälä 
202191d14251STvrtko Ursulin static void i9xx_hpd_irq_handler(struct drm_i915_private *dev_priv,
20221ae3c34cSVille Syrjälä 				 u32 hotplug_status)
20231ae3c34cSVille Syrjälä {
20241ae3c34cSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
20253ff60f89SOscar Mateo 
202691d14251STvrtko Ursulin 	if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
202791d14251STvrtko Ursulin 	    IS_CHERRYVIEW(dev_priv)) {
202816c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
202916c6c56bSVille Syrjälä 
203058f2cf24SVille Syrjälä 		if (hotplug_trigger) {
2031cf53902fSRodrigo Vivi 			intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
2032cf53902fSRodrigo Vivi 					   hotplug_trigger, hotplug_trigger,
2033cf53902fSRodrigo Vivi 					   hpd_status_g4x,
2034fd63e2a9SImre Deak 					   i9xx_port_hotplug_long_detect);
203558f2cf24SVille Syrjälä 
203691d14251STvrtko Ursulin 			intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
203758f2cf24SVille Syrjälä 		}
2038369712e8SJani Nikula 
2039369712e8SJani Nikula 		if (hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
204091d14251STvrtko Ursulin 			dp_aux_irq_handler(dev_priv);
204116c6c56bSVille Syrjälä 	} else {
204216c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
204316c6c56bSVille Syrjälä 
204458f2cf24SVille Syrjälä 		if (hotplug_trigger) {
2045cf53902fSRodrigo Vivi 			intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
2046cf53902fSRodrigo Vivi 					   hotplug_trigger, hotplug_trigger,
2047cf53902fSRodrigo Vivi 					   hpd_status_i915,
2048fd63e2a9SImre Deak 					   i9xx_port_hotplug_long_detect);
204991d14251STvrtko Ursulin 			intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
205016c6c56bSVille Syrjälä 		}
20513ff60f89SOscar Mateo 	}
205258f2cf24SVille Syrjälä }
205316c6c56bSVille Syrjälä 
2054c1874ed7SImre Deak static irqreturn_t valleyview_irq_handler(int irq, void *arg)
2055c1874ed7SImre Deak {
205645a83f84SDaniel Vetter 	struct drm_device *dev = arg;
2057fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2058c1874ed7SImre Deak 	irqreturn_t ret = IRQ_NONE;
2059c1874ed7SImre Deak 
20602dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
20612dd2a883SImre Deak 		return IRQ_NONE;
20622dd2a883SImre Deak 
20631f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
20641f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
20651f814dacSImre Deak 
20661e1cace9SVille Syrjälä 	do {
20676e814800SVille Syrjälä 		u32 iir, gt_iir, pm_iir;
20682ecb8ca4SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
20691ae3c34cSVille Syrjälä 		u32 hotplug_status = 0;
2070a5e485a9SVille Syrjälä 		u32 ier = 0;
20713ff60f89SOscar Mateo 
2072c1874ed7SImre Deak 		gt_iir = I915_READ(GTIIR);
2073c1874ed7SImre Deak 		pm_iir = I915_READ(GEN6_PMIIR);
20743ff60f89SOscar Mateo 		iir = I915_READ(VLV_IIR);
2075c1874ed7SImre Deak 
2076c1874ed7SImre Deak 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
20771e1cace9SVille Syrjälä 			break;
2078c1874ed7SImre Deak 
2079c1874ed7SImre Deak 		ret = IRQ_HANDLED;
2080c1874ed7SImre Deak 
2081a5e485a9SVille Syrjälä 		/*
2082a5e485a9SVille Syrjälä 		 * Theory on interrupt generation, based on empirical evidence:
2083a5e485a9SVille Syrjälä 		 *
2084a5e485a9SVille Syrjälä 		 * x = ((VLV_IIR & VLV_IER) ||
2085a5e485a9SVille Syrjälä 		 *      (((GT_IIR & GT_IER) || (GEN6_PMIIR & GEN6_PMIER)) &&
2086a5e485a9SVille Syrjälä 		 *       (VLV_MASTER_IER & MASTER_INTERRUPT_ENABLE)));
2087a5e485a9SVille Syrjälä 		 *
2088a5e485a9SVille Syrjälä 		 * A CPU interrupt will only be raised when 'x' has a 0->1 edge.
2089a5e485a9SVille Syrjälä 		 * Hence we clear MASTER_INTERRUPT_ENABLE and VLV_IER to
2090a5e485a9SVille Syrjälä 		 * guarantee the CPU interrupt will be raised again even if we
2091a5e485a9SVille Syrjälä 		 * don't end up clearing all the VLV_IIR, GT_IIR, GEN6_PMIIR
2092a5e485a9SVille Syrjälä 		 * bits this time around.
2093a5e485a9SVille Syrjälä 		 */
20944a0a0202SVille Syrjälä 		I915_WRITE(VLV_MASTER_IER, 0);
2095a5e485a9SVille Syrjälä 		ier = I915_READ(VLV_IER);
2096a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, 0);
20974a0a0202SVille Syrjälä 
20984a0a0202SVille Syrjälä 		if (gt_iir)
20994a0a0202SVille Syrjälä 			I915_WRITE(GTIIR, gt_iir);
21004a0a0202SVille Syrjälä 		if (pm_iir)
21014a0a0202SVille Syrjälä 			I915_WRITE(GEN6_PMIIR, pm_iir);
21024a0a0202SVille Syrjälä 
21037ce4d1f2SVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
21041ae3c34cSVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
21057ce4d1f2SVille Syrjälä 
21063ff60f89SOscar Mateo 		/* Call regardless, as some status bits might not be
21073ff60f89SOscar Mateo 		 * signalled in iir */
2108eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
21097ce4d1f2SVille Syrjälä 
2110eef57324SJerome Anand 		if (iir & (I915_LPE_PIPE_A_INTERRUPT |
2111eef57324SJerome Anand 			   I915_LPE_PIPE_B_INTERRUPT))
2112eef57324SJerome Anand 			intel_lpe_audio_irq_handler(dev_priv);
2113eef57324SJerome Anand 
21147ce4d1f2SVille Syrjälä 		/*
21157ce4d1f2SVille Syrjälä 		 * VLV_IIR is single buffered, and reflects the level
21167ce4d1f2SVille Syrjälä 		 * from PIPESTAT/PORT_HOTPLUG_STAT, hence clear it last.
21177ce4d1f2SVille Syrjälä 		 */
21187ce4d1f2SVille Syrjälä 		if (iir)
21197ce4d1f2SVille Syrjälä 			I915_WRITE(VLV_IIR, iir);
21204a0a0202SVille Syrjälä 
2121a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, ier);
21224a0a0202SVille Syrjälä 		I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
21231ae3c34cSVille Syrjälä 
212452894874SVille Syrjälä 		if (gt_iir)
2125261e40b8SVille Syrjälä 			snb_gt_irq_handler(dev_priv, gt_iir);
212652894874SVille Syrjälä 		if (pm_iir)
212752894874SVille Syrjälä 			gen6_rps_irq_handler(dev_priv, pm_iir);
212852894874SVille Syrjälä 
21291ae3c34cSVille Syrjälä 		if (hotplug_status)
213091d14251STvrtko Ursulin 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
21312ecb8ca4SVille Syrjälä 
213291d14251STvrtko Ursulin 		valleyview_pipestat_irq_handler(dev_priv, pipe_stats);
21331e1cace9SVille Syrjälä 	} while (0);
21347e231dbeSJesse Barnes 
21351f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
21361f814dacSImre Deak 
21377e231dbeSJesse Barnes 	return ret;
21387e231dbeSJesse Barnes }
21397e231dbeSJesse Barnes 
214043f328d7SVille Syrjälä static irqreturn_t cherryview_irq_handler(int irq, void *arg)
214143f328d7SVille Syrjälä {
214245a83f84SDaniel Vetter 	struct drm_device *dev = arg;
2143fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
214443f328d7SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
214543f328d7SVille Syrjälä 
21462dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
21472dd2a883SImre Deak 		return IRQ_NONE;
21482dd2a883SImre Deak 
21491f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
21501f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
21511f814dacSImre Deak 
2152579de73bSChris Wilson 	do {
21536e814800SVille Syrjälä 		u32 master_ctl, iir;
21542ecb8ca4SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
21551ae3c34cSVille Syrjälä 		u32 hotplug_status = 0;
2156f0fd96f5SChris Wilson 		u32 gt_iir[4];
2157a5e485a9SVille Syrjälä 		u32 ier = 0;
2158a5e485a9SVille Syrjälä 
21598e5fd599SVille Syrjälä 		master_ctl = I915_READ(GEN8_MASTER_IRQ) & ~GEN8_MASTER_IRQ_CONTROL;
21603278f67fSVille Syrjälä 		iir = I915_READ(VLV_IIR);
21613278f67fSVille Syrjälä 
21623278f67fSVille Syrjälä 		if (master_ctl == 0 && iir == 0)
21638e5fd599SVille Syrjälä 			break;
216443f328d7SVille Syrjälä 
216527b6c122SOscar Mateo 		ret = IRQ_HANDLED;
216627b6c122SOscar Mateo 
2167a5e485a9SVille Syrjälä 		/*
2168a5e485a9SVille Syrjälä 		 * Theory on interrupt generation, based on empirical evidence:
2169a5e485a9SVille Syrjälä 		 *
2170a5e485a9SVille Syrjälä 		 * x = ((VLV_IIR & VLV_IER) ||
2171a5e485a9SVille Syrjälä 		 *      ((GEN8_MASTER_IRQ & ~GEN8_MASTER_IRQ_CONTROL) &&
2172a5e485a9SVille Syrjälä 		 *       (GEN8_MASTER_IRQ & GEN8_MASTER_IRQ_CONTROL)));
2173a5e485a9SVille Syrjälä 		 *
2174a5e485a9SVille Syrjälä 		 * A CPU interrupt will only be raised when 'x' has a 0->1 edge.
2175a5e485a9SVille Syrjälä 		 * Hence we clear GEN8_MASTER_IRQ_CONTROL and VLV_IER to
2176a5e485a9SVille Syrjälä 		 * guarantee the CPU interrupt will be raised again even if we
2177a5e485a9SVille Syrjälä 		 * don't end up clearing all the VLV_IIR and GEN8_MASTER_IRQ_CONTROL
2178a5e485a9SVille Syrjälä 		 * bits this time around.
2179a5e485a9SVille Syrjälä 		 */
218043f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, 0);
2181a5e485a9SVille Syrjälä 		ier = I915_READ(VLV_IER);
2182a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, 0);
218343f328d7SVille Syrjälä 
2184e30e251aSVille Syrjälä 		gen8_gt_irq_ack(dev_priv, master_ctl, gt_iir);
218527b6c122SOscar Mateo 
218627b6c122SOscar Mateo 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
21871ae3c34cSVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
218843f328d7SVille Syrjälä 
218927b6c122SOscar Mateo 		/* Call regardless, as some status bits might not be
219027b6c122SOscar Mateo 		 * signalled in iir */
2191eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
219243f328d7SVille Syrjälä 
2193eef57324SJerome Anand 		if (iir & (I915_LPE_PIPE_A_INTERRUPT |
2194eef57324SJerome Anand 			   I915_LPE_PIPE_B_INTERRUPT |
2195eef57324SJerome Anand 			   I915_LPE_PIPE_C_INTERRUPT))
2196eef57324SJerome Anand 			intel_lpe_audio_irq_handler(dev_priv);
2197eef57324SJerome Anand 
21987ce4d1f2SVille Syrjälä 		/*
21997ce4d1f2SVille Syrjälä 		 * VLV_IIR is single buffered, and reflects the level
22007ce4d1f2SVille Syrjälä 		 * from PIPESTAT/PORT_HOTPLUG_STAT, hence clear it last.
22017ce4d1f2SVille Syrjälä 		 */
22027ce4d1f2SVille Syrjälä 		if (iir)
22037ce4d1f2SVille Syrjälä 			I915_WRITE(VLV_IIR, iir);
22047ce4d1f2SVille Syrjälä 
2205a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, ier);
2206e5328c43SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
22071ae3c34cSVille Syrjälä 
2208f0fd96f5SChris Wilson 		gen8_gt_irq_handler(dev_priv, master_ctl, gt_iir);
2209e30e251aSVille Syrjälä 
22101ae3c34cSVille Syrjälä 		if (hotplug_status)
221191d14251STvrtko Ursulin 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
22122ecb8ca4SVille Syrjälä 
221391d14251STvrtko Ursulin 		valleyview_pipestat_irq_handler(dev_priv, pipe_stats);
2214579de73bSChris Wilson 	} while (0);
22153278f67fSVille Syrjälä 
22161f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
22171f814dacSImre Deak 
221843f328d7SVille Syrjälä 	return ret;
221943f328d7SVille Syrjälä }
222043f328d7SVille Syrjälä 
222191d14251STvrtko Ursulin static void ibx_hpd_irq_handler(struct drm_i915_private *dev_priv,
222291d14251STvrtko Ursulin 				u32 hotplug_trigger,
222340e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2224776ad806SJesse Barnes {
222542db67d6SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2226776ad806SJesse Barnes 
22276a39d7c9SJani Nikula 	/*
22286a39d7c9SJani Nikula 	 * Somehow the PCH doesn't seem to really ack the interrupt to the CPU
22296a39d7c9SJani Nikula 	 * unless we touch the hotplug register, even if hotplug_trigger is
22306a39d7c9SJani Nikula 	 * zero. Not acking leads to "The master control interrupt lied (SDE)!"
22316a39d7c9SJani Nikula 	 * errors.
22326a39d7c9SJani Nikula 	 */
223313cf5504SDave Airlie 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
22346a39d7c9SJani Nikula 	if (!hotplug_trigger) {
22356a39d7c9SJani Nikula 		u32 mask = PORTA_HOTPLUG_STATUS_MASK |
22366a39d7c9SJani Nikula 			PORTD_HOTPLUG_STATUS_MASK |
22376a39d7c9SJani Nikula 			PORTC_HOTPLUG_STATUS_MASK |
22386a39d7c9SJani Nikula 			PORTB_HOTPLUG_STATUS_MASK;
22396a39d7c9SJani Nikula 		dig_hotplug_reg &= ~mask;
22406a39d7c9SJani Nikula 	}
22416a39d7c9SJani Nikula 
224213cf5504SDave Airlie 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
22436a39d7c9SJani Nikula 	if (!hotplug_trigger)
22446a39d7c9SJani Nikula 		return;
224513cf5504SDave Airlie 
2246cf53902fSRodrigo Vivi 	intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask, hotplug_trigger,
224740e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2248fd63e2a9SImre Deak 			   pch_port_hotplug_long_detect);
224940e56410SVille Syrjälä 
225091d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2251aaf5ec2eSSonika Jindal }
225291d131d2SDaniel Vetter 
225391d14251STvrtko Ursulin static void ibx_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
225440e56410SVille Syrjälä {
225540e56410SVille Syrjälä 	int pipe;
225640e56410SVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
225740e56410SVille Syrjälä 
225891d14251STvrtko Ursulin 	ibx_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ibx);
225940e56410SVille Syrjälä 
2260cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
2261cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
2262776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
2263cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
2264cfc33bf7SVille Syrjälä 				 port_name(port));
2265cfc33bf7SVille Syrjälä 	}
2266776ad806SJesse Barnes 
2267ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
226891d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
2269ce99c256SDaniel Vetter 
2270776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
227191d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
2272776ad806SJesse Barnes 
2273776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
2274776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
2275776ad806SJesse Barnes 
2276776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
2277776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
2278776ad806SJesse Barnes 
2279776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
2280776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
2281776ad806SJesse Barnes 
22829db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
2283055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
22849db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
22859db4a9c7SJesse Barnes 					 pipe_name(pipe),
22869db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
2287776ad806SJesse Barnes 
2288776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
2289776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
2290776ad806SJesse Barnes 
2291776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
2292776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
2293776ad806SJesse Barnes 
2294776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
2295a2196033SMatthias Kaehlcke 		intel_pch_fifo_underrun_irq_handler(dev_priv, PIPE_A);
22968664281bSPaulo Zanoni 
22978664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
2298a2196033SMatthias Kaehlcke 		intel_pch_fifo_underrun_irq_handler(dev_priv, PIPE_B);
22998664281bSPaulo Zanoni }
23008664281bSPaulo Zanoni 
230191d14251STvrtko Ursulin static void ivb_err_int_handler(struct drm_i915_private *dev_priv)
23028664281bSPaulo Zanoni {
23038664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
23045a69b89fSDaniel Vetter 	enum pipe pipe;
23058664281bSPaulo Zanoni 
2306de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
2307de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
2308de032bf4SPaulo Zanoni 
2309055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
23101f7247c0SDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe))
23111f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
23128664281bSPaulo Zanoni 
23135a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
231491d14251STvrtko Ursulin 			if (IS_IVYBRIDGE(dev_priv))
231591d14251STvrtko Ursulin 				ivb_pipe_crc_irq_handler(dev_priv, pipe);
23165a69b89fSDaniel Vetter 			else
231791d14251STvrtko Ursulin 				hsw_pipe_crc_irq_handler(dev_priv, pipe);
23185a69b89fSDaniel Vetter 		}
23195a69b89fSDaniel Vetter 	}
23208bf1e9f1SShuang He 
23218664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
23228664281bSPaulo Zanoni }
23238664281bSPaulo Zanoni 
232491d14251STvrtko Ursulin static void cpt_serr_int_handler(struct drm_i915_private *dev_priv)
23258664281bSPaulo Zanoni {
23268664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
232745c1cd87SMika Kahola 	enum pipe pipe;
23288664281bSPaulo Zanoni 
2329de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
2330de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
2331de032bf4SPaulo Zanoni 
233245c1cd87SMika Kahola 	for_each_pipe(dev_priv, pipe)
233345c1cd87SMika Kahola 		if (serr_int & SERR_INT_TRANS_FIFO_UNDERRUN(pipe))
233445c1cd87SMika Kahola 			intel_pch_fifo_underrun_irq_handler(dev_priv, pipe);
23358664281bSPaulo Zanoni 
23368664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
2337776ad806SJesse Barnes }
2338776ad806SJesse Barnes 
233991d14251STvrtko Ursulin static void cpt_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
234023e81d69SAdam Jackson {
234123e81d69SAdam Jackson 	int pipe;
23426dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
2343aaf5ec2eSSonika Jindal 
234491d14251STvrtko Ursulin 	ibx_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_cpt);
234591d131d2SDaniel Vetter 
2346cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
2347cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
234823e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
2349cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
2350cfc33bf7SVille Syrjälä 				 port_name(port));
2351cfc33bf7SVille Syrjälä 	}
235223e81d69SAdam Jackson 
235323e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
235491d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
235523e81d69SAdam Jackson 
235623e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
235791d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
235823e81d69SAdam Jackson 
235923e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
236023e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
236123e81d69SAdam Jackson 
236223e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
236323e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
236423e81d69SAdam Jackson 
236523e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
2366055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
236723e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
236823e81d69SAdam Jackson 					 pipe_name(pipe),
236923e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
23708664281bSPaulo Zanoni 
23718664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
237291d14251STvrtko Ursulin 		cpt_serr_int_handler(dev_priv);
237323e81d69SAdam Jackson }
237423e81d69SAdam Jackson 
237531604222SAnusha Srivatsa static void icp_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
237631604222SAnusha Srivatsa {
237731604222SAnusha Srivatsa 	u32 ddi_hotplug_trigger = pch_iir & SDE_DDI_MASK_ICP;
237831604222SAnusha Srivatsa 	u32 tc_hotplug_trigger = pch_iir & SDE_TC_MASK_ICP;
237931604222SAnusha Srivatsa 	u32 pin_mask = 0, long_mask = 0;
238031604222SAnusha Srivatsa 
238131604222SAnusha Srivatsa 	if (ddi_hotplug_trigger) {
238231604222SAnusha Srivatsa 		u32 dig_hotplug_reg;
238331604222SAnusha Srivatsa 
238431604222SAnusha Srivatsa 		dig_hotplug_reg = I915_READ(SHOTPLUG_CTL_DDI);
238531604222SAnusha Srivatsa 		I915_WRITE(SHOTPLUG_CTL_DDI, dig_hotplug_reg);
238631604222SAnusha Srivatsa 
238731604222SAnusha Srivatsa 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
238831604222SAnusha Srivatsa 				   ddi_hotplug_trigger,
238931604222SAnusha Srivatsa 				   dig_hotplug_reg, hpd_icp,
239031604222SAnusha Srivatsa 				   icp_ddi_port_hotplug_long_detect);
239131604222SAnusha Srivatsa 	}
239231604222SAnusha Srivatsa 
239331604222SAnusha Srivatsa 	if (tc_hotplug_trigger) {
239431604222SAnusha Srivatsa 		u32 dig_hotplug_reg;
239531604222SAnusha Srivatsa 
239631604222SAnusha Srivatsa 		dig_hotplug_reg = I915_READ(SHOTPLUG_CTL_TC);
239731604222SAnusha Srivatsa 		I915_WRITE(SHOTPLUG_CTL_TC, dig_hotplug_reg);
239831604222SAnusha Srivatsa 
239931604222SAnusha Srivatsa 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
240031604222SAnusha Srivatsa 				   tc_hotplug_trigger,
240131604222SAnusha Srivatsa 				   dig_hotplug_reg, hpd_icp,
240231604222SAnusha Srivatsa 				   icp_tc_port_hotplug_long_detect);
240331604222SAnusha Srivatsa 	}
240431604222SAnusha Srivatsa 
240531604222SAnusha Srivatsa 	if (pin_mask)
240631604222SAnusha Srivatsa 		intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
240731604222SAnusha Srivatsa 
240831604222SAnusha Srivatsa 	if (pch_iir & SDE_GMBUS_ICP)
240931604222SAnusha Srivatsa 		gmbus_irq_handler(dev_priv);
241031604222SAnusha Srivatsa }
241131604222SAnusha Srivatsa 
241291d14251STvrtko Ursulin static void spt_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
24136dbf30ceSVille Syrjälä {
24146dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_SPT &
24156dbf30ceSVille Syrjälä 		~SDE_PORTE_HOTPLUG_SPT;
24166dbf30ceSVille Syrjälä 	u32 hotplug2_trigger = pch_iir & SDE_PORTE_HOTPLUG_SPT;
24176dbf30ceSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
24186dbf30ceSVille Syrjälä 
24196dbf30ceSVille Syrjälä 	if (hotplug_trigger) {
24206dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
24216dbf30ceSVille Syrjälä 
24226dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
24236dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
24246dbf30ceSVille Syrjälä 
2425cf53902fSRodrigo Vivi 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
2426cf53902fSRodrigo Vivi 				   hotplug_trigger, dig_hotplug_reg, hpd_spt,
242774c0b395SVille Syrjälä 				   spt_port_hotplug_long_detect);
24286dbf30ceSVille Syrjälä 	}
24296dbf30ceSVille Syrjälä 
24306dbf30ceSVille Syrjälä 	if (hotplug2_trigger) {
24316dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
24326dbf30ceSVille Syrjälä 
24336dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG2);
24346dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG2, dig_hotplug_reg);
24356dbf30ceSVille Syrjälä 
2436cf53902fSRodrigo Vivi 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
2437cf53902fSRodrigo Vivi 				   hotplug2_trigger, dig_hotplug_reg, hpd_spt,
24386dbf30ceSVille Syrjälä 				   spt_port_hotplug2_long_detect);
24396dbf30ceSVille Syrjälä 	}
24406dbf30ceSVille Syrjälä 
24416dbf30ceSVille Syrjälä 	if (pin_mask)
244291d14251STvrtko Ursulin 		intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
24436dbf30ceSVille Syrjälä 
24446dbf30ceSVille Syrjälä 	if (pch_iir & SDE_GMBUS_CPT)
244591d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
24466dbf30ceSVille Syrjälä }
24476dbf30ceSVille Syrjälä 
244891d14251STvrtko Ursulin static void ilk_hpd_irq_handler(struct drm_i915_private *dev_priv,
244991d14251STvrtko Ursulin 				u32 hotplug_trigger,
245040e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2451c008bc6eSPaulo Zanoni {
2452e4ce95aaSVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2453e4ce95aaSVille Syrjälä 
2454e4ce95aaSVille Syrjälä 	dig_hotplug_reg = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
2455e4ce95aaSVille Syrjälä 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, dig_hotplug_reg);
2456e4ce95aaSVille Syrjälä 
2457cf53902fSRodrigo Vivi 	intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask, hotplug_trigger,
245840e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2459e4ce95aaSVille Syrjälä 			   ilk_port_hotplug_long_detect);
246040e56410SVille Syrjälä 
246191d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2462e4ce95aaSVille Syrjälä }
2463c008bc6eSPaulo Zanoni 
246491d14251STvrtko Ursulin static void ilk_display_irq_handler(struct drm_i915_private *dev_priv,
246591d14251STvrtko Ursulin 				    u32 de_iir)
246640e56410SVille Syrjälä {
246740e56410SVille Syrjälä 	enum pipe pipe;
246840e56410SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG;
246940e56410SVille Syrjälä 
247040e56410SVille Syrjälä 	if (hotplug_trigger)
247191d14251STvrtko Ursulin 		ilk_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ilk);
247240e56410SVille Syrjälä 
2473c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
247491d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
2475c008bc6eSPaulo Zanoni 
2476c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
247791d14251STvrtko Ursulin 		intel_opregion_asle_intr(dev_priv);
2478c008bc6eSPaulo Zanoni 
2479c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
2480c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
2481c008bc6eSPaulo Zanoni 
2482055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2483fd3a4024SDaniel Vetter 		if (de_iir & DE_PIPE_VBLANK(pipe))
2484fd3a4024SDaniel Vetter 			drm_handle_vblank(&dev_priv->drm, pipe);
2485c008bc6eSPaulo Zanoni 
248640da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
24871f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
2488c008bc6eSPaulo Zanoni 
248940da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
249091d14251STvrtko Ursulin 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
2491c008bc6eSPaulo Zanoni 	}
2492c008bc6eSPaulo Zanoni 
2493c008bc6eSPaulo Zanoni 	/* check event from PCH */
2494c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
2495c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
2496c008bc6eSPaulo Zanoni 
249791d14251STvrtko Ursulin 		if (HAS_PCH_CPT(dev_priv))
249891d14251STvrtko Ursulin 			cpt_irq_handler(dev_priv, pch_iir);
2499c008bc6eSPaulo Zanoni 		else
250091d14251STvrtko Ursulin 			ibx_irq_handler(dev_priv, pch_iir);
2501c008bc6eSPaulo Zanoni 
2502c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
2503c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
2504c008bc6eSPaulo Zanoni 	}
2505c008bc6eSPaulo Zanoni 
2506cf819effSLucas De Marchi 	if (IS_GEN(dev_priv, 5) && de_iir & DE_PCU_EVENT)
250791d14251STvrtko Ursulin 		ironlake_rps_change_irq_handler(dev_priv);
2508c008bc6eSPaulo Zanoni }
2509c008bc6eSPaulo Zanoni 
251091d14251STvrtko Ursulin static void ivb_display_irq_handler(struct drm_i915_private *dev_priv,
251191d14251STvrtko Ursulin 				    u32 de_iir)
25129719fb98SPaulo Zanoni {
251307d27e20SDamien Lespiau 	enum pipe pipe;
251423bb4cb5SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG_IVB;
251523bb4cb5SVille Syrjälä 
251640e56410SVille Syrjälä 	if (hotplug_trigger)
251791d14251STvrtko Ursulin 		ilk_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ivb);
25189719fb98SPaulo Zanoni 
25199719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
252091d14251STvrtko Ursulin 		ivb_err_int_handler(dev_priv);
25219719fb98SPaulo Zanoni 
252254fd3149SDhinakaran Pandiyan 	if (de_iir & DE_EDP_PSR_INT_HSW) {
252354fd3149SDhinakaran Pandiyan 		u32 psr_iir = I915_READ(EDP_PSR_IIR);
252454fd3149SDhinakaran Pandiyan 
252554fd3149SDhinakaran Pandiyan 		intel_psr_irq_handler(dev_priv, psr_iir);
252654fd3149SDhinakaran Pandiyan 		I915_WRITE(EDP_PSR_IIR, psr_iir);
252754fd3149SDhinakaran Pandiyan 	}
2528fc340442SDaniel Vetter 
25299719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
253091d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
25319719fb98SPaulo Zanoni 
25329719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
253391d14251STvrtko Ursulin 		intel_opregion_asle_intr(dev_priv);
25349719fb98SPaulo Zanoni 
2535055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2536fd3a4024SDaniel Vetter 		if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)))
2537fd3a4024SDaniel Vetter 			drm_handle_vblank(&dev_priv->drm, pipe);
25389719fb98SPaulo Zanoni 	}
25399719fb98SPaulo Zanoni 
25409719fb98SPaulo Zanoni 	/* check event from PCH */
254191d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv) && (de_iir & DE_PCH_EVENT_IVB)) {
25429719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
25439719fb98SPaulo Zanoni 
254491d14251STvrtko Ursulin 		cpt_irq_handler(dev_priv, pch_iir);
25459719fb98SPaulo Zanoni 
25469719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
25479719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
25489719fb98SPaulo Zanoni 	}
25499719fb98SPaulo Zanoni }
25509719fb98SPaulo Zanoni 
255172c90f62SOscar Mateo /*
255272c90f62SOscar Mateo  * To handle irqs with the minimum potential races with fresh interrupts, we:
255372c90f62SOscar Mateo  * 1 - Disable Master Interrupt Control.
255472c90f62SOscar Mateo  * 2 - Find the source(s) of the interrupt.
255572c90f62SOscar Mateo  * 3 - Clear the Interrupt Identity bits (IIR).
255672c90f62SOscar Mateo  * 4 - Process the interrupt(s) that had bits set in the IIRs.
255772c90f62SOscar Mateo  * 5 - Re-enable Master Interrupt Control.
255872c90f62SOscar Mateo  */
2559f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
2560b1f14ad0SJesse Barnes {
256145a83f84SDaniel Vetter 	struct drm_device *dev = arg;
2562fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2563f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
25640e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
2565b1f14ad0SJesse Barnes 
25662dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
25672dd2a883SImre Deak 		return IRQ_NONE;
25682dd2a883SImre Deak 
25691f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
25701f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
25711f814dacSImre Deak 
2572b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
2573b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
2574b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
25750e43406bSChris Wilson 
257644498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
257744498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
257844498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
257944498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
258044498aeaSPaulo Zanoni 	 * due to its back queue). */
258191d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv)) {
258244498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
258344498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
2584ab5c608bSBen Widawsky 	}
258544498aeaSPaulo Zanoni 
258672c90f62SOscar Mateo 	/* Find, clear, then process each source of interrupt */
258772c90f62SOscar Mateo 
25880e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
25890e43406bSChris Wilson 	if (gt_iir) {
259072c90f62SOscar Mateo 		I915_WRITE(GTIIR, gt_iir);
259172c90f62SOscar Mateo 		ret = IRQ_HANDLED;
259291d14251STvrtko Ursulin 		if (INTEL_GEN(dev_priv) >= 6)
2593261e40b8SVille Syrjälä 			snb_gt_irq_handler(dev_priv, gt_iir);
2594d8fc8a47SPaulo Zanoni 		else
2595261e40b8SVille Syrjälä 			ilk_gt_irq_handler(dev_priv, gt_iir);
25960e43406bSChris Wilson 	}
2597b1f14ad0SJesse Barnes 
2598b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
25990e43406bSChris Wilson 	if (de_iir) {
260072c90f62SOscar Mateo 		I915_WRITE(DEIIR, de_iir);
260172c90f62SOscar Mateo 		ret = IRQ_HANDLED;
260291d14251STvrtko Ursulin 		if (INTEL_GEN(dev_priv) >= 7)
260391d14251STvrtko Ursulin 			ivb_display_irq_handler(dev_priv, de_iir);
2604f1af8fc1SPaulo Zanoni 		else
260591d14251STvrtko Ursulin 			ilk_display_irq_handler(dev_priv, de_iir);
26060e43406bSChris Wilson 	}
26070e43406bSChris Wilson 
260891d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 6) {
2609f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
26100e43406bSChris Wilson 		if (pm_iir) {
2611b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
26120e43406bSChris Wilson 			ret = IRQ_HANDLED;
261372c90f62SOscar Mateo 			gen6_rps_irq_handler(dev_priv, pm_iir);
26140e43406bSChris Wilson 		}
2615f1af8fc1SPaulo Zanoni 	}
2616b1f14ad0SJesse Barnes 
2617b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
261874093f3eSChris Wilson 	if (!HAS_PCH_NOP(dev_priv))
261944498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
2620b1f14ad0SJesse Barnes 
26211f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
26221f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
26231f814dacSImre Deak 
2624b1f14ad0SJesse Barnes 	return ret;
2625b1f14ad0SJesse Barnes }
2626b1f14ad0SJesse Barnes 
262791d14251STvrtko Ursulin static void bxt_hpd_irq_handler(struct drm_i915_private *dev_priv,
262891d14251STvrtko Ursulin 				u32 hotplug_trigger,
262940e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2630d04a492dSShashank Sharma {
2631cebd87a0SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2632d04a492dSShashank Sharma 
2633a52bb15bSVille Syrjälä 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
2634a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
2635d04a492dSShashank Sharma 
2636cf53902fSRodrigo Vivi 	intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask, hotplug_trigger,
263740e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2638cebd87a0SVille Syrjälä 			   bxt_port_hotplug_long_detect);
263940e56410SVille Syrjälä 
264091d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2641d04a492dSShashank Sharma }
2642d04a492dSShashank Sharma 
2643121e758eSDhinakaran Pandiyan static void gen11_hpd_irq_handler(struct drm_i915_private *dev_priv, u32 iir)
2644121e758eSDhinakaran Pandiyan {
2645121e758eSDhinakaran Pandiyan 	u32 pin_mask = 0, long_mask = 0;
2646b796b971SDhinakaran Pandiyan 	u32 trigger_tc = iir & GEN11_DE_TC_HOTPLUG_MASK;
2647b796b971SDhinakaran Pandiyan 	u32 trigger_tbt = iir & GEN11_DE_TBT_HOTPLUG_MASK;
2648121e758eSDhinakaran Pandiyan 
2649121e758eSDhinakaran Pandiyan 	if (trigger_tc) {
2650b796b971SDhinakaran Pandiyan 		u32 dig_hotplug_reg;
2651b796b971SDhinakaran Pandiyan 
2652121e758eSDhinakaran Pandiyan 		dig_hotplug_reg = I915_READ(GEN11_TC_HOTPLUG_CTL);
2653121e758eSDhinakaran Pandiyan 		I915_WRITE(GEN11_TC_HOTPLUG_CTL, dig_hotplug_reg);
2654121e758eSDhinakaran Pandiyan 
2655121e758eSDhinakaran Pandiyan 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask, trigger_tc,
2656b796b971SDhinakaran Pandiyan 				   dig_hotplug_reg, hpd_gen11,
2657121e758eSDhinakaran Pandiyan 				   gen11_port_hotplug_long_detect);
2658121e758eSDhinakaran Pandiyan 	}
2659b796b971SDhinakaran Pandiyan 
2660b796b971SDhinakaran Pandiyan 	if (trigger_tbt) {
2661b796b971SDhinakaran Pandiyan 		u32 dig_hotplug_reg;
2662b796b971SDhinakaran Pandiyan 
2663b796b971SDhinakaran Pandiyan 		dig_hotplug_reg = I915_READ(GEN11_TBT_HOTPLUG_CTL);
2664b796b971SDhinakaran Pandiyan 		I915_WRITE(GEN11_TBT_HOTPLUG_CTL, dig_hotplug_reg);
2665b796b971SDhinakaran Pandiyan 
2666b796b971SDhinakaran Pandiyan 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask, trigger_tbt,
2667b796b971SDhinakaran Pandiyan 				   dig_hotplug_reg, hpd_gen11,
2668b796b971SDhinakaran Pandiyan 				   gen11_port_hotplug_long_detect);
2669b796b971SDhinakaran Pandiyan 	}
2670b796b971SDhinakaran Pandiyan 
2671b796b971SDhinakaran Pandiyan 	if (pin_mask)
2672b796b971SDhinakaran Pandiyan 		intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2673b796b971SDhinakaran Pandiyan 	else
2674b796b971SDhinakaran Pandiyan 		DRM_ERROR("Unexpected DE HPD interrupt 0x%08x\n", iir);
2675121e758eSDhinakaran Pandiyan }
2676121e758eSDhinakaran Pandiyan 
26779d17210fSLucas De Marchi static u32 gen8_de_port_aux_mask(struct drm_i915_private *dev_priv)
26789d17210fSLucas De Marchi {
26799d17210fSLucas De Marchi 	u32 mask = GEN8_AUX_CHANNEL_A;
26809d17210fSLucas De Marchi 
26819d17210fSLucas De Marchi 	if (INTEL_GEN(dev_priv) >= 9)
26829d17210fSLucas De Marchi 		mask |= GEN9_AUX_CHANNEL_B |
26839d17210fSLucas De Marchi 			GEN9_AUX_CHANNEL_C |
26849d17210fSLucas De Marchi 			GEN9_AUX_CHANNEL_D;
26859d17210fSLucas De Marchi 
26869d17210fSLucas De Marchi 	if (IS_CNL_WITH_PORT_F(dev_priv))
26879d17210fSLucas De Marchi 		mask |= CNL_AUX_CHANNEL_F;
26889d17210fSLucas De Marchi 
26899d17210fSLucas De Marchi 	if (INTEL_GEN(dev_priv) >= 11)
26909d17210fSLucas De Marchi 		mask |= ICL_AUX_CHANNEL_E |
26919d17210fSLucas De Marchi 			CNL_AUX_CHANNEL_F;
26929d17210fSLucas De Marchi 
26939d17210fSLucas De Marchi 	return mask;
26949d17210fSLucas De Marchi }
26959d17210fSLucas De Marchi 
2696f11a0f46STvrtko Ursulin static irqreturn_t
2697f11a0f46STvrtko Ursulin gen8_de_irq_handler(struct drm_i915_private *dev_priv, u32 master_ctl)
2698abd58f01SBen Widawsky {
2699abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
2700f11a0f46STvrtko Ursulin 	u32 iir;
2701c42664ccSDaniel Vetter 	enum pipe pipe;
270288e04703SJesse Barnes 
2703abd58f01SBen Widawsky 	if (master_ctl & GEN8_DE_MISC_IRQ) {
2704e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_MISC_IIR);
2705e32192e1STvrtko Ursulin 		if (iir) {
2706e04f7eceSVille Syrjälä 			bool found = false;
2707e04f7eceSVille Syrjälä 
2708e32192e1STvrtko Ursulin 			I915_WRITE(GEN8_DE_MISC_IIR, iir);
2709abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
2710e04f7eceSVille Syrjälä 
2711e04f7eceSVille Syrjälä 			if (iir & GEN8_DE_MISC_GSE) {
271291d14251STvrtko Ursulin 				intel_opregion_asle_intr(dev_priv);
2713e04f7eceSVille Syrjälä 				found = true;
2714e04f7eceSVille Syrjälä 			}
2715e04f7eceSVille Syrjälä 
2716e04f7eceSVille Syrjälä 			if (iir & GEN8_DE_EDP_PSR) {
271754fd3149SDhinakaran Pandiyan 				u32 psr_iir = I915_READ(EDP_PSR_IIR);
271854fd3149SDhinakaran Pandiyan 
271954fd3149SDhinakaran Pandiyan 				intel_psr_irq_handler(dev_priv, psr_iir);
272054fd3149SDhinakaran Pandiyan 				I915_WRITE(EDP_PSR_IIR, psr_iir);
2721e04f7eceSVille Syrjälä 				found = true;
2722e04f7eceSVille Syrjälä 			}
2723e04f7eceSVille Syrjälä 
2724e04f7eceSVille Syrjälä 			if (!found)
272538cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Misc interrupt\n");
2726abd58f01SBen Widawsky 		}
272738cc46d7SOscar Mateo 		else
272838cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
2729abd58f01SBen Widawsky 	}
2730abd58f01SBen Widawsky 
2731121e758eSDhinakaran Pandiyan 	if (INTEL_GEN(dev_priv) >= 11 && (master_ctl & GEN11_DE_HPD_IRQ)) {
2732121e758eSDhinakaran Pandiyan 		iir = I915_READ(GEN11_DE_HPD_IIR);
2733121e758eSDhinakaran Pandiyan 		if (iir) {
2734121e758eSDhinakaran Pandiyan 			I915_WRITE(GEN11_DE_HPD_IIR, iir);
2735121e758eSDhinakaran Pandiyan 			ret = IRQ_HANDLED;
2736121e758eSDhinakaran Pandiyan 			gen11_hpd_irq_handler(dev_priv, iir);
2737121e758eSDhinakaran Pandiyan 		} else {
2738121e758eSDhinakaran Pandiyan 			DRM_ERROR("The master control interrupt lied, (DE HPD)!\n");
2739121e758eSDhinakaran Pandiyan 		}
2740121e758eSDhinakaran Pandiyan 	}
2741121e758eSDhinakaran Pandiyan 
27426d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
2743e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_PORT_IIR);
2744e32192e1STvrtko Ursulin 		if (iir) {
2745e32192e1STvrtko Ursulin 			u32 tmp_mask;
2746d04a492dSShashank Sharma 			bool found = false;
2747cebd87a0SVille Syrjälä 
2748e32192e1STvrtko Ursulin 			I915_WRITE(GEN8_DE_PORT_IIR, iir);
27496d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
275088e04703SJesse Barnes 
27519d17210fSLucas De Marchi 			if (iir & gen8_de_port_aux_mask(dev_priv)) {
275291d14251STvrtko Ursulin 				dp_aux_irq_handler(dev_priv);
2753d04a492dSShashank Sharma 				found = true;
2754d04a492dSShashank Sharma 			}
2755d04a492dSShashank Sharma 
2756cc3f90f0SAnder Conselvan de Oliveira 			if (IS_GEN9_LP(dev_priv)) {
2757e32192e1STvrtko Ursulin 				tmp_mask = iir & BXT_DE_PORT_HOTPLUG_MASK;
2758e32192e1STvrtko Ursulin 				if (tmp_mask) {
275991d14251STvrtko Ursulin 					bxt_hpd_irq_handler(dev_priv, tmp_mask,
276091d14251STvrtko Ursulin 							    hpd_bxt);
2761d04a492dSShashank Sharma 					found = true;
2762d04a492dSShashank Sharma 				}
2763e32192e1STvrtko Ursulin 			} else if (IS_BROADWELL(dev_priv)) {
2764e32192e1STvrtko Ursulin 				tmp_mask = iir & GEN8_PORT_DP_A_HOTPLUG;
2765e32192e1STvrtko Ursulin 				if (tmp_mask) {
276691d14251STvrtko Ursulin 					ilk_hpd_irq_handler(dev_priv,
276791d14251STvrtko Ursulin 							    tmp_mask, hpd_bdw);
2768e32192e1STvrtko Ursulin 					found = true;
2769e32192e1STvrtko Ursulin 				}
2770e32192e1STvrtko Ursulin 			}
2771d04a492dSShashank Sharma 
2772cc3f90f0SAnder Conselvan de Oliveira 			if (IS_GEN9_LP(dev_priv) && (iir & BXT_DE_PORT_GMBUS)) {
277391d14251STvrtko Ursulin 				gmbus_irq_handler(dev_priv);
27749e63743eSShashank Sharma 				found = true;
27759e63743eSShashank Sharma 			}
27769e63743eSShashank Sharma 
2777d04a492dSShashank Sharma 			if (!found)
277838cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Port interrupt\n");
27796d766f02SDaniel Vetter 		}
278038cc46d7SOscar Mateo 		else
278138cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
27826d766f02SDaniel Vetter 	}
27836d766f02SDaniel Vetter 
2784055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2785fd3a4024SDaniel Vetter 		u32 fault_errors;
2786abd58f01SBen Widawsky 
2787c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2788c42664ccSDaniel Vetter 			continue;
2789c42664ccSDaniel Vetter 
2790e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
2791e32192e1STvrtko Ursulin 		if (!iir) {
2792e32192e1STvrtko Ursulin 			DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
2793e32192e1STvrtko Ursulin 			continue;
2794e32192e1STvrtko Ursulin 		}
2795770de83dSDamien Lespiau 
2796e32192e1STvrtko Ursulin 		ret = IRQ_HANDLED;
2797e32192e1STvrtko Ursulin 		I915_WRITE(GEN8_DE_PIPE_IIR(pipe), iir);
2798e32192e1STvrtko Ursulin 
2799fd3a4024SDaniel Vetter 		if (iir & GEN8_PIPE_VBLANK)
2800fd3a4024SDaniel Vetter 			drm_handle_vblank(&dev_priv->drm, pipe);
2801abd58f01SBen Widawsky 
2802e32192e1STvrtko Ursulin 		if (iir & GEN8_PIPE_CDCLK_CRC_DONE)
280391d14251STvrtko Ursulin 			hsw_pipe_crc_irq_handler(dev_priv, pipe);
28040fbe7870SDaniel Vetter 
2805e32192e1STvrtko Ursulin 		if (iir & GEN8_PIPE_FIFO_UNDERRUN)
2806e32192e1STvrtko Ursulin 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
280738d83c96SDaniel Vetter 
2808e32192e1STvrtko Ursulin 		fault_errors = iir;
2809bca2bf2aSPandiyan, Dhinakaran 		if (INTEL_GEN(dev_priv) >= 9)
2810e32192e1STvrtko Ursulin 			fault_errors &= GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
2811770de83dSDamien Lespiau 		else
2812e32192e1STvrtko Ursulin 			fault_errors &= GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
2813770de83dSDamien Lespiau 
2814770de83dSDamien Lespiau 		if (fault_errors)
28151353ec38STvrtko Ursulin 			DRM_ERROR("Fault errors on pipe %c: 0x%08x\n",
281630100f2bSDaniel Vetter 				  pipe_name(pipe),
2817e32192e1STvrtko Ursulin 				  fault_errors);
2818abd58f01SBen Widawsky 	}
2819abd58f01SBen Widawsky 
282091d14251STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv) && !HAS_PCH_NOP(dev_priv) &&
2821266ea3d9SShashank Sharma 	    master_ctl & GEN8_DE_PCH_IRQ) {
282292d03a80SDaniel Vetter 		/*
282392d03a80SDaniel Vetter 		 * FIXME(BDW): Assume for now that the new interrupt handling
282492d03a80SDaniel Vetter 		 * scheme also closed the SDE interrupt handling race we've seen
282592d03a80SDaniel Vetter 		 * on older pch-split platforms. But this needs testing.
282692d03a80SDaniel Vetter 		 */
2827e32192e1STvrtko Ursulin 		iir = I915_READ(SDEIIR);
2828e32192e1STvrtko Ursulin 		if (iir) {
2829e32192e1STvrtko Ursulin 			I915_WRITE(SDEIIR, iir);
283092d03a80SDaniel Vetter 			ret = IRQ_HANDLED;
28316dbf30ceSVille Syrjälä 
283231604222SAnusha Srivatsa 			if (HAS_PCH_ICP(dev_priv))
283331604222SAnusha Srivatsa 				icp_irq_handler(dev_priv, iir);
283431604222SAnusha Srivatsa 			else if (HAS_PCH_SPT(dev_priv) ||
283531604222SAnusha Srivatsa 				 HAS_PCH_KBP(dev_priv) ||
28367b22b8c4SRodrigo Vivi 				 HAS_PCH_CNP(dev_priv))
283791d14251STvrtko Ursulin 				spt_irq_handler(dev_priv, iir);
28386dbf30ceSVille Syrjälä 			else
283991d14251STvrtko Ursulin 				cpt_irq_handler(dev_priv, iir);
28402dfb0b81SJani Nikula 		} else {
28412dfb0b81SJani Nikula 			/*
28422dfb0b81SJani Nikula 			 * Like on previous PCH there seems to be something
28432dfb0b81SJani Nikula 			 * fishy going on with forwarding PCH interrupts.
28442dfb0b81SJani Nikula 			 */
28452dfb0b81SJani Nikula 			DRM_DEBUG_DRIVER("The master control interrupt lied (SDE)!\n");
28462dfb0b81SJani Nikula 		}
284792d03a80SDaniel Vetter 	}
284892d03a80SDaniel Vetter 
2849f11a0f46STvrtko Ursulin 	return ret;
2850f11a0f46STvrtko Ursulin }
2851f11a0f46STvrtko Ursulin 
28524376b9c9SMika Kuoppala static inline u32 gen8_master_intr_disable(void __iomem * const regs)
28534376b9c9SMika Kuoppala {
28544376b9c9SMika Kuoppala 	raw_reg_write(regs, GEN8_MASTER_IRQ, 0);
28554376b9c9SMika Kuoppala 
28564376b9c9SMika Kuoppala 	/*
28574376b9c9SMika Kuoppala 	 * Now with master disabled, get a sample of level indications
28584376b9c9SMika Kuoppala 	 * for this interrupt. Indications will be cleared on related acks.
28594376b9c9SMika Kuoppala 	 * New indications can and will light up during processing,
28604376b9c9SMika Kuoppala 	 * and will generate new interrupt after enabling master.
28614376b9c9SMika Kuoppala 	 */
28624376b9c9SMika Kuoppala 	return raw_reg_read(regs, GEN8_MASTER_IRQ);
28634376b9c9SMika Kuoppala }
28644376b9c9SMika Kuoppala 
28654376b9c9SMika Kuoppala static inline void gen8_master_intr_enable(void __iomem * const regs)
28664376b9c9SMika Kuoppala {
28674376b9c9SMika Kuoppala 	raw_reg_write(regs, GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
28684376b9c9SMika Kuoppala }
28694376b9c9SMika Kuoppala 
2870f11a0f46STvrtko Ursulin static irqreturn_t gen8_irq_handler(int irq, void *arg)
2871f11a0f46STvrtko Ursulin {
2872f0fd96f5SChris Wilson 	struct drm_i915_private *dev_priv = to_i915(arg);
28734376b9c9SMika Kuoppala 	void __iomem * const regs = dev_priv->regs;
2874f11a0f46STvrtko Ursulin 	u32 master_ctl;
2875f0fd96f5SChris Wilson 	u32 gt_iir[4];
2876f11a0f46STvrtko Ursulin 
2877f11a0f46STvrtko Ursulin 	if (!intel_irqs_enabled(dev_priv))
2878f11a0f46STvrtko Ursulin 		return IRQ_NONE;
2879f11a0f46STvrtko Ursulin 
28804376b9c9SMika Kuoppala 	master_ctl = gen8_master_intr_disable(regs);
28814376b9c9SMika Kuoppala 	if (!master_ctl) {
28824376b9c9SMika Kuoppala 		gen8_master_intr_enable(regs);
2883f11a0f46STvrtko Ursulin 		return IRQ_NONE;
28844376b9c9SMika Kuoppala 	}
2885f11a0f46STvrtko Ursulin 
2886f11a0f46STvrtko Ursulin 	/* Find, clear, then process each source of interrupt */
288755ef72f2SChris Wilson 	gen8_gt_irq_ack(dev_priv, master_ctl, gt_iir);
2888f0fd96f5SChris Wilson 
2889f0fd96f5SChris Wilson 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
2890f0fd96f5SChris Wilson 	if (master_ctl & ~GEN8_GT_IRQS) {
2891f0fd96f5SChris Wilson 		disable_rpm_wakeref_asserts(dev_priv);
289255ef72f2SChris Wilson 		gen8_de_irq_handler(dev_priv, master_ctl);
2893f0fd96f5SChris Wilson 		enable_rpm_wakeref_asserts(dev_priv);
2894f0fd96f5SChris Wilson 	}
2895f11a0f46STvrtko Ursulin 
28964376b9c9SMika Kuoppala 	gen8_master_intr_enable(regs);
2897abd58f01SBen Widawsky 
2898f0fd96f5SChris Wilson 	gen8_gt_irq_handler(dev_priv, master_ctl, gt_iir);
28991f814dacSImre Deak 
290055ef72f2SChris Wilson 	return IRQ_HANDLED;
2901abd58f01SBen Widawsky }
2902abd58f01SBen Widawsky 
290351951ae7SMika Kuoppala static u32
2904f744dbc2SMika Kuoppala gen11_gt_engine_identity(struct drm_i915_private * const i915,
290551951ae7SMika Kuoppala 			 const unsigned int bank, const unsigned int bit)
290651951ae7SMika Kuoppala {
290751951ae7SMika Kuoppala 	void __iomem * const regs = i915->regs;
290851951ae7SMika Kuoppala 	u32 timeout_ts;
290951951ae7SMika Kuoppala 	u32 ident;
291051951ae7SMika Kuoppala 
291196606f3bSOscar Mateo 	lockdep_assert_held(&i915->irq_lock);
291296606f3bSOscar Mateo 
291351951ae7SMika Kuoppala 	raw_reg_write(regs, GEN11_IIR_REG_SELECTOR(bank), BIT(bit));
291451951ae7SMika Kuoppala 
291551951ae7SMika Kuoppala 	/*
291651951ae7SMika Kuoppala 	 * NB: Specs do not specify how long to spin wait,
291751951ae7SMika Kuoppala 	 * so we do ~100us as an educated guess.
291851951ae7SMika Kuoppala 	 */
291951951ae7SMika Kuoppala 	timeout_ts = (local_clock() >> 10) + 100;
292051951ae7SMika Kuoppala 	do {
292151951ae7SMika Kuoppala 		ident = raw_reg_read(regs, GEN11_INTR_IDENTITY_REG(bank));
292251951ae7SMika Kuoppala 	} while (!(ident & GEN11_INTR_DATA_VALID) &&
292351951ae7SMika Kuoppala 		 !time_after32(local_clock() >> 10, timeout_ts));
292451951ae7SMika Kuoppala 
292551951ae7SMika Kuoppala 	if (unlikely(!(ident & GEN11_INTR_DATA_VALID))) {
292651951ae7SMika Kuoppala 		DRM_ERROR("INTR_IDENTITY_REG%u:%u 0x%08x not valid!\n",
292751951ae7SMika Kuoppala 			  bank, bit, ident);
292851951ae7SMika Kuoppala 		return 0;
292951951ae7SMika Kuoppala 	}
293051951ae7SMika Kuoppala 
293151951ae7SMika Kuoppala 	raw_reg_write(regs, GEN11_INTR_IDENTITY_REG(bank),
293251951ae7SMika Kuoppala 		      GEN11_INTR_DATA_VALID);
293351951ae7SMika Kuoppala 
2934f744dbc2SMika Kuoppala 	return ident;
2935f744dbc2SMika Kuoppala }
2936f744dbc2SMika Kuoppala 
2937f744dbc2SMika Kuoppala static void
2938f744dbc2SMika Kuoppala gen11_other_irq_handler(struct drm_i915_private * const i915,
2939f744dbc2SMika Kuoppala 			const u8 instance, const u16 iir)
2940f744dbc2SMika Kuoppala {
2941d02b98b8SOscar Mateo 	if (instance == OTHER_GTPM_INSTANCE)
2942d02b98b8SOscar Mateo 		return gen6_rps_irq_handler(i915, iir);
2943d02b98b8SOscar Mateo 
2944f744dbc2SMika Kuoppala 	WARN_ONCE(1, "unhandled other interrupt instance=0x%x, iir=0x%x\n",
2945f744dbc2SMika Kuoppala 		  instance, iir);
2946f744dbc2SMika Kuoppala }
2947f744dbc2SMika Kuoppala 
2948f744dbc2SMika Kuoppala static void
2949f744dbc2SMika Kuoppala gen11_engine_irq_handler(struct drm_i915_private * const i915,
2950f744dbc2SMika Kuoppala 			 const u8 class, const u8 instance, const u16 iir)
2951f744dbc2SMika Kuoppala {
2952f744dbc2SMika Kuoppala 	struct intel_engine_cs *engine;
2953f744dbc2SMika Kuoppala 
2954f744dbc2SMika Kuoppala 	if (instance <= MAX_ENGINE_INSTANCE)
2955f744dbc2SMika Kuoppala 		engine = i915->engine_class[class][instance];
2956f744dbc2SMika Kuoppala 	else
2957f744dbc2SMika Kuoppala 		engine = NULL;
2958f744dbc2SMika Kuoppala 
2959f744dbc2SMika Kuoppala 	if (likely(engine))
2960f744dbc2SMika Kuoppala 		return gen8_cs_irq_handler(engine, iir);
2961f744dbc2SMika Kuoppala 
2962f744dbc2SMika Kuoppala 	WARN_ONCE(1, "unhandled engine interrupt class=0x%x, instance=0x%x\n",
2963f744dbc2SMika Kuoppala 		  class, instance);
2964f744dbc2SMika Kuoppala }
2965f744dbc2SMika Kuoppala 
2966f744dbc2SMika Kuoppala static void
2967f744dbc2SMika Kuoppala gen11_gt_identity_handler(struct drm_i915_private * const i915,
2968f744dbc2SMika Kuoppala 			  const u32 identity)
2969f744dbc2SMika Kuoppala {
2970f744dbc2SMika Kuoppala 	const u8 class = GEN11_INTR_ENGINE_CLASS(identity);
2971f744dbc2SMika Kuoppala 	const u8 instance = GEN11_INTR_ENGINE_INSTANCE(identity);
2972f744dbc2SMika Kuoppala 	const u16 intr = GEN11_INTR_ENGINE_INTR(identity);
2973f744dbc2SMika Kuoppala 
2974f744dbc2SMika Kuoppala 	if (unlikely(!intr))
2975f744dbc2SMika Kuoppala 		return;
2976f744dbc2SMika Kuoppala 
2977f744dbc2SMika Kuoppala 	if (class <= COPY_ENGINE_CLASS)
2978f744dbc2SMika Kuoppala 		return gen11_engine_irq_handler(i915, class, instance, intr);
2979f744dbc2SMika Kuoppala 
2980f744dbc2SMika Kuoppala 	if (class == OTHER_CLASS)
2981f744dbc2SMika Kuoppala 		return gen11_other_irq_handler(i915, instance, intr);
2982f744dbc2SMika Kuoppala 
2983f744dbc2SMika Kuoppala 	WARN_ONCE(1, "unknown interrupt class=0x%x, instance=0x%x, intr=0x%x\n",
2984f744dbc2SMika Kuoppala 		  class, instance, intr);
298551951ae7SMika Kuoppala }
298651951ae7SMika Kuoppala 
298751951ae7SMika Kuoppala static void
298896606f3bSOscar Mateo gen11_gt_bank_handler(struct drm_i915_private * const i915,
298996606f3bSOscar Mateo 		      const unsigned int bank)
299051951ae7SMika Kuoppala {
299151951ae7SMika Kuoppala 	void __iomem * const regs = i915->regs;
299251951ae7SMika Kuoppala 	unsigned long intr_dw;
299351951ae7SMika Kuoppala 	unsigned int bit;
299451951ae7SMika Kuoppala 
299596606f3bSOscar Mateo 	lockdep_assert_held(&i915->irq_lock);
299651951ae7SMika Kuoppala 
299751951ae7SMika Kuoppala 	intr_dw = raw_reg_read(regs, GEN11_GT_INTR_DW(bank));
299851951ae7SMika Kuoppala 
299951951ae7SMika Kuoppala 	if (unlikely(!intr_dw)) {
300051951ae7SMika Kuoppala 		DRM_ERROR("GT_INTR_DW%u blank!\n", bank);
300196606f3bSOscar Mateo 		return;
300251951ae7SMika Kuoppala 	}
300351951ae7SMika Kuoppala 
300451951ae7SMika Kuoppala 	for_each_set_bit(bit, &intr_dw, 32) {
3005f744dbc2SMika Kuoppala 		const u32 ident = gen11_gt_engine_identity(i915,
3006f744dbc2SMika Kuoppala 							   bank, bit);
300751951ae7SMika Kuoppala 
3008f744dbc2SMika Kuoppala 		gen11_gt_identity_handler(i915, ident);
300951951ae7SMika Kuoppala 	}
301051951ae7SMika Kuoppala 
301151951ae7SMika Kuoppala 	/* Clear must be after shared has been served for engine */
301251951ae7SMika Kuoppala 	raw_reg_write(regs, GEN11_GT_INTR_DW(bank), intr_dw);
301351951ae7SMika Kuoppala }
301496606f3bSOscar Mateo 
301596606f3bSOscar Mateo static void
301696606f3bSOscar Mateo gen11_gt_irq_handler(struct drm_i915_private * const i915,
301796606f3bSOscar Mateo 		     const u32 master_ctl)
301896606f3bSOscar Mateo {
301996606f3bSOscar Mateo 	unsigned int bank;
302096606f3bSOscar Mateo 
302196606f3bSOscar Mateo 	spin_lock(&i915->irq_lock);
302296606f3bSOscar Mateo 
302396606f3bSOscar Mateo 	for (bank = 0; bank < 2; bank++) {
302496606f3bSOscar Mateo 		if (master_ctl & GEN11_GT_DW_IRQ(bank))
302596606f3bSOscar Mateo 			gen11_gt_bank_handler(i915, bank);
302696606f3bSOscar Mateo 	}
302796606f3bSOscar Mateo 
302896606f3bSOscar Mateo 	spin_unlock(&i915->irq_lock);
302951951ae7SMika Kuoppala }
303051951ae7SMika Kuoppala 
30317a909383SChris Wilson static u32
30327a909383SChris Wilson gen11_gu_misc_irq_ack(struct drm_i915_private *dev_priv, const u32 master_ctl)
3033df0d28c1SDhinakaran Pandiyan {
3034df0d28c1SDhinakaran Pandiyan 	void __iomem * const regs = dev_priv->regs;
30357a909383SChris Wilson 	u32 iir;
3036df0d28c1SDhinakaran Pandiyan 
3037df0d28c1SDhinakaran Pandiyan 	if (!(master_ctl & GEN11_GU_MISC_IRQ))
30387a909383SChris Wilson 		return 0;
3039df0d28c1SDhinakaran Pandiyan 
30407a909383SChris Wilson 	iir = raw_reg_read(regs, GEN11_GU_MISC_IIR);
30417a909383SChris Wilson 	if (likely(iir))
30427a909383SChris Wilson 		raw_reg_write(regs, GEN11_GU_MISC_IIR, iir);
30437a909383SChris Wilson 
30447a909383SChris Wilson 	return iir;
3045df0d28c1SDhinakaran Pandiyan }
3046df0d28c1SDhinakaran Pandiyan 
3047df0d28c1SDhinakaran Pandiyan static void
30487a909383SChris Wilson gen11_gu_misc_irq_handler(struct drm_i915_private *dev_priv, const u32 iir)
3049df0d28c1SDhinakaran Pandiyan {
3050df0d28c1SDhinakaran Pandiyan 	if (iir & GEN11_GU_MISC_GSE)
3051df0d28c1SDhinakaran Pandiyan 		intel_opregion_asle_intr(dev_priv);
3052df0d28c1SDhinakaran Pandiyan }
3053df0d28c1SDhinakaran Pandiyan 
305481067b71SMika Kuoppala static inline u32 gen11_master_intr_disable(void __iomem * const regs)
305581067b71SMika Kuoppala {
305681067b71SMika Kuoppala 	raw_reg_write(regs, GEN11_GFX_MSTR_IRQ, 0);
305781067b71SMika Kuoppala 
305881067b71SMika Kuoppala 	/*
305981067b71SMika Kuoppala 	 * Now with master disabled, get a sample of level indications
306081067b71SMika Kuoppala 	 * for this interrupt. Indications will be cleared on related acks.
306181067b71SMika Kuoppala 	 * New indications can and will light up during processing,
306281067b71SMika Kuoppala 	 * and will generate new interrupt after enabling master.
306381067b71SMika Kuoppala 	 */
306481067b71SMika Kuoppala 	return raw_reg_read(regs, GEN11_GFX_MSTR_IRQ);
306581067b71SMika Kuoppala }
306681067b71SMika Kuoppala 
306781067b71SMika Kuoppala static inline void gen11_master_intr_enable(void __iomem * const regs)
306881067b71SMika Kuoppala {
306981067b71SMika Kuoppala 	raw_reg_write(regs, GEN11_GFX_MSTR_IRQ, GEN11_MASTER_IRQ);
307081067b71SMika Kuoppala }
307181067b71SMika Kuoppala 
307251951ae7SMika Kuoppala static irqreturn_t gen11_irq_handler(int irq, void *arg)
307351951ae7SMika Kuoppala {
307451951ae7SMika Kuoppala 	struct drm_i915_private * const i915 = to_i915(arg);
307551951ae7SMika Kuoppala 	void __iomem * const regs = i915->regs;
307651951ae7SMika Kuoppala 	u32 master_ctl;
3077df0d28c1SDhinakaran Pandiyan 	u32 gu_misc_iir;
307851951ae7SMika Kuoppala 
307951951ae7SMika Kuoppala 	if (!intel_irqs_enabled(i915))
308051951ae7SMika Kuoppala 		return IRQ_NONE;
308151951ae7SMika Kuoppala 
308281067b71SMika Kuoppala 	master_ctl = gen11_master_intr_disable(regs);
308381067b71SMika Kuoppala 	if (!master_ctl) {
308481067b71SMika Kuoppala 		gen11_master_intr_enable(regs);
308551951ae7SMika Kuoppala 		return IRQ_NONE;
308681067b71SMika Kuoppala 	}
308751951ae7SMika Kuoppala 
308851951ae7SMika Kuoppala 	/* Find, clear, then process each source of interrupt. */
308951951ae7SMika Kuoppala 	gen11_gt_irq_handler(i915, master_ctl);
309051951ae7SMika Kuoppala 
309151951ae7SMika Kuoppala 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
309251951ae7SMika Kuoppala 	if (master_ctl & GEN11_DISPLAY_IRQ) {
309351951ae7SMika Kuoppala 		const u32 disp_ctl = raw_reg_read(regs, GEN11_DISPLAY_INT_CTL);
309451951ae7SMika Kuoppala 
309551951ae7SMika Kuoppala 		disable_rpm_wakeref_asserts(i915);
309651951ae7SMika Kuoppala 		/*
309751951ae7SMika Kuoppala 		 * GEN11_DISPLAY_INT_CTL has same format as GEN8_MASTER_IRQ
309851951ae7SMika Kuoppala 		 * for the display related bits.
309951951ae7SMika Kuoppala 		 */
310051951ae7SMika Kuoppala 		gen8_de_irq_handler(i915, disp_ctl);
310151951ae7SMika Kuoppala 		enable_rpm_wakeref_asserts(i915);
310251951ae7SMika Kuoppala 	}
310351951ae7SMika Kuoppala 
31047a909383SChris Wilson 	gu_misc_iir = gen11_gu_misc_irq_ack(i915, master_ctl);
3105df0d28c1SDhinakaran Pandiyan 
310681067b71SMika Kuoppala 	gen11_master_intr_enable(regs);
310751951ae7SMika Kuoppala 
31087a909383SChris Wilson 	gen11_gu_misc_irq_handler(i915, gu_misc_iir);
3109df0d28c1SDhinakaran Pandiyan 
311051951ae7SMika Kuoppala 	return IRQ_HANDLED;
311151951ae7SMika Kuoppala }
311251951ae7SMika Kuoppala 
311342f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
311442f52ef8SKeith Packard  * we use as a pipe index
311542f52ef8SKeith Packard  */
311686e83e35SChris Wilson static int i8xx_enable_vblank(struct drm_device *dev, unsigned int pipe)
31170a3e67a4SJesse Barnes {
3118fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3119e9d21d7fSKeith Packard 	unsigned long irqflags;
312071e0ffa5SJesse Barnes 
31211ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
312286e83e35SChris Wilson 	i915_enable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_STATUS);
312386e83e35SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
312486e83e35SChris Wilson 
312586e83e35SChris Wilson 	return 0;
312686e83e35SChris Wilson }
312786e83e35SChris Wilson 
312886e83e35SChris Wilson static int i965_enable_vblank(struct drm_device *dev, unsigned int pipe)
312986e83e35SChris Wilson {
313086e83e35SChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
313186e83e35SChris Wilson 	unsigned long irqflags;
313286e83e35SChris Wilson 
313386e83e35SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
31347c463586SKeith Packard 	i915_enable_pipestat(dev_priv, pipe,
3135755e9019SImre Deak 			     PIPE_START_VBLANK_INTERRUPT_STATUS);
31361ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
31378692d00eSChris Wilson 
31380a3e67a4SJesse Barnes 	return 0;
31390a3e67a4SJesse Barnes }
31400a3e67a4SJesse Barnes 
314188e72717SThierry Reding static int ironlake_enable_vblank(struct drm_device *dev, unsigned int pipe)
3142f796cf8fSJesse Barnes {
3143fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3144f796cf8fSJesse Barnes 	unsigned long irqflags;
3145a9c287c9SJani Nikula 	u32 bit = INTEL_GEN(dev_priv) >= 7 ?
314686e83e35SChris Wilson 		DE_PIPE_VBLANK_IVB(pipe) : DE_PIPE_VBLANK(pipe);
3147f796cf8fSJesse Barnes 
3148f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3149fbdedaeaSVille Syrjälä 	ilk_enable_display_irq(dev_priv, bit);
3150b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3151b1f14ad0SJesse Barnes 
31522e8bf223SDhinakaran Pandiyan 	/* Even though there is no DMC, frame counter can get stuck when
31532e8bf223SDhinakaran Pandiyan 	 * PSR is active as no frames are generated.
31542e8bf223SDhinakaran Pandiyan 	 */
31552e8bf223SDhinakaran Pandiyan 	if (HAS_PSR(dev_priv))
31562e8bf223SDhinakaran Pandiyan 		drm_vblank_restore(dev, pipe);
31572e8bf223SDhinakaran Pandiyan 
3158b1f14ad0SJesse Barnes 	return 0;
3159b1f14ad0SJesse Barnes }
3160b1f14ad0SJesse Barnes 
316188e72717SThierry Reding static int gen8_enable_vblank(struct drm_device *dev, unsigned int pipe)
3162abd58f01SBen Widawsky {
3163fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3164abd58f01SBen Widawsky 	unsigned long irqflags;
3165abd58f01SBen Widawsky 
3166abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3167013d3752SVille Syrjälä 	bdw_enable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
3168abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3169013d3752SVille Syrjälä 
31702e8bf223SDhinakaran Pandiyan 	/* Even if there is no DMC, frame counter can get stuck when
31712e8bf223SDhinakaran Pandiyan 	 * PSR is active as no frames are generated, so check only for PSR.
31722e8bf223SDhinakaran Pandiyan 	 */
31732e8bf223SDhinakaran Pandiyan 	if (HAS_PSR(dev_priv))
31742e8bf223SDhinakaran Pandiyan 		drm_vblank_restore(dev, pipe);
31752e8bf223SDhinakaran Pandiyan 
3176abd58f01SBen Widawsky 	return 0;
3177abd58f01SBen Widawsky }
3178abd58f01SBen Widawsky 
317942f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
318042f52ef8SKeith Packard  * we use as a pipe index
318142f52ef8SKeith Packard  */
318286e83e35SChris Wilson static void i8xx_disable_vblank(struct drm_device *dev, unsigned int pipe)
318386e83e35SChris Wilson {
318486e83e35SChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
318586e83e35SChris Wilson 	unsigned long irqflags;
318686e83e35SChris Wilson 
318786e83e35SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
318886e83e35SChris Wilson 	i915_disable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_STATUS);
318986e83e35SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
319086e83e35SChris Wilson }
319186e83e35SChris Wilson 
319286e83e35SChris Wilson static void i965_disable_vblank(struct drm_device *dev, unsigned int pipe)
31930a3e67a4SJesse Barnes {
3194fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3195e9d21d7fSKeith Packard 	unsigned long irqflags;
31960a3e67a4SJesse Barnes 
31971ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
31987c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
3199755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
32001ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
32010a3e67a4SJesse Barnes }
32020a3e67a4SJesse Barnes 
320388e72717SThierry Reding static void ironlake_disable_vblank(struct drm_device *dev, unsigned int pipe)
3204f796cf8fSJesse Barnes {
3205fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3206f796cf8fSJesse Barnes 	unsigned long irqflags;
3207a9c287c9SJani Nikula 	u32 bit = INTEL_GEN(dev_priv) >= 7 ?
320886e83e35SChris Wilson 		DE_PIPE_VBLANK_IVB(pipe) : DE_PIPE_VBLANK(pipe);
3209f796cf8fSJesse Barnes 
3210f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3211fbdedaeaSVille Syrjälä 	ilk_disable_display_irq(dev_priv, bit);
3212b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3213b1f14ad0SJesse Barnes }
3214b1f14ad0SJesse Barnes 
321588e72717SThierry Reding static void gen8_disable_vblank(struct drm_device *dev, unsigned int pipe)
3216abd58f01SBen Widawsky {
3217fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3218abd58f01SBen Widawsky 	unsigned long irqflags;
3219abd58f01SBen Widawsky 
3220abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3221013d3752SVille Syrjälä 	bdw_disable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
3222abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3223abd58f01SBen Widawsky }
3224abd58f01SBen Widawsky 
3225b243f530STvrtko Ursulin static void ibx_irq_reset(struct drm_i915_private *dev_priv)
322691738a95SPaulo Zanoni {
32276e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
322891738a95SPaulo Zanoni 		return;
322991738a95SPaulo Zanoni 
32303488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(SDE);
3231105b122eSPaulo Zanoni 
32326e266956STvrtko Ursulin 	if (HAS_PCH_CPT(dev_priv) || HAS_PCH_LPT(dev_priv))
3233105b122eSPaulo Zanoni 		I915_WRITE(SERR_INT, 0xffffffff);
3234622364b6SPaulo Zanoni }
3235105b122eSPaulo Zanoni 
323691738a95SPaulo Zanoni /*
3237622364b6SPaulo Zanoni  * SDEIER is also touched by the interrupt handler to work around missed PCH
3238622364b6SPaulo Zanoni  * interrupts. Hence we can't update it after the interrupt handler is enabled -
3239622364b6SPaulo Zanoni  * instead we unconditionally enable all PCH interrupt sources here, but then
3240622364b6SPaulo Zanoni  * only unmask them as needed with SDEIMR.
3241622364b6SPaulo Zanoni  *
3242622364b6SPaulo Zanoni  * This function needs to be called before interrupts are enabled.
324391738a95SPaulo Zanoni  */
3244622364b6SPaulo Zanoni static void ibx_irq_pre_postinstall(struct drm_device *dev)
3245622364b6SPaulo Zanoni {
3246fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3247622364b6SPaulo Zanoni 
32486e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
3249622364b6SPaulo Zanoni 		return;
3250622364b6SPaulo Zanoni 
3251622364b6SPaulo Zanoni 	WARN_ON(I915_READ(SDEIER) != 0);
325291738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
325391738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
325491738a95SPaulo Zanoni }
325591738a95SPaulo Zanoni 
3256b243f530STvrtko Ursulin static void gen5_gt_irq_reset(struct drm_i915_private *dev_priv)
3257d18ea1b5SDaniel Vetter {
32583488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(GT);
3259b243f530STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 6)
32603488d4ebSVille Syrjälä 		GEN3_IRQ_RESET(GEN6_PM);
3261d18ea1b5SDaniel Vetter }
3262d18ea1b5SDaniel Vetter 
326370591a41SVille Syrjälä static void vlv_display_irq_reset(struct drm_i915_private *dev_priv)
326470591a41SVille Syrjälä {
326571b8b41dSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
326671b8b41dSVille Syrjälä 		I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
326771b8b41dSVille Syrjälä 	else
326871b8b41dSVille Syrjälä 		I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
326971b8b41dSVille Syrjälä 
3270ad22d106SVille Syrjälä 	i915_hotplug_interrupt_update_locked(dev_priv, 0xffffffff, 0);
327170591a41SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
327270591a41SVille Syrjälä 
327344d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
327470591a41SVille Syrjälä 
32753488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(VLV_);
32768bd099a7SChris Wilson 	dev_priv->irq_mask = ~0u;
327770591a41SVille Syrjälä }
327870591a41SVille Syrjälä 
32798bb61306SVille Syrjälä static void vlv_display_irq_postinstall(struct drm_i915_private *dev_priv)
32808bb61306SVille Syrjälä {
32818bb61306SVille Syrjälä 	u32 pipestat_mask;
32829ab981f2SVille Syrjälä 	u32 enable_mask;
32838bb61306SVille Syrjälä 	enum pipe pipe;
32848bb61306SVille Syrjälä 
3285842ebf7aSVille Syrjälä 	pipestat_mask = PIPE_CRC_DONE_INTERRUPT_STATUS;
32868bb61306SVille Syrjälä 
32878bb61306SVille Syrjälä 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
32888bb61306SVille Syrjälä 	for_each_pipe(dev_priv, pipe)
32898bb61306SVille Syrjälä 		i915_enable_pipestat(dev_priv, pipe, pipestat_mask);
32908bb61306SVille Syrjälä 
32919ab981f2SVille Syrjälä 	enable_mask = I915_DISPLAY_PORT_INTERRUPT |
32928bb61306SVille Syrjälä 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3293ebf5f921SVille Syrjälä 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3294ebf5f921SVille Syrjälä 		I915_LPE_PIPE_A_INTERRUPT |
3295ebf5f921SVille Syrjälä 		I915_LPE_PIPE_B_INTERRUPT;
3296ebf5f921SVille Syrjälä 
32978bb61306SVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
3298ebf5f921SVille Syrjälä 		enable_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT |
3299ebf5f921SVille Syrjälä 			I915_LPE_PIPE_C_INTERRUPT;
33006b7eafc1SVille Syrjälä 
33018bd099a7SChris Wilson 	WARN_ON(dev_priv->irq_mask != ~0u);
33026b7eafc1SVille Syrjälä 
33039ab981f2SVille Syrjälä 	dev_priv->irq_mask = ~enable_mask;
33048bb61306SVille Syrjälä 
33053488d4ebSVille Syrjälä 	GEN3_IRQ_INIT(VLV_, dev_priv->irq_mask, enable_mask);
33068bb61306SVille Syrjälä }
33078bb61306SVille Syrjälä 
33088bb61306SVille Syrjälä /* drm_dma.h hooks
33098bb61306SVille Syrjälä */
33108bb61306SVille Syrjälä static void ironlake_irq_reset(struct drm_device *dev)
33118bb61306SVille Syrjälä {
3312fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
33138bb61306SVille Syrjälä 
33143488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(DE);
3315cf819effSLucas De Marchi 	if (IS_GEN(dev_priv, 7))
33168bb61306SVille Syrjälä 		I915_WRITE(GEN7_ERR_INT, 0xffffffff);
33178bb61306SVille Syrjälä 
3318fc340442SDaniel Vetter 	if (IS_HASWELL(dev_priv)) {
3319fc340442SDaniel Vetter 		I915_WRITE(EDP_PSR_IMR, 0xffffffff);
3320fc340442SDaniel Vetter 		I915_WRITE(EDP_PSR_IIR, 0xffffffff);
3321fc340442SDaniel Vetter 	}
3322fc340442SDaniel Vetter 
3323b243f530STvrtko Ursulin 	gen5_gt_irq_reset(dev_priv);
33248bb61306SVille Syrjälä 
3325b243f530STvrtko Ursulin 	ibx_irq_reset(dev_priv);
33268bb61306SVille Syrjälä }
33278bb61306SVille Syrjälä 
33286bcdb1c8SVille Syrjälä static void valleyview_irq_reset(struct drm_device *dev)
33297e231dbeSJesse Barnes {
3330fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
33317e231dbeSJesse Barnes 
333234c7b8a7SVille Syrjälä 	I915_WRITE(VLV_MASTER_IER, 0);
333334c7b8a7SVille Syrjälä 	POSTING_READ(VLV_MASTER_IER);
333434c7b8a7SVille Syrjälä 
3335b243f530STvrtko Ursulin 	gen5_gt_irq_reset(dev_priv);
33367e231dbeSJesse Barnes 
3337ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
33389918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
333970591a41SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3340ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
33417e231dbeSJesse Barnes }
33427e231dbeSJesse Barnes 
3343d6e3cca3SDaniel Vetter static void gen8_gt_irq_reset(struct drm_i915_private *dev_priv)
3344d6e3cca3SDaniel Vetter {
3345d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 0);
3346d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 1);
3347d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 2);
3348d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 3);
3349d6e3cca3SDaniel Vetter }
3350d6e3cca3SDaniel Vetter 
3351823f6b38SPaulo Zanoni static void gen8_irq_reset(struct drm_device *dev)
3352abd58f01SBen Widawsky {
3353fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3354abd58f01SBen Widawsky 	int pipe;
3355abd58f01SBen Widawsky 
33564376b9c9SMika Kuoppala 	gen8_master_intr_disable(dev_priv->regs);
3357abd58f01SBen Widawsky 
3358d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
3359abd58f01SBen Widawsky 
3360e04f7eceSVille Syrjälä 	I915_WRITE(EDP_PSR_IMR, 0xffffffff);
3361e04f7eceSVille Syrjälä 	I915_WRITE(EDP_PSR_IIR, 0xffffffff);
3362e04f7eceSVille Syrjälä 
3363055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3364f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3365813bde43SPaulo Zanoni 						   POWER_DOMAIN_PIPE(pipe)))
3366f86f3fb0SPaulo Zanoni 			GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3367abd58f01SBen Widawsky 
33683488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(GEN8_DE_PORT_);
33693488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(GEN8_DE_MISC_);
33703488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(GEN8_PCU_);
3371abd58f01SBen Widawsky 
33726e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
3373b243f530STvrtko Ursulin 		ibx_irq_reset(dev_priv);
3374abd58f01SBen Widawsky }
3375abd58f01SBen Widawsky 
337651951ae7SMika Kuoppala static void gen11_gt_irq_reset(struct drm_i915_private *dev_priv)
337751951ae7SMika Kuoppala {
337851951ae7SMika Kuoppala 	/* Disable RCS, BCS, VCS and VECS class engines. */
337951951ae7SMika Kuoppala 	I915_WRITE(GEN11_RENDER_COPY_INTR_ENABLE, 0);
338051951ae7SMika Kuoppala 	I915_WRITE(GEN11_VCS_VECS_INTR_ENABLE,	  0);
338151951ae7SMika Kuoppala 
338251951ae7SMika Kuoppala 	/* Restore masks irqs on RCS, BCS, VCS and VECS engines. */
338351951ae7SMika Kuoppala 	I915_WRITE(GEN11_RCS0_RSVD_INTR_MASK,	~0);
338451951ae7SMika Kuoppala 	I915_WRITE(GEN11_BCS_RSVD_INTR_MASK,	~0);
338551951ae7SMika Kuoppala 	I915_WRITE(GEN11_VCS0_VCS1_INTR_MASK,	~0);
338651951ae7SMika Kuoppala 	I915_WRITE(GEN11_VCS2_VCS3_INTR_MASK,	~0);
338751951ae7SMika Kuoppala 	I915_WRITE(GEN11_VECS0_VECS1_INTR_MASK,	~0);
3388d02b98b8SOscar Mateo 
3389d02b98b8SOscar Mateo 	I915_WRITE(GEN11_GPM_WGBOXPERF_INTR_ENABLE, 0);
3390d02b98b8SOscar Mateo 	I915_WRITE(GEN11_GPM_WGBOXPERF_INTR_MASK,  ~0);
339151951ae7SMika Kuoppala }
339251951ae7SMika Kuoppala 
339351951ae7SMika Kuoppala static void gen11_irq_reset(struct drm_device *dev)
339451951ae7SMika Kuoppala {
339551951ae7SMika Kuoppala 	struct drm_i915_private *dev_priv = dev->dev_private;
339651951ae7SMika Kuoppala 	int pipe;
339751951ae7SMika Kuoppala 
339881067b71SMika Kuoppala 	gen11_master_intr_disable(dev_priv->regs);
339951951ae7SMika Kuoppala 
340051951ae7SMika Kuoppala 	gen11_gt_irq_reset(dev_priv);
340151951ae7SMika Kuoppala 
340251951ae7SMika Kuoppala 	I915_WRITE(GEN11_DISPLAY_INT_CTL, 0);
340351951ae7SMika Kuoppala 
340462819dfdSJosé Roberto de Souza 	I915_WRITE(EDP_PSR_IMR, 0xffffffff);
340562819dfdSJosé Roberto de Souza 	I915_WRITE(EDP_PSR_IIR, 0xffffffff);
340662819dfdSJosé Roberto de Souza 
340751951ae7SMika Kuoppala 	for_each_pipe(dev_priv, pipe)
340851951ae7SMika Kuoppala 		if (intel_display_power_is_enabled(dev_priv,
340951951ae7SMika Kuoppala 						   POWER_DOMAIN_PIPE(pipe)))
341051951ae7SMika Kuoppala 			GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
341151951ae7SMika Kuoppala 
341251951ae7SMika Kuoppala 	GEN3_IRQ_RESET(GEN8_DE_PORT_);
341351951ae7SMika Kuoppala 	GEN3_IRQ_RESET(GEN8_DE_MISC_);
3414121e758eSDhinakaran Pandiyan 	GEN3_IRQ_RESET(GEN11_DE_HPD_);
3415df0d28c1SDhinakaran Pandiyan 	GEN3_IRQ_RESET(GEN11_GU_MISC_);
341651951ae7SMika Kuoppala 	GEN3_IRQ_RESET(GEN8_PCU_);
341731604222SAnusha Srivatsa 
341831604222SAnusha Srivatsa 	if (HAS_PCH_ICP(dev_priv))
341931604222SAnusha Srivatsa 		GEN3_IRQ_RESET(SDE);
342051951ae7SMika Kuoppala }
342151951ae7SMika Kuoppala 
34224c6c03beSDamien Lespiau void gen8_irq_power_well_post_enable(struct drm_i915_private *dev_priv,
3423001bd2cbSImre Deak 				     u8 pipe_mask)
3424d49bdb0eSPaulo Zanoni {
3425a9c287c9SJani Nikula 	u32 extra_ier = GEN8_PIPE_VBLANK | GEN8_PIPE_FIFO_UNDERRUN;
34266831f3e3SVille Syrjälä 	enum pipe pipe;
3427d49bdb0eSPaulo Zanoni 
342813321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
34299dfe2e3aSImre Deak 
34309dfe2e3aSImre Deak 	if (!intel_irqs_enabled(dev_priv)) {
34319dfe2e3aSImre Deak 		spin_unlock_irq(&dev_priv->irq_lock);
34329dfe2e3aSImre Deak 		return;
34339dfe2e3aSImre Deak 	}
34349dfe2e3aSImre Deak 
34356831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
34366831f3e3SVille Syrjälä 		GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
34376831f3e3SVille Syrjälä 				  dev_priv->de_irq_mask[pipe],
34386831f3e3SVille Syrjälä 				  ~dev_priv->de_irq_mask[pipe] | extra_ier);
34399dfe2e3aSImre Deak 
344013321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3441d49bdb0eSPaulo Zanoni }
3442d49bdb0eSPaulo Zanoni 
3443aae8ba84SVille Syrjälä void gen8_irq_power_well_pre_disable(struct drm_i915_private *dev_priv,
3444001bd2cbSImre Deak 				     u8 pipe_mask)
3445aae8ba84SVille Syrjälä {
34466831f3e3SVille Syrjälä 	enum pipe pipe;
34476831f3e3SVille Syrjälä 
3448aae8ba84SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
34499dfe2e3aSImre Deak 
34509dfe2e3aSImre Deak 	if (!intel_irqs_enabled(dev_priv)) {
34519dfe2e3aSImre Deak 		spin_unlock_irq(&dev_priv->irq_lock);
34529dfe2e3aSImre Deak 		return;
34539dfe2e3aSImre Deak 	}
34549dfe2e3aSImre Deak 
34556831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
34566831f3e3SVille Syrjälä 		GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
34579dfe2e3aSImre Deak 
3458aae8ba84SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3459aae8ba84SVille Syrjälä 
3460aae8ba84SVille Syrjälä 	/* make sure we're done processing display irqs */
346191c8a326SChris Wilson 	synchronize_irq(dev_priv->drm.irq);
3462aae8ba84SVille Syrjälä }
3463aae8ba84SVille Syrjälä 
34646bcdb1c8SVille Syrjälä static void cherryview_irq_reset(struct drm_device *dev)
346543f328d7SVille Syrjälä {
3466fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
346743f328d7SVille Syrjälä 
346843f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
346943f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
347043f328d7SVille Syrjälä 
3471d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
347243f328d7SVille Syrjälä 
34733488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(GEN8_PCU_);
347443f328d7SVille Syrjälä 
3475ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
34769918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
347770591a41SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3478ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
347943f328d7SVille Syrjälä }
348043f328d7SVille Syrjälä 
348191d14251STvrtko Ursulin static u32 intel_hpd_enabled_irqs(struct drm_i915_private *dev_priv,
348287a02106SVille Syrjälä 				  const u32 hpd[HPD_NUM_PINS])
348387a02106SVille Syrjälä {
348487a02106SVille Syrjälä 	struct intel_encoder *encoder;
348587a02106SVille Syrjälä 	u32 enabled_irqs = 0;
348687a02106SVille Syrjälä 
348791c8a326SChris Wilson 	for_each_intel_encoder(&dev_priv->drm, encoder)
348887a02106SVille Syrjälä 		if (dev_priv->hotplug.stats[encoder->hpd_pin].state == HPD_ENABLED)
348987a02106SVille Syrjälä 			enabled_irqs |= hpd[encoder->hpd_pin];
349087a02106SVille Syrjälä 
349187a02106SVille Syrjälä 	return enabled_irqs;
349287a02106SVille Syrjälä }
349387a02106SVille Syrjälä 
34941a56b1a2SImre Deak static void ibx_hpd_detection_setup(struct drm_i915_private *dev_priv)
34951a56b1a2SImre Deak {
34961a56b1a2SImre Deak 	u32 hotplug;
34971a56b1a2SImre Deak 
34981a56b1a2SImre Deak 	/*
34991a56b1a2SImre Deak 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
35001a56b1a2SImre Deak 	 * duration to 2ms (which is the minimum in the Display Port spec).
35011a56b1a2SImre Deak 	 * The pulse duration bits are reserved on LPT+.
35021a56b1a2SImre Deak 	 */
35031a56b1a2SImre Deak 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
35041a56b1a2SImre Deak 	hotplug &= ~(PORTB_PULSE_DURATION_MASK |
35051a56b1a2SImre Deak 		     PORTC_PULSE_DURATION_MASK |
35061a56b1a2SImre Deak 		     PORTD_PULSE_DURATION_MASK);
35071a56b1a2SImre Deak 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
35081a56b1a2SImre Deak 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
35091a56b1a2SImre Deak 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
35101a56b1a2SImre Deak 	/*
35111a56b1a2SImre Deak 	 * When CPU and PCH are on the same package, port A
35121a56b1a2SImre Deak 	 * HPD must be enabled in both north and south.
35131a56b1a2SImre Deak 	 */
35141a56b1a2SImre Deak 	if (HAS_PCH_LPT_LP(dev_priv))
35151a56b1a2SImre Deak 		hotplug |= PORTA_HOTPLUG_ENABLE;
35161a56b1a2SImre Deak 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
35171a56b1a2SImre Deak }
35181a56b1a2SImre Deak 
351991d14251STvrtko Ursulin static void ibx_hpd_irq_setup(struct drm_i915_private *dev_priv)
352082a28bcfSDaniel Vetter {
35211a56b1a2SImre Deak 	u32 hotplug_irqs, enabled_irqs;
352282a28bcfSDaniel Vetter 
352391d14251STvrtko Ursulin 	if (HAS_PCH_IBX(dev_priv)) {
3524fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
352591d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ibx);
352682a28bcfSDaniel Vetter 	} else {
3527fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
352891d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_cpt);
352982a28bcfSDaniel Vetter 	}
353082a28bcfSDaniel Vetter 
3531fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
353282a28bcfSDaniel Vetter 
35331a56b1a2SImre Deak 	ibx_hpd_detection_setup(dev_priv);
35346dbf30ceSVille Syrjälä }
353526951cafSXiong Zhang 
353631604222SAnusha Srivatsa static void icp_hpd_detection_setup(struct drm_i915_private *dev_priv)
353731604222SAnusha Srivatsa {
353831604222SAnusha Srivatsa 	u32 hotplug;
353931604222SAnusha Srivatsa 
354031604222SAnusha Srivatsa 	hotplug = I915_READ(SHOTPLUG_CTL_DDI);
354131604222SAnusha Srivatsa 	hotplug |= ICP_DDIA_HPD_ENABLE |
354231604222SAnusha Srivatsa 		   ICP_DDIB_HPD_ENABLE;
354331604222SAnusha Srivatsa 	I915_WRITE(SHOTPLUG_CTL_DDI, hotplug);
354431604222SAnusha Srivatsa 
354531604222SAnusha Srivatsa 	hotplug = I915_READ(SHOTPLUG_CTL_TC);
354631604222SAnusha Srivatsa 	hotplug |= ICP_TC_HPD_ENABLE(PORT_TC1) |
354731604222SAnusha Srivatsa 		   ICP_TC_HPD_ENABLE(PORT_TC2) |
354831604222SAnusha Srivatsa 		   ICP_TC_HPD_ENABLE(PORT_TC3) |
354931604222SAnusha Srivatsa 		   ICP_TC_HPD_ENABLE(PORT_TC4);
355031604222SAnusha Srivatsa 	I915_WRITE(SHOTPLUG_CTL_TC, hotplug);
355131604222SAnusha Srivatsa }
355231604222SAnusha Srivatsa 
355331604222SAnusha Srivatsa static void icp_hpd_irq_setup(struct drm_i915_private *dev_priv)
355431604222SAnusha Srivatsa {
355531604222SAnusha Srivatsa 	u32 hotplug_irqs, enabled_irqs;
355631604222SAnusha Srivatsa 
355731604222SAnusha Srivatsa 	hotplug_irqs = SDE_DDI_MASK_ICP | SDE_TC_MASK_ICP;
355831604222SAnusha Srivatsa 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_icp);
355931604222SAnusha Srivatsa 
356031604222SAnusha Srivatsa 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
356131604222SAnusha Srivatsa 
356231604222SAnusha Srivatsa 	icp_hpd_detection_setup(dev_priv);
356331604222SAnusha Srivatsa }
356431604222SAnusha Srivatsa 
3565121e758eSDhinakaran Pandiyan static void gen11_hpd_detection_setup(struct drm_i915_private *dev_priv)
3566121e758eSDhinakaran Pandiyan {
3567121e758eSDhinakaran Pandiyan 	u32 hotplug;
3568121e758eSDhinakaran Pandiyan 
3569121e758eSDhinakaran Pandiyan 	hotplug = I915_READ(GEN11_TC_HOTPLUG_CTL);
3570121e758eSDhinakaran Pandiyan 	hotplug |= GEN11_HOTPLUG_CTL_ENABLE(PORT_TC1) |
3571121e758eSDhinakaran Pandiyan 		   GEN11_HOTPLUG_CTL_ENABLE(PORT_TC2) |
3572121e758eSDhinakaran Pandiyan 		   GEN11_HOTPLUG_CTL_ENABLE(PORT_TC3) |
3573121e758eSDhinakaran Pandiyan 		   GEN11_HOTPLUG_CTL_ENABLE(PORT_TC4);
3574121e758eSDhinakaran Pandiyan 	I915_WRITE(GEN11_TC_HOTPLUG_CTL, hotplug);
3575b796b971SDhinakaran Pandiyan 
3576b796b971SDhinakaran Pandiyan 	hotplug = I915_READ(GEN11_TBT_HOTPLUG_CTL);
3577b796b971SDhinakaran Pandiyan 	hotplug |= GEN11_HOTPLUG_CTL_ENABLE(PORT_TC1) |
3578b796b971SDhinakaran Pandiyan 		   GEN11_HOTPLUG_CTL_ENABLE(PORT_TC2) |
3579b796b971SDhinakaran Pandiyan 		   GEN11_HOTPLUG_CTL_ENABLE(PORT_TC3) |
3580b796b971SDhinakaran Pandiyan 		   GEN11_HOTPLUG_CTL_ENABLE(PORT_TC4);
3581b796b971SDhinakaran Pandiyan 	I915_WRITE(GEN11_TBT_HOTPLUG_CTL, hotplug);
3582121e758eSDhinakaran Pandiyan }
3583121e758eSDhinakaran Pandiyan 
3584121e758eSDhinakaran Pandiyan static void gen11_hpd_irq_setup(struct drm_i915_private *dev_priv)
3585121e758eSDhinakaran Pandiyan {
3586121e758eSDhinakaran Pandiyan 	u32 hotplug_irqs, enabled_irqs;
3587121e758eSDhinakaran Pandiyan 	u32 val;
3588121e758eSDhinakaran Pandiyan 
3589b796b971SDhinakaran Pandiyan 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_gen11);
3590b796b971SDhinakaran Pandiyan 	hotplug_irqs = GEN11_DE_TC_HOTPLUG_MASK | GEN11_DE_TBT_HOTPLUG_MASK;
3591121e758eSDhinakaran Pandiyan 
3592121e758eSDhinakaran Pandiyan 	val = I915_READ(GEN11_DE_HPD_IMR);
3593121e758eSDhinakaran Pandiyan 	val &= ~hotplug_irqs;
3594121e758eSDhinakaran Pandiyan 	I915_WRITE(GEN11_DE_HPD_IMR, val);
3595121e758eSDhinakaran Pandiyan 	POSTING_READ(GEN11_DE_HPD_IMR);
3596121e758eSDhinakaran Pandiyan 
3597121e758eSDhinakaran Pandiyan 	gen11_hpd_detection_setup(dev_priv);
359831604222SAnusha Srivatsa 
359931604222SAnusha Srivatsa 	if (HAS_PCH_ICP(dev_priv))
360031604222SAnusha Srivatsa 		icp_hpd_irq_setup(dev_priv);
3601121e758eSDhinakaran Pandiyan }
3602121e758eSDhinakaran Pandiyan 
36032a57d9ccSImre Deak static void spt_hpd_detection_setup(struct drm_i915_private *dev_priv)
36042a57d9ccSImre Deak {
36053b92e263SRodrigo Vivi 	u32 val, hotplug;
36063b92e263SRodrigo Vivi 
36073b92e263SRodrigo Vivi 	/* Display WA #1179 WaHardHangonHotPlug: cnp */
36083b92e263SRodrigo Vivi 	if (HAS_PCH_CNP(dev_priv)) {
36093b92e263SRodrigo Vivi 		val = I915_READ(SOUTH_CHICKEN1);
36103b92e263SRodrigo Vivi 		val &= ~CHASSIS_CLK_REQ_DURATION_MASK;
36113b92e263SRodrigo Vivi 		val |= CHASSIS_CLK_REQ_DURATION(0xf);
36123b92e263SRodrigo Vivi 		I915_WRITE(SOUTH_CHICKEN1, val);
36133b92e263SRodrigo Vivi 	}
36142a57d9ccSImre Deak 
36152a57d9ccSImre Deak 	/* Enable digital hotplug on the PCH */
36162a57d9ccSImre Deak 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
36172a57d9ccSImre Deak 	hotplug |= PORTA_HOTPLUG_ENABLE |
36182a57d9ccSImre Deak 		   PORTB_HOTPLUG_ENABLE |
36192a57d9ccSImre Deak 		   PORTC_HOTPLUG_ENABLE |
36202a57d9ccSImre Deak 		   PORTD_HOTPLUG_ENABLE;
36212a57d9ccSImre Deak 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
36222a57d9ccSImre Deak 
36232a57d9ccSImre Deak 	hotplug = I915_READ(PCH_PORT_HOTPLUG2);
36242a57d9ccSImre Deak 	hotplug |= PORTE_HOTPLUG_ENABLE;
36252a57d9ccSImre Deak 	I915_WRITE(PCH_PORT_HOTPLUG2, hotplug);
36262a57d9ccSImre Deak }
36272a57d9ccSImre Deak 
362891d14251STvrtko Ursulin static void spt_hpd_irq_setup(struct drm_i915_private *dev_priv)
36296dbf30ceSVille Syrjälä {
36302a57d9ccSImre Deak 	u32 hotplug_irqs, enabled_irqs;
36316dbf30ceSVille Syrjälä 
36326dbf30ceSVille Syrjälä 	hotplug_irqs = SDE_HOTPLUG_MASK_SPT;
363391d14251STvrtko Ursulin 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_spt);
36346dbf30ceSVille Syrjälä 
36356dbf30ceSVille Syrjälä 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
36366dbf30ceSVille Syrjälä 
36372a57d9ccSImre Deak 	spt_hpd_detection_setup(dev_priv);
363826951cafSXiong Zhang }
36397fe0b973SKeith Packard 
36401a56b1a2SImre Deak static void ilk_hpd_detection_setup(struct drm_i915_private *dev_priv)
36411a56b1a2SImre Deak {
36421a56b1a2SImre Deak 	u32 hotplug;
36431a56b1a2SImre Deak 
36441a56b1a2SImre Deak 	/*
36451a56b1a2SImre Deak 	 * Enable digital hotplug on the CPU, and configure the DP short pulse
36461a56b1a2SImre Deak 	 * duration to 2ms (which is the minimum in the Display Port spec)
36471a56b1a2SImre Deak 	 * The pulse duration bits are reserved on HSW+.
36481a56b1a2SImre Deak 	 */
36491a56b1a2SImre Deak 	hotplug = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
36501a56b1a2SImre Deak 	hotplug &= ~DIGITAL_PORTA_PULSE_DURATION_MASK;
36511a56b1a2SImre Deak 	hotplug |= DIGITAL_PORTA_HOTPLUG_ENABLE |
36521a56b1a2SImre Deak 		   DIGITAL_PORTA_PULSE_DURATION_2ms;
36531a56b1a2SImre Deak 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, hotplug);
36541a56b1a2SImre Deak }
36551a56b1a2SImre Deak 
365691d14251STvrtko Ursulin static void ilk_hpd_irq_setup(struct drm_i915_private *dev_priv)
3657e4ce95aaSVille Syrjälä {
36581a56b1a2SImre Deak 	u32 hotplug_irqs, enabled_irqs;
3659e4ce95aaSVille Syrjälä 
366091d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 8) {
36613a3b3c7dSVille Syrjälä 		hotplug_irqs = GEN8_PORT_DP_A_HOTPLUG;
366291d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_bdw);
36633a3b3c7dSVille Syrjälä 
36643a3b3c7dSVille Syrjälä 		bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
366591d14251STvrtko Ursulin 	} else if (INTEL_GEN(dev_priv) >= 7) {
366623bb4cb5SVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG_IVB;
366791d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ivb);
36683a3b3c7dSVille Syrjälä 
36693a3b3c7dSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
367023bb4cb5SVille Syrjälä 	} else {
3671e4ce95aaSVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG;
367291d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ilk);
3673e4ce95aaSVille Syrjälä 
3674e4ce95aaSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
36753a3b3c7dSVille Syrjälä 	}
3676e4ce95aaSVille Syrjälä 
36771a56b1a2SImre Deak 	ilk_hpd_detection_setup(dev_priv);
3678e4ce95aaSVille Syrjälä 
367991d14251STvrtko Ursulin 	ibx_hpd_irq_setup(dev_priv);
3680e4ce95aaSVille Syrjälä }
3681e4ce95aaSVille Syrjälä 
36822a57d9ccSImre Deak static void __bxt_hpd_detection_setup(struct drm_i915_private *dev_priv,
36832a57d9ccSImre Deak 				      u32 enabled_irqs)
3684e0a20ad7SShashank Sharma {
36852a57d9ccSImre Deak 	u32 hotplug;
3686e0a20ad7SShashank Sharma 
3687a52bb15bSVille Syrjälä 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
36882a57d9ccSImre Deak 	hotplug |= PORTA_HOTPLUG_ENABLE |
36892a57d9ccSImre Deak 		   PORTB_HOTPLUG_ENABLE |
36902a57d9ccSImre Deak 		   PORTC_HOTPLUG_ENABLE;
3691d252bf68SShubhangi Shrivastava 
3692d252bf68SShubhangi Shrivastava 	DRM_DEBUG_KMS("Invert bit setting: hp_ctl:%x hp_port:%x\n",
3693d252bf68SShubhangi Shrivastava 		      hotplug, enabled_irqs);
3694d252bf68SShubhangi Shrivastava 	hotplug &= ~BXT_DDI_HPD_INVERT_MASK;
3695d252bf68SShubhangi Shrivastava 
3696d252bf68SShubhangi Shrivastava 	/*
3697d252bf68SShubhangi Shrivastava 	 * For BXT invert bit has to be set based on AOB design
3698d252bf68SShubhangi Shrivastava 	 * for HPD detection logic, update it based on VBT fields.
3699d252bf68SShubhangi Shrivastava 	 */
3700d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIA) &&
3701d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_A))
3702d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIA_HPD_INVERT;
3703d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIB) &&
3704d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_B))
3705d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIB_HPD_INVERT;
3706d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIC) &&
3707d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_C))
3708d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIC_HPD_INVERT;
3709d252bf68SShubhangi Shrivastava 
3710a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
3711e0a20ad7SShashank Sharma }
3712e0a20ad7SShashank Sharma 
37132a57d9ccSImre Deak static void bxt_hpd_detection_setup(struct drm_i915_private *dev_priv)
37142a57d9ccSImre Deak {
37152a57d9ccSImre Deak 	__bxt_hpd_detection_setup(dev_priv, BXT_DE_PORT_HOTPLUG_MASK);
37162a57d9ccSImre Deak }
37172a57d9ccSImre Deak 
37182a57d9ccSImre Deak static void bxt_hpd_irq_setup(struct drm_i915_private *dev_priv)
37192a57d9ccSImre Deak {
37202a57d9ccSImre Deak 	u32 hotplug_irqs, enabled_irqs;
37212a57d9ccSImre Deak 
37222a57d9ccSImre Deak 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_bxt);
37232a57d9ccSImre Deak 	hotplug_irqs = BXT_DE_PORT_HOTPLUG_MASK;
37242a57d9ccSImre Deak 
37252a57d9ccSImre Deak 	bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
37262a57d9ccSImre Deak 
37272a57d9ccSImre Deak 	__bxt_hpd_detection_setup(dev_priv, enabled_irqs);
37282a57d9ccSImre Deak }
37292a57d9ccSImre Deak 
3730d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
3731d46da437SPaulo Zanoni {
3732fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
373382a28bcfSDaniel Vetter 	u32 mask;
3734d46da437SPaulo Zanoni 
37356e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
3736692a04cfSDaniel Vetter 		return;
3737692a04cfSDaniel Vetter 
37386e266956STvrtko Ursulin 	if (HAS_PCH_IBX(dev_priv))
37395c673b60SDaniel Vetter 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
37404ebc6509SDhinakaran Pandiyan 	else if (HAS_PCH_CPT(dev_priv) || HAS_PCH_LPT(dev_priv))
37415c673b60SDaniel Vetter 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
37424ebc6509SDhinakaran Pandiyan 	else
37434ebc6509SDhinakaran Pandiyan 		mask = SDE_GMBUS_CPT;
37448664281bSPaulo Zanoni 
37453488d4ebSVille Syrjälä 	gen3_assert_iir_is_zero(dev_priv, SDEIIR);
3746d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
37472a57d9ccSImre Deak 
37482a57d9ccSImre Deak 	if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
37492a57d9ccSImre Deak 	    HAS_PCH_LPT(dev_priv))
37501a56b1a2SImre Deak 		ibx_hpd_detection_setup(dev_priv);
37512a57d9ccSImre Deak 	else
37522a57d9ccSImre Deak 		spt_hpd_detection_setup(dev_priv);
3753d46da437SPaulo Zanoni }
3754d46da437SPaulo Zanoni 
37550a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
37560a9a8c91SDaniel Vetter {
3757fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
37580a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
37590a9a8c91SDaniel Vetter 
37600a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
37610a9a8c91SDaniel Vetter 
37620a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
37633c9192bcSTvrtko Ursulin 	if (HAS_L3_DPF(dev_priv)) {
37640a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
3765772c2a51STvrtko Ursulin 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev_priv);
3766772c2a51STvrtko Ursulin 		gt_irqs |= GT_PARITY_ERROR(dev_priv);
37670a9a8c91SDaniel Vetter 	}
37680a9a8c91SDaniel Vetter 
37690a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
3770cf819effSLucas De Marchi 	if (IS_GEN(dev_priv, 5)) {
3771f8973c21SChris Wilson 		gt_irqs |= ILK_BSD_USER_INTERRUPT;
37720a9a8c91SDaniel Vetter 	} else {
37730a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
37740a9a8c91SDaniel Vetter 	}
37750a9a8c91SDaniel Vetter 
37763488d4ebSVille Syrjälä 	GEN3_IRQ_INIT(GT, dev_priv->gt_irq_mask, gt_irqs);
37770a9a8c91SDaniel Vetter 
3778b243f530STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 6) {
377978e68d36SImre Deak 		/*
378078e68d36SImre Deak 		 * RPS interrupts will get enabled/disabled on demand when RPS
378178e68d36SImre Deak 		 * itself is enabled/disabled.
378278e68d36SImre Deak 		 */
3783*8a68d464SChris Wilson 		if (HAS_ENGINE(dev_priv, VECS0)) {
37840a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
3785f4e9af4fSAkash Goel 			dev_priv->pm_ier |= PM_VEBOX_USER_INTERRUPT;
3786f4e9af4fSAkash Goel 		}
37870a9a8c91SDaniel Vetter 
3788f4e9af4fSAkash Goel 		dev_priv->pm_imr = 0xffffffff;
37893488d4ebSVille Syrjälä 		GEN3_IRQ_INIT(GEN6_PM, dev_priv->pm_imr, pm_irqs);
37900a9a8c91SDaniel Vetter 	}
37910a9a8c91SDaniel Vetter }
37920a9a8c91SDaniel Vetter 
3793f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
3794036a4a7dSZhenyu Wang {
3795fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
37968e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
37978e76f8dcSPaulo Zanoni 
3798b243f530STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 7) {
37998e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
3800842ebf7aSVille Syrjälä 				DE_PCH_EVENT_IVB | DE_AUX_CHANNEL_A_IVB);
38018e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
380223bb4cb5SVille Syrjälä 			      DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB |
380323bb4cb5SVille Syrjälä 			      DE_DP_A_HOTPLUG_IVB);
38048e76f8dcSPaulo Zanoni 	} else {
38058e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3806842ebf7aSVille Syrjälä 				DE_AUX_CHANNEL_A | DE_PIPEB_CRC_DONE |
3807842ebf7aSVille Syrjälä 				DE_PIPEA_CRC_DONE | DE_POISON);
3808e4ce95aaSVille Syrjälä 		extra_mask = (DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
3809e4ce95aaSVille Syrjälä 			      DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN |
3810e4ce95aaSVille Syrjälä 			      DE_DP_A_HOTPLUG);
38118e76f8dcSPaulo Zanoni 	}
3812036a4a7dSZhenyu Wang 
3813fc340442SDaniel Vetter 	if (IS_HASWELL(dev_priv)) {
3814fc340442SDaniel Vetter 		gen3_assert_iir_is_zero(dev_priv, EDP_PSR_IIR);
38151aeb1b5fSDhinakaran Pandiyan 		intel_psr_irq_control(dev_priv, dev_priv->psr.debug);
3816fc340442SDaniel Vetter 		display_mask |= DE_EDP_PSR_INT_HSW;
3817fc340442SDaniel Vetter 	}
3818fc340442SDaniel Vetter 
38191ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3820036a4a7dSZhenyu Wang 
3821622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3822622364b6SPaulo Zanoni 
38233488d4ebSVille Syrjälä 	GEN3_IRQ_INIT(DE, dev_priv->irq_mask, display_mask | extra_mask);
3824036a4a7dSZhenyu Wang 
38250a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
3826036a4a7dSZhenyu Wang 
38271a56b1a2SImre Deak 	ilk_hpd_detection_setup(dev_priv);
38281a56b1a2SImre Deak 
3829d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
38307fe0b973SKeith Packard 
383150a0bc90STvrtko Ursulin 	if (IS_IRONLAKE_M(dev_priv)) {
38326005ce42SDaniel Vetter 		/* Enable PCU event interrupts
38336005ce42SDaniel Vetter 		 *
38346005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
38354bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
38364bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
3837d6207435SDaniel Vetter 		spin_lock_irq(&dev_priv->irq_lock);
3838fbdedaeaSVille Syrjälä 		ilk_enable_display_irq(dev_priv, DE_PCU_EVENT);
3839d6207435SDaniel Vetter 		spin_unlock_irq(&dev_priv->irq_lock);
3840f97108d1SJesse Barnes 	}
3841f97108d1SJesse Barnes 
3842036a4a7dSZhenyu Wang 	return 0;
3843036a4a7dSZhenyu Wang }
3844036a4a7dSZhenyu Wang 
3845f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3846f8b79e58SImre Deak {
384767520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
3848f8b79e58SImre Deak 
3849f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3850f8b79e58SImre Deak 		return;
3851f8b79e58SImre Deak 
3852f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3853f8b79e58SImre Deak 
3854d6c69803SVille Syrjälä 	if (intel_irqs_enabled(dev_priv)) {
3855d6c69803SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3856ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3857f8b79e58SImre Deak 	}
3858d6c69803SVille Syrjälä }
3859f8b79e58SImre Deak 
3860f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3861f8b79e58SImre Deak {
386267520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
3863f8b79e58SImre Deak 
3864f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3865f8b79e58SImre Deak 		return;
3866f8b79e58SImre Deak 
3867f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3868f8b79e58SImre Deak 
3869950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3870ad22d106SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3871f8b79e58SImre Deak }
3872f8b79e58SImre Deak 
38730e6c9a9eSVille Syrjälä 
38740e6c9a9eSVille Syrjälä static int valleyview_irq_postinstall(struct drm_device *dev)
38750e6c9a9eSVille Syrjälä {
3876fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
38770e6c9a9eSVille Syrjälä 
38780a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
38797e231dbeSJesse Barnes 
3880ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
38819918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3882ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3883ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3884ad22d106SVille Syrjälä 
38857e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
388634c7b8a7SVille Syrjälä 	POSTING_READ(VLV_MASTER_IER);
388720afbda2SDaniel Vetter 
388820afbda2SDaniel Vetter 	return 0;
388920afbda2SDaniel Vetter }
389020afbda2SDaniel Vetter 
3891abd58f01SBen Widawsky static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
3892abd58f01SBen Widawsky {
3893abd58f01SBen Widawsky 	/* These are interrupts we'll toggle with the ring mask register */
3894a9c287c9SJani Nikula 	u32 gt_interrupts[] = {
3895*8a68d464SChris Wilson 		(GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
389673d477f6SOscar Mateo 		 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
389773d477f6SOscar Mateo 		 GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT |
3898*8a68d464SChris Wilson 		 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT),
3899*8a68d464SChris Wilson 
3900*8a68d464SChris Wilson 		(GT_RENDER_USER_INTERRUPT << GEN8_VCS0_IRQ_SHIFT |
3901*8a68d464SChris Wilson 		 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS0_IRQ_SHIFT |
3902abd58f01SBen Widawsky 		 GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
3903*8a68d464SChris Wilson 		 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT),
3904*8a68d464SChris Wilson 
3905abd58f01SBen Widawsky 		0,
3906*8a68d464SChris Wilson 
3907*8a68d464SChris Wilson 		(GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT |
3908*8a68d464SChris Wilson 		 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT)
3909abd58f01SBen Widawsky 	};
3910abd58f01SBen Widawsky 
3911f4e9af4fSAkash Goel 	dev_priv->pm_ier = 0x0;
3912f4e9af4fSAkash Goel 	dev_priv->pm_imr = ~dev_priv->pm_ier;
39139a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 0, ~gt_interrupts[0], gt_interrupts[0]);
39149a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 1, ~gt_interrupts[1], gt_interrupts[1]);
391578e68d36SImre Deak 	/*
391678e68d36SImre Deak 	 * RPS interrupts will get enabled/disabled on demand when RPS itself
391726705e20SSagar Arun Kamble 	 * is enabled/disabled. Same wil be the case for GuC interrupts.
391878e68d36SImre Deak 	 */
3919f4e9af4fSAkash Goel 	GEN8_IRQ_INIT_NDX(GT, 2, dev_priv->pm_imr, dev_priv->pm_ier);
39209a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 3, ~gt_interrupts[3], gt_interrupts[3]);
3921abd58f01SBen Widawsky }
3922abd58f01SBen Widawsky 
3923abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3924abd58f01SBen Widawsky {
3925a9c287c9SJani Nikula 	u32 de_pipe_masked = GEN8_PIPE_CDCLK_CRC_DONE;
3926a9c287c9SJani Nikula 	u32 de_pipe_enables;
39273a3b3c7dSVille Syrjälä 	u32 de_port_masked = GEN8_AUX_CHANNEL_A;
39283a3b3c7dSVille Syrjälä 	u32 de_port_enables;
3929df0d28c1SDhinakaran Pandiyan 	u32 de_misc_masked = GEN8_DE_EDP_PSR;
39303a3b3c7dSVille Syrjälä 	enum pipe pipe;
3931770de83dSDamien Lespiau 
3932df0d28c1SDhinakaran Pandiyan 	if (INTEL_GEN(dev_priv) <= 10)
3933df0d28c1SDhinakaran Pandiyan 		de_misc_masked |= GEN8_DE_MISC_GSE;
3934df0d28c1SDhinakaran Pandiyan 
3935bca2bf2aSPandiyan, Dhinakaran 	if (INTEL_GEN(dev_priv) >= 9) {
3936842ebf7aSVille Syrjälä 		de_pipe_masked |= GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
39373a3b3c7dSVille Syrjälä 		de_port_masked |= GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
393888e04703SJesse Barnes 				  GEN9_AUX_CHANNEL_D;
3939cc3f90f0SAnder Conselvan de Oliveira 		if (IS_GEN9_LP(dev_priv))
39403a3b3c7dSVille Syrjälä 			de_port_masked |= BXT_DE_PORT_GMBUS;
39413a3b3c7dSVille Syrjälä 	} else {
3942842ebf7aSVille Syrjälä 		de_pipe_masked |= GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
39433a3b3c7dSVille Syrjälä 	}
3944770de83dSDamien Lespiau 
3945bb187e93SJames Ausmus 	if (INTEL_GEN(dev_priv) >= 11)
3946bb187e93SJames Ausmus 		de_port_masked |= ICL_AUX_CHANNEL_E;
3947bb187e93SJames Ausmus 
39489bb635d9SDhinakaran Pandiyan 	if (IS_CNL_WITH_PORT_F(dev_priv) || INTEL_GEN(dev_priv) >= 11)
3949a324fcacSRodrigo Vivi 		de_port_masked |= CNL_AUX_CHANNEL_F;
3950a324fcacSRodrigo Vivi 
3951770de83dSDamien Lespiau 	de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
3952770de83dSDamien Lespiau 					   GEN8_PIPE_FIFO_UNDERRUN;
3953770de83dSDamien Lespiau 
39543a3b3c7dSVille Syrjälä 	de_port_enables = de_port_masked;
3955cc3f90f0SAnder Conselvan de Oliveira 	if (IS_GEN9_LP(dev_priv))
3956a52bb15bSVille Syrjälä 		de_port_enables |= BXT_DE_PORT_HOTPLUG_MASK;
3957a52bb15bSVille Syrjälä 	else if (IS_BROADWELL(dev_priv))
39583a3b3c7dSVille Syrjälä 		de_port_enables |= GEN8_PORT_DP_A_HOTPLUG;
39593a3b3c7dSVille Syrjälä 
3960e04f7eceSVille Syrjälä 	gen3_assert_iir_is_zero(dev_priv, EDP_PSR_IIR);
396154fd3149SDhinakaran Pandiyan 	intel_psr_irq_control(dev_priv, dev_priv->psr.debug);
3962e04f7eceSVille Syrjälä 
39630a195c02SMika Kahola 	for_each_pipe(dev_priv, pipe) {
39640a195c02SMika Kahola 		dev_priv->de_irq_mask[pipe] = ~de_pipe_masked;
3965abd58f01SBen Widawsky 
3966f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3967813bde43SPaulo Zanoni 				POWER_DOMAIN_PIPE(pipe)))
3968813bde43SPaulo Zanoni 			GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
3969813bde43SPaulo Zanoni 					  dev_priv->de_irq_mask[pipe],
397035079899SPaulo Zanoni 					  de_pipe_enables);
39710a195c02SMika Kahola 	}
3972abd58f01SBen Widawsky 
39733488d4ebSVille Syrjälä 	GEN3_IRQ_INIT(GEN8_DE_PORT_, ~de_port_masked, de_port_enables);
39743488d4ebSVille Syrjälä 	GEN3_IRQ_INIT(GEN8_DE_MISC_, ~de_misc_masked, de_misc_masked);
39752a57d9ccSImre Deak 
3976121e758eSDhinakaran Pandiyan 	if (INTEL_GEN(dev_priv) >= 11) {
3977121e758eSDhinakaran Pandiyan 		u32 de_hpd_masked = 0;
3978b796b971SDhinakaran Pandiyan 		u32 de_hpd_enables = GEN11_DE_TC_HOTPLUG_MASK |
3979b796b971SDhinakaran Pandiyan 				     GEN11_DE_TBT_HOTPLUG_MASK;
3980121e758eSDhinakaran Pandiyan 
3981121e758eSDhinakaran Pandiyan 		GEN3_IRQ_INIT(GEN11_DE_HPD_, ~de_hpd_masked, de_hpd_enables);
3982121e758eSDhinakaran Pandiyan 		gen11_hpd_detection_setup(dev_priv);
3983121e758eSDhinakaran Pandiyan 	} else if (IS_GEN9_LP(dev_priv)) {
39842a57d9ccSImre Deak 		bxt_hpd_detection_setup(dev_priv);
3985121e758eSDhinakaran Pandiyan 	} else if (IS_BROADWELL(dev_priv)) {
39861a56b1a2SImre Deak 		ilk_hpd_detection_setup(dev_priv);
3987abd58f01SBen Widawsky 	}
3988121e758eSDhinakaran Pandiyan }
3989abd58f01SBen Widawsky 
3990abd58f01SBen Widawsky static int gen8_irq_postinstall(struct drm_device *dev)
3991abd58f01SBen Widawsky {
3992fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3993abd58f01SBen Widawsky 
39946e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
3995622364b6SPaulo Zanoni 		ibx_irq_pre_postinstall(dev);
3996622364b6SPaulo Zanoni 
3997abd58f01SBen Widawsky 	gen8_gt_irq_postinstall(dev_priv);
3998abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3999abd58f01SBen Widawsky 
40006e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
4001abd58f01SBen Widawsky 		ibx_irq_postinstall(dev);
4002abd58f01SBen Widawsky 
40034376b9c9SMika Kuoppala 	gen8_master_intr_enable(dev_priv->regs);
4004abd58f01SBen Widawsky 
4005abd58f01SBen Widawsky 	return 0;
4006abd58f01SBen Widawsky }
4007abd58f01SBen Widawsky 
400851951ae7SMika Kuoppala static void gen11_gt_irq_postinstall(struct drm_i915_private *dev_priv)
400951951ae7SMika Kuoppala {
401051951ae7SMika Kuoppala 	const u32 irqs = GT_RENDER_USER_INTERRUPT | GT_CONTEXT_SWITCH_INTERRUPT;
401151951ae7SMika Kuoppala 
401251951ae7SMika Kuoppala 	BUILD_BUG_ON(irqs & 0xffff0000);
401351951ae7SMika Kuoppala 
401451951ae7SMika Kuoppala 	/* Enable RCS, BCS, VCS and VECS class interrupts. */
401551951ae7SMika Kuoppala 	I915_WRITE(GEN11_RENDER_COPY_INTR_ENABLE, irqs << 16 | irqs);
401651951ae7SMika Kuoppala 	I915_WRITE(GEN11_VCS_VECS_INTR_ENABLE,	  irqs << 16 | irqs);
401751951ae7SMika Kuoppala 
401851951ae7SMika Kuoppala 	/* Unmask irqs on RCS, BCS, VCS and VECS engines. */
401951951ae7SMika Kuoppala 	I915_WRITE(GEN11_RCS0_RSVD_INTR_MASK,	~(irqs << 16));
402051951ae7SMika Kuoppala 	I915_WRITE(GEN11_BCS_RSVD_INTR_MASK,	~(irqs << 16));
402151951ae7SMika Kuoppala 	I915_WRITE(GEN11_VCS0_VCS1_INTR_MASK,	~(irqs | irqs << 16));
402251951ae7SMika Kuoppala 	I915_WRITE(GEN11_VCS2_VCS3_INTR_MASK,	~(irqs | irqs << 16));
402351951ae7SMika Kuoppala 	I915_WRITE(GEN11_VECS0_VECS1_INTR_MASK,	~(irqs | irqs << 16));
402451951ae7SMika Kuoppala 
4025d02b98b8SOscar Mateo 	/*
4026d02b98b8SOscar Mateo 	 * RPS interrupts will get enabled/disabled on demand when RPS itself
4027d02b98b8SOscar Mateo 	 * is enabled/disabled.
4028d02b98b8SOscar Mateo 	 */
4029d02b98b8SOscar Mateo 	dev_priv->pm_ier = 0x0;
4030d02b98b8SOscar Mateo 	dev_priv->pm_imr = ~dev_priv->pm_ier;
4031d02b98b8SOscar Mateo 	I915_WRITE(GEN11_GPM_WGBOXPERF_INTR_ENABLE, 0);
4032d02b98b8SOscar Mateo 	I915_WRITE(GEN11_GPM_WGBOXPERF_INTR_MASK,  ~0);
403351951ae7SMika Kuoppala }
403451951ae7SMika Kuoppala 
403531604222SAnusha Srivatsa static void icp_irq_postinstall(struct drm_device *dev)
403631604222SAnusha Srivatsa {
403731604222SAnusha Srivatsa 	struct drm_i915_private *dev_priv = to_i915(dev);
403831604222SAnusha Srivatsa 	u32 mask = SDE_GMBUS_ICP;
403931604222SAnusha Srivatsa 
404031604222SAnusha Srivatsa 	WARN_ON(I915_READ(SDEIER) != 0);
404131604222SAnusha Srivatsa 	I915_WRITE(SDEIER, 0xffffffff);
404231604222SAnusha Srivatsa 	POSTING_READ(SDEIER);
404331604222SAnusha Srivatsa 
404431604222SAnusha Srivatsa 	gen3_assert_iir_is_zero(dev_priv, SDEIIR);
404531604222SAnusha Srivatsa 	I915_WRITE(SDEIMR, ~mask);
404631604222SAnusha Srivatsa 
404731604222SAnusha Srivatsa 	icp_hpd_detection_setup(dev_priv);
404831604222SAnusha Srivatsa }
404931604222SAnusha Srivatsa 
405051951ae7SMika Kuoppala static int gen11_irq_postinstall(struct drm_device *dev)
405151951ae7SMika Kuoppala {
405251951ae7SMika Kuoppala 	struct drm_i915_private *dev_priv = dev->dev_private;
4053df0d28c1SDhinakaran Pandiyan 	u32 gu_misc_masked = GEN11_GU_MISC_GSE;
405451951ae7SMika Kuoppala 
405531604222SAnusha Srivatsa 	if (HAS_PCH_ICP(dev_priv))
405631604222SAnusha Srivatsa 		icp_irq_postinstall(dev);
405731604222SAnusha Srivatsa 
405851951ae7SMika Kuoppala 	gen11_gt_irq_postinstall(dev_priv);
405951951ae7SMika Kuoppala 	gen8_de_irq_postinstall(dev_priv);
406051951ae7SMika Kuoppala 
4061df0d28c1SDhinakaran Pandiyan 	GEN3_IRQ_INIT(GEN11_GU_MISC_, ~gu_misc_masked, gu_misc_masked);
4062df0d28c1SDhinakaran Pandiyan 
406351951ae7SMika Kuoppala 	I915_WRITE(GEN11_DISPLAY_INT_CTL, GEN11_DISPLAY_IRQ_ENABLE);
406451951ae7SMika Kuoppala 
406581067b71SMika Kuoppala 	gen11_master_intr_enable(dev_priv->regs);
4066c25f0c6aSDaniele Ceraolo Spurio 	POSTING_READ(GEN11_GFX_MSTR_IRQ);
406751951ae7SMika Kuoppala 
406851951ae7SMika Kuoppala 	return 0;
406951951ae7SMika Kuoppala }
407051951ae7SMika Kuoppala 
407143f328d7SVille Syrjälä static int cherryview_irq_postinstall(struct drm_device *dev)
407243f328d7SVille Syrjälä {
4073fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
407443f328d7SVille Syrjälä 
407543f328d7SVille Syrjälä 	gen8_gt_irq_postinstall(dev_priv);
407643f328d7SVille Syrjälä 
4077ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
40789918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
4079ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
4080ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
4081ad22d106SVille Syrjälä 
4082e5328c43SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
408343f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
408443f328d7SVille Syrjälä 
408543f328d7SVille Syrjälä 	return 0;
408643f328d7SVille Syrjälä }
408743f328d7SVille Syrjälä 
40886bcdb1c8SVille Syrjälä static void i8xx_irq_reset(struct drm_device *dev)
4089c2798b19SChris Wilson {
4090fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4091c2798b19SChris Wilson 
409244d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
409344d9241eSVille Syrjälä 
4094e9e9848aSVille Syrjälä 	GEN2_IRQ_RESET();
4095c2798b19SChris Wilson }
4096c2798b19SChris Wilson 
4097c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
4098c2798b19SChris Wilson {
4099fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4100e9e9848aSVille Syrjälä 	u16 enable_mask;
4101c2798b19SChris Wilson 
4102045cebd2SVille Syrjälä 	I915_WRITE16(EMR, ~(I915_ERROR_PAGE_TABLE |
4103045cebd2SVille Syrjälä 			    I915_ERROR_MEMORY_REFRESH));
4104c2798b19SChris Wilson 
4105c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
4106c2798b19SChris Wilson 	dev_priv->irq_mask =
4107c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
410816659bc5SVille Syrjälä 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
410916659bc5SVille Syrjälä 		  I915_MASTER_ERROR_INTERRUPT);
4110c2798b19SChris Wilson 
4111e9e9848aSVille Syrjälä 	enable_mask =
4112c2798b19SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4113c2798b19SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
411416659bc5SVille Syrjälä 		I915_MASTER_ERROR_INTERRUPT |
4115e9e9848aSVille Syrjälä 		I915_USER_INTERRUPT;
4116e9e9848aSVille Syrjälä 
4117e9e9848aSVille Syrjälä 	GEN2_IRQ_INIT(, dev_priv->irq_mask, enable_mask);
4118c2798b19SChris Wilson 
4119379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4120379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4121d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4122755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4123755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4124d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4125379ef82dSDaniel Vetter 
4126c2798b19SChris Wilson 	return 0;
4127c2798b19SChris Wilson }
4128c2798b19SChris Wilson 
412978c357ddSVille Syrjälä static void i8xx_error_irq_ack(struct drm_i915_private *dev_priv,
413078c357ddSVille Syrjälä 			       u16 *eir, u16 *eir_stuck)
413178c357ddSVille Syrjälä {
413278c357ddSVille Syrjälä 	u16 emr;
413378c357ddSVille Syrjälä 
413478c357ddSVille Syrjälä 	*eir = I915_READ16(EIR);
413578c357ddSVille Syrjälä 
413678c357ddSVille Syrjälä 	if (*eir)
413778c357ddSVille Syrjälä 		I915_WRITE16(EIR, *eir);
413878c357ddSVille Syrjälä 
413978c357ddSVille Syrjälä 	*eir_stuck = I915_READ16(EIR);
414078c357ddSVille Syrjälä 	if (*eir_stuck == 0)
414178c357ddSVille Syrjälä 		return;
414278c357ddSVille Syrjälä 
414378c357ddSVille Syrjälä 	/*
414478c357ddSVille Syrjälä 	 * Toggle all EMR bits to make sure we get an edge
414578c357ddSVille Syrjälä 	 * in the ISR master error bit if we don't clear
414678c357ddSVille Syrjälä 	 * all the EIR bits. Otherwise the edge triggered
414778c357ddSVille Syrjälä 	 * IIR on i965/g4x wouldn't notice that an interrupt
414878c357ddSVille Syrjälä 	 * is still pending. Also some EIR bits can't be
414978c357ddSVille Syrjälä 	 * cleared except by handling the underlying error
415078c357ddSVille Syrjälä 	 * (or by a GPU reset) so we mask any bit that
415178c357ddSVille Syrjälä 	 * remains set.
415278c357ddSVille Syrjälä 	 */
415378c357ddSVille Syrjälä 	emr = I915_READ16(EMR);
415478c357ddSVille Syrjälä 	I915_WRITE16(EMR, 0xffff);
415578c357ddSVille Syrjälä 	I915_WRITE16(EMR, emr | *eir_stuck);
415678c357ddSVille Syrjälä }
415778c357ddSVille Syrjälä 
415878c357ddSVille Syrjälä static void i8xx_error_irq_handler(struct drm_i915_private *dev_priv,
415978c357ddSVille Syrjälä 				   u16 eir, u16 eir_stuck)
416078c357ddSVille Syrjälä {
416178c357ddSVille Syrjälä 	DRM_DEBUG("Master Error: EIR 0x%04x\n", eir);
416278c357ddSVille Syrjälä 
416378c357ddSVille Syrjälä 	if (eir_stuck)
416478c357ddSVille Syrjälä 		DRM_DEBUG_DRIVER("EIR stuck: 0x%04x, masked\n", eir_stuck);
416578c357ddSVille Syrjälä }
416678c357ddSVille Syrjälä 
416778c357ddSVille Syrjälä static void i9xx_error_irq_ack(struct drm_i915_private *dev_priv,
416878c357ddSVille Syrjälä 			       u32 *eir, u32 *eir_stuck)
416978c357ddSVille Syrjälä {
417078c357ddSVille Syrjälä 	u32 emr;
417178c357ddSVille Syrjälä 
417278c357ddSVille Syrjälä 	*eir = I915_READ(EIR);
417378c357ddSVille Syrjälä 
417478c357ddSVille Syrjälä 	I915_WRITE(EIR, *eir);
417578c357ddSVille Syrjälä 
417678c357ddSVille Syrjälä 	*eir_stuck = I915_READ(EIR);
417778c357ddSVille Syrjälä 	if (*eir_stuck == 0)
417878c357ddSVille Syrjälä 		return;
417978c357ddSVille Syrjälä 
418078c357ddSVille Syrjälä 	/*
418178c357ddSVille Syrjälä 	 * Toggle all EMR bits to make sure we get an edge
418278c357ddSVille Syrjälä 	 * in the ISR master error bit if we don't clear
418378c357ddSVille Syrjälä 	 * all the EIR bits. Otherwise the edge triggered
418478c357ddSVille Syrjälä 	 * IIR on i965/g4x wouldn't notice that an interrupt
418578c357ddSVille Syrjälä 	 * is still pending. Also some EIR bits can't be
418678c357ddSVille Syrjälä 	 * cleared except by handling the underlying error
418778c357ddSVille Syrjälä 	 * (or by a GPU reset) so we mask any bit that
418878c357ddSVille Syrjälä 	 * remains set.
418978c357ddSVille Syrjälä 	 */
419078c357ddSVille Syrjälä 	emr = I915_READ(EMR);
419178c357ddSVille Syrjälä 	I915_WRITE(EMR, 0xffffffff);
419278c357ddSVille Syrjälä 	I915_WRITE(EMR, emr | *eir_stuck);
419378c357ddSVille Syrjälä }
419478c357ddSVille Syrjälä 
419578c357ddSVille Syrjälä static void i9xx_error_irq_handler(struct drm_i915_private *dev_priv,
419678c357ddSVille Syrjälä 				   u32 eir, u32 eir_stuck)
419778c357ddSVille Syrjälä {
419878c357ddSVille Syrjälä 	DRM_DEBUG("Master Error, EIR 0x%08x\n", eir);
419978c357ddSVille Syrjälä 
420078c357ddSVille Syrjälä 	if (eir_stuck)
420178c357ddSVille Syrjälä 		DRM_DEBUG_DRIVER("EIR stuck: 0x%08x, masked\n", eir_stuck);
420278c357ddSVille Syrjälä }
420378c357ddSVille Syrjälä 
4204ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
4205c2798b19SChris Wilson {
420645a83f84SDaniel Vetter 	struct drm_device *dev = arg;
4207fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4208af722d28SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
4209c2798b19SChris Wilson 
42102dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
42112dd2a883SImre Deak 		return IRQ_NONE;
42122dd2a883SImre Deak 
42131f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
42141f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
42151f814dacSImre Deak 
4216af722d28SVille Syrjälä 	do {
4217af722d28SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
421878c357ddSVille Syrjälä 		u16 eir = 0, eir_stuck = 0;
4219af722d28SVille Syrjälä 		u16 iir;
4220af722d28SVille Syrjälä 
4221c2798b19SChris Wilson 		iir = I915_READ16(IIR);
4222c2798b19SChris Wilson 		if (iir == 0)
4223af722d28SVille Syrjälä 			break;
4224c2798b19SChris Wilson 
4225af722d28SVille Syrjälä 		ret = IRQ_HANDLED;
4226c2798b19SChris Wilson 
4227eb64343cSVille Syrjälä 		/* Call regardless, as some status bits might not be
4228eb64343cSVille Syrjälä 		 * signalled in iir */
4229eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
4230c2798b19SChris Wilson 
423178c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
423278c357ddSVille Syrjälä 			i8xx_error_irq_ack(dev_priv, &eir, &eir_stuck);
423378c357ddSVille Syrjälä 
4234fd3a4024SDaniel Vetter 		I915_WRITE16(IIR, iir);
4235c2798b19SChris Wilson 
4236c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
4237*8a68d464SChris Wilson 			intel_engine_breadcrumbs_irq(dev_priv->engine[RCS0]);
4238c2798b19SChris Wilson 
423978c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
424078c357ddSVille Syrjälä 			i8xx_error_irq_handler(dev_priv, eir, eir_stuck);
4241af722d28SVille Syrjälä 
4242eb64343cSVille Syrjälä 		i8xx_pipestat_irq_handler(dev_priv, iir, pipe_stats);
4243af722d28SVille Syrjälä 	} while (0);
4244c2798b19SChris Wilson 
42451f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
42461f814dacSImre Deak 
42471f814dacSImre Deak 	return ret;
4248c2798b19SChris Wilson }
4249c2798b19SChris Wilson 
42506bcdb1c8SVille Syrjälä static void i915_irq_reset(struct drm_device *dev)
4251a266c7d5SChris Wilson {
4252fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4253a266c7d5SChris Wilson 
425456b857a5STvrtko Ursulin 	if (I915_HAS_HOTPLUG(dev_priv)) {
42550706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4256a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4257a266c7d5SChris Wilson 	}
4258a266c7d5SChris Wilson 
425944d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
426044d9241eSVille Syrjälä 
4261ba7eb789SVille Syrjälä 	GEN3_IRQ_RESET();
4262a266c7d5SChris Wilson }
4263a266c7d5SChris Wilson 
4264a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
4265a266c7d5SChris Wilson {
4266fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
426738bde180SChris Wilson 	u32 enable_mask;
4268a266c7d5SChris Wilson 
4269045cebd2SVille Syrjälä 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE |
4270045cebd2SVille Syrjälä 			  I915_ERROR_MEMORY_REFRESH));
427138bde180SChris Wilson 
427238bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
427338bde180SChris Wilson 	dev_priv->irq_mask =
427438bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
427538bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
427616659bc5SVille Syrjälä 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
427716659bc5SVille Syrjälä 		  I915_MASTER_ERROR_INTERRUPT);
427838bde180SChris Wilson 
427938bde180SChris Wilson 	enable_mask =
428038bde180SChris Wilson 		I915_ASLE_INTERRUPT |
428138bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
428238bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
428316659bc5SVille Syrjälä 		I915_MASTER_ERROR_INTERRUPT |
428438bde180SChris Wilson 		I915_USER_INTERRUPT;
428538bde180SChris Wilson 
428656b857a5STvrtko Ursulin 	if (I915_HAS_HOTPLUG(dev_priv)) {
4287a266c7d5SChris Wilson 		/* Enable in IER... */
4288a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
4289a266c7d5SChris Wilson 		/* and unmask in IMR */
4290a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
4291a266c7d5SChris Wilson 	}
4292a266c7d5SChris Wilson 
4293ba7eb789SVille Syrjälä 	GEN3_IRQ_INIT(, dev_priv->irq_mask, enable_mask);
4294a266c7d5SChris Wilson 
4295379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4296379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4297d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4298755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4299755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4300d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4301379ef82dSDaniel Vetter 
4302c30bb1fdSVille Syrjälä 	i915_enable_asle_pipestat(dev_priv);
4303c30bb1fdSVille Syrjälä 
430420afbda2SDaniel Vetter 	return 0;
430520afbda2SDaniel Vetter }
430620afbda2SDaniel Vetter 
4307ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
4308a266c7d5SChris Wilson {
430945a83f84SDaniel Vetter 	struct drm_device *dev = arg;
4310fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4311af722d28SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
4312a266c7d5SChris Wilson 
43132dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
43142dd2a883SImre Deak 		return IRQ_NONE;
43152dd2a883SImre Deak 
43161f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
43171f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
43181f814dacSImre Deak 
431938bde180SChris Wilson 	do {
4320eb64343cSVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
432178c357ddSVille Syrjälä 		u32 eir = 0, eir_stuck = 0;
4322af722d28SVille Syrjälä 		u32 hotplug_status = 0;
4323af722d28SVille Syrjälä 		u32 iir;
4324a266c7d5SChris Wilson 
4325af722d28SVille Syrjälä 		iir = I915_READ(IIR);
4326af722d28SVille Syrjälä 		if (iir == 0)
4327af722d28SVille Syrjälä 			break;
4328af722d28SVille Syrjälä 
4329af722d28SVille Syrjälä 		ret = IRQ_HANDLED;
4330af722d28SVille Syrjälä 
4331af722d28SVille Syrjälä 		if (I915_HAS_HOTPLUG(dev_priv) &&
4332af722d28SVille Syrjälä 		    iir & I915_DISPLAY_PORT_INTERRUPT)
4333af722d28SVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
4334a266c7d5SChris Wilson 
4335eb64343cSVille Syrjälä 		/* Call regardless, as some status bits might not be
4336eb64343cSVille Syrjälä 		 * signalled in iir */
4337eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
4338a266c7d5SChris Wilson 
433978c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
434078c357ddSVille Syrjälä 			i9xx_error_irq_ack(dev_priv, &eir, &eir_stuck);
434178c357ddSVille Syrjälä 
4342fd3a4024SDaniel Vetter 		I915_WRITE(IIR, iir);
4343a266c7d5SChris Wilson 
4344a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
4345*8a68d464SChris Wilson 			intel_engine_breadcrumbs_irq(dev_priv->engine[RCS0]);
4346a266c7d5SChris Wilson 
434778c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
434878c357ddSVille Syrjälä 			i9xx_error_irq_handler(dev_priv, eir, eir_stuck);
4349a266c7d5SChris Wilson 
4350af722d28SVille Syrjälä 		if (hotplug_status)
4351af722d28SVille Syrjälä 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
4352af722d28SVille Syrjälä 
4353af722d28SVille Syrjälä 		i915_pipestat_irq_handler(dev_priv, iir, pipe_stats);
4354af722d28SVille Syrjälä 	} while (0);
4355a266c7d5SChris Wilson 
43561f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
43571f814dacSImre Deak 
4358a266c7d5SChris Wilson 	return ret;
4359a266c7d5SChris Wilson }
4360a266c7d5SChris Wilson 
43616bcdb1c8SVille Syrjälä static void i965_irq_reset(struct drm_device *dev)
4362a266c7d5SChris Wilson {
4363fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4364a266c7d5SChris Wilson 
43650706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4366a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4367a266c7d5SChris Wilson 
436844d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
436944d9241eSVille Syrjälä 
4370ba7eb789SVille Syrjälä 	GEN3_IRQ_RESET();
4371a266c7d5SChris Wilson }
4372a266c7d5SChris Wilson 
4373a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
4374a266c7d5SChris Wilson {
4375fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4376bbba0a97SChris Wilson 	u32 enable_mask;
4377a266c7d5SChris Wilson 	u32 error_mask;
4378a266c7d5SChris Wilson 
4379045cebd2SVille Syrjälä 	/*
4380045cebd2SVille Syrjälä 	 * Enable some error detection, note the instruction error mask
4381045cebd2SVille Syrjälä 	 * bit is reserved, so we leave it masked.
4382045cebd2SVille Syrjälä 	 */
4383045cebd2SVille Syrjälä 	if (IS_G4X(dev_priv)) {
4384045cebd2SVille Syrjälä 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
4385045cebd2SVille Syrjälä 			       GM45_ERROR_MEM_PRIV |
4386045cebd2SVille Syrjälä 			       GM45_ERROR_CP_PRIV |
4387045cebd2SVille Syrjälä 			       I915_ERROR_MEMORY_REFRESH);
4388045cebd2SVille Syrjälä 	} else {
4389045cebd2SVille Syrjälä 		error_mask = ~(I915_ERROR_PAGE_TABLE |
4390045cebd2SVille Syrjälä 			       I915_ERROR_MEMORY_REFRESH);
4391045cebd2SVille Syrjälä 	}
4392045cebd2SVille Syrjälä 	I915_WRITE(EMR, error_mask);
4393045cebd2SVille Syrjälä 
4394a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
4395c30bb1fdSVille Syrjälä 	dev_priv->irq_mask =
4396c30bb1fdSVille Syrjälä 		~(I915_ASLE_INTERRUPT |
4397adca4730SChris Wilson 		  I915_DISPLAY_PORT_INTERRUPT |
4398bbba0a97SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4399bbba0a97SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
440078c357ddSVille Syrjälä 		  I915_MASTER_ERROR_INTERRUPT);
4401bbba0a97SChris Wilson 
4402c30bb1fdSVille Syrjälä 	enable_mask =
4403c30bb1fdSVille Syrjälä 		I915_ASLE_INTERRUPT |
4404c30bb1fdSVille Syrjälä 		I915_DISPLAY_PORT_INTERRUPT |
4405c30bb1fdSVille Syrjälä 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4406c30bb1fdSVille Syrjälä 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
440778c357ddSVille Syrjälä 		I915_MASTER_ERROR_INTERRUPT |
4408c30bb1fdSVille Syrjälä 		I915_USER_INTERRUPT;
4409bbba0a97SChris Wilson 
441091d14251STvrtko Ursulin 	if (IS_G4X(dev_priv))
4411bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
4412a266c7d5SChris Wilson 
4413c30bb1fdSVille Syrjälä 	GEN3_IRQ_INIT(, dev_priv->irq_mask, enable_mask);
4414c30bb1fdSVille Syrjälä 
4415b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4416b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4417d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4418755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
4419755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4420755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4421d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4422a266c7d5SChris Wilson 
442391d14251STvrtko Ursulin 	i915_enable_asle_pipestat(dev_priv);
442420afbda2SDaniel Vetter 
442520afbda2SDaniel Vetter 	return 0;
442620afbda2SDaniel Vetter }
442720afbda2SDaniel Vetter 
442891d14251STvrtko Ursulin static void i915_hpd_irq_setup(struct drm_i915_private *dev_priv)
442920afbda2SDaniel Vetter {
443020afbda2SDaniel Vetter 	u32 hotplug_en;
443120afbda2SDaniel Vetter 
443267520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
4433b5ea2d56SDaniel Vetter 
4434adca4730SChris Wilson 	/* Note HDMI and DP share hotplug bits */
4435e5868a31SEgbert Eich 	/* enable bits are the same for all generations */
443691d14251STvrtko Ursulin 	hotplug_en = intel_hpd_enabled_irqs(dev_priv, hpd_mask_i915);
4437a266c7d5SChris Wilson 	/* Programming the CRT detection parameters tends
4438a266c7d5SChris Wilson 	   to generate a spurious hotplug event about three
4439a266c7d5SChris Wilson 	   seconds later.  So just do it once.
4440a266c7d5SChris Wilson 	*/
444191d14251STvrtko Ursulin 	if (IS_G4X(dev_priv))
4442a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
4443a266c7d5SChris Wilson 	hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
4444a266c7d5SChris Wilson 
4445a266c7d5SChris Wilson 	/* Ignore TV since it's buggy */
44460706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv,
4447f9e3dc78SJani Nikula 					     HOTPLUG_INT_EN_MASK |
4448f9e3dc78SJani Nikula 					     CRT_HOTPLUG_VOLTAGE_COMPARE_MASK |
4449f9e3dc78SJani Nikula 					     CRT_HOTPLUG_ACTIVATION_PERIOD_64,
44500706f17cSEgbert Eich 					     hotplug_en);
4451a266c7d5SChris Wilson }
4452a266c7d5SChris Wilson 
4453ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
4454a266c7d5SChris Wilson {
445545a83f84SDaniel Vetter 	struct drm_device *dev = arg;
4456fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4457af722d28SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
4458a266c7d5SChris Wilson 
44592dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
44602dd2a883SImre Deak 		return IRQ_NONE;
44612dd2a883SImre Deak 
44621f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
44631f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
44641f814dacSImre Deak 
4465af722d28SVille Syrjälä 	do {
4466eb64343cSVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
446778c357ddSVille Syrjälä 		u32 eir = 0, eir_stuck = 0;
4468af722d28SVille Syrjälä 		u32 hotplug_status = 0;
4469af722d28SVille Syrjälä 		u32 iir;
44702c8ba29fSChris Wilson 
4471af722d28SVille Syrjälä 		iir = I915_READ(IIR);
4472af722d28SVille Syrjälä 		if (iir == 0)
4473af722d28SVille Syrjälä 			break;
4474af722d28SVille Syrjälä 
4475af722d28SVille Syrjälä 		ret = IRQ_HANDLED;
4476af722d28SVille Syrjälä 
4477af722d28SVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
4478af722d28SVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
4479a266c7d5SChris Wilson 
4480eb64343cSVille Syrjälä 		/* Call regardless, as some status bits might not be
4481eb64343cSVille Syrjälä 		 * signalled in iir */
4482eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
4483a266c7d5SChris Wilson 
448478c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
448578c357ddSVille Syrjälä 			i9xx_error_irq_ack(dev_priv, &eir, &eir_stuck);
448678c357ddSVille Syrjälä 
4487fd3a4024SDaniel Vetter 		I915_WRITE(IIR, iir);
4488a266c7d5SChris Wilson 
4489a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
4490*8a68d464SChris Wilson 			intel_engine_breadcrumbs_irq(dev_priv->engine[RCS0]);
4491af722d28SVille Syrjälä 
4492a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
4493*8a68d464SChris Wilson 			intel_engine_breadcrumbs_irq(dev_priv->engine[VCS0]);
4494a266c7d5SChris Wilson 
449578c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
449678c357ddSVille Syrjälä 			i9xx_error_irq_handler(dev_priv, eir, eir_stuck);
4497515ac2bbSDaniel Vetter 
4498af722d28SVille Syrjälä 		if (hotplug_status)
4499af722d28SVille Syrjälä 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
4500af722d28SVille Syrjälä 
4501af722d28SVille Syrjälä 		i965_pipestat_irq_handler(dev_priv, iir, pipe_stats);
4502af722d28SVille Syrjälä 	} while (0);
4503a266c7d5SChris Wilson 
45041f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
45051f814dacSImre Deak 
4506a266c7d5SChris Wilson 	return ret;
4507a266c7d5SChris Wilson }
4508a266c7d5SChris Wilson 
4509fca52a55SDaniel Vetter /**
4510fca52a55SDaniel Vetter  * intel_irq_init - initializes irq support
4511fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4512fca52a55SDaniel Vetter  *
4513fca52a55SDaniel Vetter  * This function initializes all the irq support including work items, timers
4514fca52a55SDaniel Vetter  * and all the vtables. It does not setup the interrupt itself though.
4515fca52a55SDaniel Vetter  */
4516b963291cSDaniel Vetter void intel_irq_init(struct drm_i915_private *dev_priv)
4517f71d4af4SJesse Barnes {
451891c8a326SChris Wilson 	struct drm_device *dev = &dev_priv->drm;
4519562d9baeSSagar Arun Kamble 	struct intel_rps *rps = &dev_priv->gt_pm.rps;
4520cefcff8fSJoonas Lahtinen 	int i;
45218b2e326dSChris Wilson 
452277913b39SJani Nikula 	intel_hpd_init_work(dev_priv);
452377913b39SJani Nikula 
4524562d9baeSSagar Arun Kamble 	INIT_WORK(&rps->work, gen6_pm_rps_work);
4525cefcff8fSJoonas Lahtinen 
4526a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
4527cefcff8fSJoonas Lahtinen 	for (i = 0; i < MAX_L3_SLICES; ++i)
4528cefcff8fSJoonas Lahtinen 		dev_priv->l3_parity.remap_info[i] = NULL;
45298b2e326dSChris Wilson 
45304805fe82STvrtko Ursulin 	if (HAS_GUC_SCHED(dev_priv))
453126705e20SSagar Arun Kamble 		dev_priv->pm_guc_events = GEN9_GUC_TO_HOST_INT_EVENT;
453226705e20SSagar Arun Kamble 
4533a6706b45SDeepak S 	/* Let's track the enabled rps events */
4534666a4537SWayne Boyer 	if (IS_VALLEYVIEW(dev_priv))
45356c65a587SVille Syrjälä 		/* WaGsvRC0ResidencyMethod:vlv */
4536e0e8c7cbSChris Wilson 		dev_priv->pm_rps_events = GEN6_PM_RP_UP_EI_EXPIRED;
453731685c25SDeepak S 	else
45384668f695SChris Wilson 		dev_priv->pm_rps_events = (GEN6_PM_RP_UP_THRESHOLD |
45394668f695SChris Wilson 					   GEN6_PM_RP_DOWN_THRESHOLD |
45404668f695SChris Wilson 					   GEN6_PM_RP_DOWN_TIMEOUT);
4541a6706b45SDeepak S 
4542562d9baeSSagar Arun Kamble 	rps->pm_intrmsk_mbz = 0;
45431800ad25SSagar Arun Kamble 
45441800ad25SSagar Arun Kamble 	/*
4545acf2dc22SMika Kuoppala 	 * SNB,IVB,HSW can while VLV,CHV may hard hang on looping batchbuffer
45461800ad25SSagar Arun Kamble 	 * if GEN6_PM_UP_EI_EXPIRED is masked.
45471800ad25SSagar Arun Kamble 	 *
45481800ad25SSagar Arun Kamble 	 * TODO: verify if this can be reproduced on VLV,CHV.
45491800ad25SSagar Arun Kamble 	 */
4550bca2bf2aSPandiyan, Dhinakaran 	if (INTEL_GEN(dev_priv) <= 7)
4551562d9baeSSagar Arun Kamble 		rps->pm_intrmsk_mbz |= GEN6_PM_RP_UP_EI_EXPIRED;
45521800ad25SSagar Arun Kamble 
4553bca2bf2aSPandiyan, Dhinakaran 	if (INTEL_GEN(dev_priv) >= 8)
4554562d9baeSSagar Arun Kamble 		rps->pm_intrmsk_mbz |= GEN8_PMINTR_DISABLE_REDIRECT_TO_GUC;
45551800ad25SSagar Arun Kamble 
455632db0b65SVille Syrjälä 	if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
4557fd8f507cSVille Syrjälä 		dev->driver->get_vblank_counter = g4x_get_vblank_counter;
455832db0b65SVille Syrjälä 	else if (INTEL_GEN(dev_priv) >= 3)
4559391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
4560f71d4af4SJesse Barnes 
456121da2700SVille Syrjälä 	/*
456221da2700SVille Syrjälä 	 * Opt out of the vblank disable timer on everything except gen2.
456321da2700SVille Syrjälä 	 * Gen2 doesn't have a hardware frame counter and so depends on
456421da2700SVille Syrjälä 	 * vblank interrupts to produce sane vblank seuquence numbers.
456521da2700SVille Syrjälä 	 */
4566cf819effSLucas De Marchi 	if (!IS_GEN(dev_priv, 2))
456721da2700SVille Syrjälä 		dev->vblank_disable_immediate = true;
456821da2700SVille Syrjälä 
4569262fd485SChris Wilson 	/* Most platforms treat the display irq block as an always-on
4570262fd485SChris Wilson 	 * power domain. vlv/chv can disable it at runtime and need
4571262fd485SChris Wilson 	 * special care to avoid writing any of the display block registers
4572262fd485SChris Wilson 	 * outside of the power domain. We defer setting up the display irqs
4573262fd485SChris Wilson 	 * in this case to the runtime pm.
4574262fd485SChris Wilson 	 */
4575262fd485SChris Wilson 	dev_priv->display_irqs_enabled = true;
4576262fd485SChris Wilson 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4577262fd485SChris Wilson 		dev_priv->display_irqs_enabled = false;
4578262fd485SChris Wilson 
4579317eaa95SLyude 	dev_priv->hotplug.hpd_storm_threshold = HPD_STORM_DEFAULT_THRESHOLD;
45809a64c650SLyude Paul 	/* If we have MST support, we want to avoid doing short HPD IRQ storm
45819a64c650SLyude Paul 	 * detection, as short HPD storms will occur as a natural part of
45829a64c650SLyude Paul 	 * sideband messaging with MST.
45839a64c650SLyude Paul 	 * On older platforms however, IRQ storms can occur with both long and
45849a64c650SLyude Paul 	 * short pulses, as seen on some G4x systems.
45859a64c650SLyude Paul 	 */
45869a64c650SLyude Paul 	dev_priv->hotplug.hpd_short_storm_enabled = !HAS_DP_MST(dev_priv);
4587317eaa95SLyude 
45881bf6ad62SDaniel Vetter 	dev->driver->get_vblank_timestamp = drm_calc_vbltimestamp_from_scanoutpos;
4589f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
4590f71d4af4SJesse Barnes 
4591b963291cSDaniel Vetter 	if (IS_CHERRYVIEW(dev_priv)) {
459243f328d7SVille Syrjälä 		dev->driver->irq_handler = cherryview_irq_handler;
45936bcdb1c8SVille Syrjälä 		dev->driver->irq_preinstall = cherryview_irq_reset;
459443f328d7SVille Syrjälä 		dev->driver->irq_postinstall = cherryview_irq_postinstall;
45956bcdb1c8SVille Syrjälä 		dev->driver->irq_uninstall = cherryview_irq_reset;
459686e83e35SChris Wilson 		dev->driver->enable_vblank = i965_enable_vblank;
459786e83e35SChris Wilson 		dev->driver->disable_vblank = i965_disable_vblank;
459843f328d7SVille Syrjälä 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4599b963291cSDaniel Vetter 	} else if (IS_VALLEYVIEW(dev_priv)) {
46007e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
46016bcdb1c8SVille Syrjälä 		dev->driver->irq_preinstall = valleyview_irq_reset;
46027e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
46036bcdb1c8SVille Syrjälä 		dev->driver->irq_uninstall = valleyview_irq_reset;
460486e83e35SChris Wilson 		dev->driver->enable_vblank = i965_enable_vblank;
460586e83e35SChris Wilson 		dev->driver->disable_vblank = i965_disable_vblank;
4606fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
460751951ae7SMika Kuoppala 	} else if (INTEL_GEN(dev_priv) >= 11) {
460851951ae7SMika Kuoppala 		dev->driver->irq_handler = gen11_irq_handler;
460951951ae7SMika Kuoppala 		dev->driver->irq_preinstall = gen11_irq_reset;
461051951ae7SMika Kuoppala 		dev->driver->irq_postinstall = gen11_irq_postinstall;
461151951ae7SMika Kuoppala 		dev->driver->irq_uninstall = gen11_irq_reset;
461251951ae7SMika Kuoppala 		dev->driver->enable_vblank = gen8_enable_vblank;
461351951ae7SMika Kuoppala 		dev->driver->disable_vblank = gen8_disable_vblank;
4614121e758eSDhinakaran Pandiyan 		dev_priv->display.hpd_irq_setup = gen11_hpd_irq_setup;
4615bca2bf2aSPandiyan, Dhinakaran 	} else if (INTEL_GEN(dev_priv) >= 8) {
4616abd58f01SBen Widawsky 		dev->driver->irq_handler = gen8_irq_handler;
4617723761b8SDaniel Vetter 		dev->driver->irq_preinstall = gen8_irq_reset;
4618abd58f01SBen Widawsky 		dev->driver->irq_postinstall = gen8_irq_postinstall;
46196bcdb1c8SVille Syrjälä 		dev->driver->irq_uninstall = gen8_irq_reset;
4620abd58f01SBen Widawsky 		dev->driver->enable_vblank = gen8_enable_vblank;
4621abd58f01SBen Widawsky 		dev->driver->disable_vblank = gen8_disable_vblank;
4622cc3f90f0SAnder Conselvan de Oliveira 		if (IS_GEN9_LP(dev_priv))
4623e0a20ad7SShashank Sharma 			dev_priv->display.hpd_irq_setup = bxt_hpd_irq_setup;
46247b22b8c4SRodrigo Vivi 		else if (HAS_PCH_SPT(dev_priv) || HAS_PCH_KBP(dev_priv) ||
46257b22b8c4SRodrigo Vivi 			 HAS_PCH_CNP(dev_priv))
46266dbf30ceSVille Syrjälä 			dev_priv->display.hpd_irq_setup = spt_hpd_irq_setup;
46276dbf30ceSVille Syrjälä 		else
46283a3b3c7dSVille Syrjälä 			dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
46296e266956STvrtko Ursulin 	} else if (HAS_PCH_SPLIT(dev_priv)) {
4630f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
4631723761b8SDaniel Vetter 		dev->driver->irq_preinstall = ironlake_irq_reset;
4632f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
46336bcdb1c8SVille Syrjälä 		dev->driver->irq_uninstall = ironlake_irq_reset;
4634f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
4635f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
4636e4ce95aaSVille Syrjälä 		dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
4637f71d4af4SJesse Barnes 	} else {
4638cf819effSLucas De Marchi 		if (IS_GEN(dev_priv, 2)) {
46396bcdb1c8SVille Syrjälä 			dev->driver->irq_preinstall = i8xx_irq_reset;
4640c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
4641c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
46426bcdb1c8SVille Syrjälä 			dev->driver->irq_uninstall = i8xx_irq_reset;
464386e83e35SChris Wilson 			dev->driver->enable_vblank = i8xx_enable_vblank;
464486e83e35SChris Wilson 			dev->driver->disable_vblank = i8xx_disable_vblank;
4645cf819effSLucas De Marchi 		} else if (IS_GEN(dev_priv, 3)) {
46466bcdb1c8SVille Syrjälä 			dev->driver->irq_preinstall = i915_irq_reset;
4647a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
46486bcdb1c8SVille Syrjälä 			dev->driver->irq_uninstall = i915_irq_reset;
4649a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
465086e83e35SChris Wilson 			dev->driver->enable_vblank = i8xx_enable_vblank;
465186e83e35SChris Wilson 			dev->driver->disable_vblank = i8xx_disable_vblank;
4652c2798b19SChris Wilson 		} else {
46536bcdb1c8SVille Syrjälä 			dev->driver->irq_preinstall = i965_irq_reset;
4654a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
46556bcdb1c8SVille Syrjälä 			dev->driver->irq_uninstall = i965_irq_reset;
4656a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
465786e83e35SChris Wilson 			dev->driver->enable_vblank = i965_enable_vblank;
465886e83e35SChris Wilson 			dev->driver->disable_vblank = i965_disable_vblank;
4659c2798b19SChris Wilson 		}
4660778eb334SVille Syrjälä 		if (I915_HAS_HOTPLUG(dev_priv))
4661778eb334SVille Syrjälä 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4662f71d4af4SJesse Barnes 	}
4663f71d4af4SJesse Barnes }
466420afbda2SDaniel Vetter 
4665fca52a55SDaniel Vetter /**
4666cefcff8fSJoonas Lahtinen  * intel_irq_fini - deinitializes IRQ support
4667cefcff8fSJoonas Lahtinen  * @i915: i915 device instance
4668cefcff8fSJoonas Lahtinen  *
4669cefcff8fSJoonas Lahtinen  * This function deinitializes all the IRQ support.
4670cefcff8fSJoonas Lahtinen  */
4671cefcff8fSJoonas Lahtinen void intel_irq_fini(struct drm_i915_private *i915)
4672cefcff8fSJoonas Lahtinen {
4673cefcff8fSJoonas Lahtinen 	int i;
4674cefcff8fSJoonas Lahtinen 
4675cefcff8fSJoonas Lahtinen 	for (i = 0; i < MAX_L3_SLICES; ++i)
4676cefcff8fSJoonas Lahtinen 		kfree(i915->l3_parity.remap_info[i]);
4677cefcff8fSJoonas Lahtinen }
4678cefcff8fSJoonas Lahtinen 
4679cefcff8fSJoonas Lahtinen /**
4680fca52a55SDaniel Vetter  * intel_irq_install - enables the hardware interrupt
4681fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4682fca52a55SDaniel Vetter  *
4683fca52a55SDaniel Vetter  * This function enables the hardware interrupt handling, but leaves the hotplug
4684fca52a55SDaniel Vetter  * handling still disabled. It is called after intel_irq_init().
4685fca52a55SDaniel Vetter  *
4686fca52a55SDaniel Vetter  * In the driver load and resume code we need working interrupts in a few places
4687fca52a55SDaniel Vetter  * but don't want to deal with the hassle of concurrent probe and hotplug
4688fca52a55SDaniel Vetter  * workers. Hence the split into this two-stage approach.
4689fca52a55SDaniel Vetter  */
46902aeb7d3aSDaniel Vetter int intel_irq_install(struct drm_i915_private *dev_priv)
46912aeb7d3aSDaniel Vetter {
46922aeb7d3aSDaniel Vetter 	/*
46932aeb7d3aSDaniel Vetter 	 * We enable some interrupt sources in our postinstall hooks, so mark
46942aeb7d3aSDaniel Vetter 	 * interrupts as enabled _before_ actually enabling them to avoid
46952aeb7d3aSDaniel Vetter 	 * special cases in our ordering checks.
46962aeb7d3aSDaniel Vetter 	 */
4697ad1443f0SSagar Arun Kamble 	dev_priv->runtime_pm.irqs_enabled = true;
46982aeb7d3aSDaniel Vetter 
469991c8a326SChris Wilson 	return drm_irq_install(&dev_priv->drm, dev_priv->drm.pdev->irq);
47002aeb7d3aSDaniel Vetter }
47012aeb7d3aSDaniel Vetter 
4702fca52a55SDaniel Vetter /**
4703fca52a55SDaniel Vetter  * intel_irq_uninstall - finilizes all irq handling
4704fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4705fca52a55SDaniel Vetter  *
4706fca52a55SDaniel Vetter  * This stops interrupt and hotplug handling and unregisters and frees all
4707fca52a55SDaniel Vetter  * resources acquired in the init functions.
4708fca52a55SDaniel Vetter  */
47092aeb7d3aSDaniel Vetter void intel_irq_uninstall(struct drm_i915_private *dev_priv)
47102aeb7d3aSDaniel Vetter {
471191c8a326SChris Wilson 	drm_irq_uninstall(&dev_priv->drm);
47122aeb7d3aSDaniel Vetter 	intel_hpd_cancel_work(dev_priv);
4713ad1443f0SSagar Arun Kamble 	dev_priv->runtime_pm.irqs_enabled = false;
47142aeb7d3aSDaniel Vetter }
47152aeb7d3aSDaniel Vetter 
4716fca52a55SDaniel Vetter /**
4717fca52a55SDaniel Vetter  * intel_runtime_pm_disable_interrupts - runtime interrupt disabling
4718fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4719fca52a55SDaniel Vetter  *
4720fca52a55SDaniel Vetter  * This function is used to disable interrupts at runtime, both in the runtime
4721fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4722fca52a55SDaniel Vetter  */
4723b963291cSDaniel Vetter void intel_runtime_pm_disable_interrupts(struct drm_i915_private *dev_priv)
4724c67a470bSPaulo Zanoni {
472591c8a326SChris Wilson 	dev_priv->drm.driver->irq_uninstall(&dev_priv->drm);
4726ad1443f0SSagar Arun Kamble 	dev_priv->runtime_pm.irqs_enabled = false;
472791c8a326SChris Wilson 	synchronize_irq(dev_priv->drm.irq);
4728c67a470bSPaulo Zanoni }
4729c67a470bSPaulo Zanoni 
4730fca52a55SDaniel Vetter /**
4731fca52a55SDaniel Vetter  * intel_runtime_pm_enable_interrupts - runtime interrupt enabling
4732fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4733fca52a55SDaniel Vetter  *
4734fca52a55SDaniel Vetter  * This function is used to enable interrupts at runtime, both in the runtime
4735fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4736fca52a55SDaniel Vetter  */
4737b963291cSDaniel Vetter void intel_runtime_pm_enable_interrupts(struct drm_i915_private *dev_priv)
4738c67a470bSPaulo Zanoni {
4739ad1443f0SSagar Arun Kamble 	dev_priv->runtime_pm.irqs_enabled = true;
474091c8a326SChris Wilson 	dev_priv->drm.driver->irq_preinstall(&dev_priv->drm);
474191c8a326SChris Wilson 	dev_priv->drm.driver->irq_postinstall(&dev_priv->drm);
4742c67a470bSPaulo Zanoni }
4743