xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 29b43ae2a61d4836948cbefdcfb08ab02463e821)
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)
14308a68d464SChris Wilson 		intel_engine_breadcrumbs_irq(dev_priv->engine[RCS0]);
1431f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
14328a68d464SChris 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)
14398a68d464SChris Wilson 		intel_engine_breadcrumbs_irq(dev_priv->engine[RCS0]);
1440cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
14418a68d464SChris Wilson 		intel_engine_breadcrumbs_irq(dev_priv->engine[VCS0]);
1442cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
14438a68d464SChris 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 | \
14788a68d464SChris 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 
14908a68d464SChris 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)) {
15138a68d464SChris Wilson 		gen8_cs_irq_handler(i915->engine[RCS0],
151451f6b0f9SChris Wilson 				    gt_iir[0] >> GEN8_RCS_IRQ_SHIFT);
15158a68d464SChris Wilson 		gen8_cs_irq_handler(i915->engine[BCS0],
151651f6b0f9SChris Wilson 				    gt_iir[0] >> GEN8_BCS_IRQ_SHIFT);
1517e30e251aSVille Syrjälä 	}
1518e30e251aSVille Syrjälä 
15198a68d464SChris Wilson 	if (master_ctl & (GEN8_GT_VCS0_IRQ | GEN8_GT_VCS1_IRQ)) {
15208a68d464SChris Wilson 		gen8_cs_irq_handler(i915->engine[VCS0],
15218a68d464SChris Wilson 				    gt_iir[1] >> GEN8_VCS0_IRQ_SHIFT);
15228a68d464SChris 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) {
15278a68d464SChris 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)
18058a68d464SChris 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ä 
2832*29b43ae2SRodrigo Vivi 			if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
283331604222SAnusha Srivatsa 				icp_irq_handler(dev_priv, iir);
2834c6c30b91SRodrigo Vivi 			else if (INTEL_PCH_TYPE(dev_priv) >= PCH_SPT)
283591d14251STvrtko Ursulin 				spt_irq_handler(dev_priv, iir);
28366dbf30ceSVille Syrjälä 			else
283791d14251STvrtko Ursulin 				cpt_irq_handler(dev_priv, iir);
28382dfb0b81SJani Nikula 		} else {
28392dfb0b81SJani Nikula 			/*
28402dfb0b81SJani Nikula 			 * Like on previous PCH there seems to be something
28412dfb0b81SJani Nikula 			 * fishy going on with forwarding PCH interrupts.
28422dfb0b81SJani Nikula 			 */
28432dfb0b81SJani Nikula 			DRM_DEBUG_DRIVER("The master control interrupt lied (SDE)!\n");
28442dfb0b81SJani Nikula 		}
284592d03a80SDaniel Vetter 	}
284692d03a80SDaniel Vetter 
2847f11a0f46STvrtko Ursulin 	return ret;
2848f11a0f46STvrtko Ursulin }
2849f11a0f46STvrtko Ursulin 
28504376b9c9SMika Kuoppala static inline u32 gen8_master_intr_disable(void __iomem * const regs)
28514376b9c9SMika Kuoppala {
28524376b9c9SMika Kuoppala 	raw_reg_write(regs, GEN8_MASTER_IRQ, 0);
28534376b9c9SMika Kuoppala 
28544376b9c9SMika Kuoppala 	/*
28554376b9c9SMika Kuoppala 	 * Now with master disabled, get a sample of level indications
28564376b9c9SMika Kuoppala 	 * for this interrupt. Indications will be cleared on related acks.
28574376b9c9SMika Kuoppala 	 * New indications can and will light up during processing,
28584376b9c9SMika Kuoppala 	 * and will generate new interrupt after enabling master.
28594376b9c9SMika Kuoppala 	 */
28604376b9c9SMika Kuoppala 	return raw_reg_read(regs, GEN8_MASTER_IRQ);
28614376b9c9SMika Kuoppala }
28624376b9c9SMika Kuoppala 
28634376b9c9SMika Kuoppala static inline void gen8_master_intr_enable(void __iomem * const regs)
28644376b9c9SMika Kuoppala {
28654376b9c9SMika Kuoppala 	raw_reg_write(regs, GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
28664376b9c9SMika Kuoppala }
28674376b9c9SMika Kuoppala 
2868f11a0f46STvrtko Ursulin static irqreturn_t gen8_irq_handler(int irq, void *arg)
2869f11a0f46STvrtko Ursulin {
2870f0fd96f5SChris Wilson 	struct drm_i915_private *dev_priv = to_i915(arg);
28714376b9c9SMika Kuoppala 	void __iomem * const regs = dev_priv->regs;
2872f11a0f46STvrtko Ursulin 	u32 master_ctl;
2873f0fd96f5SChris Wilson 	u32 gt_iir[4];
2874f11a0f46STvrtko Ursulin 
2875f11a0f46STvrtko Ursulin 	if (!intel_irqs_enabled(dev_priv))
2876f11a0f46STvrtko Ursulin 		return IRQ_NONE;
2877f11a0f46STvrtko Ursulin 
28784376b9c9SMika Kuoppala 	master_ctl = gen8_master_intr_disable(regs);
28794376b9c9SMika Kuoppala 	if (!master_ctl) {
28804376b9c9SMika Kuoppala 		gen8_master_intr_enable(regs);
2881f11a0f46STvrtko Ursulin 		return IRQ_NONE;
28824376b9c9SMika Kuoppala 	}
2883f11a0f46STvrtko Ursulin 
2884f11a0f46STvrtko Ursulin 	/* Find, clear, then process each source of interrupt */
288555ef72f2SChris Wilson 	gen8_gt_irq_ack(dev_priv, master_ctl, gt_iir);
2886f0fd96f5SChris Wilson 
2887f0fd96f5SChris Wilson 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
2888f0fd96f5SChris Wilson 	if (master_ctl & ~GEN8_GT_IRQS) {
2889f0fd96f5SChris Wilson 		disable_rpm_wakeref_asserts(dev_priv);
289055ef72f2SChris Wilson 		gen8_de_irq_handler(dev_priv, master_ctl);
2891f0fd96f5SChris Wilson 		enable_rpm_wakeref_asserts(dev_priv);
2892f0fd96f5SChris Wilson 	}
2893f11a0f46STvrtko Ursulin 
28944376b9c9SMika Kuoppala 	gen8_master_intr_enable(regs);
2895abd58f01SBen Widawsky 
2896f0fd96f5SChris Wilson 	gen8_gt_irq_handler(dev_priv, master_ctl, gt_iir);
28971f814dacSImre Deak 
289855ef72f2SChris Wilson 	return IRQ_HANDLED;
2899abd58f01SBen Widawsky }
2900abd58f01SBen Widawsky 
290151951ae7SMika Kuoppala static u32
2902f744dbc2SMika Kuoppala gen11_gt_engine_identity(struct drm_i915_private * const i915,
290351951ae7SMika Kuoppala 			 const unsigned int bank, const unsigned int bit)
290451951ae7SMika Kuoppala {
290551951ae7SMika Kuoppala 	void __iomem * const regs = i915->regs;
290651951ae7SMika Kuoppala 	u32 timeout_ts;
290751951ae7SMika Kuoppala 	u32 ident;
290851951ae7SMika Kuoppala 
290996606f3bSOscar Mateo 	lockdep_assert_held(&i915->irq_lock);
291096606f3bSOscar Mateo 
291151951ae7SMika Kuoppala 	raw_reg_write(regs, GEN11_IIR_REG_SELECTOR(bank), BIT(bit));
291251951ae7SMika Kuoppala 
291351951ae7SMika Kuoppala 	/*
291451951ae7SMika Kuoppala 	 * NB: Specs do not specify how long to spin wait,
291551951ae7SMika Kuoppala 	 * so we do ~100us as an educated guess.
291651951ae7SMika Kuoppala 	 */
291751951ae7SMika Kuoppala 	timeout_ts = (local_clock() >> 10) + 100;
291851951ae7SMika Kuoppala 	do {
291951951ae7SMika Kuoppala 		ident = raw_reg_read(regs, GEN11_INTR_IDENTITY_REG(bank));
292051951ae7SMika Kuoppala 	} while (!(ident & GEN11_INTR_DATA_VALID) &&
292151951ae7SMika Kuoppala 		 !time_after32(local_clock() >> 10, timeout_ts));
292251951ae7SMika Kuoppala 
292351951ae7SMika Kuoppala 	if (unlikely(!(ident & GEN11_INTR_DATA_VALID))) {
292451951ae7SMika Kuoppala 		DRM_ERROR("INTR_IDENTITY_REG%u:%u 0x%08x not valid!\n",
292551951ae7SMika Kuoppala 			  bank, bit, ident);
292651951ae7SMika Kuoppala 		return 0;
292751951ae7SMika Kuoppala 	}
292851951ae7SMika Kuoppala 
292951951ae7SMika Kuoppala 	raw_reg_write(regs, GEN11_INTR_IDENTITY_REG(bank),
293051951ae7SMika Kuoppala 		      GEN11_INTR_DATA_VALID);
293151951ae7SMika Kuoppala 
2932f744dbc2SMika Kuoppala 	return ident;
2933f744dbc2SMika Kuoppala }
2934f744dbc2SMika Kuoppala 
2935f744dbc2SMika Kuoppala static void
2936f744dbc2SMika Kuoppala gen11_other_irq_handler(struct drm_i915_private * const i915,
2937f744dbc2SMika Kuoppala 			const u8 instance, const u16 iir)
2938f744dbc2SMika Kuoppala {
2939d02b98b8SOscar Mateo 	if (instance == OTHER_GTPM_INSTANCE)
2940d02b98b8SOscar Mateo 		return gen6_rps_irq_handler(i915, iir);
2941d02b98b8SOscar Mateo 
2942f744dbc2SMika Kuoppala 	WARN_ONCE(1, "unhandled other interrupt instance=0x%x, iir=0x%x\n",
2943f744dbc2SMika Kuoppala 		  instance, iir);
2944f744dbc2SMika Kuoppala }
2945f744dbc2SMika Kuoppala 
2946f744dbc2SMika Kuoppala static void
2947f744dbc2SMika Kuoppala gen11_engine_irq_handler(struct drm_i915_private * const i915,
2948f744dbc2SMika Kuoppala 			 const u8 class, const u8 instance, const u16 iir)
2949f744dbc2SMika Kuoppala {
2950f744dbc2SMika Kuoppala 	struct intel_engine_cs *engine;
2951f744dbc2SMika Kuoppala 
2952f744dbc2SMika Kuoppala 	if (instance <= MAX_ENGINE_INSTANCE)
2953f744dbc2SMika Kuoppala 		engine = i915->engine_class[class][instance];
2954f744dbc2SMika Kuoppala 	else
2955f744dbc2SMika Kuoppala 		engine = NULL;
2956f744dbc2SMika Kuoppala 
2957f744dbc2SMika Kuoppala 	if (likely(engine))
2958f744dbc2SMika Kuoppala 		return gen8_cs_irq_handler(engine, iir);
2959f744dbc2SMika Kuoppala 
2960f744dbc2SMika Kuoppala 	WARN_ONCE(1, "unhandled engine interrupt class=0x%x, instance=0x%x\n",
2961f744dbc2SMika Kuoppala 		  class, instance);
2962f744dbc2SMika Kuoppala }
2963f744dbc2SMika Kuoppala 
2964f744dbc2SMika Kuoppala static void
2965f744dbc2SMika Kuoppala gen11_gt_identity_handler(struct drm_i915_private * const i915,
2966f744dbc2SMika Kuoppala 			  const u32 identity)
2967f744dbc2SMika Kuoppala {
2968f744dbc2SMika Kuoppala 	const u8 class = GEN11_INTR_ENGINE_CLASS(identity);
2969f744dbc2SMika Kuoppala 	const u8 instance = GEN11_INTR_ENGINE_INSTANCE(identity);
2970f744dbc2SMika Kuoppala 	const u16 intr = GEN11_INTR_ENGINE_INTR(identity);
2971f744dbc2SMika Kuoppala 
2972f744dbc2SMika Kuoppala 	if (unlikely(!intr))
2973f744dbc2SMika Kuoppala 		return;
2974f744dbc2SMika Kuoppala 
2975f744dbc2SMika Kuoppala 	if (class <= COPY_ENGINE_CLASS)
2976f744dbc2SMika Kuoppala 		return gen11_engine_irq_handler(i915, class, instance, intr);
2977f744dbc2SMika Kuoppala 
2978f744dbc2SMika Kuoppala 	if (class == OTHER_CLASS)
2979f744dbc2SMika Kuoppala 		return gen11_other_irq_handler(i915, instance, intr);
2980f744dbc2SMika Kuoppala 
2981f744dbc2SMika Kuoppala 	WARN_ONCE(1, "unknown interrupt class=0x%x, instance=0x%x, intr=0x%x\n",
2982f744dbc2SMika Kuoppala 		  class, instance, intr);
298351951ae7SMika Kuoppala }
298451951ae7SMika Kuoppala 
298551951ae7SMika Kuoppala static void
298696606f3bSOscar Mateo gen11_gt_bank_handler(struct drm_i915_private * const i915,
298796606f3bSOscar Mateo 		      const unsigned int bank)
298851951ae7SMika Kuoppala {
298951951ae7SMika Kuoppala 	void __iomem * const regs = i915->regs;
299051951ae7SMika Kuoppala 	unsigned long intr_dw;
299151951ae7SMika Kuoppala 	unsigned int bit;
299251951ae7SMika Kuoppala 
299396606f3bSOscar Mateo 	lockdep_assert_held(&i915->irq_lock);
299451951ae7SMika Kuoppala 
299551951ae7SMika Kuoppala 	intr_dw = raw_reg_read(regs, GEN11_GT_INTR_DW(bank));
299651951ae7SMika Kuoppala 
299751951ae7SMika Kuoppala 	if (unlikely(!intr_dw)) {
299851951ae7SMika Kuoppala 		DRM_ERROR("GT_INTR_DW%u blank!\n", bank);
299996606f3bSOscar Mateo 		return;
300051951ae7SMika Kuoppala 	}
300151951ae7SMika Kuoppala 
300251951ae7SMika Kuoppala 	for_each_set_bit(bit, &intr_dw, 32) {
3003f744dbc2SMika Kuoppala 		const u32 ident = gen11_gt_engine_identity(i915,
3004f744dbc2SMika Kuoppala 							   bank, bit);
300551951ae7SMika Kuoppala 
3006f744dbc2SMika Kuoppala 		gen11_gt_identity_handler(i915, ident);
300751951ae7SMika Kuoppala 	}
300851951ae7SMika Kuoppala 
300951951ae7SMika Kuoppala 	/* Clear must be after shared has been served for engine */
301051951ae7SMika Kuoppala 	raw_reg_write(regs, GEN11_GT_INTR_DW(bank), intr_dw);
301151951ae7SMika Kuoppala }
301296606f3bSOscar Mateo 
301396606f3bSOscar Mateo static void
301496606f3bSOscar Mateo gen11_gt_irq_handler(struct drm_i915_private * const i915,
301596606f3bSOscar Mateo 		     const u32 master_ctl)
301696606f3bSOscar Mateo {
301796606f3bSOscar Mateo 	unsigned int bank;
301896606f3bSOscar Mateo 
301996606f3bSOscar Mateo 	spin_lock(&i915->irq_lock);
302096606f3bSOscar Mateo 
302196606f3bSOscar Mateo 	for (bank = 0; bank < 2; bank++) {
302296606f3bSOscar Mateo 		if (master_ctl & GEN11_GT_DW_IRQ(bank))
302396606f3bSOscar Mateo 			gen11_gt_bank_handler(i915, bank);
302496606f3bSOscar Mateo 	}
302596606f3bSOscar Mateo 
302696606f3bSOscar Mateo 	spin_unlock(&i915->irq_lock);
302751951ae7SMika Kuoppala }
302851951ae7SMika Kuoppala 
30297a909383SChris Wilson static u32
30307a909383SChris Wilson gen11_gu_misc_irq_ack(struct drm_i915_private *dev_priv, const u32 master_ctl)
3031df0d28c1SDhinakaran Pandiyan {
3032df0d28c1SDhinakaran Pandiyan 	void __iomem * const regs = dev_priv->regs;
30337a909383SChris Wilson 	u32 iir;
3034df0d28c1SDhinakaran Pandiyan 
3035df0d28c1SDhinakaran Pandiyan 	if (!(master_ctl & GEN11_GU_MISC_IRQ))
30367a909383SChris Wilson 		return 0;
3037df0d28c1SDhinakaran Pandiyan 
30387a909383SChris Wilson 	iir = raw_reg_read(regs, GEN11_GU_MISC_IIR);
30397a909383SChris Wilson 	if (likely(iir))
30407a909383SChris Wilson 		raw_reg_write(regs, GEN11_GU_MISC_IIR, iir);
30417a909383SChris Wilson 
30427a909383SChris Wilson 	return iir;
3043df0d28c1SDhinakaran Pandiyan }
3044df0d28c1SDhinakaran Pandiyan 
3045df0d28c1SDhinakaran Pandiyan static void
30467a909383SChris Wilson gen11_gu_misc_irq_handler(struct drm_i915_private *dev_priv, const u32 iir)
3047df0d28c1SDhinakaran Pandiyan {
3048df0d28c1SDhinakaran Pandiyan 	if (iir & GEN11_GU_MISC_GSE)
3049df0d28c1SDhinakaran Pandiyan 		intel_opregion_asle_intr(dev_priv);
3050df0d28c1SDhinakaran Pandiyan }
3051df0d28c1SDhinakaran Pandiyan 
305281067b71SMika Kuoppala static inline u32 gen11_master_intr_disable(void __iomem * const regs)
305381067b71SMika Kuoppala {
305481067b71SMika Kuoppala 	raw_reg_write(regs, GEN11_GFX_MSTR_IRQ, 0);
305581067b71SMika Kuoppala 
305681067b71SMika Kuoppala 	/*
305781067b71SMika Kuoppala 	 * Now with master disabled, get a sample of level indications
305881067b71SMika Kuoppala 	 * for this interrupt. Indications will be cleared on related acks.
305981067b71SMika Kuoppala 	 * New indications can and will light up during processing,
306081067b71SMika Kuoppala 	 * and will generate new interrupt after enabling master.
306181067b71SMika Kuoppala 	 */
306281067b71SMika Kuoppala 	return raw_reg_read(regs, GEN11_GFX_MSTR_IRQ);
306381067b71SMika Kuoppala }
306481067b71SMika Kuoppala 
306581067b71SMika Kuoppala static inline void gen11_master_intr_enable(void __iomem * const regs)
306681067b71SMika Kuoppala {
306781067b71SMika Kuoppala 	raw_reg_write(regs, GEN11_GFX_MSTR_IRQ, GEN11_MASTER_IRQ);
306881067b71SMika Kuoppala }
306981067b71SMika Kuoppala 
307051951ae7SMika Kuoppala static irqreturn_t gen11_irq_handler(int irq, void *arg)
307151951ae7SMika Kuoppala {
307251951ae7SMika Kuoppala 	struct drm_i915_private * const i915 = to_i915(arg);
307351951ae7SMika Kuoppala 	void __iomem * const regs = i915->regs;
307451951ae7SMika Kuoppala 	u32 master_ctl;
3075df0d28c1SDhinakaran Pandiyan 	u32 gu_misc_iir;
307651951ae7SMika Kuoppala 
307751951ae7SMika Kuoppala 	if (!intel_irqs_enabled(i915))
307851951ae7SMika Kuoppala 		return IRQ_NONE;
307951951ae7SMika Kuoppala 
308081067b71SMika Kuoppala 	master_ctl = gen11_master_intr_disable(regs);
308181067b71SMika Kuoppala 	if (!master_ctl) {
308281067b71SMika Kuoppala 		gen11_master_intr_enable(regs);
308351951ae7SMika Kuoppala 		return IRQ_NONE;
308481067b71SMika Kuoppala 	}
308551951ae7SMika Kuoppala 
308651951ae7SMika Kuoppala 	/* Find, clear, then process each source of interrupt. */
308751951ae7SMika Kuoppala 	gen11_gt_irq_handler(i915, master_ctl);
308851951ae7SMika Kuoppala 
308951951ae7SMika Kuoppala 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
309051951ae7SMika Kuoppala 	if (master_ctl & GEN11_DISPLAY_IRQ) {
309151951ae7SMika Kuoppala 		const u32 disp_ctl = raw_reg_read(regs, GEN11_DISPLAY_INT_CTL);
309251951ae7SMika Kuoppala 
309351951ae7SMika Kuoppala 		disable_rpm_wakeref_asserts(i915);
309451951ae7SMika Kuoppala 		/*
309551951ae7SMika Kuoppala 		 * GEN11_DISPLAY_INT_CTL has same format as GEN8_MASTER_IRQ
309651951ae7SMika Kuoppala 		 * for the display related bits.
309751951ae7SMika Kuoppala 		 */
309851951ae7SMika Kuoppala 		gen8_de_irq_handler(i915, disp_ctl);
309951951ae7SMika Kuoppala 		enable_rpm_wakeref_asserts(i915);
310051951ae7SMika Kuoppala 	}
310151951ae7SMika Kuoppala 
31027a909383SChris Wilson 	gu_misc_iir = gen11_gu_misc_irq_ack(i915, master_ctl);
3103df0d28c1SDhinakaran Pandiyan 
310481067b71SMika Kuoppala 	gen11_master_intr_enable(regs);
310551951ae7SMika Kuoppala 
31067a909383SChris Wilson 	gen11_gu_misc_irq_handler(i915, gu_misc_iir);
3107df0d28c1SDhinakaran Pandiyan 
310851951ae7SMika Kuoppala 	return IRQ_HANDLED;
310951951ae7SMika Kuoppala }
311051951ae7SMika Kuoppala 
311142f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
311242f52ef8SKeith Packard  * we use as a pipe index
311342f52ef8SKeith Packard  */
311486e83e35SChris Wilson static int i8xx_enable_vblank(struct drm_device *dev, unsigned int pipe)
31150a3e67a4SJesse Barnes {
3116fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3117e9d21d7fSKeith Packard 	unsigned long irqflags;
311871e0ffa5SJesse Barnes 
31191ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
312086e83e35SChris Wilson 	i915_enable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_STATUS);
312186e83e35SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
312286e83e35SChris Wilson 
312386e83e35SChris Wilson 	return 0;
312486e83e35SChris Wilson }
312586e83e35SChris Wilson 
312686e83e35SChris Wilson static int i965_enable_vblank(struct drm_device *dev, unsigned int pipe)
312786e83e35SChris Wilson {
312886e83e35SChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
312986e83e35SChris Wilson 	unsigned long irqflags;
313086e83e35SChris Wilson 
313186e83e35SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
31327c463586SKeith Packard 	i915_enable_pipestat(dev_priv, pipe,
3133755e9019SImre Deak 			     PIPE_START_VBLANK_INTERRUPT_STATUS);
31341ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
31358692d00eSChris Wilson 
31360a3e67a4SJesse Barnes 	return 0;
31370a3e67a4SJesse Barnes }
31380a3e67a4SJesse Barnes 
313988e72717SThierry Reding static int ironlake_enable_vblank(struct drm_device *dev, unsigned int pipe)
3140f796cf8fSJesse Barnes {
3141fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3142f796cf8fSJesse Barnes 	unsigned long irqflags;
3143a9c287c9SJani Nikula 	u32 bit = INTEL_GEN(dev_priv) >= 7 ?
314486e83e35SChris Wilson 		DE_PIPE_VBLANK_IVB(pipe) : DE_PIPE_VBLANK(pipe);
3145f796cf8fSJesse Barnes 
3146f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3147fbdedaeaSVille Syrjälä 	ilk_enable_display_irq(dev_priv, bit);
3148b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3149b1f14ad0SJesse Barnes 
31502e8bf223SDhinakaran Pandiyan 	/* Even though there is no DMC, frame counter can get stuck when
31512e8bf223SDhinakaran Pandiyan 	 * PSR is active as no frames are generated.
31522e8bf223SDhinakaran Pandiyan 	 */
31532e8bf223SDhinakaran Pandiyan 	if (HAS_PSR(dev_priv))
31542e8bf223SDhinakaran Pandiyan 		drm_vblank_restore(dev, pipe);
31552e8bf223SDhinakaran Pandiyan 
3156b1f14ad0SJesse Barnes 	return 0;
3157b1f14ad0SJesse Barnes }
3158b1f14ad0SJesse Barnes 
315988e72717SThierry Reding static int gen8_enable_vblank(struct drm_device *dev, unsigned int pipe)
3160abd58f01SBen Widawsky {
3161fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3162abd58f01SBen Widawsky 	unsigned long irqflags;
3163abd58f01SBen Widawsky 
3164abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3165013d3752SVille Syrjälä 	bdw_enable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
3166abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3167013d3752SVille Syrjälä 
31682e8bf223SDhinakaran Pandiyan 	/* Even if there is no DMC, frame counter can get stuck when
31692e8bf223SDhinakaran Pandiyan 	 * PSR is active as no frames are generated, so check only for PSR.
31702e8bf223SDhinakaran Pandiyan 	 */
31712e8bf223SDhinakaran Pandiyan 	if (HAS_PSR(dev_priv))
31722e8bf223SDhinakaran Pandiyan 		drm_vblank_restore(dev, pipe);
31732e8bf223SDhinakaran Pandiyan 
3174abd58f01SBen Widawsky 	return 0;
3175abd58f01SBen Widawsky }
3176abd58f01SBen Widawsky 
317742f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
317842f52ef8SKeith Packard  * we use as a pipe index
317942f52ef8SKeith Packard  */
318086e83e35SChris Wilson static void i8xx_disable_vblank(struct drm_device *dev, unsigned int pipe)
318186e83e35SChris Wilson {
318286e83e35SChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
318386e83e35SChris Wilson 	unsigned long irqflags;
318486e83e35SChris Wilson 
318586e83e35SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
318686e83e35SChris Wilson 	i915_disable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_STATUS);
318786e83e35SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
318886e83e35SChris Wilson }
318986e83e35SChris Wilson 
319086e83e35SChris Wilson static void i965_disable_vblank(struct drm_device *dev, unsigned int pipe)
31910a3e67a4SJesse Barnes {
3192fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3193e9d21d7fSKeith Packard 	unsigned long irqflags;
31940a3e67a4SJesse Barnes 
31951ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
31967c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
3197755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
31981ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
31990a3e67a4SJesse Barnes }
32000a3e67a4SJesse Barnes 
320188e72717SThierry Reding static void ironlake_disable_vblank(struct drm_device *dev, unsigned int pipe)
3202f796cf8fSJesse Barnes {
3203fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3204f796cf8fSJesse Barnes 	unsigned long irqflags;
3205a9c287c9SJani Nikula 	u32 bit = INTEL_GEN(dev_priv) >= 7 ?
320686e83e35SChris Wilson 		DE_PIPE_VBLANK_IVB(pipe) : DE_PIPE_VBLANK(pipe);
3207f796cf8fSJesse Barnes 
3208f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3209fbdedaeaSVille Syrjälä 	ilk_disable_display_irq(dev_priv, bit);
3210b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3211b1f14ad0SJesse Barnes }
3212b1f14ad0SJesse Barnes 
321388e72717SThierry Reding static void gen8_disable_vblank(struct drm_device *dev, unsigned int pipe)
3214abd58f01SBen Widawsky {
3215fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3216abd58f01SBen Widawsky 	unsigned long irqflags;
3217abd58f01SBen Widawsky 
3218abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3219013d3752SVille Syrjälä 	bdw_disable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
3220abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3221abd58f01SBen Widawsky }
3222abd58f01SBen Widawsky 
3223b243f530STvrtko Ursulin static void ibx_irq_reset(struct drm_i915_private *dev_priv)
322491738a95SPaulo Zanoni {
32256e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
322691738a95SPaulo Zanoni 		return;
322791738a95SPaulo Zanoni 
32283488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(SDE);
3229105b122eSPaulo Zanoni 
32306e266956STvrtko Ursulin 	if (HAS_PCH_CPT(dev_priv) || HAS_PCH_LPT(dev_priv))
3231105b122eSPaulo Zanoni 		I915_WRITE(SERR_INT, 0xffffffff);
3232622364b6SPaulo Zanoni }
3233105b122eSPaulo Zanoni 
323491738a95SPaulo Zanoni /*
3235622364b6SPaulo Zanoni  * SDEIER is also touched by the interrupt handler to work around missed PCH
3236622364b6SPaulo Zanoni  * interrupts. Hence we can't update it after the interrupt handler is enabled -
3237622364b6SPaulo Zanoni  * instead we unconditionally enable all PCH interrupt sources here, but then
3238622364b6SPaulo Zanoni  * only unmask them as needed with SDEIMR.
3239622364b6SPaulo Zanoni  *
3240622364b6SPaulo Zanoni  * This function needs to be called before interrupts are enabled.
324191738a95SPaulo Zanoni  */
3242622364b6SPaulo Zanoni static void ibx_irq_pre_postinstall(struct drm_device *dev)
3243622364b6SPaulo Zanoni {
3244fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3245622364b6SPaulo Zanoni 
32466e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
3247622364b6SPaulo Zanoni 		return;
3248622364b6SPaulo Zanoni 
3249622364b6SPaulo Zanoni 	WARN_ON(I915_READ(SDEIER) != 0);
325091738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
325191738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
325291738a95SPaulo Zanoni }
325391738a95SPaulo Zanoni 
3254b243f530STvrtko Ursulin static void gen5_gt_irq_reset(struct drm_i915_private *dev_priv)
3255d18ea1b5SDaniel Vetter {
32563488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(GT);
3257b243f530STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 6)
32583488d4ebSVille Syrjälä 		GEN3_IRQ_RESET(GEN6_PM);
3259d18ea1b5SDaniel Vetter }
3260d18ea1b5SDaniel Vetter 
326170591a41SVille Syrjälä static void vlv_display_irq_reset(struct drm_i915_private *dev_priv)
326270591a41SVille Syrjälä {
326371b8b41dSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
326471b8b41dSVille Syrjälä 		I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
326571b8b41dSVille Syrjälä 	else
326671b8b41dSVille Syrjälä 		I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
326771b8b41dSVille Syrjälä 
3268ad22d106SVille Syrjälä 	i915_hotplug_interrupt_update_locked(dev_priv, 0xffffffff, 0);
326970591a41SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
327070591a41SVille Syrjälä 
327144d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
327270591a41SVille Syrjälä 
32733488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(VLV_);
32748bd099a7SChris Wilson 	dev_priv->irq_mask = ~0u;
327570591a41SVille Syrjälä }
327670591a41SVille Syrjälä 
32778bb61306SVille Syrjälä static void vlv_display_irq_postinstall(struct drm_i915_private *dev_priv)
32788bb61306SVille Syrjälä {
32798bb61306SVille Syrjälä 	u32 pipestat_mask;
32809ab981f2SVille Syrjälä 	u32 enable_mask;
32818bb61306SVille Syrjälä 	enum pipe pipe;
32828bb61306SVille Syrjälä 
3283842ebf7aSVille Syrjälä 	pipestat_mask = PIPE_CRC_DONE_INTERRUPT_STATUS;
32848bb61306SVille Syrjälä 
32858bb61306SVille Syrjälä 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
32868bb61306SVille Syrjälä 	for_each_pipe(dev_priv, pipe)
32878bb61306SVille Syrjälä 		i915_enable_pipestat(dev_priv, pipe, pipestat_mask);
32888bb61306SVille Syrjälä 
32899ab981f2SVille Syrjälä 	enable_mask = I915_DISPLAY_PORT_INTERRUPT |
32908bb61306SVille Syrjälä 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3291ebf5f921SVille Syrjälä 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3292ebf5f921SVille Syrjälä 		I915_LPE_PIPE_A_INTERRUPT |
3293ebf5f921SVille Syrjälä 		I915_LPE_PIPE_B_INTERRUPT;
3294ebf5f921SVille Syrjälä 
32958bb61306SVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
3296ebf5f921SVille Syrjälä 		enable_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT |
3297ebf5f921SVille Syrjälä 			I915_LPE_PIPE_C_INTERRUPT;
32986b7eafc1SVille Syrjälä 
32998bd099a7SChris Wilson 	WARN_ON(dev_priv->irq_mask != ~0u);
33006b7eafc1SVille Syrjälä 
33019ab981f2SVille Syrjälä 	dev_priv->irq_mask = ~enable_mask;
33028bb61306SVille Syrjälä 
33033488d4ebSVille Syrjälä 	GEN3_IRQ_INIT(VLV_, dev_priv->irq_mask, enable_mask);
33048bb61306SVille Syrjälä }
33058bb61306SVille Syrjälä 
33068bb61306SVille Syrjälä /* drm_dma.h hooks
33078bb61306SVille Syrjälä */
33088bb61306SVille Syrjälä static void ironlake_irq_reset(struct drm_device *dev)
33098bb61306SVille Syrjälä {
3310fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
33118bb61306SVille Syrjälä 
33123488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(DE);
3313cf819effSLucas De Marchi 	if (IS_GEN(dev_priv, 7))
33148bb61306SVille Syrjälä 		I915_WRITE(GEN7_ERR_INT, 0xffffffff);
33158bb61306SVille Syrjälä 
3316fc340442SDaniel Vetter 	if (IS_HASWELL(dev_priv)) {
3317fc340442SDaniel Vetter 		I915_WRITE(EDP_PSR_IMR, 0xffffffff);
3318fc340442SDaniel Vetter 		I915_WRITE(EDP_PSR_IIR, 0xffffffff);
3319fc340442SDaniel Vetter 	}
3320fc340442SDaniel Vetter 
3321b243f530STvrtko Ursulin 	gen5_gt_irq_reset(dev_priv);
33228bb61306SVille Syrjälä 
3323b243f530STvrtko Ursulin 	ibx_irq_reset(dev_priv);
33248bb61306SVille Syrjälä }
33258bb61306SVille Syrjälä 
33266bcdb1c8SVille Syrjälä static void valleyview_irq_reset(struct drm_device *dev)
33277e231dbeSJesse Barnes {
3328fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
33297e231dbeSJesse Barnes 
333034c7b8a7SVille Syrjälä 	I915_WRITE(VLV_MASTER_IER, 0);
333134c7b8a7SVille Syrjälä 	POSTING_READ(VLV_MASTER_IER);
333234c7b8a7SVille Syrjälä 
3333b243f530STvrtko Ursulin 	gen5_gt_irq_reset(dev_priv);
33347e231dbeSJesse Barnes 
3335ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
33369918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
333770591a41SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3338ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
33397e231dbeSJesse Barnes }
33407e231dbeSJesse Barnes 
3341d6e3cca3SDaniel Vetter static void gen8_gt_irq_reset(struct drm_i915_private *dev_priv)
3342d6e3cca3SDaniel Vetter {
3343d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 0);
3344d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 1);
3345d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 2);
3346d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 3);
3347d6e3cca3SDaniel Vetter }
3348d6e3cca3SDaniel Vetter 
3349823f6b38SPaulo Zanoni static void gen8_irq_reset(struct drm_device *dev)
3350abd58f01SBen Widawsky {
3351fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3352abd58f01SBen Widawsky 	int pipe;
3353abd58f01SBen Widawsky 
33544376b9c9SMika Kuoppala 	gen8_master_intr_disable(dev_priv->regs);
3355abd58f01SBen Widawsky 
3356d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
3357abd58f01SBen Widawsky 
3358e04f7eceSVille Syrjälä 	I915_WRITE(EDP_PSR_IMR, 0xffffffff);
3359e04f7eceSVille Syrjälä 	I915_WRITE(EDP_PSR_IIR, 0xffffffff);
3360e04f7eceSVille Syrjälä 
3361055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3362f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3363813bde43SPaulo Zanoni 						   POWER_DOMAIN_PIPE(pipe)))
3364f86f3fb0SPaulo Zanoni 			GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3365abd58f01SBen Widawsky 
33663488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(GEN8_DE_PORT_);
33673488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(GEN8_DE_MISC_);
33683488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(GEN8_PCU_);
3369abd58f01SBen Widawsky 
33706e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
3371b243f530STvrtko Ursulin 		ibx_irq_reset(dev_priv);
3372abd58f01SBen Widawsky }
3373abd58f01SBen Widawsky 
337451951ae7SMika Kuoppala static void gen11_gt_irq_reset(struct drm_i915_private *dev_priv)
337551951ae7SMika Kuoppala {
337651951ae7SMika Kuoppala 	/* Disable RCS, BCS, VCS and VECS class engines. */
337751951ae7SMika Kuoppala 	I915_WRITE(GEN11_RENDER_COPY_INTR_ENABLE, 0);
337851951ae7SMika Kuoppala 	I915_WRITE(GEN11_VCS_VECS_INTR_ENABLE,	  0);
337951951ae7SMika Kuoppala 
338051951ae7SMika Kuoppala 	/* Restore masks irqs on RCS, BCS, VCS and VECS engines. */
338151951ae7SMika Kuoppala 	I915_WRITE(GEN11_RCS0_RSVD_INTR_MASK,	~0);
338251951ae7SMika Kuoppala 	I915_WRITE(GEN11_BCS_RSVD_INTR_MASK,	~0);
338351951ae7SMika Kuoppala 	I915_WRITE(GEN11_VCS0_VCS1_INTR_MASK,	~0);
338451951ae7SMika Kuoppala 	I915_WRITE(GEN11_VCS2_VCS3_INTR_MASK,	~0);
338551951ae7SMika Kuoppala 	I915_WRITE(GEN11_VECS0_VECS1_INTR_MASK,	~0);
3386d02b98b8SOscar Mateo 
3387d02b98b8SOscar Mateo 	I915_WRITE(GEN11_GPM_WGBOXPERF_INTR_ENABLE, 0);
3388d02b98b8SOscar Mateo 	I915_WRITE(GEN11_GPM_WGBOXPERF_INTR_MASK,  ~0);
338951951ae7SMika Kuoppala }
339051951ae7SMika Kuoppala 
339151951ae7SMika Kuoppala static void gen11_irq_reset(struct drm_device *dev)
339251951ae7SMika Kuoppala {
339351951ae7SMika Kuoppala 	struct drm_i915_private *dev_priv = dev->dev_private;
339451951ae7SMika Kuoppala 	int pipe;
339551951ae7SMika Kuoppala 
339681067b71SMika Kuoppala 	gen11_master_intr_disable(dev_priv->regs);
339751951ae7SMika Kuoppala 
339851951ae7SMika Kuoppala 	gen11_gt_irq_reset(dev_priv);
339951951ae7SMika Kuoppala 
340051951ae7SMika Kuoppala 	I915_WRITE(GEN11_DISPLAY_INT_CTL, 0);
340151951ae7SMika Kuoppala 
340262819dfdSJosé Roberto de Souza 	I915_WRITE(EDP_PSR_IMR, 0xffffffff);
340362819dfdSJosé Roberto de Souza 	I915_WRITE(EDP_PSR_IIR, 0xffffffff);
340462819dfdSJosé Roberto de Souza 
340551951ae7SMika Kuoppala 	for_each_pipe(dev_priv, pipe)
340651951ae7SMika Kuoppala 		if (intel_display_power_is_enabled(dev_priv,
340751951ae7SMika Kuoppala 						   POWER_DOMAIN_PIPE(pipe)))
340851951ae7SMika Kuoppala 			GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
340951951ae7SMika Kuoppala 
341051951ae7SMika Kuoppala 	GEN3_IRQ_RESET(GEN8_DE_PORT_);
341151951ae7SMika Kuoppala 	GEN3_IRQ_RESET(GEN8_DE_MISC_);
3412121e758eSDhinakaran Pandiyan 	GEN3_IRQ_RESET(GEN11_DE_HPD_);
3413df0d28c1SDhinakaran Pandiyan 	GEN3_IRQ_RESET(GEN11_GU_MISC_);
341451951ae7SMika Kuoppala 	GEN3_IRQ_RESET(GEN8_PCU_);
341531604222SAnusha Srivatsa 
3416*29b43ae2SRodrigo Vivi 	if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
341731604222SAnusha Srivatsa 		GEN3_IRQ_RESET(SDE);
341851951ae7SMika Kuoppala }
341951951ae7SMika Kuoppala 
34204c6c03beSDamien Lespiau void gen8_irq_power_well_post_enable(struct drm_i915_private *dev_priv,
3421001bd2cbSImre Deak 				     u8 pipe_mask)
3422d49bdb0eSPaulo Zanoni {
3423a9c287c9SJani Nikula 	u32 extra_ier = GEN8_PIPE_VBLANK | GEN8_PIPE_FIFO_UNDERRUN;
34246831f3e3SVille Syrjälä 	enum pipe pipe;
3425d49bdb0eSPaulo Zanoni 
342613321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
34279dfe2e3aSImre Deak 
34289dfe2e3aSImre Deak 	if (!intel_irqs_enabled(dev_priv)) {
34299dfe2e3aSImre Deak 		spin_unlock_irq(&dev_priv->irq_lock);
34309dfe2e3aSImre Deak 		return;
34319dfe2e3aSImre Deak 	}
34329dfe2e3aSImre Deak 
34336831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
34346831f3e3SVille Syrjälä 		GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
34356831f3e3SVille Syrjälä 				  dev_priv->de_irq_mask[pipe],
34366831f3e3SVille Syrjälä 				  ~dev_priv->de_irq_mask[pipe] | extra_ier);
34379dfe2e3aSImre Deak 
343813321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3439d49bdb0eSPaulo Zanoni }
3440d49bdb0eSPaulo Zanoni 
3441aae8ba84SVille Syrjälä void gen8_irq_power_well_pre_disable(struct drm_i915_private *dev_priv,
3442001bd2cbSImre Deak 				     u8 pipe_mask)
3443aae8ba84SVille Syrjälä {
34446831f3e3SVille Syrjälä 	enum pipe pipe;
34456831f3e3SVille Syrjälä 
3446aae8ba84SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
34479dfe2e3aSImre Deak 
34489dfe2e3aSImre Deak 	if (!intel_irqs_enabled(dev_priv)) {
34499dfe2e3aSImre Deak 		spin_unlock_irq(&dev_priv->irq_lock);
34509dfe2e3aSImre Deak 		return;
34519dfe2e3aSImre Deak 	}
34529dfe2e3aSImre Deak 
34536831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
34546831f3e3SVille Syrjälä 		GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
34559dfe2e3aSImre Deak 
3456aae8ba84SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3457aae8ba84SVille Syrjälä 
3458aae8ba84SVille Syrjälä 	/* make sure we're done processing display irqs */
345991c8a326SChris Wilson 	synchronize_irq(dev_priv->drm.irq);
3460aae8ba84SVille Syrjälä }
3461aae8ba84SVille Syrjälä 
34626bcdb1c8SVille Syrjälä static void cherryview_irq_reset(struct drm_device *dev)
346343f328d7SVille Syrjälä {
3464fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
346543f328d7SVille Syrjälä 
346643f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
346743f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
346843f328d7SVille Syrjälä 
3469d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
347043f328d7SVille Syrjälä 
34713488d4ebSVille Syrjälä 	GEN3_IRQ_RESET(GEN8_PCU_);
347243f328d7SVille Syrjälä 
3473ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
34749918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
347570591a41SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3476ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
347743f328d7SVille Syrjälä }
347843f328d7SVille Syrjälä 
347991d14251STvrtko Ursulin static u32 intel_hpd_enabled_irqs(struct drm_i915_private *dev_priv,
348087a02106SVille Syrjälä 				  const u32 hpd[HPD_NUM_PINS])
348187a02106SVille Syrjälä {
348287a02106SVille Syrjälä 	struct intel_encoder *encoder;
348387a02106SVille Syrjälä 	u32 enabled_irqs = 0;
348487a02106SVille Syrjälä 
348591c8a326SChris Wilson 	for_each_intel_encoder(&dev_priv->drm, encoder)
348687a02106SVille Syrjälä 		if (dev_priv->hotplug.stats[encoder->hpd_pin].state == HPD_ENABLED)
348787a02106SVille Syrjälä 			enabled_irqs |= hpd[encoder->hpd_pin];
348887a02106SVille Syrjälä 
348987a02106SVille Syrjälä 	return enabled_irqs;
349087a02106SVille Syrjälä }
349187a02106SVille Syrjälä 
34921a56b1a2SImre Deak static void ibx_hpd_detection_setup(struct drm_i915_private *dev_priv)
34931a56b1a2SImre Deak {
34941a56b1a2SImre Deak 	u32 hotplug;
34951a56b1a2SImre Deak 
34961a56b1a2SImre Deak 	/*
34971a56b1a2SImre Deak 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
34981a56b1a2SImre Deak 	 * duration to 2ms (which is the minimum in the Display Port spec).
34991a56b1a2SImre Deak 	 * The pulse duration bits are reserved on LPT+.
35001a56b1a2SImre Deak 	 */
35011a56b1a2SImre Deak 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
35021a56b1a2SImre Deak 	hotplug &= ~(PORTB_PULSE_DURATION_MASK |
35031a56b1a2SImre Deak 		     PORTC_PULSE_DURATION_MASK |
35041a56b1a2SImre Deak 		     PORTD_PULSE_DURATION_MASK);
35051a56b1a2SImre Deak 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
35061a56b1a2SImre Deak 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
35071a56b1a2SImre Deak 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
35081a56b1a2SImre Deak 	/*
35091a56b1a2SImre Deak 	 * When CPU and PCH are on the same package, port A
35101a56b1a2SImre Deak 	 * HPD must be enabled in both north and south.
35111a56b1a2SImre Deak 	 */
35121a56b1a2SImre Deak 	if (HAS_PCH_LPT_LP(dev_priv))
35131a56b1a2SImre Deak 		hotplug |= PORTA_HOTPLUG_ENABLE;
35141a56b1a2SImre Deak 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
35151a56b1a2SImre Deak }
35161a56b1a2SImre Deak 
351791d14251STvrtko Ursulin static void ibx_hpd_irq_setup(struct drm_i915_private *dev_priv)
351882a28bcfSDaniel Vetter {
35191a56b1a2SImre Deak 	u32 hotplug_irqs, enabled_irqs;
352082a28bcfSDaniel Vetter 
352191d14251STvrtko Ursulin 	if (HAS_PCH_IBX(dev_priv)) {
3522fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
352391d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ibx);
352482a28bcfSDaniel Vetter 	} else {
3525fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
352691d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_cpt);
352782a28bcfSDaniel Vetter 	}
352882a28bcfSDaniel Vetter 
3529fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
353082a28bcfSDaniel Vetter 
35311a56b1a2SImre Deak 	ibx_hpd_detection_setup(dev_priv);
35326dbf30ceSVille Syrjälä }
353326951cafSXiong Zhang 
353431604222SAnusha Srivatsa static void icp_hpd_detection_setup(struct drm_i915_private *dev_priv)
353531604222SAnusha Srivatsa {
353631604222SAnusha Srivatsa 	u32 hotplug;
353731604222SAnusha Srivatsa 
353831604222SAnusha Srivatsa 	hotplug = I915_READ(SHOTPLUG_CTL_DDI);
353931604222SAnusha Srivatsa 	hotplug |= ICP_DDIA_HPD_ENABLE |
354031604222SAnusha Srivatsa 		   ICP_DDIB_HPD_ENABLE;
354131604222SAnusha Srivatsa 	I915_WRITE(SHOTPLUG_CTL_DDI, hotplug);
354231604222SAnusha Srivatsa 
354331604222SAnusha Srivatsa 	hotplug = I915_READ(SHOTPLUG_CTL_TC);
354431604222SAnusha Srivatsa 	hotplug |= ICP_TC_HPD_ENABLE(PORT_TC1) |
354531604222SAnusha Srivatsa 		   ICP_TC_HPD_ENABLE(PORT_TC2) |
354631604222SAnusha Srivatsa 		   ICP_TC_HPD_ENABLE(PORT_TC3) |
354731604222SAnusha Srivatsa 		   ICP_TC_HPD_ENABLE(PORT_TC4);
354831604222SAnusha Srivatsa 	I915_WRITE(SHOTPLUG_CTL_TC, hotplug);
354931604222SAnusha Srivatsa }
355031604222SAnusha Srivatsa 
355131604222SAnusha Srivatsa static void icp_hpd_irq_setup(struct drm_i915_private *dev_priv)
355231604222SAnusha Srivatsa {
355331604222SAnusha Srivatsa 	u32 hotplug_irqs, enabled_irqs;
355431604222SAnusha Srivatsa 
355531604222SAnusha Srivatsa 	hotplug_irqs = SDE_DDI_MASK_ICP | SDE_TC_MASK_ICP;
355631604222SAnusha Srivatsa 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_icp);
355731604222SAnusha Srivatsa 
355831604222SAnusha Srivatsa 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
355931604222SAnusha Srivatsa 
356031604222SAnusha Srivatsa 	icp_hpd_detection_setup(dev_priv);
356131604222SAnusha Srivatsa }
356231604222SAnusha Srivatsa 
3563121e758eSDhinakaran Pandiyan static void gen11_hpd_detection_setup(struct drm_i915_private *dev_priv)
3564121e758eSDhinakaran Pandiyan {
3565121e758eSDhinakaran Pandiyan 	u32 hotplug;
3566121e758eSDhinakaran Pandiyan 
3567121e758eSDhinakaran Pandiyan 	hotplug = I915_READ(GEN11_TC_HOTPLUG_CTL);
3568121e758eSDhinakaran Pandiyan 	hotplug |= GEN11_HOTPLUG_CTL_ENABLE(PORT_TC1) |
3569121e758eSDhinakaran Pandiyan 		   GEN11_HOTPLUG_CTL_ENABLE(PORT_TC2) |
3570121e758eSDhinakaran Pandiyan 		   GEN11_HOTPLUG_CTL_ENABLE(PORT_TC3) |
3571121e758eSDhinakaran Pandiyan 		   GEN11_HOTPLUG_CTL_ENABLE(PORT_TC4);
3572121e758eSDhinakaran Pandiyan 	I915_WRITE(GEN11_TC_HOTPLUG_CTL, hotplug);
3573b796b971SDhinakaran Pandiyan 
3574b796b971SDhinakaran Pandiyan 	hotplug = I915_READ(GEN11_TBT_HOTPLUG_CTL);
3575b796b971SDhinakaran Pandiyan 	hotplug |= GEN11_HOTPLUG_CTL_ENABLE(PORT_TC1) |
3576b796b971SDhinakaran Pandiyan 		   GEN11_HOTPLUG_CTL_ENABLE(PORT_TC2) |
3577b796b971SDhinakaran Pandiyan 		   GEN11_HOTPLUG_CTL_ENABLE(PORT_TC3) |
3578b796b971SDhinakaran Pandiyan 		   GEN11_HOTPLUG_CTL_ENABLE(PORT_TC4);
3579b796b971SDhinakaran Pandiyan 	I915_WRITE(GEN11_TBT_HOTPLUG_CTL, hotplug);
3580121e758eSDhinakaran Pandiyan }
3581121e758eSDhinakaran Pandiyan 
3582121e758eSDhinakaran Pandiyan static void gen11_hpd_irq_setup(struct drm_i915_private *dev_priv)
3583121e758eSDhinakaran Pandiyan {
3584121e758eSDhinakaran Pandiyan 	u32 hotplug_irqs, enabled_irqs;
3585121e758eSDhinakaran Pandiyan 	u32 val;
3586121e758eSDhinakaran Pandiyan 
3587b796b971SDhinakaran Pandiyan 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_gen11);
3588b796b971SDhinakaran Pandiyan 	hotplug_irqs = GEN11_DE_TC_HOTPLUG_MASK | GEN11_DE_TBT_HOTPLUG_MASK;
3589121e758eSDhinakaran Pandiyan 
3590121e758eSDhinakaran Pandiyan 	val = I915_READ(GEN11_DE_HPD_IMR);
3591121e758eSDhinakaran Pandiyan 	val &= ~hotplug_irqs;
3592121e758eSDhinakaran Pandiyan 	I915_WRITE(GEN11_DE_HPD_IMR, val);
3593121e758eSDhinakaran Pandiyan 	POSTING_READ(GEN11_DE_HPD_IMR);
3594121e758eSDhinakaran Pandiyan 
3595121e758eSDhinakaran Pandiyan 	gen11_hpd_detection_setup(dev_priv);
359631604222SAnusha Srivatsa 
3597*29b43ae2SRodrigo Vivi 	if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
359831604222SAnusha Srivatsa 		icp_hpd_irq_setup(dev_priv);
3599121e758eSDhinakaran Pandiyan }
3600121e758eSDhinakaran Pandiyan 
36012a57d9ccSImre Deak static void spt_hpd_detection_setup(struct drm_i915_private *dev_priv)
36022a57d9ccSImre Deak {
36033b92e263SRodrigo Vivi 	u32 val, hotplug;
36043b92e263SRodrigo Vivi 
36053b92e263SRodrigo Vivi 	/* Display WA #1179 WaHardHangonHotPlug: cnp */
36063b92e263SRodrigo Vivi 	if (HAS_PCH_CNP(dev_priv)) {
36073b92e263SRodrigo Vivi 		val = I915_READ(SOUTH_CHICKEN1);
36083b92e263SRodrigo Vivi 		val &= ~CHASSIS_CLK_REQ_DURATION_MASK;
36093b92e263SRodrigo Vivi 		val |= CHASSIS_CLK_REQ_DURATION(0xf);
36103b92e263SRodrigo Vivi 		I915_WRITE(SOUTH_CHICKEN1, val);
36113b92e263SRodrigo Vivi 	}
36122a57d9ccSImre Deak 
36132a57d9ccSImre Deak 	/* Enable digital hotplug on the PCH */
36142a57d9ccSImre Deak 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
36152a57d9ccSImre Deak 	hotplug |= PORTA_HOTPLUG_ENABLE |
36162a57d9ccSImre Deak 		   PORTB_HOTPLUG_ENABLE |
36172a57d9ccSImre Deak 		   PORTC_HOTPLUG_ENABLE |
36182a57d9ccSImre Deak 		   PORTD_HOTPLUG_ENABLE;
36192a57d9ccSImre Deak 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
36202a57d9ccSImre Deak 
36212a57d9ccSImre Deak 	hotplug = I915_READ(PCH_PORT_HOTPLUG2);
36222a57d9ccSImre Deak 	hotplug |= PORTE_HOTPLUG_ENABLE;
36232a57d9ccSImre Deak 	I915_WRITE(PCH_PORT_HOTPLUG2, hotplug);
36242a57d9ccSImre Deak }
36252a57d9ccSImre Deak 
362691d14251STvrtko Ursulin static void spt_hpd_irq_setup(struct drm_i915_private *dev_priv)
36276dbf30ceSVille Syrjälä {
36282a57d9ccSImre Deak 	u32 hotplug_irqs, enabled_irqs;
36296dbf30ceSVille Syrjälä 
36306dbf30ceSVille Syrjälä 	hotplug_irqs = SDE_HOTPLUG_MASK_SPT;
363191d14251STvrtko Ursulin 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_spt);
36326dbf30ceSVille Syrjälä 
36336dbf30ceSVille Syrjälä 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
36346dbf30ceSVille Syrjälä 
36352a57d9ccSImre Deak 	spt_hpd_detection_setup(dev_priv);
363626951cafSXiong Zhang }
36377fe0b973SKeith Packard 
36381a56b1a2SImre Deak static void ilk_hpd_detection_setup(struct drm_i915_private *dev_priv)
36391a56b1a2SImre Deak {
36401a56b1a2SImre Deak 	u32 hotplug;
36411a56b1a2SImre Deak 
36421a56b1a2SImre Deak 	/*
36431a56b1a2SImre Deak 	 * Enable digital hotplug on the CPU, and configure the DP short pulse
36441a56b1a2SImre Deak 	 * duration to 2ms (which is the minimum in the Display Port spec)
36451a56b1a2SImre Deak 	 * The pulse duration bits are reserved on HSW+.
36461a56b1a2SImre Deak 	 */
36471a56b1a2SImre Deak 	hotplug = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
36481a56b1a2SImre Deak 	hotplug &= ~DIGITAL_PORTA_PULSE_DURATION_MASK;
36491a56b1a2SImre Deak 	hotplug |= DIGITAL_PORTA_HOTPLUG_ENABLE |
36501a56b1a2SImre Deak 		   DIGITAL_PORTA_PULSE_DURATION_2ms;
36511a56b1a2SImre Deak 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, hotplug);
36521a56b1a2SImre Deak }
36531a56b1a2SImre Deak 
365491d14251STvrtko Ursulin static void ilk_hpd_irq_setup(struct drm_i915_private *dev_priv)
3655e4ce95aaSVille Syrjälä {
36561a56b1a2SImre Deak 	u32 hotplug_irqs, enabled_irqs;
3657e4ce95aaSVille Syrjälä 
365891d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 8) {
36593a3b3c7dSVille Syrjälä 		hotplug_irqs = GEN8_PORT_DP_A_HOTPLUG;
366091d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_bdw);
36613a3b3c7dSVille Syrjälä 
36623a3b3c7dSVille Syrjälä 		bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
366391d14251STvrtko Ursulin 	} else if (INTEL_GEN(dev_priv) >= 7) {
366423bb4cb5SVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG_IVB;
366591d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ivb);
36663a3b3c7dSVille Syrjälä 
36673a3b3c7dSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
366823bb4cb5SVille Syrjälä 	} else {
3669e4ce95aaSVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG;
367091d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ilk);
3671e4ce95aaSVille Syrjälä 
3672e4ce95aaSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
36733a3b3c7dSVille Syrjälä 	}
3674e4ce95aaSVille Syrjälä 
36751a56b1a2SImre Deak 	ilk_hpd_detection_setup(dev_priv);
3676e4ce95aaSVille Syrjälä 
367791d14251STvrtko Ursulin 	ibx_hpd_irq_setup(dev_priv);
3678e4ce95aaSVille Syrjälä }
3679e4ce95aaSVille Syrjälä 
36802a57d9ccSImre Deak static void __bxt_hpd_detection_setup(struct drm_i915_private *dev_priv,
36812a57d9ccSImre Deak 				      u32 enabled_irqs)
3682e0a20ad7SShashank Sharma {
36832a57d9ccSImre Deak 	u32 hotplug;
3684e0a20ad7SShashank Sharma 
3685a52bb15bSVille Syrjälä 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
36862a57d9ccSImre Deak 	hotplug |= PORTA_HOTPLUG_ENABLE |
36872a57d9ccSImre Deak 		   PORTB_HOTPLUG_ENABLE |
36882a57d9ccSImre Deak 		   PORTC_HOTPLUG_ENABLE;
3689d252bf68SShubhangi Shrivastava 
3690d252bf68SShubhangi Shrivastava 	DRM_DEBUG_KMS("Invert bit setting: hp_ctl:%x hp_port:%x\n",
3691d252bf68SShubhangi Shrivastava 		      hotplug, enabled_irqs);
3692d252bf68SShubhangi Shrivastava 	hotplug &= ~BXT_DDI_HPD_INVERT_MASK;
3693d252bf68SShubhangi Shrivastava 
3694d252bf68SShubhangi Shrivastava 	/*
3695d252bf68SShubhangi Shrivastava 	 * For BXT invert bit has to be set based on AOB design
3696d252bf68SShubhangi Shrivastava 	 * for HPD detection logic, update it based on VBT fields.
3697d252bf68SShubhangi Shrivastava 	 */
3698d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIA) &&
3699d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_A))
3700d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIA_HPD_INVERT;
3701d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIB) &&
3702d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_B))
3703d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIB_HPD_INVERT;
3704d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIC) &&
3705d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_C))
3706d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIC_HPD_INVERT;
3707d252bf68SShubhangi Shrivastava 
3708a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
3709e0a20ad7SShashank Sharma }
3710e0a20ad7SShashank Sharma 
37112a57d9ccSImre Deak static void bxt_hpd_detection_setup(struct drm_i915_private *dev_priv)
37122a57d9ccSImre Deak {
37132a57d9ccSImre Deak 	__bxt_hpd_detection_setup(dev_priv, BXT_DE_PORT_HOTPLUG_MASK);
37142a57d9ccSImre Deak }
37152a57d9ccSImre Deak 
37162a57d9ccSImre Deak static void bxt_hpd_irq_setup(struct drm_i915_private *dev_priv)
37172a57d9ccSImre Deak {
37182a57d9ccSImre Deak 	u32 hotplug_irqs, enabled_irqs;
37192a57d9ccSImre Deak 
37202a57d9ccSImre Deak 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_bxt);
37212a57d9ccSImre Deak 	hotplug_irqs = BXT_DE_PORT_HOTPLUG_MASK;
37222a57d9ccSImre Deak 
37232a57d9ccSImre Deak 	bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
37242a57d9ccSImre Deak 
37252a57d9ccSImre Deak 	__bxt_hpd_detection_setup(dev_priv, enabled_irqs);
37262a57d9ccSImre Deak }
37272a57d9ccSImre Deak 
3728d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
3729d46da437SPaulo Zanoni {
3730fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
373182a28bcfSDaniel Vetter 	u32 mask;
3732d46da437SPaulo Zanoni 
37336e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
3734692a04cfSDaniel Vetter 		return;
3735692a04cfSDaniel Vetter 
37366e266956STvrtko Ursulin 	if (HAS_PCH_IBX(dev_priv))
37375c673b60SDaniel Vetter 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
37384ebc6509SDhinakaran Pandiyan 	else if (HAS_PCH_CPT(dev_priv) || HAS_PCH_LPT(dev_priv))
37395c673b60SDaniel Vetter 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
37404ebc6509SDhinakaran Pandiyan 	else
37414ebc6509SDhinakaran Pandiyan 		mask = SDE_GMBUS_CPT;
37428664281bSPaulo Zanoni 
37433488d4ebSVille Syrjälä 	gen3_assert_iir_is_zero(dev_priv, SDEIIR);
3744d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
37452a57d9ccSImre Deak 
37462a57d9ccSImre Deak 	if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
37472a57d9ccSImre Deak 	    HAS_PCH_LPT(dev_priv))
37481a56b1a2SImre Deak 		ibx_hpd_detection_setup(dev_priv);
37492a57d9ccSImre Deak 	else
37502a57d9ccSImre Deak 		spt_hpd_detection_setup(dev_priv);
3751d46da437SPaulo Zanoni }
3752d46da437SPaulo Zanoni 
37530a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
37540a9a8c91SDaniel Vetter {
3755fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
37560a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
37570a9a8c91SDaniel Vetter 
37580a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
37590a9a8c91SDaniel Vetter 
37600a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
37613c9192bcSTvrtko Ursulin 	if (HAS_L3_DPF(dev_priv)) {
37620a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
3763772c2a51STvrtko Ursulin 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev_priv);
3764772c2a51STvrtko Ursulin 		gt_irqs |= GT_PARITY_ERROR(dev_priv);
37650a9a8c91SDaniel Vetter 	}
37660a9a8c91SDaniel Vetter 
37670a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
3768cf819effSLucas De Marchi 	if (IS_GEN(dev_priv, 5)) {
3769f8973c21SChris Wilson 		gt_irqs |= ILK_BSD_USER_INTERRUPT;
37700a9a8c91SDaniel Vetter 	} else {
37710a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
37720a9a8c91SDaniel Vetter 	}
37730a9a8c91SDaniel Vetter 
37743488d4ebSVille Syrjälä 	GEN3_IRQ_INIT(GT, dev_priv->gt_irq_mask, gt_irqs);
37750a9a8c91SDaniel Vetter 
3776b243f530STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 6) {
377778e68d36SImre Deak 		/*
377878e68d36SImre Deak 		 * RPS interrupts will get enabled/disabled on demand when RPS
377978e68d36SImre Deak 		 * itself is enabled/disabled.
378078e68d36SImre Deak 		 */
37818a68d464SChris Wilson 		if (HAS_ENGINE(dev_priv, VECS0)) {
37820a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
3783f4e9af4fSAkash Goel 			dev_priv->pm_ier |= PM_VEBOX_USER_INTERRUPT;
3784f4e9af4fSAkash Goel 		}
37850a9a8c91SDaniel Vetter 
3786f4e9af4fSAkash Goel 		dev_priv->pm_imr = 0xffffffff;
37873488d4ebSVille Syrjälä 		GEN3_IRQ_INIT(GEN6_PM, dev_priv->pm_imr, pm_irqs);
37880a9a8c91SDaniel Vetter 	}
37890a9a8c91SDaniel Vetter }
37900a9a8c91SDaniel Vetter 
3791f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
3792036a4a7dSZhenyu Wang {
3793fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
37948e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
37958e76f8dcSPaulo Zanoni 
3796b243f530STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 7) {
37978e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
3798842ebf7aSVille Syrjälä 				DE_PCH_EVENT_IVB | DE_AUX_CHANNEL_A_IVB);
37998e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
380023bb4cb5SVille Syrjälä 			      DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB |
380123bb4cb5SVille Syrjälä 			      DE_DP_A_HOTPLUG_IVB);
38028e76f8dcSPaulo Zanoni 	} else {
38038e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3804842ebf7aSVille Syrjälä 				DE_AUX_CHANNEL_A | DE_PIPEB_CRC_DONE |
3805842ebf7aSVille Syrjälä 				DE_PIPEA_CRC_DONE | DE_POISON);
3806e4ce95aaSVille Syrjälä 		extra_mask = (DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
3807e4ce95aaSVille Syrjälä 			      DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN |
3808e4ce95aaSVille Syrjälä 			      DE_DP_A_HOTPLUG);
38098e76f8dcSPaulo Zanoni 	}
3810036a4a7dSZhenyu Wang 
3811fc340442SDaniel Vetter 	if (IS_HASWELL(dev_priv)) {
3812fc340442SDaniel Vetter 		gen3_assert_iir_is_zero(dev_priv, EDP_PSR_IIR);
38131aeb1b5fSDhinakaran Pandiyan 		intel_psr_irq_control(dev_priv, dev_priv->psr.debug);
3814fc340442SDaniel Vetter 		display_mask |= DE_EDP_PSR_INT_HSW;
3815fc340442SDaniel Vetter 	}
3816fc340442SDaniel Vetter 
38171ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3818036a4a7dSZhenyu Wang 
3819622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3820622364b6SPaulo Zanoni 
38213488d4ebSVille Syrjälä 	GEN3_IRQ_INIT(DE, dev_priv->irq_mask, display_mask | extra_mask);
3822036a4a7dSZhenyu Wang 
38230a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
3824036a4a7dSZhenyu Wang 
38251a56b1a2SImre Deak 	ilk_hpd_detection_setup(dev_priv);
38261a56b1a2SImre Deak 
3827d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
38287fe0b973SKeith Packard 
382950a0bc90STvrtko Ursulin 	if (IS_IRONLAKE_M(dev_priv)) {
38306005ce42SDaniel Vetter 		/* Enable PCU event interrupts
38316005ce42SDaniel Vetter 		 *
38326005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
38334bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
38344bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
3835d6207435SDaniel Vetter 		spin_lock_irq(&dev_priv->irq_lock);
3836fbdedaeaSVille Syrjälä 		ilk_enable_display_irq(dev_priv, DE_PCU_EVENT);
3837d6207435SDaniel Vetter 		spin_unlock_irq(&dev_priv->irq_lock);
3838f97108d1SJesse Barnes 	}
3839f97108d1SJesse Barnes 
3840036a4a7dSZhenyu Wang 	return 0;
3841036a4a7dSZhenyu Wang }
3842036a4a7dSZhenyu Wang 
3843f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3844f8b79e58SImre Deak {
384567520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
3846f8b79e58SImre Deak 
3847f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3848f8b79e58SImre Deak 		return;
3849f8b79e58SImre Deak 
3850f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3851f8b79e58SImre Deak 
3852d6c69803SVille Syrjälä 	if (intel_irqs_enabled(dev_priv)) {
3853d6c69803SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3854ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3855f8b79e58SImre Deak 	}
3856d6c69803SVille Syrjälä }
3857f8b79e58SImre Deak 
3858f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3859f8b79e58SImre Deak {
386067520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
3861f8b79e58SImre Deak 
3862f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3863f8b79e58SImre Deak 		return;
3864f8b79e58SImre Deak 
3865f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3866f8b79e58SImre Deak 
3867950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3868ad22d106SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3869f8b79e58SImre Deak }
3870f8b79e58SImre Deak 
38710e6c9a9eSVille Syrjälä 
38720e6c9a9eSVille Syrjälä static int valleyview_irq_postinstall(struct drm_device *dev)
38730e6c9a9eSVille Syrjälä {
3874fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
38750e6c9a9eSVille Syrjälä 
38760a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
38777e231dbeSJesse Barnes 
3878ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
38799918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3880ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3881ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3882ad22d106SVille Syrjälä 
38837e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
388434c7b8a7SVille Syrjälä 	POSTING_READ(VLV_MASTER_IER);
388520afbda2SDaniel Vetter 
388620afbda2SDaniel Vetter 	return 0;
388720afbda2SDaniel Vetter }
388820afbda2SDaniel Vetter 
3889abd58f01SBen Widawsky static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
3890abd58f01SBen Widawsky {
3891abd58f01SBen Widawsky 	/* These are interrupts we'll toggle with the ring mask register */
3892a9c287c9SJani Nikula 	u32 gt_interrupts[] = {
38938a68d464SChris Wilson 		(GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
389473d477f6SOscar Mateo 		 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
389573d477f6SOscar Mateo 		 GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT |
38968a68d464SChris Wilson 		 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT),
38978a68d464SChris Wilson 
38988a68d464SChris Wilson 		(GT_RENDER_USER_INTERRUPT << GEN8_VCS0_IRQ_SHIFT |
38998a68d464SChris Wilson 		 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS0_IRQ_SHIFT |
3900abd58f01SBen Widawsky 		 GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
39018a68d464SChris Wilson 		 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT),
39028a68d464SChris Wilson 
3903abd58f01SBen Widawsky 		0,
39048a68d464SChris Wilson 
39058a68d464SChris Wilson 		(GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT |
39068a68d464SChris Wilson 		 GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT)
3907abd58f01SBen Widawsky 	};
3908abd58f01SBen Widawsky 
3909f4e9af4fSAkash Goel 	dev_priv->pm_ier = 0x0;
3910f4e9af4fSAkash Goel 	dev_priv->pm_imr = ~dev_priv->pm_ier;
39119a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 0, ~gt_interrupts[0], gt_interrupts[0]);
39129a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 1, ~gt_interrupts[1], gt_interrupts[1]);
391378e68d36SImre Deak 	/*
391478e68d36SImre Deak 	 * RPS interrupts will get enabled/disabled on demand when RPS itself
391526705e20SSagar Arun Kamble 	 * is enabled/disabled. Same wil be the case for GuC interrupts.
391678e68d36SImre Deak 	 */
3917f4e9af4fSAkash Goel 	GEN8_IRQ_INIT_NDX(GT, 2, dev_priv->pm_imr, dev_priv->pm_ier);
39189a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 3, ~gt_interrupts[3], gt_interrupts[3]);
3919abd58f01SBen Widawsky }
3920abd58f01SBen Widawsky 
3921abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3922abd58f01SBen Widawsky {
3923a9c287c9SJani Nikula 	u32 de_pipe_masked = GEN8_PIPE_CDCLK_CRC_DONE;
3924a9c287c9SJani Nikula 	u32 de_pipe_enables;
39253a3b3c7dSVille Syrjälä 	u32 de_port_masked = GEN8_AUX_CHANNEL_A;
39263a3b3c7dSVille Syrjälä 	u32 de_port_enables;
3927df0d28c1SDhinakaran Pandiyan 	u32 de_misc_masked = GEN8_DE_EDP_PSR;
39283a3b3c7dSVille Syrjälä 	enum pipe pipe;
3929770de83dSDamien Lespiau 
3930df0d28c1SDhinakaran Pandiyan 	if (INTEL_GEN(dev_priv) <= 10)
3931df0d28c1SDhinakaran Pandiyan 		de_misc_masked |= GEN8_DE_MISC_GSE;
3932df0d28c1SDhinakaran Pandiyan 
3933bca2bf2aSPandiyan, Dhinakaran 	if (INTEL_GEN(dev_priv) >= 9) {
3934842ebf7aSVille Syrjälä 		de_pipe_masked |= GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
39353a3b3c7dSVille Syrjälä 		de_port_masked |= GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
393688e04703SJesse Barnes 				  GEN9_AUX_CHANNEL_D;
3937cc3f90f0SAnder Conselvan de Oliveira 		if (IS_GEN9_LP(dev_priv))
39383a3b3c7dSVille Syrjälä 			de_port_masked |= BXT_DE_PORT_GMBUS;
39393a3b3c7dSVille Syrjälä 	} else {
3940842ebf7aSVille Syrjälä 		de_pipe_masked |= GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
39413a3b3c7dSVille Syrjälä 	}
3942770de83dSDamien Lespiau 
3943bb187e93SJames Ausmus 	if (INTEL_GEN(dev_priv) >= 11)
3944bb187e93SJames Ausmus 		de_port_masked |= ICL_AUX_CHANNEL_E;
3945bb187e93SJames Ausmus 
39469bb635d9SDhinakaran Pandiyan 	if (IS_CNL_WITH_PORT_F(dev_priv) || INTEL_GEN(dev_priv) >= 11)
3947a324fcacSRodrigo Vivi 		de_port_masked |= CNL_AUX_CHANNEL_F;
3948a324fcacSRodrigo Vivi 
3949770de83dSDamien Lespiau 	de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
3950770de83dSDamien Lespiau 					   GEN8_PIPE_FIFO_UNDERRUN;
3951770de83dSDamien Lespiau 
39523a3b3c7dSVille Syrjälä 	de_port_enables = de_port_masked;
3953cc3f90f0SAnder Conselvan de Oliveira 	if (IS_GEN9_LP(dev_priv))
3954a52bb15bSVille Syrjälä 		de_port_enables |= BXT_DE_PORT_HOTPLUG_MASK;
3955a52bb15bSVille Syrjälä 	else if (IS_BROADWELL(dev_priv))
39563a3b3c7dSVille Syrjälä 		de_port_enables |= GEN8_PORT_DP_A_HOTPLUG;
39573a3b3c7dSVille Syrjälä 
3958e04f7eceSVille Syrjälä 	gen3_assert_iir_is_zero(dev_priv, EDP_PSR_IIR);
395954fd3149SDhinakaran Pandiyan 	intel_psr_irq_control(dev_priv, dev_priv->psr.debug);
3960e04f7eceSVille Syrjälä 
39610a195c02SMika Kahola 	for_each_pipe(dev_priv, pipe) {
39620a195c02SMika Kahola 		dev_priv->de_irq_mask[pipe] = ~de_pipe_masked;
3963abd58f01SBen Widawsky 
3964f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3965813bde43SPaulo Zanoni 				POWER_DOMAIN_PIPE(pipe)))
3966813bde43SPaulo Zanoni 			GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
3967813bde43SPaulo Zanoni 					  dev_priv->de_irq_mask[pipe],
396835079899SPaulo Zanoni 					  de_pipe_enables);
39690a195c02SMika Kahola 	}
3970abd58f01SBen Widawsky 
39713488d4ebSVille Syrjälä 	GEN3_IRQ_INIT(GEN8_DE_PORT_, ~de_port_masked, de_port_enables);
39723488d4ebSVille Syrjälä 	GEN3_IRQ_INIT(GEN8_DE_MISC_, ~de_misc_masked, de_misc_masked);
39732a57d9ccSImre Deak 
3974121e758eSDhinakaran Pandiyan 	if (INTEL_GEN(dev_priv) >= 11) {
3975121e758eSDhinakaran Pandiyan 		u32 de_hpd_masked = 0;
3976b796b971SDhinakaran Pandiyan 		u32 de_hpd_enables = GEN11_DE_TC_HOTPLUG_MASK |
3977b796b971SDhinakaran Pandiyan 				     GEN11_DE_TBT_HOTPLUG_MASK;
3978121e758eSDhinakaran Pandiyan 
3979121e758eSDhinakaran Pandiyan 		GEN3_IRQ_INIT(GEN11_DE_HPD_, ~de_hpd_masked, de_hpd_enables);
3980121e758eSDhinakaran Pandiyan 		gen11_hpd_detection_setup(dev_priv);
3981121e758eSDhinakaran Pandiyan 	} else if (IS_GEN9_LP(dev_priv)) {
39822a57d9ccSImre Deak 		bxt_hpd_detection_setup(dev_priv);
3983121e758eSDhinakaran Pandiyan 	} else if (IS_BROADWELL(dev_priv)) {
39841a56b1a2SImre Deak 		ilk_hpd_detection_setup(dev_priv);
3985abd58f01SBen Widawsky 	}
3986121e758eSDhinakaran Pandiyan }
3987abd58f01SBen Widawsky 
3988abd58f01SBen Widawsky static int gen8_irq_postinstall(struct drm_device *dev)
3989abd58f01SBen Widawsky {
3990fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3991abd58f01SBen Widawsky 
39926e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
3993622364b6SPaulo Zanoni 		ibx_irq_pre_postinstall(dev);
3994622364b6SPaulo Zanoni 
3995abd58f01SBen Widawsky 	gen8_gt_irq_postinstall(dev_priv);
3996abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3997abd58f01SBen Widawsky 
39986e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
3999abd58f01SBen Widawsky 		ibx_irq_postinstall(dev);
4000abd58f01SBen Widawsky 
40014376b9c9SMika Kuoppala 	gen8_master_intr_enable(dev_priv->regs);
4002abd58f01SBen Widawsky 
4003abd58f01SBen Widawsky 	return 0;
4004abd58f01SBen Widawsky }
4005abd58f01SBen Widawsky 
400651951ae7SMika Kuoppala static void gen11_gt_irq_postinstall(struct drm_i915_private *dev_priv)
400751951ae7SMika Kuoppala {
400851951ae7SMika Kuoppala 	const u32 irqs = GT_RENDER_USER_INTERRUPT | GT_CONTEXT_SWITCH_INTERRUPT;
400951951ae7SMika Kuoppala 
401051951ae7SMika Kuoppala 	BUILD_BUG_ON(irqs & 0xffff0000);
401151951ae7SMika Kuoppala 
401251951ae7SMika Kuoppala 	/* Enable RCS, BCS, VCS and VECS class interrupts. */
401351951ae7SMika Kuoppala 	I915_WRITE(GEN11_RENDER_COPY_INTR_ENABLE, irqs << 16 | irqs);
401451951ae7SMika Kuoppala 	I915_WRITE(GEN11_VCS_VECS_INTR_ENABLE,	  irqs << 16 | irqs);
401551951ae7SMika Kuoppala 
401651951ae7SMika Kuoppala 	/* Unmask irqs on RCS, BCS, VCS and VECS engines. */
401751951ae7SMika Kuoppala 	I915_WRITE(GEN11_RCS0_RSVD_INTR_MASK,	~(irqs << 16));
401851951ae7SMika Kuoppala 	I915_WRITE(GEN11_BCS_RSVD_INTR_MASK,	~(irqs << 16));
401951951ae7SMika Kuoppala 	I915_WRITE(GEN11_VCS0_VCS1_INTR_MASK,	~(irqs | irqs << 16));
402051951ae7SMika Kuoppala 	I915_WRITE(GEN11_VCS2_VCS3_INTR_MASK,	~(irqs | irqs << 16));
402151951ae7SMika Kuoppala 	I915_WRITE(GEN11_VECS0_VECS1_INTR_MASK,	~(irqs | irqs << 16));
402251951ae7SMika Kuoppala 
4023d02b98b8SOscar Mateo 	/*
4024d02b98b8SOscar Mateo 	 * RPS interrupts will get enabled/disabled on demand when RPS itself
4025d02b98b8SOscar Mateo 	 * is enabled/disabled.
4026d02b98b8SOscar Mateo 	 */
4027d02b98b8SOscar Mateo 	dev_priv->pm_ier = 0x0;
4028d02b98b8SOscar Mateo 	dev_priv->pm_imr = ~dev_priv->pm_ier;
4029d02b98b8SOscar Mateo 	I915_WRITE(GEN11_GPM_WGBOXPERF_INTR_ENABLE, 0);
4030d02b98b8SOscar Mateo 	I915_WRITE(GEN11_GPM_WGBOXPERF_INTR_MASK,  ~0);
403151951ae7SMika Kuoppala }
403251951ae7SMika Kuoppala 
403331604222SAnusha Srivatsa static void icp_irq_postinstall(struct drm_device *dev)
403431604222SAnusha Srivatsa {
403531604222SAnusha Srivatsa 	struct drm_i915_private *dev_priv = to_i915(dev);
403631604222SAnusha Srivatsa 	u32 mask = SDE_GMBUS_ICP;
403731604222SAnusha Srivatsa 
403831604222SAnusha Srivatsa 	WARN_ON(I915_READ(SDEIER) != 0);
403931604222SAnusha Srivatsa 	I915_WRITE(SDEIER, 0xffffffff);
404031604222SAnusha Srivatsa 	POSTING_READ(SDEIER);
404131604222SAnusha Srivatsa 
404231604222SAnusha Srivatsa 	gen3_assert_iir_is_zero(dev_priv, SDEIIR);
404331604222SAnusha Srivatsa 	I915_WRITE(SDEIMR, ~mask);
404431604222SAnusha Srivatsa 
404531604222SAnusha Srivatsa 	icp_hpd_detection_setup(dev_priv);
404631604222SAnusha Srivatsa }
404731604222SAnusha Srivatsa 
404851951ae7SMika Kuoppala static int gen11_irq_postinstall(struct drm_device *dev)
404951951ae7SMika Kuoppala {
405051951ae7SMika Kuoppala 	struct drm_i915_private *dev_priv = dev->dev_private;
4051df0d28c1SDhinakaran Pandiyan 	u32 gu_misc_masked = GEN11_GU_MISC_GSE;
405251951ae7SMika Kuoppala 
4053*29b43ae2SRodrigo Vivi 	if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
405431604222SAnusha Srivatsa 		icp_irq_postinstall(dev);
405531604222SAnusha Srivatsa 
405651951ae7SMika Kuoppala 	gen11_gt_irq_postinstall(dev_priv);
405751951ae7SMika Kuoppala 	gen8_de_irq_postinstall(dev_priv);
405851951ae7SMika Kuoppala 
4059df0d28c1SDhinakaran Pandiyan 	GEN3_IRQ_INIT(GEN11_GU_MISC_, ~gu_misc_masked, gu_misc_masked);
4060df0d28c1SDhinakaran Pandiyan 
406151951ae7SMika Kuoppala 	I915_WRITE(GEN11_DISPLAY_INT_CTL, GEN11_DISPLAY_IRQ_ENABLE);
406251951ae7SMika Kuoppala 
406381067b71SMika Kuoppala 	gen11_master_intr_enable(dev_priv->regs);
4064c25f0c6aSDaniele Ceraolo Spurio 	POSTING_READ(GEN11_GFX_MSTR_IRQ);
406551951ae7SMika Kuoppala 
406651951ae7SMika Kuoppala 	return 0;
406751951ae7SMika Kuoppala }
406851951ae7SMika Kuoppala 
406943f328d7SVille Syrjälä static int cherryview_irq_postinstall(struct drm_device *dev)
407043f328d7SVille Syrjälä {
4071fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
407243f328d7SVille Syrjälä 
407343f328d7SVille Syrjälä 	gen8_gt_irq_postinstall(dev_priv);
407443f328d7SVille Syrjälä 
4075ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
40769918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
4077ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
4078ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
4079ad22d106SVille Syrjälä 
4080e5328c43SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
408143f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
408243f328d7SVille Syrjälä 
408343f328d7SVille Syrjälä 	return 0;
408443f328d7SVille Syrjälä }
408543f328d7SVille Syrjälä 
40866bcdb1c8SVille Syrjälä static void i8xx_irq_reset(struct drm_device *dev)
4087c2798b19SChris Wilson {
4088fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4089c2798b19SChris Wilson 
409044d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
409144d9241eSVille Syrjälä 
4092e9e9848aSVille Syrjälä 	GEN2_IRQ_RESET();
4093c2798b19SChris Wilson }
4094c2798b19SChris Wilson 
4095c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
4096c2798b19SChris Wilson {
4097fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4098e9e9848aSVille Syrjälä 	u16 enable_mask;
4099c2798b19SChris Wilson 
4100045cebd2SVille Syrjälä 	I915_WRITE16(EMR, ~(I915_ERROR_PAGE_TABLE |
4101045cebd2SVille Syrjälä 			    I915_ERROR_MEMORY_REFRESH));
4102c2798b19SChris Wilson 
4103c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
4104c2798b19SChris Wilson 	dev_priv->irq_mask =
4105c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
410616659bc5SVille Syrjälä 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
410716659bc5SVille Syrjälä 		  I915_MASTER_ERROR_INTERRUPT);
4108c2798b19SChris Wilson 
4109e9e9848aSVille Syrjälä 	enable_mask =
4110c2798b19SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4111c2798b19SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
411216659bc5SVille Syrjälä 		I915_MASTER_ERROR_INTERRUPT |
4113e9e9848aSVille Syrjälä 		I915_USER_INTERRUPT;
4114e9e9848aSVille Syrjälä 
4115e9e9848aSVille Syrjälä 	GEN2_IRQ_INIT(, dev_priv->irq_mask, enable_mask);
4116c2798b19SChris Wilson 
4117379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4118379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4119d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4120755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4121755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4122d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4123379ef82dSDaniel Vetter 
4124c2798b19SChris Wilson 	return 0;
4125c2798b19SChris Wilson }
4126c2798b19SChris Wilson 
412778c357ddSVille Syrjälä static void i8xx_error_irq_ack(struct drm_i915_private *dev_priv,
412878c357ddSVille Syrjälä 			       u16 *eir, u16 *eir_stuck)
412978c357ddSVille Syrjälä {
413078c357ddSVille Syrjälä 	u16 emr;
413178c357ddSVille Syrjälä 
413278c357ddSVille Syrjälä 	*eir = I915_READ16(EIR);
413378c357ddSVille Syrjälä 
413478c357ddSVille Syrjälä 	if (*eir)
413578c357ddSVille Syrjälä 		I915_WRITE16(EIR, *eir);
413678c357ddSVille Syrjälä 
413778c357ddSVille Syrjälä 	*eir_stuck = I915_READ16(EIR);
413878c357ddSVille Syrjälä 	if (*eir_stuck == 0)
413978c357ddSVille Syrjälä 		return;
414078c357ddSVille Syrjälä 
414178c357ddSVille Syrjälä 	/*
414278c357ddSVille Syrjälä 	 * Toggle all EMR bits to make sure we get an edge
414378c357ddSVille Syrjälä 	 * in the ISR master error bit if we don't clear
414478c357ddSVille Syrjälä 	 * all the EIR bits. Otherwise the edge triggered
414578c357ddSVille Syrjälä 	 * IIR on i965/g4x wouldn't notice that an interrupt
414678c357ddSVille Syrjälä 	 * is still pending. Also some EIR bits can't be
414778c357ddSVille Syrjälä 	 * cleared except by handling the underlying error
414878c357ddSVille Syrjälä 	 * (or by a GPU reset) so we mask any bit that
414978c357ddSVille Syrjälä 	 * remains set.
415078c357ddSVille Syrjälä 	 */
415178c357ddSVille Syrjälä 	emr = I915_READ16(EMR);
415278c357ddSVille Syrjälä 	I915_WRITE16(EMR, 0xffff);
415378c357ddSVille Syrjälä 	I915_WRITE16(EMR, emr | *eir_stuck);
415478c357ddSVille Syrjälä }
415578c357ddSVille Syrjälä 
415678c357ddSVille Syrjälä static void i8xx_error_irq_handler(struct drm_i915_private *dev_priv,
415778c357ddSVille Syrjälä 				   u16 eir, u16 eir_stuck)
415878c357ddSVille Syrjälä {
415978c357ddSVille Syrjälä 	DRM_DEBUG("Master Error: EIR 0x%04x\n", eir);
416078c357ddSVille Syrjälä 
416178c357ddSVille Syrjälä 	if (eir_stuck)
416278c357ddSVille Syrjälä 		DRM_DEBUG_DRIVER("EIR stuck: 0x%04x, masked\n", eir_stuck);
416378c357ddSVille Syrjälä }
416478c357ddSVille Syrjälä 
416578c357ddSVille Syrjälä static void i9xx_error_irq_ack(struct drm_i915_private *dev_priv,
416678c357ddSVille Syrjälä 			       u32 *eir, u32 *eir_stuck)
416778c357ddSVille Syrjälä {
416878c357ddSVille Syrjälä 	u32 emr;
416978c357ddSVille Syrjälä 
417078c357ddSVille Syrjälä 	*eir = I915_READ(EIR);
417178c357ddSVille Syrjälä 
417278c357ddSVille Syrjälä 	I915_WRITE(EIR, *eir);
417378c357ddSVille Syrjälä 
417478c357ddSVille Syrjälä 	*eir_stuck = I915_READ(EIR);
417578c357ddSVille Syrjälä 	if (*eir_stuck == 0)
417678c357ddSVille Syrjälä 		return;
417778c357ddSVille Syrjälä 
417878c357ddSVille Syrjälä 	/*
417978c357ddSVille Syrjälä 	 * Toggle all EMR bits to make sure we get an edge
418078c357ddSVille Syrjälä 	 * in the ISR master error bit if we don't clear
418178c357ddSVille Syrjälä 	 * all the EIR bits. Otherwise the edge triggered
418278c357ddSVille Syrjälä 	 * IIR on i965/g4x wouldn't notice that an interrupt
418378c357ddSVille Syrjälä 	 * is still pending. Also some EIR bits can't be
418478c357ddSVille Syrjälä 	 * cleared except by handling the underlying error
418578c357ddSVille Syrjälä 	 * (or by a GPU reset) so we mask any bit that
418678c357ddSVille Syrjälä 	 * remains set.
418778c357ddSVille Syrjälä 	 */
418878c357ddSVille Syrjälä 	emr = I915_READ(EMR);
418978c357ddSVille Syrjälä 	I915_WRITE(EMR, 0xffffffff);
419078c357ddSVille Syrjälä 	I915_WRITE(EMR, emr | *eir_stuck);
419178c357ddSVille Syrjälä }
419278c357ddSVille Syrjälä 
419378c357ddSVille Syrjälä static void i9xx_error_irq_handler(struct drm_i915_private *dev_priv,
419478c357ddSVille Syrjälä 				   u32 eir, u32 eir_stuck)
419578c357ddSVille Syrjälä {
419678c357ddSVille Syrjälä 	DRM_DEBUG("Master Error, EIR 0x%08x\n", eir);
419778c357ddSVille Syrjälä 
419878c357ddSVille Syrjälä 	if (eir_stuck)
419978c357ddSVille Syrjälä 		DRM_DEBUG_DRIVER("EIR stuck: 0x%08x, masked\n", eir_stuck);
420078c357ddSVille Syrjälä }
420178c357ddSVille Syrjälä 
4202ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
4203c2798b19SChris Wilson {
420445a83f84SDaniel Vetter 	struct drm_device *dev = arg;
4205fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4206af722d28SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
4207c2798b19SChris Wilson 
42082dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
42092dd2a883SImre Deak 		return IRQ_NONE;
42102dd2a883SImre Deak 
42111f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
42121f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
42131f814dacSImre Deak 
4214af722d28SVille Syrjälä 	do {
4215af722d28SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
421678c357ddSVille Syrjälä 		u16 eir = 0, eir_stuck = 0;
4217af722d28SVille Syrjälä 		u16 iir;
4218af722d28SVille Syrjälä 
4219c2798b19SChris Wilson 		iir = I915_READ16(IIR);
4220c2798b19SChris Wilson 		if (iir == 0)
4221af722d28SVille Syrjälä 			break;
4222c2798b19SChris Wilson 
4223af722d28SVille Syrjälä 		ret = IRQ_HANDLED;
4224c2798b19SChris Wilson 
4225eb64343cSVille Syrjälä 		/* Call regardless, as some status bits might not be
4226eb64343cSVille Syrjälä 		 * signalled in iir */
4227eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
4228c2798b19SChris Wilson 
422978c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
423078c357ddSVille Syrjälä 			i8xx_error_irq_ack(dev_priv, &eir, &eir_stuck);
423178c357ddSVille Syrjälä 
4232fd3a4024SDaniel Vetter 		I915_WRITE16(IIR, iir);
4233c2798b19SChris Wilson 
4234c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
42358a68d464SChris Wilson 			intel_engine_breadcrumbs_irq(dev_priv->engine[RCS0]);
4236c2798b19SChris Wilson 
423778c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
423878c357ddSVille Syrjälä 			i8xx_error_irq_handler(dev_priv, eir, eir_stuck);
4239af722d28SVille Syrjälä 
4240eb64343cSVille Syrjälä 		i8xx_pipestat_irq_handler(dev_priv, iir, pipe_stats);
4241af722d28SVille Syrjälä 	} while (0);
4242c2798b19SChris Wilson 
42431f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
42441f814dacSImre Deak 
42451f814dacSImre Deak 	return ret;
4246c2798b19SChris Wilson }
4247c2798b19SChris Wilson 
42486bcdb1c8SVille Syrjälä static void i915_irq_reset(struct drm_device *dev)
4249a266c7d5SChris Wilson {
4250fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4251a266c7d5SChris Wilson 
425256b857a5STvrtko Ursulin 	if (I915_HAS_HOTPLUG(dev_priv)) {
42530706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4254a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4255a266c7d5SChris Wilson 	}
4256a266c7d5SChris Wilson 
425744d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
425844d9241eSVille Syrjälä 
4259ba7eb789SVille Syrjälä 	GEN3_IRQ_RESET();
4260a266c7d5SChris Wilson }
4261a266c7d5SChris Wilson 
4262a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
4263a266c7d5SChris Wilson {
4264fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
426538bde180SChris Wilson 	u32 enable_mask;
4266a266c7d5SChris Wilson 
4267045cebd2SVille Syrjälä 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE |
4268045cebd2SVille Syrjälä 			  I915_ERROR_MEMORY_REFRESH));
426938bde180SChris Wilson 
427038bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
427138bde180SChris Wilson 	dev_priv->irq_mask =
427238bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
427338bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
427416659bc5SVille Syrjälä 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
427516659bc5SVille Syrjälä 		  I915_MASTER_ERROR_INTERRUPT);
427638bde180SChris Wilson 
427738bde180SChris Wilson 	enable_mask =
427838bde180SChris Wilson 		I915_ASLE_INTERRUPT |
427938bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
428038bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
428116659bc5SVille Syrjälä 		I915_MASTER_ERROR_INTERRUPT |
428238bde180SChris Wilson 		I915_USER_INTERRUPT;
428338bde180SChris Wilson 
428456b857a5STvrtko Ursulin 	if (I915_HAS_HOTPLUG(dev_priv)) {
4285a266c7d5SChris Wilson 		/* Enable in IER... */
4286a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
4287a266c7d5SChris Wilson 		/* and unmask in IMR */
4288a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
4289a266c7d5SChris Wilson 	}
4290a266c7d5SChris Wilson 
4291ba7eb789SVille Syrjälä 	GEN3_IRQ_INIT(, dev_priv->irq_mask, enable_mask);
4292a266c7d5SChris Wilson 
4293379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4294379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4295d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4296755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4297755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4298d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4299379ef82dSDaniel Vetter 
4300c30bb1fdSVille Syrjälä 	i915_enable_asle_pipestat(dev_priv);
4301c30bb1fdSVille Syrjälä 
430220afbda2SDaniel Vetter 	return 0;
430320afbda2SDaniel Vetter }
430420afbda2SDaniel Vetter 
4305ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
4306a266c7d5SChris Wilson {
430745a83f84SDaniel Vetter 	struct drm_device *dev = arg;
4308fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4309af722d28SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
4310a266c7d5SChris Wilson 
43112dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
43122dd2a883SImre Deak 		return IRQ_NONE;
43132dd2a883SImre Deak 
43141f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
43151f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
43161f814dacSImre Deak 
431738bde180SChris Wilson 	do {
4318eb64343cSVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
431978c357ddSVille Syrjälä 		u32 eir = 0, eir_stuck = 0;
4320af722d28SVille Syrjälä 		u32 hotplug_status = 0;
4321af722d28SVille Syrjälä 		u32 iir;
4322a266c7d5SChris Wilson 
4323af722d28SVille Syrjälä 		iir = I915_READ(IIR);
4324af722d28SVille Syrjälä 		if (iir == 0)
4325af722d28SVille Syrjälä 			break;
4326af722d28SVille Syrjälä 
4327af722d28SVille Syrjälä 		ret = IRQ_HANDLED;
4328af722d28SVille Syrjälä 
4329af722d28SVille Syrjälä 		if (I915_HAS_HOTPLUG(dev_priv) &&
4330af722d28SVille Syrjälä 		    iir & I915_DISPLAY_PORT_INTERRUPT)
4331af722d28SVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
4332a266c7d5SChris Wilson 
4333eb64343cSVille Syrjälä 		/* Call regardless, as some status bits might not be
4334eb64343cSVille Syrjälä 		 * signalled in iir */
4335eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
4336a266c7d5SChris Wilson 
433778c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
433878c357ddSVille Syrjälä 			i9xx_error_irq_ack(dev_priv, &eir, &eir_stuck);
433978c357ddSVille Syrjälä 
4340fd3a4024SDaniel Vetter 		I915_WRITE(IIR, iir);
4341a266c7d5SChris Wilson 
4342a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
43438a68d464SChris Wilson 			intel_engine_breadcrumbs_irq(dev_priv->engine[RCS0]);
4344a266c7d5SChris Wilson 
434578c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
434678c357ddSVille Syrjälä 			i9xx_error_irq_handler(dev_priv, eir, eir_stuck);
4347a266c7d5SChris Wilson 
4348af722d28SVille Syrjälä 		if (hotplug_status)
4349af722d28SVille Syrjälä 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
4350af722d28SVille Syrjälä 
4351af722d28SVille Syrjälä 		i915_pipestat_irq_handler(dev_priv, iir, pipe_stats);
4352af722d28SVille Syrjälä 	} while (0);
4353a266c7d5SChris Wilson 
43541f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
43551f814dacSImre Deak 
4356a266c7d5SChris Wilson 	return ret;
4357a266c7d5SChris Wilson }
4358a266c7d5SChris Wilson 
43596bcdb1c8SVille Syrjälä static void i965_irq_reset(struct drm_device *dev)
4360a266c7d5SChris Wilson {
4361fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4362a266c7d5SChris Wilson 
43630706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4364a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4365a266c7d5SChris Wilson 
436644d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
436744d9241eSVille Syrjälä 
4368ba7eb789SVille Syrjälä 	GEN3_IRQ_RESET();
4369a266c7d5SChris Wilson }
4370a266c7d5SChris Wilson 
4371a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
4372a266c7d5SChris Wilson {
4373fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4374bbba0a97SChris Wilson 	u32 enable_mask;
4375a266c7d5SChris Wilson 	u32 error_mask;
4376a266c7d5SChris Wilson 
4377045cebd2SVille Syrjälä 	/*
4378045cebd2SVille Syrjälä 	 * Enable some error detection, note the instruction error mask
4379045cebd2SVille Syrjälä 	 * bit is reserved, so we leave it masked.
4380045cebd2SVille Syrjälä 	 */
4381045cebd2SVille Syrjälä 	if (IS_G4X(dev_priv)) {
4382045cebd2SVille Syrjälä 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
4383045cebd2SVille Syrjälä 			       GM45_ERROR_MEM_PRIV |
4384045cebd2SVille Syrjälä 			       GM45_ERROR_CP_PRIV |
4385045cebd2SVille Syrjälä 			       I915_ERROR_MEMORY_REFRESH);
4386045cebd2SVille Syrjälä 	} else {
4387045cebd2SVille Syrjälä 		error_mask = ~(I915_ERROR_PAGE_TABLE |
4388045cebd2SVille Syrjälä 			       I915_ERROR_MEMORY_REFRESH);
4389045cebd2SVille Syrjälä 	}
4390045cebd2SVille Syrjälä 	I915_WRITE(EMR, error_mask);
4391045cebd2SVille Syrjälä 
4392a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
4393c30bb1fdSVille Syrjälä 	dev_priv->irq_mask =
4394c30bb1fdSVille Syrjälä 		~(I915_ASLE_INTERRUPT |
4395adca4730SChris Wilson 		  I915_DISPLAY_PORT_INTERRUPT |
4396bbba0a97SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4397bbba0a97SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
439878c357ddSVille Syrjälä 		  I915_MASTER_ERROR_INTERRUPT);
4399bbba0a97SChris Wilson 
4400c30bb1fdSVille Syrjälä 	enable_mask =
4401c30bb1fdSVille Syrjälä 		I915_ASLE_INTERRUPT |
4402c30bb1fdSVille Syrjälä 		I915_DISPLAY_PORT_INTERRUPT |
4403c30bb1fdSVille Syrjälä 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4404c30bb1fdSVille Syrjälä 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
440578c357ddSVille Syrjälä 		I915_MASTER_ERROR_INTERRUPT |
4406c30bb1fdSVille Syrjälä 		I915_USER_INTERRUPT;
4407bbba0a97SChris Wilson 
440891d14251STvrtko Ursulin 	if (IS_G4X(dev_priv))
4409bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
4410a266c7d5SChris Wilson 
4411c30bb1fdSVille Syrjälä 	GEN3_IRQ_INIT(, dev_priv->irq_mask, enable_mask);
4412c30bb1fdSVille Syrjälä 
4413b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4414b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4415d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4416755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
4417755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4418755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4419d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4420a266c7d5SChris Wilson 
442191d14251STvrtko Ursulin 	i915_enable_asle_pipestat(dev_priv);
442220afbda2SDaniel Vetter 
442320afbda2SDaniel Vetter 	return 0;
442420afbda2SDaniel Vetter }
442520afbda2SDaniel Vetter 
442691d14251STvrtko Ursulin static void i915_hpd_irq_setup(struct drm_i915_private *dev_priv)
442720afbda2SDaniel Vetter {
442820afbda2SDaniel Vetter 	u32 hotplug_en;
442920afbda2SDaniel Vetter 
443067520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
4431b5ea2d56SDaniel Vetter 
4432adca4730SChris Wilson 	/* Note HDMI and DP share hotplug bits */
4433e5868a31SEgbert Eich 	/* enable bits are the same for all generations */
443491d14251STvrtko Ursulin 	hotplug_en = intel_hpd_enabled_irqs(dev_priv, hpd_mask_i915);
4435a266c7d5SChris Wilson 	/* Programming the CRT detection parameters tends
4436a266c7d5SChris Wilson 	   to generate a spurious hotplug event about three
4437a266c7d5SChris Wilson 	   seconds later.  So just do it once.
4438a266c7d5SChris Wilson 	*/
443991d14251STvrtko Ursulin 	if (IS_G4X(dev_priv))
4440a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
4441a266c7d5SChris Wilson 	hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
4442a266c7d5SChris Wilson 
4443a266c7d5SChris Wilson 	/* Ignore TV since it's buggy */
44440706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv,
4445f9e3dc78SJani Nikula 					     HOTPLUG_INT_EN_MASK |
4446f9e3dc78SJani Nikula 					     CRT_HOTPLUG_VOLTAGE_COMPARE_MASK |
4447f9e3dc78SJani Nikula 					     CRT_HOTPLUG_ACTIVATION_PERIOD_64,
44480706f17cSEgbert Eich 					     hotplug_en);
4449a266c7d5SChris Wilson }
4450a266c7d5SChris Wilson 
4451ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
4452a266c7d5SChris Wilson {
445345a83f84SDaniel Vetter 	struct drm_device *dev = arg;
4454fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4455af722d28SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
4456a266c7d5SChris Wilson 
44572dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
44582dd2a883SImre Deak 		return IRQ_NONE;
44592dd2a883SImre Deak 
44601f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
44611f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
44621f814dacSImre Deak 
4463af722d28SVille Syrjälä 	do {
4464eb64343cSVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
446578c357ddSVille Syrjälä 		u32 eir = 0, eir_stuck = 0;
4466af722d28SVille Syrjälä 		u32 hotplug_status = 0;
4467af722d28SVille Syrjälä 		u32 iir;
44682c8ba29fSChris Wilson 
4469af722d28SVille Syrjälä 		iir = I915_READ(IIR);
4470af722d28SVille Syrjälä 		if (iir == 0)
4471af722d28SVille Syrjälä 			break;
4472af722d28SVille Syrjälä 
4473af722d28SVille Syrjälä 		ret = IRQ_HANDLED;
4474af722d28SVille Syrjälä 
4475af722d28SVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
4476af722d28SVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
4477a266c7d5SChris Wilson 
4478eb64343cSVille Syrjälä 		/* Call regardless, as some status bits might not be
4479eb64343cSVille Syrjälä 		 * signalled in iir */
4480eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
4481a266c7d5SChris Wilson 
448278c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
448378c357ddSVille Syrjälä 			i9xx_error_irq_ack(dev_priv, &eir, &eir_stuck);
448478c357ddSVille Syrjälä 
4485fd3a4024SDaniel Vetter 		I915_WRITE(IIR, iir);
4486a266c7d5SChris Wilson 
4487a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
44888a68d464SChris Wilson 			intel_engine_breadcrumbs_irq(dev_priv->engine[RCS0]);
4489af722d28SVille Syrjälä 
4490a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
44918a68d464SChris Wilson 			intel_engine_breadcrumbs_irq(dev_priv->engine[VCS0]);
4492a266c7d5SChris Wilson 
449378c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
449478c357ddSVille Syrjälä 			i9xx_error_irq_handler(dev_priv, eir, eir_stuck);
4495515ac2bbSDaniel Vetter 
4496af722d28SVille Syrjälä 		if (hotplug_status)
4497af722d28SVille Syrjälä 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
4498af722d28SVille Syrjälä 
4499af722d28SVille Syrjälä 		i965_pipestat_irq_handler(dev_priv, iir, pipe_stats);
4500af722d28SVille Syrjälä 	} while (0);
4501a266c7d5SChris Wilson 
45021f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
45031f814dacSImre Deak 
4504a266c7d5SChris Wilson 	return ret;
4505a266c7d5SChris Wilson }
4506a266c7d5SChris Wilson 
4507fca52a55SDaniel Vetter /**
4508fca52a55SDaniel Vetter  * intel_irq_init - initializes irq support
4509fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4510fca52a55SDaniel Vetter  *
4511fca52a55SDaniel Vetter  * This function initializes all the irq support including work items, timers
4512fca52a55SDaniel Vetter  * and all the vtables. It does not setup the interrupt itself though.
4513fca52a55SDaniel Vetter  */
4514b963291cSDaniel Vetter void intel_irq_init(struct drm_i915_private *dev_priv)
4515f71d4af4SJesse Barnes {
451691c8a326SChris Wilson 	struct drm_device *dev = &dev_priv->drm;
4517562d9baeSSagar Arun Kamble 	struct intel_rps *rps = &dev_priv->gt_pm.rps;
4518cefcff8fSJoonas Lahtinen 	int i;
45198b2e326dSChris Wilson 
452077913b39SJani Nikula 	intel_hpd_init_work(dev_priv);
452177913b39SJani Nikula 
4522562d9baeSSagar Arun Kamble 	INIT_WORK(&rps->work, gen6_pm_rps_work);
4523cefcff8fSJoonas Lahtinen 
4524a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
4525cefcff8fSJoonas Lahtinen 	for (i = 0; i < MAX_L3_SLICES; ++i)
4526cefcff8fSJoonas Lahtinen 		dev_priv->l3_parity.remap_info[i] = NULL;
45278b2e326dSChris Wilson 
45284805fe82STvrtko Ursulin 	if (HAS_GUC_SCHED(dev_priv))
452926705e20SSagar Arun Kamble 		dev_priv->pm_guc_events = GEN9_GUC_TO_HOST_INT_EVENT;
453026705e20SSagar Arun Kamble 
4531a6706b45SDeepak S 	/* Let's track the enabled rps events */
4532666a4537SWayne Boyer 	if (IS_VALLEYVIEW(dev_priv))
45336c65a587SVille Syrjälä 		/* WaGsvRC0ResidencyMethod:vlv */
4534e0e8c7cbSChris Wilson 		dev_priv->pm_rps_events = GEN6_PM_RP_UP_EI_EXPIRED;
453531685c25SDeepak S 	else
45364668f695SChris Wilson 		dev_priv->pm_rps_events = (GEN6_PM_RP_UP_THRESHOLD |
45374668f695SChris Wilson 					   GEN6_PM_RP_DOWN_THRESHOLD |
45384668f695SChris Wilson 					   GEN6_PM_RP_DOWN_TIMEOUT);
4539a6706b45SDeepak S 
4540562d9baeSSagar Arun Kamble 	rps->pm_intrmsk_mbz = 0;
45411800ad25SSagar Arun Kamble 
45421800ad25SSagar Arun Kamble 	/*
4543acf2dc22SMika Kuoppala 	 * SNB,IVB,HSW can while VLV,CHV may hard hang on looping batchbuffer
45441800ad25SSagar Arun Kamble 	 * if GEN6_PM_UP_EI_EXPIRED is masked.
45451800ad25SSagar Arun Kamble 	 *
45461800ad25SSagar Arun Kamble 	 * TODO: verify if this can be reproduced on VLV,CHV.
45471800ad25SSagar Arun Kamble 	 */
4548bca2bf2aSPandiyan, Dhinakaran 	if (INTEL_GEN(dev_priv) <= 7)
4549562d9baeSSagar Arun Kamble 		rps->pm_intrmsk_mbz |= GEN6_PM_RP_UP_EI_EXPIRED;
45501800ad25SSagar Arun Kamble 
4551bca2bf2aSPandiyan, Dhinakaran 	if (INTEL_GEN(dev_priv) >= 8)
4552562d9baeSSagar Arun Kamble 		rps->pm_intrmsk_mbz |= GEN8_PMINTR_DISABLE_REDIRECT_TO_GUC;
45531800ad25SSagar Arun Kamble 
455432db0b65SVille Syrjälä 	if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
4555fd8f507cSVille Syrjälä 		dev->driver->get_vblank_counter = g4x_get_vblank_counter;
455632db0b65SVille Syrjälä 	else if (INTEL_GEN(dev_priv) >= 3)
4557391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
4558f71d4af4SJesse Barnes 
455921da2700SVille Syrjälä 	/*
456021da2700SVille Syrjälä 	 * Opt out of the vblank disable timer on everything except gen2.
456121da2700SVille Syrjälä 	 * Gen2 doesn't have a hardware frame counter and so depends on
456221da2700SVille Syrjälä 	 * vblank interrupts to produce sane vblank seuquence numbers.
456321da2700SVille Syrjälä 	 */
4564cf819effSLucas De Marchi 	if (!IS_GEN(dev_priv, 2))
456521da2700SVille Syrjälä 		dev->vblank_disable_immediate = true;
456621da2700SVille Syrjälä 
4567262fd485SChris Wilson 	/* Most platforms treat the display irq block as an always-on
4568262fd485SChris Wilson 	 * power domain. vlv/chv can disable it at runtime and need
4569262fd485SChris Wilson 	 * special care to avoid writing any of the display block registers
4570262fd485SChris Wilson 	 * outside of the power domain. We defer setting up the display irqs
4571262fd485SChris Wilson 	 * in this case to the runtime pm.
4572262fd485SChris Wilson 	 */
4573262fd485SChris Wilson 	dev_priv->display_irqs_enabled = true;
4574262fd485SChris Wilson 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4575262fd485SChris Wilson 		dev_priv->display_irqs_enabled = false;
4576262fd485SChris Wilson 
4577317eaa95SLyude 	dev_priv->hotplug.hpd_storm_threshold = HPD_STORM_DEFAULT_THRESHOLD;
45789a64c650SLyude Paul 	/* If we have MST support, we want to avoid doing short HPD IRQ storm
45799a64c650SLyude Paul 	 * detection, as short HPD storms will occur as a natural part of
45809a64c650SLyude Paul 	 * sideband messaging with MST.
45819a64c650SLyude Paul 	 * On older platforms however, IRQ storms can occur with both long and
45829a64c650SLyude Paul 	 * short pulses, as seen on some G4x systems.
45839a64c650SLyude Paul 	 */
45849a64c650SLyude Paul 	dev_priv->hotplug.hpd_short_storm_enabled = !HAS_DP_MST(dev_priv);
4585317eaa95SLyude 
45861bf6ad62SDaniel Vetter 	dev->driver->get_vblank_timestamp = drm_calc_vbltimestamp_from_scanoutpos;
4587f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
4588f71d4af4SJesse Barnes 
4589b963291cSDaniel Vetter 	if (IS_CHERRYVIEW(dev_priv)) {
459043f328d7SVille Syrjälä 		dev->driver->irq_handler = cherryview_irq_handler;
45916bcdb1c8SVille Syrjälä 		dev->driver->irq_preinstall = cherryview_irq_reset;
459243f328d7SVille Syrjälä 		dev->driver->irq_postinstall = cherryview_irq_postinstall;
45936bcdb1c8SVille Syrjälä 		dev->driver->irq_uninstall = cherryview_irq_reset;
459486e83e35SChris Wilson 		dev->driver->enable_vblank = i965_enable_vblank;
459586e83e35SChris Wilson 		dev->driver->disable_vblank = i965_disable_vblank;
459643f328d7SVille Syrjälä 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4597b963291cSDaniel Vetter 	} else if (IS_VALLEYVIEW(dev_priv)) {
45987e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
45996bcdb1c8SVille Syrjälä 		dev->driver->irq_preinstall = valleyview_irq_reset;
46007e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
46016bcdb1c8SVille Syrjälä 		dev->driver->irq_uninstall = valleyview_irq_reset;
460286e83e35SChris Wilson 		dev->driver->enable_vblank = i965_enable_vblank;
460386e83e35SChris Wilson 		dev->driver->disable_vblank = i965_disable_vblank;
4604fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
460551951ae7SMika Kuoppala 	} else if (INTEL_GEN(dev_priv) >= 11) {
460651951ae7SMika Kuoppala 		dev->driver->irq_handler = gen11_irq_handler;
460751951ae7SMika Kuoppala 		dev->driver->irq_preinstall = gen11_irq_reset;
460851951ae7SMika Kuoppala 		dev->driver->irq_postinstall = gen11_irq_postinstall;
460951951ae7SMika Kuoppala 		dev->driver->irq_uninstall = gen11_irq_reset;
461051951ae7SMika Kuoppala 		dev->driver->enable_vblank = gen8_enable_vblank;
461151951ae7SMika Kuoppala 		dev->driver->disable_vblank = gen8_disable_vblank;
4612121e758eSDhinakaran Pandiyan 		dev_priv->display.hpd_irq_setup = gen11_hpd_irq_setup;
4613bca2bf2aSPandiyan, Dhinakaran 	} else if (INTEL_GEN(dev_priv) >= 8) {
4614abd58f01SBen Widawsky 		dev->driver->irq_handler = gen8_irq_handler;
4615723761b8SDaniel Vetter 		dev->driver->irq_preinstall = gen8_irq_reset;
4616abd58f01SBen Widawsky 		dev->driver->irq_postinstall = gen8_irq_postinstall;
46176bcdb1c8SVille Syrjälä 		dev->driver->irq_uninstall = gen8_irq_reset;
4618abd58f01SBen Widawsky 		dev->driver->enable_vblank = gen8_enable_vblank;
4619abd58f01SBen Widawsky 		dev->driver->disable_vblank = gen8_disable_vblank;
4620cc3f90f0SAnder Conselvan de Oliveira 		if (IS_GEN9_LP(dev_priv))
4621e0a20ad7SShashank Sharma 			dev_priv->display.hpd_irq_setup = bxt_hpd_irq_setup;
4622c6c30b91SRodrigo Vivi 		else if (INTEL_PCH_TYPE(dev_priv) >= PCH_SPT)
46236dbf30ceSVille Syrjälä 			dev_priv->display.hpd_irq_setup = spt_hpd_irq_setup;
46246dbf30ceSVille Syrjälä 		else
46253a3b3c7dSVille Syrjälä 			dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
46266e266956STvrtko Ursulin 	} else if (HAS_PCH_SPLIT(dev_priv)) {
4627f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
4628723761b8SDaniel Vetter 		dev->driver->irq_preinstall = ironlake_irq_reset;
4629f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
46306bcdb1c8SVille Syrjälä 		dev->driver->irq_uninstall = ironlake_irq_reset;
4631f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
4632f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
4633e4ce95aaSVille Syrjälä 		dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
4634f71d4af4SJesse Barnes 	} else {
4635cf819effSLucas De Marchi 		if (IS_GEN(dev_priv, 2)) {
46366bcdb1c8SVille Syrjälä 			dev->driver->irq_preinstall = i8xx_irq_reset;
4637c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
4638c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
46396bcdb1c8SVille Syrjälä 			dev->driver->irq_uninstall = i8xx_irq_reset;
464086e83e35SChris Wilson 			dev->driver->enable_vblank = i8xx_enable_vblank;
464186e83e35SChris Wilson 			dev->driver->disable_vblank = i8xx_disable_vblank;
4642cf819effSLucas De Marchi 		} else if (IS_GEN(dev_priv, 3)) {
46436bcdb1c8SVille Syrjälä 			dev->driver->irq_preinstall = i915_irq_reset;
4644a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
46456bcdb1c8SVille Syrjälä 			dev->driver->irq_uninstall = i915_irq_reset;
4646a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
464786e83e35SChris Wilson 			dev->driver->enable_vblank = i8xx_enable_vblank;
464886e83e35SChris Wilson 			dev->driver->disable_vblank = i8xx_disable_vblank;
4649c2798b19SChris Wilson 		} else {
46506bcdb1c8SVille Syrjälä 			dev->driver->irq_preinstall = i965_irq_reset;
4651a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
46526bcdb1c8SVille Syrjälä 			dev->driver->irq_uninstall = i965_irq_reset;
4653a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
465486e83e35SChris Wilson 			dev->driver->enable_vblank = i965_enable_vblank;
465586e83e35SChris Wilson 			dev->driver->disable_vblank = i965_disable_vblank;
4656c2798b19SChris Wilson 		}
4657778eb334SVille Syrjälä 		if (I915_HAS_HOTPLUG(dev_priv))
4658778eb334SVille Syrjälä 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4659f71d4af4SJesse Barnes 	}
4660f71d4af4SJesse Barnes }
466120afbda2SDaniel Vetter 
4662fca52a55SDaniel Vetter /**
4663cefcff8fSJoonas Lahtinen  * intel_irq_fini - deinitializes IRQ support
4664cefcff8fSJoonas Lahtinen  * @i915: i915 device instance
4665cefcff8fSJoonas Lahtinen  *
4666cefcff8fSJoonas Lahtinen  * This function deinitializes all the IRQ support.
4667cefcff8fSJoonas Lahtinen  */
4668cefcff8fSJoonas Lahtinen void intel_irq_fini(struct drm_i915_private *i915)
4669cefcff8fSJoonas Lahtinen {
4670cefcff8fSJoonas Lahtinen 	int i;
4671cefcff8fSJoonas Lahtinen 
4672cefcff8fSJoonas Lahtinen 	for (i = 0; i < MAX_L3_SLICES; ++i)
4673cefcff8fSJoonas Lahtinen 		kfree(i915->l3_parity.remap_info[i]);
4674cefcff8fSJoonas Lahtinen }
4675cefcff8fSJoonas Lahtinen 
4676cefcff8fSJoonas Lahtinen /**
4677fca52a55SDaniel Vetter  * intel_irq_install - enables the hardware interrupt
4678fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4679fca52a55SDaniel Vetter  *
4680fca52a55SDaniel Vetter  * This function enables the hardware interrupt handling, but leaves the hotplug
4681fca52a55SDaniel Vetter  * handling still disabled. It is called after intel_irq_init().
4682fca52a55SDaniel Vetter  *
4683fca52a55SDaniel Vetter  * In the driver load and resume code we need working interrupts in a few places
4684fca52a55SDaniel Vetter  * but don't want to deal with the hassle of concurrent probe and hotplug
4685fca52a55SDaniel Vetter  * workers. Hence the split into this two-stage approach.
4686fca52a55SDaniel Vetter  */
46872aeb7d3aSDaniel Vetter int intel_irq_install(struct drm_i915_private *dev_priv)
46882aeb7d3aSDaniel Vetter {
46892aeb7d3aSDaniel Vetter 	/*
46902aeb7d3aSDaniel Vetter 	 * We enable some interrupt sources in our postinstall hooks, so mark
46912aeb7d3aSDaniel Vetter 	 * interrupts as enabled _before_ actually enabling them to avoid
46922aeb7d3aSDaniel Vetter 	 * special cases in our ordering checks.
46932aeb7d3aSDaniel Vetter 	 */
4694ad1443f0SSagar Arun Kamble 	dev_priv->runtime_pm.irqs_enabled = true;
46952aeb7d3aSDaniel Vetter 
469691c8a326SChris Wilson 	return drm_irq_install(&dev_priv->drm, dev_priv->drm.pdev->irq);
46972aeb7d3aSDaniel Vetter }
46982aeb7d3aSDaniel Vetter 
4699fca52a55SDaniel Vetter /**
4700fca52a55SDaniel Vetter  * intel_irq_uninstall - finilizes all irq handling
4701fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4702fca52a55SDaniel Vetter  *
4703fca52a55SDaniel Vetter  * This stops interrupt and hotplug handling and unregisters and frees all
4704fca52a55SDaniel Vetter  * resources acquired in the init functions.
4705fca52a55SDaniel Vetter  */
47062aeb7d3aSDaniel Vetter void intel_irq_uninstall(struct drm_i915_private *dev_priv)
47072aeb7d3aSDaniel Vetter {
470891c8a326SChris Wilson 	drm_irq_uninstall(&dev_priv->drm);
47092aeb7d3aSDaniel Vetter 	intel_hpd_cancel_work(dev_priv);
4710ad1443f0SSagar Arun Kamble 	dev_priv->runtime_pm.irqs_enabled = false;
47112aeb7d3aSDaniel Vetter }
47122aeb7d3aSDaniel Vetter 
4713fca52a55SDaniel Vetter /**
4714fca52a55SDaniel Vetter  * intel_runtime_pm_disable_interrupts - runtime interrupt disabling
4715fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4716fca52a55SDaniel Vetter  *
4717fca52a55SDaniel Vetter  * This function is used to disable interrupts at runtime, both in the runtime
4718fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4719fca52a55SDaniel Vetter  */
4720b963291cSDaniel Vetter void intel_runtime_pm_disable_interrupts(struct drm_i915_private *dev_priv)
4721c67a470bSPaulo Zanoni {
472291c8a326SChris Wilson 	dev_priv->drm.driver->irq_uninstall(&dev_priv->drm);
4723ad1443f0SSagar Arun Kamble 	dev_priv->runtime_pm.irqs_enabled = false;
472491c8a326SChris Wilson 	synchronize_irq(dev_priv->drm.irq);
4725c67a470bSPaulo Zanoni }
4726c67a470bSPaulo Zanoni 
4727fca52a55SDaniel Vetter /**
4728fca52a55SDaniel Vetter  * intel_runtime_pm_enable_interrupts - runtime interrupt enabling
4729fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4730fca52a55SDaniel Vetter  *
4731fca52a55SDaniel Vetter  * This function is used to enable interrupts at runtime, both in the runtime
4732fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4733fca52a55SDaniel Vetter  */
4734b963291cSDaniel Vetter void intel_runtime_pm_enable_interrupts(struct drm_i915_private *dev_priv)
4735c67a470bSPaulo Zanoni {
4736ad1443f0SSagar Arun Kamble 	dev_priv->runtime_pm.irqs_enabled = true;
473791c8a326SChris Wilson 	dev_priv->drm.driver->irq_preinstall(&dev_priv->drm);
473891c8a326SChris Wilson 	dev_priv->drm.driver->irq_postinstall(&dev_priv->drm);
4739c67a470bSPaulo Zanoni }
4740