xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 74bb98baa0e45a0a0157e1370b4614d569447e87)
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 
31b2c88f5bSDamien Lespiau #include <linux/circ_buf.h>
3255367a27SJani Nikula #include <linux/slab.h>
3355367a27SJani Nikula #include <linux/sysrq.h>
3455367a27SJani Nikula 
35fcd70cd3SDaniel Vetter #include <drm/drm_drv.h>
3655367a27SJani Nikula #include <drm/drm_irq.h>
37760285e7SDavid Howells #include <drm/i915_drm.h>
3855367a27SJani Nikula 
391d455f8dSJani Nikula #include "display/intel_display_types.h"
40df0566a6SJani Nikula #include "display/intel_fifo_underrun.h"
41df0566a6SJani Nikula #include "display/intel_hotplug.h"
42df0566a6SJani Nikula #include "display/intel_lpe_audio.h"
43df0566a6SJani Nikula #include "display/intel_psr.h"
44df0566a6SJani Nikula 
452239e6dfSDaniele Ceraolo Spurio #include "gt/intel_gt.h"
46cf1c97dcSAndi Shyti #include "gt/intel_gt_irq.h"
47d762043fSAndi Shyti #include "gt/intel_gt_pm_irq.h"
483e7abf81SAndi Shyti #include "gt/intel_rps.h"
492239e6dfSDaniele Ceraolo Spurio 
50c0e09200SDave Airlie #include "i915_drv.h"
51440e2b3dSJani Nikula #include "i915_irq.h"
521c5d22f7SChris Wilson #include "i915_trace.h"
53d13616dbSJani Nikula #include "intel_pm.h"
54c0e09200SDave Airlie 
55fca52a55SDaniel Vetter /**
56fca52a55SDaniel Vetter  * DOC: interrupt handling
57fca52a55SDaniel Vetter  *
58fca52a55SDaniel Vetter  * These functions provide the basic support for enabling and disabling the
59fca52a55SDaniel Vetter  * interrupt handling support. There's a lot more functionality in i915_irq.c
60fca52a55SDaniel Vetter  * and related files, but that will be described in separate chapters.
61fca52a55SDaniel Vetter  */
62fca52a55SDaniel Vetter 
6348ef15d3SJosé Roberto de Souza typedef bool (*long_pulse_detect_func)(enum hpd_pin pin, u32 val);
6448ef15d3SJosé Roberto de Souza 
65e4ce95aaSVille Syrjälä static const u32 hpd_ilk[HPD_NUM_PINS] = {
66e4ce95aaSVille Syrjälä 	[HPD_PORT_A] = DE_DP_A_HOTPLUG,
67e4ce95aaSVille Syrjälä };
68e4ce95aaSVille Syrjälä 
6923bb4cb5SVille Syrjälä static const u32 hpd_ivb[HPD_NUM_PINS] = {
7023bb4cb5SVille Syrjälä 	[HPD_PORT_A] = DE_DP_A_HOTPLUG_IVB,
7123bb4cb5SVille Syrjälä };
7223bb4cb5SVille Syrjälä 
733a3b3c7dSVille Syrjälä static const u32 hpd_bdw[HPD_NUM_PINS] = {
743a3b3c7dSVille Syrjälä 	[HPD_PORT_A] = GEN8_PORT_DP_A_HOTPLUG,
753a3b3c7dSVille Syrjälä };
763a3b3c7dSVille Syrjälä 
777c7e10dbSVille Syrjälä static const u32 hpd_ibx[HPD_NUM_PINS] = {
78e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG,
79e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG,
80e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG,
81e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG,
82e5868a31SEgbert Eich 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG
83e5868a31SEgbert Eich };
84e5868a31SEgbert Eich 
857c7e10dbSVille Syrjälä static const u32 hpd_cpt[HPD_NUM_PINS] = {
86e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG_CPT,
8773c352a2SDaniel Vetter 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG_CPT,
88e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
89e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
90e5868a31SEgbert Eich 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT
91e5868a31SEgbert Eich };
92e5868a31SEgbert Eich 
9326951cafSXiong Zhang static const u32 hpd_spt[HPD_NUM_PINS] = {
9474c0b395SVille Syrjälä 	[HPD_PORT_A] = SDE_PORTA_HOTPLUG_SPT,
9526951cafSXiong Zhang 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
9626951cafSXiong Zhang 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
9726951cafSXiong Zhang 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT,
9826951cafSXiong Zhang 	[HPD_PORT_E] = SDE_PORTE_HOTPLUG_SPT
9926951cafSXiong Zhang };
10026951cafSXiong Zhang 
1017c7e10dbSVille Syrjälä static const u32 hpd_mask_i915[HPD_NUM_PINS] = {
102e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_EN,
103e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_EN,
104e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_EN,
105e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_EN,
106e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_EN,
107e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_EN
108e5868a31SEgbert Eich };
109e5868a31SEgbert Eich 
1107c7e10dbSVille Syrjälä static const u32 hpd_status_g4x[HPD_NUM_PINS] = {
111e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
112e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_G4X,
113e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_G4X,
114e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
115e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
116e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
117e5868a31SEgbert Eich };
118e5868a31SEgbert Eich 
1194bca26d0SVille Syrjälä static const u32 hpd_status_i915[HPD_NUM_PINS] = {
120e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
121e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_I915,
122e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_I915,
123e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
124e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
125e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
126e5868a31SEgbert Eich };
127e5868a31SEgbert Eich 
128e0a20ad7SShashank Sharma /* BXT hpd list */
129e0a20ad7SShashank Sharma static const u32 hpd_bxt[HPD_NUM_PINS] = {
1307f3561beSSonika Jindal 	[HPD_PORT_A] = BXT_DE_PORT_HP_DDIA,
131e0a20ad7SShashank Sharma 	[HPD_PORT_B] = BXT_DE_PORT_HP_DDIB,
132e0a20ad7SShashank Sharma 	[HPD_PORT_C] = BXT_DE_PORT_HP_DDIC
133e0a20ad7SShashank Sharma };
134e0a20ad7SShashank Sharma 
135b796b971SDhinakaran Pandiyan static const u32 hpd_gen11[HPD_NUM_PINS] = {
136b796b971SDhinakaran Pandiyan 	[HPD_PORT_C] = GEN11_TC1_HOTPLUG | GEN11_TBT1_HOTPLUG,
137b796b971SDhinakaran Pandiyan 	[HPD_PORT_D] = GEN11_TC2_HOTPLUG | GEN11_TBT2_HOTPLUG,
138b796b971SDhinakaran Pandiyan 	[HPD_PORT_E] = GEN11_TC3_HOTPLUG | GEN11_TBT3_HOTPLUG,
139b796b971SDhinakaran Pandiyan 	[HPD_PORT_F] = GEN11_TC4_HOTPLUG | GEN11_TBT4_HOTPLUG
140121e758eSDhinakaran Pandiyan };
141121e758eSDhinakaran Pandiyan 
14248ef15d3SJosé Roberto de Souza static const u32 hpd_gen12[HPD_NUM_PINS] = {
14348ef15d3SJosé Roberto de Souza 	[HPD_PORT_D] = GEN11_TC1_HOTPLUG | GEN11_TBT1_HOTPLUG,
14448ef15d3SJosé Roberto de Souza 	[HPD_PORT_E] = GEN11_TC2_HOTPLUG | GEN11_TBT2_HOTPLUG,
14548ef15d3SJosé Roberto de Souza 	[HPD_PORT_F] = GEN11_TC3_HOTPLUG | GEN11_TBT3_HOTPLUG,
14648ef15d3SJosé Roberto de Souza 	[HPD_PORT_G] = GEN11_TC4_HOTPLUG | GEN11_TBT4_HOTPLUG,
14748ef15d3SJosé Roberto de Souza 	[HPD_PORT_H] = GEN12_TC5_HOTPLUG | GEN12_TBT5_HOTPLUG,
14848ef15d3SJosé Roberto de Souza 	[HPD_PORT_I] = GEN12_TC6_HOTPLUG | GEN12_TBT6_HOTPLUG
14948ef15d3SJosé Roberto de Souza };
15048ef15d3SJosé Roberto de Souza 
15131604222SAnusha Srivatsa static const u32 hpd_icp[HPD_NUM_PINS] = {
152b32821c0SLucas De Marchi 	[HPD_PORT_A] = SDE_DDI_HOTPLUG_ICP(PORT_A),
153b32821c0SLucas De Marchi 	[HPD_PORT_B] = SDE_DDI_HOTPLUG_ICP(PORT_B),
154b32821c0SLucas De Marchi 	[HPD_PORT_C] = SDE_TC_HOTPLUG_ICP(PORT_TC1),
155b32821c0SLucas De Marchi 	[HPD_PORT_D] = SDE_TC_HOTPLUG_ICP(PORT_TC2),
156b32821c0SLucas De Marchi 	[HPD_PORT_E] = SDE_TC_HOTPLUG_ICP(PORT_TC3),
157b32821c0SLucas De Marchi 	[HPD_PORT_F] = SDE_TC_HOTPLUG_ICP(PORT_TC4),
15831604222SAnusha Srivatsa };
15931604222SAnusha Srivatsa 
16052dfdba0SLucas De Marchi static const u32 hpd_tgp[HPD_NUM_PINS] = {
161b32821c0SLucas De Marchi 	[HPD_PORT_A] = SDE_DDI_HOTPLUG_ICP(PORT_A),
162b32821c0SLucas De Marchi 	[HPD_PORT_B] = SDE_DDI_HOTPLUG_ICP(PORT_B),
163b32821c0SLucas De Marchi 	[HPD_PORT_C] = SDE_DDI_HOTPLUG_ICP(PORT_C),
164b32821c0SLucas De Marchi 	[HPD_PORT_D] = SDE_TC_HOTPLUG_ICP(PORT_TC1),
165b32821c0SLucas De Marchi 	[HPD_PORT_E] = SDE_TC_HOTPLUG_ICP(PORT_TC2),
166b32821c0SLucas De Marchi 	[HPD_PORT_F] = SDE_TC_HOTPLUG_ICP(PORT_TC3),
167b32821c0SLucas De Marchi 	[HPD_PORT_G] = SDE_TC_HOTPLUG_ICP(PORT_TC4),
168b32821c0SLucas De Marchi 	[HPD_PORT_H] = SDE_TC_HOTPLUG_ICP(PORT_TC5),
169b32821c0SLucas De Marchi 	[HPD_PORT_I] = SDE_TC_HOTPLUG_ICP(PORT_TC6),
17052dfdba0SLucas De Marchi };
17152dfdba0SLucas De Marchi 
172cf1c97dcSAndi Shyti void gen3_irq_reset(struct intel_uncore *uncore, i915_reg_t imr,
17368eb49b1SPaulo Zanoni 		    i915_reg_t iir, i915_reg_t ier)
17468eb49b1SPaulo Zanoni {
17565f42cdcSPaulo Zanoni 	intel_uncore_write(uncore, imr, 0xffffffff);
17665f42cdcSPaulo Zanoni 	intel_uncore_posting_read(uncore, imr);
17768eb49b1SPaulo Zanoni 
17865f42cdcSPaulo Zanoni 	intel_uncore_write(uncore, ier, 0);
17968eb49b1SPaulo Zanoni 
1805c502442SPaulo Zanoni 	/* IIR can theoretically queue up two events. Be paranoid. */
18165f42cdcSPaulo Zanoni 	intel_uncore_write(uncore, iir, 0xffffffff);
18265f42cdcSPaulo Zanoni 	intel_uncore_posting_read(uncore, iir);
18365f42cdcSPaulo Zanoni 	intel_uncore_write(uncore, iir, 0xffffffff);
18465f42cdcSPaulo Zanoni 	intel_uncore_posting_read(uncore, iir);
18568eb49b1SPaulo Zanoni }
1865c502442SPaulo Zanoni 
187cf1c97dcSAndi Shyti void gen2_irq_reset(struct intel_uncore *uncore)
18868eb49b1SPaulo Zanoni {
18965f42cdcSPaulo Zanoni 	intel_uncore_write16(uncore, GEN2_IMR, 0xffff);
19065f42cdcSPaulo Zanoni 	intel_uncore_posting_read16(uncore, GEN2_IMR);
191a9d356a6SPaulo Zanoni 
19265f42cdcSPaulo Zanoni 	intel_uncore_write16(uncore, GEN2_IER, 0);
19368eb49b1SPaulo Zanoni 
19468eb49b1SPaulo Zanoni 	/* IIR can theoretically queue up two events. Be paranoid. */
19565f42cdcSPaulo Zanoni 	intel_uncore_write16(uncore, GEN2_IIR, 0xffff);
19665f42cdcSPaulo Zanoni 	intel_uncore_posting_read16(uncore, GEN2_IIR);
19765f42cdcSPaulo Zanoni 	intel_uncore_write16(uncore, GEN2_IIR, 0xffff);
19865f42cdcSPaulo Zanoni 	intel_uncore_posting_read16(uncore, GEN2_IIR);
19968eb49b1SPaulo Zanoni }
20068eb49b1SPaulo Zanoni 
201337ba017SPaulo Zanoni /*
202337ba017SPaulo Zanoni  * We should clear IMR at preinstall/uninstall, and just check at postinstall.
203337ba017SPaulo Zanoni  */
20465f42cdcSPaulo Zanoni static void gen3_assert_iir_is_zero(struct intel_uncore *uncore, i915_reg_t reg)
205b51a2842SVille Syrjälä {
20665f42cdcSPaulo Zanoni 	u32 val = intel_uncore_read(uncore, reg);
207b51a2842SVille Syrjälä 
208b51a2842SVille Syrjälä 	if (val == 0)
209b51a2842SVille Syrjälä 		return;
210b51a2842SVille Syrjälä 
211b51a2842SVille Syrjälä 	WARN(1, "Interrupt register 0x%x is not zero: 0x%08x\n",
212f0f59a00SVille Syrjälä 	     i915_mmio_reg_offset(reg), val);
21365f42cdcSPaulo Zanoni 	intel_uncore_write(uncore, reg, 0xffffffff);
21465f42cdcSPaulo Zanoni 	intel_uncore_posting_read(uncore, reg);
21565f42cdcSPaulo Zanoni 	intel_uncore_write(uncore, reg, 0xffffffff);
21665f42cdcSPaulo Zanoni 	intel_uncore_posting_read(uncore, reg);
217b51a2842SVille Syrjälä }
218337ba017SPaulo Zanoni 
21965f42cdcSPaulo Zanoni static void gen2_assert_iir_is_zero(struct intel_uncore *uncore)
220e9e9848aSVille Syrjälä {
22165f42cdcSPaulo Zanoni 	u16 val = intel_uncore_read16(uncore, GEN2_IIR);
222e9e9848aSVille Syrjälä 
223e9e9848aSVille Syrjälä 	if (val == 0)
224e9e9848aSVille Syrjälä 		return;
225e9e9848aSVille Syrjälä 
226e9e9848aSVille Syrjälä 	WARN(1, "Interrupt register 0x%x is not zero: 0x%08x\n",
2279d9523d8SPaulo Zanoni 	     i915_mmio_reg_offset(GEN2_IIR), val);
22865f42cdcSPaulo Zanoni 	intel_uncore_write16(uncore, GEN2_IIR, 0xffff);
22965f42cdcSPaulo Zanoni 	intel_uncore_posting_read16(uncore, GEN2_IIR);
23065f42cdcSPaulo Zanoni 	intel_uncore_write16(uncore, GEN2_IIR, 0xffff);
23165f42cdcSPaulo Zanoni 	intel_uncore_posting_read16(uncore, GEN2_IIR);
232e9e9848aSVille Syrjälä }
233e9e9848aSVille Syrjälä 
234cf1c97dcSAndi Shyti void gen3_irq_init(struct intel_uncore *uncore,
23568eb49b1SPaulo Zanoni 		   i915_reg_t imr, u32 imr_val,
23668eb49b1SPaulo Zanoni 		   i915_reg_t ier, u32 ier_val,
23768eb49b1SPaulo Zanoni 		   i915_reg_t iir)
23868eb49b1SPaulo Zanoni {
23965f42cdcSPaulo Zanoni 	gen3_assert_iir_is_zero(uncore, iir);
24035079899SPaulo Zanoni 
24165f42cdcSPaulo Zanoni 	intel_uncore_write(uncore, ier, ier_val);
24265f42cdcSPaulo Zanoni 	intel_uncore_write(uncore, imr, imr_val);
24365f42cdcSPaulo Zanoni 	intel_uncore_posting_read(uncore, imr);
24468eb49b1SPaulo Zanoni }
24535079899SPaulo Zanoni 
246cf1c97dcSAndi Shyti void gen2_irq_init(struct intel_uncore *uncore,
2472918c3caSPaulo Zanoni 		   u32 imr_val, u32 ier_val)
24868eb49b1SPaulo Zanoni {
24965f42cdcSPaulo Zanoni 	gen2_assert_iir_is_zero(uncore);
25068eb49b1SPaulo Zanoni 
25165f42cdcSPaulo Zanoni 	intel_uncore_write16(uncore, GEN2_IER, ier_val);
25265f42cdcSPaulo Zanoni 	intel_uncore_write16(uncore, GEN2_IMR, imr_val);
25365f42cdcSPaulo Zanoni 	intel_uncore_posting_read16(uncore, GEN2_IMR);
25468eb49b1SPaulo Zanoni }
25568eb49b1SPaulo Zanoni 
2560706f17cSEgbert Eich /* For display hotplug interrupt */
2570706f17cSEgbert Eich static inline void
2580706f17cSEgbert Eich i915_hotplug_interrupt_update_locked(struct drm_i915_private *dev_priv,
259a9c287c9SJani Nikula 				     u32 mask,
260a9c287c9SJani Nikula 				     u32 bits)
2610706f17cSEgbert Eich {
262a9c287c9SJani Nikula 	u32 val;
2630706f17cSEgbert Eich 
26467520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
2650706f17cSEgbert Eich 	WARN_ON(bits & ~mask);
2660706f17cSEgbert Eich 
2670706f17cSEgbert Eich 	val = I915_READ(PORT_HOTPLUG_EN);
2680706f17cSEgbert Eich 	val &= ~mask;
2690706f17cSEgbert Eich 	val |= bits;
2700706f17cSEgbert Eich 	I915_WRITE(PORT_HOTPLUG_EN, val);
2710706f17cSEgbert Eich }
2720706f17cSEgbert Eich 
2730706f17cSEgbert Eich /**
2740706f17cSEgbert Eich  * i915_hotplug_interrupt_update - update hotplug interrupt enable
2750706f17cSEgbert Eich  * @dev_priv: driver private
2760706f17cSEgbert Eich  * @mask: bits to update
2770706f17cSEgbert Eich  * @bits: bits to enable
2780706f17cSEgbert Eich  * NOTE: the HPD enable bits are modified both inside and outside
2790706f17cSEgbert Eich  * of an interrupt context. To avoid that read-modify-write cycles
2800706f17cSEgbert Eich  * interfer, these bits are protected by a spinlock. Since this
2810706f17cSEgbert Eich  * function is usually not called from a context where the lock is
2820706f17cSEgbert Eich  * held already, this function acquires the lock itself. A non-locking
2830706f17cSEgbert Eich  * version is also available.
2840706f17cSEgbert Eich  */
2850706f17cSEgbert Eich void i915_hotplug_interrupt_update(struct drm_i915_private *dev_priv,
286a9c287c9SJani Nikula 				   u32 mask,
287a9c287c9SJani Nikula 				   u32 bits)
2880706f17cSEgbert Eich {
2890706f17cSEgbert Eich 	spin_lock_irq(&dev_priv->irq_lock);
2900706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv, mask, bits);
2910706f17cSEgbert Eich 	spin_unlock_irq(&dev_priv->irq_lock);
2920706f17cSEgbert Eich }
2930706f17cSEgbert Eich 
294d9dc34f1SVille Syrjälä /**
295d9dc34f1SVille Syrjälä  * ilk_update_display_irq - update DEIMR
296d9dc34f1SVille Syrjälä  * @dev_priv: driver private
297d9dc34f1SVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
298d9dc34f1SVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
299d9dc34f1SVille Syrjälä  */
300fbdedaeaSVille Syrjälä void ilk_update_display_irq(struct drm_i915_private *dev_priv,
301a9c287c9SJani Nikula 			    u32 interrupt_mask,
302a9c287c9SJani Nikula 			    u32 enabled_irq_mask)
303036a4a7dSZhenyu Wang {
304a9c287c9SJani Nikula 	u32 new_val;
305d9dc34f1SVille Syrjälä 
30667520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
3074bc9d430SDaniel Vetter 
308d9dc34f1SVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
309d9dc34f1SVille Syrjälä 
3109df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
311c67a470bSPaulo Zanoni 		return;
312c67a470bSPaulo Zanoni 
313d9dc34f1SVille Syrjälä 	new_val = dev_priv->irq_mask;
314d9dc34f1SVille Syrjälä 	new_val &= ~interrupt_mask;
315d9dc34f1SVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
316d9dc34f1SVille Syrjälä 
317d9dc34f1SVille Syrjälä 	if (new_val != dev_priv->irq_mask) {
318d9dc34f1SVille Syrjälä 		dev_priv->irq_mask = new_val;
3191ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
3203143a2bfSChris Wilson 		POSTING_READ(DEIMR);
321036a4a7dSZhenyu Wang 	}
322036a4a7dSZhenyu Wang }
323036a4a7dSZhenyu Wang 
3240961021aSBen Widawsky /**
3253a3b3c7dSVille Syrjälä  * bdw_update_port_irq - update DE port interrupt
3263a3b3c7dSVille Syrjälä  * @dev_priv: driver private
3273a3b3c7dSVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
3283a3b3c7dSVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
3293a3b3c7dSVille Syrjälä  */
3303a3b3c7dSVille Syrjälä static void bdw_update_port_irq(struct drm_i915_private *dev_priv,
331a9c287c9SJani Nikula 				u32 interrupt_mask,
332a9c287c9SJani Nikula 				u32 enabled_irq_mask)
3333a3b3c7dSVille Syrjälä {
334a9c287c9SJani Nikula 	u32 new_val;
335a9c287c9SJani Nikula 	u32 old_val;
3363a3b3c7dSVille Syrjälä 
33767520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
3383a3b3c7dSVille Syrjälä 
3393a3b3c7dSVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
3403a3b3c7dSVille Syrjälä 
3413a3b3c7dSVille Syrjälä 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
3423a3b3c7dSVille Syrjälä 		return;
3433a3b3c7dSVille Syrjälä 
3443a3b3c7dSVille Syrjälä 	old_val = I915_READ(GEN8_DE_PORT_IMR);
3453a3b3c7dSVille Syrjälä 
3463a3b3c7dSVille Syrjälä 	new_val = old_val;
3473a3b3c7dSVille Syrjälä 	new_val &= ~interrupt_mask;
3483a3b3c7dSVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
3493a3b3c7dSVille Syrjälä 
3503a3b3c7dSVille Syrjälä 	if (new_val != old_val) {
3513a3b3c7dSVille Syrjälä 		I915_WRITE(GEN8_DE_PORT_IMR, new_val);
3523a3b3c7dSVille Syrjälä 		POSTING_READ(GEN8_DE_PORT_IMR);
3533a3b3c7dSVille Syrjälä 	}
3543a3b3c7dSVille Syrjälä }
3553a3b3c7dSVille Syrjälä 
3563a3b3c7dSVille Syrjälä /**
357013d3752SVille Syrjälä  * bdw_update_pipe_irq - update DE pipe interrupt
358013d3752SVille Syrjälä  * @dev_priv: driver private
359013d3752SVille Syrjälä  * @pipe: pipe whose interrupt to update
360013d3752SVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
361013d3752SVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
362013d3752SVille Syrjälä  */
363013d3752SVille Syrjälä void bdw_update_pipe_irq(struct drm_i915_private *dev_priv,
364013d3752SVille Syrjälä 			 enum pipe pipe,
365a9c287c9SJani Nikula 			 u32 interrupt_mask,
366a9c287c9SJani Nikula 			 u32 enabled_irq_mask)
367013d3752SVille Syrjälä {
368a9c287c9SJani Nikula 	u32 new_val;
369013d3752SVille Syrjälä 
37067520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
371013d3752SVille Syrjälä 
372013d3752SVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
373013d3752SVille Syrjälä 
374013d3752SVille Syrjälä 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
375013d3752SVille Syrjälä 		return;
376013d3752SVille Syrjälä 
377013d3752SVille Syrjälä 	new_val = dev_priv->de_irq_mask[pipe];
378013d3752SVille Syrjälä 	new_val &= ~interrupt_mask;
379013d3752SVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
380013d3752SVille Syrjälä 
381013d3752SVille Syrjälä 	if (new_val != dev_priv->de_irq_mask[pipe]) {
382013d3752SVille Syrjälä 		dev_priv->de_irq_mask[pipe] = new_val;
383013d3752SVille Syrjälä 		I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
384013d3752SVille Syrjälä 		POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
385013d3752SVille Syrjälä 	}
386013d3752SVille Syrjälä }
387013d3752SVille Syrjälä 
388013d3752SVille Syrjälä /**
389fee884edSDaniel Vetter  * ibx_display_interrupt_update - update SDEIMR
390fee884edSDaniel Vetter  * @dev_priv: driver private
391fee884edSDaniel Vetter  * @interrupt_mask: mask of interrupt bits to update
392fee884edSDaniel Vetter  * @enabled_irq_mask: mask of interrupt bits to enable
393fee884edSDaniel Vetter  */
39447339cd9SDaniel Vetter void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
395a9c287c9SJani Nikula 				  u32 interrupt_mask,
396a9c287c9SJani Nikula 				  u32 enabled_irq_mask)
397fee884edSDaniel Vetter {
398a9c287c9SJani Nikula 	u32 sdeimr = I915_READ(SDEIMR);
399fee884edSDaniel Vetter 	sdeimr &= ~interrupt_mask;
400fee884edSDaniel Vetter 	sdeimr |= (~enabled_irq_mask & interrupt_mask);
401fee884edSDaniel Vetter 
40215a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
40315a17aaeSDaniel Vetter 
40467520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
405fee884edSDaniel Vetter 
4069df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
407c67a470bSPaulo Zanoni 		return;
408c67a470bSPaulo Zanoni 
409fee884edSDaniel Vetter 	I915_WRITE(SDEIMR, sdeimr);
410fee884edSDaniel Vetter 	POSTING_READ(SDEIMR);
411fee884edSDaniel Vetter }
4128664281bSPaulo Zanoni 
4136b12ca56SVille Syrjälä u32 i915_pipestat_enable_mask(struct drm_i915_private *dev_priv,
4146b12ca56SVille Syrjälä 			      enum pipe pipe)
4157c463586SKeith Packard {
4166b12ca56SVille Syrjälä 	u32 status_mask = dev_priv->pipestat_irq_mask[pipe];
41710c59c51SImre Deak 	u32 enable_mask = status_mask << 16;
41810c59c51SImre Deak 
4196b12ca56SVille Syrjälä 	lockdep_assert_held(&dev_priv->irq_lock);
4206b12ca56SVille Syrjälä 
4216b12ca56SVille Syrjälä 	if (INTEL_GEN(dev_priv) < 5)
4226b12ca56SVille Syrjälä 		goto out;
4236b12ca56SVille Syrjälä 
42410c59c51SImre Deak 	/*
425724a6905SVille Syrjälä 	 * On pipe A we don't support the PSR interrupt yet,
426724a6905SVille Syrjälä 	 * on pipe B and C the same bit MBZ.
42710c59c51SImre Deak 	 */
42810c59c51SImre Deak 	if (WARN_ON_ONCE(status_mask & PIPE_A_PSR_STATUS_VLV))
42910c59c51SImre Deak 		return 0;
430724a6905SVille Syrjälä 	/*
431724a6905SVille Syrjälä 	 * On pipe B and C we don't support the PSR interrupt yet, on pipe
432724a6905SVille Syrjälä 	 * A the same bit is for perf counters which we don't use either.
433724a6905SVille Syrjälä 	 */
434724a6905SVille Syrjälä 	if (WARN_ON_ONCE(status_mask & PIPE_B_PSR_STATUS_VLV))
435724a6905SVille Syrjälä 		return 0;
43610c59c51SImre Deak 
43710c59c51SImre Deak 	enable_mask &= ~(PIPE_FIFO_UNDERRUN_STATUS |
43810c59c51SImre Deak 			 SPRITE0_FLIP_DONE_INT_EN_VLV |
43910c59c51SImre Deak 			 SPRITE1_FLIP_DONE_INT_EN_VLV);
44010c59c51SImre Deak 	if (status_mask & SPRITE0_FLIP_DONE_INT_STATUS_VLV)
44110c59c51SImre Deak 		enable_mask |= SPRITE0_FLIP_DONE_INT_EN_VLV;
44210c59c51SImre Deak 	if (status_mask & SPRITE1_FLIP_DONE_INT_STATUS_VLV)
44310c59c51SImre Deak 		enable_mask |= SPRITE1_FLIP_DONE_INT_EN_VLV;
44410c59c51SImre Deak 
4456b12ca56SVille Syrjälä out:
4466b12ca56SVille Syrjälä 	WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
4476b12ca56SVille Syrjälä 		  status_mask & ~PIPESTAT_INT_STATUS_MASK,
4486b12ca56SVille Syrjälä 		  "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
4496b12ca56SVille Syrjälä 		  pipe_name(pipe), enable_mask, status_mask);
4506b12ca56SVille Syrjälä 
45110c59c51SImre Deak 	return enable_mask;
45210c59c51SImre Deak }
45310c59c51SImre Deak 
4546b12ca56SVille Syrjälä void i915_enable_pipestat(struct drm_i915_private *dev_priv,
4556b12ca56SVille Syrjälä 			  enum pipe pipe, u32 status_mask)
456755e9019SImre Deak {
4576b12ca56SVille Syrjälä 	i915_reg_t reg = PIPESTAT(pipe);
458755e9019SImre Deak 	u32 enable_mask;
459755e9019SImre Deak 
4606b12ca56SVille Syrjälä 	WARN_ONCE(status_mask & ~PIPESTAT_INT_STATUS_MASK,
4616b12ca56SVille Syrjälä 		  "pipe %c: status_mask=0x%x\n",
4626b12ca56SVille Syrjälä 		  pipe_name(pipe), status_mask);
4636b12ca56SVille Syrjälä 
4646b12ca56SVille Syrjälä 	lockdep_assert_held(&dev_priv->irq_lock);
4656b12ca56SVille Syrjälä 	WARN_ON(!intel_irqs_enabled(dev_priv));
4666b12ca56SVille Syrjälä 
4676b12ca56SVille Syrjälä 	if ((dev_priv->pipestat_irq_mask[pipe] & status_mask) == status_mask)
4686b12ca56SVille Syrjälä 		return;
4696b12ca56SVille Syrjälä 
4706b12ca56SVille Syrjälä 	dev_priv->pipestat_irq_mask[pipe] |= status_mask;
4716b12ca56SVille Syrjälä 	enable_mask = i915_pipestat_enable_mask(dev_priv, pipe);
4726b12ca56SVille Syrjälä 
4736b12ca56SVille Syrjälä 	I915_WRITE(reg, enable_mask | status_mask);
4746b12ca56SVille Syrjälä 	POSTING_READ(reg);
475755e9019SImre Deak }
476755e9019SImre Deak 
4776b12ca56SVille Syrjälä void i915_disable_pipestat(struct drm_i915_private *dev_priv,
4786b12ca56SVille Syrjälä 			   enum pipe pipe, u32 status_mask)
479755e9019SImre Deak {
4806b12ca56SVille Syrjälä 	i915_reg_t reg = PIPESTAT(pipe);
481755e9019SImre Deak 	u32 enable_mask;
482755e9019SImre Deak 
4836b12ca56SVille Syrjälä 	WARN_ONCE(status_mask & ~PIPESTAT_INT_STATUS_MASK,
4846b12ca56SVille Syrjälä 		  "pipe %c: status_mask=0x%x\n",
4856b12ca56SVille Syrjälä 		  pipe_name(pipe), status_mask);
4866b12ca56SVille Syrjälä 
4876b12ca56SVille Syrjälä 	lockdep_assert_held(&dev_priv->irq_lock);
4886b12ca56SVille Syrjälä 	WARN_ON(!intel_irqs_enabled(dev_priv));
4896b12ca56SVille Syrjälä 
4906b12ca56SVille Syrjälä 	if ((dev_priv->pipestat_irq_mask[pipe] & status_mask) == 0)
4916b12ca56SVille Syrjälä 		return;
4926b12ca56SVille Syrjälä 
4936b12ca56SVille Syrjälä 	dev_priv->pipestat_irq_mask[pipe] &= ~status_mask;
4946b12ca56SVille Syrjälä 	enable_mask = i915_pipestat_enable_mask(dev_priv, pipe);
4956b12ca56SVille Syrjälä 
4966b12ca56SVille Syrjälä 	I915_WRITE(reg, enable_mask | status_mask);
4976b12ca56SVille Syrjälä 	POSTING_READ(reg);
498755e9019SImre Deak }
499755e9019SImre Deak 
500f3e30485SVille Syrjälä static bool i915_has_asle(struct drm_i915_private *dev_priv)
501f3e30485SVille Syrjälä {
502f3e30485SVille Syrjälä 	if (!dev_priv->opregion.asle)
503f3e30485SVille Syrjälä 		return false;
504f3e30485SVille Syrjälä 
505f3e30485SVille Syrjälä 	return IS_PINEVIEW(dev_priv) || IS_MOBILE(dev_priv);
506f3e30485SVille Syrjälä }
507f3e30485SVille Syrjälä 
508c0e09200SDave Airlie /**
509f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
51014bb2c11STvrtko Ursulin  * @dev_priv: i915 device private
51101c66889SZhao Yakui  */
51291d14251STvrtko Ursulin static void i915_enable_asle_pipestat(struct drm_i915_private *dev_priv)
51301c66889SZhao Yakui {
514f3e30485SVille Syrjälä 	if (!i915_has_asle(dev_priv))
515f49e38ddSJani Nikula 		return;
516f49e38ddSJani Nikula 
51713321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
51801c66889SZhao Yakui 
519755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_STATUS);
52091d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 4)
5213b6c42e8SDaniel Vetter 		i915_enable_pipestat(dev_priv, PIPE_A,
522755e9019SImre Deak 				     PIPE_LEGACY_BLC_EVENT_STATUS);
5231ec14ad3SChris Wilson 
52413321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
52501c66889SZhao Yakui }
52601c66889SZhao Yakui 
527f75f3746SVille Syrjälä /*
528f75f3746SVille Syrjälä  * This timing diagram depicts the video signal in and
529f75f3746SVille Syrjälä  * around the vertical blanking period.
530f75f3746SVille Syrjälä  *
531f75f3746SVille Syrjälä  * Assumptions about the fictitious mode used in this example:
532f75f3746SVille Syrjälä  *  vblank_start >= 3
533f75f3746SVille Syrjälä  *  vsync_start = vblank_start + 1
534f75f3746SVille Syrjälä  *  vsync_end = vblank_start + 2
535f75f3746SVille Syrjälä  *  vtotal = vblank_start + 3
536f75f3746SVille Syrjälä  *
537f75f3746SVille Syrjälä  *           start of vblank:
538f75f3746SVille Syrjälä  *           latch double buffered registers
539f75f3746SVille Syrjälä  *           increment frame counter (ctg+)
540f75f3746SVille Syrjälä  *           generate start of vblank interrupt (gen4+)
541f75f3746SVille Syrjälä  *           |
542f75f3746SVille Syrjälä  *           |          frame start:
543f75f3746SVille Syrjälä  *           |          generate frame start interrupt (aka. vblank interrupt) (gmch)
544f75f3746SVille Syrjälä  *           |          may be shifted forward 1-3 extra lines via PIPECONF
545f75f3746SVille Syrjälä  *           |          |
546f75f3746SVille Syrjälä  *           |          |  start of vsync:
547f75f3746SVille Syrjälä  *           |          |  generate vsync interrupt
548f75f3746SVille Syrjälä  *           |          |  |
549f75f3746SVille Syrjälä  * ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx
550f75f3746SVille Syrjälä  *       .   \hs/   .      \hs/          \hs/          \hs/   .      \hs/
551f75f3746SVille Syrjälä  * ----va---> <-----------------vb--------------------> <--------va-------------
552f75f3746SVille Syrjälä  *       |          |       <----vs----->                     |
553f75f3746SVille Syrjälä  * -vbs-----> <---vbs+1---> <---vbs+2---> <-----0-----> <-----1-----> <-----2--- (scanline counter gen2)
554f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2---> <-----0--- (scanline counter gen3+)
555f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2- (scanline counter hsw+ hdmi)
556f75f3746SVille Syrjälä  *       |          |                                         |
557f75f3746SVille Syrjälä  *       last visible pixel                                   first visible pixel
558f75f3746SVille Syrjälä  *                  |                                         increment frame counter (gen3/4)
559f75f3746SVille Syrjälä  *                  pixel counter = vblank_start * htotal     pixel counter = 0 (gen3/4)
560f75f3746SVille Syrjälä  *
561f75f3746SVille Syrjälä  * x  = horizontal active
562f75f3746SVille Syrjälä  * _  = horizontal blanking
563f75f3746SVille Syrjälä  * hs = horizontal sync
564f75f3746SVille Syrjälä  * va = vertical active
565f75f3746SVille Syrjälä  * vb = vertical blanking
566f75f3746SVille Syrjälä  * vs = vertical sync
567f75f3746SVille Syrjälä  * vbs = vblank_start (number)
568f75f3746SVille Syrjälä  *
569f75f3746SVille Syrjälä  * Summary:
570f75f3746SVille Syrjälä  * - most events happen at the start of horizontal sync
571f75f3746SVille Syrjälä  * - frame start happens at the start of horizontal blank, 1-4 lines
572f75f3746SVille Syrjälä  *   (depending on PIPECONF settings) after the start of vblank
573f75f3746SVille Syrjälä  * - gen3/4 pixel and frame counter are synchronized with the start
574f75f3746SVille Syrjälä  *   of horizontal active on the first line of vertical active
575f75f3746SVille Syrjälä  */
576f75f3746SVille Syrjälä 
57742f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
57842f52ef8SKeith Packard  * we use as a pipe index
57942f52ef8SKeith Packard  */
58008fa8fd0SVille Syrjälä u32 i915_get_vblank_counter(struct drm_crtc *crtc)
5810a3e67a4SJesse Barnes {
58208fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
58308fa8fd0SVille Syrjälä 	struct drm_vblank_crtc *vblank = &dev_priv->drm.vblank[drm_crtc_index(crtc)];
58432db0b65SVille Syrjälä 	const struct drm_display_mode *mode = &vblank->hwmode;
58508fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
586f0f59a00SVille Syrjälä 	i915_reg_t high_frame, low_frame;
5870b2a8e09SVille Syrjälä 	u32 high1, high2, low, pixel, vbl_start, hsync_start, htotal;
588694e409dSVille Syrjälä 	unsigned long irqflags;
589391f75e2SVille Syrjälä 
59032db0b65SVille Syrjälä 	/*
59132db0b65SVille Syrjälä 	 * On i965gm TV output the frame counter only works up to
59232db0b65SVille Syrjälä 	 * the point when we enable the TV encoder. After that the
59332db0b65SVille Syrjälä 	 * frame counter ceases to work and reads zero. We need a
59432db0b65SVille Syrjälä 	 * vblank wait before enabling the TV encoder and so we
59532db0b65SVille Syrjälä 	 * have to enable vblank interrupts while the frame counter
59632db0b65SVille Syrjälä 	 * is still in a working state. However the core vblank code
59732db0b65SVille Syrjälä 	 * does not like us returning non-zero frame counter values
59832db0b65SVille Syrjälä 	 * when we've told it that we don't have a working frame
59932db0b65SVille Syrjälä 	 * counter. Thus we must stop non-zero values leaking out.
60032db0b65SVille Syrjälä 	 */
60132db0b65SVille Syrjälä 	if (!vblank->max_vblank_count)
60232db0b65SVille Syrjälä 		return 0;
60332db0b65SVille Syrjälä 
6040b2a8e09SVille Syrjälä 	htotal = mode->crtc_htotal;
6050b2a8e09SVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
6060b2a8e09SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
6070b2a8e09SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
6080b2a8e09SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
609391f75e2SVille Syrjälä 
6100b2a8e09SVille Syrjälä 	/* Convert to pixel count */
6110b2a8e09SVille Syrjälä 	vbl_start *= htotal;
6120b2a8e09SVille Syrjälä 
6130b2a8e09SVille Syrjälä 	/* Start of vblank event occurs at start of hsync */
6140b2a8e09SVille Syrjälä 	vbl_start -= htotal - hsync_start;
6150b2a8e09SVille Syrjälä 
6169db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
6179db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
6185eddb70bSChris Wilson 
619694e409dSVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
620694e409dSVille Syrjälä 
6210a3e67a4SJesse Barnes 	/*
6220a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
6230a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
6240a3e67a4SJesse Barnes 	 * register.
6250a3e67a4SJesse Barnes 	 */
6260a3e67a4SJesse Barnes 	do {
627694e409dSVille Syrjälä 		high1 = I915_READ_FW(high_frame) & PIPE_FRAME_HIGH_MASK;
628694e409dSVille Syrjälä 		low   = I915_READ_FW(low_frame);
629694e409dSVille Syrjälä 		high2 = I915_READ_FW(high_frame) & PIPE_FRAME_HIGH_MASK;
6300a3e67a4SJesse Barnes 	} while (high1 != high2);
6310a3e67a4SJesse Barnes 
632694e409dSVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
633694e409dSVille Syrjälä 
6345eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
635391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
6365eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
637391f75e2SVille Syrjälä 
638391f75e2SVille Syrjälä 	/*
639391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
640391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
641391f75e2SVille Syrjälä 	 * counter against vblank start.
642391f75e2SVille Syrjälä 	 */
643edc08d0aSVille Syrjälä 	return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
6440a3e67a4SJesse Barnes }
6450a3e67a4SJesse Barnes 
64608fa8fd0SVille Syrjälä u32 g4x_get_vblank_counter(struct drm_crtc *crtc)
6479880b7a5SJesse Barnes {
64808fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
64908fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
6509880b7a5SJesse Barnes 
651649636efSVille Syrjälä 	return I915_READ(PIPE_FRMCOUNT_G4X(pipe));
6529880b7a5SJesse Barnes }
6539880b7a5SJesse Barnes 
654aec0246fSUma Shankar /*
655aec0246fSUma Shankar  * On certain encoders on certain platforms, pipe
656aec0246fSUma Shankar  * scanline register will not work to get the scanline,
657aec0246fSUma Shankar  * since the timings are driven from the PORT or issues
658aec0246fSUma Shankar  * with scanline register updates.
659aec0246fSUma Shankar  * This function will use Framestamp and current
660aec0246fSUma Shankar  * timestamp registers to calculate the scanline.
661aec0246fSUma Shankar  */
662aec0246fSUma Shankar static u32 __intel_get_crtc_scanline_from_timestamp(struct intel_crtc *crtc)
663aec0246fSUma Shankar {
664aec0246fSUma Shankar 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
665aec0246fSUma Shankar 	struct drm_vblank_crtc *vblank =
666aec0246fSUma Shankar 		&crtc->base.dev->vblank[drm_crtc_index(&crtc->base)];
667aec0246fSUma Shankar 	const struct drm_display_mode *mode = &vblank->hwmode;
668aec0246fSUma Shankar 	u32 vblank_start = mode->crtc_vblank_start;
669aec0246fSUma Shankar 	u32 vtotal = mode->crtc_vtotal;
670aec0246fSUma Shankar 	u32 htotal = mode->crtc_htotal;
671aec0246fSUma Shankar 	u32 clock = mode->crtc_clock;
672aec0246fSUma Shankar 	u32 scanline, scan_prev_time, scan_curr_time, scan_post_time;
673aec0246fSUma Shankar 
674aec0246fSUma Shankar 	/*
675aec0246fSUma Shankar 	 * To avoid the race condition where we might cross into the
676aec0246fSUma Shankar 	 * next vblank just between the PIPE_FRMTMSTMP and TIMESTAMP_CTR
677aec0246fSUma Shankar 	 * reads. We make sure we read PIPE_FRMTMSTMP and TIMESTAMP_CTR
678aec0246fSUma Shankar 	 * during the same frame.
679aec0246fSUma Shankar 	 */
680aec0246fSUma Shankar 	do {
681aec0246fSUma Shankar 		/*
682aec0246fSUma Shankar 		 * This field provides read back of the display
683aec0246fSUma Shankar 		 * pipe frame time stamp. The time stamp value
684aec0246fSUma Shankar 		 * is sampled at every start of vertical blank.
685aec0246fSUma Shankar 		 */
686aec0246fSUma Shankar 		scan_prev_time = I915_READ_FW(PIPE_FRMTMSTMP(crtc->pipe));
687aec0246fSUma Shankar 
688aec0246fSUma Shankar 		/*
689aec0246fSUma Shankar 		 * The TIMESTAMP_CTR register has the current
690aec0246fSUma Shankar 		 * time stamp value.
691aec0246fSUma Shankar 		 */
692aec0246fSUma Shankar 		scan_curr_time = I915_READ_FW(IVB_TIMESTAMP_CTR);
693aec0246fSUma Shankar 
694aec0246fSUma Shankar 		scan_post_time = I915_READ_FW(PIPE_FRMTMSTMP(crtc->pipe));
695aec0246fSUma Shankar 	} while (scan_post_time != scan_prev_time);
696aec0246fSUma Shankar 
697aec0246fSUma Shankar 	scanline = div_u64(mul_u32_u32(scan_curr_time - scan_prev_time,
698aec0246fSUma Shankar 					clock), 1000 * htotal);
699aec0246fSUma Shankar 	scanline = min(scanline, vtotal - 1);
700aec0246fSUma Shankar 	scanline = (scanline + vblank_start) % vtotal;
701aec0246fSUma Shankar 
702aec0246fSUma Shankar 	return scanline;
703aec0246fSUma Shankar }
704aec0246fSUma Shankar 
70575aa3f63SVille Syrjälä /* I915_READ_FW, only for fast reads of display block, no need for forcewake etc. */
706a225f079SVille Syrjälä static int __intel_get_crtc_scanline(struct intel_crtc *crtc)
707a225f079SVille Syrjälä {
708a225f079SVille Syrjälä 	struct drm_device *dev = crtc->base.dev;
709fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
7105caa0feaSDaniel Vetter 	const struct drm_display_mode *mode;
7115caa0feaSDaniel Vetter 	struct drm_vblank_crtc *vblank;
712a225f079SVille Syrjälä 	enum pipe pipe = crtc->pipe;
71380715b2fSVille Syrjälä 	int position, vtotal;
714a225f079SVille Syrjälä 
71572259536SVille Syrjälä 	if (!crtc->active)
71672259536SVille Syrjälä 		return -1;
71772259536SVille Syrjälä 
7185caa0feaSDaniel Vetter 	vblank = &crtc->base.dev->vblank[drm_crtc_index(&crtc->base)];
7195caa0feaSDaniel Vetter 	mode = &vblank->hwmode;
7205caa0feaSDaniel Vetter 
721aec0246fSUma Shankar 	if (mode->private_flags & I915_MODE_FLAG_GET_SCANLINE_FROM_TIMESTAMP)
722aec0246fSUma Shankar 		return __intel_get_crtc_scanline_from_timestamp(crtc);
723aec0246fSUma Shankar 
72480715b2fSVille Syrjälä 	vtotal = mode->crtc_vtotal;
725a225f079SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
726a225f079SVille Syrjälä 		vtotal /= 2;
727a225f079SVille Syrjälä 
728cf819effSLucas De Marchi 	if (IS_GEN(dev_priv, 2))
72975aa3f63SVille Syrjälä 		position = I915_READ_FW(PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
730a225f079SVille Syrjälä 	else
73175aa3f63SVille Syrjälä 		position = I915_READ_FW(PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
732a225f079SVille Syrjälä 
733a225f079SVille Syrjälä 	/*
73441b578fbSJesse Barnes 	 * On HSW, the DSL reg (0x70000) appears to return 0 if we
73541b578fbSJesse Barnes 	 * read it just before the start of vblank.  So try it again
73641b578fbSJesse Barnes 	 * so we don't accidentally end up spanning a vblank frame
73741b578fbSJesse Barnes 	 * increment, causing the pipe_update_end() code to squak at us.
73841b578fbSJesse Barnes 	 *
73941b578fbSJesse Barnes 	 * The nature of this problem means we can't simply check the ISR
74041b578fbSJesse Barnes 	 * bit and return the vblank start value; nor can we use the scanline
74141b578fbSJesse Barnes 	 * debug register in the transcoder as it appears to have the same
74241b578fbSJesse Barnes 	 * problem.  We may need to extend this to include other platforms,
74341b578fbSJesse Barnes 	 * but so far testing only shows the problem on HSW.
74441b578fbSJesse Barnes 	 */
74591d14251STvrtko Ursulin 	if (HAS_DDI(dev_priv) && !position) {
74641b578fbSJesse Barnes 		int i, temp;
74741b578fbSJesse Barnes 
74841b578fbSJesse Barnes 		for (i = 0; i < 100; i++) {
74941b578fbSJesse Barnes 			udelay(1);
750707bdd3fSVille Syrjälä 			temp = I915_READ_FW(PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
75141b578fbSJesse Barnes 			if (temp != position) {
75241b578fbSJesse Barnes 				position = temp;
75341b578fbSJesse Barnes 				break;
75441b578fbSJesse Barnes 			}
75541b578fbSJesse Barnes 		}
75641b578fbSJesse Barnes 	}
75741b578fbSJesse Barnes 
75841b578fbSJesse Barnes 	/*
75980715b2fSVille Syrjälä 	 * See update_scanline_offset() for the details on the
76080715b2fSVille Syrjälä 	 * scanline_offset adjustment.
761a225f079SVille Syrjälä 	 */
76280715b2fSVille Syrjälä 	return (position + crtc->scanline_offset) % vtotal;
763a225f079SVille Syrjälä }
764a225f079SVille Syrjälä 
765e8edae54SVille Syrjälä bool i915_get_crtc_scanoutpos(struct drm_device *dev, unsigned int index,
7661bf6ad62SDaniel Vetter 			      bool in_vblank_irq, int *vpos, int *hpos,
7673bb403bfSVille Syrjälä 			      ktime_t *stime, ktime_t *etime,
7683bb403bfSVille Syrjälä 			      const struct drm_display_mode *mode)
7690af7e4dfSMario Kleiner {
770fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
771e8edae54SVille Syrjälä 	struct intel_crtc *crtc = to_intel_crtc(drm_crtc_from_index(dev, index));
772e8edae54SVille Syrjälä 	enum pipe pipe = crtc->pipe;
7733aa18df8SVille Syrjälä 	int position;
77478e8fc6bSVille Syrjälä 	int vbl_start, vbl_end, hsync_start, htotal, vtotal;
775ad3543edSMario Kleiner 	unsigned long irqflags;
7768a920e24SVille Syrjälä 	bool use_scanline_counter = INTEL_GEN(dev_priv) >= 5 ||
7778a920e24SVille Syrjälä 		IS_G4X(dev_priv) || IS_GEN(dev_priv, 2) ||
7788a920e24SVille Syrjälä 		mode->private_flags & I915_MODE_FLAG_USE_SCANLINE_COUNTER;
7790af7e4dfSMario Kleiner 
780fc467a22SMaarten Lankhorst 	if (WARN_ON(!mode->crtc_clock)) {
7810af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
7829db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
7831bf6ad62SDaniel Vetter 		return false;
7840af7e4dfSMario Kleiner 	}
7850af7e4dfSMario Kleiner 
786c2baf4b7SVille Syrjälä 	htotal = mode->crtc_htotal;
78778e8fc6bSVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
788c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
789c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
790c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
7910af7e4dfSMario Kleiner 
792d31faf65SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
793d31faf65SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
794d31faf65SVille Syrjälä 		vbl_end /= 2;
795d31faf65SVille Syrjälä 		vtotal /= 2;
796d31faf65SVille Syrjälä 	}
797d31faf65SVille Syrjälä 
798ad3543edSMario Kleiner 	/*
799ad3543edSMario Kleiner 	 * Lock uncore.lock, as we will do multiple timing critical raw
800ad3543edSMario Kleiner 	 * register reads, potentially with preemption disabled, so the
801ad3543edSMario Kleiner 	 * following code must not block on uncore.lock.
802ad3543edSMario Kleiner 	 */
803ad3543edSMario Kleiner 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
804ad3543edSMario Kleiner 
805ad3543edSMario Kleiner 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
806ad3543edSMario Kleiner 
807ad3543edSMario Kleiner 	/* Get optional system timestamp before query. */
808ad3543edSMario Kleiner 	if (stime)
809ad3543edSMario Kleiner 		*stime = ktime_get();
810ad3543edSMario Kleiner 
8118a920e24SVille Syrjälä 	if (use_scanline_counter) {
8120af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
8130af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
8140af7e4dfSMario Kleiner 		 */
815e8edae54SVille Syrjälä 		position = __intel_get_crtc_scanline(crtc);
8160af7e4dfSMario Kleiner 	} else {
8170af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
8180af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
8190af7e4dfSMario Kleiner 		 * scanout position.
8200af7e4dfSMario Kleiner 		 */
82175aa3f63SVille Syrjälä 		position = (I915_READ_FW(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
8220af7e4dfSMario Kleiner 
8233aa18df8SVille Syrjälä 		/* convert to pixel counts */
8243aa18df8SVille Syrjälä 		vbl_start *= htotal;
8253aa18df8SVille Syrjälä 		vbl_end *= htotal;
8263aa18df8SVille Syrjälä 		vtotal *= htotal;
82778e8fc6bSVille Syrjälä 
82878e8fc6bSVille Syrjälä 		/*
8297e78f1cbSVille Syrjälä 		 * In interlaced modes, the pixel counter counts all pixels,
8307e78f1cbSVille Syrjälä 		 * so one field will have htotal more pixels. In order to avoid
8317e78f1cbSVille Syrjälä 		 * the reported position from jumping backwards when the pixel
8327e78f1cbSVille Syrjälä 		 * counter is beyond the length of the shorter field, just
8337e78f1cbSVille Syrjälä 		 * clamp the position the length of the shorter field. This
8347e78f1cbSVille Syrjälä 		 * matches how the scanline counter based position works since
8357e78f1cbSVille Syrjälä 		 * the scanline counter doesn't count the two half lines.
8367e78f1cbSVille Syrjälä 		 */
8377e78f1cbSVille Syrjälä 		if (position >= vtotal)
8387e78f1cbSVille Syrjälä 			position = vtotal - 1;
8397e78f1cbSVille Syrjälä 
8407e78f1cbSVille Syrjälä 		/*
84178e8fc6bSVille Syrjälä 		 * Start of vblank interrupt is triggered at start of hsync,
84278e8fc6bSVille Syrjälä 		 * just prior to the first active line of vblank. However we
84378e8fc6bSVille Syrjälä 		 * consider lines to start at the leading edge of horizontal
84478e8fc6bSVille Syrjälä 		 * active. So, should we get here before we've crossed into
84578e8fc6bSVille Syrjälä 		 * the horizontal active of the first line in vblank, we would
84678e8fc6bSVille Syrjälä 		 * not set the DRM_SCANOUTPOS_INVBL flag. In order to fix that,
84778e8fc6bSVille Syrjälä 		 * always add htotal-hsync_start to the current pixel position.
84878e8fc6bSVille Syrjälä 		 */
84978e8fc6bSVille Syrjälä 		position = (position + htotal - hsync_start) % vtotal;
8503aa18df8SVille Syrjälä 	}
8513aa18df8SVille Syrjälä 
852ad3543edSMario Kleiner 	/* Get optional system timestamp after query. */
853ad3543edSMario Kleiner 	if (etime)
854ad3543edSMario Kleiner 		*etime = ktime_get();
855ad3543edSMario Kleiner 
856ad3543edSMario Kleiner 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
857ad3543edSMario Kleiner 
858ad3543edSMario Kleiner 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
859ad3543edSMario Kleiner 
8603aa18df8SVille Syrjälä 	/*
8613aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
8623aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
8633aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
8643aa18df8SVille Syrjälä 	 * up since vbl_end.
8653aa18df8SVille Syrjälä 	 */
8663aa18df8SVille Syrjälä 	if (position >= vbl_start)
8673aa18df8SVille Syrjälä 		position -= vbl_end;
8683aa18df8SVille Syrjälä 	else
8693aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
8703aa18df8SVille Syrjälä 
8718a920e24SVille Syrjälä 	if (use_scanline_counter) {
8723aa18df8SVille Syrjälä 		*vpos = position;
8733aa18df8SVille Syrjälä 		*hpos = 0;
8743aa18df8SVille Syrjälä 	} else {
8750af7e4dfSMario Kleiner 		*vpos = position / htotal;
8760af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
8770af7e4dfSMario Kleiner 	}
8780af7e4dfSMario Kleiner 
8791bf6ad62SDaniel Vetter 	return true;
8800af7e4dfSMario Kleiner }
8810af7e4dfSMario Kleiner 
882a225f079SVille Syrjälä int intel_get_crtc_scanline(struct intel_crtc *crtc)
883a225f079SVille Syrjälä {
884fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
885a225f079SVille Syrjälä 	unsigned long irqflags;
886a225f079SVille Syrjälä 	int position;
887a225f079SVille Syrjälä 
888a225f079SVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
889a225f079SVille Syrjälä 	position = __intel_get_crtc_scanline(crtc);
890a225f079SVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
891a225f079SVille Syrjälä 
892a225f079SVille Syrjälä 	return position;
893a225f079SVille Syrjälä }
894a225f079SVille Syrjälä 
895e3689190SBen Widawsky /**
896*74bb98baSLucas De Marchi  * ivb_parity_work - Workqueue called when a parity error interrupt
897e3689190SBen Widawsky  * occurred.
898e3689190SBen Widawsky  * @work: workqueue struct
899e3689190SBen Widawsky  *
900e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
901e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
902e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
903e3689190SBen Widawsky  */
904*74bb98baSLucas De Marchi static void ivb_parity_work(struct work_struct *work)
905e3689190SBen Widawsky {
9062d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
907cefcff8fSJoonas Lahtinen 		container_of(work, typeof(*dev_priv), l3_parity.error_work);
908cf1c97dcSAndi Shyti 	struct intel_gt *gt = &dev_priv->gt;
909e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
91035a85ac6SBen Widawsky 	char *parity_event[6];
911a9c287c9SJani Nikula 	u32 misccpctl;
912a9c287c9SJani Nikula 	u8 slice = 0;
913e3689190SBen Widawsky 
914e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
915e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
916e3689190SBen Widawsky 	 * any time we access those registers.
917e3689190SBen Widawsky 	 */
91891c8a326SChris Wilson 	mutex_lock(&dev_priv->drm.struct_mutex);
919e3689190SBen Widawsky 
92035a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
92135a85ac6SBen Widawsky 	if (WARN_ON(!dev_priv->l3_parity.which_slice))
92235a85ac6SBen Widawsky 		goto out;
92335a85ac6SBen Widawsky 
924e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
925e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
926e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
927e3689190SBen Widawsky 
92835a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
929f0f59a00SVille Syrjälä 		i915_reg_t reg;
93035a85ac6SBen Widawsky 
93135a85ac6SBen Widawsky 		slice--;
9322d1fe073SJoonas Lahtinen 		if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv)))
93335a85ac6SBen Widawsky 			break;
93435a85ac6SBen Widawsky 
93535a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
93635a85ac6SBen Widawsky 
9376fa1c5f1SVille Syrjälä 		reg = GEN7_L3CDERRST1(slice);
93835a85ac6SBen Widawsky 
93935a85ac6SBen Widawsky 		error_status = I915_READ(reg);
940e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
941e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
942e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
943e3689190SBen Widawsky 
94435a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
94535a85ac6SBen Widawsky 		POSTING_READ(reg);
946e3689190SBen Widawsky 
947cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
948e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
949e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
950e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
95135a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
95235a85ac6SBen Widawsky 		parity_event[5] = NULL;
953e3689190SBen Widawsky 
95491c8a326SChris Wilson 		kobject_uevent_env(&dev_priv->drm.primary->kdev->kobj,
955e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
956e3689190SBen Widawsky 
95735a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
95835a85ac6SBen Widawsky 			  slice, row, bank, subbank);
959e3689190SBen Widawsky 
96035a85ac6SBen Widawsky 		kfree(parity_event[4]);
961e3689190SBen Widawsky 		kfree(parity_event[3]);
962e3689190SBen Widawsky 		kfree(parity_event[2]);
963e3689190SBen Widawsky 		kfree(parity_event[1]);
964e3689190SBen Widawsky 	}
965e3689190SBen Widawsky 
96635a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
96735a85ac6SBen Widawsky 
96835a85ac6SBen Widawsky out:
96935a85ac6SBen Widawsky 	WARN_ON(dev_priv->l3_parity.which_slice);
970cf1c97dcSAndi Shyti 	spin_lock_irq(&gt->irq_lock);
971cf1c97dcSAndi Shyti 	gen5_gt_enable_irq(gt, GT_PARITY_ERROR(dev_priv));
972cf1c97dcSAndi Shyti 	spin_unlock_irq(&gt->irq_lock);
97335a85ac6SBen Widawsky 
97491c8a326SChris Wilson 	mutex_unlock(&dev_priv->drm.struct_mutex);
97535a85ac6SBen Widawsky }
97635a85ac6SBen Widawsky 
977af92058fSVille Syrjälä static bool gen11_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
978121e758eSDhinakaran Pandiyan {
979af92058fSVille Syrjälä 	switch (pin) {
980af92058fSVille Syrjälä 	case HPD_PORT_C:
981121e758eSDhinakaran Pandiyan 		return val & GEN11_HOTPLUG_CTL_LONG_DETECT(PORT_TC1);
982af92058fSVille Syrjälä 	case HPD_PORT_D:
983121e758eSDhinakaran Pandiyan 		return val & GEN11_HOTPLUG_CTL_LONG_DETECT(PORT_TC2);
984af92058fSVille Syrjälä 	case HPD_PORT_E:
985121e758eSDhinakaran Pandiyan 		return val & GEN11_HOTPLUG_CTL_LONG_DETECT(PORT_TC3);
986af92058fSVille Syrjälä 	case HPD_PORT_F:
987121e758eSDhinakaran Pandiyan 		return val & GEN11_HOTPLUG_CTL_LONG_DETECT(PORT_TC4);
988121e758eSDhinakaran Pandiyan 	default:
989121e758eSDhinakaran Pandiyan 		return false;
990121e758eSDhinakaran Pandiyan 	}
991121e758eSDhinakaran Pandiyan }
992121e758eSDhinakaran Pandiyan 
99348ef15d3SJosé Roberto de Souza static bool gen12_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
99448ef15d3SJosé Roberto de Souza {
99548ef15d3SJosé Roberto de Souza 	switch (pin) {
99648ef15d3SJosé Roberto de Souza 	case HPD_PORT_D:
99748ef15d3SJosé Roberto de Souza 		return val & GEN11_HOTPLUG_CTL_LONG_DETECT(PORT_TC1);
99848ef15d3SJosé Roberto de Souza 	case HPD_PORT_E:
99948ef15d3SJosé Roberto de Souza 		return val & GEN11_HOTPLUG_CTL_LONG_DETECT(PORT_TC2);
100048ef15d3SJosé Roberto de Souza 	case HPD_PORT_F:
100148ef15d3SJosé Roberto de Souza 		return val & GEN11_HOTPLUG_CTL_LONG_DETECT(PORT_TC3);
100248ef15d3SJosé Roberto de Souza 	case HPD_PORT_G:
100348ef15d3SJosé Roberto de Souza 		return val & GEN11_HOTPLUG_CTL_LONG_DETECT(PORT_TC4);
100448ef15d3SJosé Roberto de Souza 	case HPD_PORT_H:
100548ef15d3SJosé Roberto de Souza 		return val & GEN11_HOTPLUG_CTL_LONG_DETECT(PORT_TC5);
100648ef15d3SJosé Roberto de Souza 	case HPD_PORT_I:
100748ef15d3SJosé Roberto de Souza 		return val & GEN11_HOTPLUG_CTL_LONG_DETECT(PORT_TC6);
100848ef15d3SJosé Roberto de Souza 	default:
100948ef15d3SJosé Roberto de Souza 		return false;
101048ef15d3SJosé Roberto de Souza 	}
101148ef15d3SJosé Roberto de Souza }
101248ef15d3SJosé Roberto de Souza 
1013af92058fSVille Syrjälä static bool bxt_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
101463c88d22SImre Deak {
1015af92058fSVille Syrjälä 	switch (pin) {
1016af92058fSVille Syrjälä 	case HPD_PORT_A:
1017195baa06SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
1018af92058fSVille Syrjälä 	case HPD_PORT_B:
101963c88d22SImre Deak 		return val & PORTB_HOTPLUG_LONG_DETECT;
1020af92058fSVille Syrjälä 	case HPD_PORT_C:
102163c88d22SImre Deak 		return val & PORTC_HOTPLUG_LONG_DETECT;
102263c88d22SImre Deak 	default:
102363c88d22SImre Deak 		return false;
102463c88d22SImre Deak 	}
102563c88d22SImre Deak }
102663c88d22SImre Deak 
1027af92058fSVille Syrjälä static bool icp_ddi_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
102831604222SAnusha Srivatsa {
1029af92058fSVille Syrjälä 	switch (pin) {
1030af92058fSVille Syrjälä 	case HPD_PORT_A:
1031ed3126faSLucas De Marchi 		return val & SHOTPLUG_CTL_DDI_HPD_LONG_DETECT(PORT_A);
1032af92058fSVille Syrjälä 	case HPD_PORT_B:
1033ed3126faSLucas De Marchi 		return val & SHOTPLUG_CTL_DDI_HPD_LONG_DETECT(PORT_B);
10348ef7e340SMatt Roper 	case HPD_PORT_C:
1035ed3126faSLucas De Marchi 		return val & SHOTPLUG_CTL_DDI_HPD_LONG_DETECT(PORT_C);
103631604222SAnusha Srivatsa 	default:
103731604222SAnusha Srivatsa 		return false;
103831604222SAnusha Srivatsa 	}
103931604222SAnusha Srivatsa }
104031604222SAnusha Srivatsa 
1041af92058fSVille Syrjälä static bool icp_tc_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
104231604222SAnusha Srivatsa {
1043af92058fSVille Syrjälä 	switch (pin) {
1044af92058fSVille Syrjälä 	case HPD_PORT_C:
104531604222SAnusha Srivatsa 		return val & ICP_TC_HPD_LONG_DETECT(PORT_TC1);
1046af92058fSVille Syrjälä 	case HPD_PORT_D:
104731604222SAnusha Srivatsa 		return val & ICP_TC_HPD_LONG_DETECT(PORT_TC2);
1048af92058fSVille Syrjälä 	case HPD_PORT_E:
104931604222SAnusha Srivatsa 		return val & ICP_TC_HPD_LONG_DETECT(PORT_TC3);
1050af92058fSVille Syrjälä 	case HPD_PORT_F:
105131604222SAnusha Srivatsa 		return val & ICP_TC_HPD_LONG_DETECT(PORT_TC4);
105231604222SAnusha Srivatsa 	default:
105331604222SAnusha Srivatsa 		return false;
105431604222SAnusha Srivatsa 	}
105531604222SAnusha Srivatsa }
105631604222SAnusha Srivatsa 
105752dfdba0SLucas De Marchi static bool tgp_tc_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
105852dfdba0SLucas De Marchi {
105952dfdba0SLucas De Marchi 	switch (pin) {
106052dfdba0SLucas De Marchi 	case HPD_PORT_D:
106152dfdba0SLucas De Marchi 		return val & ICP_TC_HPD_LONG_DETECT(PORT_TC1);
106252dfdba0SLucas De Marchi 	case HPD_PORT_E:
106352dfdba0SLucas De Marchi 		return val & ICP_TC_HPD_LONG_DETECT(PORT_TC2);
106452dfdba0SLucas De Marchi 	case HPD_PORT_F:
106552dfdba0SLucas De Marchi 		return val & ICP_TC_HPD_LONG_DETECT(PORT_TC3);
106652dfdba0SLucas De Marchi 	case HPD_PORT_G:
106752dfdba0SLucas De Marchi 		return val & ICP_TC_HPD_LONG_DETECT(PORT_TC4);
106852dfdba0SLucas De Marchi 	case HPD_PORT_H:
106952dfdba0SLucas De Marchi 		return val & ICP_TC_HPD_LONG_DETECT(PORT_TC5);
107052dfdba0SLucas De Marchi 	case HPD_PORT_I:
107152dfdba0SLucas De Marchi 		return val & ICP_TC_HPD_LONG_DETECT(PORT_TC6);
107252dfdba0SLucas De Marchi 	default:
107352dfdba0SLucas De Marchi 		return false;
107452dfdba0SLucas De Marchi 	}
107552dfdba0SLucas De Marchi }
107652dfdba0SLucas De Marchi 
1077af92058fSVille Syrjälä static bool spt_port_hotplug2_long_detect(enum hpd_pin pin, u32 val)
10786dbf30ceSVille Syrjälä {
1079af92058fSVille Syrjälä 	switch (pin) {
1080af92058fSVille Syrjälä 	case HPD_PORT_E:
10816dbf30ceSVille Syrjälä 		return val & PORTE_HOTPLUG_LONG_DETECT;
10826dbf30ceSVille Syrjälä 	default:
10836dbf30ceSVille Syrjälä 		return false;
10846dbf30ceSVille Syrjälä 	}
10856dbf30ceSVille Syrjälä }
10866dbf30ceSVille Syrjälä 
1087af92058fSVille Syrjälä static bool spt_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
108874c0b395SVille Syrjälä {
1089af92058fSVille Syrjälä 	switch (pin) {
1090af92058fSVille Syrjälä 	case HPD_PORT_A:
109174c0b395SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
1092af92058fSVille Syrjälä 	case HPD_PORT_B:
109374c0b395SVille Syrjälä 		return val & PORTB_HOTPLUG_LONG_DETECT;
1094af92058fSVille Syrjälä 	case HPD_PORT_C:
109574c0b395SVille Syrjälä 		return val & PORTC_HOTPLUG_LONG_DETECT;
1096af92058fSVille Syrjälä 	case HPD_PORT_D:
109774c0b395SVille Syrjälä 		return val & PORTD_HOTPLUG_LONG_DETECT;
109874c0b395SVille Syrjälä 	default:
109974c0b395SVille Syrjälä 		return false;
110074c0b395SVille Syrjälä 	}
110174c0b395SVille Syrjälä }
110274c0b395SVille Syrjälä 
1103af92058fSVille Syrjälä static bool ilk_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
1104e4ce95aaSVille Syrjälä {
1105af92058fSVille Syrjälä 	switch (pin) {
1106af92058fSVille Syrjälä 	case HPD_PORT_A:
1107e4ce95aaSVille Syrjälä 		return val & DIGITAL_PORTA_HOTPLUG_LONG_DETECT;
1108e4ce95aaSVille Syrjälä 	default:
1109e4ce95aaSVille Syrjälä 		return false;
1110e4ce95aaSVille Syrjälä 	}
1111e4ce95aaSVille Syrjälä }
1112e4ce95aaSVille Syrjälä 
1113af92058fSVille Syrjälä static bool pch_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
111413cf5504SDave Airlie {
1115af92058fSVille Syrjälä 	switch (pin) {
1116af92058fSVille Syrjälä 	case HPD_PORT_B:
1117676574dfSJani Nikula 		return val & PORTB_HOTPLUG_LONG_DETECT;
1118af92058fSVille Syrjälä 	case HPD_PORT_C:
1119676574dfSJani Nikula 		return val & PORTC_HOTPLUG_LONG_DETECT;
1120af92058fSVille Syrjälä 	case HPD_PORT_D:
1121676574dfSJani Nikula 		return val & PORTD_HOTPLUG_LONG_DETECT;
1122676574dfSJani Nikula 	default:
1123676574dfSJani Nikula 		return false;
112413cf5504SDave Airlie 	}
112513cf5504SDave Airlie }
112613cf5504SDave Airlie 
1127af92058fSVille Syrjälä static bool i9xx_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
112813cf5504SDave Airlie {
1129af92058fSVille Syrjälä 	switch (pin) {
1130af92058fSVille Syrjälä 	case HPD_PORT_B:
1131676574dfSJani Nikula 		return val & PORTB_HOTPLUG_INT_LONG_PULSE;
1132af92058fSVille Syrjälä 	case HPD_PORT_C:
1133676574dfSJani Nikula 		return val & PORTC_HOTPLUG_INT_LONG_PULSE;
1134af92058fSVille Syrjälä 	case HPD_PORT_D:
1135676574dfSJani Nikula 		return val & PORTD_HOTPLUG_INT_LONG_PULSE;
1136676574dfSJani Nikula 	default:
1137676574dfSJani Nikula 		return false;
113813cf5504SDave Airlie 	}
113913cf5504SDave Airlie }
114013cf5504SDave Airlie 
114142db67d6SVille Syrjälä /*
114242db67d6SVille Syrjälä  * Get a bit mask of pins that have triggered, and which ones may be long.
114342db67d6SVille Syrjälä  * This can be called multiple times with the same masks to accumulate
114442db67d6SVille Syrjälä  * hotplug detection results from several registers.
114542db67d6SVille Syrjälä  *
114642db67d6SVille Syrjälä  * Note that the caller is expected to zero out the masks initially.
114742db67d6SVille Syrjälä  */
1148cf53902fSRodrigo Vivi static void intel_get_hpd_pins(struct drm_i915_private *dev_priv,
1149cf53902fSRodrigo Vivi 			       u32 *pin_mask, u32 *long_mask,
11508c841e57SJani Nikula 			       u32 hotplug_trigger, u32 dig_hotplug_reg,
1151fd63e2a9SImre Deak 			       const u32 hpd[HPD_NUM_PINS],
1152af92058fSVille Syrjälä 			       bool long_pulse_detect(enum hpd_pin pin, u32 val))
1153676574dfSJani Nikula {
1154e9be2850SVille Syrjälä 	enum hpd_pin pin;
1155676574dfSJani Nikula 
115652dfdba0SLucas De Marchi 	BUILD_BUG_ON(BITS_PER_TYPE(*pin_mask) < HPD_NUM_PINS);
115752dfdba0SLucas De Marchi 
1158e9be2850SVille Syrjälä 	for_each_hpd_pin(pin) {
1159e9be2850SVille Syrjälä 		if ((hpd[pin] & hotplug_trigger) == 0)
11608c841e57SJani Nikula 			continue;
11618c841e57SJani Nikula 
1162e9be2850SVille Syrjälä 		*pin_mask |= BIT(pin);
1163676574dfSJani Nikula 
1164af92058fSVille Syrjälä 		if (long_pulse_detect(pin, dig_hotplug_reg))
1165e9be2850SVille Syrjälä 			*long_mask |= BIT(pin);
1166676574dfSJani Nikula 	}
1167676574dfSJani Nikula 
1168f88f0478SVille Syrjälä 	DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x, dig 0x%08x, pins 0x%08x, long 0x%08x\n",
1169f88f0478SVille Syrjälä 			 hotplug_trigger, dig_hotplug_reg, *pin_mask, *long_mask);
1170676574dfSJani Nikula 
1171676574dfSJani Nikula }
1172676574dfSJani Nikula 
117391d14251STvrtko Ursulin static void gmbus_irq_handler(struct drm_i915_private *dev_priv)
1174515ac2bbSDaniel Vetter {
117528c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1176515ac2bbSDaniel Vetter }
1177515ac2bbSDaniel Vetter 
117891d14251STvrtko Ursulin static void dp_aux_irq_handler(struct drm_i915_private *dev_priv)
1179ce99c256SDaniel Vetter {
11809ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1181ce99c256SDaniel Vetter }
1182ce99c256SDaniel Vetter 
11838bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
118491d14251STvrtko Ursulin static void display_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
118591d14251STvrtko Ursulin 					 enum pipe pipe,
1186a9c287c9SJani Nikula 					 u32 crc0, u32 crc1,
1187a9c287c9SJani Nikula 					 u32 crc2, u32 crc3,
1188a9c287c9SJani Nikula 					 u32 crc4)
11898bf1e9f1SShuang He {
11908bf1e9f1SShuang He 	struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
11918c6b709dSTomeu Vizoso 	struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
11925cee6c45SVille Syrjälä 	u32 crcs[5] = { crc0, crc1, crc2, crc3, crc4 };
11935cee6c45SVille Syrjälä 
11945cee6c45SVille Syrjälä 	trace_intel_pipe_crc(crtc, crcs);
1195b2c88f5bSDamien Lespiau 
1196d538bbdfSDamien Lespiau 	spin_lock(&pipe_crc->lock);
11978c6b709dSTomeu Vizoso 	/*
11988c6b709dSTomeu Vizoso 	 * For some not yet identified reason, the first CRC is
11998c6b709dSTomeu Vizoso 	 * bonkers. So let's just wait for the next vblank and read
12008c6b709dSTomeu Vizoso 	 * out the buggy result.
12018c6b709dSTomeu Vizoso 	 *
1202163e8aecSRodrigo Vivi 	 * On GEN8+ sometimes the second CRC is bonkers as well, so
12038c6b709dSTomeu Vizoso 	 * don't trust that one either.
12048c6b709dSTomeu Vizoso 	 */
1205033b7a23SMaarten Lankhorst 	if (pipe_crc->skipped <= 0 ||
1206163e8aecSRodrigo Vivi 	    (INTEL_GEN(dev_priv) >= 8 && pipe_crc->skipped == 1)) {
12078c6b709dSTomeu Vizoso 		pipe_crc->skipped++;
12088c6b709dSTomeu Vizoso 		spin_unlock(&pipe_crc->lock);
12098c6b709dSTomeu Vizoso 		return;
12108c6b709dSTomeu Vizoso 	}
12118c6b709dSTomeu Vizoso 	spin_unlock(&pipe_crc->lock);
12126cc42152SMaarten Lankhorst 
1213246ee524STomeu Vizoso 	drm_crtc_add_crc_entry(&crtc->base, true,
1214ca814b25SDaniel Vetter 				drm_crtc_accurate_vblank_count(&crtc->base),
1215246ee524STomeu Vizoso 				crcs);
12168c6b709dSTomeu Vizoso }
1217277de95eSDaniel Vetter #else
1218277de95eSDaniel Vetter static inline void
121991d14251STvrtko Ursulin display_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
122091d14251STvrtko Ursulin 			     enum pipe pipe,
1221a9c287c9SJani Nikula 			     u32 crc0, u32 crc1,
1222a9c287c9SJani Nikula 			     u32 crc2, u32 crc3,
1223a9c287c9SJani Nikula 			     u32 crc4) {}
1224277de95eSDaniel Vetter #endif
1225eba94eb9SDaniel Vetter 
1226277de95eSDaniel Vetter 
122791d14251STvrtko Ursulin static void hsw_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
122891d14251STvrtko Ursulin 				     enum pipe pipe)
12295a69b89fSDaniel Vetter {
123091d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
12315a69b89fSDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
12325a69b89fSDaniel Vetter 				     0, 0, 0, 0);
12335a69b89fSDaniel Vetter }
12345a69b89fSDaniel Vetter 
123591d14251STvrtko Ursulin static void ivb_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
123691d14251STvrtko Ursulin 				     enum pipe pipe)
1237eba94eb9SDaniel Vetter {
123891d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
1239eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1240eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1241eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1242eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
12438bc5e955SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1244eba94eb9SDaniel Vetter }
12455b3a856bSDaniel Vetter 
124691d14251STvrtko Ursulin static void i9xx_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
124791d14251STvrtko Ursulin 				      enum pipe pipe)
12485b3a856bSDaniel Vetter {
1249a9c287c9SJani Nikula 	u32 res1, res2;
12500b5c5ed0SDaniel Vetter 
125191d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 3)
12520b5c5ed0SDaniel Vetter 		res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
12530b5c5ed0SDaniel Vetter 	else
12540b5c5ed0SDaniel Vetter 		res1 = 0;
12550b5c5ed0SDaniel Vetter 
125691d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
12570b5c5ed0SDaniel Vetter 		res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
12580b5c5ed0SDaniel Vetter 	else
12590b5c5ed0SDaniel Vetter 		res2 = 0;
12605b3a856bSDaniel Vetter 
126191d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
12620b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_RED(pipe)),
12630b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_GREEN(pipe)),
12640b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_BLUE(pipe)),
12650b5c5ed0SDaniel Vetter 				     res1, res2);
12665b3a856bSDaniel Vetter }
12678bf1e9f1SShuang He 
126844d9241eSVille Syrjälä static void i9xx_pipestat_irq_reset(struct drm_i915_private *dev_priv)
126944d9241eSVille Syrjälä {
127044d9241eSVille Syrjälä 	enum pipe pipe;
127144d9241eSVille Syrjälä 
127244d9241eSVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
127344d9241eSVille Syrjälä 		I915_WRITE(PIPESTAT(pipe),
127444d9241eSVille Syrjälä 			   PIPESTAT_INT_STATUS_MASK |
127544d9241eSVille Syrjälä 			   PIPE_FIFO_UNDERRUN_STATUS);
127644d9241eSVille Syrjälä 
127744d9241eSVille Syrjälä 		dev_priv->pipestat_irq_mask[pipe] = 0;
127844d9241eSVille Syrjälä 	}
127944d9241eSVille Syrjälä }
128044d9241eSVille Syrjälä 
1281eb64343cSVille Syrjälä static void i9xx_pipestat_irq_ack(struct drm_i915_private *dev_priv,
128291d14251STvrtko Ursulin 				  u32 iir, u32 pipe_stats[I915_MAX_PIPES])
12837e231dbeSJesse Barnes {
1284d048a268SVille Syrjälä 	enum pipe pipe;
12857e231dbeSJesse Barnes 
128658ead0d7SImre Deak 	spin_lock(&dev_priv->irq_lock);
12871ca993d2SVille Syrjälä 
12881ca993d2SVille Syrjälä 	if (!dev_priv->display_irqs_enabled) {
12891ca993d2SVille Syrjälä 		spin_unlock(&dev_priv->irq_lock);
12901ca993d2SVille Syrjälä 		return;
12911ca993d2SVille Syrjälä 	}
12921ca993d2SVille Syrjälä 
1293055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1294f0f59a00SVille Syrjälä 		i915_reg_t reg;
12956b12ca56SVille Syrjälä 		u32 status_mask, enable_mask, iir_bit = 0;
129691d181ddSImre Deak 
1297bbb5eebfSDaniel Vetter 		/*
1298bbb5eebfSDaniel Vetter 		 * PIPESTAT bits get signalled even when the interrupt is
1299bbb5eebfSDaniel Vetter 		 * disabled with the mask bits, and some of the status bits do
1300bbb5eebfSDaniel Vetter 		 * not generate interrupts at all (like the underrun bit). Hence
1301bbb5eebfSDaniel Vetter 		 * we need to be careful that we only handle what we want to
1302bbb5eebfSDaniel Vetter 		 * handle.
1303bbb5eebfSDaniel Vetter 		 */
13040f239f4cSDaniel Vetter 
13050f239f4cSDaniel Vetter 		/* fifo underruns are filterered in the underrun handler. */
13066b12ca56SVille Syrjälä 		status_mask = PIPE_FIFO_UNDERRUN_STATUS;
1307bbb5eebfSDaniel Vetter 
1308bbb5eebfSDaniel Vetter 		switch (pipe) {
1309d048a268SVille Syrjälä 		default:
1310bbb5eebfSDaniel Vetter 		case PIPE_A:
1311bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
1312bbb5eebfSDaniel Vetter 			break;
1313bbb5eebfSDaniel Vetter 		case PIPE_B:
1314bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
1315bbb5eebfSDaniel Vetter 			break;
13163278f67fSVille Syrjälä 		case PIPE_C:
13173278f67fSVille Syrjälä 			iir_bit = I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
13183278f67fSVille Syrjälä 			break;
1319bbb5eebfSDaniel Vetter 		}
1320bbb5eebfSDaniel Vetter 		if (iir & iir_bit)
13216b12ca56SVille Syrjälä 			status_mask |= dev_priv->pipestat_irq_mask[pipe];
1322bbb5eebfSDaniel Vetter 
13236b12ca56SVille Syrjälä 		if (!status_mask)
132491d181ddSImre Deak 			continue;
132591d181ddSImre Deak 
132691d181ddSImre Deak 		reg = PIPESTAT(pipe);
13276b12ca56SVille Syrjälä 		pipe_stats[pipe] = I915_READ(reg) & status_mask;
13286b12ca56SVille Syrjälä 		enable_mask = i915_pipestat_enable_mask(dev_priv, pipe);
13297e231dbeSJesse Barnes 
13307e231dbeSJesse Barnes 		/*
13317e231dbeSJesse Barnes 		 * Clear the PIPE*STAT regs before the IIR
1332132c27c9SVille Syrjälä 		 *
1333132c27c9SVille Syrjälä 		 * Toggle the enable bits to make sure we get an
1334132c27c9SVille Syrjälä 		 * edge in the ISR pipe event bit if we don't clear
1335132c27c9SVille Syrjälä 		 * all the enabled status bits. Otherwise the edge
1336132c27c9SVille Syrjälä 		 * triggered IIR on i965/g4x wouldn't notice that
1337132c27c9SVille Syrjälä 		 * an interrupt is still pending.
13387e231dbeSJesse Barnes 		 */
1339132c27c9SVille Syrjälä 		if (pipe_stats[pipe]) {
1340132c27c9SVille Syrjälä 			I915_WRITE(reg, pipe_stats[pipe]);
1341132c27c9SVille Syrjälä 			I915_WRITE(reg, enable_mask);
1342132c27c9SVille Syrjälä 		}
13437e231dbeSJesse Barnes 	}
134458ead0d7SImre Deak 	spin_unlock(&dev_priv->irq_lock);
13452ecb8ca4SVille Syrjälä }
13462ecb8ca4SVille Syrjälä 
1347eb64343cSVille Syrjälä static void i8xx_pipestat_irq_handler(struct drm_i915_private *dev_priv,
1348eb64343cSVille Syrjälä 				      u16 iir, u32 pipe_stats[I915_MAX_PIPES])
1349eb64343cSVille Syrjälä {
1350eb64343cSVille Syrjälä 	enum pipe pipe;
1351eb64343cSVille Syrjälä 
1352eb64343cSVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
1353eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
1354eb64343cSVille Syrjälä 			drm_handle_vblank(&dev_priv->drm, pipe);
1355eb64343cSVille Syrjälä 
1356eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1357eb64343cSVille Syrjälä 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
1358eb64343cSVille Syrjälä 
1359eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
1360eb64343cSVille Syrjälä 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
1361eb64343cSVille Syrjälä 	}
1362eb64343cSVille Syrjälä }
1363eb64343cSVille Syrjälä 
1364eb64343cSVille Syrjälä static void i915_pipestat_irq_handler(struct drm_i915_private *dev_priv,
1365eb64343cSVille Syrjälä 				      u32 iir, u32 pipe_stats[I915_MAX_PIPES])
1366eb64343cSVille Syrjälä {
1367eb64343cSVille Syrjälä 	bool blc_event = false;
1368eb64343cSVille Syrjälä 	enum pipe pipe;
1369eb64343cSVille Syrjälä 
1370eb64343cSVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
1371eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
1372eb64343cSVille Syrjälä 			drm_handle_vblank(&dev_priv->drm, pipe);
1373eb64343cSVille Syrjälä 
1374eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
1375eb64343cSVille Syrjälä 			blc_event = true;
1376eb64343cSVille Syrjälä 
1377eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1378eb64343cSVille Syrjälä 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
1379eb64343cSVille Syrjälä 
1380eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
1381eb64343cSVille Syrjälä 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
1382eb64343cSVille Syrjälä 	}
1383eb64343cSVille Syrjälä 
1384eb64343cSVille Syrjälä 	if (blc_event || (iir & I915_ASLE_INTERRUPT))
1385eb64343cSVille Syrjälä 		intel_opregion_asle_intr(dev_priv);
1386eb64343cSVille Syrjälä }
1387eb64343cSVille Syrjälä 
1388eb64343cSVille Syrjälä static void i965_pipestat_irq_handler(struct drm_i915_private *dev_priv,
1389eb64343cSVille Syrjälä 				      u32 iir, u32 pipe_stats[I915_MAX_PIPES])
1390eb64343cSVille Syrjälä {
1391eb64343cSVille Syrjälä 	bool blc_event = false;
1392eb64343cSVille Syrjälä 	enum pipe pipe;
1393eb64343cSVille Syrjälä 
1394eb64343cSVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
1395eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS)
1396eb64343cSVille Syrjälä 			drm_handle_vblank(&dev_priv->drm, pipe);
1397eb64343cSVille Syrjälä 
1398eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
1399eb64343cSVille Syrjälä 			blc_event = true;
1400eb64343cSVille Syrjälä 
1401eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1402eb64343cSVille Syrjälä 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
1403eb64343cSVille Syrjälä 
1404eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
1405eb64343cSVille Syrjälä 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
1406eb64343cSVille Syrjälä 	}
1407eb64343cSVille Syrjälä 
1408eb64343cSVille Syrjälä 	if (blc_event || (iir & I915_ASLE_INTERRUPT))
1409eb64343cSVille Syrjälä 		intel_opregion_asle_intr(dev_priv);
1410eb64343cSVille Syrjälä 
1411eb64343cSVille Syrjälä 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1412eb64343cSVille Syrjälä 		gmbus_irq_handler(dev_priv);
1413eb64343cSVille Syrjälä }
1414eb64343cSVille Syrjälä 
141591d14251STvrtko Ursulin static void valleyview_pipestat_irq_handler(struct drm_i915_private *dev_priv,
14162ecb8ca4SVille Syrjälä 					    u32 pipe_stats[I915_MAX_PIPES])
14172ecb8ca4SVille Syrjälä {
14182ecb8ca4SVille Syrjälä 	enum pipe pipe;
14197e231dbeSJesse Barnes 
1420055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1421fd3a4024SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS)
1422fd3a4024SDaniel Vetter 			drm_handle_vblank(&dev_priv->drm, pipe);
14234356d586SDaniel Vetter 
14244356d586SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
142591d14251STvrtko Ursulin 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
14262d9d2b0bSVille Syrjälä 
14271f7247c0SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
14281f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
142931acc7f5SJesse Barnes 	}
143031acc7f5SJesse Barnes 
1431c1874ed7SImre Deak 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
143291d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
1433c1874ed7SImre Deak }
1434c1874ed7SImre Deak 
14351ae3c34cSVille Syrjälä static u32 i9xx_hpd_irq_ack(struct drm_i915_private *dev_priv)
143616c6c56bSVille Syrjälä {
14370ba7c51aSVille Syrjälä 	u32 hotplug_status = 0, hotplug_status_mask;
14380ba7c51aSVille Syrjälä 	int i;
143916c6c56bSVille Syrjälä 
14400ba7c51aSVille Syrjälä 	if (IS_G4X(dev_priv) ||
14410ba7c51aSVille Syrjälä 	    IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
14420ba7c51aSVille Syrjälä 		hotplug_status_mask = HOTPLUG_INT_STATUS_G4X |
14430ba7c51aSVille Syrjälä 			DP_AUX_CHANNEL_MASK_INT_STATUS_G4X;
14440ba7c51aSVille Syrjälä 	else
14450ba7c51aSVille Syrjälä 		hotplug_status_mask = HOTPLUG_INT_STATUS_I915;
14460ba7c51aSVille Syrjälä 
14470ba7c51aSVille Syrjälä 	/*
14480ba7c51aSVille Syrjälä 	 * We absolutely have to clear all the pending interrupt
14490ba7c51aSVille Syrjälä 	 * bits in PORT_HOTPLUG_STAT. Otherwise the ISR port
14500ba7c51aSVille Syrjälä 	 * interrupt bit won't have an edge, and the i965/g4x
14510ba7c51aSVille Syrjälä 	 * edge triggered IIR will not notice that an interrupt
14520ba7c51aSVille Syrjälä 	 * is still pending. We can't use PORT_HOTPLUG_EN to
14530ba7c51aSVille Syrjälä 	 * guarantee the edge as the act of toggling the enable
14540ba7c51aSVille Syrjälä 	 * bits can itself generate a new hotplug interrupt :(
14550ba7c51aSVille Syrjälä 	 */
14560ba7c51aSVille Syrjälä 	for (i = 0; i < 10; i++) {
14570ba7c51aSVille Syrjälä 		u32 tmp = I915_READ(PORT_HOTPLUG_STAT) & hotplug_status_mask;
14580ba7c51aSVille Syrjälä 
14590ba7c51aSVille Syrjälä 		if (tmp == 0)
14600ba7c51aSVille Syrjälä 			return hotplug_status;
14610ba7c51aSVille Syrjälä 
14620ba7c51aSVille Syrjälä 		hotplug_status |= tmp;
14633ff60f89SOscar Mateo 		I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
14640ba7c51aSVille Syrjälä 	}
14650ba7c51aSVille Syrjälä 
14660ba7c51aSVille Syrjälä 	WARN_ONCE(1,
14670ba7c51aSVille Syrjälä 		  "PORT_HOTPLUG_STAT did not clear (0x%08x)\n",
14680ba7c51aSVille Syrjälä 		  I915_READ(PORT_HOTPLUG_STAT));
14691ae3c34cSVille Syrjälä 
14701ae3c34cSVille Syrjälä 	return hotplug_status;
14711ae3c34cSVille Syrjälä }
14721ae3c34cSVille Syrjälä 
147391d14251STvrtko Ursulin static void i9xx_hpd_irq_handler(struct drm_i915_private *dev_priv,
14741ae3c34cSVille Syrjälä 				 u32 hotplug_status)
14751ae3c34cSVille Syrjälä {
14761ae3c34cSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
14773ff60f89SOscar Mateo 
147891d14251STvrtko Ursulin 	if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
147991d14251STvrtko Ursulin 	    IS_CHERRYVIEW(dev_priv)) {
148016c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
148116c6c56bSVille Syrjälä 
148258f2cf24SVille Syrjälä 		if (hotplug_trigger) {
1483cf53902fSRodrigo Vivi 			intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
1484cf53902fSRodrigo Vivi 					   hotplug_trigger, hotplug_trigger,
1485cf53902fSRodrigo Vivi 					   hpd_status_g4x,
1486fd63e2a9SImre Deak 					   i9xx_port_hotplug_long_detect);
148758f2cf24SVille Syrjälä 
148891d14251STvrtko Ursulin 			intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
148958f2cf24SVille Syrjälä 		}
1490369712e8SJani Nikula 
1491369712e8SJani Nikula 		if (hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
149291d14251STvrtko Ursulin 			dp_aux_irq_handler(dev_priv);
149316c6c56bSVille Syrjälä 	} else {
149416c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
149516c6c56bSVille Syrjälä 
149658f2cf24SVille Syrjälä 		if (hotplug_trigger) {
1497cf53902fSRodrigo Vivi 			intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
1498cf53902fSRodrigo Vivi 					   hotplug_trigger, hotplug_trigger,
1499cf53902fSRodrigo Vivi 					   hpd_status_i915,
1500fd63e2a9SImre Deak 					   i9xx_port_hotplug_long_detect);
150191d14251STvrtko Ursulin 			intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
150216c6c56bSVille Syrjälä 		}
15033ff60f89SOscar Mateo 	}
150458f2cf24SVille Syrjälä }
150516c6c56bSVille Syrjälä 
1506c1874ed7SImre Deak static irqreturn_t valleyview_irq_handler(int irq, void *arg)
1507c1874ed7SImre Deak {
1508b318b824SVille Syrjälä 	struct drm_i915_private *dev_priv = arg;
1509c1874ed7SImre Deak 	irqreturn_t ret = IRQ_NONE;
1510c1874ed7SImre Deak 
15112dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
15122dd2a883SImre Deak 		return IRQ_NONE;
15132dd2a883SImre Deak 
15141f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
15159102650fSDaniele Ceraolo Spurio 	disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
15161f814dacSImre Deak 
15171e1cace9SVille Syrjälä 	do {
15186e814800SVille Syrjälä 		u32 iir, gt_iir, pm_iir;
15192ecb8ca4SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
15201ae3c34cSVille Syrjälä 		u32 hotplug_status = 0;
1521a5e485a9SVille Syrjälä 		u32 ier = 0;
15223ff60f89SOscar Mateo 
1523c1874ed7SImre Deak 		gt_iir = I915_READ(GTIIR);
1524c1874ed7SImre Deak 		pm_iir = I915_READ(GEN6_PMIIR);
15253ff60f89SOscar Mateo 		iir = I915_READ(VLV_IIR);
1526c1874ed7SImre Deak 
1527c1874ed7SImre Deak 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
15281e1cace9SVille Syrjälä 			break;
1529c1874ed7SImre Deak 
1530c1874ed7SImre Deak 		ret = IRQ_HANDLED;
1531c1874ed7SImre Deak 
1532a5e485a9SVille Syrjälä 		/*
1533a5e485a9SVille Syrjälä 		 * Theory on interrupt generation, based on empirical evidence:
1534a5e485a9SVille Syrjälä 		 *
1535a5e485a9SVille Syrjälä 		 * x = ((VLV_IIR & VLV_IER) ||
1536a5e485a9SVille Syrjälä 		 *      (((GT_IIR & GT_IER) || (GEN6_PMIIR & GEN6_PMIER)) &&
1537a5e485a9SVille Syrjälä 		 *       (VLV_MASTER_IER & MASTER_INTERRUPT_ENABLE)));
1538a5e485a9SVille Syrjälä 		 *
1539a5e485a9SVille Syrjälä 		 * A CPU interrupt will only be raised when 'x' has a 0->1 edge.
1540a5e485a9SVille Syrjälä 		 * Hence we clear MASTER_INTERRUPT_ENABLE and VLV_IER to
1541a5e485a9SVille Syrjälä 		 * guarantee the CPU interrupt will be raised again even if we
1542a5e485a9SVille Syrjälä 		 * don't end up clearing all the VLV_IIR, GT_IIR, GEN6_PMIIR
1543a5e485a9SVille Syrjälä 		 * bits this time around.
1544a5e485a9SVille Syrjälä 		 */
15454a0a0202SVille Syrjälä 		I915_WRITE(VLV_MASTER_IER, 0);
1546a5e485a9SVille Syrjälä 		ier = I915_READ(VLV_IER);
1547a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, 0);
15484a0a0202SVille Syrjälä 
15494a0a0202SVille Syrjälä 		if (gt_iir)
15504a0a0202SVille Syrjälä 			I915_WRITE(GTIIR, gt_iir);
15514a0a0202SVille Syrjälä 		if (pm_iir)
15524a0a0202SVille Syrjälä 			I915_WRITE(GEN6_PMIIR, pm_iir);
15534a0a0202SVille Syrjälä 
15547ce4d1f2SVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
15551ae3c34cSVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
15567ce4d1f2SVille Syrjälä 
15573ff60f89SOscar Mateo 		/* Call regardless, as some status bits might not be
15583ff60f89SOscar Mateo 		 * signalled in iir */
1559eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
15607ce4d1f2SVille Syrjälä 
1561eef57324SJerome Anand 		if (iir & (I915_LPE_PIPE_A_INTERRUPT |
1562eef57324SJerome Anand 			   I915_LPE_PIPE_B_INTERRUPT))
1563eef57324SJerome Anand 			intel_lpe_audio_irq_handler(dev_priv);
1564eef57324SJerome Anand 
15657ce4d1f2SVille Syrjälä 		/*
15667ce4d1f2SVille Syrjälä 		 * VLV_IIR is single buffered, and reflects the level
15677ce4d1f2SVille Syrjälä 		 * from PIPESTAT/PORT_HOTPLUG_STAT, hence clear it last.
15687ce4d1f2SVille Syrjälä 		 */
15697ce4d1f2SVille Syrjälä 		if (iir)
15707ce4d1f2SVille Syrjälä 			I915_WRITE(VLV_IIR, iir);
15714a0a0202SVille Syrjälä 
1572a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, ier);
15734a0a0202SVille Syrjälä 		I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
15741ae3c34cSVille Syrjälä 
157552894874SVille Syrjälä 		if (gt_iir)
1576cf1c97dcSAndi Shyti 			gen6_gt_irq_handler(&dev_priv->gt, gt_iir);
157752894874SVille Syrjälä 		if (pm_iir)
15783e7abf81SAndi Shyti 			gen6_rps_irq_handler(&dev_priv->gt.rps, pm_iir);
157952894874SVille Syrjälä 
15801ae3c34cSVille Syrjälä 		if (hotplug_status)
158191d14251STvrtko Ursulin 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
15822ecb8ca4SVille Syrjälä 
158391d14251STvrtko Ursulin 		valleyview_pipestat_irq_handler(dev_priv, pipe_stats);
15841e1cace9SVille Syrjälä 	} while (0);
15857e231dbeSJesse Barnes 
15869102650fSDaniele Ceraolo Spurio 	enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
15871f814dacSImre Deak 
15887e231dbeSJesse Barnes 	return ret;
15897e231dbeSJesse Barnes }
15907e231dbeSJesse Barnes 
159143f328d7SVille Syrjälä static irqreturn_t cherryview_irq_handler(int irq, void *arg)
159243f328d7SVille Syrjälä {
1593b318b824SVille Syrjälä 	struct drm_i915_private *dev_priv = arg;
159443f328d7SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
159543f328d7SVille Syrjälä 
15962dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
15972dd2a883SImre Deak 		return IRQ_NONE;
15982dd2a883SImre Deak 
15991f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
16009102650fSDaniele Ceraolo Spurio 	disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
16011f814dacSImre Deak 
1602579de73bSChris Wilson 	do {
16036e814800SVille Syrjälä 		u32 master_ctl, iir;
16042ecb8ca4SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
16051ae3c34cSVille Syrjälä 		u32 hotplug_status = 0;
1606f0fd96f5SChris Wilson 		u32 gt_iir[4];
1607a5e485a9SVille Syrjälä 		u32 ier = 0;
1608a5e485a9SVille Syrjälä 
16098e5fd599SVille Syrjälä 		master_ctl = I915_READ(GEN8_MASTER_IRQ) & ~GEN8_MASTER_IRQ_CONTROL;
16103278f67fSVille Syrjälä 		iir = I915_READ(VLV_IIR);
16113278f67fSVille Syrjälä 
16123278f67fSVille Syrjälä 		if (master_ctl == 0 && iir == 0)
16138e5fd599SVille Syrjälä 			break;
161443f328d7SVille Syrjälä 
161527b6c122SOscar Mateo 		ret = IRQ_HANDLED;
161627b6c122SOscar Mateo 
1617a5e485a9SVille Syrjälä 		/*
1618a5e485a9SVille Syrjälä 		 * Theory on interrupt generation, based on empirical evidence:
1619a5e485a9SVille Syrjälä 		 *
1620a5e485a9SVille Syrjälä 		 * x = ((VLV_IIR & VLV_IER) ||
1621a5e485a9SVille Syrjälä 		 *      ((GEN8_MASTER_IRQ & ~GEN8_MASTER_IRQ_CONTROL) &&
1622a5e485a9SVille Syrjälä 		 *       (GEN8_MASTER_IRQ & GEN8_MASTER_IRQ_CONTROL)));
1623a5e485a9SVille Syrjälä 		 *
1624a5e485a9SVille Syrjälä 		 * A CPU interrupt will only be raised when 'x' has a 0->1 edge.
1625a5e485a9SVille Syrjälä 		 * Hence we clear GEN8_MASTER_IRQ_CONTROL and VLV_IER to
1626a5e485a9SVille Syrjälä 		 * guarantee the CPU interrupt will be raised again even if we
1627a5e485a9SVille Syrjälä 		 * don't end up clearing all the VLV_IIR and GEN8_MASTER_IRQ_CONTROL
1628a5e485a9SVille Syrjälä 		 * bits this time around.
1629a5e485a9SVille Syrjälä 		 */
163043f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, 0);
1631a5e485a9SVille Syrjälä 		ier = I915_READ(VLV_IER);
1632a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, 0);
163343f328d7SVille Syrjälä 
1634cf1c97dcSAndi Shyti 		gen8_gt_irq_ack(&dev_priv->gt, master_ctl, gt_iir);
163527b6c122SOscar Mateo 
163627b6c122SOscar Mateo 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
16371ae3c34cSVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
163843f328d7SVille Syrjälä 
163927b6c122SOscar Mateo 		/* Call regardless, as some status bits might not be
164027b6c122SOscar Mateo 		 * signalled in iir */
1641eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
164243f328d7SVille Syrjälä 
1643eef57324SJerome Anand 		if (iir & (I915_LPE_PIPE_A_INTERRUPT |
1644eef57324SJerome Anand 			   I915_LPE_PIPE_B_INTERRUPT |
1645eef57324SJerome Anand 			   I915_LPE_PIPE_C_INTERRUPT))
1646eef57324SJerome Anand 			intel_lpe_audio_irq_handler(dev_priv);
1647eef57324SJerome Anand 
16487ce4d1f2SVille Syrjälä 		/*
16497ce4d1f2SVille Syrjälä 		 * VLV_IIR is single buffered, and reflects the level
16507ce4d1f2SVille Syrjälä 		 * from PIPESTAT/PORT_HOTPLUG_STAT, hence clear it last.
16517ce4d1f2SVille Syrjälä 		 */
16527ce4d1f2SVille Syrjälä 		if (iir)
16537ce4d1f2SVille Syrjälä 			I915_WRITE(VLV_IIR, iir);
16547ce4d1f2SVille Syrjälä 
1655a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, ier);
1656e5328c43SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
16571ae3c34cSVille Syrjälä 
1658cf1c97dcSAndi Shyti 		gen8_gt_irq_handler(&dev_priv->gt, master_ctl, gt_iir);
1659e30e251aSVille Syrjälä 
16601ae3c34cSVille Syrjälä 		if (hotplug_status)
166191d14251STvrtko Ursulin 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
16622ecb8ca4SVille Syrjälä 
166391d14251STvrtko Ursulin 		valleyview_pipestat_irq_handler(dev_priv, pipe_stats);
1664579de73bSChris Wilson 	} while (0);
16653278f67fSVille Syrjälä 
16669102650fSDaniele Ceraolo Spurio 	enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
16671f814dacSImre Deak 
166843f328d7SVille Syrjälä 	return ret;
166943f328d7SVille Syrjälä }
167043f328d7SVille Syrjälä 
167191d14251STvrtko Ursulin static void ibx_hpd_irq_handler(struct drm_i915_private *dev_priv,
167291d14251STvrtko Ursulin 				u32 hotplug_trigger,
167340e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
1674776ad806SJesse Barnes {
167542db67d6SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
1676776ad806SJesse Barnes 
16776a39d7c9SJani Nikula 	/*
16786a39d7c9SJani Nikula 	 * Somehow the PCH doesn't seem to really ack the interrupt to the CPU
16796a39d7c9SJani Nikula 	 * unless we touch the hotplug register, even if hotplug_trigger is
16806a39d7c9SJani Nikula 	 * zero. Not acking leads to "The master control interrupt lied (SDE)!"
16816a39d7c9SJani Nikula 	 * errors.
16826a39d7c9SJani Nikula 	 */
168313cf5504SDave Airlie 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
16846a39d7c9SJani Nikula 	if (!hotplug_trigger) {
16856a39d7c9SJani Nikula 		u32 mask = PORTA_HOTPLUG_STATUS_MASK |
16866a39d7c9SJani Nikula 			PORTD_HOTPLUG_STATUS_MASK |
16876a39d7c9SJani Nikula 			PORTC_HOTPLUG_STATUS_MASK |
16886a39d7c9SJani Nikula 			PORTB_HOTPLUG_STATUS_MASK;
16896a39d7c9SJani Nikula 		dig_hotplug_reg &= ~mask;
16906a39d7c9SJani Nikula 	}
16916a39d7c9SJani Nikula 
169213cf5504SDave Airlie 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
16936a39d7c9SJani Nikula 	if (!hotplug_trigger)
16946a39d7c9SJani Nikula 		return;
169513cf5504SDave Airlie 
1696cf53902fSRodrigo Vivi 	intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask, hotplug_trigger,
169740e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
1698fd63e2a9SImre Deak 			   pch_port_hotplug_long_detect);
169940e56410SVille Syrjälä 
170091d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
1701aaf5ec2eSSonika Jindal }
170291d131d2SDaniel Vetter 
170391d14251STvrtko Ursulin static void ibx_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
170440e56410SVille Syrjälä {
1705d048a268SVille Syrjälä 	enum pipe pipe;
170640e56410SVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
170740e56410SVille Syrjälä 
170891d14251STvrtko Ursulin 	ibx_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ibx);
170940e56410SVille Syrjälä 
1710cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1711cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1712776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1713cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1714cfc33bf7SVille Syrjälä 				 port_name(port));
1715cfc33bf7SVille Syrjälä 	}
1716776ad806SJesse Barnes 
1717ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
171891d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
1719ce99c256SDaniel Vetter 
1720776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
172191d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
1722776ad806SJesse Barnes 
1723776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1724776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1725776ad806SJesse Barnes 
1726776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1727776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1728776ad806SJesse Barnes 
1729776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1730776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
1731776ad806SJesse Barnes 
17329db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
1733055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
17349db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
17359db4a9c7SJesse Barnes 					 pipe_name(pipe),
17369db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
1737776ad806SJesse Barnes 
1738776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1739776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1740776ad806SJesse Barnes 
1741776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1742776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1743776ad806SJesse Barnes 
1744776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
1745a2196033SMatthias Kaehlcke 		intel_pch_fifo_underrun_irq_handler(dev_priv, PIPE_A);
17468664281bSPaulo Zanoni 
17478664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
1748a2196033SMatthias Kaehlcke 		intel_pch_fifo_underrun_irq_handler(dev_priv, PIPE_B);
17498664281bSPaulo Zanoni }
17508664281bSPaulo Zanoni 
175191d14251STvrtko Ursulin static void ivb_err_int_handler(struct drm_i915_private *dev_priv)
17528664281bSPaulo Zanoni {
17538664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
17545a69b89fSDaniel Vetter 	enum pipe pipe;
17558664281bSPaulo Zanoni 
1756de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
1757de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1758de032bf4SPaulo Zanoni 
1759055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
17601f7247c0SDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe))
17611f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
17628664281bSPaulo Zanoni 
17635a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
176491d14251STvrtko Ursulin 			if (IS_IVYBRIDGE(dev_priv))
176591d14251STvrtko Ursulin 				ivb_pipe_crc_irq_handler(dev_priv, pipe);
17665a69b89fSDaniel Vetter 			else
176791d14251STvrtko Ursulin 				hsw_pipe_crc_irq_handler(dev_priv, pipe);
17685a69b89fSDaniel Vetter 		}
17695a69b89fSDaniel Vetter 	}
17708bf1e9f1SShuang He 
17718664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
17728664281bSPaulo Zanoni }
17738664281bSPaulo Zanoni 
177491d14251STvrtko Ursulin static void cpt_serr_int_handler(struct drm_i915_private *dev_priv)
17758664281bSPaulo Zanoni {
17768664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
177745c1cd87SMika Kahola 	enum pipe pipe;
17788664281bSPaulo Zanoni 
1779de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
1780de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
1781de032bf4SPaulo Zanoni 
178245c1cd87SMika Kahola 	for_each_pipe(dev_priv, pipe)
178345c1cd87SMika Kahola 		if (serr_int & SERR_INT_TRANS_FIFO_UNDERRUN(pipe))
178445c1cd87SMika Kahola 			intel_pch_fifo_underrun_irq_handler(dev_priv, pipe);
17858664281bSPaulo Zanoni 
17868664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
1787776ad806SJesse Barnes }
1788776ad806SJesse Barnes 
178991d14251STvrtko Ursulin static void cpt_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
179023e81d69SAdam Jackson {
1791d048a268SVille Syrjälä 	enum pipe pipe;
17926dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
1793aaf5ec2eSSonika Jindal 
179491d14251STvrtko Ursulin 	ibx_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_cpt);
179591d131d2SDaniel Vetter 
1796cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
1797cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
179823e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
1799cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
1800cfc33bf7SVille Syrjälä 				 port_name(port));
1801cfc33bf7SVille Syrjälä 	}
180223e81d69SAdam Jackson 
180323e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
180491d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
180523e81d69SAdam Jackson 
180623e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
180791d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
180823e81d69SAdam Jackson 
180923e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
181023e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
181123e81d69SAdam Jackson 
181223e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
181323e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
181423e81d69SAdam Jackson 
181523e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
1816055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
181723e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
181823e81d69SAdam Jackson 					 pipe_name(pipe),
181923e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
18208664281bSPaulo Zanoni 
18218664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
182291d14251STvrtko Ursulin 		cpt_serr_int_handler(dev_priv);
182323e81d69SAdam Jackson }
182423e81d69SAdam Jackson 
182558676af6SLucas De Marchi static void icp_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
182631604222SAnusha Srivatsa {
182758676af6SLucas De Marchi 	u32 ddi_hotplug_trigger, tc_hotplug_trigger;
182831604222SAnusha Srivatsa 	u32 pin_mask = 0, long_mask = 0;
182958676af6SLucas De Marchi 	bool (*tc_port_hotplug_long_detect)(enum hpd_pin pin, u32 val);
183058676af6SLucas De Marchi 	const u32 *pins;
183131604222SAnusha Srivatsa 
183258676af6SLucas De Marchi 	if (HAS_PCH_TGP(dev_priv)) {
183358676af6SLucas De Marchi 		ddi_hotplug_trigger = pch_iir & SDE_DDI_MASK_TGP;
183458676af6SLucas De Marchi 		tc_hotplug_trigger = pch_iir & SDE_TC_MASK_TGP;
183558676af6SLucas De Marchi 		tc_port_hotplug_long_detect = tgp_tc_port_hotplug_long_detect;
183658676af6SLucas De Marchi 		pins = hpd_tgp;
1837943682e3SMatt Roper 	} else if (HAS_PCH_JSP(dev_priv)) {
1838943682e3SMatt Roper 		ddi_hotplug_trigger = pch_iir & SDE_DDI_MASK_TGP;
1839943682e3SMatt Roper 		tc_hotplug_trigger = 0;
1840943682e3SMatt Roper 		pins = hpd_tgp;
184158676af6SLucas De Marchi 	} else if (HAS_PCH_MCC(dev_priv)) {
184253448aedSVivek Kasireddy 		ddi_hotplug_trigger = pch_iir & SDE_DDI_MASK_ICP;
184353448aedSVivek Kasireddy 		tc_hotplug_trigger = pch_iir & SDE_TC_HOTPLUG_ICP(PORT_TC1);
1844fcb9bba4SMatt Roper 		tc_port_hotplug_long_detect = icp_tc_port_hotplug_long_detect;
1845d09ad3e7SMatt Roper 		pins = hpd_icp;
18468ef7e340SMatt Roper 	} else {
1847943682e3SMatt Roper 		WARN(!HAS_PCH_ICP(dev_priv),
1848943682e3SMatt Roper 		     "Unrecognized PCH type 0x%x\n", INTEL_PCH_TYPE(dev_priv));
1849943682e3SMatt Roper 
18508ef7e340SMatt Roper 		ddi_hotplug_trigger = pch_iir & SDE_DDI_MASK_ICP;
18518ef7e340SMatt Roper 		tc_hotplug_trigger = pch_iir & SDE_TC_MASK_ICP;
185258676af6SLucas De Marchi 		tc_port_hotplug_long_detect = icp_tc_port_hotplug_long_detect;
185358676af6SLucas De Marchi 		pins = hpd_icp;
18548ef7e340SMatt Roper 	}
18558ef7e340SMatt Roper 
185631604222SAnusha Srivatsa 	if (ddi_hotplug_trigger) {
185731604222SAnusha Srivatsa 		u32 dig_hotplug_reg;
185831604222SAnusha Srivatsa 
185931604222SAnusha Srivatsa 		dig_hotplug_reg = I915_READ(SHOTPLUG_CTL_DDI);
186031604222SAnusha Srivatsa 		I915_WRITE(SHOTPLUG_CTL_DDI, dig_hotplug_reg);
186131604222SAnusha Srivatsa 
186231604222SAnusha Srivatsa 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
186331604222SAnusha Srivatsa 				   ddi_hotplug_trigger,
1864c6f7acb8SMatt Roper 				   dig_hotplug_reg, pins,
186531604222SAnusha Srivatsa 				   icp_ddi_port_hotplug_long_detect);
186631604222SAnusha Srivatsa 	}
186731604222SAnusha Srivatsa 
186831604222SAnusha Srivatsa 	if (tc_hotplug_trigger) {
186931604222SAnusha Srivatsa 		u32 dig_hotplug_reg;
187031604222SAnusha Srivatsa 
187131604222SAnusha Srivatsa 		dig_hotplug_reg = I915_READ(SHOTPLUG_CTL_TC);
187231604222SAnusha Srivatsa 		I915_WRITE(SHOTPLUG_CTL_TC, dig_hotplug_reg);
187331604222SAnusha Srivatsa 
187431604222SAnusha Srivatsa 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
187531604222SAnusha Srivatsa 				   tc_hotplug_trigger,
1876c6f7acb8SMatt Roper 				   dig_hotplug_reg, pins,
187758676af6SLucas De Marchi 				   tc_port_hotplug_long_detect);
187852dfdba0SLucas De Marchi 	}
187952dfdba0SLucas De Marchi 
188052dfdba0SLucas De Marchi 	if (pin_mask)
188152dfdba0SLucas De Marchi 		intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
188252dfdba0SLucas De Marchi 
188352dfdba0SLucas De Marchi 	if (pch_iir & SDE_GMBUS_ICP)
188452dfdba0SLucas De Marchi 		gmbus_irq_handler(dev_priv);
188552dfdba0SLucas De Marchi }
188652dfdba0SLucas De Marchi 
188791d14251STvrtko Ursulin static void spt_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
18886dbf30ceSVille Syrjälä {
18896dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_SPT &
18906dbf30ceSVille Syrjälä 		~SDE_PORTE_HOTPLUG_SPT;
18916dbf30ceSVille Syrjälä 	u32 hotplug2_trigger = pch_iir & SDE_PORTE_HOTPLUG_SPT;
18926dbf30ceSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
18936dbf30ceSVille Syrjälä 
18946dbf30ceSVille Syrjälä 	if (hotplug_trigger) {
18956dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
18966dbf30ceSVille Syrjälä 
18976dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
18986dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
18996dbf30ceSVille Syrjälä 
1900cf53902fSRodrigo Vivi 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
1901cf53902fSRodrigo Vivi 				   hotplug_trigger, dig_hotplug_reg, hpd_spt,
190274c0b395SVille Syrjälä 				   spt_port_hotplug_long_detect);
19036dbf30ceSVille Syrjälä 	}
19046dbf30ceSVille Syrjälä 
19056dbf30ceSVille Syrjälä 	if (hotplug2_trigger) {
19066dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
19076dbf30ceSVille Syrjälä 
19086dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG2);
19096dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG2, dig_hotplug_reg);
19106dbf30ceSVille Syrjälä 
1911cf53902fSRodrigo Vivi 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
1912cf53902fSRodrigo Vivi 				   hotplug2_trigger, dig_hotplug_reg, hpd_spt,
19136dbf30ceSVille Syrjälä 				   spt_port_hotplug2_long_detect);
19146dbf30ceSVille Syrjälä 	}
19156dbf30ceSVille Syrjälä 
19166dbf30ceSVille Syrjälä 	if (pin_mask)
191791d14251STvrtko Ursulin 		intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
19186dbf30ceSVille Syrjälä 
19196dbf30ceSVille Syrjälä 	if (pch_iir & SDE_GMBUS_CPT)
192091d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
19216dbf30ceSVille Syrjälä }
19226dbf30ceSVille Syrjälä 
192391d14251STvrtko Ursulin static void ilk_hpd_irq_handler(struct drm_i915_private *dev_priv,
192491d14251STvrtko Ursulin 				u32 hotplug_trigger,
192540e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
1926c008bc6eSPaulo Zanoni {
1927e4ce95aaSVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
1928e4ce95aaSVille Syrjälä 
1929e4ce95aaSVille Syrjälä 	dig_hotplug_reg = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
1930e4ce95aaSVille Syrjälä 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, dig_hotplug_reg);
1931e4ce95aaSVille Syrjälä 
1932cf53902fSRodrigo Vivi 	intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask, hotplug_trigger,
193340e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
1934e4ce95aaSVille Syrjälä 			   ilk_port_hotplug_long_detect);
193540e56410SVille Syrjälä 
193691d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
1937e4ce95aaSVille Syrjälä }
1938c008bc6eSPaulo Zanoni 
193991d14251STvrtko Ursulin static void ilk_display_irq_handler(struct drm_i915_private *dev_priv,
194091d14251STvrtko Ursulin 				    u32 de_iir)
194140e56410SVille Syrjälä {
194240e56410SVille Syrjälä 	enum pipe pipe;
194340e56410SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG;
194440e56410SVille Syrjälä 
194540e56410SVille Syrjälä 	if (hotplug_trigger)
194691d14251STvrtko Ursulin 		ilk_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ilk);
194740e56410SVille Syrjälä 
1948c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
194991d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
1950c008bc6eSPaulo Zanoni 
1951c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
195291d14251STvrtko Ursulin 		intel_opregion_asle_intr(dev_priv);
1953c008bc6eSPaulo Zanoni 
1954c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
1955c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1956c008bc6eSPaulo Zanoni 
1957055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1958fd3a4024SDaniel Vetter 		if (de_iir & DE_PIPE_VBLANK(pipe))
1959fd3a4024SDaniel Vetter 			drm_handle_vblank(&dev_priv->drm, pipe);
1960c008bc6eSPaulo Zanoni 
196140da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
19621f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
1963c008bc6eSPaulo Zanoni 
196440da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
196591d14251STvrtko Ursulin 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
1966c008bc6eSPaulo Zanoni 	}
1967c008bc6eSPaulo Zanoni 
1968c008bc6eSPaulo Zanoni 	/* check event from PCH */
1969c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
1970c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
1971c008bc6eSPaulo Zanoni 
197291d14251STvrtko Ursulin 		if (HAS_PCH_CPT(dev_priv))
197391d14251STvrtko Ursulin 			cpt_irq_handler(dev_priv, pch_iir);
1974c008bc6eSPaulo Zanoni 		else
197591d14251STvrtko Ursulin 			ibx_irq_handler(dev_priv, pch_iir);
1976c008bc6eSPaulo Zanoni 
1977c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
1978c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
1979c008bc6eSPaulo Zanoni 	}
1980c008bc6eSPaulo Zanoni 
1981cf819effSLucas De Marchi 	if (IS_GEN(dev_priv, 5) && de_iir & DE_PCU_EVENT)
19823e7abf81SAndi Shyti 		gen5_rps_irq_handler(&dev_priv->gt.rps);
1983c008bc6eSPaulo Zanoni }
1984c008bc6eSPaulo Zanoni 
198591d14251STvrtko Ursulin static void ivb_display_irq_handler(struct drm_i915_private *dev_priv,
198691d14251STvrtko Ursulin 				    u32 de_iir)
19879719fb98SPaulo Zanoni {
198807d27e20SDamien Lespiau 	enum pipe pipe;
198923bb4cb5SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG_IVB;
199023bb4cb5SVille Syrjälä 
199140e56410SVille Syrjälä 	if (hotplug_trigger)
199291d14251STvrtko Ursulin 		ilk_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ivb);
19939719fb98SPaulo Zanoni 
19949719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
199591d14251STvrtko Ursulin 		ivb_err_int_handler(dev_priv);
19969719fb98SPaulo Zanoni 
199754fd3149SDhinakaran Pandiyan 	if (de_iir & DE_EDP_PSR_INT_HSW) {
199854fd3149SDhinakaran Pandiyan 		u32 psr_iir = I915_READ(EDP_PSR_IIR);
199954fd3149SDhinakaran Pandiyan 
200054fd3149SDhinakaran Pandiyan 		intel_psr_irq_handler(dev_priv, psr_iir);
200154fd3149SDhinakaran Pandiyan 		I915_WRITE(EDP_PSR_IIR, psr_iir);
200254fd3149SDhinakaran Pandiyan 	}
2003fc340442SDaniel Vetter 
20049719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
200591d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
20069719fb98SPaulo Zanoni 
20079719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
200891d14251STvrtko Ursulin 		intel_opregion_asle_intr(dev_priv);
20099719fb98SPaulo Zanoni 
2010055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2011fd3a4024SDaniel Vetter 		if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)))
2012fd3a4024SDaniel Vetter 			drm_handle_vblank(&dev_priv->drm, pipe);
20139719fb98SPaulo Zanoni 	}
20149719fb98SPaulo Zanoni 
20159719fb98SPaulo Zanoni 	/* check event from PCH */
201691d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv) && (de_iir & DE_PCH_EVENT_IVB)) {
20179719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
20189719fb98SPaulo Zanoni 
201991d14251STvrtko Ursulin 		cpt_irq_handler(dev_priv, pch_iir);
20209719fb98SPaulo Zanoni 
20219719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
20229719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
20239719fb98SPaulo Zanoni 	}
20249719fb98SPaulo Zanoni }
20259719fb98SPaulo Zanoni 
202672c90f62SOscar Mateo /*
202772c90f62SOscar Mateo  * To handle irqs with the minimum potential races with fresh interrupts, we:
202872c90f62SOscar Mateo  * 1 - Disable Master Interrupt Control.
202972c90f62SOscar Mateo  * 2 - Find the source(s) of the interrupt.
203072c90f62SOscar Mateo  * 3 - Clear the Interrupt Identity bits (IIR).
203172c90f62SOscar Mateo  * 4 - Process the interrupt(s) that had bits set in the IIRs.
203272c90f62SOscar Mateo  * 5 - Re-enable Master Interrupt Control.
203372c90f62SOscar Mateo  */
20349eae5e27SLucas De Marchi static irqreturn_t ilk_irq_handler(int irq, void *arg)
2035b1f14ad0SJesse Barnes {
2036b318b824SVille Syrjälä 	struct drm_i915_private *dev_priv = arg;
2037f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
20380e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
2039b1f14ad0SJesse Barnes 
20402dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
20412dd2a883SImre Deak 		return IRQ_NONE;
20422dd2a883SImre Deak 
20431f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
20449102650fSDaniele Ceraolo Spurio 	disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
20451f814dacSImre Deak 
2046b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
2047b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
2048b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
20490e43406bSChris Wilson 
205044498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
205144498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
205244498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
205344498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
205444498aeaSPaulo Zanoni 	 * due to its back queue). */
205591d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv)) {
205644498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
205744498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
2058ab5c608bSBen Widawsky 	}
205944498aeaSPaulo Zanoni 
206072c90f62SOscar Mateo 	/* Find, clear, then process each source of interrupt */
206172c90f62SOscar Mateo 
20620e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
20630e43406bSChris Wilson 	if (gt_iir) {
206472c90f62SOscar Mateo 		I915_WRITE(GTIIR, gt_iir);
206572c90f62SOscar Mateo 		ret = IRQ_HANDLED;
206691d14251STvrtko Ursulin 		if (INTEL_GEN(dev_priv) >= 6)
2067cf1c97dcSAndi Shyti 			gen6_gt_irq_handler(&dev_priv->gt, gt_iir);
2068d8fc8a47SPaulo Zanoni 		else
2069cf1c97dcSAndi Shyti 			gen5_gt_irq_handler(&dev_priv->gt, gt_iir);
20700e43406bSChris Wilson 	}
2071b1f14ad0SJesse Barnes 
2072b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
20730e43406bSChris Wilson 	if (de_iir) {
207472c90f62SOscar Mateo 		I915_WRITE(DEIIR, de_iir);
207572c90f62SOscar Mateo 		ret = IRQ_HANDLED;
207691d14251STvrtko Ursulin 		if (INTEL_GEN(dev_priv) >= 7)
207791d14251STvrtko Ursulin 			ivb_display_irq_handler(dev_priv, de_iir);
2078f1af8fc1SPaulo Zanoni 		else
207991d14251STvrtko Ursulin 			ilk_display_irq_handler(dev_priv, de_iir);
20800e43406bSChris Wilson 	}
20810e43406bSChris Wilson 
208291d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 6) {
2083f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
20840e43406bSChris Wilson 		if (pm_iir) {
2085b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
20860e43406bSChris Wilson 			ret = IRQ_HANDLED;
20873e7abf81SAndi Shyti 			gen6_rps_irq_handler(&dev_priv->gt.rps, pm_iir);
20880e43406bSChris Wilson 		}
2089f1af8fc1SPaulo Zanoni 	}
2090b1f14ad0SJesse Barnes 
2091b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
209274093f3eSChris Wilson 	if (!HAS_PCH_NOP(dev_priv))
209344498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
2094b1f14ad0SJesse Barnes 
20951f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
20969102650fSDaniele Ceraolo Spurio 	enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
20971f814dacSImre Deak 
2098b1f14ad0SJesse Barnes 	return ret;
2099b1f14ad0SJesse Barnes }
2100b1f14ad0SJesse Barnes 
210191d14251STvrtko Ursulin static void bxt_hpd_irq_handler(struct drm_i915_private *dev_priv,
210291d14251STvrtko Ursulin 				u32 hotplug_trigger,
210340e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2104d04a492dSShashank Sharma {
2105cebd87a0SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2106d04a492dSShashank Sharma 
2107a52bb15bSVille Syrjälä 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
2108a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
2109d04a492dSShashank Sharma 
2110cf53902fSRodrigo Vivi 	intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask, hotplug_trigger,
211140e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2112cebd87a0SVille Syrjälä 			   bxt_port_hotplug_long_detect);
211340e56410SVille Syrjälä 
211491d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2115d04a492dSShashank Sharma }
2116d04a492dSShashank Sharma 
2117121e758eSDhinakaran Pandiyan static void gen11_hpd_irq_handler(struct drm_i915_private *dev_priv, u32 iir)
2118121e758eSDhinakaran Pandiyan {
2119121e758eSDhinakaran Pandiyan 	u32 pin_mask = 0, long_mask = 0;
2120b796b971SDhinakaran Pandiyan 	u32 trigger_tc = iir & GEN11_DE_TC_HOTPLUG_MASK;
2121b796b971SDhinakaran Pandiyan 	u32 trigger_tbt = iir & GEN11_DE_TBT_HOTPLUG_MASK;
212248ef15d3SJosé Roberto de Souza 	long_pulse_detect_func long_pulse_detect;
212348ef15d3SJosé Roberto de Souza 	const u32 *hpd;
212448ef15d3SJosé Roberto de Souza 
212548ef15d3SJosé Roberto de Souza 	if (INTEL_GEN(dev_priv) >= 12) {
212648ef15d3SJosé Roberto de Souza 		long_pulse_detect = gen12_port_hotplug_long_detect;
212748ef15d3SJosé Roberto de Souza 		hpd = hpd_gen12;
212848ef15d3SJosé Roberto de Souza 	} else {
212948ef15d3SJosé Roberto de Souza 		long_pulse_detect = gen11_port_hotplug_long_detect;
213048ef15d3SJosé Roberto de Souza 		hpd = hpd_gen11;
213148ef15d3SJosé Roberto de Souza 	}
2132121e758eSDhinakaran Pandiyan 
2133121e758eSDhinakaran Pandiyan 	if (trigger_tc) {
2134b796b971SDhinakaran Pandiyan 		u32 dig_hotplug_reg;
2135b796b971SDhinakaran Pandiyan 
2136121e758eSDhinakaran Pandiyan 		dig_hotplug_reg = I915_READ(GEN11_TC_HOTPLUG_CTL);
2137121e758eSDhinakaran Pandiyan 		I915_WRITE(GEN11_TC_HOTPLUG_CTL, dig_hotplug_reg);
2138121e758eSDhinakaran Pandiyan 
2139121e758eSDhinakaran Pandiyan 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask, trigger_tc,
214048ef15d3SJosé Roberto de Souza 				   dig_hotplug_reg, hpd, long_pulse_detect);
2141121e758eSDhinakaran Pandiyan 	}
2142b796b971SDhinakaran Pandiyan 
2143b796b971SDhinakaran Pandiyan 	if (trigger_tbt) {
2144b796b971SDhinakaran Pandiyan 		u32 dig_hotplug_reg;
2145b796b971SDhinakaran Pandiyan 
2146b796b971SDhinakaran Pandiyan 		dig_hotplug_reg = I915_READ(GEN11_TBT_HOTPLUG_CTL);
2147b796b971SDhinakaran Pandiyan 		I915_WRITE(GEN11_TBT_HOTPLUG_CTL, dig_hotplug_reg);
2148b796b971SDhinakaran Pandiyan 
2149b796b971SDhinakaran Pandiyan 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask, trigger_tbt,
215048ef15d3SJosé Roberto de Souza 				   dig_hotplug_reg, hpd, long_pulse_detect);
2151b796b971SDhinakaran Pandiyan 	}
2152b796b971SDhinakaran Pandiyan 
2153b796b971SDhinakaran Pandiyan 	if (pin_mask)
2154b796b971SDhinakaran Pandiyan 		intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2155b796b971SDhinakaran Pandiyan 	else
2156b796b971SDhinakaran Pandiyan 		DRM_ERROR("Unexpected DE HPD interrupt 0x%08x\n", iir);
2157121e758eSDhinakaran Pandiyan }
2158121e758eSDhinakaran Pandiyan 
21599d17210fSLucas De Marchi static u32 gen8_de_port_aux_mask(struct drm_i915_private *dev_priv)
21609d17210fSLucas De Marchi {
216155523360SLucas De Marchi 	u32 mask;
21629d17210fSLucas De Marchi 
216355523360SLucas De Marchi 	if (INTEL_GEN(dev_priv) >= 12)
216455523360SLucas De Marchi 		return TGL_DE_PORT_AUX_DDIA |
216555523360SLucas De Marchi 			TGL_DE_PORT_AUX_DDIB |
2166e5df52dcSMatt Roper 			TGL_DE_PORT_AUX_DDIC |
2167e5df52dcSMatt Roper 			TGL_DE_PORT_AUX_USBC1 |
2168e5df52dcSMatt Roper 			TGL_DE_PORT_AUX_USBC2 |
2169e5df52dcSMatt Roper 			TGL_DE_PORT_AUX_USBC3 |
2170e5df52dcSMatt Roper 			TGL_DE_PORT_AUX_USBC4 |
2171e5df52dcSMatt Roper 			TGL_DE_PORT_AUX_USBC5 |
2172e5df52dcSMatt Roper 			TGL_DE_PORT_AUX_USBC6;
2173e5df52dcSMatt Roper 
217455523360SLucas De Marchi 
217555523360SLucas De Marchi 	mask = GEN8_AUX_CHANNEL_A;
21769d17210fSLucas De Marchi 	if (INTEL_GEN(dev_priv) >= 9)
21779d17210fSLucas De Marchi 		mask |= GEN9_AUX_CHANNEL_B |
21789d17210fSLucas De Marchi 			GEN9_AUX_CHANNEL_C |
21799d17210fSLucas De Marchi 			GEN9_AUX_CHANNEL_D;
21809d17210fSLucas De Marchi 
218155523360SLucas De Marchi 	if (IS_CNL_WITH_PORT_F(dev_priv) || IS_GEN(dev_priv, 11))
21829d17210fSLucas De Marchi 		mask |= CNL_AUX_CHANNEL_F;
21839d17210fSLucas De Marchi 
218455523360SLucas De Marchi 	if (IS_GEN(dev_priv, 11))
218555523360SLucas De Marchi 		mask |= ICL_AUX_CHANNEL_E;
21869d17210fSLucas De Marchi 
21879d17210fSLucas De Marchi 	return mask;
21889d17210fSLucas De Marchi }
21899d17210fSLucas De Marchi 
21905270130dSVille Syrjälä static u32 gen8_de_pipe_fault_mask(struct drm_i915_private *dev_priv)
21915270130dSVille Syrjälä {
2192d506a65dSMatt Roper 	if (INTEL_GEN(dev_priv) >= 11)
2193d506a65dSMatt Roper 		return GEN11_DE_PIPE_IRQ_FAULT_ERRORS;
2194d506a65dSMatt Roper 	else if (INTEL_GEN(dev_priv) >= 9)
21955270130dSVille Syrjälä 		return GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
21965270130dSVille Syrjälä 	else
21975270130dSVille Syrjälä 		return GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
21985270130dSVille Syrjälä }
21995270130dSVille Syrjälä 
220046c63d24SJosé Roberto de Souza static void
220146c63d24SJosé Roberto de Souza gen8_de_misc_irq_handler(struct drm_i915_private *dev_priv, u32 iir)
2202abd58f01SBen Widawsky {
2203e04f7eceSVille Syrjälä 	bool found = false;
2204e04f7eceSVille Syrjälä 
2205e04f7eceSVille Syrjälä 	if (iir & GEN8_DE_MISC_GSE) {
220691d14251STvrtko Ursulin 		intel_opregion_asle_intr(dev_priv);
2207e04f7eceSVille Syrjälä 		found = true;
2208e04f7eceSVille Syrjälä 	}
2209e04f7eceSVille Syrjälä 
2210e04f7eceSVille Syrjälä 	if (iir & GEN8_DE_EDP_PSR) {
22118241cfbeSJosé Roberto de Souza 		u32 psr_iir;
22128241cfbeSJosé Roberto de Souza 		i915_reg_t iir_reg;
22138241cfbeSJosé Roberto de Souza 
22148241cfbeSJosé Roberto de Souza 		if (INTEL_GEN(dev_priv) >= 12)
22158241cfbeSJosé Roberto de Souza 			iir_reg = TRANS_PSR_IIR(dev_priv->psr.transcoder);
22168241cfbeSJosé Roberto de Souza 		else
22178241cfbeSJosé Roberto de Souza 			iir_reg = EDP_PSR_IIR;
22188241cfbeSJosé Roberto de Souza 
22198241cfbeSJosé Roberto de Souza 		psr_iir = I915_READ(iir_reg);
22208241cfbeSJosé Roberto de Souza 		I915_WRITE(iir_reg, psr_iir);
22218241cfbeSJosé Roberto de Souza 
22228241cfbeSJosé Roberto de Souza 		if (psr_iir)
22238241cfbeSJosé Roberto de Souza 			found = true;
222454fd3149SDhinakaran Pandiyan 
222554fd3149SDhinakaran Pandiyan 		intel_psr_irq_handler(dev_priv, psr_iir);
2226e04f7eceSVille Syrjälä 	}
2227e04f7eceSVille Syrjälä 
2228e04f7eceSVille Syrjälä 	if (!found)
222938cc46d7SOscar Mateo 		DRM_ERROR("Unexpected DE Misc interrupt\n");
2230abd58f01SBen Widawsky }
223146c63d24SJosé Roberto de Souza 
223246c63d24SJosé Roberto de Souza static irqreturn_t
223346c63d24SJosé Roberto de Souza gen8_de_irq_handler(struct drm_i915_private *dev_priv, u32 master_ctl)
223446c63d24SJosé Roberto de Souza {
223546c63d24SJosé Roberto de Souza 	irqreturn_t ret = IRQ_NONE;
223646c63d24SJosé Roberto de Souza 	u32 iir;
223746c63d24SJosé Roberto de Souza 	enum pipe pipe;
223846c63d24SJosé Roberto de Souza 
223946c63d24SJosé Roberto de Souza 	if (master_ctl & GEN8_DE_MISC_IRQ) {
224046c63d24SJosé Roberto de Souza 		iir = I915_READ(GEN8_DE_MISC_IIR);
224146c63d24SJosé Roberto de Souza 		if (iir) {
224246c63d24SJosé Roberto de Souza 			I915_WRITE(GEN8_DE_MISC_IIR, iir);
224346c63d24SJosé Roberto de Souza 			ret = IRQ_HANDLED;
224446c63d24SJosé Roberto de Souza 			gen8_de_misc_irq_handler(dev_priv, iir);
224546c63d24SJosé Roberto de Souza 		} else {
224638cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
2247abd58f01SBen Widawsky 		}
224846c63d24SJosé Roberto de Souza 	}
2249abd58f01SBen Widawsky 
2250121e758eSDhinakaran Pandiyan 	if (INTEL_GEN(dev_priv) >= 11 && (master_ctl & GEN11_DE_HPD_IRQ)) {
2251121e758eSDhinakaran Pandiyan 		iir = I915_READ(GEN11_DE_HPD_IIR);
2252121e758eSDhinakaran Pandiyan 		if (iir) {
2253121e758eSDhinakaran Pandiyan 			I915_WRITE(GEN11_DE_HPD_IIR, iir);
2254121e758eSDhinakaran Pandiyan 			ret = IRQ_HANDLED;
2255121e758eSDhinakaran Pandiyan 			gen11_hpd_irq_handler(dev_priv, iir);
2256121e758eSDhinakaran Pandiyan 		} else {
2257121e758eSDhinakaran Pandiyan 			DRM_ERROR("The master control interrupt lied, (DE HPD)!\n");
2258121e758eSDhinakaran Pandiyan 		}
2259121e758eSDhinakaran Pandiyan 	}
2260121e758eSDhinakaran Pandiyan 
22616d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
2262e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_PORT_IIR);
2263e32192e1STvrtko Ursulin 		if (iir) {
2264e32192e1STvrtko Ursulin 			u32 tmp_mask;
2265d04a492dSShashank Sharma 			bool found = false;
2266cebd87a0SVille Syrjälä 
2267e32192e1STvrtko Ursulin 			I915_WRITE(GEN8_DE_PORT_IIR, iir);
22686d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
226988e04703SJesse Barnes 
22709d17210fSLucas De Marchi 			if (iir & gen8_de_port_aux_mask(dev_priv)) {
227191d14251STvrtko Ursulin 				dp_aux_irq_handler(dev_priv);
2272d04a492dSShashank Sharma 				found = true;
2273d04a492dSShashank Sharma 			}
2274d04a492dSShashank Sharma 
2275cc3f90f0SAnder Conselvan de Oliveira 			if (IS_GEN9_LP(dev_priv)) {
2276e32192e1STvrtko Ursulin 				tmp_mask = iir & BXT_DE_PORT_HOTPLUG_MASK;
2277e32192e1STvrtko Ursulin 				if (tmp_mask) {
227891d14251STvrtko Ursulin 					bxt_hpd_irq_handler(dev_priv, tmp_mask,
227991d14251STvrtko Ursulin 							    hpd_bxt);
2280d04a492dSShashank Sharma 					found = true;
2281d04a492dSShashank Sharma 				}
2282e32192e1STvrtko Ursulin 			} else if (IS_BROADWELL(dev_priv)) {
2283e32192e1STvrtko Ursulin 				tmp_mask = iir & GEN8_PORT_DP_A_HOTPLUG;
2284e32192e1STvrtko Ursulin 				if (tmp_mask) {
228591d14251STvrtko Ursulin 					ilk_hpd_irq_handler(dev_priv,
228691d14251STvrtko Ursulin 							    tmp_mask, hpd_bdw);
2287e32192e1STvrtko Ursulin 					found = true;
2288e32192e1STvrtko Ursulin 				}
2289e32192e1STvrtko Ursulin 			}
2290d04a492dSShashank Sharma 
2291cc3f90f0SAnder Conselvan de Oliveira 			if (IS_GEN9_LP(dev_priv) && (iir & BXT_DE_PORT_GMBUS)) {
229291d14251STvrtko Ursulin 				gmbus_irq_handler(dev_priv);
22939e63743eSShashank Sharma 				found = true;
22949e63743eSShashank Sharma 			}
22959e63743eSShashank Sharma 
2296d04a492dSShashank Sharma 			if (!found)
229738cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Port interrupt\n");
22986d766f02SDaniel Vetter 		}
229938cc46d7SOscar Mateo 		else
230038cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
23016d766f02SDaniel Vetter 	}
23026d766f02SDaniel Vetter 
2303055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2304fd3a4024SDaniel Vetter 		u32 fault_errors;
2305abd58f01SBen Widawsky 
2306c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2307c42664ccSDaniel Vetter 			continue;
2308c42664ccSDaniel Vetter 
2309e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
2310e32192e1STvrtko Ursulin 		if (!iir) {
2311e32192e1STvrtko Ursulin 			DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
2312e32192e1STvrtko Ursulin 			continue;
2313e32192e1STvrtko Ursulin 		}
2314770de83dSDamien Lespiau 
2315e32192e1STvrtko Ursulin 		ret = IRQ_HANDLED;
2316e32192e1STvrtko Ursulin 		I915_WRITE(GEN8_DE_PIPE_IIR(pipe), iir);
2317e32192e1STvrtko Ursulin 
2318fd3a4024SDaniel Vetter 		if (iir & GEN8_PIPE_VBLANK)
2319fd3a4024SDaniel Vetter 			drm_handle_vblank(&dev_priv->drm, pipe);
2320abd58f01SBen Widawsky 
2321e32192e1STvrtko Ursulin 		if (iir & GEN8_PIPE_CDCLK_CRC_DONE)
232291d14251STvrtko Ursulin 			hsw_pipe_crc_irq_handler(dev_priv, pipe);
23230fbe7870SDaniel Vetter 
2324e32192e1STvrtko Ursulin 		if (iir & GEN8_PIPE_FIFO_UNDERRUN)
2325e32192e1STvrtko Ursulin 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
232638d83c96SDaniel Vetter 
23275270130dSVille Syrjälä 		fault_errors = iir & gen8_de_pipe_fault_mask(dev_priv);
2328770de83dSDamien Lespiau 		if (fault_errors)
23291353ec38STvrtko Ursulin 			DRM_ERROR("Fault errors on pipe %c: 0x%08x\n",
233030100f2bSDaniel Vetter 				  pipe_name(pipe),
2331e32192e1STvrtko Ursulin 				  fault_errors);
2332abd58f01SBen Widawsky 	}
2333abd58f01SBen Widawsky 
233491d14251STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv) && !HAS_PCH_NOP(dev_priv) &&
2335266ea3d9SShashank Sharma 	    master_ctl & GEN8_DE_PCH_IRQ) {
233692d03a80SDaniel Vetter 		/*
233792d03a80SDaniel Vetter 		 * FIXME(BDW): Assume for now that the new interrupt handling
233892d03a80SDaniel Vetter 		 * scheme also closed the SDE interrupt handling race we've seen
233992d03a80SDaniel Vetter 		 * on older pch-split platforms. But this needs testing.
234092d03a80SDaniel Vetter 		 */
2341e32192e1STvrtko Ursulin 		iir = I915_READ(SDEIIR);
2342e32192e1STvrtko Ursulin 		if (iir) {
2343e32192e1STvrtko Ursulin 			I915_WRITE(SDEIIR, iir);
234492d03a80SDaniel Vetter 			ret = IRQ_HANDLED;
23456dbf30ceSVille Syrjälä 
234658676af6SLucas De Marchi 			if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
234758676af6SLucas De Marchi 				icp_irq_handler(dev_priv, iir);
2348c6c30b91SRodrigo Vivi 			else if (INTEL_PCH_TYPE(dev_priv) >= PCH_SPT)
234991d14251STvrtko Ursulin 				spt_irq_handler(dev_priv, iir);
23506dbf30ceSVille Syrjälä 			else
235191d14251STvrtko Ursulin 				cpt_irq_handler(dev_priv, iir);
23522dfb0b81SJani Nikula 		} else {
23532dfb0b81SJani Nikula 			/*
23542dfb0b81SJani Nikula 			 * Like on previous PCH there seems to be something
23552dfb0b81SJani Nikula 			 * fishy going on with forwarding PCH interrupts.
23562dfb0b81SJani Nikula 			 */
23572dfb0b81SJani Nikula 			DRM_DEBUG_DRIVER("The master control interrupt lied (SDE)!\n");
23582dfb0b81SJani Nikula 		}
235992d03a80SDaniel Vetter 	}
236092d03a80SDaniel Vetter 
2361f11a0f46STvrtko Ursulin 	return ret;
2362f11a0f46STvrtko Ursulin }
2363f11a0f46STvrtko Ursulin 
23644376b9c9SMika Kuoppala static inline u32 gen8_master_intr_disable(void __iomem * const regs)
23654376b9c9SMika Kuoppala {
23664376b9c9SMika Kuoppala 	raw_reg_write(regs, GEN8_MASTER_IRQ, 0);
23674376b9c9SMika Kuoppala 
23684376b9c9SMika Kuoppala 	/*
23694376b9c9SMika Kuoppala 	 * Now with master disabled, get a sample of level indications
23704376b9c9SMika Kuoppala 	 * for this interrupt. Indications will be cleared on related acks.
23714376b9c9SMika Kuoppala 	 * New indications can and will light up during processing,
23724376b9c9SMika Kuoppala 	 * and will generate new interrupt after enabling master.
23734376b9c9SMika Kuoppala 	 */
23744376b9c9SMika Kuoppala 	return raw_reg_read(regs, GEN8_MASTER_IRQ);
23754376b9c9SMika Kuoppala }
23764376b9c9SMika Kuoppala 
23774376b9c9SMika Kuoppala static inline void gen8_master_intr_enable(void __iomem * const regs)
23784376b9c9SMika Kuoppala {
23794376b9c9SMika Kuoppala 	raw_reg_write(regs, GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
23804376b9c9SMika Kuoppala }
23814376b9c9SMika Kuoppala 
2382f11a0f46STvrtko Ursulin static irqreturn_t gen8_irq_handler(int irq, void *arg)
2383f11a0f46STvrtko Ursulin {
2384b318b824SVille Syrjälä 	struct drm_i915_private *dev_priv = arg;
238525286aacSDaniele Ceraolo Spurio 	void __iomem * const regs = dev_priv->uncore.regs;
2386f11a0f46STvrtko Ursulin 	u32 master_ctl;
2387f0fd96f5SChris Wilson 	u32 gt_iir[4];
2388f11a0f46STvrtko Ursulin 
2389f11a0f46STvrtko Ursulin 	if (!intel_irqs_enabled(dev_priv))
2390f11a0f46STvrtko Ursulin 		return IRQ_NONE;
2391f11a0f46STvrtko Ursulin 
23924376b9c9SMika Kuoppala 	master_ctl = gen8_master_intr_disable(regs);
23934376b9c9SMika Kuoppala 	if (!master_ctl) {
23944376b9c9SMika Kuoppala 		gen8_master_intr_enable(regs);
2395f11a0f46STvrtko Ursulin 		return IRQ_NONE;
23964376b9c9SMika Kuoppala 	}
2397f11a0f46STvrtko Ursulin 
2398f11a0f46STvrtko Ursulin 	/* Find, clear, then process each source of interrupt */
2399cf1c97dcSAndi Shyti 	gen8_gt_irq_ack(&dev_priv->gt, master_ctl, gt_iir);
2400f0fd96f5SChris Wilson 
2401f0fd96f5SChris Wilson 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
2402f0fd96f5SChris Wilson 	if (master_ctl & ~GEN8_GT_IRQS) {
24039102650fSDaniele Ceraolo Spurio 		disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
240455ef72f2SChris Wilson 		gen8_de_irq_handler(dev_priv, master_ctl);
24059102650fSDaniele Ceraolo Spurio 		enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
2406f0fd96f5SChris Wilson 	}
2407f11a0f46STvrtko Ursulin 
24084376b9c9SMika Kuoppala 	gen8_master_intr_enable(regs);
2409abd58f01SBen Widawsky 
2410cf1c97dcSAndi Shyti 	gen8_gt_irq_handler(&dev_priv->gt, master_ctl, gt_iir);
24111f814dacSImre Deak 
241255ef72f2SChris Wilson 	return IRQ_HANDLED;
2413abd58f01SBen Widawsky }
2414abd58f01SBen Widawsky 
241551951ae7SMika Kuoppala static u32
24169b77011eSTvrtko Ursulin gen11_gu_misc_irq_ack(struct intel_gt *gt, const u32 master_ctl)
2417df0d28c1SDhinakaran Pandiyan {
24189b77011eSTvrtko Ursulin 	void __iomem * const regs = gt->uncore->regs;
24197a909383SChris Wilson 	u32 iir;
2420df0d28c1SDhinakaran Pandiyan 
2421df0d28c1SDhinakaran Pandiyan 	if (!(master_ctl & GEN11_GU_MISC_IRQ))
24227a909383SChris Wilson 		return 0;
2423df0d28c1SDhinakaran Pandiyan 
24247a909383SChris Wilson 	iir = raw_reg_read(regs, GEN11_GU_MISC_IIR);
24257a909383SChris Wilson 	if (likely(iir))
24267a909383SChris Wilson 		raw_reg_write(regs, GEN11_GU_MISC_IIR, iir);
24277a909383SChris Wilson 
24287a909383SChris Wilson 	return iir;
2429df0d28c1SDhinakaran Pandiyan }
2430df0d28c1SDhinakaran Pandiyan 
2431df0d28c1SDhinakaran Pandiyan static void
24329b77011eSTvrtko Ursulin gen11_gu_misc_irq_handler(struct intel_gt *gt, const u32 iir)
2433df0d28c1SDhinakaran Pandiyan {
2434df0d28c1SDhinakaran Pandiyan 	if (iir & GEN11_GU_MISC_GSE)
24359b77011eSTvrtko Ursulin 		intel_opregion_asle_intr(gt->i915);
2436df0d28c1SDhinakaran Pandiyan }
2437df0d28c1SDhinakaran Pandiyan 
243881067b71SMika Kuoppala static inline u32 gen11_master_intr_disable(void __iomem * const regs)
243981067b71SMika Kuoppala {
244081067b71SMika Kuoppala 	raw_reg_write(regs, GEN11_GFX_MSTR_IRQ, 0);
244181067b71SMika Kuoppala 
244281067b71SMika Kuoppala 	/*
244381067b71SMika Kuoppala 	 * Now with master disabled, get a sample of level indications
244481067b71SMika Kuoppala 	 * for this interrupt. Indications will be cleared on related acks.
244581067b71SMika Kuoppala 	 * New indications can and will light up during processing,
244681067b71SMika Kuoppala 	 * and will generate new interrupt after enabling master.
244781067b71SMika Kuoppala 	 */
244881067b71SMika Kuoppala 	return raw_reg_read(regs, GEN11_GFX_MSTR_IRQ);
244981067b71SMika Kuoppala }
245081067b71SMika Kuoppala 
245181067b71SMika Kuoppala static inline void gen11_master_intr_enable(void __iomem * const regs)
245281067b71SMika Kuoppala {
245381067b71SMika Kuoppala 	raw_reg_write(regs, GEN11_GFX_MSTR_IRQ, GEN11_MASTER_IRQ);
245481067b71SMika Kuoppala }
245581067b71SMika Kuoppala 
2456a3265d85SMatt Roper static void
2457a3265d85SMatt Roper gen11_display_irq_handler(struct drm_i915_private *i915)
2458a3265d85SMatt Roper {
2459a3265d85SMatt Roper 	void __iomem * const regs = i915->uncore.regs;
2460a3265d85SMatt Roper 	const u32 disp_ctl = raw_reg_read(regs, GEN11_DISPLAY_INT_CTL);
2461a3265d85SMatt Roper 
2462a3265d85SMatt Roper 	disable_rpm_wakeref_asserts(&i915->runtime_pm);
2463a3265d85SMatt Roper 	/*
2464a3265d85SMatt Roper 	 * GEN11_DISPLAY_INT_CTL has same format as GEN8_MASTER_IRQ
2465a3265d85SMatt Roper 	 * for the display related bits.
2466a3265d85SMatt Roper 	 */
2467a3265d85SMatt Roper 	raw_reg_write(regs, GEN11_DISPLAY_INT_CTL, 0x0);
2468a3265d85SMatt Roper 	gen8_de_irq_handler(i915, disp_ctl);
2469a3265d85SMatt Roper 	raw_reg_write(regs, GEN11_DISPLAY_INT_CTL,
2470a3265d85SMatt Roper 		      GEN11_DISPLAY_IRQ_ENABLE);
2471a3265d85SMatt Roper 
2472a3265d85SMatt Roper 	enable_rpm_wakeref_asserts(&i915->runtime_pm);
2473a3265d85SMatt Roper }
2474a3265d85SMatt Roper 
24757be8782aSLucas De Marchi static __always_inline irqreturn_t
24767be8782aSLucas De Marchi __gen11_irq_handler(struct drm_i915_private * const i915,
24777be8782aSLucas De Marchi 		    u32 (*intr_disable)(void __iomem * const regs),
24787be8782aSLucas De Marchi 		    void (*intr_enable)(void __iomem * const regs))
247951951ae7SMika Kuoppala {
248025286aacSDaniele Ceraolo Spurio 	void __iomem * const regs = i915->uncore.regs;
24819b77011eSTvrtko Ursulin 	struct intel_gt *gt = &i915->gt;
248251951ae7SMika Kuoppala 	u32 master_ctl;
2483df0d28c1SDhinakaran Pandiyan 	u32 gu_misc_iir;
248451951ae7SMika Kuoppala 
248551951ae7SMika Kuoppala 	if (!intel_irqs_enabled(i915))
248651951ae7SMika Kuoppala 		return IRQ_NONE;
248751951ae7SMika Kuoppala 
24887be8782aSLucas De Marchi 	master_ctl = intr_disable(regs);
248981067b71SMika Kuoppala 	if (!master_ctl) {
24907be8782aSLucas De Marchi 		intr_enable(regs);
249151951ae7SMika Kuoppala 		return IRQ_NONE;
249281067b71SMika Kuoppala 	}
249351951ae7SMika Kuoppala 
249451951ae7SMika Kuoppala 	/* Find, clear, then process each source of interrupt. */
24959b77011eSTvrtko Ursulin 	gen11_gt_irq_handler(gt, master_ctl);
249651951ae7SMika Kuoppala 
249751951ae7SMika Kuoppala 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
2498a3265d85SMatt Roper 	if (master_ctl & GEN11_DISPLAY_IRQ)
2499a3265d85SMatt Roper 		gen11_display_irq_handler(i915);
250051951ae7SMika Kuoppala 
25019b77011eSTvrtko Ursulin 	gu_misc_iir = gen11_gu_misc_irq_ack(gt, master_ctl);
2502df0d28c1SDhinakaran Pandiyan 
25037be8782aSLucas De Marchi 	intr_enable(regs);
250451951ae7SMika Kuoppala 
25059b77011eSTvrtko Ursulin 	gen11_gu_misc_irq_handler(gt, gu_misc_iir);
2506df0d28c1SDhinakaran Pandiyan 
250751951ae7SMika Kuoppala 	return IRQ_HANDLED;
250851951ae7SMika Kuoppala }
250951951ae7SMika Kuoppala 
25107be8782aSLucas De Marchi static irqreturn_t gen11_irq_handler(int irq, void *arg)
25117be8782aSLucas De Marchi {
25127be8782aSLucas De Marchi 	return __gen11_irq_handler(arg,
25137be8782aSLucas De Marchi 				   gen11_master_intr_disable,
25147be8782aSLucas De Marchi 				   gen11_master_intr_enable);
25157be8782aSLucas De Marchi }
25167be8782aSLucas De Marchi 
251742f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
251842f52ef8SKeith Packard  * we use as a pipe index
251942f52ef8SKeith Packard  */
252008fa8fd0SVille Syrjälä int i8xx_enable_vblank(struct drm_crtc *crtc)
25210a3e67a4SJesse Barnes {
252208fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
252308fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
2524e9d21d7fSKeith Packard 	unsigned long irqflags;
252571e0ffa5SJesse Barnes 
25261ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
252786e83e35SChris Wilson 	i915_enable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_STATUS);
252886e83e35SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
252986e83e35SChris Wilson 
253086e83e35SChris Wilson 	return 0;
253186e83e35SChris Wilson }
253286e83e35SChris Wilson 
25337d423af9SVille Syrjälä int i915gm_enable_vblank(struct drm_crtc *crtc)
2534d938da6bSVille Syrjälä {
253508fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
2536d938da6bSVille Syrjälä 
25377d423af9SVille Syrjälä 	/*
25387d423af9SVille Syrjälä 	 * Vblank interrupts fail to wake the device up from C2+.
25397d423af9SVille Syrjälä 	 * Disabling render clock gating during C-states avoids
25407d423af9SVille Syrjälä 	 * the problem. There is a small power cost so we do this
25417d423af9SVille Syrjälä 	 * only when vblank interrupts are actually enabled.
25427d423af9SVille Syrjälä 	 */
25437d423af9SVille Syrjälä 	if (dev_priv->vblank_enabled++ == 0)
25447d423af9SVille Syrjälä 		I915_WRITE(SCPD0, _MASKED_BIT_ENABLE(CSTATE_RENDER_CLOCK_GATE_DISABLE));
2545d938da6bSVille Syrjälä 
254608fa8fd0SVille Syrjälä 	return i8xx_enable_vblank(crtc);
2547d938da6bSVille Syrjälä }
2548d938da6bSVille Syrjälä 
254908fa8fd0SVille Syrjälä int i965_enable_vblank(struct drm_crtc *crtc)
255086e83e35SChris Wilson {
255108fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
255208fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
255386e83e35SChris Wilson 	unsigned long irqflags;
255486e83e35SChris Wilson 
255586e83e35SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
25567c463586SKeith Packard 	i915_enable_pipestat(dev_priv, pipe,
2557755e9019SImre Deak 			     PIPE_START_VBLANK_INTERRUPT_STATUS);
25581ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
25598692d00eSChris Wilson 
25600a3e67a4SJesse Barnes 	return 0;
25610a3e67a4SJesse Barnes }
25620a3e67a4SJesse Barnes 
256308fa8fd0SVille Syrjälä int ilk_enable_vblank(struct drm_crtc *crtc)
2564f796cf8fSJesse Barnes {
256508fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
256608fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
2567f796cf8fSJesse Barnes 	unsigned long irqflags;
2568a9c287c9SJani Nikula 	u32 bit = INTEL_GEN(dev_priv) >= 7 ?
256986e83e35SChris Wilson 		DE_PIPE_VBLANK_IVB(pipe) : DE_PIPE_VBLANK(pipe);
2570f796cf8fSJesse Barnes 
2571f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2572fbdedaeaSVille Syrjälä 	ilk_enable_display_irq(dev_priv, bit);
2573b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2574b1f14ad0SJesse Barnes 
25752e8bf223SDhinakaran Pandiyan 	/* Even though there is no DMC, frame counter can get stuck when
25762e8bf223SDhinakaran Pandiyan 	 * PSR is active as no frames are generated.
25772e8bf223SDhinakaran Pandiyan 	 */
25782e8bf223SDhinakaran Pandiyan 	if (HAS_PSR(dev_priv))
257908fa8fd0SVille Syrjälä 		drm_crtc_vblank_restore(crtc);
25802e8bf223SDhinakaran Pandiyan 
2581b1f14ad0SJesse Barnes 	return 0;
2582b1f14ad0SJesse Barnes }
2583b1f14ad0SJesse Barnes 
258408fa8fd0SVille Syrjälä int bdw_enable_vblank(struct drm_crtc *crtc)
2585abd58f01SBen Widawsky {
258608fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
258708fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
2588abd58f01SBen Widawsky 	unsigned long irqflags;
2589abd58f01SBen Widawsky 
2590abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2591013d3752SVille Syrjälä 	bdw_enable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
2592abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2593013d3752SVille Syrjälä 
25942e8bf223SDhinakaran Pandiyan 	/* Even if there is no DMC, frame counter can get stuck when
25952e8bf223SDhinakaran Pandiyan 	 * PSR is active as no frames are generated, so check only for PSR.
25962e8bf223SDhinakaran Pandiyan 	 */
25972e8bf223SDhinakaran Pandiyan 	if (HAS_PSR(dev_priv))
259808fa8fd0SVille Syrjälä 		drm_crtc_vblank_restore(crtc);
25992e8bf223SDhinakaran Pandiyan 
2600abd58f01SBen Widawsky 	return 0;
2601abd58f01SBen Widawsky }
2602abd58f01SBen Widawsky 
260342f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
260442f52ef8SKeith Packard  * we use as a pipe index
260542f52ef8SKeith Packard  */
260608fa8fd0SVille Syrjälä void i8xx_disable_vblank(struct drm_crtc *crtc)
260786e83e35SChris Wilson {
260808fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
260908fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
261086e83e35SChris Wilson 	unsigned long irqflags;
261186e83e35SChris Wilson 
261286e83e35SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
261386e83e35SChris Wilson 	i915_disable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_STATUS);
261486e83e35SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
261586e83e35SChris Wilson }
261686e83e35SChris Wilson 
26177d423af9SVille Syrjälä void i915gm_disable_vblank(struct drm_crtc *crtc)
2618d938da6bSVille Syrjälä {
261908fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
2620d938da6bSVille Syrjälä 
262108fa8fd0SVille Syrjälä 	i8xx_disable_vblank(crtc);
2622d938da6bSVille Syrjälä 
26237d423af9SVille Syrjälä 	if (--dev_priv->vblank_enabled == 0)
26247d423af9SVille Syrjälä 		I915_WRITE(SCPD0, _MASKED_BIT_DISABLE(CSTATE_RENDER_CLOCK_GATE_DISABLE));
2625d938da6bSVille Syrjälä }
2626d938da6bSVille Syrjälä 
262708fa8fd0SVille Syrjälä void i965_disable_vblank(struct drm_crtc *crtc)
26280a3e67a4SJesse Barnes {
262908fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
263008fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
2631e9d21d7fSKeith Packard 	unsigned long irqflags;
26320a3e67a4SJesse Barnes 
26331ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
26347c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
2635755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
26361ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26370a3e67a4SJesse Barnes }
26380a3e67a4SJesse Barnes 
263908fa8fd0SVille Syrjälä void ilk_disable_vblank(struct drm_crtc *crtc)
2640f796cf8fSJesse Barnes {
264108fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
264208fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
2643f796cf8fSJesse Barnes 	unsigned long irqflags;
2644a9c287c9SJani Nikula 	u32 bit = INTEL_GEN(dev_priv) >= 7 ?
264586e83e35SChris Wilson 		DE_PIPE_VBLANK_IVB(pipe) : DE_PIPE_VBLANK(pipe);
2646f796cf8fSJesse Barnes 
2647f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2648fbdedaeaSVille Syrjälä 	ilk_disable_display_irq(dev_priv, bit);
2649b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2650b1f14ad0SJesse Barnes }
2651b1f14ad0SJesse Barnes 
265208fa8fd0SVille Syrjälä void bdw_disable_vblank(struct drm_crtc *crtc)
2653abd58f01SBen Widawsky {
265408fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
265508fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
2656abd58f01SBen Widawsky 	unsigned long irqflags;
2657abd58f01SBen Widawsky 
2658abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2659013d3752SVille Syrjälä 	bdw_disable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
2660abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2661abd58f01SBen Widawsky }
2662abd58f01SBen Widawsky 
2663b243f530STvrtko Ursulin static void ibx_irq_reset(struct drm_i915_private *dev_priv)
266491738a95SPaulo Zanoni {
2665b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
2666b16b2a2fSPaulo Zanoni 
26676e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
266891738a95SPaulo Zanoni 		return;
266991738a95SPaulo Zanoni 
2670b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, SDE);
2671105b122eSPaulo Zanoni 
26726e266956STvrtko Ursulin 	if (HAS_PCH_CPT(dev_priv) || HAS_PCH_LPT(dev_priv))
2673105b122eSPaulo Zanoni 		I915_WRITE(SERR_INT, 0xffffffff);
2674622364b6SPaulo Zanoni }
2675105b122eSPaulo Zanoni 
267691738a95SPaulo Zanoni /*
2677622364b6SPaulo Zanoni  * SDEIER is also touched by the interrupt handler to work around missed PCH
2678622364b6SPaulo Zanoni  * interrupts. Hence we can't update it after the interrupt handler is enabled -
2679622364b6SPaulo Zanoni  * instead we unconditionally enable all PCH interrupt sources here, but then
2680622364b6SPaulo Zanoni  * only unmask them as needed with SDEIMR.
2681622364b6SPaulo Zanoni  *
2682622364b6SPaulo Zanoni  * This function needs to be called before interrupts are enabled.
268391738a95SPaulo Zanoni  */
2684b318b824SVille Syrjälä static void ibx_irq_pre_postinstall(struct drm_i915_private *dev_priv)
2685622364b6SPaulo Zanoni {
26866e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
2687622364b6SPaulo Zanoni 		return;
2688622364b6SPaulo Zanoni 
2689622364b6SPaulo Zanoni 	WARN_ON(I915_READ(SDEIER) != 0);
269091738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
269191738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
269291738a95SPaulo Zanoni }
269391738a95SPaulo Zanoni 
269470591a41SVille Syrjälä static void vlv_display_irq_reset(struct drm_i915_private *dev_priv)
269570591a41SVille Syrjälä {
2696b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
2697b16b2a2fSPaulo Zanoni 
269871b8b41dSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
2699f0818984STvrtko Ursulin 		intel_uncore_write(uncore, DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
270071b8b41dSVille Syrjälä 	else
2701f0818984STvrtko Ursulin 		intel_uncore_write(uncore, DPINVGTT, DPINVGTT_STATUS_MASK);
270271b8b41dSVille Syrjälä 
2703ad22d106SVille Syrjälä 	i915_hotplug_interrupt_update_locked(dev_priv, 0xffffffff, 0);
2704f0818984STvrtko Ursulin 	intel_uncore_write(uncore, PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
270570591a41SVille Syrjälä 
270644d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
270770591a41SVille Syrjälä 
2708b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, VLV_);
27098bd099a7SChris Wilson 	dev_priv->irq_mask = ~0u;
271070591a41SVille Syrjälä }
271170591a41SVille Syrjälä 
27128bb61306SVille Syrjälä static void vlv_display_irq_postinstall(struct drm_i915_private *dev_priv)
27138bb61306SVille Syrjälä {
2714b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
2715b16b2a2fSPaulo Zanoni 
27168bb61306SVille Syrjälä 	u32 pipestat_mask;
27179ab981f2SVille Syrjälä 	u32 enable_mask;
27188bb61306SVille Syrjälä 	enum pipe pipe;
27198bb61306SVille Syrjälä 
2720842ebf7aSVille Syrjälä 	pipestat_mask = PIPE_CRC_DONE_INTERRUPT_STATUS;
27218bb61306SVille Syrjälä 
27228bb61306SVille Syrjälä 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
27238bb61306SVille Syrjälä 	for_each_pipe(dev_priv, pipe)
27248bb61306SVille Syrjälä 		i915_enable_pipestat(dev_priv, pipe, pipestat_mask);
27258bb61306SVille Syrjälä 
27269ab981f2SVille Syrjälä 	enable_mask = I915_DISPLAY_PORT_INTERRUPT |
27278bb61306SVille Syrjälä 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2728ebf5f921SVille Syrjälä 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2729ebf5f921SVille Syrjälä 		I915_LPE_PIPE_A_INTERRUPT |
2730ebf5f921SVille Syrjälä 		I915_LPE_PIPE_B_INTERRUPT;
2731ebf5f921SVille Syrjälä 
27328bb61306SVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
2733ebf5f921SVille Syrjälä 		enable_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT |
2734ebf5f921SVille Syrjälä 			I915_LPE_PIPE_C_INTERRUPT;
27356b7eafc1SVille Syrjälä 
27368bd099a7SChris Wilson 	WARN_ON(dev_priv->irq_mask != ~0u);
27376b7eafc1SVille Syrjälä 
27389ab981f2SVille Syrjälä 	dev_priv->irq_mask = ~enable_mask;
27398bb61306SVille Syrjälä 
2740b16b2a2fSPaulo Zanoni 	GEN3_IRQ_INIT(uncore, VLV_, dev_priv->irq_mask, enable_mask);
27418bb61306SVille Syrjälä }
27428bb61306SVille Syrjälä 
27438bb61306SVille Syrjälä /* drm_dma.h hooks
27448bb61306SVille Syrjälä */
27459eae5e27SLucas De Marchi static void ilk_irq_reset(struct drm_i915_private *dev_priv)
27468bb61306SVille Syrjälä {
2747b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
27488bb61306SVille Syrjälä 
2749b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, DE);
2750cf819effSLucas De Marchi 	if (IS_GEN(dev_priv, 7))
2751f0818984STvrtko Ursulin 		intel_uncore_write(uncore, GEN7_ERR_INT, 0xffffffff);
27528bb61306SVille Syrjälä 
2753fc340442SDaniel Vetter 	if (IS_HASWELL(dev_priv)) {
2754f0818984STvrtko Ursulin 		intel_uncore_write(uncore, EDP_PSR_IMR, 0xffffffff);
2755f0818984STvrtko Ursulin 		intel_uncore_write(uncore, EDP_PSR_IIR, 0xffffffff);
2756fc340442SDaniel Vetter 	}
2757fc340442SDaniel Vetter 
2758cf1c97dcSAndi Shyti 	gen5_gt_irq_reset(&dev_priv->gt);
27598bb61306SVille Syrjälä 
2760b243f530STvrtko Ursulin 	ibx_irq_reset(dev_priv);
27618bb61306SVille Syrjälä }
27628bb61306SVille Syrjälä 
2763b318b824SVille Syrjälä static void valleyview_irq_reset(struct drm_i915_private *dev_priv)
27647e231dbeSJesse Barnes {
276534c7b8a7SVille Syrjälä 	I915_WRITE(VLV_MASTER_IER, 0);
276634c7b8a7SVille Syrjälä 	POSTING_READ(VLV_MASTER_IER);
276734c7b8a7SVille Syrjälä 
2768cf1c97dcSAndi Shyti 	gen5_gt_irq_reset(&dev_priv->gt);
27697e231dbeSJesse Barnes 
2770ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
27719918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
277270591a41SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
2773ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
27747e231dbeSJesse Barnes }
27757e231dbeSJesse Barnes 
2776b318b824SVille Syrjälä static void gen8_irq_reset(struct drm_i915_private *dev_priv)
2777abd58f01SBen Widawsky {
2778b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
2779d048a268SVille Syrjälä 	enum pipe pipe;
2780abd58f01SBen Widawsky 
278125286aacSDaniele Ceraolo Spurio 	gen8_master_intr_disable(dev_priv->uncore.regs);
2782abd58f01SBen Widawsky 
2783cf1c97dcSAndi Shyti 	gen8_gt_irq_reset(&dev_priv->gt);
2784abd58f01SBen Widawsky 
2785f0818984STvrtko Ursulin 	intel_uncore_write(uncore, EDP_PSR_IMR, 0xffffffff);
2786f0818984STvrtko Ursulin 	intel_uncore_write(uncore, EDP_PSR_IIR, 0xffffffff);
2787e04f7eceSVille Syrjälä 
2788055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
2789f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
2790813bde43SPaulo Zanoni 						   POWER_DOMAIN_PIPE(pipe)))
2791b16b2a2fSPaulo Zanoni 			GEN8_IRQ_RESET_NDX(uncore, DE_PIPE, pipe);
2792abd58f01SBen Widawsky 
2793b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN8_DE_PORT_);
2794b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN8_DE_MISC_);
2795b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN8_PCU_);
2796abd58f01SBen Widawsky 
27976e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
2798b243f530STvrtko Ursulin 		ibx_irq_reset(dev_priv);
2799abd58f01SBen Widawsky }
2800abd58f01SBen Widawsky 
2801a3265d85SMatt Roper static void gen11_display_irq_reset(struct drm_i915_private *dev_priv)
280251951ae7SMika Kuoppala {
2803b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
2804d048a268SVille Syrjälä 	enum pipe pipe;
280551951ae7SMika Kuoppala 
2806f0818984STvrtko Ursulin 	intel_uncore_write(uncore, GEN11_DISPLAY_INT_CTL, 0);
280751951ae7SMika Kuoppala 
28088241cfbeSJosé Roberto de Souza 	if (INTEL_GEN(dev_priv) >= 12) {
28098241cfbeSJosé Roberto de Souza 		enum transcoder trans;
28108241cfbeSJosé Roberto de Souza 
28118241cfbeSJosé Roberto de Souza 		for (trans = TRANSCODER_A; trans <= TRANSCODER_D; trans++) {
28128241cfbeSJosé Roberto de Souza 			enum intel_display_power_domain domain;
28138241cfbeSJosé Roberto de Souza 
28148241cfbeSJosé Roberto de Souza 			domain = POWER_DOMAIN_TRANSCODER(trans);
28158241cfbeSJosé Roberto de Souza 			if (!intel_display_power_is_enabled(dev_priv, domain))
28168241cfbeSJosé Roberto de Souza 				continue;
28178241cfbeSJosé Roberto de Souza 
28188241cfbeSJosé Roberto de Souza 			intel_uncore_write(uncore, TRANS_PSR_IMR(trans), 0xffffffff);
28198241cfbeSJosé Roberto de Souza 			intel_uncore_write(uncore, TRANS_PSR_IIR(trans), 0xffffffff);
28208241cfbeSJosé Roberto de Souza 		}
28218241cfbeSJosé Roberto de Souza 	} else {
2822f0818984STvrtko Ursulin 		intel_uncore_write(uncore, EDP_PSR_IMR, 0xffffffff);
2823f0818984STvrtko Ursulin 		intel_uncore_write(uncore, EDP_PSR_IIR, 0xffffffff);
28248241cfbeSJosé Roberto de Souza 	}
282562819dfdSJosé Roberto de Souza 
282651951ae7SMika Kuoppala 	for_each_pipe(dev_priv, pipe)
282751951ae7SMika Kuoppala 		if (intel_display_power_is_enabled(dev_priv,
282851951ae7SMika Kuoppala 						   POWER_DOMAIN_PIPE(pipe)))
2829b16b2a2fSPaulo Zanoni 			GEN8_IRQ_RESET_NDX(uncore, DE_PIPE, pipe);
283051951ae7SMika Kuoppala 
2831b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN8_DE_PORT_);
2832b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN8_DE_MISC_);
2833b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN11_DE_HPD_);
283431604222SAnusha Srivatsa 
283529b43ae2SRodrigo Vivi 	if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
2836b16b2a2fSPaulo Zanoni 		GEN3_IRQ_RESET(uncore, SDE);
283751951ae7SMika Kuoppala }
283851951ae7SMika Kuoppala 
2839a3265d85SMatt Roper static void gen11_irq_reset(struct drm_i915_private *dev_priv)
2840a3265d85SMatt Roper {
2841a3265d85SMatt Roper 	struct intel_uncore *uncore = &dev_priv->uncore;
2842a3265d85SMatt Roper 
2843a3265d85SMatt Roper 	gen11_master_intr_disable(dev_priv->uncore.regs);
2844a3265d85SMatt Roper 
2845a3265d85SMatt Roper 	gen11_gt_irq_reset(&dev_priv->gt);
2846a3265d85SMatt Roper 	gen11_display_irq_reset(dev_priv);
2847a3265d85SMatt Roper 
2848a3265d85SMatt Roper 	GEN3_IRQ_RESET(uncore, GEN11_GU_MISC_);
2849a3265d85SMatt Roper 	GEN3_IRQ_RESET(uncore, GEN8_PCU_);
2850a3265d85SMatt Roper }
2851a3265d85SMatt Roper 
28524c6c03beSDamien Lespiau void gen8_irq_power_well_post_enable(struct drm_i915_private *dev_priv,
2853001bd2cbSImre Deak 				     u8 pipe_mask)
2854d49bdb0eSPaulo Zanoni {
2855b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
2856b16b2a2fSPaulo Zanoni 
2857a9c287c9SJani Nikula 	u32 extra_ier = GEN8_PIPE_VBLANK | GEN8_PIPE_FIFO_UNDERRUN;
28586831f3e3SVille Syrjälä 	enum pipe pipe;
2859d49bdb0eSPaulo Zanoni 
286013321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
28619dfe2e3aSImre Deak 
28629dfe2e3aSImre Deak 	if (!intel_irqs_enabled(dev_priv)) {
28639dfe2e3aSImre Deak 		spin_unlock_irq(&dev_priv->irq_lock);
28649dfe2e3aSImre Deak 		return;
28659dfe2e3aSImre Deak 	}
28669dfe2e3aSImre Deak 
28676831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
2868b16b2a2fSPaulo Zanoni 		GEN8_IRQ_INIT_NDX(uncore, DE_PIPE, pipe,
28696831f3e3SVille Syrjälä 				  dev_priv->de_irq_mask[pipe],
28706831f3e3SVille Syrjälä 				  ~dev_priv->de_irq_mask[pipe] | extra_ier);
28719dfe2e3aSImre Deak 
287213321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
2873d49bdb0eSPaulo Zanoni }
2874d49bdb0eSPaulo Zanoni 
2875aae8ba84SVille Syrjälä void gen8_irq_power_well_pre_disable(struct drm_i915_private *dev_priv,
2876001bd2cbSImre Deak 				     u8 pipe_mask)
2877aae8ba84SVille Syrjälä {
2878b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
28796831f3e3SVille Syrjälä 	enum pipe pipe;
28806831f3e3SVille Syrjälä 
2881aae8ba84SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
28829dfe2e3aSImre Deak 
28839dfe2e3aSImre Deak 	if (!intel_irqs_enabled(dev_priv)) {
28849dfe2e3aSImre Deak 		spin_unlock_irq(&dev_priv->irq_lock);
28859dfe2e3aSImre Deak 		return;
28869dfe2e3aSImre Deak 	}
28879dfe2e3aSImre Deak 
28886831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
2889b16b2a2fSPaulo Zanoni 		GEN8_IRQ_RESET_NDX(uncore, DE_PIPE, pipe);
28909dfe2e3aSImre Deak 
2891aae8ba84SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
2892aae8ba84SVille Syrjälä 
2893aae8ba84SVille Syrjälä 	/* make sure we're done processing display irqs */
2894315ca4c4SVille Syrjälä 	intel_synchronize_irq(dev_priv);
2895aae8ba84SVille Syrjälä }
2896aae8ba84SVille Syrjälä 
2897b318b824SVille Syrjälä static void cherryview_irq_reset(struct drm_i915_private *dev_priv)
289843f328d7SVille Syrjälä {
2899b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
290043f328d7SVille Syrjälä 
290143f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
290243f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
290343f328d7SVille Syrjälä 
2904cf1c97dcSAndi Shyti 	gen8_gt_irq_reset(&dev_priv->gt);
290543f328d7SVille Syrjälä 
2906b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN8_PCU_);
290743f328d7SVille Syrjälä 
2908ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
29099918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
291070591a41SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
2911ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
291243f328d7SVille Syrjälä }
291343f328d7SVille Syrjälä 
291491d14251STvrtko Ursulin static u32 intel_hpd_enabled_irqs(struct drm_i915_private *dev_priv,
291587a02106SVille Syrjälä 				  const u32 hpd[HPD_NUM_PINS])
291687a02106SVille Syrjälä {
291787a02106SVille Syrjälä 	struct intel_encoder *encoder;
291887a02106SVille Syrjälä 	u32 enabled_irqs = 0;
291987a02106SVille Syrjälä 
292091c8a326SChris Wilson 	for_each_intel_encoder(&dev_priv->drm, encoder)
292187a02106SVille Syrjälä 		if (dev_priv->hotplug.stats[encoder->hpd_pin].state == HPD_ENABLED)
292287a02106SVille Syrjälä 			enabled_irqs |= hpd[encoder->hpd_pin];
292387a02106SVille Syrjälä 
292487a02106SVille Syrjälä 	return enabled_irqs;
292587a02106SVille Syrjälä }
292687a02106SVille Syrjälä 
29271a56b1a2SImre Deak static void ibx_hpd_detection_setup(struct drm_i915_private *dev_priv)
29281a56b1a2SImre Deak {
29291a56b1a2SImre Deak 	u32 hotplug;
29301a56b1a2SImre Deak 
29311a56b1a2SImre Deak 	/*
29321a56b1a2SImre Deak 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
29331a56b1a2SImre Deak 	 * duration to 2ms (which is the minimum in the Display Port spec).
29341a56b1a2SImre Deak 	 * The pulse duration bits are reserved on LPT+.
29351a56b1a2SImre Deak 	 */
29361a56b1a2SImre Deak 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
29371a56b1a2SImre Deak 	hotplug &= ~(PORTB_PULSE_DURATION_MASK |
29381a56b1a2SImre Deak 		     PORTC_PULSE_DURATION_MASK |
29391a56b1a2SImre Deak 		     PORTD_PULSE_DURATION_MASK);
29401a56b1a2SImre Deak 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
29411a56b1a2SImre Deak 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
29421a56b1a2SImre Deak 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
29431a56b1a2SImre Deak 	/*
29441a56b1a2SImre Deak 	 * When CPU and PCH are on the same package, port A
29451a56b1a2SImre Deak 	 * HPD must be enabled in both north and south.
29461a56b1a2SImre Deak 	 */
29471a56b1a2SImre Deak 	if (HAS_PCH_LPT_LP(dev_priv))
29481a56b1a2SImre Deak 		hotplug |= PORTA_HOTPLUG_ENABLE;
29491a56b1a2SImre Deak 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
29501a56b1a2SImre Deak }
29511a56b1a2SImre Deak 
295291d14251STvrtko Ursulin static void ibx_hpd_irq_setup(struct drm_i915_private *dev_priv)
295382a28bcfSDaniel Vetter {
29541a56b1a2SImre Deak 	u32 hotplug_irqs, enabled_irqs;
295582a28bcfSDaniel Vetter 
295691d14251STvrtko Ursulin 	if (HAS_PCH_IBX(dev_priv)) {
2957fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
295891d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ibx);
295982a28bcfSDaniel Vetter 	} else {
2960fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
296191d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_cpt);
296282a28bcfSDaniel Vetter 	}
296382a28bcfSDaniel Vetter 
2964fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
296582a28bcfSDaniel Vetter 
29661a56b1a2SImre Deak 	ibx_hpd_detection_setup(dev_priv);
29676dbf30ceSVille Syrjälä }
296826951cafSXiong Zhang 
296952dfdba0SLucas De Marchi static void icp_hpd_detection_setup(struct drm_i915_private *dev_priv,
297052dfdba0SLucas De Marchi 				    u32 ddi_hotplug_enable_mask,
297152dfdba0SLucas De Marchi 				    u32 tc_hotplug_enable_mask)
297231604222SAnusha Srivatsa {
297331604222SAnusha Srivatsa 	u32 hotplug;
297431604222SAnusha Srivatsa 
297531604222SAnusha Srivatsa 	hotplug = I915_READ(SHOTPLUG_CTL_DDI);
297652dfdba0SLucas De Marchi 	hotplug |= ddi_hotplug_enable_mask;
297731604222SAnusha Srivatsa 	I915_WRITE(SHOTPLUG_CTL_DDI, hotplug);
297831604222SAnusha Srivatsa 
29798ef7e340SMatt Roper 	if (tc_hotplug_enable_mask) {
298031604222SAnusha Srivatsa 		hotplug = I915_READ(SHOTPLUG_CTL_TC);
298152dfdba0SLucas De Marchi 		hotplug |= tc_hotplug_enable_mask;
298231604222SAnusha Srivatsa 		I915_WRITE(SHOTPLUG_CTL_TC, hotplug);
298331604222SAnusha Srivatsa 	}
29848ef7e340SMatt Roper }
298531604222SAnusha Srivatsa 
298640e98130SLucas De Marchi static void icp_hpd_irq_setup(struct drm_i915_private *dev_priv,
298740e98130SLucas De Marchi 			      u32 sde_ddi_mask, u32 sde_tc_mask,
298840e98130SLucas De Marchi 			      u32 ddi_enable_mask, u32 tc_enable_mask,
298940e98130SLucas De Marchi 			      const u32 *pins)
299031604222SAnusha Srivatsa {
299131604222SAnusha Srivatsa 	u32 hotplug_irqs, enabled_irqs;
299231604222SAnusha Srivatsa 
299340e98130SLucas De Marchi 	hotplug_irqs = sde_ddi_mask | sde_tc_mask;
299440e98130SLucas De Marchi 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, pins);
299531604222SAnusha Srivatsa 
2996f49108d0SMatt Roper 	I915_WRITE(SHPD_FILTER_CNT, SHPD_FILTER_CNT_500_ADJ);
2997f49108d0SMatt Roper 
299831604222SAnusha Srivatsa 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
299931604222SAnusha Srivatsa 
300040e98130SLucas De Marchi 	icp_hpd_detection_setup(dev_priv, ddi_enable_mask, tc_enable_mask);
300152dfdba0SLucas De Marchi }
300252dfdba0SLucas De Marchi 
300340e98130SLucas De Marchi /*
300440e98130SLucas De Marchi  * EHL doesn't need most of gen11_hpd_irq_setup, it's handling only the
300540e98130SLucas De Marchi  * equivalent of SDE.
300640e98130SLucas De Marchi  */
30078ef7e340SMatt Roper static void mcc_hpd_irq_setup(struct drm_i915_private *dev_priv)
30088ef7e340SMatt Roper {
300940e98130SLucas De Marchi 	icp_hpd_irq_setup(dev_priv,
301053448aedSVivek Kasireddy 			  SDE_DDI_MASK_ICP, SDE_TC_HOTPLUG_ICP(PORT_TC1),
301153448aedSVivek Kasireddy 			  ICP_DDI_HPD_ENABLE_MASK, ICP_TC_HPD_ENABLE(PORT_TC1),
3012d09ad3e7SMatt Roper 			  hpd_icp);
301331604222SAnusha Srivatsa }
301431604222SAnusha Srivatsa 
3015943682e3SMatt Roper /*
3016943682e3SMatt Roper  * JSP behaves exactly the same as MCC above except that port C is mapped to
3017943682e3SMatt Roper  * the DDI-C pins instead of the TC1 pins.  This means we should follow TGP's
3018943682e3SMatt Roper  * masks & tables rather than ICP's masks & tables.
3019943682e3SMatt Roper  */
3020943682e3SMatt Roper static void jsp_hpd_irq_setup(struct drm_i915_private *dev_priv)
3021943682e3SMatt Roper {
3022943682e3SMatt Roper 	icp_hpd_irq_setup(dev_priv,
3023943682e3SMatt Roper 			  SDE_DDI_MASK_TGP, 0,
3024943682e3SMatt Roper 			  TGP_DDI_HPD_ENABLE_MASK, 0,
3025943682e3SMatt Roper 			  hpd_tgp);
3026943682e3SMatt Roper }
3027943682e3SMatt Roper 
3028121e758eSDhinakaran Pandiyan static void gen11_hpd_detection_setup(struct drm_i915_private *dev_priv)
3029121e758eSDhinakaran Pandiyan {
3030121e758eSDhinakaran Pandiyan 	u32 hotplug;
3031121e758eSDhinakaran Pandiyan 
3032121e758eSDhinakaran Pandiyan 	hotplug = I915_READ(GEN11_TC_HOTPLUG_CTL);
3033121e758eSDhinakaran Pandiyan 	hotplug |= GEN11_HOTPLUG_CTL_ENABLE(PORT_TC1) |
3034121e758eSDhinakaran Pandiyan 		   GEN11_HOTPLUG_CTL_ENABLE(PORT_TC2) |
3035121e758eSDhinakaran Pandiyan 		   GEN11_HOTPLUG_CTL_ENABLE(PORT_TC3) |
3036121e758eSDhinakaran Pandiyan 		   GEN11_HOTPLUG_CTL_ENABLE(PORT_TC4);
3037121e758eSDhinakaran Pandiyan 	I915_WRITE(GEN11_TC_HOTPLUG_CTL, hotplug);
3038b796b971SDhinakaran Pandiyan 
3039b796b971SDhinakaran Pandiyan 	hotplug = I915_READ(GEN11_TBT_HOTPLUG_CTL);
3040b796b971SDhinakaran Pandiyan 	hotplug |= GEN11_HOTPLUG_CTL_ENABLE(PORT_TC1) |
3041b796b971SDhinakaran Pandiyan 		   GEN11_HOTPLUG_CTL_ENABLE(PORT_TC2) |
3042b796b971SDhinakaran Pandiyan 		   GEN11_HOTPLUG_CTL_ENABLE(PORT_TC3) |
3043b796b971SDhinakaran Pandiyan 		   GEN11_HOTPLUG_CTL_ENABLE(PORT_TC4);
3044b796b971SDhinakaran Pandiyan 	I915_WRITE(GEN11_TBT_HOTPLUG_CTL, hotplug);
3045121e758eSDhinakaran Pandiyan }
3046121e758eSDhinakaran Pandiyan 
3047121e758eSDhinakaran Pandiyan static void gen11_hpd_irq_setup(struct drm_i915_private *dev_priv)
3048121e758eSDhinakaran Pandiyan {
3049121e758eSDhinakaran Pandiyan 	u32 hotplug_irqs, enabled_irqs;
305048ef15d3SJosé Roberto de Souza 	const u32 *hpd;
3051121e758eSDhinakaran Pandiyan 	u32 val;
3052121e758eSDhinakaran Pandiyan 
305348ef15d3SJosé Roberto de Souza 	hpd = INTEL_GEN(dev_priv) >= 12 ? hpd_gen12 : hpd_gen11;
305448ef15d3SJosé Roberto de Souza 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd);
3055b796b971SDhinakaran Pandiyan 	hotplug_irqs = GEN11_DE_TC_HOTPLUG_MASK | GEN11_DE_TBT_HOTPLUG_MASK;
3056121e758eSDhinakaran Pandiyan 
3057121e758eSDhinakaran Pandiyan 	val = I915_READ(GEN11_DE_HPD_IMR);
3058121e758eSDhinakaran Pandiyan 	val &= ~hotplug_irqs;
3059121e758eSDhinakaran Pandiyan 	I915_WRITE(GEN11_DE_HPD_IMR, val);
3060121e758eSDhinakaran Pandiyan 	POSTING_READ(GEN11_DE_HPD_IMR);
3061121e758eSDhinakaran Pandiyan 
3062121e758eSDhinakaran Pandiyan 	gen11_hpd_detection_setup(dev_priv);
306331604222SAnusha Srivatsa 
306452dfdba0SLucas De Marchi 	if (INTEL_PCH_TYPE(dev_priv) >= PCH_TGP)
306540e98130SLucas De Marchi 		icp_hpd_irq_setup(dev_priv, SDE_DDI_MASK_TGP, SDE_TC_MASK_TGP,
306640e98130SLucas De Marchi 				  TGP_DDI_HPD_ENABLE_MASK,
306740e98130SLucas De Marchi 				  TGP_TC_HPD_ENABLE_MASK, hpd_tgp);
306852dfdba0SLucas De Marchi 	else if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
306940e98130SLucas De Marchi 		icp_hpd_irq_setup(dev_priv, SDE_DDI_MASK_ICP, SDE_TC_MASK_ICP,
307040e98130SLucas De Marchi 				  ICP_DDI_HPD_ENABLE_MASK,
307140e98130SLucas De Marchi 				  ICP_TC_HPD_ENABLE_MASK, hpd_icp);
3072121e758eSDhinakaran Pandiyan }
3073121e758eSDhinakaran Pandiyan 
30742a57d9ccSImre Deak static void spt_hpd_detection_setup(struct drm_i915_private *dev_priv)
30752a57d9ccSImre Deak {
30763b92e263SRodrigo Vivi 	u32 val, hotplug;
30773b92e263SRodrigo Vivi 
30783b92e263SRodrigo Vivi 	/* Display WA #1179 WaHardHangonHotPlug: cnp */
30793b92e263SRodrigo Vivi 	if (HAS_PCH_CNP(dev_priv)) {
30803b92e263SRodrigo Vivi 		val = I915_READ(SOUTH_CHICKEN1);
30813b92e263SRodrigo Vivi 		val &= ~CHASSIS_CLK_REQ_DURATION_MASK;
30823b92e263SRodrigo Vivi 		val |= CHASSIS_CLK_REQ_DURATION(0xf);
30833b92e263SRodrigo Vivi 		I915_WRITE(SOUTH_CHICKEN1, val);
30843b92e263SRodrigo Vivi 	}
30852a57d9ccSImre Deak 
30862a57d9ccSImre Deak 	/* Enable digital hotplug on the PCH */
30872a57d9ccSImre Deak 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
30882a57d9ccSImre Deak 	hotplug |= PORTA_HOTPLUG_ENABLE |
30892a57d9ccSImre Deak 		   PORTB_HOTPLUG_ENABLE |
30902a57d9ccSImre Deak 		   PORTC_HOTPLUG_ENABLE |
30912a57d9ccSImre Deak 		   PORTD_HOTPLUG_ENABLE;
30922a57d9ccSImre Deak 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
30932a57d9ccSImre Deak 
30942a57d9ccSImre Deak 	hotplug = I915_READ(PCH_PORT_HOTPLUG2);
30952a57d9ccSImre Deak 	hotplug |= PORTE_HOTPLUG_ENABLE;
30962a57d9ccSImre Deak 	I915_WRITE(PCH_PORT_HOTPLUG2, hotplug);
30972a57d9ccSImre Deak }
30982a57d9ccSImre Deak 
309991d14251STvrtko Ursulin static void spt_hpd_irq_setup(struct drm_i915_private *dev_priv)
31006dbf30ceSVille Syrjälä {
31012a57d9ccSImre Deak 	u32 hotplug_irqs, enabled_irqs;
31026dbf30ceSVille Syrjälä 
3103f49108d0SMatt Roper 	if (INTEL_PCH_TYPE(dev_priv) >= PCH_CNP)
3104f49108d0SMatt Roper 		I915_WRITE(SHPD_FILTER_CNT, SHPD_FILTER_CNT_500_ADJ);
3105f49108d0SMatt Roper 
31066dbf30ceSVille Syrjälä 	hotplug_irqs = SDE_HOTPLUG_MASK_SPT;
310791d14251STvrtko Ursulin 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_spt);
31086dbf30ceSVille Syrjälä 
31096dbf30ceSVille Syrjälä 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
31106dbf30ceSVille Syrjälä 
31112a57d9ccSImre Deak 	spt_hpd_detection_setup(dev_priv);
311226951cafSXiong Zhang }
31137fe0b973SKeith Packard 
31141a56b1a2SImre Deak static void ilk_hpd_detection_setup(struct drm_i915_private *dev_priv)
31151a56b1a2SImre Deak {
31161a56b1a2SImre Deak 	u32 hotplug;
31171a56b1a2SImre Deak 
31181a56b1a2SImre Deak 	/*
31191a56b1a2SImre Deak 	 * Enable digital hotplug on the CPU, and configure the DP short pulse
31201a56b1a2SImre Deak 	 * duration to 2ms (which is the minimum in the Display Port spec)
31211a56b1a2SImre Deak 	 * The pulse duration bits are reserved on HSW+.
31221a56b1a2SImre Deak 	 */
31231a56b1a2SImre Deak 	hotplug = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
31241a56b1a2SImre Deak 	hotplug &= ~DIGITAL_PORTA_PULSE_DURATION_MASK;
31251a56b1a2SImre Deak 	hotplug |= DIGITAL_PORTA_HOTPLUG_ENABLE |
31261a56b1a2SImre Deak 		   DIGITAL_PORTA_PULSE_DURATION_2ms;
31271a56b1a2SImre Deak 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, hotplug);
31281a56b1a2SImre Deak }
31291a56b1a2SImre Deak 
313091d14251STvrtko Ursulin static void ilk_hpd_irq_setup(struct drm_i915_private *dev_priv)
3131e4ce95aaSVille Syrjälä {
31321a56b1a2SImre Deak 	u32 hotplug_irqs, enabled_irqs;
3133e4ce95aaSVille Syrjälä 
313491d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 8) {
31353a3b3c7dSVille Syrjälä 		hotplug_irqs = GEN8_PORT_DP_A_HOTPLUG;
313691d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_bdw);
31373a3b3c7dSVille Syrjälä 
31383a3b3c7dSVille Syrjälä 		bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
313991d14251STvrtko Ursulin 	} else if (INTEL_GEN(dev_priv) >= 7) {
314023bb4cb5SVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG_IVB;
314191d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ivb);
31423a3b3c7dSVille Syrjälä 
31433a3b3c7dSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
314423bb4cb5SVille Syrjälä 	} else {
3145e4ce95aaSVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG;
314691d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ilk);
3147e4ce95aaSVille Syrjälä 
3148e4ce95aaSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
31493a3b3c7dSVille Syrjälä 	}
3150e4ce95aaSVille Syrjälä 
31511a56b1a2SImre Deak 	ilk_hpd_detection_setup(dev_priv);
3152e4ce95aaSVille Syrjälä 
315391d14251STvrtko Ursulin 	ibx_hpd_irq_setup(dev_priv);
3154e4ce95aaSVille Syrjälä }
3155e4ce95aaSVille Syrjälä 
31562a57d9ccSImre Deak static void __bxt_hpd_detection_setup(struct drm_i915_private *dev_priv,
31572a57d9ccSImre Deak 				      u32 enabled_irqs)
3158e0a20ad7SShashank Sharma {
31592a57d9ccSImre Deak 	u32 hotplug;
3160e0a20ad7SShashank Sharma 
3161a52bb15bSVille Syrjälä 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
31622a57d9ccSImre Deak 	hotplug |= PORTA_HOTPLUG_ENABLE |
31632a57d9ccSImre Deak 		   PORTB_HOTPLUG_ENABLE |
31642a57d9ccSImre Deak 		   PORTC_HOTPLUG_ENABLE;
3165d252bf68SShubhangi Shrivastava 
3166d252bf68SShubhangi Shrivastava 	DRM_DEBUG_KMS("Invert bit setting: hp_ctl:%x hp_port:%x\n",
3167d252bf68SShubhangi Shrivastava 		      hotplug, enabled_irqs);
3168d252bf68SShubhangi Shrivastava 	hotplug &= ~BXT_DDI_HPD_INVERT_MASK;
3169d252bf68SShubhangi Shrivastava 
3170d252bf68SShubhangi Shrivastava 	/*
3171d252bf68SShubhangi Shrivastava 	 * For BXT invert bit has to be set based on AOB design
3172d252bf68SShubhangi Shrivastava 	 * for HPD detection logic, update it based on VBT fields.
3173d252bf68SShubhangi Shrivastava 	 */
3174d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIA) &&
3175d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_A))
3176d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIA_HPD_INVERT;
3177d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIB) &&
3178d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_B))
3179d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIB_HPD_INVERT;
3180d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIC) &&
3181d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_C))
3182d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIC_HPD_INVERT;
3183d252bf68SShubhangi Shrivastava 
3184a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
3185e0a20ad7SShashank Sharma }
3186e0a20ad7SShashank Sharma 
31872a57d9ccSImre Deak static void bxt_hpd_detection_setup(struct drm_i915_private *dev_priv)
31882a57d9ccSImre Deak {
31892a57d9ccSImre Deak 	__bxt_hpd_detection_setup(dev_priv, BXT_DE_PORT_HOTPLUG_MASK);
31902a57d9ccSImre Deak }
31912a57d9ccSImre Deak 
31922a57d9ccSImre Deak static void bxt_hpd_irq_setup(struct drm_i915_private *dev_priv)
31932a57d9ccSImre Deak {
31942a57d9ccSImre Deak 	u32 hotplug_irqs, enabled_irqs;
31952a57d9ccSImre Deak 
31962a57d9ccSImre Deak 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_bxt);
31972a57d9ccSImre Deak 	hotplug_irqs = BXT_DE_PORT_HOTPLUG_MASK;
31982a57d9ccSImre Deak 
31992a57d9ccSImre Deak 	bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
32002a57d9ccSImre Deak 
32012a57d9ccSImre Deak 	__bxt_hpd_detection_setup(dev_priv, enabled_irqs);
32022a57d9ccSImre Deak }
32032a57d9ccSImre Deak 
3204b318b824SVille Syrjälä static void ibx_irq_postinstall(struct drm_i915_private *dev_priv)
3205d46da437SPaulo Zanoni {
320682a28bcfSDaniel Vetter 	u32 mask;
3207d46da437SPaulo Zanoni 
32086e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
3209692a04cfSDaniel Vetter 		return;
3210692a04cfSDaniel Vetter 
32116e266956STvrtko Ursulin 	if (HAS_PCH_IBX(dev_priv))
32125c673b60SDaniel Vetter 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
32134ebc6509SDhinakaran Pandiyan 	else if (HAS_PCH_CPT(dev_priv) || HAS_PCH_LPT(dev_priv))
32145c673b60SDaniel Vetter 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
32154ebc6509SDhinakaran Pandiyan 	else
32164ebc6509SDhinakaran Pandiyan 		mask = SDE_GMBUS_CPT;
32178664281bSPaulo Zanoni 
321865f42cdcSPaulo Zanoni 	gen3_assert_iir_is_zero(&dev_priv->uncore, SDEIIR);
3219d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
32202a57d9ccSImre Deak 
32212a57d9ccSImre Deak 	if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
32222a57d9ccSImre Deak 	    HAS_PCH_LPT(dev_priv))
32231a56b1a2SImre Deak 		ibx_hpd_detection_setup(dev_priv);
32242a57d9ccSImre Deak 	else
32252a57d9ccSImre Deak 		spt_hpd_detection_setup(dev_priv);
3226d46da437SPaulo Zanoni }
3227d46da437SPaulo Zanoni 
32289eae5e27SLucas De Marchi static void ilk_irq_postinstall(struct drm_i915_private *dev_priv)
3229036a4a7dSZhenyu Wang {
3230b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
32318e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
32328e76f8dcSPaulo Zanoni 
3233b243f530STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 7) {
32348e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
3235842ebf7aSVille Syrjälä 				DE_PCH_EVENT_IVB | DE_AUX_CHANNEL_A_IVB);
32368e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
323723bb4cb5SVille Syrjälä 			      DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB |
323823bb4cb5SVille Syrjälä 			      DE_DP_A_HOTPLUG_IVB);
32398e76f8dcSPaulo Zanoni 	} else {
32408e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3241842ebf7aSVille Syrjälä 				DE_AUX_CHANNEL_A | DE_PIPEB_CRC_DONE |
3242842ebf7aSVille Syrjälä 				DE_PIPEA_CRC_DONE | DE_POISON);
3243e4ce95aaSVille Syrjälä 		extra_mask = (DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
3244e4ce95aaSVille Syrjälä 			      DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN |
3245e4ce95aaSVille Syrjälä 			      DE_DP_A_HOTPLUG);
32468e76f8dcSPaulo Zanoni 	}
3247036a4a7dSZhenyu Wang 
3248fc340442SDaniel Vetter 	if (IS_HASWELL(dev_priv)) {
3249b16b2a2fSPaulo Zanoni 		gen3_assert_iir_is_zero(uncore, EDP_PSR_IIR);
3250fc340442SDaniel Vetter 		display_mask |= DE_EDP_PSR_INT_HSW;
3251fc340442SDaniel Vetter 	}
3252fc340442SDaniel Vetter 
32531ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3254036a4a7dSZhenyu Wang 
3255b318b824SVille Syrjälä 	ibx_irq_pre_postinstall(dev_priv);
3256622364b6SPaulo Zanoni 
3257b16b2a2fSPaulo Zanoni 	GEN3_IRQ_INIT(uncore, DE, dev_priv->irq_mask,
3258b16b2a2fSPaulo Zanoni 		      display_mask | extra_mask);
3259036a4a7dSZhenyu Wang 
3260cf1c97dcSAndi Shyti 	gen5_gt_irq_postinstall(&dev_priv->gt);
3261036a4a7dSZhenyu Wang 
32621a56b1a2SImre Deak 	ilk_hpd_detection_setup(dev_priv);
32631a56b1a2SImre Deak 
3264b318b824SVille Syrjälä 	ibx_irq_postinstall(dev_priv);
32657fe0b973SKeith Packard 
326650a0bc90STvrtko Ursulin 	if (IS_IRONLAKE_M(dev_priv)) {
32676005ce42SDaniel Vetter 		/* Enable PCU event interrupts
32686005ce42SDaniel Vetter 		 *
32696005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
32704bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
32714bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
3272d6207435SDaniel Vetter 		spin_lock_irq(&dev_priv->irq_lock);
3273fbdedaeaSVille Syrjälä 		ilk_enable_display_irq(dev_priv, DE_PCU_EVENT);
3274d6207435SDaniel Vetter 		spin_unlock_irq(&dev_priv->irq_lock);
3275f97108d1SJesse Barnes 	}
3276036a4a7dSZhenyu Wang }
3277036a4a7dSZhenyu Wang 
3278f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3279f8b79e58SImre Deak {
328067520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
3281f8b79e58SImre Deak 
3282f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3283f8b79e58SImre Deak 		return;
3284f8b79e58SImre Deak 
3285f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3286f8b79e58SImre Deak 
3287d6c69803SVille Syrjälä 	if (intel_irqs_enabled(dev_priv)) {
3288d6c69803SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3289ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3290f8b79e58SImre Deak 	}
3291d6c69803SVille Syrjälä }
3292f8b79e58SImre Deak 
3293f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3294f8b79e58SImre Deak {
329567520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
3296f8b79e58SImre Deak 
3297f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3298f8b79e58SImre Deak 		return;
3299f8b79e58SImre Deak 
3300f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3301f8b79e58SImre Deak 
3302950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3303ad22d106SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3304f8b79e58SImre Deak }
3305f8b79e58SImre Deak 
33060e6c9a9eSVille Syrjälä 
3307b318b824SVille Syrjälä static void valleyview_irq_postinstall(struct drm_i915_private *dev_priv)
33080e6c9a9eSVille Syrjälä {
3309cf1c97dcSAndi Shyti 	gen5_gt_irq_postinstall(&dev_priv->gt);
33107e231dbeSJesse Barnes 
3311ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
33129918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3313ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3314ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3315ad22d106SVille Syrjälä 
33167e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
331734c7b8a7SVille Syrjälä 	POSTING_READ(VLV_MASTER_IER);
331820afbda2SDaniel Vetter }
331920afbda2SDaniel Vetter 
3320abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3321abd58f01SBen Widawsky {
3322b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
3323b16b2a2fSPaulo Zanoni 
3324a9c287c9SJani Nikula 	u32 de_pipe_masked = GEN8_PIPE_CDCLK_CRC_DONE;
3325a9c287c9SJani Nikula 	u32 de_pipe_enables;
33263a3b3c7dSVille Syrjälä 	u32 de_port_masked = GEN8_AUX_CHANNEL_A;
33273a3b3c7dSVille Syrjälä 	u32 de_port_enables;
3328df0d28c1SDhinakaran Pandiyan 	u32 de_misc_masked = GEN8_DE_EDP_PSR;
33293a3b3c7dSVille Syrjälä 	enum pipe pipe;
3330770de83dSDamien Lespiau 
3331df0d28c1SDhinakaran Pandiyan 	if (INTEL_GEN(dev_priv) <= 10)
3332df0d28c1SDhinakaran Pandiyan 		de_misc_masked |= GEN8_DE_MISC_GSE;
3333df0d28c1SDhinakaran Pandiyan 
3334bca2bf2aSPandiyan, Dhinakaran 	if (INTEL_GEN(dev_priv) >= 9) {
3335842ebf7aSVille Syrjälä 		de_pipe_masked |= GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
33363a3b3c7dSVille Syrjälä 		de_port_masked |= GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
333788e04703SJesse Barnes 				  GEN9_AUX_CHANNEL_D;
3338cc3f90f0SAnder Conselvan de Oliveira 		if (IS_GEN9_LP(dev_priv))
33393a3b3c7dSVille Syrjälä 			de_port_masked |= BXT_DE_PORT_GMBUS;
33403a3b3c7dSVille Syrjälä 	} else {
3341842ebf7aSVille Syrjälä 		de_pipe_masked |= GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
33423a3b3c7dSVille Syrjälä 	}
3343770de83dSDamien Lespiau 
3344bb187e93SJames Ausmus 	if (INTEL_GEN(dev_priv) >= 11)
3345bb187e93SJames Ausmus 		de_port_masked |= ICL_AUX_CHANNEL_E;
3346bb187e93SJames Ausmus 
33479bb635d9SDhinakaran Pandiyan 	if (IS_CNL_WITH_PORT_F(dev_priv) || INTEL_GEN(dev_priv) >= 11)
3348a324fcacSRodrigo Vivi 		de_port_masked |= CNL_AUX_CHANNEL_F;
3349a324fcacSRodrigo Vivi 
3350770de83dSDamien Lespiau 	de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
3351770de83dSDamien Lespiau 					   GEN8_PIPE_FIFO_UNDERRUN;
3352770de83dSDamien Lespiau 
33533a3b3c7dSVille Syrjälä 	de_port_enables = de_port_masked;
3354cc3f90f0SAnder Conselvan de Oliveira 	if (IS_GEN9_LP(dev_priv))
3355a52bb15bSVille Syrjälä 		de_port_enables |= BXT_DE_PORT_HOTPLUG_MASK;
3356a52bb15bSVille Syrjälä 	else if (IS_BROADWELL(dev_priv))
33573a3b3c7dSVille Syrjälä 		de_port_enables |= GEN8_PORT_DP_A_HOTPLUG;
33583a3b3c7dSVille Syrjälä 
33598241cfbeSJosé Roberto de Souza 	if (INTEL_GEN(dev_priv) >= 12) {
33608241cfbeSJosé Roberto de Souza 		enum transcoder trans;
33618241cfbeSJosé Roberto de Souza 
33628241cfbeSJosé Roberto de Souza 		for (trans = TRANSCODER_A; trans <= TRANSCODER_D; trans++) {
33638241cfbeSJosé Roberto de Souza 			enum intel_display_power_domain domain;
33648241cfbeSJosé Roberto de Souza 
33658241cfbeSJosé Roberto de Souza 			domain = POWER_DOMAIN_TRANSCODER(trans);
33668241cfbeSJosé Roberto de Souza 			if (!intel_display_power_is_enabled(dev_priv, domain))
33678241cfbeSJosé Roberto de Souza 				continue;
33688241cfbeSJosé Roberto de Souza 
33698241cfbeSJosé Roberto de Souza 			gen3_assert_iir_is_zero(uncore, TRANS_PSR_IIR(trans));
33708241cfbeSJosé Roberto de Souza 		}
33718241cfbeSJosé Roberto de Souza 	} else {
3372b16b2a2fSPaulo Zanoni 		gen3_assert_iir_is_zero(uncore, EDP_PSR_IIR);
33738241cfbeSJosé Roberto de Souza 	}
3374e04f7eceSVille Syrjälä 
33750a195c02SMika Kahola 	for_each_pipe(dev_priv, pipe) {
33760a195c02SMika Kahola 		dev_priv->de_irq_mask[pipe] = ~de_pipe_masked;
3377abd58f01SBen Widawsky 
3378f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3379813bde43SPaulo Zanoni 				POWER_DOMAIN_PIPE(pipe)))
3380b16b2a2fSPaulo Zanoni 			GEN8_IRQ_INIT_NDX(uncore, DE_PIPE, pipe,
3381813bde43SPaulo Zanoni 					  dev_priv->de_irq_mask[pipe],
338235079899SPaulo Zanoni 					  de_pipe_enables);
33830a195c02SMika Kahola 	}
3384abd58f01SBen Widawsky 
3385b16b2a2fSPaulo Zanoni 	GEN3_IRQ_INIT(uncore, GEN8_DE_PORT_, ~de_port_masked, de_port_enables);
3386b16b2a2fSPaulo Zanoni 	GEN3_IRQ_INIT(uncore, GEN8_DE_MISC_, ~de_misc_masked, de_misc_masked);
33872a57d9ccSImre Deak 
3388121e758eSDhinakaran Pandiyan 	if (INTEL_GEN(dev_priv) >= 11) {
3389121e758eSDhinakaran Pandiyan 		u32 de_hpd_masked = 0;
3390b796b971SDhinakaran Pandiyan 		u32 de_hpd_enables = GEN11_DE_TC_HOTPLUG_MASK |
3391b796b971SDhinakaran Pandiyan 				     GEN11_DE_TBT_HOTPLUG_MASK;
3392121e758eSDhinakaran Pandiyan 
3393b16b2a2fSPaulo Zanoni 		GEN3_IRQ_INIT(uncore, GEN11_DE_HPD_, ~de_hpd_masked,
3394b16b2a2fSPaulo Zanoni 			      de_hpd_enables);
3395121e758eSDhinakaran Pandiyan 		gen11_hpd_detection_setup(dev_priv);
3396121e758eSDhinakaran Pandiyan 	} else if (IS_GEN9_LP(dev_priv)) {
33972a57d9ccSImre Deak 		bxt_hpd_detection_setup(dev_priv);
3398121e758eSDhinakaran Pandiyan 	} else if (IS_BROADWELL(dev_priv)) {
33991a56b1a2SImre Deak 		ilk_hpd_detection_setup(dev_priv);
3400abd58f01SBen Widawsky 	}
3401121e758eSDhinakaran Pandiyan }
3402abd58f01SBen Widawsky 
3403b318b824SVille Syrjälä static void gen8_irq_postinstall(struct drm_i915_private *dev_priv)
3404abd58f01SBen Widawsky {
34056e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
3406b318b824SVille Syrjälä 		ibx_irq_pre_postinstall(dev_priv);
3407622364b6SPaulo Zanoni 
3408cf1c97dcSAndi Shyti 	gen8_gt_irq_postinstall(&dev_priv->gt);
3409abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3410abd58f01SBen Widawsky 
34116e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
3412b318b824SVille Syrjälä 		ibx_irq_postinstall(dev_priv);
3413abd58f01SBen Widawsky 
341425286aacSDaniele Ceraolo Spurio 	gen8_master_intr_enable(dev_priv->uncore.regs);
3415abd58f01SBen Widawsky }
3416abd58f01SBen Widawsky 
3417b318b824SVille Syrjälä static void icp_irq_postinstall(struct drm_i915_private *dev_priv)
341831604222SAnusha Srivatsa {
341931604222SAnusha Srivatsa 	u32 mask = SDE_GMBUS_ICP;
342031604222SAnusha Srivatsa 
342131604222SAnusha Srivatsa 	WARN_ON(I915_READ(SDEIER) != 0);
342231604222SAnusha Srivatsa 	I915_WRITE(SDEIER, 0xffffffff);
342331604222SAnusha Srivatsa 	POSTING_READ(SDEIER);
342431604222SAnusha Srivatsa 
342565f42cdcSPaulo Zanoni 	gen3_assert_iir_is_zero(&dev_priv->uncore, SDEIIR);
342631604222SAnusha Srivatsa 	I915_WRITE(SDEIMR, ~mask);
342731604222SAnusha Srivatsa 
342852dfdba0SLucas De Marchi 	if (HAS_PCH_TGP(dev_priv))
342952dfdba0SLucas De Marchi 		icp_hpd_detection_setup(dev_priv, TGP_DDI_HPD_ENABLE_MASK,
343052dfdba0SLucas De Marchi 					TGP_TC_HPD_ENABLE_MASK);
3431e83c4673SVivek Kasireddy 	else if (HAS_PCH_JSP(dev_priv))
34328ef7e340SMatt Roper 		icp_hpd_detection_setup(dev_priv, TGP_DDI_HPD_ENABLE_MASK, 0);
3433e83c4673SVivek Kasireddy 	else if (HAS_PCH_MCC(dev_priv))
3434e83c4673SVivek Kasireddy 		icp_hpd_detection_setup(dev_priv, ICP_DDI_HPD_ENABLE_MASK,
3435e83c4673SVivek Kasireddy 					ICP_TC_HPD_ENABLE(PORT_TC1));
343652dfdba0SLucas De Marchi 	else
343752dfdba0SLucas De Marchi 		icp_hpd_detection_setup(dev_priv, ICP_DDI_HPD_ENABLE_MASK,
343852dfdba0SLucas De Marchi 					ICP_TC_HPD_ENABLE_MASK);
343931604222SAnusha Srivatsa }
344031604222SAnusha Srivatsa 
3441b318b824SVille Syrjälä static void gen11_irq_postinstall(struct drm_i915_private *dev_priv)
344251951ae7SMika Kuoppala {
3443b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
3444df0d28c1SDhinakaran Pandiyan 	u32 gu_misc_masked = GEN11_GU_MISC_GSE;
344551951ae7SMika Kuoppala 
344629b43ae2SRodrigo Vivi 	if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
3447b318b824SVille Syrjälä 		icp_irq_postinstall(dev_priv);
344831604222SAnusha Srivatsa 
34499b77011eSTvrtko Ursulin 	gen11_gt_irq_postinstall(&dev_priv->gt);
345051951ae7SMika Kuoppala 	gen8_de_irq_postinstall(dev_priv);
345151951ae7SMika Kuoppala 
3452b16b2a2fSPaulo Zanoni 	GEN3_IRQ_INIT(uncore, GEN11_GU_MISC_, ~gu_misc_masked, gu_misc_masked);
3453df0d28c1SDhinakaran Pandiyan 
345451951ae7SMika Kuoppala 	I915_WRITE(GEN11_DISPLAY_INT_CTL, GEN11_DISPLAY_IRQ_ENABLE);
345551951ae7SMika Kuoppala 
34569b77011eSTvrtko Ursulin 	gen11_master_intr_enable(uncore->regs);
3457c25f0c6aSDaniele Ceraolo Spurio 	POSTING_READ(GEN11_GFX_MSTR_IRQ);
345851951ae7SMika Kuoppala }
345951951ae7SMika Kuoppala 
3460b318b824SVille Syrjälä static void cherryview_irq_postinstall(struct drm_i915_private *dev_priv)
346143f328d7SVille Syrjälä {
3462cf1c97dcSAndi Shyti 	gen8_gt_irq_postinstall(&dev_priv->gt);
346343f328d7SVille Syrjälä 
3464ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
34659918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3466ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3467ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3468ad22d106SVille Syrjälä 
3469e5328c43SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
347043f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
347143f328d7SVille Syrjälä }
347243f328d7SVille Syrjälä 
3473b318b824SVille Syrjälä static void i8xx_irq_reset(struct drm_i915_private *dev_priv)
3474c2798b19SChris Wilson {
3475b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
3476c2798b19SChris Wilson 
347744d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
347844d9241eSVille Syrjälä 
3479b16b2a2fSPaulo Zanoni 	GEN2_IRQ_RESET(uncore);
3480c2798b19SChris Wilson }
3481c2798b19SChris Wilson 
3482b318b824SVille Syrjälä static void i8xx_irq_postinstall(struct drm_i915_private *dev_priv)
3483c2798b19SChris Wilson {
3484b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
3485e9e9848aSVille Syrjälä 	u16 enable_mask;
3486c2798b19SChris Wilson 
34874f5fd91fSTvrtko Ursulin 	intel_uncore_write16(uncore,
34884f5fd91fSTvrtko Ursulin 			     EMR,
34894f5fd91fSTvrtko Ursulin 			     ~(I915_ERROR_PAGE_TABLE |
3490045cebd2SVille Syrjälä 			       I915_ERROR_MEMORY_REFRESH));
3491c2798b19SChris Wilson 
3492c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
3493c2798b19SChris Wilson 	dev_priv->irq_mask =
3494c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
349516659bc5SVille Syrjälä 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
349616659bc5SVille Syrjälä 		  I915_MASTER_ERROR_INTERRUPT);
3497c2798b19SChris Wilson 
3498e9e9848aSVille Syrjälä 	enable_mask =
3499c2798b19SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3500c2798b19SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
350116659bc5SVille Syrjälä 		I915_MASTER_ERROR_INTERRUPT |
3502e9e9848aSVille Syrjälä 		I915_USER_INTERRUPT;
3503e9e9848aSVille Syrjälä 
3504b16b2a2fSPaulo Zanoni 	GEN2_IRQ_INIT(uncore, dev_priv->irq_mask, enable_mask);
3505c2798b19SChris Wilson 
3506379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3507379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3508d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3509755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3510755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3511d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3512c2798b19SChris Wilson }
3513c2798b19SChris Wilson 
35144f5fd91fSTvrtko Ursulin static void i8xx_error_irq_ack(struct drm_i915_private *i915,
351578c357ddSVille Syrjälä 			       u16 *eir, u16 *eir_stuck)
351678c357ddSVille Syrjälä {
35174f5fd91fSTvrtko Ursulin 	struct intel_uncore *uncore = &i915->uncore;
351878c357ddSVille Syrjälä 	u16 emr;
351978c357ddSVille Syrjälä 
35204f5fd91fSTvrtko Ursulin 	*eir = intel_uncore_read16(uncore, EIR);
352178c357ddSVille Syrjälä 
352278c357ddSVille Syrjälä 	if (*eir)
35234f5fd91fSTvrtko Ursulin 		intel_uncore_write16(uncore, EIR, *eir);
352478c357ddSVille Syrjälä 
35254f5fd91fSTvrtko Ursulin 	*eir_stuck = intel_uncore_read16(uncore, EIR);
352678c357ddSVille Syrjälä 	if (*eir_stuck == 0)
352778c357ddSVille Syrjälä 		return;
352878c357ddSVille Syrjälä 
352978c357ddSVille Syrjälä 	/*
353078c357ddSVille Syrjälä 	 * Toggle all EMR bits to make sure we get an edge
353178c357ddSVille Syrjälä 	 * in the ISR master error bit if we don't clear
353278c357ddSVille Syrjälä 	 * all the EIR bits. Otherwise the edge triggered
353378c357ddSVille Syrjälä 	 * IIR on i965/g4x wouldn't notice that an interrupt
353478c357ddSVille Syrjälä 	 * is still pending. Also some EIR bits can't be
353578c357ddSVille Syrjälä 	 * cleared except by handling the underlying error
353678c357ddSVille Syrjälä 	 * (or by a GPU reset) so we mask any bit that
353778c357ddSVille Syrjälä 	 * remains set.
353878c357ddSVille Syrjälä 	 */
35394f5fd91fSTvrtko Ursulin 	emr = intel_uncore_read16(uncore, EMR);
35404f5fd91fSTvrtko Ursulin 	intel_uncore_write16(uncore, EMR, 0xffff);
35414f5fd91fSTvrtko Ursulin 	intel_uncore_write16(uncore, EMR, emr | *eir_stuck);
354278c357ddSVille Syrjälä }
354378c357ddSVille Syrjälä 
354478c357ddSVille Syrjälä static void i8xx_error_irq_handler(struct drm_i915_private *dev_priv,
354578c357ddSVille Syrjälä 				   u16 eir, u16 eir_stuck)
354678c357ddSVille Syrjälä {
354778c357ddSVille Syrjälä 	DRM_DEBUG("Master Error: EIR 0x%04x\n", eir);
354878c357ddSVille Syrjälä 
354978c357ddSVille Syrjälä 	if (eir_stuck)
355078c357ddSVille Syrjälä 		DRM_DEBUG_DRIVER("EIR stuck: 0x%04x, masked\n", eir_stuck);
355178c357ddSVille Syrjälä }
355278c357ddSVille Syrjälä 
355378c357ddSVille Syrjälä static void i9xx_error_irq_ack(struct drm_i915_private *dev_priv,
355478c357ddSVille Syrjälä 			       u32 *eir, u32 *eir_stuck)
355578c357ddSVille Syrjälä {
355678c357ddSVille Syrjälä 	u32 emr;
355778c357ddSVille Syrjälä 
355878c357ddSVille Syrjälä 	*eir = I915_READ(EIR);
355978c357ddSVille Syrjälä 
356078c357ddSVille Syrjälä 	I915_WRITE(EIR, *eir);
356178c357ddSVille Syrjälä 
356278c357ddSVille Syrjälä 	*eir_stuck = I915_READ(EIR);
356378c357ddSVille Syrjälä 	if (*eir_stuck == 0)
356478c357ddSVille Syrjälä 		return;
356578c357ddSVille Syrjälä 
356678c357ddSVille Syrjälä 	/*
356778c357ddSVille Syrjälä 	 * Toggle all EMR bits to make sure we get an edge
356878c357ddSVille Syrjälä 	 * in the ISR master error bit if we don't clear
356978c357ddSVille Syrjälä 	 * all the EIR bits. Otherwise the edge triggered
357078c357ddSVille Syrjälä 	 * IIR on i965/g4x wouldn't notice that an interrupt
357178c357ddSVille Syrjälä 	 * is still pending. Also some EIR bits can't be
357278c357ddSVille Syrjälä 	 * cleared except by handling the underlying error
357378c357ddSVille Syrjälä 	 * (or by a GPU reset) so we mask any bit that
357478c357ddSVille Syrjälä 	 * remains set.
357578c357ddSVille Syrjälä 	 */
357678c357ddSVille Syrjälä 	emr = I915_READ(EMR);
357778c357ddSVille Syrjälä 	I915_WRITE(EMR, 0xffffffff);
357878c357ddSVille Syrjälä 	I915_WRITE(EMR, emr | *eir_stuck);
357978c357ddSVille Syrjälä }
358078c357ddSVille Syrjälä 
358178c357ddSVille Syrjälä static void i9xx_error_irq_handler(struct drm_i915_private *dev_priv,
358278c357ddSVille Syrjälä 				   u32 eir, u32 eir_stuck)
358378c357ddSVille Syrjälä {
358478c357ddSVille Syrjälä 	DRM_DEBUG("Master Error, EIR 0x%08x\n", eir);
358578c357ddSVille Syrjälä 
358678c357ddSVille Syrjälä 	if (eir_stuck)
358778c357ddSVille Syrjälä 		DRM_DEBUG_DRIVER("EIR stuck: 0x%08x, masked\n", eir_stuck);
358878c357ddSVille Syrjälä }
358978c357ddSVille Syrjälä 
3590ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3591c2798b19SChris Wilson {
3592b318b824SVille Syrjälä 	struct drm_i915_private *dev_priv = arg;
3593af722d28SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
3594c2798b19SChris Wilson 
35952dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
35962dd2a883SImre Deak 		return IRQ_NONE;
35972dd2a883SImre Deak 
35981f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
35999102650fSDaniele Ceraolo Spurio 	disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
36001f814dacSImre Deak 
3601af722d28SVille Syrjälä 	do {
3602af722d28SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
360378c357ddSVille Syrjälä 		u16 eir = 0, eir_stuck = 0;
3604af722d28SVille Syrjälä 		u16 iir;
3605af722d28SVille Syrjälä 
36064f5fd91fSTvrtko Ursulin 		iir = intel_uncore_read16(&dev_priv->uncore, GEN2_IIR);
3607c2798b19SChris Wilson 		if (iir == 0)
3608af722d28SVille Syrjälä 			break;
3609c2798b19SChris Wilson 
3610af722d28SVille Syrjälä 		ret = IRQ_HANDLED;
3611c2798b19SChris Wilson 
3612eb64343cSVille Syrjälä 		/* Call regardless, as some status bits might not be
3613eb64343cSVille Syrjälä 		 * signalled in iir */
3614eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
3615c2798b19SChris Wilson 
361678c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
361778c357ddSVille Syrjälä 			i8xx_error_irq_ack(dev_priv, &eir, &eir_stuck);
361878c357ddSVille Syrjälä 
36194f5fd91fSTvrtko Ursulin 		intel_uncore_write16(&dev_priv->uncore, GEN2_IIR, iir);
3620c2798b19SChris Wilson 
3621c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
362254400257SChris Wilson 			intel_engine_signal_breadcrumbs(dev_priv->engine[RCS0]);
3623c2798b19SChris Wilson 
362478c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
362578c357ddSVille Syrjälä 			i8xx_error_irq_handler(dev_priv, eir, eir_stuck);
3626af722d28SVille Syrjälä 
3627eb64343cSVille Syrjälä 		i8xx_pipestat_irq_handler(dev_priv, iir, pipe_stats);
3628af722d28SVille Syrjälä 	} while (0);
3629c2798b19SChris Wilson 
36309102650fSDaniele Ceraolo Spurio 	enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
36311f814dacSImre Deak 
36321f814dacSImre Deak 	return ret;
3633c2798b19SChris Wilson }
3634c2798b19SChris Wilson 
3635b318b824SVille Syrjälä static void i915_irq_reset(struct drm_i915_private *dev_priv)
3636a266c7d5SChris Wilson {
3637b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
3638a266c7d5SChris Wilson 
363956b857a5STvrtko Ursulin 	if (I915_HAS_HOTPLUG(dev_priv)) {
36400706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
3641a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3642a266c7d5SChris Wilson 	}
3643a266c7d5SChris Wilson 
364444d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
364544d9241eSVille Syrjälä 
3646b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN2_);
3647a266c7d5SChris Wilson }
3648a266c7d5SChris Wilson 
3649b318b824SVille Syrjälä static void i915_irq_postinstall(struct drm_i915_private *dev_priv)
3650a266c7d5SChris Wilson {
3651b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
365238bde180SChris Wilson 	u32 enable_mask;
3653a266c7d5SChris Wilson 
3654045cebd2SVille Syrjälä 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE |
3655045cebd2SVille Syrjälä 			  I915_ERROR_MEMORY_REFRESH));
365638bde180SChris Wilson 
365738bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
365838bde180SChris Wilson 	dev_priv->irq_mask =
365938bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
366038bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
366116659bc5SVille Syrjälä 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
366216659bc5SVille Syrjälä 		  I915_MASTER_ERROR_INTERRUPT);
366338bde180SChris Wilson 
366438bde180SChris Wilson 	enable_mask =
366538bde180SChris Wilson 		I915_ASLE_INTERRUPT |
366638bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
366738bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
366816659bc5SVille Syrjälä 		I915_MASTER_ERROR_INTERRUPT |
366938bde180SChris Wilson 		I915_USER_INTERRUPT;
367038bde180SChris Wilson 
367156b857a5STvrtko Ursulin 	if (I915_HAS_HOTPLUG(dev_priv)) {
3672a266c7d5SChris Wilson 		/* Enable in IER... */
3673a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
3674a266c7d5SChris Wilson 		/* and unmask in IMR */
3675a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
3676a266c7d5SChris Wilson 	}
3677a266c7d5SChris Wilson 
3678b16b2a2fSPaulo Zanoni 	GEN3_IRQ_INIT(uncore, GEN2_, dev_priv->irq_mask, enable_mask);
3679a266c7d5SChris Wilson 
3680379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3681379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3682d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3683755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3684755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3685d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3686379ef82dSDaniel Vetter 
3687c30bb1fdSVille Syrjälä 	i915_enable_asle_pipestat(dev_priv);
368820afbda2SDaniel Vetter }
368920afbda2SDaniel Vetter 
3690ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
3691a266c7d5SChris Wilson {
3692b318b824SVille Syrjälä 	struct drm_i915_private *dev_priv = arg;
3693af722d28SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
3694a266c7d5SChris Wilson 
36952dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
36962dd2a883SImre Deak 		return IRQ_NONE;
36972dd2a883SImre Deak 
36981f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
36999102650fSDaniele Ceraolo Spurio 	disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
37001f814dacSImre Deak 
370138bde180SChris Wilson 	do {
3702eb64343cSVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
370378c357ddSVille Syrjälä 		u32 eir = 0, eir_stuck = 0;
3704af722d28SVille Syrjälä 		u32 hotplug_status = 0;
3705af722d28SVille Syrjälä 		u32 iir;
3706a266c7d5SChris Wilson 
37079d9523d8SPaulo Zanoni 		iir = I915_READ(GEN2_IIR);
3708af722d28SVille Syrjälä 		if (iir == 0)
3709af722d28SVille Syrjälä 			break;
3710af722d28SVille Syrjälä 
3711af722d28SVille Syrjälä 		ret = IRQ_HANDLED;
3712af722d28SVille Syrjälä 
3713af722d28SVille Syrjälä 		if (I915_HAS_HOTPLUG(dev_priv) &&
3714af722d28SVille Syrjälä 		    iir & I915_DISPLAY_PORT_INTERRUPT)
3715af722d28SVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
3716a266c7d5SChris Wilson 
3717eb64343cSVille Syrjälä 		/* Call regardless, as some status bits might not be
3718eb64343cSVille Syrjälä 		 * signalled in iir */
3719eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
3720a266c7d5SChris Wilson 
372178c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
372278c357ddSVille Syrjälä 			i9xx_error_irq_ack(dev_priv, &eir, &eir_stuck);
372378c357ddSVille Syrjälä 
37249d9523d8SPaulo Zanoni 		I915_WRITE(GEN2_IIR, iir);
3725a266c7d5SChris Wilson 
3726a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
372754400257SChris Wilson 			intel_engine_signal_breadcrumbs(dev_priv->engine[RCS0]);
3728a266c7d5SChris Wilson 
372978c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
373078c357ddSVille Syrjälä 			i9xx_error_irq_handler(dev_priv, eir, eir_stuck);
3731a266c7d5SChris Wilson 
3732af722d28SVille Syrjälä 		if (hotplug_status)
3733af722d28SVille Syrjälä 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
3734af722d28SVille Syrjälä 
3735af722d28SVille Syrjälä 		i915_pipestat_irq_handler(dev_priv, iir, pipe_stats);
3736af722d28SVille Syrjälä 	} while (0);
3737a266c7d5SChris Wilson 
37389102650fSDaniele Ceraolo Spurio 	enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
37391f814dacSImre Deak 
3740a266c7d5SChris Wilson 	return ret;
3741a266c7d5SChris Wilson }
3742a266c7d5SChris Wilson 
3743b318b824SVille Syrjälä static void i965_irq_reset(struct drm_i915_private *dev_priv)
3744a266c7d5SChris Wilson {
3745b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
3746a266c7d5SChris Wilson 
37470706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
3748a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3749a266c7d5SChris Wilson 
375044d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
375144d9241eSVille Syrjälä 
3752b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN2_);
3753a266c7d5SChris Wilson }
3754a266c7d5SChris Wilson 
3755b318b824SVille Syrjälä static void i965_irq_postinstall(struct drm_i915_private *dev_priv)
3756a266c7d5SChris Wilson {
3757b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
3758bbba0a97SChris Wilson 	u32 enable_mask;
3759a266c7d5SChris Wilson 	u32 error_mask;
3760a266c7d5SChris Wilson 
3761045cebd2SVille Syrjälä 	/*
3762045cebd2SVille Syrjälä 	 * Enable some error detection, note the instruction error mask
3763045cebd2SVille Syrjälä 	 * bit is reserved, so we leave it masked.
3764045cebd2SVille Syrjälä 	 */
3765045cebd2SVille Syrjälä 	if (IS_G4X(dev_priv)) {
3766045cebd2SVille Syrjälä 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
3767045cebd2SVille Syrjälä 			       GM45_ERROR_MEM_PRIV |
3768045cebd2SVille Syrjälä 			       GM45_ERROR_CP_PRIV |
3769045cebd2SVille Syrjälä 			       I915_ERROR_MEMORY_REFRESH);
3770045cebd2SVille Syrjälä 	} else {
3771045cebd2SVille Syrjälä 		error_mask = ~(I915_ERROR_PAGE_TABLE |
3772045cebd2SVille Syrjälä 			       I915_ERROR_MEMORY_REFRESH);
3773045cebd2SVille Syrjälä 	}
3774045cebd2SVille Syrjälä 	I915_WRITE(EMR, error_mask);
3775045cebd2SVille Syrjälä 
3776a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
3777c30bb1fdSVille Syrjälä 	dev_priv->irq_mask =
3778c30bb1fdSVille Syrjälä 		~(I915_ASLE_INTERRUPT |
3779adca4730SChris Wilson 		  I915_DISPLAY_PORT_INTERRUPT |
3780bbba0a97SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3781bbba0a97SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
378278c357ddSVille Syrjälä 		  I915_MASTER_ERROR_INTERRUPT);
3783bbba0a97SChris Wilson 
3784c30bb1fdSVille Syrjälä 	enable_mask =
3785c30bb1fdSVille Syrjälä 		I915_ASLE_INTERRUPT |
3786c30bb1fdSVille Syrjälä 		I915_DISPLAY_PORT_INTERRUPT |
3787c30bb1fdSVille Syrjälä 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3788c30bb1fdSVille Syrjälä 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
378978c357ddSVille Syrjälä 		I915_MASTER_ERROR_INTERRUPT |
3790c30bb1fdSVille Syrjälä 		I915_USER_INTERRUPT;
3791bbba0a97SChris Wilson 
379291d14251STvrtko Ursulin 	if (IS_G4X(dev_priv))
3793bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
3794a266c7d5SChris Wilson 
3795b16b2a2fSPaulo Zanoni 	GEN3_IRQ_INIT(uncore, GEN2_, dev_priv->irq_mask, enable_mask);
3796c30bb1fdSVille Syrjälä 
3797b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3798b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3799d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3800755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3801755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3802755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3803d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3804a266c7d5SChris Wilson 
380591d14251STvrtko Ursulin 	i915_enable_asle_pipestat(dev_priv);
380620afbda2SDaniel Vetter }
380720afbda2SDaniel Vetter 
380891d14251STvrtko Ursulin static void i915_hpd_irq_setup(struct drm_i915_private *dev_priv)
380920afbda2SDaniel Vetter {
381020afbda2SDaniel Vetter 	u32 hotplug_en;
381120afbda2SDaniel Vetter 
381267520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
3813b5ea2d56SDaniel Vetter 
3814adca4730SChris Wilson 	/* Note HDMI and DP share hotplug bits */
3815e5868a31SEgbert Eich 	/* enable bits are the same for all generations */
381691d14251STvrtko Ursulin 	hotplug_en = intel_hpd_enabled_irqs(dev_priv, hpd_mask_i915);
3817a266c7d5SChris Wilson 	/* Programming the CRT detection parameters tends
3818a266c7d5SChris Wilson 	   to generate a spurious hotplug event about three
3819a266c7d5SChris Wilson 	   seconds later.  So just do it once.
3820a266c7d5SChris Wilson 	*/
382191d14251STvrtko Ursulin 	if (IS_G4X(dev_priv))
3822a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
3823a266c7d5SChris Wilson 	hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
3824a266c7d5SChris Wilson 
3825a266c7d5SChris Wilson 	/* Ignore TV since it's buggy */
38260706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv,
3827f9e3dc78SJani Nikula 					     HOTPLUG_INT_EN_MASK |
3828f9e3dc78SJani Nikula 					     CRT_HOTPLUG_VOLTAGE_COMPARE_MASK |
3829f9e3dc78SJani Nikula 					     CRT_HOTPLUG_ACTIVATION_PERIOD_64,
38300706f17cSEgbert Eich 					     hotplug_en);
3831a266c7d5SChris Wilson }
3832a266c7d5SChris Wilson 
3833ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
3834a266c7d5SChris Wilson {
3835b318b824SVille Syrjälä 	struct drm_i915_private *dev_priv = arg;
3836af722d28SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
3837a266c7d5SChris Wilson 
38382dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
38392dd2a883SImre Deak 		return IRQ_NONE;
38402dd2a883SImre Deak 
38411f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
38429102650fSDaniele Ceraolo Spurio 	disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
38431f814dacSImre Deak 
3844af722d28SVille Syrjälä 	do {
3845eb64343cSVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
384678c357ddSVille Syrjälä 		u32 eir = 0, eir_stuck = 0;
3847af722d28SVille Syrjälä 		u32 hotplug_status = 0;
3848af722d28SVille Syrjälä 		u32 iir;
38492c8ba29fSChris Wilson 
38509d9523d8SPaulo Zanoni 		iir = I915_READ(GEN2_IIR);
3851af722d28SVille Syrjälä 		if (iir == 0)
3852af722d28SVille Syrjälä 			break;
3853af722d28SVille Syrjälä 
3854af722d28SVille Syrjälä 		ret = IRQ_HANDLED;
3855af722d28SVille Syrjälä 
3856af722d28SVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
3857af722d28SVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
3858a266c7d5SChris Wilson 
3859eb64343cSVille Syrjälä 		/* Call regardless, as some status bits might not be
3860eb64343cSVille Syrjälä 		 * signalled in iir */
3861eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
3862a266c7d5SChris Wilson 
386378c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
386478c357ddSVille Syrjälä 			i9xx_error_irq_ack(dev_priv, &eir, &eir_stuck);
386578c357ddSVille Syrjälä 
38669d9523d8SPaulo Zanoni 		I915_WRITE(GEN2_IIR, iir);
3867a266c7d5SChris Wilson 
3868a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
386954400257SChris Wilson 			intel_engine_signal_breadcrumbs(dev_priv->engine[RCS0]);
3870af722d28SVille Syrjälä 
3871a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
387254400257SChris Wilson 			intel_engine_signal_breadcrumbs(dev_priv->engine[VCS0]);
3873a266c7d5SChris Wilson 
387478c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
387578c357ddSVille Syrjälä 			i9xx_error_irq_handler(dev_priv, eir, eir_stuck);
3876515ac2bbSDaniel Vetter 
3877af722d28SVille Syrjälä 		if (hotplug_status)
3878af722d28SVille Syrjälä 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
3879af722d28SVille Syrjälä 
3880af722d28SVille Syrjälä 		i965_pipestat_irq_handler(dev_priv, iir, pipe_stats);
3881af722d28SVille Syrjälä 	} while (0);
3882a266c7d5SChris Wilson 
38839102650fSDaniele Ceraolo Spurio 	enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
38841f814dacSImre Deak 
3885a266c7d5SChris Wilson 	return ret;
3886a266c7d5SChris Wilson }
3887a266c7d5SChris Wilson 
3888fca52a55SDaniel Vetter /**
3889fca52a55SDaniel Vetter  * intel_irq_init - initializes irq support
3890fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
3891fca52a55SDaniel Vetter  *
3892fca52a55SDaniel Vetter  * This function initializes all the irq support including work items, timers
3893fca52a55SDaniel Vetter  * and all the vtables. It does not setup the interrupt itself though.
3894fca52a55SDaniel Vetter  */
3895b963291cSDaniel Vetter void intel_irq_init(struct drm_i915_private *dev_priv)
3896f71d4af4SJesse Barnes {
389791c8a326SChris Wilson 	struct drm_device *dev = &dev_priv->drm;
3898cefcff8fSJoonas Lahtinen 	int i;
38998b2e326dSChris Wilson 
390077913b39SJani Nikula 	intel_hpd_init_work(dev_priv);
390177913b39SJani Nikula 
3902*74bb98baSLucas De Marchi 	INIT_WORK(&dev_priv->l3_parity.error_work, ivb_parity_work);
3903cefcff8fSJoonas Lahtinen 	for (i = 0; i < MAX_L3_SLICES; ++i)
3904cefcff8fSJoonas Lahtinen 		dev_priv->l3_parity.remap_info[i] = NULL;
39058b2e326dSChris Wilson 
3906633023a4SDaniele Ceraolo Spurio 	/* pre-gen11 the guc irqs bits are in the upper 16 bits of the pm reg */
3907702668e6SDaniele Ceraolo Spurio 	if (HAS_GT_UC(dev_priv) && INTEL_GEN(dev_priv) < 11)
39082239e6dfSDaniele Ceraolo Spurio 		dev_priv->gt.pm_guc_events = GUC_INTR_GUC2HOST << 16;
390926705e20SSagar Arun Kamble 
391021da2700SVille Syrjälä 	dev->vblank_disable_immediate = true;
391121da2700SVille Syrjälä 
3912262fd485SChris Wilson 	/* Most platforms treat the display irq block as an always-on
3913262fd485SChris Wilson 	 * power domain. vlv/chv can disable it at runtime and need
3914262fd485SChris Wilson 	 * special care to avoid writing any of the display block registers
3915262fd485SChris Wilson 	 * outside of the power domain. We defer setting up the display irqs
3916262fd485SChris Wilson 	 * in this case to the runtime pm.
3917262fd485SChris Wilson 	 */
3918262fd485SChris Wilson 	dev_priv->display_irqs_enabled = true;
3919262fd485SChris Wilson 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
3920262fd485SChris Wilson 		dev_priv->display_irqs_enabled = false;
3921262fd485SChris Wilson 
3922317eaa95SLyude 	dev_priv->hotplug.hpd_storm_threshold = HPD_STORM_DEFAULT_THRESHOLD;
39239a64c650SLyude Paul 	/* If we have MST support, we want to avoid doing short HPD IRQ storm
39249a64c650SLyude Paul 	 * detection, as short HPD storms will occur as a natural part of
39259a64c650SLyude Paul 	 * sideband messaging with MST.
39269a64c650SLyude Paul 	 * On older platforms however, IRQ storms can occur with both long and
39279a64c650SLyude Paul 	 * short pulses, as seen on some G4x systems.
39289a64c650SLyude Paul 	 */
39299a64c650SLyude Paul 	dev_priv->hotplug.hpd_short_storm_enabled = !HAS_DP_MST(dev_priv);
3930317eaa95SLyude 
3931b318b824SVille Syrjälä 	if (HAS_GMCH(dev_priv)) {
3932b318b824SVille Syrjälä 		if (I915_HAS_HOTPLUG(dev_priv))
393343f328d7SVille Syrjälä 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3934b318b824SVille Syrjälä 	} else {
3935943682e3SMatt Roper 		if (HAS_PCH_JSP(dev_priv))
3936943682e3SMatt Roper 			dev_priv->display.hpd_irq_setup = jsp_hpd_irq_setup;
3937943682e3SMatt Roper 		else if (HAS_PCH_MCC(dev_priv))
39388ef7e340SMatt Roper 			dev_priv->display.hpd_irq_setup = mcc_hpd_irq_setup;
39398ef7e340SMatt Roper 		else if (INTEL_GEN(dev_priv) >= 11)
3940121e758eSDhinakaran Pandiyan 			dev_priv->display.hpd_irq_setup = gen11_hpd_irq_setup;
3941b318b824SVille Syrjälä 		else if (IS_GEN9_LP(dev_priv))
3942e0a20ad7SShashank Sharma 			dev_priv->display.hpd_irq_setup = bxt_hpd_irq_setup;
3943c6c30b91SRodrigo Vivi 		else if (INTEL_PCH_TYPE(dev_priv) >= PCH_SPT)
39446dbf30ceSVille Syrjälä 			dev_priv->display.hpd_irq_setup = spt_hpd_irq_setup;
39456dbf30ceSVille Syrjälä 		else
39463a3b3c7dSVille Syrjälä 			dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
3947f71d4af4SJesse Barnes 	}
3948f71d4af4SJesse Barnes }
394920afbda2SDaniel Vetter 
3950fca52a55SDaniel Vetter /**
3951cefcff8fSJoonas Lahtinen  * intel_irq_fini - deinitializes IRQ support
3952cefcff8fSJoonas Lahtinen  * @i915: i915 device instance
3953cefcff8fSJoonas Lahtinen  *
3954cefcff8fSJoonas Lahtinen  * This function deinitializes all the IRQ support.
3955cefcff8fSJoonas Lahtinen  */
3956cefcff8fSJoonas Lahtinen void intel_irq_fini(struct drm_i915_private *i915)
3957cefcff8fSJoonas Lahtinen {
3958cefcff8fSJoonas Lahtinen 	int i;
3959cefcff8fSJoonas Lahtinen 
3960cefcff8fSJoonas Lahtinen 	for (i = 0; i < MAX_L3_SLICES; ++i)
3961cefcff8fSJoonas Lahtinen 		kfree(i915->l3_parity.remap_info[i]);
3962cefcff8fSJoonas Lahtinen }
3963cefcff8fSJoonas Lahtinen 
3964b318b824SVille Syrjälä static irq_handler_t intel_irq_handler(struct drm_i915_private *dev_priv)
3965b318b824SVille Syrjälä {
3966b318b824SVille Syrjälä 	if (HAS_GMCH(dev_priv)) {
3967b318b824SVille Syrjälä 		if (IS_CHERRYVIEW(dev_priv))
3968b318b824SVille Syrjälä 			return cherryview_irq_handler;
3969b318b824SVille Syrjälä 		else if (IS_VALLEYVIEW(dev_priv))
3970b318b824SVille Syrjälä 			return valleyview_irq_handler;
3971b318b824SVille Syrjälä 		else if (IS_GEN(dev_priv, 4))
3972b318b824SVille Syrjälä 			return i965_irq_handler;
3973b318b824SVille Syrjälä 		else if (IS_GEN(dev_priv, 3))
3974b318b824SVille Syrjälä 			return i915_irq_handler;
3975b318b824SVille Syrjälä 		else
3976b318b824SVille Syrjälä 			return i8xx_irq_handler;
3977b318b824SVille Syrjälä 	} else {
3978b318b824SVille Syrjälä 		if (INTEL_GEN(dev_priv) >= 11)
3979b318b824SVille Syrjälä 			return gen11_irq_handler;
3980b318b824SVille Syrjälä 		else if (INTEL_GEN(dev_priv) >= 8)
3981b318b824SVille Syrjälä 			return gen8_irq_handler;
3982b318b824SVille Syrjälä 		else
39839eae5e27SLucas De Marchi 			return ilk_irq_handler;
3984b318b824SVille Syrjälä 	}
3985b318b824SVille Syrjälä }
3986b318b824SVille Syrjälä 
3987b318b824SVille Syrjälä static void intel_irq_reset(struct drm_i915_private *dev_priv)
3988b318b824SVille Syrjälä {
3989b318b824SVille Syrjälä 	if (HAS_GMCH(dev_priv)) {
3990b318b824SVille Syrjälä 		if (IS_CHERRYVIEW(dev_priv))
3991b318b824SVille Syrjälä 			cherryview_irq_reset(dev_priv);
3992b318b824SVille Syrjälä 		else if (IS_VALLEYVIEW(dev_priv))
3993b318b824SVille Syrjälä 			valleyview_irq_reset(dev_priv);
3994b318b824SVille Syrjälä 		else if (IS_GEN(dev_priv, 4))
3995b318b824SVille Syrjälä 			i965_irq_reset(dev_priv);
3996b318b824SVille Syrjälä 		else if (IS_GEN(dev_priv, 3))
3997b318b824SVille Syrjälä 			i915_irq_reset(dev_priv);
3998b318b824SVille Syrjälä 		else
3999b318b824SVille Syrjälä 			i8xx_irq_reset(dev_priv);
4000b318b824SVille Syrjälä 	} else {
4001b318b824SVille Syrjälä 		if (INTEL_GEN(dev_priv) >= 11)
4002b318b824SVille Syrjälä 			gen11_irq_reset(dev_priv);
4003b318b824SVille Syrjälä 		else if (INTEL_GEN(dev_priv) >= 8)
4004b318b824SVille Syrjälä 			gen8_irq_reset(dev_priv);
4005b318b824SVille Syrjälä 		else
40069eae5e27SLucas De Marchi 			ilk_irq_reset(dev_priv);
4007b318b824SVille Syrjälä 	}
4008b318b824SVille Syrjälä }
4009b318b824SVille Syrjälä 
4010b318b824SVille Syrjälä static void intel_irq_postinstall(struct drm_i915_private *dev_priv)
4011b318b824SVille Syrjälä {
4012b318b824SVille Syrjälä 	if (HAS_GMCH(dev_priv)) {
4013b318b824SVille Syrjälä 		if (IS_CHERRYVIEW(dev_priv))
4014b318b824SVille Syrjälä 			cherryview_irq_postinstall(dev_priv);
4015b318b824SVille Syrjälä 		else if (IS_VALLEYVIEW(dev_priv))
4016b318b824SVille Syrjälä 			valleyview_irq_postinstall(dev_priv);
4017b318b824SVille Syrjälä 		else if (IS_GEN(dev_priv, 4))
4018b318b824SVille Syrjälä 			i965_irq_postinstall(dev_priv);
4019b318b824SVille Syrjälä 		else if (IS_GEN(dev_priv, 3))
4020b318b824SVille Syrjälä 			i915_irq_postinstall(dev_priv);
4021b318b824SVille Syrjälä 		else
4022b318b824SVille Syrjälä 			i8xx_irq_postinstall(dev_priv);
4023b318b824SVille Syrjälä 	} else {
4024b318b824SVille Syrjälä 		if (INTEL_GEN(dev_priv) >= 11)
4025b318b824SVille Syrjälä 			gen11_irq_postinstall(dev_priv);
4026b318b824SVille Syrjälä 		else if (INTEL_GEN(dev_priv) >= 8)
4027b318b824SVille Syrjälä 			gen8_irq_postinstall(dev_priv);
4028b318b824SVille Syrjälä 		else
40299eae5e27SLucas De Marchi 			ilk_irq_postinstall(dev_priv);
4030b318b824SVille Syrjälä 	}
4031b318b824SVille Syrjälä }
4032b318b824SVille Syrjälä 
4033cefcff8fSJoonas Lahtinen /**
4034fca52a55SDaniel Vetter  * intel_irq_install - enables the hardware interrupt
4035fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4036fca52a55SDaniel Vetter  *
4037fca52a55SDaniel Vetter  * This function enables the hardware interrupt handling, but leaves the hotplug
4038fca52a55SDaniel Vetter  * handling still disabled. It is called after intel_irq_init().
4039fca52a55SDaniel Vetter  *
4040fca52a55SDaniel Vetter  * In the driver load and resume code we need working interrupts in a few places
4041fca52a55SDaniel Vetter  * but don't want to deal with the hassle of concurrent probe and hotplug
4042fca52a55SDaniel Vetter  * workers. Hence the split into this two-stage approach.
4043fca52a55SDaniel Vetter  */
40442aeb7d3aSDaniel Vetter int intel_irq_install(struct drm_i915_private *dev_priv)
40452aeb7d3aSDaniel Vetter {
4046b318b824SVille Syrjälä 	int irq = dev_priv->drm.pdev->irq;
4047b318b824SVille Syrjälä 	int ret;
4048b318b824SVille Syrjälä 
40492aeb7d3aSDaniel Vetter 	/*
40502aeb7d3aSDaniel Vetter 	 * We enable some interrupt sources in our postinstall hooks, so mark
40512aeb7d3aSDaniel Vetter 	 * interrupts as enabled _before_ actually enabling them to avoid
40522aeb7d3aSDaniel Vetter 	 * special cases in our ordering checks.
40532aeb7d3aSDaniel Vetter 	 */
4054ad1443f0SSagar Arun Kamble 	dev_priv->runtime_pm.irqs_enabled = true;
40552aeb7d3aSDaniel Vetter 
4056b318b824SVille Syrjälä 	dev_priv->drm.irq_enabled = true;
4057b318b824SVille Syrjälä 
4058b318b824SVille Syrjälä 	intel_irq_reset(dev_priv);
4059b318b824SVille Syrjälä 
4060b318b824SVille Syrjälä 	ret = request_irq(irq, intel_irq_handler(dev_priv),
4061b318b824SVille Syrjälä 			  IRQF_SHARED, DRIVER_NAME, dev_priv);
4062b318b824SVille Syrjälä 	if (ret < 0) {
4063b318b824SVille Syrjälä 		dev_priv->drm.irq_enabled = false;
4064b318b824SVille Syrjälä 		return ret;
4065b318b824SVille Syrjälä 	}
4066b318b824SVille Syrjälä 
4067b318b824SVille Syrjälä 	intel_irq_postinstall(dev_priv);
4068b318b824SVille Syrjälä 
4069b318b824SVille Syrjälä 	return ret;
40702aeb7d3aSDaniel Vetter }
40712aeb7d3aSDaniel Vetter 
4072fca52a55SDaniel Vetter /**
4073fca52a55SDaniel Vetter  * intel_irq_uninstall - finilizes all irq handling
4074fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4075fca52a55SDaniel Vetter  *
4076fca52a55SDaniel Vetter  * This stops interrupt and hotplug handling and unregisters and frees all
4077fca52a55SDaniel Vetter  * resources acquired in the init functions.
4078fca52a55SDaniel Vetter  */
40792aeb7d3aSDaniel Vetter void intel_irq_uninstall(struct drm_i915_private *dev_priv)
40802aeb7d3aSDaniel Vetter {
4081b318b824SVille Syrjälä 	int irq = dev_priv->drm.pdev->irq;
4082b318b824SVille Syrjälä 
4083b318b824SVille Syrjälä 	/*
4084789fa874SJanusz Krzysztofik 	 * FIXME we can get called twice during driver probe
4085789fa874SJanusz Krzysztofik 	 * error handling as well as during driver remove due to
4086789fa874SJanusz Krzysztofik 	 * intel_modeset_driver_remove() calling us out of sequence.
4087789fa874SJanusz Krzysztofik 	 * Would be nice if it didn't do that...
4088b318b824SVille Syrjälä 	 */
4089b318b824SVille Syrjälä 	if (!dev_priv->drm.irq_enabled)
4090b318b824SVille Syrjälä 		return;
4091b318b824SVille Syrjälä 
4092b318b824SVille Syrjälä 	dev_priv->drm.irq_enabled = false;
4093b318b824SVille Syrjälä 
4094b318b824SVille Syrjälä 	intel_irq_reset(dev_priv);
4095b318b824SVille Syrjälä 
4096b318b824SVille Syrjälä 	free_irq(irq, dev_priv);
4097b318b824SVille Syrjälä 
40982aeb7d3aSDaniel Vetter 	intel_hpd_cancel_work(dev_priv);
4099ad1443f0SSagar Arun Kamble 	dev_priv->runtime_pm.irqs_enabled = false;
41002aeb7d3aSDaniel Vetter }
41012aeb7d3aSDaniel Vetter 
4102fca52a55SDaniel Vetter /**
4103fca52a55SDaniel Vetter  * intel_runtime_pm_disable_interrupts - runtime interrupt disabling
4104fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4105fca52a55SDaniel Vetter  *
4106fca52a55SDaniel Vetter  * This function is used to disable interrupts at runtime, both in the runtime
4107fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4108fca52a55SDaniel Vetter  */
4109b963291cSDaniel Vetter void intel_runtime_pm_disable_interrupts(struct drm_i915_private *dev_priv)
4110c67a470bSPaulo Zanoni {
4111b318b824SVille Syrjälä 	intel_irq_reset(dev_priv);
4112ad1443f0SSagar Arun Kamble 	dev_priv->runtime_pm.irqs_enabled = false;
4113315ca4c4SVille Syrjälä 	intel_synchronize_irq(dev_priv);
4114c67a470bSPaulo Zanoni }
4115c67a470bSPaulo Zanoni 
4116fca52a55SDaniel Vetter /**
4117fca52a55SDaniel Vetter  * intel_runtime_pm_enable_interrupts - runtime interrupt enabling
4118fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4119fca52a55SDaniel Vetter  *
4120fca52a55SDaniel Vetter  * This function is used to enable interrupts at runtime, both in the runtime
4121fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4122fca52a55SDaniel Vetter  */
4123b963291cSDaniel Vetter void intel_runtime_pm_enable_interrupts(struct drm_i915_private *dev_priv)
4124c67a470bSPaulo Zanoni {
4125ad1443f0SSagar Arun Kamble 	dev_priv->runtime_pm.irqs_enabled = true;
4126b318b824SVille Syrjälä 	intel_irq_reset(dev_priv);
4127b318b824SVille Syrjälä 	intel_irq_postinstall(dev_priv);
4128c67a470bSPaulo Zanoni }
4129d64575eeSJani Nikula 
4130d64575eeSJani Nikula bool intel_irqs_enabled(struct drm_i915_private *dev_priv)
4131d64575eeSJani Nikula {
4132d64575eeSJani Nikula 	/*
4133d64575eeSJani Nikula 	 * We only use drm_irq_uninstall() at unload and VT switch, so
4134d64575eeSJani Nikula 	 * this is the only thing we need to check.
4135d64575eeSJani Nikula 	 */
4136d64575eeSJani Nikula 	return dev_priv->runtime_pm.irqs_enabled;
4137d64575eeSJani Nikula }
4138d64575eeSJani Nikula 
4139d64575eeSJani Nikula void intel_synchronize_irq(struct drm_i915_private *i915)
4140d64575eeSJani Nikula {
4141d64575eeSJani Nikula 	synchronize_irq(i915->drm.pdev->irq);
4142d64575eeSJani Nikula }
4143