xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 4bb18054adc4939a3c1f895d8d0a1556a5f8b26a)
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>
3755367a27SJani Nikula 
381d455f8dSJani Nikula #include "display/intel_display_types.h"
39df0566a6SJani Nikula #include "display/intel_fifo_underrun.h"
40df0566a6SJani Nikula #include "display/intel_hotplug.h"
41df0566a6SJani Nikula #include "display/intel_lpe_audio.h"
42df0566a6SJani Nikula #include "display/intel_psr.h"
43df0566a6SJani Nikula 
44b3786b29SChris Wilson #include "gt/intel_breadcrumbs.h"
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 
639c6508b9SThomas Gleixner /*
649c6508b9SThomas Gleixner  * Interrupt statistic for PMU. Increments the counter only if the
659c6508b9SThomas Gleixner  * interrupt originated from the the GPU so interrupts from a device which
669c6508b9SThomas Gleixner  * shares the interrupt line are not accounted.
679c6508b9SThomas Gleixner  */
689c6508b9SThomas Gleixner static inline void pmu_irq_stats(struct drm_i915_private *i915,
699c6508b9SThomas Gleixner 				 irqreturn_t res)
709c6508b9SThomas Gleixner {
719c6508b9SThomas Gleixner 	if (unlikely(res != IRQ_HANDLED))
729c6508b9SThomas Gleixner 		return;
739c6508b9SThomas Gleixner 
749c6508b9SThomas Gleixner 	/*
759c6508b9SThomas Gleixner 	 * A clever compiler translates that into INC. A not so clever one
769c6508b9SThomas Gleixner 	 * should at least prevent store tearing.
779c6508b9SThomas Gleixner 	 */
789c6508b9SThomas Gleixner 	WRITE_ONCE(i915->pmu.irq_count, i915->pmu.irq_count + 1);
799c6508b9SThomas Gleixner }
809c6508b9SThomas Gleixner 
8148ef15d3SJosé Roberto de Souza typedef bool (*long_pulse_detect_func)(enum hpd_pin pin, u32 val);
822ea63927SVille Syrjälä typedef u32 (*hotplug_enables_func)(struct drm_i915_private *i915,
832ea63927SVille Syrjälä 				    enum hpd_pin pin);
8448ef15d3SJosé Roberto de Souza 
85e4ce95aaSVille Syrjälä static const u32 hpd_ilk[HPD_NUM_PINS] = {
86e4ce95aaSVille Syrjälä 	[HPD_PORT_A] = DE_DP_A_HOTPLUG,
87e4ce95aaSVille Syrjälä };
88e4ce95aaSVille Syrjälä 
8923bb4cb5SVille Syrjälä static const u32 hpd_ivb[HPD_NUM_PINS] = {
9023bb4cb5SVille Syrjälä 	[HPD_PORT_A] = DE_DP_A_HOTPLUG_IVB,
9123bb4cb5SVille Syrjälä };
9223bb4cb5SVille Syrjälä 
933a3b3c7dSVille Syrjälä static const u32 hpd_bdw[HPD_NUM_PINS] = {
94e5abaab3SVille Syrjälä 	[HPD_PORT_A] = GEN8_DE_PORT_HOTPLUG(HPD_PORT_A),
953a3b3c7dSVille Syrjälä };
963a3b3c7dSVille Syrjälä 
977c7e10dbSVille Syrjälä static const u32 hpd_ibx[HPD_NUM_PINS] = {
98e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG,
99e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG,
100e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG,
101e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG,
1027203d49cSVille Syrjälä 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG,
103e5868a31SEgbert Eich };
104e5868a31SEgbert Eich 
1057c7e10dbSVille Syrjälä static const u32 hpd_cpt[HPD_NUM_PINS] = {
106e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG_CPT,
10773c352a2SDaniel Vetter 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG_CPT,
108e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
109e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
1107203d49cSVille Syrjälä 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT,
111e5868a31SEgbert Eich };
112e5868a31SEgbert Eich 
11326951cafSXiong Zhang static const u32 hpd_spt[HPD_NUM_PINS] = {
11474c0b395SVille Syrjälä 	[HPD_PORT_A] = SDE_PORTA_HOTPLUG_SPT,
11526951cafSXiong Zhang 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
11626951cafSXiong Zhang 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
11726951cafSXiong Zhang 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT,
1187203d49cSVille Syrjälä 	[HPD_PORT_E] = SDE_PORTE_HOTPLUG_SPT,
11926951cafSXiong Zhang };
12026951cafSXiong Zhang 
1217c7e10dbSVille Syrjälä static const u32 hpd_mask_i915[HPD_NUM_PINS] = {
122e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_EN,
123e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_EN,
124e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_EN,
125e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_EN,
126e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_EN,
1277203d49cSVille Syrjälä 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_EN,
128e5868a31SEgbert Eich };
129e5868a31SEgbert Eich 
1307c7e10dbSVille Syrjälä static const u32 hpd_status_g4x[HPD_NUM_PINS] = {
131e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
132e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_G4X,
133e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_G4X,
134e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
135e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
1367203d49cSVille Syrjälä 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS,
137e5868a31SEgbert Eich };
138e5868a31SEgbert Eich 
1394bca26d0SVille Syrjälä static const u32 hpd_status_i915[HPD_NUM_PINS] = {
140e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
141e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_I915,
142e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_I915,
143e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
144e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
1457203d49cSVille Syrjälä 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS,
146e5868a31SEgbert Eich };
147e5868a31SEgbert Eich 
148e0a20ad7SShashank Sharma static const u32 hpd_bxt[HPD_NUM_PINS] = {
149e5abaab3SVille Syrjälä 	[HPD_PORT_A] = GEN8_DE_PORT_HOTPLUG(HPD_PORT_A),
150e5abaab3SVille Syrjälä 	[HPD_PORT_B] = GEN8_DE_PORT_HOTPLUG(HPD_PORT_B),
151e5abaab3SVille Syrjälä 	[HPD_PORT_C] = GEN8_DE_PORT_HOTPLUG(HPD_PORT_C),
152e0a20ad7SShashank Sharma };
153e0a20ad7SShashank Sharma 
154b796b971SDhinakaran Pandiyan static const u32 hpd_gen11[HPD_NUM_PINS] = {
1555b76e860SVille Syrjälä 	[HPD_PORT_TC1] = GEN11_TC_HOTPLUG(HPD_PORT_TC1) | GEN11_TBT_HOTPLUG(HPD_PORT_TC1),
1565b76e860SVille Syrjälä 	[HPD_PORT_TC2] = GEN11_TC_HOTPLUG(HPD_PORT_TC2) | GEN11_TBT_HOTPLUG(HPD_PORT_TC2),
1575b76e860SVille Syrjälä 	[HPD_PORT_TC3] = GEN11_TC_HOTPLUG(HPD_PORT_TC3) | GEN11_TBT_HOTPLUG(HPD_PORT_TC3),
1585b76e860SVille Syrjälä 	[HPD_PORT_TC4] = GEN11_TC_HOTPLUG(HPD_PORT_TC4) | GEN11_TBT_HOTPLUG(HPD_PORT_TC4),
1595b76e860SVille Syrjälä 	[HPD_PORT_TC5] = GEN11_TC_HOTPLUG(HPD_PORT_TC5) | GEN11_TBT_HOTPLUG(HPD_PORT_TC5),
1605b76e860SVille Syrjälä 	[HPD_PORT_TC6] = GEN11_TC_HOTPLUG(HPD_PORT_TC6) | GEN11_TBT_HOTPLUG(HPD_PORT_TC6),
16148ef15d3SJosé Roberto de Souza };
16248ef15d3SJosé Roberto de Souza 
16331604222SAnusha Srivatsa static const u32 hpd_icp[HPD_NUM_PINS] = {
1645f371a81SVille Syrjälä 	[HPD_PORT_A] = SDE_DDI_HOTPLUG_ICP(HPD_PORT_A),
1655f371a81SVille Syrjälä 	[HPD_PORT_B] = SDE_DDI_HOTPLUG_ICP(HPD_PORT_B),
1665f371a81SVille Syrjälä 	[HPD_PORT_C] = SDE_DDI_HOTPLUG_ICP(HPD_PORT_C),
16797011359SVille Syrjälä 	[HPD_PORT_TC1] = SDE_TC_HOTPLUG_ICP(HPD_PORT_TC1),
16897011359SVille Syrjälä 	[HPD_PORT_TC2] = SDE_TC_HOTPLUG_ICP(HPD_PORT_TC2),
16997011359SVille Syrjälä 	[HPD_PORT_TC3] = SDE_TC_HOTPLUG_ICP(HPD_PORT_TC3),
17097011359SVille Syrjälä 	[HPD_PORT_TC4] = SDE_TC_HOTPLUG_ICP(HPD_PORT_TC4),
17197011359SVille Syrjälä 	[HPD_PORT_TC5] = SDE_TC_HOTPLUG_ICP(HPD_PORT_TC5),
17297011359SVille Syrjälä 	[HPD_PORT_TC6] = SDE_TC_HOTPLUG_ICP(HPD_PORT_TC6),
17352dfdba0SLucas De Marchi };
17452dfdba0SLucas De Marchi 
175229f31e2SLucas De Marchi static const u32 hpd_sde_dg1[HPD_NUM_PINS] = {
1765f371a81SVille Syrjälä 	[HPD_PORT_A] = SDE_DDI_HOTPLUG_ICP(HPD_PORT_A),
1775f371a81SVille Syrjälä 	[HPD_PORT_B] = SDE_DDI_HOTPLUG_ICP(HPD_PORT_B),
1785f371a81SVille Syrjälä 	[HPD_PORT_C] = SDE_DDI_HOTPLUG_ICP(HPD_PORT_C),
1795f371a81SVille Syrjälä 	[HPD_PORT_D] = SDE_DDI_HOTPLUG_ICP(HPD_PORT_D),
180229f31e2SLucas De Marchi };
181229f31e2SLucas De Marchi 
1820398993bSVille Syrjälä static void intel_hpd_init_pins(struct drm_i915_private *dev_priv)
1830398993bSVille Syrjälä {
1840398993bSVille Syrjälä 	struct i915_hotplug *hpd = &dev_priv->hotplug;
1850398993bSVille Syrjälä 
1860398993bSVille Syrjälä 	if (HAS_GMCH(dev_priv)) {
1870398993bSVille Syrjälä 		if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
1880398993bSVille Syrjälä 		    IS_CHERRYVIEW(dev_priv))
1890398993bSVille Syrjälä 			hpd->hpd = hpd_status_g4x;
1900398993bSVille Syrjälä 		else
1910398993bSVille Syrjälä 			hpd->hpd = hpd_status_i915;
1920398993bSVille Syrjälä 		return;
1930398993bSVille Syrjälä 	}
1940398993bSVille Syrjälä 
195da51e4baSVille Syrjälä 	if (INTEL_GEN(dev_priv) >= 11)
1960398993bSVille Syrjälä 		hpd->hpd = hpd_gen11;
1970398993bSVille Syrjälä 	else if (IS_GEN9_LP(dev_priv))
1980398993bSVille Syrjälä 		hpd->hpd = hpd_bxt;
1990398993bSVille Syrjälä 	else if (INTEL_GEN(dev_priv) >= 8)
2000398993bSVille Syrjälä 		hpd->hpd = hpd_bdw;
2010398993bSVille Syrjälä 	else if (INTEL_GEN(dev_priv) >= 7)
2020398993bSVille Syrjälä 		hpd->hpd = hpd_ivb;
2030398993bSVille Syrjälä 	else
2040398993bSVille Syrjälä 		hpd->hpd = hpd_ilk;
2050398993bSVille Syrjälä 
206229f31e2SLucas De Marchi 	if ((INTEL_PCH_TYPE(dev_priv) < PCH_DG1) &&
207229f31e2SLucas De Marchi 	    (!HAS_PCH_SPLIT(dev_priv) || HAS_PCH_NOP(dev_priv)))
2080398993bSVille Syrjälä 		return;
2090398993bSVille Syrjälä 
210229f31e2SLucas De Marchi 	if (HAS_PCH_DG1(dev_priv))
211229f31e2SLucas De Marchi 		hpd->pch_hpd = hpd_sde_dg1;
212229f31e2SLucas De Marchi 	else if (HAS_PCH_TGP(dev_priv) || HAS_PCH_JSP(dev_priv) ||
213da51e4baSVille Syrjälä 		 HAS_PCH_ICP(dev_priv) || HAS_PCH_MCC(dev_priv))
2140398993bSVille Syrjälä 		hpd->pch_hpd = hpd_icp;
2150398993bSVille Syrjälä 	else if (HAS_PCH_CNP(dev_priv) || HAS_PCH_SPT(dev_priv))
2160398993bSVille Syrjälä 		hpd->pch_hpd = hpd_spt;
2170398993bSVille Syrjälä 	else if (HAS_PCH_LPT(dev_priv) || HAS_PCH_CPT(dev_priv))
2180398993bSVille Syrjälä 		hpd->pch_hpd = hpd_cpt;
2190398993bSVille Syrjälä 	else if (HAS_PCH_IBX(dev_priv))
2200398993bSVille Syrjälä 		hpd->pch_hpd = hpd_ibx;
2210398993bSVille Syrjälä 	else
2220398993bSVille Syrjälä 		MISSING_CASE(INTEL_PCH_TYPE(dev_priv));
2230398993bSVille Syrjälä }
2240398993bSVille Syrjälä 
225aca9310aSAnshuman Gupta static void
226aca9310aSAnshuman Gupta intel_handle_vblank(struct drm_i915_private *dev_priv, enum pipe pipe)
227aca9310aSAnshuman Gupta {
228aca9310aSAnshuman Gupta 	struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
229aca9310aSAnshuman Gupta 
230aca9310aSAnshuman Gupta 	drm_crtc_handle_vblank(&crtc->base);
231aca9310aSAnshuman Gupta }
232aca9310aSAnshuman Gupta 
233cf1c97dcSAndi Shyti void gen3_irq_reset(struct intel_uncore *uncore, i915_reg_t imr,
23468eb49b1SPaulo Zanoni 		    i915_reg_t iir, i915_reg_t ier)
23568eb49b1SPaulo Zanoni {
23665f42cdcSPaulo Zanoni 	intel_uncore_write(uncore, imr, 0xffffffff);
23765f42cdcSPaulo Zanoni 	intel_uncore_posting_read(uncore, imr);
23868eb49b1SPaulo Zanoni 
23965f42cdcSPaulo Zanoni 	intel_uncore_write(uncore, ier, 0);
24068eb49b1SPaulo Zanoni 
2415c502442SPaulo Zanoni 	/* IIR can theoretically queue up two events. Be paranoid. */
24265f42cdcSPaulo Zanoni 	intel_uncore_write(uncore, iir, 0xffffffff);
24365f42cdcSPaulo Zanoni 	intel_uncore_posting_read(uncore, iir);
24465f42cdcSPaulo Zanoni 	intel_uncore_write(uncore, iir, 0xffffffff);
24565f42cdcSPaulo Zanoni 	intel_uncore_posting_read(uncore, iir);
24668eb49b1SPaulo Zanoni }
2475c502442SPaulo Zanoni 
248cf1c97dcSAndi Shyti void gen2_irq_reset(struct intel_uncore *uncore)
24968eb49b1SPaulo Zanoni {
25065f42cdcSPaulo Zanoni 	intel_uncore_write16(uncore, GEN2_IMR, 0xffff);
25165f42cdcSPaulo Zanoni 	intel_uncore_posting_read16(uncore, GEN2_IMR);
252a9d356a6SPaulo Zanoni 
25365f42cdcSPaulo Zanoni 	intel_uncore_write16(uncore, GEN2_IER, 0);
25468eb49b1SPaulo Zanoni 
25568eb49b1SPaulo Zanoni 	/* IIR can theoretically queue up two events. Be paranoid. */
25665f42cdcSPaulo Zanoni 	intel_uncore_write16(uncore, GEN2_IIR, 0xffff);
25765f42cdcSPaulo Zanoni 	intel_uncore_posting_read16(uncore, GEN2_IIR);
25865f42cdcSPaulo Zanoni 	intel_uncore_write16(uncore, GEN2_IIR, 0xffff);
25965f42cdcSPaulo Zanoni 	intel_uncore_posting_read16(uncore, GEN2_IIR);
26068eb49b1SPaulo Zanoni }
26168eb49b1SPaulo Zanoni 
262337ba017SPaulo Zanoni /*
263337ba017SPaulo Zanoni  * We should clear IMR at preinstall/uninstall, and just check at postinstall.
264337ba017SPaulo Zanoni  */
26565f42cdcSPaulo Zanoni static void gen3_assert_iir_is_zero(struct intel_uncore *uncore, i915_reg_t reg)
266b51a2842SVille Syrjälä {
26765f42cdcSPaulo Zanoni 	u32 val = intel_uncore_read(uncore, reg);
268b51a2842SVille Syrjälä 
269b51a2842SVille Syrjälä 	if (val == 0)
270b51a2842SVille Syrjälä 		return;
271b51a2842SVille Syrjälä 
272a9f236d1SPankaj Bharadiya 	drm_WARN(&uncore->i915->drm, 1,
273a9f236d1SPankaj Bharadiya 		 "Interrupt register 0x%x is not zero: 0x%08x\n",
274f0f59a00SVille Syrjälä 		 i915_mmio_reg_offset(reg), val);
27565f42cdcSPaulo Zanoni 	intel_uncore_write(uncore, reg, 0xffffffff);
27665f42cdcSPaulo Zanoni 	intel_uncore_posting_read(uncore, reg);
27765f42cdcSPaulo Zanoni 	intel_uncore_write(uncore, reg, 0xffffffff);
27865f42cdcSPaulo Zanoni 	intel_uncore_posting_read(uncore, reg);
279b51a2842SVille Syrjälä }
280337ba017SPaulo Zanoni 
28165f42cdcSPaulo Zanoni static void gen2_assert_iir_is_zero(struct intel_uncore *uncore)
282e9e9848aSVille Syrjälä {
28365f42cdcSPaulo Zanoni 	u16 val = intel_uncore_read16(uncore, GEN2_IIR);
284e9e9848aSVille Syrjälä 
285e9e9848aSVille Syrjälä 	if (val == 0)
286e9e9848aSVille Syrjälä 		return;
287e9e9848aSVille Syrjälä 
288a9f236d1SPankaj Bharadiya 	drm_WARN(&uncore->i915->drm, 1,
289a9f236d1SPankaj Bharadiya 		 "Interrupt register 0x%x is not zero: 0x%08x\n",
2909d9523d8SPaulo Zanoni 		 i915_mmio_reg_offset(GEN2_IIR), val);
29165f42cdcSPaulo Zanoni 	intel_uncore_write16(uncore, GEN2_IIR, 0xffff);
29265f42cdcSPaulo Zanoni 	intel_uncore_posting_read16(uncore, GEN2_IIR);
29365f42cdcSPaulo Zanoni 	intel_uncore_write16(uncore, GEN2_IIR, 0xffff);
29465f42cdcSPaulo Zanoni 	intel_uncore_posting_read16(uncore, GEN2_IIR);
295e9e9848aSVille Syrjälä }
296e9e9848aSVille Syrjälä 
297cf1c97dcSAndi Shyti void gen3_irq_init(struct intel_uncore *uncore,
29868eb49b1SPaulo Zanoni 		   i915_reg_t imr, u32 imr_val,
29968eb49b1SPaulo Zanoni 		   i915_reg_t ier, u32 ier_val,
30068eb49b1SPaulo Zanoni 		   i915_reg_t iir)
30168eb49b1SPaulo Zanoni {
30265f42cdcSPaulo Zanoni 	gen3_assert_iir_is_zero(uncore, iir);
30335079899SPaulo Zanoni 
30465f42cdcSPaulo Zanoni 	intel_uncore_write(uncore, ier, ier_val);
30565f42cdcSPaulo Zanoni 	intel_uncore_write(uncore, imr, imr_val);
30665f42cdcSPaulo Zanoni 	intel_uncore_posting_read(uncore, imr);
30768eb49b1SPaulo Zanoni }
30835079899SPaulo Zanoni 
309cf1c97dcSAndi Shyti void gen2_irq_init(struct intel_uncore *uncore,
3102918c3caSPaulo Zanoni 		   u32 imr_val, u32 ier_val)
31168eb49b1SPaulo Zanoni {
31265f42cdcSPaulo Zanoni 	gen2_assert_iir_is_zero(uncore);
31368eb49b1SPaulo Zanoni 
31465f42cdcSPaulo Zanoni 	intel_uncore_write16(uncore, GEN2_IER, ier_val);
31565f42cdcSPaulo Zanoni 	intel_uncore_write16(uncore, GEN2_IMR, imr_val);
31665f42cdcSPaulo Zanoni 	intel_uncore_posting_read16(uncore, GEN2_IMR);
31768eb49b1SPaulo Zanoni }
31868eb49b1SPaulo Zanoni 
3190706f17cSEgbert Eich /* For display hotplug interrupt */
3200706f17cSEgbert Eich static inline void
3210706f17cSEgbert Eich i915_hotplug_interrupt_update_locked(struct drm_i915_private *dev_priv,
322a9c287c9SJani Nikula 				     u32 mask,
323a9c287c9SJani Nikula 				     u32 bits)
3240706f17cSEgbert Eich {
325a9c287c9SJani Nikula 	u32 val;
3260706f17cSEgbert Eich 
32767520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
32848a1b8d4SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, bits & ~mask);
3290706f17cSEgbert Eich 
3302939eb06SJani Nikula 	val = intel_uncore_read(&dev_priv->uncore, PORT_HOTPLUG_EN);
3310706f17cSEgbert Eich 	val &= ~mask;
3320706f17cSEgbert Eich 	val |= bits;
3332939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, PORT_HOTPLUG_EN, val);
3340706f17cSEgbert Eich }
3350706f17cSEgbert Eich 
3360706f17cSEgbert Eich /**
3370706f17cSEgbert Eich  * i915_hotplug_interrupt_update - update hotplug interrupt enable
3380706f17cSEgbert Eich  * @dev_priv: driver private
3390706f17cSEgbert Eich  * @mask: bits to update
3400706f17cSEgbert Eich  * @bits: bits to enable
3410706f17cSEgbert Eich  * NOTE: the HPD enable bits are modified both inside and outside
3420706f17cSEgbert Eich  * of an interrupt context. To avoid that read-modify-write cycles
3430706f17cSEgbert Eich  * interfer, these bits are protected by a spinlock. Since this
3440706f17cSEgbert Eich  * function is usually not called from a context where the lock is
3450706f17cSEgbert Eich  * held already, this function acquires the lock itself. A non-locking
3460706f17cSEgbert Eich  * version is also available.
3470706f17cSEgbert Eich  */
3480706f17cSEgbert Eich void i915_hotplug_interrupt_update(struct drm_i915_private *dev_priv,
349a9c287c9SJani Nikula 				   u32 mask,
350a9c287c9SJani Nikula 				   u32 bits)
3510706f17cSEgbert Eich {
3520706f17cSEgbert Eich 	spin_lock_irq(&dev_priv->irq_lock);
3530706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv, mask, bits);
3540706f17cSEgbert Eich 	spin_unlock_irq(&dev_priv->irq_lock);
3550706f17cSEgbert Eich }
3560706f17cSEgbert Eich 
357d9dc34f1SVille Syrjälä /**
358d9dc34f1SVille Syrjälä  * ilk_update_display_irq - update DEIMR
359d9dc34f1SVille Syrjälä  * @dev_priv: driver private
360d9dc34f1SVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
361d9dc34f1SVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
362d9dc34f1SVille Syrjälä  */
363fbdedaeaSVille Syrjälä void ilk_update_display_irq(struct drm_i915_private *dev_priv,
364a9c287c9SJani Nikula 			    u32 interrupt_mask,
365a9c287c9SJani Nikula 			    u32 enabled_irq_mask)
366036a4a7dSZhenyu Wang {
367a9c287c9SJani Nikula 	u32 new_val;
368d9dc34f1SVille Syrjälä 
36967520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
37048a1b8d4SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, enabled_irq_mask & ~interrupt_mask);
371d9dc34f1SVille Syrjälä 
372d9dc34f1SVille Syrjälä 	new_val = dev_priv->irq_mask;
373d9dc34f1SVille Syrjälä 	new_val &= ~interrupt_mask;
374d9dc34f1SVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
375d9dc34f1SVille Syrjälä 
376e44adb5dSChris Wilson 	if (new_val != dev_priv->irq_mask &&
377e44adb5dSChris Wilson 	    !drm_WARN_ON(&dev_priv->drm, !intel_irqs_enabled(dev_priv))) {
378d9dc34f1SVille Syrjälä 		dev_priv->irq_mask = new_val;
3792939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, DEIMR, dev_priv->irq_mask);
3802939eb06SJani Nikula 		intel_uncore_posting_read(&dev_priv->uncore, DEIMR);
381036a4a7dSZhenyu Wang 	}
382036a4a7dSZhenyu Wang }
383036a4a7dSZhenyu Wang 
3840961021aSBen Widawsky /**
3853a3b3c7dSVille Syrjälä  * bdw_update_port_irq - update DE port interrupt
3863a3b3c7dSVille Syrjälä  * @dev_priv: driver private
3873a3b3c7dSVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
3883a3b3c7dSVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
3893a3b3c7dSVille Syrjälä  */
3903a3b3c7dSVille Syrjälä static void bdw_update_port_irq(struct drm_i915_private *dev_priv,
391a9c287c9SJani Nikula 				u32 interrupt_mask,
392a9c287c9SJani Nikula 				u32 enabled_irq_mask)
3933a3b3c7dSVille Syrjälä {
394a9c287c9SJani Nikula 	u32 new_val;
395a9c287c9SJani Nikula 	u32 old_val;
3963a3b3c7dSVille Syrjälä 
39767520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
3983a3b3c7dSVille Syrjälä 
39948a1b8d4SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, enabled_irq_mask & ~interrupt_mask);
4003a3b3c7dSVille Syrjälä 
40148a1b8d4SPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, !intel_irqs_enabled(dev_priv)))
4023a3b3c7dSVille Syrjälä 		return;
4033a3b3c7dSVille Syrjälä 
4042939eb06SJani Nikula 	old_val = intel_uncore_read(&dev_priv->uncore, GEN8_DE_PORT_IMR);
4053a3b3c7dSVille Syrjälä 
4063a3b3c7dSVille Syrjälä 	new_val = old_val;
4073a3b3c7dSVille Syrjälä 	new_val &= ~interrupt_mask;
4083a3b3c7dSVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
4093a3b3c7dSVille Syrjälä 
4103a3b3c7dSVille Syrjälä 	if (new_val != old_val) {
4112939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, GEN8_DE_PORT_IMR, new_val);
4122939eb06SJani Nikula 		intel_uncore_posting_read(&dev_priv->uncore, GEN8_DE_PORT_IMR);
4133a3b3c7dSVille Syrjälä 	}
4143a3b3c7dSVille Syrjälä }
4153a3b3c7dSVille Syrjälä 
4163a3b3c7dSVille Syrjälä /**
417013d3752SVille Syrjälä  * bdw_update_pipe_irq - update DE pipe interrupt
418013d3752SVille Syrjälä  * @dev_priv: driver private
419013d3752SVille Syrjälä  * @pipe: pipe whose interrupt to update
420013d3752SVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
421013d3752SVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
422013d3752SVille Syrjälä  */
423013d3752SVille Syrjälä void bdw_update_pipe_irq(struct drm_i915_private *dev_priv,
424013d3752SVille Syrjälä 			 enum pipe pipe,
425a9c287c9SJani Nikula 			 u32 interrupt_mask,
426a9c287c9SJani Nikula 			 u32 enabled_irq_mask)
427013d3752SVille Syrjälä {
428a9c287c9SJani Nikula 	u32 new_val;
429013d3752SVille Syrjälä 
43067520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
431013d3752SVille Syrjälä 
43248a1b8d4SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, enabled_irq_mask & ~interrupt_mask);
433013d3752SVille Syrjälä 
43448a1b8d4SPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, !intel_irqs_enabled(dev_priv)))
435013d3752SVille Syrjälä 		return;
436013d3752SVille Syrjälä 
437013d3752SVille Syrjälä 	new_val = dev_priv->de_irq_mask[pipe];
438013d3752SVille Syrjälä 	new_val &= ~interrupt_mask;
439013d3752SVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
440013d3752SVille Syrjälä 
441013d3752SVille Syrjälä 	if (new_val != dev_priv->de_irq_mask[pipe]) {
442013d3752SVille Syrjälä 		dev_priv->de_irq_mask[pipe] = new_val;
4432939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
4442939eb06SJani Nikula 		intel_uncore_posting_read(&dev_priv->uncore, GEN8_DE_PIPE_IMR(pipe));
445013d3752SVille Syrjälä 	}
446013d3752SVille Syrjälä }
447013d3752SVille Syrjälä 
448013d3752SVille Syrjälä /**
449fee884edSDaniel Vetter  * ibx_display_interrupt_update - update SDEIMR
450fee884edSDaniel Vetter  * @dev_priv: driver private
451fee884edSDaniel Vetter  * @interrupt_mask: mask of interrupt bits to update
452fee884edSDaniel Vetter  * @enabled_irq_mask: mask of interrupt bits to enable
453fee884edSDaniel Vetter  */
45447339cd9SDaniel Vetter void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
455a9c287c9SJani Nikula 				  u32 interrupt_mask,
456a9c287c9SJani Nikula 				  u32 enabled_irq_mask)
457fee884edSDaniel Vetter {
4582939eb06SJani Nikula 	u32 sdeimr = intel_uncore_read(&dev_priv->uncore, SDEIMR);
459fee884edSDaniel Vetter 	sdeimr &= ~interrupt_mask;
460fee884edSDaniel Vetter 	sdeimr |= (~enabled_irq_mask & interrupt_mask);
461fee884edSDaniel Vetter 
46248a1b8d4SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, enabled_irq_mask & ~interrupt_mask);
46315a17aaeSDaniel Vetter 
46467520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
465fee884edSDaniel Vetter 
46648a1b8d4SPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, !intel_irqs_enabled(dev_priv)))
467c67a470bSPaulo Zanoni 		return;
468c67a470bSPaulo Zanoni 
4692939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, SDEIMR, sdeimr);
4702939eb06SJani Nikula 	intel_uncore_posting_read(&dev_priv->uncore, SDEIMR);
471fee884edSDaniel Vetter }
4728664281bSPaulo Zanoni 
4736b12ca56SVille Syrjälä u32 i915_pipestat_enable_mask(struct drm_i915_private *dev_priv,
4746b12ca56SVille Syrjälä 			      enum pipe pipe)
4757c463586SKeith Packard {
4766b12ca56SVille Syrjälä 	u32 status_mask = dev_priv->pipestat_irq_mask[pipe];
47710c59c51SImre Deak 	u32 enable_mask = status_mask << 16;
47810c59c51SImre Deak 
4796b12ca56SVille Syrjälä 	lockdep_assert_held(&dev_priv->irq_lock);
4806b12ca56SVille Syrjälä 
4816b12ca56SVille Syrjälä 	if (INTEL_GEN(dev_priv) < 5)
4826b12ca56SVille Syrjälä 		goto out;
4836b12ca56SVille Syrjälä 
48410c59c51SImre Deak 	/*
485724a6905SVille Syrjälä 	 * On pipe A we don't support the PSR interrupt yet,
486724a6905SVille Syrjälä 	 * on pipe B and C the same bit MBZ.
48710c59c51SImre Deak 	 */
48848a1b8d4SPankaj Bharadiya 	if (drm_WARN_ON_ONCE(&dev_priv->drm,
48948a1b8d4SPankaj Bharadiya 			     status_mask & PIPE_A_PSR_STATUS_VLV))
49010c59c51SImre Deak 		return 0;
491724a6905SVille Syrjälä 	/*
492724a6905SVille Syrjälä 	 * On pipe B and C we don't support the PSR interrupt yet, on pipe
493724a6905SVille Syrjälä 	 * A the same bit is for perf counters which we don't use either.
494724a6905SVille Syrjälä 	 */
49548a1b8d4SPankaj Bharadiya 	if (drm_WARN_ON_ONCE(&dev_priv->drm,
49648a1b8d4SPankaj Bharadiya 			     status_mask & PIPE_B_PSR_STATUS_VLV))
497724a6905SVille Syrjälä 		return 0;
49810c59c51SImre Deak 
49910c59c51SImre Deak 	enable_mask &= ~(PIPE_FIFO_UNDERRUN_STATUS |
50010c59c51SImre Deak 			 SPRITE0_FLIP_DONE_INT_EN_VLV |
50110c59c51SImre Deak 			 SPRITE1_FLIP_DONE_INT_EN_VLV);
50210c59c51SImre Deak 	if (status_mask & SPRITE0_FLIP_DONE_INT_STATUS_VLV)
50310c59c51SImre Deak 		enable_mask |= SPRITE0_FLIP_DONE_INT_EN_VLV;
50410c59c51SImre Deak 	if (status_mask & SPRITE1_FLIP_DONE_INT_STATUS_VLV)
50510c59c51SImre Deak 		enable_mask |= SPRITE1_FLIP_DONE_INT_EN_VLV;
50610c59c51SImre Deak 
5076b12ca56SVille Syrjälä out:
50848a1b8d4SPankaj Bharadiya 	drm_WARN_ONCE(&dev_priv->drm,
50948a1b8d4SPankaj Bharadiya 		      enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
5106b12ca56SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
5116b12ca56SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
5126b12ca56SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask);
5136b12ca56SVille Syrjälä 
51410c59c51SImre Deak 	return enable_mask;
51510c59c51SImre Deak }
51610c59c51SImre Deak 
5176b12ca56SVille Syrjälä void i915_enable_pipestat(struct drm_i915_private *dev_priv,
5186b12ca56SVille Syrjälä 			  enum pipe pipe, u32 status_mask)
519755e9019SImre Deak {
5206b12ca56SVille Syrjälä 	i915_reg_t reg = PIPESTAT(pipe);
521755e9019SImre Deak 	u32 enable_mask;
522755e9019SImre Deak 
52348a1b8d4SPankaj Bharadiya 	drm_WARN_ONCE(&dev_priv->drm, status_mask & ~PIPESTAT_INT_STATUS_MASK,
5246b12ca56SVille Syrjälä 		      "pipe %c: status_mask=0x%x\n",
5256b12ca56SVille Syrjälä 		      pipe_name(pipe), status_mask);
5266b12ca56SVille Syrjälä 
5276b12ca56SVille Syrjälä 	lockdep_assert_held(&dev_priv->irq_lock);
52848a1b8d4SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, !intel_irqs_enabled(dev_priv));
5296b12ca56SVille Syrjälä 
5306b12ca56SVille Syrjälä 	if ((dev_priv->pipestat_irq_mask[pipe] & status_mask) == status_mask)
5316b12ca56SVille Syrjälä 		return;
5326b12ca56SVille Syrjälä 
5336b12ca56SVille Syrjälä 	dev_priv->pipestat_irq_mask[pipe] |= status_mask;
5346b12ca56SVille Syrjälä 	enable_mask = i915_pipestat_enable_mask(dev_priv, pipe);
5356b12ca56SVille Syrjälä 
5362939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, reg, enable_mask | status_mask);
5372939eb06SJani Nikula 	intel_uncore_posting_read(&dev_priv->uncore, reg);
538755e9019SImre Deak }
539755e9019SImre Deak 
5406b12ca56SVille Syrjälä void i915_disable_pipestat(struct drm_i915_private *dev_priv,
5416b12ca56SVille Syrjälä 			   enum pipe pipe, u32 status_mask)
542755e9019SImre Deak {
5436b12ca56SVille Syrjälä 	i915_reg_t reg = PIPESTAT(pipe);
544755e9019SImre Deak 	u32 enable_mask;
545755e9019SImre Deak 
54648a1b8d4SPankaj Bharadiya 	drm_WARN_ONCE(&dev_priv->drm, status_mask & ~PIPESTAT_INT_STATUS_MASK,
5476b12ca56SVille Syrjälä 		      "pipe %c: status_mask=0x%x\n",
5486b12ca56SVille Syrjälä 		      pipe_name(pipe), status_mask);
5496b12ca56SVille Syrjälä 
5506b12ca56SVille Syrjälä 	lockdep_assert_held(&dev_priv->irq_lock);
55148a1b8d4SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, !intel_irqs_enabled(dev_priv));
5526b12ca56SVille Syrjälä 
5536b12ca56SVille Syrjälä 	if ((dev_priv->pipestat_irq_mask[pipe] & status_mask) == 0)
5546b12ca56SVille Syrjälä 		return;
5556b12ca56SVille Syrjälä 
5566b12ca56SVille Syrjälä 	dev_priv->pipestat_irq_mask[pipe] &= ~status_mask;
5576b12ca56SVille Syrjälä 	enable_mask = i915_pipestat_enable_mask(dev_priv, pipe);
5586b12ca56SVille Syrjälä 
5592939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, reg, enable_mask | status_mask);
5602939eb06SJani Nikula 	intel_uncore_posting_read(&dev_priv->uncore, reg);
561755e9019SImre Deak }
562755e9019SImre Deak 
563f3e30485SVille Syrjälä static bool i915_has_asle(struct drm_i915_private *dev_priv)
564f3e30485SVille Syrjälä {
565f3e30485SVille Syrjälä 	if (!dev_priv->opregion.asle)
566f3e30485SVille Syrjälä 		return false;
567f3e30485SVille Syrjälä 
568f3e30485SVille Syrjälä 	return IS_PINEVIEW(dev_priv) || IS_MOBILE(dev_priv);
569f3e30485SVille Syrjälä }
570f3e30485SVille Syrjälä 
571c0e09200SDave Airlie /**
572f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
57314bb2c11STvrtko Ursulin  * @dev_priv: i915 device private
57401c66889SZhao Yakui  */
57591d14251STvrtko Ursulin static void i915_enable_asle_pipestat(struct drm_i915_private *dev_priv)
57601c66889SZhao Yakui {
577f3e30485SVille Syrjälä 	if (!i915_has_asle(dev_priv))
578f49e38ddSJani Nikula 		return;
579f49e38ddSJani Nikula 
58013321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
58101c66889SZhao Yakui 
582755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_STATUS);
58391d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 4)
5843b6c42e8SDaniel Vetter 		i915_enable_pipestat(dev_priv, PIPE_A,
585755e9019SImre Deak 				     PIPE_LEGACY_BLC_EVENT_STATUS);
5861ec14ad3SChris Wilson 
58713321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
58801c66889SZhao Yakui }
58901c66889SZhao Yakui 
590f75f3746SVille Syrjälä /*
591f75f3746SVille Syrjälä  * This timing diagram depicts the video signal in and
592f75f3746SVille Syrjälä  * around the vertical blanking period.
593f75f3746SVille Syrjälä  *
594f75f3746SVille Syrjälä  * Assumptions about the fictitious mode used in this example:
595f75f3746SVille Syrjälä  *  vblank_start >= 3
596f75f3746SVille Syrjälä  *  vsync_start = vblank_start + 1
597f75f3746SVille Syrjälä  *  vsync_end = vblank_start + 2
598f75f3746SVille Syrjälä  *  vtotal = vblank_start + 3
599f75f3746SVille Syrjälä  *
600f75f3746SVille Syrjälä  *           start of vblank:
601f75f3746SVille Syrjälä  *           latch double buffered registers
602f75f3746SVille Syrjälä  *           increment frame counter (ctg+)
603f75f3746SVille Syrjälä  *           generate start of vblank interrupt (gen4+)
604f75f3746SVille Syrjälä  *           |
605f75f3746SVille Syrjälä  *           |          frame start:
606f75f3746SVille Syrjälä  *           |          generate frame start interrupt (aka. vblank interrupt) (gmch)
607f75f3746SVille Syrjälä  *           |          may be shifted forward 1-3 extra lines via PIPECONF
608f75f3746SVille Syrjälä  *           |          |
609f75f3746SVille Syrjälä  *           |          |  start of vsync:
610f75f3746SVille Syrjälä  *           |          |  generate vsync interrupt
611f75f3746SVille Syrjälä  *           |          |  |
612f75f3746SVille Syrjälä  * ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx
613f75f3746SVille Syrjälä  *       .   \hs/   .      \hs/          \hs/          \hs/   .      \hs/
614f75f3746SVille Syrjälä  * ----va---> <-----------------vb--------------------> <--------va-------------
615f75f3746SVille Syrjälä  *       |          |       <----vs----->                     |
616f75f3746SVille Syrjälä  * -vbs-----> <---vbs+1---> <---vbs+2---> <-----0-----> <-----1-----> <-----2--- (scanline counter gen2)
617f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2---> <-----0--- (scanline counter gen3+)
618f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2- (scanline counter hsw+ hdmi)
619f75f3746SVille Syrjälä  *       |          |                                         |
620f75f3746SVille Syrjälä  *       last visible pixel                                   first visible pixel
621f75f3746SVille Syrjälä  *                  |                                         increment frame counter (gen3/4)
622f75f3746SVille Syrjälä  *                  pixel counter = vblank_start * htotal     pixel counter = 0 (gen3/4)
623f75f3746SVille Syrjälä  *
624f75f3746SVille Syrjälä  * x  = horizontal active
625f75f3746SVille Syrjälä  * _  = horizontal blanking
626f75f3746SVille Syrjälä  * hs = horizontal sync
627f75f3746SVille Syrjälä  * va = vertical active
628f75f3746SVille Syrjälä  * vb = vertical blanking
629f75f3746SVille Syrjälä  * vs = vertical sync
630f75f3746SVille Syrjälä  * vbs = vblank_start (number)
631f75f3746SVille Syrjälä  *
632f75f3746SVille Syrjälä  * Summary:
633f75f3746SVille Syrjälä  * - most events happen at the start of horizontal sync
634f75f3746SVille Syrjälä  * - frame start happens at the start of horizontal blank, 1-4 lines
635f75f3746SVille Syrjälä  *   (depending on PIPECONF settings) after the start of vblank
636f75f3746SVille Syrjälä  * - gen3/4 pixel and frame counter are synchronized with the start
637f75f3746SVille Syrjälä  *   of horizontal active on the first line of vertical active
638f75f3746SVille Syrjälä  */
639f75f3746SVille Syrjälä 
64042f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
64142f52ef8SKeith Packard  * we use as a pipe index
64242f52ef8SKeith Packard  */
64308fa8fd0SVille Syrjälä u32 i915_get_vblank_counter(struct drm_crtc *crtc)
6440a3e67a4SJesse Barnes {
64508fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
64608fa8fd0SVille Syrjälä 	struct drm_vblank_crtc *vblank = &dev_priv->drm.vblank[drm_crtc_index(crtc)];
64732db0b65SVille Syrjälä 	const struct drm_display_mode *mode = &vblank->hwmode;
64808fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
649f0f59a00SVille Syrjälä 	i915_reg_t high_frame, low_frame;
6500b2a8e09SVille Syrjälä 	u32 high1, high2, low, pixel, vbl_start, hsync_start, htotal;
651694e409dSVille Syrjälä 	unsigned long irqflags;
652391f75e2SVille Syrjälä 
65332db0b65SVille Syrjälä 	/*
65432db0b65SVille Syrjälä 	 * On i965gm TV output the frame counter only works up to
65532db0b65SVille Syrjälä 	 * the point when we enable the TV encoder. After that the
65632db0b65SVille Syrjälä 	 * frame counter ceases to work and reads zero. We need a
65732db0b65SVille Syrjälä 	 * vblank wait before enabling the TV encoder and so we
65832db0b65SVille Syrjälä 	 * have to enable vblank interrupts while the frame counter
65932db0b65SVille Syrjälä 	 * is still in a working state. However the core vblank code
66032db0b65SVille Syrjälä 	 * does not like us returning non-zero frame counter values
66132db0b65SVille Syrjälä 	 * when we've told it that we don't have a working frame
66232db0b65SVille Syrjälä 	 * counter. Thus we must stop non-zero values leaking out.
66332db0b65SVille Syrjälä 	 */
66432db0b65SVille Syrjälä 	if (!vblank->max_vblank_count)
66532db0b65SVille Syrjälä 		return 0;
66632db0b65SVille Syrjälä 
6670b2a8e09SVille Syrjälä 	htotal = mode->crtc_htotal;
6680b2a8e09SVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
6690b2a8e09SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
6700b2a8e09SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
6710b2a8e09SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
672391f75e2SVille Syrjälä 
6730b2a8e09SVille Syrjälä 	/* Convert to pixel count */
6740b2a8e09SVille Syrjälä 	vbl_start *= htotal;
6750b2a8e09SVille Syrjälä 
6760b2a8e09SVille Syrjälä 	/* Start of vblank event occurs at start of hsync */
6770b2a8e09SVille Syrjälä 	vbl_start -= htotal - hsync_start;
6780b2a8e09SVille Syrjälä 
6799db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
6809db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
6815eddb70bSChris Wilson 
682694e409dSVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
683694e409dSVille Syrjälä 
6840a3e67a4SJesse Barnes 	/*
6850a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
6860a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
6870a3e67a4SJesse Barnes 	 * register.
6880a3e67a4SJesse Barnes 	 */
6890a3e67a4SJesse Barnes 	do {
6908cbda6b2SJani Nikula 		high1 = intel_de_read_fw(dev_priv, high_frame) & PIPE_FRAME_HIGH_MASK;
6918cbda6b2SJani Nikula 		low   = intel_de_read_fw(dev_priv, low_frame);
6928cbda6b2SJani Nikula 		high2 = intel_de_read_fw(dev_priv, high_frame) & PIPE_FRAME_HIGH_MASK;
6930a3e67a4SJesse Barnes 	} while (high1 != high2);
6940a3e67a4SJesse Barnes 
695694e409dSVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
696694e409dSVille Syrjälä 
6975eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
698391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
6995eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
700391f75e2SVille Syrjälä 
701391f75e2SVille Syrjälä 	/*
702391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
703391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
704391f75e2SVille Syrjälä 	 * counter against vblank start.
705391f75e2SVille Syrjälä 	 */
706edc08d0aSVille Syrjälä 	return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
7070a3e67a4SJesse Barnes }
7080a3e67a4SJesse Barnes 
70908fa8fd0SVille Syrjälä u32 g4x_get_vblank_counter(struct drm_crtc *crtc)
7109880b7a5SJesse Barnes {
71108fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
71233267703SVandita Kulkarni 	struct drm_vblank_crtc *vblank = &dev_priv->drm.vblank[drm_crtc_index(crtc)];
71308fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
7149880b7a5SJesse Barnes 
71533267703SVandita Kulkarni 	if (!vblank->max_vblank_count)
71633267703SVandita Kulkarni 		return 0;
71733267703SVandita Kulkarni 
7182939eb06SJani Nikula 	return intel_uncore_read(&dev_priv->uncore, PIPE_FRMCOUNT_G4X(pipe));
7199880b7a5SJesse Barnes }
7209880b7a5SJesse Barnes 
72106d6fda5SVille Syrjälä static u32 intel_crtc_scanlines_since_frame_timestamp(struct intel_crtc *crtc)
722aec0246fSUma Shankar {
723aec0246fSUma Shankar 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
724aec0246fSUma Shankar 	struct drm_vblank_crtc *vblank =
725aec0246fSUma Shankar 		&crtc->base.dev->vblank[drm_crtc_index(&crtc->base)];
726aec0246fSUma Shankar 	const struct drm_display_mode *mode = &vblank->hwmode;
727aec0246fSUma Shankar 	u32 htotal = mode->crtc_htotal;
728aec0246fSUma Shankar 	u32 clock = mode->crtc_clock;
72906d6fda5SVille Syrjälä 	u32 scan_prev_time, scan_curr_time, scan_post_time;
730aec0246fSUma Shankar 
731aec0246fSUma Shankar 	/*
732aec0246fSUma Shankar 	 * To avoid the race condition where we might cross into the
733aec0246fSUma Shankar 	 * next vblank just between the PIPE_FRMTMSTMP and TIMESTAMP_CTR
734aec0246fSUma Shankar 	 * reads. We make sure we read PIPE_FRMTMSTMP and TIMESTAMP_CTR
735aec0246fSUma Shankar 	 * during the same frame.
736aec0246fSUma Shankar 	 */
737aec0246fSUma Shankar 	do {
738aec0246fSUma Shankar 		/*
739aec0246fSUma Shankar 		 * This field provides read back of the display
740aec0246fSUma Shankar 		 * pipe frame time stamp. The time stamp value
741aec0246fSUma Shankar 		 * is sampled at every start of vertical blank.
742aec0246fSUma Shankar 		 */
7438cbda6b2SJani Nikula 		scan_prev_time = intel_de_read_fw(dev_priv,
7448cbda6b2SJani Nikula 						  PIPE_FRMTMSTMP(crtc->pipe));
745aec0246fSUma Shankar 
746aec0246fSUma Shankar 		/*
747aec0246fSUma Shankar 		 * The TIMESTAMP_CTR register has the current
748aec0246fSUma Shankar 		 * time stamp value.
749aec0246fSUma Shankar 		 */
7508cbda6b2SJani Nikula 		scan_curr_time = intel_de_read_fw(dev_priv, IVB_TIMESTAMP_CTR);
751aec0246fSUma Shankar 
7528cbda6b2SJani Nikula 		scan_post_time = intel_de_read_fw(dev_priv,
7538cbda6b2SJani Nikula 						  PIPE_FRMTMSTMP(crtc->pipe));
754aec0246fSUma Shankar 	} while (scan_post_time != scan_prev_time);
755aec0246fSUma Shankar 
75606d6fda5SVille Syrjälä 	return div_u64(mul_u32_u32(scan_curr_time - scan_prev_time,
757aec0246fSUma Shankar 				   clock), 1000 * htotal);
75806d6fda5SVille Syrjälä }
75906d6fda5SVille Syrjälä 
76006d6fda5SVille Syrjälä /*
76106d6fda5SVille Syrjälä  * On certain encoders on certain platforms, pipe
76206d6fda5SVille Syrjälä  * scanline register will not work to get the scanline,
76306d6fda5SVille Syrjälä  * since the timings are driven from the PORT or issues
76406d6fda5SVille Syrjälä  * with scanline register updates.
76506d6fda5SVille Syrjälä  * This function will use Framestamp and current
76606d6fda5SVille Syrjälä  * timestamp registers to calculate the scanline.
76706d6fda5SVille Syrjälä  */
76806d6fda5SVille Syrjälä static u32 __intel_get_crtc_scanline_from_timestamp(struct intel_crtc *crtc)
76906d6fda5SVille Syrjälä {
77006d6fda5SVille Syrjälä 	struct drm_vblank_crtc *vblank =
77106d6fda5SVille Syrjälä 		&crtc->base.dev->vblank[drm_crtc_index(&crtc->base)];
77206d6fda5SVille Syrjälä 	const struct drm_display_mode *mode = &vblank->hwmode;
77306d6fda5SVille Syrjälä 	u32 vblank_start = mode->crtc_vblank_start;
77406d6fda5SVille Syrjälä 	u32 vtotal = mode->crtc_vtotal;
77506d6fda5SVille Syrjälä 	u32 scanline;
77606d6fda5SVille Syrjälä 
77706d6fda5SVille Syrjälä 	scanline = intel_crtc_scanlines_since_frame_timestamp(crtc);
778aec0246fSUma Shankar 	scanline = min(scanline, vtotal - 1);
779aec0246fSUma Shankar 	scanline = (scanline + vblank_start) % vtotal;
780aec0246fSUma Shankar 
781aec0246fSUma Shankar 	return scanline;
782aec0246fSUma Shankar }
783aec0246fSUma Shankar 
7848cbda6b2SJani Nikula /*
7858cbda6b2SJani Nikula  * intel_de_read_fw(), only for fast reads of display block, no need for
7868cbda6b2SJani Nikula  * forcewake etc.
7878cbda6b2SJani Nikula  */
788a225f079SVille Syrjälä static int __intel_get_crtc_scanline(struct intel_crtc *crtc)
789a225f079SVille Syrjälä {
790a225f079SVille Syrjälä 	struct drm_device *dev = crtc->base.dev;
791fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
7925caa0feaSDaniel Vetter 	const struct drm_display_mode *mode;
7935caa0feaSDaniel Vetter 	struct drm_vblank_crtc *vblank;
794a225f079SVille Syrjälä 	enum pipe pipe = crtc->pipe;
79580715b2fSVille Syrjälä 	int position, vtotal;
796a225f079SVille Syrjälä 
79772259536SVille Syrjälä 	if (!crtc->active)
79872259536SVille Syrjälä 		return -1;
79972259536SVille Syrjälä 
8005caa0feaSDaniel Vetter 	vblank = &crtc->base.dev->vblank[drm_crtc_index(&crtc->base)];
8015caa0feaSDaniel Vetter 	mode = &vblank->hwmode;
8025caa0feaSDaniel Vetter 
803af157b76SVille Syrjälä 	if (crtc->mode_flags & I915_MODE_FLAG_GET_SCANLINE_FROM_TIMESTAMP)
804aec0246fSUma Shankar 		return __intel_get_crtc_scanline_from_timestamp(crtc);
805aec0246fSUma Shankar 
80680715b2fSVille Syrjälä 	vtotal = mode->crtc_vtotal;
807a225f079SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
808a225f079SVille Syrjälä 		vtotal /= 2;
809a225f079SVille Syrjälä 
810cf819effSLucas De Marchi 	if (IS_GEN(dev_priv, 2))
8118cbda6b2SJani Nikula 		position = intel_de_read_fw(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
812a225f079SVille Syrjälä 	else
8138cbda6b2SJani Nikula 		position = intel_de_read_fw(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
814a225f079SVille Syrjälä 
815a225f079SVille Syrjälä 	/*
81641b578fbSJesse Barnes 	 * On HSW, the DSL reg (0x70000) appears to return 0 if we
81741b578fbSJesse Barnes 	 * read it just before the start of vblank.  So try it again
81841b578fbSJesse Barnes 	 * so we don't accidentally end up spanning a vblank frame
81941b578fbSJesse Barnes 	 * increment, causing the pipe_update_end() code to squak at us.
82041b578fbSJesse Barnes 	 *
82141b578fbSJesse Barnes 	 * The nature of this problem means we can't simply check the ISR
82241b578fbSJesse Barnes 	 * bit and return the vblank start value; nor can we use the scanline
82341b578fbSJesse Barnes 	 * debug register in the transcoder as it appears to have the same
82441b578fbSJesse Barnes 	 * problem.  We may need to extend this to include other platforms,
82541b578fbSJesse Barnes 	 * but so far testing only shows the problem on HSW.
82641b578fbSJesse Barnes 	 */
82791d14251STvrtko Ursulin 	if (HAS_DDI(dev_priv) && !position) {
82841b578fbSJesse Barnes 		int i, temp;
82941b578fbSJesse Barnes 
83041b578fbSJesse Barnes 		for (i = 0; i < 100; i++) {
83141b578fbSJesse Barnes 			udelay(1);
8328cbda6b2SJani Nikula 			temp = intel_de_read_fw(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
83341b578fbSJesse Barnes 			if (temp != position) {
83441b578fbSJesse Barnes 				position = temp;
83541b578fbSJesse Barnes 				break;
83641b578fbSJesse Barnes 			}
83741b578fbSJesse Barnes 		}
83841b578fbSJesse Barnes 	}
83941b578fbSJesse Barnes 
84041b578fbSJesse Barnes 	/*
84180715b2fSVille Syrjälä 	 * See update_scanline_offset() for the details on the
84280715b2fSVille Syrjälä 	 * scanline_offset adjustment.
843a225f079SVille Syrjälä 	 */
84480715b2fSVille Syrjälä 	return (position + crtc->scanline_offset) % vtotal;
845a225f079SVille Syrjälä }
846a225f079SVille Syrjälä 
8474bbffbf3SThomas Zimmermann static bool i915_get_crtc_scanoutpos(struct drm_crtc *_crtc,
8484bbffbf3SThomas Zimmermann 				     bool in_vblank_irq,
8494bbffbf3SThomas Zimmermann 				     int *vpos, int *hpos,
8503bb403bfSVille Syrjälä 				     ktime_t *stime, ktime_t *etime,
8513bb403bfSVille Syrjälä 				     const struct drm_display_mode *mode)
8520af7e4dfSMario Kleiner {
8534bbffbf3SThomas Zimmermann 	struct drm_device *dev = _crtc->dev;
854fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
8554bbffbf3SThomas Zimmermann 	struct intel_crtc *crtc = to_intel_crtc(_crtc);
856e8edae54SVille Syrjälä 	enum pipe pipe = crtc->pipe;
8573aa18df8SVille Syrjälä 	int position;
85878e8fc6bSVille Syrjälä 	int vbl_start, vbl_end, hsync_start, htotal, vtotal;
859ad3543edSMario Kleiner 	unsigned long irqflags;
8608a920e24SVille Syrjälä 	bool use_scanline_counter = INTEL_GEN(dev_priv) >= 5 ||
8618a920e24SVille Syrjälä 		IS_G4X(dev_priv) || IS_GEN(dev_priv, 2) ||
862af157b76SVille Syrjälä 		crtc->mode_flags & I915_MODE_FLAG_USE_SCANLINE_COUNTER;
8630af7e4dfSMario Kleiner 
86448a1b8d4SPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, !mode->crtc_clock)) {
86500376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm,
86600376ccfSWambui Karuga 			"trying to get scanoutpos for disabled "
8679db4a9c7SJesse Barnes 			"pipe %c\n", pipe_name(pipe));
8681bf6ad62SDaniel Vetter 		return false;
8690af7e4dfSMario Kleiner 	}
8700af7e4dfSMario Kleiner 
871c2baf4b7SVille Syrjälä 	htotal = mode->crtc_htotal;
87278e8fc6bSVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
873c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
874c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
875c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
8760af7e4dfSMario Kleiner 
877d31faf65SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
878d31faf65SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
879d31faf65SVille Syrjälä 		vbl_end /= 2;
880d31faf65SVille Syrjälä 		vtotal /= 2;
881d31faf65SVille Syrjälä 	}
882d31faf65SVille Syrjälä 
883ad3543edSMario Kleiner 	/*
884ad3543edSMario Kleiner 	 * Lock uncore.lock, as we will do multiple timing critical raw
885ad3543edSMario Kleiner 	 * register reads, potentially with preemption disabled, so the
886ad3543edSMario Kleiner 	 * following code must not block on uncore.lock.
887ad3543edSMario Kleiner 	 */
888ad3543edSMario Kleiner 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
889ad3543edSMario Kleiner 
890ad3543edSMario Kleiner 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
891ad3543edSMario Kleiner 
892ad3543edSMario Kleiner 	/* Get optional system timestamp before query. */
893ad3543edSMario Kleiner 	if (stime)
894ad3543edSMario Kleiner 		*stime = ktime_get();
895ad3543edSMario Kleiner 
8967a2ec4a0SVille Syrjälä 	if (crtc->mode_flags & I915_MODE_FLAG_VRR) {
8977a2ec4a0SVille Syrjälä 		int scanlines = intel_crtc_scanlines_since_frame_timestamp(crtc);
8987a2ec4a0SVille Syrjälä 
8997a2ec4a0SVille Syrjälä 		position = __intel_get_crtc_scanline(crtc);
9007a2ec4a0SVille Syrjälä 
9017a2ec4a0SVille Syrjälä 		/*
9027a2ec4a0SVille Syrjälä 		 * Already exiting vblank? If so, shift our position
9037a2ec4a0SVille Syrjälä 		 * so it looks like we're already apporaching the full
9047a2ec4a0SVille Syrjälä 		 * vblank end. This should make the generated timestamp
9057a2ec4a0SVille Syrjälä 		 * more or less match when the active portion will start.
9067a2ec4a0SVille Syrjälä 		 */
9077a2ec4a0SVille Syrjälä 		if (position >= vbl_start && scanlines < position)
9087a2ec4a0SVille Syrjälä 			position = min(crtc->vmax_vblank_start + scanlines, vtotal - 1);
9097a2ec4a0SVille Syrjälä 	} else if (use_scanline_counter) {
9100af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
9110af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
9120af7e4dfSMario Kleiner 		 */
913e8edae54SVille Syrjälä 		position = __intel_get_crtc_scanline(crtc);
9140af7e4dfSMario Kleiner 	} else {
9150af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
9160af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
9170af7e4dfSMario Kleiner 		 * scanout position.
9180af7e4dfSMario Kleiner 		 */
9198cbda6b2SJani Nikula 		position = (intel_de_read_fw(dev_priv, PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
9200af7e4dfSMario Kleiner 
9213aa18df8SVille Syrjälä 		/* convert to pixel counts */
9223aa18df8SVille Syrjälä 		vbl_start *= htotal;
9233aa18df8SVille Syrjälä 		vbl_end *= htotal;
9243aa18df8SVille Syrjälä 		vtotal *= htotal;
92578e8fc6bSVille Syrjälä 
92678e8fc6bSVille Syrjälä 		/*
9277e78f1cbSVille Syrjälä 		 * In interlaced modes, the pixel counter counts all pixels,
9287e78f1cbSVille Syrjälä 		 * so one field will have htotal more pixels. In order to avoid
9297e78f1cbSVille Syrjälä 		 * the reported position from jumping backwards when the pixel
9307e78f1cbSVille Syrjälä 		 * counter is beyond the length of the shorter field, just
9317e78f1cbSVille Syrjälä 		 * clamp the position the length of the shorter field. This
9327e78f1cbSVille Syrjälä 		 * matches how the scanline counter based position works since
9337e78f1cbSVille Syrjälä 		 * the scanline counter doesn't count the two half lines.
9347e78f1cbSVille Syrjälä 		 */
9357e78f1cbSVille Syrjälä 		if (position >= vtotal)
9367e78f1cbSVille Syrjälä 			position = vtotal - 1;
9377e78f1cbSVille Syrjälä 
9387e78f1cbSVille Syrjälä 		/*
93978e8fc6bSVille Syrjälä 		 * Start of vblank interrupt is triggered at start of hsync,
94078e8fc6bSVille Syrjälä 		 * just prior to the first active line of vblank. However we
94178e8fc6bSVille Syrjälä 		 * consider lines to start at the leading edge of horizontal
94278e8fc6bSVille Syrjälä 		 * active. So, should we get here before we've crossed into
94378e8fc6bSVille Syrjälä 		 * the horizontal active of the first line in vblank, we would
94478e8fc6bSVille Syrjälä 		 * not set the DRM_SCANOUTPOS_INVBL flag. In order to fix that,
94578e8fc6bSVille Syrjälä 		 * always add htotal-hsync_start to the current pixel position.
94678e8fc6bSVille Syrjälä 		 */
94778e8fc6bSVille Syrjälä 		position = (position + htotal - hsync_start) % vtotal;
9483aa18df8SVille Syrjälä 	}
9493aa18df8SVille Syrjälä 
950ad3543edSMario Kleiner 	/* Get optional system timestamp after query. */
951ad3543edSMario Kleiner 	if (etime)
952ad3543edSMario Kleiner 		*etime = ktime_get();
953ad3543edSMario Kleiner 
954ad3543edSMario Kleiner 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
955ad3543edSMario Kleiner 
956ad3543edSMario Kleiner 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
957ad3543edSMario Kleiner 
9583aa18df8SVille Syrjälä 	/*
9593aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
9603aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
9613aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
9623aa18df8SVille Syrjälä 	 * up since vbl_end.
9633aa18df8SVille Syrjälä 	 */
9643aa18df8SVille Syrjälä 	if (position >= vbl_start)
9653aa18df8SVille Syrjälä 		position -= vbl_end;
9663aa18df8SVille Syrjälä 	else
9673aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
9683aa18df8SVille Syrjälä 
9698a920e24SVille Syrjälä 	if (use_scanline_counter) {
9703aa18df8SVille Syrjälä 		*vpos = position;
9713aa18df8SVille Syrjälä 		*hpos = 0;
9723aa18df8SVille Syrjälä 	} else {
9730af7e4dfSMario Kleiner 		*vpos = position / htotal;
9740af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
9750af7e4dfSMario Kleiner 	}
9760af7e4dfSMario Kleiner 
9771bf6ad62SDaniel Vetter 	return true;
9780af7e4dfSMario Kleiner }
9790af7e4dfSMario Kleiner 
9804bbffbf3SThomas Zimmermann bool intel_crtc_get_vblank_timestamp(struct drm_crtc *crtc, int *max_error,
9814bbffbf3SThomas Zimmermann 				     ktime_t *vblank_time, bool in_vblank_irq)
9824bbffbf3SThomas Zimmermann {
9834bbffbf3SThomas Zimmermann 	return drm_crtc_vblank_helper_get_vblank_timestamp_internal(
9844bbffbf3SThomas Zimmermann 		crtc, max_error, vblank_time, in_vblank_irq,
98548e67807SThomas Zimmermann 		i915_get_crtc_scanoutpos);
9864bbffbf3SThomas Zimmermann }
9874bbffbf3SThomas Zimmermann 
988a225f079SVille Syrjälä int intel_get_crtc_scanline(struct intel_crtc *crtc)
989a225f079SVille Syrjälä {
990fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
991a225f079SVille Syrjälä 	unsigned long irqflags;
992a225f079SVille Syrjälä 	int position;
993a225f079SVille Syrjälä 
994a225f079SVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
995a225f079SVille Syrjälä 	position = __intel_get_crtc_scanline(crtc);
996a225f079SVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
997a225f079SVille Syrjälä 
998a225f079SVille Syrjälä 	return position;
999a225f079SVille Syrjälä }
1000a225f079SVille Syrjälä 
1001e3689190SBen Widawsky /**
100274bb98baSLucas De Marchi  * ivb_parity_work - Workqueue called when a parity error interrupt
1003e3689190SBen Widawsky  * occurred.
1004e3689190SBen Widawsky  * @work: workqueue struct
1005e3689190SBen Widawsky  *
1006e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
1007e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
1008e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
1009e3689190SBen Widawsky  */
101074bb98baSLucas De Marchi static void ivb_parity_work(struct work_struct *work)
1011e3689190SBen Widawsky {
10122d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
1013cefcff8fSJoonas Lahtinen 		container_of(work, typeof(*dev_priv), l3_parity.error_work);
1014cf1c97dcSAndi Shyti 	struct intel_gt *gt = &dev_priv->gt;
1015e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
101635a85ac6SBen Widawsky 	char *parity_event[6];
1017a9c287c9SJani Nikula 	u32 misccpctl;
1018a9c287c9SJani Nikula 	u8 slice = 0;
1019e3689190SBen Widawsky 
1020e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
1021e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
1022e3689190SBen Widawsky 	 * any time we access those registers.
1023e3689190SBen Widawsky 	 */
102491c8a326SChris Wilson 	mutex_lock(&dev_priv->drm.struct_mutex);
1025e3689190SBen Widawsky 
102635a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
102748a1b8d4SPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, !dev_priv->l3_parity.which_slice))
102835a85ac6SBen Widawsky 		goto out;
102935a85ac6SBen Widawsky 
10302939eb06SJani Nikula 	misccpctl = intel_uncore_read(&dev_priv->uncore, GEN7_MISCCPCTL);
10312939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
10322939eb06SJani Nikula 	intel_uncore_posting_read(&dev_priv->uncore, GEN7_MISCCPCTL);
1033e3689190SBen Widawsky 
103435a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
1035f0f59a00SVille Syrjälä 		i915_reg_t reg;
103635a85ac6SBen Widawsky 
103735a85ac6SBen Widawsky 		slice--;
103848a1b8d4SPankaj Bharadiya 		if (drm_WARN_ON_ONCE(&dev_priv->drm,
103948a1b8d4SPankaj Bharadiya 				     slice >= NUM_L3_SLICES(dev_priv)))
104035a85ac6SBen Widawsky 			break;
104135a85ac6SBen Widawsky 
104235a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
104335a85ac6SBen Widawsky 
10446fa1c5f1SVille Syrjälä 		reg = GEN7_L3CDERRST1(slice);
104535a85ac6SBen Widawsky 
10462939eb06SJani Nikula 		error_status = intel_uncore_read(&dev_priv->uncore, reg);
1047e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
1048e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
1049e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1050e3689190SBen Widawsky 
10512939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
10522939eb06SJani Nikula 		intel_uncore_posting_read(&dev_priv->uncore, reg);
1053e3689190SBen Widawsky 
1054cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1055e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1056e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1057e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
105835a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
105935a85ac6SBen Widawsky 		parity_event[5] = NULL;
1060e3689190SBen Widawsky 
106191c8a326SChris Wilson 		kobject_uevent_env(&dev_priv->drm.primary->kdev->kobj,
1062e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
1063e3689190SBen Widawsky 
106435a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
106535a85ac6SBen Widawsky 			  slice, row, bank, subbank);
1066e3689190SBen Widawsky 
106735a85ac6SBen Widawsky 		kfree(parity_event[4]);
1068e3689190SBen Widawsky 		kfree(parity_event[3]);
1069e3689190SBen Widawsky 		kfree(parity_event[2]);
1070e3689190SBen Widawsky 		kfree(parity_event[1]);
1071e3689190SBen Widawsky 	}
1072e3689190SBen Widawsky 
10732939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, GEN7_MISCCPCTL, misccpctl);
107435a85ac6SBen Widawsky 
107535a85ac6SBen Widawsky out:
107648a1b8d4SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, dev_priv->l3_parity.which_slice);
1077cf1c97dcSAndi Shyti 	spin_lock_irq(&gt->irq_lock);
1078cf1c97dcSAndi Shyti 	gen5_gt_enable_irq(gt, GT_PARITY_ERROR(dev_priv));
1079cf1c97dcSAndi Shyti 	spin_unlock_irq(&gt->irq_lock);
108035a85ac6SBen Widawsky 
108191c8a326SChris Wilson 	mutex_unlock(&dev_priv->drm.struct_mutex);
108235a85ac6SBen Widawsky }
108335a85ac6SBen Widawsky 
1084af92058fSVille Syrjälä static bool gen11_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
1085121e758eSDhinakaran Pandiyan {
1086af92058fSVille Syrjälä 	switch (pin) {
1087da51e4baSVille Syrjälä 	case HPD_PORT_TC1:
1088da51e4baSVille Syrjälä 	case HPD_PORT_TC2:
1089da51e4baSVille Syrjälä 	case HPD_PORT_TC3:
1090da51e4baSVille Syrjälä 	case HPD_PORT_TC4:
1091da51e4baSVille Syrjälä 	case HPD_PORT_TC5:
1092da51e4baSVille Syrjälä 	case HPD_PORT_TC6:
10934294fa5fSVille Syrjälä 		return val & GEN11_HOTPLUG_CTL_LONG_DETECT(pin);
109448ef15d3SJosé Roberto de Souza 	default:
109548ef15d3SJosé Roberto de Souza 		return false;
109648ef15d3SJosé Roberto de Souza 	}
109748ef15d3SJosé Roberto de Souza }
109848ef15d3SJosé Roberto de Souza 
1099af92058fSVille Syrjälä static bool bxt_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
110063c88d22SImre Deak {
1101af92058fSVille Syrjälä 	switch (pin) {
1102af92058fSVille Syrjälä 	case HPD_PORT_A:
1103195baa06SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
1104af92058fSVille Syrjälä 	case HPD_PORT_B:
110563c88d22SImre Deak 		return val & PORTB_HOTPLUG_LONG_DETECT;
1106af92058fSVille Syrjälä 	case HPD_PORT_C:
110763c88d22SImre Deak 		return val & PORTC_HOTPLUG_LONG_DETECT;
110863c88d22SImre Deak 	default:
110963c88d22SImre Deak 		return false;
111063c88d22SImre Deak 	}
111163c88d22SImre Deak }
111263c88d22SImre Deak 
1113af92058fSVille Syrjälä static bool icp_ddi_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
111431604222SAnusha Srivatsa {
1115af92058fSVille Syrjälä 	switch (pin) {
1116af92058fSVille Syrjälä 	case HPD_PORT_A:
1117af92058fSVille Syrjälä 	case HPD_PORT_B:
11188ef7e340SMatt Roper 	case HPD_PORT_C:
1119229f31e2SLucas De Marchi 	case HPD_PORT_D:
11204294fa5fSVille Syrjälä 		return val & SHOTPLUG_CTL_DDI_HPD_LONG_DETECT(pin);
112131604222SAnusha Srivatsa 	default:
112231604222SAnusha Srivatsa 		return false;
112331604222SAnusha Srivatsa 	}
112431604222SAnusha Srivatsa }
112531604222SAnusha Srivatsa 
1126af92058fSVille Syrjälä static bool icp_tc_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
112731604222SAnusha Srivatsa {
1128af92058fSVille Syrjälä 	switch (pin) {
1129da51e4baSVille Syrjälä 	case HPD_PORT_TC1:
1130da51e4baSVille Syrjälä 	case HPD_PORT_TC2:
1131da51e4baSVille Syrjälä 	case HPD_PORT_TC3:
1132da51e4baSVille Syrjälä 	case HPD_PORT_TC4:
1133da51e4baSVille Syrjälä 	case HPD_PORT_TC5:
1134da51e4baSVille Syrjälä 	case HPD_PORT_TC6:
11354294fa5fSVille Syrjälä 		return val & ICP_TC_HPD_LONG_DETECT(pin);
113652dfdba0SLucas De Marchi 	default:
113752dfdba0SLucas De Marchi 		return false;
113852dfdba0SLucas De Marchi 	}
113952dfdba0SLucas De Marchi }
114052dfdba0SLucas De Marchi 
1141af92058fSVille Syrjälä static bool spt_port_hotplug2_long_detect(enum hpd_pin pin, u32 val)
11426dbf30ceSVille Syrjälä {
1143af92058fSVille Syrjälä 	switch (pin) {
1144af92058fSVille Syrjälä 	case HPD_PORT_E:
11456dbf30ceSVille Syrjälä 		return val & PORTE_HOTPLUG_LONG_DETECT;
11466dbf30ceSVille Syrjälä 	default:
11476dbf30ceSVille Syrjälä 		return false;
11486dbf30ceSVille Syrjälä 	}
11496dbf30ceSVille Syrjälä }
11506dbf30ceSVille Syrjälä 
1151af92058fSVille Syrjälä static bool spt_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
115274c0b395SVille Syrjälä {
1153af92058fSVille Syrjälä 	switch (pin) {
1154af92058fSVille Syrjälä 	case HPD_PORT_A:
115574c0b395SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
1156af92058fSVille Syrjälä 	case HPD_PORT_B:
115774c0b395SVille Syrjälä 		return val & PORTB_HOTPLUG_LONG_DETECT;
1158af92058fSVille Syrjälä 	case HPD_PORT_C:
115974c0b395SVille Syrjälä 		return val & PORTC_HOTPLUG_LONG_DETECT;
1160af92058fSVille Syrjälä 	case HPD_PORT_D:
116174c0b395SVille Syrjälä 		return val & PORTD_HOTPLUG_LONG_DETECT;
116274c0b395SVille Syrjälä 	default:
116374c0b395SVille Syrjälä 		return false;
116474c0b395SVille Syrjälä 	}
116574c0b395SVille Syrjälä }
116674c0b395SVille Syrjälä 
1167af92058fSVille Syrjälä static bool ilk_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
1168e4ce95aaSVille Syrjälä {
1169af92058fSVille Syrjälä 	switch (pin) {
1170af92058fSVille Syrjälä 	case HPD_PORT_A:
1171e4ce95aaSVille Syrjälä 		return val & DIGITAL_PORTA_HOTPLUG_LONG_DETECT;
1172e4ce95aaSVille Syrjälä 	default:
1173e4ce95aaSVille Syrjälä 		return false;
1174e4ce95aaSVille Syrjälä 	}
1175e4ce95aaSVille Syrjälä }
1176e4ce95aaSVille Syrjälä 
1177af92058fSVille Syrjälä static bool pch_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
117813cf5504SDave Airlie {
1179af92058fSVille Syrjälä 	switch (pin) {
1180af92058fSVille Syrjälä 	case HPD_PORT_B:
1181676574dfSJani Nikula 		return val & PORTB_HOTPLUG_LONG_DETECT;
1182af92058fSVille Syrjälä 	case HPD_PORT_C:
1183676574dfSJani Nikula 		return val & PORTC_HOTPLUG_LONG_DETECT;
1184af92058fSVille Syrjälä 	case HPD_PORT_D:
1185676574dfSJani Nikula 		return val & PORTD_HOTPLUG_LONG_DETECT;
1186676574dfSJani Nikula 	default:
1187676574dfSJani Nikula 		return false;
118813cf5504SDave Airlie 	}
118913cf5504SDave Airlie }
119013cf5504SDave Airlie 
1191af92058fSVille Syrjälä static bool i9xx_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
119213cf5504SDave Airlie {
1193af92058fSVille Syrjälä 	switch (pin) {
1194af92058fSVille Syrjälä 	case HPD_PORT_B:
1195676574dfSJani Nikula 		return val & PORTB_HOTPLUG_INT_LONG_PULSE;
1196af92058fSVille Syrjälä 	case HPD_PORT_C:
1197676574dfSJani Nikula 		return val & PORTC_HOTPLUG_INT_LONG_PULSE;
1198af92058fSVille Syrjälä 	case HPD_PORT_D:
1199676574dfSJani Nikula 		return val & PORTD_HOTPLUG_INT_LONG_PULSE;
1200676574dfSJani Nikula 	default:
1201676574dfSJani Nikula 		return false;
120213cf5504SDave Airlie 	}
120313cf5504SDave Airlie }
120413cf5504SDave Airlie 
120542db67d6SVille Syrjälä /*
120642db67d6SVille Syrjälä  * Get a bit mask of pins that have triggered, and which ones may be long.
120742db67d6SVille Syrjälä  * This can be called multiple times with the same masks to accumulate
120842db67d6SVille Syrjälä  * hotplug detection results from several registers.
120942db67d6SVille Syrjälä  *
121042db67d6SVille Syrjälä  * Note that the caller is expected to zero out the masks initially.
121142db67d6SVille Syrjälä  */
1212cf53902fSRodrigo Vivi static void intel_get_hpd_pins(struct drm_i915_private *dev_priv,
1213cf53902fSRodrigo Vivi 			       u32 *pin_mask, u32 *long_mask,
12148c841e57SJani Nikula 			       u32 hotplug_trigger, u32 dig_hotplug_reg,
1215fd63e2a9SImre Deak 			       const u32 hpd[HPD_NUM_PINS],
1216af92058fSVille Syrjälä 			       bool long_pulse_detect(enum hpd_pin pin, u32 val))
1217676574dfSJani Nikula {
1218e9be2850SVille Syrjälä 	enum hpd_pin pin;
1219676574dfSJani Nikula 
122052dfdba0SLucas De Marchi 	BUILD_BUG_ON(BITS_PER_TYPE(*pin_mask) < HPD_NUM_PINS);
122152dfdba0SLucas De Marchi 
1222e9be2850SVille Syrjälä 	for_each_hpd_pin(pin) {
1223e9be2850SVille Syrjälä 		if ((hpd[pin] & hotplug_trigger) == 0)
12248c841e57SJani Nikula 			continue;
12258c841e57SJani Nikula 
1226e9be2850SVille Syrjälä 		*pin_mask |= BIT(pin);
1227676574dfSJani Nikula 
1228af92058fSVille Syrjälä 		if (long_pulse_detect(pin, dig_hotplug_reg))
1229e9be2850SVille Syrjälä 			*long_mask |= BIT(pin);
1230676574dfSJani Nikula 	}
1231676574dfSJani Nikula 
123200376ccfSWambui Karuga 	drm_dbg(&dev_priv->drm,
123300376ccfSWambui Karuga 		"hotplug event received, stat 0x%08x, dig 0x%08x, pins 0x%08x, long 0x%08x\n",
1234f88f0478SVille Syrjälä 		hotplug_trigger, dig_hotplug_reg, *pin_mask, *long_mask);
1235676574dfSJani Nikula 
1236676574dfSJani Nikula }
1237676574dfSJani Nikula 
1238a0e066b8SVille Syrjälä static u32 intel_hpd_enabled_irqs(struct drm_i915_private *dev_priv,
1239a0e066b8SVille Syrjälä 				  const u32 hpd[HPD_NUM_PINS])
1240a0e066b8SVille Syrjälä {
1241a0e066b8SVille Syrjälä 	struct intel_encoder *encoder;
1242a0e066b8SVille Syrjälä 	u32 enabled_irqs = 0;
1243a0e066b8SVille Syrjälä 
1244a0e066b8SVille Syrjälä 	for_each_intel_encoder(&dev_priv->drm, encoder)
1245a0e066b8SVille Syrjälä 		if (dev_priv->hotplug.stats[encoder->hpd_pin].state == HPD_ENABLED)
1246a0e066b8SVille Syrjälä 			enabled_irqs |= hpd[encoder->hpd_pin];
1247a0e066b8SVille Syrjälä 
1248a0e066b8SVille Syrjälä 	return enabled_irqs;
1249a0e066b8SVille Syrjälä }
1250a0e066b8SVille Syrjälä 
1251a0e066b8SVille Syrjälä static u32 intel_hpd_hotplug_irqs(struct drm_i915_private *dev_priv,
1252a0e066b8SVille Syrjälä 				  const u32 hpd[HPD_NUM_PINS])
1253a0e066b8SVille Syrjälä {
1254a0e066b8SVille Syrjälä 	struct intel_encoder *encoder;
1255a0e066b8SVille Syrjälä 	u32 hotplug_irqs = 0;
1256a0e066b8SVille Syrjälä 
1257a0e066b8SVille Syrjälä 	for_each_intel_encoder(&dev_priv->drm, encoder)
1258a0e066b8SVille Syrjälä 		hotplug_irqs |= hpd[encoder->hpd_pin];
1259a0e066b8SVille Syrjälä 
1260a0e066b8SVille Syrjälä 	return hotplug_irqs;
1261a0e066b8SVille Syrjälä }
1262a0e066b8SVille Syrjälä 
12632ea63927SVille Syrjälä static u32 intel_hpd_hotplug_enables(struct drm_i915_private *i915,
12642ea63927SVille Syrjälä 				     hotplug_enables_func hotplug_enables)
12652ea63927SVille Syrjälä {
12662ea63927SVille Syrjälä 	struct intel_encoder *encoder;
12672ea63927SVille Syrjälä 	u32 hotplug = 0;
12682ea63927SVille Syrjälä 
12692ea63927SVille Syrjälä 	for_each_intel_encoder(&i915->drm, encoder)
12702ea63927SVille Syrjälä 		hotplug |= hotplug_enables(i915, encoder->hpd_pin);
12712ea63927SVille Syrjälä 
12722ea63927SVille Syrjälä 	return hotplug;
12732ea63927SVille Syrjälä }
12742ea63927SVille Syrjälä 
127591d14251STvrtko Ursulin static void gmbus_irq_handler(struct drm_i915_private *dev_priv)
1276515ac2bbSDaniel Vetter {
127728c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1278515ac2bbSDaniel Vetter }
1279515ac2bbSDaniel Vetter 
128091d14251STvrtko Ursulin static void dp_aux_irq_handler(struct drm_i915_private *dev_priv)
1281ce99c256SDaniel Vetter {
12829ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1283ce99c256SDaniel Vetter }
1284ce99c256SDaniel Vetter 
12858bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
128691d14251STvrtko Ursulin static void display_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
128791d14251STvrtko Ursulin 					 enum pipe pipe,
1288a9c287c9SJani Nikula 					 u32 crc0, u32 crc1,
1289a9c287c9SJani Nikula 					 u32 crc2, u32 crc3,
1290a9c287c9SJani Nikula 					 u32 crc4)
12918bf1e9f1SShuang He {
12928c6b709dSTomeu Vizoso 	struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
129300535527SJani Nikula 	struct intel_pipe_crc *pipe_crc = &crtc->pipe_crc;
12945cee6c45SVille Syrjälä 	u32 crcs[5] = { crc0, crc1, crc2, crc3, crc4 };
12955cee6c45SVille Syrjälä 
12965cee6c45SVille Syrjälä 	trace_intel_pipe_crc(crtc, crcs);
1297b2c88f5bSDamien Lespiau 
1298d538bbdfSDamien Lespiau 	spin_lock(&pipe_crc->lock);
12998c6b709dSTomeu Vizoso 	/*
13008c6b709dSTomeu Vizoso 	 * For some not yet identified reason, the first CRC is
13018c6b709dSTomeu Vizoso 	 * bonkers. So let's just wait for the next vblank and read
13028c6b709dSTomeu Vizoso 	 * out the buggy result.
13038c6b709dSTomeu Vizoso 	 *
1304163e8aecSRodrigo Vivi 	 * On GEN8+ sometimes the second CRC is bonkers as well, so
13058c6b709dSTomeu Vizoso 	 * don't trust that one either.
13068c6b709dSTomeu Vizoso 	 */
1307033b7a23SMaarten Lankhorst 	if (pipe_crc->skipped <= 0 ||
1308163e8aecSRodrigo Vivi 	    (INTEL_GEN(dev_priv) >= 8 && pipe_crc->skipped == 1)) {
13098c6b709dSTomeu Vizoso 		pipe_crc->skipped++;
13108c6b709dSTomeu Vizoso 		spin_unlock(&pipe_crc->lock);
13118c6b709dSTomeu Vizoso 		return;
13128c6b709dSTomeu Vizoso 	}
13138c6b709dSTomeu Vizoso 	spin_unlock(&pipe_crc->lock);
13146cc42152SMaarten Lankhorst 
1315246ee524STomeu Vizoso 	drm_crtc_add_crc_entry(&crtc->base, true,
1316ca814b25SDaniel Vetter 				drm_crtc_accurate_vblank_count(&crtc->base),
1317246ee524STomeu Vizoso 				crcs);
13188c6b709dSTomeu Vizoso }
1319277de95eSDaniel Vetter #else
1320277de95eSDaniel Vetter static inline void
132191d14251STvrtko Ursulin display_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
132291d14251STvrtko Ursulin 			     enum pipe pipe,
1323a9c287c9SJani Nikula 			     u32 crc0, u32 crc1,
1324a9c287c9SJani Nikula 			     u32 crc2, u32 crc3,
1325a9c287c9SJani Nikula 			     u32 crc4) {}
1326277de95eSDaniel Vetter #endif
1327eba94eb9SDaniel Vetter 
13281288f9b0SKarthik B S static void flip_done_handler(struct drm_i915_private *i915,
13291288f9b0SKarthik B S 			      enum pipe pipe)
13301288f9b0SKarthik B S {
13311288f9b0SKarthik B S 	struct intel_crtc *crtc = intel_get_crtc_for_pipe(i915, pipe);
13321288f9b0SKarthik B S 	struct drm_crtc_state *crtc_state = crtc->base.state;
13331288f9b0SKarthik B S 	struct drm_pending_vblank_event *e = crtc_state->event;
13341288f9b0SKarthik B S 	struct drm_device *dev = &i915->drm;
13351288f9b0SKarthik B S 	unsigned long irqflags;
13361288f9b0SKarthik B S 
13371288f9b0SKarthik B S 	spin_lock_irqsave(&dev->event_lock, irqflags);
13381288f9b0SKarthik B S 
13391288f9b0SKarthik B S 	crtc_state->event = NULL;
13401288f9b0SKarthik B S 
13411288f9b0SKarthik B S 	drm_crtc_send_vblank_event(&crtc->base, e);
13421288f9b0SKarthik B S 
13431288f9b0SKarthik B S 	spin_unlock_irqrestore(&dev->event_lock, irqflags);
13441288f9b0SKarthik B S }
1345277de95eSDaniel Vetter 
134691d14251STvrtko Ursulin static void hsw_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
134791d14251STvrtko Ursulin 				     enum pipe pipe)
13485a69b89fSDaniel Vetter {
134991d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
13502939eb06SJani Nikula 				     intel_uncore_read(&dev_priv->uncore, PIPE_CRC_RES_1_IVB(pipe)),
13515a69b89fSDaniel Vetter 				     0, 0, 0, 0);
13525a69b89fSDaniel Vetter }
13535a69b89fSDaniel Vetter 
135491d14251STvrtko Ursulin static void ivb_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
135591d14251STvrtko Ursulin 				     enum pipe pipe)
1356eba94eb9SDaniel Vetter {
135791d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
13582939eb06SJani Nikula 				     intel_uncore_read(&dev_priv->uncore, PIPE_CRC_RES_1_IVB(pipe)),
13592939eb06SJani Nikula 				     intel_uncore_read(&dev_priv->uncore, PIPE_CRC_RES_2_IVB(pipe)),
13602939eb06SJani Nikula 				     intel_uncore_read(&dev_priv->uncore, PIPE_CRC_RES_3_IVB(pipe)),
13612939eb06SJani Nikula 				     intel_uncore_read(&dev_priv->uncore, PIPE_CRC_RES_4_IVB(pipe)),
13622939eb06SJani Nikula 				     intel_uncore_read(&dev_priv->uncore, PIPE_CRC_RES_5_IVB(pipe)));
1363eba94eb9SDaniel Vetter }
13645b3a856bSDaniel Vetter 
136591d14251STvrtko Ursulin static void i9xx_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
136691d14251STvrtko Ursulin 				      enum pipe pipe)
13675b3a856bSDaniel Vetter {
1368a9c287c9SJani Nikula 	u32 res1, res2;
13690b5c5ed0SDaniel Vetter 
137091d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 3)
13712939eb06SJani Nikula 		res1 = intel_uncore_read(&dev_priv->uncore, PIPE_CRC_RES_RES1_I915(pipe));
13720b5c5ed0SDaniel Vetter 	else
13730b5c5ed0SDaniel Vetter 		res1 = 0;
13740b5c5ed0SDaniel Vetter 
137591d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
13762939eb06SJani Nikula 		res2 = intel_uncore_read(&dev_priv->uncore, PIPE_CRC_RES_RES2_G4X(pipe));
13770b5c5ed0SDaniel Vetter 	else
13780b5c5ed0SDaniel Vetter 		res2 = 0;
13795b3a856bSDaniel Vetter 
138091d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
13812939eb06SJani Nikula 				     intel_uncore_read(&dev_priv->uncore, PIPE_CRC_RES_RED(pipe)),
13822939eb06SJani Nikula 				     intel_uncore_read(&dev_priv->uncore, PIPE_CRC_RES_GREEN(pipe)),
13832939eb06SJani Nikula 				     intel_uncore_read(&dev_priv->uncore, PIPE_CRC_RES_BLUE(pipe)),
13840b5c5ed0SDaniel Vetter 				     res1, res2);
13855b3a856bSDaniel Vetter }
13868bf1e9f1SShuang He 
138744d9241eSVille Syrjälä static void i9xx_pipestat_irq_reset(struct drm_i915_private *dev_priv)
138844d9241eSVille Syrjälä {
138944d9241eSVille Syrjälä 	enum pipe pipe;
139044d9241eSVille Syrjälä 
139144d9241eSVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
13922939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, PIPESTAT(pipe),
139344d9241eSVille Syrjälä 			   PIPESTAT_INT_STATUS_MASK |
139444d9241eSVille Syrjälä 			   PIPE_FIFO_UNDERRUN_STATUS);
139544d9241eSVille Syrjälä 
139644d9241eSVille Syrjälä 		dev_priv->pipestat_irq_mask[pipe] = 0;
139744d9241eSVille Syrjälä 	}
139844d9241eSVille Syrjälä }
139944d9241eSVille Syrjälä 
1400eb64343cSVille Syrjälä static void i9xx_pipestat_irq_ack(struct drm_i915_private *dev_priv,
140191d14251STvrtko Ursulin 				  u32 iir, u32 pipe_stats[I915_MAX_PIPES])
14027e231dbeSJesse Barnes {
1403d048a268SVille Syrjälä 	enum pipe pipe;
14047e231dbeSJesse Barnes 
140558ead0d7SImre Deak 	spin_lock(&dev_priv->irq_lock);
14061ca993d2SVille Syrjälä 
14071ca993d2SVille Syrjälä 	if (!dev_priv->display_irqs_enabled) {
14081ca993d2SVille Syrjälä 		spin_unlock(&dev_priv->irq_lock);
14091ca993d2SVille Syrjälä 		return;
14101ca993d2SVille Syrjälä 	}
14111ca993d2SVille Syrjälä 
1412055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1413f0f59a00SVille Syrjälä 		i915_reg_t reg;
14146b12ca56SVille Syrjälä 		u32 status_mask, enable_mask, iir_bit = 0;
141591d181ddSImre Deak 
1416bbb5eebfSDaniel Vetter 		/*
1417bbb5eebfSDaniel Vetter 		 * PIPESTAT bits get signalled even when the interrupt is
1418bbb5eebfSDaniel Vetter 		 * disabled with the mask bits, and some of the status bits do
1419bbb5eebfSDaniel Vetter 		 * not generate interrupts at all (like the underrun bit). Hence
1420bbb5eebfSDaniel Vetter 		 * we need to be careful that we only handle what we want to
1421bbb5eebfSDaniel Vetter 		 * handle.
1422bbb5eebfSDaniel Vetter 		 */
14230f239f4cSDaniel Vetter 
14240f239f4cSDaniel Vetter 		/* fifo underruns are filterered in the underrun handler. */
14256b12ca56SVille Syrjälä 		status_mask = PIPE_FIFO_UNDERRUN_STATUS;
1426bbb5eebfSDaniel Vetter 
1427bbb5eebfSDaniel Vetter 		switch (pipe) {
1428d048a268SVille Syrjälä 		default:
1429bbb5eebfSDaniel Vetter 		case PIPE_A:
1430bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
1431bbb5eebfSDaniel Vetter 			break;
1432bbb5eebfSDaniel Vetter 		case PIPE_B:
1433bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
1434bbb5eebfSDaniel Vetter 			break;
14353278f67fSVille Syrjälä 		case PIPE_C:
14363278f67fSVille Syrjälä 			iir_bit = I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
14373278f67fSVille Syrjälä 			break;
1438bbb5eebfSDaniel Vetter 		}
1439bbb5eebfSDaniel Vetter 		if (iir & iir_bit)
14406b12ca56SVille Syrjälä 			status_mask |= dev_priv->pipestat_irq_mask[pipe];
1441bbb5eebfSDaniel Vetter 
14426b12ca56SVille Syrjälä 		if (!status_mask)
144391d181ddSImre Deak 			continue;
144491d181ddSImre Deak 
144591d181ddSImre Deak 		reg = PIPESTAT(pipe);
14462939eb06SJani Nikula 		pipe_stats[pipe] = intel_uncore_read(&dev_priv->uncore, reg) & status_mask;
14476b12ca56SVille Syrjälä 		enable_mask = i915_pipestat_enable_mask(dev_priv, pipe);
14487e231dbeSJesse Barnes 
14497e231dbeSJesse Barnes 		/*
14507e231dbeSJesse Barnes 		 * Clear the PIPE*STAT regs before the IIR
1451132c27c9SVille Syrjälä 		 *
1452132c27c9SVille Syrjälä 		 * Toggle the enable bits to make sure we get an
1453132c27c9SVille Syrjälä 		 * edge in the ISR pipe event bit if we don't clear
1454132c27c9SVille Syrjälä 		 * all the enabled status bits. Otherwise the edge
1455132c27c9SVille Syrjälä 		 * triggered IIR on i965/g4x wouldn't notice that
1456132c27c9SVille Syrjälä 		 * an interrupt is still pending.
14577e231dbeSJesse Barnes 		 */
1458132c27c9SVille Syrjälä 		if (pipe_stats[pipe]) {
14592939eb06SJani Nikula 			intel_uncore_write(&dev_priv->uncore, reg, pipe_stats[pipe]);
14602939eb06SJani Nikula 			intel_uncore_write(&dev_priv->uncore, reg, enable_mask);
1461132c27c9SVille Syrjälä 		}
14627e231dbeSJesse Barnes 	}
146358ead0d7SImre Deak 	spin_unlock(&dev_priv->irq_lock);
14642ecb8ca4SVille Syrjälä }
14652ecb8ca4SVille Syrjälä 
1466eb64343cSVille Syrjälä static void i8xx_pipestat_irq_handler(struct drm_i915_private *dev_priv,
1467eb64343cSVille Syrjälä 				      u16 iir, u32 pipe_stats[I915_MAX_PIPES])
1468eb64343cSVille Syrjälä {
1469eb64343cSVille Syrjälä 	enum pipe pipe;
1470eb64343cSVille Syrjälä 
1471eb64343cSVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
1472eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
1473aca9310aSAnshuman Gupta 			intel_handle_vblank(dev_priv, pipe);
1474eb64343cSVille Syrjälä 
1475eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1476eb64343cSVille Syrjälä 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
1477eb64343cSVille Syrjälä 
1478eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
1479eb64343cSVille Syrjälä 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
1480eb64343cSVille Syrjälä 	}
1481eb64343cSVille Syrjälä }
1482eb64343cSVille Syrjälä 
1483eb64343cSVille Syrjälä static void i915_pipestat_irq_handler(struct drm_i915_private *dev_priv,
1484eb64343cSVille Syrjälä 				      u32 iir, u32 pipe_stats[I915_MAX_PIPES])
1485eb64343cSVille Syrjälä {
1486eb64343cSVille Syrjälä 	bool blc_event = false;
1487eb64343cSVille Syrjälä 	enum pipe pipe;
1488eb64343cSVille Syrjälä 
1489eb64343cSVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
1490eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
1491aca9310aSAnshuman Gupta 			intel_handle_vblank(dev_priv, pipe);
1492eb64343cSVille Syrjälä 
1493eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
1494eb64343cSVille Syrjälä 			blc_event = true;
1495eb64343cSVille Syrjälä 
1496eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1497eb64343cSVille Syrjälä 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
1498eb64343cSVille Syrjälä 
1499eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
1500eb64343cSVille Syrjälä 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
1501eb64343cSVille Syrjälä 	}
1502eb64343cSVille Syrjälä 
1503eb64343cSVille Syrjälä 	if (blc_event || (iir & I915_ASLE_INTERRUPT))
1504eb64343cSVille Syrjälä 		intel_opregion_asle_intr(dev_priv);
1505eb64343cSVille Syrjälä }
1506eb64343cSVille Syrjälä 
1507eb64343cSVille Syrjälä static void i965_pipestat_irq_handler(struct drm_i915_private *dev_priv,
1508eb64343cSVille Syrjälä 				      u32 iir, u32 pipe_stats[I915_MAX_PIPES])
1509eb64343cSVille Syrjälä {
1510eb64343cSVille Syrjälä 	bool blc_event = false;
1511eb64343cSVille Syrjälä 	enum pipe pipe;
1512eb64343cSVille Syrjälä 
1513eb64343cSVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
1514eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS)
1515aca9310aSAnshuman Gupta 			intel_handle_vblank(dev_priv, pipe);
1516eb64343cSVille Syrjälä 
1517eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
1518eb64343cSVille Syrjälä 			blc_event = true;
1519eb64343cSVille Syrjälä 
1520eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1521eb64343cSVille Syrjälä 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
1522eb64343cSVille Syrjälä 
1523eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
1524eb64343cSVille Syrjälä 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
1525eb64343cSVille Syrjälä 	}
1526eb64343cSVille Syrjälä 
1527eb64343cSVille Syrjälä 	if (blc_event || (iir & I915_ASLE_INTERRUPT))
1528eb64343cSVille Syrjälä 		intel_opregion_asle_intr(dev_priv);
1529eb64343cSVille Syrjälä 
1530eb64343cSVille Syrjälä 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1531eb64343cSVille Syrjälä 		gmbus_irq_handler(dev_priv);
1532eb64343cSVille Syrjälä }
1533eb64343cSVille Syrjälä 
153491d14251STvrtko Ursulin static void valleyview_pipestat_irq_handler(struct drm_i915_private *dev_priv,
15352ecb8ca4SVille Syrjälä 					    u32 pipe_stats[I915_MAX_PIPES])
15362ecb8ca4SVille Syrjälä {
15372ecb8ca4SVille Syrjälä 	enum pipe pipe;
15387e231dbeSJesse Barnes 
1539055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1540fd3a4024SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS)
1541aca9310aSAnshuman Gupta 			intel_handle_vblank(dev_priv, pipe);
15424356d586SDaniel Vetter 
15434356d586SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
154491d14251STvrtko Ursulin 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
15452d9d2b0bSVille Syrjälä 
15461f7247c0SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
15471f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
154831acc7f5SJesse Barnes 	}
154931acc7f5SJesse Barnes 
1550c1874ed7SImre Deak 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
155191d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
1552c1874ed7SImre Deak }
1553c1874ed7SImre Deak 
15541ae3c34cSVille Syrjälä static u32 i9xx_hpd_irq_ack(struct drm_i915_private *dev_priv)
155516c6c56bSVille Syrjälä {
15560ba7c51aSVille Syrjälä 	u32 hotplug_status = 0, hotplug_status_mask;
15570ba7c51aSVille Syrjälä 	int i;
155816c6c56bSVille Syrjälä 
15590ba7c51aSVille Syrjälä 	if (IS_G4X(dev_priv) ||
15600ba7c51aSVille Syrjälä 	    IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
15610ba7c51aSVille Syrjälä 		hotplug_status_mask = HOTPLUG_INT_STATUS_G4X |
15620ba7c51aSVille Syrjälä 			DP_AUX_CHANNEL_MASK_INT_STATUS_G4X;
15630ba7c51aSVille Syrjälä 	else
15640ba7c51aSVille Syrjälä 		hotplug_status_mask = HOTPLUG_INT_STATUS_I915;
15650ba7c51aSVille Syrjälä 
15660ba7c51aSVille Syrjälä 	/*
15670ba7c51aSVille Syrjälä 	 * We absolutely have to clear all the pending interrupt
15680ba7c51aSVille Syrjälä 	 * bits in PORT_HOTPLUG_STAT. Otherwise the ISR port
15690ba7c51aSVille Syrjälä 	 * interrupt bit won't have an edge, and the i965/g4x
15700ba7c51aSVille Syrjälä 	 * edge triggered IIR will not notice that an interrupt
15710ba7c51aSVille Syrjälä 	 * is still pending. We can't use PORT_HOTPLUG_EN to
15720ba7c51aSVille Syrjälä 	 * guarantee the edge as the act of toggling the enable
15730ba7c51aSVille Syrjälä 	 * bits can itself generate a new hotplug interrupt :(
15740ba7c51aSVille Syrjälä 	 */
15750ba7c51aSVille Syrjälä 	for (i = 0; i < 10; i++) {
15762939eb06SJani Nikula 		u32 tmp = intel_uncore_read(&dev_priv->uncore, PORT_HOTPLUG_STAT) & hotplug_status_mask;
15770ba7c51aSVille Syrjälä 
15780ba7c51aSVille Syrjälä 		if (tmp == 0)
15790ba7c51aSVille Syrjälä 			return hotplug_status;
15800ba7c51aSVille Syrjälä 
15810ba7c51aSVille Syrjälä 		hotplug_status |= tmp;
15822939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, PORT_HOTPLUG_STAT, hotplug_status);
15830ba7c51aSVille Syrjälä 	}
15840ba7c51aSVille Syrjälä 
158548a1b8d4SPankaj Bharadiya 	drm_WARN_ONCE(&dev_priv->drm, 1,
15860ba7c51aSVille Syrjälä 		      "PORT_HOTPLUG_STAT did not clear (0x%08x)\n",
15872939eb06SJani Nikula 		      intel_uncore_read(&dev_priv->uncore, PORT_HOTPLUG_STAT));
15881ae3c34cSVille Syrjälä 
15891ae3c34cSVille Syrjälä 	return hotplug_status;
15901ae3c34cSVille Syrjälä }
15911ae3c34cSVille Syrjälä 
159291d14251STvrtko Ursulin static void i9xx_hpd_irq_handler(struct drm_i915_private *dev_priv,
15931ae3c34cSVille Syrjälä 				 u32 hotplug_status)
15941ae3c34cSVille Syrjälä {
15951ae3c34cSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
15960398993bSVille Syrjälä 	u32 hotplug_trigger;
15973ff60f89SOscar Mateo 
15980398993bSVille Syrjälä 	if (IS_G4X(dev_priv) ||
15990398993bSVille Syrjälä 	    IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
16000398993bSVille Syrjälä 		hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
16010398993bSVille Syrjälä 	else
16020398993bSVille Syrjälä 		hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
160316c6c56bSVille Syrjälä 
160458f2cf24SVille Syrjälä 	if (hotplug_trigger) {
1605cf53902fSRodrigo Vivi 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
1606cf53902fSRodrigo Vivi 				   hotplug_trigger, hotplug_trigger,
16070398993bSVille Syrjälä 				   dev_priv->hotplug.hpd,
1608fd63e2a9SImre Deak 				   i9xx_port_hotplug_long_detect);
160958f2cf24SVille Syrjälä 
161091d14251STvrtko Ursulin 		intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
161158f2cf24SVille Syrjälä 	}
1612369712e8SJani Nikula 
16130398993bSVille Syrjälä 	if ((IS_G4X(dev_priv) ||
16140398993bSVille Syrjälä 	     IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
16150398993bSVille Syrjälä 	    hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
161691d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
161758f2cf24SVille Syrjälä }
161816c6c56bSVille Syrjälä 
1619c1874ed7SImre Deak static irqreturn_t valleyview_irq_handler(int irq, void *arg)
1620c1874ed7SImre Deak {
1621b318b824SVille Syrjälä 	struct drm_i915_private *dev_priv = arg;
1622c1874ed7SImre Deak 	irqreturn_t ret = IRQ_NONE;
1623c1874ed7SImre Deak 
16242dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
16252dd2a883SImre Deak 		return IRQ_NONE;
16262dd2a883SImre Deak 
16271f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
16289102650fSDaniele Ceraolo Spurio 	disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
16291f814dacSImre Deak 
16301e1cace9SVille Syrjälä 	do {
16316e814800SVille Syrjälä 		u32 iir, gt_iir, pm_iir;
16322ecb8ca4SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
16331ae3c34cSVille Syrjälä 		u32 hotplug_status = 0;
1634a5e485a9SVille Syrjälä 		u32 ier = 0;
16353ff60f89SOscar Mateo 
16362939eb06SJani Nikula 		gt_iir = intel_uncore_read(&dev_priv->uncore, GTIIR);
16372939eb06SJani Nikula 		pm_iir = intel_uncore_read(&dev_priv->uncore, GEN6_PMIIR);
16382939eb06SJani Nikula 		iir = intel_uncore_read(&dev_priv->uncore, VLV_IIR);
1639c1874ed7SImre Deak 
1640c1874ed7SImre Deak 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
16411e1cace9SVille Syrjälä 			break;
1642c1874ed7SImre Deak 
1643c1874ed7SImre Deak 		ret = IRQ_HANDLED;
1644c1874ed7SImre Deak 
1645a5e485a9SVille Syrjälä 		/*
1646a5e485a9SVille Syrjälä 		 * Theory on interrupt generation, based on empirical evidence:
1647a5e485a9SVille Syrjälä 		 *
1648a5e485a9SVille Syrjälä 		 * x = ((VLV_IIR & VLV_IER) ||
1649a5e485a9SVille Syrjälä 		 *      (((GT_IIR & GT_IER) || (GEN6_PMIIR & GEN6_PMIER)) &&
1650a5e485a9SVille Syrjälä 		 *       (VLV_MASTER_IER & MASTER_INTERRUPT_ENABLE)));
1651a5e485a9SVille Syrjälä 		 *
1652a5e485a9SVille Syrjälä 		 * A CPU interrupt will only be raised when 'x' has a 0->1 edge.
1653a5e485a9SVille Syrjälä 		 * Hence we clear MASTER_INTERRUPT_ENABLE and VLV_IER to
1654a5e485a9SVille Syrjälä 		 * guarantee the CPU interrupt will be raised again even if we
1655a5e485a9SVille Syrjälä 		 * don't end up clearing all the VLV_IIR, GT_IIR, GEN6_PMIIR
1656a5e485a9SVille Syrjälä 		 * bits this time around.
1657a5e485a9SVille Syrjälä 		 */
16582939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, VLV_MASTER_IER, 0);
16592939eb06SJani Nikula 		ier = intel_uncore_read(&dev_priv->uncore, VLV_IER);
16602939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, VLV_IER, 0);
16614a0a0202SVille Syrjälä 
16624a0a0202SVille Syrjälä 		if (gt_iir)
16632939eb06SJani Nikula 			intel_uncore_write(&dev_priv->uncore, GTIIR, gt_iir);
16644a0a0202SVille Syrjälä 		if (pm_iir)
16652939eb06SJani Nikula 			intel_uncore_write(&dev_priv->uncore, GEN6_PMIIR, pm_iir);
16664a0a0202SVille Syrjälä 
16677ce4d1f2SVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
16681ae3c34cSVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
16697ce4d1f2SVille Syrjälä 
16703ff60f89SOscar Mateo 		/* Call regardless, as some status bits might not be
16713ff60f89SOscar Mateo 		 * signalled in iir */
1672eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
16737ce4d1f2SVille Syrjälä 
1674eef57324SJerome Anand 		if (iir & (I915_LPE_PIPE_A_INTERRUPT |
1675eef57324SJerome Anand 			   I915_LPE_PIPE_B_INTERRUPT))
1676eef57324SJerome Anand 			intel_lpe_audio_irq_handler(dev_priv);
1677eef57324SJerome Anand 
16787ce4d1f2SVille Syrjälä 		/*
16797ce4d1f2SVille Syrjälä 		 * VLV_IIR is single buffered, and reflects the level
16807ce4d1f2SVille Syrjälä 		 * from PIPESTAT/PORT_HOTPLUG_STAT, hence clear it last.
16817ce4d1f2SVille Syrjälä 		 */
16827ce4d1f2SVille Syrjälä 		if (iir)
16832939eb06SJani Nikula 			intel_uncore_write(&dev_priv->uncore, VLV_IIR, iir);
16844a0a0202SVille Syrjälä 
16852939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, VLV_IER, ier);
16862939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
16871ae3c34cSVille Syrjälä 
168852894874SVille Syrjälä 		if (gt_iir)
1689cf1c97dcSAndi Shyti 			gen6_gt_irq_handler(&dev_priv->gt, gt_iir);
169052894874SVille Syrjälä 		if (pm_iir)
16913e7abf81SAndi Shyti 			gen6_rps_irq_handler(&dev_priv->gt.rps, pm_iir);
169252894874SVille Syrjälä 
16931ae3c34cSVille Syrjälä 		if (hotplug_status)
169491d14251STvrtko Ursulin 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
16952ecb8ca4SVille Syrjälä 
169691d14251STvrtko Ursulin 		valleyview_pipestat_irq_handler(dev_priv, pipe_stats);
16971e1cace9SVille Syrjälä 	} while (0);
16987e231dbeSJesse Barnes 
16999c6508b9SThomas Gleixner 	pmu_irq_stats(dev_priv, ret);
17009c6508b9SThomas Gleixner 
17019102650fSDaniele Ceraolo Spurio 	enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
17021f814dacSImre Deak 
17037e231dbeSJesse Barnes 	return ret;
17047e231dbeSJesse Barnes }
17057e231dbeSJesse Barnes 
170643f328d7SVille Syrjälä static irqreturn_t cherryview_irq_handler(int irq, void *arg)
170743f328d7SVille Syrjälä {
1708b318b824SVille Syrjälä 	struct drm_i915_private *dev_priv = arg;
170943f328d7SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
171043f328d7SVille Syrjälä 
17112dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
17122dd2a883SImre Deak 		return IRQ_NONE;
17132dd2a883SImre Deak 
17141f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
17159102650fSDaniele Ceraolo Spurio 	disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
17161f814dacSImre Deak 
1717579de73bSChris Wilson 	do {
17186e814800SVille Syrjälä 		u32 master_ctl, iir;
17192ecb8ca4SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
17201ae3c34cSVille Syrjälä 		u32 hotplug_status = 0;
1721a5e485a9SVille Syrjälä 		u32 ier = 0;
1722a5e485a9SVille Syrjälä 
17232939eb06SJani Nikula 		master_ctl = intel_uncore_read(&dev_priv->uncore, GEN8_MASTER_IRQ) & ~GEN8_MASTER_IRQ_CONTROL;
17242939eb06SJani Nikula 		iir = intel_uncore_read(&dev_priv->uncore, VLV_IIR);
17253278f67fSVille Syrjälä 
17263278f67fSVille Syrjälä 		if (master_ctl == 0 && iir == 0)
17278e5fd599SVille Syrjälä 			break;
172843f328d7SVille Syrjälä 
172927b6c122SOscar Mateo 		ret = IRQ_HANDLED;
173027b6c122SOscar Mateo 
1731a5e485a9SVille Syrjälä 		/*
1732a5e485a9SVille Syrjälä 		 * Theory on interrupt generation, based on empirical evidence:
1733a5e485a9SVille Syrjälä 		 *
1734a5e485a9SVille Syrjälä 		 * x = ((VLV_IIR & VLV_IER) ||
1735a5e485a9SVille Syrjälä 		 *      ((GEN8_MASTER_IRQ & ~GEN8_MASTER_IRQ_CONTROL) &&
1736a5e485a9SVille Syrjälä 		 *       (GEN8_MASTER_IRQ & GEN8_MASTER_IRQ_CONTROL)));
1737a5e485a9SVille Syrjälä 		 *
1738a5e485a9SVille Syrjälä 		 * A CPU interrupt will only be raised when 'x' has a 0->1 edge.
1739a5e485a9SVille Syrjälä 		 * Hence we clear GEN8_MASTER_IRQ_CONTROL and VLV_IER to
1740a5e485a9SVille Syrjälä 		 * guarantee the CPU interrupt will be raised again even if we
1741a5e485a9SVille Syrjälä 		 * don't end up clearing all the VLV_IIR and GEN8_MASTER_IRQ_CONTROL
1742a5e485a9SVille Syrjälä 		 * bits this time around.
1743a5e485a9SVille Syrjälä 		 */
17442939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, GEN8_MASTER_IRQ, 0);
17452939eb06SJani Nikula 		ier = intel_uncore_read(&dev_priv->uncore, VLV_IER);
17462939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, VLV_IER, 0);
174743f328d7SVille Syrjälä 
17486cc32f15SChris Wilson 		gen8_gt_irq_handler(&dev_priv->gt, master_ctl);
174927b6c122SOscar Mateo 
175027b6c122SOscar Mateo 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
17511ae3c34cSVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
175243f328d7SVille Syrjälä 
175327b6c122SOscar Mateo 		/* Call regardless, as some status bits might not be
175427b6c122SOscar Mateo 		 * signalled in iir */
1755eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
175643f328d7SVille Syrjälä 
1757eef57324SJerome Anand 		if (iir & (I915_LPE_PIPE_A_INTERRUPT |
1758eef57324SJerome Anand 			   I915_LPE_PIPE_B_INTERRUPT |
1759eef57324SJerome Anand 			   I915_LPE_PIPE_C_INTERRUPT))
1760eef57324SJerome Anand 			intel_lpe_audio_irq_handler(dev_priv);
1761eef57324SJerome Anand 
17627ce4d1f2SVille Syrjälä 		/*
17637ce4d1f2SVille Syrjälä 		 * VLV_IIR is single buffered, and reflects the level
17647ce4d1f2SVille Syrjälä 		 * from PIPESTAT/PORT_HOTPLUG_STAT, hence clear it last.
17657ce4d1f2SVille Syrjälä 		 */
17667ce4d1f2SVille Syrjälä 		if (iir)
17672939eb06SJani Nikula 			intel_uncore_write(&dev_priv->uncore, VLV_IIR, iir);
17687ce4d1f2SVille Syrjälä 
17692939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, VLV_IER, ier);
17702939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
17711ae3c34cSVille Syrjälä 
17721ae3c34cSVille Syrjälä 		if (hotplug_status)
177391d14251STvrtko Ursulin 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
17742ecb8ca4SVille Syrjälä 
177591d14251STvrtko Ursulin 		valleyview_pipestat_irq_handler(dev_priv, pipe_stats);
1776579de73bSChris Wilson 	} while (0);
17773278f67fSVille Syrjälä 
17789c6508b9SThomas Gleixner 	pmu_irq_stats(dev_priv, ret);
17799c6508b9SThomas Gleixner 
17809102650fSDaniele Ceraolo Spurio 	enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
17811f814dacSImre Deak 
178243f328d7SVille Syrjälä 	return ret;
178343f328d7SVille Syrjälä }
178443f328d7SVille Syrjälä 
178591d14251STvrtko Ursulin static void ibx_hpd_irq_handler(struct drm_i915_private *dev_priv,
17860398993bSVille Syrjälä 				u32 hotplug_trigger)
1787776ad806SJesse Barnes {
178842db67d6SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
1789776ad806SJesse Barnes 
17906a39d7c9SJani Nikula 	/*
17916a39d7c9SJani Nikula 	 * Somehow the PCH doesn't seem to really ack the interrupt to the CPU
17926a39d7c9SJani Nikula 	 * unless we touch the hotplug register, even if hotplug_trigger is
17936a39d7c9SJani Nikula 	 * zero. Not acking leads to "The master control interrupt lied (SDE)!"
17946a39d7c9SJani Nikula 	 * errors.
17956a39d7c9SJani Nikula 	 */
17962939eb06SJani Nikula 	dig_hotplug_reg = intel_uncore_read(&dev_priv->uncore, PCH_PORT_HOTPLUG);
17976a39d7c9SJani Nikula 	if (!hotplug_trigger) {
17986a39d7c9SJani Nikula 		u32 mask = PORTA_HOTPLUG_STATUS_MASK |
17996a39d7c9SJani Nikula 			PORTD_HOTPLUG_STATUS_MASK |
18006a39d7c9SJani Nikula 			PORTC_HOTPLUG_STATUS_MASK |
18016a39d7c9SJani Nikula 			PORTB_HOTPLUG_STATUS_MASK;
18026a39d7c9SJani Nikula 		dig_hotplug_reg &= ~mask;
18036a39d7c9SJani Nikula 	}
18046a39d7c9SJani Nikula 
18052939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, PCH_PORT_HOTPLUG, dig_hotplug_reg);
18066a39d7c9SJani Nikula 	if (!hotplug_trigger)
18076a39d7c9SJani Nikula 		return;
180813cf5504SDave Airlie 
18090398993bSVille Syrjälä 	intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
18100398993bSVille Syrjälä 			   hotplug_trigger, dig_hotplug_reg,
18110398993bSVille Syrjälä 			   dev_priv->hotplug.pch_hpd,
1812fd63e2a9SImre Deak 			   pch_port_hotplug_long_detect);
181340e56410SVille Syrjälä 
181491d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
1815aaf5ec2eSSonika Jindal }
181691d131d2SDaniel Vetter 
181791d14251STvrtko Ursulin static void ibx_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
181840e56410SVille Syrjälä {
1819d048a268SVille Syrjälä 	enum pipe pipe;
182040e56410SVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
182140e56410SVille Syrjälä 
18220398993bSVille Syrjälä 	ibx_hpd_irq_handler(dev_priv, hotplug_trigger);
182340e56410SVille Syrjälä 
1824cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1825cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1826776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
182700376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm, "PCH audio power change on port %d\n",
1828cfc33bf7SVille Syrjälä 			port_name(port));
1829cfc33bf7SVille Syrjälä 	}
1830776ad806SJesse Barnes 
1831ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
183291d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
1833ce99c256SDaniel Vetter 
1834776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
183591d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
1836776ad806SJesse Barnes 
1837776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
183800376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm, "PCH HDCP audio interrupt\n");
1839776ad806SJesse Barnes 
1840776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
184100376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm, "PCH transcoder audio interrupt\n");
1842776ad806SJesse Barnes 
1843776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
184400376ccfSWambui Karuga 		drm_err(&dev_priv->drm, "PCH poison interrupt\n");
1845776ad806SJesse Barnes 
1846b8b65ccdSAnshuman Gupta 	if (pch_iir & SDE_FDI_MASK) {
1847055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
184800376ccfSWambui Karuga 			drm_dbg(&dev_priv->drm, "  pipe %c FDI IIR: 0x%08x\n",
18499db4a9c7SJesse Barnes 				pipe_name(pipe),
18502939eb06SJani Nikula 				intel_uncore_read(&dev_priv->uncore, FDI_RX_IIR(pipe)));
1851b8b65ccdSAnshuman Gupta 	}
1852776ad806SJesse Barnes 
1853776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
185400376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm, "PCH transcoder CRC done interrupt\n");
1855776ad806SJesse Barnes 
1856776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
185700376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm,
185800376ccfSWambui Karuga 			"PCH transcoder CRC error interrupt\n");
1859776ad806SJesse Barnes 
1860776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
1861a2196033SMatthias Kaehlcke 		intel_pch_fifo_underrun_irq_handler(dev_priv, PIPE_A);
18628664281bSPaulo Zanoni 
18638664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
1864a2196033SMatthias Kaehlcke 		intel_pch_fifo_underrun_irq_handler(dev_priv, PIPE_B);
18658664281bSPaulo Zanoni }
18668664281bSPaulo Zanoni 
186791d14251STvrtko Ursulin static void ivb_err_int_handler(struct drm_i915_private *dev_priv)
18688664281bSPaulo Zanoni {
18692939eb06SJani Nikula 	u32 err_int = intel_uncore_read(&dev_priv->uncore, GEN7_ERR_INT);
18705a69b89fSDaniel Vetter 	enum pipe pipe;
18718664281bSPaulo Zanoni 
1872de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
187300376ccfSWambui Karuga 		drm_err(&dev_priv->drm, "Poison interrupt\n");
1874de032bf4SPaulo Zanoni 
1875055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
18761f7247c0SDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe))
18771f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
18788664281bSPaulo Zanoni 
18795a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
188091d14251STvrtko Ursulin 			if (IS_IVYBRIDGE(dev_priv))
188191d14251STvrtko Ursulin 				ivb_pipe_crc_irq_handler(dev_priv, pipe);
18825a69b89fSDaniel Vetter 			else
188391d14251STvrtko Ursulin 				hsw_pipe_crc_irq_handler(dev_priv, pipe);
18845a69b89fSDaniel Vetter 		}
18855a69b89fSDaniel Vetter 	}
18868bf1e9f1SShuang He 
18872939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, GEN7_ERR_INT, err_int);
18888664281bSPaulo Zanoni }
18898664281bSPaulo Zanoni 
189091d14251STvrtko Ursulin static void cpt_serr_int_handler(struct drm_i915_private *dev_priv)
18918664281bSPaulo Zanoni {
18922939eb06SJani Nikula 	u32 serr_int = intel_uncore_read(&dev_priv->uncore, SERR_INT);
189345c1cd87SMika Kahola 	enum pipe pipe;
18948664281bSPaulo Zanoni 
1895de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
189600376ccfSWambui Karuga 		drm_err(&dev_priv->drm, "PCH poison interrupt\n");
1897de032bf4SPaulo Zanoni 
189845c1cd87SMika Kahola 	for_each_pipe(dev_priv, pipe)
189945c1cd87SMika Kahola 		if (serr_int & SERR_INT_TRANS_FIFO_UNDERRUN(pipe))
190045c1cd87SMika Kahola 			intel_pch_fifo_underrun_irq_handler(dev_priv, pipe);
19018664281bSPaulo Zanoni 
19022939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, SERR_INT, serr_int);
1903776ad806SJesse Barnes }
1904776ad806SJesse Barnes 
190591d14251STvrtko Ursulin static void cpt_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
190623e81d69SAdam Jackson {
1907d048a268SVille Syrjälä 	enum pipe pipe;
19086dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
1909aaf5ec2eSSonika Jindal 
19100398993bSVille Syrjälä 	ibx_hpd_irq_handler(dev_priv, hotplug_trigger);
191191d131d2SDaniel Vetter 
1912cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
1913cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
191423e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
191500376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm, "PCH audio power change on port %c\n",
1916cfc33bf7SVille Syrjälä 			port_name(port));
1917cfc33bf7SVille Syrjälä 	}
191823e81d69SAdam Jackson 
191923e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
192091d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
192123e81d69SAdam Jackson 
192223e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
192391d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
192423e81d69SAdam Jackson 
192523e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
192600376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm, "Audio CP request interrupt\n");
192723e81d69SAdam Jackson 
192823e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
192900376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm, "Audio CP change interrupt\n");
193023e81d69SAdam Jackson 
1931b8b65ccdSAnshuman Gupta 	if (pch_iir & SDE_FDI_MASK_CPT) {
1932055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
193300376ccfSWambui Karuga 			drm_dbg(&dev_priv->drm, "  pipe %c FDI IIR: 0x%08x\n",
193423e81d69SAdam Jackson 				pipe_name(pipe),
19352939eb06SJani Nikula 				intel_uncore_read(&dev_priv->uncore, FDI_RX_IIR(pipe)));
1936b8b65ccdSAnshuman Gupta 	}
19378664281bSPaulo Zanoni 
19388664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
193991d14251STvrtko Ursulin 		cpt_serr_int_handler(dev_priv);
194023e81d69SAdam Jackson }
194123e81d69SAdam Jackson 
194258676af6SLucas De Marchi static void icp_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
194331604222SAnusha Srivatsa {
1944e76ab2cfSVille Syrjälä 	u32 ddi_hotplug_trigger = pch_iir & SDE_DDI_HOTPLUG_MASK_ICP;
1945e76ab2cfSVille Syrjälä 	u32 tc_hotplug_trigger = pch_iir & SDE_TC_HOTPLUG_MASK_ICP;
194631604222SAnusha Srivatsa 	u32 pin_mask = 0, long_mask = 0;
194731604222SAnusha Srivatsa 
194831604222SAnusha Srivatsa 	if (ddi_hotplug_trigger) {
194931604222SAnusha Srivatsa 		u32 dig_hotplug_reg;
195031604222SAnusha Srivatsa 
19512939eb06SJani Nikula 		dig_hotplug_reg = intel_uncore_read(&dev_priv->uncore, SHOTPLUG_CTL_DDI);
19522939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, SHOTPLUG_CTL_DDI, dig_hotplug_reg);
195331604222SAnusha Srivatsa 
195431604222SAnusha Srivatsa 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
19550398993bSVille Syrjälä 				   ddi_hotplug_trigger, dig_hotplug_reg,
19560398993bSVille Syrjälä 				   dev_priv->hotplug.pch_hpd,
195731604222SAnusha Srivatsa 				   icp_ddi_port_hotplug_long_detect);
195831604222SAnusha Srivatsa 	}
195931604222SAnusha Srivatsa 
196031604222SAnusha Srivatsa 	if (tc_hotplug_trigger) {
196131604222SAnusha Srivatsa 		u32 dig_hotplug_reg;
196231604222SAnusha Srivatsa 
19632939eb06SJani Nikula 		dig_hotplug_reg = intel_uncore_read(&dev_priv->uncore, SHOTPLUG_CTL_TC);
19642939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, SHOTPLUG_CTL_TC, dig_hotplug_reg);
196531604222SAnusha Srivatsa 
196631604222SAnusha Srivatsa 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
19670398993bSVille Syrjälä 				   tc_hotplug_trigger, dig_hotplug_reg,
19680398993bSVille Syrjälä 				   dev_priv->hotplug.pch_hpd,
1969da51e4baSVille Syrjälä 				   icp_tc_port_hotplug_long_detect);
197052dfdba0SLucas De Marchi 	}
197152dfdba0SLucas De Marchi 
197252dfdba0SLucas De Marchi 	if (pin_mask)
197352dfdba0SLucas De Marchi 		intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
197452dfdba0SLucas De Marchi 
197552dfdba0SLucas De Marchi 	if (pch_iir & SDE_GMBUS_ICP)
197652dfdba0SLucas De Marchi 		gmbus_irq_handler(dev_priv);
197752dfdba0SLucas De Marchi }
197852dfdba0SLucas De Marchi 
197991d14251STvrtko Ursulin static void spt_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
19806dbf30ceSVille Syrjälä {
19816dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_SPT &
19826dbf30ceSVille Syrjälä 		~SDE_PORTE_HOTPLUG_SPT;
19836dbf30ceSVille Syrjälä 	u32 hotplug2_trigger = pch_iir & SDE_PORTE_HOTPLUG_SPT;
19846dbf30ceSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
19856dbf30ceSVille Syrjälä 
19866dbf30ceSVille Syrjälä 	if (hotplug_trigger) {
19876dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
19886dbf30ceSVille Syrjälä 
19892939eb06SJani Nikula 		dig_hotplug_reg = intel_uncore_read(&dev_priv->uncore, PCH_PORT_HOTPLUG);
19902939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, PCH_PORT_HOTPLUG, dig_hotplug_reg);
19916dbf30ceSVille Syrjälä 
1992cf53902fSRodrigo Vivi 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
19930398993bSVille Syrjälä 				   hotplug_trigger, dig_hotplug_reg,
19940398993bSVille Syrjälä 				   dev_priv->hotplug.pch_hpd,
199574c0b395SVille Syrjälä 				   spt_port_hotplug_long_detect);
19966dbf30ceSVille Syrjälä 	}
19976dbf30ceSVille Syrjälä 
19986dbf30ceSVille Syrjälä 	if (hotplug2_trigger) {
19996dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
20006dbf30ceSVille Syrjälä 
20012939eb06SJani Nikula 		dig_hotplug_reg = intel_uncore_read(&dev_priv->uncore, PCH_PORT_HOTPLUG2);
20022939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, PCH_PORT_HOTPLUG2, dig_hotplug_reg);
20036dbf30ceSVille Syrjälä 
2004cf53902fSRodrigo Vivi 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
20050398993bSVille Syrjälä 				   hotplug2_trigger, dig_hotplug_reg,
20060398993bSVille Syrjälä 				   dev_priv->hotplug.pch_hpd,
20076dbf30ceSVille Syrjälä 				   spt_port_hotplug2_long_detect);
20086dbf30ceSVille Syrjälä 	}
20096dbf30ceSVille Syrjälä 
20106dbf30ceSVille Syrjälä 	if (pin_mask)
201191d14251STvrtko Ursulin 		intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
20126dbf30ceSVille Syrjälä 
20136dbf30ceSVille Syrjälä 	if (pch_iir & SDE_GMBUS_CPT)
201491d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
20156dbf30ceSVille Syrjälä }
20166dbf30ceSVille Syrjälä 
201791d14251STvrtko Ursulin static void ilk_hpd_irq_handler(struct drm_i915_private *dev_priv,
20180398993bSVille Syrjälä 				u32 hotplug_trigger)
2019c008bc6eSPaulo Zanoni {
2020e4ce95aaSVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2021e4ce95aaSVille Syrjälä 
20222939eb06SJani Nikula 	dig_hotplug_reg = intel_uncore_read(&dev_priv->uncore, DIGITAL_PORT_HOTPLUG_CNTRL);
20232939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, DIGITAL_PORT_HOTPLUG_CNTRL, dig_hotplug_reg);
2024e4ce95aaSVille Syrjälä 
20250398993bSVille Syrjälä 	intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
20260398993bSVille Syrjälä 			   hotplug_trigger, dig_hotplug_reg,
20270398993bSVille Syrjälä 			   dev_priv->hotplug.hpd,
2028e4ce95aaSVille Syrjälä 			   ilk_port_hotplug_long_detect);
202940e56410SVille Syrjälä 
203091d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2031e4ce95aaSVille Syrjälä }
2032c008bc6eSPaulo Zanoni 
203391d14251STvrtko Ursulin static void ilk_display_irq_handler(struct drm_i915_private *dev_priv,
203491d14251STvrtko Ursulin 				    u32 de_iir)
203540e56410SVille Syrjälä {
203640e56410SVille Syrjälä 	enum pipe pipe;
203740e56410SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG;
203840e56410SVille Syrjälä 
203940e56410SVille Syrjälä 	if (hotplug_trigger)
20400398993bSVille Syrjälä 		ilk_hpd_irq_handler(dev_priv, hotplug_trigger);
204140e56410SVille Syrjälä 
2042c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
204391d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
2044c008bc6eSPaulo Zanoni 
2045c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
204691d14251STvrtko Ursulin 		intel_opregion_asle_intr(dev_priv);
2047c008bc6eSPaulo Zanoni 
2048c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
204900376ccfSWambui Karuga 		drm_err(&dev_priv->drm, "Poison interrupt\n");
2050c008bc6eSPaulo Zanoni 
2051055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2052fd3a4024SDaniel Vetter 		if (de_iir & DE_PIPE_VBLANK(pipe))
2053aca9310aSAnshuman Gupta 			intel_handle_vblank(dev_priv, pipe);
2054c008bc6eSPaulo Zanoni 
2055*4bb18054SVille Syrjälä 		if (de_iir & DE_PLANE_FLIP_DONE(pipe))
2056*4bb18054SVille Syrjälä 			flip_done_handler(dev_priv, pipe);
2057*4bb18054SVille Syrjälä 
205840da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
20591f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
2060c008bc6eSPaulo Zanoni 
206140da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
206291d14251STvrtko Ursulin 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
2063c008bc6eSPaulo Zanoni 	}
2064c008bc6eSPaulo Zanoni 
2065c008bc6eSPaulo Zanoni 	/* check event from PCH */
2066c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
20672939eb06SJani Nikula 		u32 pch_iir = intel_uncore_read(&dev_priv->uncore, SDEIIR);
2068c008bc6eSPaulo Zanoni 
206991d14251STvrtko Ursulin 		if (HAS_PCH_CPT(dev_priv))
207091d14251STvrtko Ursulin 			cpt_irq_handler(dev_priv, pch_iir);
2071c008bc6eSPaulo Zanoni 		else
207291d14251STvrtko Ursulin 			ibx_irq_handler(dev_priv, pch_iir);
2073c008bc6eSPaulo Zanoni 
2074c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
20752939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, SDEIIR, pch_iir);
2076c008bc6eSPaulo Zanoni 	}
2077c008bc6eSPaulo Zanoni 
2078cf819effSLucas De Marchi 	if (IS_GEN(dev_priv, 5) && de_iir & DE_PCU_EVENT)
20793e7abf81SAndi Shyti 		gen5_rps_irq_handler(&dev_priv->gt.rps);
2080c008bc6eSPaulo Zanoni }
2081c008bc6eSPaulo Zanoni 
208291d14251STvrtko Ursulin static void ivb_display_irq_handler(struct drm_i915_private *dev_priv,
208391d14251STvrtko Ursulin 				    u32 de_iir)
20849719fb98SPaulo Zanoni {
208507d27e20SDamien Lespiau 	enum pipe pipe;
208623bb4cb5SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG_IVB;
208723bb4cb5SVille Syrjälä 
208840e56410SVille Syrjälä 	if (hotplug_trigger)
20890398993bSVille Syrjälä 		ilk_hpd_irq_handler(dev_priv, hotplug_trigger);
20909719fb98SPaulo Zanoni 
20919719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
209291d14251STvrtko Ursulin 		ivb_err_int_handler(dev_priv);
20939719fb98SPaulo Zanoni 
209454fd3149SDhinakaran Pandiyan 	if (de_iir & DE_EDP_PSR_INT_HSW) {
20952939eb06SJani Nikula 		u32 psr_iir = intel_uncore_read(&dev_priv->uncore, EDP_PSR_IIR);
209654fd3149SDhinakaran Pandiyan 
209754fd3149SDhinakaran Pandiyan 		intel_psr_irq_handler(dev_priv, psr_iir);
20982939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, EDP_PSR_IIR, psr_iir);
209954fd3149SDhinakaran Pandiyan 	}
2100fc340442SDaniel Vetter 
21019719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
210291d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
21039719fb98SPaulo Zanoni 
21049719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
210591d14251STvrtko Ursulin 		intel_opregion_asle_intr(dev_priv);
21069719fb98SPaulo Zanoni 
2107055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
210833ef04faSVille Syrjälä 		if (de_iir & DE_PIPE_VBLANK_IVB(pipe))
2109aca9310aSAnshuman Gupta 			intel_handle_vblank(dev_priv, pipe);
21102a636e24SVille Syrjälä 
21112a636e24SVille Syrjälä 		if (de_iir & DE_PLANE_FLIP_DONE_IVB(pipe))
21122a636e24SVille Syrjälä 			flip_done_handler(dev_priv, pipe);
21139719fb98SPaulo Zanoni 	}
21149719fb98SPaulo Zanoni 
21159719fb98SPaulo Zanoni 	/* check event from PCH */
211691d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv) && (de_iir & DE_PCH_EVENT_IVB)) {
21172939eb06SJani Nikula 		u32 pch_iir = intel_uncore_read(&dev_priv->uncore, SDEIIR);
21189719fb98SPaulo Zanoni 
211991d14251STvrtko Ursulin 		cpt_irq_handler(dev_priv, pch_iir);
21209719fb98SPaulo Zanoni 
21219719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
21222939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, SDEIIR, pch_iir);
21239719fb98SPaulo Zanoni 	}
21249719fb98SPaulo Zanoni }
21259719fb98SPaulo Zanoni 
212672c90f62SOscar Mateo /*
212772c90f62SOscar Mateo  * To handle irqs with the minimum potential races with fresh interrupts, we:
212872c90f62SOscar Mateo  * 1 - Disable Master Interrupt Control.
212972c90f62SOscar Mateo  * 2 - Find the source(s) of the interrupt.
213072c90f62SOscar Mateo  * 3 - Clear the Interrupt Identity bits (IIR).
213172c90f62SOscar Mateo  * 4 - Process the interrupt(s) that had bits set in the IIRs.
213272c90f62SOscar Mateo  * 5 - Re-enable Master Interrupt Control.
213372c90f62SOscar Mateo  */
21349eae5e27SLucas De Marchi static irqreturn_t ilk_irq_handler(int irq, void *arg)
2135b1f14ad0SJesse Barnes {
2136c48a798aSChris Wilson 	struct drm_i915_private *i915 = arg;
2137c48a798aSChris Wilson 	void __iomem * const regs = i915->uncore.regs;
2138f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
21390e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
2140b1f14ad0SJesse Barnes 
2141c48a798aSChris Wilson 	if (unlikely(!intel_irqs_enabled(i915)))
21422dd2a883SImre Deak 		return IRQ_NONE;
21432dd2a883SImre Deak 
21441f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
2145c48a798aSChris Wilson 	disable_rpm_wakeref_asserts(&i915->runtime_pm);
21461f814dacSImre Deak 
2147b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
2148c48a798aSChris Wilson 	de_ier = raw_reg_read(regs, DEIER);
2149c48a798aSChris Wilson 	raw_reg_write(regs, DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
21500e43406bSChris Wilson 
215144498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
215244498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
215344498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
215444498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
215544498aeaSPaulo Zanoni 	 * due to its back queue). */
2156c48a798aSChris Wilson 	if (!HAS_PCH_NOP(i915)) {
2157c48a798aSChris Wilson 		sde_ier = raw_reg_read(regs, SDEIER);
2158c48a798aSChris Wilson 		raw_reg_write(regs, SDEIER, 0);
2159ab5c608bSBen Widawsky 	}
216044498aeaSPaulo Zanoni 
216172c90f62SOscar Mateo 	/* Find, clear, then process each source of interrupt */
216272c90f62SOscar Mateo 
2163c48a798aSChris Wilson 	gt_iir = raw_reg_read(regs, GTIIR);
21640e43406bSChris Wilson 	if (gt_iir) {
2165c48a798aSChris Wilson 		raw_reg_write(regs, GTIIR, gt_iir);
2166c48a798aSChris Wilson 		if (INTEL_GEN(i915) >= 6)
2167c48a798aSChris Wilson 			gen6_gt_irq_handler(&i915->gt, gt_iir);
2168d8fc8a47SPaulo Zanoni 		else
2169c48a798aSChris Wilson 			gen5_gt_irq_handler(&i915->gt, gt_iir);
2170c48a798aSChris Wilson 		ret = IRQ_HANDLED;
21710e43406bSChris Wilson 	}
2172b1f14ad0SJesse Barnes 
2173c48a798aSChris Wilson 	de_iir = raw_reg_read(regs, DEIIR);
21740e43406bSChris Wilson 	if (de_iir) {
2175c48a798aSChris Wilson 		raw_reg_write(regs, DEIIR, de_iir);
2176c48a798aSChris Wilson 		if (INTEL_GEN(i915) >= 7)
2177c48a798aSChris Wilson 			ivb_display_irq_handler(i915, de_iir);
2178f1af8fc1SPaulo Zanoni 		else
2179c48a798aSChris Wilson 			ilk_display_irq_handler(i915, de_iir);
21800e43406bSChris Wilson 		ret = IRQ_HANDLED;
2181c48a798aSChris Wilson 	}
2182c48a798aSChris Wilson 
2183c48a798aSChris Wilson 	if (INTEL_GEN(i915) >= 6) {
2184c48a798aSChris Wilson 		u32 pm_iir = raw_reg_read(regs, GEN6_PMIIR);
2185c48a798aSChris Wilson 		if (pm_iir) {
2186c48a798aSChris Wilson 			raw_reg_write(regs, GEN6_PMIIR, pm_iir);
2187c48a798aSChris Wilson 			gen6_rps_irq_handler(&i915->gt.rps, pm_iir);
2188c48a798aSChris Wilson 			ret = IRQ_HANDLED;
21890e43406bSChris Wilson 		}
2190f1af8fc1SPaulo Zanoni 	}
2191b1f14ad0SJesse Barnes 
2192c48a798aSChris Wilson 	raw_reg_write(regs, DEIER, de_ier);
2193c48a798aSChris Wilson 	if (sde_ier)
2194c48a798aSChris Wilson 		raw_reg_write(regs, SDEIER, sde_ier);
2195b1f14ad0SJesse Barnes 
21969c6508b9SThomas Gleixner 	pmu_irq_stats(i915, ret);
21979c6508b9SThomas Gleixner 
21981f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
2199c48a798aSChris Wilson 	enable_rpm_wakeref_asserts(&i915->runtime_pm);
22001f814dacSImre Deak 
2201b1f14ad0SJesse Barnes 	return ret;
2202b1f14ad0SJesse Barnes }
2203b1f14ad0SJesse Barnes 
220491d14251STvrtko Ursulin static void bxt_hpd_irq_handler(struct drm_i915_private *dev_priv,
22050398993bSVille Syrjälä 				u32 hotplug_trigger)
2206d04a492dSShashank Sharma {
2207cebd87a0SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2208d04a492dSShashank Sharma 
22092939eb06SJani Nikula 	dig_hotplug_reg = intel_uncore_read(&dev_priv->uncore, PCH_PORT_HOTPLUG);
22102939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, PCH_PORT_HOTPLUG, dig_hotplug_reg);
2211d04a492dSShashank Sharma 
22120398993bSVille Syrjälä 	intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
22130398993bSVille Syrjälä 			   hotplug_trigger, dig_hotplug_reg,
22140398993bSVille Syrjälä 			   dev_priv->hotplug.hpd,
2215cebd87a0SVille Syrjälä 			   bxt_port_hotplug_long_detect);
221640e56410SVille Syrjälä 
221791d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2218d04a492dSShashank Sharma }
2219d04a492dSShashank Sharma 
2220121e758eSDhinakaran Pandiyan static void gen11_hpd_irq_handler(struct drm_i915_private *dev_priv, u32 iir)
2221121e758eSDhinakaran Pandiyan {
2222121e758eSDhinakaran Pandiyan 	u32 pin_mask = 0, long_mask = 0;
2223b796b971SDhinakaran Pandiyan 	u32 trigger_tc = iir & GEN11_DE_TC_HOTPLUG_MASK;
2224b796b971SDhinakaran Pandiyan 	u32 trigger_tbt = iir & GEN11_DE_TBT_HOTPLUG_MASK;
2225121e758eSDhinakaran Pandiyan 
2226121e758eSDhinakaran Pandiyan 	if (trigger_tc) {
2227b796b971SDhinakaran Pandiyan 		u32 dig_hotplug_reg;
2228b796b971SDhinakaran Pandiyan 
22292939eb06SJani Nikula 		dig_hotplug_reg = intel_uncore_read(&dev_priv->uncore, GEN11_TC_HOTPLUG_CTL);
22302939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, GEN11_TC_HOTPLUG_CTL, dig_hotplug_reg);
2231121e758eSDhinakaran Pandiyan 
22320398993bSVille Syrjälä 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
22330398993bSVille Syrjälä 				   trigger_tc, dig_hotplug_reg,
22340398993bSVille Syrjälä 				   dev_priv->hotplug.hpd,
2235da51e4baSVille Syrjälä 				   gen11_port_hotplug_long_detect);
2236121e758eSDhinakaran Pandiyan 	}
2237b796b971SDhinakaran Pandiyan 
2238b796b971SDhinakaran Pandiyan 	if (trigger_tbt) {
2239b796b971SDhinakaran Pandiyan 		u32 dig_hotplug_reg;
2240b796b971SDhinakaran Pandiyan 
22412939eb06SJani Nikula 		dig_hotplug_reg = intel_uncore_read(&dev_priv->uncore, GEN11_TBT_HOTPLUG_CTL);
22422939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, GEN11_TBT_HOTPLUG_CTL, dig_hotplug_reg);
2243b796b971SDhinakaran Pandiyan 
22440398993bSVille Syrjälä 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
22450398993bSVille Syrjälä 				   trigger_tbt, dig_hotplug_reg,
22460398993bSVille Syrjälä 				   dev_priv->hotplug.hpd,
2247da51e4baSVille Syrjälä 				   gen11_port_hotplug_long_detect);
2248b796b971SDhinakaran Pandiyan 	}
2249b796b971SDhinakaran Pandiyan 
2250b796b971SDhinakaran Pandiyan 	if (pin_mask)
2251b796b971SDhinakaran Pandiyan 		intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2252b796b971SDhinakaran Pandiyan 	else
225300376ccfSWambui Karuga 		drm_err(&dev_priv->drm,
225400376ccfSWambui Karuga 			"Unexpected DE HPD interrupt 0x%08x\n", iir);
2255121e758eSDhinakaran Pandiyan }
2256121e758eSDhinakaran Pandiyan 
22579d17210fSLucas De Marchi static u32 gen8_de_port_aux_mask(struct drm_i915_private *dev_priv)
22589d17210fSLucas De Marchi {
225955523360SLucas De Marchi 	u32 mask;
22609d17210fSLucas De Marchi 
226155523360SLucas De Marchi 	if (INTEL_GEN(dev_priv) >= 12)
226255523360SLucas De Marchi 		return TGL_DE_PORT_AUX_DDIA |
226355523360SLucas De Marchi 			TGL_DE_PORT_AUX_DDIB |
2264e5df52dcSMatt Roper 			TGL_DE_PORT_AUX_DDIC |
2265e5df52dcSMatt Roper 			TGL_DE_PORT_AUX_USBC1 |
2266e5df52dcSMatt Roper 			TGL_DE_PORT_AUX_USBC2 |
2267e5df52dcSMatt Roper 			TGL_DE_PORT_AUX_USBC3 |
2268e5df52dcSMatt Roper 			TGL_DE_PORT_AUX_USBC4 |
2269e5df52dcSMatt Roper 			TGL_DE_PORT_AUX_USBC5 |
2270e5df52dcSMatt Roper 			TGL_DE_PORT_AUX_USBC6;
2271e5df52dcSMatt Roper 
227255523360SLucas De Marchi 
227355523360SLucas De Marchi 	mask = GEN8_AUX_CHANNEL_A;
22749d17210fSLucas De Marchi 	if (INTEL_GEN(dev_priv) >= 9)
22759d17210fSLucas De Marchi 		mask |= GEN9_AUX_CHANNEL_B |
22769d17210fSLucas De Marchi 			GEN9_AUX_CHANNEL_C |
22779d17210fSLucas De Marchi 			GEN9_AUX_CHANNEL_D;
22789d17210fSLucas De Marchi 
227955523360SLucas De Marchi 	if (IS_CNL_WITH_PORT_F(dev_priv) || IS_GEN(dev_priv, 11))
22809d17210fSLucas De Marchi 		mask |= CNL_AUX_CHANNEL_F;
22819d17210fSLucas De Marchi 
228255523360SLucas De Marchi 	if (IS_GEN(dev_priv, 11))
228355523360SLucas De Marchi 		mask |= ICL_AUX_CHANNEL_E;
22849d17210fSLucas De Marchi 
22859d17210fSLucas De Marchi 	return mask;
22869d17210fSLucas De Marchi }
22879d17210fSLucas De Marchi 
22885270130dSVille Syrjälä static u32 gen8_de_pipe_fault_mask(struct drm_i915_private *dev_priv)
22895270130dSVille Syrjälä {
229099e2d8bcSMatt Roper 	if (IS_ROCKETLAKE(dev_priv))
229199e2d8bcSMatt Roper 		return RKL_DE_PIPE_IRQ_FAULT_ERRORS;
229299e2d8bcSMatt Roper 	else if (INTEL_GEN(dev_priv) >= 11)
2293d506a65dSMatt Roper 		return GEN11_DE_PIPE_IRQ_FAULT_ERRORS;
2294d506a65dSMatt Roper 	else if (INTEL_GEN(dev_priv) >= 9)
22955270130dSVille Syrjälä 		return GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
22965270130dSVille Syrjälä 	else
22975270130dSVille Syrjälä 		return GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
22985270130dSVille Syrjälä }
22995270130dSVille Syrjälä 
230046c63d24SJosé Roberto de Souza static void
230146c63d24SJosé Roberto de Souza gen8_de_misc_irq_handler(struct drm_i915_private *dev_priv, u32 iir)
2302abd58f01SBen Widawsky {
2303e04f7eceSVille Syrjälä 	bool found = false;
2304e04f7eceSVille Syrjälä 
2305e04f7eceSVille Syrjälä 	if (iir & GEN8_DE_MISC_GSE) {
230691d14251STvrtko Ursulin 		intel_opregion_asle_intr(dev_priv);
2307e04f7eceSVille Syrjälä 		found = true;
2308e04f7eceSVille Syrjälä 	}
2309e04f7eceSVille Syrjälä 
2310e04f7eceSVille Syrjälä 	if (iir & GEN8_DE_EDP_PSR) {
23118241cfbeSJosé Roberto de Souza 		u32 psr_iir;
23128241cfbeSJosé Roberto de Souza 		i915_reg_t iir_reg;
23138241cfbeSJosé Roberto de Souza 
23148241cfbeSJosé Roberto de Souza 		if (INTEL_GEN(dev_priv) >= 12)
23158241cfbeSJosé Roberto de Souza 			iir_reg = TRANS_PSR_IIR(dev_priv->psr.transcoder);
23168241cfbeSJosé Roberto de Souza 		else
23178241cfbeSJosé Roberto de Souza 			iir_reg = EDP_PSR_IIR;
23188241cfbeSJosé Roberto de Souza 
23192939eb06SJani Nikula 		psr_iir = intel_uncore_read(&dev_priv->uncore, iir_reg);
23202939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, iir_reg, psr_iir);
23218241cfbeSJosé Roberto de Souza 
23228241cfbeSJosé Roberto de Souza 		if (psr_iir)
23238241cfbeSJosé Roberto de Souza 			found = true;
232454fd3149SDhinakaran Pandiyan 
232554fd3149SDhinakaran Pandiyan 		intel_psr_irq_handler(dev_priv, psr_iir);
2326e04f7eceSVille Syrjälä 	}
2327e04f7eceSVille Syrjälä 
2328e04f7eceSVille Syrjälä 	if (!found)
232900376ccfSWambui Karuga 		drm_err(&dev_priv->drm, "Unexpected DE Misc interrupt\n");
2330abd58f01SBen Widawsky }
233146c63d24SJosé Roberto de Souza 
233200acb329SVandita Kulkarni static void gen11_dsi_te_interrupt_handler(struct drm_i915_private *dev_priv,
233300acb329SVandita Kulkarni 					   u32 te_trigger)
233400acb329SVandita Kulkarni {
233500acb329SVandita Kulkarni 	enum pipe pipe = INVALID_PIPE;
233600acb329SVandita Kulkarni 	enum transcoder dsi_trans;
233700acb329SVandita Kulkarni 	enum port port;
233800acb329SVandita Kulkarni 	u32 val, tmp;
233900acb329SVandita Kulkarni 
234000acb329SVandita Kulkarni 	/*
234100acb329SVandita Kulkarni 	 * Incase of dual link, TE comes from DSI_1
234200acb329SVandita Kulkarni 	 * this is to check if dual link is enabled
234300acb329SVandita Kulkarni 	 */
23442939eb06SJani Nikula 	val = intel_uncore_read(&dev_priv->uncore, TRANS_DDI_FUNC_CTL2(TRANSCODER_DSI_0));
234500acb329SVandita Kulkarni 	val &= PORT_SYNC_MODE_ENABLE;
234600acb329SVandita Kulkarni 
234700acb329SVandita Kulkarni 	/*
234800acb329SVandita Kulkarni 	 * if dual link is enabled, then read DSI_0
234900acb329SVandita Kulkarni 	 * transcoder registers
235000acb329SVandita Kulkarni 	 */
235100acb329SVandita Kulkarni 	port = ((te_trigger & DSI1_TE && val) || (te_trigger & DSI0_TE)) ?
235200acb329SVandita Kulkarni 						  PORT_A : PORT_B;
235300acb329SVandita Kulkarni 	dsi_trans = (port == PORT_A) ? TRANSCODER_DSI_0 : TRANSCODER_DSI_1;
235400acb329SVandita Kulkarni 
235500acb329SVandita Kulkarni 	/* Check if DSI configured in command mode */
23562939eb06SJani Nikula 	val = intel_uncore_read(&dev_priv->uncore, DSI_TRANS_FUNC_CONF(dsi_trans));
235700acb329SVandita Kulkarni 	val = val & OP_MODE_MASK;
235800acb329SVandita Kulkarni 
235900acb329SVandita Kulkarni 	if (val != CMD_MODE_NO_GATE && val != CMD_MODE_TE_GATE) {
236000acb329SVandita Kulkarni 		drm_err(&dev_priv->drm, "DSI trancoder not configured in command mode\n");
236100acb329SVandita Kulkarni 		return;
236200acb329SVandita Kulkarni 	}
236300acb329SVandita Kulkarni 
236400acb329SVandita Kulkarni 	/* Get PIPE for handling VBLANK event */
23652939eb06SJani Nikula 	val = intel_uncore_read(&dev_priv->uncore, TRANS_DDI_FUNC_CTL(dsi_trans));
236600acb329SVandita Kulkarni 	switch (val & TRANS_DDI_EDP_INPUT_MASK) {
236700acb329SVandita Kulkarni 	case TRANS_DDI_EDP_INPUT_A_ON:
236800acb329SVandita Kulkarni 		pipe = PIPE_A;
236900acb329SVandita Kulkarni 		break;
237000acb329SVandita Kulkarni 	case TRANS_DDI_EDP_INPUT_B_ONOFF:
237100acb329SVandita Kulkarni 		pipe = PIPE_B;
237200acb329SVandita Kulkarni 		break;
237300acb329SVandita Kulkarni 	case TRANS_DDI_EDP_INPUT_C_ONOFF:
237400acb329SVandita Kulkarni 		pipe = PIPE_C;
237500acb329SVandita Kulkarni 		break;
237600acb329SVandita Kulkarni 	default:
237700acb329SVandita Kulkarni 		drm_err(&dev_priv->drm, "Invalid PIPE\n");
237800acb329SVandita Kulkarni 		return;
237900acb329SVandita Kulkarni 	}
238000acb329SVandita Kulkarni 
238100acb329SVandita Kulkarni 	intel_handle_vblank(dev_priv, pipe);
238200acb329SVandita Kulkarni 
238300acb329SVandita Kulkarni 	/* clear TE in dsi IIR */
238400acb329SVandita Kulkarni 	port = (te_trigger & DSI1_TE) ? PORT_B : PORT_A;
23852939eb06SJani Nikula 	tmp = intel_uncore_read(&dev_priv->uncore, DSI_INTR_IDENT_REG(port));
23862939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, DSI_INTR_IDENT_REG(port), tmp);
238700acb329SVandita Kulkarni }
238800acb329SVandita Kulkarni 
2389cda195f1SVille Syrjälä static u32 gen8_de_pipe_flip_done_mask(struct drm_i915_private *i915)
2390cda195f1SVille Syrjälä {
2391cda195f1SVille Syrjälä 	if (INTEL_GEN(i915) >= 9)
2392cda195f1SVille Syrjälä 		return GEN9_PIPE_PLANE1_FLIP_DONE;
2393cda195f1SVille Syrjälä 	else
2394cda195f1SVille Syrjälä 		return GEN8_PIPE_PRIMARY_FLIP_DONE;
2395cda195f1SVille Syrjälä }
2396cda195f1SVille Syrjälä 
239746c63d24SJosé Roberto de Souza static irqreturn_t
239846c63d24SJosé Roberto de Souza gen8_de_irq_handler(struct drm_i915_private *dev_priv, u32 master_ctl)
239946c63d24SJosé Roberto de Souza {
240046c63d24SJosé Roberto de Souza 	irqreturn_t ret = IRQ_NONE;
240146c63d24SJosé Roberto de Souza 	u32 iir;
240246c63d24SJosé Roberto de Souza 	enum pipe pipe;
240346c63d24SJosé Roberto de Souza 
240446c63d24SJosé Roberto de Souza 	if (master_ctl & GEN8_DE_MISC_IRQ) {
24052939eb06SJani Nikula 		iir = intel_uncore_read(&dev_priv->uncore, GEN8_DE_MISC_IIR);
240646c63d24SJosé Roberto de Souza 		if (iir) {
24072939eb06SJani Nikula 			intel_uncore_write(&dev_priv->uncore, GEN8_DE_MISC_IIR, iir);
240846c63d24SJosé Roberto de Souza 			ret = IRQ_HANDLED;
240946c63d24SJosé Roberto de Souza 			gen8_de_misc_irq_handler(dev_priv, iir);
241046c63d24SJosé Roberto de Souza 		} else {
241100376ccfSWambui Karuga 			drm_err(&dev_priv->drm,
241200376ccfSWambui Karuga 				"The master control interrupt lied (DE MISC)!\n");
2413abd58f01SBen Widawsky 		}
241446c63d24SJosé Roberto de Souza 	}
2415abd58f01SBen Widawsky 
2416121e758eSDhinakaran Pandiyan 	if (INTEL_GEN(dev_priv) >= 11 && (master_ctl & GEN11_DE_HPD_IRQ)) {
24172939eb06SJani Nikula 		iir = intel_uncore_read(&dev_priv->uncore, GEN11_DE_HPD_IIR);
2418121e758eSDhinakaran Pandiyan 		if (iir) {
24192939eb06SJani Nikula 			intel_uncore_write(&dev_priv->uncore, GEN11_DE_HPD_IIR, iir);
2420121e758eSDhinakaran Pandiyan 			ret = IRQ_HANDLED;
2421121e758eSDhinakaran Pandiyan 			gen11_hpd_irq_handler(dev_priv, iir);
2422121e758eSDhinakaran Pandiyan 		} else {
242300376ccfSWambui Karuga 			drm_err(&dev_priv->drm,
242400376ccfSWambui Karuga 				"The master control interrupt lied, (DE HPD)!\n");
2425121e758eSDhinakaran Pandiyan 		}
2426121e758eSDhinakaran Pandiyan 	}
2427121e758eSDhinakaran Pandiyan 
24286d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
24292939eb06SJani Nikula 		iir = intel_uncore_read(&dev_priv->uncore, GEN8_DE_PORT_IIR);
2430e32192e1STvrtko Ursulin 		if (iir) {
2431d04a492dSShashank Sharma 			bool found = false;
2432cebd87a0SVille Syrjälä 
24332939eb06SJani Nikula 			intel_uncore_write(&dev_priv->uncore, GEN8_DE_PORT_IIR, iir);
24346d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
243588e04703SJesse Barnes 
24369d17210fSLucas De Marchi 			if (iir & gen8_de_port_aux_mask(dev_priv)) {
243791d14251STvrtko Ursulin 				dp_aux_irq_handler(dev_priv);
2438d04a492dSShashank Sharma 				found = true;
2439d04a492dSShashank Sharma 			}
2440d04a492dSShashank Sharma 
2441cc3f90f0SAnder Conselvan de Oliveira 			if (IS_GEN9_LP(dev_priv)) {
24429a55a620SVille Syrjälä 				u32 hotplug_trigger = iir & BXT_DE_PORT_HOTPLUG_MASK;
24439a55a620SVille Syrjälä 
24449a55a620SVille Syrjälä 				if (hotplug_trigger) {
24459a55a620SVille Syrjälä 					bxt_hpd_irq_handler(dev_priv, hotplug_trigger);
2446d04a492dSShashank Sharma 					found = true;
2447d04a492dSShashank Sharma 				}
2448e32192e1STvrtko Ursulin 			} else if (IS_BROADWELL(dev_priv)) {
24499a55a620SVille Syrjälä 				u32 hotplug_trigger = iir & BDW_DE_PORT_HOTPLUG_MASK;
24509a55a620SVille Syrjälä 
24519a55a620SVille Syrjälä 				if (hotplug_trigger) {
24529a55a620SVille Syrjälä 					ilk_hpd_irq_handler(dev_priv, hotplug_trigger);
2453e32192e1STvrtko Ursulin 					found = true;
2454e32192e1STvrtko Ursulin 				}
2455e32192e1STvrtko Ursulin 			}
2456d04a492dSShashank Sharma 
2457cc3f90f0SAnder Conselvan de Oliveira 			if (IS_GEN9_LP(dev_priv) && (iir & BXT_DE_PORT_GMBUS)) {
245891d14251STvrtko Ursulin 				gmbus_irq_handler(dev_priv);
24599e63743eSShashank Sharma 				found = true;
24609e63743eSShashank Sharma 			}
24619e63743eSShashank Sharma 
246200acb329SVandita Kulkarni 			if (INTEL_GEN(dev_priv) >= 11) {
24639a55a620SVille Syrjälä 				u32 te_trigger = iir & (DSI0_TE | DSI1_TE);
24649a55a620SVille Syrjälä 
24659a55a620SVille Syrjälä 				if (te_trigger) {
24669a55a620SVille Syrjälä 					gen11_dsi_te_interrupt_handler(dev_priv, te_trigger);
246700acb329SVandita Kulkarni 					found = true;
246800acb329SVandita Kulkarni 				}
246900acb329SVandita Kulkarni 			}
247000acb329SVandita Kulkarni 
2471d04a492dSShashank Sharma 			if (!found)
247200376ccfSWambui Karuga 				drm_err(&dev_priv->drm,
247300376ccfSWambui Karuga 					"Unexpected DE Port interrupt\n");
24746d766f02SDaniel Vetter 		}
247538cc46d7SOscar Mateo 		else
247600376ccfSWambui Karuga 			drm_err(&dev_priv->drm,
247700376ccfSWambui Karuga 				"The master control interrupt lied (DE PORT)!\n");
24786d766f02SDaniel Vetter 	}
24796d766f02SDaniel Vetter 
2480055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2481fd3a4024SDaniel Vetter 		u32 fault_errors;
2482abd58f01SBen Widawsky 
2483c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2484c42664ccSDaniel Vetter 			continue;
2485c42664ccSDaniel Vetter 
24862939eb06SJani Nikula 		iir = intel_uncore_read(&dev_priv->uncore, GEN8_DE_PIPE_IIR(pipe));
2487e32192e1STvrtko Ursulin 		if (!iir) {
248800376ccfSWambui Karuga 			drm_err(&dev_priv->drm,
248900376ccfSWambui Karuga 				"The master control interrupt lied (DE PIPE)!\n");
2490e32192e1STvrtko Ursulin 			continue;
2491e32192e1STvrtko Ursulin 		}
2492770de83dSDamien Lespiau 
2493e32192e1STvrtko Ursulin 		ret = IRQ_HANDLED;
24942939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, GEN8_DE_PIPE_IIR(pipe), iir);
2495e32192e1STvrtko Ursulin 
2496fd3a4024SDaniel Vetter 		if (iir & GEN8_PIPE_VBLANK)
2497aca9310aSAnshuman Gupta 			intel_handle_vblank(dev_priv, pipe);
2498abd58f01SBen Widawsky 
2499cda195f1SVille Syrjälä 		if (iir & gen8_de_pipe_flip_done_mask(dev_priv))
25001288f9b0SKarthik B S 			flip_done_handler(dev_priv, pipe);
25011288f9b0SKarthik B S 
2502e32192e1STvrtko Ursulin 		if (iir & GEN8_PIPE_CDCLK_CRC_DONE)
250391d14251STvrtko Ursulin 			hsw_pipe_crc_irq_handler(dev_priv, pipe);
25040fbe7870SDaniel Vetter 
2505e32192e1STvrtko Ursulin 		if (iir & GEN8_PIPE_FIFO_UNDERRUN)
2506e32192e1STvrtko Ursulin 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
250738d83c96SDaniel Vetter 
25085270130dSVille Syrjälä 		fault_errors = iir & gen8_de_pipe_fault_mask(dev_priv);
2509770de83dSDamien Lespiau 		if (fault_errors)
251000376ccfSWambui Karuga 			drm_err(&dev_priv->drm,
251100376ccfSWambui Karuga 				"Fault errors on pipe %c: 0x%08x\n",
251230100f2bSDaniel Vetter 				pipe_name(pipe),
2513e32192e1STvrtko Ursulin 				fault_errors);
2514abd58f01SBen Widawsky 	}
2515abd58f01SBen Widawsky 
251691d14251STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv) && !HAS_PCH_NOP(dev_priv) &&
2517266ea3d9SShashank Sharma 	    master_ctl & GEN8_DE_PCH_IRQ) {
251892d03a80SDaniel Vetter 		/*
251992d03a80SDaniel Vetter 		 * FIXME(BDW): Assume for now that the new interrupt handling
252092d03a80SDaniel Vetter 		 * scheme also closed the SDE interrupt handling race we've seen
252192d03a80SDaniel Vetter 		 * on older pch-split platforms. But this needs testing.
252292d03a80SDaniel Vetter 		 */
25232939eb06SJani Nikula 		iir = intel_uncore_read(&dev_priv->uncore, SDEIIR);
2524e32192e1STvrtko Ursulin 		if (iir) {
25252939eb06SJani Nikula 			intel_uncore_write(&dev_priv->uncore, SDEIIR, iir);
252692d03a80SDaniel Vetter 			ret = IRQ_HANDLED;
25276dbf30ceSVille Syrjälä 
252858676af6SLucas De Marchi 			if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
252958676af6SLucas De Marchi 				icp_irq_handler(dev_priv, iir);
2530c6c30b91SRodrigo Vivi 			else if (INTEL_PCH_TYPE(dev_priv) >= PCH_SPT)
253191d14251STvrtko Ursulin 				spt_irq_handler(dev_priv, iir);
25326dbf30ceSVille Syrjälä 			else
253391d14251STvrtko Ursulin 				cpt_irq_handler(dev_priv, iir);
25342dfb0b81SJani Nikula 		} else {
25352dfb0b81SJani Nikula 			/*
25362dfb0b81SJani Nikula 			 * Like on previous PCH there seems to be something
25372dfb0b81SJani Nikula 			 * fishy going on with forwarding PCH interrupts.
25382dfb0b81SJani Nikula 			 */
253900376ccfSWambui Karuga 			drm_dbg(&dev_priv->drm,
254000376ccfSWambui Karuga 				"The master control interrupt lied (SDE)!\n");
25412dfb0b81SJani Nikula 		}
254292d03a80SDaniel Vetter 	}
254392d03a80SDaniel Vetter 
2544f11a0f46STvrtko Ursulin 	return ret;
2545f11a0f46STvrtko Ursulin }
2546f11a0f46STvrtko Ursulin 
25474376b9c9SMika Kuoppala static inline u32 gen8_master_intr_disable(void __iomem * const regs)
25484376b9c9SMika Kuoppala {
25494376b9c9SMika Kuoppala 	raw_reg_write(regs, GEN8_MASTER_IRQ, 0);
25504376b9c9SMika Kuoppala 
25514376b9c9SMika Kuoppala 	/*
25524376b9c9SMika Kuoppala 	 * Now with master disabled, get a sample of level indications
25534376b9c9SMika Kuoppala 	 * for this interrupt. Indications will be cleared on related acks.
25544376b9c9SMika Kuoppala 	 * New indications can and will light up during processing,
25554376b9c9SMika Kuoppala 	 * and will generate new interrupt after enabling master.
25564376b9c9SMika Kuoppala 	 */
25574376b9c9SMika Kuoppala 	return raw_reg_read(regs, GEN8_MASTER_IRQ);
25584376b9c9SMika Kuoppala }
25594376b9c9SMika Kuoppala 
25604376b9c9SMika Kuoppala static inline void gen8_master_intr_enable(void __iomem * const regs)
25614376b9c9SMika Kuoppala {
25624376b9c9SMika Kuoppala 	raw_reg_write(regs, GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
25634376b9c9SMika Kuoppala }
25644376b9c9SMika Kuoppala 
2565f11a0f46STvrtko Ursulin static irqreturn_t gen8_irq_handler(int irq, void *arg)
2566f11a0f46STvrtko Ursulin {
2567b318b824SVille Syrjälä 	struct drm_i915_private *dev_priv = arg;
256825286aacSDaniele Ceraolo Spurio 	void __iomem * const regs = dev_priv->uncore.regs;
2569f11a0f46STvrtko Ursulin 	u32 master_ctl;
2570f11a0f46STvrtko Ursulin 
2571f11a0f46STvrtko Ursulin 	if (!intel_irqs_enabled(dev_priv))
2572f11a0f46STvrtko Ursulin 		return IRQ_NONE;
2573f11a0f46STvrtko Ursulin 
25744376b9c9SMika Kuoppala 	master_ctl = gen8_master_intr_disable(regs);
25754376b9c9SMika Kuoppala 	if (!master_ctl) {
25764376b9c9SMika Kuoppala 		gen8_master_intr_enable(regs);
2577f11a0f46STvrtko Ursulin 		return IRQ_NONE;
25784376b9c9SMika Kuoppala 	}
2579f11a0f46STvrtko Ursulin 
25806cc32f15SChris Wilson 	/* Find, queue (onto bottom-halves), then clear each source */
25816cc32f15SChris Wilson 	gen8_gt_irq_handler(&dev_priv->gt, master_ctl);
2582f0fd96f5SChris Wilson 
2583f0fd96f5SChris Wilson 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
2584f0fd96f5SChris Wilson 	if (master_ctl & ~GEN8_GT_IRQS) {
25859102650fSDaniele Ceraolo Spurio 		disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
258655ef72f2SChris Wilson 		gen8_de_irq_handler(dev_priv, master_ctl);
25879102650fSDaniele Ceraolo Spurio 		enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
2588f0fd96f5SChris Wilson 	}
2589f11a0f46STvrtko Ursulin 
25904376b9c9SMika Kuoppala 	gen8_master_intr_enable(regs);
2591abd58f01SBen Widawsky 
25929c6508b9SThomas Gleixner 	pmu_irq_stats(dev_priv, IRQ_HANDLED);
25939c6508b9SThomas Gleixner 
259455ef72f2SChris Wilson 	return IRQ_HANDLED;
2595abd58f01SBen Widawsky }
2596abd58f01SBen Widawsky 
259751951ae7SMika Kuoppala static u32
25989b77011eSTvrtko Ursulin gen11_gu_misc_irq_ack(struct intel_gt *gt, const u32 master_ctl)
2599df0d28c1SDhinakaran Pandiyan {
26009b77011eSTvrtko Ursulin 	void __iomem * const regs = gt->uncore->regs;
26017a909383SChris Wilson 	u32 iir;
2602df0d28c1SDhinakaran Pandiyan 
2603df0d28c1SDhinakaran Pandiyan 	if (!(master_ctl & GEN11_GU_MISC_IRQ))
26047a909383SChris Wilson 		return 0;
2605df0d28c1SDhinakaran Pandiyan 
26067a909383SChris Wilson 	iir = raw_reg_read(regs, GEN11_GU_MISC_IIR);
26077a909383SChris Wilson 	if (likely(iir))
26087a909383SChris Wilson 		raw_reg_write(regs, GEN11_GU_MISC_IIR, iir);
26097a909383SChris Wilson 
26107a909383SChris Wilson 	return iir;
2611df0d28c1SDhinakaran Pandiyan }
2612df0d28c1SDhinakaran Pandiyan 
2613df0d28c1SDhinakaran Pandiyan static void
26149b77011eSTvrtko Ursulin gen11_gu_misc_irq_handler(struct intel_gt *gt, const u32 iir)
2615df0d28c1SDhinakaran Pandiyan {
2616df0d28c1SDhinakaran Pandiyan 	if (iir & GEN11_GU_MISC_GSE)
26179b77011eSTvrtko Ursulin 		intel_opregion_asle_intr(gt->i915);
2618df0d28c1SDhinakaran Pandiyan }
2619df0d28c1SDhinakaran Pandiyan 
262081067b71SMika Kuoppala static inline u32 gen11_master_intr_disable(void __iomem * const regs)
262181067b71SMika Kuoppala {
262281067b71SMika Kuoppala 	raw_reg_write(regs, GEN11_GFX_MSTR_IRQ, 0);
262381067b71SMika Kuoppala 
262481067b71SMika Kuoppala 	/*
262581067b71SMika Kuoppala 	 * Now with master disabled, get a sample of level indications
262681067b71SMika Kuoppala 	 * for this interrupt. Indications will be cleared on related acks.
262781067b71SMika Kuoppala 	 * New indications can and will light up during processing,
262881067b71SMika Kuoppala 	 * and will generate new interrupt after enabling master.
262981067b71SMika Kuoppala 	 */
263081067b71SMika Kuoppala 	return raw_reg_read(regs, GEN11_GFX_MSTR_IRQ);
263181067b71SMika Kuoppala }
263281067b71SMika Kuoppala 
263381067b71SMika Kuoppala static inline void gen11_master_intr_enable(void __iomem * const regs)
263481067b71SMika Kuoppala {
263581067b71SMika Kuoppala 	raw_reg_write(regs, GEN11_GFX_MSTR_IRQ, GEN11_MASTER_IRQ);
263681067b71SMika Kuoppala }
263781067b71SMika Kuoppala 
2638a3265d85SMatt Roper static void
2639a3265d85SMatt Roper gen11_display_irq_handler(struct drm_i915_private *i915)
2640a3265d85SMatt Roper {
2641a3265d85SMatt Roper 	void __iomem * const regs = i915->uncore.regs;
2642a3265d85SMatt Roper 	const u32 disp_ctl = raw_reg_read(regs, GEN11_DISPLAY_INT_CTL);
2643a3265d85SMatt Roper 
2644a3265d85SMatt Roper 	disable_rpm_wakeref_asserts(&i915->runtime_pm);
2645a3265d85SMatt Roper 	/*
2646a3265d85SMatt Roper 	 * GEN11_DISPLAY_INT_CTL has same format as GEN8_MASTER_IRQ
2647a3265d85SMatt Roper 	 * for the display related bits.
2648a3265d85SMatt Roper 	 */
2649a3265d85SMatt Roper 	raw_reg_write(regs, GEN11_DISPLAY_INT_CTL, 0x0);
2650a3265d85SMatt Roper 	gen8_de_irq_handler(i915, disp_ctl);
2651a3265d85SMatt Roper 	raw_reg_write(regs, GEN11_DISPLAY_INT_CTL,
2652a3265d85SMatt Roper 		      GEN11_DISPLAY_IRQ_ENABLE);
2653a3265d85SMatt Roper 
2654a3265d85SMatt Roper 	enable_rpm_wakeref_asserts(&i915->runtime_pm);
2655a3265d85SMatt Roper }
2656a3265d85SMatt Roper 
26577be8782aSLucas De Marchi static __always_inline irqreturn_t
26587be8782aSLucas De Marchi __gen11_irq_handler(struct drm_i915_private * const i915,
26597be8782aSLucas De Marchi 		    u32 (*intr_disable)(void __iomem * const regs),
26607be8782aSLucas De Marchi 		    void (*intr_enable)(void __iomem * const regs))
266151951ae7SMika Kuoppala {
266225286aacSDaniele Ceraolo Spurio 	void __iomem * const regs = i915->uncore.regs;
26639b77011eSTvrtko Ursulin 	struct intel_gt *gt = &i915->gt;
266451951ae7SMika Kuoppala 	u32 master_ctl;
2665df0d28c1SDhinakaran Pandiyan 	u32 gu_misc_iir;
266651951ae7SMika Kuoppala 
266751951ae7SMika Kuoppala 	if (!intel_irqs_enabled(i915))
266851951ae7SMika Kuoppala 		return IRQ_NONE;
266951951ae7SMika Kuoppala 
26707be8782aSLucas De Marchi 	master_ctl = intr_disable(regs);
267181067b71SMika Kuoppala 	if (!master_ctl) {
26727be8782aSLucas De Marchi 		intr_enable(regs);
267351951ae7SMika Kuoppala 		return IRQ_NONE;
267481067b71SMika Kuoppala 	}
267551951ae7SMika Kuoppala 
26766cc32f15SChris Wilson 	/* Find, queue (onto bottom-halves), then clear each source */
26779b77011eSTvrtko Ursulin 	gen11_gt_irq_handler(gt, master_ctl);
267851951ae7SMika Kuoppala 
267951951ae7SMika Kuoppala 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
2680a3265d85SMatt Roper 	if (master_ctl & GEN11_DISPLAY_IRQ)
2681a3265d85SMatt Roper 		gen11_display_irq_handler(i915);
268251951ae7SMika Kuoppala 
26839b77011eSTvrtko Ursulin 	gu_misc_iir = gen11_gu_misc_irq_ack(gt, master_ctl);
2684df0d28c1SDhinakaran Pandiyan 
26857be8782aSLucas De Marchi 	intr_enable(regs);
268651951ae7SMika Kuoppala 
26879b77011eSTvrtko Ursulin 	gen11_gu_misc_irq_handler(gt, gu_misc_iir);
2688df0d28c1SDhinakaran Pandiyan 
26899c6508b9SThomas Gleixner 	pmu_irq_stats(i915, IRQ_HANDLED);
26909c6508b9SThomas Gleixner 
269151951ae7SMika Kuoppala 	return IRQ_HANDLED;
269251951ae7SMika Kuoppala }
269351951ae7SMika Kuoppala 
26947be8782aSLucas De Marchi static irqreturn_t gen11_irq_handler(int irq, void *arg)
26957be8782aSLucas De Marchi {
26967be8782aSLucas De Marchi 	return __gen11_irq_handler(arg,
26977be8782aSLucas De Marchi 				   gen11_master_intr_disable,
26987be8782aSLucas De Marchi 				   gen11_master_intr_enable);
26997be8782aSLucas De Marchi }
27007be8782aSLucas De Marchi 
270197b492f5SLucas De Marchi static u32 dg1_master_intr_disable_and_ack(void __iomem * const regs)
270297b492f5SLucas De Marchi {
270397b492f5SLucas De Marchi 	u32 val;
270497b492f5SLucas De Marchi 
270597b492f5SLucas De Marchi 	/* First disable interrupts */
270697b492f5SLucas De Marchi 	raw_reg_write(regs, DG1_MSTR_UNIT_INTR, 0);
270797b492f5SLucas De Marchi 
270897b492f5SLucas De Marchi 	/* Get the indication levels and ack the master unit */
270997b492f5SLucas De Marchi 	val = raw_reg_read(regs, DG1_MSTR_UNIT_INTR);
271097b492f5SLucas De Marchi 	if (unlikely(!val))
271197b492f5SLucas De Marchi 		return 0;
271297b492f5SLucas De Marchi 
271397b492f5SLucas De Marchi 	raw_reg_write(regs, DG1_MSTR_UNIT_INTR, val);
271497b492f5SLucas De Marchi 
271597b492f5SLucas De Marchi 	/*
271697b492f5SLucas De Marchi 	 * Now with master disabled, get a sample of level indications
271797b492f5SLucas De Marchi 	 * for this interrupt and ack them right away - we keep GEN11_MASTER_IRQ
271897b492f5SLucas De Marchi 	 * out as this bit doesn't exist anymore for DG1
271997b492f5SLucas De Marchi 	 */
272097b492f5SLucas De Marchi 	val = raw_reg_read(regs, GEN11_GFX_MSTR_IRQ) & ~GEN11_MASTER_IRQ;
272197b492f5SLucas De Marchi 	if (unlikely(!val))
272297b492f5SLucas De Marchi 		return 0;
272397b492f5SLucas De Marchi 
272497b492f5SLucas De Marchi 	raw_reg_write(regs, GEN11_GFX_MSTR_IRQ, val);
272597b492f5SLucas De Marchi 
272697b492f5SLucas De Marchi 	return val;
272797b492f5SLucas De Marchi }
272897b492f5SLucas De Marchi 
272997b492f5SLucas De Marchi static inline void dg1_master_intr_enable(void __iomem * const regs)
273097b492f5SLucas De Marchi {
273197b492f5SLucas De Marchi 	raw_reg_write(regs, DG1_MSTR_UNIT_INTR, DG1_MSTR_IRQ);
273297b492f5SLucas De Marchi }
273397b492f5SLucas De Marchi 
273497b492f5SLucas De Marchi static irqreturn_t dg1_irq_handler(int irq, void *arg)
273597b492f5SLucas De Marchi {
273697b492f5SLucas De Marchi 	return __gen11_irq_handler(arg,
273797b492f5SLucas De Marchi 				   dg1_master_intr_disable_and_ack,
273897b492f5SLucas De Marchi 				   dg1_master_intr_enable);
273997b492f5SLucas De Marchi }
274097b492f5SLucas De Marchi 
274142f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
274242f52ef8SKeith Packard  * we use as a pipe index
274342f52ef8SKeith Packard  */
274408fa8fd0SVille Syrjälä int i8xx_enable_vblank(struct drm_crtc *crtc)
27450a3e67a4SJesse Barnes {
274608fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
274708fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
2748e9d21d7fSKeith Packard 	unsigned long irqflags;
274971e0ffa5SJesse Barnes 
27501ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
275186e83e35SChris Wilson 	i915_enable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_STATUS);
275286e83e35SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
275386e83e35SChris Wilson 
275486e83e35SChris Wilson 	return 0;
275586e83e35SChris Wilson }
275686e83e35SChris Wilson 
27577d423af9SVille Syrjälä int i915gm_enable_vblank(struct drm_crtc *crtc)
2758d938da6bSVille Syrjälä {
275908fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
2760d938da6bSVille Syrjälä 
27617d423af9SVille Syrjälä 	/*
27627d423af9SVille Syrjälä 	 * Vblank interrupts fail to wake the device up from C2+.
27637d423af9SVille Syrjälä 	 * Disabling render clock gating during C-states avoids
27647d423af9SVille Syrjälä 	 * the problem. There is a small power cost so we do this
27657d423af9SVille Syrjälä 	 * only when vblank interrupts are actually enabled.
27667d423af9SVille Syrjälä 	 */
27677d423af9SVille Syrjälä 	if (dev_priv->vblank_enabled++ == 0)
27682939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, SCPD0, _MASKED_BIT_ENABLE(CSTATE_RENDER_CLOCK_GATE_DISABLE));
2769d938da6bSVille Syrjälä 
277008fa8fd0SVille Syrjälä 	return i8xx_enable_vblank(crtc);
2771d938da6bSVille Syrjälä }
2772d938da6bSVille Syrjälä 
277308fa8fd0SVille Syrjälä int i965_enable_vblank(struct drm_crtc *crtc)
277486e83e35SChris Wilson {
277508fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
277608fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
277786e83e35SChris Wilson 	unsigned long irqflags;
277886e83e35SChris Wilson 
277986e83e35SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
27807c463586SKeith Packard 	i915_enable_pipestat(dev_priv, pipe,
2781755e9019SImre Deak 			     PIPE_START_VBLANK_INTERRUPT_STATUS);
27821ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
27838692d00eSChris Wilson 
27840a3e67a4SJesse Barnes 	return 0;
27850a3e67a4SJesse Barnes }
27860a3e67a4SJesse Barnes 
278708fa8fd0SVille Syrjälä int ilk_enable_vblank(struct drm_crtc *crtc)
2788f796cf8fSJesse Barnes {
278908fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
279008fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
2791f796cf8fSJesse Barnes 	unsigned long irqflags;
2792a9c287c9SJani Nikula 	u32 bit = INTEL_GEN(dev_priv) >= 7 ?
279386e83e35SChris Wilson 		DE_PIPE_VBLANK_IVB(pipe) : DE_PIPE_VBLANK(pipe);
2794f796cf8fSJesse Barnes 
2795f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2796fbdedaeaSVille Syrjälä 	ilk_enable_display_irq(dev_priv, bit);
2797b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2798b1f14ad0SJesse Barnes 
27992e8bf223SDhinakaran Pandiyan 	/* Even though there is no DMC, frame counter can get stuck when
28002e8bf223SDhinakaran Pandiyan 	 * PSR is active as no frames are generated.
28012e8bf223SDhinakaran Pandiyan 	 */
28022e8bf223SDhinakaran Pandiyan 	if (HAS_PSR(dev_priv))
280308fa8fd0SVille Syrjälä 		drm_crtc_vblank_restore(crtc);
28042e8bf223SDhinakaran Pandiyan 
2805b1f14ad0SJesse Barnes 	return 0;
2806b1f14ad0SJesse Barnes }
2807b1f14ad0SJesse Barnes 
28089c9e97c4SVandita Kulkarni static bool gen11_dsi_configure_te(struct intel_crtc *intel_crtc,
28099c9e97c4SVandita Kulkarni 				   bool enable)
28109c9e97c4SVandita Kulkarni {
28119c9e97c4SVandita Kulkarni 	struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
28129c9e97c4SVandita Kulkarni 	enum port port;
28139c9e97c4SVandita Kulkarni 	u32 tmp;
28149c9e97c4SVandita Kulkarni 
28159c9e97c4SVandita Kulkarni 	if (!(intel_crtc->mode_flags &
28169c9e97c4SVandita Kulkarni 	    (I915_MODE_FLAG_DSI_USE_TE1 | I915_MODE_FLAG_DSI_USE_TE0)))
28179c9e97c4SVandita Kulkarni 		return false;
28189c9e97c4SVandita Kulkarni 
28199c9e97c4SVandita Kulkarni 	/* for dual link cases we consider TE from slave */
28209c9e97c4SVandita Kulkarni 	if (intel_crtc->mode_flags & I915_MODE_FLAG_DSI_USE_TE1)
28219c9e97c4SVandita Kulkarni 		port = PORT_B;
28229c9e97c4SVandita Kulkarni 	else
28239c9e97c4SVandita Kulkarni 		port = PORT_A;
28249c9e97c4SVandita Kulkarni 
28252939eb06SJani Nikula 	tmp =  intel_uncore_read(&dev_priv->uncore, DSI_INTR_MASK_REG(port));
28269c9e97c4SVandita Kulkarni 	if (enable)
28279c9e97c4SVandita Kulkarni 		tmp &= ~DSI_TE_EVENT;
28289c9e97c4SVandita Kulkarni 	else
28299c9e97c4SVandita Kulkarni 		tmp |= DSI_TE_EVENT;
28309c9e97c4SVandita Kulkarni 
28312939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, DSI_INTR_MASK_REG(port), tmp);
28329c9e97c4SVandita Kulkarni 
28332939eb06SJani Nikula 	tmp = intel_uncore_read(&dev_priv->uncore, DSI_INTR_IDENT_REG(port));
28342939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, DSI_INTR_IDENT_REG(port), tmp);
28359c9e97c4SVandita Kulkarni 
28369c9e97c4SVandita Kulkarni 	return true;
28379c9e97c4SVandita Kulkarni }
28389c9e97c4SVandita Kulkarni 
283908fa8fd0SVille Syrjälä int bdw_enable_vblank(struct drm_crtc *crtc)
2840abd58f01SBen Widawsky {
284108fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
28429c9e97c4SVandita Kulkarni 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
28439c9e97c4SVandita Kulkarni 	enum pipe pipe = intel_crtc->pipe;
2844abd58f01SBen Widawsky 	unsigned long irqflags;
2845abd58f01SBen Widawsky 
28469c9e97c4SVandita Kulkarni 	if (gen11_dsi_configure_te(intel_crtc, true))
28479c9e97c4SVandita Kulkarni 		return 0;
28489c9e97c4SVandita Kulkarni 
2849abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2850013d3752SVille Syrjälä 	bdw_enable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
2851abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2852013d3752SVille Syrjälä 
28532e8bf223SDhinakaran Pandiyan 	/* Even if there is no DMC, frame counter can get stuck when
28542e8bf223SDhinakaran Pandiyan 	 * PSR is active as no frames are generated, so check only for PSR.
28552e8bf223SDhinakaran Pandiyan 	 */
28562e8bf223SDhinakaran Pandiyan 	if (HAS_PSR(dev_priv))
285708fa8fd0SVille Syrjälä 		drm_crtc_vblank_restore(crtc);
28582e8bf223SDhinakaran Pandiyan 
2859abd58f01SBen Widawsky 	return 0;
2860abd58f01SBen Widawsky }
2861abd58f01SBen Widawsky 
286242f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
286342f52ef8SKeith Packard  * we use as a pipe index
286442f52ef8SKeith Packard  */
286508fa8fd0SVille Syrjälä void i8xx_disable_vblank(struct drm_crtc *crtc)
286686e83e35SChris Wilson {
286708fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
286808fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
286986e83e35SChris Wilson 	unsigned long irqflags;
287086e83e35SChris Wilson 
287186e83e35SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
287286e83e35SChris Wilson 	i915_disable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_STATUS);
287386e83e35SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
287486e83e35SChris Wilson }
287586e83e35SChris Wilson 
28767d423af9SVille Syrjälä void i915gm_disable_vblank(struct drm_crtc *crtc)
2877d938da6bSVille Syrjälä {
287808fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
2879d938da6bSVille Syrjälä 
288008fa8fd0SVille Syrjälä 	i8xx_disable_vblank(crtc);
2881d938da6bSVille Syrjälä 
28827d423af9SVille Syrjälä 	if (--dev_priv->vblank_enabled == 0)
28832939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, SCPD0, _MASKED_BIT_DISABLE(CSTATE_RENDER_CLOCK_GATE_DISABLE));
2884d938da6bSVille Syrjälä }
2885d938da6bSVille Syrjälä 
288608fa8fd0SVille Syrjälä void i965_disable_vblank(struct drm_crtc *crtc)
28870a3e67a4SJesse Barnes {
288808fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
288908fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
2890e9d21d7fSKeith Packard 	unsigned long irqflags;
28910a3e67a4SJesse Barnes 
28921ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
28937c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
2894755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
28951ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
28960a3e67a4SJesse Barnes }
28970a3e67a4SJesse Barnes 
289808fa8fd0SVille Syrjälä void ilk_disable_vblank(struct drm_crtc *crtc)
2899f796cf8fSJesse Barnes {
290008fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
290108fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
2902f796cf8fSJesse Barnes 	unsigned long irqflags;
2903a9c287c9SJani Nikula 	u32 bit = INTEL_GEN(dev_priv) >= 7 ?
290486e83e35SChris Wilson 		DE_PIPE_VBLANK_IVB(pipe) : DE_PIPE_VBLANK(pipe);
2905f796cf8fSJesse Barnes 
2906f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2907fbdedaeaSVille Syrjälä 	ilk_disable_display_irq(dev_priv, bit);
2908b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2909b1f14ad0SJesse Barnes }
2910b1f14ad0SJesse Barnes 
291108fa8fd0SVille Syrjälä void bdw_disable_vblank(struct drm_crtc *crtc)
2912abd58f01SBen Widawsky {
291308fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
29149c9e97c4SVandita Kulkarni 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
29159c9e97c4SVandita Kulkarni 	enum pipe pipe = intel_crtc->pipe;
2916abd58f01SBen Widawsky 	unsigned long irqflags;
2917abd58f01SBen Widawsky 
29189c9e97c4SVandita Kulkarni 	if (gen11_dsi_configure_te(intel_crtc, false))
29199c9e97c4SVandita Kulkarni 		return;
29209c9e97c4SVandita Kulkarni 
2921abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2922013d3752SVille Syrjälä 	bdw_disable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
2923abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2924abd58f01SBen Widawsky }
2925abd58f01SBen Widawsky 
2926b243f530STvrtko Ursulin static void ibx_irq_reset(struct drm_i915_private *dev_priv)
292791738a95SPaulo Zanoni {
2928b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
2929b16b2a2fSPaulo Zanoni 
29306e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
293191738a95SPaulo Zanoni 		return;
293291738a95SPaulo Zanoni 
2933b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, SDE);
2934105b122eSPaulo Zanoni 
29356e266956STvrtko Ursulin 	if (HAS_PCH_CPT(dev_priv) || HAS_PCH_LPT(dev_priv))
29362939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, SERR_INT, 0xffffffff);
2937622364b6SPaulo Zanoni }
2938105b122eSPaulo Zanoni 
293970591a41SVille Syrjälä static void vlv_display_irq_reset(struct drm_i915_private *dev_priv)
294070591a41SVille Syrjälä {
2941b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
2942b16b2a2fSPaulo Zanoni 
294371b8b41dSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
2944f0818984STvrtko Ursulin 		intel_uncore_write(uncore, DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
294571b8b41dSVille Syrjälä 	else
2946f0818984STvrtko Ursulin 		intel_uncore_write(uncore, DPINVGTT, DPINVGTT_STATUS_MASK);
294771b8b41dSVille Syrjälä 
2948ad22d106SVille Syrjälä 	i915_hotplug_interrupt_update_locked(dev_priv, 0xffffffff, 0);
29492939eb06SJani Nikula 	intel_uncore_write(uncore, PORT_HOTPLUG_STAT, intel_uncore_read(&dev_priv->uncore, PORT_HOTPLUG_STAT));
295070591a41SVille Syrjälä 
295144d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
295270591a41SVille Syrjälä 
2953b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, VLV_);
29548bd099a7SChris Wilson 	dev_priv->irq_mask = ~0u;
295570591a41SVille Syrjälä }
295670591a41SVille Syrjälä 
29578bb61306SVille Syrjälä static void vlv_display_irq_postinstall(struct drm_i915_private *dev_priv)
29588bb61306SVille Syrjälä {
2959b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
2960b16b2a2fSPaulo Zanoni 
29618bb61306SVille Syrjälä 	u32 pipestat_mask;
29629ab981f2SVille Syrjälä 	u32 enable_mask;
29638bb61306SVille Syrjälä 	enum pipe pipe;
29648bb61306SVille Syrjälä 
2965842ebf7aSVille Syrjälä 	pipestat_mask = PIPE_CRC_DONE_INTERRUPT_STATUS;
29668bb61306SVille Syrjälä 
29678bb61306SVille Syrjälä 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
29688bb61306SVille Syrjälä 	for_each_pipe(dev_priv, pipe)
29698bb61306SVille Syrjälä 		i915_enable_pipestat(dev_priv, pipe, pipestat_mask);
29708bb61306SVille Syrjälä 
29719ab981f2SVille Syrjälä 	enable_mask = I915_DISPLAY_PORT_INTERRUPT |
29728bb61306SVille Syrjälä 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2973ebf5f921SVille Syrjälä 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2974ebf5f921SVille Syrjälä 		I915_LPE_PIPE_A_INTERRUPT |
2975ebf5f921SVille Syrjälä 		I915_LPE_PIPE_B_INTERRUPT;
2976ebf5f921SVille Syrjälä 
29778bb61306SVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
2978ebf5f921SVille Syrjälä 		enable_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT |
2979ebf5f921SVille Syrjälä 			I915_LPE_PIPE_C_INTERRUPT;
29806b7eafc1SVille Syrjälä 
298148a1b8d4SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, dev_priv->irq_mask != ~0u);
29826b7eafc1SVille Syrjälä 
29839ab981f2SVille Syrjälä 	dev_priv->irq_mask = ~enable_mask;
29848bb61306SVille Syrjälä 
2985b16b2a2fSPaulo Zanoni 	GEN3_IRQ_INIT(uncore, VLV_, dev_priv->irq_mask, enable_mask);
29868bb61306SVille Syrjälä }
29878bb61306SVille Syrjälä 
29888bb61306SVille Syrjälä /* drm_dma.h hooks
29898bb61306SVille Syrjälä */
29909eae5e27SLucas De Marchi static void ilk_irq_reset(struct drm_i915_private *dev_priv)
29918bb61306SVille Syrjälä {
2992b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
29938bb61306SVille Syrjälä 
2994b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, DE);
2995e44adb5dSChris Wilson 	dev_priv->irq_mask = ~0u;
2996e44adb5dSChris Wilson 
2997cf819effSLucas De Marchi 	if (IS_GEN(dev_priv, 7))
2998f0818984STvrtko Ursulin 		intel_uncore_write(uncore, GEN7_ERR_INT, 0xffffffff);
29998bb61306SVille Syrjälä 
3000fc340442SDaniel Vetter 	if (IS_HASWELL(dev_priv)) {
3001f0818984STvrtko Ursulin 		intel_uncore_write(uncore, EDP_PSR_IMR, 0xffffffff);
3002f0818984STvrtko Ursulin 		intel_uncore_write(uncore, EDP_PSR_IIR, 0xffffffff);
3003fc340442SDaniel Vetter 	}
3004fc340442SDaniel Vetter 
3005cf1c97dcSAndi Shyti 	gen5_gt_irq_reset(&dev_priv->gt);
30068bb61306SVille Syrjälä 
3007b243f530STvrtko Ursulin 	ibx_irq_reset(dev_priv);
30088bb61306SVille Syrjälä }
30098bb61306SVille Syrjälä 
3010b318b824SVille Syrjälä static void valleyview_irq_reset(struct drm_i915_private *dev_priv)
30117e231dbeSJesse Barnes {
30122939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, VLV_MASTER_IER, 0);
30132939eb06SJani Nikula 	intel_uncore_posting_read(&dev_priv->uncore, VLV_MASTER_IER);
301434c7b8a7SVille Syrjälä 
3015cf1c97dcSAndi Shyti 	gen5_gt_irq_reset(&dev_priv->gt);
30167e231dbeSJesse Barnes 
3017ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
30189918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
301970591a41SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3020ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
30217e231dbeSJesse Barnes }
30227e231dbeSJesse Barnes 
3023b318b824SVille Syrjälä static void gen8_irq_reset(struct drm_i915_private *dev_priv)
3024abd58f01SBen Widawsky {
3025b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
3026d048a268SVille Syrjälä 	enum pipe pipe;
3027abd58f01SBen Widawsky 
302825286aacSDaniele Ceraolo Spurio 	gen8_master_intr_disable(dev_priv->uncore.regs);
3029abd58f01SBen Widawsky 
3030cf1c97dcSAndi Shyti 	gen8_gt_irq_reset(&dev_priv->gt);
3031abd58f01SBen Widawsky 
3032f0818984STvrtko Ursulin 	intel_uncore_write(uncore, EDP_PSR_IMR, 0xffffffff);
3033f0818984STvrtko Ursulin 	intel_uncore_write(uncore, EDP_PSR_IIR, 0xffffffff);
3034e04f7eceSVille Syrjälä 
3035055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3036f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3037813bde43SPaulo Zanoni 						   POWER_DOMAIN_PIPE(pipe)))
3038b16b2a2fSPaulo Zanoni 			GEN8_IRQ_RESET_NDX(uncore, DE_PIPE, pipe);
3039abd58f01SBen Widawsky 
3040b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN8_DE_PORT_);
3041b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN8_DE_MISC_);
3042b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN8_PCU_);
3043abd58f01SBen Widawsky 
30446e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
3045b243f530STvrtko Ursulin 		ibx_irq_reset(dev_priv);
3046abd58f01SBen Widawsky }
3047abd58f01SBen Widawsky 
3048a3265d85SMatt Roper static void gen11_display_irq_reset(struct drm_i915_private *dev_priv)
304951951ae7SMika Kuoppala {
3050b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
3051d048a268SVille Syrjälä 	enum pipe pipe;
3052562ddcb7SMatt Roper 	u32 trans_mask = BIT(TRANSCODER_A) | BIT(TRANSCODER_B) |
3053562ddcb7SMatt Roper 		BIT(TRANSCODER_C) | BIT(TRANSCODER_D);
305451951ae7SMika Kuoppala 
3055f0818984STvrtko Ursulin 	intel_uncore_write(uncore, GEN11_DISPLAY_INT_CTL, 0);
305651951ae7SMika Kuoppala 
30578241cfbeSJosé Roberto de Souza 	if (INTEL_GEN(dev_priv) >= 12) {
30588241cfbeSJosé Roberto de Souza 		enum transcoder trans;
30598241cfbeSJosé Roberto de Souza 
3060562ddcb7SMatt Roper 		for_each_cpu_transcoder_masked(dev_priv, trans, trans_mask) {
30618241cfbeSJosé Roberto de Souza 			enum intel_display_power_domain domain;
30628241cfbeSJosé Roberto de Souza 
30638241cfbeSJosé Roberto de Souza 			domain = POWER_DOMAIN_TRANSCODER(trans);
30648241cfbeSJosé Roberto de Souza 			if (!intel_display_power_is_enabled(dev_priv, domain))
30658241cfbeSJosé Roberto de Souza 				continue;
30668241cfbeSJosé Roberto de Souza 
30678241cfbeSJosé Roberto de Souza 			intel_uncore_write(uncore, TRANS_PSR_IMR(trans), 0xffffffff);
30688241cfbeSJosé Roberto de Souza 			intel_uncore_write(uncore, TRANS_PSR_IIR(trans), 0xffffffff);
30698241cfbeSJosé Roberto de Souza 		}
30708241cfbeSJosé Roberto de Souza 	} else {
3071f0818984STvrtko Ursulin 		intel_uncore_write(uncore, EDP_PSR_IMR, 0xffffffff);
3072f0818984STvrtko Ursulin 		intel_uncore_write(uncore, EDP_PSR_IIR, 0xffffffff);
30738241cfbeSJosé Roberto de Souza 	}
307462819dfdSJosé Roberto de Souza 
307551951ae7SMika Kuoppala 	for_each_pipe(dev_priv, pipe)
307651951ae7SMika Kuoppala 		if (intel_display_power_is_enabled(dev_priv,
307751951ae7SMika Kuoppala 						   POWER_DOMAIN_PIPE(pipe)))
3078b16b2a2fSPaulo Zanoni 			GEN8_IRQ_RESET_NDX(uncore, DE_PIPE, pipe);
307951951ae7SMika Kuoppala 
3080b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN8_DE_PORT_);
3081b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN8_DE_MISC_);
3082b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN11_DE_HPD_);
308331604222SAnusha Srivatsa 
308429b43ae2SRodrigo Vivi 	if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
3085b16b2a2fSPaulo Zanoni 		GEN3_IRQ_RESET(uncore, SDE);
30869b2383a7SMatt Roper 
3087b896898cSBob Paauwe 	/* Wa_14010685332:cnp/cmp,tgp,adp */
3088b896898cSBob Paauwe 	if (INTEL_PCH_TYPE(dev_priv) == PCH_CNP ||
3089b896898cSBob Paauwe 	    (INTEL_PCH_TYPE(dev_priv) >= PCH_TGP &&
3090b896898cSBob Paauwe 	     INTEL_PCH_TYPE(dev_priv) < PCH_DG1)) {
30919b2383a7SMatt Roper 		intel_uncore_rmw(uncore, SOUTH_CHICKEN1,
30929b2383a7SMatt Roper 				 SBCLK_RUN_REFCLK_DIS, SBCLK_RUN_REFCLK_DIS);
30939b2383a7SMatt Roper 		intel_uncore_rmw(uncore, SOUTH_CHICKEN1,
30949b2383a7SMatt Roper 				 SBCLK_RUN_REFCLK_DIS, 0);
30959b2383a7SMatt Roper 	}
309651951ae7SMika Kuoppala }
309751951ae7SMika Kuoppala 
3098a3265d85SMatt Roper static void gen11_irq_reset(struct drm_i915_private *dev_priv)
3099a3265d85SMatt Roper {
3100a3265d85SMatt Roper 	struct intel_uncore *uncore = &dev_priv->uncore;
3101a3265d85SMatt Roper 
310297b492f5SLucas De Marchi 	if (HAS_MASTER_UNIT_IRQ(dev_priv))
310397b492f5SLucas De Marchi 		dg1_master_intr_disable_and_ack(dev_priv->uncore.regs);
310497b492f5SLucas De Marchi 	else
3105a3265d85SMatt Roper 		gen11_master_intr_disable(dev_priv->uncore.regs);
3106a3265d85SMatt Roper 
3107a3265d85SMatt Roper 	gen11_gt_irq_reset(&dev_priv->gt);
3108a3265d85SMatt Roper 	gen11_display_irq_reset(dev_priv);
3109a3265d85SMatt Roper 
3110a3265d85SMatt Roper 	GEN3_IRQ_RESET(uncore, GEN11_GU_MISC_);
3111a3265d85SMatt Roper 	GEN3_IRQ_RESET(uncore, GEN8_PCU_);
3112a3265d85SMatt Roper }
3113a3265d85SMatt Roper 
31144c6c03beSDamien Lespiau void gen8_irq_power_well_post_enable(struct drm_i915_private *dev_priv,
3115001bd2cbSImre Deak 				     u8 pipe_mask)
3116d49bdb0eSPaulo Zanoni {
3117b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
3118cda195f1SVille Syrjälä 	u32 extra_ier = GEN8_PIPE_VBLANK | GEN8_PIPE_FIFO_UNDERRUN |
3119cda195f1SVille Syrjälä 		gen8_de_pipe_flip_done_mask(dev_priv);
31206831f3e3SVille Syrjälä 	enum pipe pipe;
3121d49bdb0eSPaulo Zanoni 
312213321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
31239dfe2e3aSImre Deak 
31249dfe2e3aSImre Deak 	if (!intel_irqs_enabled(dev_priv)) {
31259dfe2e3aSImre Deak 		spin_unlock_irq(&dev_priv->irq_lock);
31269dfe2e3aSImre Deak 		return;
31279dfe2e3aSImre Deak 	}
31289dfe2e3aSImre Deak 
31296831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
3130b16b2a2fSPaulo Zanoni 		GEN8_IRQ_INIT_NDX(uncore, DE_PIPE, pipe,
31316831f3e3SVille Syrjälä 				  dev_priv->de_irq_mask[pipe],
31326831f3e3SVille Syrjälä 				  ~dev_priv->de_irq_mask[pipe] | extra_ier);
31339dfe2e3aSImre Deak 
313413321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3135d49bdb0eSPaulo Zanoni }
3136d49bdb0eSPaulo Zanoni 
3137aae8ba84SVille Syrjälä void gen8_irq_power_well_pre_disable(struct drm_i915_private *dev_priv,
3138001bd2cbSImre Deak 				     u8 pipe_mask)
3139aae8ba84SVille Syrjälä {
3140b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
31416831f3e3SVille Syrjälä 	enum pipe pipe;
31426831f3e3SVille Syrjälä 
3143aae8ba84SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
31449dfe2e3aSImre Deak 
31459dfe2e3aSImre Deak 	if (!intel_irqs_enabled(dev_priv)) {
31469dfe2e3aSImre Deak 		spin_unlock_irq(&dev_priv->irq_lock);
31479dfe2e3aSImre Deak 		return;
31489dfe2e3aSImre Deak 	}
31499dfe2e3aSImre Deak 
31506831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
3151b16b2a2fSPaulo Zanoni 		GEN8_IRQ_RESET_NDX(uncore, DE_PIPE, pipe);
31529dfe2e3aSImre Deak 
3153aae8ba84SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3154aae8ba84SVille Syrjälä 
3155aae8ba84SVille Syrjälä 	/* make sure we're done processing display irqs */
3156315ca4c4SVille Syrjälä 	intel_synchronize_irq(dev_priv);
3157aae8ba84SVille Syrjälä }
3158aae8ba84SVille Syrjälä 
3159b318b824SVille Syrjälä static void cherryview_irq_reset(struct drm_i915_private *dev_priv)
316043f328d7SVille Syrjälä {
3161b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
316243f328d7SVille Syrjälä 
31632939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, GEN8_MASTER_IRQ, 0);
31642939eb06SJani Nikula 	intel_uncore_posting_read(&dev_priv->uncore, GEN8_MASTER_IRQ);
316543f328d7SVille Syrjälä 
3166cf1c97dcSAndi Shyti 	gen8_gt_irq_reset(&dev_priv->gt);
316743f328d7SVille Syrjälä 
3168b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN8_PCU_);
316943f328d7SVille Syrjälä 
3170ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
31719918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
317270591a41SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3173ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
317443f328d7SVille Syrjälä }
317543f328d7SVille Syrjälä 
31762ea63927SVille Syrjälä static u32 ibx_hotplug_enables(struct drm_i915_private *i915,
31772ea63927SVille Syrjälä 			       enum hpd_pin pin)
31782ea63927SVille Syrjälä {
31792ea63927SVille Syrjälä 	switch (pin) {
31802ea63927SVille Syrjälä 	case HPD_PORT_A:
31812ea63927SVille Syrjälä 		/*
31822ea63927SVille Syrjälä 		 * When CPU and PCH are on the same package, port A
31832ea63927SVille Syrjälä 		 * HPD must be enabled in both north and south.
31842ea63927SVille Syrjälä 		 */
31852ea63927SVille Syrjälä 		return HAS_PCH_LPT_LP(i915) ?
31862ea63927SVille Syrjälä 			PORTA_HOTPLUG_ENABLE : 0;
31872ea63927SVille Syrjälä 	case HPD_PORT_B:
31882ea63927SVille Syrjälä 		return PORTB_HOTPLUG_ENABLE |
31892ea63927SVille Syrjälä 			PORTB_PULSE_DURATION_2ms;
31902ea63927SVille Syrjälä 	case HPD_PORT_C:
31912ea63927SVille Syrjälä 		return PORTC_HOTPLUG_ENABLE |
31922ea63927SVille Syrjälä 			PORTC_PULSE_DURATION_2ms;
31932ea63927SVille Syrjälä 	case HPD_PORT_D:
31942ea63927SVille Syrjälä 		return PORTD_HOTPLUG_ENABLE |
31952ea63927SVille Syrjälä 			PORTD_PULSE_DURATION_2ms;
31962ea63927SVille Syrjälä 	default:
31972ea63927SVille Syrjälä 		return 0;
31982ea63927SVille Syrjälä 	}
31992ea63927SVille Syrjälä }
32002ea63927SVille Syrjälä 
32011a56b1a2SImre Deak static void ibx_hpd_detection_setup(struct drm_i915_private *dev_priv)
32021a56b1a2SImre Deak {
32031a56b1a2SImre Deak 	u32 hotplug;
32041a56b1a2SImre Deak 
32051a56b1a2SImre Deak 	/*
32061a56b1a2SImre Deak 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
32071a56b1a2SImre Deak 	 * duration to 2ms (which is the minimum in the Display Port spec).
32081a56b1a2SImre Deak 	 * The pulse duration bits are reserved on LPT+.
32091a56b1a2SImre Deak 	 */
32102939eb06SJani Nikula 	hotplug = intel_uncore_read(&dev_priv->uncore, PCH_PORT_HOTPLUG);
32112ea63927SVille Syrjälä 	hotplug &= ~(PORTA_HOTPLUG_ENABLE |
32122ea63927SVille Syrjälä 		     PORTB_HOTPLUG_ENABLE |
32132ea63927SVille Syrjälä 		     PORTC_HOTPLUG_ENABLE |
32142ea63927SVille Syrjälä 		     PORTD_HOTPLUG_ENABLE |
32152ea63927SVille Syrjälä 		     PORTB_PULSE_DURATION_MASK |
32161a56b1a2SImre Deak 		     PORTC_PULSE_DURATION_MASK |
32171a56b1a2SImre Deak 		     PORTD_PULSE_DURATION_MASK);
32182ea63927SVille Syrjälä 	hotplug |= intel_hpd_hotplug_enables(dev_priv, ibx_hotplug_enables);
32192939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, PCH_PORT_HOTPLUG, hotplug);
32201a56b1a2SImre Deak }
32211a56b1a2SImre Deak 
322291d14251STvrtko Ursulin static void ibx_hpd_irq_setup(struct drm_i915_private *dev_priv)
322382a28bcfSDaniel Vetter {
32241a56b1a2SImre Deak 	u32 hotplug_irqs, enabled_irqs;
322582a28bcfSDaniel Vetter 
32260398993bSVille Syrjälä 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, dev_priv->hotplug.pch_hpd);
32276d3144ebSVille Syrjälä 	hotplug_irqs = intel_hpd_hotplug_irqs(dev_priv, dev_priv->hotplug.pch_hpd);
322882a28bcfSDaniel Vetter 
3229fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
323082a28bcfSDaniel Vetter 
32311a56b1a2SImre Deak 	ibx_hpd_detection_setup(dev_priv);
32326dbf30ceSVille Syrjälä }
323326951cafSXiong Zhang 
32342ea63927SVille Syrjälä static u32 icp_ddi_hotplug_enables(struct drm_i915_private *i915,
32352ea63927SVille Syrjälä 				   enum hpd_pin pin)
32362ea63927SVille Syrjälä {
32372ea63927SVille Syrjälä 	switch (pin) {
32382ea63927SVille Syrjälä 	case HPD_PORT_A:
32392ea63927SVille Syrjälä 	case HPD_PORT_B:
32402ea63927SVille Syrjälä 	case HPD_PORT_C:
32412ea63927SVille Syrjälä 	case HPD_PORT_D:
32422ea63927SVille Syrjälä 		return SHOTPLUG_CTL_DDI_HPD_ENABLE(pin);
32432ea63927SVille Syrjälä 	default:
32442ea63927SVille Syrjälä 		return 0;
32452ea63927SVille Syrjälä 	}
32462ea63927SVille Syrjälä }
32472ea63927SVille Syrjälä 
32482ea63927SVille Syrjälä static u32 icp_tc_hotplug_enables(struct drm_i915_private *i915,
32492ea63927SVille Syrjälä 				  enum hpd_pin pin)
32502ea63927SVille Syrjälä {
32512ea63927SVille Syrjälä 	switch (pin) {
32522ea63927SVille Syrjälä 	case HPD_PORT_TC1:
32532ea63927SVille Syrjälä 	case HPD_PORT_TC2:
32542ea63927SVille Syrjälä 	case HPD_PORT_TC3:
32552ea63927SVille Syrjälä 	case HPD_PORT_TC4:
32562ea63927SVille Syrjälä 	case HPD_PORT_TC5:
32572ea63927SVille Syrjälä 	case HPD_PORT_TC6:
32582ea63927SVille Syrjälä 		return ICP_TC_HPD_ENABLE(pin);
32592ea63927SVille Syrjälä 	default:
32602ea63927SVille Syrjälä 		return 0;
32612ea63927SVille Syrjälä 	}
32622ea63927SVille Syrjälä }
32632ea63927SVille Syrjälä 
32642ea63927SVille Syrjälä static void icp_ddi_hpd_detection_setup(struct drm_i915_private *dev_priv)
326531604222SAnusha Srivatsa {
326631604222SAnusha Srivatsa 	u32 hotplug;
326731604222SAnusha Srivatsa 
32682939eb06SJani Nikula 	hotplug = intel_uncore_read(&dev_priv->uncore, SHOTPLUG_CTL_DDI);
32692ea63927SVille Syrjälä 	hotplug &= ~(SHOTPLUG_CTL_DDI_HPD_ENABLE(HPD_PORT_A) |
32702ea63927SVille Syrjälä 		     SHOTPLUG_CTL_DDI_HPD_ENABLE(HPD_PORT_B) |
32712ea63927SVille Syrjälä 		     SHOTPLUG_CTL_DDI_HPD_ENABLE(HPD_PORT_C) |
32722ea63927SVille Syrjälä 		     SHOTPLUG_CTL_DDI_HPD_ENABLE(HPD_PORT_D));
32732ea63927SVille Syrjälä 	hotplug |= intel_hpd_hotplug_enables(dev_priv, icp_ddi_hotplug_enables);
32742939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, SHOTPLUG_CTL_DDI, hotplug);
327531604222SAnusha Srivatsa }
3276815f4ef2SVille Syrjälä 
32772ea63927SVille Syrjälä static void icp_tc_hpd_detection_setup(struct drm_i915_private *dev_priv)
3278815f4ef2SVille Syrjälä {
3279815f4ef2SVille Syrjälä 	u32 hotplug;
3280815f4ef2SVille Syrjälä 
32812939eb06SJani Nikula 	hotplug = intel_uncore_read(&dev_priv->uncore, SHOTPLUG_CTL_TC);
32822ea63927SVille Syrjälä 	hotplug &= ~(ICP_TC_HPD_ENABLE(HPD_PORT_TC1) |
32832ea63927SVille Syrjälä 		     ICP_TC_HPD_ENABLE(HPD_PORT_TC2) |
32842ea63927SVille Syrjälä 		     ICP_TC_HPD_ENABLE(HPD_PORT_TC3) |
32852ea63927SVille Syrjälä 		     ICP_TC_HPD_ENABLE(HPD_PORT_TC4) |
32862ea63927SVille Syrjälä 		     ICP_TC_HPD_ENABLE(HPD_PORT_TC5) |
32872ea63927SVille Syrjälä 		     ICP_TC_HPD_ENABLE(HPD_PORT_TC6));
32882ea63927SVille Syrjälä 	hotplug |= intel_hpd_hotplug_enables(dev_priv, icp_tc_hotplug_enables);
32892939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, SHOTPLUG_CTL_TC, hotplug);
32908ef7e340SMatt Roper }
329131604222SAnusha Srivatsa 
32922ea63927SVille Syrjälä static void icp_hpd_irq_setup(struct drm_i915_private *dev_priv)
329331604222SAnusha Srivatsa {
329431604222SAnusha Srivatsa 	u32 hotplug_irqs, enabled_irqs;
329531604222SAnusha Srivatsa 
32960398993bSVille Syrjälä 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, dev_priv->hotplug.pch_hpd);
32976d3144ebSVille Syrjälä 	hotplug_irqs = intel_hpd_hotplug_irqs(dev_priv, dev_priv->hotplug.pch_hpd);
329831604222SAnusha Srivatsa 
3299f619e516SAnusha Srivatsa 	if (INTEL_PCH_TYPE(dev_priv) <= PCH_TGP)
33002939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, SHPD_FILTER_CNT, SHPD_FILTER_CNT_500_ADJ);
3301f49108d0SMatt Roper 
330231604222SAnusha Srivatsa 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
330331604222SAnusha Srivatsa 
33042ea63927SVille Syrjälä 	icp_ddi_hpd_detection_setup(dev_priv);
33052ea63927SVille Syrjälä 	icp_tc_hpd_detection_setup(dev_priv);
330652dfdba0SLucas De Marchi }
330752dfdba0SLucas De Marchi 
33082ea63927SVille Syrjälä static u32 gen11_hotplug_enables(struct drm_i915_private *i915,
33092ea63927SVille Syrjälä 				 enum hpd_pin pin)
33108ef7e340SMatt Roper {
33112ea63927SVille Syrjälä 	switch (pin) {
33122ea63927SVille Syrjälä 	case HPD_PORT_TC1:
33132ea63927SVille Syrjälä 	case HPD_PORT_TC2:
33142ea63927SVille Syrjälä 	case HPD_PORT_TC3:
33152ea63927SVille Syrjälä 	case HPD_PORT_TC4:
33162ea63927SVille Syrjälä 	case HPD_PORT_TC5:
33172ea63927SVille Syrjälä 	case HPD_PORT_TC6:
33182ea63927SVille Syrjälä 		return GEN11_HOTPLUG_CTL_ENABLE(pin);
33192ea63927SVille Syrjälä 	default:
33202ea63927SVille Syrjälä 		return 0;
332131604222SAnusha Srivatsa 	}
3322943682e3SMatt Roper }
3323943682e3SMatt Roper 
3324229f31e2SLucas De Marchi static void dg1_hpd_irq_setup(struct drm_i915_private *dev_priv)
3325229f31e2SLucas De Marchi {
3326b18c1eb9SClinton A Taylor 	u32 val;
3327b18c1eb9SClinton A Taylor 
33282939eb06SJani Nikula 	val = intel_uncore_read(&dev_priv->uncore, SOUTH_CHICKEN1);
3329b18c1eb9SClinton A Taylor 	val |= (INVERT_DDIA_HPD |
3330b18c1eb9SClinton A Taylor 		INVERT_DDIB_HPD |
3331b18c1eb9SClinton A Taylor 		INVERT_DDIC_HPD |
3332b18c1eb9SClinton A Taylor 		INVERT_DDID_HPD);
33332939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, SOUTH_CHICKEN1, val);
3334b18c1eb9SClinton A Taylor 
33352ea63927SVille Syrjälä 	icp_hpd_irq_setup(dev_priv);
3336229f31e2SLucas De Marchi }
3337229f31e2SLucas De Marchi 
333852c7f5f1SVille Syrjälä static void gen11_tc_hpd_detection_setup(struct drm_i915_private *dev_priv)
3339121e758eSDhinakaran Pandiyan {
3340121e758eSDhinakaran Pandiyan 	u32 hotplug;
3341121e758eSDhinakaran Pandiyan 
33422939eb06SJani Nikula 	hotplug = intel_uncore_read(&dev_priv->uncore, GEN11_TC_HOTPLUG_CTL);
33432ea63927SVille Syrjälä 	hotplug &= ~(GEN11_HOTPLUG_CTL_ENABLE(HPD_PORT_TC1) |
33445b76e860SVille Syrjälä 		     GEN11_HOTPLUG_CTL_ENABLE(HPD_PORT_TC2) |
33455b76e860SVille Syrjälä 		     GEN11_HOTPLUG_CTL_ENABLE(HPD_PORT_TC3) |
33465b76e860SVille Syrjälä 		     GEN11_HOTPLUG_CTL_ENABLE(HPD_PORT_TC4) |
33475b76e860SVille Syrjälä 		     GEN11_HOTPLUG_CTL_ENABLE(HPD_PORT_TC5) |
33482ea63927SVille Syrjälä 		     GEN11_HOTPLUG_CTL_ENABLE(HPD_PORT_TC6));
33492ea63927SVille Syrjälä 	hotplug |= intel_hpd_hotplug_enables(dev_priv, gen11_hotplug_enables);
33502939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, GEN11_TC_HOTPLUG_CTL, hotplug);
335152c7f5f1SVille Syrjälä }
335252c7f5f1SVille Syrjälä 
335352c7f5f1SVille Syrjälä static void gen11_tbt_hpd_detection_setup(struct drm_i915_private *dev_priv)
335452c7f5f1SVille Syrjälä {
335552c7f5f1SVille Syrjälä 	u32 hotplug;
3356b796b971SDhinakaran Pandiyan 
33572939eb06SJani Nikula 	hotplug = intel_uncore_read(&dev_priv->uncore, GEN11_TBT_HOTPLUG_CTL);
33582ea63927SVille Syrjälä 	hotplug &= ~(GEN11_HOTPLUG_CTL_ENABLE(HPD_PORT_TC1) |
33595b76e860SVille Syrjälä 		     GEN11_HOTPLUG_CTL_ENABLE(HPD_PORT_TC2) |
33605b76e860SVille Syrjälä 		     GEN11_HOTPLUG_CTL_ENABLE(HPD_PORT_TC3) |
33615b76e860SVille Syrjälä 		     GEN11_HOTPLUG_CTL_ENABLE(HPD_PORT_TC4) |
33625b76e860SVille Syrjälä 		     GEN11_HOTPLUG_CTL_ENABLE(HPD_PORT_TC5) |
33632ea63927SVille Syrjälä 		     GEN11_HOTPLUG_CTL_ENABLE(HPD_PORT_TC6));
33642ea63927SVille Syrjälä 	hotplug |= intel_hpd_hotplug_enables(dev_priv, gen11_hotplug_enables);
33652939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, GEN11_TBT_HOTPLUG_CTL, hotplug);
3366121e758eSDhinakaran Pandiyan }
3367121e758eSDhinakaran Pandiyan 
3368121e758eSDhinakaran Pandiyan static void gen11_hpd_irq_setup(struct drm_i915_private *dev_priv)
3369121e758eSDhinakaran Pandiyan {
3370121e758eSDhinakaran Pandiyan 	u32 hotplug_irqs, enabled_irqs;
3371121e758eSDhinakaran Pandiyan 	u32 val;
3372121e758eSDhinakaran Pandiyan 
33730398993bSVille Syrjälä 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, dev_priv->hotplug.hpd);
33746d3144ebSVille Syrjälä 	hotplug_irqs = intel_hpd_hotplug_irqs(dev_priv, dev_priv->hotplug.hpd);
3375121e758eSDhinakaran Pandiyan 
33762939eb06SJani Nikula 	val = intel_uncore_read(&dev_priv->uncore, GEN11_DE_HPD_IMR);
3377121e758eSDhinakaran Pandiyan 	val &= ~hotplug_irqs;
3378587a87b9SImre Deak 	val |= ~enabled_irqs & hotplug_irqs;
33792939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, GEN11_DE_HPD_IMR, val);
33802939eb06SJani Nikula 	intel_uncore_posting_read(&dev_priv->uncore, GEN11_DE_HPD_IMR);
3381121e758eSDhinakaran Pandiyan 
338252c7f5f1SVille Syrjälä 	gen11_tc_hpd_detection_setup(dev_priv);
338352c7f5f1SVille Syrjälä 	gen11_tbt_hpd_detection_setup(dev_priv);
338431604222SAnusha Srivatsa 
33852ea63927SVille Syrjälä 	if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
33862ea63927SVille Syrjälä 		icp_hpd_irq_setup(dev_priv);
33872ea63927SVille Syrjälä }
33882ea63927SVille Syrjälä 
33892ea63927SVille Syrjälä static u32 spt_hotplug_enables(struct drm_i915_private *i915,
33902ea63927SVille Syrjälä 			       enum hpd_pin pin)
33912ea63927SVille Syrjälä {
33922ea63927SVille Syrjälä 	switch (pin) {
33932ea63927SVille Syrjälä 	case HPD_PORT_A:
33942ea63927SVille Syrjälä 		return PORTA_HOTPLUG_ENABLE;
33952ea63927SVille Syrjälä 	case HPD_PORT_B:
33962ea63927SVille Syrjälä 		return PORTB_HOTPLUG_ENABLE;
33972ea63927SVille Syrjälä 	case HPD_PORT_C:
33982ea63927SVille Syrjälä 		return PORTC_HOTPLUG_ENABLE;
33992ea63927SVille Syrjälä 	case HPD_PORT_D:
34002ea63927SVille Syrjälä 		return PORTD_HOTPLUG_ENABLE;
34012ea63927SVille Syrjälä 	default:
34022ea63927SVille Syrjälä 		return 0;
34032ea63927SVille Syrjälä 	}
34042ea63927SVille Syrjälä }
34052ea63927SVille Syrjälä 
34062ea63927SVille Syrjälä static u32 spt_hotplug2_enables(struct drm_i915_private *i915,
34072ea63927SVille Syrjälä 				enum hpd_pin pin)
34082ea63927SVille Syrjälä {
34092ea63927SVille Syrjälä 	switch (pin) {
34102ea63927SVille Syrjälä 	case HPD_PORT_E:
34112ea63927SVille Syrjälä 		return PORTE_HOTPLUG_ENABLE;
34122ea63927SVille Syrjälä 	default:
34132ea63927SVille Syrjälä 		return 0;
34142ea63927SVille Syrjälä 	}
3415121e758eSDhinakaran Pandiyan }
3416121e758eSDhinakaran Pandiyan 
34172a57d9ccSImre Deak static void spt_hpd_detection_setup(struct drm_i915_private *dev_priv)
34182a57d9ccSImre Deak {
34193b92e263SRodrigo Vivi 	u32 val, hotplug;
34203b92e263SRodrigo Vivi 
34213b92e263SRodrigo Vivi 	/* Display WA #1179 WaHardHangonHotPlug: cnp */
34223b92e263SRodrigo Vivi 	if (HAS_PCH_CNP(dev_priv)) {
34232939eb06SJani Nikula 		val = intel_uncore_read(&dev_priv->uncore, SOUTH_CHICKEN1);
34243b92e263SRodrigo Vivi 		val &= ~CHASSIS_CLK_REQ_DURATION_MASK;
34253b92e263SRodrigo Vivi 		val |= CHASSIS_CLK_REQ_DURATION(0xf);
34262939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, SOUTH_CHICKEN1, val);
34273b92e263SRodrigo Vivi 	}
34282a57d9ccSImre Deak 
34292a57d9ccSImre Deak 	/* Enable digital hotplug on the PCH */
34302939eb06SJani Nikula 	hotplug = intel_uncore_read(&dev_priv->uncore, PCH_PORT_HOTPLUG);
34312ea63927SVille Syrjälä 	hotplug &= ~(PORTA_HOTPLUG_ENABLE |
34322a57d9ccSImre Deak 		     PORTB_HOTPLUG_ENABLE |
34332a57d9ccSImre Deak 		     PORTC_HOTPLUG_ENABLE |
34342ea63927SVille Syrjälä 		     PORTD_HOTPLUG_ENABLE);
34352ea63927SVille Syrjälä 	hotplug |= intel_hpd_hotplug_enables(dev_priv, spt_hotplug_enables);
34362939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, PCH_PORT_HOTPLUG, hotplug);
34372a57d9ccSImre Deak 
34382939eb06SJani Nikula 	hotplug = intel_uncore_read(&dev_priv->uncore, PCH_PORT_HOTPLUG2);
34392ea63927SVille Syrjälä 	hotplug &= ~PORTE_HOTPLUG_ENABLE;
34402ea63927SVille Syrjälä 	hotplug |= intel_hpd_hotplug_enables(dev_priv, spt_hotplug2_enables);
34412939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, PCH_PORT_HOTPLUG2, hotplug);
34422a57d9ccSImre Deak }
34432a57d9ccSImre Deak 
344491d14251STvrtko Ursulin static void spt_hpd_irq_setup(struct drm_i915_private *dev_priv)
34456dbf30ceSVille Syrjälä {
34462a57d9ccSImre Deak 	u32 hotplug_irqs, enabled_irqs;
34476dbf30ceSVille Syrjälä 
3448f49108d0SMatt Roper 	if (INTEL_PCH_TYPE(dev_priv) >= PCH_CNP)
34492939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, SHPD_FILTER_CNT, SHPD_FILTER_CNT_500_ADJ);
3450f49108d0SMatt Roper 
34510398993bSVille Syrjälä 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, dev_priv->hotplug.pch_hpd);
34526d3144ebSVille Syrjälä 	hotplug_irqs = intel_hpd_hotplug_irqs(dev_priv, dev_priv->hotplug.pch_hpd);
34536dbf30ceSVille Syrjälä 
34546dbf30ceSVille Syrjälä 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
34556dbf30ceSVille Syrjälä 
34562a57d9ccSImre Deak 	spt_hpd_detection_setup(dev_priv);
345726951cafSXiong Zhang }
34587fe0b973SKeith Packard 
34592ea63927SVille Syrjälä static u32 ilk_hotplug_enables(struct drm_i915_private *i915,
34602ea63927SVille Syrjälä 			       enum hpd_pin pin)
34612ea63927SVille Syrjälä {
34622ea63927SVille Syrjälä 	switch (pin) {
34632ea63927SVille Syrjälä 	case HPD_PORT_A:
34642ea63927SVille Syrjälä 		return DIGITAL_PORTA_HOTPLUG_ENABLE |
34652ea63927SVille Syrjälä 			DIGITAL_PORTA_PULSE_DURATION_2ms;
34662ea63927SVille Syrjälä 	default:
34672ea63927SVille Syrjälä 		return 0;
34682ea63927SVille Syrjälä 	}
34692ea63927SVille Syrjälä }
34702ea63927SVille Syrjälä 
34711a56b1a2SImre Deak static void ilk_hpd_detection_setup(struct drm_i915_private *dev_priv)
34721a56b1a2SImre Deak {
34731a56b1a2SImre Deak 	u32 hotplug;
34741a56b1a2SImre Deak 
34751a56b1a2SImre Deak 	/*
34761a56b1a2SImre Deak 	 * Enable digital hotplug on the CPU, and configure the DP short pulse
34771a56b1a2SImre Deak 	 * duration to 2ms (which is the minimum in the Display Port spec)
34781a56b1a2SImre Deak 	 * The pulse duration bits are reserved on HSW+.
34791a56b1a2SImre Deak 	 */
34802939eb06SJani Nikula 	hotplug = intel_uncore_read(&dev_priv->uncore, DIGITAL_PORT_HOTPLUG_CNTRL);
34812ea63927SVille Syrjälä 	hotplug &= ~(DIGITAL_PORTA_HOTPLUG_ENABLE |
34822ea63927SVille Syrjälä 		     DIGITAL_PORTA_PULSE_DURATION_MASK);
34832ea63927SVille Syrjälä 	hotplug |= intel_hpd_hotplug_enables(dev_priv, ilk_hotplug_enables);
34842939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, DIGITAL_PORT_HOTPLUG_CNTRL, hotplug);
34851a56b1a2SImre Deak }
34861a56b1a2SImre Deak 
348791d14251STvrtko Ursulin static void ilk_hpd_irq_setup(struct drm_i915_private *dev_priv)
3488e4ce95aaSVille Syrjälä {
34891a56b1a2SImre Deak 	u32 hotplug_irqs, enabled_irqs;
3490e4ce95aaSVille Syrjälä 
34910398993bSVille Syrjälä 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, dev_priv->hotplug.hpd);
34926d3144ebSVille Syrjälä 	hotplug_irqs = intel_hpd_hotplug_irqs(dev_priv, dev_priv->hotplug.hpd);
34933a3b3c7dSVille Syrjälä 
34946d3144ebSVille Syrjälä 	if (INTEL_GEN(dev_priv) >= 8)
34953a3b3c7dSVille Syrjälä 		bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
34966d3144ebSVille Syrjälä 	else
34973a3b3c7dSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
3498e4ce95aaSVille Syrjälä 
34991a56b1a2SImre Deak 	ilk_hpd_detection_setup(dev_priv);
3500e4ce95aaSVille Syrjälä 
350191d14251STvrtko Ursulin 	ibx_hpd_irq_setup(dev_priv);
3502e4ce95aaSVille Syrjälä }
3503e4ce95aaSVille Syrjälä 
35042ea63927SVille Syrjälä static u32 bxt_hotplug_enables(struct drm_i915_private *i915,
35052ea63927SVille Syrjälä 			       enum hpd_pin pin)
35062ea63927SVille Syrjälä {
35072ea63927SVille Syrjälä 	u32 hotplug;
35082ea63927SVille Syrjälä 
35092ea63927SVille Syrjälä 	switch (pin) {
35102ea63927SVille Syrjälä 	case HPD_PORT_A:
35112ea63927SVille Syrjälä 		hotplug = PORTA_HOTPLUG_ENABLE;
35122ea63927SVille Syrjälä 		if (intel_bios_is_port_hpd_inverted(i915, PORT_A))
35132ea63927SVille Syrjälä 			hotplug |= BXT_DDIA_HPD_INVERT;
35142ea63927SVille Syrjälä 		return hotplug;
35152ea63927SVille Syrjälä 	case HPD_PORT_B:
35162ea63927SVille Syrjälä 		hotplug = PORTB_HOTPLUG_ENABLE;
35172ea63927SVille Syrjälä 		if (intel_bios_is_port_hpd_inverted(i915, PORT_B))
35182ea63927SVille Syrjälä 			hotplug |= BXT_DDIB_HPD_INVERT;
35192ea63927SVille Syrjälä 		return hotplug;
35202ea63927SVille Syrjälä 	case HPD_PORT_C:
35212ea63927SVille Syrjälä 		hotplug = PORTC_HOTPLUG_ENABLE;
35222ea63927SVille Syrjälä 		if (intel_bios_is_port_hpd_inverted(i915, PORT_C))
35232ea63927SVille Syrjälä 			hotplug |= BXT_DDIC_HPD_INVERT;
35242ea63927SVille Syrjälä 		return hotplug;
35252ea63927SVille Syrjälä 	default:
35262ea63927SVille Syrjälä 		return 0;
35272ea63927SVille Syrjälä 	}
35282ea63927SVille Syrjälä }
35292ea63927SVille Syrjälä 
35302ea63927SVille Syrjälä static void bxt_hpd_detection_setup(struct drm_i915_private *dev_priv)
3531e0a20ad7SShashank Sharma {
35322a57d9ccSImre Deak 	u32 hotplug;
3533e0a20ad7SShashank Sharma 
35342939eb06SJani Nikula 	hotplug = intel_uncore_read(&dev_priv->uncore, PCH_PORT_HOTPLUG);
35352ea63927SVille Syrjälä 	hotplug &= ~(PORTA_HOTPLUG_ENABLE |
35362a57d9ccSImre Deak 		     PORTB_HOTPLUG_ENABLE |
35372ea63927SVille Syrjälä 		     PORTC_HOTPLUG_ENABLE |
35382ea63927SVille Syrjälä 		     BXT_DDIA_HPD_INVERT |
35392ea63927SVille Syrjälä 		     BXT_DDIB_HPD_INVERT |
35402ea63927SVille Syrjälä 		     BXT_DDIC_HPD_INVERT);
35412ea63927SVille Syrjälä 	hotplug |= intel_hpd_hotplug_enables(dev_priv, bxt_hotplug_enables);
35422939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, PCH_PORT_HOTPLUG, hotplug);
3543e0a20ad7SShashank Sharma }
3544e0a20ad7SShashank Sharma 
35452a57d9ccSImre Deak static void bxt_hpd_irq_setup(struct drm_i915_private *dev_priv)
35462a57d9ccSImre Deak {
35472a57d9ccSImre Deak 	u32 hotplug_irqs, enabled_irqs;
35482a57d9ccSImre Deak 
35490398993bSVille Syrjälä 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, dev_priv->hotplug.hpd);
35506d3144ebSVille Syrjälä 	hotplug_irqs = intel_hpd_hotplug_irqs(dev_priv, dev_priv->hotplug.hpd);
35512a57d9ccSImre Deak 
35522a57d9ccSImre Deak 	bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
35532a57d9ccSImre Deak 
35542ea63927SVille Syrjälä 	bxt_hpd_detection_setup(dev_priv);
35552a57d9ccSImre Deak }
35562a57d9ccSImre Deak 
3557a0a6d8cbSVille Syrjälä /*
3558a0a6d8cbSVille Syrjälä  * SDEIER is also touched by the interrupt handler to work around missed PCH
3559a0a6d8cbSVille Syrjälä  * interrupts. Hence we can't update it after the interrupt handler is enabled -
3560a0a6d8cbSVille Syrjälä  * instead we unconditionally enable all PCH interrupt sources here, but then
3561a0a6d8cbSVille Syrjälä  * only unmask them as needed with SDEIMR.
3562a0a6d8cbSVille Syrjälä  *
3563a0a6d8cbSVille Syrjälä  * Note that we currently do this after installing the interrupt handler,
3564a0a6d8cbSVille Syrjälä  * but before we enable the master interrupt. That should be sufficient
3565a0a6d8cbSVille Syrjälä  * to avoid races with the irq handler, assuming we have MSI. Shared legacy
3566a0a6d8cbSVille Syrjälä  * interrupts could still race.
3567a0a6d8cbSVille Syrjälä  */
3568b318b824SVille Syrjälä static void ibx_irq_postinstall(struct drm_i915_private *dev_priv)
3569d46da437SPaulo Zanoni {
3570a0a6d8cbSVille Syrjälä 	struct intel_uncore *uncore = &dev_priv->uncore;
357182a28bcfSDaniel Vetter 	u32 mask;
3572d46da437SPaulo Zanoni 
35736e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
3574692a04cfSDaniel Vetter 		return;
3575692a04cfSDaniel Vetter 
35766e266956STvrtko Ursulin 	if (HAS_PCH_IBX(dev_priv))
35775c673b60SDaniel Vetter 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
35784ebc6509SDhinakaran Pandiyan 	else if (HAS_PCH_CPT(dev_priv) || HAS_PCH_LPT(dev_priv))
35795c673b60SDaniel Vetter 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
35804ebc6509SDhinakaran Pandiyan 	else
35814ebc6509SDhinakaran Pandiyan 		mask = SDE_GMBUS_CPT;
35828664281bSPaulo Zanoni 
3583a0a6d8cbSVille Syrjälä 	GEN3_IRQ_INIT(uncore, SDE, ~mask, 0xffffffff);
3584d46da437SPaulo Zanoni }
3585d46da437SPaulo Zanoni 
35869eae5e27SLucas De Marchi static void ilk_irq_postinstall(struct drm_i915_private *dev_priv)
3587036a4a7dSZhenyu Wang {
3588b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
35898e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
35908e76f8dcSPaulo Zanoni 
3591b243f530STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 7) {
35928e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
3593842ebf7aSVille Syrjälä 				DE_PCH_EVENT_IVB | DE_AUX_CHANNEL_A_IVB);
35948e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
359523bb4cb5SVille Syrjälä 			      DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB |
35962a636e24SVille Syrjälä 			      DE_PLANE_FLIP_DONE_IVB(PLANE_C) |
35972a636e24SVille Syrjälä 			      DE_PLANE_FLIP_DONE_IVB(PLANE_B) |
35982a636e24SVille Syrjälä 			      DE_PLANE_FLIP_DONE_IVB(PLANE_A) |
359923bb4cb5SVille Syrjälä 			      DE_DP_A_HOTPLUG_IVB);
36008e76f8dcSPaulo Zanoni 	} else {
36018e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3602842ebf7aSVille Syrjälä 				DE_AUX_CHANNEL_A | DE_PIPEB_CRC_DONE |
3603842ebf7aSVille Syrjälä 				DE_PIPEA_CRC_DONE | DE_POISON);
3604c6073d4cSVille Syrjälä 		extra_mask = (DE_PIPEA_VBLANK | DE_PIPEB_VBLANK |
3605e4ce95aaSVille Syrjälä 			      DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN |
3606*4bb18054SVille Syrjälä 			      DE_PLANE_FLIP_DONE(PLANE_A) |
3607*4bb18054SVille Syrjälä 			      DE_PLANE_FLIP_DONE(PLANE_B) |
3608e4ce95aaSVille Syrjälä 			      DE_DP_A_HOTPLUG);
36098e76f8dcSPaulo Zanoni 	}
3610036a4a7dSZhenyu Wang 
3611fc340442SDaniel Vetter 	if (IS_HASWELL(dev_priv)) {
3612b16b2a2fSPaulo Zanoni 		gen3_assert_iir_is_zero(uncore, EDP_PSR_IIR);
3613fc340442SDaniel Vetter 		display_mask |= DE_EDP_PSR_INT_HSW;
3614fc340442SDaniel Vetter 	}
3615fc340442SDaniel Vetter 
3616c6073d4cSVille Syrjälä 	if (IS_IRONLAKE_M(dev_priv))
3617c6073d4cSVille Syrjälä 		extra_mask |= DE_PCU_EVENT;
3618c6073d4cSVille Syrjälä 
36191ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3620036a4a7dSZhenyu Wang 
3621a0a6d8cbSVille Syrjälä 	ibx_irq_postinstall(dev_priv);
3622622364b6SPaulo Zanoni 
3623a9922912SVille Syrjälä 	gen5_gt_irq_postinstall(&dev_priv->gt);
3624a9922912SVille Syrjälä 
3625b16b2a2fSPaulo Zanoni 	GEN3_IRQ_INIT(uncore, DE, dev_priv->irq_mask,
3626b16b2a2fSPaulo Zanoni 		      display_mask | extra_mask);
3627036a4a7dSZhenyu Wang }
3628036a4a7dSZhenyu Wang 
3629f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3630f8b79e58SImre Deak {
363167520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
3632f8b79e58SImre Deak 
3633f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3634f8b79e58SImre Deak 		return;
3635f8b79e58SImre Deak 
3636f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3637f8b79e58SImre Deak 
3638d6c69803SVille Syrjälä 	if (intel_irqs_enabled(dev_priv)) {
3639d6c69803SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3640ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3641f8b79e58SImre Deak 	}
3642d6c69803SVille Syrjälä }
3643f8b79e58SImre Deak 
3644f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3645f8b79e58SImre Deak {
364667520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
3647f8b79e58SImre Deak 
3648f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3649f8b79e58SImre Deak 		return;
3650f8b79e58SImre Deak 
3651f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3652f8b79e58SImre Deak 
3653950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3654ad22d106SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3655f8b79e58SImre Deak }
3656f8b79e58SImre Deak 
36570e6c9a9eSVille Syrjälä 
3658b318b824SVille Syrjälä static void valleyview_irq_postinstall(struct drm_i915_private *dev_priv)
36590e6c9a9eSVille Syrjälä {
3660cf1c97dcSAndi Shyti 	gen5_gt_irq_postinstall(&dev_priv->gt);
36617e231dbeSJesse Barnes 
3662ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
36639918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3664ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3665ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3666ad22d106SVille Syrjälä 
36672939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
36682939eb06SJani Nikula 	intel_uncore_posting_read(&dev_priv->uncore, VLV_MASTER_IER);
366920afbda2SDaniel Vetter }
367020afbda2SDaniel Vetter 
3671abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3672abd58f01SBen Widawsky {
3673b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
3674b16b2a2fSPaulo Zanoni 
3675869129eeSMatt Roper 	u32 de_pipe_masked = gen8_de_pipe_fault_mask(dev_priv) |
3676869129eeSMatt Roper 		GEN8_PIPE_CDCLK_CRC_DONE;
3677a9c287c9SJani Nikula 	u32 de_pipe_enables;
3678054318c7SImre Deak 	u32 de_port_masked = gen8_de_port_aux_mask(dev_priv);
36793a3b3c7dSVille Syrjälä 	u32 de_port_enables;
3680df0d28c1SDhinakaran Pandiyan 	u32 de_misc_masked = GEN8_DE_EDP_PSR;
3681562ddcb7SMatt Roper 	u32 trans_mask = BIT(TRANSCODER_A) | BIT(TRANSCODER_B) |
3682562ddcb7SMatt Roper 		BIT(TRANSCODER_C) | BIT(TRANSCODER_D);
36833a3b3c7dSVille Syrjälä 	enum pipe pipe;
3684770de83dSDamien Lespiau 
3685df0d28c1SDhinakaran Pandiyan 	if (INTEL_GEN(dev_priv) <= 10)
3686df0d28c1SDhinakaran Pandiyan 		de_misc_masked |= GEN8_DE_MISC_GSE;
3687df0d28c1SDhinakaran Pandiyan 
3688cc3f90f0SAnder Conselvan de Oliveira 	if (IS_GEN9_LP(dev_priv))
36893a3b3c7dSVille Syrjälä 		de_port_masked |= BXT_DE_PORT_GMBUS;
3690a324fcacSRodrigo Vivi 
36919c9e97c4SVandita Kulkarni 	if (INTEL_GEN(dev_priv) >= 11) {
36929c9e97c4SVandita Kulkarni 		enum port port;
36939c9e97c4SVandita Kulkarni 
36949c9e97c4SVandita Kulkarni 		if (intel_bios_is_dsi_present(dev_priv, &port))
36959c9e97c4SVandita Kulkarni 			de_port_masked |= DSI0_TE | DSI1_TE;
36969c9e97c4SVandita Kulkarni 	}
36979c9e97c4SVandita Kulkarni 
3698cda195f1SVille Syrjälä 	de_pipe_enables = de_pipe_masked |
3699cda195f1SVille Syrjälä 		GEN8_PIPE_VBLANK | GEN8_PIPE_FIFO_UNDERRUN |
3700cda195f1SVille Syrjälä 		gen8_de_pipe_flip_done_mask(dev_priv);
37011288f9b0SKarthik B S 
37023a3b3c7dSVille Syrjälä 	de_port_enables = de_port_masked;
3703cc3f90f0SAnder Conselvan de Oliveira 	if (IS_GEN9_LP(dev_priv))
3704a52bb15bSVille Syrjälä 		de_port_enables |= BXT_DE_PORT_HOTPLUG_MASK;
3705a52bb15bSVille Syrjälä 	else if (IS_BROADWELL(dev_priv))
3706e5abaab3SVille Syrjälä 		de_port_enables |= BDW_DE_PORT_HOTPLUG_MASK;
37073a3b3c7dSVille Syrjälä 
37088241cfbeSJosé Roberto de Souza 	if (INTEL_GEN(dev_priv) >= 12) {
37098241cfbeSJosé Roberto de Souza 		enum transcoder trans;
37108241cfbeSJosé Roberto de Souza 
3711562ddcb7SMatt Roper 		for_each_cpu_transcoder_masked(dev_priv, trans, trans_mask) {
37128241cfbeSJosé Roberto de Souza 			enum intel_display_power_domain domain;
37138241cfbeSJosé Roberto de Souza 
37148241cfbeSJosé Roberto de Souza 			domain = POWER_DOMAIN_TRANSCODER(trans);
37158241cfbeSJosé Roberto de Souza 			if (!intel_display_power_is_enabled(dev_priv, domain))
37168241cfbeSJosé Roberto de Souza 				continue;
37178241cfbeSJosé Roberto de Souza 
37188241cfbeSJosé Roberto de Souza 			gen3_assert_iir_is_zero(uncore, TRANS_PSR_IIR(trans));
37198241cfbeSJosé Roberto de Souza 		}
37208241cfbeSJosé Roberto de Souza 	} else {
3721b16b2a2fSPaulo Zanoni 		gen3_assert_iir_is_zero(uncore, EDP_PSR_IIR);
37228241cfbeSJosé Roberto de Souza 	}
3723e04f7eceSVille Syrjälä 
37240a195c02SMika Kahola 	for_each_pipe(dev_priv, pipe) {
37250a195c02SMika Kahola 		dev_priv->de_irq_mask[pipe] = ~de_pipe_masked;
3726abd58f01SBen Widawsky 
3727f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3728813bde43SPaulo Zanoni 				POWER_DOMAIN_PIPE(pipe)))
3729b16b2a2fSPaulo Zanoni 			GEN8_IRQ_INIT_NDX(uncore, DE_PIPE, pipe,
3730813bde43SPaulo Zanoni 					  dev_priv->de_irq_mask[pipe],
373135079899SPaulo Zanoni 					  de_pipe_enables);
37320a195c02SMika Kahola 	}
3733abd58f01SBen Widawsky 
3734b16b2a2fSPaulo Zanoni 	GEN3_IRQ_INIT(uncore, GEN8_DE_PORT_, ~de_port_masked, de_port_enables);
3735b16b2a2fSPaulo Zanoni 	GEN3_IRQ_INIT(uncore, GEN8_DE_MISC_, ~de_misc_masked, de_misc_masked);
37362a57d9ccSImre Deak 
3737121e758eSDhinakaran Pandiyan 	if (INTEL_GEN(dev_priv) >= 11) {
3738121e758eSDhinakaran Pandiyan 		u32 de_hpd_masked = 0;
3739b796b971SDhinakaran Pandiyan 		u32 de_hpd_enables = GEN11_DE_TC_HOTPLUG_MASK |
3740b796b971SDhinakaran Pandiyan 				     GEN11_DE_TBT_HOTPLUG_MASK;
3741121e758eSDhinakaran Pandiyan 
3742b16b2a2fSPaulo Zanoni 		GEN3_IRQ_INIT(uncore, GEN11_DE_HPD_, ~de_hpd_masked,
3743b16b2a2fSPaulo Zanoni 			      de_hpd_enables);
3744abd58f01SBen Widawsky 	}
3745121e758eSDhinakaran Pandiyan }
3746abd58f01SBen Widawsky 
3747b318b824SVille Syrjälä static void gen8_irq_postinstall(struct drm_i915_private *dev_priv)
3748abd58f01SBen Widawsky {
37496e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
3750a0a6d8cbSVille Syrjälä 		ibx_irq_postinstall(dev_priv);
3751622364b6SPaulo Zanoni 
3752cf1c97dcSAndi Shyti 	gen8_gt_irq_postinstall(&dev_priv->gt);
3753abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3754abd58f01SBen Widawsky 
375525286aacSDaniele Ceraolo Spurio 	gen8_master_intr_enable(dev_priv->uncore.regs);
3756abd58f01SBen Widawsky }
3757abd58f01SBen Widawsky 
3758b318b824SVille Syrjälä static void icp_irq_postinstall(struct drm_i915_private *dev_priv)
375931604222SAnusha Srivatsa {
37609696f041SVille Syrjälä 	struct intel_uncore *uncore = &dev_priv->uncore;
376131604222SAnusha Srivatsa 	u32 mask = SDE_GMBUS_ICP;
376231604222SAnusha Srivatsa 
37639696f041SVille Syrjälä 	GEN3_IRQ_INIT(uncore, SDE, ~mask, 0xffffffff);
376431604222SAnusha Srivatsa }
376531604222SAnusha Srivatsa 
3766b318b824SVille Syrjälä static void gen11_irq_postinstall(struct drm_i915_private *dev_priv)
376751951ae7SMika Kuoppala {
3768b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
3769df0d28c1SDhinakaran Pandiyan 	u32 gu_misc_masked = GEN11_GU_MISC_GSE;
377051951ae7SMika Kuoppala 
377129b43ae2SRodrigo Vivi 	if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
3772b318b824SVille Syrjälä 		icp_irq_postinstall(dev_priv);
377331604222SAnusha Srivatsa 
37749b77011eSTvrtko Ursulin 	gen11_gt_irq_postinstall(&dev_priv->gt);
377551951ae7SMika Kuoppala 	gen8_de_irq_postinstall(dev_priv);
377651951ae7SMika Kuoppala 
3777b16b2a2fSPaulo Zanoni 	GEN3_IRQ_INIT(uncore, GEN11_GU_MISC_, ~gu_misc_masked, gu_misc_masked);
3778df0d28c1SDhinakaran Pandiyan 
37792939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, GEN11_DISPLAY_INT_CTL, GEN11_DISPLAY_IRQ_ENABLE);
378051951ae7SMika Kuoppala 
378197b492f5SLucas De Marchi 	if (HAS_MASTER_UNIT_IRQ(dev_priv)) {
378297b492f5SLucas De Marchi 		dg1_master_intr_enable(uncore->regs);
37832939eb06SJani Nikula 		intel_uncore_posting_read(&dev_priv->uncore, DG1_MSTR_UNIT_INTR);
378497b492f5SLucas De Marchi 	} else {
37859b77011eSTvrtko Ursulin 		gen11_master_intr_enable(uncore->regs);
37862939eb06SJani Nikula 		intel_uncore_posting_read(&dev_priv->uncore, GEN11_GFX_MSTR_IRQ);
378751951ae7SMika Kuoppala 	}
378897b492f5SLucas De Marchi }
378951951ae7SMika Kuoppala 
3790b318b824SVille Syrjälä static void cherryview_irq_postinstall(struct drm_i915_private *dev_priv)
379143f328d7SVille Syrjälä {
3792cf1c97dcSAndi Shyti 	gen8_gt_irq_postinstall(&dev_priv->gt);
379343f328d7SVille Syrjälä 
3794ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
37959918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3796ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3797ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3798ad22d106SVille Syrjälä 
37992939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
38002939eb06SJani Nikula 	intel_uncore_posting_read(&dev_priv->uncore, GEN8_MASTER_IRQ);
380143f328d7SVille Syrjälä }
380243f328d7SVille Syrjälä 
3803b318b824SVille Syrjälä static void i8xx_irq_reset(struct drm_i915_private *dev_priv)
3804c2798b19SChris Wilson {
3805b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
3806c2798b19SChris Wilson 
380744d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
380844d9241eSVille Syrjälä 
3809b16b2a2fSPaulo Zanoni 	GEN2_IRQ_RESET(uncore);
3810e44adb5dSChris Wilson 	dev_priv->irq_mask = ~0u;
3811c2798b19SChris Wilson }
3812c2798b19SChris Wilson 
3813b318b824SVille Syrjälä static void i8xx_irq_postinstall(struct drm_i915_private *dev_priv)
3814c2798b19SChris Wilson {
3815b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
3816e9e9848aSVille Syrjälä 	u16 enable_mask;
3817c2798b19SChris Wilson 
38184f5fd91fSTvrtko Ursulin 	intel_uncore_write16(uncore,
38194f5fd91fSTvrtko Ursulin 			     EMR,
38204f5fd91fSTvrtko Ursulin 			     ~(I915_ERROR_PAGE_TABLE |
3821045cebd2SVille Syrjälä 			       I915_ERROR_MEMORY_REFRESH));
3822c2798b19SChris Wilson 
3823c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
3824c2798b19SChris Wilson 	dev_priv->irq_mask =
3825c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
382616659bc5SVille Syrjälä 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
382716659bc5SVille Syrjälä 		  I915_MASTER_ERROR_INTERRUPT);
3828c2798b19SChris Wilson 
3829e9e9848aSVille Syrjälä 	enable_mask =
3830c2798b19SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3831c2798b19SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
383216659bc5SVille Syrjälä 		I915_MASTER_ERROR_INTERRUPT |
3833e9e9848aSVille Syrjälä 		I915_USER_INTERRUPT;
3834e9e9848aSVille Syrjälä 
3835b16b2a2fSPaulo Zanoni 	GEN2_IRQ_INIT(uncore, dev_priv->irq_mask, enable_mask);
3836c2798b19SChris Wilson 
3837379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3838379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3839d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3840755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3841755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3842d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3843c2798b19SChris Wilson }
3844c2798b19SChris Wilson 
38454f5fd91fSTvrtko Ursulin static void i8xx_error_irq_ack(struct drm_i915_private *i915,
384678c357ddSVille Syrjälä 			       u16 *eir, u16 *eir_stuck)
384778c357ddSVille Syrjälä {
38484f5fd91fSTvrtko Ursulin 	struct intel_uncore *uncore = &i915->uncore;
384978c357ddSVille Syrjälä 	u16 emr;
385078c357ddSVille Syrjälä 
38514f5fd91fSTvrtko Ursulin 	*eir = intel_uncore_read16(uncore, EIR);
385278c357ddSVille Syrjälä 
385378c357ddSVille Syrjälä 	if (*eir)
38544f5fd91fSTvrtko Ursulin 		intel_uncore_write16(uncore, EIR, *eir);
385578c357ddSVille Syrjälä 
38564f5fd91fSTvrtko Ursulin 	*eir_stuck = intel_uncore_read16(uncore, EIR);
385778c357ddSVille Syrjälä 	if (*eir_stuck == 0)
385878c357ddSVille Syrjälä 		return;
385978c357ddSVille Syrjälä 
386078c357ddSVille Syrjälä 	/*
386178c357ddSVille Syrjälä 	 * Toggle all EMR bits to make sure we get an edge
386278c357ddSVille Syrjälä 	 * in the ISR master error bit if we don't clear
386378c357ddSVille Syrjälä 	 * all the EIR bits. Otherwise the edge triggered
386478c357ddSVille Syrjälä 	 * IIR on i965/g4x wouldn't notice that an interrupt
386578c357ddSVille Syrjälä 	 * is still pending. Also some EIR bits can't be
386678c357ddSVille Syrjälä 	 * cleared except by handling the underlying error
386778c357ddSVille Syrjälä 	 * (or by a GPU reset) so we mask any bit that
386878c357ddSVille Syrjälä 	 * remains set.
386978c357ddSVille Syrjälä 	 */
38704f5fd91fSTvrtko Ursulin 	emr = intel_uncore_read16(uncore, EMR);
38714f5fd91fSTvrtko Ursulin 	intel_uncore_write16(uncore, EMR, 0xffff);
38724f5fd91fSTvrtko Ursulin 	intel_uncore_write16(uncore, EMR, emr | *eir_stuck);
387378c357ddSVille Syrjälä }
387478c357ddSVille Syrjälä 
387578c357ddSVille Syrjälä static void i8xx_error_irq_handler(struct drm_i915_private *dev_priv,
387678c357ddSVille Syrjälä 				   u16 eir, u16 eir_stuck)
387778c357ddSVille Syrjälä {
387878c357ddSVille Syrjälä 	DRM_DEBUG("Master Error: EIR 0x%04x\n", eir);
387978c357ddSVille Syrjälä 
388078c357ddSVille Syrjälä 	if (eir_stuck)
388100376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm, "EIR stuck: 0x%04x, masked\n",
388200376ccfSWambui Karuga 			eir_stuck);
388378c357ddSVille Syrjälä }
388478c357ddSVille Syrjälä 
388578c357ddSVille Syrjälä static void i9xx_error_irq_ack(struct drm_i915_private *dev_priv,
388678c357ddSVille Syrjälä 			       u32 *eir, u32 *eir_stuck)
388778c357ddSVille Syrjälä {
388878c357ddSVille Syrjälä 	u32 emr;
388978c357ddSVille Syrjälä 
38902939eb06SJani Nikula 	*eir = intel_uncore_read(&dev_priv->uncore, EIR);
389178c357ddSVille Syrjälä 
38922939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, EIR, *eir);
389378c357ddSVille Syrjälä 
38942939eb06SJani Nikula 	*eir_stuck = intel_uncore_read(&dev_priv->uncore, EIR);
389578c357ddSVille Syrjälä 	if (*eir_stuck == 0)
389678c357ddSVille Syrjälä 		return;
389778c357ddSVille Syrjälä 
389878c357ddSVille Syrjälä 	/*
389978c357ddSVille Syrjälä 	 * Toggle all EMR bits to make sure we get an edge
390078c357ddSVille Syrjälä 	 * in the ISR master error bit if we don't clear
390178c357ddSVille Syrjälä 	 * all the EIR bits. Otherwise the edge triggered
390278c357ddSVille Syrjälä 	 * IIR on i965/g4x wouldn't notice that an interrupt
390378c357ddSVille Syrjälä 	 * is still pending. Also some EIR bits can't be
390478c357ddSVille Syrjälä 	 * cleared except by handling the underlying error
390578c357ddSVille Syrjälä 	 * (or by a GPU reset) so we mask any bit that
390678c357ddSVille Syrjälä 	 * remains set.
390778c357ddSVille Syrjälä 	 */
39082939eb06SJani Nikula 	emr = intel_uncore_read(&dev_priv->uncore, EMR);
39092939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, EMR, 0xffffffff);
39102939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, EMR, emr | *eir_stuck);
391178c357ddSVille Syrjälä }
391278c357ddSVille Syrjälä 
391378c357ddSVille Syrjälä static void i9xx_error_irq_handler(struct drm_i915_private *dev_priv,
391478c357ddSVille Syrjälä 				   u32 eir, u32 eir_stuck)
391578c357ddSVille Syrjälä {
391678c357ddSVille Syrjälä 	DRM_DEBUG("Master Error, EIR 0x%08x\n", eir);
391778c357ddSVille Syrjälä 
391878c357ddSVille Syrjälä 	if (eir_stuck)
391900376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm, "EIR stuck: 0x%08x, masked\n",
392000376ccfSWambui Karuga 			eir_stuck);
392178c357ddSVille Syrjälä }
392278c357ddSVille Syrjälä 
3923ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3924c2798b19SChris Wilson {
3925b318b824SVille Syrjälä 	struct drm_i915_private *dev_priv = arg;
3926af722d28SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
3927c2798b19SChris Wilson 
39282dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
39292dd2a883SImre Deak 		return IRQ_NONE;
39302dd2a883SImre Deak 
39311f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
39329102650fSDaniele Ceraolo Spurio 	disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
39331f814dacSImre Deak 
3934af722d28SVille Syrjälä 	do {
3935af722d28SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
393678c357ddSVille Syrjälä 		u16 eir = 0, eir_stuck = 0;
3937af722d28SVille Syrjälä 		u16 iir;
3938af722d28SVille Syrjälä 
39394f5fd91fSTvrtko Ursulin 		iir = intel_uncore_read16(&dev_priv->uncore, GEN2_IIR);
3940c2798b19SChris Wilson 		if (iir == 0)
3941af722d28SVille Syrjälä 			break;
3942c2798b19SChris Wilson 
3943af722d28SVille Syrjälä 		ret = IRQ_HANDLED;
3944c2798b19SChris Wilson 
3945eb64343cSVille Syrjälä 		/* Call regardless, as some status bits might not be
3946eb64343cSVille Syrjälä 		 * signalled in iir */
3947eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
3948c2798b19SChris Wilson 
394978c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
395078c357ddSVille Syrjälä 			i8xx_error_irq_ack(dev_priv, &eir, &eir_stuck);
395178c357ddSVille Syrjälä 
39524f5fd91fSTvrtko Ursulin 		intel_uncore_write16(&dev_priv->uncore, GEN2_IIR, iir);
3953c2798b19SChris Wilson 
3954c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
395573c8bfb7SChris Wilson 			intel_engine_signal_breadcrumbs(dev_priv->gt.engine[RCS0]);
3956c2798b19SChris Wilson 
395778c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
395878c357ddSVille Syrjälä 			i8xx_error_irq_handler(dev_priv, eir, eir_stuck);
3959af722d28SVille Syrjälä 
3960eb64343cSVille Syrjälä 		i8xx_pipestat_irq_handler(dev_priv, iir, pipe_stats);
3961af722d28SVille Syrjälä 	} while (0);
3962c2798b19SChris Wilson 
39639c6508b9SThomas Gleixner 	pmu_irq_stats(dev_priv, ret);
39649c6508b9SThomas Gleixner 
39659102650fSDaniele Ceraolo Spurio 	enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
39661f814dacSImre Deak 
39671f814dacSImre Deak 	return ret;
3968c2798b19SChris Wilson }
3969c2798b19SChris Wilson 
3970b318b824SVille Syrjälä static void i915_irq_reset(struct drm_i915_private *dev_priv)
3971a266c7d5SChris Wilson {
3972b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
3973a266c7d5SChris Wilson 
397456b857a5STvrtko Ursulin 	if (I915_HAS_HOTPLUG(dev_priv)) {
39750706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
39762939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, PORT_HOTPLUG_STAT, intel_uncore_read(&dev_priv->uncore, PORT_HOTPLUG_STAT));
3977a266c7d5SChris Wilson 	}
3978a266c7d5SChris Wilson 
397944d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
398044d9241eSVille Syrjälä 
3981b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN2_);
3982e44adb5dSChris Wilson 	dev_priv->irq_mask = ~0u;
3983a266c7d5SChris Wilson }
3984a266c7d5SChris Wilson 
3985b318b824SVille Syrjälä static void i915_irq_postinstall(struct drm_i915_private *dev_priv)
3986a266c7d5SChris Wilson {
3987b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
398838bde180SChris Wilson 	u32 enable_mask;
3989a266c7d5SChris Wilson 
39902939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, EMR, ~(I915_ERROR_PAGE_TABLE |
3991045cebd2SVille Syrjälä 			  I915_ERROR_MEMORY_REFRESH));
399238bde180SChris Wilson 
399338bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
399438bde180SChris Wilson 	dev_priv->irq_mask =
399538bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
399638bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
399716659bc5SVille Syrjälä 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
399816659bc5SVille Syrjälä 		  I915_MASTER_ERROR_INTERRUPT);
399938bde180SChris Wilson 
400038bde180SChris Wilson 	enable_mask =
400138bde180SChris Wilson 		I915_ASLE_INTERRUPT |
400238bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
400338bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
400416659bc5SVille Syrjälä 		I915_MASTER_ERROR_INTERRUPT |
400538bde180SChris Wilson 		I915_USER_INTERRUPT;
400638bde180SChris Wilson 
400756b857a5STvrtko Ursulin 	if (I915_HAS_HOTPLUG(dev_priv)) {
4008a266c7d5SChris Wilson 		/* Enable in IER... */
4009a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
4010a266c7d5SChris Wilson 		/* and unmask in IMR */
4011a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
4012a266c7d5SChris Wilson 	}
4013a266c7d5SChris Wilson 
4014b16b2a2fSPaulo Zanoni 	GEN3_IRQ_INIT(uncore, GEN2_, dev_priv->irq_mask, enable_mask);
4015a266c7d5SChris Wilson 
4016379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4017379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4018d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4019755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4020755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4021d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4022379ef82dSDaniel Vetter 
4023c30bb1fdSVille Syrjälä 	i915_enable_asle_pipestat(dev_priv);
402420afbda2SDaniel Vetter }
402520afbda2SDaniel Vetter 
4026ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
4027a266c7d5SChris Wilson {
4028b318b824SVille Syrjälä 	struct drm_i915_private *dev_priv = arg;
4029af722d28SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
4030a266c7d5SChris Wilson 
40312dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
40322dd2a883SImre Deak 		return IRQ_NONE;
40332dd2a883SImre Deak 
40341f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
40359102650fSDaniele Ceraolo Spurio 	disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
40361f814dacSImre Deak 
403738bde180SChris Wilson 	do {
4038eb64343cSVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
403978c357ddSVille Syrjälä 		u32 eir = 0, eir_stuck = 0;
4040af722d28SVille Syrjälä 		u32 hotplug_status = 0;
4041af722d28SVille Syrjälä 		u32 iir;
4042a266c7d5SChris Wilson 
40432939eb06SJani Nikula 		iir = intel_uncore_read(&dev_priv->uncore, GEN2_IIR);
4044af722d28SVille Syrjälä 		if (iir == 0)
4045af722d28SVille Syrjälä 			break;
4046af722d28SVille Syrjälä 
4047af722d28SVille Syrjälä 		ret = IRQ_HANDLED;
4048af722d28SVille Syrjälä 
4049af722d28SVille Syrjälä 		if (I915_HAS_HOTPLUG(dev_priv) &&
4050af722d28SVille Syrjälä 		    iir & I915_DISPLAY_PORT_INTERRUPT)
4051af722d28SVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
4052a266c7d5SChris Wilson 
4053eb64343cSVille Syrjälä 		/* Call regardless, as some status bits might not be
4054eb64343cSVille Syrjälä 		 * signalled in iir */
4055eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
4056a266c7d5SChris Wilson 
405778c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
405878c357ddSVille Syrjälä 			i9xx_error_irq_ack(dev_priv, &eir, &eir_stuck);
405978c357ddSVille Syrjälä 
40602939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, GEN2_IIR, iir);
4061a266c7d5SChris Wilson 
4062a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
406373c8bfb7SChris Wilson 			intel_engine_signal_breadcrumbs(dev_priv->gt.engine[RCS0]);
4064a266c7d5SChris Wilson 
406578c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
406678c357ddSVille Syrjälä 			i9xx_error_irq_handler(dev_priv, eir, eir_stuck);
4067a266c7d5SChris Wilson 
4068af722d28SVille Syrjälä 		if (hotplug_status)
4069af722d28SVille Syrjälä 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
4070af722d28SVille Syrjälä 
4071af722d28SVille Syrjälä 		i915_pipestat_irq_handler(dev_priv, iir, pipe_stats);
4072af722d28SVille Syrjälä 	} while (0);
4073a266c7d5SChris Wilson 
40749c6508b9SThomas Gleixner 	pmu_irq_stats(dev_priv, ret);
40759c6508b9SThomas Gleixner 
40769102650fSDaniele Ceraolo Spurio 	enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
40771f814dacSImre Deak 
4078a266c7d5SChris Wilson 	return ret;
4079a266c7d5SChris Wilson }
4080a266c7d5SChris Wilson 
4081b318b824SVille Syrjälä static void i965_irq_reset(struct drm_i915_private *dev_priv)
4082a266c7d5SChris Wilson {
4083b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
4084a266c7d5SChris Wilson 
40850706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
40862939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, PORT_HOTPLUG_STAT, intel_uncore_read(&dev_priv->uncore, PORT_HOTPLUG_STAT));
4087a266c7d5SChris Wilson 
408844d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
408944d9241eSVille Syrjälä 
4090b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN2_);
4091e44adb5dSChris Wilson 	dev_priv->irq_mask = ~0u;
4092a266c7d5SChris Wilson }
4093a266c7d5SChris Wilson 
4094b318b824SVille Syrjälä static void i965_irq_postinstall(struct drm_i915_private *dev_priv)
4095a266c7d5SChris Wilson {
4096b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
4097bbba0a97SChris Wilson 	u32 enable_mask;
4098a266c7d5SChris Wilson 	u32 error_mask;
4099a266c7d5SChris Wilson 
4100045cebd2SVille Syrjälä 	/*
4101045cebd2SVille Syrjälä 	 * Enable some error detection, note the instruction error mask
4102045cebd2SVille Syrjälä 	 * bit is reserved, so we leave it masked.
4103045cebd2SVille Syrjälä 	 */
4104045cebd2SVille Syrjälä 	if (IS_G4X(dev_priv)) {
4105045cebd2SVille Syrjälä 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
4106045cebd2SVille Syrjälä 			       GM45_ERROR_MEM_PRIV |
4107045cebd2SVille Syrjälä 			       GM45_ERROR_CP_PRIV |
4108045cebd2SVille Syrjälä 			       I915_ERROR_MEMORY_REFRESH);
4109045cebd2SVille Syrjälä 	} else {
4110045cebd2SVille Syrjälä 		error_mask = ~(I915_ERROR_PAGE_TABLE |
4111045cebd2SVille Syrjälä 			       I915_ERROR_MEMORY_REFRESH);
4112045cebd2SVille Syrjälä 	}
41132939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, EMR, error_mask);
4114045cebd2SVille Syrjälä 
4115a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
4116c30bb1fdSVille Syrjälä 	dev_priv->irq_mask =
4117c30bb1fdSVille Syrjälä 		~(I915_ASLE_INTERRUPT |
4118adca4730SChris Wilson 		  I915_DISPLAY_PORT_INTERRUPT |
4119bbba0a97SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4120bbba0a97SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
412178c357ddSVille Syrjälä 		  I915_MASTER_ERROR_INTERRUPT);
4122bbba0a97SChris Wilson 
4123c30bb1fdSVille Syrjälä 	enable_mask =
4124c30bb1fdSVille Syrjälä 		I915_ASLE_INTERRUPT |
4125c30bb1fdSVille Syrjälä 		I915_DISPLAY_PORT_INTERRUPT |
4126c30bb1fdSVille Syrjälä 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4127c30bb1fdSVille Syrjälä 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
412878c357ddSVille Syrjälä 		I915_MASTER_ERROR_INTERRUPT |
4129c30bb1fdSVille Syrjälä 		I915_USER_INTERRUPT;
4130bbba0a97SChris Wilson 
413191d14251STvrtko Ursulin 	if (IS_G4X(dev_priv))
4132bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
4133a266c7d5SChris Wilson 
4134b16b2a2fSPaulo Zanoni 	GEN3_IRQ_INIT(uncore, GEN2_, dev_priv->irq_mask, enable_mask);
4135c30bb1fdSVille Syrjälä 
4136b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4137b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4138d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4139755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
4140755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4141755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4142d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4143a266c7d5SChris Wilson 
414491d14251STvrtko Ursulin 	i915_enable_asle_pipestat(dev_priv);
414520afbda2SDaniel Vetter }
414620afbda2SDaniel Vetter 
414791d14251STvrtko Ursulin static void i915_hpd_irq_setup(struct drm_i915_private *dev_priv)
414820afbda2SDaniel Vetter {
414920afbda2SDaniel Vetter 	u32 hotplug_en;
415020afbda2SDaniel Vetter 
415167520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
4152b5ea2d56SDaniel Vetter 
4153adca4730SChris Wilson 	/* Note HDMI and DP share hotplug bits */
4154e5868a31SEgbert Eich 	/* enable bits are the same for all generations */
415591d14251STvrtko Ursulin 	hotplug_en = intel_hpd_enabled_irqs(dev_priv, hpd_mask_i915);
4156a266c7d5SChris Wilson 	/* Programming the CRT detection parameters tends
4157a266c7d5SChris Wilson 	   to generate a spurious hotplug event about three
4158a266c7d5SChris Wilson 	   seconds later.  So just do it once.
4159a266c7d5SChris Wilson 	*/
416091d14251STvrtko Ursulin 	if (IS_G4X(dev_priv))
4161a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
4162a266c7d5SChris Wilson 	hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
4163a266c7d5SChris Wilson 
4164a266c7d5SChris Wilson 	/* Ignore TV since it's buggy */
41650706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv,
4166f9e3dc78SJani Nikula 					     HOTPLUG_INT_EN_MASK |
4167f9e3dc78SJani Nikula 					     CRT_HOTPLUG_VOLTAGE_COMPARE_MASK |
4168f9e3dc78SJani Nikula 					     CRT_HOTPLUG_ACTIVATION_PERIOD_64,
41690706f17cSEgbert Eich 					     hotplug_en);
4170a266c7d5SChris Wilson }
4171a266c7d5SChris Wilson 
4172ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
4173a266c7d5SChris Wilson {
4174b318b824SVille Syrjälä 	struct drm_i915_private *dev_priv = arg;
4175af722d28SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
4176a266c7d5SChris Wilson 
41772dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
41782dd2a883SImre Deak 		return IRQ_NONE;
41792dd2a883SImre Deak 
41801f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
41819102650fSDaniele Ceraolo Spurio 	disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
41821f814dacSImre Deak 
4183af722d28SVille Syrjälä 	do {
4184eb64343cSVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
418578c357ddSVille Syrjälä 		u32 eir = 0, eir_stuck = 0;
4186af722d28SVille Syrjälä 		u32 hotplug_status = 0;
4187af722d28SVille Syrjälä 		u32 iir;
41882c8ba29fSChris Wilson 
41892939eb06SJani Nikula 		iir = intel_uncore_read(&dev_priv->uncore, GEN2_IIR);
4190af722d28SVille Syrjälä 		if (iir == 0)
4191af722d28SVille Syrjälä 			break;
4192af722d28SVille Syrjälä 
4193af722d28SVille Syrjälä 		ret = IRQ_HANDLED;
4194af722d28SVille Syrjälä 
4195af722d28SVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
4196af722d28SVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
4197a266c7d5SChris Wilson 
4198eb64343cSVille Syrjälä 		/* Call regardless, as some status bits might not be
4199eb64343cSVille Syrjälä 		 * signalled in iir */
4200eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
4201a266c7d5SChris Wilson 
420278c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
420378c357ddSVille Syrjälä 			i9xx_error_irq_ack(dev_priv, &eir, &eir_stuck);
420478c357ddSVille Syrjälä 
42052939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, GEN2_IIR, iir);
4206a266c7d5SChris Wilson 
4207a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
420873c8bfb7SChris Wilson 			intel_engine_signal_breadcrumbs(dev_priv->gt.engine[RCS0]);
4209af722d28SVille Syrjälä 
4210a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
421173c8bfb7SChris Wilson 			intel_engine_signal_breadcrumbs(dev_priv->gt.engine[VCS0]);
4212a266c7d5SChris Wilson 
421378c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
421478c357ddSVille Syrjälä 			i9xx_error_irq_handler(dev_priv, eir, eir_stuck);
4215515ac2bbSDaniel Vetter 
4216af722d28SVille Syrjälä 		if (hotplug_status)
4217af722d28SVille Syrjälä 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
4218af722d28SVille Syrjälä 
4219af722d28SVille Syrjälä 		i965_pipestat_irq_handler(dev_priv, iir, pipe_stats);
4220af722d28SVille Syrjälä 	} while (0);
4221a266c7d5SChris Wilson 
42229c6508b9SThomas Gleixner 	pmu_irq_stats(dev_priv, IRQ_HANDLED);
42239c6508b9SThomas Gleixner 
42249102650fSDaniele Ceraolo Spurio 	enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
42251f814dacSImre Deak 
4226a266c7d5SChris Wilson 	return ret;
4227a266c7d5SChris Wilson }
4228a266c7d5SChris Wilson 
4229fca52a55SDaniel Vetter /**
4230fca52a55SDaniel Vetter  * intel_irq_init - initializes irq support
4231fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4232fca52a55SDaniel Vetter  *
4233fca52a55SDaniel Vetter  * This function initializes all the irq support including work items, timers
4234fca52a55SDaniel Vetter  * and all the vtables. It does not setup the interrupt itself though.
4235fca52a55SDaniel Vetter  */
4236b963291cSDaniel Vetter void intel_irq_init(struct drm_i915_private *dev_priv)
4237f71d4af4SJesse Barnes {
423891c8a326SChris Wilson 	struct drm_device *dev = &dev_priv->drm;
4239cefcff8fSJoonas Lahtinen 	int i;
42408b2e326dSChris Wilson 
424174bb98baSLucas De Marchi 	INIT_WORK(&dev_priv->l3_parity.error_work, ivb_parity_work);
4242cefcff8fSJoonas Lahtinen 	for (i = 0; i < MAX_L3_SLICES; ++i)
4243cefcff8fSJoonas Lahtinen 		dev_priv->l3_parity.remap_info[i] = NULL;
42448b2e326dSChris Wilson 
4245633023a4SDaniele Ceraolo Spurio 	/* pre-gen11 the guc irqs bits are in the upper 16 bits of the pm reg */
4246702668e6SDaniele Ceraolo Spurio 	if (HAS_GT_UC(dev_priv) && INTEL_GEN(dev_priv) < 11)
42472239e6dfSDaniele Ceraolo Spurio 		dev_priv->gt.pm_guc_events = GUC_INTR_GUC2HOST << 16;
424826705e20SSagar Arun Kamble 
42499a450b68SLucas De Marchi 	if (!HAS_DISPLAY(dev_priv))
42509a450b68SLucas De Marchi 		return;
42519a450b68SLucas De Marchi 
425296bd87b7SLucas De Marchi 	intel_hpd_init_pins(dev_priv);
425396bd87b7SLucas De Marchi 
425496bd87b7SLucas De Marchi 	intel_hpd_init_work(dev_priv);
425596bd87b7SLucas De Marchi 
425621da2700SVille Syrjälä 	dev->vblank_disable_immediate = true;
425721da2700SVille Syrjälä 
4258262fd485SChris Wilson 	/* Most platforms treat the display irq block as an always-on
4259262fd485SChris Wilson 	 * power domain. vlv/chv can disable it at runtime and need
4260262fd485SChris Wilson 	 * special care to avoid writing any of the display block registers
4261262fd485SChris Wilson 	 * outside of the power domain. We defer setting up the display irqs
4262262fd485SChris Wilson 	 * in this case to the runtime pm.
4263262fd485SChris Wilson 	 */
4264262fd485SChris Wilson 	dev_priv->display_irqs_enabled = true;
4265262fd485SChris Wilson 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4266262fd485SChris Wilson 		dev_priv->display_irqs_enabled = false;
4267262fd485SChris Wilson 
4268317eaa95SLyude 	dev_priv->hotplug.hpd_storm_threshold = HPD_STORM_DEFAULT_THRESHOLD;
42699a64c650SLyude Paul 	/* If we have MST support, we want to avoid doing short HPD IRQ storm
42709a64c650SLyude Paul 	 * detection, as short HPD storms will occur as a natural part of
42719a64c650SLyude Paul 	 * sideband messaging with MST.
42729a64c650SLyude Paul 	 * On older platforms however, IRQ storms can occur with both long and
42739a64c650SLyude Paul 	 * short pulses, as seen on some G4x systems.
42749a64c650SLyude Paul 	 */
42759a64c650SLyude Paul 	dev_priv->hotplug.hpd_short_storm_enabled = !HAS_DP_MST(dev_priv);
4276317eaa95SLyude 
42772ccf2e03SChris Wilson 	if (HAS_GMCH(dev_priv)) {
42782ccf2e03SChris Wilson 		if (I915_HAS_HOTPLUG(dev_priv))
42792ccf2e03SChris Wilson 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
42802ccf2e03SChris Wilson 	} else {
4281229f31e2SLucas De Marchi 		if (HAS_PCH_DG1(dev_priv))
4282229f31e2SLucas De Marchi 			dev_priv->display.hpd_irq_setup = dg1_hpd_irq_setup;
42838ef7e340SMatt Roper 		else if (INTEL_GEN(dev_priv) >= 11)
4284121e758eSDhinakaran Pandiyan 			dev_priv->display.hpd_irq_setup = gen11_hpd_irq_setup;
4285b318b824SVille Syrjälä 		else if (IS_GEN9_LP(dev_priv))
4286e0a20ad7SShashank Sharma 			dev_priv->display.hpd_irq_setup = bxt_hpd_irq_setup;
4287c6c30b91SRodrigo Vivi 		else if (INTEL_PCH_TYPE(dev_priv) >= PCH_SPT)
42886dbf30ceSVille Syrjälä 			dev_priv->display.hpd_irq_setup = spt_hpd_irq_setup;
42896dbf30ceSVille Syrjälä 		else
42903a3b3c7dSVille Syrjälä 			dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
4291f71d4af4SJesse Barnes 	}
42922ccf2e03SChris Wilson }
429320afbda2SDaniel Vetter 
4294fca52a55SDaniel Vetter /**
4295cefcff8fSJoonas Lahtinen  * intel_irq_fini - deinitializes IRQ support
4296cefcff8fSJoonas Lahtinen  * @i915: i915 device instance
4297cefcff8fSJoonas Lahtinen  *
4298cefcff8fSJoonas Lahtinen  * This function deinitializes all the IRQ support.
4299cefcff8fSJoonas Lahtinen  */
4300cefcff8fSJoonas Lahtinen void intel_irq_fini(struct drm_i915_private *i915)
4301cefcff8fSJoonas Lahtinen {
4302cefcff8fSJoonas Lahtinen 	int i;
4303cefcff8fSJoonas Lahtinen 
4304cefcff8fSJoonas Lahtinen 	for (i = 0; i < MAX_L3_SLICES; ++i)
4305cefcff8fSJoonas Lahtinen 		kfree(i915->l3_parity.remap_info[i]);
4306cefcff8fSJoonas Lahtinen }
4307cefcff8fSJoonas Lahtinen 
4308b318b824SVille Syrjälä static irq_handler_t intel_irq_handler(struct drm_i915_private *dev_priv)
4309b318b824SVille Syrjälä {
4310b318b824SVille Syrjälä 	if (HAS_GMCH(dev_priv)) {
4311b318b824SVille Syrjälä 		if (IS_CHERRYVIEW(dev_priv))
4312b318b824SVille Syrjälä 			return cherryview_irq_handler;
4313b318b824SVille Syrjälä 		else if (IS_VALLEYVIEW(dev_priv))
4314b318b824SVille Syrjälä 			return valleyview_irq_handler;
4315b318b824SVille Syrjälä 		else if (IS_GEN(dev_priv, 4))
4316b318b824SVille Syrjälä 			return i965_irq_handler;
4317b318b824SVille Syrjälä 		else if (IS_GEN(dev_priv, 3))
4318b318b824SVille Syrjälä 			return i915_irq_handler;
4319b318b824SVille Syrjälä 		else
4320b318b824SVille Syrjälä 			return i8xx_irq_handler;
4321b318b824SVille Syrjälä 	} else {
432297b492f5SLucas De Marchi 		if (HAS_MASTER_UNIT_IRQ(dev_priv))
432397b492f5SLucas De Marchi 			return dg1_irq_handler;
4324b318b824SVille Syrjälä 		if (INTEL_GEN(dev_priv) >= 11)
4325b318b824SVille Syrjälä 			return gen11_irq_handler;
4326b318b824SVille Syrjälä 		else if (INTEL_GEN(dev_priv) >= 8)
4327b318b824SVille Syrjälä 			return gen8_irq_handler;
4328b318b824SVille Syrjälä 		else
43299eae5e27SLucas De Marchi 			return ilk_irq_handler;
4330b318b824SVille Syrjälä 	}
4331b318b824SVille Syrjälä }
4332b318b824SVille Syrjälä 
4333b318b824SVille Syrjälä static void intel_irq_reset(struct drm_i915_private *dev_priv)
4334b318b824SVille Syrjälä {
4335b318b824SVille Syrjälä 	if (HAS_GMCH(dev_priv)) {
4336b318b824SVille Syrjälä 		if (IS_CHERRYVIEW(dev_priv))
4337b318b824SVille Syrjälä 			cherryview_irq_reset(dev_priv);
4338b318b824SVille Syrjälä 		else if (IS_VALLEYVIEW(dev_priv))
4339b318b824SVille Syrjälä 			valleyview_irq_reset(dev_priv);
4340b318b824SVille Syrjälä 		else if (IS_GEN(dev_priv, 4))
4341b318b824SVille Syrjälä 			i965_irq_reset(dev_priv);
4342b318b824SVille Syrjälä 		else if (IS_GEN(dev_priv, 3))
4343b318b824SVille Syrjälä 			i915_irq_reset(dev_priv);
4344b318b824SVille Syrjälä 		else
4345b318b824SVille Syrjälä 			i8xx_irq_reset(dev_priv);
4346b318b824SVille Syrjälä 	} else {
4347b318b824SVille Syrjälä 		if (INTEL_GEN(dev_priv) >= 11)
4348b318b824SVille Syrjälä 			gen11_irq_reset(dev_priv);
4349b318b824SVille Syrjälä 		else if (INTEL_GEN(dev_priv) >= 8)
4350b318b824SVille Syrjälä 			gen8_irq_reset(dev_priv);
4351b318b824SVille Syrjälä 		else
43529eae5e27SLucas De Marchi 			ilk_irq_reset(dev_priv);
4353b318b824SVille Syrjälä 	}
4354b318b824SVille Syrjälä }
4355b318b824SVille Syrjälä 
4356b318b824SVille Syrjälä static void intel_irq_postinstall(struct drm_i915_private *dev_priv)
4357b318b824SVille Syrjälä {
4358b318b824SVille Syrjälä 	if (HAS_GMCH(dev_priv)) {
4359b318b824SVille Syrjälä 		if (IS_CHERRYVIEW(dev_priv))
4360b318b824SVille Syrjälä 			cherryview_irq_postinstall(dev_priv);
4361b318b824SVille Syrjälä 		else if (IS_VALLEYVIEW(dev_priv))
4362b318b824SVille Syrjälä 			valleyview_irq_postinstall(dev_priv);
4363b318b824SVille Syrjälä 		else if (IS_GEN(dev_priv, 4))
4364b318b824SVille Syrjälä 			i965_irq_postinstall(dev_priv);
4365b318b824SVille Syrjälä 		else if (IS_GEN(dev_priv, 3))
4366b318b824SVille Syrjälä 			i915_irq_postinstall(dev_priv);
4367b318b824SVille Syrjälä 		else
4368b318b824SVille Syrjälä 			i8xx_irq_postinstall(dev_priv);
4369b318b824SVille Syrjälä 	} else {
4370b318b824SVille Syrjälä 		if (INTEL_GEN(dev_priv) >= 11)
4371b318b824SVille Syrjälä 			gen11_irq_postinstall(dev_priv);
4372b318b824SVille Syrjälä 		else if (INTEL_GEN(dev_priv) >= 8)
4373b318b824SVille Syrjälä 			gen8_irq_postinstall(dev_priv);
4374b318b824SVille Syrjälä 		else
43759eae5e27SLucas De Marchi 			ilk_irq_postinstall(dev_priv);
4376b318b824SVille Syrjälä 	}
4377b318b824SVille Syrjälä }
4378b318b824SVille Syrjälä 
4379cefcff8fSJoonas Lahtinen /**
4380fca52a55SDaniel Vetter  * intel_irq_install - enables the hardware interrupt
4381fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4382fca52a55SDaniel Vetter  *
4383fca52a55SDaniel Vetter  * This function enables the hardware interrupt handling, but leaves the hotplug
4384fca52a55SDaniel Vetter  * handling still disabled. It is called after intel_irq_init().
4385fca52a55SDaniel Vetter  *
4386fca52a55SDaniel Vetter  * In the driver load and resume code we need working interrupts in a few places
4387fca52a55SDaniel Vetter  * but don't want to deal with the hassle of concurrent probe and hotplug
4388fca52a55SDaniel Vetter  * workers. Hence the split into this two-stage approach.
4389fca52a55SDaniel Vetter  */
43902aeb7d3aSDaniel Vetter int intel_irq_install(struct drm_i915_private *dev_priv)
43912aeb7d3aSDaniel Vetter {
4392b318b824SVille Syrjälä 	int irq = dev_priv->drm.pdev->irq;
4393b318b824SVille Syrjälä 	int ret;
4394b318b824SVille Syrjälä 
43952aeb7d3aSDaniel Vetter 	/*
43962aeb7d3aSDaniel Vetter 	 * We enable some interrupt sources in our postinstall hooks, so mark
43972aeb7d3aSDaniel Vetter 	 * interrupts as enabled _before_ actually enabling them to avoid
43982aeb7d3aSDaniel Vetter 	 * special cases in our ordering checks.
43992aeb7d3aSDaniel Vetter 	 */
4400ad1443f0SSagar Arun Kamble 	dev_priv->runtime_pm.irqs_enabled = true;
44012aeb7d3aSDaniel Vetter 
4402b318b824SVille Syrjälä 	dev_priv->drm.irq_enabled = true;
4403b318b824SVille Syrjälä 
4404b318b824SVille Syrjälä 	intel_irq_reset(dev_priv);
4405b318b824SVille Syrjälä 
4406b318b824SVille Syrjälä 	ret = request_irq(irq, intel_irq_handler(dev_priv),
4407b318b824SVille Syrjälä 			  IRQF_SHARED, DRIVER_NAME, dev_priv);
4408b318b824SVille Syrjälä 	if (ret < 0) {
4409b318b824SVille Syrjälä 		dev_priv->drm.irq_enabled = false;
4410b318b824SVille Syrjälä 		return ret;
4411b318b824SVille Syrjälä 	}
4412b318b824SVille Syrjälä 
4413b318b824SVille Syrjälä 	intel_irq_postinstall(dev_priv);
4414b318b824SVille Syrjälä 
4415b318b824SVille Syrjälä 	return ret;
44162aeb7d3aSDaniel Vetter }
44172aeb7d3aSDaniel Vetter 
4418fca52a55SDaniel Vetter /**
4419fca52a55SDaniel Vetter  * intel_irq_uninstall - finilizes all irq handling
4420fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4421fca52a55SDaniel Vetter  *
4422fca52a55SDaniel Vetter  * This stops interrupt and hotplug handling and unregisters and frees all
4423fca52a55SDaniel Vetter  * resources acquired in the init functions.
4424fca52a55SDaniel Vetter  */
44252aeb7d3aSDaniel Vetter void intel_irq_uninstall(struct drm_i915_private *dev_priv)
44262aeb7d3aSDaniel Vetter {
4427b318b824SVille Syrjälä 	int irq = dev_priv->drm.pdev->irq;
4428b318b824SVille Syrjälä 
4429b318b824SVille Syrjälä 	/*
4430789fa874SJanusz Krzysztofik 	 * FIXME we can get called twice during driver probe
4431789fa874SJanusz Krzysztofik 	 * error handling as well as during driver remove due to
4432789fa874SJanusz Krzysztofik 	 * intel_modeset_driver_remove() calling us out of sequence.
4433789fa874SJanusz Krzysztofik 	 * Would be nice if it didn't do that...
4434b318b824SVille Syrjälä 	 */
4435b318b824SVille Syrjälä 	if (!dev_priv->drm.irq_enabled)
4436b318b824SVille Syrjälä 		return;
4437b318b824SVille Syrjälä 
4438b318b824SVille Syrjälä 	dev_priv->drm.irq_enabled = false;
4439b318b824SVille Syrjälä 
4440b318b824SVille Syrjälä 	intel_irq_reset(dev_priv);
4441b318b824SVille Syrjälä 
4442b318b824SVille Syrjälä 	free_irq(irq, dev_priv);
4443b318b824SVille Syrjälä 
44442aeb7d3aSDaniel Vetter 	intel_hpd_cancel_work(dev_priv);
4445ad1443f0SSagar Arun Kamble 	dev_priv->runtime_pm.irqs_enabled = false;
44462aeb7d3aSDaniel Vetter }
44472aeb7d3aSDaniel Vetter 
4448fca52a55SDaniel Vetter /**
4449fca52a55SDaniel Vetter  * intel_runtime_pm_disable_interrupts - runtime interrupt disabling
4450fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4451fca52a55SDaniel Vetter  *
4452fca52a55SDaniel Vetter  * This function is used to disable interrupts at runtime, both in the runtime
4453fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4454fca52a55SDaniel Vetter  */
4455b963291cSDaniel Vetter void intel_runtime_pm_disable_interrupts(struct drm_i915_private *dev_priv)
4456c67a470bSPaulo Zanoni {
4457b318b824SVille Syrjälä 	intel_irq_reset(dev_priv);
4458ad1443f0SSagar Arun Kamble 	dev_priv->runtime_pm.irqs_enabled = false;
4459315ca4c4SVille Syrjälä 	intel_synchronize_irq(dev_priv);
4460c67a470bSPaulo Zanoni }
4461c67a470bSPaulo Zanoni 
4462fca52a55SDaniel Vetter /**
4463fca52a55SDaniel Vetter  * intel_runtime_pm_enable_interrupts - runtime interrupt enabling
4464fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4465fca52a55SDaniel Vetter  *
4466fca52a55SDaniel Vetter  * This function is used to enable interrupts at runtime, both in the runtime
4467fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4468fca52a55SDaniel Vetter  */
4469b963291cSDaniel Vetter void intel_runtime_pm_enable_interrupts(struct drm_i915_private *dev_priv)
4470c67a470bSPaulo Zanoni {
4471ad1443f0SSagar Arun Kamble 	dev_priv->runtime_pm.irqs_enabled = true;
4472b318b824SVille Syrjälä 	intel_irq_reset(dev_priv);
4473b318b824SVille Syrjälä 	intel_irq_postinstall(dev_priv);
4474c67a470bSPaulo Zanoni }
4475d64575eeSJani Nikula 
4476d64575eeSJani Nikula bool intel_irqs_enabled(struct drm_i915_private *dev_priv)
4477d64575eeSJani Nikula {
4478d64575eeSJani Nikula 	/*
4479d64575eeSJani Nikula 	 * We only use drm_irq_uninstall() at unload and VT switch, so
4480d64575eeSJani Nikula 	 * this is the only thing we need to check.
4481d64575eeSJani Nikula 	 */
4482d64575eeSJani Nikula 	return dev_priv->runtime_pm.irqs_enabled;
4483d64575eeSJani Nikula }
4484d64575eeSJani Nikula 
4485d64575eeSJani Nikula void intel_synchronize_irq(struct drm_i915_private *i915)
4486d64575eeSJani Nikula {
4487d64575eeSJani Nikula 	synchronize_irq(i915->drm.pdev->irq);
4488d64575eeSJani Nikula }
4489