xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 7794b6deb12176112cf6050dd8507cf216e801b9)
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 
377785ae0bSVille Syrjälä #include "display/intel_de.h"
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ä 
195373abf1aSMatt Roper 	if (DISPLAY_VER(dev_priv) >= 11)
1960398993bSVille Syrjälä 		hpd->hpd = hpd_gen11;
19770bfb307SMatt Roper 	else if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
1980398993bSVille Syrjälä 		hpd->hpd = hpd_bxt;
199373abf1aSMatt Roper 	else if (DISPLAY_VER(dev_priv) >= 8)
2000398993bSVille Syrjälä 		hpd->hpd = hpd_bdw;
201373abf1aSMatt Roper 	else if (DISPLAY_VER(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ä 
2103176fb66SMatt Roper 	if (INTEL_PCH_TYPE(dev_priv) >= PCH_DG1)
211229f31e2SLucas De Marchi 		hpd->pch_hpd = hpd_sde_dg1;
212fa58c9e4SAnusha Srivatsa 	else if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
2130398993bSVille Syrjälä 		hpd->pch_hpd = hpd_icp;
2140398993bSVille Syrjälä 	else if (HAS_PCH_CNP(dev_priv) || HAS_PCH_SPT(dev_priv))
2150398993bSVille Syrjälä 		hpd->pch_hpd = hpd_spt;
2160398993bSVille Syrjälä 	else if (HAS_PCH_LPT(dev_priv) || HAS_PCH_CPT(dev_priv))
2170398993bSVille Syrjälä 		hpd->pch_hpd = hpd_cpt;
2180398993bSVille Syrjälä 	else if (HAS_PCH_IBX(dev_priv))
2190398993bSVille Syrjälä 		hpd->pch_hpd = hpd_ibx;
2200398993bSVille Syrjälä 	else
2210398993bSVille Syrjälä 		MISSING_CASE(INTEL_PCH_TYPE(dev_priv));
2220398993bSVille Syrjälä }
2230398993bSVille Syrjälä 
224aca9310aSAnshuman Gupta static void
225aca9310aSAnshuman Gupta intel_handle_vblank(struct drm_i915_private *dev_priv, enum pipe pipe)
226aca9310aSAnshuman Gupta {
227*7794b6deSJani Nikula 	struct intel_crtc *crtc = intel_crtc_for_pipe(dev_priv, pipe);
228aca9310aSAnshuman Gupta 
229aca9310aSAnshuman Gupta 	drm_crtc_handle_vblank(&crtc->base);
230aca9310aSAnshuman Gupta }
231aca9310aSAnshuman Gupta 
232cf1c97dcSAndi Shyti void gen3_irq_reset(struct intel_uncore *uncore, i915_reg_t imr,
23368eb49b1SPaulo Zanoni 		    i915_reg_t iir, i915_reg_t ier)
23468eb49b1SPaulo Zanoni {
23565f42cdcSPaulo Zanoni 	intel_uncore_write(uncore, imr, 0xffffffff);
23665f42cdcSPaulo Zanoni 	intel_uncore_posting_read(uncore, imr);
23768eb49b1SPaulo Zanoni 
23865f42cdcSPaulo Zanoni 	intel_uncore_write(uncore, ier, 0);
23968eb49b1SPaulo Zanoni 
2405c502442SPaulo Zanoni 	/* IIR can theoretically queue up two events. Be paranoid. */
24165f42cdcSPaulo Zanoni 	intel_uncore_write(uncore, iir, 0xffffffff);
24265f42cdcSPaulo Zanoni 	intel_uncore_posting_read(uncore, iir);
24365f42cdcSPaulo Zanoni 	intel_uncore_write(uncore, iir, 0xffffffff);
24465f42cdcSPaulo Zanoni 	intel_uncore_posting_read(uncore, iir);
24568eb49b1SPaulo Zanoni }
2465c502442SPaulo Zanoni 
247cf1c97dcSAndi Shyti void gen2_irq_reset(struct intel_uncore *uncore)
24868eb49b1SPaulo Zanoni {
24965f42cdcSPaulo Zanoni 	intel_uncore_write16(uncore, GEN2_IMR, 0xffff);
25065f42cdcSPaulo Zanoni 	intel_uncore_posting_read16(uncore, GEN2_IMR);
251a9d356a6SPaulo Zanoni 
25265f42cdcSPaulo Zanoni 	intel_uncore_write16(uncore, GEN2_IER, 0);
25368eb49b1SPaulo Zanoni 
25468eb49b1SPaulo Zanoni 	/* IIR can theoretically queue up two events. Be paranoid. */
25565f42cdcSPaulo Zanoni 	intel_uncore_write16(uncore, GEN2_IIR, 0xffff);
25665f42cdcSPaulo Zanoni 	intel_uncore_posting_read16(uncore, GEN2_IIR);
25765f42cdcSPaulo Zanoni 	intel_uncore_write16(uncore, GEN2_IIR, 0xffff);
25865f42cdcSPaulo Zanoni 	intel_uncore_posting_read16(uncore, GEN2_IIR);
25968eb49b1SPaulo Zanoni }
26068eb49b1SPaulo Zanoni 
261337ba017SPaulo Zanoni /*
262337ba017SPaulo Zanoni  * We should clear IMR at preinstall/uninstall, and just check at postinstall.
263337ba017SPaulo Zanoni  */
26465f42cdcSPaulo Zanoni static void gen3_assert_iir_is_zero(struct intel_uncore *uncore, i915_reg_t reg)
265b51a2842SVille Syrjälä {
26665f42cdcSPaulo Zanoni 	u32 val = intel_uncore_read(uncore, reg);
267b51a2842SVille Syrjälä 
268b51a2842SVille Syrjälä 	if (val == 0)
269b51a2842SVille Syrjälä 		return;
270b51a2842SVille Syrjälä 
271a9f236d1SPankaj Bharadiya 	drm_WARN(&uncore->i915->drm, 1,
272a9f236d1SPankaj Bharadiya 		 "Interrupt register 0x%x is not zero: 0x%08x\n",
273f0f59a00SVille Syrjälä 		 i915_mmio_reg_offset(reg), val);
27465f42cdcSPaulo Zanoni 	intel_uncore_write(uncore, reg, 0xffffffff);
27565f42cdcSPaulo Zanoni 	intel_uncore_posting_read(uncore, reg);
27665f42cdcSPaulo Zanoni 	intel_uncore_write(uncore, reg, 0xffffffff);
27765f42cdcSPaulo Zanoni 	intel_uncore_posting_read(uncore, reg);
278b51a2842SVille Syrjälä }
279337ba017SPaulo Zanoni 
28065f42cdcSPaulo Zanoni static void gen2_assert_iir_is_zero(struct intel_uncore *uncore)
281e9e9848aSVille Syrjälä {
28265f42cdcSPaulo Zanoni 	u16 val = intel_uncore_read16(uncore, GEN2_IIR);
283e9e9848aSVille Syrjälä 
284e9e9848aSVille Syrjälä 	if (val == 0)
285e9e9848aSVille Syrjälä 		return;
286e9e9848aSVille Syrjälä 
287a9f236d1SPankaj Bharadiya 	drm_WARN(&uncore->i915->drm, 1,
288a9f236d1SPankaj Bharadiya 		 "Interrupt register 0x%x is not zero: 0x%08x\n",
2899d9523d8SPaulo Zanoni 		 i915_mmio_reg_offset(GEN2_IIR), val);
29065f42cdcSPaulo Zanoni 	intel_uncore_write16(uncore, GEN2_IIR, 0xffff);
29165f42cdcSPaulo Zanoni 	intel_uncore_posting_read16(uncore, GEN2_IIR);
29265f42cdcSPaulo Zanoni 	intel_uncore_write16(uncore, GEN2_IIR, 0xffff);
29365f42cdcSPaulo Zanoni 	intel_uncore_posting_read16(uncore, GEN2_IIR);
294e9e9848aSVille Syrjälä }
295e9e9848aSVille Syrjälä 
296cf1c97dcSAndi Shyti void gen3_irq_init(struct intel_uncore *uncore,
29768eb49b1SPaulo Zanoni 		   i915_reg_t imr, u32 imr_val,
29868eb49b1SPaulo Zanoni 		   i915_reg_t ier, u32 ier_val,
29968eb49b1SPaulo Zanoni 		   i915_reg_t iir)
30068eb49b1SPaulo Zanoni {
30165f42cdcSPaulo Zanoni 	gen3_assert_iir_is_zero(uncore, iir);
30235079899SPaulo Zanoni 
30365f42cdcSPaulo Zanoni 	intel_uncore_write(uncore, ier, ier_val);
30465f42cdcSPaulo Zanoni 	intel_uncore_write(uncore, imr, imr_val);
30565f42cdcSPaulo Zanoni 	intel_uncore_posting_read(uncore, imr);
30668eb49b1SPaulo Zanoni }
30735079899SPaulo Zanoni 
308cf1c97dcSAndi Shyti void gen2_irq_init(struct intel_uncore *uncore,
3092918c3caSPaulo Zanoni 		   u32 imr_val, u32 ier_val)
31068eb49b1SPaulo Zanoni {
31165f42cdcSPaulo Zanoni 	gen2_assert_iir_is_zero(uncore);
31268eb49b1SPaulo Zanoni 
31365f42cdcSPaulo Zanoni 	intel_uncore_write16(uncore, GEN2_IER, ier_val);
31465f42cdcSPaulo Zanoni 	intel_uncore_write16(uncore, GEN2_IMR, imr_val);
31565f42cdcSPaulo Zanoni 	intel_uncore_posting_read16(uncore, GEN2_IMR);
31668eb49b1SPaulo Zanoni }
31768eb49b1SPaulo Zanoni 
3180706f17cSEgbert Eich /* For display hotplug interrupt */
3190706f17cSEgbert Eich static inline void
3200706f17cSEgbert Eich i915_hotplug_interrupt_update_locked(struct drm_i915_private *dev_priv,
321a9c287c9SJani Nikula 				     u32 mask,
322a9c287c9SJani Nikula 				     u32 bits)
3230706f17cSEgbert Eich {
324a9c287c9SJani Nikula 	u32 val;
3250706f17cSEgbert Eich 
32667520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
32748a1b8d4SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, bits & ~mask);
3280706f17cSEgbert Eich 
3292939eb06SJani Nikula 	val = intel_uncore_read(&dev_priv->uncore, PORT_HOTPLUG_EN);
3300706f17cSEgbert Eich 	val &= ~mask;
3310706f17cSEgbert Eich 	val |= bits;
3322939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, PORT_HOTPLUG_EN, val);
3330706f17cSEgbert Eich }
3340706f17cSEgbert Eich 
3350706f17cSEgbert Eich /**
3360706f17cSEgbert Eich  * i915_hotplug_interrupt_update - update hotplug interrupt enable
3370706f17cSEgbert Eich  * @dev_priv: driver private
3380706f17cSEgbert Eich  * @mask: bits to update
3390706f17cSEgbert Eich  * @bits: bits to enable
3400706f17cSEgbert Eich  * NOTE: the HPD enable bits are modified both inside and outside
3410706f17cSEgbert Eich  * of an interrupt context. To avoid that read-modify-write cycles
3420706f17cSEgbert Eich  * interfer, these bits are protected by a spinlock. Since this
3430706f17cSEgbert Eich  * function is usually not called from a context where the lock is
3440706f17cSEgbert Eich  * held already, this function acquires the lock itself. A non-locking
3450706f17cSEgbert Eich  * version is also available.
3460706f17cSEgbert Eich  */
3470706f17cSEgbert Eich void i915_hotplug_interrupt_update(struct drm_i915_private *dev_priv,
348a9c287c9SJani Nikula 				   u32 mask,
349a9c287c9SJani Nikula 				   u32 bits)
3500706f17cSEgbert Eich {
3510706f17cSEgbert Eich 	spin_lock_irq(&dev_priv->irq_lock);
3520706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv, mask, bits);
3530706f17cSEgbert Eich 	spin_unlock_irq(&dev_priv->irq_lock);
3540706f17cSEgbert Eich }
3550706f17cSEgbert Eich 
356d9dc34f1SVille Syrjälä /**
357d9dc34f1SVille Syrjälä  * ilk_update_display_irq - update DEIMR
358d9dc34f1SVille Syrjälä  * @dev_priv: driver private
359d9dc34f1SVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
360d9dc34f1SVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
361d9dc34f1SVille Syrjälä  */
3629e6dcf33SJani Nikula static void ilk_update_display_irq(struct drm_i915_private *dev_priv,
3639e6dcf33SJani Nikula 				   u32 interrupt_mask, u32 enabled_irq_mask)
364036a4a7dSZhenyu Wang {
365a9c287c9SJani Nikula 	u32 new_val;
366d9dc34f1SVille Syrjälä 
36767520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
36848a1b8d4SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, enabled_irq_mask & ~interrupt_mask);
369d9dc34f1SVille Syrjälä 
370d9dc34f1SVille Syrjälä 	new_val = dev_priv->irq_mask;
371d9dc34f1SVille Syrjälä 	new_val &= ~interrupt_mask;
372d9dc34f1SVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
373d9dc34f1SVille Syrjälä 
374e44adb5dSChris Wilson 	if (new_val != dev_priv->irq_mask &&
375e44adb5dSChris Wilson 	    !drm_WARN_ON(&dev_priv->drm, !intel_irqs_enabled(dev_priv))) {
376d9dc34f1SVille Syrjälä 		dev_priv->irq_mask = new_val;
3772939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, DEIMR, dev_priv->irq_mask);
3782939eb06SJani Nikula 		intel_uncore_posting_read(&dev_priv->uncore, DEIMR);
379036a4a7dSZhenyu Wang 	}
380036a4a7dSZhenyu Wang }
381036a4a7dSZhenyu Wang 
3829e6dcf33SJani Nikula void ilk_enable_display_irq(struct drm_i915_private *i915, u32 bits)
3839e6dcf33SJani Nikula {
3849e6dcf33SJani Nikula 	ilk_update_display_irq(i915, bits, bits);
3859e6dcf33SJani Nikula }
3869e6dcf33SJani Nikula 
3879e6dcf33SJani Nikula void ilk_disable_display_irq(struct drm_i915_private *i915, u32 bits)
3889e6dcf33SJani Nikula {
3899e6dcf33SJani Nikula 	ilk_update_display_irq(i915, bits, 0);
3909e6dcf33SJani Nikula }
3919e6dcf33SJani Nikula 
3920961021aSBen Widawsky /**
3933a3b3c7dSVille Syrjälä  * bdw_update_port_irq - update DE port interrupt
3943a3b3c7dSVille Syrjälä  * @dev_priv: driver private
3953a3b3c7dSVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
3963a3b3c7dSVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
3973a3b3c7dSVille Syrjälä  */
3983a3b3c7dSVille Syrjälä static void bdw_update_port_irq(struct drm_i915_private *dev_priv,
399a9c287c9SJani Nikula 				u32 interrupt_mask,
400a9c287c9SJani Nikula 				u32 enabled_irq_mask)
4013a3b3c7dSVille Syrjälä {
402a9c287c9SJani Nikula 	u32 new_val;
403a9c287c9SJani Nikula 	u32 old_val;
4043a3b3c7dSVille Syrjälä 
40567520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
4063a3b3c7dSVille Syrjälä 
40748a1b8d4SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, enabled_irq_mask & ~interrupt_mask);
4083a3b3c7dSVille Syrjälä 
40948a1b8d4SPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, !intel_irqs_enabled(dev_priv)))
4103a3b3c7dSVille Syrjälä 		return;
4113a3b3c7dSVille Syrjälä 
4122939eb06SJani Nikula 	old_val = intel_uncore_read(&dev_priv->uncore, GEN8_DE_PORT_IMR);
4133a3b3c7dSVille Syrjälä 
4143a3b3c7dSVille Syrjälä 	new_val = old_val;
4153a3b3c7dSVille Syrjälä 	new_val &= ~interrupt_mask;
4163a3b3c7dSVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
4173a3b3c7dSVille Syrjälä 
4183a3b3c7dSVille Syrjälä 	if (new_val != old_val) {
4192939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, GEN8_DE_PORT_IMR, new_val);
4202939eb06SJani Nikula 		intel_uncore_posting_read(&dev_priv->uncore, GEN8_DE_PORT_IMR);
4213a3b3c7dSVille Syrjälä 	}
4223a3b3c7dSVille Syrjälä }
4233a3b3c7dSVille Syrjälä 
4243a3b3c7dSVille Syrjälä /**
425013d3752SVille Syrjälä  * bdw_update_pipe_irq - update DE pipe interrupt
426013d3752SVille Syrjälä  * @dev_priv: driver private
427013d3752SVille Syrjälä  * @pipe: pipe whose interrupt to update
428013d3752SVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
429013d3752SVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
430013d3752SVille Syrjälä  */
4319e6dcf33SJani Nikula static void bdw_update_pipe_irq(struct drm_i915_private *dev_priv,
4329e6dcf33SJani Nikula 				enum pipe pipe, u32 interrupt_mask,
433a9c287c9SJani Nikula 				u32 enabled_irq_mask)
434013d3752SVille Syrjälä {
435a9c287c9SJani Nikula 	u32 new_val;
436013d3752SVille Syrjälä 
43767520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
438013d3752SVille Syrjälä 
43948a1b8d4SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, enabled_irq_mask & ~interrupt_mask);
440013d3752SVille Syrjälä 
44148a1b8d4SPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, !intel_irqs_enabled(dev_priv)))
442013d3752SVille Syrjälä 		return;
443013d3752SVille Syrjälä 
444013d3752SVille Syrjälä 	new_val = dev_priv->de_irq_mask[pipe];
445013d3752SVille Syrjälä 	new_val &= ~interrupt_mask;
446013d3752SVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
447013d3752SVille Syrjälä 
448013d3752SVille Syrjälä 	if (new_val != dev_priv->de_irq_mask[pipe]) {
449013d3752SVille Syrjälä 		dev_priv->de_irq_mask[pipe] = new_val;
4502939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
4512939eb06SJani Nikula 		intel_uncore_posting_read(&dev_priv->uncore, GEN8_DE_PIPE_IMR(pipe));
452013d3752SVille Syrjälä 	}
453013d3752SVille Syrjälä }
454013d3752SVille Syrjälä 
4559e6dcf33SJani Nikula void bdw_enable_pipe_irq(struct drm_i915_private *i915,
4569e6dcf33SJani Nikula 			 enum pipe pipe, u32 bits)
4579e6dcf33SJani Nikula {
4589e6dcf33SJani Nikula 	bdw_update_pipe_irq(i915, pipe, bits, bits);
4599e6dcf33SJani Nikula }
4609e6dcf33SJani Nikula 
4619e6dcf33SJani Nikula void bdw_disable_pipe_irq(struct drm_i915_private *i915,
4629e6dcf33SJani Nikula 			  enum pipe pipe, u32 bits)
4639e6dcf33SJani Nikula {
4649e6dcf33SJani Nikula 	bdw_update_pipe_irq(i915, pipe, bits, 0);
4659e6dcf33SJani Nikula }
4669e6dcf33SJani Nikula 
467013d3752SVille Syrjälä /**
468fee884edSDaniel Vetter  * ibx_display_interrupt_update - update SDEIMR
469fee884edSDaniel Vetter  * @dev_priv: driver private
470fee884edSDaniel Vetter  * @interrupt_mask: mask of interrupt bits to update
471fee884edSDaniel Vetter  * @enabled_irq_mask: mask of interrupt bits to enable
472fee884edSDaniel Vetter  */
4739e6dcf33SJani Nikula static void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
474a9c287c9SJani Nikula 					 u32 interrupt_mask,
475a9c287c9SJani Nikula 					 u32 enabled_irq_mask)
476fee884edSDaniel Vetter {
4772939eb06SJani Nikula 	u32 sdeimr = intel_uncore_read(&dev_priv->uncore, SDEIMR);
478fee884edSDaniel Vetter 	sdeimr &= ~interrupt_mask;
479fee884edSDaniel Vetter 	sdeimr |= (~enabled_irq_mask & interrupt_mask);
480fee884edSDaniel Vetter 
48148a1b8d4SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, enabled_irq_mask & ~interrupt_mask);
48215a17aaeSDaniel Vetter 
48367520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
484fee884edSDaniel Vetter 
48548a1b8d4SPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, !intel_irqs_enabled(dev_priv)))
486c67a470bSPaulo Zanoni 		return;
487c67a470bSPaulo Zanoni 
4882939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, SDEIMR, sdeimr);
4892939eb06SJani Nikula 	intel_uncore_posting_read(&dev_priv->uncore, SDEIMR);
490fee884edSDaniel Vetter }
4918664281bSPaulo Zanoni 
4929e6dcf33SJani Nikula void ibx_enable_display_interrupt(struct drm_i915_private *i915, u32 bits)
4939e6dcf33SJani Nikula {
4949e6dcf33SJani Nikula 	ibx_display_interrupt_update(i915, bits, bits);
4959e6dcf33SJani Nikula }
4969e6dcf33SJani Nikula 
4979e6dcf33SJani Nikula void ibx_disable_display_interrupt(struct drm_i915_private *i915, u32 bits)
4989e6dcf33SJani Nikula {
4999e6dcf33SJani Nikula 	ibx_display_interrupt_update(i915, bits, 0);
5009e6dcf33SJani Nikula }
5019e6dcf33SJani Nikula 
5026b12ca56SVille Syrjälä u32 i915_pipestat_enable_mask(struct drm_i915_private *dev_priv,
5036b12ca56SVille Syrjälä 			      enum pipe pipe)
5047c463586SKeith Packard {
5056b12ca56SVille Syrjälä 	u32 status_mask = dev_priv->pipestat_irq_mask[pipe];
50610c59c51SImre Deak 	u32 enable_mask = status_mask << 16;
50710c59c51SImre Deak 
5086b12ca56SVille Syrjälä 	lockdep_assert_held(&dev_priv->irq_lock);
5096b12ca56SVille Syrjälä 
510373abf1aSMatt Roper 	if (DISPLAY_VER(dev_priv) < 5)
5116b12ca56SVille Syrjälä 		goto out;
5126b12ca56SVille Syrjälä 
51310c59c51SImre Deak 	/*
514724a6905SVille Syrjälä 	 * On pipe A we don't support the PSR interrupt yet,
515724a6905SVille Syrjälä 	 * on pipe B and C the same bit MBZ.
51610c59c51SImre Deak 	 */
51748a1b8d4SPankaj Bharadiya 	if (drm_WARN_ON_ONCE(&dev_priv->drm,
51848a1b8d4SPankaj Bharadiya 			     status_mask & PIPE_A_PSR_STATUS_VLV))
51910c59c51SImre Deak 		return 0;
520724a6905SVille Syrjälä 	/*
521724a6905SVille Syrjälä 	 * On pipe B and C we don't support the PSR interrupt yet, on pipe
522724a6905SVille Syrjälä 	 * A the same bit is for perf counters which we don't use either.
523724a6905SVille Syrjälä 	 */
52448a1b8d4SPankaj Bharadiya 	if (drm_WARN_ON_ONCE(&dev_priv->drm,
52548a1b8d4SPankaj Bharadiya 			     status_mask & PIPE_B_PSR_STATUS_VLV))
526724a6905SVille Syrjälä 		return 0;
52710c59c51SImre Deak 
52810c59c51SImre Deak 	enable_mask &= ~(PIPE_FIFO_UNDERRUN_STATUS |
52910c59c51SImre Deak 			 SPRITE0_FLIP_DONE_INT_EN_VLV |
53010c59c51SImre Deak 			 SPRITE1_FLIP_DONE_INT_EN_VLV);
53110c59c51SImre Deak 	if (status_mask & SPRITE0_FLIP_DONE_INT_STATUS_VLV)
53210c59c51SImre Deak 		enable_mask |= SPRITE0_FLIP_DONE_INT_EN_VLV;
53310c59c51SImre Deak 	if (status_mask & SPRITE1_FLIP_DONE_INT_STATUS_VLV)
53410c59c51SImre Deak 		enable_mask |= SPRITE1_FLIP_DONE_INT_EN_VLV;
53510c59c51SImre Deak 
5366b12ca56SVille Syrjälä out:
53748a1b8d4SPankaj Bharadiya 	drm_WARN_ONCE(&dev_priv->drm,
53848a1b8d4SPankaj Bharadiya 		      enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
5396b12ca56SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
5406b12ca56SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
5416b12ca56SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask);
5426b12ca56SVille Syrjälä 
54310c59c51SImre Deak 	return enable_mask;
54410c59c51SImre Deak }
54510c59c51SImre Deak 
5466b12ca56SVille Syrjälä void i915_enable_pipestat(struct drm_i915_private *dev_priv,
5476b12ca56SVille Syrjälä 			  enum pipe pipe, u32 status_mask)
548755e9019SImre Deak {
5496b12ca56SVille Syrjälä 	i915_reg_t reg = PIPESTAT(pipe);
550755e9019SImre Deak 	u32 enable_mask;
551755e9019SImre Deak 
55248a1b8d4SPankaj Bharadiya 	drm_WARN_ONCE(&dev_priv->drm, status_mask & ~PIPESTAT_INT_STATUS_MASK,
5536b12ca56SVille Syrjälä 		      "pipe %c: status_mask=0x%x\n",
5546b12ca56SVille Syrjälä 		      pipe_name(pipe), status_mask);
5556b12ca56SVille Syrjälä 
5566b12ca56SVille Syrjälä 	lockdep_assert_held(&dev_priv->irq_lock);
55748a1b8d4SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, !intel_irqs_enabled(dev_priv));
5586b12ca56SVille Syrjälä 
5596b12ca56SVille Syrjälä 	if ((dev_priv->pipestat_irq_mask[pipe] & status_mask) == status_mask)
5606b12ca56SVille Syrjälä 		return;
5616b12ca56SVille Syrjälä 
5626b12ca56SVille Syrjälä 	dev_priv->pipestat_irq_mask[pipe] |= status_mask;
5636b12ca56SVille Syrjälä 	enable_mask = i915_pipestat_enable_mask(dev_priv, pipe);
5646b12ca56SVille Syrjälä 
5652939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, reg, enable_mask | status_mask);
5662939eb06SJani Nikula 	intel_uncore_posting_read(&dev_priv->uncore, reg);
567755e9019SImre Deak }
568755e9019SImre Deak 
5696b12ca56SVille Syrjälä void i915_disable_pipestat(struct drm_i915_private *dev_priv,
5706b12ca56SVille Syrjälä 			   enum pipe pipe, u32 status_mask)
571755e9019SImre Deak {
5726b12ca56SVille Syrjälä 	i915_reg_t reg = PIPESTAT(pipe);
573755e9019SImre Deak 	u32 enable_mask;
574755e9019SImre Deak 
57548a1b8d4SPankaj Bharadiya 	drm_WARN_ONCE(&dev_priv->drm, status_mask & ~PIPESTAT_INT_STATUS_MASK,
5766b12ca56SVille Syrjälä 		      "pipe %c: status_mask=0x%x\n",
5776b12ca56SVille Syrjälä 		      pipe_name(pipe), status_mask);
5786b12ca56SVille Syrjälä 
5796b12ca56SVille Syrjälä 	lockdep_assert_held(&dev_priv->irq_lock);
58048a1b8d4SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, !intel_irqs_enabled(dev_priv));
5816b12ca56SVille Syrjälä 
5826b12ca56SVille Syrjälä 	if ((dev_priv->pipestat_irq_mask[pipe] & status_mask) == 0)
5836b12ca56SVille Syrjälä 		return;
5846b12ca56SVille Syrjälä 
5856b12ca56SVille Syrjälä 	dev_priv->pipestat_irq_mask[pipe] &= ~status_mask;
5866b12ca56SVille Syrjälä 	enable_mask = i915_pipestat_enable_mask(dev_priv, pipe);
5876b12ca56SVille Syrjälä 
5882939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, reg, enable_mask | status_mask);
5892939eb06SJani Nikula 	intel_uncore_posting_read(&dev_priv->uncore, reg);
590755e9019SImre Deak }
591755e9019SImre Deak 
592f3e30485SVille Syrjälä static bool i915_has_asle(struct drm_i915_private *dev_priv)
593f3e30485SVille Syrjälä {
594f3e30485SVille Syrjälä 	if (!dev_priv->opregion.asle)
595f3e30485SVille Syrjälä 		return false;
596f3e30485SVille Syrjälä 
597f3e30485SVille Syrjälä 	return IS_PINEVIEW(dev_priv) || IS_MOBILE(dev_priv);
598f3e30485SVille Syrjälä }
599f3e30485SVille Syrjälä 
600c0e09200SDave Airlie /**
601f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
60214bb2c11STvrtko Ursulin  * @dev_priv: i915 device private
60301c66889SZhao Yakui  */
60491d14251STvrtko Ursulin static void i915_enable_asle_pipestat(struct drm_i915_private *dev_priv)
60501c66889SZhao Yakui {
606f3e30485SVille Syrjälä 	if (!i915_has_asle(dev_priv))
607f49e38ddSJani Nikula 		return;
608f49e38ddSJani Nikula 
60913321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
61001c66889SZhao Yakui 
611755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_STATUS);
612373abf1aSMatt Roper 	if (DISPLAY_VER(dev_priv) >= 4)
6133b6c42e8SDaniel Vetter 		i915_enable_pipestat(dev_priv, PIPE_A,
614755e9019SImre Deak 				     PIPE_LEGACY_BLC_EVENT_STATUS);
6151ec14ad3SChris Wilson 
61613321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
61701c66889SZhao Yakui }
61801c66889SZhao Yakui 
619f75f3746SVille Syrjälä /*
620f75f3746SVille Syrjälä  * This timing diagram depicts the video signal in and
621f75f3746SVille Syrjälä  * around the vertical blanking period.
622f75f3746SVille Syrjälä  *
623f75f3746SVille Syrjälä  * Assumptions about the fictitious mode used in this example:
624f75f3746SVille Syrjälä  *  vblank_start >= 3
625f75f3746SVille Syrjälä  *  vsync_start = vblank_start + 1
626f75f3746SVille Syrjälä  *  vsync_end = vblank_start + 2
627f75f3746SVille Syrjälä  *  vtotal = vblank_start + 3
628f75f3746SVille Syrjälä  *
629f75f3746SVille Syrjälä  *           start of vblank:
630f75f3746SVille Syrjälä  *           latch double buffered registers
631f75f3746SVille Syrjälä  *           increment frame counter (ctg+)
632f75f3746SVille Syrjälä  *           generate start of vblank interrupt (gen4+)
633f75f3746SVille Syrjälä  *           |
634f75f3746SVille Syrjälä  *           |          frame start:
635f75f3746SVille Syrjälä  *           |          generate frame start interrupt (aka. vblank interrupt) (gmch)
636f75f3746SVille Syrjälä  *           |          may be shifted forward 1-3 extra lines via PIPECONF
637f75f3746SVille Syrjälä  *           |          |
638f75f3746SVille Syrjälä  *           |          |  start of vsync:
639f75f3746SVille Syrjälä  *           |          |  generate vsync interrupt
640f75f3746SVille Syrjälä  *           |          |  |
641f75f3746SVille Syrjälä  * ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx
642f75f3746SVille Syrjälä  *       .   \hs/   .      \hs/          \hs/          \hs/   .      \hs/
643f75f3746SVille Syrjälä  * ----va---> <-----------------vb--------------------> <--------va-------------
644f75f3746SVille Syrjälä  *       |          |       <----vs----->                     |
645f75f3746SVille Syrjälä  * -vbs-----> <---vbs+1---> <---vbs+2---> <-----0-----> <-----1-----> <-----2--- (scanline counter gen2)
646f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2---> <-----0--- (scanline counter gen3+)
647f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2- (scanline counter hsw+ hdmi)
648f75f3746SVille Syrjälä  *       |          |                                         |
649f75f3746SVille Syrjälä  *       last visible pixel                                   first visible pixel
650f75f3746SVille Syrjälä  *                  |                                         increment frame counter (gen3/4)
651f75f3746SVille Syrjälä  *                  pixel counter = vblank_start * htotal     pixel counter = 0 (gen3/4)
652f75f3746SVille Syrjälä  *
653f75f3746SVille Syrjälä  * x  = horizontal active
654f75f3746SVille Syrjälä  * _  = horizontal blanking
655f75f3746SVille Syrjälä  * hs = horizontal sync
656f75f3746SVille Syrjälä  * va = vertical active
657f75f3746SVille Syrjälä  * vb = vertical blanking
658f75f3746SVille Syrjälä  * vs = vertical sync
659f75f3746SVille Syrjälä  * vbs = vblank_start (number)
660f75f3746SVille Syrjälä  *
661f75f3746SVille Syrjälä  * Summary:
662f75f3746SVille Syrjälä  * - most events happen at the start of horizontal sync
663f75f3746SVille Syrjälä  * - frame start happens at the start of horizontal blank, 1-4 lines
664f75f3746SVille Syrjälä  *   (depending on PIPECONF settings) after the start of vblank
665f75f3746SVille Syrjälä  * - gen3/4 pixel and frame counter are synchronized with the start
666f75f3746SVille Syrjälä  *   of horizontal active on the first line of vertical active
667f75f3746SVille Syrjälä  */
668f75f3746SVille Syrjälä 
66942f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
67042f52ef8SKeith Packard  * we use as a pipe index
67142f52ef8SKeith Packard  */
67208fa8fd0SVille Syrjälä u32 i915_get_vblank_counter(struct drm_crtc *crtc)
6730a3e67a4SJesse Barnes {
67408fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
67508fa8fd0SVille Syrjälä 	struct drm_vblank_crtc *vblank = &dev_priv->drm.vblank[drm_crtc_index(crtc)];
67632db0b65SVille Syrjälä 	const struct drm_display_mode *mode = &vblank->hwmode;
67708fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
678f0f59a00SVille Syrjälä 	i915_reg_t high_frame, low_frame;
6790b2a8e09SVille Syrjälä 	u32 high1, high2, low, pixel, vbl_start, hsync_start, htotal;
680694e409dSVille Syrjälä 	unsigned long irqflags;
681391f75e2SVille Syrjälä 
68232db0b65SVille Syrjälä 	/*
68332db0b65SVille Syrjälä 	 * On i965gm TV output the frame counter only works up to
68432db0b65SVille Syrjälä 	 * the point when we enable the TV encoder. After that the
68532db0b65SVille Syrjälä 	 * frame counter ceases to work and reads zero. We need a
68632db0b65SVille Syrjälä 	 * vblank wait before enabling the TV encoder and so we
68732db0b65SVille Syrjälä 	 * have to enable vblank interrupts while the frame counter
68832db0b65SVille Syrjälä 	 * is still in a working state. However the core vblank code
68932db0b65SVille Syrjälä 	 * does not like us returning non-zero frame counter values
69032db0b65SVille Syrjälä 	 * when we've told it that we don't have a working frame
69132db0b65SVille Syrjälä 	 * counter. Thus we must stop non-zero values leaking out.
69232db0b65SVille Syrjälä 	 */
69332db0b65SVille Syrjälä 	if (!vblank->max_vblank_count)
69432db0b65SVille Syrjälä 		return 0;
69532db0b65SVille Syrjälä 
6960b2a8e09SVille Syrjälä 	htotal = mode->crtc_htotal;
6970b2a8e09SVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
6980b2a8e09SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
6990b2a8e09SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
7000b2a8e09SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
701391f75e2SVille Syrjälä 
7020b2a8e09SVille Syrjälä 	/* Convert to pixel count */
7030b2a8e09SVille Syrjälä 	vbl_start *= htotal;
7040b2a8e09SVille Syrjälä 
7050b2a8e09SVille Syrjälä 	/* Start of vblank event occurs at start of hsync */
7060b2a8e09SVille Syrjälä 	vbl_start -= htotal - hsync_start;
7070b2a8e09SVille Syrjälä 
7089db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
7099db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
7105eddb70bSChris Wilson 
711694e409dSVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
712694e409dSVille Syrjälä 
7130a3e67a4SJesse Barnes 	/*
7140a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
7150a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
7160a3e67a4SJesse Barnes 	 * register.
7170a3e67a4SJesse Barnes 	 */
7180a3e67a4SJesse Barnes 	do {
7198cbda6b2SJani Nikula 		high1 = intel_de_read_fw(dev_priv, high_frame) & PIPE_FRAME_HIGH_MASK;
7208cbda6b2SJani Nikula 		low   = intel_de_read_fw(dev_priv, low_frame);
7218cbda6b2SJani Nikula 		high2 = intel_de_read_fw(dev_priv, high_frame) & PIPE_FRAME_HIGH_MASK;
7220a3e67a4SJesse Barnes 	} while (high1 != high2);
7230a3e67a4SJesse Barnes 
724694e409dSVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
725694e409dSVille Syrjälä 
7265eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
727391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
7285eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
729391f75e2SVille Syrjälä 
730391f75e2SVille Syrjälä 	/*
731391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
732391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
733391f75e2SVille Syrjälä 	 * counter against vblank start.
734391f75e2SVille Syrjälä 	 */
735edc08d0aSVille Syrjälä 	return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
7360a3e67a4SJesse Barnes }
7370a3e67a4SJesse Barnes 
73808fa8fd0SVille Syrjälä u32 g4x_get_vblank_counter(struct drm_crtc *crtc)
7399880b7a5SJesse Barnes {
74008fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
74133267703SVandita Kulkarni 	struct drm_vblank_crtc *vblank = &dev_priv->drm.vblank[drm_crtc_index(crtc)];
74208fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
7439880b7a5SJesse Barnes 
74433267703SVandita Kulkarni 	if (!vblank->max_vblank_count)
74533267703SVandita Kulkarni 		return 0;
74633267703SVandita Kulkarni 
7472939eb06SJani Nikula 	return intel_uncore_read(&dev_priv->uncore, PIPE_FRMCOUNT_G4X(pipe));
7489880b7a5SJesse Barnes }
7499880b7a5SJesse Barnes 
75006d6fda5SVille Syrjälä static u32 intel_crtc_scanlines_since_frame_timestamp(struct intel_crtc *crtc)
751aec0246fSUma Shankar {
752aec0246fSUma Shankar 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
753aec0246fSUma Shankar 	struct drm_vblank_crtc *vblank =
754aec0246fSUma Shankar 		&crtc->base.dev->vblank[drm_crtc_index(&crtc->base)];
755aec0246fSUma Shankar 	const struct drm_display_mode *mode = &vblank->hwmode;
756aec0246fSUma Shankar 	u32 htotal = mode->crtc_htotal;
757aec0246fSUma Shankar 	u32 clock = mode->crtc_clock;
75806d6fda5SVille Syrjälä 	u32 scan_prev_time, scan_curr_time, scan_post_time;
759aec0246fSUma Shankar 
760aec0246fSUma Shankar 	/*
761aec0246fSUma Shankar 	 * To avoid the race condition where we might cross into the
762aec0246fSUma Shankar 	 * next vblank just between the PIPE_FRMTMSTMP and TIMESTAMP_CTR
763aec0246fSUma Shankar 	 * reads. We make sure we read PIPE_FRMTMSTMP and TIMESTAMP_CTR
764aec0246fSUma Shankar 	 * during the same frame.
765aec0246fSUma Shankar 	 */
766aec0246fSUma Shankar 	do {
767aec0246fSUma Shankar 		/*
768aec0246fSUma Shankar 		 * This field provides read back of the display
769aec0246fSUma Shankar 		 * pipe frame time stamp. The time stamp value
770aec0246fSUma Shankar 		 * is sampled at every start of vertical blank.
771aec0246fSUma Shankar 		 */
7728cbda6b2SJani Nikula 		scan_prev_time = intel_de_read_fw(dev_priv,
7738cbda6b2SJani Nikula 						  PIPE_FRMTMSTMP(crtc->pipe));
774aec0246fSUma Shankar 
775aec0246fSUma Shankar 		/*
776aec0246fSUma Shankar 		 * The TIMESTAMP_CTR register has the current
777aec0246fSUma Shankar 		 * time stamp value.
778aec0246fSUma Shankar 		 */
7798cbda6b2SJani Nikula 		scan_curr_time = intel_de_read_fw(dev_priv, IVB_TIMESTAMP_CTR);
780aec0246fSUma Shankar 
7818cbda6b2SJani Nikula 		scan_post_time = intel_de_read_fw(dev_priv,
7828cbda6b2SJani Nikula 						  PIPE_FRMTMSTMP(crtc->pipe));
783aec0246fSUma Shankar 	} while (scan_post_time != scan_prev_time);
784aec0246fSUma Shankar 
78506d6fda5SVille Syrjälä 	return div_u64(mul_u32_u32(scan_curr_time - scan_prev_time,
786aec0246fSUma Shankar 				   clock), 1000 * htotal);
78706d6fda5SVille Syrjälä }
78806d6fda5SVille Syrjälä 
78906d6fda5SVille Syrjälä /*
79006d6fda5SVille Syrjälä  * On certain encoders on certain platforms, pipe
79106d6fda5SVille Syrjälä  * scanline register will not work to get the scanline,
79206d6fda5SVille Syrjälä  * since the timings are driven from the PORT or issues
79306d6fda5SVille Syrjälä  * with scanline register updates.
79406d6fda5SVille Syrjälä  * This function will use Framestamp and current
79506d6fda5SVille Syrjälä  * timestamp registers to calculate the scanline.
79606d6fda5SVille Syrjälä  */
79706d6fda5SVille Syrjälä static u32 __intel_get_crtc_scanline_from_timestamp(struct intel_crtc *crtc)
79806d6fda5SVille Syrjälä {
79906d6fda5SVille Syrjälä 	struct drm_vblank_crtc *vblank =
80006d6fda5SVille Syrjälä 		&crtc->base.dev->vblank[drm_crtc_index(&crtc->base)];
80106d6fda5SVille Syrjälä 	const struct drm_display_mode *mode = &vblank->hwmode;
80206d6fda5SVille Syrjälä 	u32 vblank_start = mode->crtc_vblank_start;
80306d6fda5SVille Syrjälä 	u32 vtotal = mode->crtc_vtotal;
80406d6fda5SVille Syrjälä 	u32 scanline;
80506d6fda5SVille Syrjälä 
80606d6fda5SVille Syrjälä 	scanline = intel_crtc_scanlines_since_frame_timestamp(crtc);
807aec0246fSUma Shankar 	scanline = min(scanline, vtotal - 1);
808aec0246fSUma Shankar 	scanline = (scanline + vblank_start) % vtotal;
809aec0246fSUma Shankar 
810aec0246fSUma Shankar 	return scanline;
811aec0246fSUma Shankar }
812aec0246fSUma Shankar 
8138cbda6b2SJani Nikula /*
8148cbda6b2SJani Nikula  * intel_de_read_fw(), only for fast reads of display block, no need for
8158cbda6b2SJani Nikula  * forcewake etc.
8168cbda6b2SJani Nikula  */
817a225f079SVille Syrjälä static int __intel_get_crtc_scanline(struct intel_crtc *crtc)
818a225f079SVille Syrjälä {
819a225f079SVille Syrjälä 	struct drm_device *dev = crtc->base.dev;
820fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
8215caa0feaSDaniel Vetter 	const struct drm_display_mode *mode;
8225caa0feaSDaniel Vetter 	struct drm_vblank_crtc *vblank;
823a225f079SVille Syrjälä 	enum pipe pipe = crtc->pipe;
82480715b2fSVille Syrjälä 	int position, vtotal;
825a225f079SVille Syrjälä 
82672259536SVille Syrjälä 	if (!crtc->active)
8272c6afc36SVille Syrjälä 		return 0;
82872259536SVille Syrjälä 
8295caa0feaSDaniel Vetter 	vblank = &crtc->base.dev->vblank[drm_crtc_index(&crtc->base)];
8305caa0feaSDaniel Vetter 	mode = &vblank->hwmode;
8315caa0feaSDaniel Vetter 
832af157b76SVille Syrjälä 	if (crtc->mode_flags & I915_MODE_FLAG_GET_SCANLINE_FROM_TIMESTAMP)
833aec0246fSUma Shankar 		return __intel_get_crtc_scanline_from_timestamp(crtc);
834aec0246fSUma Shankar 
83580715b2fSVille Syrjälä 	vtotal = mode->crtc_vtotal;
836a225f079SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
837a225f079SVille Syrjälä 		vtotal /= 2;
838a225f079SVille Syrjälä 
83993e7e61eSLucas De Marchi 	if (DISPLAY_VER(dev_priv) == 2)
8408cbda6b2SJani Nikula 		position = intel_de_read_fw(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
841a225f079SVille Syrjälä 	else
8428cbda6b2SJani Nikula 		position = intel_de_read_fw(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
843a225f079SVille Syrjälä 
844a225f079SVille Syrjälä 	/*
84541b578fbSJesse Barnes 	 * On HSW, the DSL reg (0x70000) appears to return 0 if we
84641b578fbSJesse Barnes 	 * read it just before the start of vblank.  So try it again
84741b578fbSJesse Barnes 	 * so we don't accidentally end up spanning a vblank frame
84841b578fbSJesse Barnes 	 * increment, causing the pipe_update_end() code to squak at us.
84941b578fbSJesse Barnes 	 *
85041b578fbSJesse Barnes 	 * The nature of this problem means we can't simply check the ISR
85141b578fbSJesse Barnes 	 * bit and return the vblank start value; nor can we use the scanline
85241b578fbSJesse Barnes 	 * debug register in the transcoder as it appears to have the same
85341b578fbSJesse Barnes 	 * problem.  We may need to extend this to include other platforms,
85441b578fbSJesse Barnes 	 * but so far testing only shows the problem on HSW.
85541b578fbSJesse Barnes 	 */
85691d14251STvrtko Ursulin 	if (HAS_DDI(dev_priv) && !position) {
85741b578fbSJesse Barnes 		int i, temp;
85841b578fbSJesse Barnes 
85941b578fbSJesse Barnes 		for (i = 0; i < 100; i++) {
86041b578fbSJesse Barnes 			udelay(1);
8618cbda6b2SJani Nikula 			temp = intel_de_read_fw(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
86241b578fbSJesse Barnes 			if (temp != position) {
86341b578fbSJesse Barnes 				position = temp;
86441b578fbSJesse Barnes 				break;
86541b578fbSJesse Barnes 			}
86641b578fbSJesse Barnes 		}
86741b578fbSJesse Barnes 	}
86841b578fbSJesse Barnes 
86941b578fbSJesse Barnes 	/*
87080715b2fSVille Syrjälä 	 * See update_scanline_offset() for the details on the
87180715b2fSVille Syrjälä 	 * scanline_offset adjustment.
872a225f079SVille Syrjälä 	 */
87380715b2fSVille Syrjälä 	return (position + crtc->scanline_offset) % vtotal;
874a225f079SVille Syrjälä }
875a225f079SVille Syrjälä 
8764bbffbf3SThomas Zimmermann static bool i915_get_crtc_scanoutpos(struct drm_crtc *_crtc,
8774bbffbf3SThomas Zimmermann 				     bool in_vblank_irq,
8784bbffbf3SThomas Zimmermann 				     int *vpos, int *hpos,
8793bb403bfSVille Syrjälä 				     ktime_t *stime, ktime_t *etime,
8803bb403bfSVille Syrjälä 				     const struct drm_display_mode *mode)
8810af7e4dfSMario Kleiner {
8824bbffbf3SThomas Zimmermann 	struct drm_device *dev = _crtc->dev;
883fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
8844bbffbf3SThomas Zimmermann 	struct intel_crtc *crtc = to_intel_crtc(_crtc);
885e8edae54SVille Syrjälä 	enum pipe pipe = crtc->pipe;
8863aa18df8SVille Syrjälä 	int position;
88778e8fc6bSVille Syrjälä 	int vbl_start, vbl_end, hsync_start, htotal, vtotal;
888ad3543edSMario Kleiner 	unsigned long irqflags;
889373abf1aSMatt Roper 	bool use_scanline_counter = DISPLAY_VER(dev_priv) >= 5 ||
89093e7e61eSLucas De Marchi 		IS_G4X(dev_priv) || DISPLAY_VER(dev_priv) == 2 ||
891af157b76SVille Syrjälä 		crtc->mode_flags & I915_MODE_FLAG_USE_SCANLINE_COUNTER;
8920af7e4dfSMario Kleiner 
89348a1b8d4SPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, !mode->crtc_clock)) {
89400376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm,
89500376ccfSWambui Karuga 			"trying to get scanoutpos for disabled "
8969db4a9c7SJesse Barnes 			"pipe %c\n", pipe_name(pipe));
8971bf6ad62SDaniel Vetter 		return false;
8980af7e4dfSMario Kleiner 	}
8990af7e4dfSMario Kleiner 
900c2baf4b7SVille Syrjälä 	htotal = mode->crtc_htotal;
90178e8fc6bSVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
902c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
903c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
904c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
9050af7e4dfSMario Kleiner 
906d31faf65SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
907d31faf65SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
908d31faf65SVille Syrjälä 		vbl_end /= 2;
909d31faf65SVille Syrjälä 		vtotal /= 2;
910d31faf65SVille Syrjälä 	}
911d31faf65SVille Syrjälä 
912ad3543edSMario Kleiner 	/*
913ad3543edSMario Kleiner 	 * Lock uncore.lock, as we will do multiple timing critical raw
914ad3543edSMario Kleiner 	 * register reads, potentially with preemption disabled, so the
915ad3543edSMario Kleiner 	 * following code must not block on uncore.lock.
916ad3543edSMario Kleiner 	 */
917ad3543edSMario Kleiner 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
918ad3543edSMario Kleiner 
919ad3543edSMario Kleiner 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
920ad3543edSMario Kleiner 
921ad3543edSMario Kleiner 	/* Get optional system timestamp before query. */
922ad3543edSMario Kleiner 	if (stime)
923ad3543edSMario Kleiner 		*stime = ktime_get();
924ad3543edSMario Kleiner 
9257a2ec4a0SVille Syrjälä 	if (crtc->mode_flags & I915_MODE_FLAG_VRR) {
9267a2ec4a0SVille Syrjälä 		int scanlines = intel_crtc_scanlines_since_frame_timestamp(crtc);
9277a2ec4a0SVille Syrjälä 
9287a2ec4a0SVille Syrjälä 		position = __intel_get_crtc_scanline(crtc);
9297a2ec4a0SVille Syrjälä 
9307a2ec4a0SVille Syrjälä 		/*
9317a2ec4a0SVille Syrjälä 		 * Already exiting vblank? If so, shift our position
9327a2ec4a0SVille Syrjälä 		 * so it looks like we're already apporaching the full
9337a2ec4a0SVille Syrjälä 		 * vblank end. This should make the generated timestamp
9347a2ec4a0SVille Syrjälä 		 * more or less match when the active portion will start.
9357a2ec4a0SVille Syrjälä 		 */
9367a2ec4a0SVille Syrjälä 		if (position >= vbl_start && scanlines < position)
9377a2ec4a0SVille Syrjälä 			position = min(crtc->vmax_vblank_start + scanlines, vtotal - 1);
9387a2ec4a0SVille Syrjälä 	} else if (use_scanline_counter) {
9390af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
9400af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
9410af7e4dfSMario Kleiner 		 */
942e8edae54SVille Syrjälä 		position = __intel_get_crtc_scanline(crtc);
9430af7e4dfSMario Kleiner 	} else {
9440af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
9450af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
9460af7e4dfSMario Kleiner 		 * scanout position.
9470af7e4dfSMario Kleiner 		 */
9488cbda6b2SJani Nikula 		position = (intel_de_read_fw(dev_priv, PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
9490af7e4dfSMario Kleiner 
9503aa18df8SVille Syrjälä 		/* convert to pixel counts */
9513aa18df8SVille Syrjälä 		vbl_start *= htotal;
9523aa18df8SVille Syrjälä 		vbl_end *= htotal;
9533aa18df8SVille Syrjälä 		vtotal *= htotal;
95478e8fc6bSVille Syrjälä 
95578e8fc6bSVille Syrjälä 		/*
9567e78f1cbSVille Syrjälä 		 * In interlaced modes, the pixel counter counts all pixels,
9577e78f1cbSVille Syrjälä 		 * so one field will have htotal more pixels. In order to avoid
9587e78f1cbSVille Syrjälä 		 * the reported position from jumping backwards when the pixel
9597e78f1cbSVille Syrjälä 		 * counter is beyond the length of the shorter field, just
9607e78f1cbSVille Syrjälä 		 * clamp the position the length of the shorter field. This
9617e78f1cbSVille Syrjälä 		 * matches how the scanline counter based position works since
9627e78f1cbSVille Syrjälä 		 * the scanline counter doesn't count the two half lines.
9637e78f1cbSVille Syrjälä 		 */
9647e78f1cbSVille Syrjälä 		if (position >= vtotal)
9657e78f1cbSVille Syrjälä 			position = vtotal - 1;
9667e78f1cbSVille Syrjälä 
9677e78f1cbSVille Syrjälä 		/*
96878e8fc6bSVille Syrjälä 		 * Start of vblank interrupt is triggered at start of hsync,
96978e8fc6bSVille Syrjälä 		 * just prior to the first active line of vblank. However we
97078e8fc6bSVille Syrjälä 		 * consider lines to start at the leading edge of horizontal
97178e8fc6bSVille Syrjälä 		 * active. So, should we get here before we've crossed into
97278e8fc6bSVille Syrjälä 		 * the horizontal active of the first line in vblank, we would
97378e8fc6bSVille Syrjälä 		 * not set the DRM_SCANOUTPOS_INVBL flag. In order to fix that,
97478e8fc6bSVille Syrjälä 		 * always add htotal-hsync_start to the current pixel position.
97578e8fc6bSVille Syrjälä 		 */
97678e8fc6bSVille Syrjälä 		position = (position + htotal - hsync_start) % vtotal;
9773aa18df8SVille Syrjälä 	}
9783aa18df8SVille Syrjälä 
979ad3543edSMario Kleiner 	/* Get optional system timestamp after query. */
980ad3543edSMario Kleiner 	if (etime)
981ad3543edSMario Kleiner 		*etime = ktime_get();
982ad3543edSMario Kleiner 
983ad3543edSMario Kleiner 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
984ad3543edSMario Kleiner 
985ad3543edSMario Kleiner 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
986ad3543edSMario Kleiner 
9873aa18df8SVille Syrjälä 	/*
9883aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
9893aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
9903aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
9913aa18df8SVille Syrjälä 	 * up since vbl_end.
9923aa18df8SVille Syrjälä 	 */
9933aa18df8SVille Syrjälä 	if (position >= vbl_start)
9943aa18df8SVille Syrjälä 		position -= vbl_end;
9953aa18df8SVille Syrjälä 	else
9963aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
9973aa18df8SVille Syrjälä 
9988a920e24SVille Syrjälä 	if (use_scanline_counter) {
9993aa18df8SVille Syrjälä 		*vpos = position;
10003aa18df8SVille Syrjälä 		*hpos = 0;
10013aa18df8SVille Syrjälä 	} else {
10020af7e4dfSMario Kleiner 		*vpos = position / htotal;
10030af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
10040af7e4dfSMario Kleiner 	}
10050af7e4dfSMario Kleiner 
10061bf6ad62SDaniel Vetter 	return true;
10070af7e4dfSMario Kleiner }
10080af7e4dfSMario Kleiner 
10094bbffbf3SThomas Zimmermann bool intel_crtc_get_vblank_timestamp(struct drm_crtc *crtc, int *max_error,
10104bbffbf3SThomas Zimmermann 				     ktime_t *vblank_time, bool in_vblank_irq)
10114bbffbf3SThomas Zimmermann {
10124bbffbf3SThomas Zimmermann 	return drm_crtc_vblank_helper_get_vblank_timestamp_internal(
10134bbffbf3SThomas Zimmermann 		crtc, max_error, vblank_time, in_vblank_irq,
101448e67807SThomas Zimmermann 		i915_get_crtc_scanoutpos);
10154bbffbf3SThomas Zimmermann }
10164bbffbf3SThomas Zimmermann 
1017a225f079SVille Syrjälä int intel_get_crtc_scanline(struct intel_crtc *crtc)
1018a225f079SVille Syrjälä {
1019fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1020a225f079SVille Syrjälä 	unsigned long irqflags;
1021a225f079SVille Syrjälä 	int position;
1022a225f079SVille Syrjälä 
1023a225f079SVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
1024a225f079SVille Syrjälä 	position = __intel_get_crtc_scanline(crtc);
1025a225f079SVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
1026a225f079SVille Syrjälä 
1027a225f079SVille Syrjälä 	return position;
1028a225f079SVille Syrjälä }
1029a225f079SVille Syrjälä 
1030e3689190SBen Widawsky /**
103174bb98baSLucas De Marchi  * ivb_parity_work - Workqueue called when a parity error interrupt
1032e3689190SBen Widawsky  * occurred.
1033e3689190SBen Widawsky  * @work: workqueue struct
1034e3689190SBen Widawsky  *
1035e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
1036e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
1037e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
1038e3689190SBen Widawsky  */
103974bb98baSLucas De Marchi static void ivb_parity_work(struct work_struct *work)
1040e3689190SBen Widawsky {
10412d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
1042cefcff8fSJoonas Lahtinen 		container_of(work, typeof(*dev_priv), l3_parity.error_work);
1043cf1c97dcSAndi Shyti 	struct intel_gt *gt = &dev_priv->gt;
1044e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
104535a85ac6SBen Widawsky 	char *parity_event[6];
1046a9c287c9SJani Nikula 	u32 misccpctl;
1047a9c287c9SJani Nikula 	u8 slice = 0;
1048e3689190SBen Widawsky 
1049e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
1050e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
1051e3689190SBen Widawsky 	 * any time we access those registers.
1052e3689190SBen Widawsky 	 */
105391c8a326SChris Wilson 	mutex_lock(&dev_priv->drm.struct_mutex);
1054e3689190SBen Widawsky 
105535a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
105648a1b8d4SPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, !dev_priv->l3_parity.which_slice))
105735a85ac6SBen Widawsky 		goto out;
105835a85ac6SBen Widawsky 
10592939eb06SJani Nikula 	misccpctl = intel_uncore_read(&dev_priv->uncore, GEN7_MISCCPCTL);
10602939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
10612939eb06SJani Nikula 	intel_uncore_posting_read(&dev_priv->uncore, GEN7_MISCCPCTL);
1062e3689190SBen Widawsky 
106335a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
1064f0f59a00SVille Syrjälä 		i915_reg_t reg;
106535a85ac6SBen Widawsky 
106635a85ac6SBen Widawsky 		slice--;
106748a1b8d4SPankaj Bharadiya 		if (drm_WARN_ON_ONCE(&dev_priv->drm,
106848a1b8d4SPankaj Bharadiya 				     slice >= NUM_L3_SLICES(dev_priv)))
106935a85ac6SBen Widawsky 			break;
107035a85ac6SBen Widawsky 
107135a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
107235a85ac6SBen Widawsky 
10736fa1c5f1SVille Syrjälä 		reg = GEN7_L3CDERRST1(slice);
107435a85ac6SBen Widawsky 
10752939eb06SJani Nikula 		error_status = intel_uncore_read(&dev_priv->uncore, reg);
1076e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
1077e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
1078e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1079e3689190SBen Widawsky 
10802939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
10812939eb06SJani Nikula 		intel_uncore_posting_read(&dev_priv->uncore, reg);
1082e3689190SBen Widawsky 
1083cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1084e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1085e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1086e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
108735a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
108835a85ac6SBen Widawsky 		parity_event[5] = NULL;
1089e3689190SBen Widawsky 
109091c8a326SChris Wilson 		kobject_uevent_env(&dev_priv->drm.primary->kdev->kobj,
1091e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
1092e3689190SBen Widawsky 
109335a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
109435a85ac6SBen Widawsky 			  slice, row, bank, subbank);
1095e3689190SBen Widawsky 
109635a85ac6SBen Widawsky 		kfree(parity_event[4]);
1097e3689190SBen Widawsky 		kfree(parity_event[3]);
1098e3689190SBen Widawsky 		kfree(parity_event[2]);
1099e3689190SBen Widawsky 		kfree(parity_event[1]);
1100e3689190SBen Widawsky 	}
1101e3689190SBen Widawsky 
11022939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, GEN7_MISCCPCTL, misccpctl);
110335a85ac6SBen Widawsky 
110435a85ac6SBen Widawsky out:
110548a1b8d4SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, dev_priv->l3_parity.which_slice);
1106cf1c97dcSAndi Shyti 	spin_lock_irq(&gt->irq_lock);
1107cf1c97dcSAndi Shyti 	gen5_gt_enable_irq(gt, GT_PARITY_ERROR(dev_priv));
1108cf1c97dcSAndi Shyti 	spin_unlock_irq(&gt->irq_lock);
110935a85ac6SBen Widawsky 
111091c8a326SChris Wilson 	mutex_unlock(&dev_priv->drm.struct_mutex);
111135a85ac6SBen Widawsky }
111235a85ac6SBen Widawsky 
1113af92058fSVille Syrjälä static bool gen11_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
1114121e758eSDhinakaran Pandiyan {
1115af92058fSVille Syrjälä 	switch (pin) {
1116da51e4baSVille Syrjälä 	case HPD_PORT_TC1:
1117da51e4baSVille Syrjälä 	case HPD_PORT_TC2:
1118da51e4baSVille Syrjälä 	case HPD_PORT_TC3:
1119da51e4baSVille Syrjälä 	case HPD_PORT_TC4:
1120da51e4baSVille Syrjälä 	case HPD_PORT_TC5:
1121da51e4baSVille Syrjälä 	case HPD_PORT_TC6:
11224294fa5fSVille Syrjälä 		return val & GEN11_HOTPLUG_CTL_LONG_DETECT(pin);
112348ef15d3SJosé Roberto de Souza 	default:
112448ef15d3SJosé Roberto de Souza 		return false;
112548ef15d3SJosé Roberto de Souza 	}
112648ef15d3SJosé Roberto de Souza }
112748ef15d3SJosé Roberto de Souza 
1128af92058fSVille Syrjälä static bool bxt_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
112963c88d22SImre Deak {
1130af92058fSVille Syrjälä 	switch (pin) {
1131af92058fSVille Syrjälä 	case HPD_PORT_A:
1132195baa06SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
1133af92058fSVille Syrjälä 	case HPD_PORT_B:
113463c88d22SImre Deak 		return val & PORTB_HOTPLUG_LONG_DETECT;
1135af92058fSVille Syrjälä 	case HPD_PORT_C:
113663c88d22SImre Deak 		return val & PORTC_HOTPLUG_LONG_DETECT;
113763c88d22SImre Deak 	default:
113863c88d22SImre Deak 		return false;
113963c88d22SImre Deak 	}
114063c88d22SImre Deak }
114163c88d22SImre Deak 
1142af92058fSVille Syrjälä static bool icp_ddi_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
114331604222SAnusha Srivatsa {
1144af92058fSVille Syrjälä 	switch (pin) {
1145af92058fSVille Syrjälä 	case HPD_PORT_A:
1146af92058fSVille Syrjälä 	case HPD_PORT_B:
11478ef7e340SMatt Roper 	case HPD_PORT_C:
1148229f31e2SLucas De Marchi 	case HPD_PORT_D:
11494294fa5fSVille Syrjälä 		return val & SHOTPLUG_CTL_DDI_HPD_LONG_DETECT(pin);
115031604222SAnusha Srivatsa 	default:
115131604222SAnusha Srivatsa 		return false;
115231604222SAnusha Srivatsa 	}
115331604222SAnusha Srivatsa }
115431604222SAnusha Srivatsa 
1155af92058fSVille Syrjälä static bool icp_tc_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
115631604222SAnusha Srivatsa {
1157af92058fSVille Syrjälä 	switch (pin) {
1158da51e4baSVille Syrjälä 	case HPD_PORT_TC1:
1159da51e4baSVille Syrjälä 	case HPD_PORT_TC2:
1160da51e4baSVille Syrjälä 	case HPD_PORT_TC3:
1161da51e4baSVille Syrjälä 	case HPD_PORT_TC4:
1162da51e4baSVille Syrjälä 	case HPD_PORT_TC5:
1163da51e4baSVille Syrjälä 	case HPD_PORT_TC6:
11644294fa5fSVille Syrjälä 		return val & ICP_TC_HPD_LONG_DETECT(pin);
116552dfdba0SLucas De Marchi 	default:
116652dfdba0SLucas De Marchi 		return false;
116752dfdba0SLucas De Marchi 	}
116852dfdba0SLucas De Marchi }
116952dfdba0SLucas De Marchi 
1170af92058fSVille Syrjälä static bool spt_port_hotplug2_long_detect(enum hpd_pin pin, u32 val)
11716dbf30ceSVille Syrjälä {
1172af92058fSVille Syrjälä 	switch (pin) {
1173af92058fSVille Syrjälä 	case HPD_PORT_E:
11746dbf30ceSVille Syrjälä 		return val & PORTE_HOTPLUG_LONG_DETECT;
11756dbf30ceSVille Syrjälä 	default:
11766dbf30ceSVille Syrjälä 		return false;
11776dbf30ceSVille Syrjälä 	}
11786dbf30ceSVille Syrjälä }
11796dbf30ceSVille Syrjälä 
1180af92058fSVille Syrjälä static bool spt_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
118174c0b395SVille Syrjälä {
1182af92058fSVille Syrjälä 	switch (pin) {
1183af92058fSVille Syrjälä 	case HPD_PORT_A:
118474c0b395SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
1185af92058fSVille Syrjälä 	case HPD_PORT_B:
118674c0b395SVille Syrjälä 		return val & PORTB_HOTPLUG_LONG_DETECT;
1187af92058fSVille Syrjälä 	case HPD_PORT_C:
118874c0b395SVille Syrjälä 		return val & PORTC_HOTPLUG_LONG_DETECT;
1189af92058fSVille Syrjälä 	case HPD_PORT_D:
119074c0b395SVille Syrjälä 		return val & PORTD_HOTPLUG_LONG_DETECT;
119174c0b395SVille Syrjälä 	default:
119274c0b395SVille Syrjälä 		return false;
119374c0b395SVille Syrjälä 	}
119474c0b395SVille Syrjälä }
119574c0b395SVille Syrjälä 
1196af92058fSVille Syrjälä static bool ilk_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
1197e4ce95aaSVille Syrjälä {
1198af92058fSVille Syrjälä 	switch (pin) {
1199af92058fSVille Syrjälä 	case HPD_PORT_A:
1200e4ce95aaSVille Syrjälä 		return val & DIGITAL_PORTA_HOTPLUG_LONG_DETECT;
1201e4ce95aaSVille Syrjälä 	default:
1202e4ce95aaSVille Syrjälä 		return false;
1203e4ce95aaSVille Syrjälä 	}
1204e4ce95aaSVille Syrjälä }
1205e4ce95aaSVille Syrjälä 
1206af92058fSVille Syrjälä static bool pch_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
120713cf5504SDave Airlie {
1208af92058fSVille Syrjälä 	switch (pin) {
1209af92058fSVille Syrjälä 	case HPD_PORT_B:
1210676574dfSJani Nikula 		return val & PORTB_HOTPLUG_LONG_DETECT;
1211af92058fSVille Syrjälä 	case HPD_PORT_C:
1212676574dfSJani Nikula 		return val & PORTC_HOTPLUG_LONG_DETECT;
1213af92058fSVille Syrjälä 	case HPD_PORT_D:
1214676574dfSJani Nikula 		return val & PORTD_HOTPLUG_LONG_DETECT;
1215676574dfSJani Nikula 	default:
1216676574dfSJani Nikula 		return false;
121713cf5504SDave Airlie 	}
121813cf5504SDave Airlie }
121913cf5504SDave Airlie 
1220af92058fSVille Syrjälä static bool i9xx_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
122113cf5504SDave Airlie {
1222af92058fSVille Syrjälä 	switch (pin) {
1223af92058fSVille Syrjälä 	case HPD_PORT_B:
1224676574dfSJani Nikula 		return val & PORTB_HOTPLUG_INT_LONG_PULSE;
1225af92058fSVille Syrjälä 	case HPD_PORT_C:
1226676574dfSJani Nikula 		return val & PORTC_HOTPLUG_INT_LONG_PULSE;
1227af92058fSVille Syrjälä 	case HPD_PORT_D:
1228676574dfSJani Nikula 		return val & PORTD_HOTPLUG_INT_LONG_PULSE;
1229676574dfSJani Nikula 	default:
1230676574dfSJani Nikula 		return false;
123113cf5504SDave Airlie 	}
123213cf5504SDave Airlie }
123313cf5504SDave Airlie 
123442db67d6SVille Syrjälä /*
123542db67d6SVille Syrjälä  * Get a bit mask of pins that have triggered, and which ones may be long.
123642db67d6SVille Syrjälä  * This can be called multiple times with the same masks to accumulate
123742db67d6SVille Syrjälä  * hotplug detection results from several registers.
123842db67d6SVille Syrjälä  *
123942db67d6SVille Syrjälä  * Note that the caller is expected to zero out the masks initially.
124042db67d6SVille Syrjälä  */
1241cf53902fSRodrigo Vivi static void intel_get_hpd_pins(struct drm_i915_private *dev_priv,
1242cf53902fSRodrigo Vivi 			       u32 *pin_mask, u32 *long_mask,
12438c841e57SJani Nikula 			       u32 hotplug_trigger, u32 dig_hotplug_reg,
1244fd63e2a9SImre Deak 			       const u32 hpd[HPD_NUM_PINS],
1245af92058fSVille Syrjälä 			       bool long_pulse_detect(enum hpd_pin pin, u32 val))
1246676574dfSJani Nikula {
1247e9be2850SVille Syrjälä 	enum hpd_pin pin;
1248676574dfSJani Nikula 
124952dfdba0SLucas De Marchi 	BUILD_BUG_ON(BITS_PER_TYPE(*pin_mask) < HPD_NUM_PINS);
125052dfdba0SLucas De Marchi 
1251e9be2850SVille Syrjälä 	for_each_hpd_pin(pin) {
1252e9be2850SVille Syrjälä 		if ((hpd[pin] & hotplug_trigger) == 0)
12538c841e57SJani Nikula 			continue;
12548c841e57SJani Nikula 
1255e9be2850SVille Syrjälä 		*pin_mask |= BIT(pin);
1256676574dfSJani Nikula 
1257af92058fSVille Syrjälä 		if (long_pulse_detect(pin, dig_hotplug_reg))
1258e9be2850SVille Syrjälä 			*long_mask |= BIT(pin);
1259676574dfSJani Nikula 	}
1260676574dfSJani Nikula 
126100376ccfSWambui Karuga 	drm_dbg(&dev_priv->drm,
126200376ccfSWambui Karuga 		"hotplug event received, stat 0x%08x, dig 0x%08x, pins 0x%08x, long 0x%08x\n",
1263f88f0478SVille Syrjälä 		hotplug_trigger, dig_hotplug_reg, *pin_mask, *long_mask);
1264676574dfSJani Nikula 
1265676574dfSJani Nikula }
1266676574dfSJani Nikula 
1267a0e066b8SVille Syrjälä static u32 intel_hpd_enabled_irqs(struct drm_i915_private *dev_priv,
1268a0e066b8SVille Syrjälä 				  const u32 hpd[HPD_NUM_PINS])
1269a0e066b8SVille Syrjälä {
1270a0e066b8SVille Syrjälä 	struct intel_encoder *encoder;
1271a0e066b8SVille Syrjälä 	u32 enabled_irqs = 0;
1272a0e066b8SVille Syrjälä 
1273a0e066b8SVille Syrjälä 	for_each_intel_encoder(&dev_priv->drm, encoder)
1274a0e066b8SVille Syrjälä 		if (dev_priv->hotplug.stats[encoder->hpd_pin].state == HPD_ENABLED)
1275a0e066b8SVille Syrjälä 			enabled_irqs |= hpd[encoder->hpd_pin];
1276a0e066b8SVille Syrjälä 
1277a0e066b8SVille Syrjälä 	return enabled_irqs;
1278a0e066b8SVille Syrjälä }
1279a0e066b8SVille Syrjälä 
1280a0e066b8SVille Syrjälä static u32 intel_hpd_hotplug_irqs(struct drm_i915_private *dev_priv,
1281a0e066b8SVille Syrjälä 				  const u32 hpd[HPD_NUM_PINS])
1282a0e066b8SVille Syrjälä {
1283a0e066b8SVille Syrjälä 	struct intel_encoder *encoder;
1284a0e066b8SVille Syrjälä 	u32 hotplug_irqs = 0;
1285a0e066b8SVille Syrjälä 
1286a0e066b8SVille Syrjälä 	for_each_intel_encoder(&dev_priv->drm, encoder)
1287a0e066b8SVille Syrjälä 		hotplug_irqs |= hpd[encoder->hpd_pin];
1288a0e066b8SVille Syrjälä 
1289a0e066b8SVille Syrjälä 	return hotplug_irqs;
1290a0e066b8SVille Syrjälä }
1291a0e066b8SVille Syrjälä 
12922ea63927SVille Syrjälä static u32 intel_hpd_hotplug_enables(struct drm_i915_private *i915,
12932ea63927SVille Syrjälä 				     hotplug_enables_func hotplug_enables)
12942ea63927SVille Syrjälä {
12952ea63927SVille Syrjälä 	struct intel_encoder *encoder;
12962ea63927SVille Syrjälä 	u32 hotplug = 0;
12972ea63927SVille Syrjälä 
12982ea63927SVille Syrjälä 	for_each_intel_encoder(&i915->drm, encoder)
12992ea63927SVille Syrjälä 		hotplug |= hotplug_enables(i915, encoder->hpd_pin);
13002ea63927SVille Syrjälä 
13012ea63927SVille Syrjälä 	return hotplug;
13022ea63927SVille Syrjälä }
13032ea63927SVille Syrjälä 
130491d14251STvrtko Ursulin static void gmbus_irq_handler(struct drm_i915_private *dev_priv)
1305515ac2bbSDaniel Vetter {
130628c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1307515ac2bbSDaniel Vetter }
1308515ac2bbSDaniel Vetter 
130991d14251STvrtko Ursulin static void dp_aux_irq_handler(struct drm_i915_private *dev_priv)
1310ce99c256SDaniel Vetter {
13119ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1312ce99c256SDaniel Vetter }
1313ce99c256SDaniel Vetter 
13148bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
131591d14251STvrtko Ursulin static void display_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
131691d14251STvrtko Ursulin 					 enum pipe pipe,
1317a9c287c9SJani Nikula 					 u32 crc0, u32 crc1,
1318a9c287c9SJani Nikula 					 u32 crc2, u32 crc3,
1319a9c287c9SJani Nikula 					 u32 crc4)
13208bf1e9f1SShuang He {
1321*7794b6deSJani Nikula 	struct intel_crtc *crtc = intel_crtc_for_pipe(dev_priv, pipe);
132200535527SJani Nikula 	struct intel_pipe_crc *pipe_crc = &crtc->pipe_crc;
13235cee6c45SVille Syrjälä 	u32 crcs[5] = { crc0, crc1, crc2, crc3, crc4 };
13245cee6c45SVille Syrjälä 
13255cee6c45SVille Syrjälä 	trace_intel_pipe_crc(crtc, crcs);
1326b2c88f5bSDamien Lespiau 
1327d538bbdfSDamien Lespiau 	spin_lock(&pipe_crc->lock);
13288c6b709dSTomeu Vizoso 	/*
13298c6b709dSTomeu Vizoso 	 * For some not yet identified reason, the first CRC is
13308c6b709dSTomeu Vizoso 	 * bonkers. So let's just wait for the next vblank and read
13318c6b709dSTomeu Vizoso 	 * out the buggy result.
13328c6b709dSTomeu Vizoso 	 *
1333163e8aecSRodrigo Vivi 	 * On GEN8+ sometimes the second CRC is bonkers as well, so
13348c6b709dSTomeu Vizoso 	 * don't trust that one either.
13358c6b709dSTomeu Vizoso 	 */
1336033b7a23SMaarten Lankhorst 	if (pipe_crc->skipped <= 0 ||
1337373abf1aSMatt Roper 	    (DISPLAY_VER(dev_priv) >= 8 && pipe_crc->skipped == 1)) {
13388c6b709dSTomeu Vizoso 		pipe_crc->skipped++;
13398c6b709dSTomeu Vizoso 		spin_unlock(&pipe_crc->lock);
13408c6b709dSTomeu Vizoso 		return;
13418c6b709dSTomeu Vizoso 	}
13428c6b709dSTomeu Vizoso 	spin_unlock(&pipe_crc->lock);
13436cc42152SMaarten Lankhorst 
1344246ee524STomeu Vizoso 	drm_crtc_add_crc_entry(&crtc->base, true,
1345ca814b25SDaniel Vetter 				drm_crtc_accurate_vblank_count(&crtc->base),
1346246ee524STomeu Vizoso 				crcs);
13478c6b709dSTomeu Vizoso }
1348277de95eSDaniel Vetter #else
1349277de95eSDaniel Vetter static inline void
135091d14251STvrtko Ursulin display_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
135191d14251STvrtko Ursulin 			     enum pipe pipe,
1352a9c287c9SJani Nikula 			     u32 crc0, u32 crc1,
1353a9c287c9SJani Nikula 			     u32 crc2, u32 crc3,
1354a9c287c9SJani Nikula 			     u32 crc4) {}
1355277de95eSDaniel Vetter #endif
1356eba94eb9SDaniel Vetter 
13571288f9b0SKarthik B S static void flip_done_handler(struct drm_i915_private *i915,
13581288f9b0SKarthik B S 			      enum pipe pipe)
13591288f9b0SKarthik B S {
1360*7794b6deSJani Nikula 	struct intel_crtc *crtc = intel_crtc_for_pipe(i915, pipe);
13611288f9b0SKarthik B S 	struct drm_crtc_state *crtc_state = crtc->base.state;
13621288f9b0SKarthik B S 	struct drm_pending_vblank_event *e = crtc_state->event;
13631288f9b0SKarthik B S 	struct drm_device *dev = &i915->drm;
13641288f9b0SKarthik B S 	unsigned long irqflags;
13651288f9b0SKarthik B S 
13661288f9b0SKarthik B S 	spin_lock_irqsave(&dev->event_lock, irqflags);
13671288f9b0SKarthik B S 
13681288f9b0SKarthik B S 	crtc_state->event = NULL;
13691288f9b0SKarthik B S 
13701288f9b0SKarthik B S 	drm_crtc_send_vblank_event(&crtc->base, e);
13711288f9b0SKarthik B S 
13721288f9b0SKarthik B S 	spin_unlock_irqrestore(&dev->event_lock, irqflags);
13731288f9b0SKarthik B S }
1374277de95eSDaniel Vetter 
137591d14251STvrtko Ursulin static void hsw_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
137691d14251STvrtko Ursulin 				     enum pipe pipe)
13775a69b89fSDaniel Vetter {
137891d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
13792939eb06SJani Nikula 				     intel_uncore_read(&dev_priv->uncore, PIPE_CRC_RES_1_IVB(pipe)),
13805a69b89fSDaniel Vetter 				     0, 0, 0, 0);
13815a69b89fSDaniel Vetter }
13825a69b89fSDaniel Vetter 
138391d14251STvrtko Ursulin static void ivb_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
138491d14251STvrtko Ursulin 				     enum pipe pipe)
1385eba94eb9SDaniel Vetter {
138691d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
13872939eb06SJani Nikula 				     intel_uncore_read(&dev_priv->uncore, PIPE_CRC_RES_1_IVB(pipe)),
13882939eb06SJani Nikula 				     intel_uncore_read(&dev_priv->uncore, PIPE_CRC_RES_2_IVB(pipe)),
13892939eb06SJani Nikula 				     intel_uncore_read(&dev_priv->uncore, PIPE_CRC_RES_3_IVB(pipe)),
13902939eb06SJani Nikula 				     intel_uncore_read(&dev_priv->uncore, PIPE_CRC_RES_4_IVB(pipe)),
13912939eb06SJani Nikula 				     intel_uncore_read(&dev_priv->uncore, PIPE_CRC_RES_5_IVB(pipe)));
1392eba94eb9SDaniel Vetter }
13935b3a856bSDaniel Vetter 
139491d14251STvrtko Ursulin static void i9xx_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
139591d14251STvrtko Ursulin 				      enum pipe pipe)
13965b3a856bSDaniel Vetter {
1397a9c287c9SJani Nikula 	u32 res1, res2;
13980b5c5ed0SDaniel Vetter 
1399373abf1aSMatt Roper 	if (DISPLAY_VER(dev_priv) >= 3)
14002939eb06SJani Nikula 		res1 = intel_uncore_read(&dev_priv->uncore, PIPE_CRC_RES_RES1_I915(pipe));
14010b5c5ed0SDaniel Vetter 	else
14020b5c5ed0SDaniel Vetter 		res1 = 0;
14030b5c5ed0SDaniel Vetter 
1404373abf1aSMatt Roper 	if (DISPLAY_VER(dev_priv) >= 5 || IS_G4X(dev_priv))
14052939eb06SJani Nikula 		res2 = intel_uncore_read(&dev_priv->uncore, PIPE_CRC_RES_RES2_G4X(pipe));
14060b5c5ed0SDaniel Vetter 	else
14070b5c5ed0SDaniel Vetter 		res2 = 0;
14085b3a856bSDaniel Vetter 
140991d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
14102939eb06SJani Nikula 				     intel_uncore_read(&dev_priv->uncore, PIPE_CRC_RES_RED(pipe)),
14112939eb06SJani Nikula 				     intel_uncore_read(&dev_priv->uncore, PIPE_CRC_RES_GREEN(pipe)),
14122939eb06SJani Nikula 				     intel_uncore_read(&dev_priv->uncore, PIPE_CRC_RES_BLUE(pipe)),
14130b5c5ed0SDaniel Vetter 				     res1, res2);
14145b3a856bSDaniel Vetter }
14158bf1e9f1SShuang He 
141644d9241eSVille Syrjälä static void i9xx_pipestat_irq_reset(struct drm_i915_private *dev_priv)
141744d9241eSVille Syrjälä {
141844d9241eSVille Syrjälä 	enum pipe pipe;
141944d9241eSVille Syrjälä 
142044d9241eSVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
14212939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, PIPESTAT(pipe),
142244d9241eSVille Syrjälä 			   PIPESTAT_INT_STATUS_MASK |
142344d9241eSVille Syrjälä 			   PIPE_FIFO_UNDERRUN_STATUS);
142444d9241eSVille Syrjälä 
142544d9241eSVille Syrjälä 		dev_priv->pipestat_irq_mask[pipe] = 0;
142644d9241eSVille Syrjälä 	}
142744d9241eSVille Syrjälä }
142844d9241eSVille Syrjälä 
1429eb64343cSVille Syrjälä static void i9xx_pipestat_irq_ack(struct drm_i915_private *dev_priv,
143091d14251STvrtko Ursulin 				  u32 iir, u32 pipe_stats[I915_MAX_PIPES])
14317e231dbeSJesse Barnes {
1432d048a268SVille Syrjälä 	enum pipe pipe;
14337e231dbeSJesse Barnes 
143458ead0d7SImre Deak 	spin_lock(&dev_priv->irq_lock);
14351ca993d2SVille Syrjälä 
14361ca993d2SVille Syrjälä 	if (!dev_priv->display_irqs_enabled) {
14371ca993d2SVille Syrjälä 		spin_unlock(&dev_priv->irq_lock);
14381ca993d2SVille Syrjälä 		return;
14391ca993d2SVille Syrjälä 	}
14401ca993d2SVille Syrjälä 
1441055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1442f0f59a00SVille Syrjälä 		i915_reg_t reg;
14436b12ca56SVille Syrjälä 		u32 status_mask, enable_mask, iir_bit = 0;
144491d181ddSImre Deak 
1445bbb5eebfSDaniel Vetter 		/*
1446bbb5eebfSDaniel Vetter 		 * PIPESTAT bits get signalled even when the interrupt is
1447bbb5eebfSDaniel Vetter 		 * disabled with the mask bits, and some of the status bits do
1448bbb5eebfSDaniel Vetter 		 * not generate interrupts at all (like the underrun bit). Hence
1449bbb5eebfSDaniel Vetter 		 * we need to be careful that we only handle what we want to
1450bbb5eebfSDaniel Vetter 		 * handle.
1451bbb5eebfSDaniel Vetter 		 */
14520f239f4cSDaniel Vetter 
14530f239f4cSDaniel Vetter 		/* fifo underruns are filterered in the underrun handler. */
14546b12ca56SVille Syrjälä 		status_mask = PIPE_FIFO_UNDERRUN_STATUS;
1455bbb5eebfSDaniel Vetter 
1456bbb5eebfSDaniel Vetter 		switch (pipe) {
1457d048a268SVille Syrjälä 		default:
1458bbb5eebfSDaniel Vetter 		case PIPE_A:
1459bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
1460bbb5eebfSDaniel Vetter 			break;
1461bbb5eebfSDaniel Vetter 		case PIPE_B:
1462bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
1463bbb5eebfSDaniel Vetter 			break;
14643278f67fSVille Syrjälä 		case PIPE_C:
14653278f67fSVille Syrjälä 			iir_bit = I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
14663278f67fSVille Syrjälä 			break;
1467bbb5eebfSDaniel Vetter 		}
1468bbb5eebfSDaniel Vetter 		if (iir & iir_bit)
14696b12ca56SVille Syrjälä 			status_mask |= dev_priv->pipestat_irq_mask[pipe];
1470bbb5eebfSDaniel Vetter 
14716b12ca56SVille Syrjälä 		if (!status_mask)
147291d181ddSImre Deak 			continue;
147391d181ddSImre Deak 
147491d181ddSImre Deak 		reg = PIPESTAT(pipe);
14752939eb06SJani Nikula 		pipe_stats[pipe] = intel_uncore_read(&dev_priv->uncore, reg) & status_mask;
14766b12ca56SVille Syrjälä 		enable_mask = i915_pipestat_enable_mask(dev_priv, pipe);
14777e231dbeSJesse Barnes 
14787e231dbeSJesse Barnes 		/*
14797e231dbeSJesse Barnes 		 * Clear the PIPE*STAT regs before the IIR
1480132c27c9SVille Syrjälä 		 *
1481132c27c9SVille Syrjälä 		 * Toggle the enable bits to make sure we get an
1482132c27c9SVille Syrjälä 		 * edge in the ISR pipe event bit if we don't clear
1483132c27c9SVille Syrjälä 		 * all the enabled status bits. Otherwise the edge
1484132c27c9SVille Syrjälä 		 * triggered IIR on i965/g4x wouldn't notice that
1485132c27c9SVille Syrjälä 		 * an interrupt is still pending.
14867e231dbeSJesse Barnes 		 */
1487132c27c9SVille Syrjälä 		if (pipe_stats[pipe]) {
14882939eb06SJani Nikula 			intel_uncore_write(&dev_priv->uncore, reg, pipe_stats[pipe]);
14892939eb06SJani Nikula 			intel_uncore_write(&dev_priv->uncore, reg, enable_mask);
1490132c27c9SVille Syrjälä 		}
14917e231dbeSJesse Barnes 	}
149258ead0d7SImre Deak 	spin_unlock(&dev_priv->irq_lock);
14932ecb8ca4SVille Syrjälä }
14942ecb8ca4SVille Syrjälä 
1495eb64343cSVille Syrjälä static void i8xx_pipestat_irq_handler(struct drm_i915_private *dev_priv,
1496eb64343cSVille Syrjälä 				      u16 iir, u32 pipe_stats[I915_MAX_PIPES])
1497eb64343cSVille Syrjälä {
1498eb64343cSVille Syrjälä 	enum pipe pipe;
1499eb64343cSVille Syrjälä 
1500eb64343cSVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
1501eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
1502aca9310aSAnshuman Gupta 			intel_handle_vblank(dev_priv, pipe);
1503eb64343cSVille Syrjälä 
1504eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1505eb64343cSVille Syrjälä 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
1506eb64343cSVille Syrjälä 
1507eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
1508eb64343cSVille Syrjälä 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
1509eb64343cSVille Syrjälä 	}
1510eb64343cSVille Syrjälä }
1511eb64343cSVille Syrjälä 
1512eb64343cSVille Syrjälä static void i915_pipestat_irq_handler(struct drm_i915_private *dev_priv,
1513eb64343cSVille Syrjälä 				      u32 iir, u32 pipe_stats[I915_MAX_PIPES])
1514eb64343cSVille Syrjälä {
1515eb64343cSVille Syrjälä 	bool blc_event = false;
1516eb64343cSVille Syrjälä 	enum pipe pipe;
1517eb64343cSVille Syrjälä 
1518eb64343cSVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
1519eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
1520aca9310aSAnshuman Gupta 			intel_handle_vblank(dev_priv, pipe);
1521eb64343cSVille Syrjälä 
1522eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
1523eb64343cSVille Syrjälä 			blc_event = true;
1524eb64343cSVille Syrjälä 
1525eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1526eb64343cSVille Syrjälä 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
1527eb64343cSVille Syrjälä 
1528eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
1529eb64343cSVille Syrjälä 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
1530eb64343cSVille Syrjälä 	}
1531eb64343cSVille Syrjälä 
1532eb64343cSVille Syrjälä 	if (blc_event || (iir & I915_ASLE_INTERRUPT))
1533eb64343cSVille Syrjälä 		intel_opregion_asle_intr(dev_priv);
1534eb64343cSVille Syrjälä }
1535eb64343cSVille Syrjälä 
1536eb64343cSVille Syrjälä static void i965_pipestat_irq_handler(struct drm_i915_private *dev_priv,
1537eb64343cSVille Syrjälä 				      u32 iir, u32 pipe_stats[I915_MAX_PIPES])
1538eb64343cSVille Syrjälä {
1539eb64343cSVille Syrjälä 	bool blc_event = false;
1540eb64343cSVille Syrjälä 	enum pipe pipe;
1541eb64343cSVille Syrjälä 
1542eb64343cSVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
1543eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS)
1544aca9310aSAnshuman Gupta 			intel_handle_vblank(dev_priv, pipe);
1545eb64343cSVille Syrjälä 
1546eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
1547eb64343cSVille Syrjälä 			blc_event = true;
1548eb64343cSVille Syrjälä 
1549eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1550eb64343cSVille Syrjälä 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
1551eb64343cSVille Syrjälä 
1552eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
1553eb64343cSVille Syrjälä 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
1554eb64343cSVille Syrjälä 	}
1555eb64343cSVille Syrjälä 
1556eb64343cSVille Syrjälä 	if (blc_event || (iir & I915_ASLE_INTERRUPT))
1557eb64343cSVille Syrjälä 		intel_opregion_asle_intr(dev_priv);
1558eb64343cSVille Syrjälä 
1559eb64343cSVille Syrjälä 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1560eb64343cSVille Syrjälä 		gmbus_irq_handler(dev_priv);
1561eb64343cSVille Syrjälä }
1562eb64343cSVille Syrjälä 
156391d14251STvrtko Ursulin static void valleyview_pipestat_irq_handler(struct drm_i915_private *dev_priv,
15642ecb8ca4SVille Syrjälä 					    u32 pipe_stats[I915_MAX_PIPES])
15652ecb8ca4SVille Syrjälä {
15662ecb8ca4SVille Syrjälä 	enum pipe pipe;
15677e231dbeSJesse Barnes 
1568055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1569fd3a4024SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS)
1570aca9310aSAnshuman Gupta 			intel_handle_vblank(dev_priv, pipe);
15714356d586SDaniel Vetter 
15726ede6b06SVille Syrjälä 		if (pipe_stats[pipe] & PLANE_FLIP_DONE_INT_STATUS_VLV)
15736ede6b06SVille Syrjälä 			flip_done_handler(dev_priv, pipe);
15746ede6b06SVille Syrjälä 
15754356d586SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
157691d14251STvrtko Ursulin 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
15772d9d2b0bSVille Syrjälä 
15781f7247c0SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
15791f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
158031acc7f5SJesse Barnes 	}
158131acc7f5SJesse Barnes 
1582c1874ed7SImre Deak 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
158391d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
1584c1874ed7SImre Deak }
1585c1874ed7SImre Deak 
15861ae3c34cSVille Syrjälä static u32 i9xx_hpd_irq_ack(struct drm_i915_private *dev_priv)
158716c6c56bSVille Syrjälä {
15880ba7c51aSVille Syrjälä 	u32 hotplug_status = 0, hotplug_status_mask;
15890ba7c51aSVille Syrjälä 	int i;
159016c6c56bSVille Syrjälä 
15910ba7c51aSVille Syrjälä 	if (IS_G4X(dev_priv) ||
15920ba7c51aSVille Syrjälä 	    IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
15930ba7c51aSVille Syrjälä 		hotplug_status_mask = HOTPLUG_INT_STATUS_G4X |
15940ba7c51aSVille Syrjälä 			DP_AUX_CHANNEL_MASK_INT_STATUS_G4X;
15950ba7c51aSVille Syrjälä 	else
15960ba7c51aSVille Syrjälä 		hotplug_status_mask = HOTPLUG_INT_STATUS_I915;
15970ba7c51aSVille Syrjälä 
15980ba7c51aSVille Syrjälä 	/*
15990ba7c51aSVille Syrjälä 	 * We absolutely have to clear all the pending interrupt
16000ba7c51aSVille Syrjälä 	 * bits in PORT_HOTPLUG_STAT. Otherwise the ISR port
16010ba7c51aSVille Syrjälä 	 * interrupt bit won't have an edge, and the i965/g4x
16020ba7c51aSVille Syrjälä 	 * edge triggered IIR will not notice that an interrupt
16030ba7c51aSVille Syrjälä 	 * is still pending. We can't use PORT_HOTPLUG_EN to
16040ba7c51aSVille Syrjälä 	 * guarantee the edge as the act of toggling the enable
16050ba7c51aSVille Syrjälä 	 * bits can itself generate a new hotplug interrupt :(
16060ba7c51aSVille Syrjälä 	 */
16070ba7c51aSVille Syrjälä 	for (i = 0; i < 10; i++) {
16082939eb06SJani Nikula 		u32 tmp = intel_uncore_read(&dev_priv->uncore, PORT_HOTPLUG_STAT) & hotplug_status_mask;
16090ba7c51aSVille Syrjälä 
16100ba7c51aSVille Syrjälä 		if (tmp == 0)
16110ba7c51aSVille Syrjälä 			return hotplug_status;
16120ba7c51aSVille Syrjälä 
16130ba7c51aSVille Syrjälä 		hotplug_status |= tmp;
16142939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, PORT_HOTPLUG_STAT, hotplug_status);
16150ba7c51aSVille Syrjälä 	}
16160ba7c51aSVille Syrjälä 
161748a1b8d4SPankaj Bharadiya 	drm_WARN_ONCE(&dev_priv->drm, 1,
16180ba7c51aSVille Syrjälä 		      "PORT_HOTPLUG_STAT did not clear (0x%08x)\n",
16192939eb06SJani Nikula 		      intel_uncore_read(&dev_priv->uncore, PORT_HOTPLUG_STAT));
16201ae3c34cSVille Syrjälä 
16211ae3c34cSVille Syrjälä 	return hotplug_status;
16221ae3c34cSVille Syrjälä }
16231ae3c34cSVille Syrjälä 
162491d14251STvrtko Ursulin static void i9xx_hpd_irq_handler(struct drm_i915_private *dev_priv,
16251ae3c34cSVille Syrjälä 				 u32 hotplug_status)
16261ae3c34cSVille Syrjälä {
16271ae3c34cSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
16280398993bSVille Syrjälä 	u32 hotplug_trigger;
16293ff60f89SOscar Mateo 
16300398993bSVille Syrjälä 	if (IS_G4X(dev_priv) ||
16310398993bSVille Syrjälä 	    IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
16320398993bSVille Syrjälä 		hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
16330398993bSVille Syrjälä 	else
16340398993bSVille Syrjälä 		hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
163516c6c56bSVille Syrjälä 
163658f2cf24SVille Syrjälä 	if (hotplug_trigger) {
1637cf53902fSRodrigo Vivi 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
1638cf53902fSRodrigo Vivi 				   hotplug_trigger, hotplug_trigger,
16390398993bSVille Syrjälä 				   dev_priv->hotplug.hpd,
1640fd63e2a9SImre Deak 				   i9xx_port_hotplug_long_detect);
164158f2cf24SVille Syrjälä 
164291d14251STvrtko Ursulin 		intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
164358f2cf24SVille Syrjälä 	}
1644369712e8SJani Nikula 
16450398993bSVille Syrjälä 	if ((IS_G4X(dev_priv) ||
16460398993bSVille Syrjälä 	     IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
16470398993bSVille Syrjälä 	    hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
164891d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
164958f2cf24SVille Syrjälä }
165016c6c56bSVille Syrjälä 
1651c1874ed7SImre Deak static irqreturn_t valleyview_irq_handler(int irq, void *arg)
1652c1874ed7SImre Deak {
1653b318b824SVille Syrjälä 	struct drm_i915_private *dev_priv = arg;
1654c1874ed7SImre Deak 	irqreturn_t ret = IRQ_NONE;
1655c1874ed7SImre Deak 
16562dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
16572dd2a883SImre Deak 		return IRQ_NONE;
16582dd2a883SImre Deak 
16591f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
16609102650fSDaniele Ceraolo Spurio 	disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
16611f814dacSImre Deak 
16621e1cace9SVille Syrjälä 	do {
16636e814800SVille Syrjälä 		u32 iir, gt_iir, pm_iir;
16642ecb8ca4SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
16651ae3c34cSVille Syrjälä 		u32 hotplug_status = 0;
1666a5e485a9SVille Syrjälä 		u32 ier = 0;
16673ff60f89SOscar Mateo 
16682939eb06SJani Nikula 		gt_iir = intel_uncore_read(&dev_priv->uncore, GTIIR);
16692939eb06SJani Nikula 		pm_iir = intel_uncore_read(&dev_priv->uncore, GEN6_PMIIR);
16702939eb06SJani Nikula 		iir = intel_uncore_read(&dev_priv->uncore, VLV_IIR);
1671c1874ed7SImre Deak 
1672c1874ed7SImre Deak 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
16731e1cace9SVille Syrjälä 			break;
1674c1874ed7SImre Deak 
1675c1874ed7SImre Deak 		ret = IRQ_HANDLED;
1676c1874ed7SImre Deak 
1677a5e485a9SVille Syrjälä 		/*
1678a5e485a9SVille Syrjälä 		 * Theory on interrupt generation, based on empirical evidence:
1679a5e485a9SVille Syrjälä 		 *
1680a5e485a9SVille Syrjälä 		 * x = ((VLV_IIR & VLV_IER) ||
1681a5e485a9SVille Syrjälä 		 *      (((GT_IIR & GT_IER) || (GEN6_PMIIR & GEN6_PMIER)) &&
1682a5e485a9SVille Syrjälä 		 *       (VLV_MASTER_IER & MASTER_INTERRUPT_ENABLE)));
1683a5e485a9SVille Syrjälä 		 *
1684a5e485a9SVille Syrjälä 		 * A CPU interrupt will only be raised when 'x' has a 0->1 edge.
1685a5e485a9SVille Syrjälä 		 * Hence we clear MASTER_INTERRUPT_ENABLE and VLV_IER to
1686a5e485a9SVille Syrjälä 		 * guarantee the CPU interrupt will be raised again even if we
1687a5e485a9SVille Syrjälä 		 * don't end up clearing all the VLV_IIR, GT_IIR, GEN6_PMIIR
1688a5e485a9SVille Syrjälä 		 * bits this time around.
1689a5e485a9SVille Syrjälä 		 */
16902939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, VLV_MASTER_IER, 0);
16912939eb06SJani Nikula 		ier = intel_uncore_read(&dev_priv->uncore, VLV_IER);
16922939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, VLV_IER, 0);
16934a0a0202SVille Syrjälä 
16944a0a0202SVille Syrjälä 		if (gt_iir)
16952939eb06SJani Nikula 			intel_uncore_write(&dev_priv->uncore, GTIIR, gt_iir);
16964a0a0202SVille Syrjälä 		if (pm_iir)
16972939eb06SJani Nikula 			intel_uncore_write(&dev_priv->uncore, GEN6_PMIIR, pm_iir);
16984a0a0202SVille Syrjälä 
16997ce4d1f2SVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
17001ae3c34cSVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
17017ce4d1f2SVille Syrjälä 
17023ff60f89SOscar Mateo 		/* Call regardless, as some status bits might not be
17033ff60f89SOscar Mateo 		 * signalled in iir */
1704eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
17057ce4d1f2SVille Syrjälä 
1706eef57324SJerome Anand 		if (iir & (I915_LPE_PIPE_A_INTERRUPT |
1707eef57324SJerome Anand 			   I915_LPE_PIPE_B_INTERRUPT))
1708eef57324SJerome Anand 			intel_lpe_audio_irq_handler(dev_priv);
1709eef57324SJerome Anand 
17107ce4d1f2SVille Syrjälä 		/*
17117ce4d1f2SVille Syrjälä 		 * VLV_IIR is single buffered, and reflects the level
17127ce4d1f2SVille Syrjälä 		 * from PIPESTAT/PORT_HOTPLUG_STAT, hence clear it last.
17137ce4d1f2SVille Syrjälä 		 */
17147ce4d1f2SVille Syrjälä 		if (iir)
17152939eb06SJani Nikula 			intel_uncore_write(&dev_priv->uncore, VLV_IIR, iir);
17164a0a0202SVille Syrjälä 
17172939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, VLV_IER, ier);
17182939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
17191ae3c34cSVille Syrjälä 
172052894874SVille Syrjälä 		if (gt_iir)
1721cf1c97dcSAndi Shyti 			gen6_gt_irq_handler(&dev_priv->gt, gt_iir);
172252894874SVille Syrjälä 		if (pm_iir)
17233e7abf81SAndi Shyti 			gen6_rps_irq_handler(&dev_priv->gt.rps, pm_iir);
172452894874SVille Syrjälä 
17251ae3c34cSVille Syrjälä 		if (hotplug_status)
172691d14251STvrtko Ursulin 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
17272ecb8ca4SVille Syrjälä 
172891d14251STvrtko Ursulin 		valleyview_pipestat_irq_handler(dev_priv, pipe_stats);
17291e1cace9SVille Syrjälä 	} while (0);
17307e231dbeSJesse Barnes 
17319c6508b9SThomas Gleixner 	pmu_irq_stats(dev_priv, ret);
17329c6508b9SThomas Gleixner 
17339102650fSDaniele Ceraolo Spurio 	enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
17341f814dacSImre Deak 
17357e231dbeSJesse Barnes 	return ret;
17367e231dbeSJesse Barnes }
17377e231dbeSJesse Barnes 
173843f328d7SVille Syrjälä static irqreturn_t cherryview_irq_handler(int irq, void *arg)
173943f328d7SVille Syrjälä {
1740b318b824SVille Syrjälä 	struct drm_i915_private *dev_priv = arg;
174143f328d7SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
174243f328d7SVille Syrjälä 
17432dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
17442dd2a883SImre Deak 		return IRQ_NONE;
17452dd2a883SImre Deak 
17461f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
17479102650fSDaniele Ceraolo Spurio 	disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
17481f814dacSImre Deak 
1749579de73bSChris Wilson 	do {
17506e814800SVille Syrjälä 		u32 master_ctl, iir;
17512ecb8ca4SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
17521ae3c34cSVille Syrjälä 		u32 hotplug_status = 0;
1753a5e485a9SVille Syrjälä 		u32 ier = 0;
1754a5e485a9SVille Syrjälä 
17552939eb06SJani Nikula 		master_ctl = intel_uncore_read(&dev_priv->uncore, GEN8_MASTER_IRQ) & ~GEN8_MASTER_IRQ_CONTROL;
17562939eb06SJani Nikula 		iir = intel_uncore_read(&dev_priv->uncore, VLV_IIR);
17573278f67fSVille Syrjälä 
17583278f67fSVille Syrjälä 		if (master_ctl == 0 && iir == 0)
17598e5fd599SVille Syrjälä 			break;
176043f328d7SVille Syrjälä 
176127b6c122SOscar Mateo 		ret = IRQ_HANDLED;
176227b6c122SOscar Mateo 
1763a5e485a9SVille Syrjälä 		/*
1764a5e485a9SVille Syrjälä 		 * Theory on interrupt generation, based on empirical evidence:
1765a5e485a9SVille Syrjälä 		 *
1766a5e485a9SVille Syrjälä 		 * x = ((VLV_IIR & VLV_IER) ||
1767a5e485a9SVille Syrjälä 		 *      ((GEN8_MASTER_IRQ & ~GEN8_MASTER_IRQ_CONTROL) &&
1768a5e485a9SVille Syrjälä 		 *       (GEN8_MASTER_IRQ & GEN8_MASTER_IRQ_CONTROL)));
1769a5e485a9SVille Syrjälä 		 *
1770a5e485a9SVille Syrjälä 		 * A CPU interrupt will only be raised when 'x' has a 0->1 edge.
1771a5e485a9SVille Syrjälä 		 * Hence we clear GEN8_MASTER_IRQ_CONTROL and VLV_IER to
1772a5e485a9SVille Syrjälä 		 * guarantee the CPU interrupt will be raised again even if we
1773a5e485a9SVille Syrjälä 		 * don't end up clearing all the VLV_IIR and GEN8_MASTER_IRQ_CONTROL
1774a5e485a9SVille Syrjälä 		 * bits this time around.
1775a5e485a9SVille Syrjälä 		 */
17762939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, GEN8_MASTER_IRQ, 0);
17772939eb06SJani Nikula 		ier = intel_uncore_read(&dev_priv->uncore, VLV_IER);
17782939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, VLV_IER, 0);
177943f328d7SVille Syrjälä 
17806cc32f15SChris Wilson 		gen8_gt_irq_handler(&dev_priv->gt, master_ctl);
178127b6c122SOscar Mateo 
178227b6c122SOscar Mateo 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
17831ae3c34cSVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
178443f328d7SVille Syrjälä 
178527b6c122SOscar Mateo 		/* Call regardless, as some status bits might not be
178627b6c122SOscar Mateo 		 * signalled in iir */
1787eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
178843f328d7SVille Syrjälä 
1789eef57324SJerome Anand 		if (iir & (I915_LPE_PIPE_A_INTERRUPT |
1790eef57324SJerome Anand 			   I915_LPE_PIPE_B_INTERRUPT |
1791eef57324SJerome Anand 			   I915_LPE_PIPE_C_INTERRUPT))
1792eef57324SJerome Anand 			intel_lpe_audio_irq_handler(dev_priv);
1793eef57324SJerome Anand 
17947ce4d1f2SVille Syrjälä 		/*
17957ce4d1f2SVille Syrjälä 		 * VLV_IIR is single buffered, and reflects the level
17967ce4d1f2SVille Syrjälä 		 * from PIPESTAT/PORT_HOTPLUG_STAT, hence clear it last.
17977ce4d1f2SVille Syrjälä 		 */
17987ce4d1f2SVille Syrjälä 		if (iir)
17992939eb06SJani Nikula 			intel_uncore_write(&dev_priv->uncore, VLV_IIR, iir);
18007ce4d1f2SVille Syrjälä 
18012939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, VLV_IER, ier);
18022939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
18031ae3c34cSVille Syrjälä 
18041ae3c34cSVille Syrjälä 		if (hotplug_status)
180591d14251STvrtko Ursulin 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
18062ecb8ca4SVille Syrjälä 
180791d14251STvrtko Ursulin 		valleyview_pipestat_irq_handler(dev_priv, pipe_stats);
1808579de73bSChris Wilson 	} while (0);
18093278f67fSVille Syrjälä 
18109c6508b9SThomas Gleixner 	pmu_irq_stats(dev_priv, ret);
18119c6508b9SThomas Gleixner 
18129102650fSDaniele Ceraolo Spurio 	enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
18131f814dacSImre Deak 
181443f328d7SVille Syrjälä 	return ret;
181543f328d7SVille Syrjälä }
181643f328d7SVille Syrjälä 
181791d14251STvrtko Ursulin static void ibx_hpd_irq_handler(struct drm_i915_private *dev_priv,
18180398993bSVille Syrjälä 				u32 hotplug_trigger)
1819776ad806SJesse Barnes {
182042db67d6SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
1821776ad806SJesse Barnes 
18226a39d7c9SJani Nikula 	/*
18236a39d7c9SJani Nikula 	 * Somehow the PCH doesn't seem to really ack the interrupt to the CPU
18246a39d7c9SJani Nikula 	 * unless we touch the hotplug register, even if hotplug_trigger is
18256a39d7c9SJani Nikula 	 * zero. Not acking leads to "The master control interrupt lied (SDE)!"
18266a39d7c9SJani Nikula 	 * errors.
18276a39d7c9SJani Nikula 	 */
18282939eb06SJani Nikula 	dig_hotplug_reg = intel_uncore_read(&dev_priv->uncore, PCH_PORT_HOTPLUG);
18296a39d7c9SJani Nikula 	if (!hotplug_trigger) {
18306a39d7c9SJani Nikula 		u32 mask = PORTA_HOTPLUG_STATUS_MASK |
18316a39d7c9SJani Nikula 			PORTD_HOTPLUG_STATUS_MASK |
18326a39d7c9SJani Nikula 			PORTC_HOTPLUG_STATUS_MASK |
18336a39d7c9SJani Nikula 			PORTB_HOTPLUG_STATUS_MASK;
18346a39d7c9SJani Nikula 		dig_hotplug_reg &= ~mask;
18356a39d7c9SJani Nikula 	}
18366a39d7c9SJani Nikula 
18372939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, PCH_PORT_HOTPLUG, dig_hotplug_reg);
18386a39d7c9SJani Nikula 	if (!hotplug_trigger)
18396a39d7c9SJani Nikula 		return;
184013cf5504SDave Airlie 
18410398993bSVille Syrjälä 	intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
18420398993bSVille Syrjälä 			   hotplug_trigger, dig_hotplug_reg,
18430398993bSVille Syrjälä 			   dev_priv->hotplug.pch_hpd,
1844fd63e2a9SImre Deak 			   pch_port_hotplug_long_detect);
184540e56410SVille Syrjälä 
184691d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
1847aaf5ec2eSSonika Jindal }
184891d131d2SDaniel Vetter 
184991d14251STvrtko Ursulin static void ibx_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
185040e56410SVille Syrjälä {
1851d048a268SVille Syrjälä 	enum pipe pipe;
185240e56410SVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
185340e56410SVille Syrjälä 
18540398993bSVille Syrjälä 	ibx_hpd_irq_handler(dev_priv, hotplug_trigger);
185540e56410SVille Syrjälä 
1856cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1857cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1858776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
185900376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm, "PCH audio power change on port %d\n",
1860cfc33bf7SVille Syrjälä 			port_name(port));
1861cfc33bf7SVille Syrjälä 	}
1862776ad806SJesse Barnes 
1863ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
186491d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
1865ce99c256SDaniel Vetter 
1866776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
186791d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
1868776ad806SJesse Barnes 
1869776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
187000376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm, "PCH HDCP audio interrupt\n");
1871776ad806SJesse Barnes 
1872776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
187300376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm, "PCH transcoder audio interrupt\n");
1874776ad806SJesse Barnes 
1875776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
187600376ccfSWambui Karuga 		drm_err(&dev_priv->drm, "PCH poison interrupt\n");
1877776ad806SJesse Barnes 
1878b8b65ccdSAnshuman Gupta 	if (pch_iir & SDE_FDI_MASK) {
1879055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
188000376ccfSWambui Karuga 			drm_dbg(&dev_priv->drm, "  pipe %c FDI IIR: 0x%08x\n",
18819db4a9c7SJesse Barnes 				pipe_name(pipe),
18822939eb06SJani Nikula 				intel_uncore_read(&dev_priv->uncore, FDI_RX_IIR(pipe)));
1883b8b65ccdSAnshuman Gupta 	}
1884776ad806SJesse Barnes 
1885776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
188600376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm, "PCH transcoder CRC done interrupt\n");
1887776ad806SJesse Barnes 
1888776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
188900376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm,
189000376ccfSWambui Karuga 			"PCH transcoder CRC error interrupt\n");
1891776ad806SJesse Barnes 
1892776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
1893a2196033SMatthias Kaehlcke 		intel_pch_fifo_underrun_irq_handler(dev_priv, PIPE_A);
18948664281bSPaulo Zanoni 
18958664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
1896a2196033SMatthias Kaehlcke 		intel_pch_fifo_underrun_irq_handler(dev_priv, PIPE_B);
18978664281bSPaulo Zanoni }
18988664281bSPaulo Zanoni 
189991d14251STvrtko Ursulin static void ivb_err_int_handler(struct drm_i915_private *dev_priv)
19008664281bSPaulo Zanoni {
19012939eb06SJani Nikula 	u32 err_int = intel_uncore_read(&dev_priv->uncore, GEN7_ERR_INT);
19025a69b89fSDaniel Vetter 	enum pipe pipe;
19038664281bSPaulo Zanoni 
1904de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
190500376ccfSWambui Karuga 		drm_err(&dev_priv->drm, "Poison interrupt\n");
1906de032bf4SPaulo Zanoni 
1907055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
19081f7247c0SDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe))
19091f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
19108664281bSPaulo Zanoni 
19115a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
191291d14251STvrtko Ursulin 			if (IS_IVYBRIDGE(dev_priv))
191391d14251STvrtko Ursulin 				ivb_pipe_crc_irq_handler(dev_priv, pipe);
19145a69b89fSDaniel Vetter 			else
191591d14251STvrtko Ursulin 				hsw_pipe_crc_irq_handler(dev_priv, pipe);
19165a69b89fSDaniel Vetter 		}
19175a69b89fSDaniel Vetter 	}
19188bf1e9f1SShuang He 
19192939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, GEN7_ERR_INT, err_int);
19208664281bSPaulo Zanoni }
19218664281bSPaulo Zanoni 
192291d14251STvrtko Ursulin static void cpt_serr_int_handler(struct drm_i915_private *dev_priv)
19238664281bSPaulo Zanoni {
19242939eb06SJani Nikula 	u32 serr_int = intel_uncore_read(&dev_priv->uncore, SERR_INT);
192545c1cd87SMika Kahola 	enum pipe pipe;
19268664281bSPaulo Zanoni 
1927de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
192800376ccfSWambui Karuga 		drm_err(&dev_priv->drm, "PCH poison interrupt\n");
1929de032bf4SPaulo Zanoni 
193045c1cd87SMika Kahola 	for_each_pipe(dev_priv, pipe)
193145c1cd87SMika Kahola 		if (serr_int & SERR_INT_TRANS_FIFO_UNDERRUN(pipe))
193245c1cd87SMika Kahola 			intel_pch_fifo_underrun_irq_handler(dev_priv, pipe);
19338664281bSPaulo Zanoni 
19342939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, SERR_INT, serr_int);
1935776ad806SJesse Barnes }
1936776ad806SJesse Barnes 
193791d14251STvrtko Ursulin static void cpt_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
193823e81d69SAdam Jackson {
1939d048a268SVille Syrjälä 	enum pipe pipe;
19406dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
1941aaf5ec2eSSonika Jindal 
19420398993bSVille Syrjälä 	ibx_hpd_irq_handler(dev_priv, hotplug_trigger);
194391d131d2SDaniel Vetter 
1944cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
1945cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
194623e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
194700376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm, "PCH audio power change on port %c\n",
1948cfc33bf7SVille Syrjälä 			port_name(port));
1949cfc33bf7SVille Syrjälä 	}
195023e81d69SAdam Jackson 
195123e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
195291d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
195323e81d69SAdam Jackson 
195423e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
195591d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
195623e81d69SAdam Jackson 
195723e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
195800376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm, "Audio CP request interrupt\n");
195923e81d69SAdam Jackson 
196023e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
196100376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm, "Audio CP change interrupt\n");
196223e81d69SAdam Jackson 
1963b8b65ccdSAnshuman Gupta 	if (pch_iir & SDE_FDI_MASK_CPT) {
1964055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
196500376ccfSWambui Karuga 			drm_dbg(&dev_priv->drm, "  pipe %c FDI IIR: 0x%08x\n",
196623e81d69SAdam Jackson 				pipe_name(pipe),
19672939eb06SJani Nikula 				intel_uncore_read(&dev_priv->uncore, FDI_RX_IIR(pipe)));
1968b8b65ccdSAnshuman Gupta 	}
19698664281bSPaulo Zanoni 
19708664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
197191d14251STvrtko Ursulin 		cpt_serr_int_handler(dev_priv);
197223e81d69SAdam Jackson }
197323e81d69SAdam Jackson 
197458676af6SLucas De Marchi static void icp_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
197531604222SAnusha Srivatsa {
1976e76ab2cfSVille Syrjälä 	u32 ddi_hotplug_trigger = pch_iir & SDE_DDI_HOTPLUG_MASK_ICP;
1977e76ab2cfSVille Syrjälä 	u32 tc_hotplug_trigger = pch_iir & SDE_TC_HOTPLUG_MASK_ICP;
197831604222SAnusha Srivatsa 	u32 pin_mask = 0, long_mask = 0;
197931604222SAnusha Srivatsa 
198031604222SAnusha Srivatsa 	if (ddi_hotplug_trigger) {
198131604222SAnusha Srivatsa 		u32 dig_hotplug_reg;
198231604222SAnusha Srivatsa 
19832939eb06SJani Nikula 		dig_hotplug_reg = intel_uncore_read(&dev_priv->uncore, SHOTPLUG_CTL_DDI);
19842939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, SHOTPLUG_CTL_DDI, dig_hotplug_reg);
198531604222SAnusha Srivatsa 
198631604222SAnusha Srivatsa 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
19870398993bSVille Syrjälä 				   ddi_hotplug_trigger, dig_hotplug_reg,
19880398993bSVille Syrjälä 				   dev_priv->hotplug.pch_hpd,
198931604222SAnusha Srivatsa 				   icp_ddi_port_hotplug_long_detect);
199031604222SAnusha Srivatsa 	}
199131604222SAnusha Srivatsa 
199231604222SAnusha Srivatsa 	if (tc_hotplug_trigger) {
199331604222SAnusha Srivatsa 		u32 dig_hotplug_reg;
199431604222SAnusha Srivatsa 
19952939eb06SJani Nikula 		dig_hotplug_reg = intel_uncore_read(&dev_priv->uncore, SHOTPLUG_CTL_TC);
19962939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, SHOTPLUG_CTL_TC, dig_hotplug_reg);
199731604222SAnusha Srivatsa 
199831604222SAnusha Srivatsa 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
19990398993bSVille Syrjälä 				   tc_hotplug_trigger, dig_hotplug_reg,
20000398993bSVille Syrjälä 				   dev_priv->hotplug.pch_hpd,
2001da51e4baSVille Syrjälä 				   icp_tc_port_hotplug_long_detect);
200252dfdba0SLucas De Marchi 	}
200352dfdba0SLucas De Marchi 
200452dfdba0SLucas De Marchi 	if (pin_mask)
200552dfdba0SLucas De Marchi 		intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
200652dfdba0SLucas De Marchi 
200752dfdba0SLucas De Marchi 	if (pch_iir & SDE_GMBUS_ICP)
200852dfdba0SLucas De Marchi 		gmbus_irq_handler(dev_priv);
200952dfdba0SLucas De Marchi }
201052dfdba0SLucas De Marchi 
201191d14251STvrtko Ursulin static void spt_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
20126dbf30ceSVille Syrjälä {
20136dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_SPT &
20146dbf30ceSVille Syrjälä 		~SDE_PORTE_HOTPLUG_SPT;
20156dbf30ceSVille Syrjälä 	u32 hotplug2_trigger = pch_iir & SDE_PORTE_HOTPLUG_SPT;
20166dbf30ceSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
20176dbf30ceSVille Syrjälä 
20186dbf30ceSVille Syrjälä 	if (hotplug_trigger) {
20196dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
20206dbf30ceSVille Syrjälä 
20212939eb06SJani Nikula 		dig_hotplug_reg = intel_uncore_read(&dev_priv->uncore, PCH_PORT_HOTPLUG);
20222939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, PCH_PORT_HOTPLUG, dig_hotplug_reg);
20236dbf30ceSVille Syrjälä 
2024cf53902fSRodrigo Vivi 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
20250398993bSVille Syrjälä 				   hotplug_trigger, dig_hotplug_reg,
20260398993bSVille Syrjälä 				   dev_priv->hotplug.pch_hpd,
202774c0b395SVille Syrjälä 				   spt_port_hotplug_long_detect);
20286dbf30ceSVille Syrjälä 	}
20296dbf30ceSVille Syrjälä 
20306dbf30ceSVille Syrjälä 	if (hotplug2_trigger) {
20316dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
20326dbf30ceSVille Syrjälä 
20332939eb06SJani Nikula 		dig_hotplug_reg = intel_uncore_read(&dev_priv->uncore, PCH_PORT_HOTPLUG2);
20342939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, PCH_PORT_HOTPLUG2, dig_hotplug_reg);
20356dbf30ceSVille Syrjälä 
2036cf53902fSRodrigo Vivi 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
20370398993bSVille Syrjälä 				   hotplug2_trigger, dig_hotplug_reg,
20380398993bSVille Syrjälä 				   dev_priv->hotplug.pch_hpd,
20396dbf30ceSVille Syrjälä 				   spt_port_hotplug2_long_detect);
20406dbf30ceSVille Syrjälä 	}
20416dbf30ceSVille Syrjälä 
20426dbf30ceSVille Syrjälä 	if (pin_mask)
204391d14251STvrtko Ursulin 		intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
20446dbf30ceSVille Syrjälä 
20456dbf30ceSVille Syrjälä 	if (pch_iir & SDE_GMBUS_CPT)
204691d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
20476dbf30ceSVille Syrjälä }
20486dbf30ceSVille Syrjälä 
204991d14251STvrtko Ursulin static void ilk_hpd_irq_handler(struct drm_i915_private *dev_priv,
20500398993bSVille Syrjälä 				u32 hotplug_trigger)
2051c008bc6eSPaulo Zanoni {
2052e4ce95aaSVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2053e4ce95aaSVille Syrjälä 
20542939eb06SJani Nikula 	dig_hotplug_reg = intel_uncore_read(&dev_priv->uncore, DIGITAL_PORT_HOTPLUG_CNTRL);
20552939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, DIGITAL_PORT_HOTPLUG_CNTRL, dig_hotplug_reg);
2056e4ce95aaSVille Syrjälä 
20570398993bSVille Syrjälä 	intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
20580398993bSVille Syrjälä 			   hotplug_trigger, dig_hotplug_reg,
20590398993bSVille Syrjälä 			   dev_priv->hotplug.hpd,
2060e4ce95aaSVille Syrjälä 			   ilk_port_hotplug_long_detect);
206140e56410SVille Syrjälä 
206291d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2063e4ce95aaSVille Syrjälä }
2064c008bc6eSPaulo Zanoni 
206591d14251STvrtko Ursulin static void ilk_display_irq_handler(struct drm_i915_private *dev_priv,
206691d14251STvrtko Ursulin 				    u32 de_iir)
206740e56410SVille Syrjälä {
206840e56410SVille Syrjälä 	enum pipe pipe;
206940e56410SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG;
207040e56410SVille Syrjälä 
207140e56410SVille Syrjälä 	if (hotplug_trigger)
20720398993bSVille Syrjälä 		ilk_hpd_irq_handler(dev_priv, hotplug_trigger);
207340e56410SVille Syrjälä 
2074c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
207591d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
2076c008bc6eSPaulo Zanoni 
2077c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
207891d14251STvrtko Ursulin 		intel_opregion_asle_intr(dev_priv);
2079c008bc6eSPaulo Zanoni 
2080c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
208100376ccfSWambui Karuga 		drm_err(&dev_priv->drm, "Poison interrupt\n");
2082c008bc6eSPaulo Zanoni 
2083055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2084fd3a4024SDaniel Vetter 		if (de_iir & DE_PIPE_VBLANK(pipe))
2085aca9310aSAnshuman Gupta 			intel_handle_vblank(dev_priv, pipe);
2086c008bc6eSPaulo Zanoni 
20874bb18054SVille Syrjälä 		if (de_iir & DE_PLANE_FLIP_DONE(pipe))
20884bb18054SVille Syrjälä 			flip_done_handler(dev_priv, pipe);
20894bb18054SVille Syrjälä 
209040da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
20911f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
2092c008bc6eSPaulo Zanoni 
209340da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
209491d14251STvrtko Ursulin 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
2095c008bc6eSPaulo Zanoni 	}
2096c008bc6eSPaulo Zanoni 
2097c008bc6eSPaulo Zanoni 	/* check event from PCH */
2098c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
20992939eb06SJani Nikula 		u32 pch_iir = intel_uncore_read(&dev_priv->uncore, SDEIIR);
2100c008bc6eSPaulo Zanoni 
210191d14251STvrtko Ursulin 		if (HAS_PCH_CPT(dev_priv))
210291d14251STvrtko Ursulin 			cpt_irq_handler(dev_priv, pch_iir);
2103c008bc6eSPaulo Zanoni 		else
210491d14251STvrtko Ursulin 			ibx_irq_handler(dev_priv, pch_iir);
2105c008bc6eSPaulo Zanoni 
2106c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
21072939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, SDEIIR, pch_iir);
2108c008bc6eSPaulo Zanoni 	}
2109c008bc6eSPaulo Zanoni 
211093e7e61eSLucas De Marchi 	if (DISPLAY_VER(dev_priv) == 5 && de_iir & DE_PCU_EVENT)
21113e7abf81SAndi Shyti 		gen5_rps_irq_handler(&dev_priv->gt.rps);
2112c008bc6eSPaulo Zanoni }
2113c008bc6eSPaulo Zanoni 
211491d14251STvrtko Ursulin static void ivb_display_irq_handler(struct drm_i915_private *dev_priv,
211591d14251STvrtko Ursulin 				    u32 de_iir)
21169719fb98SPaulo Zanoni {
211707d27e20SDamien Lespiau 	enum pipe pipe;
211823bb4cb5SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG_IVB;
211923bb4cb5SVille Syrjälä 
212040e56410SVille Syrjälä 	if (hotplug_trigger)
21210398993bSVille Syrjälä 		ilk_hpd_irq_handler(dev_priv, hotplug_trigger);
21229719fb98SPaulo Zanoni 
21239719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
212491d14251STvrtko Ursulin 		ivb_err_int_handler(dev_priv);
21259719fb98SPaulo Zanoni 
21269719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
212791d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
21289719fb98SPaulo Zanoni 
21299719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
213091d14251STvrtko Ursulin 		intel_opregion_asle_intr(dev_priv);
21319719fb98SPaulo Zanoni 
2132055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
213333ef04faSVille Syrjälä 		if (de_iir & DE_PIPE_VBLANK_IVB(pipe))
2134aca9310aSAnshuman Gupta 			intel_handle_vblank(dev_priv, pipe);
21352a636e24SVille Syrjälä 
21362a636e24SVille Syrjälä 		if (de_iir & DE_PLANE_FLIP_DONE_IVB(pipe))
21372a636e24SVille Syrjälä 			flip_done_handler(dev_priv, pipe);
21389719fb98SPaulo Zanoni 	}
21399719fb98SPaulo Zanoni 
21409719fb98SPaulo Zanoni 	/* check event from PCH */
214191d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv) && (de_iir & DE_PCH_EVENT_IVB)) {
21422939eb06SJani Nikula 		u32 pch_iir = intel_uncore_read(&dev_priv->uncore, SDEIIR);
21439719fb98SPaulo Zanoni 
214491d14251STvrtko Ursulin 		cpt_irq_handler(dev_priv, pch_iir);
21459719fb98SPaulo Zanoni 
21469719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
21472939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, SDEIIR, pch_iir);
21489719fb98SPaulo Zanoni 	}
21499719fb98SPaulo Zanoni }
21509719fb98SPaulo Zanoni 
215172c90f62SOscar Mateo /*
215272c90f62SOscar Mateo  * To handle irqs with the minimum potential races with fresh interrupts, we:
215372c90f62SOscar Mateo  * 1 - Disable Master Interrupt Control.
215472c90f62SOscar Mateo  * 2 - Find the source(s) of the interrupt.
215572c90f62SOscar Mateo  * 3 - Clear the Interrupt Identity bits (IIR).
215672c90f62SOscar Mateo  * 4 - Process the interrupt(s) that had bits set in the IIRs.
215772c90f62SOscar Mateo  * 5 - Re-enable Master Interrupt Control.
215872c90f62SOscar Mateo  */
21599eae5e27SLucas De Marchi static irqreturn_t ilk_irq_handler(int irq, void *arg)
2160b1f14ad0SJesse Barnes {
2161c48a798aSChris Wilson 	struct drm_i915_private *i915 = arg;
2162c48a798aSChris Wilson 	void __iomem * const regs = i915->uncore.regs;
2163f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
21640e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
2165b1f14ad0SJesse Barnes 
2166c48a798aSChris Wilson 	if (unlikely(!intel_irqs_enabled(i915)))
21672dd2a883SImre Deak 		return IRQ_NONE;
21682dd2a883SImre Deak 
21691f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
2170c48a798aSChris Wilson 	disable_rpm_wakeref_asserts(&i915->runtime_pm);
21711f814dacSImre Deak 
2172b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
2173c48a798aSChris Wilson 	de_ier = raw_reg_read(regs, DEIER);
2174c48a798aSChris Wilson 	raw_reg_write(regs, DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
21750e43406bSChris Wilson 
217644498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
217744498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
217844498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
217944498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
218044498aeaSPaulo Zanoni 	 * due to its back queue). */
2181c48a798aSChris Wilson 	if (!HAS_PCH_NOP(i915)) {
2182c48a798aSChris Wilson 		sde_ier = raw_reg_read(regs, SDEIER);
2183c48a798aSChris Wilson 		raw_reg_write(regs, SDEIER, 0);
2184ab5c608bSBen Widawsky 	}
218544498aeaSPaulo Zanoni 
218672c90f62SOscar Mateo 	/* Find, clear, then process each source of interrupt */
218772c90f62SOscar Mateo 
2188c48a798aSChris Wilson 	gt_iir = raw_reg_read(regs, GTIIR);
21890e43406bSChris Wilson 	if (gt_iir) {
2190c48a798aSChris Wilson 		raw_reg_write(regs, GTIIR, gt_iir);
2191651e7d48SLucas De Marchi 		if (GRAPHICS_VER(i915) >= 6)
2192c48a798aSChris Wilson 			gen6_gt_irq_handler(&i915->gt, gt_iir);
2193d8fc8a47SPaulo Zanoni 		else
2194c48a798aSChris Wilson 			gen5_gt_irq_handler(&i915->gt, gt_iir);
2195c48a798aSChris Wilson 		ret = IRQ_HANDLED;
21960e43406bSChris Wilson 	}
2197b1f14ad0SJesse Barnes 
2198c48a798aSChris Wilson 	de_iir = raw_reg_read(regs, DEIIR);
21990e43406bSChris Wilson 	if (de_iir) {
2200c48a798aSChris Wilson 		raw_reg_write(regs, DEIIR, de_iir);
2201373abf1aSMatt Roper 		if (DISPLAY_VER(i915) >= 7)
2202c48a798aSChris Wilson 			ivb_display_irq_handler(i915, de_iir);
2203f1af8fc1SPaulo Zanoni 		else
2204c48a798aSChris Wilson 			ilk_display_irq_handler(i915, de_iir);
22050e43406bSChris Wilson 		ret = IRQ_HANDLED;
2206c48a798aSChris Wilson 	}
2207c48a798aSChris Wilson 
2208651e7d48SLucas De Marchi 	if (GRAPHICS_VER(i915) >= 6) {
2209c48a798aSChris Wilson 		u32 pm_iir = raw_reg_read(regs, GEN6_PMIIR);
2210c48a798aSChris Wilson 		if (pm_iir) {
2211c48a798aSChris Wilson 			raw_reg_write(regs, GEN6_PMIIR, pm_iir);
2212c48a798aSChris Wilson 			gen6_rps_irq_handler(&i915->gt.rps, pm_iir);
2213c48a798aSChris Wilson 			ret = IRQ_HANDLED;
22140e43406bSChris Wilson 		}
2215f1af8fc1SPaulo Zanoni 	}
2216b1f14ad0SJesse Barnes 
2217c48a798aSChris Wilson 	raw_reg_write(regs, DEIER, de_ier);
2218c48a798aSChris Wilson 	if (sde_ier)
2219c48a798aSChris Wilson 		raw_reg_write(regs, SDEIER, sde_ier);
2220b1f14ad0SJesse Barnes 
22219c6508b9SThomas Gleixner 	pmu_irq_stats(i915, ret);
22229c6508b9SThomas Gleixner 
22231f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
2224c48a798aSChris Wilson 	enable_rpm_wakeref_asserts(&i915->runtime_pm);
22251f814dacSImre Deak 
2226b1f14ad0SJesse Barnes 	return ret;
2227b1f14ad0SJesse Barnes }
2228b1f14ad0SJesse Barnes 
222991d14251STvrtko Ursulin static void bxt_hpd_irq_handler(struct drm_i915_private *dev_priv,
22300398993bSVille Syrjälä 				u32 hotplug_trigger)
2231d04a492dSShashank Sharma {
2232cebd87a0SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2233d04a492dSShashank Sharma 
22342939eb06SJani Nikula 	dig_hotplug_reg = intel_uncore_read(&dev_priv->uncore, PCH_PORT_HOTPLUG);
22352939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, PCH_PORT_HOTPLUG, dig_hotplug_reg);
2236d04a492dSShashank Sharma 
22370398993bSVille Syrjälä 	intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
22380398993bSVille Syrjälä 			   hotplug_trigger, dig_hotplug_reg,
22390398993bSVille Syrjälä 			   dev_priv->hotplug.hpd,
2240cebd87a0SVille Syrjälä 			   bxt_port_hotplug_long_detect);
224140e56410SVille Syrjälä 
224291d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2243d04a492dSShashank Sharma }
2244d04a492dSShashank Sharma 
2245121e758eSDhinakaran Pandiyan static void gen11_hpd_irq_handler(struct drm_i915_private *dev_priv, u32 iir)
2246121e758eSDhinakaran Pandiyan {
2247121e758eSDhinakaran Pandiyan 	u32 pin_mask = 0, long_mask = 0;
2248b796b971SDhinakaran Pandiyan 	u32 trigger_tc = iir & GEN11_DE_TC_HOTPLUG_MASK;
2249b796b971SDhinakaran Pandiyan 	u32 trigger_tbt = iir & GEN11_DE_TBT_HOTPLUG_MASK;
2250121e758eSDhinakaran Pandiyan 
2251121e758eSDhinakaran Pandiyan 	if (trigger_tc) {
2252b796b971SDhinakaran Pandiyan 		u32 dig_hotplug_reg;
2253b796b971SDhinakaran Pandiyan 
22542939eb06SJani Nikula 		dig_hotplug_reg = intel_uncore_read(&dev_priv->uncore, GEN11_TC_HOTPLUG_CTL);
22552939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, GEN11_TC_HOTPLUG_CTL, dig_hotplug_reg);
2256121e758eSDhinakaran Pandiyan 
22570398993bSVille Syrjälä 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
22580398993bSVille Syrjälä 				   trigger_tc, dig_hotplug_reg,
22590398993bSVille Syrjälä 				   dev_priv->hotplug.hpd,
2260da51e4baSVille Syrjälä 				   gen11_port_hotplug_long_detect);
2261121e758eSDhinakaran Pandiyan 	}
2262b796b971SDhinakaran Pandiyan 
2263b796b971SDhinakaran Pandiyan 	if (trigger_tbt) {
2264b796b971SDhinakaran Pandiyan 		u32 dig_hotplug_reg;
2265b796b971SDhinakaran Pandiyan 
22662939eb06SJani Nikula 		dig_hotplug_reg = intel_uncore_read(&dev_priv->uncore, GEN11_TBT_HOTPLUG_CTL);
22672939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, GEN11_TBT_HOTPLUG_CTL, dig_hotplug_reg);
2268b796b971SDhinakaran Pandiyan 
22690398993bSVille Syrjälä 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
22700398993bSVille Syrjälä 				   trigger_tbt, dig_hotplug_reg,
22710398993bSVille Syrjälä 				   dev_priv->hotplug.hpd,
2272da51e4baSVille Syrjälä 				   gen11_port_hotplug_long_detect);
2273b796b971SDhinakaran Pandiyan 	}
2274b796b971SDhinakaran Pandiyan 
2275b796b971SDhinakaran Pandiyan 	if (pin_mask)
2276b796b971SDhinakaran Pandiyan 		intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2277b796b971SDhinakaran Pandiyan 	else
227800376ccfSWambui Karuga 		drm_err(&dev_priv->drm,
227900376ccfSWambui Karuga 			"Unexpected DE HPD interrupt 0x%08x\n", iir);
2280121e758eSDhinakaran Pandiyan }
2281121e758eSDhinakaran Pandiyan 
22829d17210fSLucas De Marchi static u32 gen8_de_port_aux_mask(struct drm_i915_private *dev_priv)
22839d17210fSLucas De Marchi {
228455523360SLucas De Marchi 	u32 mask;
22859d17210fSLucas De Marchi 
228620fe778fSMatt Roper 	if (DISPLAY_VER(dev_priv) >= 13)
228720fe778fSMatt Roper 		return TGL_DE_PORT_AUX_DDIA |
228820fe778fSMatt Roper 			TGL_DE_PORT_AUX_DDIB |
228920fe778fSMatt Roper 			TGL_DE_PORT_AUX_DDIC |
229020fe778fSMatt Roper 			XELPD_DE_PORT_AUX_DDID |
229120fe778fSMatt Roper 			XELPD_DE_PORT_AUX_DDIE |
229220fe778fSMatt Roper 			TGL_DE_PORT_AUX_USBC1 |
229320fe778fSMatt Roper 			TGL_DE_PORT_AUX_USBC2 |
229420fe778fSMatt Roper 			TGL_DE_PORT_AUX_USBC3 |
229520fe778fSMatt Roper 			TGL_DE_PORT_AUX_USBC4;
229620fe778fSMatt Roper 	else if (DISPLAY_VER(dev_priv) >= 12)
229755523360SLucas De Marchi 		return TGL_DE_PORT_AUX_DDIA |
229855523360SLucas De Marchi 			TGL_DE_PORT_AUX_DDIB |
2299e5df52dcSMatt Roper 			TGL_DE_PORT_AUX_DDIC |
2300e5df52dcSMatt Roper 			TGL_DE_PORT_AUX_USBC1 |
2301e5df52dcSMatt Roper 			TGL_DE_PORT_AUX_USBC2 |
2302e5df52dcSMatt Roper 			TGL_DE_PORT_AUX_USBC3 |
2303e5df52dcSMatt Roper 			TGL_DE_PORT_AUX_USBC4 |
2304e5df52dcSMatt Roper 			TGL_DE_PORT_AUX_USBC5 |
2305e5df52dcSMatt Roper 			TGL_DE_PORT_AUX_USBC6;
2306e5df52dcSMatt Roper 
230755523360SLucas De Marchi 
230855523360SLucas De Marchi 	mask = GEN8_AUX_CHANNEL_A;
2309373abf1aSMatt Roper 	if (DISPLAY_VER(dev_priv) >= 9)
23109d17210fSLucas De Marchi 		mask |= GEN9_AUX_CHANNEL_B |
23119d17210fSLucas De Marchi 			GEN9_AUX_CHANNEL_C |
23129d17210fSLucas De Marchi 			GEN9_AUX_CHANNEL_D;
23139d17210fSLucas De Marchi 
2314938a8a9aSLucas De Marchi 	if (DISPLAY_VER(dev_priv) == 11) {
2315938a8a9aSLucas De Marchi 		mask |= ICL_AUX_CHANNEL_F;
231655523360SLucas De Marchi 		mask |= ICL_AUX_CHANNEL_E;
2317938a8a9aSLucas De Marchi 	}
23189d17210fSLucas De Marchi 
23199d17210fSLucas De Marchi 	return mask;
23209d17210fSLucas De Marchi }
23219d17210fSLucas De Marchi 
23225270130dSVille Syrjälä static u32 gen8_de_pipe_fault_mask(struct drm_i915_private *dev_priv)
23235270130dSVille Syrjälä {
23241649a4ccSMatt Roper 	if (DISPLAY_VER(dev_priv) >= 13 || HAS_D12_PLANE_MINIMIZATION(dev_priv))
232599e2d8bcSMatt Roper 		return RKL_DE_PIPE_IRQ_FAULT_ERRORS;
2326373abf1aSMatt Roper 	else if (DISPLAY_VER(dev_priv) >= 11)
2327d506a65dSMatt Roper 		return GEN11_DE_PIPE_IRQ_FAULT_ERRORS;
2328373abf1aSMatt Roper 	else if (DISPLAY_VER(dev_priv) >= 9)
23295270130dSVille Syrjälä 		return GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
23305270130dSVille Syrjälä 	else
23315270130dSVille Syrjälä 		return GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
23325270130dSVille Syrjälä }
23335270130dSVille Syrjälä 
233446c63d24SJosé Roberto de Souza static void
233546c63d24SJosé Roberto de Souza gen8_de_misc_irq_handler(struct drm_i915_private *dev_priv, u32 iir)
2336abd58f01SBen Widawsky {
2337e04f7eceSVille Syrjälä 	bool found = false;
2338e04f7eceSVille Syrjälä 
2339e04f7eceSVille Syrjälä 	if (iir & GEN8_DE_MISC_GSE) {
234091d14251STvrtko Ursulin 		intel_opregion_asle_intr(dev_priv);
2341e04f7eceSVille Syrjälä 		found = true;
2342e04f7eceSVille Syrjälä 	}
2343e04f7eceSVille Syrjälä 
2344e04f7eceSVille Syrjälä 	if (iir & GEN8_DE_EDP_PSR) {
2345b64d6c51SGwan-gyeong Mun 		struct intel_encoder *encoder;
23468241cfbeSJosé Roberto de Souza 		u32 psr_iir;
23478241cfbeSJosé Roberto de Souza 		i915_reg_t iir_reg;
23488241cfbeSJosé Roberto de Souza 
2349a22af61dSJosé Roberto de Souza 		for_each_intel_encoder_with_psr(&dev_priv->drm, encoder) {
2350b64d6c51SGwan-gyeong Mun 			struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2351b64d6c51SGwan-gyeong Mun 
2352373abf1aSMatt Roper 			if (DISPLAY_VER(dev_priv) >= 12)
2353b64d6c51SGwan-gyeong Mun 				iir_reg = TRANS_PSR_IIR(intel_dp->psr.transcoder);
23548241cfbeSJosé Roberto de Souza 			else
23558241cfbeSJosé Roberto de Souza 				iir_reg = EDP_PSR_IIR;
23568241cfbeSJosé Roberto de Souza 
23572939eb06SJani Nikula 			psr_iir = intel_uncore_read(&dev_priv->uncore, iir_reg);
23582939eb06SJani Nikula 			intel_uncore_write(&dev_priv->uncore, iir_reg, psr_iir);
23598241cfbeSJosé Roberto de Souza 
23608241cfbeSJosé Roberto de Souza 			if (psr_iir)
23618241cfbeSJosé Roberto de Souza 				found = true;
236254fd3149SDhinakaran Pandiyan 
2363b64d6c51SGwan-gyeong Mun 			intel_psr_irq_handler(intel_dp, psr_iir);
2364b64d6c51SGwan-gyeong Mun 
2365b64d6c51SGwan-gyeong Mun 			/* prior GEN12 only have one EDP PSR */
2366373abf1aSMatt Roper 			if (DISPLAY_VER(dev_priv) < 12)
2367b64d6c51SGwan-gyeong Mun 				break;
2368b64d6c51SGwan-gyeong Mun 		}
2369e04f7eceSVille Syrjälä 	}
2370e04f7eceSVille Syrjälä 
2371e04f7eceSVille Syrjälä 	if (!found)
237200376ccfSWambui Karuga 		drm_err(&dev_priv->drm, "Unexpected DE Misc interrupt\n");
2373abd58f01SBen Widawsky }
237446c63d24SJosé Roberto de Souza 
237500acb329SVandita Kulkarni static void gen11_dsi_te_interrupt_handler(struct drm_i915_private *dev_priv,
237600acb329SVandita Kulkarni 					   u32 te_trigger)
237700acb329SVandita Kulkarni {
237800acb329SVandita Kulkarni 	enum pipe pipe = INVALID_PIPE;
237900acb329SVandita Kulkarni 	enum transcoder dsi_trans;
238000acb329SVandita Kulkarni 	enum port port;
238100acb329SVandita Kulkarni 	u32 val, tmp;
238200acb329SVandita Kulkarni 
238300acb329SVandita Kulkarni 	/*
238400acb329SVandita Kulkarni 	 * Incase of dual link, TE comes from DSI_1
238500acb329SVandita Kulkarni 	 * this is to check if dual link is enabled
238600acb329SVandita Kulkarni 	 */
23872939eb06SJani Nikula 	val = intel_uncore_read(&dev_priv->uncore, TRANS_DDI_FUNC_CTL2(TRANSCODER_DSI_0));
238800acb329SVandita Kulkarni 	val &= PORT_SYNC_MODE_ENABLE;
238900acb329SVandita Kulkarni 
239000acb329SVandita Kulkarni 	/*
239100acb329SVandita Kulkarni 	 * if dual link is enabled, then read DSI_0
239200acb329SVandita Kulkarni 	 * transcoder registers
239300acb329SVandita Kulkarni 	 */
239400acb329SVandita Kulkarni 	port = ((te_trigger & DSI1_TE && val) || (te_trigger & DSI0_TE)) ?
239500acb329SVandita Kulkarni 						  PORT_A : PORT_B;
239600acb329SVandita Kulkarni 	dsi_trans = (port == PORT_A) ? TRANSCODER_DSI_0 : TRANSCODER_DSI_1;
239700acb329SVandita Kulkarni 
239800acb329SVandita Kulkarni 	/* Check if DSI configured in command mode */
23992939eb06SJani Nikula 	val = intel_uncore_read(&dev_priv->uncore, DSI_TRANS_FUNC_CONF(dsi_trans));
240000acb329SVandita Kulkarni 	val = val & OP_MODE_MASK;
240100acb329SVandita Kulkarni 
240200acb329SVandita Kulkarni 	if (val != CMD_MODE_NO_GATE && val != CMD_MODE_TE_GATE) {
240300acb329SVandita Kulkarni 		drm_err(&dev_priv->drm, "DSI trancoder not configured in command mode\n");
240400acb329SVandita Kulkarni 		return;
240500acb329SVandita Kulkarni 	}
240600acb329SVandita Kulkarni 
240700acb329SVandita Kulkarni 	/* Get PIPE for handling VBLANK event */
24082939eb06SJani Nikula 	val = intel_uncore_read(&dev_priv->uncore, TRANS_DDI_FUNC_CTL(dsi_trans));
240900acb329SVandita Kulkarni 	switch (val & TRANS_DDI_EDP_INPUT_MASK) {
241000acb329SVandita Kulkarni 	case TRANS_DDI_EDP_INPUT_A_ON:
241100acb329SVandita Kulkarni 		pipe = PIPE_A;
241200acb329SVandita Kulkarni 		break;
241300acb329SVandita Kulkarni 	case TRANS_DDI_EDP_INPUT_B_ONOFF:
241400acb329SVandita Kulkarni 		pipe = PIPE_B;
241500acb329SVandita Kulkarni 		break;
241600acb329SVandita Kulkarni 	case TRANS_DDI_EDP_INPUT_C_ONOFF:
241700acb329SVandita Kulkarni 		pipe = PIPE_C;
241800acb329SVandita Kulkarni 		break;
241900acb329SVandita Kulkarni 	default:
242000acb329SVandita Kulkarni 		drm_err(&dev_priv->drm, "Invalid PIPE\n");
242100acb329SVandita Kulkarni 		return;
242200acb329SVandita Kulkarni 	}
242300acb329SVandita Kulkarni 
242400acb329SVandita Kulkarni 	intel_handle_vblank(dev_priv, pipe);
242500acb329SVandita Kulkarni 
242600acb329SVandita Kulkarni 	/* clear TE in dsi IIR */
242700acb329SVandita Kulkarni 	port = (te_trigger & DSI1_TE) ? PORT_B : PORT_A;
24282939eb06SJani Nikula 	tmp = intel_uncore_read(&dev_priv->uncore, DSI_INTR_IDENT_REG(port));
24292939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, DSI_INTR_IDENT_REG(port), tmp);
243000acb329SVandita Kulkarni }
243100acb329SVandita Kulkarni 
2432cda195f1SVille Syrjälä static u32 gen8_de_pipe_flip_done_mask(struct drm_i915_private *i915)
2433cda195f1SVille Syrjälä {
2434373abf1aSMatt Roper 	if (DISPLAY_VER(i915) >= 9)
2435cda195f1SVille Syrjälä 		return GEN9_PIPE_PLANE1_FLIP_DONE;
2436cda195f1SVille Syrjälä 	else
2437cda195f1SVille Syrjälä 		return GEN8_PIPE_PRIMARY_FLIP_DONE;
2438cda195f1SVille Syrjälä }
2439cda195f1SVille Syrjälä 
24408bcc0840SMatt Roper u32 gen8_de_pipe_underrun_mask(struct drm_i915_private *dev_priv)
24418bcc0840SMatt Roper {
24428bcc0840SMatt Roper 	u32 mask = GEN8_PIPE_FIFO_UNDERRUN;
24438bcc0840SMatt Roper 
24448bcc0840SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 13)
24458bcc0840SMatt Roper 		mask |= XELPD_PIPE_SOFT_UNDERRUN |
24468bcc0840SMatt Roper 			XELPD_PIPE_HARD_UNDERRUN;
24478bcc0840SMatt Roper 
24488bcc0840SMatt Roper 	return mask;
24498bcc0840SMatt Roper }
24508bcc0840SMatt Roper 
245146c63d24SJosé Roberto de Souza static irqreturn_t
245246c63d24SJosé Roberto de Souza gen8_de_irq_handler(struct drm_i915_private *dev_priv, u32 master_ctl)
245346c63d24SJosé Roberto de Souza {
245446c63d24SJosé Roberto de Souza 	irqreturn_t ret = IRQ_NONE;
245546c63d24SJosé Roberto de Souza 	u32 iir;
245646c63d24SJosé Roberto de Souza 	enum pipe pipe;
245746c63d24SJosé Roberto de Souza 
2458a844cfbeSJosé Roberto de Souza 	drm_WARN_ON_ONCE(&dev_priv->drm, !HAS_DISPLAY(dev_priv));
2459a844cfbeSJosé Roberto de Souza 
246046c63d24SJosé Roberto de Souza 	if (master_ctl & GEN8_DE_MISC_IRQ) {
24612939eb06SJani Nikula 		iir = intel_uncore_read(&dev_priv->uncore, GEN8_DE_MISC_IIR);
246246c63d24SJosé Roberto de Souza 		if (iir) {
24632939eb06SJani Nikula 			intel_uncore_write(&dev_priv->uncore, GEN8_DE_MISC_IIR, iir);
246446c63d24SJosé Roberto de Souza 			ret = IRQ_HANDLED;
246546c63d24SJosé Roberto de Souza 			gen8_de_misc_irq_handler(dev_priv, iir);
246646c63d24SJosé Roberto de Souza 		} else {
246700376ccfSWambui Karuga 			drm_err(&dev_priv->drm,
246800376ccfSWambui Karuga 				"The master control interrupt lied (DE MISC)!\n");
2469abd58f01SBen Widawsky 		}
247046c63d24SJosé Roberto de Souza 	}
2471abd58f01SBen Widawsky 
2472373abf1aSMatt Roper 	if (DISPLAY_VER(dev_priv) >= 11 && (master_ctl & GEN11_DE_HPD_IRQ)) {
24732939eb06SJani Nikula 		iir = intel_uncore_read(&dev_priv->uncore, GEN11_DE_HPD_IIR);
2474121e758eSDhinakaran Pandiyan 		if (iir) {
24752939eb06SJani Nikula 			intel_uncore_write(&dev_priv->uncore, GEN11_DE_HPD_IIR, iir);
2476121e758eSDhinakaran Pandiyan 			ret = IRQ_HANDLED;
2477121e758eSDhinakaran Pandiyan 			gen11_hpd_irq_handler(dev_priv, iir);
2478121e758eSDhinakaran Pandiyan 		} else {
247900376ccfSWambui Karuga 			drm_err(&dev_priv->drm,
248000376ccfSWambui Karuga 				"The master control interrupt lied, (DE HPD)!\n");
2481121e758eSDhinakaran Pandiyan 		}
2482121e758eSDhinakaran Pandiyan 	}
2483121e758eSDhinakaran Pandiyan 
24846d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
24852939eb06SJani Nikula 		iir = intel_uncore_read(&dev_priv->uncore, GEN8_DE_PORT_IIR);
2486e32192e1STvrtko Ursulin 		if (iir) {
2487d04a492dSShashank Sharma 			bool found = false;
2488cebd87a0SVille Syrjälä 
24892939eb06SJani Nikula 			intel_uncore_write(&dev_priv->uncore, GEN8_DE_PORT_IIR, iir);
24906d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
249188e04703SJesse Barnes 
24929d17210fSLucas De Marchi 			if (iir & gen8_de_port_aux_mask(dev_priv)) {
249391d14251STvrtko Ursulin 				dp_aux_irq_handler(dev_priv);
2494d04a492dSShashank Sharma 				found = true;
2495d04a492dSShashank Sharma 			}
2496d04a492dSShashank Sharma 
249770bfb307SMatt Roper 			if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) {
24989a55a620SVille Syrjälä 				u32 hotplug_trigger = iir & BXT_DE_PORT_HOTPLUG_MASK;
24999a55a620SVille Syrjälä 
25009a55a620SVille Syrjälä 				if (hotplug_trigger) {
25019a55a620SVille Syrjälä 					bxt_hpd_irq_handler(dev_priv, hotplug_trigger);
2502d04a492dSShashank Sharma 					found = true;
2503d04a492dSShashank Sharma 				}
2504e32192e1STvrtko Ursulin 			} else if (IS_BROADWELL(dev_priv)) {
25059a55a620SVille Syrjälä 				u32 hotplug_trigger = iir & BDW_DE_PORT_HOTPLUG_MASK;
25069a55a620SVille Syrjälä 
25079a55a620SVille Syrjälä 				if (hotplug_trigger) {
25089a55a620SVille Syrjälä 					ilk_hpd_irq_handler(dev_priv, hotplug_trigger);
2509e32192e1STvrtko Ursulin 					found = true;
2510e32192e1STvrtko Ursulin 				}
2511e32192e1STvrtko Ursulin 			}
2512d04a492dSShashank Sharma 
251370bfb307SMatt Roper 			if ((IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) &&
251470bfb307SMatt Roper 			    (iir & BXT_DE_PORT_GMBUS)) {
251591d14251STvrtko Ursulin 				gmbus_irq_handler(dev_priv);
25169e63743eSShashank Sharma 				found = true;
25179e63743eSShashank Sharma 			}
25189e63743eSShashank Sharma 
2519373abf1aSMatt Roper 			if (DISPLAY_VER(dev_priv) >= 11) {
25209a55a620SVille Syrjälä 				u32 te_trigger = iir & (DSI0_TE | DSI1_TE);
25219a55a620SVille Syrjälä 
25229a55a620SVille Syrjälä 				if (te_trigger) {
25239a55a620SVille Syrjälä 					gen11_dsi_te_interrupt_handler(dev_priv, te_trigger);
252400acb329SVandita Kulkarni 					found = true;
252500acb329SVandita Kulkarni 				}
252600acb329SVandita Kulkarni 			}
252700acb329SVandita Kulkarni 
2528d04a492dSShashank Sharma 			if (!found)
252900376ccfSWambui Karuga 				drm_err(&dev_priv->drm,
253000376ccfSWambui Karuga 					"Unexpected DE Port interrupt\n");
25316d766f02SDaniel Vetter 		}
253238cc46d7SOscar Mateo 		else
253300376ccfSWambui Karuga 			drm_err(&dev_priv->drm,
253400376ccfSWambui Karuga 				"The master control interrupt lied (DE PORT)!\n");
25356d766f02SDaniel Vetter 	}
25366d766f02SDaniel Vetter 
2537055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2538fd3a4024SDaniel Vetter 		u32 fault_errors;
2539abd58f01SBen Widawsky 
2540c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2541c42664ccSDaniel Vetter 			continue;
2542c42664ccSDaniel Vetter 
25432939eb06SJani Nikula 		iir = intel_uncore_read(&dev_priv->uncore, GEN8_DE_PIPE_IIR(pipe));
2544e32192e1STvrtko Ursulin 		if (!iir) {
254500376ccfSWambui Karuga 			drm_err(&dev_priv->drm,
254600376ccfSWambui Karuga 				"The master control interrupt lied (DE PIPE)!\n");
2547e32192e1STvrtko Ursulin 			continue;
2548e32192e1STvrtko Ursulin 		}
2549770de83dSDamien Lespiau 
2550e32192e1STvrtko Ursulin 		ret = IRQ_HANDLED;
25512939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, GEN8_DE_PIPE_IIR(pipe), iir);
2552e32192e1STvrtko Ursulin 
2553fd3a4024SDaniel Vetter 		if (iir & GEN8_PIPE_VBLANK)
2554aca9310aSAnshuman Gupta 			intel_handle_vblank(dev_priv, pipe);
2555abd58f01SBen Widawsky 
2556cda195f1SVille Syrjälä 		if (iir & gen8_de_pipe_flip_done_mask(dev_priv))
25571288f9b0SKarthik B S 			flip_done_handler(dev_priv, pipe);
25581288f9b0SKarthik B S 
2559e32192e1STvrtko Ursulin 		if (iir & GEN8_PIPE_CDCLK_CRC_DONE)
256091d14251STvrtko Ursulin 			hsw_pipe_crc_irq_handler(dev_priv, pipe);
25610fbe7870SDaniel Vetter 
25628bcc0840SMatt Roper 		if (iir & gen8_de_pipe_underrun_mask(dev_priv))
2563e32192e1STvrtko Ursulin 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
256438d83c96SDaniel Vetter 
25655270130dSVille Syrjälä 		fault_errors = iir & gen8_de_pipe_fault_mask(dev_priv);
2566770de83dSDamien Lespiau 		if (fault_errors)
256700376ccfSWambui Karuga 			drm_err(&dev_priv->drm,
256800376ccfSWambui Karuga 				"Fault errors on pipe %c: 0x%08x\n",
256930100f2bSDaniel Vetter 				pipe_name(pipe),
2570e32192e1STvrtko Ursulin 				fault_errors);
2571abd58f01SBen Widawsky 	}
2572abd58f01SBen Widawsky 
257391d14251STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv) && !HAS_PCH_NOP(dev_priv) &&
2574266ea3d9SShashank Sharma 	    master_ctl & GEN8_DE_PCH_IRQ) {
257592d03a80SDaniel Vetter 		/*
257692d03a80SDaniel Vetter 		 * FIXME(BDW): Assume for now that the new interrupt handling
257792d03a80SDaniel Vetter 		 * scheme also closed the SDE interrupt handling race we've seen
257892d03a80SDaniel Vetter 		 * on older pch-split platforms. But this needs testing.
257992d03a80SDaniel Vetter 		 */
25802939eb06SJani Nikula 		iir = intel_uncore_read(&dev_priv->uncore, SDEIIR);
2581e32192e1STvrtko Ursulin 		if (iir) {
25822939eb06SJani Nikula 			intel_uncore_write(&dev_priv->uncore, SDEIIR, iir);
258392d03a80SDaniel Vetter 			ret = IRQ_HANDLED;
25846dbf30ceSVille Syrjälä 
258558676af6SLucas De Marchi 			if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
258658676af6SLucas De Marchi 				icp_irq_handler(dev_priv, iir);
2587c6c30b91SRodrigo Vivi 			else if (INTEL_PCH_TYPE(dev_priv) >= PCH_SPT)
258891d14251STvrtko Ursulin 				spt_irq_handler(dev_priv, iir);
25896dbf30ceSVille Syrjälä 			else
259091d14251STvrtko Ursulin 				cpt_irq_handler(dev_priv, iir);
25912dfb0b81SJani Nikula 		} else {
25922dfb0b81SJani Nikula 			/*
25932dfb0b81SJani Nikula 			 * Like on previous PCH there seems to be something
25942dfb0b81SJani Nikula 			 * fishy going on with forwarding PCH interrupts.
25952dfb0b81SJani Nikula 			 */
259600376ccfSWambui Karuga 			drm_dbg(&dev_priv->drm,
259700376ccfSWambui Karuga 				"The master control interrupt lied (SDE)!\n");
25982dfb0b81SJani Nikula 		}
259992d03a80SDaniel Vetter 	}
260092d03a80SDaniel Vetter 
2601f11a0f46STvrtko Ursulin 	return ret;
2602f11a0f46STvrtko Ursulin }
2603f11a0f46STvrtko Ursulin 
26044376b9c9SMika Kuoppala static inline u32 gen8_master_intr_disable(void __iomem * const regs)
26054376b9c9SMika Kuoppala {
26064376b9c9SMika Kuoppala 	raw_reg_write(regs, GEN8_MASTER_IRQ, 0);
26074376b9c9SMika Kuoppala 
26084376b9c9SMika Kuoppala 	/*
26094376b9c9SMika Kuoppala 	 * Now with master disabled, get a sample of level indications
26104376b9c9SMika Kuoppala 	 * for this interrupt. Indications will be cleared on related acks.
26114376b9c9SMika Kuoppala 	 * New indications can and will light up during processing,
26124376b9c9SMika Kuoppala 	 * and will generate new interrupt after enabling master.
26134376b9c9SMika Kuoppala 	 */
26144376b9c9SMika Kuoppala 	return raw_reg_read(regs, GEN8_MASTER_IRQ);
26154376b9c9SMika Kuoppala }
26164376b9c9SMika Kuoppala 
26174376b9c9SMika Kuoppala static inline void gen8_master_intr_enable(void __iomem * const regs)
26184376b9c9SMika Kuoppala {
26194376b9c9SMika Kuoppala 	raw_reg_write(regs, GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
26204376b9c9SMika Kuoppala }
26214376b9c9SMika Kuoppala 
2622f11a0f46STvrtko Ursulin static irqreturn_t gen8_irq_handler(int irq, void *arg)
2623f11a0f46STvrtko Ursulin {
2624b318b824SVille Syrjälä 	struct drm_i915_private *dev_priv = arg;
262525286aacSDaniele Ceraolo Spurio 	void __iomem * const regs = dev_priv->uncore.regs;
2626f11a0f46STvrtko Ursulin 	u32 master_ctl;
2627f11a0f46STvrtko Ursulin 
2628f11a0f46STvrtko Ursulin 	if (!intel_irqs_enabled(dev_priv))
2629f11a0f46STvrtko Ursulin 		return IRQ_NONE;
2630f11a0f46STvrtko Ursulin 
26314376b9c9SMika Kuoppala 	master_ctl = gen8_master_intr_disable(regs);
26324376b9c9SMika Kuoppala 	if (!master_ctl) {
26334376b9c9SMika Kuoppala 		gen8_master_intr_enable(regs);
2634f11a0f46STvrtko Ursulin 		return IRQ_NONE;
26354376b9c9SMika Kuoppala 	}
2636f11a0f46STvrtko Ursulin 
26376cc32f15SChris Wilson 	/* Find, queue (onto bottom-halves), then clear each source */
26386cc32f15SChris Wilson 	gen8_gt_irq_handler(&dev_priv->gt, master_ctl);
2639f0fd96f5SChris Wilson 
2640f0fd96f5SChris Wilson 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
2641f0fd96f5SChris Wilson 	if (master_ctl & ~GEN8_GT_IRQS) {
26429102650fSDaniele Ceraolo Spurio 		disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
264355ef72f2SChris Wilson 		gen8_de_irq_handler(dev_priv, master_ctl);
26449102650fSDaniele Ceraolo Spurio 		enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
2645f0fd96f5SChris Wilson 	}
2646f11a0f46STvrtko Ursulin 
26474376b9c9SMika Kuoppala 	gen8_master_intr_enable(regs);
2648abd58f01SBen Widawsky 
26499c6508b9SThomas Gleixner 	pmu_irq_stats(dev_priv, IRQ_HANDLED);
26509c6508b9SThomas Gleixner 
265155ef72f2SChris Wilson 	return IRQ_HANDLED;
2652abd58f01SBen Widawsky }
2653abd58f01SBen Widawsky 
265451951ae7SMika Kuoppala static u32
26559b77011eSTvrtko Ursulin gen11_gu_misc_irq_ack(struct intel_gt *gt, const u32 master_ctl)
2656df0d28c1SDhinakaran Pandiyan {
26579b77011eSTvrtko Ursulin 	void __iomem * const regs = gt->uncore->regs;
26587a909383SChris Wilson 	u32 iir;
2659df0d28c1SDhinakaran Pandiyan 
2660df0d28c1SDhinakaran Pandiyan 	if (!(master_ctl & GEN11_GU_MISC_IRQ))
26617a909383SChris Wilson 		return 0;
2662df0d28c1SDhinakaran Pandiyan 
26637a909383SChris Wilson 	iir = raw_reg_read(regs, GEN11_GU_MISC_IIR);
26647a909383SChris Wilson 	if (likely(iir))
26657a909383SChris Wilson 		raw_reg_write(regs, GEN11_GU_MISC_IIR, iir);
26667a909383SChris Wilson 
26677a909383SChris Wilson 	return iir;
2668df0d28c1SDhinakaran Pandiyan }
2669df0d28c1SDhinakaran Pandiyan 
2670df0d28c1SDhinakaran Pandiyan static void
26719b77011eSTvrtko Ursulin gen11_gu_misc_irq_handler(struct intel_gt *gt, const u32 iir)
2672df0d28c1SDhinakaran Pandiyan {
2673df0d28c1SDhinakaran Pandiyan 	if (iir & GEN11_GU_MISC_GSE)
26749b77011eSTvrtko Ursulin 		intel_opregion_asle_intr(gt->i915);
2675df0d28c1SDhinakaran Pandiyan }
2676df0d28c1SDhinakaran Pandiyan 
267781067b71SMika Kuoppala static inline u32 gen11_master_intr_disable(void __iomem * const regs)
267881067b71SMika Kuoppala {
267981067b71SMika Kuoppala 	raw_reg_write(regs, GEN11_GFX_MSTR_IRQ, 0);
268081067b71SMika Kuoppala 
268181067b71SMika Kuoppala 	/*
268281067b71SMika Kuoppala 	 * Now with master disabled, get a sample of level indications
268381067b71SMika Kuoppala 	 * for this interrupt. Indications will be cleared on related acks.
268481067b71SMika Kuoppala 	 * New indications can and will light up during processing,
268581067b71SMika Kuoppala 	 * and will generate new interrupt after enabling master.
268681067b71SMika Kuoppala 	 */
268781067b71SMika Kuoppala 	return raw_reg_read(regs, GEN11_GFX_MSTR_IRQ);
268881067b71SMika Kuoppala }
268981067b71SMika Kuoppala 
269081067b71SMika Kuoppala static inline void gen11_master_intr_enable(void __iomem * const regs)
269181067b71SMika Kuoppala {
269281067b71SMika Kuoppala 	raw_reg_write(regs, GEN11_GFX_MSTR_IRQ, GEN11_MASTER_IRQ);
269381067b71SMika Kuoppala }
269481067b71SMika Kuoppala 
2695a3265d85SMatt Roper static void
2696a3265d85SMatt Roper gen11_display_irq_handler(struct drm_i915_private *i915)
2697a3265d85SMatt Roper {
2698a3265d85SMatt Roper 	void __iomem * const regs = i915->uncore.regs;
2699a3265d85SMatt Roper 	const u32 disp_ctl = raw_reg_read(regs, GEN11_DISPLAY_INT_CTL);
2700a3265d85SMatt Roper 
2701a3265d85SMatt Roper 	disable_rpm_wakeref_asserts(&i915->runtime_pm);
2702a3265d85SMatt Roper 	/*
2703a3265d85SMatt Roper 	 * GEN11_DISPLAY_INT_CTL has same format as GEN8_MASTER_IRQ
2704a3265d85SMatt Roper 	 * for the display related bits.
2705a3265d85SMatt Roper 	 */
2706a3265d85SMatt Roper 	raw_reg_write(regs, GEN11_DISPLAY_INT_CTL, 0x0);
2707a3265d85SMatt Roper 	gen8_de_irq_handler(i915, disp_ctl);
2708a3265d85SMatt Roper 	raw_reg_write(regs, GEN11_DISPLAY_INT_CTL,
2709a3265d85SMatt Roper 		      GEN11_DISPLAY_IRQ_ENABLE);
2710a3265d85SMatt Roper 
2711a3265d85SMatt Roper 	enable_rpm_wakeref_asserts(&i915->runtime_pm);
2712a3265d85SMatt Roper }
2713a3265d85SMatt Roper 
271422e26af7SPaulo Zanoni static irqreturn_t gen11_irq_handler(int irq, void *arg)
271551951ae7SMika Kuoppala {
271622e26af7SPaulo Zanoni 	struct drm_i915_private *i915 = arg;
271725286aacSDaniele Ceraolo Spurio 	void __iomem * const regs = i915->uncore.regs;
27189b77011eSTvrtko Ursulin 	struct intel_gt *gt = &i915->gt;
271951951ae7SMika Kuoppala 	u32 master_ctl;
2720df0d28c1SDhinakaran Pandiyan 	u32 gu_misc_iir;
272151951ae7SMika Kuoppala 
272251951ae7SMika Kuoppala 	if (!intel_irqs_enabled(i915))
272351951ae7SMika Kuoppala 		return IRQ_NONE;
272451951ae7SMika Kuoppala 
272522e26af7SPaulo Zanoni 	master_ctl = gen11_master_intr_disable(regs);
272681067b71SMika Kuoppala 	if (!master_ctl) {
272722e26af7SPaulo Zanoni 		gen11_master_intr_enable(regs);
272851951ae7SMika Kuoppala 		return IRQ_NONE;
272981067b71SMika Kuoppala 	}
273051951ae7SMika Kuoppala 
27316cc32f15SChris Wilson 	/* Find, queue (onto bottom-halves), then clear each source */
27329b77011eSTvrtko Ursulin 	gen11_gt_irq_handler(gt, master_ctl);
273351951ae7SMika Kuoppala 
273451951ae7SMika Kuoppala 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
2735a3265d85SMatt Roper 	if (master_ctl & GEN11_DISPLAY_IRQ)
2736a3265d85SMatt Roper 		gen11_display_irq_handler(i915);
273751951ae7SMika Kuoppala 
27389b77011eSTvrtko Ursulin 	gu_misc_iir = gen11_gu_misc_irq_ack(gt, master_ctl);
2739df0d28c1SDhinakaran Pandiyan 
274022e26af7SPaulo Zanoni 	gen11_master_intr_enable(regs);
274151951ae7SMika Kuoppala 
27429b77011eSTvrtko Ursulin 	gen11_gu_misc_irq_handler(gt, gu_misc_iir);
2743df0d28c1SDhinakaran Pandiyan 
27449c6508b9SThomas Gleixner 	pmu_irq_stats(i915, IRQ_HANDLED);
27459c6508b9SThomas Gleixner 
274651951ae7SMika Kuoppala 	return IRQ_HANDLED;
274751951ae7SMika Kuoppala }
274851951ae7SMika Kuoppala 
274922e26af7SPaulo Zanoni static inline u32 dg1_master_intr_disable(void __iomem * const regs)
275097b492f5SLucas De Marchi {
275197b492f5SLucas De Marchi 	u32 val;
275297b492f5SLucas De Marchi 
275397b492f5SLucas De Marchi 	/* First disable interrupts */
275422e26af7SPaulo Zanoni 	raw_reg_write(regs, DG1_MSTR_TILE_INTR, 0);
275597b492f5SLucas De Marchi 
275697b492f5SLucas De Marchi 	/* Get the indication levels and ack the master unit */
275722e26af7SPaulo Zanoni 	val = raw_reg_read(regs, DG1_MSTR_TILE_INTR);
275897b492f5SLucas De Marchi 	if (unlikely(!val))
275997b492f5SLucas De Marchi 		return 0;
276097b492f5SLucas De Marchi 
276122e26af7SPaulo Zanoni 	raw_reg_write(regs, DG1_MSTR_TILE_INTR, val);
276297b492f5SLucas De Marchi 
276397b492f5SLucas De Marchi 	return val;
276497b492f5SLucas De Marchi }
276597b492f5SLucas De Marchi 
276697b492f5SLucas De Marchi static inline void dg1_master_intr_enable(void __iomem * const regs)
276797b492f5SLucas De Marchi {
276822e26af7SPaulo Zanoni 	raw_reg_write(regs, DG1_MSTR_TILE_INTR, DG1_MSTR_IRQ);
276997b492f5SLucas De Marchi }
277097b492f5SLucas De Marchi 
277197b492f5SLucas De Marchi static irqreturn_t dg1_irq_handler(int irq, void *arg)
277297b492f5SLucas De Marchi {
277322e26af7SPaulo Zanoni 	struct drm_i915_private * const i915 = arg;
277422e26af7SPaulo Zanoni 	struct intel_gt *gt = &i915->gt;
277522e26af7SPaulo Zanoni 	void __iomem * const regs = i915->uncore.regs;
277622e26af7SPaulo Zanoni 	u32 master_tile_ctl, master_ctl;
277722e26af7SPaulo Zanoni 	u32 gu_misc_iir;
277822e26af7SPaulo Zanoni 
277922e26af7SPaulo Zanoni 	if (!intel_irqs_enabled(i915))
278022e26af7SPaulo Zanoni 		return IRQ_NONE;
278122e26af7SPaulo Zanoni 
278222e26af7SPaulo Zanoni 	master_tile_ctl = dg1_master_intr_disable(regs);
278322e26af7SPaulo Zanoni 	if (!master_tile_ctl) {
278422e26af7SPaulo Zanoni 		dg1_master_intr_enable(regs);
278522e26af7SPaulo Zanoni 		return IRQ_NONE;
278622e26af7SPaulo Zanoni 	}
278722e26af7SPaulo Zanoni 
278822e26af7SPaulo Zanoni 	/* FIXME: we only support tile 0 for now. */
278922e26af7SPaulo Zanoni 	if (master_tile_ctl & DG1_MSTR_TILE(0)) {
279022e26af7SPaulo Zanoni 		master_ctl = raw_reg_read(regs, GEN11_GFX_MSTR_IRQ);
279122e26af7SPaulo Zanoni 		raw_reg_write(regs, GEN11_GFX_MSTR_IRQ, master_ctl);
279222e26af7SPaulo Zanoni 	} else {
279322e26af7SPaulo Zanoni 		DRM_ERROR("Tile not supported: 0x%08x\n", master_tile_ctl);
279422e26af7SPaulo Zanoni 		dg1_master_intr_enable(regs);
279522e26af7SPaulo Zanoni 		return IRQ_NONE;
279622e26af7SPaulo Zanoni 	}
279722e26af7SPaulo Zanoni 
279822e26af7SPaulo Zanoni 	gen11_gt_irq_handler(gt, master_ctl);
279922e26af7SPaulo Zanoni 
280022e26af7SPaulo Zanoni 	if (master_ctl & GEN11_DISPLAY_IRQ)
280122e26af7SPaulo Zanoni 		gen11_display_irq_handler(i915);
280222e26af7SPaulo Zanoni 
280322e26af7SPaulo Zanoni 	gu_misc_iir = gen11_gu_misc_irq_ack(gt, master_ctl);
280422e26af7SPaulo Zanoni 
280522e26af7SPaulo Zanoni 	dg1_master_intr_enable(regs);
280622e26af7SPaulo Zanoni 
280722e26af7SPaulo Zanoni 	gen11_gu_misc_irq_handler(gt, gu_misc_iir);
280822e26af7SPaulo Zanoni 
280922e26af7SPaulo Zanoni 	pmu_irq_stats(i915, IRQ_HANDLED);
281022e26af7SPaulo Zanoni 
281122e26af7SPaulo Zanoni 	return IRQ_HANDLED;
281297b492f5SLucas De Marchi }
281397b492f5SLucas De Marchi 
281442f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
281542f52ef8SKeith Packard  * we use as a pipe index
281642f52ef8SKeith Packard  */
281708fa8fd0SVille Syrjälä int i8xx_enable_vblank(struct drm_crtc *crtc)
28180a3e67a4SJesse Barnes {
281908fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
282008fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
2821e9d21d7fSKeith Packard 	unsigned long irqflags;
282271e0ffa5SJesse Barnes 
28231ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
282486e83e35SChris Wilson 	i915_enable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_STATUS);
282586e83e35SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
282686e83e35SChris Wilson 
282786e83e35SChris Wilson 	return 0;
282886e83e35SChris Wilson }
282986e83e35SChris Wilson 
28307d423af9SVille Syrjälä int i915gm_enable_vblank(struct drm_crtc *crtc)
2831d938da6bSVille Syrjälä {
283208fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
2833d938da6bSVille Syrjälä 
28347d423af9SVille Syrjälä 	/*
28357d423af9SVille Syrjälä 	 * Vblank interrupts fail to wake the device up from C2+.
28367d423af9SVille Syrjälä 	 * Disabling render clock gating during C-states avoids
28377d423af9SVille Syrjälä 	 * the problem. There is a small power cost so we do this
28387d423af9SVille Syrjälä 	 * only when vblank interrupts are actually enabled.
28397d423af9SVille Syrjälä 	 */
28407d423af9SVille Syrjälä 	if (dev_priv->vblank_enabled++ == 0)
28412939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, SCPD0, _MASKED_BIT_ENABLE(CSTATE_RENDER_CLOCK_GATE_DISABLE));
2842d938da6bSVille Syrjälä 
284308fa8fd0SVille Syrjälä 	return i8xx_enable_vblank(crtc);
2844d938da6bSVille Syrjälä }
2845d938da6bSVille Syrjälä 
284608fa8fd0SVille Syrjälä int i965_enable_vblank(struct drm_crtc *crtc)
284786e83e35SChris Wilson {
284808fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
284908fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
285086e83e35SChris Wilson 	unsigned long irqflags;
285186e83e35SChris Wilson 
285286e83e35SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
28537c463586SKeith Packard 	i915_enable_pipestat(dev_priv, pipe,
2854755e9019SImre Deak 			     PIPE_START_VBLANK_INTERRUPT_STATUS);
28551ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
28568692d00eSChris Wilson 
28570a3e67a4SJesse Barnes 	return 0;
28580a3e67a4SJesse Barnes }
28590a3e67a4SJesse Barnes 
286008fa8fd0SVille Syrjälä int ilk_enable_vblank(struct drm_crtc *crtc)
2861f796cf8fSJesse Barnes {
286208fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
286308fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
2864f796cf8fSJesse Barnes 	unsigned long irqflags;
2865373abf1aSMatt Roper 	u32 bit = DISPLAY_VER(dev_priv) >= 7 ?
286686e83e35SChris Wilson 		DE_PIPE_VBLANK_IVB(pipe) : DE_PIPE_VBLANK(pipe);
2867f796cf8fSJesse Barnes 
2868f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2869fbdedaeaSVille Syrjälä 	ilk_enable_display_irq(dev_priv, bit);
2870b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2871b1f14ad0SJesse Barnes 
28722e8bf223SDhinakaran Pandiyan 	/* Even though there is no DMC, frame counter can get stuck when
28732e8bf223SDhinakaran Pandiyan 	 * PSR is active as no frames are generated.
28742e8bf223SDhinakaran Pandiyan 	 */
28752e8bf223SDhinakaran Pandiyan 	if (HAS_PSR(dev_priv))
287608fa8fd0SVille Syrjälä 		drm_crtc_vblank_restore(crtc);
28772e8bf223SDhinakaran Pandiyan 
2878b1f14ad0SJesse Barnes 	return 0;
2879b1f14ad0SJesse Barnes }
2880b1f14ad0SJesse Barnes 
28819c9e97c4SVandita Kulkarni static bool gen11_dsi_configure_te(struct intel_crtc *intel_crtc,
28829c9e97c4SVandita Kulkarni 				   bool enable)
28839c9e97c4SVandita Kulkarni {
28849c9e97c4SVandita Kulkarni 	struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
28859c9e97c4SVandita Kulkarni 	enum port port;
28869c9e97c4SVandita Kulkarni 	u32 tmp;
28879c9e97c4SVandita Kulkarni 
28889c9e97c4SVandita Kulkarni 	if (!(intel_crtc->mode_flags &
28899c9e97c4SVandita Kulkarni 	    (I915_MODE_FLAG_DSI_USE_TE1 | I915_MODE_FLAG_DSI_USE_TE0)))
28909c9e97c4SVandita Kulkarni 		return false;
28919c9e97c4SVandita Kulkarni 
28929c9e97c4SVandita Kulkarni 	/* for dual link cases we consider TE from slave */
28939c9e97c4SVandita Kulkarni 	if (intel_crtc->mode_flags & I915_MODE_FLAG_DSI_USE_TE1)
28949c9e97c4SVandita Kulkarni 		port = PORT_B;
28959c9e97c4SVandita Kulkarni 	else
28969c9e97c4SVandita Kulkarni 		port = PORT_A;
28979c9e97c4SVandita Kulkarni 
28982939eb06SJani Nikula 	tmp =  intel_uncore_read(&dev_priv->uncore, DSI_INTR_MASK_REG(port));
28999c9e97c4SVandita Kulkarni 	if (enable)
29009c9e97c4SVandita Kulkarni 		tmp &= ~DSI_TE_EVENT;
29019c9e97c4SVandita Kulkarni 	else
29029c9e97c4SVandita Kulkarni 		tmp |= DSI_TE_EVENT;
29039c9e97c4SVandita Kulkarni 
29042939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, DSI_INTR_MASK_REG(port), tmp);
29059c9e97c4SVandita Kulkarni 
29062939eb06SJani Nikula 	tmp = intel_uncore_read(&dev_priv->uncore, DSI_INTR_IDENT_REG(port));
29072939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, DSI_INTR_IDENT_REG(port), tmp);
29089c9e97c4SVandita Kulkarni 
29099c9e97c4SVandita Kulkarni 	return true;
29109c9e97c4SVandita Kulkarni }
29119c9e97c4SVandita Kulkarni 
2912f15f01a7SVille Syrjälä int bdw_enable_vblank(struct drm_crtc *_crtc)
2913abd58f01SBen Widawsky {
2914f15f01a7SVille Syrjälä 	struct intel_crtc *crtc = to_intel_crtc(_crtc);
2915f15f01a7SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2916f15f01a7SVille Syrjälä 	enum pipe pipe = crtc->pipe;
2917abd58f01SBen Widawsky 	unsigned long irqflags;
2918abd58f01SBen Widawsky 
2919f15f01a7SVille Syrjälä 	if (gen11_dsi_configure_te(crtc, true))
29209c9e97c4SVandita Kulkarni 		return 0;
29219c9e97c4SVandita Kulkarni 
2922abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2923013d3752SVille Syrjälä 	bdw_enable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
2924abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2925013d3752SVille Syrjälä 
29262e8bf223SDhinakaran Pandiyan 	/* Even if there is no DMC, frame counter can get stuck when
29272e8bf223SDhinakaran Pandiyan 	 * PSR is active as no frames are generated, so check only for PSR.
29282e8bf223SDhinakaran Pandiyan 	 */
29292e8bf223SDhinakaran Pandiyan 	if (HAS_PSR(dev_priv))
2930f15f01a7SVille Syrjälä 		drm_crtc_vblank_restore(&crtc->base);
29312e8bf223SDhinakaran Pandiyan 
2932abd58f01SBen Widawsky 	return 0;
2933abd58f01SBen Widawsky }
2934abd58f01SBen Widawsky 
293542f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
293642f52ef8SKeith Packard  * we use as a pipe index
293742f52ef8SKeith Packard  */
293808fa8fd0SVille Syrjälä void i8xx_disable_vblank(struct drm_crtc *crtc)
293986e83e35SChris Wilson {
294008fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
294108fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
294286e83e35SChris Wilson 	unsigned long irqflags;
294386e83e35SChris Wilson 
294486e83e35SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
294586e83e35SChris Wilson 	i915_disable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_STATUS);
294686e83e35SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
294786e83e35SChris Wilson }
294886e83e35SChris Wilson 
29497d423af9SVille Syrjälä void i915gm_disable_vblank(struct drm_crtc *crtc)
2950d938da6bSVille Syrjälä {
295108fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
2952d938da6bSVille Syrjälä 
295308fa8fd0SVille Syrjälä 	i8xx_disable_vblank(crtc);
2954d938da6bSVille Syrjälä 
29557d423af9SVille Syrjälä 	if (--dev_priv->vblank_enabled == 0)
29562939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, SCPD0, _MASKED_BIT_DISABLE(CSTATE_RENDER_CLOCK_GATE_DISABLE));
2957d938da6bSVille Syrjälä }
2958d938da6bSVille Syrjälä 
295908fa8fd0SVille Syrjälä void i965_disable_vblank(struct drm_crtc *crtc)
29600a3e67a4SJesse Barnes {
296108fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
296208fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
2963e9d21d7fSKeith Packard 	unsigned long irqflags;
29640a3e67a4SJesse Barnes 
29651ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
29667c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
2967755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
29681ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
29690a3e67a4SJesse Barnes }
29700a3e67a4SJesse Barnes 
297108fa8fd0SVille Syrjälä void ilk_disable_vblank(struct drm_crtc *crtc)
2972f796cf8fSJesse Barnes {
297308fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
297408fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
2975f796cf8fSJesse Barnes 	unsigned long irqflags;
2976373abf1aSMatt Roper 	u32 bit = DISPLAY_VER(dev_priv) >= 7 ?
297786e83e35SChris Wilson 		DE_PIPE_VBLANK_IVB(pipe) : DE_PIPE_VBLANK(pipe);
2978f796cf8fSJesse Barnes 
2979f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2980fbdedaeaSVille Syrjälä 	ilk_disable_display_irq(dev_priv, bit);
2981b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2982b1f14ad0SJesse Barnes }
2983b1f14ad0SJesse Barnes 
2984f15f01a7SVille Syrjälä void bdw_disable_vblank(struct drm_crtc *_crtc)
2985abd58f01SBen Widawsky {
2986f15f01a7SVille Syrjälä 	struct intel_crtc *crtc = to_intel_crtc(_crtc);
2987f15f01a7SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2988f15f01a7SVille Syrjälä 	enum pipe pipe = crtc->pipe;
2989abd58f01SBen Widawsky 	unsigned long irqflags;
2990abd58f01SBen Widawsky 
2991f15f01a7SVille Syrjälä 	if (gen11_dsi_configure_te(crtc, false))
29929c9e97c4SVandita Kulkarni 		return;
29939c9e97c4SVandita Kulkarni 
2994abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2995013d3752SVille Syrjälä 	bdw_disable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
2996abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2997abd58f01SBen Widawsky }
2998abd58f01SBen Widawsky 
2999b243f530STvrtko Ursulin static void ibx_irq_reset(struct drm_i915_private *dev_priv)
300091738a95SPaulo Zanoni {
3001b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
3002b16b2a2fSPaulo Zanoni 
30036e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
300491738a95SPaulo Zanoni 		return;
300591738a95SPaulo Zanoni 
3006b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, SDE);
3007105b122eSPaulo Zanoni 
30086e266956STvrtko Ursulin 	if (HAS_PCH_CPT(dev_priv) || HAS_PCH_LPT(dev_priv))
30092939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, SERR_INT, 0xffffffff);
3010622364b6SPaulo Zanoni }
3011105b122eSPaulo Zanoni 
301270591a41SVille Syrjälä static void vlv_display_irq_reset(struct drm_i915_private *dev_priv)
301370591a41SVille Syrjälä {
3014b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
3015b16b2a2fSPaulo Zanoni 
301671b8b41dSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
3017f0818984STvrtko Ursulin 		intel_uncore_write(uncore, DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
301871b8b41dSVille Syrjälä 	else
30197d938bc0SVille Syrjälä 		intel_uncore_write(uncore, DPINVGTT, DPINVGTT_STATUS_MASK_VLV);
302071b8b41dSVille Syrjälä 
3021ad22d106SVille Syrjälä 	i915_hotplug_interrupt_update_locked(dev_priv, 0xffffffff, 0);
30222939eb06SJani Nikula 	intel_uncore_write(uncore, PORT_HOTPLUG_STAT, intel_uncore_read(&dev_priv->uncore, PORT_HOTPLUG_STAT));
302370591a41SVille Syrjälä 
302444d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
302570591a41SVille Syrjälä 
3026b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, VLV_);
30278bd099a7SChris Wilson 	dev_priv->irq_mask = ~0u;
302870591a41SVille Syrjälä }
302970591a41SVille Syrjälä 
30308bb61306SVille Syrjälä static void vlv_display_irq_postinstall(struct drm_i915_private *dev_priv)
30318bb61306SVille Syrjälä {
3032b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
3033b16b2a2fSPaulo Zanoni 
30348bb61306SVille Syrjälä 	u32 pipestat_mask;
30359ab981f2SVille Syrjälä 	u32 enable_mask;
30368bb61306SVille Syrjälä 	enum pipe pipe;
30378bb61306SVille Syrjälä 
3038842ebf7aSVille Syrjälä 	pipestat_mask = PIPE_CRC_DONE_INTERRUPT_STATUS;
30398bb61306SVille Syrjälä 
30408bb61306SVille Syrjälä 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
30418bb61306SVille Syrjälä 	for_each_pipe(dev_priv, pipe)
30428bb61306SVille Syrjälä 		i915_enable_pipestat(dev_priv, pipe, pipestat_mask);
30438bb61306SVille Syrjälä 
30449ab981f2SVille Syrjälä 	enable_mask = I915_DISPLAY_PORT_INTERRUPT |
30458bb61306SVille Syrjälä 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3046ebf5f921SVille Syrjälä 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3047ebf5f921SVille Syrjälä 		I915_LPE_PIPE_A_INTERRUPT |
3048ebf5f921SVille Syrjälä 		I915_LPE_PIPE_B_INTERRUPT;
3049ebf5f921SVille Syrjälä 
30508bb61306SVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
3051ebf5f921SVille Syrjälä 		enable_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT |
3052ebf5f921SVille Syrjälä 			I915_LPE_PIPE_C_INTERRUPT;
30536b7eafc1SVille Syrjälä 
305448a1b8d4SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, dev_priv->irq_mask != ~0u);
30556b7eafc1SVille Syrjälä 
30569ab981f2SVille Syrjälä 	dev_priv->irq_mask = ~enable_mask;
30578bb61306SVille Syrjälä 
3058b16b2a2fSPaulo Zanoni 	GEN3_IRQ_INIT(uncore, VLV_, dev_priv->irq_mask, enable_mask);
30598bb61306SVille Syrjälä }
30608bb61306SVille Syrjälä 
30618bb61306SVille Syrjälä /* drm_dma.h hooks
30628bb61306SVille Syrjälä */
30639eae5e27SLucas De Marchi static void ilk_irq_reset(struct drm_i915_private *dev_priv)
30648bb61306SVille Syrjälä {
3065b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
30668bb61306SVille Syrjälä 
3067b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, DE);
3068e44adb5dSChris Wilson 	dev_priv->irq_mask = ~0u;
3069e44adb5dSChris Wilson 
3070651e7d48SLucas De Marchi 	if (GRAPHICS_VER(dev_priv) == 7)
3071f0818984STvrtko Ursulin 		intel_uncore_write(uncore, GEN7_ERR_INT, 0xffffffff);
30728bb61306SVille Syrjälä 
3073fc340442SDaniel Vetter 	if (IS_HASWELL(dev_priv)) {
3074f0818984STvrtko Ursulin 		intel_uncore_write(uncore, EDP_PSR_IMR, 0xffffffff);
3075f0818984STvrtko Ursulin 		intel_uncore_write(uncore, EDP_PSR_IIR, 0xffffffff);
3076fc340442SDaniel Vetter 	}
3077fc340442SDaniel Vetter 
3078cf1c97dcSAndi Shyti 	gen5_gt_irq_reset(&dev_priv->gt);
30798bb61306SVille Syrjälä 
3080b243f530STvrtko Ursulin 	ibx_irq_reset(dev_priv);
30818bb61306SVille Syrjälä }
30828bb61306SVille Syrjälä 
3083b318b824SVille Syrjälä static void valleyview_irq_reset(struct drm_i915_private *dev_priv)
30847e231dbeSJesse Barnes {
30852939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, VLV_MASTER_IER, 0);
30862939eb06SJani Nikula 	intel_uncore_posting_read(&dev_priv->uncore, VLV_MASTER_IER);
308734c7b8a7SVille Syrjälä 
3088cf1c97dcSAndi Shyti 	gen5_gt_irq_reset(&dev_priv->gt);
30897e231dbeSJesse Barnes 
3090ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
30919918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
309270591a41SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3093ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
30947e231dbeSJesse Barnes }
30957e231dbeSJesse Barnes 
3096a844cfbeSJosé Roberto de Souza static void gen8_display_irq_reset(struct drm_i915_private *dev_priv)
3097abd58f01SBen Widawsky {
3098b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
3099d048a268SVille Syrjälä 	enum pipe pipe;
3100abd58f01SBen Widawsky 
3101a844cfbeSJosé Roberto de Souza 	if (!HAS_DISPLAY(dev_priv))
3102a844cfbeSJosé Roberto de Souza 		return;
3103abd58f01SBen Widawsky 
3104f0818984STvrtko Ursulin 	intel_uncore_write(uncore, EDP_PSR_IMR, 0xffffffff);
3105f0818984STvrtko Ursulin 	intel_uncore_write(uncore, EDP_PSR_IIR, 0xffffffff);
3106e04f7eceSVille Syrjälä 
3107055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3108f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3109813bde43SPaulo Zanoni 						   POWER_DOMAIN_PIPE(pipe)))
3110b16b2a2fSPaulo Zanoni 			GEN8_IRQ_RESET_NDX(uncore, DE_PIPE, pipe);
3111abd58f01SBen Widawsky 
3112b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN8_DE_PORT_);
3113b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN8_DE_MISC_);
3114a844cfbeSJosé Roberto de Souza }
3115a844cfbeSJosé Roberto de Souza 
3116a844cfbeSJosé Roberto de Souza static void gen8_irq_reset(struct drm_i915_private *dev_priv)
3117a844cfbeSJosé Roberto de Souza {
3118a844cfbeSJosé Roberto de Souza 	struct intel_uncore *uncore = &dev_priv->uncore;
3119a844cfbeSJosé Roberto de Souza 
3120a844cfbeSJosé Roberto de Souza 	gen8_master_intr_disable(dev_priv->uncore.regs);
3121a844cfbeSJosé Roberto de Souza 
3122a844cfbeSJosé Roberto de Souza 	gen8_gt_irq_reset(&dev_priv->gt);
3123a844cfbeSJosé Roberto de Souza 	gen8_display_irq_reset(dev_priv);
3124b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN8_PCU_);
3125abd58f01SBen Widawsky 
31266e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
3127b243f530STvrtko Ursulin 		ibx_irq_reset(dev_priv);
312859b7cb44STejas Upadhyay 
3129abd58f01SBen Widawsky }
3130abd58f01SBen Widawsky 
3131a3265d85SMatt Roper static void gen11_display_irq_reset(struct drm_i915_private *dev_priv)
313251951ae7SMika Kuoppala {
3133b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
3134d048a268SVille Syrjälä 	enum pipe pipe;
3135562ddcb7SMatt Roper 	u32 trans_mask = BIT(TRANSCODER_A) | BIT(TRANSCODER_B) |
3136562ddcb7SMatt Roper 		BIT(TRANSCODER_C) | BIT(TRANSCODER_D);
313751951ae7SMika Kuoppala 
3138a844cfbeSJosé Roberto de Souza 	if (!HAS_DISPLAY(dev_priv))
3139a844cfbeSJosé Roberto de Souza 		return;
3140a844cfbeSJosé Roberto de Souza 
3141f0818984STvrtko Ursulin 	intel_uncore_write(uncore, GEN11_DISPLAY_INT_CTL, 0);
314251951ae7SMika Kuoppala 
3143373abf1aSMatt Roper 	if (DISPLAY_VER(dev_priv) >= 12) {
31448241cfbeSJosé Roberto de Souza 		enum transcoder trans;
31458241cfbeSJosé Roberto de Souza 
3146562ddcb7SMatt Roper 		for_each_cpu_transcoder_masked(dev_priv, trans, trans_mask) {
31478241cfbeSJosé Roberto de Souza 			enum intel_display_power_domain domain;
31488241cfbeSJosé Roberto de Souza 
31498241cfbeSJosé Roberto de Souza 			domain = POWER_DOMAIN_TRANSCODER(trans);
31508241cfbeSJosé Roberto de Souza 			if (!intel_display_power_is_enabled(dev_priv, domain))
31518241cfbeSJosé Roberto de Souza 				continue;
31528241cfbeSJosé Roberto de Souza 
31538241cfbeSJosé Roberto de Souza 			intel_uncore_write(uncore, TRANS_PSR_IMR(trans), 0xffffffff);
31548241cfbeSJosé Roberto de Souza 			intel_uncore_write(uncore, TRANS_PSR_IIR(trans), 0xffffffff);
31558241cfbeSJosé Roberto de Souza 		}
31568241cfbeSJosé Roberto de Souza 	} else {
3157f0818984STvrtko Ursulin 		intel_uncore_write(uncore, EDP_PSR_IMR, 0xffffffff);
3158f0818984STvrtko Ursulin 		intel_uncore_write(uncore, EDP_PSR_IIR, 0xffffffff);
31598241cfbeSJosé Roberto de Souza 	}
316062819dfdSJosé Roberto de Souza 
316151951ae7SMika Kuoppala 	for_each_pipe(dev_priv, pipe)
316251951ae7SMika Kuoppala 		if (intel_display_power_is_enabled(dev_priv,
316351951ae7SMika Kuoppala 						   POWER_DOMAIN_PIPE(pipe)))
3164b16b2a2fSPaulo Zanoni 			GEN8_IRQ_RESET_NDX(uncore, DE_PIPE, pipe);
316551951ae7SMika Kuoppala 
3166b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN8_DE_PORT_);
3167b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN8_DE_MISC_);
3168b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN11_DE_HPD_);
316931604222SAnusha Srivatsa 
317029b43ae2SRodrigo Vivi 	if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
3171b16b2a2fSPaulo Zanoni 		GEN3_IRQ_RESET(uncore, SDE);
317251951ae7SMika Kuoppala }
317351951ae7SMika Kuoppala 
3174a3265d85SMatt Roper static void gen11_irq_reset(struct drm_i915_private *dev_priv)
3175a3265d85SMatt Roper {
3176a3265d85SMatt Roper 	struct intel_uncore *uncore = &dev_priv->uncore;
3177a3265d85SMatt Roper 
3178a3265d85SMatt Roper 	gen11_master_intr_disable(dev_priv->uncore.regs);
3179a3265d85SMatt Roper 
3180a3265d85SMatt Roper 	gen11_gt_irq_reset(&dev_priv->gt);
3181a3265d85SMatt Roper 	gen11_display_irq_reset(dev_priv);
3182a3265d85SMatt Roper 
3183a3265d85SMatt Roper 	GEN3_IRQ_RESET(uncore, GEN11_GU_MISC_);
3184a3265d85SMatt Roper 	GEN3_IRQ_RESET(uncore, GEN8_PCU_);
3185a3265d85SMatt Roper }
3186a3265d85SMatt Roper 
318722e26af7SPaulo Zanoni static void dg1_irq_reset(struct drm_i915_private *dev_priv)
318822e26af7SPaulo Zanoni {
318922e26af7SPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
319022e26af7SPaulo Zanoni 
319122e26af7SPaulo Zanoni 	dg1_master_intr_disable(dev_priv->uncore.regs);
319222e26af7SPaulo Zanoni 
319322e26af7SPaulo Zanoni 	gen11_gt_irq_reset(&dev_priv->gt);
319422e26af7SPaulo Zanoni 	gen11_display_irq_reset(dev_priv);
319522e26af7SPaulo Zanoni 
319622e26af7SPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN11_GU_MISC_);
319722e26af7SPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN8_PCU_);
319822e26af7SPaulo Zanoni }
319922e26af7SPaulo Zanoni 
32004c6c03beSDamien Lespiau void gen8_irq_power_well_post_enable(struct drm_i915_private *dev_priv,
3201001bd2cbSImre Deak 				     u8 pipe_mask)
3202d49bdb0eSPaulo Zanoni {
3203b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
32048bcc0840SMatt Roper 	u32 extra_ier = GEN8_PIPE_VBLANK |
32058bcc0840SMatt Roper 		gen8_de_pipe_underrun_mask(dev_priv) |
3206cda195f1SVille Syrjälä 		gen8_de_pipe_flip_done_mask(dev_priv);
32076831f3e3SVille Syrjälä 	enum pipe pipe;
3208d49bdb0eSPaulo Zanoni 
320913321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
32109dfe2e3aSImre Deak 
32119dfe2e3aSImre Deak 	if (!intel_irqs_enabled(dev_priv)) {
32129dfe2e3aSImre Deak 		spin_unlock_irq(&dev_priv->irq_lock);
32139dfe2e3aSImre Deak 		return;
32149dfe2e3aSImre Deak 	}
32159dfe2e3aSImre Deak 
32166831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
3217b16b2a2fSPaulo Zanoni 		GEN8_IRQ_INIT_NDX(uncore, DE_PIPE, pipe,
32186831f3e3SVille Syrjälä 				  dev_priv->de_irq_mask[pipe],
32196831f3e3SVille Syrjälä 				  ~dev_priv->de_irq_mask[pipe] | extra_ier);
32209dfe2e3aSImre Deak 
322113321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3222d49bdb0eSPaulo Zanoni }
3223d49bdb0eSPaulo Zanoni 
3224aae8ba84SVille Syrjälä void gen8_irq_power_well_pre_disable(struct drm_i915_private *dev_priv,
3225001bd2cbSImre Deak 				     u8 pipe_mask)
3226aae8ba84SVille Syrjälä {
3227b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
32286831f3e3SVille Syrjälä 	enum pipe pipe;
32296831f3e3SVille Syrjälä 
3230aae8ba84SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
32319dfe2e3aSImre Deak 
32329dfe2e3aSImre Deak 	if (!intel_irqs_enabled(dev_priv)) {
32339dfe2e3aSImre Deak 		spin_unlock_irq(&dev_priv->irq_lock);
32349dfe2e3aSImre Deak 		return;
32359dfe2e3aSImre Deak 	}
32369dfe2e3aSImre Deak 
32376831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
3238b16b2a2fSPaulo Zanoni 		GEN8_IRQ_RESET_NDX(uncore, DE_PIPE, pipe);
32399dfe2e3aSImre Deak 
3240aae8ba84SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3241aae8ba84SVille Syrjälä 
3242aae8ba84SVille Syrjälä 	/* make sure we're done processing display irqs */
3243315ca4c4SVille Syrjälä 	intel_synchronize_irq(dev_priv);
3244aae8ba84SVille Syrjälä }
3245aae8ba84SVille Syrjälä 
3246b318b824SVille Syrjälä static void cherryview_irq_reset(struct drm_i915_private *dev_priv)
324743f328d7SVille Syrjälä {
3248b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
324943f328d7SVille Syrjälä 
32502939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, GEN8_MASTER_IRQ, 0);
32512939eb06SJani Nikula 	intel_uncore_posting_read(&dev_priv->uncore, GEN8_MASTER_IRQ);
325243f328d7SVille Syrjälä 
3253cf1c97dcSAndi Shyti 	gen8_gt_irq_reset(&dev_priv->gt);
325443f328d7SVille Syrjälä 
3255b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN8_PCU_);
325643f328d7SVille Syrjälä 
3257ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
32589918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
325970591a41SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3260ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
326143f328d7SVille Syrjälä }
326243f328d7SVille Syrjälä 
32632ea63927SVille Syrjälä static u32 ibx_hotplug_enables(struct drm_i915_private *i915,
32642ea63927SVille Syrjälä 			       enum hpd_pin pin)
32652ea63927SVille Syrjälä {
32662ea63927SVille Syrjälä 	switch (pin) {
32672ea63927SVille Syrjälä 	case HPD_PORT_A:
32682ea63927SVille Syrjälä 		/*
32692ea63927SVille Syrjälä 		 * When CPU and PCH are on the same package, port A
32702ea63927SVille Syrjälä 		 * HPD must be enabled in both north and south.
32712ea63927SVille Syrjälä 		 */
32722ea63927SVille Syrjälä 		return HAS_PCH_LPT_LP(i915) ?
32732ea63927SVille Syrjälä 			PORTA_HOTPLUG_ENABLE : 0;
32742ea63927SVille Syrjälä 	case HPD_PORT_B:
32752ea63927SVille Syrjälä 		return PORTB_HOTPLUG_ENABLE |
32762ea63927SVille Syrjälä 			PORTB_PULSE_DURATION_2ms;
32772ea63927SVille Syrjälä 	case HPD_PORT_C:
32782ea63927SVille Syrjälä 		return PORTC_HOTPLUG_ENABLE |
32792ea63927SVille Syrjälä 			PORTC_PULSE_DURATION_2ms;
32802ea63927SVille Syrjälä 	case HPD_PORT_D:
32812ea63927SVille Syrjälä 		return PORTD_HOTPLUG_ENABLE |
32822ea63927SVille Syrjälä 			PORTD_PULSE_DURATION_2ms;
32832ea63927SVille Syrjälä 	default:
32842ea63927SVille Syrjälä 		return 0;
32852ea63927SVille Syrjälä 	}
32862ea63927SVille Syrjälä }
32872ea63927SVille Syrjälä 
32881a56b1a2SImre Deak static void ibx_hpd_detection_setup(struct drm_i915_private *dev_priv)
32891a56b1a2SImre Deak {
32901a56b1a2SImre Deak 	u32 hotplug;
32911a56b1a2SImre Deak 
32921a56b1a2SImre Deak 	/*
32931a56b1a2SImre Deak 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
32941a56b1a2SImre Deak 	 * duration to 2ms (which is the minimum in the Display Port spec).
32951a56b1a2SImre Deak 	 * The pulse duration bits are reserved on LPT+.
32961a56b1a2SImre Deak 	 */
32972939eb06SJani Nikula 	hotplug = intel_uncore_read(&dev_priv->uncore, PCH_PORT_HOTPLUG);
32982ea63927SVille Syrjälä 	hotplug &= ~(PORTA_HOTPLUG_ENABLE |
32992ea63927SVille Syrjälä 		     PORTB_HOTPLUG_ENABLE |
33002ea63927SVille Syrjälä 		     PORTC_HOTPLUG_ENABLE |
33012ea63927SVille Syrjälä 		     PORTD_HOTPLUG_ENABLE |
33022ea63927SVille Syrjälä 		     PORTB_PULSE_DURATION_MASK |
33031a56b1a2SImre Deak 		     PORTC_PULSE_DURATION_MASK |
33041a56b1a2SImre Deak 		     PORTD_PULSE_DURATION_MASK);
33052ea63927SVille Syrjälä 	hotplug |= intel_hpd_hotplug_enables(dev_priv, ibx_hotplug_enables);
33062939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, PCH_PORT_HOTPLUG, hotplug);
33071a56b1a2SImre Deak }
33081a56b1a2SImre Deak 
330991d14251STvrtko Ursulin static void ibx_hpd_irq_setup(struct drm_i915_private *dev_priv)
331082a28bcfSDaniel Vetter {
33111a56b1a2SImre Deak 	u32 hotplug_irqs, enabled_irqs;
331282a28bcfSDaniel Vetter 
33130398993bSVille Syrjälä 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, dev_priv->hotplug.pch_hpd);
33146d3144ebSVille Syrjälä 	hotplug_irqs = intel_hpd_hotplug_irqs(dev_priv, dev_priv->hotplug.pch_hpd);
331582a28bcfSDaniel Vetter 
3316fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
331782a28bcfSDaniel Vetter 
33181a56b1a2SImre Deak 	ibx_hpd_detection_setup(dev_priv);
33196dbf30ceSVille Syrjälä }
332026951cafSXiong Zhang 
33212ea63927SVille Syrjälä static u32 icp_ddi_hotplug_enables(struct drm_i915_private *i915,
33222ea63927SVille Syrjälä 				   enum hpd_pin pin)
33232ea63927SVille Syrjälä {
33242ea63927SVille Syrjälä 	switch (pin) {
33252ea63927SVille Syrjälä 	case HPD_PORT_A:
33262ea63927SVille Syrjälä 	case HPD_PORT_B:
33272ea63927SVille Syrjälä 	case HPD_PORT_C:
33282ea63927SVille Syrjälä 	case HPD_PORT_D:
33292ea63927SVille Syrjälä 		return SHOTPLUG_CTL_DDI_HPD_ENABLE(pin);
33302ea63927SVille Syrjälä 	default:
33312ea63927SVille Syrjälä 		return 0;
33322ea63927SVille Syrjälä 	}
33332ea63927SVille Syrjälä }
33342ea63927SVille Syrjälä 
33352ea63927SVille Syrjälä static u32 icp_tc_hotplug_enables(struct drm_i915_private *i915,
33362ea63927SVille Syrjälä 				  enum hpd_pin pin)
33372ea63927SVille Syrjälä {
33382ea63927SVille Syrjälä 	switch (pin) {
33392ea63927SVille Syrjälä 	case HPD_PORT_TC1:
33402ea63927SVille Syrjälä 	case HPD_PORT_TC2:
33412ea63927SVille Syrjälä 	case HPD_PORT_TC3:
33422ea63927SVille Syrjälä 	case HPD_PORT_TC4:
33432ea63927SVille Syrjälä 	case HPD_PORT_TC5:
33442ea63927SVille Syrjälä 	case HPD_PORT_TC6:
33452ea63927SVille Syrjälä 		return ICP_TC_HPD_ENABLE(pin);
33462ea63927SVille Syrjälä 	default:
33472ea63927SVille Syrjälä 		return 0;
33482ea63927SVille Syrjälä 	}
33492ea63927SVille Syrjälä }
33502ea63927SVille Syrjälä 
33512ea63927SVille Syrjälä static void icp_ddi_hpd_detection_setup(struct drm_i915_private *dev_priv)
335231604222SAnusha Srivatsa {
335331604222SAnusha Srivatsa 	u32 hotplug;
335431604222SAnusha Srivatsa 
33552939eb06SJani Nikula 	hotplug = intel_uncore_read(&dev_priv->uncore, SHOTPLUG_CTL_DDI);
33562ea63927SVille Syrjälä 	hotplug &= ~(SHOTPLUG_CTL_DDI_HPD_ENABLE(HPD_PORT_A) |
33572ea63927SVille Syrjälä 		     SHOTPLUG_CTL_DDI_HPD_ENABLE(HPD_PORT_B) |
33582ea63927SVille Syrjälä 		     SHOTPLUG_CTL_DDI_HPD_ENABLE(HPD_PORT_C) |
33592ea63927SVille Syrjälä 		     SHOTPLUG_CTL_DDI_HPD_ENABLE(HPD_PORT_D));
33602ea63927SVille Syrjälä 	hotplug |= intel_hpd_hotplug_enables(dev_priv, icp_ddi_hotplug_enables);
33612939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, SHOTPLUG_CTL_DDI, hotplug);
336231604222SAnusha Srivatsa }
3363815f4ef2SVille Syrjälä 
33642ea63927SVille Syrjälä static void icp_tc_hpd_detection_setup(struct drm_i915_private *dev_priv)
3365815f4ef2SVille Syrjälä {
3366815f4ef2SVille Syrjälä 	u32 hotplug;
3367815f4ef2SVille Syrjälä 
33682939eb06SJani Nikula 	hotplug = intel_uncore_read(&dev_priv->uncore, SHOTPLUG_CTL_TC);
33692ea63927SVille Syrjälä 	hotplug &= ~(ICP_TC_HPD_ENABLE(HPD_PORT_TC1) |
33702ea63927SVille Syrjälä 		     ICP_TC_HPD_ENABLE(HPD_PORT_TC2) |
33712ea63927SVille Syrjälä 		     ICP_TC_HPD_ENABLE(HPD_PORT_TC3) |
33722ea63927SVille Syrjälä 		     ICP_TC_HPD_ENABLE(HPD_PORT_TC4) |
33732ea63927SVille Syrjälä 		     ICP_TC_HPD_ENABLE(HPD_PORT_TC5) |
33742ea63927SVille Syrjälä 		     ICP_TC_HPD_ENABLE(HPD_PORT_TC6));
33752ea63927SVille Syrjälä 	hotplug |= intel_hpd_hotplug_enables(dev_priv, icp_tc_hotplug_enables);
33762939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, SHOTPLUG_CTL_TC, hotplug);
33778ef7e340SMatt Roper }
337831604222SAnusha Srivatsa 
33792ea63927SVille Syrjälä static void icp_hpd_irq_setup(struct drm_i915_private *dev_priv)
338031604222SAnusha Srivatsa {
338131604222SAnusha Srivatsa 	u32 hotplug_irqs, enabled_irqs;
338231604222SAnusha Srivatsa 
33830398993bSVille Syrjälä 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, dev_priv->hotplug.pch_hpd);
33846d3144ebSVille Syrjälä 	hotplug_irqs = intel_hpd_hotplug_irqs(dev_priv, dev_priv->hotplug.pch_hpd);
338531604222SAnusha Srivatsa 
3386f619e516SAnusha Srivatsa 	if (INTEL_PCH_TYPE(dev_priv) <= PCH_TGP)
33872939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, SHPD_FILTER_CNT, SHPD_FILTER_CNT_500_ADJ);
3388f49108d0SMatt Roper 
338931604222SAnusha Srivatsa 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
339031604222SAnusha Srivatsa 
33912ea63927SVille Syrjälä 	icp_ddi_hpd_detection_setup(dev_priv);
33922ea63927SVille Syrjälä 	icp_tc_hpd_detection_setup(dev_priv);
339352dfdba0SLucas De Marchi }
339452dfdba0SLucas De Marchi 
33952ea63927SVille Syrjälä static u32 gen11_hotplug_enables(struct drm_i915_private *i915,
33962ea63927SVille Syrjälä 				 enum hpd_pin pin)
33978ef7e340SMatt Roper {
33982ea63927SVille Syrjälä 	switch (pin) {
33992ea63927SVille Syrjälä 	case HPD_PORT_TC1:
34002ea63927SVille Syrjälä 	case HPD_PORT_TC2:
34012ea63927SVille Syrjälä 	case HPD_PORT_TC3:
34022ea63927SVille Syrjälä 	case HPD_PORT_TC4:
34032ea63927SVille Syrjälä 	case HPD_PORT_TC5:
34042ea63927SVille Syrjälä 	case HPD_PORT_TC6:
34052ea63927SVille Syrjälä 		return GEN11_HOTPLUG_CTL_ENABLE(pin);
34062ea63927SVille Syrjälä 	default:
34072ea63927SVille Syrjälä 		return 0;
340831604222SAnusha Srivatsa 	}
3409943682e3SMatt Roper }
3410943682e3SMatt Roper 
3411229f31e2SLucas De Marchi static void dg1_hpd_irq_setup(struct drm_i915_private *dev_priv)
3412229f31e2SLucas De Marchi {
3413b18c1eb9SClinton A Taylor 	u32 val;
3414b18c1eb9SClinton A Taylor 
34152939eb06SJani Nikula 	val = intel_uncore_read(&dev_priv->uncore, SOUTH_CHICKEN1);
3416b18c1eb9SClinton A Taylor 	val |= (INVERT_DDIA_HPD |
3417b18c1eb9SClinton A Taylor 		INVERT_DDIB_HPD |
3418b18c1eb9SClinton A Taylor 		INVERT_DDIC_HPD |
3419b18c1eb9SClinton A Taylor 		INVERT_DDID_HPD);
34202939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, SOUTH_CHICKEN1, val);
3421b18c1eb9SClinton A Taylor 
34222ea63927SVille Syrjälä 	icp_hpd_irq_setup(dev_priv);
3423229f31e2SLucas De Marchi }
3424229f31e2SLucas De Marchi 
342552c7f5f1SVille Syrjälä static void gen11_tc_hpd_detection_setup(struct drm_i915_private *dev_priv)
3426121e758eSDhinakaran Pandiyan {
3427121e758eSDhinakaran Pandiyan 	u32 hotplug;
3428121e758eSDhinakaran Pandiyan 
34292939eb06SJani Nikula 	hotplug = intel_uncore_read(&dev_priv->uncore, GEN11_TC_HOTPLUG_CTL);
34302ea63927SVille Syrjälä 	hotplug &= ~(GEN11_HOTPLUG_CTL_ENABLE(HPD_PORT_TC1) |
34315b76e860SVille Syrjälä 		     GEN11_HOTPLUG_CTL_ENABLE(HPD_PORT_TC2) |
34325b76e860SVille Syrjälä 		     GEN11_HOTPLUG_CTL_ENABLE(HPD_PORT_TC3) |
34335b76e860SVille Syrjälä 		     GEN11_HOTPLUG_CTL_ENABLE(HPD_PORT_TC4) |
34345b76e860SVille Syrjälä 		     GEN11_HOTPLUG_CTL_ENABLE(HPD_PORT_TC5) |
34352ea63927SVille Syrjälä 		     GEN11_HOTPLUG_CTL_ENABLE(HPD_PORT_TC6));
34362ea63927SVille Syrjälä 	hotplug |= intel_hpd_hotplug_enables(dev_priv, gen11_hotplug_enables);
34372939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, GEN11_TC_HOTPLUG_CTL, hotplug);
343852c7f5f1SVille Syrjälä }
343952c7f5f1SVille Syrjälä 
344052c7f5f1SVille Syrjälä static void gen11_tbt_hpd_detection_setup(struct drm_i915_private *dev_priv)
344152c7f5f1SVille Syrjälä {
344252c7f5f1SVille Syrjälä 	u32 hotplug;
3443b796b971SDhinakaran Pandiyan 
34442939eb06SJani Nikula 	hotplug = intel_uncore_read(&dev_priv->uncore, GEN11_TBT_HOTPLUG_CTL);
34452ea63927SVille Syrjälä 	hotplug &= ~(GEN11_HOTPLUG_CTL_ENABLE(HPD_PORT_TC1) |
34465b76e860SVille Syrjälä 		     GEN11_HOTPLUG_CTL_ENABLE(HPD_PORT_TC2) |
34475b76e860SVille Syrjälä 		     GEN11_HOTPLUG_CTL_ENABLE(HPD_PORT_TC3) |
34485b76e860SVille Syrjälä 		     GEN11_HOTPLUG_CTL_ENABLE(HPD_PORT_TC4) |
34495b76e860SVille Syrjälä 		     GEN11_HOTPLUG_CTL_ENABLE(HPD_PORT_TC5) |
34502ea63927SVille Syrjälä 		     GEN11_HOTPLUG_CTL_ENABLE(HPD_PORT_TC6));
34512ea63927SVille Syrjälä 	hotplug |= intel_hpd_hotplug_enables(dev_priv, gen11_hotplug_enables);
34522939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, GEN11_TBT_HOTPLUG_CTL, hotplug);
3453121e758eSDhinakaran Pandiyan }
3454121e758eSDhinakaran Pandiyan 
3455121e758eSDhinakaran Pandiyan static void gen11_hpd_irq_setup(struct drm_i915_private *dev_priv)
3456121e758eSDhinakaran Pandiyan {
3457121e758eSDhinakaran Pandiyan 	u32 hotplug_irqs, enabled_irqs;
3458121e758eSDhinakaran Pandiyan 	u32 val;
3459121e758eSDhinakaran Pandiyan 
34600398993bSVille Syrjälä 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, dev_priv->hotplug.hpd);
34616d3144ebSVille Syrjälä 	hotplug_irqs = intel_hpd_hotplug_irqs(dev_priv, dev_priv->hotplug.hpd);
3462121e758eSDhinakaran Pandiyan 
34632939eb06SJani Nikula 	val = intel_uncore_read(&dev_priv->uncore, GEN11_DE_HPD_IMR);
3464121e758eSDhinakaran Pandiyan 	val &= ~hotplug_irqs;
3465587a87b9SImre Deak 	val |= ~enabled_irqs & hotplug_irqs;
34662939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, GEN11_DE_HPD_IMR, val);
34672939eb06SJani Nikula 	intel_uncore_posting_read(&dev_priv->uncore, GEN11_DE_HPD_IMR);
3468121e758eSDhinakaran Pandiyan 
346952c7f5f1SVille Syrjälä 	gen11_tc_hpd_detection_setup(dev_priv);
347052c7f5f1SVille Syrjälä 	gen11_tbt_hpd_detection_setup(dev_priv);
347131604222SAnusha Srivatsa 
34722ea63927SVille Syrjälä 	if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
34732ea63927SVille Syrjälä 		icp_hpd_irq_setup(dev_priv);
34742ea63927SVille Syrjälä }
34752ea63927SVille Syrjälä 
34762ea63927SVille Syrjälä static u32 spt_hotplug_enables(struct drm_i915_private *i915,
34772ea63927SVille Syrjälä 			       enum hpd_pin pin)
34782ea63927SVille Syrjälä {
34792ea63927SVille Syrjälä 	switch (pin) {
34802ea63927SVille Syrjälä 	case HPD_PORT_A:
34812ea63927SVille Syrjälä 		return PORTA_HOTPLUG_ENABLE;
34822ea63927SVille Syrjälä 	case HPD_PORT_B:
34832ea63927SVille Syrjälä 		return PORTB_HOTPLUG_ENABLE;
34842ea63927SVille Syrjälä 	case HPD_PORT_C:
34852ea63927SVille Syrjälä 		return PORTC_HOTPLUG_ENABLE;
34862ea63927SVille Syrjälä 	case HPD_PORT_D:
34872ea63927SVille Syrjälä 		return PORTD_HOTPLUG_ENABLE;
34882ea63927SVille Syrjälä 	default:
34892ea63927SVille Syrjälä 		return 0;
34902ea63927SVille Syrjälä 	}
34912ea63927SVille Syrjälä }
34922ea63927SVille Syrjälä 
34932ea63927SVille Syrjälä static u32 spt_hotplug2_enables(struct drm_i915_private *i915,
34942ea63927SVille Syrjälä 				enum hpd_pin pin)
34952ea63927SVille Syrjälä {
34962ea63927SVille Syrjälä 	switch (pin) {
34972ea63927SVille Syrjälä 	case HPD_PORT_E:
34982ea63927SVille Syrjälä 		return PORTE_HOTPLUG_ENABLE;
34992ea63927SVille Syrjälä 	default:
35002ea63927SVille Syrjälä 		return 0;
35012ea63927SVille Syrjälä 	}
3502121e758eSDhinakaran Pandiyan }
3503121e758eSDhinakaran Pandiyan 
35042a57d9ccSImre Deak static void spt_hpd_detection_setup(struct drm_i915_private *dev_priv)
35052a57d9ccSImre Deak {
35063b92e263SRodrigo Vivi 	u32 val, hotplug;
35073b92e263SRodrigo Vivi 
35083b92e263SRodrigo Vivi 	/* Display WA #1179 WaHardHangonHotPlug: cnp */
35093b92e263SRodrigo Vivi 	if (HAS_PCH_CNP(dev_priv)) {
35102939eb06SJani Nikula 		val = intel_uncore_read(&dev_priv->uncore, SOUTH_CHICKEN1);
35113b92e263SRodrigo Vivi 		val &= ~CHASSIS_CLK_REQ_DURATION_MASK;
35123b92e263SRodrigo Vivi 		val |= CHASSIS_CLK_REQ_DURATION(0xf);
35132939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, SOUTH_CHICKEN1, val);
35143b92e263SRodrigo Vivi 	}
35152a57d9ccSImre Deak 
35162a57d9ccSImre Deak 	/* Enable digital hotplug on the PCH */
35172939eb06SJani Nikula 	hotplug = intel_uncore_read(&dev_priv->uncore, PCH_PORT_HOTPLUG);
35182ea63927SVille Syrjälä 	hotplug &= ~(PORTA_HOTPLUG_ENABLE |
35192a57d9ccSImre Deak 		     PORTB_HOTPLUG_ENABLE |
35202a57d9ccSImre Deak 		     PORTC_HOTPLUG_ENABLE |
35212ea63927SVille Syrjälä 		     PORTD_HOTPLUG_ENABLE);
35222ea63927SVille Syrjälä 	hotplug |= intel_hpd_hotplug_enables(dev_priv, spt_hotplug_enables);
35232939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, PCH_PORT_HOTPLUG, hotplug);
35242a57d9ccSImre Deak 
35252939eb06SJani Nikula 	hotplug = intel_uncore_read(&dev_priv->uncore, PCH_PORT_HOTPLUG2);
35262ea63927SVille Syrjälä 	hotplug &= ~PORTE_HOTPLUG_ENABLE;
35272ea63927SVille Syrjälä 	hotplug |= intel_hpd_hotplug_enables(dev_priv, spt_hotplug2_enables);
35282939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, PCH_PORT_HOTPLUG2, hotplug);
35292a57d9ccSImre Deak }
35302a57d9ccSImre Deak 
353191d14251STvrtko Ursulin static void spt_hpd_irq_setup(struct drm_i915_private *dev_priv)
35326dbf30ceSVille Syrjälä {
35332a57d9ccSImre Deak 	u32 hotplug_irqs, enabled_irqs;
35346dbf30ceSVille Syrjälä 
3535f49108d0SMatt Roper 	if (INTEL_PCH_TYPE(dev_priv) >= PCH_CNP)
35362939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, SHPD_FILTER_CNT, SHPD_FILTER_CNT_500_ADJ);
3537f49108d0SMatt Roper 
35380398993bSVille Syrjälä 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, dev_priv->hotplug.pch_hpd);
35396d3144ebSVille Syrjälä 	hotplug_irqs = intel_hpd_hotplug_irqs(dev_priv, dev_priv->hotplug.pch_hpd);
35406dbf30ceSVille Syrjälä 
35416dbf30ceSVille Syrjälä 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
35426dbf30ceSVille Syrjälä 
35432a57d9ccSImre Deak 	spt_hpd_detection_setup(dev_priv);
354426951cafSXiong Zhang }
35457fe0b973SKeith Packard 
35462ea63927SVille Syrjälä static u32 ilk_hotplug_enables(struct drm_i915_private *i915,
35472ea63927SVille Syrjälä 			       enum hpd_pin pin)
35482ea63927SVille Syrjälä {
35492ea63927SVille Syrjälä 	switch (pin) {
35502ea63927SVille Syrjälä 	case HPD_PORT_A:
35512ea63927SVille Syrjälä 		return DIGITAL_PORTA_HOTPLUG_ENABLE |
35522ea63927SVille Syrjälä 			DIGITAL_PORTA_PULSE_DURATION_2ms;
35532ea63927SVille Syrjälä 	default:
35542ea63927SVille Syrjälä 		return 0;
35552ea63927SVille Syrjälä 	}
35562ea63927SVille Syrjälä }
35572ea63927SVille Syrjälä 
35581a56b1a2SImre Deak static void ilk_hpd_detection_setup(struct drm_i915_private *dev_priv)
35591a56b1a2SImre Deak {
35601a56b1a2SImre Deak 	u32 hotplug;
35611a56b1a2SImre Deak 
35621a56b1a2SImre Deak 	/*
35631a56b1a2SImre Deak 	 * Enable digital hotplug on the CPU, and configure the DP short pulse
35641a56b1a2SImre Deak 	 * duration to 2ms (which is the minimum in the Display Port spec)
35651a56b1a2SImre Deak 	 * The pulse duration bits are reserved on HSW+.
35661a56b1a2SImre Deak 	 */
35672939eb06SJani Nikula 	hotplug = intel_uncore_read(&dev_priv->uncore, DIGITAL_PORT_HOTPLUG_CNTRL);
35682ea63927SVille Syrjälä 	hotplug &= ~(DIGITAL_PORTA_HOTPLUG_ENABLE |
35692ea63927SVille Syrjälä 		     DIGITAL_PORTA_PULSE_DURATION_MASK);
35702ea63927SVille Syrjälä 	hotplug |= intel_hpd_hotplug_enables(dev_priv, ilk_hotplug_enables);
35712939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, DIGITAL_PORT_HOTPLUG_CNTRL, hotplug);
35721a56b1a2SImre Deak }
35731a56b1a2SImre Deak 
357491d14251STvrtko Ursulin static void ilk_hpd_irq_setup(struct drm_i915_private *dev_priv)
3575e4ce95aaSVille Syrjälä {
35761a56b1a2SImre Deak 	u32 hotplug_irqs, enabled_irqs;
3577e4ce95aaSVille Syrjälä 
35780398993bSVille Syrjälä 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, dev_priv->hotplug.hpd);
35796d3144ebSVille Syrjälä 	hotplug_irqs = intel_hpd_hotplug_irqs(dev_priv, dev_priv->hotplug.hpd);
35803a3b3c7dSVille Syrjälä 
3581373abf1aSMatt Roper 	if (DISPLAY_VER(dev_priv) >= 8)
35823a3b3c7dSVille Syrjälä 		bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
35836d3144ebSVille Syrjälä 	else
35843a3b3c7dSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
3585e4ce95aaSVille Syrjälä 
35861a56b1a2SImre Deak 	ilk_hpd_detection_setup(dev_priv);
3587e4ce95aaSVille Syrjälä 
358891d14251STvrtko Ursulin 	ibx_hpd_irq_setup(dev_priv);
3589e4ce95aaSVille Syrjälä }
3590e4ce95aaSVille Syrjälä 
35912ea63927SVille Syrjälä static u32 bxt_hotplug_enables(struct drm_i915_private *i915,
35922ea63927SVille Syrjälä 			       enum hpd_pin pin)
35932ea63927SVille Syrjälä {
35942ea63927SVille Syrjälä 	u32 hotplug;
35952ea63927SVille Syrjälä 
35962ea63927SVille Syrjälä 	switch (pin) {
35972ea63927SVille Syrjälä 	case HPD_PORT_A:
35982ea63927SVille Syrjälä 		hotplug = PORTA_HOTPLUG_ENABLE;
35992ea63927SVille Syrjälä 		if (intel_bios_is_port_hpd_inverted(i915, PORT_A))
36002ea63927SVille Syrjälä 			hotplug |= BXT_DDIA_HPD_INVERT;
36012ea63927SVille Syrjälä 		return hotplug;
36022ea63927SVille Syrjälä 	case HPD_PORT_B:
36032ea63927SVille Syrjälä 		hotplug = PORTB_HOTPLUG_ENABLE;
36042ea63927SVille Syrjälä 		if (intel_bios_is_port_hpd_inverted(i915, PORT_B))
36052ea63927SVille Syrjälä 			hotplug |= BXT_DDIB_HPD_INVERT;
36062ea63927SVille Syrjälä 		return hotplug;
36072ea63927SVille Syrjälä 	case HPD_PORT_C:
36082ea63927SVille Syrjälä 		hotplug = PORTC_HOTPLUG_ENABLE;
36092ea63927SVille Syrjälä 		if (intel_bios_is_port_hpd_inverted(i915, PORT_C))
36102ea63927SVille Syrjälä 			hotplug |= BXT_DDIC_HPD_INVERT;
36112ea63927SVille Syrjälä 		return hotplug;
36122ea63927SVille Syrjälä 	default:
36132ea63927SVille Syrjälä 		return 0;
36142ea63927SVille Syrjälä 	}
36152ea63927SVille Syrjälä }
36162ea63927SVille Syrjälä 
36172ea63927SVille Syrjälä static void bxt_hpd_detection_setup(struct drm_i915_private *dev_priv)
3618e0a20ad7SShashank Sharma {
36192a57d9ccSImre Deak 	u32 hotplug;
3620e0a20ad7SShashank Sharma 
36212939eb06SJani Nikula 	hotplug = intel_uncore_read(&dev_priv->uncore, PCH_PORT_HOTPLUG);
36222ea63927SVille Syrjälä 	hotplug &= ~(PORTA_HOTPLUG_ENABLE |
36232a57d9ccSImre Deak 		     PORTB_HOTPLUG_ENABLE |
36242ea63927SVille Syrjälä 		     PORTC_HOTPLUG_ENABLE |
36252ea63927SVille Syrjälä 		     BXT_DDIA_HPD_INVERT |
36262ea63927SVille Syrjälä 		     BXT_DDIB_HPD_INVERT |
36272ea63927SVille Syrjälä 		     BXT_DDIC_HPD_INVERT);
36282ea63927SVille Syrjälä 	hotplug |= intel_hpd_hotplug_enables(dev_priv, bxt_hotplug_enables);
36292939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, PCH_PORT_HOTPLUG, hotplug);
3630e0a20ad7SShashank Sharma }
3631e0a20ad7SShashank Sharma 
36322a57d9ccSImre Deak static void bxt_hpd_irq_setup(struct drm_i915_private *dev_priv)
36332a57d9ccSImre Deak {
36342a57d9ccSImre Deak 	u32 hotplug_irqs, enabled_irqs;
36352a57d9ccSImre Deak 
36360398993bSVille Syrjälä 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, dev_priv->hotplug.hpd);
36376d3144ebSVille Syrjälä 	hotplug_irqs = intel_hpd_hotplug_irqs(dev_priv, dev_priv->hotplug.hpd);
36382a57d9ccSImre Deak 
36392a57d9ccSImre Deak 	bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
36402a57d9ccSImre Deak 
36412ea63927SVille Syrjälä 	bxt_hpd_detection_setup(dev_priv);
36422a57d9ccSImre Deak }
36432a57d9ccSImre Deak 
3644a0a6d8cbSVille Syrjälä /*
3645a0a6d8cbSVille Syrjälä  * SDEIER is also touched by the interrupt handler to work around missed PCH
3646a0a6d8cbSVille Syrjälä  * interrupts. Hence we can't update it after the interrupt handler is enabled -
3647a0a6d8cbSVille Syrjälä  * instead we unconditionally enable all PCH interrupt sources here, but then
3648a0a6d8cbSVille Syrjälä  * only unmask them as needed with SDEIMR.
3649a0a6d8cbSVille Syrjälä  *
3650a0a6d8cbSVille Syrjälä  * Note that we currently do this after installing the interrupt handler,
3651a0a6d8cbSVille Syrjälä  * but before we enable the master interrupt. That should be sufficient
3652a0a6d8cbSVille Syrjälä  * to avoid races with the irq handler, assuming we have MSI. Shared legacy
3653a0a6d8cbSVille Syrjälä  * interrupts could still race.
3654a0a6d8cbSVille Syrjälä  */
3655b318b824SVille Syrjälä static void ibx_irq_postinstall(struct drm_i915_private *dev_priv)
3656d46da437SPaulo Zanoni {
3657a0a6d8cbSVille Syrjälä 	struct intel_uncore *uncore = &dev_priv->uncore;
365882a28bcfSDaniel Vetter 	u32 mask;
3659d46da437SPaulo Zanoni 
36606e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
3661692a04cfSDaniel Vetter 		return;
3662692a04cfSDaniel Vetter 
36636e266956STvrtko Ursulin 	if (HAS_PCH_IBX(dev_priv))
36645c673b60SDaniel Vetter 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
36654ebc6509SDhinakaran Pandiyan 	else if (HAS_PCH_CPT(dev_priv) || HAS_PCH_LPT(dev_priv))
36665c673b60SDaniel Vetter 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
36674ebc6509SDhinakaran Pandiyan 	else
36684ebc6509SDhinakaran Pandiyan 		mask = SDE_GMBUS_CPT;
36698664281bSPaulo Zanoni 
3670a0a6d8cbSVille Syrjälä 	GEN3_IRQ_INIT(uncore, SDE, ~mask, 0xffffffff);
3671d46da437SPaulo Zanoni }
3672d46da437SPaulo Zanoni 
36739eae5e27SLucas De Marchi static void ilk_irq_postinstall(struct drm_i915_private *dev_priv)
3674036a4a7dSZhenyu Wang {
3675b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
36768e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
36778e76f8dcSPaulo Zanoni 
3678651e7d48SLucas De Marchi 	if (GRAPHICS_VER(dev_priv) >= 7) {
36798e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
3680842ebf7aSVille Syrjälä 				DE_PCH_EVENT_IVB | DE_AUX_CHANNEL_A_IVB);
36818e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
368223bb4cb5SVille Syrjälä 			      DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB |
36832a636e24SVille Syrjälä 			      DE_PLANE_FLIP_DONE_IVB(PLANE_C) |
36842a636e24SVille Syrjälä 			      DE_PLANE_FLIP_DONE_IVB(PLANE_B) |
36852a636e24SVille Syrjälä 			      DE_PLANE_FLIP_DONE_IVB(PLANE_A) |
368623bb4cb5SVille Syrjälä 			      DE_DP_A_HOTPLUG_IVB);
36878e76f8dcSPaulo Zanoni 	} else {
36888e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3689842ebf7aSVille Syrjälä 				DE_AUX_CHANNEL_A | DE_PIPEB_CRC_DONE |
3690842ebf7aSVille Syrjälä 				DE_PIPEA_CRC_DONE | DE_POISON);
3691c6073d4cSVille Syrjälä 		extra_mask = (DE_PIPEA_VBLANK | DE_PIPEB_VBLANK |
3692e4ce95aaSVille Syrjälä 			      DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN |
36934bb18054SVille Syrjälä 			      DE_PLANE_FLIP_DONE(PLANE_A) |
36944bb18054SVille Syrjälä 			      DE_PLANE_FLIP_DONE(PLANE_B) |
3695e4ce95aaSVille Syrjälä 			      DE_DP_A_HOTPLUG);
36968e76f8dcSPaulo Zanoni 	}
3697036a4a7dSZhenyu Wang 
3698fc340442SDaniel Vetter 	if (IS_HASWELL(dev_priv)) {
3699b16b2a2fSPaulo Zanoni 		gen3_assert_iir_is_zero(uncore, EDP_PSR_IIR);
3700fc340442SDaniel Vetter 		display_mask |= DE_EDP_PSR_INT_HSW;
3701fc340442SDaniel Vetter 	}
3702fc340442SDaniel Vetter 
3703c6073d4cSVille Syrjälä 	if (IS_IRONLAKE_M(dev_priv))
3704c6073d4cSVille Syrjälä 		extra_mask |= DE_PCU_EVENT;
3705c6073d4cSVille Syrjälä 
37061ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3707036a4a7dSZhenyu Wang 
3708a0a6d8cbSVille Syrjälä 	ibx_irq_postinstall(dev_priv);
3709622364b6SPaulo Zanoni 
3710a9922912SVille Syrjälä 	gen5_gt_irq_postinstall(&dev_priv->gt);
3711a9922912SVille Syrjälä 
3712b16b2a2fSPaulo Zanoni 	GEN3_IRQ_INIT(uncore, DE, dev_priv->irq_mask,
3713b16b2a2fSPaulo Zanoni 		      display_mask | extra_mask);
3714036a4a7dSZhenyu Wang }
3715036a4a7dSZhenyu Wang 
3716f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3717f8b79e58SImre Deak {
371867520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
3719f8b79e58SImre Deak 
3720f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3721f8b79e58SImre Deak 		return;
3722f8b79e58SImre Deak 
3723f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3724f8b79e58SImre Deak 
3725d6c69803SVille Syrjälä 	if (intel_irqs_enabled(dev_priv)) {
3726d6c69803SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3727ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3728f8b79e58SImre Deak 	}
3729d6c69803SVille Syrjälä }
3730f8b79e58SImre Deak 
3731f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3732f8b79e58SImre Deak {
373367520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
3734f8b79e58SImre Deak 
3735f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3736f8b79e58SImre Deak 		return;
3737f8b79e58SImre Deak 
3738f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3739f8b79e58SImre Deak 
3740950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3741ad22d106SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3742f8b79e58SImre Deak }
3743f8b79e58SImre Deak 
37440e6c9a9eSVille Syrjälä 
3745b318b824SVille Syrjälä static void valleyview_irq_postinstall(struct drm_i915_private *dev_priv)
37460e6c9a9eSVille Syrjälä {
3747cf1c97dcSAndi Shyti 	gen5_gt_irq_postinstall(&dev_priv->gt);
37487e231dbeSJesse Barnes 
3749ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
37509918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3751ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3752ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3753ad22d106SVille Syrjälä 
37542939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
37552939eb06SJani Nikula 	intel_uncore_posting_read(&dev_priv->uncore, VLV_MASTER_IER);
375620afbda2SDaniel Vetter }
375720afbda2SDaniel Vetter 
3758abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3759abd58f01SBen Widawsky {
3760b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
3761b16b2a2fSPaulo Zanoni 
3762869129eeSMatt Roper 	u32 de_pipe_masked = gen8_de_pipe_fault_mask(dev_priv) |
3763869129eeSMatt Roper 		GEN8_PIPE_CDCLK_CRC_DONE;
3764a9c287c9SJani Nikula 	u32 de_pipe_enables;
3765054318c7SImre Deak 	u32 de_port_masked = gen8_de_port_aux_mask(dev_priv);
37663a3b3c7dSVille Syrjälä 	u32 de_port_enables;
3767df0d28c1SDhinakaran Pandiyan 	u32 de_misc_masked = GEN8_DE_EDP_PSR;
3768562ddcb7SMatt Roper 	u32 trans_mask = BIT(TRANSCODER_A) | BIT(TRANSCODER_B) |
3769562ddcb7SMatt Roper 		BIT(TRANSCODER_C) | BIT(TRANSCODER_D);
37703a3b3c7dSVille Syrjälä 	enum pipe pipe;
3771770de83dSDamien Lespiau 
3772a844cfbeSJosé Roberto de Souza 	if (!HAS_DISPLAY(dev_priv))
3773a844cfbeSJosé Roberto de Souza 		return;
3774a844cfbeSJosé Roberto de Souza 
3775373abf1aSMatt Roper 	if (DISPLAY_VER(dev_priv) <= 10)
3776df0d28c1SDhinakaran Pandiyan 		de_misc_masked |= GEN8_DE_MISC_GSE;
3777df0d28c1SDhinakaran Pandiyan 
377870bfb307SMatt Roper 	if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
37793a3b3c7dSVille Syrjälä 		de_port_masked |= BXT_DE_PORT_GMBUS;
3780a324fcacSRodrigo Vivi 
3781373abf1aSMatt Roper 	if (DISPLAY_VER(dev_priv) >= 11) {
37829c9e97c4SVandita Kulkarni 		enum port port;
37839c9e97c4SVandita Kulkarni 
37849c9e97c4SVandita Kulkarni 		if (intel_bios_is_dsi_present(dev_priv, &port))
37859c9e97c4SVandita Kulkarni 			de_port_masked |= DSI0_TE | DSI1_TE;
37869c9e97c4SVandita Kulkarni 	}
37879c9e97c4SVandita Kulkarni 
3788cda195f1SVille Syrjälä 	de_pipe_enables = de_pipe_masked |
37898bcc0840SMatt Roper 		GEN8_PIPE_VBLANK |
37908bcc0840SMatt Roper 		gen8_de_pipe_underrun_mask(dev_priv) |
3791cda195f1SVille Syrjälä 		gen8_de_pipe_flip_done_mask(dev_priv);
37921288f9b0SKarthik B S 
37933a3b3c7dSVille Syrjälä 	de_port_enables = de_port_masked;
379470bfb307SMatt Roper 	if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
3795a52bb15bSVille Syrjälä 		de_port_enables |= BXT_DE_PORT_HOTPLUG_MASK;
3796a52bb15bSVille Syrjälä 	else if (IS_BROADWELL(dev_priv))
3797e5abaab3SVille Syrjälä 		de_port_enables |= BDW_DE_PORT_HOTPLUG_MASK;
37983a3b3c7dSVille Syrjälä 
3799373abf1aSMatt Roper 	if (DISPLAY_VER(dev_priv) >= 12) {
38008241cfbeSJosé Roberto de Souza 		enum transcoder trans;
38018241cfbeSJosé Roberto de Souza 
3802562ddcb7SMatt Roper 		for_each_cpu_transcoder_masked(dev_priv, trans, trans_mask) {
38038241cfbeSJosé Roberto de Souza 			enum intel_display_power_domain domain;
38048241cfbeSJosé Roberto de Souza 
38058241cfbeSJosé Roberto de Souza 			domain = POWER_DOMAIN_TRANSCODER(trans);
38068241cfbeSJosé Roberto de Souza 			if (!intel_display_power_is_enabled(dev_priv, domain))
38078241cfbeSJosé Roberto de Souza 				continue;
38088241cfbeSJosé Roberto de Souza 
38098241cfbeSJosé Roberto de Souza 			gen3_assert_iir_is_zero(uncore, TRANS_PSR_IIR(trans));
38108241cfbeSJosé Roberto de Souza 		}
38118241cfbeSJosé Roberto de Souza 	} else {
3812b16b2a2fSPaulo Zanoni 		gen3_assert_iir_is_zero(uncore, EDP_PSR_IIR);
38138241cfbeSJosé Roberto de Souza 	}
3814e04f7eceSVille Syrjälä 
38150a195c02SMika Kahola 	for_each_pipe(dev_priv, pipe) {
38160a195c02SMika Kahola 		dev_priv->de_irq_mask[pipe] = ~de_pipe_masked;
3817abd58f01SBen Widawsky 
3818f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3819813bde43SPaulo Zanoni 				POWER_DOMAIN_PIPE(pipe)))
3820b16b2a2fSPaulo Zanoni 			GEN8_IRQ_INIT_NDX(uncore, DE_PIPE, pipe,
3821813bde43SPaulo Zanoni 					  dev_priv->de_irq_mask[pipe],
382235079899SPaulo Zanoni 					  de_pipe_enables);
38230a195c02SMika Kahola 	}
3824abd58f01SBen Widawsky 
3825b16b2a2fSPaulo Zanoni 	GEN3_IRQ_INIT(uncore, GEN8_DE_PORT_, ~de_port_masked, de_port_enables);
3826b16b2a2fSPaulo Zanoni 	GEN3_IRQ_INIT(uncore, GEN8_DE_MISC_, ~de_misc_masked, de_misc_masked);
38272a57d9ccSImre Deak 
3828373abf1aSMatt Roper 	if (DISPLAY_VER(dev_priv) >= 11) {
3829121e758eSDhinakaran Pandiyan 		u32 de_hpd_masked = 0;
3830b796b971SDhinakaran Pandiyan 		u32 de_hpd_enables = GEN11_DE_TC_HOTPLUG_MASK |
3831b796b971SDhinakaran Pandiyan 				     GEN11_DE_TBT_HOTPLUG_MASK;
3832121e758eSDhinakaran Pandiyan 
3833b16b2a2fSPaulo Zanoni 		GEN3_IRQ_INIT(uncore, GEN11_DE_HPD_, ~de_hpd_masked,
3834b16b2a2fSPaulo Zanoni 			      de_hpd_enables);
3835abd58f01SBen Widawsky 	}
3836121e758eSDhinakaran Pandiyan }
3837abd58f01SBen Widawsky 
383859b7cb44STejas Upadhyay static void icp_irq_postinstall(struct drm_i915_private *dev_priv)
383959b7cb44STejas Upadhyay {
384059b7cb44STejas Upadhyay 	struct intel_uncore *uncore = &dev_priv->uncore;
384159b7cb44STejas Upadhyay 	u32 mask = SDE_GMBUS_ICP;
384259b7cb44STejas Upadhyay 
384359b7cb44STejas Upadhyay 	GEN3_IRQ_INIT(uncore, SDE, ~mask, 0xffffffff);
384459b7cb44STejas Upadhyay }
384559b7cb44STejas Upadhyay 
3846b318b824SVille Syrjälä static void gen8_irq_postinstall(struct drm_i915_private *dev_priv)
3847abd58f01SBen Widawsky {
384859b7cb44STejas Upadhyay 	if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
384959b7cb44STejas Upadhyay 		icp_irq_postinstall(dev_priv);
385059b7cb44STejas Upadhyay 	else if (HAS_PCH_SPLIT(dev_priv))
3851a0a6d8cbSVille Syrjälä 		ibx_irq_postinstall(dev_priv);
3852622364b6SPaulo Zanoni 
3853cf1c97dcSAndi Shyti 	gen8_gt_irq_postinstall(&dev_priv->gt);
3854abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3855abd58f01SBen Widawsky 
385625286aacSDaniele Ceraolo Spurio 	gen8_master_intr_enable(dev_priv->uncore.regs);
3857abd58f01SBen Widawsky }
3858abd58f01SBen Widawsky 
3859a844cfbeSJosé Roberto de Souza static void gen11_de_irq_postinstall(struct drm_i915_private *dev_priv)
3860a844cfbeSJosé Roberto de Souza {
3861a844cfbeSJosé Roberto de Souza 	if (!HAS_DISPLAY(dev_priv))
3862a844cfbeSJosé Roberto de Souza 		return;
3863a844cfbeSJosé Roberto de Souza 
3864a844cfbeSJosé Roberto de Souza 	gen8_de_irq_postinstall(dev_priv);
3865a844cfbeSJosé Roberto de Souza 
3866a844cfbeSJosé Roberto de Souza 	intel_uncore_write(&dev_priv->uncore, GEN11_DISPLAY_INT_CTL,
3867a844cfbeSJosé Roberto de Souza 			   GEN11_DISPLAY_IRQ_ENABLE);
3868a844cfbeSJosé Roberto de Souza }
386931604222SAnusha Srivatsa 
3870b318b824SVille Syrjälä static void gen11_irq_postinstall(struct drm_i915_private *dev_priv)
387151951ae7SMika Kuoppala {
3872b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
3873df0d28c1SDhinakaran Pandiyan 	u32 gu_misc_masked = GEN11_GU_MISC_GSE;
387451951ae7SMika Kuoppala 
387529b43ae2SRodrigo Vivi 	if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
3876b318b824SVille Syrjälä 		icp_irq_postinstall(dev_priv);
387731604222SAnusha Srivatsa 
38789b77011eSTvrtko Ursulin 	gen11_gt_irq_postinstall(&dev_priv->gt);
3879a844cfbeSJosé Roberto de Souza 	gen11_de_irq_postinstall(dev_priv);
388051951ae7SMika Kuoppala 
3881b16b2a2fSPaulo Zanoni 	GEN3_IRQ_INIT(uncore, GEN11_GU_MISC_, ~gu_misc_masked, gu_misc_masked);
3882df0d28c1SDhinakaran Pandiyan 
38839b77011eSTvrtko Ursulin 	gen11_master_intr_enable(uncore->regs);
38842939eb06SJani Nikula 	intel_uncore_posting_read(&dev_priv->uncore, GEN11_GFX_MSTR_IRQ);
388551951ae7SMika Kuoppala }
388622e26af7SPaulo Zanoni 
388722e26af7SPaulo Zanoni static void dg1_irq_postinstall(struct drm_i915_private *dev_priv)
388822e26af7SPaulo Zanoni {
388922e26af7SPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
389022e26af7SPaulo Zanoni 	u32 gu_misc_masked = GEN11_GU_MISC_GSE;
389122e26af7SPaulo Zanoni 
389222e26af7SPaulo Zanoni 	gen11_gt_irq_postinstall(&dev_priv->gt);
389322e26af7SPaulo Zanoni 
389422e26af7SPaulo Zanoni 	GEN3_IRQ_INIT(uncore, GEN11_GU_MISC_, ~gu_misc_masked, gu_misc_masked);
389522e26af7SPaulo Zanoni 
389622e26af7SPaulo Zanoni 	if (HAS_DISPLAY(dev_priv)) {
389722e26af7SPaulo Zanoni 		icp_irq_postinstall(dev_priv);
389822e26af7SPaulo Zanoni 		gen8_de_irq_postinstall(dev_priv);
389922e26af7SPaulo Zanoni 		intel_uncore_write(&dev_priv->uncore, GEN11_DISPLAY_INT_CTL,
390022e26af7SPaulo Zanoni 				   GEN11_DISPLAY_IRQ_ENABLE);
390122e26af7SPaulo Zanoni 	}
390222e26af7SPaulo Zanoni 
390322e26af7SPaulo Zanoni 	dg1_master_intr_enable(dev_priv->uncore.regs);
390422e26af7SPaulo Zanoni 	intel_uncore_posting_read(&dev_priv->uncore, DG1_MSTR_TILE_INTR);
390597b492f5SLucas De Marchi }
390651951ae7SMika Kuoppala 
3907b318b824SVille Syrjälä static void cherryview_irq_postinstall(struct drm_i915_private *dev_priv)
390843f328d7SVille Syrjälä {
3909cf1c97dcSAndi Shyti 	gen8_gt_irq_postinstall(&dev_priv->gt);
391043f328d7SVille Syrjälä 
3911ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
39129918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3913ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3914ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3915ad22d106SVille Syrjälä 
39162939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
39172939eb06SJani Nikula 	intel_uncore_posting_read(&dev_priv->uncore, GEN8_MASTER_IRQ);
391843f328d7SVille Syrjälä }
391943f328d7SVille Syrjälä 
3920b318b824SVille Syrjälä static void i8xx_irq_reset(struct drm_i915_private *dev_priv)
3921c2798b19SChris Wilson {
3922b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
3923c2798b19SChris Wilson 
392444d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
392544d9241eSVille Syrjälä 
3926b16b2a2fSPaulo Zanoni 	GEN2_IRQ_RESET(uncore);
3927e44adb5dSChris Wilson 	dev_priv->irq_mask = ~0u;
3928c2798b19SChris Wilson }
3929c2798b19SChris Wilson 
3930b318b824SVille Syrjälä static void i8xx_irq_postinstall(struct drm_i915_private *dev_priv)
3931c2798b19SChris Wilson {
3932b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
3933e9e9848aSVille Syrjälä 	u16 enable_mask;
3934c2798b19SChris Wilson 
39354f5fd91fSTvrtko Ursulin 	intel_uncore_write16(uncore,
39364f5fd91fSTvrtko Ursulin 			     EMR,
39374f5fd91fSTvrtko Ursulin 			     ~(I915_ERROR_PAGE_TABLE |
3938045cebd2SVille Syrjälä 			       I915_ERROR_MEMORY_REFRESH));
3939c2798b19SChris Wilson 
3940c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
3941c2798b19SChris Wilson 	dev_priv->irq_mask =
3942c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
394316659bc5SVille Syrjälä 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
394416659bc5SVille Syrjälä 		  I915_MASTER_ERROR_INTERRUPT);
3945c2798b19SChris Wilson 
3946e9e9848aSVille Syrjälä 	enable_mask =
3947c2798b19SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3948c2798b19SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
394916659bc5SVille Syrjälä 		I915_MASTER_ERROR_INTERRUPT |
3950e9e9848aSVille Syrjälä 		I915_USER_INTERRUPT;
3951e9e9848aSVille Syrjälä 
3952b16b2a2fSPaulo Zanoni 	GEN2_IRQ_INIT(uncore, dev_priv->irq_mask, enable_mask);
3953c2798b19SChris Wilson 
3954379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3955379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3956d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3957755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3958755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3959d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3960c2798b19SChris Wilson }
3961c2798b19SChris Wilson 
39624f5fd91fSTvrtko Ursulin static void i8xx_error_irq_ack(struct drm_i915_private *i915,
396378c357ddSVille Syrjälä 			       u16 *eir, u16 *eir_stuck)
396478c357ddSVille Syrjälä {
39654f5fd91fSTvrtko Ursulin 	struct intel_uncore *uncore = &i915->uncore;
396678c357ddSVille Syrjälä 	u16 emr;
396778c357ddSVille Syrjälä 
39684f5fd91fSTvrtko Ursulin 	*eir = intel_uncore_read16(uncore, EIR);
396978c357ddSVille Syrjälä 
397078c357ddSVille Syrjälä 	if (*eir)
39714f5fd91fSTvrtko Ursulin 		intel_uncore_write16(uncore, EIR, *eir);
397278c357ddSVille Syrjälä 
39734f5fd91fSTvrtko Ursulin 	*eir_stuck = intel_uncore_read16(uncore, EIR);
397478c357ddSVille Syrjälä 	if (*eir_stuck == 0)
397578c357ddSVille Syrjälä 		return;
397678c357ddSVille Syrjälä 
397778c357ddSVille Syrjälä 	/*
397878c357ddSVille Syrjälä 	 * Toggle all EMR bits to make sure we get an edge
397978c357ddSVille Syrjälä 	 * in the ISR master error bit if we don't clear
398078c357ddSVille Syrjälä 	 * all the EIR bits. Otherwise the edge triggered
398178c357ddSVille Syrjälä 	 * IIR on i965/g4x wouldn't notice that an interrupt
398278c357ddSVille Syrjälä 	 * is still pending. Also some EIR bits can't be
398378c357ddSVille Syrjälä 	 * cleared except by handling the underlying error
398478c357ddSVille Syrjälä 	 * (or by a GPU reset) so we mask any bit that
398578c357ddSVille Syrjälä 	 * remains set.
398678c357ddSVille Syrjälä 	 */
39874f5fd91fSTvrtko Ursulin 	emr = intel_uncore_read16(uncore, EMR);
39884f5fd91fSTvrtko Ursulin 	intel_uncore_write16(uncore, EMR, 0xffff);
39894f5fd91fSTvrtko Ursulin 	intel_uncore_write16(uncore, EMR, emr | *eir_stuck);
399078c357ddSVille Syrjälä }
399178c357ddSVille Syrjälä 
399278c357ddSVille Syrjälä static void i8xx_error_irq_handler(struct drm_i915_private *dev_priv,
399378c357ddSVille Syrjälä 				   u16 eir, u16 eir_stuck)
399478c357ddSVille Syrjälä {
399578c357ddSVille Syrjälä 	DRM_DEBUG("Master Error: EIR 0x%04x\n", eir);
399678c357ddSVille Syrjälä 
399778c357ddSVille Syrjälä 	if (eir_stuck)
399800376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm, "EIR stuck: 0x%04x, masked\n",
399900376ccfSWambui Karuga 			eir_stuck);
400078c357ddSVille Syrjälä }
400178c357ddSVille Syrjälä 
400278c357ddSVille Syrjälä static void i9xx_error_irq_ack(struct drm_i915_private *dev_priv,
400378c357ddSVille Syrjälä 			       u32 *eir, u32 *eir_stuck)
400478c357ddSVille Syrjälä {
400578c357ddSVille Syrjälä 	u32 emr;
400678c357ddSVille Syrjälä 
40072939eb06SJani Nikula 	*eir = intel_uncore_read(&dev_priv->uncore, EIR);
400878c357ddSVille Syrjälä 
40092939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, EIR, *eir);
401078c357ddSVille Syrjälä 
40112939eb06SJani Nikula 	*eir_stuck = intel_uncore_read(&dev_priv->uncore, EIR);
401278c357ddSVille Syrjälä 	if (*eir_stuck == 0)
401378c357ddSVille Syrjälä 		return;
401478c357ddSVille Syrjälä 
401578c357ddSVille Syrjälä 	/*
401678c357ddSVille Syrjälä 	 * Toggle all EMR bits to make sure we get an edge
401778c357ddSVille Syrjälä 	 * in the ISR master error bit if we don't clear
401878c357ddSVille Syrjälä 	 * all the EIR bits. Otherwise the edge triggered
401978c357ddSVille Syrjälä 	 * IIR on i965/g4x wouldn't notice that an interrupt
402078c357ddSVille Syrjälä 	 * is still pending. Also some EIR bits can't be
402178c357ddSVille Syrjälä 	 * cleared except by handling the underlying error
402278c357ddSVille Syrjälä 	 * (or by a GPU reset) so we mask any bit that
402378c357ddSVille Syrjälä 	 * remains set.
402478c357ddSVille Syrjälä 	 */
40252939eb06SJani Nikula 	emr = intel_uncore_read(&dev_priv->uncore, EMR);
40262939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, EMR, 0xffffffff);
40272939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, EMR, emr | *eir_stuck);
402878c357ddSVille Syrjälä }
402978c357ddSVille Syrjälä 
403078c357ddSVille Syrjälä static void i9xx_error_irq_handler(struct drm_i915_private *dev_priv,
403178c357ddSVille Syrjälä 				   u32 eir, u32 eir_stuck)
403278c357ddSVille Syrjälä {
403378c357ddSVille Syrjälä 	DRM_DEBUG("Master Error, EIR 0x%08x\n", eir);
403478c357ddSVille Syrjälä 
403578c357ddSVille Syrjälä 	if (eir_stuck)
403600376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm, "EIR stuck: 0x%08x, masked\n",
403700376ccfSWambui Karuga 			eir_stuck);
403878c357ddSVille Syrjälä }
403978c357ddSVille Syrjälä 
4040ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
4041c2798b19SChris Wilson {
4042b318b824SVille Syrjälä 	struct drm_i915_private *dev_priv = arg;
4043af722d28SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
4044c2798b19SChris Wilson 
40452dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
40462dd2a883SImre Deak 		return IRQ_NONE;
40472dd2a883SImre Deak 
40481f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
40499102650fSDaniele Ceraolo Spurio 	disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
40501f814dacSImre Deak 
4051af722d28SVille Syrjälä 	do {
4052af722d28SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
405378c357ddSVille Syrjälä 		u16 eir = 0, eir_stuck = 0;
4054af722d28SVille Syrjälä 		u16 iir;
4055af722d28SVille Syrjälä 
40564f5fd91fSTvrtko Ursulin 		iir = intel_uncore_read16(&dev_priv->uncore, GEN2_IIR);
4057c2798b19SChris Wilson 		if (iir == 0)
4058af722d28SVille Syrjälä 			break;
4059c2798b19SChris Wilson 
4060af722d28SVille Syrjälä 		ret = IRQ_HANDLED;
4061c2798b19SChris Wilson 
4062eb64343cSVille Syrjälä 		/* Call regardless, as some status bits might not be
4063eb64343cSVille Syrjälä 		 * signalled in iir */
4064eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
4065c2798b19SChris Wilson 
406678c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
406778c357ddSVille Syrjälä 			i8xx_error_irq_ack(dev_priv, &eir, &eir_stuck);
406878c357ddSVille Syrjälä 
40694f5fd91fSTvrtko Ursulin 		intel_uncore_write16(&dev_priv->uncore, GEN2_IIR, iir);
4070c2798b19SChris Wilson 
4071c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
40720669a6e1SChris Wilson 			intel_engine_cs_irq(dev_priv->gt.engine[RCS0], iir);
4073c2798b19SChris Wilson 
407478c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
407578c357ddSVille Syrjälä 			i8xx_error_irq_handler(dev_priv, eir, eir_stuck);
4076af722d28SVille Syrjälä 
4077eb64343cSVille Syrjälä 		i8xx_pipestat_irq_handler(dev_priv, iir, pipe_stats);
4078af722d28SVille Syrjälä 	} while (0);
4079c2798b19SChris Wilson 
40809c6508b9SThomas Gleixner 	pmu_irq_stats(dev_priv, ret);
40819c6508b9SThomas Gleixner 
40829102650fSDaniele Ceraolo Spurio 	enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
40831f814dacSImre Deak 
40841f814dacSImre Deak 	return ret;
4085c2798b19SChris Wilson }
4086c2798b19SChris Wilson 
4087b318b824SVille Syrjälä static void i915_irq_reset(struct drm_i915_private *dev_priv)
4088a266c7d5SChris Wilson {
4089b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
4090a266c7d5SChris Wilson 
409156b857a5STvrtko Ursulin 	if (I915_HAS_HOTPLUG(dev_priv)) {
40920706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
40932939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, PORT_HOTPLUG_STAT, intel_uncore_read(&dev_priv->uncore, PORT_HOTPLUG_STAT));
4094a266c7d5SChris Wilson 	}
4095a266c7d5SChris Wilson 
409644d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
409744d9241eSVille Syrjälä 
4098b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN2_);
4099e44adb5dSChris Wilson 	dev_priv->irq_mask = ~0u;
4100a266c7d5SChris Wilson }
4101a266c7d5SChris Wilson 
4102b318b824SVille Syrjälä static void i915_irq_postinstall(struct drm_i915_private *dev_priv)
4103a266c7d5SChris Wilson {
4104b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
410538bde180SChris Wilson 	u32 enable_mask;
4106a266c7d5SChris Wilson 
41072939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, EMR, ~(I915_ERROR_PAGE_TABLE |
4108045cebd2SVille Syrjälä 			  I915_ERROR_MEMORY_REFRESH));
410938bde180SChris Wilson 
411038bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
411138bde180SChris Wilson 	dev_priv->irq_mask =
411238bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
411338bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
411416659bc5SVille Syrjälä 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
411516659bc5SVille Syrjälä 		  I915_MASTER_ERROR_INTERRUPT);
411638bde180SChris Wilson 
411738bde180SChris Wilson 	enable_mask =
411838bde180SChris Wilson 		I915_ASLE_INTERRUPT |
411938bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
412038bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
412116659bc5SVille Syrjälä 		I915_MASTER_ERROR_INTERRUPT |
412238bde180SChris Wilson 		I915_USER_INTERRUPT;
412338bde180SChris Wilson 
412456b857a5STvrtko Ursulin 	if (I915_HAS_HOTPLUG(dev_priv)) {
4125a266c7d5SChris Wilson 		/* Enable in IER... */
4126a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
4127a266c7d5SChris Wilson 		/* and unmask in IMR */
4128a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
4129a266c7d5SChris Wilson 	}
4130a266c7d5SChris Wilson 
4131b16b2a2fSPaulo Zanoni 	GEN3_IRQ_INIT(uncore, GEN2_, dev_priv->irq_mask, enable_mask);
4132a266c7d5SChris Wilson 
4133379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4134379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4135d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4136755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4137755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4138d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4139379ef82dSDaniel Vetter 
4140c30bb1fdSVille Syrjälä 	i915_enable_asle_pipestat(dev_priv);
414120afbda2SDaniel Vetter }
414220afbda2SDaniel Vetter 
4143ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
4144a266c7d5SChris Wilson {
4145b318b824SVille Syrjälä 	struct drm_i915_private *dev_priv = arg;
4146af722d28SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
4147a266c7d5SChris Wilson 
41482dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
41492dd2a883SImre Deak 		return IRQ_NONE;
41502dd2a883SImre Deak 
41511f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
41529102650fSDaniele Ceraolo Spurio 	disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
41531f814dacSImre Deak 
415438bde180SChris Wilson 	do {
4155eb64343cSVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
415678c357ddSVille Syrjälä 		u32 eir = 0, eir_stuck = 0;
4157af722d28SVille Syrjälä 		u32 hotplug_status = 0;
4158af722d28SVille Syrjälä 		u32 iir;
4159a266c7d5SChris Wilson 
41602939eb06SJani Nikula 		iir = intel_uncore_read(&dev_priv->uncore, GEN2_IIR);
4161af722d28SVille Syrjälä 		if (iir == 0)
4162af722d28SVille Syrjälä 			break;
4163af722d28SVille Syrjälä 
4164af722d28SVille Syrjälä 		ret = IRQ_HANDLED;
4165af722d28SVille Syrjälä 
4166af722d28SVille Syrjälä 		if (I915_HAS_HOTPLUG(dev_priv) &&
4167af722d28SVille Syrjälä 		    iir & I915_DISPLAY_PORT_INTERRUPT)
4168af722d28SVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
4169a266c7d5SChris Wilson 
4170eb64343cSVille Syrjälä 		/* Call regardless, as some status bits might not be
4171eb64343cSVille Syrjälä 		 * signalled in iir */
4172eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
4173a266c7d5SChris Wilson 
417478c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
417578c357ddSVille Syrjälä 			i9xx_error_irq_ack(dev_priv, &eir, &eir_stuck);
417678c357ddSVille Syrjälä 
41772939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, GEN2_IIR, iir);
4178a266c7d5SChris Wilson 
4179a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
41800669a6e1SChris Wilson 			intel_engine_cs_irq(dev_priv->gt.engine[RCS0], iir);
4181a266c7d5SChris Wilson 
418278c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
418378c357ddSVille Syrjälä 			i9xx_error_irq_handler(dev_priv, eir, eir_stuck);
4184a266c7d5SChris Wilson 
4185af722d28SVille Syrjälä 		if (hotplug_status)
4186af722d28SVille Syrjälä 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
4187af722d28SVille Syrjälä 
4188af722d28SVille Syrjälä 		i915_pipestat_irq_handler(dev_priv, iir, pipe_stats);
4189af722d28SVille Syrjälä 	} while (0);
4190a266c7d5SChris Wilson 
41919c6508b9SThomas Gleixner 	pmu_irq_stats(dev_priv, ret);
41929c6508b9SThomas Gleixner 
41939102650fSDaniele Ceraolo Spurio 	enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
41941f814dacSImre Deak 
4195a266c7d5SChris Wilson 	return ret;
4196a266c7d5SChris Wilson }
4197a266c7d5SChris Wilson 
4198b318b824SVille Syrjälä static void i965_irq_reset(struct drm_i915_private *dev_priv)
4199a266c7d5SChris Wilson {
4200b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
4201a266c7d5SChris Wilson 
42020706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
42032939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, PORT_HOTPLUG_STAT, intel_uncore_read(&dev_priv->uncore, PORT_HOTPLUG_STAT));
4204a266c7d5SChris Wilson 
420544d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
420644d9241eSVille Syrjälä 
4207b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN2_);
4208e44adb5dSChris Wilson 	dev_priv->irq_mask = ~0u;
4209a266c7d5SChris Wilson }
4210a266c7d5SChris Wilson 
4211b318b824SVille Syrjälä static void i965_irq_postinstall(struct drm_i915_private *dev_priv)
4212a266c7d5SChris Wilson {
4213b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
4214bbba0a97SChris Wilson 	u32 enable_mask;
4215a266c7d5SChris Wilson 	u32 error_mask;
4216a266c7d5SChris Wilson 
4217045cebd2SVille Syrjälä 	/*
4218045cebd2SVille Syrjälä 	 * Enable some error detection, note the instruction error mask
4219045cebd2SVille Syrjälä 	 * bit is reserved, so we leave it masked.
4220045cebd2SVille Syrjälä 	 */
4221045cebd2SVille Syrjälä 	if (IS_G4X(dev_priv)) {
4222045cebd2SVille Syrjälä 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
4223045cebd2SVille Syrjälä 			       GM45_ERROR_MEM_PRIV |
4224045cebd2SVille Syrjälä 			       GM45_ERROR_CP_PRIV |
4225045cebd2SVille Syrjälä 			       I915_ERROR_MEMORY_REFRESH);
4226045cebd2SVille Syrjälä 	} else {
4227045cebd2SVille Syrjälä 		error_mask = ~(I915_ERROR_PAGE_TABLE |
4228045cebd2SVille Syrjälä 			       I915_ERROR_MEMORY_REFRESH);
4229045cebd2SVille Syrjälä 	}
42302939eb06SJani Nikula 	intel_uncore_write(&dev_priv->uncore, EMR, error_mask);
4231045cebd2SVille Syrjälä 
4232a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
4233c30bb1fdSVille Syrjälä 	dev_priv->irq_mask =
4234c30bb1fdSVille Syrjälä 		~(I915_ASLE_INTERRUPT |
4235adca4730SChris Wilson 		  I915_DISPLAY_PORT_INTERRUPT |
4236bbba0a97SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4237bbba0a97SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
423878c357ddSVille Syrjälä 		  I915_MASTER_ERROR_INTERRUPT);
4239bbba0a97SChris Wilson 
4240c30bb1fdSVille Syrjälä 	enable_mask =
4241c30bb1fdSVille Syrjälä 		I915_ASLE_INTERRUPT |
4242c30bb1fdSVille Syrjälä 		I915_DISPLAY_PORT_INTERRUPT |
4243c30bb1fdSVille Syrjälä 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4244c30bb1fdSVille Syrjälä 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
424578c357ddSVille Syrjälä 		I915_MASTER_ERROR_INTERRUPT |
4246c30bb1fdSVille Syrjälä 		I915_USER_INTERRUPT;
4247bbba0a97SChris Wilson 
424891d14251STvrtko Ursulin 	if (IS_G4X(dev_priv))
4249bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
4250a266c7d5SChris Wilson 
4251b16b2a2fSPaulo Zanoni 	GEN3_IRQ_INIT(uncore, GEN2_, dev_priv->irq_mask, enable_mask);
4252c30bb1fdSVille Syrjälä 
4253b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4254b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4255d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4256755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
4257755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4258755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4259d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4260a266c7d5SChris Wilson 
426191d14251STvrtko Ursulin 	i915_enable_asle_pipestat(dev_priv);
426220afbda2SDaniel Vetter }
426320afbda2SDaniel Vetter 
426491d14251STvrtko Ursulin static void i915_hpd_irq_setup(struct drm_i915_private *dev_priv)
426520afbda2SDaniel Vetter {
426620afbda2SDaniel Vetter 	u32 hotplug_en;
426720afbda2SDaniel Vetter 
426867520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
4269b5ea2d56SDaniel Vetter 
4270adca4730SChris Wilson 	/* Note HDMI and DP share hotplug bits */
4271e5868a31SEgbert Eich 	/* enable bits are the same for all generations */
427291d14251STvrtko Ursulin 	hotplug_en = intel_hpd_enabled_irqs(dev_priv, hpd_mask_i915);
4273a266c7d5SChris Wilson 	/* Programming the CRT detection parameters tends
4274a266c7d5SChris Wilson 	   to generate a spurious hotplug event about three
4275a266c7d5SChris Wilson 	   seconds later.  So just do it once.
4276a266c7d5SChris Wilson 	*/
427791d14251STvrtko Ursulin 	if (IS_G4X(dev_priv))
4278a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
4279a266c7d5SChris Wilson 	hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
4280a266c7d5SChris Wilson 
4281a266c7d5SChris Wilson 	/* Ignore TV since it's buggy */
42820706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv,
4283f9e3dc78SJani Nikula 					     HOTPLUG_INT_EN_MASK |
4284f9e3dc78SJani Nikula 					     CRT_HOTPLUG_VOLTAGE_COMPARE_MASK |
4285f9e3dc78SJani Nikula 					     CRT_HOTPLUG_ACTIVATION_PERIOD_64,
42860706f17cSEgbert Eich 					     hotplug_en);
4287a266c7d5SChris Wilson }
4288a266c7d5SChris Wilson 
4289ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
4290a266c7d5SChris Wilson {
4291b318b824SVille Syrjälä 	struct drm_i915_private *dev_priv = arg;
4292af722d28SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
4293a266c7d5SChris Wilson 
42942dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
42952dd2a883SImre Deak 		return IRQ_NONE;
42962dd2a883SImre Deak 
42971f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
42989102650fSDaniele Ceraolo Spurio 	disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
42991f814dacSImre Deak 
4300af722d28SVille Syrjälä 	do {
4301eb64343cSVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
430278c357ddSVille Syrjälä 		u32 eir = 0, eir_stuck = 0;
4303af722d28SVille Syrjälä 		u32 hotplug_status = 0;
4304af722d28SVille Syrjälä 		u32 iir;
43052c8ba29fSChris Wilson 
43062939eb06SJani Nikula 		iir = intel_uncore_read(&dev_priv->uncore, GEN2_IIR);
4307af722d28SVille Syrjälä 		if (iir == 0)
4308af722d28SVille Syrjälä 			break;
4309af722d28SVille Syrjälä 
4310af722d28SVille Syrjälä 		ret = IRQ_HANDLED;
4311af722d28SVille Syrjälä 
4312af722d28SVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
4313af722d28SVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
4314a266c7d5SChris Wilson 
4315eb64343cSVille Syrjälä 		/* Call regardless, as some status bits might not be
4316eb64343cSVille Syrjälä 		 * signalled in iir */
4317eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
4318a266c7d5SChris Wilson 
431978c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
432078c357ddSVille Syrjälä 			i9xx_error_irq_ack(dev_priv, &eir, &eir_stuck);
432178c357ddSVille Syrjälä 
43222939eb06SJani Nikula 		intel_uncore_write(&dev_priv->uncore, GEN2_IIR, iir);
4323a266c7d5SChris Wilson 
4324a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
43250669a6e1SChris Wilson 			intel_engine_cs_irq(dev_priv->gt.engine[RCS0],
43260669a6e1SChris Wilson 					    iir);
4327af722d28SVille Syrjälä 
4328a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
43290669a6e1SChris Wilson 			intel_engine_cs_irq(dev_priv->gt.engine[VCS0],
43300669a6e1SChris Wilson 					    iir >> 25);
4331a266c7d5SChris Wilson 
433278c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
433378c357ddSVille Syrjälä 			i9xx_error_irq_handler(dev_priv, eir, eir_stuck);
4334515ac2bbSDaniel Vetter 
4335af722d28SVille Syrjälä 		if (hotplug_status)
4336af722d28SVille Syrjälä 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
4337af722d28SVille Syrjälä 
4338af722d28SVille Syrjälä 		i965_pipestat_irq_handler(dev_priv, iir, pipe_stats);
4339af722d28SVille Syrjälä 	} while (0);
4340a266c7d5SChris Wilson 
43419c6508b9SThomas Gleixner 	pmu_irq_stats(dev_priv, IRQ_HANDLED);
43429c6508b9SThomas Gleixner 
43439102650fSDaniele Ceraolo Spurio 	enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
43441f814dacSImre Deak 
4345a266c7d5SChris Wilson 	return ret;
4346a266c7d5SChris Wilson }
4347a266c7d5SChris Wilson 
4348cd030c7cSDave Airlie #define HPD_FUNCS(platform)					 \
4349cd030c7cSDave Airlie static const struct intel_hotplug_funcs platform##_hpd_funcs = { \
4350cd030c7cSDave Airlie 	.hpd_irq_setup = platform##_hpd_irq_setup,		 \
4351cd030c7cSDave Airlie }
4352cd030c7cSDave Airlie 
4353cd030c7cSDave Airlie HPD_FUNCS(i915);
4354cd030c7cSDave Airlie HPD_FUNCS(dg1);
4355cd030c7cSDave Airlie HPD_FUNCS(gen11);
4356cd030c7cSDave Airlie HPD_FUNCS(bxt);
4357cd030c7cSDave Airlie HPD_FUNCS(icp);
4358cd030c7cSDave Airlie HPD_FUNCS(spt);
4359cd030c7cSDave Airlie HPD_FUNCS(ilk);
4360cd030c7cSDave Airlie #undef HPD_FUNCS
4361cd030c7cSDave Airlie 
4362fca52a55SDaniel Vetter /**
4363fca52a55SDaniel Vetter  * intel_irq_init - initializes irq support
4364fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4365fca52a55SDaniel Vetter  *
4366fca52a55SDaniel Vetter  * This function initializes all the irq support including work items, timers
4367fca52a55SDaniel Vetter  * and all the vtables. It does not setup the interrupt itself though.
4368fca52a55SDaniel Vetter  */
4369b963291cSDaniel Vetter void intel_irq_init(struct drm_i915_private *dev_priv)
4370f71d4af4SJesse Barnes {
437191c8a326SChris Wilson 	struct drm_device *dev = &dev_priv->drm;
4372cefcff8fSJoonas Lahtinen 	int i;
43738b2e326dSChris Wilson 
437474bb98baSLucas De Marchi 	INIT_WORK(&dev_priv->l3_parity.error_work, ivb_parity_work);
4375cefcff8fSJoonas Lahtinen 	for (i = 0; i < MAX_L3_SLICES; ++i)
4376cefcff8fSJoonas Lahtinen 		dev_priv->l3_parity.remap_info[i] = NULL;
43778b2e326dSChris Wilson 
4378633023a4SDaniele Ceraolo Spurio 	/* pre-gen11 the guc irqs bits are in the upper 16 bits of the pm reg */
4379651e7d48SLucas De Marchi 	if (HAS_GT_UC(dev_priv) && GRAPHICS_VER(dev_priv) < 11)
43802239e6dfSDaniele Ceraolo Spurio 		dev_priv->gt.pm_guc_events = GUC_INTR_GUC2HOST << 16;
438126705e20SSagar Arun Kamble 
43829a450b68SLucas De Marchi 	if (!HAS_DISPLAY(dev_priv))
43839a450b68SLucas De Marchi 		return;
43849a450b68SLucas De Marchi 
438596bd87b7SLucas De Marchi 	intel_hpd_init_pins(dev_priv);
438696bd87b7SLucas De Marchi 
438796bd87b7SLucas De Marchi 	intel_hpd_init_work(dev_priv);
438896bd87b7SLucas De Marchi 
438921da2700SVille Syrjälä 	dev->vblank_disable_immediate = true;
439021da2700SVille Syrjälä 
4391262fd485SChris Wilson 	/* Most platforms treat the display irq block as an always-on
4392262fd485SChris Wilson 	 * power domain. vlv/chv can disable it at runtime and need
4393262fd485SChris Wilson 	 * special care to avoid writing any of the display block registers
4394262fd485SChris Wilson 	 * outside of the power domain. We defer setting up the display irqs
4395262fd485SChris Wilson 	 * in this case to the runtime pm.
4396262fd485SChris Wilson 	 */
4397262fd485SChris Wilson 	dev_priv->display_irqs_enabled = true;
4398262fd485SChris Wilson 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4399262fd485SChris Wilson 		dev_priv->display_irqs_enabled = false;
4400262fd485SChris Wilson 
4401317eaa95SLyude 	dev_priv->hotplug.hpd_storm_threshold = HPD_STORM_DEFAULT_THRESHOLD;
44029a64c650SLyude Paul 	/* If we have MST support, we want to avoid doing short HPD IRQ storm
44039a64c650SLyude Paul 	 * detection, as short HPD storms will occur as a natural part of
44049a64c650SLyude Paul 	 * sideband messaging with MST.
44059a64c650SLyude Paul 	 * On older platforms however, IRQ storms can occur with both long and
44069a64c650SLyude Paul 	 * short pulses, as seen on some G4x systems.
44079a64c650SLyude Paul 	 */
44089a64c650SLyude Paul 	dev_priv->hotplug.hpd_short_storm_enabled = !HAS_DP_MST(dev_priv);
4409317eaa95SLyude 
44102ccf2e03SChris Wilson 	if (HAS_GMCH(dev_priv)) {
44112ccf2e03SChris Wilson 		if (I915_HAS_HOTPLUG(dev_priv))
4412cd030c7cSDave Airlie 			dev_priv->hotplug_funcs = &i915_hpd_funcs;
44132ccf2e03SChris Wilson 	} else {
4414229f31e2SLucas De Marchi 		if (HAS_PCH_DG1(dev_priv))
4415cd030c7cSDave Airlie 			dev_priv->hotplug_funcs = &dg1_hpd_funcs;
4416373abf1aSMatt Roper 		else if (DISPLAY_VER(dev_priv) >= 11)
4417cd030c7cSDave Airlie 			dev_priv->hotplug_funcs = &gen11_hpd_funcs;
441870bfb307SMatt Roper 		else if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
4419cd030c7cSDave Airlie 			dev_priv->hotplug_funcs = &bxt_hpd_funcs;
4420cec3295bSLyude Paul 		else if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
4421cd030c7cSDave Airlie 			dev_priv->hotplug_funcs = &icp_hpd_funcs;
4422c6c30b91SRodrigo Vivi 		else if (INTEL_PCH_TYPE(dev_priv) >= PCH_SPT)
4423cd030c7cSDave Airlie 			dev_priv->hotplug_funcs = &spt_hpd_funcs;
44246dbf30ceSVille Syrjälä 		else
4425cd030c7cSDave Airlie 			dev_priv->hotplug_funcs = &ilk_hpd_funcs;
4426f71d4af4SJesse Barnes 	}
44272ccf2e03SChris Wilson }
442820afbda2SDaniel Vetter 
4429fca52a55SDaniel Vetter /**
4430cefcff8fSJoonas Lahtinen  * intel_irq_fini - deinitializes IRQ support
4431cefcff8fSJoonas Lahtinen  * @i915: i915 device instance
4432cefcff8fSJoonas Lahtinen  *
4433cefcff8fSJoonas Lahtinen  * This function deinitializes all the IRQ support.
4434cefcff8fSJoonas Lahtinen  */
4435cefcff8fSJoonas Lahtinen void intel_irq_fini(struct drm_i915_private *i915)
4436cefcff8fSJoonas Lahtinen {
4437cefcff8fSJoonas Lahtinen 	int i;
4438cefcff8fSJoonas Lahtinen 
4439cefcff8fSJoonas Lahtinen 	for (i = 0; i < MAX_L3_SLICES; ++i)
4440cefcff8fSJoonas Lahtinen 		kfree(i915->l3_parity.remap_info[i]);
4441cefcff8fSJoonas Lahtinen }
4442cefcff8fSJoonas Lahtinen 
4443b318b824SVille Syrjälä static irq_handler_t intel_irq_handler(struct drm_i915_private *dev_priv)
4444b318b824SVille Syrjälä {
4445b318b824SVille Syrjälä 	if (HAS_GMCH(dev_priv)) {
4446b318b824SVille Syrjälä 		if (IS_CHERRYVIEW(dev_priv))
4447b318b824SVille Syrjälä 			return cherryview_irq_handler;
4448b318b824SVille Syrjälä 		else if (IS_VALLEYVIEW(dev_priv))
4449b318b824SVille Syrjälä 			return valleyview_irq_handler;
4450651e7d48SLucas De Marchi 		else if (GRAPHICS_VER(dev_priv) == 4)
4451b318b824SVille Syrjälä 			return i965_irq_handler;
4452651e7d48SLucas De Marchi 		else if (GRAPHICS_VER(dev_priv) == 3)
4453b318b824SVille Syrjälä 			return i915_irq_handler;
4454b318b824SVille Syrjälä 		else
4455b318b824SVille Syrjälä 			return i8xx_irq_handler;
4456b318b824SVille Syrjälä 	} else {
445722e26af7SPaulo Zanoni 		if (GRAPHICS_VER_FULL(dev_priv) >= IP_VER(12, 10))
445897b492f5SLucas De Marchi 			return dg1_irq_handler;
445922e26af7SPaulo Zanoni 		else if (GRAPHICS_VER(dev_priv) >= 11)
4460b318b824SVille Syrjälä 			return gen11_irq_handler;
4461651e7d48SLucas De Marchi 		else if (GRAPHICS_VER(dev_priv) >= 8)
4462b318b824SVille Syrjälä 			return gen8_irq_handler;
4463b318b824SVille Syrjälä 		else
44649eae5e27SLucas De Marchi 			return ilk_irq_handler;
4465b318b824SVille Syrjälä 	}
4466b318b824SVille Syrjälä }
4467b318b824SVille Syrjälä 
4468b318b824SVille Syrjälä static void intel_irq_reset(struct drm_i915_private *dev_priv)
4469b318b824SVille Syrjälä {
4470b318b824SVille Syrjälä 	if (HAS_GMCH(dev_priv)) {
4471b318b824SVille Syrjälä 		if (IS_CHERRYVIEW(dev_priv))
4472b318b824SVille Syrjälä 			cherryview_irq_reset(dev_priv);
4473b318b824SVille Syrjälä 		else if (IS_VALLEYVIEW(dev_priv))
4474b318b824SVille Syrjälä 			valleyview_irq_reset(dev_priv);
4475651e7d48SLucas De Marchi 		else if (GRAPHICS_VER(dev_priv) == 4)
4476b318b824SVille Syrjälä 			i965_irq_reset(dev_priv);
4477651e7d48SLucas De Marchi 		else if (GRAPHICS_VER(dev_priv) == 3)
4478b318b824SVille Syrjälä 			i915_irq_reset(dev_priv);
4479b318b824SVille Syrjälä 		else
4480b318b824SVille Syrjälä 			i8xx_irq_reset(dev_priv);
4481b318b824SVille Syrjälä 	} else {
448222e26af7SPaulo Zanoni 		if (GRAPHICS_VER_FULL(dev_priv) >= IP_VER(12, 10))
448322e26af7SPaulo Zanoni 			dg1_irq_reset(dev_priv);
448422e26af7SPaulo Zanoni 		else if (GRAPHICS_VER(dev_priv) >= 11)
4485b318b824SVille Syrjälä 			gen11_irq_reset(dev_priv);
4486651e7d48SLucas De Marchi 		else if (GRAPHICS_VER(dev_priv) >= 8)
4487b318b824SVille Syrjälä 			gen8_irq_reset(dev_priv);
4488b318b824SVille Syrjälä 		else
44899eae5e27SLucas De Marchi 			ilk_irq_reset(dev_priv);
4490b318b824SVille Syrjälä 	}
4491b318b824SVille Syrjälä }
4492b318b824SVille Syrjälä 
4493b318b824SVille Syrjälä static void intel_irq_postinstall(struct drm_i915_private *dev_priv)
4494b318b824SVille Syrjälä {
4495b318b824SVille Syrjälä 	if (HAS_GMCH(dev_priv)) {
4496b318b824SVille Syrjälä 		if (IS_CHERRYVIEW(dev_priv))
4497b318b824SVille Syrjälä 			cherryview_irq_postinstall(dev_priv);
4498b318b824SVille Syrjälä 		else if (IS_VALLEYVIEW(dev_priv))
4499b318b824SVille Syrjälä 			valleyview_irq_postinstall(dev_priv);
4500651e7d48SLucas De Marchi 		else if (GRAPHICS_VER(dev_priv) == 4)
4501b318b824SVille Syrjälä 			i965_irq_postinstall(dev_priv);
4502651e7d48SLucas De Marchi 		else if (GRAPHICS_VER(dev_priv) == 3)
4503b318b824SVille Syrjälä 			i915_irq_postinstall(dev_priv);
4504b318b824SVille Syrjälä 		else
4505b318b824SVille Syrjälä 			i8xx_irq_postinstall(dev_priv);
4506b318b824SVille Syrjälä 	} else {
450722e26af7SPaulo Zanoni 		if (GRAPHICS_VER_FULL(dev_priv) >= IP_VER(12, 10))
450822e26af7SPaulo Zanoni 			dg1_irq_postinstall(dev_priv);
450922e26af7SPaulo Zanoni 		else if (GRAPHICS_VER(dev_priv) >= 11)
4510b318b824SVille Syrjälä 			gen11_irq_postinstall(dev_priv);
4511651e7d48SLucas De Marchi 		else if (GRAPHICS_VER(dev_priv) >= 8)
4512b318b824SVille Syrjälä 			gen8_irq_postinstall(dev_priv);
4513b318b824SVille Syrjälä 		else
45149eae5e27SLucas De Marchi 			ilk_irq_postinstall(dev_priv);
4515b318b824SVille Syrjälä 	}
4516b318b824SVille Syrjälä }
4517b318b824SVille Syrjälä 
4518cefcff8fSJoonas Lahtinen /**
4519fca52a55SDaniel Vetter  * intel_irq_install - enables the hardware interrupt
4520fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4521fca52a55SDaniel Vetter  *
4522fca52a55SDaniel Vetter  * This function enables the hardware interrupt handling, but leaves the hotplug
4523fca52a55SDaniel Vetter  * handling still disabled. It is called after intel_irq_init().
4524fca52a55SDaniel Vetter  *
4525fca52a55SDaniel Vetter  * In the driver load and resume code we need working interrupts in a few places
4526fca52a55SDaniel Vetter  * but don't want to deal with the hassle of concurrent probe and hotplug
4527fca52a55SDaniel Vetter  * workers. Hence the split into this two-stage approach.
4528fca52a55SDaniel Vetter  */
45292aeb7d3aSDaniel Vetter int intel_irq_install(struct drm_i915_private *dev_priv)
45302aeb7d3aSDaniel Vetter {
45318ff5446aSThomas Zimmermann 	int irq = to_pci_dev(dev_priv->drm.dev)->irq;
4532b318b824SVille Syrjälä 	int ret;
4533b318b824SVille Syrjälä 
45342aeb7d3aSDaniel Vetter 	/*
45352aeb7d3aSDaniel Vetter 	 * We enable some interrupt sources in our postinstall hooks, so mark
45362aeb7d3aSDaniel Vetter 	 * interrupts as enabled _before_ actually enabling them to avoid
45372aeb7d3aSDaniel Vetter 	 * special cases in our ordering checks.
45382aeb7d3aSDaniel Vetter 	 */
4539ad1443f0SSagar Arun Kamble 	dev_priv->runtime_pm.irqs_enabled = true;
45402aeb7d3aSDaniel Vetter 
4541ac1723c1SThomas Zimmermann 	dev_priv->irq_enabled = true;
4542b318b824SVille Syrjälä 
4543b318b824SVille Syrjälä 	intel_irq_reset(dev_priv);
4544b318b824SVille Syrjälä 
4545b318b824SVille Syrjälä 	ret = request_irq(irq, intel_irq_handler(dev_priv),
4546b318b824SVille Syrjälä 			  IRQF_SHARED, DRIVER_NAME, dev_priv);
4547b318b824SVille Syrjälä 	if (ret < 0) {
4548ac1723c1SThomas Zimmermann 		dev_priv->irq_enabled = false;
4549b318b824SVille Syrjälä 		return ret;
4550b318b824SVille Syrjälä 	}
4551b318b824SVille Syrjälä 
4552b318b824SVille Syrjälä 	intel_irq_postinstall(dev_priv);
4553b318b824SVille Syrjälä 
4554b318b824SVille Syrjälä 	return ret;
45552aeb7d3aSDaniel Vetter }
45562aeb7d3aSDaniel Vetter 
4557fca52a55SDaniel Vetter /**
4558fca52a55SDaniel Vetter  * intel_irq_uninstall - finilizes all irq handling
4559fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4560fca52a55SDaniel Vetter  *
4561fca52a55SDaniel Vetter  * This stops interrupt and hotplug handling and unregisters and frees all
4562fca52a55SDaniel Vetter  * resources acquired in the init functions.
4563fca52a55SDaniel Vetter  */
45642aeb7d3aSDaniel Vetter void intel_irq_uninstall(struct drm_i915_private *dev_priv)
45652aeb7d3aSDaniel Vetter {
45668ff5446aSThomas Zimmermann 	int irq = to_pci_dev(dev_priv->drm.dev)->irq;
4567b318b824SVille Syrjälä 
4568b318b824SVille Syrjälä 	/*
4569789fa874SJanusz Krzysztofik 	 * FIXME we can get called twice during driver probe
4570789fa874SJanusz Krzysztofik 	 * error handling as well as during driver remove due to
4571789fa874SJanusz Krzysztofik 	 * intel_modeset_driver_remove() calling us out of sequence.
4572789fa874SJanusz Krzysztofik 	 * Would be nice if it didn't do that...
4573b318b824SVille Syrjälä 	 */
4574ac1723c1SThomas Zimmermann 	if (!dev_priv->irq_enabled)
4575b318b824SVille Syrjälä 		return;
4576b318b824SVille Syrjälä 
4577ac1723c1SThomas Zimmermann 	dev_priv->irq_enabled = false;
4578b318b824SVille Syrjälä 
4579b318b824SVille Syrjälä 	intel_irq_reset(dev_priv);
4580b318b824SVille Syrjälä 
4581b318b824SVille Syrjälä 	free_irq(irq, dev_priv);
4582b318b824SVille Syrjälä 
45832aeb7d3aSDaniel Vetter 	intel_hpd_cancel_work(dev_priv);
4584ad1443f0SSagar Arun Kamble 	dev_priv->runtime_pm.irqs_enabled = false;
45852aeb7d3aSDaniel Vetter }
45862aeb7d3aSDaniel Vetter 
4587fca52a55SDaniel Vetter /**
4588fca52a55SDaniel Vetter  * intel_runtime_pm_disable_interrupts - runtime interrupt disabling
4589fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4590fca52a55SDaniel Vetter  *
4591fca52a55SDaniel Vetter  * This function is used to disable interrupts at runtime, both in the runtime
4592fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4593fca52a55SDaniel Vetter  */
4594b963291cSDaniel Vetter void intel_runtime_pm_disable_interrupts(struct drm_i915_private *dev_priv)
4595c67a470bSPaulo Zanoni {
4596b318b824SVille Syrjälä 	intel_irq_reset(dev_priv);
4597ad1443f0SSagar Arun Kamble 	dev_priv->runtime_pm.irqs_enabled = false;
4598315ca4c4SVille Syrjälä 	intel_synchronize_irq(dev_priv);
4599c67a470bSPaulo Zanoni }
4600c67a470bSPaulo Zanoni 
4601fca52a55SDaniel Vetter /**
4602fca52a55SDaniel Vetter  * intel_runtime_pm_enable_interrupts - runtime interrupt enabling
4603fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4604fca52a55SDaniel Vetter  *
4605fca52a55SDaniel Vetter  * This function is used to enable interrupts at runtime, both in the runtime
4606fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4607fca52a55SDaniel Vetter  */
4608b963291cSDaniel Vetter void intel_runtime_pm_enable_interrupts(struct drm_i915_private *dev_priv)
4609c67a470bSPaulo Zanoni {
4610ad1443f0SSagar Arun Kamble 	dev_priv->runtime_pm.irqs_enabled = true;
4611b318b824SVille Syrjälä 	intel_irq_reset(dev_priv);
4612b318b824SVille Syrjälä 	intel_irq_postinstall(dev_priv);
4613c67a470bSPaulo Zanoni }
4614d64575eeSJani Nikula 
4615d64575eeSJani Nikula bool intel_irqs_enabled(struct drm_i915_private *dev_priv)
4616d64575eeSJani Nikula {
4617d64575eeSJani Nikula 	return dev_priv->runtime_pm.irqs_enabled;
4618d64575eeSJani Nikula }
4619d64575eeSJani Nikula 
4620d64575eeSJani Nikula void intel_synchronize_irq(struct drm_i915_private *i915)
4621d64575eeSJani Nikula {
46228ff5446aSThomas Zimmermann 	synchronize_irq(to_pci_dev(i915->drm.dev)->irq);
4623d64575eeSJani Nikula }
4624320ad343SThomas Zimmermann 
4625320ad343SThomas Zimmermann void intel_synchronize_hardirq(struct drm_i915_private *i915)
4626320ad343SThomas Zimmermann {
4627320ad343SThomas Zimmermann 	synchronize_hardirq(to_pci_dev(i915->drm.dev)->irq);
4628320ad343SThomas Zimmermann }
4629