xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 00376ccfb2a457fcd7041e2d5740faa8a81ab8cb)
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ä 
211a9f236d1SPankaj Bharadiya 	drm_WARN(&uncore->i915->drm, 1,
212a9f236d1SPankaj Bharadiya 		 "Interrupt register 0x%x is not zero: 0x%08x\n",
213f0f59a00SVille Syrjälä 		 i915_mmio_reg_offset(reg), val);
21465f42cdcSPaulo Zanoni 	intel_uncore_write(uncore, reg, 0xffffffff);
21565f42cdcSPaulo Zanoni 	intel_uncore_posting_read(uncore, reg);
21665f42cdcSPaulo Zanoni 	intel_uncore_write(uncore, reg, 0xffffffff);
21765f42cdcSPaulo Zanoni 	intel_uncore_posting_read(uncore, reg);
218b51a2842SVille Syrjälä }
219337ba017SPaulo Zanoni 
22065f42cdcSPaulo Zanoni static void gen2_assert_iir_is_zero(struct intel_uncore *uncore)
221e9e9848aSVille Syrjälä {
22265f42cdcSPaulo Zanoni 	u16 val = intel_uncore_read16(uncore, GEN2_IIR);
223e9e9848aSVille Syrjälä 
224e9e9848aSVille Syrjälä 	if (val == 0)
225e9e9848aSVille Syrjälä 		return;
226e9e9848aSVille Syrjälä 
227a9f236d1SPankaj Bharadiya 	drm_WARN(&uncore->i915->drm, 1,
228a9f236d1SPankaj Bharadiya 		 "Interrupt register 0x%x is not zero: 0x%08x\n",
2299d9523d8SPaulo Zanoni 		 i915_mmio_reg_offset(GEN2_IIR), val);
23065f42cdcSPaulo Zanoni 	intel_uncore_write16(uncore, GEN2_IIR, 0xffff);
23165f42cdcSPaulo Zanoni 	intel_uncore_posting_read16(uncore, GEN2_IIR);
23265f42cdcSPaulo Zanoni 	intel_uncore_write16(uncore, GEN2_IIR, 0xffff);
23365f42cdcSPaulo Zanoni 	intel_uncore_posting_read16(uncore, GEN2_IIR);
234e9e9848aSVille Syrjälä }
235e9e9848aSVille Syrjälä 
236cf1c97dcSAndi Shyti void gen3_irq_init(struct intel_uncore *uncore,
23768eb49b1SPaulo Zanoni 		   i915_reg_t imr, u32 imr_val,
23868eb49b1SPaulo Zanoni 		   i915_reg_t ier, u32 ier_val,
23968eb49b1SPaulo Zanoni 		   i915_reg_t iir)
24068eb49b1SPaulo Zanoni {
24165f42cdcSPaulo Zanoni 	gen3_assert_iir_is_zero(uncore, iir);
24235079899SPaulo Zanoni 
24365f42cdcSPaulo Zanoni 	intel_uncore_write(uncore, ier, ier_val);
24465f42cdcSPaulo Zanoni 	intel_uncore_write(uncore, imr, imr_val);
24565f42cdcSPaulo Zanoni 	intel_uncore_posting_read(uncore, imr);
24668eb49b1SPaulo Zanoni }
24735079899SPaulo Zanoni 
248cf1c97dcSAndi Shyti void gen2_irq_init(struct intel_uncore *uncore,
2492918c3caSPaulo Zanoni 		   u32 imr_val, u32 ier_val)
25068eb49b1SPaulo Zanoni {
25165f42cdcSPaulo Zanoni 	gen2_assert_iir_is_zero(uncore);
25268eb49b1SPaulo Zanoni 
25365f42cdcSPaulo Zanoni 	intel_uncore_write16(uncore, GEN2_IER, ier_val);
25465f42cdcSPaulo Zanoni 	intel_uncore_write16(uncore, GEN2_IMR, imr_val);
25565f42cdcSPaulo Zanoni 	intel_uncore_posting_read16(uncore, GEN2_IMR);
25668eb49b1SPaulo Zanoni }
25768eb49b1SPaulo Zanoni 
2580706f17cSEgbert Eich /* For display hotplug interrupt */
2590706f17cSEgbert Eich static inline void
2600706f17cSEgbert Eich i915_hotplug_interrupt_update_locked(struct drm_i915_private *dev_priv,
261a9c287c9SJani Nikula 				     u32 mask,
262a9c287c9SJani Nikula 				     u32 bits)
2630706f17cSEgbert Eich {
264a9c287c9SJani Nikula 	u32 val;
2650706f17cSEgbert Eich 
26667520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
26748a1b8d4SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, bits & ~mask);
2680706f17cSEgbert Eich 
2690706f17cSEgbert Eich 	val = I915_READ(PORT_HOTPLUG_EN);
2700706f17cSEgbert Eich 	val &= ~mask;
2710706f17cSEgbert Eich 	val |= bits;
2720706f17cSEgbert Eich 	I915_WRITE(PORT_HOTPLUG_EN, val);
2730706f17cSEgbert Eich }
2740706f17cSEgbert Eich 
2750706f17cSEgbert Eich /**
2760706f17cSEgbert Eich  * i915_hotplug_interrupt_update - update hotplug interrupt enable
2770706f17cSEgbert Eich  * @dev_priv: driver private
2780706f17cSEgbert Eich  * @mask: bits to update
2790706f17cSEgbert Eich  * @bits: bits to enable
2800706f17cSEgbert Eich  * NOTE: the HPD enable bits are modified both inside and outside
2810706f17cSEgbert Eich  * of an interrupt context. To avoid that read-modify-write cycles
2820706f17cSEgbert Eich  * interfer, these bits are protected by a spinlock. Since this
2830706f17cSEgbert Eich  * function is usually not called from a context where the lock is
2840706f17cSEgbert Eich  * held already, this function acquires the lock itself. A non-locking
2850706f17cSEgbert Eich  * version is also available.
2860706f17cSEgbert Eich  */
2870706f17cSEgbert Eich void i915_hotplug_interrupt_update(struct drm_i915_private *dev_priv,
288a9c287c9SJani Nikula 				   u32 mask,
289a9c287c9SJani Nikula 				   u32 bits)
2900706f17cSEgbert Eich {
2910706f17cSEgbert Eich 	spin_lock_irq(&dev_priv->irq_lock);
2920706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv, mask, bits);
2930706f17cSEgbert Eich 	spin_unlock_irq(&dev_priv->irq_lock);
2940706f17cSEgbert Eich }
2950706f17cSEgbert Eich 
296d9dc34f1SVille Syrjälä /**
297d9dc34f1SVille Syrjälä  * ilk_update_display_irq - update DEIMR
298d9dc34f1SVille Syrjälä  * @dev_priv: driver private
299d9dc34f1SVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
300d9dc34f1SVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
301d9dc34f1SVille Syrjälä  */
302fbdedaeaSVille Syrjälä void ilk_update_display_irq(struct drm_i915_private *dev_priv,
303a9c287c9SJani Nikula 			    u32 interrupt_mask,
304a9c287c9SJani Nikula 			    u32 enabled_irq_mask)
305036a4a7dSZhenyu Wang {
306a9c287c9SJani Nikula 	u32 new_val;
307d9dc34f1SVille Syrjälä 
30867520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
3094bc9d430SDaniel Vetter 
31048a1b8d4SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, enabled_irq_mask & ~interrupt_mask);
311d9dc34f1SVille Syrjälä 
31248a1b8d4SPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, !intel_irqs_enabled(dev_priv)))
313c67a470bSPaulo Zanoni 		return;
314c67a470bSPaulo Zanoni 
315d9dc34f1SVille Syrjälä 	new_val = dev_priv->irq_mask;
316d9dc34f1SVille Syrjälä 	new_val &= ~interrupt_mask;
317d9dc34f1SVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
318d9dc34f1SVille Syrjälä 
319d9dc34f1SVille Syrjälä 	if (new_val != dev_priv->irq_mask) {
320d9dc34f1SVille Syrjälä 		dev_priv->irq_mask = new_val;
3211ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
3223143a2bfSChris Wilson 		POSTING_READ(DEIMR);
323036a4a7dSZhenyu Wang 	}
324036a4a7dSZhenyu Wang }
325036a4a7dSZhenyu Wang 
3260961021aSBen Widawsky /**
3273a3b3c7dSVille Syrjälä  * bdw_update_port_irq - update DE port interrupt
3283a3b3c7dSVille Syrjälä  * @dev_priv: driver private
3293a3b3c7dSVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
3303a3b3c7dSVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
3313a3b3c7dSVille Syrjälä  */
3323a3b3c7dSVille Syrjälä static void bdw_update_port_irq(struct drm_i915_private *dev_priv,
333a9c287c9SJani Nikula 				u32 interrupt_mask,
334a9c287c9SJani Nikula 				u32 enabled_irq_mask)
3353a3b3c7dSVille Syrjälä {
336a9c287c9SJani Nikula 	u32 new_val;
337a9c287c9SJani Nikula 	u32 old_val;
3383a3b3c7dSVille Syrjälä 
33967520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
3403a3b3c7dSVille Syrjälä 
34148a1b8d4SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, enabled_irq_mask & ~interrupt_mask);
3423a3b3c7dSVille Syrjälä 
34348a1b8d4SPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, !intel_irqs_enabled(dev_priv)))
3443a3b3c7dSVille Syrjälä 		return;
3453a3b3c7dSVille Syrjälä 
3463a3b3c7dSVille Syrjälä 	old_val = I915_READ(GEN8_DE_PORT_IMR);
3473a3b3c7dSVille Syrjälä 
3483a3b3c7dSVille Syrjälä 	new_val = old_val;
3493a3b3c7dSVille Syrjälä 	new_val &= ~interrupt_mask;
3503a3b3c7dSVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
3513a3b3c7dSVille Syrjälä 
3523a3b3c7dSVille Syrjälä 	if (new_val != old_val) {
3533a3b3c7dSVille Syrjälä 		I915_WRITE(GEN8_DE_PORT_IMR, new_val);
3543a3b3c7dSVille Syrjälä 		POSTING_READ(GEN8_DE_PORT_IMR);
3553a3b3c7dSVille Syrjälä 	}
3563a3b3c7dSVille Syrjälä }
3573a3b3c7dSVille Syrjälä 
3583a3b3c7dSVille Syrjälä /**
359013d3752SVille Syrjälä  * bdw_update_pipe_irq - update DE pipe interrupt
360013d3752SVille Syrjälä  * @dev_priv: driver private
361013d3752SVille Syrjälä  * @pipe: pipe whose interrupt to update
362013d3752SVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
363013d3752SVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
364013d3752SVille Syrjälä  */
365013d3752SVille Syrjälä void bdw_update_pipe_irq(struct drm_i915_private *dev_priv,
366013d3752SVille Syrjälä 			 enum pipe pipe,
367a9c287c9SJani Nikula 			 u32 interrupt_mask,
368a9c287c9SJani Nikula 			 u32 enabled_irq_mask)
369013d3752SVille Syrjälä {
370a9c287c9SJani Nikula 	u32 new_val;
371013d3752SVille Syrjälä 
37267520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
373013d3752SVille Syrjälä 
37448a1b8d4SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, enabled_irq_mask & ~interrupt_mask);
375013d3752SVille Syrjälä 
37648a1b8d4SPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, !intel_irqs_enabled(dev_priv)))
377013d3752SVille Syrjälä 		return;
378013d3752SVille Syrjälä 
379013d3752SVille Syrjälä 	new_val = dev_priv->de_irq_mask[pipe];
380013d3752SVille Syrjälä 	new_val &= ~interrupt_mask;
381013d3752SVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
382013d3752SVille Syrjälä 
383013d3752SVille Syrjälä 	if (new_val != dev_priv->de_irq_mask[pipe]) {
384013d3752SVille Syrjälä 		dev_priv->de_irq_mask[pipe] = new_val;
385013d3752SVille Syrjälä 		I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
386013d3752SVille Syrjälä 		POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
387013d3752SVille Syrjälä 	}
388013d3752SVille Syrjälä }
389013d3752SVille Syrjälä 
390013d3752SVille Syrjälä /**
391fee884edSDaniel Vetter  * ibx_display_interrupt_update - update SDEIMR
392fee884edSDaniel Vetter  * @dev_priv: driver private
393fee884edSDaniel Vetter  * @interrupt_mask: mask of interrupt bits to update
394fee884edSDaniel Vetter  * @enabled_irq_mask: mask of interrupt bits to enable
395fee884edSDaniel Vetter  */
39647339cd9SDaniel Vetter void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
397a9c287c9SJani Nikula 				  u32 interrupt_mask,
398a9c287c9SJani Nikula 				  u32 enabled_irq_mask)
399fee884edSDaniel Vetter {
400a9c287c9SJani Nikula 	u32 sdeimr = I915_READ(SDEIMR);
401fee884edSDaniel Vetter 	sdeimr &= ~interrupt_mask;
402fee884edSDaniel Vetter 	sdeimr |= (~enabled_irq_mask & interrupt_mask);
403fee884edSDaniel Vetter 
40448a1b8d4SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, enabled_irq_mask & ~interrupt_mask);
40515a17aaeSDaniel Vetter 
40667520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
407fee884edSDaniel Vetter 
40848a1b8d4SPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, !intel_irqs_enabled(dev_priv)))
409c67a470bSPaulo Zanoni 		return;
410c67a470bSPaulo Zanoni 
411fee884edSDaniel Vetter 	I915_WRITE(SDEIMR, sdeimr);
412fee884edSDaniel Vetter 	POSTING_READ(SDEIMR);
413fee884edSDaniel Vetter }
4148664281bSPaulo Zanoni 
4156b12ca56SVille Syrjälä u32 i915_pipestat_enable_mask(struct drm_i915_private *dev_priv,
4166b12ca56SVille Syrjälä 			      enum pipe pipe)
4177c463586SKeith Packard {
4186b12ca56SVille Syrjälä 	u32 status_mask = dev_priv->pipestat_irq_mask[pipe];
41910c59c51SImre Deak 	u32 enable_mask = status_mask << 16;
42010c59c51SImre Deak 
4216b12ca56SVille Syrjälä 	lockdep_assert_held(&dev_priv->irq_lock);
4226b12ca56SVille Syrjälä 
4236b12ca56SVille Syrjälä 	if (INTEL_GEN(dev_priv) < 5)
4246b12ca56SVille Syrjälä 		goto out;
4256b12ca56SVille Syrjälä 
42610c59c51SImre Deak 	/*
427724a6905SVille Syrjälä 	 * On pipe A we don't support the PSR interrupt yet,
428724a6905SVille Syrjälä 	 * on pipe B and C the same bit MBZ.
42910c59c51SImre Deak 	 */
43048a1b8d4SPankaj Bharadiya 	if (drm_WARN_ON_ONCE(&dev_priv->drm,
43148a1b8d4SPankaj Bharadiya 			     status_mask & PIPE_A_PSR_STATUS_VLV))
43210c59c51SImre Deak 		return 0;
433724a6905SVille Syrjälä 	/*
434724a6905SVille Syrjälä 	 * On pipe B and C we don't support the PSR interrupt yet, on pipe
435724a6905SVille Syrjälä 	 * A the same bit is for perf counters which we don't use either.
436724a6905SVille Syrjälä 	 */
43748a1b8d4SPankaj Bharadiya 	if (drm_WARN_ON_ONCE(&dev_priv->drm,
43848a1b8d4SPankaj Bharadiya 			     status_mask & PIPE_B_PSR_STATUS_VLV))
439724a6905SVille Syrjälä 		return 0;
44010c59c51SImre Deak 
44110c59c51SImre Deak 	enable_mask &= ~(PIPE_FIFO_UNDERRUN_STATUS |
44210c59c51SImre Deak 			 SPRITE0_FLIP_DONE_INT_EN_VLV |
44310c59c51SImre Deak 			 SPRITE1_FLIP_DONE_INT_EN_VLV);
44410c59c51SImre Deak 	if (status_mask & SPRITE0_FLIP_DONE_INT_STATUS_VLV)
44510c59c51SImre Deak 		enable_mask |= SPRITE0_FLIP_DONE_INT_EN_VLV;
44610c59c51SImre Deak 	if (status_mask & SPRITE1_FLIP_DONE_INT_STATUS_VLV)
44710c59c51SImre Deak 		enable_mask |= SPRITE1_FLIP_DONE_INT_EN_VLV;
44810c59c51SImre Deak 
4496b12ca56SVille Syrjälä out:
45048a1b8d4SPankaj Bharadiya 	drm_WARN_ONCE(&dev_priv->drm,
45148a1b8d4SPankaj Bharadiya 		      enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
4526b12ca56SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
4536b12ca56SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
4546b12ca56SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask);
4556b12ca56SVille Syrjälä 
45610c59c51SImre Deak 	return enable_mask;
45710c59c51SImre Deak }
45810c59c51SImre Deak 
4596b12ca56SVille Syrjälä void i915_enable_pipestat(struct drm_i915_private *dev_priv,
4606b12ca56SVille Syrjälä 			  enum pipe pipe, u32 status_mask)
461755e9019SImre Deak {
4626b12ca56SVille Syrjälä 	i915_reg_t reg = PIPESTAT(pipe);
463755e9019SImre Deak 	u32 enable_mask;
464755e9019SImre Deak 
46548a1b8d4SPankaj Bharadiya 	drm_WARN_ONCE(&dev_priv->drm, status_mask & ~PIPESTAT_INT_STATUS_MASK,
4666b12ca56SVille Syrjälä 		      "pipe %c: status_mask=0x%x\n",
4676b12ca56SVille Syrjälä 		      pipe_name(pipe), status_mask);
4686b12ca56SVille Syrjälä 
4696b12ca56SVille Syrjälä 	lockdep_assert_held(&dev_priv->irq_lock);
47048a1b8d4SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, !intel_irqs_enabled(dev_priv));
4716b12ca56SVille Syrjälä 
4726b12ca56SVille Syrjälä 	if ((dev_priv->pipestat_irq_mask[pipe] & status_mask) == status_mask)
4736b12ca56SVille Syrjälä 		return;
4746b12ca56SVille Syrjälä 
4756b12ca56SVille Syrjälä 	dev_priv->pipestat_irq_mask[pipe] |= status_mask;
4766b12ca56SVille Syrjälä 	enable_mask = i915_pipestat_enable_mask(dev_priv, pipe);
4776b12ca56SVille Syrjälä 
4786b12ca56SVille Syrjälä 	I915_WRITE(reg, enable_mask | status_mask);
4796b12ca56SVille Syrjälä 	POSTING_READ(reg);
480755e9019SImre Deak }
481755e9019SImre Deak 
4826b12ca56SVille Syrjälä void i915_disable_pipestat(struct drm_i915_private *dev_priv,
4836b12ca56SVille Syrjälä 			   enum pipe pipe, u32 status_mask)
484755e9019SImre Deak {
4856b12ca56SVille Syrjälä 	i915_reg_t reg = PIPESTAT(pipe);
486755e9019SImre Deak 	u32 enable_mask;
487755e9019SImre Deak 
48848a1b8d4SPankaj Bharadiya 	drm_WARN_ONCE(&dev_priv->drm, status_mask & ~PIPESTAT_INT_STATUS_MASK,
4896b12ca56SVille Syrjälä 		      "pipe %c: status_mask=0x%x\n",
4906b12ca56SVille Syrjälä 		      pipe_name(pipe), status_mask);
4916b12ca56SVille Syrjälä 
4926b12ca56SVille Syrjälä 	lockdep_assert_held(&dev_priv->irq_lock);
49348a1b8d4SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, !intel_irqs_enabled(dev_priv));
4946b12ca56SVille Syrjälä 
4956b12ca56SVille Syrjälä 	if ((dev_priv->pipestat_irq_mask[pipe] & status_mask) == 0)
4966b12ca56SVille Syrjälä 		return;
4976b12ca56SVille Syrjälä 
4986b12ca56SVille Syrjälä 	dev_priv->pipestat_irq_mask[pipe] &= ~status_mask;
4996b12ca56SVille Syrjälä 	enable_mask = i915_pipestat_enable_mask(dev_priv, pipe);
5006b12ca56SVille Syrjälä 
5016b12ca56SVille Syrjälä 	I915_WRITE(reg, enable_mask | status_mask);
5026b12ca56SVille Syrjälä 	POSTING_READ(reg);
503755e9019SImre Deak }
504755e9019SImre Deak 
505f3e30485SVille Syrjälä static bool i915_has_asle(struct drm_i915_private *dev_priv)
506f3e30485SVille Syrjälä {
507f3e30485SVille Syrjälä 	if (!dev_priv->opregion.asle)
508f3e30485SVille Syrjälä 		return false;
509f3e30485SVille Syrjälä 
510f3e30485SVille Syrjälä 	return IS_PINEVIEW(dev_priv) || IS_MOBILE(dev_priv);
511f3e30485SVille Syrjälä }
512f3e30485SVille Syrjälä 
513c0e09200SDave Airlie /**
514f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
51514bb2c11STvrtko Ursulin  * @dev_priv: i915 device private
51601c66889SZhao Yakui  */
51791d14251STvrtko Ursulin static void i915_enable_asle_pipestat(struct drm_i915_private *dev_priv)
51801c66889SZhao Yakui {
519f3e30485SVille Syrjälä 	if (!i915_has_asle(dev_priv))
520f49e38ddSJani Nikula 		return;
521f49e38ddSJani Nikula 
52213321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
52301c66889SZhao Yakui 
524755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_STATUS);
52591d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 4)
5263b6c42e8SDaniel Vetter 		i915_enable_pipestat(dev_priv, PIPE_A,
527755e9019SImre Deak 				     PIPE_LEGACY_BLC_EVENT_STATUS);
5281ec14ad3SChris Wilson 
52913321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
53001c66889SZhao Yakui }
53101c66889SZhao Yakui 
532f75f3746SVille Syrjälä /*
533f75f3746SVille Syrjälä  * This timing diagram depicts the video signal in and
534f75f3746SVille Syrjälä  * around the vertical blanking period.
535f75f3746SVille Syrjälä  *
536f75f3746SVille Syrjälä  * Assumptions about the fictitious mode used in this example:
537f75f3746SVille Syrjälä  *  vblank_start >= 3
538f75f3746SVille Syrjälä  *  vsync_start = vblank_start + 1
539f75f3746SVille Syrjälä  *  vsync_end = vblank_start + 2
540f75f3746SVille Syrjälä  *  vtotal = vblank_start + 3
541f75f3746SVille Syrjälä  *
542f75f3746SVille Syrjälä  *           start of vblank:
543f75f3746SVille Syrjälä  *           latch double buffered registers
544f75f3746SVille Syrjälä  *           increment frame counter (ctg+)
545f75f3746SVille Syrjälä  *           generate start of vblank interrupt (gen4+)
546f75f3746SVille Syrjälä  *           |
547f75f3746SVille Syrjälä  *           |          frame start:
548f75f3746SVille Syrjälä  *           |          generate frame start interrupt (aka. vblank interrupt) (gmch)
549f75f3746SVille Syrjälä  *           |          may be shifted forward 1-3 extra lines via PIPECONF
550f75f3746SVille Syrjälä  *           |          |
551f75f3746SVille Syrjälä  *           |          |  start of vsync:
552f75f3746SVille Syrjälä  *           |          |  generate vsync interrupt
553f75f3746SVille Syrjälä  *           |          |  |
554f75f3746SVille Syrjälä  * ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx
555f75f3746SVille Syrjälä  *       .   \hs/   .      \hs/          \hs/          \hs/   .      \hs/
556f75f3746SVille Syrjälä  * ----va---> <-----------------vb--------------------> <--------va-------------
557f75f3746SVille Syrjälä  *       |          |       <----vs----->                     |
558f75f3746SVille Syrjälä  * -vbs-----> <---vbs+1---> <---vbs+2---> <-----0-----> <-----1-----> <-----2--- (scanline counter gen2)
559f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2---> <-----0--- (scanline counter gen3+)
560f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2- (scanline counter hsw+ hdmi)
561f75f3746SVille Syrjälä  *       |          |                                         |
562f75f3746SVille Syrjälä  *       last visible pixel                                   first visible pixel
563f75f3746SVille Syrjälä  *                  |                                         increment frame counter (gen3/4)
564f75f3746SVille Syrjälä  *                  pixel counter = vblank_start * htotal     pixel counter = 0 (gen3/4)
565f75f3746SVille Syrjälä  *
566f75f3746SVille Syrjälä  * x  = horizontal active
567f75f3746SVille Syrjälä  * _  = horizontal blanking
568f75f3746SVille Syrjälä  * hs = horizontal sync
569f75f3746SVille Syrjälä  * va = vertical active
570f75f3746SVille Syrjälä  * vb = vertical blanking
571f75f3746SVille Syrjälä  * vs = vertical sync
572f75f3746SVille Syrjälä  * vbs = vblank_start (number)
573f75f3746SVille Syrjälä  *
574f75f3746SVille Syrjälä  * Summary:
575f75f3746SVille Syrjälä  * - most events happen at the start of horizontal sync
576f75f3746SVille Syrjälä  * - frame start happens at the start of horizontal blank, 1-4 lines
577f75f3746SVille Syrjälä  *   (depending on PIPECONF settings) after the start of vblank
578f75f3746SVille Syrjälä  * - gen3/4 pixel and frame counter are synchronized with the start
579f75f3746SVille Syrjälä  *   of horizontal active on the first line of vertical active
580f75f3746SVille Syrjälä  */
581f75f3746SVille Syrjälä 
58242f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
58342f52ef8SKeith Packard  * we use as a pipe index
58442f52ef8SKeith Packard  */
58508fa8fd0SVille Syrjälä u32 i915_get_vblank_counter(struct drm_crtc *crtc)
5860a3e67a4SJesse Barnes {
58708fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
58808fa8fd0SVille Syrjälä 	struct drm_vblank_crtc *vblank = &dev_priv->drm.vblank[drm_crtc_index(crtc)];
58932db0b65SVille Syrjälä 	const struct drm_display_mode *mode = &vblank->hwmode;
59008fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
591f0f59a00SVille Syrjälä 	i915_reg_t high_frame, low_frame;
5920b2a8e09SVille Syrjälä 	u32 high1, high2, low, pixel, vbl_start, hsync_start, htotal;
593694e409dSVille Syrjälä 	unsigned long irqflags;
594391f75e2SVille Syrjälä 
59532db0b65SVille Syrjälä 	/*
59632db0b65SVille Syrjälä 	 * On i965gm TV output the frame counter only works up to
59732db0b65SVille Syrjälä 	 * the point when we enable the TV encoder. After that the
59832db0b65SVille Syrjälä 	 * frame counter ceases to work and reads zero. We need a
59932db0b65SVille Syrjälä 	 * vblank wait before enabling the TV encoder and so we
60032db0b65SVille Syrjälä 	 * have to enable vblank interrupts while the frame counter
60132db0b65SVille Syrjälä 	 * is still in a working state. However the core vblank code
60232db0b65SVille Syrjälä 	 * does not like us returning non-zero frame counter values
60332db0b65SVille Syrjälä 	 * when we've told it that we don't have a working frame
60432db0b65SVille Syrjälä 	 * counter. Thus we must stop non-zero values leaking out.
60532db0b65SVille Syrjälä 	 */
60632db0b65SVille Syrjälä 	if (!vblank->max_vblank_count)
60732db0b65SVille Syrjälä 		return 0;
60832db0b65SVille Syrjälä 
6090b2a8e09SVille Syrjälä 	htotal = mode->crtc_htotal;
6100b2a8e09SVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
6110b2a8e09SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
6120b2a8e09SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
6130b2a8e09SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
614391f75e2SVille Syrjälä 
6150b2a8e09SVille Syrjälä 	/* Convert to pixel count */
6160b2a8e09SVille Syrjälä 	vbl_start *= htotal;
6170b2a8e09SVille Syrjälä 
6180b2a8e09SVille Syrjälä 	/* Start of vblank event occurs at start of hsync */
6190b2a8e09SVille Syrjälä 	vbl_start -= htotal - hsync_start;
6200b2a8e09SVille Syrjälä 
6219db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
6229db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
6235eddb70bSChris Wilson 
624694e409dSVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
625694e409dSVille Syrjälä 
6260a3e67a4SJesse Barnes 	/*
6270a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
6280a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
6290a3e67a4SJesse Barnes 	 * register.
6300a3e67a4SJesse Barnes 	 */
6310a3e67a4SJesse Barnes 	do {
6328cbda6b2SJani Nikula 		high1 = intel_de_read_fw(dev_priv, high_frame) & PIPE_FRAME_HIGH_MASK;
6338cbda6b2SJani Nikula 		low   = intel_de_read_fw(dev_priv, low_frame);
6348cbda6b2SJani Nikula 		high2 = intel_de_read_fw(dev_priv, high_frame) & PIPE_FRAME_HIGH_MASK;
6350a3e67a4SJesse Barnes 	} while (high1 != high2);
6360a3e67a4SJesse Barnes 
637694e409dSVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
638694e409dSVille Syrjälä 
6395eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
640391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
6415eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
642391f75e2SVille Syrjälä 
643391f75e2SVille Syrjälä 	/*
644391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
645391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
646391f75e2SVille Syrjälä 	 * counter against vblank start.
647391f75e2SVille Syrjälä 	 */
648edc08d0aSVille Syrjälä 	return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
6490a3e67a4SJesse Barnes }
6500a3e67a4SJesse Barnes 
65108fa8fd0SVille Syrjälä u32 g4x_get_vblank_counter(struct drm_crtc *crtc)
6529880b7a5SJesse Barnes {
65308fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
65408fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
6559880b7a5SJesse Barnes 
656649636efSVille Syrjälä 	return I915_READ(PIPE_FRMCOUNT_G4X(pipe));
6579880b7a5SJesse Barnes }
6589880b7a5SJesse Barnes 
659aec0246fSUma Shankar /*
660aec0246fSUma Shankar  * On certain encoders on certain platforms, pipe
661aec0246fSUma Shankar  * scanline register will not work to get the scanline,
662aec0246fSUma Shankar  * since the timings are driven from the PORT or issues
663aec0246fSUma Shankar  * with scanline register updates.
664aec0246fSUma Shankar  * This function will use Framestamp and current
665aec0246fSUma Shankar  * timestamp registers to calculate the scanline.
666aec0246fSUma Shankar  */
667aec0246fSUma Shankar static u32 __intel_get_crtc_scanline_from_timestamp(struct intel_crtc *crtc)
668aec0246fSUma Shankar {
669aec0246fSUma Shankar 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
670aec0246fSUma Shankar 	struct drm_vblank_crtc *vblank =
671aec0246fSUma Shankar 		&crtc->base.dev->vblank[drm_crtc_index(&crtc->base)];
672aec0246fSUma Shankar 	const struct drm_display_mode *mode = &vblank->hwmode;
673aec0246fSUma Shankar 	u32 vblank_start = mode->crtc_vblank_start;
674aec0246fSUma Shankar 	u32 vtotal = mode->crtc_vtotal;
675aec0246fSUma Shankar 	u32 htotal = mode->crtc_htotal;
676aec0246fSUma Shankar 	u32 clock = mode->crtc_clock;
677aec0246fSUma Shankar 	u32 scanline, scan_prev_time, scan_curr_time, scan_post_time;
678aec0246fSUma Shankar 
679aec0246fSUma Shankar 	/*
680aec0246fSUma Shankar 	 * To avoid the race condition where we might cross into the
681aec0246fSUma Shankar 	 * next vblank just between the PIPE_FRMTMSTMP and TIMESTAMP_CTR
682aec0246fSUma Shankar 	 * reads. We make sure we read PIPE_FRMTMSTMP and TIMESTAMP_CTR
683aec0246fSUma Shankar 	 * during the same frame.
684aec0246fSUma Shankar 	 */
685aec0246fSUma Shankar 	do {
686aec0246fSUma Shankar 		/*
687aec0246fSUma Shankar 		 * This field provides read back of the display
688aec0246fSUma Shankar 		 * pipe frame time stamp. The time stamp value
689aec0246fSUma Shankar 		 * is sampled at every start of vertical blank.
690aec0246fSUma Shankar 		 */
6918cbda6b2SJani Nikula 		scan_prev_time = intel_de_read_fw(dev_priv,
6928cbda6b2SJani Nikula 						  PIPE_FRMTMSTMP(crtc->pipe));
693aec0246fSUma Shankar 
694aec0246fSUma Shankar 		/*
695aec0246fSUma Shankar 		 * The TIMESTAMP_CTR register has the current
696aec0246fSUma Shankar 		 * time stamp value.
697aec0246fSUma Shankar 		 */
6988cbda6b2SJani Nikula 		scan_curr_time = intel_de_read_fw(dev_priv, IVB_TIMESTAMP_CTR);
699aec0246fSUma Shankar 
7008cbda6b2SJani Nikula 		scan_post_time = intel_de_read_fw(dev_priv,
7018cbda6b2SJani Nikula 						  PIPE_FRMTMSTMP(crtc->pipe));
702aec0246fSUma Shankar 	} while (scan_post_time != scan_prev_time);
703aec0246fSUma Shankar 
704aec0246fSUma Shankar 	scanline = div_u64(mul_u32_u32(scan_curr_time - scan_prev_time,
705aec0246fSUma Shankar 					clock), 1000 * htotal);
706aec0246fSUma Shankar 	scanline = min(scanline, vtotal - 1);
707aec0246fSUma Shankar 	scanline = (scanline + vblank_start) % vtotal;
708aec0246fSUma Shankar 
709aec0246fSUma Shankar 	return scanline;
710aec0246fSUma Shankar }
711aec0246fSUma Shankar 
7128cbda6b2SJani Nikula /*
7138cbda6b2SJani Nikula  * intel_de_read_fw(), only for fast reads of display block, no need for
7148cbda6b2SJani Nikula  * forcewake etc.
7158cbda6b2SJani Nikula  */
716a225f079SVille Syrjälä static int __intel_get_crtc_scanline(struct intel_crtc *crtc)
717a225f079SVille Syrjälä {
718a225f079SVille Syrjälä 	struct drm_device *dev = crtc->base.dev;
719fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
7205caa0feaSDaniel Vetter 	const struct drm_display_mode *mode;
7215caa0feaSDaniel Vetter 	struct drm_vblank_crtc *vblank;
722a225f079SVille Syrjälä 	enum pipe pipe = crtc->pipe;
72380715b2fSVille Syrjälä 	int position, vtotal;
724a225f079SVille Syrjälä 
72572259536SVille Syrjälä 	if (!crtc->active)
72672259536SVille Syrjälä 		return -1;
72772259536SVille Syrjälä 
7285caa0feaSDaniel Vetter 	vblank = &crtc->base.dev->vblank[drm_crtc_index(&crtc->base)];
7295caa0feaSDaniel Vetter 	mode = &vblank->hwmode;
7305caa0feaSDaniel Vetter 
731aec0246fSUma Shankar 	if (mode->private_flags & I915_MODE_FLAG_GET_SCANLINE_FROM_TIMESTAMP)
732aec0246fSUma Shankar 		return __intel_get_crtc_scanline_from_timestamp(crtc);
733aec0246fSUma Shankar 
73480715b2fSVille Syrjälä 	vtotal = mode->crtc_vtotal;
735a225f079SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
736a225f079SVille Syrjälä 		vtotal /= 2;
737a225f079SVille Syrjälä 
738cf819effSLucas De Marchi 	if (IS_GEN(dev_priv, 2))
7398cbda6b2SJani Nikula 		position = intel_de_read_fw(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
740a225f079SVille Syrjälä 	else
7418cbda6b2SJani Nikula 		position = intel_de_read_fw(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
742a225f079SVille Syrjälä 
743a225f079SVille Syrjälä 	/*
74441b578fbSJesse Barnes 	 * On HSW, the DSL reg (0x70000) appears to return 0 if we
74541b578fbSJesse Barnes 	 * read it just before the start of vblank.  So try it again
74641b578fbSJesse Barnes 	 * so we don't accidentally end up spanning a vblank frame
74741b578fbSJesse Barnes 	 * increment, causing the pipe_update_end() code to squak at us.
74841b578fbSJesse Barnes 	 *
74941b578fbSJesse Barnes 	 * The nature of this problem means we can't simply check the ISR
75041b578fbSJesse Barnes 	 * bit and return the vblank start value; nor can we use the scanline
75141b578fbSJesse Barnes 	 * debug register in the transcoder as it appears to have the same
75241b578fbSJesse Barnes 	 * problem.  We may need to extend this to include other platforms,
75341b578fbSJesse Barnes 	 * but so far testing only shows the problem on HSW.
75441b578fbSJesse Barnes 	 */
75591d14251STvrtko Ursulin 	if (HAS_DDI(dev_priv) && !position) {
75641b578fbSJesse Barnes 		int i, temp;
75741b578fbSJesse Barnes 
75841b578fbSJesse Barnes 		for (i = 0; i < 100; i++) {
75941b578fbSJesse Barnes 			udelay(1);
7608cbda6b2SJani Nikula 			temp = intel_de_read_fw(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
76141b578fbSJesse Barnes 			if (temp != position) {
76241b578fbSJesse Barnes 				position = temp;
76341b578fbSJesse Barnes 				break;
76441b578fbSJesse Barnes 			}
76541b578fbSJesse Barnes 		}
76641b578fbSJesse Barnes 	}
76741b578fbSJesse Barnes 
76841b578fbSJesse Barnes 	/*
76980715b2fSVille Syrjälä 	 * See update_scanline_offset() for the details on the
77080715b2fSVille Syrjälä 	 * scanline_offset adjustment.
771a225f079SVille Syrjälä 	 */
77280715b2fSVille Syrjälä 	return (position + crtc->scanline_offset) % vtotal;
773a225f079SVille Syrjälä }
774a225f079SVille Syrjälä 
775e8edae54SVille Syrjälä bool i915_get_crtc_scanoutpos(struct drm_device *dev, unsigned int index,
7761bf6ad62SDaniel Vetter 			      bool in_vblank_irq, int *vpos, int *hpos,
7773bb403bfSVille Syrjälä 			      ktime_t *stime, ktime_t *etime,
7783bb403bfSVille Syrjälä 			      const struct drm_display_mode *mode)
7790af7e4dfSMario Kleiner {
780fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
781e8edae54SVille Syrjälä 	struct intel_crtc *crtc = to_intel_crtc(drm_crtc_from_index(dev, index));
782e8edae54SVille Syrjälä 	enum pipe pipe = crtc->pipe;
7833aa18df8SVille Syrjälä 	int position;
78478e8fc6bSVille Syrjälä 	int vbl_start, vbl_end, hsync_start, htotal, vtotal;
785ad3543edSMario Kleiner 	unsigned long irqflags;
7868a920e24SVille Syrjälä 	bool use_scanline_counter = INTEL_GEN(dev_priv) >= 5 ||
7878a920e24SVille Syrjälä 		IS_G4X(dev_priv) || IS_GEN(dev_priv, 2) ||
7888a920e24SVille Syrjälä 		mode->private_flags & I915_MODE_FLAG_USE_SCANLINE_COUNTER;
7890af7e4dfSMario Kleiner 
79048a1b8d4SPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, !mode->crtc_clock)) {
791*00376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm,
792*00376ccfSWambui Karuga 			"trying to get scanoutpos for disabled "
7939db4a9c7SJesse Barnes 			"pipe %c\n", pipe_name(pipe));
7941bf6ad62SDaniel Vetter 		return false;
7950af7e4dfSMario Kleiner 	}
7960af7e4dfSMario Kleiner 
797c2baf4b7SVille Syrjälä 	htotal = mode->crtc_htotal;
79878e8fc6bSVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
799c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
800c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
801c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
8020af7e4dfSMario Kleiner 
803d31faf65SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
804d31faf65SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
805d31faf65SVille Syrjälä 		vbl_end /= 2;
806d31faf65SVille Syrjälä 		vtotal /= 2;
807d31faf65SVille Syrjälä 	}
808d31faf65SVille Syrjälä 
809ad3543edSMario Kleiner 	/*
810ad3543edSMario Kleiner 	 * Lock uncore.lock, as we will do multiple timing critical raw
811ad3543edSMario Kleiner 	 * register reads, potentially with preemption disabled, so the
812ad3543edSMario Kleiner 	 * following code must not block on uncore.lock.
813ad3543edSMario Kleiner 	 */
814ad3543edSMario Kleiner 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
815ad3543edSMario Kleiner 
816ad3543edSMario Kleiner 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
817ad3543edSMario Kleiner 
818ad3543edSMario Kleiner 	/* Get optional system timestamp before query. */
819ad3543edSMario Kleiner 	if (stime)
820ad3543edSMario Kleiner 		*stime = ktime_get();
821ad3543edSMario Kleiner 
8228a920e24SVille Syrjälä 	if (use_scanline_counter) {
8230af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
8240af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
8250af7e4dfSMario Kleiner 		 */
826e8edae54SVille Syrjälä 		position = __intel_get_crtc_scanline(crtc);
8270af7e4dfSMario Kleiner 	} else {
8280af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
8290af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
8300af7e4dfSMario Kleiner 		 * scanout position.
8310af7e4dfSMario Kleiner 		 */
8328cbda6b2SJani Nikula 		position = (intel_de_read_fw(dev_priv, PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
8330af7e4dfSMario Kleiner 
8343aa18df8SVille Syrjälä 		/* convert to pixel counts */
8353aa18df8SVille Syrjälä 		vbl_start *= htotal;
8363aa18df8SVille Syrjälä 		vbl_end *= htotal;
8373aa18df8SVille Syrjälä 		vtotal *= htotal;
83878e8fc6bSVille Syrjälä 
83978e8fc6bSVille Syrjälä 		/*
8407e78f1cbSVille Syrjälä 		 * In interlaced modes, the pixel counter counts all pixels,
8417e78f1cbSVille Syrjälä 		 * so one field will have htotal more pixels. In order to avoid
8427e78f1cbSVille Syrjälä 		 * the reported position from jumping backwards when the pixel
8437e78f1cbSVille Syrjälä 		 * counter is beyond the length of the shorter field, just
8447e78f1cbSVille Syrjälä 		 * clamp the position the length of the shorter field. This
8457e78f1cbSVille Syrjälä 		 * matches how the scanline counter based position works since
8467e78f1cbSVille Syrjälä 		 * the scanline counter doesn't count the two half lines.
8477e78f1cbSVille Syrjälä 		 */
8487e78f1cbSVille Syrjälä 		if (position >= vtotal)
8497e78f1cbSVille Syrjälä 			position = vtotal - 1;
8507e78f1cbSVille Syrjälä 
8517e78f1cbSVille Syrjälä 		/*
85278e8fc6bSVille Syrjälä 		 * Start of vblank interrupt is triggered at start of hsync,
85378e8fc6bSVille Syrjälä 		 * just prior to the first active line of vblank. However we
85478e8fc6bSVille Syrjälä 		 * consider lines to start at the leading edge of horizontal
85578e8fc6bSVille Syrjälä 		 * active. So, should we get here before we've crossed into
85678e8fc6bSVille Syrjälä 		 * the horizontal active of the first line in vblank, we would
85778e8fc6bSVille Syrjälä 		 * not set the DRM_SCANOUTPOS_INVBL flag. In order to fix that,
85878e8fc6bSVille Syrjälä 		 * always add htotal-hsync_start to the current pixel position.
85978e8fc6bSVille Syrjälä 		 */
86078e8fc6bSVille Syrjälä 		position = (position + htotal - hsync_start) % vtotal;
8613aa18df8SVille Syrjälä 	}
8623aa18df8SVille Syrjälä 
863ad3543edSMario Kleiner 	/* Get optional system timestamp after query. */
864ad3543edSMario Kleiner 	if (etime)
865ad3543edSMario Kleiner 		*etime = ktime_get();
866ad3543edSMario Kleiner 
867ad3543edSMario Kleiner 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
868ad3543edSMario Kleiner 
869ad3543edSMario Kleiner 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
870ad3543edSMario Kleiner 
8713aa18df8SVille Syrjälä 	/*
8723aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
8733aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
8743aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
8753aa18df8SVille Syrjälä 	 * up since vbl_end.
8763aa18df8SVille Syrjälä 	 */
8773aa18df8SVille Syrjälä 	if (position >= vbl_start)
8783aa18df8SVille Syrjälä 		position -= vbl_end;
8793aa18df8SVille Syrjälä 	else
8803aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
8813aa18df8SVille Syrjälä 
8828a920e24SVille Syrjälä 	if (use_scanline_counter) {
8833aa18df8SVille Syrjälä 		*vpos = position;
8843aa18df8SVille Syrjälä 		*hpos = 0;
8853aa18df8SVille Syrjälä 	} else {
8860af7e4dfSMario Kleiner 		*vpos = position / htotal;
8870af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
8880af7e4dfSMario Kleiner 	}
8890af7e4dfSMario Kleiner 
8901bf6ad62SDaniel Vetter 	return true;
8910af7e4dfSMario Kleiner }
8920af7e4dfSMario Kleiner 
893a225f079SVille Syrjälä int intel_get_crtc_scanline(struct intel_crtc *crtc)
894a225f079SVille Syrjälä {
895fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
896a225f079SVille Syrjälä 	unsigned long irqflags;
897a225f079SVille Syrjälä 	int position;
898a225f079SVille Syrjälä 
899a225f079SVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
900a225f079SVille Syrjälä 	position = __intel_get_crtc_scanline(crtc);
901a225f079SVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
902a225f079SVille Syrjälä 
903a225f079SVille Syrjälä 	return position;
904a225f079SVille Syrjälä }
905a225f079SVille Syrjälä 
906e3689190SBen Widawsky /**
90774bb98baSLucas De Marchi  * ivb_parity_work - Workqueue called when a parity error interrupt
908e3689190SBen Widawsky  * occurred.
909e3689190SBen Widawsky  * @work: workqueue struct
910e3689190SBen Widawsky  *
911e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
912e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
913e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
914e3689190SBen Widawsky  */
91574bb98baSLucas De Marchi static void ivb_parity_work(struct work_struct *work)
916e3689190SBen Widawsky {
9172d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
918cefcff8fSJoonas Lahtinen 		container_of(work, typeof(*dev_priv), l3_parity.error_work);
919cf1c97dcSAndi Shyti 	struct intel_gt *gt = &dev_priv->gt;
920e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
92135a85ac6SBen Widawsky 	char *parity_event[6];
922a9c287c9SJani Nikula 	u32 misccpctl;
923a9c287c9SJani Nikula 	u8 slice = 0;
924e3689190SBen Widawsky 
925e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
926e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
927e3689190SBen Widawsky 	 * any time we access those registers.
928e3689190SBen Widawsky 	 */
92991c8a326SChris Wilson 	mutex_lock(&dev_priv->drm.struct_mutex);
930e3689190SBen Widawsky 
93135a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
93248a1b8d4SPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, !dev_priv->l3_parity.which_slice))
93335a85ac6SBen Widawsky 		goto out;
93435a85ac6SBen Widawsky 
935e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
936e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
937e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
938e3689190SBen Widawsky 
93935a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
940f0f59a00SVille Syrjälä 		i915_reg_t reg;
94135a85ac6SBen Widawsky 
94235a85ac6SBen Widawsky 		slice--;
94348a1b8d4SPankaj Bharadiya 		if (drm_WARN_ON_ONCE(&dev_priv->drm,
94448a1b8d4SPankaj Bharadiya 				     slice >= NUM_L3_SLICES(dev_priv)))
94535a85ac6SBen Widawsky 			break;
94635a85ac6SBen Widawsky 
94735a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
94835a85ac6SBen Widawsky 
9496fa1c5f1SVille Syrjälä 		reg = GEN7_L3CDERRST1(slice);
95035a85ac6SBen Widawsky 
95135a85ac6SBen Widawsky 		error_status = I915_READ(reg);
952e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
953e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
954e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
955e3689190SBen Widawsky 
95635a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
95735a85ac6SBen Widawsky 		POSTING_READ(reg);
958e3689190SBen Widawsky 
959cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
960e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
961e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
962e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
96335a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
96435a85ac6SBen Widawsky 		parity_event[5] = NULL;
965e3689190SBen Widawsky 
96691c8a326SChris Wilson 		kobject_uevent_env(&dev_priv->drm.primary->kdev->kobj,
967e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
968e3689190SBen Widawsky 
96935a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
97035a85ac6SBen Widawsky 			  slice, row, bank, subbank);
971e3689190SBen Widawsky 
97235a85ac6SBen Widawsky 		kfree(parity_event[4]);
973e3689190SBen Widawsky 		kfree(parity_event[3]);
974e3689190SBen Widawsky 		kfree(parity_event[2]);
975e3689190SBen Widawsky 		kfree(parity_event[1]);
976e3689190SBen Widawsky 	}
977e3689190SBen Widawsky 
97835a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
97935a85ac6SBen Widawsky 
98035a85ac6SBen Widawsky out:
98148a1b8d4SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, dev_priv->l3_parity.which_slice);
982cf1c97dcSAndi Shyti 	spin_lock_irq(&gt->irq_lock);
983cf1c97dcSAndi Shyti 	gen5_gt_enable_irq(gt, GT_PARITY_ERROR(dev_priv));
984cf1c97dcSAndi Shyti 	spin_unlock_irq(&gt->irq_lock);
98535a85ac6SBen Widawsky 
98691c8a326SChris Wilson 	mutex_unlock(&dev_priv->drm.struct_mutex);
98735a85ac6SBen Widawsky }
98835a85ac6SBen Widawsky 
989af92058fSVille Syrjälä static bool gen11_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
990121e758eSDhinakaran Pandiyan {
991af92058fSVille Syrjälä 	switch (pin) {
992af92058fSVille Syrjälä 	case HPD_PORT_C:
993121e758eSDhinakaran Pandiyan 		return val & GEN11_HOTPLUG_CTL_LONG_DETECT(PORT_TC1);
994af92058fSVille Syrjälä 	case HPD_PORT_D:
995121e758eSDhinakaran Pandiyan 		return val & GEN11_HOTPLUG_CTL_LONG_DETECT(PORT_TC2);
996af92058fSVille Syrjälä 	case HPD_PORT_E:
997121e758eSDhinakaran Pandiyan 		return val & GEN11_HOTPLUG_CTL_LONG_DETECT(PORT_TC3);
998af92058fSVille Syrjälä 	case HPD_PORT_F:
999121e758eSDhinakaran Pandiyan 		return val & GEN11_HOTPLUG_CTL_LONG_DETECT(PORT_TC4);
1000121e758eSDhinakaran Pandiyan 	default:
1001121e758eSDhinakaran Pandiyan 		return false;
1002121e758eSDhinakaran Pandiyan 	}
1003121e758eSDhinakaran Pandiyan }
1004121e758eSDhinakaran Pandiyan 
100548ef15d3SJosé Roberto de Souza static bool gen12_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
100648ef15d3SJosé Roberto de Souza {
100748ef15d3SJosé Roberto de Souza 	switch (pin) {
100848ef15d3SJosé Roberto de Souza 	case HPD_PORT_D:
100948ef15d3SJosé Roberto de Souza 		return val & GEN11_HOTPLUG_CTL_LONG_DETECT(PORT_TC1);
101048ef15d3SJosé Roberto de Souza 	case HPD_PORT_E:
101148ef15d3SJosé Roberto de Souza 		return val & GEN11_HOTPLUG_CTL_LONG_DETECT(PORT_TC2);
101248ef15d3SJosé Roberto de Souza 	case HPD_PORT_F:
101348ef15d3SJosé Roberto de Souza 		return val & GEN11_HOTPLUG_CTL_LONG_DETECT(PORT_TC3);
101448ef15d3SJosé Roberto de Souza 	case HPD_PORT_G:
101548ef15d3SJosé Roberto de Souza 		return val & GEN11_HOTPLUG_CTL_LONG_DETECT(PORT_TC4);
101648ef15d3SJosé Roberto de Souza 	case HPD_PORT_H:
101748ef15d3SJosé Roberto de Souza 		return val & GEN11_HOTPLUG_CTL_LONG_DETECT(PORT_TC5);
101848ef15d3SJosé Roberto de Souza 	case HPD_PORT_I:
101948ef15d3SJosé Roberto de Souza 		return val & GEN11_HOTPLUG_CTL_LONG_DETECT(PORT_TC6);
102048ef15d3SJosé Roberto de Souza 	default:
102148ef15d3SJosé Roberto de Souza 		return false;
102248ef15d3SJosé Roberto de Souza 	}
102348ef15d3SJosé Roberto de Souza }
102448ef15d3SJosé Roberto de Souza 
1025af92058fSVille Syrjälä static bool bxt_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
102663c88d22SImre Deak {
1027af92058fSVille Syrjälä 	switch (pin) {
1028af92058fSVille Syrjälä 	case HPD_PORT_A:
1029195baa06SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
1030af92058fSVille Syrjälä 	case HPD_PORT_B:
103163c88d22SImre Deak 		return val & PORTB_HOTPLUG_LONG_DETECT;
1032af92058fSVille Syrjälä 	case HPD_PORT_C:
103363c88d22SImre Deak 		return val & PORTC_HOTPLUG_LONG_DETECT;
103463c88d22SImre Deak 	default:
103563c88d22SImre Deak 		return false;
103663c88d22SImre Deak 	}
103763c88d22SImre Deak }
103863c88d22SImre Deak 
1039af92058fSVille Syrjälä static bool icp_ddi_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
104031604222SAnusha Srivatsa {
1041af92058fSVille Syrjälä 	switch (pin) {
1042af92058fSVille Syrjälä 	case HPD_PORT_A:
1043ed3126faSLucas De Marchi 		return val & SHOTPLUG_CTL_DDI_HPD_LONG_DETECT(PORT_A);
1044af92058fSVille Syrjälä 	case HPD_PORT_B:
1045ed3126faSLucas De Marchi 		return val & SHOTPLUG_CTL_DDI_HPD_LONG_DETECT(PORT_B);
10468ef7e340SMatt Roper 	case HPD_PORT_C:
1047ed3126faSLucas De Marchi 		return val & SHOTPLUG_CTL_DDI_HPD_LONG_DETECT(PORT_C);
104831604222SAnusha Srivatsa 	default:
104931604222SAnusha Srivatsa 		return false;
105031604222SAnusha Srivatsa 	}
105131604222SAnusha Srivatsa }
105231604222SAnusha Srivatsa 
1053af92058fSVille Syrjälä static bool icp_tc_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
105431604222SAnusha Srivatsa {
1055af92058fSVille Syrjälä 	switch (pin) {
1056af92058fSVille Syrjälä 	case HPD_PORT_C:
105731604222SAnusha Srivatsa 		return val & ICP_TC_HPD_LONG_DETECT(PORT_TC1);
1058af92058fSVille Syrjälä 	case HPD_PORT_D:
105931604222SAnusha Srivatsa 		return val & ICP_TC_HPD_LONG_DETECT(PORT_TC2);
1060af92058fSVille Syrjälä 	case HPD_PORT_E:
106131604222SAnusha Srivatsa 		return val & ICP_TC_HPD_LONG_DETECT(PORT_TC3);
1062af92058fSVille Syrjälä 	case HPD_PORT_F:
106331604222SAnusha Srivatsa 		return val & ICP_TC_HPD_LONG_DETECT(PORT_TC4);
106431604222SAnusha Srivatsa 	default:
106531604222SAnusha Srivatsa 		return false;
106631604222SAnusha Srivatsa 	}
106731604222SAnusha Srivatsa }
106831604222SAnusha Srivatsa 
106952dfdba0SLucas De Marchi static bool tgp_tc_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
107052dfdba0SLucas De Marchi {
107152dfdba0SLucas De Marchi 	switch (pin) {
107252dfdba0SLucas De Marchi 	case HPD_PORT_D:
107352dfdba0SLucas De Marchi 		return val & ICP_TC_HPD_LONG_DETECT(PORT_TC1);
107452dfdba0SLucas De Marchi 	case HPD_PORT_E:
107552dfdba0SLucas De Marchi 		return val & ICP_TC_HPD_LONG_DETECT(PORT_TC2);
107652dfdba0SLucas De Marchi 	case HPD_PORT_F:
107752dfdba0SLucas De Marchi 		return val & ICP_TC_HPD_LONG_DETECT(PORT_TC3);
107852dfdba0SLucas De Marchi 	case HPD_PORT_G:
107952dfdba0SLucas De Marchi 		return val & ICP_TC_HPD_LONG_DETECT(PORT_TC4);
108052dfdba0SLucas De Marchi 	case HPD_PORT_H:
108152dfdba0SLucas De Marchi 		return val & ICP_TC_HPD_LONG_DETECT(PORT_TC5);
108252dfdba0SLucas De Marchi 	case HPD_PORT_I:
108352dfdba0SLucas De Marchi 		return val & ICP_TC_HPD_LONG_DETECT(PORT_TC6);
108452dfdba0SLucas De Marchi 	default:
108552dfdba0SLucas De Marchi 		return false;
108652dfdba0SLucas De Marchi 	}
108752dfdba0SLucas De Marchi }
108852dfdba0SLucas De Marchi 
1089af92058fSVille Syrjälä static bool spt_port_hotplug2_long_detect(enum hpd_pin pin, u32 val)
10906dbf30ceSVille Syrjälä {
1091af92058fSVille Syrjälä 	switch (pin) {
1092af92058fSVille Syrjälä 	case HPD_PORT_E:
10936dbf30ceSVille Syrjälä 		return val & PORTE_HOTPLUG_LONG_DETECT;
10946dbf30ceSVille Syrjälä 	default:
10956dbf30ceSVille Syrjälä 		return false;
10966dbf30ceSVille Syrjälä 	}
10976dbf30ceSVille Syrjälä }
10986dbf30ceSVille Syrjälä 
1099af92058fSVille Syrjälä static bool spt_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
110074c0b395SVille Syrjälä {
1101af92058fSVille Syrjälä 	switch (pin) {
1102af92058fSVille Syrjälä 	case HPD_PORT_A:
110374c0b395SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
1104af92058fSVille Syrjälä 	case HPD_PORT_B:
110574c0b395SVille Syrjälä 		return val & PORTB_HOTPLUG_LONG_DETECT;
1106af92058fSVille Syrjälä 	case HPD_PORT_C:
110774c0b395SVille Syrjälä 		return val & PORTC_HOTPLUG_LONG_DETECT;
1108af92058fSVille Syrjälä 	case HPD_PORT_D:
110974c0b395SVille Syrjälä 		return val & PORTD_HOTPLUG_LONG_DETECT;
111074c0b395SVille Syrjälä 	default:
111174c0b395SVille Syrjälä 		return false;
111274c0b395SVille Syrjälä 	}
111374c0b395SVille Syrjälä }
111474c0b395SVille Syrjälä 
1115af92058fSVille Syrjälä static bool ilk_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
1116e4ce95aaSVille Syrjälä {
1117af92058fSVille Syrjälä 	switch (pin) {
1118af92058fSVille Syrjälä 	case HPD_PORT_A:
1119e4ce95aaSVille Syrjälä 		return val & DIGITAL_PORTA_HOTPLUG_LONG_DETECT;
1120e4ce95aaSVille Syrjälä 	default:
1121e4ce95aaSVille Syrjälä 		return false;
1122e4ce95aaSVille Syrjälä 	}
1123e4ce95aaSVille Syrjälä }
1124e4ce95aaSVille Syrjälä 
1125af92058fSVille Syrjälä static bool pch_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
112613cf5504SDave Airlie {
1127af92058fSVille Syrjälä 	switch (pin) {
1128af92058fSVille Syrjälä 	case HPD_PORT_B:
1129676574dfSJani Nikula 		return val & PORTB_HOTPLUG_LONG_DETECT;
1130af92058fSVille Syrjälä 	case HPD_PORT_C:
1131676574dfSJani Nikula 		return val & PORTC_HOTPLUG_LONG_DETECT;
1132af92058fSVille Syrjälä 	case HPD_PORT_D:
1133676574dfSJani Nikula 		return val & PORTD_HOTPLUG_LONG_DETECT;
1134676574dfSJani Nikula 	default:
1135676574dfSJani Nikula 		return false;
113613cf5504SDave Airlie 	}
113713cf5504SDave Airlie }
113813cf5504SDave Airlie 
1139af92058fSVille Syrjälä static bool i9xx_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
114013cf5504SDave Airlie {
1141af92058fSVille Syrjälä 	switch (pin) {
1142af92058fSVille Syrjälä 	case HPD_PORT_B:
1143676574dfSJani Nikula 		return val & PORTB_HOTPLUG_INT_LONG_PULSE;
1144af92058fSVille Syrjälä 	case HPD_PORT_C:
1145676574dfSJani Nikula 		return val & PORTC_HOTPLUG_INT_LONG_PULSE;
1146af92058fSVille Syrjälä 	case HPD_PORT_D:
1147676574dfSJani Nikula 		return val & PORTD_HOTPLUG_INT_LONG_PULSE;
1148676574dfSJani Nikula 	default:
1149676574dfSJani Nikula 		return false;
115013cf5504SDave Airlie 	}
115113cf5504SDave Airlie }
115213cf5504SDave Airlie 
115342db67d6SVille Syrjälä /*
115442db67d6SVille Syrjälä  * Get a bit mask of pins that have triggered, and which ones may be long.
115542db67d6SVille Syrjälä  * This can be called multiple times with the same masks to accumulate
115642db67d6SVille Syrjälä  * hotplug detection results from several registers.
115742db67d6SVille Syrjälä  *
115842db67d6SVille Syrjälä  * Note that the caller is expected to zero out the masks initially.
115942db67d6SVille Syrjälä  */
1160cf53902fSRodrigo Vivi static void intel_get_hpd_pins(struct drm_i915_private *dev_priv,
1161cf53902fSRodrigo Vivi 			       u32 *pin_mask, u32 *long_mask,
11628c841e57SJani Nikula 			       u32 hotplug_trigger, u32 dig_hotplug_reg,
1163fd63e2a9SImre Deak 			       const u32 hpd[HPD_NUM_PINS],
1164af92058fSVille Syrjälä 			       bool long_pulse_detect(enum hpd_pin pin, u32 val))
1165676574dfSJani Nikula {
1166e9be2850SVille Syrjälä 	enum hpd_pin pin;
1167676574dfSJani Nikula 
116852dfdba0SLucas De Marchi 	BUILD_BUG_ON(BITS_PER_TYPE(*pin_mask) < HPD_NUM_PINS);
116952dfdba0SLucas De Marchi 
1170e9be2850SVille Syrjälä 	for_each_hpd_pin(pin) {
1171e9be2850SVille Syrjälä 		if ((hpd[pin] & hotplug_trigger) == 0)
11728c841e57SJani Nikula 			continue;
11738c841e57SJani Nikula 
1174e9be2850SVille Syrjälä 		*pin_mask |= BIT(pin);
1175676574dfSJani Nikula 
1176af92058fSVille Syrjälä 		if (long_pulse_detect(pin, dig_hotplug_reg))
1177e9be2850SVille Syrjälä 			*long_mask |= BIT(pin);
1178676574dfSJani Nikula 	}
1179676574dfSJani Nikula 
1180*00376ccfSWambui Karuga 	drm_dbg(&dev_priv->drm,
1181*00376ccfSWambui Karuga 		"hotplug event received, stat 0x%08x, dig 0x%08x, pins 0x%08x, long 0x%08x\n",
1182f88f0478SVille Syrjälä 		hotplug_trigger, dig_hotplug_reg, *pin_mask, *long_mask);
1183676574dfSJani Nikula 
1184676574dfSJani Nikula }
1185676574dfSJani Nikula 
118691d14251STvrtko Ursulin static void gmbus_irq_handler(struct drm_i915_private *dev_priv)
1187515ac2bbSDaniel Vetter {
118828c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1189515ac2bbSDaniel Vetter }
1190515ac2bbSDaniel Vetter 
119191d14251STvrtko Ursulin static void dp_aux_irq_handler(struct drm_i915_private *dev_priv)
1192ce99c256SDaniel Vetter {
11939ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1194ce99c256SDaniel Vetter }
1195ce99c256SDaniel Vetter 
11968bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
119791d14251STvrtko Ursulin static void display_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
119891d14251STvrtko Ursulin 					 enum pipe pipe,
1199a9c287c9SJani Nikula 					 u32 crc0, u32 crc1,
1200a9c287c9SJani Nikula 					 u32 crc2, u32 crc3,
1201a9c287c9SJani Nikula 					 u32 crc4)
12028bf1e9f1SShuang He {
12038bf1e9f1SShuang He 	struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
12048c6b709dSTomeu Vizoso 	struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
12055cee6c45SVille Syrjälä 	u32 crcs[5] = { crc0, crc1, crc2, crc3, crc4 };
12065cee6c45SVille Syrjälä 
12075cee6c45SVille Syrjälä 	trace_intel_pipe_crc(crtc, crcs);
1208b2c88f5bSDamien Lespiau 
1209d538bbdfSDamien Lespiau 	spin_lock(&pipe_crc->lock);
12108c6b709dSTomeu Vizoso 	/*
12118c6b709dSTomeu Vizoso 	 * For some not yet identified reason, the first CRC is
12128c6b709dSTomeu Vizoso 	 * bonkers. So let's just wait for the next vblank and read
12138c6b709dSTomeu Vizoso 	 * out the buggy result.
12148c6b709dSTomeu Vizoso 	 *
1215163e8aecSRodrigo Vivi 	 * On GEN8+ sometimes the second CRC is bonkers as well, so
12168c6b709dSTomeu Vizoso 	 * don't trust that one either.
12178c6b709dSTomeu Vizoso 	 */
1218033b7a23SMaarten Lankhorst 	if (pipe_crc->skipped <= 0 ||
1219163e8aecSRodrigo Vivi 	    (INTEL_GEN(dev_priv) >= 8 && pipe_crc->skipped == 1)) {
12208c6b709dSTomeu Vizoso 		pipe_crc->skipped++;
12218c6b709dSTomeu Vizoso 		spin_unlock(&pipe_crc->lock);
12228c6b709dSTomeu Vizoso 		return;
12238c6b709dSTomeu Vizoso 	}
12248c6b709dSTomeu Vizoso 	spin_unlock(&pipe_crc->lock);
12256cc42152SMaarten Lankhorst 
1226246ee524STomeu Vizoso 	drm_crtc_add_crc_entry(&crtc->base, true,
1227ca814b25SDaniel Vetter 				drm_crtc_accurate_vblank_count(&crtc->base),
1228246ee524STomeu Vizoso 				crcs);
12298c6b709dSTomeu Vizoso }
1230277de95eSDaniel Vetter #else
1231277de95eSDaniel Vetter static inline void
123291d14251STvrtko Ursulin display_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
123391d14251STvrtko Ursulin 			     enum pipe pipe,
1234a9c287c9SJani Nikula 			     u32 crc0, u32 crc1,
1235a9c287c9SJani Nikula 			     u32 crc2, u32 crc3,
1236a9c287c9SJani Nikula 			     u32 crc4) {}
1237277de95eSDaniel Vetter #endif
1238eba94eb9SDaniel Vetter 
1239277de95eSDaniel Vetter 
124091d14251STvrtko Ursulin static void hsw_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
124191d14251STvrtko Ursulin 				     enum pipe pipe)
12425a69b89fSDaniel Vetter {
124391d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
12445a69b89fSDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
12455a69b89fSDaniel Vetter 				     0, 0, 0, 0);
12465a69b89fSDaniel Vetter }
12475a69b89fSDaniel Vetter 
124891d14251STvrtko Ursulin static void ivb_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
124991d14251STvrtko Ursulin 				     enum pipe pipe)
1250eba94eb9SDaniel Vetter {
125191d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
1252eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1253eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1254eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1255eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
12568bc5e955SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1257eba94eb9SDaniel Vetter }
12585b3a856bSDaniel Vetter 
125991d14251STvrtko Ursulin static void i9xx_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
126091d14251STvrtko Ursulin 				      enum pipe pipe)
12615b3a856bSDaniel Vetter {
1262a9c287c9SJani Nikula 	u32 res1, res2;
12630b5c5ed0SDaniel Vetter 
126491d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 3)
12650b5c5ed0SDaniel Vetter 		res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
12660b5c5ed0SDaniel Vetter 	else
12670b5c5ed0SDaniel Vetter 		res1 = 0;
12680b5c5ed0SDaniel Vetter 
126991d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
12700b5c5ed0SDaniel Vetter 		res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
12710b5c5ed0SDaniel Vetter 	else
12720b5c5ed0SDaniel Vetter 		res2 = 0;
12735b3a856bSDaniel Vetter 
127491d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
12750b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_RED(pipe)),
12760b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_GREEN(pipe)),
12770b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_BLUE(pipe)),
12780b5c5ed0SDaniel Vetter 				     res1, res2);
12795b3a856bSDaniel Vetter }
12808bf1e9f1SShuang He 
128144d9241eSVille Syrjälä static void i9xx_pipestat_irq_reset(struct drm_i915_private *dev_priv)
128244d9241eSVille Syrjälä {
128344d9241eSVille Syrjälä 	enum pipe pipe;
128444d9241eSVille Syrjälä 
128544d9241eSVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
128644d9241eSVille Syrjälä 		I915_WRITE(PIPESTAT(pipe),
128744d9241eSVille Syrjälä 			   PIPESTAT_INT_STATUS_MASK |
128844d9241eSVille Syrjälä 			   PIPE_FIFO_UNDERRUN_STATUS);
128944d9241eSVille Syrjälä 
129044d9241eSVille Syrjälä 		dev_priv->pipestat_irq_mask[pipe] = 0;
129144d9241eSVille Syrjälä 	}
129244d9241eSVille Syrjälä }
129344d9241eSVille Syrjälä 
1294eb64343cSVille Syrjälä static void i9xx_pipestat_irq_ack(struct drm_i915_private *dev_priv,
129591d14251STvrtko Ursulin 				  u32 iir, u32 pipe_stats[I915_MAX_PIPES])
12967e231dbeSJesse Barnes {
1297d048a268SVille Syrjälä 	enum pipe pipe;
12987e231dbeSJesse Barnes 
129958ead0d7SImre Deak 	spin_lock(&dev_priv->irq_lock);
13001ca993d2SVille Syrjälä 
13011ca993d2SVille Syrjälä 	if (!dev_priv->display_irqs_enabled) {
13021ca993d2SVille Syrjälä 		spin_unlock(&dev_priv->irq_lock);
13031ca993d2SVille Syrjälä 		return;
13041ca993d2SVille Syrjälä 	}
13051ca993d2SVille Syrjälä 
1306055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1307f0f59a00SVille Syrjälä 		i915_reg_t reg;
13086b12ca56SVille Syrjälä 		u32 status_mask, enable_mask, iir_bit = 0;
130991d181ddSImre Deak 
1310bbb5eebfSDaniel Vetter 		/*
1311bbb5eebfSDaniel Vetter 		 * PIPESTAT bits get signalled even when the interrupt is
1312bbb5eebfSDaniel Vetter 		 * disabled with the mask bits, and some of the status bits do
1313bbb5eebfSDaniel Vetter 		 * not generate interrupts at all (like the underrun bit). Hence
1314bbb5eebfSDaniel Vetter 		 * we need to be careful that we only handle what we want to
1315bbb5eebfSDaniel Vetter 		 * handle.
1316bbb5eebfSDaniel Vetter 		 */
13170f239f4cSDaniel Vetter 
13180f239f4cSDaniel Vetter 		/* fifo underruns are filterered in the underrun handler. */
13196b12ca56SVille Syrjälä 		status_mask = PIPE_FIFO_UNDERRUN_STATUS;
1320bbb5eebfSDaniel Vetter 
1321bbb5eebfSDaniel Vetter 		switch (pipe) {
1322d048a268SVille Syrjälä 		default:
1323bbb5eebfSDaniel Vetter 		case PIPE_A:
1324bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
1325bbb5eebfSDaniel Vetter 			break;
1326bbb5eebfSDaniel Vetter 		case PIPE_B:
1327bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
1328bbb5eebfSDaniel Vetter 			break;
13293278f67fSVille Syrjälä 		case PIPE_C:
13303278f67fSVille Syrjälä 			iir_bit = I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
13313278f67fSVille Syrjälä 			break;
1332bbb5eebfSDaniel Vetter 		}
1333bbb5eebfSDaniel Vetter 		if (iir & iir_bit)
13346b12ca56SVille Syrjälä 			status_mask |= dev_priv->pipestat_irq_mask[pipe];
1335bbb5eebfSDaniel Vetter 
13366b12ca56SVille Syrjälä 		if (!status_mask)
133791d181ddSImre Deak 			continue;
133891d181ddSImre Deak 
133991d181ddSImre Deak 		reg = PIPESTAT(pipe);
13406b12ca56SVille Syrjälä 		pipe_stats[pipe] = I915_READ(reg) & status_mask;
13416b12ca56SVille Syrjälä 		enable_mask = i915_pipestat_enable_mask(dev_priv, pipe);
13427e231dbeSJesse Barnes 
13437e231dbeSJesse Barnes 		/*
13447e231dbeSJesse Barnes 		 * Clear the PIPE*STAT regs before the IIR
1345132c27c9SVille Syrjälä 		 *
1346132c27c9SVille Syrjälä 		 * Toggle the enable bits to make sure we get an
1347132c27c9SVille Syrjälä 		 * edge in the ISR pipe event bit if we don't clear
1348132c27c9SVille Syrjälä 		 * all the enabled status bits. Otherwise the edge
1349132c27c9SVille Syrjälä 		 * triggered IIR on i965/g4x wouldn't notice that
1350132c27c9SVille Syrjälä 		 * an interrupt is still pending.
13517e231dbeSJesse Barnes 		 */
1352132c27c9SVille Syrjälä 		if (pipe_stats[pipe]) {
1353132c27c9SVille Syrjälä 			I915_WRITE(reg, pipe_stats[pipe]);
1354132c27c9SVille Syrjälä 			I915_WRITE(reg, enable_mask);
1355132c27c9SVille Syrjälä 		}
13567e231dbeSJesse Barnes 	}
135758ead0d7SImre Deak 	spin_unlock(&dev_priv->irq_lock);
13582ecb8ca4SVille Syrjälä }
13592ecb8ca4SVille Syrjälä 
1360eb64343cSVille Syrjälä static void i8xx_pipestat_irq_handler(struct drm_i915_private *dev_priv,
1361eb64343cSVille Syrjälä 				      u16 iir, u32 pipe_stats[I915_MAX_PIPES])
1362eb64343cSVille Syrjälä {
1363eb64343cSVille Syrjälä 	enum pipe pipe;
1364eb64343cSVille Syrjälä 
1365eb64343cSVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
1366eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
1367eb64343cSVille Syrjälä 			drm_handle_vblank(&dev_priv->drm, pipe);
1368eb64343cSVille Syrjälä 
1369eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1370eb64343cSVille Syrjälä 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
1371eb64343cSVille Syrjälä 
1372eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
1373eb64343cSVille Syrjälä 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
1374eb64343cSVille Syrjälä 	}
1375eb64343cSVille Syrjälä }
1376eb64343cSVille Syrjälä 
1377eb64343cSVille Syrjälä static void i915_pipestat_irq_handler(struct drm_i915_private *dev_priv,
1378eb64343cSVille Syrjälä 				      u32 iir, u32 pipe_stats[I915_MAX_PIPES])
1379eb64343cSVille Syrjälä {
1380eb64343cSVille Syrjälä 	bool blc_event = false;
1381eb64343cSVille Syrjälä 	enum pipe pipe;
1382eb64343cSVille Syrjälä 
1383eb64343cSVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
1384eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
1385eb64343cSVille Syrjälä 			drm_handle_vblank(&dev_priv->drm, pipe);
1386eb64343cSVille Syrjälä 
1387eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
1388eb64343cSVille Syrjälä 			blc_event = true;
1389eb64343cSVille Syrjälä 
1390eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1391eb64343cSVille Syrjälä 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
1392eb64343cSVille Syrjälä 
1393eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
1394eb64343cSVille Syrjälä 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
1395eb64343cSVille Syrjälä 	}
1396eb64343cSVille Syrjälä 
1397eb64343cSVille Syrjälä 	if (blc_event || (iir & I915_ASLE_INTERRUPT))
1398eb64343cSVille Syrjälä 		intel_opregion_asle_intr(dev_priv);
1399eb64343cSVille Syrjälä }
1400eb64343cSVille Syrjälä 
1401eb64343cSVille Syrjälä static void i965_pipestat_irq_handler(struct drm_i915_private *dev_priv,
1402eb64343cSVille Syrjälä 				      u32 iir, u32 pipe_stats[I915_MAX_PIPES])
1403eb64343cSVille Syrjälä {
1404eb64343cSVille Syrjälä 	bool blc_event = false;
1405eb64343cSVille Syrjälä 	enum pipe pipe;
1406eb64343cSVille Syrjälä 
1407eb64343cSVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
1408eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS)
1409eb64343cSVille Syrjälä 			drm_handle_vblank(&dev_priv->drm, pipe);
1410eb64343cSVille Syrjälä 
1411eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
1412eb64343cSVille Syrjälä 			blc_event = true;
1413eb64343cSVille Syrjälä 
1414eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1415eb64343cSVille Syrjälä 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
1416eb64343cSVille Syrjälä 
1417eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
1418eb64343cSVille Syrjälä 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
1419eb64343cSVille Syrjälä 	}
1420eb64343cSVille Syrjälä 
1421eb64343cSVille Syrjälä 	if (blc_event || (iir & I915_ASLE_INTERRUPT))
1422eb64343cSVille Syrjälä 		intel_opregion_asle_intr(dev_priv);
1423eb64343cSVille Syrjälä 
1424eb64343cSVille Syrjälä 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1425eb64343cSVille Syrjälä 		gmbus_irq_handler(dev_priv);
1426eb64343cSVille Syrjälä }
1427eb64343cSVille Syrjälä 
142891d14251STvrtko Ursulin static void valleyview_pipestat_irq_handler(struct drm_i915_private *dev_priv,
14292ecb8ca4SVille Syrjälä 					    u32 pipe_stats[I915_MAX_PIPES])
14302ecb8ca4SVille Syrjälä {
14312ecb8ca4SVille Syrjälä 	enum pipe pipe;
14327e231dbeSJesse Barnes 
1433055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1434fd3a4024SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS)
1435fd3a4024SDaniel Vetter 			drm_handle_vblank(&dev_priv->drm, pipe);
14364356d586SDaniel Vetter 
14374356d586SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
143891d14251STvrtko Ursulin 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
14392d9d2b0bSVille Syrjälä 
14401f7247c0SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
14411f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
144231acc7f5SJesse Barnes 	}
144331acc7f5SJesse Barnes 
1444c1874ed7SImre Deak 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
144591d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
1446c1874ed7SImre Deak }
1447c1874ed7SImre Deak 
14481ae3c34cSVille Syrjälä static u32 i9xx_hpd_irq_ack(struct drm_i915_private *dev_priv)
144916c6c56bSVille Syrjälä {
14500ba7c51aSVille Syrjälä 	u32 hotplug_status = 0, hotplug_status_mask;
14510ba7c51aSVille Syrjälä 	int i;
145216c6c56bSVille Syrjälä 
14530ba7c51aSVille Syrjälä 	if (IS_G4X(dev_priv) ||
14540ba7c51aSVille Syrjälä 	    IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
14550ba7c51aSVille Syrjälä 		hotplug_status_mask = HOTPLUG_INT_STATUS_G4X |
14560ba7c51aSVille Syrjälä 			DP_AUX_CHANNEL_MASK_INT_STATUS_G4X;
14570ba7c51aSVille Syrjälä 	else
14580ba7c51aSVille Syrjälä 		hotplug_status_mask = HOTPLUG_INT_STATUS_I915;
14590ba7c51aSVille Syrjälä 
14600ba7c51aSVille Syrjälä 	/*
14610ba7c51aSVille Syrjälä 	 * We absolutely have to clear all the pending interrupt
14620ba7c51aSVille Syrjälä 	 * bits in PORT_HOTPLUG_STAT. Otherwise the ISR port
14630ba7c51aSVille Syrjälä 	 * interrupt bit won't have an edge, and the i965/g4x
14640ba7c51aSVille Syrjälä 	 * edge triggered IIR will not notice that an interrupt
14650ba7c51aSVille Syrjälä 	 * is still pending. We can't use PORT_HOTPLUG_EN to
14660ba7c51aSVille Syrjälä 	 * guarantee the edge as the act of toggling the enable
14670ba7c51aSVille Syrjälä 	 * bits can itself generate a new hotplug interrupt :(
14680ba7c51aSVille Syrjälä 	 */
14690ba7c51aSVille Syrjälä 	for (i = 0; i < 10; i++) {
14700ba7c51aSVille Syrjälä 		u32 tmp = I915_READ(PORT_HOTPLUG_STAT) & hotplug_status_mask;
14710ba7c51aSVille Syrjälä 
14720ba7c51aSVille Syrjälä 		if (tmp == 0)
14730ba7c51aSVille Syrjälä 			return hotplug_status;
14740ba7c51aSVille Syrjälä 
14750ba7c51aSVille Syrjälä 		hotplug_status |= tmp;
14763ff60f89SOscar Mateo 		I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
14770ba7c51aSVille Syrjälä 	}
14780ba7c51aSVille Syrjälä 
147948a1b8d4SPankaj Bharadiya 	drm_WARN_ONCE(&dev_priv->drm, 1,
14800ba7c51aSVille Syrjälä 		      "PORT_HOTPLUG_STAT did not clear (0x%08x)\n",
14810ba7c51aSVille Syrjälä 		      I915_READ(PORT_HOTPLUG_STAT));
14821ae3c34cSVille Syrjälä 
14831ae3c34cSVille Syrjälä 	return hotplug_status;
14841ae3c34cSVille Syrjälä }
14851ae3c34cSVille Syrjälä 
148691d14251STvrtko Ursulin static void i9xx_hpd_irq_handler(struct drm_i915_private *dev_priv,
14871ae3c34cSVille Syrjälä 				 u32 hotplug_status)
14881ae3c34cSVille Syrjälä {
14891ae3c34cSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
14903ff60f89SOscar Mateo 
149191d14251STvrtko Ursulin 	if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
149291d14251STvrtko Ursulin 	    IS_CHERRYVIEW(dev_priv)) {
149316c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
149416c6c56bSVille Syrjälä 
149558f2cf24SVille Syrjälä 		if (hotplug_trigger) {
1496cf53902fSRodrigo Vivi 			intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
1497cf53902fSRodrigo Vivi 					   hotplug_trigger, hotplug_trigger,
1498cf53902fSRodrigo Vivi 					   hpd_status_g4x,
1499fd63e2a9SImre Deak 					   i9xx_port_hotplug_long_detect);
150058f2cf24SVille Syrjälä 
150191d14251STvrtko Ursulin 			intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
150258f2cf24SVille Syrjälä 		}
1503369712e8SJani Nikula 
1504369712e8SJani Nikula 		if (hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
150591d14251STvrtko Ursulin 			dp_aux_irq_handler(dev_priv);
150616c6c56bSVille Syrjälä 	} else {
150716c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
150816c6c56bSVille Syrjälä 
150958f2cf24SVille Syrjälä 		if (hotplug_trigger) {
1510cf53902fSRodrigo Vivi 			intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
1511cf53902fSRodrigo Vivi 					   hotplug_trigger, hotplug_trigger,
1512cf53902fSRodrigo Vivi 					   hpd_status_i915,
1513fd63e2a9SImre Deak 					   i9xx_port_hotplug_long_detect);
151491d14251STvrtko Ursulin 			intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
151516c6c56bSVille Syrjälä 		}
15163ff60f89SOscar Mateo 	}
151758f2cf24SVille Syrjälä }
151816c6c56bSVille Syrjälä 
1519c1874ed7SImre Deak static irqreturn_t valleyview_irq_handler(int irq, void *arg)
1520c1874ed7SImre Deak {
1521b318b824SVille Syrjälä 	struct drm_i915_private *dev_priv = arg;
1522c1874ed7SImre Deak 	irqreturn_t ret = IRQ_NONE;
1523c1874ed7SImre Deak 
15242dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
15252dd2a883SImre Deak 		return IRQ_NONE;
15262dd2a883SImre Deak 
15271f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
15289102650fSDaniele Ceraolo Spurio 	disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
15291f814dacSImre Deak 
15301e1cace9SVille Syrjälä 	do {
15316e814800SVille Syrjälä 		u32 iir, gt_iir, pm_iir;
15322ecb8ca4SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
15331ae3c34cSVille Syrjälä 		u32 hotplug_status = 0;
1534a5e485a9SVille Syrjälä 		u32 ier = 0;
15353ff60f89SOscar Mateo 
1536c1874ed7SImre Deak 		gt_iir = I915_READ(GTIIR);
1537c1874ed7SImre Deak 		pm_iir = I915_READ(GEN6_PMIIR);
15383ff60f89SOscar Mateo 		iir = I915_READ(VLV_IIR);
1539c1874ed7SImre Deak 
1540c1874ed7SImre Deak 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
15411e1cace9SVille Syrjälä 			break;
1542c1874ed7SImre Deak 
1543c1874ed7SImre Deak 		ret = IRQ_HANDLED;
1544c1874ed7SImre Deak 
1545a5e485a9SVille Syrjälä 		/*
1546a5e485a9SVille Syrjälä 		 * Theory on interrupt generation, based on empirical evidence:
1547a5e485a9SVille Syrjälä 		 *
1548a5e485a9SVille Syrjälä 		 * x = ((VLV_IIR & VLV_IER) ||
1549a5e485a9SVille Syrjälä 		 *      (((GT_IIR & GT_IER) || (GEN6_PMIIR & GEN6_PMIER)) &&
1550a5e485a9SVille Syrjälä 		 *       (VLV_MASTER_IER & MASTER_INTERRUPT_ENABLE)));
1551a5e485a9SVille Syrjälä 		 *
1552a5e485a9SVille Syrjälä 		 * A CPU interrupt will only be raised when 'x' has a 0->1 edge.
1553a5e485a9SVille Syrjälä 		 * Hence we clear MASTER_INTERRUPT_ENABLE and VLV_IER to
1554a5e485a9SVille Syrjälä 		 * guarantee the CPU interrupt will be raised again even if we
1555a5e485a9SVille Syrjälä 		 * don't end up clearing all the VLV_IIR, GT_IIR, GEN6_PMIIR
1556a5e485a9SVille Syrjälä 		 * bits this time around.
1557a5e485a9SVille Syrjälä 		 */
15584a0a0202SVille Syrjälä 		I915_WRITE(VLV_MASTER_IER, 0);
1559a5e485a9SVille Syrjälä 		ier = I915_READ(VLV_IER);
1560a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, 0);
15614a0a0202SVille Syrjälä 
15624a0a0202SVille Syrjälä 		if (gt_iir)
15634a0a0202SVille Syrjälä 			I915_WRITE(GTIIR, gt_iir);
15644a0a0202SVille Syrjälä 		if (pm_iir)
15654a0a0202SVille Syrjälä 			I915_WRITE(GEN6_PMIIR, pm_iir);
15664a0a0202SVille Syrjälä 
15677ce4d1f2SVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
15681ae3c34cSVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
15697ce4d1f2SVille Syrjälä 
15703ff60f89SOscar Mateo 		/* Call regardless, as some status bits might not be
15713ff60f89SOscar Mateo 		 * signalled in iir */
1572eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
15737ce4d1f2SVille Syrjälä 
1574eef57324SJerome Anand 		if (iir & (I915_LPE_PIPE_A_INTERRUPT |
1575eef57324SJerome Anand 			   I915_LPE_PIPE_B_INTERRUPT))
1576eef57324SJerome Anand 			intel_lpe_audio_irq_handler(dev_priv);
1577eef57324SJerome Anand 
15787ce4d1f2SVille Syrjälä 		/*
15797ce4d1f2SVille Syrjälä 		 * VLV_IIR is single buffered, and reflects the level
15807ce4d1f2SVille Syrjälä 		 * from PIPESTAT/PORT_HOTPLUG_STAT, hence clear it last.
15817ce4d1f2SVille Syrjälä 		 */
15827ce4d1f2SVille Syrjälä 		if (iir)
15837ce4d1f2SVille Syrjälä 			I915_WRITE(VLV_IIR, iir);
15844a0a0202SVille Syrjälä 
1585a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, ier);
15864a0a0202SVille Syrjälä 		I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
15871ae3c34cSVille Syrjälä 
158852894874SVille Syrjälä 		if (gt_iir)
1589cf1c97dcSAndi Shyti 			gen6_gt_irq_handler(&dev_priv->gt, gt_iir);
159052894874SVille Syrjälä 		if (pm_iir)
15913e7abf81SAndi Shyti 			gen6_rps_irq_handler(&dev_priv->gt.rps, pm_iir);
159252894874SVille Syrjälä 
15931ae3c34cSVille Syrjälä 		if (hotplug_status)
159491d14251STvrtko Ursulin 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
15952ecb8ca4SVille Syrjälä 
159691d14251STvrtko Ursulin 		valleyview_pipestat_irq_handler(dev_priv, pipe_stats);
15971e1cace9SVille Syrjälä 	} while (0);
15987e231dbeSJesse Barnes 
15999102650fSDaniele Ceraolo Spurio 	enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
16001f814dacSImre Deak 
16017e231dbeSJesse Barnes 	return ret;
16027e231dbeSJesse Barnes }
16037e231dbeSJesse Barnes 
160443f328d7SVille Syrjälä static irqreturn_t cherryview_irq_handler(int irq, void *arg)
160543f328d7SVille Syrjälä {
1606b318b824SVille Syrjälä 	struct drm_i915_private *dev_priv = arg;
160743f328d7SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
160843f328d7SVille Syrjälä 
16092dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
16102dd2a883SImre Deak 		return IRQ_NONE;
16112dd2a883SImre Deak 
16121f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
16139102650fSDaniele Ceraolo Spurio 	disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
16141f814dacSImre Deak 
1615579de73bSChris Wilson 	do {
16166e814800SVille Syrjälä 		u32 master_ctl, iir;
16172ecb8ca4SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
16181ae3c34cSVille Syrjälä 		u32 hotplug_status = 0;
1619a5e485a9SVille Syrjälä 		u32 ier = 0;
1620a5e485a9SVille Syrjälä 
16218e5fd599SVille Syrjälä 		master_ctl = I915_READ(GEN8_MASTER_IRQ) & ~GEN8_MASTER_IRQ_CONTROL;
16223278f67fSVille Syrjälä 		iir = I915_READ(VLV_IIR);
16233278f67fSVille Syrjälä 
16243278f67fSVille Syrjälä 		if (master_ctl == 0 && iir == 0)
16258e5fd599SVille Syrjälä 			break;
162643f328d7SVille Syrjälä 
162727b6c122SOscar Mateo 		ret = IRQ_HANDLED;
162827b6c122SOscar Mateo 
1629a5e485a9SVille Syrjälä 		/*
1630a5e485a9SVille Syrjälä 		 * Theory on interrupt generation, based on empirical evidence:
1631a5e485a9SVille Syrjälä 		 *
1632a5e485a9SVille Syrjälä 		 * x = ((VLV_IIR & VLV_IER) ||
1633a5e485a9SVille Syrjälä 		 *      ((GEN8_MASTER_IRQ & ~GEN8_MASTER_IRQ_CONTROL) &&
1634a5e485a9SVille Syrjälä 		 *       (GEN8_MASTER_IRQ & GEN8_MASTER_IRQ_CONTROL)));
1635a5e485a9SVille Syrjälä 		 *
1636a5e485a9SVille Syrjälä 		 * A CPU interrupt will only be raised when 'x' has a 0->1 edge.
1637a5e485a9SVille Syrjälä 		 * Hence we clear GEN8_MASTER_IRQ_CONTROL and VLV_IER to
1638a5e485a9SVille Syrjälä 		 * guarantee the CPU interrupt will be raised again even if we
1639a5e485a9SVille Syrjälä 		 * don't end up clearing all the VLV_IIR and GEN8_MASTER_IRQ_CONTROL
1640a5e485a9SVille Syrjälä 		 * bits this time around.
1641a5e485a9SVille Syrjälä 		 */
164243f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, 0);
1643a5e485a9SVille Syrjälä 		ier = I915_READ(VLV_IER);
1644a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, 0);
164543f328d7SVille Syrjälä 
16466cc32f15SChris Wilson 		gen8_gt_irq_handler(&dev_priv->gt, master_ctl);
164727b6c122SOscar Mateo 
164827b6c122SOscar Mateo 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
16491ae3c34cSVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
165043f328d7SVille Syrjälä 
165127b6c122SOscar Mateo 		/* Call regardless, as some status bits might not be
165227b6c122SOscar Mateo 		 * signalled in iir */
1653eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
165443f328d7SVille Syrjälä 
1655eef57324SJerome Anand 		if (iir & (I915_LPE_PIPE_A_INTERRUPT |
1656eef57324SJerome Anand 			   I915_LPE_PIPE_B_INTERRUPT |
1657eef57324SJerome Anand 			   I915_LPE_PIPE_C_INTERRUPT))
1658eef57324SJerome Anand 			intel_lpe_audio_irq_handler(dev_priv);
1659eef57324SJerome Anand 
16607ce4d1f2SVille Syrjälä 		/*
16617ce4d1f2SVille Syrjälä 		 * VLV_IIR is single buffered, and reflects the level
16627ce4d1f2SVille Syrjälä 		 * from PIPESTAT/PORT_HOTPLUG_STAT, hence clear it last.
16637ce4d1f2SVille Syrjälä 		 */
16647ce4d1f2SVille Syrjälä 		if (iir)
16657ce4d1f2SVille Syrjälä 			I915_WRITE(VLV_IIR, iir);
16667ce4d1f2SVille Syrjälä 
1667a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, ier);
1668e5328c43SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
16691ae3c34cSVille Syrjälä 
16701ae3c34cSVille Syrjälä 		if (hotplug_status)
167191d14251STvrtko Ursulin 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
16722ecb8ca4SVille Syrjälä 
167391d14251STvrtko Ursulin 		valleyview_pipestat_irq_handler(dev_priv, pipe_stats);
1674579de73bSChris Wilson 	} while (0);
16753278f67fSVille Syrjälä 
16769102650fSDaniele Ceraolo Spurio 	enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
16771f814dacSImre Deak 
167843f328d7SVille Syrjälä 	return ret;
167943f328d7SVille Syrjälä }
168043f328d7SVille Syrjälä 
168191d14251STvrtko Ursulin static void ibx_hpd_irq_handler(struct drm_i915_private *dev_priv,
168291d14251STvrtko Ursulin 				u32 hotplug_trigger,
168340e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
1684776ad806SJesse Barnes {
168542db67d6SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
1686776ad806SJesse Barnes 
16876a39d7c9SJani Nikula 	/*
16886a39d7c9SJani Nikula 	 * Somehow the PCH doesn't seem to really ack the interrupt to the CPU
16896a39d7c9SJani Nikula 	 * unless we touch the hotplug register, even if hotplug_trigger is
16906a39d7c9SJani Nikula 	 * zero. Not acking leads to "The master control interrupt lied (SDE)!"
16916a39d7c9SJani Nikula 	 * errors.
16926a39d7c9SJani Nikula 	 */
169313cf5504SDave Airlie 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
16946a39d7c9SJani Nikula 	if (!hotplug_trigger) {
16956a39d7c9SJani Nikula 		u32 mask = PORTA_HOTPLUG_STATUS_MASK |
16966a39d7c9SJani Nikula 			PORTD_HOTPLUG_STATUS_MASK |
16976a39d7c9SJani Nikula 			PORTC_HOTPLUG_STATUS_MASK |
16986a39d7c9SJani Nikula 			PORTB_HOTPLUG_STATUS_MASK;
16996a39d7c9SJani Nikula 		dig_hotplug_reg &= ~mask;
17006a39d7c9SJani Nikula 	}
17016a39d7c9SJani Nikula 
170213cf5504SDave Airlie 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
17036a39d7c9SJani Nikula 	if (!hotplug_trigger)
17046a39d7c9SJani Nikula 		return;
170513cf5504SDave Airlie 
1706cf53902fSRodrigo Vivi 	intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask, hotplug_trigger,
170740e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
1708fd63e2a9SImre Deak 			   pch_port_hotplug_long_detect);
170940e56410SVille Syrjälä 
171091d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
1711aaf5ec2eSSonika Jindal }
171291d131d2SDaniel Vetter 
171391d14251STvrtko Ursulin static void ibx_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
171440e56410SVille Syrjälä {
1715d048a268SVille Syrjälä 	enum pipe pipe;
171640e56410SVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
171740e56410SVille Syrjälä 
171891d14251STvrtko Ursulin 	ibx_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ibx);
171940e56410SVille Syrjälä 
1720cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1721cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1722776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1723*00376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm, "PCH audio power change on port %d\n",
1724cfc33bf7SVille Syrjälä 			port_name(port));
1725cfc33bf7SVille Syrjälä 	}
1726776ad806SJesse Barnes 
1727ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
172891d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
1729ce99c256SDaniel Vetter 
1730776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
173191d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
1732776ad806SJesse Barnes 
1733776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1734*00376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm, "PCH HDCP audio interrupt\n");
1735776ad806SJesse Barnes 
1736776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1737*00376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm, "PCH transcoder audio interrupt\n");
1738776ad806SJesse Barnes 
1739776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1740*00376ccfSWambui Karuga 		drm_err(&dev_priv->drm, "PCH poison interrupt\n");
1741776ad806SJesse Barnes 
17429db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
1743055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
1744*00376ccfSWambui Karuga 			drm_dbg(&dev_priv->drm, "  pipe %c FDI IIR: 0x%08x\n",
17459db4a9c7SJesse Barnes 				pipe_name(pipe),
17469db4a9c7SJesse Barnes 				I915_READ(FDI_RX_IIR(pipe)));
1747776ad806SJesse Barnes 
1748776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1749*00376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm, "PCH transcoder CRC done interrupt\n");
1750776ad806SJesse Barnes 
1751776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1752*00376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm,
1753*00376ccfSWambui Karuga 			"PCH transcoder CRC error interrupt\n");
1754776ad806SJesse Barnes 
1755776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
1756a2196033SMatthias Kaehlcke 		intel_pch_fifo_underrun_irq_handler(dev_priv, PIPE_A);
17578664281bSPaulo Zanoni 
17588664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
1759a2196033SMatthias Kaehlcke 		intel_pch_fifo_underrun_irq_handler(dev_priv, PIPE_B);
17608664281bSPaulo Zanoni }
17618664281bSPaulo Zanoni 
176291d14251STvrtko Ursulin static void ivb_err_int_handler(struct drm_i915_private *dev_priv)
17638664281bSPaulo Zanoni {
17648664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
17655a69b89fSDaniel Vetter 	enum pipe pipe;
17668664281bSPaulo Zanoni 
1767de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
1768*00376ccfSWambui Karuga 		drm_err(&dev_priv->drm, "Poison interrupt\n");
1769de032bf4SPaulo Zanoni 
1770055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
17711f7247c0SDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe))
17721f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
17738664281bSPaulo Zanoni 
17745a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
177591d14251STvrtko Ursulin 			if (IS_IVYBRIDGE(dev_priv))
177691d14251STvrtko Ursulin 				ivb_pipe_crc_irq_handler(dev_priv, pipe);
17775a69b89fSDaniel Vetter 			else
177891d14251STvrtko Ursulin 				hsw_pipe_crc_irq_handler(dev_priv, pipe);
17795a69b89fSDaniel Vetter 		}
17805a69b89fSDaniel Vetter 	}
17818bf1e9f1SShuang He 
17828664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
17838664281bSPaulo Zanoni }
17848664281bSPaulo Zanoni 
178591d14251STvrtko Ursulin static void cpt_serr_int_handler(struct drm_i915_private *dev_priv)
17868664281bSPaulo Zanoni {
17878664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
178845c1cd87SMika Kahola 	enum pipe pipe;
17898664281bSPaulo Zanoni 
1790de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
1791*00376ccfSWambui Karuga 		drm_err(&dev_priv->drm, "PCH poison interrupt\n");
1792de032bf4SPaulo Zanoni 
179345c1cd87SMika Kahola 	for_each_pipe(dev_priv, pipe)
179445c1cd87SMika Kahola 		if (serr_int & SERR_INT_TRANS_FIFO_UNDERRUN(pipe))
179545c1cd87SMika Kahola 			intel_pch_fifo_underrun_irq_handler(dev_priv, pipe);
17968664281bSPaulo Zanoni 
17978664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
1798776ad806SJesse Barnes }
1799776ad806SJesse Barnes 
180091d14251STvrtko Ursulin static void cpt_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
180123e81d69SAdam Jackson {
1802d048a268SVille Syrjälä 	enum pipe pipe;
18036dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
1804aaf5ec2eSSonika Jindal 
180591d14251STvrtko Ursulin 	ibx_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_cpt);
180691d131d2SDaniel Vetter 
1807cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
1808cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
180923e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
1810*00376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm, "PCH audio power change on port %c\n",
1811cfc33bf7SVille Syrjälä 			port_name(port));
1812cfc33bf7SVille Syrjälä 	}
181323e81d69SAdam Jackson 
181423e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
181591d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
181623e81d69SAdam Jackson 
181723e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
181891d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
181923e81d69SAdam Jackson 
182023e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
1821*00376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm, "Audio CP request interrupt\n");
182223e81d69SAdam Jackson 
182323e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
1824*00376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm, "Audio CP change interrupt\n");
182523e81d69SAdam Jackson 
182623e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
1827055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
1828*00376ccfSWambui Karuga 			drm_dbg(&dev_priv->drm, "  pipe %c FDI IIR: 0x%08x\n",
182923e81d69SAdam Jackson 				pipe_name(pipe),
183023e81d69SAdam Jackson 				I915_READ(FDI_RX_IIR(pipe)));
18318664281bSPaulo Zanoni 
18328664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
183391d14251STvrtko Ursulin 		cpt_serr_int_handler(dev_priv);
183423e81d69SAdam Jackson }
183523e81d69SAdam Jackson 
183658676af6SLucas De Marchi static void icp_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
183731604222SAnusha Srivatsa {
183858676af6SLucas De Marchi 	u32 ddi_hotplug_trigger, tc_hotplug_trigger;
183931604222SAnusha Srivatsa 	u32 pin_mask = 0, long_mask = 0;
184058676af6SLucas De Marchi 	bool (*tc_port_hotplug_long_detect)(enum hpd_pin pin, u32 val);
184158676af6SLucas De Marchi 	const u32 *pins;
184231604222SAnusha Srivatsa 
184358676af6SLucas De Marchi 	if (HAS_PCH_TGP(dev_priv)) {
184458676af6SLucas De Marchi 		ddi_hotplug_trigger = pch_iir & SDE_DDI_MASK_TGP;
184558676af6SLucas De Marchi 		tc_hotplug_trigger = pch_iir & SDE_TC_MASK_TGP;
184658676af6SLucas De Marchi 		tc_port_hotplug_long_detect = tgp_tc_port_hotplug_long_detect;
184758676af6SLucas De Marchi 		pins = hpd_tgp;
1848943682e3SMatt Roper 	} else if (HAS_PCH_JSP(dev_priv)) {
1849943682e3SMatt Roper 		ddi_hotplug_trigger = pch_iir & SDE_DDI_MASK_TGP;
1850943682e3SMatt Roper 		tc_hotplug_trigger = 0;
1851943682e3SMatt Roper 		pins = hpd_tgp;
185258676af6SLucas De Marchi 	} else if (HAS_PCH_MCC(dev_priv)) {
185353448aedSVivek Kasireddy 		ddi_hotplug_trigger = pch_iir & SDE_DDI_MASK_ICP;
185453448aedSVivek Kasireddy 		tc_hotplug_trigger = pch_iir & SDE_TC_HOTPLUG_ICP(PORT_TC1);
1855fcb9bba4SMatt Roper 		tc_port_hotplug_long_detect = icp_tc_port_hotplug_long_detect;
1856d09ad3e7SMatt Roper 		pins = hpd_icp;
18578ef7e340SMatt Roper 	} else {
185848a1b8d4SPankaj Bharadiya 		drm_WARN(&dev_priv->drm, !HAS_PCH_ICP(dev_priv),
185948a1b8d4SPankaj Bharadiya 			 "Unrecognized PCH type 0x%x\n",
186048a1b8d4SPankaj Bharadiya 			 INTEL_PCH_TYPE(dev_priv));
1861943682e3SMatt Roper 
18628ef7e340SMatt Roper 		ddi_hotplug_trigger = pch_iir & SDE_DDI_MASK_ICP;
18638ef7e340SMatt Roper 		tc_hotplug_trigger = pch_iir & SDE_TC_MASK_ICP;
186458676af6SLucas De Marchi 		tc_port_hotplug_long_detect = icp_tc_port_hotplug_long_detect;
186558676af6SLucas De Marchi 		pins = hpd_icp;
18668ef7e340SMatt Roper 	}
18678ef7e340SMatt Roper 
186831604222SAnusha Srivatsa 	if (ddi_hotplug_trigger) {
186931604222SAnusha Srivatsa 		u32 dig_hotplug_reg;
187031604222SAnusha Srivatsa 
187131604222SAnusha Srivatsa 		dig_hotplug_reg = I915_READ(SHOTPLUG_CTL_DDI);
187231604222SAnusha Srivatsa 		I915_WRITE(SHOTPLUG_CTL_DDI, dig_hotplug_reg);
187331604222SAnusha Srivatsa 
187431604222SAnusha Srivatsa 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
187531604222SAnusha Srivatsa 				   ddi_hotplug_trigger,
1876c6f7acb8SMatt Roper 				   dig_hotplug_reg, pins,
187731604222SAnusha Srivatsa 				   icp_ddi_port_hotplug_long_detect);
187831604222SAnusha Srivatsa 	}
187931604222SAnusha Srivatsa 
188031604222SAnusha Srivatsa 	if (tc_hotplug_trigger) {
188131604222SAnusha Srivatsa 		u32 dig_hotplug_reg;
188231604222SAnusha Srivatsa 
188331604222SAnusha Srivatsa 		dig_hotplug_reg = I915_READ(SHOTPLUG_CTL_TC);
188431604222SAnusha Srivatsa 		I915_WRITE(SHOTPLUG_CTL_TC, dig_hotplug_reg);
188531604222SAnusha Srivatsa 
188631604222SAnusha Srivatsa 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
188731604222SAnusha Srivatsa 				   tc_hotplug_trigger,
1888c6f7acb8SMatt Roper 				   dig_hotplug_reg, pins,
188958676af6SLucas De Marchi 				   tc_port_hotplug_long_detect);
189052dfdba0SLucas De Marchi 	}
189152dfdba0SLucas De Marchi 
189252dfdba0SLucas De Marchi 	if (pin_mask)
189352dfdba0SLucas De Marchi 		intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
189452dfdba0SLucas De Marchi 
189552dfdba0SLucas De Marchi 	if (pch_iir & SDE_GMBUS_ICP)
189652dfdba0SLucas De Marchi 		gmbus_irq_handler(dev_priv);
189752dfdba0SLucas De Marchi }
189852dfdba0SLucas De Marchi 
189991d14251STvrtko Ursulin static void spt_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
19006dbf30ceSVille Syrjälä {
19016dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_SPT &
19026dbf30ceSVille Syrjälä 		~SDE_PORTE_HOTPLUG_SPT;
19036dbf30ceSVille Syrjälä 	u32 hotplug2_trigger = pch_iir & SDE_PORTE_HOTPLUG_SPT;
19046dbf30ceSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
19056dbf30ceSVille Syrjälä 
19066dbf30ceSVille Syrjälä 	if (hotplug_trigger) {
19076dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
19086dbf30ceSVille Syrjälä 
19096dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
19106dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
19116dbf30ceSVille Syrjälä 
1912cf53902fSRodrigo Vivi 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
1913cf53902fSRodrigo Vivi 				   hotplug_trigger, dig_hotplug_reg, hpd_spt,
191474c0b395SVille Syrjälä 				   spt_port_hotplug_long_detect);
19156dbf30ceSVille Syrjälä 	}
19166dbf30ceSVille Syrjälä 
19176dbf30ceSVille Syrjälä 	if (hotplug2_trigger) {
19186dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
19196dbf30ceSVille Syrjälä 
19206dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG2);
19216dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG2, dig_hotplug_reg);
19226dbf30ceSVille Syrjälä 
1923cf53902fSRodrigo Vivi 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
1924cf53902fSRodrigo Vivi 				   hotplug2_trigger, dig_hotplug_reg, hpd_spt,
19256dbf30ceSVille Syrjälä 				   spt_port_hotplug2_long_detect);
19266dbf30ceSVille Syrjälä 	}
19276dbf30ceSVille Syrjälä 
19286dbf30ceSVille Syrjälä 	if (pin_mask)
192991d14251STvrtko Ursulin 		intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
19306dbf30ceSVille Syrjälä 
19316dbf30ceSVille Syrjälä 	if (pch_iir & SDE_GMBUS_CPT)
193291d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
19336dbf30ceSVille Syrjälä }
19346dbf30ceSVille Syrjälä 
193591d14251STvrtko Ursulin static void ilk_hpd_irq_handler(struct drm_i915_private *dev_priv,
193691d14251STvrtko Ursulin 				u32 hotplug_trigger,
193740e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
1938c008bc6eSPaulo Zanoni {
1939e4ce95aaSVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
1940e4ce95aaSVille Syrjälä 
1941e4ce95aaSVille Syrjälä 	dig_hotplug_reg = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
1942e4ce95aaSVille Syrjälä 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, dig_hotplug_reg);
1943e4ce95aaSVille Syrjälä 
1944cf53902fSRodrigo Vivi 	intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask, hotplug_trigger,
194540e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
1946e4ce95aaSVille Syrjälä 			   ilk_port_hotplug_long_detect);
194740e56410SVille Syrjälä 
194891d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
1949e4ce95aaSVille Syrjälä }
1950c008bc6eSPaulo Zanoni 
195191d14251STvrtko Ursulin static void ilk_display_irq_handler(struct drm_i915_private *dev_priv,
195291d14251STvrtko Ursulin 				    u32 de_iir)
195340e56410SVille Syrjälä {
195440e56410SVille Syrjälä 	enum pipe pipe;
195540e56410SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG;
195640e56410SVille Syrjälä 
195740e56410SVille Syrjälä 	if (hotplug_trigger)
195891d14251STvrtko Ursulin 		ilk_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ilk);
195940e56410SVille Syrjälä 
1960c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
196191d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
1962c008bc6eSPaulo Zanoni 
1963c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
196491d14251STvrtko Ursulin 		intel_opregion_asle_intr(dev_priv);
1965c008bc6eSPaulo Zanoni 
1966c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
1967*00376ccfSWambui Karuga 		drm_err(&dev_priv->drm, "Poison interrupt\n");
1968c008bc6eSPaulo Zanoni 
1969055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1970fd3a4024SDaniel Vetter 		if (de_iir & DE_PIPE_VBLANK(pipe))
1971fd3a4024SDaniel Vetter 			drm_handle_vblank(&dev_priv->drm, pipe);
1972c008bc6eSPaulo Zanoni 
197340da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
19741f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
1975c008bc6eSPaulo Zanoni 
197640da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
197791d14251STvrtko Ursulin 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
1978c008bc6eSPaulo Zanoni 	}
1979c008bc6eSPaulo Zanoni 
1980c008bc6eSPaulo Zanoni 	/* check event from PCH */
1981c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
1982c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
1983c008bc6eSPaulo Zanoni 
198491d14251STvrtko Ursulin 		if (HAS_PCH_CPT(dev_priv))
198591d14251STvrtko Ursulin 			cpt_irq_handler(dev_priv, pch_iir);
1986c008bc6eSPaulo Zanoni 		else
198791d14251STvrtko Ursulin 			ibx_irq_handler(dev_priv, pch_iir);
1988c008bc6eSPaulo Zanoni 
1989c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
1990c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
1991c008bc6eSPaulo Zanoni 	}
1992c008bc6eSPaulo Zanoni 
1993cf819effSLucas De Marchi 	if (IS_GEN(dev_priv, 5) && de_iir & DE_PCU_EVENT)
19943e7abf81SAndi Shyti 		gen5_rps_irq_handler(&dev_priv->gt.rps);
1995c008bc6eSPaulo Zanoni }
1996c008bc6eSPaulo Zanoni 
199791d14251STvrtko Ursulin static void ivb_display_irq_handler(struct drm_i915_private *dev_priv,
199891d14251STvrtko Ursulin 				    u32 de_iir)
19999719fb98SPaulo Zanoni {
200007d27e20SDamien Lespiau 	enum pipe pipe;
200123bb4cb5SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG_IVB;
200223bb4cb5SVille Syrjälä 
200340e56410SVille Syrjälä 	if (hotplug_trigger)
200491d14251STvrtko Ursulin 		ilk_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ivb);
20059719fb98SPaulo Zanoni 
20069719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
200791d14251STvrtko Ursulin 		ivb_err_int_handler(dev_priv);
20089719fb98SPaulo Zanoni 
200954fd3149SDhinakaran Pandiyan 	if (de_iir & DE_EDP_PSR_INT_HSW) {
201054fd3149SDhinakaran Pandiyan 		u32 psr_iir = I915_READ(EDP_PSR_IIR);
201154fd3149SDhinakaran Pandiyan 
201254fd3149SDhinakaran Pandiyan 		intel_psr_irq_handler(dev_priv, psr_iir);
201354fd3149SDhinakaran Pandiyan 		I915_WRITE(EDP_PSR_IIR, psr_iir);
201454fd3149SDhinakaran Pandiyan 	}
2015fc340442SDaniel Vetter 
20169719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
201791d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
20189719fb98SPaulo Zanoni 
20199719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
202091d14251STvrtko Ursulin 		intel_opregion_asle_intr(dev_priv);
20219719fb98SPaulo Zanoni 
2022055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2023fd3a4024SDaniel Vetter 		if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)))
2024fd3a4024SDaniel Vetter 			drm_handle_vblank(&dev_priv->drm, pipe);
20259719fb98SPaulo Zanoni 	}
20269719fb98SPaulo Zanoni 
20279719fb98SPaulo Zanoni 	/* check event from PCH */
202891d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv) && (de_iir & DE_PCH_EVENT_IVB)) {
20299719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
20309719fb98SPaulo Zanoni 
203191d14251STvrtko Ursulin 		cpt_irq_handler(dev_priv, pch_iir);
20329719fb98SPaulo Zanoni 
20339719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
20349719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
20359719fb98SPaulo Zanoni 	}
20369719fb98SPaulo Zanoni }
20379719fb98SPaulo Zanoni 
203872c90f62SOscar Mateo /*
203972c90f62SOscar Mateo  * To handle irqs with the minimum potential races with fresh interrupts, we:
204072c90f62SOscar Mateo  * 1 - Disable Master Interrupt Control.
204172c90f62SOscar Mateo  * 2 - Find the source(s) of the interrupt.
204272c90f62SOscar Mateo  * 3 - Clear the Interrupt Identity bits (IIR).
204372c90f62SOscar Mateo  * 4 - Process the interrupt(s) that had bits set in the IIRs.
204472c90f62SOscar Mateo  * 5 - Re-enable Master Interrupt Control.
204572c90f62SOscar Mateo  */
20469eae5e27SLucas De Marchi static irqreturn_t ilk_irq_handler(int irq, void *arg)
2047b1f14ad0SJesse Barnes {
2048b318b824SVille Syrjälä 	struct drm_i915_private *dev_priv = arg;
2049f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
20500e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
2051b1f14ad0SJesse Barnes 
20522dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
20532dd2a883SImre Deak 		return IRQ_NONE;
20542dd2a883SImre Deak 
20551f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
20569102650fSDaniele Ceraolo Spurio 	disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
20571f814dacSImre Deak 
2058b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
2059b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
2060b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
20610e43406bSChris Wilson 
206244498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
206344498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
206444498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
206544498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
206644498aeaSPaulo Zanoni 	 * due to its back queue). */
206791d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv)) {
206844498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
206944498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
2070ab5c608bSBen Widawsky 	}
207144498aeaSPaulo Zanoni 
207272c90f62SOscar Mateo 	/* Find, clear, then process each source of interrupt */
207372c90f62SOscar Mateo 
20740e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
20750e43406bSChris Wilson 	if (gt_iir) {
207672c90f62SOscar Mateo 		I915_WRITE(GTIIR, gt_iir);
207772c90f62SOscar Mateo 		ret = IRQ_HANDLED;
207891d14251STvrtko Ursulin 		if (INTEL_GEN(dev_priv) >= 6)
2079cf1c97dcSAndi Shyti 			gen6_gt_irq_handler(&dev_priv->gt, gt_iir);
2080d8fc8a47SPaulo Zanoni 		else
2081cf1c97dcSAndi Shyti 			gen5_gt_irq_handler(&dev_priv->gt, gt_iir);
20820e43406bSChris Wilson 	}
2083b1f14ad0SJesse Barnes 
2084b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
20850e43406bSChris Wilson 	if (de_iir) {
208672c90f62SOscar Mateo 		I915_WRITE(DEIIR, de_iir);
208772c90f62SOscar Mateo 		ret = IRQ_HANDLED;
208891d14251STvrtko Ursulin 		if (INTEL_GEN(dev_priv) >= 7)
208991d14251STvrtko Ursulin 			ivb_display_irq_handler(dev_priv, de_iir);
2090f1af8fc1SPaulo Zanoni 		else
209191d14251STvrtko Ursulin 			ilk_display_irq_handler(dev_priv, de_iir);
20920e43406bSChris Wilson 	}
20930e43406bSChris Wilson 
209491d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 6) {
2095f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
20960e43406bSChris Wilson 		if (pm_iir) {
2097b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
20980e43406bSChris Wilson 			ret = IRQ_HANDLED;
20993e7abf81SAndi Shyti 			gen6_rps_irq_handler(&dev_priv->gt.rps, pm_iir);
21000e43406bSChris Wilson 		}
2101f1af8fc1SPaulo Zanoni 	}
2102b1f14ad0SJesse Barnes 
2103b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
210474093f3eSChris Wilson 	if (!HAS_PCH_NOP(dev_priv))
210544498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
2106b1f14ad0SJesse Barnes 
21071f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
21089102650fSDaniele Ceraolo Spurio 	enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
21091f814dacSImre Deak 
2110b1f14ad0SJesse Barnes 	return ret;
2111b1f14ad0SJesse Barnes }
2112b1f14ad0SJesse Barnes 
211391d14251STvrtko Ursulin static void bxt_hpd_irq_handler(struct drm_i915_private *dev_priv,
211491d14251STvrtko Ursulin 				u32 hotplug_trigger,
211540e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2116d04a492dSShashank Sharma {
2117cebd87a0SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2118d04a492dSShashank Sharma 
2119a52bb15bSVille Syrjälä 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
2120a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
2121d04a492dSShashank Sharma 
2122cf53902fSRodrigo Vivi 	intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask, hotplug_trigger,
212340e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2124cebd87a0SVille Syrjälä 			   bxt_port_hotplug_long_detect);
212540e56410SVille Syrjälä 
212691d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2127d04a492dSShashank Sharma }
2128d04a492dSShashank Sharma 
2129121e758eSDhinakaran Pandiyan static void gen11_hpd_irq_handler(struct drm_i915_private *dev_priv, u32 iir)
2130121e758eSDhinakaran Pandiyan {
2131121e758eSDhinakaran Pandiyan 	u32 pin_mask = 0, long_mask = 0;
2132b796b971SDhinakaran Pandiyan 	u32 trigger_tc = iir & GEN11_DE_TC_HOTPLUG_MASK;
2133b796b971SDhinakaran Pandiyan 	u32 trigger_tbt = iir & GEN11_DE_TBT_HOTPLUG_MASK;
213448ef15d3SJosé Roberto de Souza 	long_pulse_detect_func long_pulse_detect;
213548ef15d3SJosé Roberto de Souza 	const u32 *hpd;
213648ef15d3SJosé Roberto de Souza 
213748ef15d3SJosé Roberto de Souza 	if (INTEL_GEN(dev_priv) >= 12) {
213848ef15d3SJosé Roberto de Souza 		long_pulse_detect = gen12_port_hotplug_long_detect;
213948ef15d3SJosé Roberto de Souza 		hpd = hpd_gen12;
214048ef15d3SJosé Roberto de Souza 	} else {
214148ef15d3SJosé Roberto de Souza 		long_pulse_detect = gen11_port_hotplug_long_detect;
214248ef15d3SJosé Roberto de Souza 		hpd = hpd_gen11;
214348ef15d3SJosé Roberto de Souza 	}
2144121e758eSDhinakaran Pandiyan 
2145121e758eSDhinakaran Pandiyan 	if (trigger_tc) {
2146b796b971SDhinakaran Pandiyan 		u32 dig_hotplug_reg;
2147b796b971SDhinakaran Pandiyan 
2148121e758eSDhinakaran Pandiyan 		dig_hotplug_reg = I915_READ(GEN11_TC_HOTPLUG_CTL);
2149121e758eSDhinakaran Pandiyan 		I915_WRITE(GEN11_TC_HOTPLUG_CTL, dig_hotplug_reg);
2150121e758eSDhinakaran Pandiyan 
2151121e758eSDhinakaran Pandiyan 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask, trigger_tc,
215248ef15d3SJosé Roberto de Souza 				   dig_hotplug_reg, hpd, long_pulse_detect);
2153121e758eSDhinakaran Pandiyan 	}
2154b796b971SDhinakaran Pandiyan 
2155b796b971SDhinakaran Pandiyan 	if (trigger_tbt) {
2156b796b971SDhinakaran Pandiyan 		u32 dig_hotplug_reg;
2157b796b971SDhinakaran Pandiyan 
2158b796b971SDhinakaran Pandiyan 		dig_hotplug_reg = I915_READ(GEN11_TBT_HOTPLUG_CTL);
2159b796b971SDhinakaran Pandiyan 		I915_WRITE(GEN11_TBT_HOTPLUG_CTL, dig_hotplug_reg);
2160b796b971SDhinakaran Pandiyan 
2161b796b971SDhinakaran Pandiyan 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask, trigger_tbt,
216248ef15d3SJosé Roberto de Souza 				   dig_hotplug_reg, hpd, long_pulse_detect);
2163b796b971SDhinakaran Pandiyan 	}
2164b796b971SDhinakaran Pandiyan 
2165b796b971SDhinakaran Pandiyan 	if (pin_mask)
2166b796b971SDhinakaran Pandiyan 		intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2167b796b971SDhinakaran Pandiyan 	else
2168*00376ccfSWambui Karuga 		drm_err(&dev_priv->drm,
2169*00376ccfSWambui Karuga 			"Unexpected DE HPD interrupt 0x%08x\n", iir);
2170121e758eSDhinakaran Pandiyan }
2171121e758eSDhinakaran Pandiyan 
21729d17210fSLucas De Marchi static u32 gen8_de_port_aux_mask(struct drm_i915_private *dev_priv)
21739d17210fSLucas De Marchi {
217455523360SLucas De Marchi 	u32 mask;
21759d17210fSLucas De Marchi 
217655523360SLucas De Marchi 	if (INTEL_GEN(dev_priv) >= 12)
217755523360SLucas De Marchi 		return TGL_DE_PORT_AUX_DDIA |
217855523360SLucas De Marchi 			TGL_DE_PORT_AUX_DDIB |
2179e5df52dcSMatt Roper 			TGL_DE_PORT_AUX_DDIC |
2180e5df52dcSMatt Roper 			TGL_DE_PORT_AUX_USBC1 |
2181e5df52dcSMatt Roper 			TGL_DE_PORT_AUX_USBC2 |
2182e5df52dcSMatt Roper 			TGL_DE_PORT_AUX_USBC3 |
2183e5df52dcSMatt Roper 			TGL_DE_PORT_AUX_USBC4 |
2184e5df52dcSMatt Roper 			TGL_DE_PORT_AUX_USBC5 |
2185e5df52dcSMatt Roper 			TGL_DE_PORT_AUX_USBC6;
2186e5df52dcSMatt Roper 
218755523360SLucas De Marchi 
218855523360SLucas De Marchi 	mask = GEN8_AUX_CHANNEL_A;
21899d17210fSLucas De Marchi 	if (INTEL_GEN(dev_priv) >= 9)
21909d17210fSLucas De Marchi 		mask |= GEN9_AUX_CHANNEL_B |
21919d17210fSLucas De Marchi 			GEN9_AUX_CHANNEL_C |
21929d17210fSLucas De Marchi 			GEN9_AUX_CHANNEL_D;
21939d17210fSLucas De Marchi 
219455523360SLucas De Marchi 	if (IS_CNL_WITH_PORT_F(dev_priv) || IS_GEN(dev_priv, 11))
21959d17210fSLucas De Marchi 		mask |= CNL_AUX_CHANNEL_F;
21969d17210fSLucas De Marchi 
219755523360SLucas De Marchi 	if (IS_GEN(dev_priv, 11))
219855523360SLucas De Marchi 		mask |= ICL_AUX_CHANNEL_E;
21999d17210fSLucas De Marchi 
22009d17210fSLucas De Marchi 	return mask;
22019d17210fSLucas De Marchi }
22029d17210fSLucas De Marchi 
22035270130dSVille Syrjälä static u32 gen8_de_pipe_fault_mask(struct drm_i915_private *dev_priv)
22045270130dSVille Syrjälä {
2205d506a65dSMatt Roper 	if (INTEL_GEN(dev_priv) >= 11)
2206d506a65dSMatt Roper 		return GEN11_DE_PIPE_IRQ_FAULT_ERRORS;
2207d506a65dSMatt Roper 	else if (INTEL_GEN(dev_priv) >= 9)
22085270130dSVille Syrjälä 		return GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
22095270130dSVille Syrjälä 	else
22105270130dSVille Syrjälä 		return GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
22115270130dSVille Syrjälä }
22125270130dSVille Syrjälä 
221346c63d24SJosé Roberto de Souza static void
221446c63d24SJosé Roberto de Souza gen8_de_misc_irq_handler(struct drm_i915_private *dev_priv, u32 iir)
2215abd58f01SBen Widawsky {
2216e04f7eceSVille Syrjälä 	bool found = false;
2217e04f7eceSVille Syrjälä 
2218e04f7eceSVille Syrjälä 	if (iir & GEN8_DE_MISC_GSE) {
221991d14251STvrtko Ursulin 		intel_opregion_asle_intr(dev_priv);
2220e04f7eceSVille Syrjälä 		found = true;
2221e04f7eceSVille Syrjälä 	}
2222e04f7eceSVille Syrjälä 
2223e04f7eceSVille Syrjälä 	if (iir & GEN8_DE_EDP_PSR) {
22248241cfbeSJosé Roberto de Souza 		u32 psr_iir;
22258241cfbeSJosé Roberto de Souza 		i915_reg_t iir_reg;
22268241cfbeSJosé Roberto de Souza 
22278241cfbeSJosé Roberto de Souza 		if (INTEL_GEN(dev_priv) >= 12)
22288241cfbeSJosé Roberto de Souza 			iir_reg = TRANS_PSR_IIR(dev_priv->psr.transcoder);
22298241cfbeSJosé Roberto de Souza 		else
22308241cfbeSJosé Roberto de Souza 			iir_reg = EDP_PSR_IIR;
22318241cfbeSJosé Roberto de Souza 
22328241cfbeSJosé Roberto de Souza 		psr_iir = I915_READ(iir_reg);
22338241cfbeSJosé Roberto de Souza 		I915_WRITE(iir_reg, psr_iir);
22348241cfbeSJosé Roberto de Souza 
22358241cfbeSJosé Roberto de Souza 		if (psr_iir)
22368241cfbeSJosé Roberto de Souza 			found = true;
223754fd3149SDhinakaran Pandiyan 
223854fd3149SDhinakaran Pandiyan 		intel_psr_irq_handler(dev_priv, psr_iir);
2239e04f7eceSVille Syrjälä 	}
2240e04f7eceSVille Syrjälä 
2241e04f7eceSVille Syrjälä 	if (!found)
2242*00376ccfSWambui Karuga 		drm_err(&dev_priv->drm, "Unexpected DE Misc interrupt\n");
2243abd58f01SBen Widawsky }
224446c63d24SJosé Roberto de Souza 
224546c63d24SJosé Roberto de Souza static irqreturn_t
224646c63d24SJosé Roberto de Souza gen8_de_irq_handler(struct drm_i915_private *dev_priv, u32 master_ctl)
224746c63d24SJosé Roberto de Souza {
224846c63d24SJosé Roberto de Souza 	irqreturn_t ret = IRQ_NONE;
224946c63d24SJosé Roberto de Souza 	u32 iir;
225046c63d24SJosé Roberto de Souza 	enum pipe pipe;
225146c63d24SJosé Roberto de Souza 
225246c63d24SJosé Roberto de Souza 	if (master_ctl & GEN8_DE_MISC_IRQ) {
225346c63d24SJosé Roberto de Souza 		iir = I915_READ(GEN8_DE_MISC_IIR);
225446c63d24SJosé Roberto de Souza 		if (iir) {
225546c63d24SJosé Roberto de Souza 			I915_WRITE(GEN8_DE_MISC_IIR, iir);
225646c63d24SJosé Roberto de Souza 			ret = IRQ_HANDLED;
225746c63d24SJosé Roberto de Souza 			gen8_de_misc_irq_handler(dev_priv, iir);
225846c63d24SJosé Roberto de Souza 		} else {
2259*00376ccfSWambui Karuga 			drm_err(&dev_priv->drm,
2260*00376ccfSWambui Karuga 				"The master control interrupt lied (DE MISC)!\n");
2261abd58f01SBen Widawsky 		}
226246c63d24SJosé Roberto de Souza 	}
2263abd58f01SBen Widawsky 
2264121e758eSDhinakaran Pandiyan 	if (INTEL_GEN(dev_priv) >= 11 && (master_ctl & GEN11_DE_HPD_IRQ)) {
2265121e758eSDhinakaran Pandiyan 		iir = I915_READ(GEN11_DE_HPD_IIR);
2266121e758eSDhinakaran Pandiyan 		if (iir) {
2267121e758eSDhinakaran Pandiyan 			I915_WRITE(GEN11_DE_HPD_IIR, iir);
2268121e758eSDhinakaran Pandiyan 			ret = IRQ_HANDLED;
2269121e758eSDhinakaran Pandiyan 			gen11_hpd_irq_handler(dev_priv, iir);
2270121e758eSDhinakaran Pandiyan 		} else {
2271*00376ccfSWambui Karuga 			drm_err(&dev_priv->drm,
2272*00376ccfSWambui Karuga 				"The master control interrupt lied, (DE HPD)!\n");
2273121e758eSDhinakaran Pandiyan 		}
2274121e758eSDhinakaran Pandiyan 	}
2275121e758eSDhinakaran Pandiyan 
22766d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
2277e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_PORT_IIR);
2278e32192e1STvrtko Ursulin 		if (iir) {
2279e32192e1STvrtko Ursulin 			u32 tmp_mask;
2280d04a492dSShashank Sharma 			bool found = false;
2281cebd87a0SVille Syrjälä 
2282e32192e1STvrtko Ursulin 			I915_WRITE(GEN8_DE_PORT_IIR, iir);
22836d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
228488e04703SJesse Barnes 
22859d17210fSLucas De Marchi 			if (iir & gen8_de_port_aux_mask(dev_priv)) {
228691d14251STvrtko Ursulin 				dp_aux_irq_handler(dev_priv);
2287d04a492dSShashank Sharma 				found = true;
2288d04a492dSShashank Sharma 			}
2289d04a492dSShashank Sharma 
2290cc3f90f0SAnder Conselvan de Oliveira 			if (IS_GEN9_LP(dev_priv)) {
2291e32192e1STvrtko Ursulin 				tmp_mask = iir & BXT_DE_PORT_HOTPLUG_MASK;
2292e32192e1STvrtko Ursulin 				if (tmp_mask) {
229391d14251STvrtko Ursulin 					bxt_hpd_irq_handler(dev_priv, tmp_mask,
229491d14251STvrtko Ursulin 							    hpd_bxt);
2295d04a492dSShashank Sharma 					found = true;
2296d04a492dSShashank Sharma 				}
2297e32192e1STvrtko Ursulin 			} else if (IS_BROADWELL(dev_priv)) {
2298e32192e1STvrtko Ursulin 				tmp_mask = iir & GEN8_PORT_DP_A_HOTPLUG;
2299e32192e1STvrtko Ursulin 				if (tmp_mask) {
230091d14251STvrtko Ursulin 					ilk_hpd_irq_handler(dev_priv,
230191d14251STvrtko Ursulin 							    tmp_mask, hpd_bdw);
2302e32192e1STvrtko Ursulin 					found = true;
2303e32192e1STvrtko Ursulin 				}
2304e32192e1STvrtko Ursulin 			}
2305d04a492dSShashank Sharma 
2306cc3f90f0SAnder Conselvan de Oliveira 			if (IS_GEN9_LP(dev_priv) && (iir & BXT_DE_PORT_GMBUS)) {
230791d14251STvrtko Ursulin 				gmbus_irq_handler(dev_priv);
23089e63743eSShashank Sharma 				found = true;
23099e63743eSShashank Sharma 			}
23109e63743eSShashank Sharma 
2311d04a492dSShashank Sharma 			if (!found)
2312*00376ccfSWambui Karuga 				drm_err(&dev_priv->drm,
2313*00376ccfSWambui Karuga 					"Unexpected DE Port interrupt\n");
23146d766f02SDaniel Vetter 		}
231538cc46d7SOscar Mateo 		else
2316*00376ccfSWambui Karuga 			drm_err(&dev_priv->drm,
2317*00376ccfSWambui Karuga 				"The master control interrupt lied (DE PORT)!\n");
23186d766f02SDaniel Vetter 	}
23196d766f02SDaniel Vetter 
2320055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2321fd3a4024SDaniel Vetter 		u32 fault_errors;
2322abd58f01SBen Widawsky 
2323c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2324c42664ccSDaniel Vetter 			continue;
2325c42664ccSDaniel Vetter 
2326e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
2327e32192e1STvrtko Ursulin 		if (!iir) {
2328*00376ccfSWambui Karuga 			drm_err(&dev_priv->drm,
2329*00376ccfSWambui Karuga 				"The master control interrupt lied (DE PIPE)!\n");
2330e32192e1STvrtko Ursulin 			continue;
2331e32192e1STvrtko Ursulin 		}
2332770de83dSDamien Lespiau 
2333e32192e1STvrtko Ursulin 		ret = IRQ_HANDLED;
2334e32192e1STvrtko Ursulin 		I915_WRITE(GEN8_DE_PIPE_IIR(pipe), iir);
2335e32192e1STvrtko Ursulin 
2336fd3a4024SDaniel Vetter 		if (iir & GEN8_PIPE_VBLANK)
2337fd3a4024SDaniel Vetter 			drm_handle_vblank(&dev_priv->drm, pipe);
2338abd58f01SBen Widawsky 
2339e32192e1STvrtko Ursulin 		if (iir & GEN8_PIPE_CDCLK_CRC_DONE)
234091d14251STvrtko Ursulin 			hsw_pipe_crc_irq_handler(dev_priv, pipe);
23410fbe7870SDaniel Vetter 
2342e32192e1STvrtko Ursulin 		if (iir & GEN8_PIPE_FIFO_UNDERRUN)
2343e32192e1STvrtko Ursulin 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
234438d83c96SDaniel Vetter 
23455270130dSVille Syrjälä 		fault_errors = iir & gen8_de_pipe_fault_mask(dev_priv);
2346770de83dSDamien Lespiau 		if (fault_errors)
2347*00376ccfSWambui Karuga 			drm_err(&dev_priv->drm,
2348*00376ccfSWambui Karuga 				"Fault errors on pipe %c: 0x%08x\n",
234930100f2bSDaniel Vetter 				pipe_name(pipe),
2350e32192e1STvrtko Ursulin 				fault_errors);
2351abd58f01SBen Widawsky 	}
2352abd58f01SBen Widawsky 
235391d14251STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv) && !HAS_PCH_NOP(dev_priv) &&
2354266ea3d9SShashank Sharma 	    master_ctl & GEN8_DE_PCH_IRQ) {
235592d03a80SDaniel Vetter 		/*
235692d03a80SDaniel Vetter 		 * FIXME(BDW): Assume for now that the new interrupt handling
235792d03a80SDaniel Vetter 		 * scheme also closed the SDE interrupt handling race we've seen
235892d03a80SDaniel Vetter 		 * on older pch-split platforms. But this needs testing.
235992d03a80SDaniel Vetter 		 */
2360e32192e1STvrtko Ursulin 		iir = I915_READ(SDEIIR);
2361e32192e1STvrtko Ursulin 		if (iir) {
2362e32192e1STvrtko Ursulin 			I915_WRITE(SDEIIR, iir);
236392d03a80SDaniel Vetter 			ret = IRQ_HANDLED;
23646dbf30ceSVille Syrjälä 
236558676af6SLucas De Marchi 			if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
236658676af6SLucas De Marchi 				icp_irq_handler(dev_priv, iir);
2367c6c30b91SRodrigo Vivi 			else if (INTEL_PCH_TYPE(dev_priv) >= PCH_SPT)
236891d14251STvrtko Ursulin 				spt_irq_handler(dev_priv, iir);
23696dbf30ceSVille Syrjälä 			else
237091d14251STvrtko Ursulin 				cpt_irq_handler(dev_priv, iir);
23712dfb0b81SJani Nikula 		} else {
23722dfb0b81SJani Nikula 			/*
23732dfb0b81SJani Nikula 			 * Like on previous PCH there seems to be something
23742dfb0b81SJani Nikula 			 * fishy going on with forwarding PCH interrupts.
23752dfb0b81SJani Nikula 			 */
2376*00376ccfSWambui Karuga 			drm_dbg(&dev_priv->drm,
2377*00376ccfSWambui Karuga 				"The master control interrupt lied (SDE)!\n");
23782dfb0b81SJani Nikula 		}
237992d03a80SDaniel Vetter 	}
238092d03a80SDaniel Vetter 
2381f11a0f46STvrtko Ursulin 	return ret;
2382f11a0f46STvrtko Ursulin }
2383f11a0f46STvrtko Ursulin 
23844376b9c9SMika Kuoppala static inline u32 gen8_master_intr_disable(void __iomem * const regs)
23854376b9c9SMika Kuoppala {
23864376b9c9SMika Kuoppala 	raw_reg_write(regs, GEN8_MASTER_IRQ, 0);
23874376b9c9SMika Kuoppala 
23884376b9c9SMika Kuoppala 	/*
23894376b9c9SMika Kuoppala 	 * Now with master disabled, get a sample of level indications
23904376b9c9SMika Kuoppala 	 * for this interrupt. Indications will be cleared on related acks.
23914376b9c9SMika Kuoppala 	 * New indications can and will light up during processing,
23924376b9c9SMika Kuoppala 	 * and will generate new interrupt after enabling master.
23934376b9c9SMika Kuoppala 	 */
23944376b9c9SMika Kuoppala 	return raw_reg_read(regs, GEN8_MASTER_IRQ);
23954376b9c9SMika Kuoppala }
23964376b9c9SMika Kuoppala 
23974376b9c9SMika Kuoppala static inline void gen8_master_intr_enable(void __iomem * const regs)
23984376b9c9SMika Kuoppala {
23994376b9c9SMika Kuoppala 	raw_reg_write(regs, GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
24004376b9c9SMika Kuoppala }
24014376b9c9SMika Kuoppala 
2402f11a0f46STvrtko Ursulin static irqreturn_t gen8_irq_handler(int irq, void *arg)
2403f11a0f46STvrtko Ursulin {
2404b318b824SVille Syrjälä 	struct drm_i915_private *dev_priv = arg;
240525286aacSDaniele Ceraolo Spurio 	void __iomem * const regs = dev_priv->uncore.regs;
2406f11a0f46STvrtko Ursulin 	u32 master_ctl;
2407f11a0f46STvrtko Ursulin 
2408f11a0f46STvrtko Ursulin 	if (!intel_irqs_enabled(dev_priv))
2409f11a0f46STvrtko Ursulin 		return IRQ_NONE;
2410f11a0f46STvrtko Ursulin 
24114376b9c9SMika Kuoppala 	master_ctl = gen8_master_intr_disable(regs);
24124376b9c9SMika Kuoppala 	if (!master_ctl) {
24134376b9c9SMika Kuoppala 		gen8_master_intr_enable(regs);
2414f11a0f46STvrtko Ursulin 		return IRQ_NONE;
24154376b9c9SMika Kuoppala 	}
2416f11a0f46STvrtko Ursulin 
24176cc32f15SChris Wilson 	/* Find, queue (onto bottom-halves), then clear each source */
24186cc32f15SChris Wilson 	gen8_gt_irq_handler(&dev_priv->gt, master_ctl);
2419f0fd96f5SChris Wilson 
2420f0fd96f5SChris Wilson 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
2421f0fd96f5SChris Wilson 	if (master_ctl & ~GEN8_GT_IRQS) {
24229102650fSDaniele Ceraolo Spurio 		disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
242355ef72f2SChris Wilson 		gen8_de_irq_handler(dev_priv, master_ctl);
24249102650fSDaniele Ceraolo Spurio 		enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
2425f0fd96f5SChris Wilson 	}
2426f11a0f46STvrtko Ursulin 
24274376b9c9SMika Kuoppala 	gen8_master_intr_enable(regs);
2428abd58f01SBen Widawsky 
242955ef72f2SChris Wilson 	return IRQ_HANDLED;
2430abd58f01SBen Widawsky }
2431abd58f01SBen Widawsky 
243251951ae7SMika Kuoppala static u32
24339b77011eSTvrtko Ursulin gen11_gu_misc_irq_ack(struct intel_gt *gt, const u32 master_ctl)
2434df0d28c1SDhinakaran Pandiyan {
24359b77011eSTvrtko Ursulin 	void __iomem * const regs = gt->uncore->regs;
24367a909383SChris Wilson 	u32 iir;
2437df0d28c1SDhinakaran Pandiyan 
2438df0d28c1SDhinakaran Pandiyan 	if (!(master_ctl & GEN11_GU_MISC_IRQ))
24397a909383SChris Wilson 		return 0;
2440df0d28c1SDhinakaran Pandiyan 
24417a909383SChris Wilson 	iir = raw_reg_read(regs, GEN11_GU_MISC_IIR);
24427a909383SChris Wilson 	if (likely(iir))
24437a909383SChris Wilson 		raw_reg_write(regs, GEN11_GU_MISC_IIR, iir);
24447a909383SChris Wilson 
24457a909383SChris Wilson 	return iir;
2446df0d28c1SDhinakaran Pandiyan }
2447df0d28c1SDhinakaran Pandiyan 
2448df0d28c1SDhinakaran Pandiyan static void
24499b77011eSTvrtko Ursulin gen11_gu_misc_irq_handler(struct intel_gt *gt, const u32 iir)
2450df0d28c1SDhinakaran Pandiyan {
2451df0d28c1SDhinakaran Pandiyan 	if (iir & GEN11_GU_MISC_GSE)
24529b77011eSTvrtko Ursulin 		intel_opregion_asle_intr(gt->i915);
2453df0d28c1SDhinakaran Pandiyan }
2454df0d28c1SDhinakaran Pandiyan 
245581067b71SMika Kuoppala static inline u32 gen11_master_intr_disable(void __iomem * const regs)
245681067b71SMika Kuoppala {
245781067b71SMika Kuoppala 	raw_reg_write(regs, GEN11_GFX_MSTR_IRQ, 0);
245881067b71SMika Kuoppala 
245981067b71SMika Kuoppala 	/*
246081067b71SMika Kuoppala 	 * Now with master disabled, get a sample of level indications
246181067b71SMika Kuoppala 	 * for this interrupt. Indications will be cleared on related acks.
246281067b71SMika Kuoppala 	 * New indications can and will light up during processing,
246381067b71SMika Kuoppala 	 * and will generate new interrupt after enabling master.
246481067b71SMika Kuoppala 	 */
246581067b71SMika Kuoppala 	return raw_reg_read(regs, GEN11_GFX_MSTR_IRQ);
246681067b71SMika Kuoppala }
246781067b71SMika Kuoppala 
246881067b71SMika Kuoppala static inline void gen11_master_intr_enable(void __iomem * const regs)
246981067b71SMika Kuoppala {
247081067b71SMika Kuoppala 	raw_reg_write(regs, GEN11_GFX_MSTR_IRQ, GEN11_MASTER_IRQ);
247181067b71SMika Kuoppala }
247281067b71SMika Kuoppala 
2473a3265d85SMatt Roper static void
2474a3265d85SMatt Roper gen11_display_irq_handler(struct drm_i915_private *i915)
2475a3265d85SMatt Roper {
2476a3265d85SMatt Roper 	void __iomem * const regs = i915->uncore.regs;
2477a3265d85SMatt Roper 	const u32 disp_ctl = raw_reg_read(regs, GEN11_DISPLAY_INT_CTL);
2478a3265d85SMatt Roper 
2479a3265d85SMatt Roper 	disable_rpm_wakeref_asserts(&i915->runtime_pm);
2480a3265d85SMatt Roper 	/*
2481a3265d85SMatt Roper 	 * GEN11_DISPLAY_INT_CTL has same format as GEN8_MASTER_IRQ
2482a3265d85SMatt Roper 	 * for the display related bits.
2483a3265d85SMatt Roper 	 */
2484a3265d85SMatt Roper 	raw_reg_write(regs, GEN11_DISPLAY_INT_CTL, 0x0);
2485a3265d85SMatt Roper 	gen8_de_irq_handler(i915, disp_ctl);
2486a3265d85SMatt Roper 	raw_reg_write(regs, GEN11_DISPLAY_INT_CTL,
2487a3265d85SMatt Roper 		      GEN11_DISPLAY_IRQ_ENABLE);
2488a3265d85SMatt Roper 
2489a3265d85SMatt Roper 	enable_rpm_wakeref_asserts(&i915->runtime_pm);
2490a3265d85SMatt Roper }
2491a3265d85SMatt Roper 
24927be8782aSLucas De Marchi static __always_inline irqreturn_t
24937be8782aSLucas De Marchi __gen11_irq_handler(struct drm_i915_private * const i915,
24947be8782aSLucas De Marchi 		    u32 (*intr_disable)(void __iomem * const regs),
24957be8782aSLucas De Marchi 		    void (*intr_enable)(void __iomem * const regs))
249651951ae7SMika Kuoppala {
249725286aacSDaniele Ceraolo Spurio 	void __iomem * const regs = i915->uncore.regs;
24989b77011eSTvrtko Ursulin 	struct intel_gt *gt = &i915->gt;
249951951ae7SMika Kuoppala 	u32 master_ctl;
2500df0d28c1SDhinakaran Pandiyan 	u32 gu_misc_iir;
250151951ae7SMika Kuoppala 
250251951ae7SMika Kuoppala 	if (!intel_irqs_enabled(i915))
250351951ae7SMika Kuoppala 		return IRQ_NONE;
250451951ae7SMika Kuoppala 
25057be8782aSLucas De Marchi 	master_ctl = intr_disable(regs);
250681067b71SMika Kuoppala 	if (!master_ctl) {
25077be8782aSLucas De Marchi 		intr_enable(regs);
250851951ae7SMika Kuoppala 		return IRQ_NONE;
250981067b71SMika Kuoppala 	}
251051951ae7SMika Kuoppala 
25116cc32f15SChris Wilson 	/* Find, queue (onto bottom-halves), then clear each source */
25129b77011eSTvrtko Ursulin 	gen11_gt_irq_handler(gt, master_ctl);
251351951ae7SMika Kuoppala 
251451951ae7SMika Kuoppala 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
2515a3265d85SMatt Roper 	if (master_ctl & GEN11_DISPLAY_IRQ)
2516a3265d85SMatt Roper 		gen11_display_irq_handler(i915);
251751951ae7SMika Kuoppala 
25189b77011eSTvrtko Ursulin 	gu_misc_iir = gen11_gu_misc_irq_ack(gt, master_ctl);
2519df0d28c1SDhinakaran Pandiyan 
25207be8782aSLucas De Marchi 	intr_enable(regs);
252151951ae7SMika Kuoppala 
25229b77011eSTvrtko Ursulin 	gen11_gu_misc_irq_handler(gt, gu_misc_iir);
2523df0d28c1SDhinakaran Pandiyan 
252451951ae7SMika Kuoppala 	return IRQ_HANDLED;
252551951ae7SMika Kuoppala }
252651951ae7SMika Kuoppala 
25277be8782aSLucas De Marchi static irqreturn_t gen11_irq_handler(int irq, void *arg)
25287be8782aSLucas De Marchi {
25297be8782aSLucas De Marchi 	return __gen11_irq_handler(arg,
25307be8782aSLucas De Marchi 				   gen11_master_intr_disable,
25317be8782aSLucas De Marchi 				   gen11_master_intr_enable);
25327be8782aSLucas De Marchi }
25337be8782aSLucas De Marchi 
253442f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
253542f52ef8SKeith Packard  * we use as a pipe index
253642f52ef8SKeith Packard  */
253708fa8fd0SVille Syrjälä int i8xx_enable_vblank(struct drm_crtc *crtc)
25380a3e67a4SJesse Barnes {
253908fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
254008fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
2541e9d21d7fSKeith Packard 	unsigned long irqflags;
254271e0ffa5SJesse Barnes 
25431ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
254486e83e35SChris Wilson 	i915_enable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_STATUS);
254586e83e35SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
254686e83e35SChris Wilson 
254786e83e35SChris Wilson 	return 0;
254886e83e35SChris Wilson }
254986e83e35SChris Wilson 
25507d423af9SVille Syrjälä int i915gm_enable_vblank(struct drm_crtc *crtc)
2551d938da6bSVille Syrjälä {
255208fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
2553d938da6bSVille Syrjälä 
25547d423af9SVille Syrjälä 	/*
25557d423af9SVille Syrjälä 	 * Vblank interrupts fail to wake the device up from C2+.
25567d423af9SVille Syrjälä 	 * Disabling render clock gating during C-states avoids
25577d423af9SVille Syrjälä 	 * the problem. There is a small power cost so we do this
25587d423af9SVille Syrjälä 	 * only when vblank interrupts are actually enabled.
25597d423af9SVille Syrjälä 	 */
25607d423af9SVille Syrjälä 	if (dev_priv->vblank_enabled++ == 0)
25617d423af9SVille Syrjälä 		I915_WRITE(SCPD0, _MASKED_BIT_ENABLE(CSTATE_RENDER_CLOCK_GATE_DISABLE));
2562d938da6bSVille Syrjälä 
256308fa8fd0SVille Syrjälä 	return i8xx_enable_vblank(crtc);
2564d938da6bSVille Syrjälä }
2565d938da6bSVille Syrjälä 
256608fa8fd0SVille Syrjälä int i965_enable_vblank(struct drm_crtc *crtc)
256786e83e35SChris Wilson {
256808fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
256908fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
257086e83e35SChris Wilson 	unsigned long irqflags;
257186e83e35SChris Wilson 
257286e83e35SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
25737c463586SKeith Packard 	i915_enable_pipestat(dev_priv, pipe,
2574755e9019SImre Deak 			     PIPE_START_VBLANK_INTERRUPT_STATUS);
25751ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
25768692d00eSChris Wilson 
25770a3e67a4SJesse Barnes 	return 0;
25780a3e67a4SJesse Barnes }
25790a3e67a4SJesse Barnes 
258008fa8fd0SVille Syrjälä int ilk_enable_vblank(struct drm_crtc *crtc)
2581f796cf8fSJesse Barnes {
258208fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
258308fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
2584f796cf8fSJesse Barnes 	unsigned long irqflags;
2585a9c287c9SJani Nikula 	u32 bit = INTEL_GEN(dev_priv) >= 7 ?
258686e83e35SChris Wilson 		DE_PIPE_VBLANK_IVB(pipe) : DE_PIPE_VBLANK(pipe);
2587f796cf8fSJesse Barnes 
2588f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2589fbdedaeaSVille Syrjälä 	ilk_enable_display_irq(dev_priv, bit);
2590b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2591b1f14ad0SJesse Barnes 
25922e8bf223SDhinakaran Pandiyan 	/* Even though there is no DMC, frame counter can get stuck when
25932e8bf223SDhinakaran Pandiyan 	 * PSR is active as no frames are generated.
25942e8bf223SDhinakaran Pandiyan 	 */
25952e8bf223SDhinakaran Pandiyan 	if (HAS_PSR(dev_priv))
259608fa8fd0SVille Syrjälä 		drm_crtc_vblank_restore(crtc);
25972e8bf223SDhinakaran Pandiyan 
2598b1f14ad0SJesse Barnes 	return 0;
2599b1f14ad0SJesse Barnes }
2600b1f14ad0SJesse Barnes 
260108fa8fd0SVille Syrjälä int bdw_enable_vblank(struct drm_crtc *crtc)
2602abd58f01SBen Widawsky {
260308fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
260408fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
2605abd58f01SBen Widawsky 	unsigned long irqflags;
2606abd58f01SBen Widawsky 
2607abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2608013d3752SVille Syrjälä 	bdw_enable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
2609abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2610013d3752SVille Syrjälä 
26112e8bf223SDhinakaran Pandiyan 	/* Even if there is no DMC, frame counter can get stuck when
26122e8bf223SDhinakaran Pandiyan 	 * PSR is active as no frames are generated, so check only for PSR.
26132e8bf223SDhinakaran Pandiyan 	 */
26142e8bf223SDhinakaran Pandiyan 	if (HAS_PSR(dev_priv))
261508fa8fd0SVille Syrjälä 		drm_crtc_vblank_restore(crtc);
26162e8bf223SDhinakaran Pandiyan 
2617abd58f01SBen Widawsky 	return 0;
2618abd58f01SBen Widawsky }
2619abd58f01SBen Widawsky 
262042f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
262142f52ef8SKeith Packard  * we use as a pipe index
262242f52ef8SKeith Packard  */
262308fa8fd0SVille Syrjälä void i8xx_disable_vblank(struct drm_crtc *crtc)
262486e83e35SChris Wilson {
262508fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
262608fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
262786e83e35SChris Wilson 	unsigned long irqflags;
262886e83e35SChris Wilson 
262986e83e35SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
263086e83e35SChris Wilson 	i915_disable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_STATUS);
263186e83e35SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
263286e83e35SChris Wilson }
263386e83e35SChris Wilson 
26347d423af9SVille Syrjälä void i915gm_disable_vblank(struct drm_crtc *crtc)
2635d938da6bSVille Syrjälä {
263608fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
2637d938da6bSVille Syrjälä 
263808fa8fd0SVille Syrjälä 	i8xx_disable_vblank(crtc);
2639d938da6bSVille Syrjälä 
26407d423af9SVille Syrjälä 	if (--dev_priv->vblank_enabled == 0)
26417d423af9SVille Syrjälä 		I915_WRITE(SCPD0, _MASKED_BIT_DISABLE(CSTATE_RENDER_CLOCK_GATE_DISABLE));
2642d938da6bSVille Syrjälä }
2643d938da6bSVille Syrjälä 
264408fa8fd0SVille Syrjälä void i965_disable_vblank(struct drm_crtc *crtc)
26450a3e67a4SJesse Barnes {
264608fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
264708fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
2648e9d21d7fSKeith Packard 	unsigned long irqflags;
26490a3e67a4SJesse Barnes 
26501ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
26517c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
2652755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
26531ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26540a3e67a4SJesse Barnes }
26550a3e67a4SJesse Barnes 
265608fa8fd0SVille Syrjälä void ilk_disable_vblank(struct drm_crtc *crtc)
2657f796cf8fSJesse Barnes {
265808fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
265908fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
2660f796cf8fSJesse Barnes 	unsigned long irqflags;
2661a9c287c9SJani Nikula 	u32 bit = INTEL_GEN(dev_priv) >= 7 ?
266286e83e35SChris Wilson 		DE_PIPE_VBLANK_IVB(pipe) : DE_PIPE_VBLANK(pipe);
2663f796cf8fSJesse Barnes 
2664f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2665fbdedaeaSVille Syrjälä 	ilk_disable_display_irq(dev_priv, bit);
2666b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2667b1f14ad0SJesse Barnes }
2668b1f14ad0SJesse Barnes 
266908fa8fd0SVille Syrjälä void bdw_disable_vblank(struct drm_crtc *crtc)
2670abd58f01SBen Widawsky {
267108fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
267208fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
2673abd58f01SBen Widawsky 	unsigned long irqflags;
2674abd58f01SBen Widawsky 
2675abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2676013d3752SVille Syrjälä 	bdw_disable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
2677abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2678abd58f01SBen Widawsky }
2679abd58f01SBen Widawsky 
2680b243f530STvrtko Ursulin static void ibx_irq_reset(struct drm_i915_private *dev_priv)
268191738a95SPaulo Zanoni {
2682b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
2683b16b2a2fSPaulo Zanoni 
26846e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
268591738a95SPaulo Zanoni 		return;
268691738a95SPaulo Zanoni 
2687b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, SDE);
2688105b122eSPaulo Zanoni 
26896e266956STvrtko Ursulin 	if (HAS_PCH_CPT(dev_priv) || HAS_PCH_LPT(dev_priv))
2690105b122eSPaulo Zanoni 		I915_WRITE(SERR_INT, 0xffffffff);
2691622364b6SPaulo Zanoni }
2692105b122eSPaulo Zanoni 
269391738a95SPaulo Zanoni /*
2694622364b6SPaulo Zanoni  * SDEIER is also touched by the interrupt handler to work around missed PCH
2695622364b6SPaulo Zanoni  * interrupts. Hence we can't update it after the interrupt handler is enabled -
2696622364b6SPaulo Zanoni  * instead we unconditionally enable all PCH interrupt sources here, but then
2697622364b6SPaulo Zanoni  * only unmask them as needed with SDEIMR.
2698622364b6SPaulo Zanoni  *
2699622364b6SPaulo Zanoni  * This function needs to be called before interrupts are enabled.
270091738a95SPaulo Zanoni  */
2701b318b824SVille Syrjälä static void ibx_irq_pre_postinstall(struct drm_i915_private *dev_priv)
2702622364b6SPaulo Zanoni {
27036e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
2704622364b6SPaulo Zanoni 		return;
2705622364b6SPaulo Zanoni 
270648a1b8d4SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, I915_READ(SDEIER) != 0);
270791738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
270891738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
270991738a95SPaulo Zanoni }
271091738a95SPaulo Zanoni 
271170591a41SVille Syrjälä static void vlv_display_irq_reset(struct drm_i915_private *dev_priv)
271270591a41SVille Syrjälä {
2713b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
2714b16b2a2fSPaulo Zanoni 
271571b8b41dSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
2716f0818984STvrtko Ursulin 		intel_uncore_write(uncore, DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
271771b8b41dSVille Syrjälä 	else
2718f0818984STvrtko Ursulin 		intel_uncore_write(uncore, DPINVGTT, DPINVGTT_STATUS_MASK);
271971b8b41dSVille Syrjälä 
2720ad22d106SVille Syrjälä 	i915_hotplug_interrupt_update_locked(dev_priv, 0xffffffff, 0);
2721f0818984STvrtko Ursulin 	intel_uncore_write(uncore, PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
272270591a41SVille Syrjälä 
272344d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
272470591a41SVille Syrjälä 
2725b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, VLV_);
27268bd099a7SChris Wilson 	dev_priv->irq_mask = ~0u;
272770591a41SVille Syrjälä }
272870591a41SVille Syrjälä 
27298bb61306SVille Syrjälä static void vlv_display_irq_postinstall(struct drm_i915_private *dev_priv)
27308bb61306SVille Syrjälä {
2731b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
2732b16b2a2fSPaulo Zanoni 
27338bb61306SVille Syrjälä 	u32 pipestat_mask;
27349ab981f2SVille Syrjälä 	u32 enable_mask;
27358bb61306SVille Syrjälä 	enum pipe pipe;
27368bb61306SVille Syrjälä 
2737842ebf7aSVille Syrjälä 	pipestat_mask = PIPE_CRC_DONE_INTERRUPT_STATUS;
27388bb61306SVille Syrjälä 
27398bb61306SVille Syrjälä 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
27408bb61306SVille Syrjälä 	for_each_pipe(dev_priv, pipe)
27418bb61306SVille Syrjälä 		i915_enable_pipestat(dev_priv, pipe, pipestat_mask);
27428bb61306SVille Syrjälä 
27439ab981f2SVille Syrjälä 	enable_mask = I915_DISPLAY_PORT_INTERRUPT |
27448bb61306SVille Syrjälä 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2745ebf5f921SVille Syrjälä 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2746ebf5f921SVille Syrjälä 		I915_LPE_PIPE_A_INTERRUPT |
2747ebf5f921SVille Syrjälä 		I915_LPE_PIPE_B_INTERRUPT;
2748ebf5f921SVille Syrjälä 
27498bb61306SVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
2750ebf5f921SVille Syrjälä 		enable_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT |
2751ebf5f921SVille Syrjälä 			I915_LPE_PIPE_C_INTERRUPT;
27526b7eafc1SVille Syrjälä 
275348a1b8d4SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, dev_priv->irq_mask != ~0u);
27546b7eafc1SVille Syrjälä 
27559ab981f2SVille Syrjälä 	dev_priv->irq_mask = ~enable_mask;
27568bb61306SVille Syrjälä 
2757b16b2a2fSPaulo Zanoni 	GEN3_IRQ_INIT(uncore, VLV_, dev_priv->irq_mask, enable_mask);
27588bb61306SVille Syrjälä }
27598bb61306SVille Syrjälä 
27608bb61306SVille Syrjälä /* drm_dma.h hooks
27618bb61306SVille Syrjälä */
27629eae5e27SLucas De Marchi static void ilk_irq_reset(struct drm_i915_private *dev_priv)
27638bb61306SVille Syrjälä {
2764b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
27658bb61306SVille Syrjälä 
2766b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, DE);
2767cf819effSLucas De Marchi 	if (IS_GEN(dev_priv, 7))
2768f0818984STvrtko Ursulin 		intel_uncore_write(uncore, GEN7_ERR_INT, 0xffffffff);
27698bb61306SVille Syrjälä 
2770fc340442SDaniel Vetter 	if (IS_HASWELL(dev_priv)) {
2771f0818984STvrtko Ursulin 		intel_uncore_write(uncore, EDP_PSR_IMR, 0xffffffff);
2772f0818984STvrtko Ursulin 		intel_uncore_write(uncore, EDP_PSR_IIR, 0xffffffff);
2773fc340442SDaniel Vetter 	}
2774fc340442SDaniel Vetter 
2775cf1c97dcSAndi Shyti 	gen5_gt_irq_reset(&dev_priv->gt);
27768bb61306SVille Syrjälä 
2777b243f530STvrtko Ursulin 	ibx_irq_reset(dev_priv);
27788bb61306SVille Syrjälä }
27798bb61306SVille Syrjälä 
2780b318b824SVille Syrjälä static void valleyview_irq_reset(struct drm_i915_private *dev_priv)
27817e231dbeSJesse Barnes {
278234c7b8a7SVille Syrjälä 	I915_WRITE(VLV_MASTER_IER, 0);
278334c7b8a7SVille Syrjälä 	POSTING_READ(VLV_MASTER_IER);
278434c7b8a7SVille Syrjälä 
2785cf1c97dcSAndi Shyti 	gen5_gt_irq_reset(&dev_priv->gt);
27867e231dbeSJesse Barnes 
2787ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
27889918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
278970591a41SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
2790ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
27917e231dbeSJesse Barnes }
27927e231dbeSJesse Barnes 
2793b318b824SVille Syrjälä static void gen8_irq_reset(struct drm_i915_private *dev_priv)
2794abd58f01SBen Widawsky {
2795b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
2796d048a268SVille Syrjälä 	enum pipe pipe;
2797abd58f01SBen Widawsky 
279825286aacSDaniele Ceraolo Spurio 	gen8_master_intr_disable(dev_priv->uncore.regs);
2799abd58f01SBen Widawsky 
2800cf1c97dcSAndi Shyti 	gen8_gt_irq_reset(&dev_priv->gt);
2801abd58f01SBen Widawsky 
2802f0818984STvrtko Ursulin 	intel_uncore_write(uncore, EDP_PSR_IMR, 0xffffffff);
2803f0818984STvrtko Ursulin 	intel_uncore_write(uncore, EDP_PSR_IIR, 0xffffffff);
2804e04f7eceSVille Syrjälä 
2805055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
2806f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
2807813bde43SPaulo Zanoni 						   POWER_DOMAIN_PIPE(pipe)))
2808b16b2a2fSPaulo Zanoni 			GEN8_IRQ_RESET_NDX(uncore, DE_PIPE, pipe);
2809abd58f01SBen Widawsky 
2810b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN8_DE_PORT_);
2811b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN8_DE_MISC_);
2812b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN8_PCU_);
2813abd58f01SBen Widawsky 
28146e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
2815b243f530STvrtko Ursulin 		ibx_irq_reset(dev_priv);
2816abd58f01SBen Widawsky }
2817abd58f01SBen Widawsky 
2818a3265d85SMatt Roper static void gen11_display_irq_reset(struct drm_i915_private *dev_priv)
281951951ae7SMika Kuoppala {
2820b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
2821d048a268SVille Syrjälä 	enum pipe pipe;
282251951ae7SMika Kuoppala 
2823f0818984STvrtko Ursulin 	intel_uncore_write(uncore, GEN11_DISPLAY_INT_CTL, 0);
282451951ae7SMika Kuoppala 
28258241cfbeSJosé Roberto de Souza 	if (INTEL_GEN(dev_priv) >= 12) {
28268241cfbeSJosé Roberto de Souza 		enum transcoder trans;
28278241cfbeSJosé Roberto de Souza 
28288241cfbeSJosé Roberto de Souza 		for (trans = TRANSCODER_A; trans <= TRANSCODER_D; trans++) {
28298241cfbeSJosé Roberto de Souza 			enum intel_display_power_domain domain;
28308241cfbeSJosé Roberto de Souza 
28318241cfbeSJosé Roberto de Souza 			domain = POWER_DOMAIN_TRANSCODER(trans);
28328241cfbeSJosé Roberto de Souza 			if (!intel_display_power_is_enabled(dev_priv, domain))
28338241cfbeSJosé Roberto de Souza 				continue;
28348241cfbeSJosé Roberto de Souza 
28358241cfbeSJosé Roberto de Souza 			intel_uncore_write(uncore, TRANS_PSR_IMR(trans), 0xffffffff);
28368241cfbeSJosé Roberto de Souza 			intel_uncore_write(uncore, TRANS_PSR_IIR(trans), 0xffffffff);
28378241cfbeSJosé Roberto de Souza 		}
28388241cfbeSJosé Roberto de Souza 	} else {
2839f0818984STvrtko Ursulin 		intel_uncore_write(uncore, EDP_PSR_IMR, 0xffffffff);
2840f0818984STvrtko Ursulin 		intel_uncore_write(uncore, EDP_PSR_IIR, 0xffffffff);
28418241cfbeSJosé Roberto de Souza 	}
284262819dfdSJosé Roberto de Souza 
284351951ae7SMika Kuoppala 	for_each_pipe(dev_priv, pipe)
284451951ae7SMika Kuoppala 		if (intel_display_power_is_enabled(dev_priv,
284551951ae7SMika Kuoppala 						   POWER_DOMAIN_PIPE(pipe)))
2846b16b2a2fSPaulo Zanoni 			GEN8_IRQ_RESET_NDX(uncore, DE_PIPE, pipe);
284751951ae7SMika Kuoppala 
2848b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN8_DE_PORT_);
2849b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN8_DE_MISC_);
2850b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN11_DE_HPD_);
285131604222SAnusha Srivatsa 
285229b43ae2SRodrigo Vivi 	if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
2853b16b2a2fSPaulo Zanoni 		GEN3_IRQ_RESET(uncore, SDE);
285451951ae7SMika Kuoppala }
285551951ae7SMika Kuoppala 
2856a3265d85SMatt Roper static void gen11_irq_reset(struct drm_i915_private *dev_priv)
2857a3265d85SMatt Roper {
2858a3265d85SMatt Roper 	struct intel_uncore *uncore = &dev_priv->uncore;
2859a3265d85SMatt Roper 
2860a3265d85SMatt Roper 	gen11_master_intr_disable(dev_priv->uncore.regs);
2861a3265d85SMatt Roper 
2862a3265d85SMatt Roper 	gen11_gt_irq_reset(&dev_priv->gt);
2863a3265d85SMatt Roper 	gen11_display_irq_reset(dev_priv);
2864a3265d85SMatt Roper 
2865a3265d85SMatt Roper 	GEN3_IRQ_RESET(uncore, GEN11_GU_MISC_);
2866a3265d85SMatt Roper 	GEN3_IRQ_RESET(uncore, GEN8_PCU_);
2867a3265d85SMatt Roper }
2868a3265d85SMatt Roper 
28694c6c03beSDamien Lespiau void gen8_irq_power_well_post_enable(struct drm_i915_private *dev_priv,
2870001bd2cbSImre Deak 				     u8 pipe_mask)
2871d49bdb0eSPaulo Zanoni {
2872b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
2873b16b2a2fSPaulo Zanoni 
2874a9c287c9SJani Nikula 	u32 extra_ier = GEN8_PIPE_VBLANK | GEN8_PIPE_FIFO_UNDERRUN;
28756831f3e3SVille Syrjälä 	enum pipe pipe;
2876d49bdb0eSPaulo Zanoni 
287713321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
28789dfe2e3aSImre Deak 
28799dfe2e3aSImre Deak 	if (!intel_irqs_enabled(dev_priv)) {
28809dfe2e3aSImre Deak 		spin_unlock_irq(&dev_priv->irq_lock);
28819dfe2e3aSImre Deak 		return;
28829dfe2e3aSImre Deak 	}
28839dfe2e3aSImre Deak 
28846831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
2885b16b2a2fSPaulo Zanoni 		GEN8_IRQ_INIT_NDX(uncore, DE_PIPE, pipe,
28866831f3e3SVille Syrjälä 				  dev_priv->de_irq_mask[pipe],
28876831f3e3SVille Syrjälä 				  ~dev_priv->de_irq_mask[pipe] | extra_ier);
28889dfe2e3aSImre Deak 
288913321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
2890d49bdb0eSPaulo Zanoni }
2891d49bdb0eSPaulo Zanoni 
2892aae8ba84SVille Syrjälä void gen8_irq_power_well_pre_disable(struct drm_i915_private *dev_priv,
2893001bd2cbSImre Deak 				     u8 pipe_mask)
2894aae8ba84SVille Syrjälä {
2895b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
28966831f3e3SVille Syrjälä 	enum pipe pipe;
28976831f3e3SVille Syrjälä 
2898aae8ba84SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
28999dfe2e3aSImre Deak 
29009dfe2e3aSImre Deak 	if (!intel_irqs_enabled(dev_priv)) {
29019dfe2e3aSImre Deak 		spin_unlock_irq(&dev_priv->irq_lock);
29029dfe2e3aSImre Deak 		return;
29039dfe2e3aSImre Deak 	}
29049dfe2e3aSImre Deak 
29056831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
2906b16b2a2fSPaulo Zanoni 		GEN8_IRQ_RESET_NDX(uncore, DE_PIPE, pipe);
29079dfe2e3aSImre Deak 
2908aae8ba84SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
2909aae8ba84SVille Syrjälä 
2910aae8ba84SVille Syrjälä 	/* make sure we're done processing display irqs */
2911315ca4c4SVille Syrjälä 	intel_synchronize_irq(dev_priv);
2912aae8ba84SVille Syrjälä }
2913aae8ba84SVille Syrjälä 
2914b318b824SVille Syrjälä static void cherryview_irq_reset(struct drm_i915_private *dev_priv)
291543f328d7SVille Syrjälä {
2916b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
291743f328d7SVille Syrjälä 
291843f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
291943f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
292043f328d7SVille Syrjälä 
2921cf1c97dcSAndi Shyti 	gen8_gt_irq_reset(&dev_priv->gt);
292243f328d7SVille Syrjälä 
2923b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN8_PCU_);
292443f328d7SVille Syrjälä 
2925ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
29269918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
292770591a41SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
2928ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
292943f328d7SVille Syrjälä }
293043f328d7SVille Syrjälä 
293191d14251STvrtko Ursulin static u32 intel_hpd_enabled_irqs(struct drm_i915_private *dev_priv,
293287a02106SVille Syrjälä 				  const u32 hpd[HPD_NUM_PINS])
293387a02106SVille Syrjälä {
293487a02106SVille Syrjälä 	struct intel_encoder *encoder;
293587a02106SVille Syrjälä 	u32 enabled_irqs = 0;
293687a02106SVille Syrjälä 
293791c8a326SChris Wilson 	for_each_intel_encoder(&dev_priv->drm, encoder)
293887a02106SVille Syrjälä 		if (dev_priv->hotplug.stats[encoder->hpd_pin].state == HPD_ENABLED)
293987a02106SVille Syrjälä 			enabled_irqs |= hpd[encoder->hpd_pin];
294087a02106SVille Syrjälä 
294187a02106SVille Syrjälä 	return enabled_irqs;
294287a02106SVille Syrjälä }
294387a02106SVille Syrjälä 
29441a56b1a2SImre Deak static void ibx_hpd_detection_setup(struct drm_i915_private *dev_priv)
29451a56b1a2SImre Deak {
29461a56b1a2SImre Deak 	u32 hotplug;
29471a56b1a2SImre Deak 
29481a56b1a2SImre Deak 	/*
29491a56b1a2SImre Deak 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
29501a56b1a2SImre Deak 	 * duration to 2ms (which is the minimum in the Display Port spec).
29511a56b1a2SImre Deak 	 * The pulse duration bits are reserved on LPT+.
29521a56b1a2SImre Deak 	 */
29531a56b1a2SImre Deak 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
29541a56b1a2SImre Deak 	hotplug &= ~(PORTB_PULSE_DURATION_MASK |
29551a56b1a2SImre Deak 		     PORTC_PULSE_DURATION_MASK |
29561a56b1a2SImre Deak 		     PORTD_PULSE_DURATION_MASK);
29571a56b1a2SImre Deak 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
29581a56b1a2SImre Deak 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
29591a56b1a2SImre Deak 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
29601a56b1a2SImre Deak 	/*
29611a56b1a2SImre Deak 	 * When CPU and PCH are on the same package, port A
29621a56b1a2SImre Deak 	 * HPD must be enabled in both north and south.
29631a56b1a2SImre Deak 	 */
29641a56b1a2SImre Deak 	if (HAS_PCH_LPT_LP(dev_priv))
29651a56b1a2SImre Deak 		hotplug |= PORTA_HOTPLUG_ENABLE;
29661a56b1a2SImre Deak 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
29671a56b1a2SImre Deak }
29681a56b1a2SImre Deak 
296991d14251STvrtko Ursulin static void ibx_hpd_irq_setup(struct drm_i915_private *dev_priv)
297082a28bcfSDaniel Vetter {
29711a56b1a2SImre Deak 	u32 hotplug_irqs, enabled_irqs;
297282a28bcfSDaniel Vetter 
297391d14251STvrtko Ursulin 	if (HAS_PCH_IBX(dev_priv)) {
2974fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
297591d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ibx);
297682a28bcfSDaniel Vetter 	} else {
2977fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
297891d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_cpt);
297982a28bcfSDaniel Vetter 	}
298082a28bcfSDaniel Vetter 
2981fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
298282a28bcfSDaniel Vetter 
29831a56b1a2SImre Deak 	ibx_hpd_detection_setup(dev_priv);
29846dbf30ceSVille Syrjälä }
298526951cafSXiong Zhang 
298652dfdba0SLucas De Marchi static void icp_hpd_detection_setup(struct drm_i915_private *dev_priv,
298752dfdba0SLucas De Marchi 				    u32 ddi_hotplug_enable_mask,
298852dfdba0SLucas De Marchi 				    u32 tc_hotplug_enable_mask)
298931604222SAnusha Srivatsa {
299031604222SAnusha Srivatsa 	u32 hotplug;
299131604222SAnusha Srivatsa 
299231604222SAnusha Srivatsa 	hotplug = I915_READ(SHOTPLUG_CTL_DDI);
299352dfdba0SLucas De Marchi 	hotplug |= ddi_hotplug_enable_mask;
299431604222SAnusha Srivatsa 	I915_WRITE(SHOTPLUG_CTL_DDI, hotplug);
299531604222SAnusha Srivatsa 
29968ef7e340SMatt Roper 	if (tc_hotplug_enable_mask) {
299731604222SAnusha Srivatsa 		hotplug = I915_READ(SHOTPLUG_CTL_TC);
299852dfdba0SLucas De Marchi 		hotplug |= tc_hotplug_enable_mask;
299931604222SAnusha Srivatsa 		I915_WRITE(SHOTPLUG_CTL_TC, hotplug);
300031604222SAnusha Srivatsa 	}
30018ef7e340SMatt Roper }
300231604222SAnusha Srivatsa 
300340e98130SLucas De Marchi static void icp_hpd_irq_setup(struct drm_i915_private *dev_priv,
300440e98130SLucas De Marchi 			      u32 sde_ddi_mask, u32 sde_tc_mask,
300540e98130SLucas De Marchi 			      u32 ddi_enable_mask, u32 tc_enable_mask,
300640e98130SLucas De Marchi 			      const u32 *pins)
300731604222SAnusha Srivatsa {
300831604222SAnusha Srivatsa 	u32 hotplug_irqs, enabled_irqs;
300931604222SAnusha Srivatsa 
301040e98130SLucas De Marchi 	hotplug_irqs = sde_ddi_mask | sde_tc_mask;
301140e98130SLucas De Marchi 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, pins);
301231604222SAnusha Srivatsa 
3013f49108d0SMatt Roper 	I915_WRITE(SHPD_FILTER_CNT, SHPD_FILTER_CNT_500_ADJ);
3014f49108d0SMatt Roper 
301531604222SAnusha Srivatsa 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
301631604222SAnusha Srivatsa 
301740e98130SLucas De Marchi 	icp_hpd_detection_setup(dev_priv, ddi_enable_mask, tc_enable_mask);
301852dfdba0SLucas De Marchi }
301952dfdba0SLucas De Marchi 
302040e98130SLucas De Marchi /*
302140e98130SLucas De Marchi  * EHL doesn't need most of gen11_hpd_irq_setup, it's handling only the
302240e98130SLucas De Marchi  * equivalent of SDE.
302340e98130SLucas De Marchi  */
30248ef7e340SMatt Roper static void mcc_hpd_irq_setup(struct drm_i915_private *dev_priv)
30258ef7e340SMatt Roper {
302640e98130SLucas De Marchi 	icp_hpd_irq_setup(dev_priv,
302753448aedSVivek Kasireddy 			  SDE_DDI_MASK_ICP, SDE_TC_HOTPLUG_ICP(PORT_TC1),
302853448aedSVivek Kasireddy 			  ICP_DDI_HPD_ENABLE_MASK, ICP_TC_HPD_ENABLE(PORT_TC1),
3029d09ad3e7SMatt Roper 			  hpd_icp);
303031604222SAnusha Srivatsa }
303131604222SAnusha Srivatsa 
3032943682e3SMatt Roper /*
3033943682e3SMatt Roper  * JSP behaves exactly the same as MCC above except that port C is mapped to
3034943682e3SMatt Roper  * the DDI-C pins instead of the TC1 pins.  This means we should follow TGP's
3035943682e3SMatt Roper  * masks & tables rather than ICP's masks & tables.
3036943682e3SMatt Roper  */
3037943682e3SMatt Roper static void jsp_hpd_irq_setup(struct drm_i915_private *dev_priv)
3038943682e3SMatt Roper {
3039943682e3SMatt Roper 	icp_hpd_irq_setup(dev_priv,
3040943682e3SMatt Roper 			  SDE_DDI_MASK_TGP, 0,
3041943682e3SMatt Roper 			  TGP_DDI_HPD_ENABLE_MASK, 0,
3042943682e3SMatt Roper 			  hpd_tgp);
3043943682e3SMatt Roper }
3044943682e3SMatt Roper 
3045121e758eSDhinakaran Pandiyan static void gen11_hpd_detection_setup(struct drm_i915_private *dev_priv)
3046121e758eSDhinakaran Pandiyan {
3047121e758eSDhinakaran Pandiyan 	u32 hotplug;
3048121e758eSDhinakaran Pandiyan 
3049121e758eSDhinakaran Pandiyan 	hotplug = I915_READ(GEN11_TC_HOTPLUG_CTL);
3050121e758eSDhinakaran Pandiyan 	hotplug |= GEN11_HOTPLUG_CTL_ENABLE(PORT_TC1) |
3051121e758eSDhinakaran Pandiyan 		   GEN11_HOTPLUG_CTL_ENABLE(PORT_TC2) |
3052121e758eSDhinakaran Pandiyan 		   GEN11_HOTPLUG_CTL_ENABLE(PORT_TC3) |
3053121e758eSDhinakaran Pandiyan 		   GEN11_HOTPLUG_CTL_ENABLE(PORT_TC4);
3054121e758eSDhinakaran Pandiyan 	I915_WRITE(GEN11_TC_HOTPLUG_CTL, hotplug);
3055b796b971SDhinakaran Pandiyan 
3056b796b971SDhinakaran Pandiyan 	hotplug = I915_READ(GEN11_TBT_HOTPLUG_CTL);
3057b796b971SDhinakaran Pandiyan 	hotplug |= GEN11_HOTPLUG_CTL_ENABLE(PORT_TC1) |
3058b796b971SDhinakaran Pandiyan 		   GEN11_HOTPLUG_CTL_ENABLE(PORT_TC2) |
3059b796b971SDhinakaran Pandiyan 		   GEN11_HOTPLUG_CTL_ENABLE(PORT_TC3) |
3060b796b971SDhinakaran Pandiyan 		   GEN11_HOTPLUG_CTL_ENABLE(PORT_TC4);
3061b796b971SDhinakaran Pandiyan 	I915_WRITE(GEN11_TBT_HOTPLUG_CTL, hotplug);
3062121e758eSDhinakaran Pandiyan }
3063121e758eSDhinakaran Pandiyan 
3064121e758eSDhinakaran Pandiyan static void gen11_hpd_irq_setup(struct drm_i915_private *dev_priv)
3065121e758eSDhinakaran Pandiyan {
3066121e758eSDhinakaran Pandiyan 	u32 hotplug_irqs, enabled_irqs;
306748ef15d3SJosé Roberto de Souza 	const u32 *hpd;
3068121e758eSDhinakaran Pandiyan 	u32 val;
3069121e758eSDhinakaran Pandiyan 
307048ef15d3SJosé Roberto de Souza 	hpd = INTEL_GEN(dev_priv) >= 12 ? hpd_gen12 : hpd_gen11;
307148ef15d3SJosé Roberto de Souza 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd);
3072b796b971SDhinakaran Pandiyan 	hotplug_irqs = GEN11_DE_TC_HOTPLUG_MASK | GEN11_DE_TBT_HOTPLUG_MASK;
3073121e758eSDhinakaran Pandiyan 
3074121e758eSDhinakaran Pandiyan 	val = I915_READ(GEN11_DE_HPD_IMR);
3075121e758eSDhinakaran Pandiyan 	val &= ~hotplug_irqs;
3076121e758eSDhinakaran Pandiyan 	I915_WRITE(GEN11_DE_HPD_IMR, val);
3077121e758eSDhinakaran Pandiyan 	POSTING_READ(GEN11_DE_HPD_IMR);
3078121e758eSDhinakaran Pandiyan 
3079121e758eSDhinakaran Pandiyan 	gen11_hpd_detection_setup(dev_priv);
308031604222SAnusha Srivatsa 
308152dfdba0SLucas De Marchi 	if (INTEL_PCH_TYPE(dev_priv) >= PCH_TGP)
308240e98130SLucas De Marchi 		icp_hpd_irq_setup(dev_priv, SDE_DDI_MASK_TGP, SDE_TC_MASK_TGP,
308340e98130SLucas De Marchi 				  TGP_DDI_HPD_ENABLE_MASK,
308440e98130SLucas De Marchi 				  TGP_TC_HPD_ENABLE_MASK, hpd_tgp);
308552dfdba0SLucas De Marchi 	else if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
308640e98130SLucas De Marchi 		icp_hpd_irq_setup(dev_priv, SDE_DDI_MASK_ICP, SDE_TC_MASK_ICP,
308740e98130SLucas De Marchi 				  ICP_DDI_HPD_ENABLE_MASK,
308840e98130SLucas De Marchi 				  ICP_TC_HPD_ENABLE_MASK, hpd_icp);
3089121e758eSDhinakaran Pandiyan }
3090121e758eSDhinakaran Pandiyan 
30912a57d9ccSImre Deak static void spt_hpd_detection_setup(struct drm_i915_private *dev_priv)
30922a57d9ccSImre Deak {
30933b92e263SRodrigo Vivi 	u32 val, hotplug;
30943b92e263SRodrigo Vivi 
30953b92e263SRodrigo Vivi 	/* Display WA #1179 WaHardHangonHotPlug: cnp */
30963b92e263SRodrigo Vivi 	if (HAS_PCH_CNP(dev_priv)) {
30973b92e263SRodrigo Vivi 		val = I915_READ(SOUTH_CHICKEN1);
30983b92e263SRodrigo Vivi 		val &= ~CHASSIS_CLK_REQ_DURATION_MASK;
30993b92e263SRodrigo Vivi 		val |= CHASSIS_CLK_REQ_DURATION(0xf);
31003b92e263SRodrigo Vivi 		I915_WRITE(SOUTH_CHICKEN1, val);
31013b92e263SRodrigo Vivi 	}
31022a57d9ccSImre Deak 
31032a57d9ccSImre Deak 	/* Enable digital hotplug on the PCH */
31042a57d9ccSImre Deak 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
31052a57d9ccSImre Deak 	hotplug |= PORTA_HOTPLUG_ENABLE |
31062a57d9ccSImre Deak 		   PORTB_HOTPLUG_ENABLE |
31072a57d9ccSImre Deak 		   PORTC_HOTPLUG_ENABLE |
31082a57d9ccSImre Deak 		   PORTD_HOTPLUG_ENABLE;
31092a57d9ccSImre Deak 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
31102a57d9ccSImre Deak 
31112a57d9ccSImre Deak 	hotplug = I915_READ(PCH_PORT_HOTPLUG2);
31122a57d9ccSImre Deak 	hotplug |= PORTE_HOTPLUG_ENABLE;
31132a57d9ccSImre Deak 	I915_WRITE(PCH_PORT_HOTPLUG2, hotplug);
31142a57d9ccSImre Deak }
31152a57d9ccSImre Deak 
311691d14251STvrtko Ursulin static void spt_hpd_irq_setup(struct drm_i915_private *dev_priv)
31176dbf30ceSVille Syrjälä {
31182a57d9ccSImre Deak 	u32 hotplug_irqs, enabled_irqs;
31196dbf30ceSVille Syrjälä 
3120f49108d0SMatt Roper 	if (INTEL_PCH_TYPE(dev_priv) >= PCH_CNP)
3121f49108d0SMatt Roper 		I915_WRITE(SHPD_FILTER_CNT, SHPD_FILTER_CNT_500_ADJ);
3122f49108d0SMatt Roper 
31236dbf30ceSVille Syrjälä 	hotplug_irqs = SDE_HOTPLUG_MASK_SPT;
312491d14251STvrtko Ursulin 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_spt);
31256dbf30ceSVille Syrjälä 
31266dbf30ceSVille Syrjälä 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
31276dbf30ceSVille Syrjälä 
31282a57d9ccSImre Deak 	spt_hpd_detection_setup(dev_priv);
312926951cafSXiong Zhang }
31307fe0b973SKeith Packard 
31311a56b1a2SImre Deak static void ilk_hpd_detection_setup(struct drm_i915_private *dev_priv)
31321a56b1a2SImre Deak {
31331a56b1a2SImre Deak 	u32 hotplug;
31341a56b1a2SImre Deak 
31351a56b1a2SImre Deak 	/*
31361a56b1a2SImre Deak 	 * Enable digital hotplug on the CPU, and configure the DP short pulse
31371a56b1a2SImre Deak 	 * duration to 2ms (which is the minimum in the Display Port spec)
31381a56b1a2SImre Deak 	 * The pulse duration bits are reserved on HSW+.
31391a56b1a2SImre Deak 	 */
31401a56b1a2SImre Deak 	hotplug = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
31411a56b1a2SImre Deak 	hotplug &= ~DIGITAL_PORTA_PULSE_DURATION_MASK;
31421a56b1a2SImre Deak 	hotplug |= DIGITAL_PORTA_HOTPLUG_ENABLE |
31431a56b1a2SImre Deak 		   DIGITAL_PORTA_PULSE_DURATION_2ms;
31441a56b1a2SImre Deak 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, hotplug);
31451a56b1a2SImre Deak }
31461a56b1a2SImre Deak 
314791d14251STvrtko Ursulin static void ilk_hpd_irq_setup(struct drm_i915_private *dev_priv)
3148e4ce95aaSVille Syrjälä {
31491a56b1a2SImre Deak 	u32 hotplug_irqs, enabled_irqs;
3150e4ce95aaSVille Syrjälä 
315191d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 8) {
31523a3b3c7dSVille Syrjälä 		hotplug_irqs = GEN8_PORT_DP_A_HOTPLUG;
315391d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_bdw);
31543a3b3c7dSVille Syrjälä 
31553a3b3c7dSVille Syrjälä 		bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
315691d14251STvrtko Ursulin 	} else if (INTEL_GEN(dev_priv) >= 7) {
315723bb4cb5SVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG_IVB;
315891d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ivb);
31593a3b3c7dSVille Syrjälä 
31603a3b3c7dSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
316123bb4cb5SVille Syrjälä 	} else {
3162e4ce95aaSVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG;
316391d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ilk);
3164e4ce95aaSVille Syrjälä 
3165e4ce95aaSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
31663a3b3c7dSVille Syrjälä 	}
3167e4ce95aaSVille Syrjälä 
31681a56b1a2SImre Deak 	ilk_hpd_detection_setup(dev_priv);
3169e4ce95aaSVille Syrjälä 
317091d14251STvrtko Ursulin 	ibx_hpd_irq_setup(dev_priv);
3171e4ce95aaSVille Syrjälä }
3172e4ce95aaSVille Syrjälä 
31732a57d9ccSImre Deak static void __bxt_hpd_detection_setup(struct drm_i915_private *dev_priv,
31742a57d9ccSImre Deak 				      u32 enabled_irqs)
3175e0a20ad7SShashank Sharma {
31762a57d9ccSImre Deak 	u32 hotplug;
3177e0a20ad7SShashank Sharma 
3178a52bb15bSVille Syrjälä 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
31792a57d9ccSImre Deak 	hotplug |= PORTA_HOTPLUG_ENABLE |
31802a57d9ccSImre Deak 		   PORTB_HOTPLUG_ENABLE |
31812a57d9ccSImre Deak 		   PORTC_HOTPLUG_ENABLE;
3182d252bf68SShubhangi Shrivastava 
3183*00376ccfSWambui Karuga 	drm_dbg_kms(&dev_priv->drm,
3184*00376ccfSWambui Karuga 		    "Invert bit setting: hp_ctl:%x hp_port:%x\n",
3185d252bf68SShubhangi Shrivastava 		    hotplug, enabled_irqs);
3186d252bf68SShubhangi Shrivastava 	hotplug &= ~BXT_DDI_HPD_INVERT_MASK;
3187d252bf68SShubhangi Shrivastava 
3188d252bf68SShubhangi Shrivastava 	/*
3189d252bf68SShubhangi Shrivastava 	 * For BXT invert bit has to be set based on AOB design
3190d252bf68SShubhangi Shrivastava 	 * for HPD detection logic, update it based on VBT fields.
3191d252bf68SShubhangi Shrivastava 	 */
3192d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIA) &&
3193d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_A))
3194d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIA_HPD_INVERT;
3195d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIB) &&
3196d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_B))
3197d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIB_HPD_INVERT;
3198d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIC) &&
3199d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_C))
3200d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIC_HPD_INVERT;
3201d252bf68SShubhangi Shrivastava 
3202a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
3203e0a20ad7SShashank Sharma }
3204e0a20ad7SShashank Sharma 
32052a57d9ccSImre Deak static void bxt_hpd_detection_setup(struct drm_i915_private *dev_priv)
32062a57d9ccSImre Deak {
32072a57d9ccSImre Deak 	__bxt_hpd_detection_setup(dev_priv, BXT_DE_PORT_HOTPLUG_MASK);
32082a57d9ccSImre Deak }
32092a57d9ccSImre Deak 
32102a57d9ccSImre Deak static void bxt_hpd_irq_setup(struct drm_i915_private *dev_priv)
32112a57d9ccSImre Deak {
32122a57d9ccSImre Deak 	u32 hotplug_irqs, enabled_irqs;
32132a57d9ccSImre Deak 
32142a57d9ccSImre Deak 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_bxt);
32152a57d9ccSImre Deak 	hotplug_irqs = BXT_DE_PORT_HOTPLUG_MASK;
32162a57d9ccSImre Deak 
32172a57d9ccSImre Deak 	bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
32182a57d9ccSImre Deak 
32192a57d9ccSImre Deak 	__bxt_hpd_detection_setup(dev_priv, enabled_irqs);
32202a57d9ccSImre Deak }
32212a57d9ccSImre Deak 
3222b318b824SVille Syrjälä static void ibx_irq_postinstall(struct drm_i915_private *dev_priv)
3223d46da437SPaulo Zanoni {
322482a28bcfSDaniel Vetter 	u32 mask;
3225d46da437SPaulo Zanoni 
32266e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
3227692a04cfSDaniel Vetter 		return;
3228692a04cfSDaniel Vetter 
32296e266956STvrtko Ursulin 	if (HAS_PCH_IBX(dev_priv))
32305c673b60SDaniel Vetter 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
32314ebc6509SDhinakaran Pandiyan 	else if (HAS_PCH_CPT(dev_priv) || HAS_PCH_LPT(dev_priv))
32325c673b60SDaniel Vetter 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
32334ebc6509SDhinakaran Pandiyan 	else
32344ebc6509SDhinakaran Pandiyan 		mask = SDE_GMBUS_CPT;
32358664281bSPaulo Zanoni 
323665f42cdcSPaulo Zanoni 	gen3_assert_iir_is_zero(&dev_priv->uncore, SDEIIR);
3237d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
32382a57d9ccSImre Deak 
32392a57d9ccSImre Deak 	if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
32402a57d9ccSImre Deak 	    HAS_PCH_LPT(dev_priv))
32411a56b1a2SImre Deak 		ibx_hpd_detection_setup(dev_priv);
32422a57d9ccSImre Deak 	else
32432a57d9ccSImre Deak 		spt_hpd_detection_setup(dev_priv);
3244d46da437SPaulo Zanoni }
3245d46da437SPaulo Zanoni 
32469eae5e27SLucas De Marchi static void ilk_irq_postinstall(struct drm_i915_private *dev_priv)
3247036a4a7dSZhenyu Wang {
3248b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
32498e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
32508e76f8dcSPaulo Zanoni 
3251b243f530STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 7) {
32528e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
3253842ebf7aSVille Syrjälä 				DE_PCH_EVENT_IVB | DE_AUX_CHANNEL_A_IVB);
32548e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
325523bb4cb5SVille Syrjälä 			      DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB |
325623bb4cb5SVille Syrjälä 			      DE_DP_A_HOTPLUG_IVB);
32578e76f8dcSPaulo Zanoni 	} else {
32588e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3259842ebf7aSVille Syrjälä 				DE_AUX_CHANNEL_A | DE_PIPEB_CRC_DONE |
3260842ebf7aSVille Syrjälä 				DE_PIPEA_CRC_DONE | DE_POISON);
3261e4ce95aaSVille Syrjälä 		extra_mask = (DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
3262e4ce95aaSVille Syrjälä 			      DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN |
3263e4ce95aaSVille Syrjälä 			      DE_DP_A_HOTPLUG);
32648e76f8dcSPaulo Zanoni 	}
3265036a4a7dSZhenyu Wang 
3266fc340442SDaniel Vetter 	if (IS_HASWELL(dev_priv)) {
3267b16b2a2fSPaulo Zanoni 		gen3_assert_iir_is_zero(uncore, EDP_PSR_IIR);
3268fc340442SDaniel Vetter 		display_mask |= DE_EDP_PSR_INT_HSW;
3269fc340442SDaniel Vetter 	}
3270fc340442SDaniel Vetter 
32711ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3272036a4a7dSZhenyu Wang 
3273b318b824SVille Syrjälä 	ibx_irq_pre_postinstall(dev_priv);
3274622364b6SPaulo Zanoni 
3275b16b2a2fSPaulo Zanoni 	GEN3_IRQ_INIT(uncore, DE, dev_priv->irq_mask,
3276b16b2a2fSPaulo Zanoni 		      display_mask | extra_mask);
3277036a4a7dSZhenyu Wang 
3278cf1c97dcSAndi Shyti 	gen5_gt_irq_postinstall(&dev_priv->gt);
3279036a4a7dSZhenyu Wang 
32801a56b1a2SImre Deak 	ilk_hpd_detection_setup(dev_priv);
32811a56b1a2SImre Deak 
3282b318b824SVille Syrjälä 	ibx_irq_postinstall(dev_priv);
32837fe0b973SKeith Packard 
328450a0bc90STvrtko Ursulin 	if (IS_IRONLAKE_M(dev_priv)) {
32856005ce42SDaniel Vetter 		/* Enable PCU event interrupts
32866005ce42SDaniel Vetter 		 *
32876005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
32884bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
32894bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
3290d6207435SDaniel Vetter 		spin_lock_irq(&dev_priv->irq_lock);
3291fbdedaeaSVille Syrjälä 		ilk_enable_display_irq(dev_priv, DE_PCU_EVENT);
3292d6207435SDaniel Vetter 		spin_unlock_irq(&dev_priv->irq_lock);
3293f97108d1SJesse Barnes 	}
3294036a4a7dSZhenyu Wang }
3295036a4a7dSZhenyu Wang 
3296f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3297f8b79e58SImre Deak {
329867520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
3299f8b79e58SImre Deak 
3300f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3301f8b79e58SImre Deak 		return;
3302f8b79e58SImre Deak 
3303f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3304f8b79e58SImre Deak 
3305d6c69803SVille Syrjälä 	if (intel_irqs_enabled(dev_priv)) {
3306d6c69803SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3307ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3308f8b79e58SImre Deak 	}
3309d6c69803SVille Syrjälä }
3310f8b79e58SImre Deak 
3311f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3312f8b79e58SImre Deak {
331367520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
3314f8b79e58SImre Deak 
3315f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3316f8b79e58SImre Deak 		return;
3317f8b79e58SImre Deak 
3318f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3319f8b79e58SImre Deak 
3320950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3321ad22d106SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3322f8b79e58SImre Deak }
3323f8b79e58SImre Deak 
33240e6c9a9eSVille Syrjälä 
3325b318b824SVille Syrjälä static void valleyview_irq_postinstall(struct drm_i915_private *dev_priv)
33260e6c9a9eSVille Syrjälä {
3327cf1c97dcSAndi Shyti 	gen5_gt_irq_postinstall(&dev_priv->gt);
33287e231dbeSJesse Barnes 
3329ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
33309918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3331ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3332ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3333ad22d106SVille Syrjälä 
33347e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
333534c7b8a7SVille Syrjälä 	POSTING_READ(VLV_MASTER_IER);
333620afbda2SDaniel Vetter }
333720afbda2SDaniel Vetter 
3338abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3339abd58f01SBen Widawsky {
3340b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
3341b16b2a2fSPaulo Zanoni 
3342a9c287c9SJani Nikula 	u32 de_pipe_masked = GEN8_PIPE_CDCLK_CRC_DONE;
3343a9c287c9SJani Nikula 	u32 de_pipe_enables;
33443a3b3c7dSVille Syrjälä 	u32 de_port_masked = GEN8_AUX_CHANNEL_A;
33453a3b3c7dSVille Syrjälä 	u32 de_port_enables;
3346df0d28c1SDhinakaran Pandiyan 	u32 de_misc_masked = GEN8_DE_EDP_PSR;
33473a3b3c7dSVille Syrjälä 	enum pipe pipe;
3348770de83dSDamien Lespiau 
3349df0d28c1SDhinakaran Pandiyan 	if (INTEL_GEN(dev_priv) <= 10)
3350df0d28c1SDhinakaran Pandiyan 		de_misc_masked |= GEN8_DE_MISC_GSE;
3351df0d28c1SDhinakaran Pandiyan 
3352bca2bf2aSPandiyan, Dhinakaran 	if (INTEL_GEN(dev_priv) >= 9) {
3353842ebf7aSVille Syrjälä 		de_pipe_masked |= GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
33543a3b3c7dSVille Syrjälä 		de_port_masked |= GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
335588e04703SJesse Barnes 				  GEN9_AUX_CHANNEL_D;
3356cc3f90f0SAnder Conselvan de Oliveira 		if (IS_GEN9_LP(dev_priv))
33573a3b3c7dSVille Syrjälä 			de_port_masked |= BXT_DE_PORT_GMBUS;
33583a3b3c7dSVille Syrjälä 	} else {
3359842ebf7aSVille Syrjälä 		de_pipe_masked |= GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
33603a3b3c7dSVille Syrjälä 	}
3361770de83dSDamien Lespiau 
3362bb187e93SJames Ausmus 	if (INTEL_GEN(dev_priv) >= 11)
3363bb187e93SJames Ausmus 		de_port_masked |= ICL_AUX_CHANNEL_E;
3364bb187e93SJames Ausmus 
33659bb635d9SDhinakaran Pandiyan 	if (IS_CNL_WITH_PORT_F(dev_priv) || INTEL_GEN(dev_priv) >= 11)
3366a324fcacSRodrigo Vivi 		de_port_masked |= CNL_AUX_CHANNEL_F;
3367a324fcacSRodrigo Vivi 
3368770de83dSDamien Lespiau 	de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
3369770de83dSDamien Lespiau 					   GEN8_PIPE_FIFO_UNDERRUN;
3370770de83dSDamien Lespiau 
33713a3b3c7dSVille Syrjälä 	de_port_enables = de_port_masked;
3372cc3f90f0SAnder Conselvan de Oliveira 	if (IS_GEN9_LP(dev_priv))
3373a52bb15bSVille Syrjälä 		de_port_enables |= BXT_DE_PORT_HOTPLUG_MASK;
3374a52bb15bSVille Syrjälä 	else if (IS_BROADWELL(dev_priv))
33753a3b3c7dSVille Syrjälä 		de_port_enables |= GEN8_PORT_DP_A_HOTPLUG;
33763a3b3c7dSVille Syrjälä 
33778241cfbeSJosé Roberto de Souza 	if (INTEL_GEN(dev_priv) >= 12) {
33788241cfbeSJosé Roberto de Souza 		enum transcoder trans;
33798241cfbeSJosé Roberto de Souza 
33808241cfbeSJosé Roberto de Souza 		for (trans = TRANSCODER_A; trans <= TRANSCODER_D; trans++) {
33818241cfbeSJosé Roberto de Souza 			enum intel_display_power_domain domain;
33828241cfbeSJosé Roberto de Souza 
33838241cfbeSJosé Roberto de Souza 			domain = POWER_DOMAIN_TRANSCODER(trans);
33848241cfbeSJosé Roberto de Souza 			if (!intel_display_power_is_enabled(dev_priv, domain))
33858241cfbeSJosé Roberto de Souza 				continue;
33868241cfbeSJosé Roberto de Souza 
33878241cfbeSJosé Roberto de Souza 			gen3_assert_iir_is_zero(uncore, TRANS_PSR_IIR(trans));
33888241cfbeSJosé Roberto de Souza 		}
33898241cfbeSJosé Roberto de Souza 	} else {
3390b16b2a2fSPaulo Zanoni 		gen3_assert_iir_is_zero(uncore, EDP_PSR_IIR);
33918241cfbeSJosé Roberto de Souza 	}
3392e04f7eceSVille Syrjälä 
33930a195c02SMika Kahola 	for_each_pipe(dev_priv, pipe) {
33940a195c02SMika Kahola 		dev_priv->de_irq_mask[pipe] = ~de_pipe_masked;
3395abd58f01SBen Widawsky 
3396f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3397813bde43SPaulo Zanoni 				POWER_DOMAIN_PIPE(pipe)))
3398b16b2a2fSPaulo Zanoni 			GEN8_IRQ_INIT_NDX(uncore, DE_PIPE, pipe,
3399813bde43SPaulo Zanoni 					  dev_priv->de_irq_mask[pipe],
340035079899SPaulo Zanoni 					  de_pipe_enables);
34010a195c02SMika Kahola 	}
3402abd58f01SBen Widawsky 
3403b16b2a2fSPaulo Zanoni 	GEN3_IRQ_INIT(uncore, GEN8_DE_PORT_, ~de_port_masked, de_port_enables);
3404b16b2a2fSPaulo Zanoni 	GEN3_IRQ_INIT(uncore, GEN8_DE_MISC_, ~de_misc_masked, de_misc_masked);
34052a57d9ccSImre Deak 
3406121e758eSDhinakaran Pandiyan 	if (INTEL_GEN(dev_priv) >= 11) {
3407121e758eSDhinakaran Pandiyan 		u32 de_hpd_masked = 0;
3408b796b971SDhinakaran Pandiyan 		u32 de_hpd_enables = GEN11_DE_TC_HOTPLUG_MASK |
3409b796b971SDhinakaran Pandiyan 				     GEN11_DE_TBT_HOTPLUG_MASK;
3410121e758eSDhinakaran Pandiyan 
3411b16b2a2fSPaulo Zanoni 		GEN3_IRQ_INIT(uncore, GEN11_DE_HPD_, ~de_hpd_masked,
3412b16b2a2fSPaulo Zanoni 			      de_hpd_enables);
3413121e758eSDhinakaran Pandiyan 		gen11_hpd_detection_setup(dev_priv);
3414121e758eSDhinakaran Pandiyan 	} else if (IS_GEN9_LP(dev_priv)) {
34152a57d9ccSImre Deak 		bxt_hpd_detection_setup(dev_priv);
3416121e758eSDhinakaran Pandiyan 	} else if (IS_BROADWELL(dev_priv)) {
34171a56b1a2SImre Deak 		ilk_hpd_detection_setup(dev_priv);
3418abd58f01SBen Widawsky 	}
3419121e758eSDhinakaran Pandiyan }
3420abd58f01SBen Widawsky 
3421b318b824SVille Syrjälä static void gen8_irq_postinstall(struct drm_i915_private *dev_priv)
3422abd58f01SBen Widawsky {
34236e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
3424b318b824SVille Syrjälä 		ibx_irq_pre_postinstall(dev_priv);
3425622364b6SPaulo Zanoni 
3426cf1c97dcSAndi Shyti 	gen8_gt_irq_postinstall(&dev_priv->gt);
3427abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3428abd58f01SBen Widawsky 
34296e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
3430b318b824SVille Syrjälä 		ibx_irq_postinstall(dev_priv);
3431abd58f01SBen Widawsky 
343225286aacSDaniele Ceraolo Spurio 	gen8_master_intr_enable(dev_priv->uncore.regs);
3433abd58f01SBen Widawsky }
3434abd58f01SBen Widawsky 
3435b318b824SVille Syrjälä static void icp_irq_postinstall(struct drm_i915_private *dev_priv)
343631604222SAnusha Srivatsa {
343731604222SAnusha Srivatsa 	u32 mask = SDE_GMBUS_ICP;
343831604222SAnusha Srivatsa 
343948a1b8d4SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, I915_READ(SDEIER) != 0);
344031604222SAnusha Srivatsa 	I915_WRITE(SDEIER, 0xffffffff);
344131604222SAnusha Srivatsa 	POSTING_READ(SDEIER);
344231604222SAnusha Srivatsa 
344365f42cdcSPaulo Zanoni 	gen3_assert_iir_is_zero(&dev_priv->uncore, SDEIIR);
344431604222SAnusha Srivatsa 	I915_WRITE(SDEIMR, ~mask);
344531604222SAnusha Srivatsa 
344652dfdba0SLucas De Marchi 	if (HAS_PCH_TGP(dev_priv))
344752dfdba0SLucas De Marchi 		icp_hpd_detection_setup(dev_priv, TGP_DDI_HPD_ENABLE_MASK,
344852dfdba0SLucas De Marchi 					TGP_TC_HPD_ENABLE_MASK);
3449e83c4673SVivek Kasireddy 	else if (HAS_PCH_JSP(dev_priv))
34508ef7e340SMatt Roper 		icp_hpd_detection_setup(dev_priv, TGP_DDI_HPD_ENABLE_MASK, 0);
3451e83c4673SVivek Kasireddy 	else if (HAS_PCH_MCC(dev_priv))
3452e83c4673SVivek Kasireddy 		icp_hpd_detection_setup(dev_priv, ICP_DDI_HPD_ENABLE_MASK,
3453e83c4673SVivek Kasireddy 					ICP_TC_HPD_ENABLE(PORT_TC1));
345452dfdba0SLucas De Marchi 	else
345552dfdba0SLucas De Marchi 		icp_hpd_detection_setup(dev_priv, ICP_DDI_HPD_ENABLE_MASK,
345652dfdba0SLucas De Marchi 					ICP_TC_HPD_ENABLE_MASK);
345731604222SAnusha Srivatsa }
345831604222SAnusha Srivatsa 
3459b318b824SVille Syrjälä static void gen11_irq_postinstall(struct drm_i915_private *dev_priv)
346051951ae7SMika Kuoppala {
3461b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
3462df0d28c1SDhinakaran Pandiyan 	u32 gu_misc_masked = GEN11_GU_MISC_GSE;
346351951ae7SMika Kuoppala 
346429b43ae2SRodrigo Vivi 	if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
3465b318b824SVille Syrjälä 		icp_irq_postinstall(dev_priv);
346631604222SAnusha Srivatsa 
34679b77011eSTvrtko Ursulin 	gen11_gt_irq_postinstall(&dev_priv->gt);
346851951ae7SMika Kuoppala 	gen8_de_irq_postinstall(dev_priv);
346951951ae7SMika Kuoppala 
3470b16b2a2fSPaulo Zanoni 	GEN3_IRQ_INIT(uncore, GEN11_GU_MISC_, ~gu_misc_masked, gu_misc_masked);
3471df0d28c1SDhinakaran Pandiyan 
347251951ae7SMika Kuoppala 	I915_WRITE(GEN11_DISPLAY_INT_CTL, GEN11_DISPLAY_IRQ_ENABLE);
347351951ae7SMika Kuoppala 
34749b77011eSTvrtko Ursulin 	gen11_master_intr_enable(uncore->regs);
3475c25f0c6aSDaniele Ceraolo Spurio 	POSTING_READ(GEN11_GFX_MSTR_IRQ);
347651951ae7SMika Kuoppala }
347751951ae7SMika Kuoppala 
3478b318b824SVille Syrjälä static void cherryview_irq_postinstall(struct drm_i915_private *dev_priv)
347943f328d7SVille Syrjälä {
3480cf1c97dcSAndi Shyti 	gen8_gt_irq_postinstall(&dev_priv->gt);
348143f328d7SVille Syrjälä 
3482ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
34839918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3484ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3485ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3486ad22d106SVille Syrjälä 
3487e5328c43SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
348843f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
348943f328d7SVille Syrjälä }
349043f328d7SVille Syrjälä 
3491b318b824SVille Syrjälä static void i8xx_irq_reset(struct drm_i915_private *dev_priv)
3492c2798b19SChris Wilson {
3493b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
3494c2798b19SChris Wilson 
349544d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
349644d9241eSVille Syrjälä 
3497b16b2a2fSPaulo Zanoni 	GEN2_IRQ_RESET(uncore);
3498c2798b19SChris Wilson }
3499c2798b19SChris Wilson 
3500b318b824SVille Syrjälä static void i8xx_irq_postinstall(struct drm_i915_private *dev_priv)
3501c2798b19SChris Wilson {
3502b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
3503e9e9848aSVille Syrjälä 	u16 enable_mask;
3504c2798b19SChris Wilson 
35054f5fd91fSTvrtko Ursulin 	intel_uncore_write16(uncore,
35064f5fd91fSTvrtko Ursulin 			     EMR,
35074f5fd91fSTvrtko Ursulin 			     ~(I915_ERROR_PAGE_TABLE |
3508045cebd2SVille Syrjälä 			       I915_ERROR_MEMORY_REFRESH));
3509c2798b19SChris Wilson 
3510c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
3511c2798b19SChris Wilson 	dev_priv->irq_mask =
3512c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
351316659bc5SVille Syrjälä 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
351416659bc5SVille Syrjälä 		  I915_MASTER_ERROR_INTERRUPT);
3515c2798b19SChris Wilson 
3516e9e9848aSVille Syrjälä 	enable_mask =
3517c2798b19SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3518c2798b19SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
351916659bc5SVille Syrjälä 		I915_MASTER_ERROR_INTERRUPT |
3520e9e9848aSVille Syrjälä 		I915_USER_INTERRUPT;
3521e9e9848aSVille Syrjälä 
3522b16b2a2fSPaulo Zanoni 	GEN2_IRQ_INIT(uncore, dev_priv->irq_mask, enable_mask);
3523c2798b19SChris Wilson 
3524379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3525379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3526d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3527755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3528755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3529d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3530c2798b19SChris Wilson }
3531c2798b19SChris Wilson 
35324f5fd91fSTvrtko Ursulin static void i8xx_error_irq_ack(struct drm_i915_private *i915,
353378c357ddSVille Syrjälä 			       u16 *eir, u16 *eir_stuck)
353478c357ddSVille Syrjälä {
35354f5fd91fSTvrtko Ursulin 	struct intel_uncore *uncore = &i915->uncore;
353678c357ddSVille Syrjälä 	u16 emr;
353778c357ddSVille Syrjälä 
35384f5fd91fSTvrtko Ursulin 	*eir = intel_uncore_read16(uncore, EIR);
353978c357ddSVille Syrjälä 
354078c357ddSVille Syrjälä 	if (*eir)
35414f5fd91fSTvrtko Ursulin 		intel_uncore_write16(uncore, EIR, *eir);
354278c357ddSVille Syrjälä 
35434f5fd91fSTvrtko Ursulin 	*eir_stuck = intel_uncore_read16(uncore, EIR);
354478c357ddSVille Syrjälä 	if (*eir_stuck == 0)
354578c357ddSVille Syrjälä 		return;
354678c357ddSVille Syrjälä 
354778c357ddSVille Syrjälä 	/*
354878c357ddSVille Syrjälä 	 * Toggle all EMR bits to make sure we get an edge
354978c357ddSVille Syrjälä 	 * in the ISR master error bit if we don't clear
355078c357ddSVille Syrjälä 	 * all the EIR bits. Otherwise the edge triggered
355178c357ddSVille Syrjälä 	 * IIR on i965/g4x wouldn't notice that an interrupt
355278c357ddSVille Syrjälä 	 * is still pending. Also some EIR bits can't be
355378c357ddSVille Syrjälä 	 * cleared except by handling the underlying error
355478c357ddSVille Syrjälä 	 * (or by a GPU reset) so we mask any bit that
355578c357ddSVille Syrjälä 	 * remains set.
355678c357ddSVille Syrjälä 	 */
35574f5fd91fSTvrtko Ursulin 	emr = intel_uncore_read16(uncore, EMR);
35584f5fd91fSTvrtko Ursulin 	intel_uncore_write16(uncore, EMR, 0xffff);
35594f5fd91fSTvrtko Ursulin 	intel_uncore_write16(uncore, EMR, emr | *eir_stuck);
356078c357ddSVille Syrjälä }
356178c357ddSVille Syrjälä 
356278c357ddSVille Syrjälä static void i8xx_error_irq_handler(struct drm_i915_private *dev_priv,
356378c357ddSVille Syrjälä 				   u16 eir, u16 eir_stuck)
356478c357ddSVille Syrjälä {
356578c357ddSVille Syrjälä 	DRM_DEBUG("Master Error: EIR 0x%04x\n", eir);
356678c357ddSVille Syrjälä 
356778c357ddSVille Syrjälä 	if (eir_stuck)
3568*00376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm, "EIR stuck: 0x%04x, masked\n",
3569*00376ccfSWambui Karuga 			eir_stuck);
357078c357ddSVille Syrjälä }
357178c357ddSVille Syrjälä 
357278c357ddSVille Syrjälä static void i9xx_error_irq_ack(struct drm_i915_private *dev_priv,
357378c357ddSVille Syrjälä 			       u32 *eir, u32 *eir_stuck)
357478c357ddSVille Syrjälä {
357578c357ddSVille Syrjälä 	u32 emr;
357678c357ddSVille Syrjälä 
357778c357ddSVille Syrjälä 	*eir = I915_READ(EIR);
357878c357ddSVille Syrjälä 
357978c357ddSVille Syrjälä 	I915_WRITE(EIR, *eir);
358078c357ddSVille Syrjälä 
358178c357ddSVille Syrjälä 	*eir_stuck = I915_READ(EIR);
358278c357ddSVille Syrjälä 	if (*eir_stuck == 0)
358378c357ddSVille Syrjälä 		return;
358478c357ddSVille Syrjälä 
358578c357ddSVille Syrjälä 	/*
358678c357ddSVille Syrjälä 	 * Toggle all EMR bits to make sure we get an edge
358778c357ddSVille Syrjälä 	 * in the ISR master error bit if we don't clear
358878c357ddSVille Syrjälä 	 * all the EIR bits. Otherwise the edge triggered
358978c357ddSVille Syrjälä 	 * IIR on i965/g4x wouldn't notice that an interrupt
359078c357ddSVille Syrjälä 	 * is still pending. Also some EIR bits can't be
359178c357ddSVille Syrjälä 	 * cleared except by handling the underlying error
359278c357ddSVille Syrjälä 	 * (or by a GPU reset) so we mask any bit that
359378c357ddSVille Syrjälä 	 * remains set.
359478c357ddSVille Syrjälä 	 */
359578c357ddSVille Syrjälä 	emr = I915_READ(EMR);
359678c357ddSVille Syrjälä 	I915_WRITE(EMR, 0xffffffff);
359778c357ddSVille Syrjälä 	I915_WRITE(EMR, emr | *eir_stuck);
359878c357ddSVille Syrjälä }
359978c357ddSVille Syrjälä 
360078c357ddSVille Syrjälä static void i9xx_error_irq_handler(struct drm_i915_private *dev_priv,
360178c357ddSVille Syrjälä 				   u32 eir, u32 eir_stuck)
360278c357ddSVille Syrjälä {
360378c357ddSVille Syrjälä 	DRM_DEBUG("Master Error, EIR 0x%08x\n", eir);
360478c357ddSVille Syrjälä 
360578c357ddSVille Syrjälä 	if (eir_stuck)
3606*00376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm, "EIR stuck: 0x%08x, masked\n",
3607*00376ccfSWambui Karuga 			eir_stuck);
360878c357ddSVille Syrjälä }
360978c357ddSVille Syrjälä 
3610ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3611c2798b19SChris Wilson {
3612b318b824SVille Syrjälä 	struct drm_i915_private *dev_priv = arg;
3613af722d28SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
3614c2798b19SChris Wilson 
36152dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
36162dd2a883SImre Deak 		return IRQ_NONE;
36172dd2a883SImre Deak 
36181f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
36199102650fSDaniele Ceraolo Spurio 	disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
36201f814dacSImre Deak 
3621af722d28SVille Syrjälä 	do {
3622af722d28SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
362378c357ddSVille Syrjälä 		u16 eir = 0, eir_stuck = 0;
3624af722d28SVille Syrjälä 		u16 iir;
3625af722d28SVille Syrjälä 
36264f5fd91fSTvrtko Ursulin 		iir = intel_uncore_read16(&dev_priv->uncore, GEN2_IIR);
3627c2798b19SChris Wilson 		if (iir == 0)
3628af722d28SVille Syrjälä 			break;
3629c2798b19SChris Wilson 
3630af722d28SVille Syrjälä 		ret = IRQ_HANDLED;
3631c2798b19SChris Wilson 
3632eb64343cSVille Syrjälä 		/* Call regardless, as some status bits might not be
3633eb64343cSVille Syrjälä 		 * signalled in iir */
3634eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
3635c2798b19SChris Wilson 
363678c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
363778c357ddSVille Syrjälä 			i8xx_error_irq_ack(dev_priv, &eir, &eir_stuck);
363878c357ddSVille Syrjälä 
36394f5fd91fSTvrtko Ursulin 		intel_uncore_write16(&dev_priv->uncore, GEN2_IIR, iir);
3640c2798b19SChris Wilson 
3641c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
364254400257SChris Wilson 			intel_engine_signal_breadcrumbs(dev_priv->engine[RCS0]);
3643c2798b19SChris Wilson 
364478c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
364578c357ddSVille Syrjälä 			i8xx_error_irq_handler(dev_priv, eir, eir_stuck);
3646af722d28SVille Syrjälä 
3647eb64343cSVille Syrjälä 		i8xx_pipestat_irq_handler(dev_priv, iir, pipe_stats);
3648af722d28SVille Syrjälä 	} while (0);
3649c2798b19SChris Wilson 
36509102650fSDaniele Ceraolo Spurio 	enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
36511f814dacSImre Deak 
36521f814dacSImre Deak 	return ret;
3653c2798b19SChris Wilson }
3654c2798b19SChris Wilson 
3655b318b824SVille Syrjälä static void i915_irq_reset(struct drm_i915_private *dev_priv)
3656a266c7d5SChris Wilson {
3657b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
3658a266c7d5SChris Wilson 
365956b857a5STvrtko Ursulin 	if (I915_HAS_HOTPLUG(dev_priv)) {
36600706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
3661a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3662a266c7d5SChris Wilson 	}
3663a266c7d5SChris Wilson 
366444d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
366544d9241eSVille Syrjälä 
3666b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN2_);
3667a266c7d5SChris Wilson }
3668a266c7d5SChris Wilson 
3669b318b824SVille Syrjälä static void i915_irq_postinstall(struct drm_i915_private *dev_priv)
3670a266c7d5SChris Wilson {
3671b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
367238bde180SChris Wilson 	u32 enable_mask;
3673a266c7d5SChris Wilson 
3674045cebd2SVille Syrjälä 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE |
3675045cebd2SVille Syrjälä 			  I915_ERROR_MEMORY_REFRESH));
367638bde180SChris Wilson 
367738bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
367838bde180SChris Wilson 	dev_priv->irq_mask =
367938bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
368038bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
368116659bc5SVille Syrjälä 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
368216659bc5SVille Syrjälä 		  I915_MASTER_ERROR_INTERRUPT);
368338bde180SChris Wilson 
368438bde180SChris Wilson 	enable_mask =
368538bde180SChris Wilson 		I915_ASLE_INTERRUPT |
368638bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
368738bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
368816659bc5SVille Syrjälä 		I915_MASTER_ERROR_INTERRUPT |
368938bde180SChris Wilson 		I915_USER_INTERRUPT;
369038bde180SChris Wilson 
369156b857a5STvrtko Ursulin 	if (I915_HAS_HOTPLUG(dev_priv)) {
3692a266c7d5SChris Wilson 		/* Enable in IER... */
3693a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
3694a266c7d5SChris Wilson 		/* and unmask in IMR */
3695a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
3696a266c7d5SChris Wilson 	}
3697a266c7d5SChris Wilson 
3698b16b2a2fSPaulo Zanoni 	GEN3_IRQ_INIT(uncore, GEN2_, dev_priv->irq_mask, enable_mask);
3699a266c7d5SChris Wilson 
3700379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3701379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3702d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3703755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3704755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3705d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3706379ef82dSDaniel Vetter 
3707c30bb1fdSVille Syrjälä 	i915_enable_asle_pipestat(dev_priv);
370820afbda2SDaniel Vetter }
370920afbda2SDaniel Vetter 
3710ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
3711a266c7d5SChris Wilson {
3712b318b824SVille Syrjälä 	struct drm_i915_private *dev_priv = arg;
3713af722d28SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
3714a266c7d5SChris Wilson 
37152dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
37162dd2a883SImre Deak 		return IRQ_NONE;
37172dd2a883SImre Deak 
37181f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
37199102650fSDaniele Ceraolo Spurio 	disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
37201f814dacSImre Deak 
372138bde180SChris Wilson 	do {
3722eb64343cSVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
372378c357ddSVille Syrjälä 		u32 eir = 0, eir_stuck = 0;
3724af722d28SVille Syrjälä 		u32 hotplug_status = 0;
3725af722d28SVille Syrjälä 		u32 iir;
3726a266c7d5SChris Wilson 
37279d9523d8SPaulo Zanoni 		iir = I915_READ(GEN2_IIR);
3728af722d28SVille Syrjälä 		if (iir == 0)
3729af722d28SVille Syrjälä 			break;
3730af722d28SVille Syrjälä 
3731af722d28SVille Syrjälä 		ret = IRQ_HANDLED;
3732af722d28SVille Syrjälä 
3733af722d28SVille Syrjälä 		if (I915_HAS_HOTPLUG(dev_priv) &&
3734af722d28SVille Syrjälä 		    iir & I915_DISPLAY_PORT_INTERRUPT)
3735af722d28SVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
3736a266c7d5SChris Wilson 
3737eb64343cSVille Syrjälä 		/* Call regardless, as some status bits might not be
3738eb64343cSVille Syrjälä 		 * signalled in iir */
3739eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
3740a266c7d5SChris Wilson 
374178c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
374278c357ddSVille Syrjälä 			i9xx_error_irq_ack(dev_priv, &eir, &eir_stuck);
374378c357ddSVille Syrjälä 
37449d9523d8SPaulo Zanoni 		I915_WRITE(GEN2_IIR, iir);
3745a266c7d5SChris Wilson 
3746a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
374754400257SChris Wilson 			intel_engine_signal_breadcrumbs(dev_priv->engine[RCS0]);
3748a266c7d5SChris Wilson 
374978c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
375078c357ddSVille Syrjälä 			i9xx_error_irq_handler(dev_priv, eir, eir_stuck);
3751a266c7d5SChris Wilson 
3752af722d28SVille Syrjälä 		if (hotplug_status)
3753af722d28SVille Syrjälä 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
3754af722d28SVille Syrjälä 
3755af722d28SVille Syrjälä 		i915_pipestat_irq_handler(dev_priv, iir, pipe_stats);
3756af722d28SVille Syrjälä 	} while (0);
3757a266c7d5SChris Wilson 
37589102650fSDaniele Ceraolo Spurio 	enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
37591f814dacSImre Deak 
3760a266c7d5SChris Wilson 	return ret;
3761a266c7d5SChris Wilson }
3762a266c7d5SChris Wilson 
3763b318b824SVille Syrjälä static void i965_irq_reset(struct drm_i915_private *dev_priv)
3764a266c7d5SChris Wilson {
3765b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
3766a266c7d5SChris Wilson 
37670706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
3768a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3769a266c7d5SChris Wilson 
377044d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
377144d9241eSVille Syrjälä 
3772b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN2_);
3773a266c7d5SChris Wilson }
3774a266c7d5SChris Wilson 
3775b318b824SVille Syrjälä static void i965_irq_postinstall(struct drm_i915_private *dev_priv)
3776a266c7d5SChris Wilson {
3777b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
3778bbba0a97SChris Wilson 	u32 enable_mask;
3779a266c7d5SChris Wilson 	u32 error_mask;
3780a266c7d5SChris Wilson 
3781045cebd2SVille Syrjälä 	/*
3782045cebd2SVille Syrjälä 	 * Enable some error detection, note the instruction error mask
3783045cebd2SVille Syrjälä 	 * bit is reserved, so we leave it masked.
3784045cebd2SVille Syrjälä 	 */
3785045cebd2SVille Syrjälä 	if (IS_G4X(dev_priv)) {
3786045cebd2SVille Syrjälä 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
3787045cebd2SVille Syrjälä 			       GM45_ERROR_MEM_PRIV |
3788045cebd2SVille Syrjälä 			       GM45_ERROR_CP_PRIV |
3789045cebd2SVille Syrjälä 			       I915_ERROR_MEMORY_REFRESH);
3790045cebd2SVille Syrjälä 	} else {
3791045cebd2SVille Syrjälä 		error_mask = ~(I915_ERROR_PAGE_TABLE |
3792045cebd2SVille Syrjälä 			       I915_ERROR_MEMORY_REFRESH);
3793045cebd2SVille Syrjälä 	}
3794045cebd2SVille Syrjälä 	I915_WRITE(EMR, error_mask);
3795045cebd2SVille Syrjälä 
3796a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
3797c30bb1fdSVille Syrjälä 	dev_priv->irq_mask =
3798c30bb1fdSVille Syrjälä 		~(I915_ASLE_INTERRUPT |
3799adca4730SChris Wilson 		  I915_DISPLAY_PORT_INTERRUPT |
3800bbba0a97SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3801bbba0a97SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
380278c357ddSVille Syrjälä 		  I915_MASTER_ERROR_INTERRUPT);
3803bbba0a97SChris Wilson 
3804c30bb1fdSVille Syrjälä 	enable_mask =
3805c30bb1fdSVille Syrjälä 		I915_ASLE_INTERRUPT |
3806c30bb1fdSVille Syrjälä 		I915_DISPLAY_PORT_INTERRUPT |
3807c30bb1fdSVille Syrjälä 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3808c30bb1fdSVille Syrjälä 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
380978c357ddSVille Syrjälä 		I915_MASTER_ERROR_INTERRUPT |
3810c30bb1fdSVille Syrjälä 		I915_USER_INTERRUPT;
3811bbba0a97SChris Wilson 
381291d14251STvrtko Ursulin 	if (IS_G4X(dev_priv))
3813bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
3814a266c7d5SChris Wilson 
3815b16b2a2fSPaulo Zanoni 	GEN3_IRQ_INIT(uncore, GEN2_, dev_priv->irq_mask, enable_mask);
3816c30bb1fdSVille Syrjälä 
3817b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3818b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3819d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3820755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3821755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3822755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3823d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3824a266c7d5SChris Wilson 
382591d14251STvrtko Ursulin 	i915_enable_asle_pipestat(dev_priv);
382620afbda2SDaniel Vetter }
382720afbda2SDaniel Vetter 
382891d14251STvrtko Ursulin static void i915_hpd_irq_setup(struct drm_i915_private *dev_priv)
382920afbda2SDaniel Vetter {
383020afbda2SDaniel Vetter 	u32 hotplug_en;
383120afbda2SDaniel Vetter 
383267520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
3833b5ea2d56SDaniel Vetter 
3834adca4730SChris Wilson 	/* Note HDMI and DP share hotplug bits */
3835e5868a31SEgbert Eich 	/* enable bits are the same for all generations */
383691d14251STvrtko Ursulin 	hotplug_en = intel_hpd_enabled_irqs(dev_priv, hpd_mask_i915);
3837a266c7d5SChris Wilson 	/* Programming the CRT detection parameters tends
3838a266c7d5SChris Wilson 	   to generate a spurious hotplug event about three
3839a266c7d5SChris Wilson 	   seconds later.  So just do it once.
3840a266c7d5SChris Wilson 	*/
384191d14251STvrtko Ursulin 	if (IS_G4X(dev_priv))
3842a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
3843a266c7d5SChris Wilson 	hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
3844a266c7d5SChris Wilson 
3845a266c7d5SChris Wilson 	/* Ignore TV since it's buggy */
38460706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv,
3847f9e3dc78SJani Nikula 					     HOTPLUG_INT_EN_MASK |
3848f9e3dc78SJani Nikula 					     CRT_HOTPLUG_VOLTAGE_COMPARE_MASK |
3849f9e3dc78SJani Nikula 					     CRT_HOTPLUG_ACTIVATION_PERIOD_64,
38500706f17cSEgbert Eich 					     hotplug_en);
3851a266c7d5SChris Wilson }
3852a266c7d5SChris Wilson 
3853ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
3854a266c7d5SChris Wilson {
3855b318b824SVille Syrjälä 	struct drm_i915_private *dev_priv = arg;
3856af722d28SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
3857a266c7d5SChris Wilson 
38582dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
38592dd2a883SImre Deak 		return IRQ_NONE;
38602dd2a883SImre Deak 
38611f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
38629102650fSDaniele Ceraolo Spurio 	disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
38631f814dacSImre Deak 
3864af722d28SVille Syrjälä 	do {
3865eb64343cSVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
386678c357ddSVille Syrjälä 		u32 eir = 0, eir_stuck = 0;
3867af722d28SVille Syrjälä 		u32 hotplug_status = 0;
3868af722d28SVille Syrjälä 		u32 iir;
38692c8ba29fSChris Wilson 
38709d9523d8SPaulo Zanoni 		iir = I915_READ(GEN2_IIR);
3871af722d28SVille Syrjälä 		if (iir == 0)
3872af722d28SVille Syrjälä 			break;
3873af722d28SVille Syrjälä 
3874af722d28SVille Syrjälä 		ret = IRQ_HANDLED;
3875af722d28SVille Syrjälä 
3876af722d28SVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
3877af722d28SVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
3878a266c7d5SChris Wilson 
3879eb64343cSVille Syrjälä 		/* Call regardless, as some status bits might not be
3880eb64343cSVille Syrjälä 		 * signalled in iir */
3881eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
3882a266c7d5SChris Wilson 
388378c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
388478c357ddSVille Syrjälä 			i9xx_error_irq_ack(dev_priv, &eir, &eir_stuck);
388578c357ddSVille Syrjälä 
38869d9523d8SPaulo Zanoni 		I915_WRITE(GEN2_IIR, iir);
3887a266c7d5SChris Wilson 
3888a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
388954400257SChris Wilson 			intel_engine_signal_breadcrumbs(dev_priv->engine[RCS0]);
3890af722d28SVille Syrjälä 
3891a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
389254400257SChris Wilson 			intel_engine_signal_breadcrumbs(dev_priv->engine[VCS0]);
3893a266c7d5SChris Wilson 
389478c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
389578c357ddSVille Syrjälä 			i9xx_error_irq_handler(dev_priv, eir, eir_stuck);
3896515ac2bbSDaniel Vetter 
3897af722d28SVille Syrjälä 		if (hotplug_status)
3898af722d28SVille Syrjälä 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
3899af722d28SVille Syrjälä 
3900af722d28SVille Syrjälä 		i965_pipestat_irq_handler(dev_priv, iir, pipe_stats);
3901af722d28SVille Syrjälä 	} while (0);
3902a266c7d5SChris Wilson 
39039102650fSDaniele Ceraolo Spurio 	enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
39041f814dacSImre Deak 
3905a266c7d5SChris Wilson 	return ret;
3906a266c7d5SChris Wilson }
3907a266c7d5SChris Wilson 
3908fca52a55SDaniel Vetter /**
3909fca52a55SDaniel Vetter  * intel_irq_init - initializes irq support
3910fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
3911fca52a55SDaniel Vetter  *
3912fca52a55SDaniel Vetter  * This function initializes all the irq support including work items, timers
3913fca52a55SDaniel Vetter  * and all the vtables. It does not setup the interrupt itself though.
3914fca52a55SDaniel Vetter  */
3915b963291cSDaniel Vetter void intel_irq_init(struct drm_i915_private *dev_priv)
3916f71d4af4SJesse Barnes {
391791c8a326SChris Wilson 	struct drm_device *dev = &dev_priv->drm;
3918cefcff8fSJoonas Lahtinen 	int i;
39198b2e326dSChris Wilson 
392077913b39SJani Nikula 	intel_hpd_init_work(dev_priv);
392177913b39SJani Nikula 
392274bb98baSLucas De Marchi 	INIT_WORK(&dev_priv->l3_parity.error_work, ivb_parity_work);
3923cefcff8fSJoonas Lahtinen 	for (i = 0; i < MAX_L3_SLICES; ++i)
3924cefcff8fSJoonas Lahtinen 		dev_priv->l3_parity.remap_info[i] = NULL;
39258b2e326dSChris Wilson 
3926633023a4SDaniele Ceraolo Spurio 	/* pre-gen11 the guc irqs bits are in the upper 16 bits of the pm reg */
3927702668e6SDaniele Ceraolo Spurio 	if (HAS_GT_UC(dev_priv) && INTEL_GEN(dev_priv) < 11)
39282239e6dfSDaniele Ceraolo Spurio 		dev_priv->gt.pm_guc_events = GUC_INTR_GUC2HOST << 16;
392926705e20SSagar Arun Kamble 
393021da2700SVille Syrjälä 	dev->vblank_disable_immediate = true;
393121da2700SVille Syrjälä 
3932262fd485SChris Wilson 	/* Most platforms treat the display irq block as an always-on
3933262fd485SChris Wilson 	 * power domain. vlv/chv can disable it at runtime and need
3934262fd485SChris Wilson 	 * special care to avoid writing any of the display block registers
3935262fd485SChris Wilson 	 * outside of the power domain. We defer setting up the display irqs
3936262fd485SChris Wilson 	 * in this case to the runtime pm.
3937262fd485SChris Wilson 	 */
3938262fd485SChris Wilson 	dev_priv->display_irqs_enabled = true;
3939262fd485SChris Wilson 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
3940262fd485SChris Wilson 		dev_priv->display_irqs_enabled = false;
3941262fd485SChris Wilson 
3942317eaa95SLyude 	dev_priv->hotplug.hpd_storm_threshold = HPD_STORM_DEFAULT_THRESHOLD;
39439a64c650SLyude Paul 	/* If we have MST support, we want to avoid doing short HPD IRQ storm
39449a64c650SLyude Paul 	 * detection, as short HPD storms will occur as a natural part of
39459a64c650SLyude Paul 	 * sideband messaging with MST.
39469a64c650SLyude Paul 	 * On older platforms however, IRQ storms can occur with both long and
39479a64c650SLyude Paul 	 * short pulses, as seen on some G4x systems.
39489a64c650SLyude Paul 	 */
39499a64c650SLyude Paul 	dev_priv->hotplug.hpd_short_storm_enabled = !HAS_DP_MST(dev_priv);
3950317eaa95SLyude 
3951b318b824SVille Syrjälä 	if (HAS_GMCH(dev_priv)) {
3952b318b824SVille Syrjälä 		if (I915_HAS_HOTPLUG(dev_priv))
395343f328d7SVille Syrjälä 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3954b318b824SVille Syrjälä 	} else {
3955943682e3SMatt Roper 		if (HAS_PCH_JSP(dev_priv))
3956943682e3SMatt Roper 			dev_priv->display.hpd_irq_setup = jsp_hpd_irq_setup;
3957943682e3SMatt Roper 		else if (HAS_PCH_MCC(dev_priv))
39588ef7e340SMatt Roper 			dev_priv->display.hpd_irq_setup = mcc_hpd_irq_setup;
39598ef7e340SMatt Roper 		else if (INTEL_GEN(dev_priv) >= 11)
3960121e758eSDhinakaran Pandiyan 			dev_priv->display.hpd_irq_setup = gen11_hpd_irq_setup;
3961b318b824SVille Syrjälä 		else if (IS_GEN9_LP(dev_priv))
3962e0a20ad7SShashank Sharma 			dev_priv->display.hpd_irq_setup = bxt_hpd_irq_setup;
3963c6c30b91SRodrigo Vivi 		else if (INTEL_PCH_TYPE(dev_priv) >= PCH_SPT)
39646dbf30ceSVille Syrjälä 			dev_priv->display.hpd_irq_setup = spt_hpd_irq_setup;
39656dbf30ceSVille Syrjälä 		else
39663a3b3c7dSVille Syrjälä 			dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
3967f71d4af4SJesse Barnes 	}
3968f71d4af4SJesse Barnes }
396920afbda2SDaniel Vetter 
3970fca52a55SDaniel Vetter /**
3971cefcff8fSJoonas Lahtinen  * intel_irq_fini - deinitializes IRQ support
3972cefcff8fSJoonas Lahtinen  * @i915: i915 device instance
3973cefcff8fSJoonas Lahtinen  *
3974cefcff8fSJoonas Lahtinen  * This function deinitializes all the IRQ support.
3975cefcff8fSJoonas Lahtinen  */
3976cefcff8fSJoonas Lahtinen void intel_irq_fini(struct drm_i915_private *i915)
3977cefcff8fSJoonas Lahtinen {
3978cefcff8fSJoonas Lahtinen 	int i;
3979cefcff8fSJoonas Lahtinen 
3980cefcff8fSJoonas Lahtinen 	for (i = 0; i < MAX_L3_SLICES; ++i)
3981cefcff8fSJoonas Lahtinen 		kfree(i915->l3_parity.remap_info[i]);
3982cefcff8fSJoonas Lahtinen }
3983cefcff8fSJoonas Lahtinen 
3984b318b824SVille Syrjälä static irq_handler_t intel_irq_handler(struct drm_i915_private *dev_priv)
3985b318b824SVille Syrjälä {
3986b318b824SVille Syrjälä 	if (HAS_GMCH(dev_priv)) {
3987b318b824SVille Syrjälä 		if (IS_CHERRYVIEW(dev_priv))
3988b318b824SVille Syrjälä 			return cherryview_irq_handler;
3989b318b824SVille Syrjälä 		else if (IS_VALLEYVIEW(dev_priv))
3990b318b824SVille Syrjälä 			return valleyview_irq_handler;
3991b318b824SVille Syrjälä 		else if (IS_GEN(dev_priv, 4))
3992b318b824SVille Syrjälä 			return i965_irq_handler;
3993b318b824SVille Syrjälä 		else if (IS_GEN(dev_priv, 3))
3994b318b824SVille Syrjälä 			return i915_irq_handler;
3995b318b824SVille Syrjälä 		else
3996b318b824SVille Syrjälä 			return i8xx_irq_handler;
3997b318b824SVille Syrjälä 	} else {
3998b318b824SVille Syrjälä 		if (INTEL_GEN(dev_priv) >= 11)
3999b318b824SVille Syrjälä 			return gen11_irq_handler;
4000b318b824SVille Syrjälä 		else if (INTEL_GEN(dev_priv) >= 8)
4001b318b824SVille Syrjälä 			return gen8_irq_handler;
4002b318b824SVille Syrjälä 		else
40039eae5e27SLucas De Marchi 			return ilk_irq_handler;
4004b318b824SVille Syrjälä 	}
4005b318b824SVille Syrjälä }
4006b318b824SVille Syrjälä 
4007b318b824SVille Syrjälä static void intel_irq_reset(struct drm_i915_private *dev_priv)
4008b318b824SVille Syrjälä {
4009b318b824SVille Syrjälä 	if (HAS_GMCH(dev_priv)) {
4010b318b824SVille Syrjälä 		if (IS_CHERRYVIEW(dev_priv))
4011b318b824SVille Syrjälä 			cherryview_irq_reset(dev_priv);
4012b318b824SVille Syrjälä 		else if (IS_VALLEYVIEW(dev_priv))
4013b318b824SVille Syrjälä 			valleyview_irq_reset(dev_priv);
4014b318b824SVille Syrjälä 		else if (IS_GEN(dev_priv, 4))
4015b318b824SVille Syrjälä 			i965_irq_reset(dev_priv);
4016b318b824SVille Syrjälä 		else if (IS_GEN(dev_priv, 3))
4017b318b824SVille Syrjälä 			i915_irq_reset(dev_priv);
4018b318b824SVille Syrjälä 		else
4019b318b824SVille Syrjälä 			i8xx_irq_reset(dev_priv);
4020b318b824SVille Syrjälä 	} else {
4021b318b824SVille Syrjälä 		if (INTEL_GEN(dev_priv) >= 11)
4022b318b824SVille Syrjälä 			gen11_irq_reset(dev_priv);
4023b318b824SVille Syrjälä 		else if (INTEL_GEN(dev_priv) >= 8)
4024b318b824SVille Syrjälä 			gen8_irq_reset(dev_priv);
4025b318b824SVille Syrjälä 		else
40269eae5e27SLucas De Marchi 			ilk_irq_reset(dev_priv);
4027b318b824SVille Syrjälä 	}
4028b318b824SVille Syrjälä }
4029b318b824SVille Syrjälä 
4030b318b824SVille Syrjälä static void intel_irq_postinstall(struct drm_i915_private *dev_priv)
4031b318b824SVille Syrjälä {
4032b318b824SVille Syrjälä 	if (HAS_GMCH(dev_priv)) {
4033b318b824SVille Syrjälä 		if (IS_CHERRYVIEW(dev_priv))
4034b318b824SVille Syrjälä 			cherryview_irq_postinstall(dev_priv);
4035b318b824SVille Syrjälä 		else if (IS_VALLEYVIEW(dev_priv))
4036b318b824SVille Syrjälä 			valleyview_irq_postinstall(dev_priv);
4037b318b824SVille Syrjälä 		else if (IS_GEN(dev_priv, 4))
4038b318b824SVille Syrjälä 			i965_irq_postinstall(dev_priv);
4039b318b824SVille Syrjälä 		else if (IS_GEN(dev_priv, 3))
4040b318b824SVille Syrjälä 			i915_irq_postinstall(dev_priv);
4041b318b824SVille Syrjälä 		else
4042b318b824SVille Syrjälä 			i8xx_irq_postinstall(dev_priv);
4043b318b824SVille Syrjälä 	} else {
4044b318b824SVille Syrjälä 		if (INTEL_GEN(dev_priv) >= 11)
4045b318b824SVille Syrjälä 			gen11_irq_postinstall(dev_priv);
4046b318b824SVille Syrjälä 		else if (INTEL_GEN(dev_priv) >= 8)
4047b318b824SVille Syrjälä 			gen8_irq_postinstall(dev_priv);
4048b318b824SVille Syrjälä 		else
40499eae5e27SLucas De Marchi 			ilk_irq_postinstall(dev_priv);
4050b318b824SVille Syrjälä 	}
4051b318b824SVille Syrjälä }
4052b318b824SVille Syrjälä 
4053cefcff8fSJoonas Lahtinen /**
4054fca52a55SDaniel Vetter  * intel_irq_install - enables the hardware interrupt
4055fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4056fca52a55SDaniel Vetter  *
4057fca52a55SDaniel Vetter  * This function enables the hardware interrupt handling, but leaves the hotplug
4058fca52a55SDaniel Vetter  * handling still disabled. It is called after intel_irq_init().
4059fca52a55SDaniel Vetter  *
4060fca52a55SDaniel Vetter  * In the driver load and resume code we need working interrupts in a few places
4061fca52a55SDaniel Vetter  * but don't want to deal with the hassle of concurrent probe and hotplug
4062fca52a55SDaniel Vetter  * workers. Hence the split into this two-stage approach.
4063fca52a55SDaniel Vetter  */
40642aeb7d3aSDaniel Vetter int intel_irq_install(struct drm_i915_private *dev_priv)
40652aeb7d3aSDaniel Vetter {
4066b318b824SVille Syrjälä 	int irq = dev_priv->drm.pdev->irq;
4067b318b824SVille Syrjälä 	int ret;
4068b318b824SVille Syrjälä 
40692aeb7d3aSDaniel Vetter 	/*
40702aeb7d3aSDaniel Vetter 	 * We enable some interrupt sources in our postinstall hooks, so mark
40712aeb7d3aSDaniel Vetter 	 * interrupts as enabled _before_ actually enabling them to avoid
40722aeb7d3aSDaniel Vetter 	 * special cases in our ordering checks.
40732aeb7d3aSDaniel Vetter 	 */
4074ad1443f0SSagar Arun Kamble 	dev_priv->runtime_pm.irqs_enabled = true;
40752aeb7d3aSDaniel Vetter 
4076b318b824SVille Syrjälä 	dev_priv->drm.irq_enabled = true;
4077b318b824SVille Syrjälä 
4078b318b824SVille Syrjälä 	intel_irq_reset(dev_priv);
4079b318b824SVille Syrjälä 
4080b318b824SVille Syrjälä 	ret = request_irq(irq, intel_irq_handler(dev_priv),
4081b318b824SVille Syrjälä 			  IRQF_SHARED, DRIVER_NAME, dev_priv);
4082b318b824SVille Syrjälä 	if (ret < 0) {
4083b318b824SVille Syrjälä 		dev_priv->drm.irq_enabled = false;
4084b318b824SVille Syrjälä 		return ret;
4085b318b824SVille Syrjälä 	}
4086b318b824SVille Syrjälä 
4087b318b824SVille Syrjälä 	intel_irq_postinstall(dev_priv);
4088b318b824SVille Syrjälä 
4089b318b824SVille Syrjälä 	return ret;
40902aeb7d3aSDaniel Vetter }
40912aeb7d3aSDaniel Vetter 
4092fca52a55SDaniel Vetter /**
4093fca52a55SDaniel Vetter  * intel_irq_uninstall - finilizes all irq handling
4094fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4095fca52a55SDaniel Vetter  *
4096fca52a55SDaniel Vetter  * This stops interrupt and hotplug handling and unregisters and frees all
4097fca52a55SDaniel Vetter  * resources acquired in the init functions.
4098fca52a55SDaniel Vetter  */
40992aeb7d3aSDaniel Vetter void intel_irq_uninstall(struct drm_i915_private *dev_priv)
41002aeb7d3aSDaniel Vetter {
4101b318b824SVille Syrjälä 	int irq = dev_priv->drm.pdev->irq;
4102b318b824SVille Syrjälä 
4103b318b824SVille Syrjälä 	/*
4104789fa874SJanusz Krzysztofik 	 * FIXME we can get called twice during driver probe
4105789fa874SJanusz Krzysztofik 	 * error handling as well as during driver remove due to
4106789fa874SJanusz Krzysztofik 	 * intel_modeset_driver_remove() calling us out of sequence.
4107789fa874SJanusz Krzysztofik 	 * Would be nice if it didn't do that...
4108b318b824SVille Syrjälä 	 */
4109b318b824SVille Syrjälä 	if (!dev_priv->drm.irq_enabled)
4110b318b824SVille Syrjälä 		return;
4111b318b824SVille Syrjälä 
4112b318b824SVille Syrjälä 	dev_priv->drm.irq_enabled = false;
4113b318b824SVille Syrjälä 
4114b318b824SVille Syrjälä 	intel_irq_reset(dev_priv);
4115b318b824SVille Syrjälä 
4116b318b824SVille Syrjälä 	free_irq(irq, dev_priv);
4117b318b824SVille Syrjälä 
41182aeb7d3aSDaniel Vetter 	intel_hpd_cancel_work(dev_priv);
4119ad1443f0SSagar Arun Kamble 	dev_priv->runtime_pm.irqs_enabled = false;
41202aeb7d3aSDaniel Vetter }
41212aeb7d3aSDaniel Vetter 
4122fca52a55SDaniel Vetter /**
4123fca52a55SDaniel Vetter  * intel_runtime_pm_disable_interrupts - runtime interrupt disabling
4124fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4125fca52a55SDaniel Vetter  *
4126fca52a55SDaniel Vetter  * This function is used to disable interrupts at runtime, both in the runtime
4127fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4128fca52a55SDaniel Vetter  */
4129b963291cSDaniel Vetter void intel_runtime_pm_disable_interrupts(struct drm_i915_private *dev_priv)
4130c67a470bSPaulo Zanoni {
4131b318b824SVille Syrjälä 	intel_irq_reset(dev_priv);
4132ad1443f0SSagar Arun Kamble 	dev_priv->runtime_pm.irqs_enabled = false;
4133315ca4c4SVille Syrjälä 	intel_synchronize_irq(dev_priv);
4134c67a470bSPaulo Zanoni }
4135c67a470bSPaulo Zanoni 
4136fca52a55SDaniel Vetter /**
4137fca52a55SDaniel Vetter  * intel_runtime_pm_enable_interrupts - runtime interrupt enabling
4138fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4139fca52a55SDaniel Vetter  *
4140fca52a55SDaniel Vetter  * This function is used to enable interrupts at runtime, both in the runtime
4141fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4142fca52a55SDaniel Vetter  */
4143b963291cSDaniel Vetter void intel_runtime_pm_enable_interrupts(struct drm_i915_private *dev_priv)
4144c67a470bSPaulo Zanoni {
4145ad1443f0SSagar Arun Kamble 	dev_priv->runtime_pm.irqs_enabled = true;
4146b318b824SVille Syrjälä 	intel_irq_reset(dev_priv);
4147b318b824SVille Syrjälä 	intel_irq_postinstall(dev_priv);
4148c67a470bSPaulo Zanoni }
4149d64575eeSJani Nikula 
4150d64575eeSJani Nikula bool intel_irqs_enabled(struct drm_i915_private *dev_priv)
4151d64575eeSJani Nikula {
4152d64575eeSJani Nikula 	/*
4153d64575eeSJani Nikula 	 * We only use drm_irq_uninstall() at unload and VT switch, so
4154d64575eeSJani Nikula 	 * this is the only thing we need to check.
4155d64575eeSJani Nikula 	 */
4156d64575eeSJani Nikula 	return dev_priv->runtime_pm.irqs_enabled;
4157d64575eeSJani Nikula }
4158d64575eeSJani Nikula 
4159d64575eeSJani Nikula void intel_synchronize_irq(struct drm_i915_private *i915)
4160d64575eeSJani Nikula {
4161d64575eeSJani Nikula 	synchronize_irq(i915->drm.pdev->irq);
4162d64575eeSJani Nikula }
4163