xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision b3786b29379c0e5d1e4e162ad5464d77aa4bc4db)
1c0e09200SDave Airlie /* i915_irq.c -- IRQ support for the I915 -*- linux-c -*-
2c0e09200SDave Airlie  */
3c0e09200SDave Airlie /*
4c0e09200SDave Airlie  * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
5c0e09200SDave Airlie  * All Rights Reserved.
6c0e09200SDave Airlie  *
7c0e09200SDave Airlie  * Permission is hereby granted, free of charge, to any person obtaining a
8c0e09200SDave Airlie  * copy of this software and associated documentation files (the
9c0e09200SDave Airlie  * "Software"), to deal in the Software without restriction, including
10c0e09200SDave Airlie  * without limitation the rights to use, copy, modify, merge, publish,
11c0e09200SDave Airlie  * distribute, sub license, and/or sell copies of the Software, and to
12c0e09200SDave Airlie  * permit persons to whom the Software is furnished to do so, subject to
13c0e09200SDave Airlie  * the following conditions:
14c0e09200SDave Airlie  *
15c0e09200SDave Airlie  * The above copyright notice and this permission notice (including the
16c0e09200SDave Airlie  * next paragraph) shall be included in all copies or substantial portions
17c0e09200SDave Airlie  * of the Software.
18c0e09200SDave Airlie  *
19c0e09200SDave Airlie  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20c0e09200SDave Airlie  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21c0e09200SDave Airlie  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22c0e09200SDave Airlie  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23c0e09200SDave Airlie  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24c0e09200SDave Airlie  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25c0e09200SDave Airlie  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26c0e09200SDave Airlie  *
27c0e09200SDave Airlie  */
28c0e09200SDave Airlie 
29a70491ccSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30a70491ccSJoe Perches 
31b2c88f5bSDamien Lespiau #include <linux/circ_buf.h>
3255367a27SJani Nikula #include <linux/slab.h>
3355367a27SJani Nikula #include <linux/sysrq.h>
3455367a27SJani Nikula 
35fcd70cd3SDaniel Vetter #include <drm/drm_drv.h>
3655367a27SJani Nikula #include <drm/drm_irq.h>
3755367a27SJani Nikula 
381d455f8dSJani Nikula #include "display/intel_display_types.h"
39df0566a6SJani Nikula #include "display/intel_fifo_underrun.h"
40df0566a6SJani Nikula #include "display/intel_hotplug.h"
41df0566a6SJani Nikula #include "display/intel_lpe_audio.h"
42df0566a6SJani Nikula #include "display/intel_psr.h"
43df0566a6SJani Nikula 
44*b3786b29SChris 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 
6348ef15d3SJosé Roberto de Souza typedef bool (*long_pulse_detect_func)(enum hpd_pin pin, u32 val);
6448ef15d3SJosé Roberto de Souza 
65e4ce95aaSVille Syrjälä static const u32 hpd_ilk[HPD_NUM_PINS] = {
66e4ce95aaSVille Syrjälä 	[HPD_PORT_A] = DE_DP_A_HOTPLUG,
67e4ce95aaSVille Syrjälä };
68e4ce95aaSVille Syrjälä 
6923bb4cb5SVille Syrjälä static const u32 hpd_ivb[HPD_NUM_PINS] = {
7023bb4cb5SVille Syrjälä 	[HPD_PORT_A] = DE_DP_A_HOTPLUG_IVB,
7123bb4cb5SVille Syrjälä };
7223bb4cb5SVille Syrjälä 
733a3b3c7dSVille Syrjälä static const u32 hpd_bdw[HPD_NUM_PINS] = {
743a3b3c7dSVille Syrjälä 	[HPD_PORT_A] = GEN8_PORT_DP_A_HOTPLUG,
753a3b3c7dSVille Syrjälä };
763a3b3c7dSVille Syrjälä 
777c7e10dbSVille Syrjälä static const u32 hpd_ibx[HPD_NUM_PINS] = {
78e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG,
79e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG,
80e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG,
81e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG,
827203d49cSVille Syrjälä 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG,
83e5868a31SEgbert Eich };
84e5868a31SEgbert Eich 
857c7e10dbSVille Syrjälä static const u32 hpd_cpt[HPD_NUM_PINS] = {
86e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG_CPT,
8773c352a2SDaniel Vetter 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG_CPT,
88e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
89e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
907203d49cSVille Syrjälä 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT,
91e5868a31SEgbert Eich };
92e5868a31SEgbert Eich 
9326951cafSXiong Zhang static const u32 hpd_spt[HPD_NUM_PINS] = {
9474c0b395SVille Syrjälä 	[HPD_PORT_A] = SDE_PORTA_HOTPLUG_SPT,
9526951cafSXiong Zhang 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
9626951cafSXiong Zhang 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
9726951cafSXiong Zhang 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT,
987203d49cSVille Syrjälä 	[HPD_PORT_E] = SDE_PORTE_HOTPLUG_SPT,
9926951cafSXiong Zhang };
10026951cafSXiong Zhang 
1017c7e10dbSVille Syrjälä static const u32 hpd_mask_i915[HPD_NUM_PINS] = {
102e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_EN,
103e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_EN,
104e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_EN,
105e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_EN,
106e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_EN,
1077203d49cSVille Syrjälä 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_EN,
108e5868a31SEgbert Eich };
109e5868a31SEgbert Eich 
1107c7e10dbSVille Syrjälä static const u32 hpd_status_g4x[HPD_NUM_PINS] = {
111e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
112e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_G4X,
113e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_G4X,
114e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
115e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
1167203d49cSVille Syrjälä 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS,
117e5868a31SEgbert Eich };
118e5868a31SEgbert Eich 
1194bca26d0SVille Syrjälä static const u32 hpd_status_i915[HPD_NUM_PINS] = {
120e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
121e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_I915,
122e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_I915,
123e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
124e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
1257203d49cSVille Syrjälä 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS,
126e5868a31SEgbert Eich };
127e5868a31SEgbert Eich 
128e0a20ad7SShashank Sharma static const u32 hpd_bxt[HPD_NUM_PINS] = {
1297f3561beSSonika Jindal 	[HPD_PORT_A] = BXT_DE_PORT_HP_DDIA,
130e0a20ad7SShashank Sharma 	[HPD_PORT_B] = BXT_DE_PORT_HP_DDIB,
1317203d49cSVille Syrjälä 	[HPD_PORT_C] = BXT_DE_PORT_HP_DDIC,
132e0a20ad7SShashank Sharma };
133e0a20ad7SShashank Sharma 
134b796b971SDhinakaran Pandiyan static const u32 hpd_gen11[HPD_NUM_PINS] = {
135b796b971SDhinakaran Pandiyan 	[HPD_PORT_C] = GEN11_TC1_HOTPLUG | GEN11_TBT1_HOTPLUG,
136b796b971SDhinakaran Pandiyan 	[HPD_PORT_D] = GEN11_TC2_HOTPLUG | GEN11_TBT2_HOTPLUG,
137b796b971SDhinakaran Pandiyan 	[HPD_PORT_E] = GEN11_TC3_HOTPLUG | GEN11_TBT3_HOTPLUG,
1387203d49cSVille Syrjälä 	[HPD_PORT_F] = GEN11_TC4_HOTPLUG | GEN11_TBT4_HOTPLUG,
139121e758eSDhinakaran Pandiyan };
140121e758eSDhinakaran Pandiyan 
14148ef15d3SJosé Roberto de Souza static const u32 hpd_gen12[HPD_NUM_PINS] = {
14248ef15d3SJosé Roberto de Souza 	[HPD_PORT_D] = GEN11_TC1_HOTPLUG | GEN11_TBT1_HOTPLUG,
14348ef15d3SJosé Roberto de Souza 	[HPD_PORT_E] = GEN11_TC2_HOTPLUG | GEN11_TBT2_HOTPLUG,
14448ef15d3SJosé Roberto de Souza 	[HPD_PORT_F] = GEN11_TC3_HOTPLUG | GEN11_TBT3_HOTPLUG,
14548ef15d3SJosé Roberto de Souza 	[HPD_PORT_G] = GEN11_TC4_HOTPLUG | GEN11_TBT4_HOTPLUG,
14648ef15d3SJosé Roberto de Souza 	[HPD_PORT_H] = GEN12_TC5_HOTPLUG | GEN12_TBT5_HOTPLUG,
1477203d49cSVille Syrjälä 	[HPD_PORT_I] = GEN12_TC6_HOTPLUG | GEN12_TBT6_HOTPLUG,
14848ef15d3SJosé Roberto de Souza };
14948ef15d3SJosé Roberto de Souza 
15031604222SAnusha Srivatsa static const u32 hpd_icp[HPD_NUM_PINS] = {
151b32821c0SLucas De Marchi 	[HPD_PORT_A] = SDE_DDI_HOTPLUG_ICP(PORT_A),
152b32821c0SLucas De Marchi 	[HPD_PORT_B] = SDE_DDI_HOTPLUG_ICP(PORT_B),
153b32821c0SLucas De Marchi 	[HPD_PORT_C] = SDE_TC_HOTPLUG_ICP(PORT_TC1),
154b32821c0SLucas De Marchi 	[HPD_PORT_D] = SDE_TC_HOTPLUG_ICP(PORT_TC2),
155b32821c0SLucas De Marchi 	[HPD_PORT_E] = SDE_TC_HOTPLUG_ICP(PORT_TC3),
156b32821c0SLucas De Marchi 	[HPD_PORT_F] = SDE_TC_HOTPLUG_ICP(PORT_TC4),
15731604222SAnusha Srivatsa };
15831604222SAnusha Srivatsa 
15952dfdba0SLucas De Marchi static const u32 hpd_tgp[HPD_NUM_PINS] = {
160b32821c0SLucas De Marchi 	[HPD_PORT_A] = SDE_DDI_HOTPLUG_ICP(PORT_A),
161b32821c0SLucas De Marchi 	[HPD_PORT_B] = SDE_DDI_HOTPLUG_ICP(PORT_B),
162b32821c0SLucas De Marchi 	[HPD_PORT_C] = SDE_DDI_HOTPLUG_ICP(PORT_C),
163b32821c0SLucas De Marchi 	[HPD_PORT_D] = SDE_TC_HOTPLUG_ICP(PORT_TC1),
164b32821c0SLucas De Marchi 	[HPD_PORT_E] = SDE_TC_HOTPLUG_ICP(PORT_TC2),
165b32821c0SLucas De Marchi 	[HPD_PORT_F] = SDE_TC_HOTPLUG_ICP(PORT_TC3),
166b32821c0SLucas De Marchi 	[HPD_PORT_G] = SDE_TC_HOTPLUG_ICP(PORT_TC4),
167b32821c0SLucas De Marchi 	[HPD_PORT_H] = SDE_TC_HOTPLUG_ICP(PORT_TC5),
168b32821c0SLucas De Marchi 	[HPD_PORT_I] = SDE_TC_HOTPLUG_ICP(PORT_TC6),
16952dfdba0SLucas De Marchi };
17052dfdba0SLucas De Marchi 
1710398993bSVille Syrjälä static void intel_hpd_init_pins(struct drm_i915_private *dev_priv)
1720398993bSVille Syrjälä {
1730398993bSVille Syrjälä 	struct i915_hotplug *hpd = &dev_priv->hotplug;
1740398993bSVille Syrjälä 
1750398993bSVille Syrjälä 	if (HAS_GMCH(dev_priv)) {
1760398993bSVille Syrjälä 		if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
1770398993bSVille Syrjälä 		    IS_CHERRYVIEW(dev_priv))
1780398993bSVille Syrjälä 			hpd->hpd = hpd_status_g4x;
1790398993bSVille Syrjälä 		else
1800398993bSVille Syrjälä 			hpd->hpd = hpd_status_i915;
1810398993bSVille Syrjälä 		return;
1820398993bSVille Syrjälä 	}
1830398993bSVille Syrjälä 
1840398993bSVille Syrjälä 	if (INTEL_GEN(dev_priv) >= 12)
1850398993bSVille Syrjälä 		hpd->hpd = hpd_gen12;
1860398993bSVille Syrjälä 	else if (INTEL_GEN(dev_priv) >= 11)
1870398993bSVille Syrjälä 		hpd->hpd = hpd_gen11;
1880398993bSVille Syrjälä 	else if (IS_GEN9_LP(dev_priv))
1890398993bSVille Syrjälä 		hpd->hpd = hpd_bxt;
1900398993bSVille Syrjälä 	else if (INTEL_GEN(dev_priv) >= 8)
1910398993bSVille Syrjälä 		hpd->hpd = hpd_bdw;
1920398993bSVille Syrjälä 	else if (INTEL_GEN(dev_priv) >= 7)
1930398993bSVille Syrjälä 		hpd->hpd = hpd_ivb;
1940398993bSVille Syrjälä 	else
1950398993bSVille Syrjälä 		hpd->hpd = hpd_ilk;
1960398993bSVille Syrjälä 
1970398993bSVille Syrjälä 	if (!HAS_PCH_SPLIT(dev_priv) || HAS_PCH_NOP(dev_priv))
1980398993bSVille Syrjälä 		return;
1990398993bSVille Syrjälä 
2000398993bSVille Syrjälä 	if (HAS_PCH_TGP(dev_priv) || HAS_PCH_JSP(dev_priv))
2010398993bSVille Syrjälä 		hpd->pch_hpd = hpd_tgp;
2020398993bSVille Syrjälä 	else if (HAS_PCH_ICP(dev_priv) || HAS_PCH_MCC(dev_priv))
2030398993bSVille Syrjälä 		hpd->pch_hpd = hpd_icp;
2040398993bSVille Syrjälä 	else if (HAS_PCH_CNP(dev_priv) || HAS_PCH_SPT(dev_priv))
2050398993bSVille Syrjälä 		hpd->pch_hpd = hpd_spt;
2060398993bSVille Syrjälä 	else if (HAS_PCH_LPT(dev_priv) || HAS_PCH_CPT(dev_priv))
2070398993bSVille Syrjälä 		hpd->pch_hpd = hpd_cpt;
2080398993bSVille Syrjälä 	else if (HAS_PCH_IBX(dev_priv))
2090398993bSVille Syrjälä 		hpd->pch_hpd = hpd_ibx;
2100398993bSVille Syrjälä 	else
2110398993bSVille Syrjälä 		MISSING_CASE(INTEL_PCH_TYPE(dev_priv));
2120398993bSVille Syrjälä }
2130398993bSVille Syrjälä 
214aca9310aSAnshuman Gupta static void
215aca9310aSAnshuman Gupta intel_handle_vblank(struct drm_i915_private *dev_priv, enum pipe pipe)
216aca9310aSAnshuman Gupta {
217aca9310aSAnshuman Gupta 	struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
218aca9310aSAnshuman Gupta 
219aca9310aSAnshuman Gupta 	drm_crtc_handle_vblank(&crtc->base);
220aca9310aSAnshuman Gupta }
221aca9310aSAnshuman Gupta 
222cf1c97dcSAndi Shyti void gen3_irq_reset(struct intel_uncore *uncore, i915_reg_t imr,
22368eb49b1SPaulo Zanoni 		    i915_reg_t iir, i915_reg_t ier)
22468eb49b1SPaulo Zanoni {
22565f42cdcSPaulo Zanoni 	intel_uncore_write(uncore, imr, 0xffffffff);
22665f42cdcSPaulo Zanoni 	intel_uncore_posting_read(uncore, imr);
22768eb49b1SPaulo Zanoni 
22865f42cdcSPaulo Zanoni 	intel_uncore_write(uncore, ier, 0);
22968eb49b1SPaulo Zanoni 
2305c502442SPaulo Zanoni 	/* IIR can theoretically queue up two events. Be paranoid. */
23165f42cdcSPaulo Zanoni 	intel_uncore_write(uncore, iir, 0xffffffff);
23265f42cdcSPaulo Zanoni 	intel_uncore_posting_read(uncore, iir);
23365f42cdcSPaulo Zanoni 	intel_uncore_write(uncore, iir, 0xffffffff);
23465f42cdcSPaulo Zanoni 	intel_uncore_posting_read(uncore, iir);
23568eb49b1SPaulo Zanoni }
2365c502442SPaulo Zanoni 
237cf1c97dcSAndi Shyti void gen2_irq_reset(struct intel_uncore *uncore)
23868eb49b1SPaulo Zanoni {
23965f42cdcSPaulo Zanoni 	intel_uncore_write16(uncore, GEN2_IMR, 0xffff);
24065f42cdcSPaulo Zanoni 	intel_uncore_posting_read16(uncore, GEN2_IMR);
241a9d356a6SPaulo Zanoni 
24265f42cdcSPaulo Zanoni 	intel_uncore_write16(uncore, GEN2_IER, 0);
24368eb49b1SPaulo Zanoni 
24468eb49b1SPaulo Zanoni 	/* IIR can theoretically queue up two events. Be paranoid. */
24565f42cdcSPaulo Zanoni 	intel_uncore_write16(uncore, GEN2_IIR, 0xffff);
24665f42cdcSPaulo Zanoni 	intel_uncore_posting_read16(uncore, GEN2_IIR);
24765f42cdcSPaulo Zanoni 	intel_uncore_write16(uncore, GEN2_IIR, 0xffff);
24865f42cdcSPaulo Zanoni 	intel_uncore_posting_read16(uncore, GEN2_IIR);
24968eb49b1SPaulo Zanoni }
25068eb49b1SPaulo Zanoni 
251337ba017SPaulo Zanoni /*
252337ba017SPaulo Zanoni  * We should clear IMR at preinstall/uninstall, and just check at postinstall.
253337ba017SPaulo Zanoni  */
25465f42cdcSPaulo Zanoni static void gen3_assert_iir_is_zero(struct intel_uncore *uncore, i915_reg_t reg)
255b51a2842SVille Syrjälä {
25665f42cdcSPaulo Zanoni 	u32 val = intel_uncore_read(uncore, reg);
257b51a2842SVille Syrjälä 
258b51a2842SVille Syrjälä 	if (val == 0)
259b51a2842SVille Syrjälä 		return;
260b51a2842SVille Syrjälä 
261a9f236d1SPankaj Bharadiya 	drm_WARN(&uncore->i915->drm, 1,
262a9f236d1SPankaj Bharadiya 		 "Interrupt register 0x%x is not zero: 0x%08x\n",
263f0f59a00SVille Syrjälä 		 i915_mmio_reg_offset(reg), val);
26465f42cdcSPaulo Zanoni 	intel_uncore_write(uncore, reg, 0xffffffff);
26565f42cdcSPaulo Zanoni 	intel_uncore_posting_read(uncore, reg);
26665f42cdcSPaulo Zanoni 	intel_uncore_write(uncore, reg, 0xffffffff);
26765f42cdcSPaulo Zanoni 	intel_uncore_posting_read(uncore, reg);
268b51a2842SVille Syrjälä }
269337ba017SPaulo Zanoni 
27065f42cdcSPaulo Zanoni static void gen2_assert_iir_is_zero(struct intel_uncore *uncore)
271e9e9848aSVille Syrjälä {
27265f42cdcSPaulo Zanoni 	u16 val = intel_uncore_read16(uncore, GEN2_IIR);
273e9e9848aSVille Syrjälä 
274e9e9848aSVille Syrjälä 	if (val == 0)
275e9e9848aSVille Syrjälä 		return;
276e9e9848aSVille Syrjälä 
277a9f236d1SPankaj Bharadiya 	drm_WARN(&uncore->i915->drm, 1,
278a9f236d1SPankaj Bharadiya 		 "Interrupt register 0x%x is not zero: 0x%08x\n",
2799d9523d8SPaulo Zanoni 		 i915_mmio_reg_offset(GEN2_IIR), val);
28065f42cdcSPaulo Zanoni 	intel_uncore_write16(uncore, GEN2_IIR, 0xffff);
28165f42cdcSPaulo Zanoni 	intel_uncore_posting_read16(uncore, GEN2_IIR);
28265f42cdcSPaulo Zanoni 	intel_uncore_write16(uncore, GEN2_IIR, 0xffff);
28365f42cdcSPaulo Zanoni 	intel_uncore_posting_read16(uncore, GEN2_IIR);
284e9e9848aSVille Syrjälä }
285e9e9848aSVille Syrjälä 
286cf1c97dcSAndi Shyti void gen3_irq_init(struct intel_uncore *uncore,
28768eb49b1SPaulo Zanoni 		   i915_reg_t imr, u32 imr_val,
28868eb49b1SPaulo Zanoni 		   i915_reg_t ier, u32 ier_val,
28968eb49b1SPaulo Zanoni 		   i915_reg_t iir)
29068eb49b1SPaulo Zanoni {
29165f42cdcSPaulo Zanoni 	gen3_assert_iir_is_zero(uncore, iir);
29235079899SPaulo Zanoni 
29365f42cdcSPaulo Zanoni 	intel_uncore_write(uncore, ier, ier_val);
29465f42cdcSPaulo Zanoni 	intel_uncore_write(uncore, imr, imr_val);
29565f42cdcSPaulo Zanoni 	intel_uncore_posting_read(uncore, imr);
29668eb49b1SPaulo Zanoni }
29735079899SPaulo Zanoni 
298cf1c97dcSAndi Shyti void gen2_irq_init(struct intel_uncore *uncore,
2992918c3caSPaulo Zanoni 		   u32 imr_val, u32 ier_val)
30068eb49b1SPaulo Zanoni {
30165f42cdcSPaulo Zanoni 	gen2_assert_iir_is_zero(uncore);
30268eb49b1SPaulo Zanoni 
30365f42cdcSPaulo Zanoni 	intel_uncore_write16(uncore, GEN2_IER, ier_val);
30465f42cdcSPaulo Zanoni 	intel_uncore_write16(uncore, GEN2_IMR, imr_val);
30565f42cdcSPaulo Zanoni 	intel_uncore_posting_read16(uncore, GEN2_IMR);
30668eb49b1SPaulo Zanoni }
30768eb49b1SPaulo Zanoni 
3080706f17cSEgbert Eich /* For display hotplug interrupt */
3090706f17cSEgbert Eich static inline void
3100706f17cSEgbert Eich i915_hotplug_interrupt_update_locked(struct drm_i915_private *dev_priv,
311a9c287c9SJani Nikula 				     u32 mask,
312a9c287c9SJani Nikula 				     u32 bits)
3130706f17cSEgbert Eich {
314a9c287c9SJani Nikula 	u32 val;
3150706f17cSEgbert Eich 
31667520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
31748a1b8d4SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, bits & ~mask);
3180706f17cSEgbert Eich 
3190706f17cSEgbert Eich 	val = I915_READ(PORT_HOTPLUG_EN);
3200706f17cSEgbert Eich 	val &= ~mask;
3210706f17cSEgbert Eich 	val |= bits;
3220706f17cSEgbert Eich 	I915_WRITE(PORT_HOTPLUG_EN, val);
3230706f17cSEgbert Eich }
3240706f17cSEgbert Eich 
3250706f17cSEgbert Eich /**
3260706f17cSEgbert Eich  * i915_hotplug_interrupt_update - update hotplug interrupt enable
3270706f17cSEgbert Eich  * @dev_priv: driver private
3280706f17cSEgbert Eich  * @mask: bits to update
3290706f17cSEgbert Eich  * @bits: bits to enable
3300706f17cSEgbert Eich  * NOTE: the HPD enable bits are modified both inside and outside
3310706f17cSEgbert Eich  * of an interrupt context. To avoid that read-modify-write cycles
3320706f17cSEgbert Eich  * interfer, these bits are protected by a spinlock. Since this
3330706f17cSEgbert Eich  * function is usually not called from a context where the lock is
3340706f17cSEgbert Eich  * held already, this function acquires the lock itself. A non-locking
3350706f17cSEgbert Eich  * version is also available.
3360706f17cSEgbert Eich  */
3370706f17cSEgbert Eich void i915_hotplug_interrupt_update(struct drm_i915_private *dev_priv,
338a9c287c9SJani Nikula 				   u32 mask,
339a9c287c9SJani Nikula 				   u32 bits)
3400706f17cSEgbert Eich {
3410706f17cSEgbert Eich 	spin_lock_irq(&dev_priv->irq_lock);
3420706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv, mask, bits);
3430706f17cSEgbert Eich 	spin_unlock_irq(&dev_priv->irq_lock);
3440706f17cSEgbert Eich }
3450706f17cSEgbert Eich 
346d9dc34f1SVille Syrjälä /**
347d9dc34f1SVille Syrjälä  * ilk_update_display_irq - update DEIMR
348d9dc34f1SVille Syrjälä  * @dev_priv: driver private
349d9dc34f1SVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
350d9dc34f1SVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
351d9dc34f1SVille Syrjälä  */
352fbdedaeaSVille Syrjälä void ilk_update_display_irq(struct drm_i915_private *dev_priv,
353a9c287c9SJani Nikula 			    u32 interrupt_mask,
354a9c287c9SJani Nikula 			    u32 enabled_irq_mask)
355036a4a7dSZhenyu Wang {
356a9c287c9SJani Nikula 	u32 new_val;
357d9dc34f1SVille Syrjälä 
35867520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
3594bc9d430SDaniel Vetter 
36048a1b8d4SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, enabled_irq_mask & ~interrupt_mask);
361d9dc34f1SVille Syrjälä 
36248a1b8d4SPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, !intel_irqs_enabled(dev_priv)))
363c67a470bSPaulo Zanoni 		return;
364c67a470bSPaulo Zanoni 
365d9dc34f1SVille Syrjälä 	new_val = dev_priv->irq_mask;
366d9dc34f1SVille Syrjälä 	new_val &= ~interrupt_mask;
367d9dc34f1SVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
368d9dc34f1SVille Syrjälä 
369d9dc34f1SVille Syrjälä 	if (new_val != dev_priv->irq_mask) {
370d9dc34f1SVille Syrjälä 		dev_priv->irq_mask = new_val;
3711ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
3723143a2bfSChris Wilson 		POSTING_READ(DEIMR);
373036a4a7dSZhenyu Wang 	}
374036a4a7dSZhenyu Wang }
375036a4a7dSZhenyu Wang 
3760961021aSBen Widawsky /**
3773a3b3c7dSVille Syrjälä  * bdw_update_port_irq - update DE port interrupt
3783a3b3c7dSVille Syrjälä  * @dev_priv: driver private
3793a3b3c7dSVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
3803a3b3c7dSVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
3813a3b3c7dSVille Syrjälä  */
3823a3b3c7dSVille Syrjälä static void bdw_update_port_irq(struct drm_i915_private *dev_priv,
383a9c287c9SJani Nikula 				u32 interrupt_mask,
384a9c287c9SJani Nikula 				u32 enabled_irq_mask)
3853a3b3c7dSVille Syrjälä {
386a9c287c9SJani Nikula 	u32 new_val;
387a9c287c9SJani Nikula 	u32 old_val;
3883a3b3c7dSVille Syrjälä 
38967520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
3903a3b3c7dSVille Syrjälä 
39148a1b8d4SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, enabled_irq_mask & ~interrupt_mask);
3923a3b3c7dSVille Syrjälä 
39348a1b8d4SPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, !intel_irqs_enabled(dev_priv)))
3943a3b3c7dSVille Syrjälä 		return;
3953a3b3c7dSVille Syrjälä 
3963a3b3c7dSVille Syrjälä 	old_val = I915_READ(GEN8_DE_PORT_IMR);
3973a3b3c7dSVille Syrjälä 
3983a3b3c7dSVille Syrjälä 	new_val = old_val;
3993a3b3c7dSVille Syrjälä 	new_val &= ~interrupt_mask;
4003a3b3c7dSVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
4013a3b3c7dSVille Syrjälä 
4023a3b3c7dSVille Syrjälä 	if (new_val != old_val) {
4033a3b3c7dSVille Syrjälä 		I915_WRITE(GEN8_DE_PORT_IMR, new_val);
4043a3b3c7dSVille Syrjälä 		POSTING_READ(GEN8_DE_PORT_IMR);
4053a3b3c7dSVille Syrjälä 	}
4063a3b3c7dSVille Syrjälä }
4073a3b3c7dSVille Syrjälä 
4083a3b3c7dSVille Syrjälä /**
409013d3752SVille Syrjälä  * bdw_update_pipe_irq - update DE pipe interrupt
410013d3752SVille Syrjälä  * @dev_priv: driver private
411013d3752SVille Syrjälä  * @pipe: pipe whose interrupt to update
412013d3752SVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
413013d3752SVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
414013d3752SVille Syrjälä  */
415013d3752SVille Syrjälä void bdw_update_pipe_irq(struct drm_i915_private *dev_priv,
416013d3752SVille Syrjälä 			 enum pipe pipe,
417a9c287c9SJani Nikula 			 u32 interrupt_mask,
418a9c287c9SJani Nikula 			 u32 enabled_irq_mask)
419013d3752SVille Syrjälä {
420a9c287c9SJani Nikula 	u32 new_val;
421013d3752SVille Syrjälä 
42267520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
423013d3752SVille Syrjälä 
42448a1b8d4SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, enabled_irq_mask & ~interrupt_mask);
425013d3752SVille Syrjälä 
42648a1b8d4SPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, !intel_irqs_enabled(dev_priv)))
427013d3752SVille Syrjälä 		return;
428013d3752SVille Syrjälä 
429013d3752SVille Syrjälä 	new_val = dev_priv->de_irq_mask[pipe];
430013d3752SVille Syrjälä 	new_val &= ~interrupt_mask;
431013d3752SVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
432013d3752SVille Syrjälä 
433013d3752SVille Syrjälä 	if (new_val != dev_priv->de_irq_mask[pipe]) {
434013d3752SVille Syrjälä 		dev_priv->de_irq_mask[pipe] = new_val;
435013d3752SVille Syrjälä 		I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
436013d3752SVille Syrjälä 		POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
437013d3752SVille Syrjälä 	}
438013d3752SVille Syrjälä }
439013d3752SVille Syrjälä 
440013d3752SVille Syrjälä /**
441fee884edSDaniel Vetter  * ibx_display_interrupt_update - update SDEIMR
442fee884edSDaniel Vetter  * @dev_priv: driver private
443fee884edSDaniel Vetter  * @interrupt_mask: mask of interrupt bits to update
444fee884edSDaniel Vetter  * @enabled_irq_mask: mask of interrupt bits to enable
445fee884edSDaniel Vetter  */
44647339cd9SDaniel Vetter void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
447a9c287c9SJani Nikula 				  u32 interrupt_mask,
448a9c287c9SJani Nikula 				  u32 enabled_irq_mask)
449fee884edSDaniel Vetter {
450a9c287c9SJani Nikula 	u32 sdeimr = I915_READ(SDEIMR);
451fee884edSDaniel Vetter 	sdeimr &= ~interrupt_mask;
452fee884edSDaniel Vetter 	sdeimr |= (~enabled_irq_mask & interrupt_mask);
453fee884edSDaniel Vetter 
45448a1b8d4SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, enabled_irq_mask & ~interrupt_mask);
45515a17aaeSDaniel Vetter 
45667520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
457fee884edSDaniel Vetter 
45848a1b8d4SPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, !intel_irqs_enabled(dev_priv)))
459c67a470bSPaulo Zanoni 		return;
460c67a470bSPaulo Zanoni 
461fee884edSDaniel Vetter 	I915_WRITE(SDEIMR, sdeimr);
462fee884edSDaniel Vetter 	POSTING_READ(SDEIMR);
463fee884edSDaniel Vetter }
4648664281bSPaulo Zanoni 
4656b12ca56SVille Syrjälä u32 i915_pipestat_enable_mask(struct drm_i915_private *dev_priv,
4666b12ca56SVille Syrjälä 			      enum pipe pipe)
4677c463586SKeith Packard {
4686b12ca56SVille Syrjälä 	u32 status_mask = dev_priv->pipestat_irq_mask[pipe];
46910c59c51SImre Deak 	u32 enable_mask = status_mask << 16;
47010c59c51SImre Deak 
4716b12ca56SVille Syrjälä 	lockdep_assert_held(&dev_priv->irq_lock);
4726b12ca56SVille Syrjälä 
4736b12ca56SVille Syrjälä 	if (INTEL_GEN(dev_priv) < 5)
4746b12ca56SVille Syrjälä 		goto out;
4756b12ca56SVille Syrjälä 
47610c59c51SImre Deak 	/*
477724a6905SVille Syrjälä 	 * On pipe A we don't support the PSR interrupt yet,
478724a6905SVille Syrjälä 	 * on pipe B and C the same bit MBZ.
47910c59c51SImre Deak 	 */
48048a1b8d4SPankaj Bharadiya 	if (drm_WARN_ON_ONCE(&dev_priv->drm,
48148a1b8d4SPankaj Bharadiya 			     status_mask & PIPE_A_PSR_STATUS_VLV))
48210c59c51SImre Deak 		return 0;
483724a6905SVille Syrjälä 	/*
484724a6905SVille Syrjälä 	 * On pipe B and C we don't support the PSR interrupt yet, on pipe
485724a6905SVille Syrjälä 	 * A the same bit is for perf counters which we don't use either.
486724a6905SVille Syrjälä 	 */
48748a1b8d4SPankaj Bharadiya 	if (drm_WARN_ON_ONCE(&dev_priv->drm,
48848a1b8d4SPankaj Bharadiya 			     status_mask & PIPE_B_PSR_STATUS_VLV))
489724a6905SVille Syrjälä 		return 0;
49010c59c51SImre Deak 
49110c59c51SImre Deak 	enable_mask &= ~(PIPE_FIFO_UNDERRUN_STATUS |
49210c59c51SImre Deak 			 SPRITE0_FLIP_DONE_INT_EN_VLV |
49310c59c51SImre Deak 			 SPRITE1_FLIP_DONE_INT_EN_VLV);
49410c59c51SImre Deak 	if (status_mask & SPRITE0_FLIP_DONE_INT_STATUS_VLV)
49510c59c51SImre Deak 		enable_mask |= SPRITE0_FLIP_DONE_INT_EN_VLV;
49610c59c51SImre Deak 	if (status_mask & SPRITE1_FLIP_DONE_INT_STATUS_VLV)
49710c59c51SImre Deak 		enable_mask |= SPRITE1_FLIP_DONE_INT_EN_VLV;
49810c59c51SImre Deak 
4996b12ca56SVille Syrjälä out:
50048a1b8d4SPankaj Bharadiya 	drm_WARN_ONCE(&dev_priv->drm,
50148a1b8d4SPankaj Bharadiya 		      enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
5026b12ca56SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
5036b12ca56SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
5046b12ca56SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask);
5056b12ca56SVille Syrjälä 
50610c59c51SImre Deak 	return enable_mask;
50710c59c51SImre Deak }
50810c59c51SImre Deak 
5096b12ca56SVille Syrjälä void i915_enable_pipestat(struct drm_i915_private *dev_priv,
5106b12ca56SVille Syrjälä 			  enum pipe pipe, u32 status_mask)
511755e9019SImre Deak {
5126b12ca56SVille Syrjälä 	i915_reg_t reg = PIPESTAT(pipe);
513755e9019SImre Deak 	u32 enable_mask;
514755e9019SImre Deak 
51548a1b8d4SPankaj Bharadiya 	drm_WARN_ONCE(&dev_priv->drm, status_mask & ~PIPESTAT_INT_STATUS_MASK,
5166b12ca56SVille Syrjälä 		      "pipe %c: status_mask=0x%x\n",
5176b12ca56SVille Syrjälä 		      pipe_name(pipe), status_mask);
5186b12ca56SVille Syrjälä 
5196b12ca56SVille Syrjälä 	lockdep_assert_held(&dev_priv->irq_lock);
52048a1b8d4SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, !intel_irqs_enabled(dev_priv));
5216b12ca56SVille Syrjälä 
5226b12ca56SVille Syrjälä 	if ((dev_priv->pipestat_irq_mask[pipe] & status_mask) == status_mask)
5236b12ca56SVille Syrjälä 		return;
5246b12ca56SVille Syrjälä 
5256b12ca56SVille Syrjälä 	dev_priv->pipestat_irq_mask[pipe] |= status_mask;
5266b12ca56SVille Syrjälä 	enable_mask = i915_pipestat_enable_mask(dev_priv, pipe);
5276b12ca56SVille Syrjälä 
5286b12ca56SVille Syrjälä 	I915_WRITE(reg, enable_mask | status_mask);
5296b12ca56SVille Syrjälä 	POSTING_READ(reg);
530755e9019SImre Deak }
531755e9019SImre Deak 
5326b12ca56SVille Syrjälä void i915_disable_pipestat(struct drm_i915_private *dev_priv,
5336b12ca56SVille Syrjälä 			   enum pipe pipe, u32 status_mask)
534755e9019SImre Deak {
5356b12ca56SVille Syrjälä 	i915_reg_t reg = PIPESTAT(pipe);
536755e9019SImre Deak 	u32 enable_mask;
537755e9019SImre Deak 
53848a1b8d4SPankaj Bharadiya 	drm_WARN_ONCE(&dev_priv->drm, status_mask & ~PIPESTAT_INT_STATUS_MASK,
5396b12ca56SVille Syrjälä 		      "pipe %c: status_mask=0x%x\n",
5406b12ca56SVille Syrjälä 		      pipe_name(pipe), status_mask);
5416b12ca56SVille Syrjälä 
5426b12ca56SVille Syrjälä 	lockdep_assert_held(&dev_priv->irq_lock);
54348a1b8d4SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, !intel_irqs_enabled(dev_priv));
5446b12ca56SVille Syrjälä 
5456b12ca56SVille Syrjälä 	if ((dev_priv->pipestat_irq_mask[pipe] & status_mask) == 0)
5466b12ca56SVille Syrjälä 		return;
5476b12ca56SVille Syrjälä 
5486b12ca56SVille Syrjälä 	dev_priv->pipestat_irq_mask[pipe] &= ~status_mask;
5496b12ca56SVille Syrjälä 	enable_mask = i915_pipestat_enable_mask(dev_priv, pipe);
5506b12ca56SVille Syrjälä 
5516b12ca56SVille Syrjälä 	I915_WRITE(reg, enable_mask | status_mask);
5526b12ca56SVille Syrjälä 	POSTING_READ(reg);
553755e9019SImre Deak }
554755e9019SImre Deak 
555f3e30485SVille Syrjälä static bool i915_has_asle(struct drm_i915_private *dev_priv)
556f3e30485SVille Syrjälä {
557f3e30485SVille Syrjälä 	if (!dev_priv->opregion.asle)
558f3e30485SVille Syrjälä 		return false;
559f3e30485SVille Syrjälä 
560f3e30485SVille Syrjälä 	return IS_PINEVIEW(dev_priv) || IS_MOBILE(dev_priv);
561f3e30485SVille Syrjälä }
562f3e30485SVille Syrjälä 
563c0e09200SDave Airlie /**
564f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
56514bb2c11STvrtko Ursulin  * @dev_priv: i915 device private
56601c66889SZhao Yakui  */
56791d14251STvrtko Ursulin static void i915_enable_asle_pipestat(struct drm_i915_private *dev_priv)
56801c66889SZhao Yakui {
569f3e30485SVille Syrjälä 	if (!i915_has_asle(dev_priv))
570f49e38ddSJani Nikula 		return;
571f49e38ddSJani Nikula 
57213321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
57301c66889SZhao Yakui 
574755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_STATUS);
57591d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 4)
5763b6c42e8SDaniel Vetter 		i915_enable_pipestat(dev_priv, PIPE_A,
577755e9019SImre Deak 				     PIPE_LEGACY_BLC_EVENT_STATUS);
5781ec14ad3SChris Wilson 
57913321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
58001c66889SZhao Yakui }
58101c66889SZhao Yakui 
582f75f3746SVille Syrjälä /*
583f75f3746SVille Syrjälä  * This timing diagram depicts the video signal in and
584f75f3746SVille Syrjälä  * around the vertical blanking period.
585f75f3746SVille Syrjälä  *
586f75f3746SVille Syrjälä  * Assumptions about the fictitious mode used in this example:
587f75f3746SVille Syrjälä  *  vblank_start >= 3
588f75f3746SVille Syrjälä  *  vsync_start = vblank_start + 1
589f75f3746SVille Syrjälä  *  vsync_end = vblank_start + 2
590f75f3746SVille Syrjälä  *  vtotal = vblank_start + 3
591f75f3746SVille Syrjälä  *
592f75f3746SVille Syrjälä  *           start of vblank:
593f75f3746SVille Syrjälä  *           latch double buffered registers
594f75f3746SVille Syrjälä  *           increment frame counter (ctg+)
595f75f3746SVille Syrjälä  *           generate start of vblank interrupt (gen4+)
596f75f3746SVille Syrjälä  *           |
597f75f3746SVille Syrjälä  *           |          frame start:
598f75f3746SVille Syrjälä  *           |          generate frame start interrupt (aka. vblank interrupt) (gmch)
599f75f3746SVille Syrjälä  *           |          may be shifted forward 1-3 extra lines via PIPECONF
600f75f3746SVille Syrjälä  *           |          |
601f75f3746SVille Syrjälä  *           |          |  start of vsync:
602f75f3746SVille Syrjälä  *           |          |  generate vsync interrupt
603f75f3746SVille Syrjälä  *           |          |  |
604f75f3746SVille Syrjälä  * ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx
605f75f3746SVille Syrjälä  *       .   \hs/   .      \hs/          \hs/          \hs/   .      \hs/
606f75f3746SVille Syrjälä  * ----va---> <-----------------vb--------------------> <--------va-------------
607f75f3746SVille Syrjälä  *       |          |       <----vs----->                     |
608f75f3746SVille Syrjälä  * -vbs-----> <---vbs+1---> <---vbs+2---> <-----0-----> <-----1-----> <-----2--- (scanline counter gen2)
609f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2---> <-----0--- (scanline counter gen3+)
610f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2- (scanline counter hsw+ hdmi)
611f75f3746SVille Syrjälä  *       |          |                                         |
612f75f3746SVille Syrjälä  *       last visible pixel                                   first visible pixel
613f75f3746SVille Syrjälä  *                  |                                         increment frame counter (gen3/4)
614f75f3746SVille Syrjälä  *                  pixel counter = vblank_start * htotal     pixel counter = 0 (gen3/4)
615f75f3746SVille Syrjälä  *
616f75f3746SVille Syrjälä  * x  = horizontal active
617f75f3746SVille Syrjälä  * _  = horizontal blanking
618f75f3746SVille Syrjälä  * hs = horizontal sync
619f75f3746SVille Syrjälä  * va = vertical active
620f75f3746SVille Syrjälä  * vb = vertical blanking
621f75f3746SVille Syrjälä  * vs = vertical sync
622f75f3746SVille Syrjälä  * vbs = vblank_start (number)
623f75f3746SVille Syrjälä  *
624f75f3746SVille Syrjälä  * Summary:
625f75f3746SVille Syrjälä  * - most events happen at the start of horizontal sync
626f75f3746SVille Syrjälä  * - frame start happens at the start of horizontal blank, 1-4 lines
627f75f3746SVille Syrjälä  *   (depending on PIPECONF settings) after the start of vblank
628f75f3746SVille Syrjälä  * - gen3/4 pixel and frame counter are synchronized with the start
629f75f3746SVille Syrjälä  *   of horizontal active on the first line of vertical active
630f75f3746SVille Syrjälä  */
631f75f3746SVille Syrjälä 
63242f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
63342f52ef8SKeith Packard  * we use as a pipe index
63442f52ef8SKeith Packard  */
63508fa8fd0SVille Syrjälä u32 i915_get_vblank_counter(struct drm_crtc *crtc)
6360a3e67a4SJesse Barnes {
63708fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
63808fa8fd0SVille Syrjälä 	struct drm_vblank_crtc *vblank = &dev_priv->drm.vblank[drm_crtc_index(crtc)];
63932db0b65SVille Syrjälä 	const struct drm_display_mode *mode = &vblank->hwmode;
64008fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
641f0f59a00SVille Syrjälä 	i915_reg_t high_frame, low_frame;
6420b2a8e09SVille Syrjälä 	u32 high1, high2, low, pixel, vbl_start, hsync_start, htotal;
643694e409dSVille Syrjälä 	unsigned long irqflags;
644391f75e2SVille Syrjälä 
64532db0b65SVille Syrjälä 	/*
64632db0b65SVille Syrjälä 	 * On i965gm TV output the frame counter only works up to
64732db0b65SVille Syrjälä 	 * the point when we enable the TV encoder. After that the
64832db0b65SVille Syrjälä 	 * frame counter ceases to work and reads zero. We need a
64932db0b65SVille Syrjälä 	 * vblank wait before enabling the TV encoder and so we
65032db0b65SVille Syrjälä 	 * have to enable vblank interrupts while the frame counter
65132db0b65SVille Syrjälä 	 * is still in a working state. However the core vblank code
65232db0b65SVille Syrjälä 	 * does not like us returning non-zero frame counter values
65332db0b65SVille Syrjälä 	 * when we've told it that we don't have a working frame
65432db0b65SVille Syrjälä 	 * counter. Thus we must stop non-zero values leaking out.
65532db0b65SVille Syrjälä 	 */
65632db0b65SVille Syrjälä 	if (!vblank->max_vblank_count)
65732db0b65SVille Syrjälä 		return 0;
65832db0b65SVille Syrjälä 
6590b2a8e09SVille Syrjälä 	htotal = mode->crtc_htotal;
6600b2a8e09SVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
6610b2a8e09SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
6620b2a8e09SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
6630b2a8e09SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
664391f75e2SVille Syrjälä 
6650b2a8e09SVille Syrjälä 	/* Convert to pixel count */
6660b2a8e09SVille Syrjälä 	vbl_start *= htotal;
6670b2a8e09SVille Syrjälä 
6680b2a8e09SVille Syrjälä 	/* Start of vblank event occurs at start of hsync */
6690b2a8e09SVille Syrjälä 	vbl_start -= htotal - hsync_start;
6700b2a8e09SVille Syrjälä 
6719db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
6729db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
6735eddb70bSChris Wilson 
674694e409dSVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
675694e409dSVille Syrjälä 
6760a3e67a4SJesse Barnes 	/*
6770a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
6780a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
6790a3e67a4SJesse Barnes 	 * register.
6800a3e67a4SJesse Barnes 	 */
6810a3e67a4SJesse Barnes 	do {
6828cbda6b2SJani Nikula 		high1 = intel_de_read_fw(dev_priv, high_frame) & PIPE_FRAME_HIGH_MASK;
6838cbda6b2SJani Nikula 		low   = intel_de_read_fw(dev_priv, low_frame);
6848cbda6b2SJani Nikula 		high2 = intel_de_read_fw(dev_priv, high_frame) & PIPE_FRAME_HIGH_MASK;
6850a3e67a4SJesse Barnes 	} while (high1 != high2);
6860a3e67a4SJesse Barnes 
687694e409dSVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
688694e409dSVille Syrjälä 
6895eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
690391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
6915eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
692391f75e2SVille Syrjälä 
693391f75e2SVille Syrjälä 	/*
694391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
695391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
696391f75e2SVille Syrjälä 	 * counter against vblank start.
697391f75e2SVille Syrjälä 	 */
698edc08d0aSVille Syrjälä 	return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
6990a3e67a4SJesse Barnes }
7000a3e67a4SJesse Barnes 
70108fa8fd0SVille Syrjälä u32 g4x_get_vblank_counter(struct drm_crtc *crtc)
7029880b7a5SJesse Barnes {
70308fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
70408fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
7059880b7a5SJesse Barnes 
706649636efSVille Syrjälä 	return I915_READ(PIPE_FRMCOUNT_G4X(pipe));
7079880b7a5SJesse Barnes }
7089880b7a5SJesse Barnes 
709aec0246fSUma Shankar /*
710aec0246fSUma Shankar  * On certain encoders on certain platforms, pipe
711aec0246fSUma Shankar  * scanline register will not work to get the scanline,
712aec0246fSUma Shankar  * since the timings are driven from the PORT or issues
713aec0246fSUma Shankar  * with scanline register updates.
714aec0246fSUma Shankar  * This function will use Framestamp and current
715aec0246fSUma Shankar  * timestamp registers to calculate the scanline.
716aec0246fSUma Shankar  */
717aec0246fSUma Shankar static u32 __intel_get_crtc_scanline_from_timestamp(struct intel_crtc *crtc)
718aec0246fSUma Shankar {
719aec0246fSUma Shankar 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
720aec0246fSUma Shankar 	struct drm_vblank_crtc *vblank =
721aec0246fSUma Shankar 		&crtc->base.dev->vblank[drm_crtc_index(&crtc->base)];
722aec0246fSUma Shankar 	const struct drm_display_mode *mode = &vblank->hwmode;
723aec0246fSUma Shankar 	u32 vblank_start = mode->crtc_vblank_start;
724aec0246fSUma Shankar 	u32 vtotal = mode->crtc_vtotal;
725aec0246fSUma Shankar 	u32 htotal = mode->crtc_htotal;
726aec0246fSUma Shankar 	u32 clock = mode->crtc_clock;
727aec0246fSUma Shankar 	u32 scanline, scan_prev_time, scan_curr_time, scan_post_time;
728aec0246fSUma Shankar 
729aec0246fSUma Shankar 	/*
730aec0246fSUma Shankar 	 * To avoid the race condition where we might cross into the
731aec0246fSUma Shankar 	 * next vblank just between the PIPE_FRMTMSTMP and TIMESTAMP_CTR
732aec0246fSUma Shankar 	 * reads. We make sure we read PIPE_FRMTMSTMP and TIMESTAMP_CTR
733aec0246fSUma Shankar 	 * during the same frame.
734aec0246fSUma Shankar 	 */
735aec0246fSUma Shankar 	do {
736aec0246fSUma Shankar 		/*
737aec0246fSUma Shankar 		 * This field provides read back of the display
738aec0246fSUma Shankar 		 * pipe frame time stamp. The time stamp value
739aec0246fSUma Shankar 		 * is sampled at every start of vertical blank.
740aec0246fSUma Shankar 		 */
7418cbda6b2SJani Nikula 		scan_prev_time = intel_de_read_fw(dev_priv,
7428cbda6b2SJani Nikula 						  PIPE_FRMTMSTMP(crtc->pipe));
743aec0246fSUma Shankar 
744aec0246fSUma Shankar 		/*
745aec0246fSUma Shankar 		 * The TIMESTAMP_CTR register has the current
746aec0246fSUma Shankar 		 * time stamp value.
747aec0246fSUma Shankar 		 */
7488cbda6b2SJani Nikula 		scan_curr_time = intel_de_read_fw(dev_priv, IVB_TIMESTAMP_CTR);
749aec0246fSUma Shankar 
7508cbda6b2SJani Nikula 		scan_post_time = intel_de_read_fw(dev_priv,
7518cbda6b2SJani Nikula 						  PIPE_FRMTMSTMP(crtc->pipe));
752aec0246fSUma Shankar 	} while (scan_post_time != scan_prev_time);
753aec0246fSUma Shankar 
754aec0246fSUma Shankar 	scanline = div_u64(mul_u32_u32(scan_curr_time - scan_prev_time,
755aec0246fSUma Shankar 					clock), 1000 * htotal);
756aec0246fSUma Shankar 	scanline = min(scanline, vtotal - 1);
757aec0246fSUma Shankar 	scanline = (scanline + vblank_start) % vtotal;
758aec0246fSUma Shankar 
759aec0246fSUma Shankar 	return scanline;
760aec0246fSUma Shankar }
761aec0246fSUma Shankar 
7628cbda6b2SJani Nikula /*
7638cbda6b2SJani Nikula  * intel_de_read_fw(), only for fast reads of display block, no need for
7648cbda6b2SJani Nikula  * forcewake etc.
7658cbda6b2SJani Nikula  */
766a225f079SVille Syrjälä static int __intel_get_crtc_scanline(struct intel_crtc *crtc)
767a225f079SVille Syrjälä {
768a225f079SVille Syrjälä 	struct drm_device *dev = crtc->base.dev;
769fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
7705caa0feaSDaniel Vetter 	const struct drm_display_mode *mode;
7715caa0feaSDaniel Vetter 	struct drm_vblank_crtc *vblank;
772a225f079SVille Syrjälä 	enum pipe pipe = crtc->pipe;
77380715b2fSVille Syrjälä 	int position, vtotal;
774a225f079SVille Syrjälä 
77572259536SVille Syrjälä 	if (!crtc->active)
77672259536SVille Syrjälä 		return -1;
77772259536SVille Syrjälä 
7785caa0feaSDaniel Vetter 	vblank = &crtc->base.dev->vblank[drm_crtc_index(&crtc->base)];
7795caa0feaSDaniel Vetter 	mode = &vblank->hwmode;
7805caa0feaSDaniel Vetter 
781af157b76SVille Syrjälä 	if (crtc->mode_flags & I915_MODE_FLAG_GET_SCANLINE_FROM_TIMESTAMP)
782aec0246fSUma Shankar 		return __intel_get_crtc_scanline_from_timestamp(crtc);
783aec0246fSUma Shankar 
78480715b2fSVille Syrjälä 	vtotal = mode->crtc_vtotal;
785a225f079SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
786a225f079SVille Syrjälä 		vtotal /= 2;
787a225f079SVille Syrjälä 
788cf819effSLucas De Marchi 	if (IS_GEN(dev_priv, 2))
7898cbda6b2SJani Nikula 		position = intel_de_read_fw(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
790a225f079SVille Syrjälä 	else
7918cbda6b2SJani Nikula 		position = intel_de_read_fw(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
792a225f079SVille Syrjälä 
793a225f079SVille Syrjälä 	/*
79441b578fbSJesse Barnes 	 * On HSW, the DSL reg (0x70000) appears to return 0 if we
79541b578fbSJesse Barnes 	 * read it just before the start of vblank.  So try it again
79641b578fbSJesse Barnes 	 * so we don't accidentally end up spanning a vblank frame
79741b578fbSJesse Barnes 	 * increment, causing the pipe_update_end() code to squak at us.
79841b578fbSJesse Barnes 	 *
79941b578fbSJesse Barnes 	 * The nature of this problem means we can't simply check the ISR
80041b578fbSJesse Barnes 	 * bit and return the vblank start value; nor can we use the scanline
80141b578fbSJesse Barnes 	 * debug register in the transcoder as it appears to have the same
80241b578fbSJesse Barnes 	 * problem.  We may need to extend this to include other platforms,
80341b578fbSJesse Barnes 	 * but so far testing only shows the problem on HSW.
80441b578fbSJesse Barnes 	 */
80591d14251STvrtko Ursulin 	if (HAS_DDI(dev_priv) && !position) {
80641b578fbSJesse Barnes 		int i, temp;
80741b578fbSJesse Barnes 
80841b578fbSJesse Barnes 		for (i = 0; i < 100; i++) {
80941b578fbSJesse Barnes 			udelay(1);
8108cbda6b2SJani Nikula 			temp = intel_de_read_fw(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
81141b578fbSJesse Barnes 			if (temp != position) {
81241b578fbSJesse Barnes 				position = temp;
81341b578fbSJesse Barnes 				break;
81441b578fbSJesse Barnes 			}
81541b578fbSJesse Barnes 		}
81641b578fbSJesse Barnes 	}
81741b578fbSJesse Barnes 
81841b578fbSJesse Barnes 	/*
81980715b2fSVille Syrjälä 	 * See update_scanline_offset() for the details on the
82080715b2fSVille Syrjälä 	 * scanline_offset adjustment.
821a225f079SVille Syrjälä 	 */
82280715b2fSVille Syrjälä 	return (position + crtc->scanline_offset) % vtotal;
823a225f079SVille Syrjälä }
824a225f079SVille Syrjälä 
8254bbffbf3SThomas Zimmermann static bool i915_get_crtc_scanoutpos(struct drm_crtc *_crtc,
8264bbffbf3SThomas Zimmermann 				     bool in_vblank_irq,
8274bbffbf3SThomas Zimmermann 				     int *vpos, int *hpos,
8283bb403bfSVille Syrjälä 				     ktime_t *stime, ktime_t *etime,
8293bb403bfSVille Syrjälä 				     const struct drm_display_mode *mode)
8300af7e4dfSMario Kleiner {
8314bbffbf3SThomas Zimmermann 	struct drm_device *dev = _crtc->dev;
832fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
8334bbffbf3SThomas Zimmermann 	struct intel_crtc *crtc = to_intel_crtc(_crtc);
834e8edae54SVille Syrjälä 	enum pipe pipe = crtc->pipe;
8353aa18df8SVille Syrjälä 	int position;
83678e8fc6bSVille Syrjälä 	int vbl_start, vbl_end, hsync_start, htotal, vtotal;
837ad3543edSMario Kleiner 	unsigned long irqflags;
8388a920e24SVille Syrjälä 	bool use_scanline_counter = INTEL_GEN(dev_priv) >= 5 ||
8398a920e24SVille Syrjälä 		IS_G4X(dev_priv) || IS_GEN(dev_priv, 2) ||
840af157b76SVille Syrjälä 		crtc->mode_flags & I915_MODE_FLAG_USE_SCANLINE_COUNTER;
8410af7e4dfSMario Kleiner 
84248a1b8d4SPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, !mode->crtc_clock)) {
84300376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm,
84400376ccfSWambui Karuga 			"trying to get scanoutpos for disabled "
8459db4a9c7SJesse Barnes 			"pipe %c\n", pipe_name(pipe));
8461bf6ad62SDaniel Vetter 		return false;
8470af7e4dfSMario Kleiner 	}
8480af7e4dfSMario Kleiner 
849c2baf4b7SVille Syrjälä 	htotal = mode->crtc_htotal;
85078e8fc6bSVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
851c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
852c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
853c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
8540af7e4dfSMario Kleiner 
855d31faf65SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
856d31faf65SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
857d31faf65SVille Syrjälä 		vbl_end /= 2;
858d31faf65SVille Syrjälä 		vtotal /= 2;
859d31faf65SVille Syrjälä 	}
860d31faf65SVille Syrjälä 
861ad3543edSMario Kleiner 	/*
862ad3543edSMario Kleiner 	 * Lock uncore.lock, as we will do multiple timing critical raw
863ad3543edSMario Kleiner 	 * register reads, potentially with preemption disabled, so the
864ad3543edSMario Kleiner 	 * following code must not block on uncore.lock.
865ad3543edSMario Kleiner 	 */
866ad3543edSMario Kleiner 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
867ad3543edSMario Kleiner 
868ad3543edSMario Kleiner 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
869ad3543edSMario Kleiner 
870ad3543edSMario Kleiner 	/* Get optional system timestamp before query. */
871ad3543edSMario Kleiner 	if (stime)
872ad3543edSMario Kleiner 		*stime = ktime_get();
873ad3543edSMario Kleiner 
8748a920e24SVille Syrjälä 	if (use_scanline_counter) {
8750af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
8760af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
8770af7e4dfSMario Kleiner 		 */
878e8edae54SVille Syrjälä 		position = __intel_get_crtc_scanline(crtc);
8790af7e4dfSMario Kleiner 	} else {
8800af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
8810af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
8820af7e4dfSMario Kleiner 		 * scanout position.
8830af7e4dfSMario Kleiner 		 */
8848cbda6b2SJani Nikula 		position = (intel_de_read_fw(dev_priv, PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
8850af7e4dfSMario Kleiner 
8863aa18df8SVille Syrjälä 		/* convert to pixel counts */
8873aa18df8SVille Syrjälä 		vbl_start *= htotal;
8883aa18df8SVille Syrjälä 		vbl_end *= htotal;
8893aa18df8SVille Syrjälä 		vtotal *= htotal;
89078e8fc6bSVille Syrjälä 
89178e8fc6bSVille Syrjälä 		/*
8927e78f1cbSVille Syrjälä 		 * In interlaced modes, the pixel counter counts all pixels,
8937e78f1cbSVille Syrjälä 		 * so one field will have htotal more pixels. In order to avoid
8947e78f1cbSVille Syrjälä 		 * the reported position from jumping backwards when the pixel
8957e78f1cbSVille Syrjälä 		 * counter is beyond the length of the shorter field, just
8967e78f1cbSVille Syrjälä 		 * clamp the position the length of the shorter field. This
8977e78f1cbSVille Syrjälä 		 * matches how the scanline counter based position works since
8987e78f1cbSVille Syrjälä 		 * the scanline counter doesn't count the two half lines.
8997e78f1cbSVille Syrjälä 		 */
9007e78f1cbSVille Syrjälä 		if (position >= vtotal)
9017e78f1cbSVille Syrjälä 			position = vtotal - 1;
9027e78f1cbSVille Syrjälä 
9037e78f1cbSVille Syrjälä 		/*
90478e8fc6bSVille Syrjälä 		 * Start of vblank interrupt is triggered at start of hsync,
90578e8fc6bSVille Syrjälä 		 * just prior to the first active line of vblank. However we
90678e8fc6bSVille Syrjälä 		 * consider lines to start at the leading edge of horizontal
90778e8fc6bSVille Syrjälä 		 * active. So, should we get here before we've crossed into
90878e8fc6bSVille Syrjälä 		 * the horizontal active of the first line in vblank, we would
90978e8fc6bSVille Syrjälä 		 * not set the DRM_SCANOUTPOS_INVBL flag. In order to fix that,
91078e8fc6bSVille Syrjälä 		 * always add htotal-hsync_start to the current pixel position.
91178e8fc6bSVille Syrjälä 		 */
91278e8fc6bSVille Syrjälä 		position = (position + htotal - hsync_start) % vtotal;
9133aa18df8SVille Syrjälä 	}
9143aa18df8SVille Syrjälä 
915ad3543edSMario Kleiner 	/* Get optional system timestamp after query. */
916ad3543edSMario Kleiner 	if (etime)
917ad3543edSMario Kleiner 		*etime = ktime_get();
918ad3543edSMario Kleiner 
919ad3543edSMario Kleiner 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
920ad3543edSMario Kleiner 
921ad3543edSMario Kleiner 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
922ad3543edSMario Kleiner 
9233aa18df8SVille Syrjälä 	/*
9243aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
9253aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
9263aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
9273aa18df8SVille Syrjälä 	 * up since vbl_end.
9283aa18df8SVille Syrjälä 	 */
9293aa18df8SVille Syrjälä 	if (position >= vbl_start)
9303aa18df8SVille Syrjälä 		position -= vbl_end;
9313aa18df8SVille Syrjälä 	else
9323aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
9333aa18df8SVille Syrjälä 
9348a920e24SVille Syrjälä 	if (use_scanline_counter) {
9353aa18df8SVille Syrjälä 		*vpos = position;
9363aa18df8SVille Syrjälä 		*hpos = 0;
9373aa18df8SVille Syrjälä 	} else {
9380af7e4dfSMario Kleiner 		*vpos = position / htotal;
9390af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
9400af7e4dfSMario Kleiner 	}
9410af7e4dfSMario Kleiner 
9421bf6ad62SDaniel Vetter 	return true;
9430af7e4dfSMario Kleiner }
9440af7e4dfSMario Kleiner 
9454bbffbf3SThomas Zimmermann bool intel_crtc_get_vblank_timestamp(struct drm_crtc *crtc, int *max_error,
9464bbffbf3SThomas Zimmermann 				     ktime_t *vblank_time, bool in_vblank_irq)
9474bbffbf3SThomas Zimmermann {
9484bbffbf3SThomas Zimmermann 	return drm_crtc_vblank_helper_get_vblank_timestamp_internal(
9494bbffbf3SThomas Zimmermann 		crtc, max_error, vblank_time, in_vblank_irq,
95048e67807SThomas Zimmermann 		i915_get_crtc_scanoutpos);
9514bbffbf3SThomas Zimmermann }
9524bbffbf3SThomas Zimmermann 
953a225f079SVille Syrjälä int intel_get_crtc_scanline(struct intel_crtc *crtc)
954a225f079SVille Syrjälä {
955fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
956a225f079SVille Syrjälä 	unsigned long irqflags;
957a225f079SVille Syrjälä 	int position;
958a225f079SVille Syrjälä 
959a225f079SVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
960a225f079SVille Syrjälä 	position = __intel_get_crtc_scanline(crtc);
961a225f079SVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
962a225f079SVille Syrjälä 
963a225f079SVille Syrjälä 	return position;
964a225f079SVille Syrjälä }
965a225f079SVille Syrjälä 
966e3689190SBen Widawsky /**
96774bb98baSLucas De Marchi  * ivb_parity_work - Workqueue called when a parity error interrupt
968e3689190SBen Widawsky  * occurred.
969e3689190SBen Widawsky  * @work: workqueue struct
970e3689190SBen Widawsky  *
971e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
972e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
973e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
974e3689190SBen Widawsky  */
97574bb98baSLucas De Marchi static void ivb_parity_work(struct work_struct *work)
976e3689190SBen Widawsky {
9772d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
978cefcff8fSJoonas Lahtinen 		container_of(work, typeof(*dev_priv), l3_parity.error_work);
979cf1c97dcSAndi Shyti 	struct intel_gt *gt = &dev_priv->gt;
980e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
98135a85ac6SBen Widawsky 	char *parity_event[6];
982a9c287c9SJani Nikula 	u32 misccpctl;
983a9c287c9SJani Nikula 	u8 slice = 0;
984e3689190SBen Widawsky 
985e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
986e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
987e3689190SBen Widawsky 	 * any time we access those registers.
988e3689190SBen Widawsky 	 */
98991c8a326SChris Wilson 	mutex_lock(&dev_priv->drm.struct_mutex);
990e3689190SBen Widawsky 
99135a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
99248a1b8d4SPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, !dev_priv->l3_parity.which_slice))
99335a85ac6SBen Widawsky 		goto out;
99435a85ac6SBen Widawsky 
995e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
996e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
997e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
998e3689190SBen Widawsky 
99935a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
1000f0f59a00SVille Syrjälä 		i915_reg_t reg;
100135a85ac6SBen Widawsky 
100235a85ac6SBen Widawsky 		slice--;
100348a1b8d4SPankaj Bharadiya 		if (drm_WARN_ON_ONCE(&dev_priv->drm,
100448a1b8d4SPankaj Bharadiya 				     slice >= NUM_L3_SLICES(dev_priv)))
100535a85ac6SBen Widawsky 			break;
100635a85ac6SBen Widawsky 
100735a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
100835a85ac6SBen Widawsky 
10096fa1c5f1SVille Syrjälä 		reg = GEN7_L3CDERRST1(slice);
101035a85ac6SBen Widawsky 
101135a85ac6SBen Widawsky 		error_status = I915_READ(reg);
1012e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
1013e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
1014e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1015e3689190SBen Widawsky 
101635a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
101735a85ac6SBen Widawsky 		POSTING_READ(reg);
1018e3689190SBen Widawsky 
1019cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1020e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1021e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1022e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
102335a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
102435a85ac6SBen Widawsky 		parity_event[5] = NULL;
1025e3689190SBen Widawsky 
102691c8a326SChris Wilson 		kobject_uevent_env(&dev_priv->drm.primary->kdev->kobj,
1027e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
1028e3689190SBen Widawsky 
102935a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
103035a85ac6SBen Widawsky 			  slice, row, bank, subbank);
1031e3689190SBen Widawsky 
103235a85ac6SBen Widawsky 		kfree(parity_event[4]);
1033e3689190SBen Widawsky 		kfree(parity_event[3]);
1034e3689190SBen Widawsky 		kfree(parity_event[2]);
1035e3689190SBen Widawsky 		kfree(parity_event[1]);
1036e3689190SBen Widawsky 	}
1037e3689190SBen Widawsky 
103835a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
103935a85ac6SBen Widawsky 
104035a85ac6SBen Widawsky out:
104148a1b8d4SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, dev_priv->l3_parity.which_slice);
1042cf1c97dcSAndi Shyti 	spin_lock_irq(&gt->irq_lock);
1043cf1c97dcSAndi Shyti 	gen5_gt_enable_irq(gt, GT_PARITY_ERROR(dev_priv));
1044cf1c97dcSAndi Shyti 	spin_unlock_irq(&gt->irq_lock);
104535a85ac6SBen Widawsky 
104691c8a326SChris Wilson 	mutex_unlock(&dev_priv->drm.struct_mutex);
104735a85ac6SBen Widawsky }
104835a85ac6SBen Widawsky 
1049af92058fSVille Syrjälä static bool gen11_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
1050121e758eSDhinakaran Pandiyan {
1051af92058fSVille Syrjälä 	switch (pin) {
1052af92058fSVille Syrjälä 	case HPD_PORT_C:
1053121e758eSDhinakaran Pandiyan 		return val & GEN11_HOTPLUG_CTL_LONG_DETECT(PORT_TC1);
1054af92058fSVille Syrjälä 	case HPD_PORT_D:
1055121e758eSDhinakaran Pandiyan 		return val & GEN11_HOTPLUG_CTL_LONG_DETECT(PORT_TC2);
1056af92058fSVille Syrjälä 	case HPD_PORT_E:
1057121e758eSDhinakaran Pandiyan 		return val & GEN11_HOTPLUG_CTL_LONG_DETECT(PORT_TC3);
1058af92058fSVille Syrjälä 	case HPD_PORT_F:
1059121e758eSDhinakaran Pandiyan 		return val & GEN11_HOTPLUG_CTL_LONG_DETECT(PORT_TC4);
1060121e758eSDhinakaran Pandiyan 	default:
1061121e758eSDhinakaran Pandiyan 		return false;
1062121e758eSDhinakaran Pandiyan 	}
1063121e758eSDhinakaran Pandiyan }
1064121e758eSDhinakaran Pandiyan 
106548ef15d3SJosé Roberto de Souza static bool gen12_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
106648ef15d3SJosé Roberto de Souza {
106748ef15d3SJosé Roberto de Souza 	switch (pin) {
106848ef15d3SJosé Roberto de Souza 	case HPD_PORT_D:
106948ef15d3SJosé Roberto de Souza 		return val & GEN11_HOTPLUG_CTL_LONG_DETECT(PORT_TC1);
107048ef15d3SJosé Roberto de Souza 	case HPD_PORT_E:
107148ef15d3SJosé Roberto de Souza 		return val & GEN11_HOTPLUG_CTL_LONG_DETECT(PORT_TC2);
107248ef15d3SJosé Roberto de Souza 	case HPD_PORT_F:
107348ef15d3SJosé Roberto de Souza 		return val & GEN11_HOTPLUG_CTL_LONG_DETECT(PORT_TC3);
107448ef15d3SJosé Roberto de Souza 	case HPD_PORT_G:
107548ef15d3SJosé Roberto de Souza 		return val & GEN11_HOTPLUG_CTL_LONG_DETECT(PORT_TC4);
107648ef15d3SJosé Roberto de Souza 	case HPD_PORT_H:
107748ef15d3SJosé Roberto de Souza 		return val & GEN11_HOTPLUG_CTL_LONG_DETECT(PORT_TC5);
107848ef15d3SJosé Roberto de Souza 	case HPD_PORT_I:
107948ef15d3SJosé Roberto de Souza 		return val & GEN11_HOTPLUG_CTL_LONG_DETECT(PORT_TC6);
108048ef15d3SJosé Roberto de Souza 	default:
108148ef15d3SJosé Roberto de Souza 		return false;
108248ef15d3SJosé Roberto de Souza 	}
108348ef15d3SJosé Roberto de Souza }
108448ef15d3SJosé Roberto de Souza 
1085af92058fSVille Syrjälä static bool bxt_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
108663c88d22SImre Deak {
1087af92058fSVille Syrjälä 	switch (pin) {
1088af92058fSVille Syrjälä 	case HPD_PORT_A:
1089195baa06SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
1090af92058fSVille Syrjälä 	case HPD_PORT_B:
109163c88d22SImre Deak 		return val & PORTB_HOTPLUG_LONG_DETECT;
1092af92058fSVille Syrjälä 	case HPD_PORT_C:
109363c88d22SImre Deak 		return val & PORTC_HOTPLUG_LONG_DETECT;
109463c88d22SImre Deak 	default:
109563c88d22SImre Deak 		return false;
109663c88d22SImre Deak 	}
109763c88d22SImre Deak }
109863c88d22SImre Deak 
1099af92058fSVille Syrjälä static bool icp_ddi_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
110031604222SAnusha Srivatsa {
1101af92058fSVille Syrjälä 	switch (pin) {
1102af92058fSVille Syrjälä 	case HPD_PORT_A:
1103ed3126faSLucas De Marchi 		return val & SHOTPLUG_CTL_DDI_HPD_LONG_DETECT(PORT_A);
1104af92058fSVille Syrjälä 	case HPD_PORT_B:
1105ed3126faSLucas De Marchi 		return val & SHOTPLUG_CTL_DDI_HPD_LONG_DETECT(PORT_B);
11068ef7e340SMatt Roper 	case HPD_PORT_C:
1107ed3126faSLucas De Marchi 		return val & SHOTPLUG_CTL_DDI_HPD_LONG_DETECT(PORT_C);
110831604222SAnusha Srivatsa 	default:
110931604222SAnusha Srivatsa 		return false;
111031604222SAnusha Srivatsa 	}
111131604222SAnusha Srivatsa }
111231604222SAnusha Srivatsa 
1113af92058fSVille Syrjälä static bool icp_tc_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
111431604222SAnusha Srivatsa {
1115af92058fSVille Syrjälä 	switch (pin) {
1116af92058fSVille Syrjälä 	case HPD_PORT_C:
111731604222SAnusha Srivatsa 		return val & ICP_TC_HPD_LONG_DETECT(PORT_TC1);
1118af92058fSVille Syrjälä 	case HPD_PORT_D:
111931604222SAnusha Srivatsa 		return val & ICP_TC_HPD_LONG_DETECT(PORT_TC2);
1120af92058fSVille Syrjälä 	case HPD_PORT_E:
112131604222SAnusha Srivatsa 		return val & ICP_TC_HPD_LONG_DETECT(PORT_TC3);
1122af92058fSVille Syrjälä 	case HPD_PORT_F:
112331604222SAnusha Srivatsa 		return val & ICP_TC_HPD_LONG_DETECT(PORT_TC4);
112431604222SAnusha Srivatsa 	default:
112531604222SAnusha Srivatsa 		return false;
112631604222SAnusha Srivatsa 	}
112731604222SAnusha Srivatsa }
112831604222SAnusha Srivatsa 
112952dfdba0SLucas De Marchi static bool tgp_tc_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
113052dfdba0SLucas De Marchi {
113152dfdba0SLucas De Marchi 	switch (pin) {
113252dfdba0SLucas De Marchi 	case HPD_PORT_D:
113352dfdba0SLucas De Marchi 		return val & ICP_TC_HPD_LONG_DETECT(PORT_TC1);
113452dfdba0SLucas De Marchi 	case HPD_PORT_E:
113552dfdba0SLucas De Marchi 		return val & ICP_TC_HPD_LONG_DETECT(PORT_TC2);
113652dfdba0SLucas De Marchi 	case HPD_PORT_F:
113752dfdba0SLucas De Marchi 		return val & ICP_TC_HPD_LONG_DETECT(PORT_TC3);
113852dfdba0SLucas De Marchi 	case HPD_PORT_G:
113952dfdba0SLucas De Marchi 		return val & ICP_TC_HPD_LONG_DETECT(PORT_TC4);
114052dfdba0SLucas De Marchi 	case HPD_PORT_H:
114152dfdba0SLucas De Marchi 		return val & ICP_TC_HPD_LONG_DETECT(PORT_TC5);
114252dfdba0SLucas De Marchi 	case HPD_PORT_I:
114352dfdba0SLucas De Marchi 		return val & ICP_TC_HPD_LONG_DETECT(PORT_TC6);
114452dfdba0SLucas De Marchi 	default:
114552dfdba0SLucas De Marchi 		return false;
114652dfdba0SLucas De Marchi 	}
114752dfdba0SLucas De Marchi }
114852dfdba0SLucas De Marchi 
1149af92058fSVille Syrjälä static bool spt_port_hotplug2_long_detect(enum hpd_pin pin, u32 val)
11506dbf30ceSVille Syrjälä {
1151af92058fSVille Syrjälä 	switch (pin) {
1152af92058fSVille Syrjälä 	case HPD_PORT_E:
11536dbf30ceSVille Syrjälä 		return val & PORTE_HOTPLUG_LONG_DETECT;
11546dbf30ceSVille Syrjälä 	default:
11556dbf30ceSVille Syrjälä 		return false;
11566dbf30ceSVille Syrjälä 	}
11576dbf30ceSVille Syrjälä }
11586dbf30ceSVille Syrjälä 
1159af92058fSVille Syrjälä static bool spt_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
116074c0b395SVille Syrjälä {
1161af92058fSVille Syrjälä 	switch (pin) {
1162af92058fSVille Syrjälä 	case HPD_PORT_A:
116374c0b395SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
1164af92058fSVille Syrjälä 	case HPD_PORT_B:
116574c0b395SVille Syrjälä 		return val & PORTB_HOTPLUG_LONG_DETECT;
1166af92058fSVille Syrjälä 	case HPD_PORT_C:
116774c0b395SVille Syrjälä 		return val & PORTC_HOTPLUG_LONG_DETECT;
1168af92058fSVille Syrjälä 	case HPD_PORT_D:
116974c0b395SVille Syrjälä 		return val & PORTD_HOTPLUG_LONG_DETECT;
117074c0b395SVille Syrjälä 	default:
117174c0b395SVille Syrjälä 		return false;
117274c0b395SVille Syrjälä 	}
117374c0b395SVille Syrjälä }
117474c0b395SVille Syrjälä 
1175af92058fSVille Syrjälä static bool ilk_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
1176e4ce95aaSVille Syrjälä {
1177af92058fSVille Syrjälä 	switch (pin) {
1178af92058fSVille Syrjälä 	case HPD_PORT_A:
1179e4ce95aaSVille Syrjälä 		return val & DIGITAL_PORTA_HOTPLUG_LONG_DETECT;
1180e4ce95aaSVille Syrjälä 	default:
1181e4ce95aaSVille Syrjälä 		return false;
1182e4ce95aaSVille Syrjälä 	}
1183e4ce95aaSVille Syrjälä }
1184e4ce95aaSVille Syrjälä 
1185af92058fSVille Syrjälä static bool pch_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
118613cf5504SDave Airlie {
1187af92058fSVille Syrjälä 	switch (pin) {
1188af92058fSVille Syrjälä 	case HPD_PORT_B:
1189676574dfSJani Nikula 		return val & PORTB_HOTPLUG_LONG_DETECT;
1190af92058fSVille Syrjälä 	case HPD_PORT_C:
1191676574dfSJani Nikula 		return val & PORTC_HOTPLUG_LONG_DETECT;
1192af92058fSVille Syrjälä 	case HPD_PORT_D:
1193676574dfSJani Nikula 		return val & PORTD_HOTPLUG_LONG_DETECT;
1194676574dfSJani Nikula 	default:
1195676574dfSJani Nikula 		return false;
119613cf5504SDave Airlie 	}
119713cf5504SDave Airlie }
119813cf5504SDave Airlie 
1199af92058fSVille Syrjälä static bool i9xx_port_hotplug_long_detect(enum hpd_pin pin, u32 val)
120013cf5504SDave Airlie {
1201af92058fSVille Syrjälä 	switch (pin) {
1202af92058fSVille Syrjälä 	case HPD_PORT_B:
1203676574dfSJani Nikula 		return val & PORTB_HOTPLUG_INT_LONG_PULSE;
1204af92058fSVille Syrjälä 	case HPD_PORT_C:
1205676574dfSJani Nikula 		return val & PORTC_HOTPLUG_INT_LONG_PULSE;
1206af92058fSVille Syrjälä 	case HPD_PORT_D:
1207676574dfSJani Nikula 		return val & PORTD_HOTPLUG_INT_LONG_PULSE;
1208676574dfSJani Nikula 	default:
1209676574dfSJani Nikula 		return false;
121013cf5504SDave Airlie 	}
121113cf5504SDave Airlie }
121213cf5504SDave Airlie 
121342db67d6SVille Syrjälä /*
121442db67d6SVille Syrjälä  * Get a bit mask of pins that have triggered, and which ones may be long.
121542db67d6SVille Syrjälä  * This can be called multiple times with the same masks to accumulate
121642db67d6SVille Syrjälä  * hotplug detection results from several registers.
121742db67d6SVille Syrjälä  *
121842db67d6SVille Syrjälä  * Note that the caller is expected to zero out the masks initially.
121942db67d6SVille Syrjälä  */
1220cf53902fSRodrigo Vivi static void intel_get_hpd_pins(struct drm_i915_private *dev_priv,
1221cf53902fSRodrigo Vivi 			       u32 *pin_mask, u32 *long_mask,
12228c841e57SJani Nikula 			       u32 hotplug_trigger, u32 dig_hotplug_reg,
1223fd63e2a9SImre Deak 			       const u32 hpd[HPD_NUM_PINS],
1224af92058fSVille Syrjälä 			       bool long_pulse_detect(enum hpd_pin pin, u32 val))
1225676574dfSJani Nikula {
1226e9be2850SVille Syrjälä 	enum hpd_pin pin;
1227676574dfSJani Nikula 
122852dfdba0SLucas De Marchi 	BUILD_BUG_ON(BITS_PER_TYPE(*pin_mask) < HPD_NUM_PINS);
122952dfdba0SLucas De Marchi 
1230e9be2850SVille Syrjälä 	for_each_hpd_pin(pin) {
1231e9be2850SVille Syrjälä 		if ((hpd[pin] & hotplug_trigger) == 0)
12328c841e57SJani Nikula 			continue;
12338c841e57SJani Nikula 
1234e9be2850SVille Syrjälä 		*pin_mask |= BIT(pin);
1235676574dfSJani Nikula 
1236af92058fSVille Syrjälä 		if (long_pulse_detect(pin, dig_hotplug_reg))
1237e9be2850SVille Syrjälä 			*long_mask |= BIT(pin);
1238676574dfSJani Nikula 	}
1239676574dfSJani Nikula 
124000376ccfSWambui Karuga 	drm_dbg(&dev_priv->drm,
124100376ccfSWambui Karuga 		"hotplug event received, stat 0x%08x, dig 0x%08x, pins 0x%08x, long 0x%08x\n",
1242f88f0478SVille Syrjälä 		hotplug_trigger, dig_hotplug_reg, *pin_mask, *long_mask);
1243676574dfSJani Nikula 
1244676574dfSJani Nikula }
1245676574dfSJani Nikula 
124691d14251STvrtko Ursulin static void gmbus_irq_handler(struct drm_i915_private *dev_priv)
1247515ac2bbSDaniel Vetter {
124828c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1249515ac2bbSDaniel Vetter }
1250515ac2bbSDaniel Vetter 
125191d14251STvrtko Ursulin static void dp_aux_irq_handler(struct drm_i915_private *dev_priv)
1252ce99c256SDaniel Vetter {
12539ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1254ce99c256SDaniel Vetter }
1255ce99c256SDaniel Vetter 
12568bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
125791d14251STvrtko Ursulin static void display_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
125891d14251STvrtko Ursulin 					 enum pipe pipe,
1259a9c287c9SJani Nikula 					 u32 crc0, u32 crc1,
1260a9c287c9SJani Nikula 					 u32 crc2, u32 crc3,
1261a9c287c9SJani Nikula 					 u32 crc4)
12628bf1e9f1SShuang He {
12638c6b709dSTomeu Vizoso 	struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
126400535527SJani Nikula 	struct intel_pipe_crc *pipe_crc = &crtc->pipe_crc;
12655cee6c45SVille Syrjälä 	u32 crcs[5] = { crc0, crc1, crc2, crc3, crc4 };
12665cee6c45SVille Syrjälä 
12675cee6c45SVille Syrjälä 	trace_intel_pipe_crc(crtc, crcs);
1268b2c88f5bSDamien Lespiau 
1269d538bbdfSDamien Lespiau 	spin_lock(&pipe_crc->lock);
12708c6b709dSTomeu Vizoso 	/*
12718c6b709dSTomeu Vizoso 	 * For some not yet identified reason, the first CRC is
12728c6b709dSTomeu Vizoso 	 * bonkers. So let's just wait for the next vblank and read
12738c6b709dSTomeu Vizoso 	 * out the buggy result.
12748c6b709dSTomeu Vizoso 	 *
1275163e8aecSRodrigo Vivi 	 * On GEN8+ sometimes the second CRC is bonkers as well, so
12768c6b709dSTomeu Vizoso 	 * don't trust that one either.
12778c6b709dSTomeu Vizoso 	 */
1278033b7a23SMaarten Lankhorst 	if (pipe_crc->skipped <= 0 ||
1279163e8aecSRodrigo Vivi 	    (INTEL_GEN(dev_priv) >= 8 && pipe_crc->skipped == 1)) {
12808c6b709dSTomeu Vizoso 		pipe_crc->skipped++;
12818c6b709dSTomeu Vizoso 		spin_unlock(&pipe_crc->lock);
12828c6b709dSTomeu Vizoso 		return;
12838c6b709dSTomeu Vizoso 	}
12848c6b709dSTomeu Vizoso 	spin_unlock(&pipe_crc->lock);
12856cc42152SMaarten Lankhorst 
1286246ee524STomeu Vizoso 	drm_crtc_add_crc_entry(&crtc->base, true,
1287ca814b25SDaniel Vetter 				drm_crtc_accurate_vblank_count(&crtc->base),
1288246ee524STomeu Vizoso 				crcs);
12898c6b709dSTomeu Vizoso }
1290277de95eSDaniel Vetter #else
1291277de95eSDaniel Vetter static inline void
129291d14251STvrtko Ursulin display_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
129391d14251STvrtko Ursulin 			     enum pipe pipe,
1294a9c287c9SJani Nikula 			     u32 crc0, u32 crc1,
1295a9c287c9SJani Nikula 			     u32 crc2, u32 crc3,
1296a9c287c9SJani Nikula 			     u32 crc4) {}
1297277de95eSDaniel Vetter #endif
1298eba94eb9SDaniel Vetter 
1299277de95eSDaniel Vetter 
130091d14251STvrtko Ursulin static void hsw_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
130191d14251STvrtko Ursulin 				     enum pipe pipe)
13025a69b89fSDaniel Vetter {
130391d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
13045a69b89fSDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
13055a69b89fSDaniel Vetter 				     0, 0, 0, 0);
13065a69b89fSDaniel Vetter }
13075a69b89fSDaniel Vetter 
130891d14251STvrtko Ursulin static void ivb_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
130991d14251STvrtko Ursulin 				     enum pipe pipe)
1310eba94eb9SDaniel Vetter {
131191d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
1312eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1313eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1314eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1315eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
13168bc5e955SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1317eba94eb9SDaniel Vetter }
13185b3a856bSDaniel Vetter 
131991d14251STvrtko Ursulin static void i9xx_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
132091d14251STvrtko Ursulin 				      enum pipe pipe)
13215b3a856bSDaniel Vetter {
1322a9c287c9SJani Nikula 	u32 res1, res2;
13230b5c5ed0SDaniel Vetter 
132491d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 3)
13250b5c5ed0SDaniel Vetter 		res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
13260b5c5ed0SDaniel Vetter 	else
13270b5c5ed0SDaniel Vetter 		res1 = 0;
13280b5c5ed0SDaniel Vetter 
132991d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
13300b5c5ed0SDaniel Vetter 		res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
13310b5c5ed0SDaniel Vetter 	else
13320b5c5ed0SDaniel Vetter 		res2 = 0;
13335b3a856bSDaniel Vetter 
133491d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
13350b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_RED(pipe)),
13360b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_GREEN(pipe)),
13370b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_BLUE(pipe)),
13380b5c5ed0SDaniel Vetter 				     res1, res2);
13395b3a856bSDaniel Vetter }
13408bf1e9f1SShuang He 
134144d9241eSVille Syrjälä static void i9xx_pipestat_irq_reset(struct drm_i915_private *dev_priv)
134244d9241eSVille Syrjälä {
134344d9241eSVille Syrjälä 	enum pipe pipe;
134444d9241eSVille Syrjälä 
134544d9241eSVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
134644d9241eSVille Syrjälä 		I915_WRITE(PIPESTAT(pipe),
134744d9241eSVille Syrjälä 			   PIPESTAT_INT_STATUS_MASK |
134844d9241eSVille Syrjälä 			   PIPE_FIFO_UNDERRUN_STATUS);
134944d9241eSVille Syrjälä 
135044d9241eSVille Syrjälä 		dev_priv->pipestat_irq_mask[pipe] = 0;
135144d9241eSVille Syrjälä 	}
135244d9241eSVille Syrjälä }
135344d9241eSVille Syrjälä 
1354eb64343cSVille Syrjälä static void i9xx_pipestat_irq_ack(struct drm_i915_private *dev_priv,
135591d14251STvrtko Ursulin 				  u32 iir, u32 pipe_stats[I915_MAX_PIPES])
13567e231dbeSJesse Barnes {
1357d048a268SVille Syrjälä 	enum pipe pipe;
13587e231dbeSJesse Barnes 
135958ead0d7SImre Deak 	spin_lock(&dev_priv->irq_lock);
13601ca993d2SVille Syrjälä 
13611ca993d2SVille Syrjälä 	if (!dev_priv->display_irqs_enabled) {
13621ca993d2SVille Syrjälä 		spin_unlock(&dev_priv->irq_lock);
13631ca993d2SVille Syrjälä 		return;
13641ca993d2SVille Syrjälä 	}
13651ca993d2SVille Syrjälä 
1366055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1367f0f59a00SVille Syrjälä 		i915_reg_t reg;
13686b12ca56SVille Syrjälä 		u32 status_mask, enable_mask, iir_bit = 0;
136991d181ddSImre Deak 
1370bbb5eebfSDaniel Vetter 		/*
1371bbb5eebfSDaniel Vetter 		 * PIPESTAT bits get signalled even when the interrupt is
1372bbb5eebfSDaniel Vetter 		 * disabled with the mask bits, and some of the status bits do
1373bbb5eebfSDaniel Vetter 		 * not generate interrupts at all (like the underrun bit). Hence
1374bbb5eebfSDaniel Vetter 		 * we need to be careful that we only handle what we want to
1375bbb5eebfSDaniel Vetter 		 * handle.
1376bbb5eebfSDaniel Vetter 		 */
13770f239f4cSDaniel Vetter 
13780f239f4cSDaniel Vetter 		/* fifo underruns are filterered in the underrun handler. */
13796b12ca56SVille Syrjälä 		status_mask = PIPE_FIFO_UNDERRUN_STATUS;
1380bbb5eebfSDaniel Vetter 
1381bbb5eebfSDaniel Vetter 		switch (pipe) {
1382d048a268SVille Syrjälä 		default:
1383bbb5eebfSDaniel Vetter 		case PIPE_A:
1384bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
1385bbb5eebfSDaniel Vetter 			break;
1386bbb5eebfSDaniel Vetter 		case PIPE_B:
1387bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
1388bbb5eebfSDaniel Vetter 			break;
13893278f67fSVille Syrjälä 		case PIPE_C:
13903278f67fSVille Syrjälä 			iir_bit = I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
13913278f67fSVille Syrjälä 			break;
1392bbb5eebfSDaniel Vetter 		}
1393bbb5eebfSDaniel Vetter 		if (iir & iir_bit)
13946b12ca56SVille Syrjälä 			status_mask |= dev_priv->pipestat_irq_mask[pipe];
1395bbb5eebfSDaniel Vetter 
13966b12ca56SVille Syrjälä 		if (!status_mask)
139791d181ddSImre Deak 			continue;
139891d181ddSImre Deak 
139991d181ddSImre Deak 		reg = PIPESTAT(pipe);
14006b12ca56SVille Syrjälä 		pipe_stats[pipe] = I915_READ(reg) & status_mask;
14016b12ca56SVille Syrjälä 		enable_mask = i915_pipestat_enable_mask(dev_priv, pipe);
14027e231dbeSJesse Barnes 
14037e231dbeSJesse Barnes 		/*
14047e231dbeSJesse Barnes 		 * Clear the PIPE*STAT regs before the IIR
1405132c27c9SVille Syrjälä 		 *
1406132c27c9SVille Syrjälä 		 * Toggle the enable bits to make sure we get an
1407132c27c9SVille Syrjälä 		 * edge in the ISR pipe event bit if we don't clear
1408132c27c9SVille Syrjälä 		 * all the enabled status bits. Otherwise the edge
1409132c27c9SVille Syrjälä 		 * triggered IIR on i965/g4x wouldn't notice that
1410132c27c9SVille Syrjälä 		 * an interrupt is still pending.
14117e231dbeSJesse Barnes 		 */
1412132c27c9SVille Syrjälä 		if (pipe_stats[pipe]) {
1413132c27c9SVille Syrjälä 			I915_WRITE(reg, pipe_stats[pipe]);
1414132c27c9SVille Syrjälä 			I915_WRITE(reg, enable_mask);
1415132c27c9SVille Syrjälä 		}
14167e231dbeSJesse Barnes 	}
141758ead0d7SImre Deak 	spin_unlock(&dev_priv->irq_lock);
14182ecb8ca4SVille Syrjälä }
14192ecb8ca4SVille Syrjälä 
1420eb64343cSVille Syrjälä static void i8xx_pipestat_irq_handler(struct drm_i915_private *dev_priv,
1421eb64343cSVille Syrjälä 				      u16 iir, u32 pipe_stats[I915_MAX_PIPES])
1422eb64343cSVille Syrjälä {
1423eb64343cSVille Syrjälä 	enum pipe pipe;
1424eb64343cSVille Syrjälä 
1425eb64343cSVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
1426eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
1427aca9310aSAnshuman Gupta 			intel_handle_vblank(dev_priv, pipe);
1428eb64343cSVille Syrjälä 
1429eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1430eb64343cSVille Syrjälä 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
1431eb64343cSVille Syrjälä 
1432eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
1433eb64343cSVille Syrjälä 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
1434eb64343cSVille Syrjälä 	}
1435eb64343cSVille Syrjälä }
1436eb64343cSVille Syrjälä 
1437eb64343cSVille Syrjälä static void i915_pipestat_irq_handler(struct drm_i915_private *dev_priv,
1438eb64343cSVille Syrjälä 				      u32 iir, u32 pipe_stats[I915_MAX_PIPES])
1439eb64343cSVille Syrjälä {
1440eb64343cSVille Syrjälä 	bool blc_event = false;
1441eb64343cSVille Syrjälä 	enum pipe pipe;
1442eb64343cSVille Syrjälä 
1443eb64343cSVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
1444eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS)
1445aca9310aSAnshuman Gupta 			intel_handle_vblank(dev_priv, pipe);
1446eb64343cSVille Syrjälä 
1447eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
1448eb64343cSVille Syrjälä 			blc_event = true;
1449eb64343cSVille Syrjälä 
1450eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1451eb64343cSVille Syrjälä 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
1452eb64343cSVille Syrjälä 
1453eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
1454eb64343cSVille Syrjälä 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
1455eb64343cSVille Syrjälä 	}
1456eb64343cSVille Syrjälä 
1457eb64343cSVille Syrjälä 	if (blc_event || (iir & I915_ASLE_INTERRUPT))
1458eb64343cSVille Syrjälä 		intel_opregion_asle_intr(dev_priv);
1459eb64343cSVille Syrjälä }
1460eb64343cSVille Syrjälä 
1461eb64343cSVille Syrjälä static void i965_pipestat_irq_handler(struct drm_i915_private *dev_priv,
1462eb64343cSVille Syrjälä 				      u32 iir, u32 pipe_stats[I915_MAX_PIPES])
1463eb64343cSVille Syrjälä {
1464eb64343cSVille Syrjälä 	bool blc_event = false;
1465eb64343cSVille Syrjälä 	enum pipe pipe;
1466eb64343cSVille Syrjälä 
1467eb64343cSVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
1468eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS)
1469aca9310aSAnshuman Gupta 			intel_handle_vblank(dev_priv, pipe);
1470eb64343cSVille Syrjälä 
1471eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
1472eb64343cSVille Syrjälä 			blc_event = true;
1473eb64343cSVille Syrjälä 
1474eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1475eb64343cSVille Syrjälä 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
1476eb64343cSVille Syrjälä 
1477eb64343cSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
1478eb64343cSVille Syrjälä 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
1479eb64343cSVille Syrjälä 	}
1480eb64343cSVille Syrjälä 
1481eb64343cSVille Syrjälä 	if (blc_event || (iir & I915_ASLE_INTERRUPT))
1482eb64343cSVille Syrjälä 		intel_opregion_asle_intr(dev_priv);
1483eb64343cSVille Syrjälä 
1484eb64343cSVille Syrjälä 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1485eb64343cSVille Syrjälä 		gmbus_irq_handler(dev_priv);
1486eb64343cSVille Syrjälä }
1487eb64343cSVille Syrjälä 
148891d14251STvrtko Ursulin static void valleyview_pipestat_irq_handler(struct drm_i915_private *dev_priv,
14892ecb8ca4SVille Syrjälä 					    u32 pipe_stats[I915_MAX_PIPES])
14902ecb8ca4SVille Syrjälä {
14912ecb8ca4SVille Syrjälä 	enum pipe pipe;
14927e231dbeSJesse Barnes 
1493055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1494fd3a4024SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS)
1495aca9310aSAnshuman Gupta 			intel_handle_vblank(dev_priv, pipe);
14964356d586SDaniel Vetter 
14974356d586SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
149891d14251STvrtko Ursulin 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
14992d9d2b0bSVille Syrjälä 
15001f7247c0SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
15011f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
150231acc7f5SJesse Barnes 	}
150331acc7f5SJesse Barnes 
1504c1874ed7SImre Deak 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
150591d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
1506c1874ed7SImre Deak }
1507c1874ed7SImre Deak 
15081ae3c34cSVille Syrjälä static u32 i9xx_hpd_irq_ack(struct drm_i915_private *dev_priv)
150916c6c56bSVille Syrjälä {
15100ba7c51aSVille Syrjälä 	u32 hotplug_status = 0, hotplug_status_mask;
15110ba7c51aSVille Syrjälä 	int i;
151216c6c56bSVille Syrjälä 
15130ba7c51aSVille Syrjälä 	if (IS_G4X(dev_priv) ||
15140ba7c51aSVille Syrjälä 	    IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
15150ba7c51aSVille Syrjälä 		hotplug_status_mask = HOTPLUG_INT_STATUS_G4X |
15160ba7c51aSVille Syrjälä 			DP_AUX_CHANNEL_MASK_INT_STATUS_G4X;
15170ba7c51aSVille Syrjälä 	else
15180ba7c51aSVille Syrjälä 		hotplug_status_mask = HOTPLUG_INT_STATUS_I915;
15190ba7c51aSVille Syrjälä 
15200ba7c51aSVille Syrjälä 	/*
15210ba7c51aSVille Syrjälä 	 * We absolutely have to clear all the pending interrupt
15220ba7c51aSVille Syrjälä 	 * bits in PORT_HOTPLUG_STAT. Otherwise the ISR port
15230ba7c51aSVille Syrjälä 	 * interrupt bit won't have an edge, and the i965/g4x
15240ba7c51aSVille Syrjälä 	 * edge triggered IIR will not notice that an interrupt
15250ba7c51aSVille Syrjälä 	 * is still pending. We can't use PORT_HOTPLUG_EN to
15260ba7c51aSVille Syrjälä 	 * guarantee the edge as the act of toggling the enable
15270ba7c51aSVille Syrjälä 	 * bits can itself generate a new hotplug interrupt :(
15280ba7c51aSVille Syrjälä 	 */
15290ba7c51aSVille Syrjälä 	for (i = 0; i < 10; i++) {
15300ba7c51aSVille Syrjälä 		u32 tmp = I915_READ(PORT_HOTPLUG_STAT) & hotplug_status_mask;
15310ba7c51aSVille Syrjälä 
15320ba7c51aSVille Syrjälä 		if (tmp == 0)
15330ba7c51aSVille Syrjälä 			return hotplug_status;
15340ba7c51aSVille Syrjälä 
15350ba7c51aSVille Syrjälä 		hotplug_status |= tmp;
15363ff60f89SOscar Mateo 		I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
15370ba7c51aSVille Syrjälä 	}
15380ba7c51aSVille Syrjälä 
153948a1b8d4SPankaj Bharadiya 	drm_WARN_ONCE(&dev_priv->drm, 1,
15400ba7c51aSVille Syrjälä 		      "PORT_HOTPLUG_STAT did not clear (0x%08x)\n",
15410ba7c51aSVille Syrjälä 		      I915_READ(PORT_HOTPLUG_STAT));
15421ae3c34cSVille Syrjälä 
15431ae3c34cSVille Syrjälä 	return hotplug_status;
15441ae3c34cSVille Syrjälä }
15451ae3c34cSVille Syrjälä 
154691d14251STvrtko Ursulin static void i9xx_hpd_irq_handler(struct drm_i915_private *dev_priv,
15471ae3c34cSVille Syrjälä 				 u32 hotplug_status)
15481ae3c34cSVille Syrjälä {
15491ae3c34cSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
15500398993bSVille Syrjälä 	u32 hotplug_trigger;
15513ff60f89SOscar Mateo 
15520398993bSVille Syrjälä 	if (IS_G4X(dev_priv) ||
15530398993bSVille Syrjälä 	    IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
15540398993bSVille Syrjälä 		hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
15550398993bSVille Syrjälä 	else
15560398993bSVille Syrjälä 		hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
155716c6c56bSVille Syrjälä 
155858f2cf24SVille Syrjälä 	if (hotplug_trigger) {
1559cf53902fSRodrigo Vivi 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
1560cf53902fSRodrigo Vivi 				   hotplug_trigger, hotplug_trigger,
15610398993bSVille Syrjälä 				   dev_priv->hotplug.hpd,
1562fd63e2a9SImre Deak 				   i9xx_port_hotplug_long_detect);
156358f2cf24SVille Syrjälä 
156491d14251STvrtko Ursulin 		intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
156558f2cf24SVille Syrjälä 	}
1566369712e8SJani Nikula 
15670398993bSVille Syrjälä 	if ((IS_G4X(dev_priv) ||
15680398993bSVille Syrjälä 	     IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
15690398993bSVille Syrjälä 	    hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
157091d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
157158f2cf24SVille Syrjälä }
157216c6c56bSVille Syrjälä 
1573c1874ed7SImre Deak static irqreturn_t valleyview_irq_handler(int irq, void *arg)
1574c1874ed7SImre Deak {
1575b318b824SVille Syrjälä 	struct drm_i915_private *dev_priv = arg;
1576c1874ed7SImre Deak 	irqreturn_t ret = IRQ_NONE;
1577c1874ed7SImre Deak 
15782dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
15792dd2a883SImre Deak 		return IRQ_NONE;
15802dd2a883SImre Deak 
15811f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
15829102650fSDaniele Ceraolo Spurio 	disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
15831f814dacSImre Deak 
15841e1cace9SVille Syrjälä 	do {
15856e814800SVille Syrjälä 		u32 iir, gt_iir, pm_iir;
15862ecb8ca4SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
15871ae3c34cSVille Syrjälä 		u32 hotplug_status = 0;
1588a5e485a9SVille Syrjälä 		u32 ier = 0;
15893ff60f89SOscar Mateo 
1590c1874ed7SImre Deak 		gt_iir = I915_READ(GTIIR);
1591c1874ed7SImre Deak 		pm_iir = I915_READ(GEN6_PMIIR);
15923ff60f89SOscar Mateo 		iir = I915_READ(VLV_IIR);
1593c1874ed7SImre Deak 
1594c1874ed7SImre Deak 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
15951e1cace9SVille Syrjälä 			break;
1596c1874ed7SImre Deak 
1597c1874ed7SImre Deak 		ret = IRQ_HANDLED;
1598c1874ed7SImre Deak 
1599a5e485a9SVille Syrjälä 		/*
1600a5e485a9SVille Syrjälä 		 * Theory on interrupt generation, based on empirical evidence:
1601a5e485a9SVille Syrjälä 		 *
1602a5e485a9SVille Syrjälä 		 * x = ((VLV_IIR & VLV_IER) ||
1603a5e485a9SVille Syrjälä 		 *      (((GT_IIR & GT_IER) || (GEN6_PMIIR & GEN6_PMIER)) &&
1604a5e485a9SVille Syrjälä 		 *       (VLV_MASTER_IER & MASTER_INTERRUPT_ENABLE)));
1605a5e485a9SVille Syrjälä 		 *
1606a5e485a9SVille Syrjälä 		 * A CPU interrupt will only be raised when 'x' has a 0->1 edge.
1607a5e485a9SVille Syrjälä 		 * Hence we clear MASTER_INTERRUPT_ENABLE and VLV_IER to
1608a5e485a9SVille Syrjälä 		 * guarantee the CPU interrupt will be raised again even if we
1609a5e485a9SVille Syrjälä 		 * don't end up clearing all the VLV_IIR, GT_IIR, GEN6_PMIIR
1610a5e485a9SVille Syrjälä 		 * bits this time around.
1611a5e485a9SVille Syrjälä 		 */
16124a0a0202SVille Syrjälä 		I915_WRITE(VLV_MASTER_IER, 0);
1613a5e485a9SVille Syrjälä 		ier = I915_READ(VLV_IER);
1614a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, 0);
16154a0a0202SVille Syrjälä 
16164a0a0202SVille Syrjälä 		if (gt_iir)
16174a0a0202SVille Syrjälä 			I915_WRITE(GTIIR, gt_iir);
16184a0a0202SVille Syrjälä 		if (pm_iir)
16194a0a0202SVille Syrjälä 			I915_WRITE(GEN6_PMIIR, pm_iir);
16204a0a0202SVille Syrjälä 
16217ce4d1f2SVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
16221ae3c34cSVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
16237ce4d1f2SVille Syrjälä 
16243ff60f89SOscar Mateo 		/* Call regardless, as some status bits might not be
16253ff60f89SOscar Mateo 		 * signalled in iir */
1626eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
16277ce4d1f2SVille Syrjälä 
1628eef57324SJerome Anand 		if (iir & (I915_LPE_PIPE_A_INTERRUPT |
1629eef57324SJerome Anand 			   I915_LPE_PIPE_B_INTERRUPT))
1630eef57324SJerome Anand 			intel_lpe_audio_irq_handler(dev_priv);
1631eef57324SJerome Anand 
16327ce4d1f2SVille Syrjälä 		/*
16337ce4d1f2SVille Syrjälä 		 * VLV_IIR is single buffered, and reflects the level
16347ce4d1f2SVille Syrjälä 		 * from PIPESTAT/PORT_HOTPLUG_STAT, hence clear it last.
16357ce4d1f2SVille Syrjälä 		 */
16367ce4d1f2SVille Syrjälä 		if (iir)
16377ce4d1f2SVille Syrjälä 			I915_WRITE(VLV_IIR, iir);
16384a0a0202SVille Syrjälä 
1639a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, ier);
16404a0a0202SVille Syrjälä 		I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
16411ae3c34cSVille Syrjälä 
164252894874SVille Syrjälä 		if (gt_iir)
1643cf1c97dcSAndi Shyti 			gen6_gt_irq_handler(&dev_priv->gt, gt_iir);
164452894874SVille Syrjälä 		if (pm_iir)
16453e7abf81SAndi Shyti 			gen6_rps_irq_handler(&dev_priv->gt.rps, pm_iir);
164652894874SVille Syrjälä 
16471ae3c34cSVille Syrjälä 		if (hotplug_status)
164891d14251STvrtko Ursulin 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
16492ecb8ca4SVille Syrjälä 
165091d14251STvrtko Ursulin 		valleyview_pipestat_irq_handler(dev_priv, pipe_stats);
16511e1cace9SVille Syrjälä 	} while (0);
16527e231dbeSJesse Barnes 
16539102650fSDaniele Ceraolo Spurio 	enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
16541f814dacSImre Deak 
16557e231dbeSJesse Barnes 	return ret;
16567e231dbeSJesse Barnes }
16577e231dbeSJesse Barnes 
165843f328d7SVille Syrjälä static irqreturn_t cherryview_irq_handler(int irq, void *arg)
165943f328d7SVille Syrjälä {
1660b318b824SVille Syrjälä 	struct drm_i915_private *dev_priv = arg;
166143f328d7SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
166243f328d7SVille Syrjälä 
16632dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
16642dd2a883SImre Deak 		return IRQ_NONE;
16652dd2a883SImre Deak 
16661f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
16679102650fSDaniele Ceraolo Spurio 	disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
16681f814dacSImre Deak 
1669579de73bSChris Wilson 	do {
16706e814800SVille Syrjälä 		u32 master_ctl, iir;
16712ecb8ca4SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
16721ae3c34cSVille Syrjälä 		u32 hotplug_status = 0;
1673a5e485a9SVille Syrjälä 		u32 ier = 0;
1674a5e485a9SVille Syrjälä 
16758e5fd599SVille Syrjälä 		master_ctl = I915_READ(GEN8_MASTER_IRQ) & ~GEN8_MASTER_IRQ_CONTROL;
16763278f67fSVille Syrjälä 		iir = I915_READ(VLV_IIR);
16773278f67fSVille Syrjälä 
16783278f67fSVille Syrjälä 		if (master_ctl == 0 && iir == 0)
16798e5fd599SVille Syrjälä 			break;
168043f328d7SVille Syrjälä 
168127b6c122SOscar Mateo 		ret = IRQ_HANDLED;
168227b6c122SOscar Mateo 
1683a5e485a9SVille Syrjälä 		/*
1684a5e485a9SVille Syrjälä 		 * Theory on interrupt generation, based on empirical evidence:
1685a5e485a9SVille Syrjälä 		 *
1686a5e485a9SVille Syrjälä 		 * x = ((VLV_IIR & VLV_IER) ||
1687a5e485a9SVille Syrjälä 		 *      ((GEN8_MASTER_IRQ & ~GEN8_MASTER_IRQ_CONTROL) &&
1688a5e485a9SVille Syrjälä 		 *       (GEN8_MASTER_IRQ & GEN8_MASTER_IRQ_CONTROL)));
1689a5e485a9SVille Syrjälä 		 *
1690a5e485a9SVille Syrjälä 		 * A CPU interrupt will only be raised when 'x' has a 0->1 edge.
1691a5e485a9SVille Syrjälä 		 * Hence we clear GEN8_MASTER_IRQ_CONTROL and VLV_IER to
1692a5e485a9SVille Syrjälä 		 * guarantee the CPU interrupt will be raised again even if we
1693a5e485a9SVille Syrjälä 		 * don't end up clearing all the VLV_IIR and GEN8_MASTER_IRQ_CONTROL
1694a5e485a9SVille Syrjälä 		 * bits this time around.
1695a5e485a9SVille Syrjälä 		 */
169643f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, 0);
1697a5e485a9SVille Syrjälä 		ier = I915_READ(VLV_IER);
1698a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, 0);
169943f328d7SVille Syrjälä 
17006cc32f15SChris Wilson 		gen8_gt_irq_handler(&dev_priv->gt, master_ctl);
170127b6c122SOscar Mateo 
170227b6c122SOscar Mateo 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
17031ae3c34cSVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
170443f328d7SVille Syrjälä 
170527b6c122SOscar Mateo 		/* Call regardless, as some status bits might not be
170627b6c122SOscar Mateo 		 * signalled in iir */
1707eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
170843f328d7SVille Syrjälä 
1709eef57324SJerome Anand 		if (iir & (I915_LPE_PIPE_A_INTERRUPT |
1710eef57324SJerome Anand 			   I915_LPE_PIPE_B_INTERRUPT |
1711eef57324SJerome Anand 			   I915_LPE_PIPE_C_INTERRUPT))
1712eef57324SJerome Anand 			intel_lpe_audio_irq_handler(dev_priv);
1713eef57324SJerome Anand 
17147ce4d1f2SVille Syrjälä 		/*
17157ce4d1f2SVille Syrjälä 		 * VLV_IIR is single buffered, and reflects the level
17167ce4d1f2SVille Syrjälä 		 * from PIPESTAT/PORT_HOTPLUG_STAT, hence clear it last.
17177ce4d1f2SVille Syrjälä 		 */
17187ce4d1f2SVille Syrjälä 		if (iir)
17197ce4d1f2SVille Syrjälä 			I915_WRITE(VLV_IIR, iir);
17207ce4d1f2SVille Syrjälä 
1721a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, ier);
1722e5328c43SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
17231ae3c34cSVille Syrjälä 
17241ae3c34cSVille Syrjälä 		if (hotplug_status)
172591d14251STvrtko Ursulin 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
17262ecb8ca4SVille Syrjälä 
172791d14251STvrtko Ursulin 		valleyview_pipestat_irq_handler(dev_priv, pipe_stats);
1728579de73bSChris Wilson 	} while (0);
17293278f67fSVille Syrjälä 
17309102650fSDaniele Ceraolo Spurio 	enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
17311f814dacSImre Deak 
173243f328d7SVille Syrjälä 	return ret;
173343f328d7SVille Syrjälä }
173443f328d7SVille Syrjälä 
173591d14251STvrtko Ursulin static void ibx_hpd_irq_handler(struct drm_i915_private *dev_priv,
17360398993bSVille Syrjälä 				u32 hotplug_trigger)
1737776ad806SJesse Barnes {
173842db67d6SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
1739776ad806SJesse Barnes 
17406a39d7c9SJani Nikula 	/*
17416a39d7c9SJani Nikula 	 * Somehow the PCH doesn't seem to really ack the interrupt to the CPU
17426a39d7c9SJani Nikula 	 * unless we touch the hotplug register, even if hotplug_trigger is
17436a39d7c9SJani Nikula 	 * zero. Not acking leads to "The master control interrupt lied (SDE)!"
17446a39d7c9SJani Nikula 	 * errors.
17456a39d7c9SJani Nikula 	 */
174613cf5504SDave Airlie 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
17476a39d7c9SJani Nikula 	if (!hotplug_trigger) {
17486a39d7c9SJani Nikula 		u32 mask = PORTA_HOTPLUG_STATUS_MASK |
17496a39d7c9SJani Nikula 			PORTD_HOTPLUG_STATUS_MASK |
17506a39d7c9SJani Nikula 			PORTC_HOTPLUG_STATUS_MASK |
17516a39d7c9SJani Nikula 			PORTB_HOTPLUG_STATUS_MASK;
17526a39d7c9SJani Nikula 		dig_hotplug_reg &= ~mask;
17536a39d7c9SJani Nikula 	}
17546a39d7c9SJani Nikula 
175513cf5504SDave Airlie 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
17566a39d7c9SJani Nikula 	if (!hotplug_trigger)
17576a39d7c9SJani Nikula 		return;
175813cf5504SDave Airlie 
17590398993bSVille Syrjälä 	intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
17600398993bSVille Syrjälä 			   hotplug_trigger, dig_hotplug_reg,
17610398993bSVille Syrjälä 			   dev_priv->hotplug.pch_hpd,
1762fd63e2a9SImre Deak 			   pch_port_hotplug_long_detect);
176340e56410SVille Syrjälä 
176491d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
1765aaf5ec2eSSonika Jindal }
176691d131d2SDaniel Vetter 
176791d14251STvrtko Ursulin static void ibx_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
176840e56410SVille Syrjälä {
1769d048a268SVille Syrjälä 	enum pipe pipe;
177040e56410SVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
177140e56410SVille Syrjälä 
17720398993bSVille Syrjälä 	ibx_hpd_irq_handler(dev_priv, hotplug_trigger);
177340e56410SVille Syrjälä 
1774cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1775cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1776776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
177700376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm, "PCH audio power change on port %d\n",
1778cfc33bf7SVille Syrjälä 			port_name(port));
1779cfc33bf7SVille Syrjälä 	}
1780776ad806SJesse Barnes 
1781ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
178291d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
1783ce99c256SDaniel Vetter 
1784776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
178591d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
1786776ad806SJesse Barnes 
1787776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
178800376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm, "PCH HDCP audio interrupt\n");
1789776ad806SJesse Barnes 
1790776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
179100376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm, "PCH transcoder audio interrupt\n");
1792776ad806SJesse Barnes 
1793776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
179400376ccfSWambui Karuga 		drm_err(&dev_priv->drm, "PCH poison interrupt\n");
1795776ad806SJesse Barnes 
1796b8b65ccdSAnshuman Gupta 	if (pch_iir & SDE_FDI_MASK) {
1797055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
179800376ccfSWambui Karuga 			drm_dbg(&dev_priv->drm, "  pipe %c FDI IIR: 0x%08x\n",
17999db4a9c7SJesse Barnes 				pipe_name(pipe),
18009db4a9c7SJesse Barnes 				I915_READ(FDI_RX_IIR(pipe)));
1801b8b65ccdSAnshuman Gupta 	}
1802776ad806SJesse Barnes 
1803776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
180400376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm, "PCH transcoder CRC done interrupt\n");
1805776ad806SJesse Barnes 
1806776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
180700376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm,
180800376ccfSWambui Karuga 			"PCH transcoder CRC error interrupt\n");
1809776ad806SJesse Barnes 
1810776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
1811a2196033SMatthias Kaehlcke 		intel_pch_fifo_underrun_irq_handler(dev_priv, PIPE_A);
18128664281bSPaulo Zanoni 
18138664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
1814a2196033SMatthias Kaehlcke 		intel_pch_fifo_underrun_irq_handler(dev_priv, PIPE_B);
18158664281bSPaulo Zanoni }
18168664281bSPaulo Zanoni 
181791d14251STvrtko Ursulin static void ivb_err_int_handler(struct drm_i915_private *dev_priv)
18188664281bSPaulo Zanoni {
18198664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
18205a69b89fSDaniel Vetter 	enum pipe pipe;
18218664281bSPaulo Zanoni 
1822de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
182300376ccfSWambui Karuga 		drm_err(&dev_priv->drm, "Poison interrupt\n");
1824de032bf4SPaulo Zanoni 
1825055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
18261f7247c0SDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe))
18271f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
18288664281bSPaulo Zanoni 
18295a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
183091d14251STvrtko Ursulin 			if (IS_IVYBRIDGE(dev_priv))
183191d14251STvrtko Ursulin 				ivb_pipe_crc_irq_handler(dev_priv, pipe);
18325a69b89fSDaniel Vetter 			else
183391d14251STvrtko Ursulin 				hsw_pipe_crc_irq_handler(dev_priv, pipe);
18345a69b89fSDaniel Vetter 		}
18355a69b89fSDaniel Vetter 	}
18368bf1e9f1SShuang He 
18378664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
18388664281bSPaulo Zanoni }
18398664281bSPaulo Zanoni 
184091d14251STvrtko Ursulin static void cpt_serr_int_handler(struct drm_i915_private *dev_priv)
18418664281bSPaulo Zanoni {
18428664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
184345c1cd87SMika Kahola 	enum pipe pipe;
18448664281bSPaulo Zanoni 
1845de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
184600376ccfSWambui Karuga 		drm_err(&dev_priv->drm, "PCH poison interrupt\n");
1847de032bf4SPaulo Zanoni 
184845c1cd87SMika Kahola 	for_each_pipe(dev_priv, pipe)
184945c1cd87SMika Kahola 		if (serr_int & SERR_INT_TRANS_FIFO_UNDERRUN(pipe))
185045c1cd87SMika Kahola 			intel_pch_fifo_underrun_irq_handler(dev_priv, pipe);
18518664281bSPaulo Zanoni 
18528664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
1853776ad806SJesse Barnes }
1854776ad806SJesse Barnes 
185591d14251STvrtko Ursulin static void cpt_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
185623e81d69SAdam Jackson {
1857d048a268SVille Syrjälä 	enum pipe pipe;
18586dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
1859aaf5ec2eSSonika Jindal 
18600398993bSVille Syrjälä 	ibx_hpd_irq_handler(dev_priv, hotplug_trigger);
186191d131d2SDaniel Vetter 
1862cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
1863cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
186423e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
186500376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm, "PCH audio power change on port %c\n",
1866cfc33bf7SVille Syrjälä 			port_name(port));
1867cfc33bf7SVille Syrjälä 	}
186823e81d69SAdam Jackson 
186923e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
187091d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
187123e81d69SAdam Jackson 
187223e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
187391d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
187423e81d69SAdam Jackson 
187523e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
187600376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm, "Audio CP request interrupt\n");
187723e81d69SAdam Jackson 
187823e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
187900376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm, "Audio CP change interrupt\n");
188023e81d69SAdam Jackson 
1881b8b65ccdSAnshuman Gupta 	if (pch_iir & SDE_FDI_MASK_CPT) {
1882055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
188300376ccfSWambui Karuga 			drm_dbg(&dev_priv->drm, "  pipe %c FDI IIR: 0x%08x\n",
188423e81d69SAdam Jackson 				pipe_name(pipe),
188523e81d69SAdam Jackson 				I915_READ(FDI_RX_IIR(pipe)));
1886b8b65ccdSAnshuman Gupta 	}
18878664281bSPaulo Zanoni 
18888664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
188991d14251STvrtko Ursulin 		cpt_serr_int_handler(dev_priv);
189023e81d69SAdam Jackson }
189123e81d69SAdam Jackson 
189258676af6SLucas De Marchi static void icp_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
189331604222SAnusha Srivatsa {
189458676af6SLucas De Marchi 	u32 ddi_hotplug_trigger, tc_hotplug_trigger;
189531604222SAnusha Srivatsa 	u32 pin_mask = 0, long_mask = 0;
189658676af6SLucas De Marchi 	bool (*tc_port_hotplug_long_detect)(enum hpd_pin pin, u32 val);
189731604222SAnusha Srivatsa 
189858676af6SLucas De Marchi 	if (HAS_PCH_TGP(dev_priv)) {
189958676af6SLucas De Marchi 		ddi_hotplug_trigger = pch_iir & SDE_DDI_MASK_TGP;
190058676af6SLucas De Marchi 		tc_hotplug_trigger = pch_iir & SDE_TC_MASK_TGP;
190158676af6SLucas De Marchi 		tc_port_hotplug_long_detect = tgp_tc_port_hotplug_long_detect;
1902943682e3SMatt Roper 	} else if (HAS_PCH_JSP(dev_priv)) {
1903943682e3SMatt Roper 		ddi_hotplug_trigger = pch_iir & SDE_DDI_MASK_TGP;
1904943682e3SMatt Roper 		tc_hotplug_trigger = 0;
190558676af6SLucas De Marchi 	} else if (HAS_PCH_MCC(dev_priv)) {
190653448aedSVivek Kasireddy 		ddi_hotplug_trigger = pch_iir & SDE_DDI_MASK_ICP;
190753448aedSVivek Kasireddy 		tc_hotplug_trigger = pch_iir & SDE_TC_HOTPLUG_ICP(PORT_TC1);
1908fcb9bba4SMatt Roper 		tc_port_hotplug_long_detect = icp_tc_port_hotplug_long_detect;
19098ef7e340SMatt Roper 	} else {
191048a1b8d4SPankaj Bharadiya 		drm_WARN(&dev_priv->drm, !HAS_PCH_ICP(dev_priv),
191148a1b8d4SPankaj Bharadiya 			 "Unrecognized PCH type 0x%x\n",
191248a1b8d4SPankaj Bharadiya 			 INTEL_PCH_TYPE(dev_priv));
1913943682e3SMatt Roper 
19148ef7e340SMatt Roper 		ddi_hotplug_trigger = pch_iir & SDE_DDI_MASK_ICP;
19158ef7e340SMatt Roper 		tc_hotplug_trigger = pch_iir & SDE_TC_MASK_ICP;
191658676af6SLucas De Marchi 		tc_port_hotplug_long_detect = icp_tc_port_hotplug_long_detect;
19178ef7e340SMatt Roper 	}
19188ef7e340SMatt Roper 
191931604222SAnusha Srivatsa 	if (ddi_hotplug_trigger) {
192031604222SAnusha Srivatsa 		u32 dig_hotplug_reg;
192131604222SAnusha Srivatsa 
192231604222SAnusha Srivatsa 		dig_hotplug_reg = I915_READ(SHOTPLUG_CTL_DDI);
192331604222SAnusha Srivatsa 		I915_WRITE(SHOTPLUG_CTL_DDI, dig_hotplug_reg);
192431604222SAnusha Srivatsa 
192531604222SAnusha Srivatsa 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
19260398993bSVille Syrjälä 				   ddi_hotplug_trigger, dig_hotplug_reg,
19270398993bSVille Syrjälä 				   dev_priv->hotplug.pch_hpd,
192831604222SAnusha Srivatsa 				   icp_ddi_port_hotplug_long_detect);
192931604222SAnusha Srivatsa 	}
193031604222SAnusha Srivatsa 
193131604222SAnusha Srivatsa 	if (tc_hotplug_trigger) {
193231604222SAnusha Srivatsa 		u32 dig_hotplug_reg;
193331604222SAnusha Srivatsa 
193431604222SAnusha Srivatsa 		dig_hotplug_reg = I915_READ(SHOTPLUG_CTL_TC);
193531604222SAnusha Srivatsa 		I915_WRITE(SHOTPLUG_CTL_TC, dig_hotplug_reg);
193631604222SAnusha Srivatsa 
193731604222SAnusha Srivatsa 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
19380398993bSVille Syrjälä 				   tc_hotplug_trigger, dig_hotplug_reg,
19390398993bSVille Syrjälä 				   dev_priv->hotplug.pch_hpd,
194058676af6SLucas De Marchi 				   tc_port_hotplug_long_detect);
194152dfdba0SLucas De Marchi 	}
194252dfdba0SLucas De Marchi 
194352dfdba0SLucas De Marchi 	if (pin_mask)
194452dfdba0SLucas De Marchi 		intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
194552dfdba0SLucas De Marchi 
194652dfdba0SLucas De Marchi 	if (pch_iir & SDE_GMBUS_ICP)
194752dfdba0SLucas De Marchi 		gmbus_irq_handler(dev_priv);
194852dfdba0SLucas De Marchi }
194952dfdba0SLucas De Marchi 
195091d14251STvrtko Ursulin static void spt_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
19516dbf30ceSVille Syrjälä {
19526dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_SPT &
19536dbf30ceSVille Syrjälä 		~SDE_PORTE_HOTPLUG_SPT;
19546dbf30ceSVille Syrjälä 	u32 hotplug2_trigger = pch_iir & SDE_PORTE_HOTPLUG_SPT;
19556dbf30ceSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
19566dbf30ceSVille Syrjälä 
19576dbf30ceSVille Syrjälä 	if (hotplug_trigger) {
19586dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
19596dbf30ceSVille Syrjälä 
19606dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
19616dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
19626dbf30ceSVille Syrjälä 
1963cf53902fSRodrigo Vivi 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
19640398993bSVille Syrjälä 				   hotplug_trigger, dig_hotplug_reg,
19650398993bSVille Syrjälä 				   dev_priv->hotplug.pch_hpd,
196674c0b395SVille Syrjälä 				   spt_port_hotplug_long_detect);
19676dbf30ceSVille Syrjälä 	}
19686dbf30ceSVille Syrjälä 
19696dbf30ceSVille Syrjälä 	if (hotplug2_trigger) {
19706dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
19716dbf30ceSVille Syrjälä 
19726dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG2);
19736dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG2, dig_hotplug_reg);
19746dbf30ceSVille Syrjälä 
1975cf53902fSRodrigo Vivi 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
19760398993bSVille Syrjälä 				   hotplug2_trigger, dig_hotplug_reg,
19770398993bSVille Syrjälä 				   dev_priv->hotplug.pch_hpd,
19786dbf30ceSVille Syrjälä 				   spt_port_hotplug2_long_detect);
19796dbf30ceSVille Syrjälä 	}
19806dbf30ceSVille Syrjälä 
19816dbf30ceSVille Syrjälä 	if (pin_mask)
198291d14251STvrtko Ursulin 		intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
19836dbf30ceSVille Syrjälä 
19846dbf30ceSVille Syrjälä 	if (pch_iir & SDE_GMBUS_CPT)
198591d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
19866dbf30ceSVille Syrjälä }
19876dbf30ceSVille Syrjälä 
198891d14251STvrtko Ursulin static void ilk_hpd_irq_handler(struct drm_i915_private *dev_priv,
19890398993bSVille Syrjälä 				u32 hotplug_trigger)
1990c008bc6eSPaulo Zanoni {
1991e4ce95aaSVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
1992e4ce95aaSVille Syrjälä 
1993e4ce95aaSVille Syrjälä 	dig_hotplug_reg = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
1994e4ce95aaSVille Syrjälä 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, dig_hotplug_reg);
1995e4ce95aaSVille Syrjälä 
19960398993bSVille Syrjälä 	intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
19970398993bSVille Syrjälä 			   hotplug_trigger, dig_hotplug_reg,
19980398993bSVille Syrjälä 			   dev_priv->hotplug.hpd,
1999e4ce95aaSVille Syrjälä 			   ilk_port_hotplug_long_detect);
200040e56410SVille Syrjälä 
200191d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2002e4ce95aaSVille Syrjälä }
2003c008bc6eSPaulo Zanoni 
200491d14251STvrtko Ursulin static void ilk_display_irq_handler(struct drm_i915_private *dev_priv,
200591d14251STvrtko Ursulin 				    u32 de_iir)
200640e56410SVille Syrjälä {
200740e56410SVille Syrjälä 	enum pipe pipe;
200840e56410SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG;
200940e56410SVille Syrjälä 
201040e56410SVille Syrjälä 	if (hotplug_trigger)
20110398993bSVille Syrjälä 		ilk_hpd_irq_handler(dev_priv, hotplug_trigger);
201240e56410SVille Syrjälä 
2013c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
201491d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
2015c008bc6eSPaulo Zanoni 
2016c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
201791d14251STvrtko Ursulin 		intel_opregion_asle_intr(dev_priv);
2018c008bc6eSPaulo Zanoni 
2019c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
202000376ccfSWambui Karuga 		drm_err(&dev_priv->drm, "Poison interrupt\n");
2021c008bc6eSPaulo Zanoni 
2022055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2023fd3a4024SDaniel Vetter 		if (de_iir & DE_PIPE_VBLANK(pipe))
2024aca9310aSAnshuman Gupta 			intel_handle_vblank(dev_priv, pipe);
2025c008bc6eSPaulo Zanoni 
202640da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
20271f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
2028c008bc6eSPaulo Zanoni 
202940da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
203091d14251STvrtko Ursulin 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
2031c008bc6eSPaulo Zanoni 	}
2032c008bc6eSPaulo Zanoni 
2033c008bc6eSPaulo Zanoni 	/* check event from PCH */
2034c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
2035c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
2036c008bc6eSPaulo Zanoni 
203791d14251STvrtko Ursulin 		if (HAS_PCH_CPT(dev_priv))
203891d14251STvrtko Ursulin 			cpt_irq_handler(dev_priv, pch_iir);
2039c008bc6eSPaulo Zanoni 		else
204091d14251STvrtko Ursulin 			ibx_irq_handler(dev_priv, pch_iir);
2041c008bc6eSPaulo Zanoni 
2042c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
2043c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
2044c008bc6eSPaulo Zanoni 	}
2045c008bc6eSPaulo Zanoni 
2046cf819effSLucas De Marchi 	if (IS_GEN(dev_priv, 5) && de_iir & DE_PCU_EVENT)
20473e7abf81SAndi Shyti 		gen5_rps_irq_handler(&dev_priv->gt.rps);
2048c008bc6eSPaulo Zanoni }
2049c008bc6eSPaulo Zanoni 
205091d14251STvrtko Ursulin static void ivb_display_irq_handler(struct drm_i915_private *dev_priv,
205191d14251STvrtko Ursulin 				    u32 de_iir)
20529719fb98SPaulo Zanoni {
205307d27e20SDamien Lespiau 	enum pipe pipe;
205423bb4cb5SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG_IVB;
205523bb4cb5SVille Syrjälä 
205640e56410SVille Syrjälä 	if (hotplug_trigger)
20570398993bSVille Syrjälä 		ilk_hpd_irq_handler(dev_priv, hotplug_trigger);
20589719fb98SPaulo Zanoni 
20599719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
206091d14251STvrtko Ursulin 		ivb_err_int_handler(dev_priv);
20619719fb98SPaulo Zanoni 
206254fd3149SDhinakaran Pandiyan 	if (de_iir & DE_EDP_PSR_INT_HSW) {
206354fd3149SDhinakaran Pandiyan 		u32 psr_iir = I915_READ(EDP_PSR_IIR);
206454fd3149SDhinakaran Pandiyan 
206554fd3149SDhinakaran Pandiyan 		intel_psr_irq_handler(dev_priv, psr_iir);
206654fd3149SDhinakaran Pandiyan 		I915_WRITE(EDP_PSR_IIR, psr_iir);
206754fd3149SDhinakaran Pandiyan 	}
2068fc340442SDaniel Vetter 
20699719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
207091d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
20719719fb98SPaulo Zanoni 
20729719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
207391d14251STvrtko Ursulin 		intel_opregion_asle_intr(dev_priv);
20749719fb98SPaulo Zanoni 
2075055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2076fd3a4024SDaniel Vetter 		if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)))
2077aca9310aSAnshuman Gupta 			intel_handle_vblank(dev_priv, pipe);
20789719fb98SPaulo Zanoni 	}
20799719fb98SPaulo Zanoni 
20809719fb98SPaulo Zanoni 	/* check event from PCH */
208191d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv) && (de_iir & DE_PCH_EVENT_IVB)) {
20829719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
20839719fb98SPaulo Zanoni 
208491d14251STvrtko Ursulin 		cpt_irq_handler(dev_priv, pch_iir);
20859719fb98SPaulo Zanoni 
20869719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
20879719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
20889719fb98SPaulo Zanoni 	}
20899719fb98SPaulo Zanoni }
20909719fb98SPaulo Zanoni 
209172c90f62SOscar Mateo /*
209272c90f62SOscar Mateo  * To handle irqs with the minimum potential races with fresh interrupts, we:
209372c90f62SOscar Mateo  * 1 - Disable Master Interrupt Control.
209472c90f62SOscar Mateo  * 2 - Find the source(s) of the interrupt.
209572c90f62SOscar Mateo  * 3 - Clear the Interrupt Identity bits (IIR).
209672c90f62SOscar Mateo  * 4 - Process the interrupt(s) that had bits set in the IIRs.
209772c90f62SOscar Mateo  * 5 - Re-enable Master Interrupt Control.
209872c90f62SOscar Mateo  */
20999eae5e27SLucas De Marchi static irqreturn_t ilk_irq_handler(int irq, void *arg)
2100b1f14ad0SJesse Barnes {
2101c48a798aSChris Wilson 	struct drm_i915_private *i915 = arg;
2102c48a798aSChris Wilson 	void __iomem * const regs = i915->uncore.regs;
2103f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
21040e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
2105b1f14ad0SJesse Barnes 
2106c48a798aSChris Wilson 	if (unlikely(!intel_irqs_enabled(i915)))
21072dd2a883SImre Deak 		return IRQ_NONE;
21082dd2a883SImre Deak 
21091f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
2110c48a798aSChris Wilson 	disable_rpm_wakeref_asserts(&i915->runtime_pm);
21111f814dacSImre Deak 
2112b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
2113c48a798aSChris Wilson 	de_ier = raw_reg_read(regs, DEIER);
2114c48a798aSChris Wilson 	raw_reg_write(regs, DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
21150e43406bSChris Wilson 
211644498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
211744498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
211844498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
211944498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
212044498aeaSPaulo Zanoni 	 * due to its back queue). */
2121c48a798aSChris Wilson 	if (!HAS_PCH_NOP(i915)) {
2122c48a798aSChris Wilson 		sde_ier = raw_reg_read(regs, SDEIER);
2123c48a798aSChris Wilson 		raw_reg_write(regs, SDEIER, 0);
2124ab5c608bSBen Widawsky 	}
212544498aeaSPaulo Zanoni 
212672c90f62SOscar Mateo 	/* Find, clear, then process each source of interrupt */
212772c90f62SOscar Mateo 
2128c48a798aSChris Wilson 	gt_iir = raw_reg_read(regs, GTIIR);
21290e43406bSChris Wilson 	if (gt_iir) {
2130c48a798aSChris Wilson 		raw_reg_write(regs, GTIIR, gt_iir);
2131c48a798aSChris Wilson 		if (INTEL_GEN(i915) >= 6)
2132c48a798aSChris Wilson 			gen6_gt_irq_handler(&i915->gt, gt_iir);
2133d8fc8a47SPaulo Zanoni 		else
2134c48a798aSChris Wilson 			gen5_gt_irq_handler(&i915->gt, gt_iir);
2135c48a798aSChris Wilson 		ret = IRQ_HANDLED;
21360e43406bSChris Wilson 	}
2137b1f14ad0SJesse Barnes 
2138c48a798aSChris Wilson 	de_iir = raw_reg_read(regs, DEIIR);
21390e43406bSChris Wilson 	if (de_iir) {
2140c48a798aSChris Wilson 		raw_reg_write(regs, DEIIR, de_iir);
2141c48a798aSChris Wilson 		if (INTEL_GEN(i915) >= 7)
2142c48a798aSChris Wilson 			ivb_display_irq_handler(i915, de_iir);
2143f1af8fc1SPaulo Zanoni 		else
2144c48a798aSChris Wilson 			ilk_display_irq_handler(i915, de_iir);
21450e43406bSChris Wilson 		ret = IRQ_HANDLED;
2146c48a798aSChris Wilson 	}
2147c48a798aSChris Wilson 
2148c48a798aSChris Wilson 	if (INTEL_GEN(i915) >= 6) {
2149c48a798aSChris Wilson 		u32 pm_iir = raw_reg_read(regs, GEN6_PMIIR);
2150c48a798aSChris Wilson 		if (pm_iir) {
2151c48a798aSChris Wilson 			raw_reg_write(regs, GEN6_PMIIR, pm_iir);
2152c48a798aSChris Wilson 			gen6_rps_irq_handler(&i915->gt.rps, pm_iir);
2153c48a798aSChris Wilson 			ret = IRQ_HANDLED;
21540e43406bSChris Wilson 		}
2155f1af8fc1SPaulo Zanoni 	}
2156b1f14ad0SJesse Barnes 
2157c48a798aSChris Wilson 	raw_reg_write(regs, DEIER, de_ier);
2158c48a798aSChris Wilson 	if (sde_ier)
2159c48a798aSChris Wilson 		raw_reg_write(regs, SDEIER, sde_ier);
2160b1f14ad0SJesse Barnes 
21611f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
2162c48a798aSChris Wilson 	enable_rpm_wakeref_asserts(&i915->runtime_pm);
21631f814dacSImre Deak 
2164b1f14ad0SJesse Barnes 	return ret;
2165b1f14ad0SJesse Barnes }
2166b1f14ad0SJesse Barnes 
216791d14251STvrtko Ursulin static void bxt_hpd_irq_handler(struct drm_i915_private *dev_priv,
21680398993bSVille Syrjälä 				u32 hotplug_trigger)
2169d04a492dSShashank Sharma {
2170cebd87a0SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2171d04a492dSShashank Sharma 
2172a52bb15bSVille Syrjälä 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
2173a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
2174d04a492dSShashank Sharma 
21750398993bSVille Syrjälä 	intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
21760398993bSVille Syrjälä 			   hotplug_trigger, dig_hotplug_reg,
21770398993bSVille Syrjälä 			   dev_priv->hotplug.hpd,
2178cebd87a0SVille Syrjälä 			   bxt_port_hotplug_long_detect);
217940e56410SVille Syrjälä 
218091d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2181d04a492dSShashank Sharma }
2182d04a492dSShashank Sharma 
2183121e758eSDhinakaran Pandiyan static void gen11_hpd_irq_handler(struct drm_i915_private *dev_priv, u32 iir)
2184121e758eSDhinakaran Pandiyan {
2185121e758eSDhinakaran Pandiyan 	u32 pin_mask = 0, long_mask = 0;
2186b796b971SDhinakaran Pandiyan 	u32 trigger_tc = iir & GEN11_DE_TC_HOTPLUG_MASK;
2187b796b971SDhinakaran Pandiyan 	u32 trigger_tbt = iir & GEN11_DE_TBT_HOTPLUG_MASK;
218848ef15d3SJosé Roberto de Souza 	long_pulse_detect_func long_pulse_detect;
218948ef15d3SJosé Roberto de Souza 
21900398993bSVille Syrjälä 	if (INTEL_GEN(dev_priv) >= 12)
219148ef15d3SJosé Roberto de Souza 		long_pulse_detect = gen12_port_hotplug_long_detect;
21920398993bSVille Syrjälä 	else
219348ef15d3SJosé Roberto de Souza 		long_pulse_detect = gen11_port_hotplug_long_detect;
2194121e758eSDhinakaran Pandiyan 
2195121e758eSDhinakaran Pandiyan 	if (trigger_tc) {
2196b796b971SDhinakaran Pandiyan 		u32 dig_hotplug_reg;
2197b796b971SDhinakaran Pandiyan 
2198121e758eSDhinakaran Pandiyan 		dig_hotplug_reg = I915_READ(GEN11_TC_HOTPLUG_CTL);
2199121e758eSDhinakaran Pandiyan 		I915_WRITE(GEN11_TC_HOTPLUG_CTL, dig_hotplug_reg);
2200121e758eSDhinakaran Pandiyan 
22010398993bSVille Syrjälä 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
22020398993bSVille Syrjälä 				   trigger_tc, dig_hotplug_reg,
22030398993bSVille Syrjälä 				   dev_priv->hotplug.hpd,
22040398993bSVille Syrjälä 				   long_pulse_detect);
2205121e758eSDhinakaran Pandiyan 	}
2206b796b971SDhinakaran Pandiyan 
2207b796b971SDhinakaran Pandiyan 	if (trigger_tbt) {
2208b796b971SDhinakaran Pandiyan 		u32 dig_hotplug_reg;
2209b796b971SDhinakaran Pandiyan 
2210b796b971SDhinakaran Pandiyan 		dig_hotplug_reg = I915_READ(GEN11_TBT_HOTPLUG_CTL);
2211b796b971SDhinakaran Pandiyan 		I915_WRITE(GEN11_TBT_HOTPLUG_CTL, dig_hotplug_reg);
2212b796b971SDhinakaran Pandiyan 
22130398993bSVille Syrjälä 		intel_get_hpd_pins(dev_priv, &pin_mask, &long_mask,
22140398993bSVille Syrjälä 				   trigger_tbt, dig_hotplug_reg,
22150398993bSVille Syrjälä 				   dev_priv->hotplug.hpd,
22160398993bSVille Syrjälä 				   long_pulse_detect);
2217b796b971SDhinakaran Pandiyan 	}
2218b796b971SDhinakaran Pandiyan 
2219b796b971SDhinakaran Pandiyan 	if (pin_mask)
2220b796b971SDhinakaran Pandiyan 		intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2221b796b971SDhinakaran Pandiyan 	else
222200376ccfSWambui Karuga 		drm_err(&dev_priv->drm,
222300376ccfSWambui Karuga 			"Unexpected DE HPD interrupt 0x%08x\n", iir);
2224121e758eSDhinakaran Pandiyan }
2225121e758eSDhinakaran Pandiyan 
22269d17210fSLucas De Marchi static u32 gen8_de_port_aux_mask(struct drm_i915_private *dev_priv)
22279d17210fSLucas De Marchi {
222855523360SLucas De Marchi 	u32 mask;
22299d17210fSLucas De Marchi 
223055523360SLucas De Marchi 	if (INTEL_GEN(dev_priv) >= 12)
223155523360SLucas De Marchi 		return TGL_DE_PORT_AUX_DDIA |
223255523360SLucas De Marchi 			TGL_DE_PORT_AUX_DDIB |
2233e5df52dcSMatt Roper 			TGL_DE_PORT_AUX_DDIC |
2234e5df52dcSMatt Roper 			TGL_DE_PORT_AUX_USBC1 |
2235e5df52dcSMatt Roper 			TGL_DE_PORT_AUX_USBC2 |
2236e5df52dcSMatt Roper 			TGL_DE_PORT_AUX_USBC3 |
2237e5df52dcSMatt Roper 			TGL_DE_PORT_AUX_USBC4 |
2238e5df52dcSMatt Roper 			TGL_DE_PORT_AUX_USBC5 |
2239e5df52dcSMatt Roper 			TGL_DE_PORT_AUX_USBC6;
2240e5df52dcSMatt Roper 
224155523360SLucas De Marchi 
224255523360SLucas De Marchi 	mask = GEN8_AUX_CHANNEL_A;
22439d17210fSLucas De Marchi 	if (INTEL_GEN(dev_priv) >= 9)
22449d17210fSLucas De Marchi 		mask |= GEN9_AUX_CHANNEL_B |
22459d17210fSLucas De Marchi 			GEN9_AUX_CHANNEL_C |
22469d17210fSLucas De Marchi 			GEN9_AUX_CHANNEL_D;
22479d17210fSLucas De Marchi 
224855523360SLucas De Marchi 	if (IS_CNL_WITH_PORT_F(dev_priv) || IS_GEN(dev_priv, 11))
22499d17210fSLucas De Marchi 		mask |= CNL_AUX_CHANNEL_F;
22509d17210fSLucas De Marchi 
225155523360SLucas De Marchi 	if (IS_GEN(dev_priv, 11))
225255523360SLucas De Marchi 		mask |= ICL_AUX_CHANNEL_E;
22539d17210fSLucas De Marchi 
22549d17210fSLucas De Marchi 	return mask;
22559d17210fSLucas De Marchi }
22569d17210fSLucas De Marchi 
22575270130dSVille Syrjälä static u32 gen8_de_pipe_fault_mask(struct drm_i915_private *dev_priv)
22585270130dSVille Syrjälä {
225999e2d8bcSMatt Roper 	if (IS_ROCKETLAKE(dev_priv))
226099e2d8bcSMatt Roper 		return RKL_DE_PIPE_IRQ_FAULT_ERRORS;
226199e2d8bcSMatt Roper 	else if (INTEL_GEN(dev_priv) >= 11)
2262d506a65dSMatt Roper 		return GEN11_DE_PIPE_IRQ_FAULT_ERRORS;
2263d506a65dSMatt Roper 	else if (INTEL_GEN(dev_priv) >= 9)
22645270130dSVille Syrjälä 		return GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
22655270130dSVille Syrjälä 	else
22665270130dSVille Syrjälä 		return GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
22675270130dSVille Syrjälä }
22685270130dSVille Syrjälä 
226946c63d24SJosé Roberto de Souza static void
227046c63d24SJosé Roberto de Souza gen8_de_misc_irq_handler(struct drm_i915_private *dev_priv, u32 iir)
2271abd58f01SBen Widawsky {
2272e04f7eceSVille Syrjälä 	bool found = false;
2273e04f7eceSVille Syrjälä 
2274e04f7eceSVille Syrjälä 	if (iir & GEN8_DE_MISC_GSE) {
227591d14251STvrtko Ursulin 		intel_opregion_asle_intr(dev_priv);
2276e04f7eceSVille Syrjälä 		found = true;
2277e04f7eceSVille Syrjälä 	}
2278e04f7eceSVille Syrjälä 
2279e04f7eceSVille Syrjälä 	if (iir & GEN8_DE_EDP_PSR) {
22808241cfbeSJosé Roberto de Souza 		u32 psr_iir;
22818241cfbeSJosé Roberto de Souza 		i915_reg_t iir_reg;
22828241cfbeSJosé Roberto de Souza 
22838241cfbeSJosé Roberto de Souza 		if (INTEL_GEN(dev_priv) >= 12)
22848241cfbeSJosé Roberto de Souza 			iir_reg = TRANS_PSR_IIR(dev_priv->psr.transcoder);
22858241cfbeSJosé Roberto de Souza 		else
22868241cfbeSJosé Roberto de Souza 			iir_reg = EDP_PSR_IIR;
22878241cfbeSJosé Roberto de Souza 
22888241cfbeSJosé Roberto de Souza 		psr_iir = I915_READ(iir_reg);
22898241cfbeSJosé Roberto de Souza 		I915_WRITE(iir_reg, psr_iir);
22908241cfbeSJosé Roberto de Souza 
22918241cfbeSJosé Roberto de Souza 		if (psr_iir)
22928241cfbeSJosé Roberto de Souza 			found = true;
229354fd3149SDhinakaran Pandiyan 
229454fd3149SDhinakaran Pandiyan 		intel_psr_irq_handler(dev_priv, psr_iir);
2295e04f7eceSVille Syrjälä 	}
2296e04f7eceSVille Syrjälä 
2297e04f7eceSVille Syrjälä 	if (!found)
229800376ccfSWambui Karuga 		drm_err(&dev_priv->drm, "Unexpected DE Misc interrupt\n");
2299abd58f01SBen Widawsky }
230046c63d24SJosé Roberto de Souza 
230146c63d24SJosé Roberto de Souza static irqreturn_t
230246c63d24SJosé Roberto de Souza gen8_de_irq_handler(struct drm_i915_private *dev_priv, u32 master_ctl)
230346c63d24SJosé Roberto de Souza {
230446c63d24SJosé Roberto de Souza 	irqreturn_t ret = IRQ_NONE;
230546c63d24SJosé Roberto de Souza 	u32 iir;
230646c63d24SJosé Roberto de Souza 	enum pipe pipe;
230746c63d24SJosé Roberto de Souza 
230846c63d24SJosé Roberto de Souza 	if (master_ctl & GEN8_DE_MISC_IRQ) {
230946c63d24SJosé Roberto de Souza 		iir = I915_READ(GEN8_DE_MISC_IIR);
231046c63d24SJosé Roberto de Souza 		if (iir) {
231146c63d24SJosé Roberto de Souza 			I915_WRITE(GEN8_DE_MISC_IIR, iir);
231246c63d24SJosé Roberto de Souza 			ret = IRQ_HANDLED;
231346c63d24SJosé Roberto de Souza 			gen8_de_misc_irq_handler(dev_priv, iir);
231446c63d24SJosé Roberto de Souza 		} else {
231500376ccfSWambui Karuga 			drm_err(&dev_priv->drm,
231600376ccfSWambui Karuga 				"The master control interrupt lied (DE MISC)!\n");
2317abd58f01SBen Widawsky 		}
231846c63d24SJosé Roberto de Souza 	}
2319abd58f01SBen Widawsky 
2320121e758eSDhinakaran Pandiyan 	if (INTEL_GEN(dev_priv) >= 11 && (master_ctl & GEN11_DE_HPD_IRQ)) {
2321121e758eSDhinakaran Pandiyan 		iir = I915_READ(GEN11_DE_HPD_IIR);
2322121e758eSDhinakaran Pandiyan 		if (iir) {
2323121e758eSDhinakaran Pandiyan 			I915_WRITE(GEN11_DE_HPD_IIR, iir);
2324121e758eSDhinakaran Pandiyan 			ret = IRQ_HANDLED;
2325121e758eSDhinakaran Pandiyan 			gen11_hpd_irq_handler(dev_priv, iir);
2326121e758eSDhinakaran Pandiyan 		} else {
232700376ccfSWambui Karuga 			drm_err(&dev_priv->drm,
232800376ccfSWambui Karuga 				"The master control interrupt lied, (DE HPD)!\n");
2329121e758eSDhinakaran Pandiyan 		}
2330121e758eSDhinakaran Pandiyan 	}
2331121e758eSDhinakaran Pandiyan 
23326d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
2333e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_PORT_IIR);
2334e32192e1STvrtko Ursulin 		if (iir) {
2335e32192e1STvrtko Ursulin 			u32 tmp_mask;
2336d04a492dSShashank Sharma 			bool found = false;
2337cebd87a0SVille Syrjälä 
2338e32192e1STvrtko Ursulin 			I915_WRITE(GEN8_DE_PORT_IIR, iir);
23396d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
234088e04703SJesse Barnes 
23419d17210fSLucas De Marchi 			if (iir & gen8_de_port_aux_mask(dev_priv)) {
234291d14251STvrtko Ursulin 				dp_aux_irq_handler(dev_priv);
2343d04a492dSShashank Sharma 				found = true;
2344d04a492dSShashank Sharma 			}
2345d04a492dSShashank Sharma 
2346cc3f90f0SAnder Conselvan de Oliveira 			if (IS_GEN9_LP(dev_priv)) {
2347e32192e1STvrtko Ursulin 				tmp_mask = iir & BXT_DE_PORT_HOTPLUG_MASK;
2348e32192e1STvrtko Ursulin 				if (tmp_mask) {
23490398993bSVille Syrjälä 					bxt_hpd_irq_handler(dev_priv, tmp_mask);
2350d04a492dSShashank Sharma 					found = true;
2351d04a492dSShashank Sharma 				}
2352e32192e1STvrtko Ursulin 			} else if (IS_BROADWELL(dev_priv)) {
2353e32192e1STvrtko Ursulin 				tmp_mask = iir & GEN8_PORT_DP_A_HOTPLUG;
2354e32192e1STvrtko Ursulin 				if (tmp_mask) {
23550398993bSVille Syrjälä 					ilk_hpd_irq_handler(dev_priv, tmp_mask);
2356e32192e1STvrtko Ursulin 					found = true;
2357e32192e1STvrtko Ursulin 				}
2358e32192e1STvrtko Ursulin 			}
2359d04a492dSShashank Sharma 
2360cc3f90f0SAnder Conselvan de Oliveira 			if (IS_GEN9_LP(dev_priv) && (iir & BXT_DE_PORT_GMBUS)) {
236191d14251STvrtko Ursulin 				gmbus_irq_handler(dev_priv);
23629e63743eSShashank Sharma 				found = true;
23639e63743eSShashank Sharma 			}
23649e63743eSShashank Sharma 
2365d04a492dSShashank Sharma 			if (!found)
236600376ccfSWambui Karuga 				drm_err(&dev_priv->drm,
236700376ccfSWambui Karuga 					"Unexpected DE Port interrupt\n");
23686d766f02SDaniel Vetter 		}
236938cc46d7SOscar Mateo 		else
237000376ccfSWambui Karuga 			drm_err(&dev_priv->drm,
237100376ccfSWambui Karuga 				"The master control interrupt lied (DE PORT)!\n");
23726d766f02SDaniel Vetter 	}
23736d766f02SDaniel Vetter 
2374055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2375fd3a4024SDaniel Vetter 		u32 fault_errors;
2376abd58f01SBen Widawsky 
2377c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2378c42664ccSDaniel Vetter 			continue;
2379c42664ccSDaniel Vetter 
2380e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
2381e32192e1STvrtko Ursulin 		if (!iir) {
238200376ccfSWambui Karuga 			drm_err(&dev_priv->drm,
238300376ccfSWambui Karuga 				"The master control interrupt lied (DE PIPE)!\n");
2384e32192e1STvrtko Ursulin 			continue;
2385e32192e1STvrtko Ursulin 		}
2386770de83dSDamien Lespiau 
2387e32192e1STvrtko Ursulin 		ret = IRQ_HANDLED;
2388e32192e1STvrtko Ursulin 		I915_WRITE(GEN8_DE_PIPE_IIR(pipe), iir);
2389e32192e1STvrtko Ursulin 
2390fd3a4024SDaniel Vetter 		if (iir & GEN8_PIPE_VBLANK)
2391aca9310aSAnshuman Gupta 			intel_handle_vblank(dev_priv, pipe);
2392abd58f01SBen Widawsky 
2393e32192e1STvrtko Ursulin 		if (iir & GEN8_PIPE_CDCLK_CRC_DONE)
239491d14251STvrtko Ursulin 			hsw_pipe_crc_irq_handler(dev_priv, pipe);
23950fbe7870SDaniel Vetter 
2396e32192e1STvrtko Ursulin 		if (iir & GEN8_PIPE_FIFO_UNDERRUN)
2397e32192e1STvrtko Ursulin 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
239838d83c96SDaniel Vetter 
23995270130dSVille Syrjälä 		fault_errors = iir & gen8_de_pipe_fault_mask(dev_priv);
2400770de83dSDamien Lespiau 		if (fault_errors)
240100376ccfSWambui Karuga 			drm_err(&dev_priv->drm,
240200376ccfSWambui Karuga 				"Fault errors on pipe %c: 0x%08x\n",
240330100f2bSDaniel Vetter 				pipe_name(pipe),
2404e32192e1STvrtko Ursulin 				fault_errors);
2405abd58f01SBen Widawsky 	}
2406abd58f01SBen Widawsky 
240791d14251STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv) && !HAS_PCH_NOP(dev_priv) &&
2408266ea3d9SShashank Sharma 	    master_ctl & GEN8_DE_PCH_IRQ) {
240992d03a80SDaniel Vetter 		/*
241092d03a80SDaniel Vetter 		 * FIXME(BDW): Assume for now that the new interrupt handling
241192d03a80SDaniel Vetter 		 * scheme also closed the SDE interrupt handling race we've seen
241292d03a80SDaniel Vetter 		 * on older pch-split platforms. But this needs testing.
241392d03a80SDaniel Vetter 		 */
2414e32192e1STvrtko Ursulin 		iir = I915_READ(SDEIIR);
2415e32192e1STvrtko Ursulin 		if (iir) {
2416e32192e1STvrtko Ursulin 			I915_WRITE(SDEIIR, iir);
241792d03a80SDaniel Vetter 			ret = IRQ_HANDLED;
24186dbf30ceSVille Syrjälä 
241958676af6SLucas De Marchi 			if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
242058676af6SLucas De Marchi 				icp_irq_handler(dev_priv, iir);
2421c6c30b91SRodrigo Vivi 			else if (INTEL_PCH_TYPE(dev_priv) >= PCH_SPT)
242291d14251STvrtko Ursulin 				spt_irq_handler(dev_priv, iir);
24236dbf30ceSVille Syrjälä 			else
242491d14251STvrtko Ursulin 				cpt_irq_handler(dev_priv, iir);
24252dfb0b81SJani Nikula 		} else {
24262dfb0b81SJani Nikula 			/*
24272dfb0b81SJani Nikula 			 * Like on previous PCH there seems to be something
24282dfb0b81SJani Nikula 			 * fishy going on with forwarding PCH interrupts.
24292dfb0b81SJani Nikula 			 */
243000376ccfSWambui Karuga 			drm_dbg(&dev_priv->drm,
243100376ccfSWambui Karuga 				"The master control interrupt lied (SDE)!\n");
24322dfb0b81SJani Nikula 		}
243392d03a80SDaniel Vetter 	}
243492d03a80SDaniel Vetter 
2435f11a0f46STvrtko Ursulin 	return ret;
2436f11a0f46STvrtko Ursulin }
2437f11a0f46STvrtko Ursulin 
24384376b9c9SMika Kuoppala static inline u32 gen8_master_intr_disable(void __iomem * const regs)
24394376b9c9SMika Kuoppala {
24404376b9c9SMika Kuoppala 	raw_reg_write(regs, GEN8_MASTER_IRQ, 0);
24414376b9c9SMika Kuoppala 
24424376b9c9SMika Kuoppala 	/*
24434376b9c9SMika Kuoppala 	 * Now with master disabled, get a sample of level indications
24444376b9c9SMika Kuoppala 	 * for this interrupt. Indications will be cleared on related acks.
24454376b9c9SMika Kuoppala 	 * New indications can and will light up during processing,
24464376b9c9SMika Kuoppala 	 * and will generate new interrupt after enabling master.
24474376b9c9SMika Kuoppala 	 */
24484376b9c9SMika Kuoppala 	return raw_reg_read(regs, GEN8_MASTER_IRQ);
24494376b9c9SMika Kuoppala }
24504376b9c9SMika Kuoppala 
24514376b9c9SMika Kuoppala static inline void gen8_master_intr_enable(void __iomem * const regs)
24524376b9c9SMika Kuoppala {
24534376b9c9SMika Kuoppala 	raw_reg_write(regs, GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
24544376b9c9SMika Kuoppala }
24554376b9c9SMika Kuoppala 
2456f11a0f46STvrtko Ursulin static irqreturn_t gen8_irq_handler(int irq, void *arg)
2457f11a0f46STvrtko Ursulin {
2458b318b824SVille Syrjälä 	struct drm_i915_private *dev_priv = arg;
245925286aacSDaniele Ceraolo Spurio 	void __iomem * const regs = dev_priv->uncore.regs;
2460f11a0f46STvrtko Ursulin 	u32 master_ctl;
2461f11a0f46STvrtko Ursulin 
2462f11a0f46STvrtko Ursulin 	if (!intel_irqs_enabled(dev_priv))
2463f11a0f46STvrtko Ursulin 		return IRQ_NONE;
2464f11a0f46STvrtko Ursulin 
24654376b9c9SMika Kuoppala 	master_ctl = gen8_master_intr_disable(regs);
24664376b9c9SMika Kuoppala 	if (!master_ctl) {
24674376b9c9SMika Kuoppala 		gen8_master_intr_enable(regs);
2468f11a0f46STvrtko Ursulin 		return IRQ_NONE;
24694376b9c9SMika Kuoppala 	}
2470f11a0f46STvrtko Ursulin 
24716cc32f15SChris Wilson 	/* Find, queue (onto bottom-halves), then clear each source */
24726cc32f15SChris Wilson 	gen8_gt_irq_handler(&dev_priv->gt, master_ctl);
2473f0fd96f5SChris Wilson 
2474f0fd96f5SChris Wilson 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
2475f0fd96f5SChris Wilson 	if (master_ctl & ~GEN8_GT_IRQS) {
24769102650fSDaniele Ceraolo Spurio 		disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
247755ef72f2SChris Wilson 		gen8_de_irq_handler(dev_priv, master_ctl);
24789102650fSDaniele Ceraolo Spurio 		enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
2479f0fd96f5SChris Wilson 	}
2480f11a0f46STvrtko Ursulin 
24814376b9c9SMika Kuoppala 	gen8_master_intr_enable(regs);
2482abd58f01SBen Widawsky 
248355ef72f2SChris Wilson 	return IRQ_HANDLED;
2484abd58f01SBen Widawsky }
2485abd58f01SBen Widawsky 
248651951ae7SMika Kuoppala static u32
24879b77011eSTvrtko Ursulin gen11_gu_misc_irq_ack(struct intel_gt *gt, const u32 master_ctl)
2488df0d28c1SDhinakaran Pandiyan {
24899b77011eSTvrtko Ursulin 	void __iomem * const regs = gt->uncore->regs;
24907a909383SChris Wilson 	u32 iir;
2491df0d28c1SDhinakaran Pandiyan 
2492df0d28c1SDhinakaran Pandiyan 	if (!(master_ctl & GEN11_GU_MISC_IRQ))
24937a909383SChris Wilson 		return 0;
2494df0d28c1SDhinakaran Pandiyan 
24957a909383SChris Wilson 	iir = raw_reg_read(regs, GEN11_GU_MISC_IIR);
24967a909383SChris Wilson 	if (likely(iir))
24977a909383SChris Wilson 		raw_reg_write(regs, GEN11_GU_MISC_IIR, iir);
24987a909383SChris Wilson 
24997a909383SChris Wilson 	return iir;
2500df0d28c1SDhinakaran Pandiyan }
2501df0d28c1SDhinakaran Pandiyan 
2502df0d28c1SDhinakaran Pandiyan static void
25039b77011eSTvrtko Ursulin gen11_gu_misc_irq_handler(struct intel_gt *gt, const u32 iir)
2504df0d28c1SDhinakaran Pandiyan {
2505df0d28c1SDhinakaran Pandiyan 	if (iir & GEN11_GU_MISC_GSE)
25069b77011eSTvrtko Ursulin 		intel_opregion_asle_intr(gt->i915);
2507df0d28c1SDhinakaran Pandiyan }
2508df0d28c1SDhinakaran Pandiyan 
250981067b71SMika Kuoppala static inline u32 gen11_master_intr_disable(void __iomem * const regs)
251081067b71SMika Kuoppala {
251181067b71SMika Kuoppala 	raw_reg_write(regs, GEN11_GFX_MSTR_IRQ, 0);
251281067b71SMika Kuoppala 
251381067b71SMika Kuoppala 	/*
251481067b71SMika Kuoppala 	 * Now with master disabled, get a sample of level indications
251581067b71SMika Kuoppala 	 * for this interrupt. Indications will be cleared on related acks.
251681067b71SMika Kuoppala 	 * New indications can and will light up during processing,
251781067b71SMika Kuoppala 	 * and will generate new interrupt after enabling master.
251881067b71SMika Kuoppala 	 */
251981067b71SMika Kuoppala 	return raw_reg_read(regs, GEN11_GFX_MSTR_IRQ);
252081067b71SMika Kuoppala }
252181067b71SMika Kuoppala 
252281067b71SMika Kuoppala static inline void gen11_master_intr_enable(void __iomem * const regs)
252381067b71SMika Kuoppala {
252481067b71SMika Kuoppala 	raw_reg_write(regs, GEN11_GFX_MSTR_IRQ, GEN11_MASTER_IRQ);
252581067b71SMika Kuoppala }
252681067b71SMika Kuoppala 
2527a3265d85SMatt Roper static void
2528a3265d85SMatt Roper gen11_display_irq_handler(struct drm_i915_private *i915)
2529a3265d85SMatt Roper {
2530a3265d85SMatt Roper 	void __iomem * const regs = i915->uncore.regs;
2531a3265d85SMatt Roper 	const u32 disp_ctl = raw_reg_read(regs, GEN11_DISPLAY_INT_CTL);
2532a3265d85SMatt Roper 
2533a3265d85SMatt Roper 	disable_rpm_wakeref_asserts(&i915->runtime_pm);
2534a3265d85SMatt Roper 	/*
2535a3265d85SMatt Roper 	 * GEN11_DISPLAY_INT_CTL has same format as GEN8_MASTER_IRQ
2536a3265d85SMatt Roper 	 * for the display related bits.
2537a3265d85SMatt Roper 	 */
2538a3265d85SMatt Roper 	raw_reg_write(regs, GEN11_DISPLAY_INT_CTL, 0x0);
2539a3265d85SMatt Roper 	gen8_de_irq_handler(i915, disp_ctl);
2540a3265d85SMatt Roper 	raw_reg_write(regs, GEN11_DISPLAY_INT_CTL,
2541a3265d85SMatt Roper 		      GEN11_DISPLAY_IRQ_ENABLE);
2542a3265d85SMatt Roper 
2543a3265d85SMatt Roper 	enable_rpm_wakeref_asserts(&i915->runtime_pm);
2544a3265d85SMatt Roper }
2545a3265d85SMatt Roper 
25467be8782aSLucas De Marchi static __always_inline irqreturn_t
25477be8782aSLucas De Marchi __gen11_irq_handler(struct drm_i915_private * const i915,
25487be8782aSLucas De Marchi 		    u32 (*intr_disable)(void __iomem * const regs),
25497be8782aSLucas De Marchi 		    void (*intr_enable)(void __iomem * const regs))
255051951ae7SMika Kuoppala {
255125286aacSDaniele Ceraolo Spurio 	void __iomem * const regs = i915->uncore.regs;
25529b77011eSTvrtko Ursulin 	struct intel_gt *gt = &i915->gt;
255351951ae7SMika Kuoppala 	u32 master_ctl;
2554df0d28c1SDhinakaran Pandiyan 	u32 gu_misc_iir;
255551951ae7SMika Kuoppala 
255651951ae7SMika Kuoppala 	if (!intel_irqs_enabled(i915))
255751951ae7SMika Kuoppala 		return IRQ_NONE;
255851951ae7SMika Kuoppala 
25597be8782aSLucas De Marchi 	master_ctl = intr_disable(regs);
256081067b71SMika Kuoppala 	if (!master_ctl) {
25617be8782aSLucas De Marchi 		intr_enable(regs);
256251951ae7SMika Kuoppala 		return IRQ_NONE;
256381067b71SMika Kuoppala 	}
256451951ae7SMika Kuoppala 
25656cc32f15SChris Wilson 	/* Find, queue (onto bottom-halves), then clear each source */
25669b77011eSTvrtko Ursulin 	gen11_gt_irq_handler(gt, master_ctl);
256751951ae7SMika Kuoppala 
256851951ae7SMika Kuoppala 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
2569a3265d85SMatt Roper 	if (master_ctl & GEN11_DISPLAY_IRQ)
2570a3265d85SMatt Roper 		gen11_display_irq_handler(i915);
257151951ae7SMika Kuoppala 
25729b77011eSTvrtko Ursulin 	gu_misc_iir = gen11_gu_misc_irq_ack(gt, master_ctl);
2573df0d28c1SDhinakaran Pandiyan 
25747be8782aSLucas De Marchi 	intr_enable(regs);
257551951ae7SMika Kuoppala 
25769b77011eSTvrtko Ursulin 	gen11_gu_misc_irq_handler(gt, gu_misc_iir);
2577df0d28c1SDhinakaran Pandiyan 
257851951ae7SMika Kuoppala 	return IRQ_HANDLED;
257951951ae7SMika Kuoppala }
258051951ae7SMika Kuoppala 
25817be8782aSLucas De Marchi static irqreturn_t gen11_irq_handler(int irq, void *arg)
25827be8782aSLucas De Marchi {
25837be8782aSLucas De Marchi 	return __gen11_irq_handler(arg,
25847be8782aSLucas De Marchi 				   gen11_master_intr_disable,
25857be8782aSLucas De Marchi 				   gen11_master_intr_enable);
25867be8782aSLucas De Marchi }
25877be8782aSLucas De Marchi 
258897b492f5SLucas De Marchi static u32 dg1_master_intr_disable_and_ack(void __iomem * const regs)
258997b492f5SLucas De Marchi {
259097b492f5SLucas De Marchi 	u32 val;
259197b492f5SLucas De Marchi 
259297b492f5SLucas De Marchi 	/* First disable interrupts */
259397b492f5SLucas De Marchi 	raw_reg_write(regs, DG1_MSTR_UNIT_INTR, 0);
259497b492f5SLucas De Marchi 
259597b492f5SLucas De Marchi 	/* Get the indication levels and ack the master unit */
259697b492f5SLucas De Marchi 	val = raw_reg_read(regs, DG1_MSTR_UNIT_INTR);
259797b492f5SLucas De Marchi 	if (unlikely(!val))
259897b492f5SLucas De Marchi 		return 0;
259997b492f5SLucas De Marchi 
260097b492f5SLucas De Marchi 	raw_reg_write(regs, DG1_MSTR_UNIT_INTR, val);
260197b492f5SLucas De Marchi 
260297b492f5SLucas De Marchi 	/*
260397b492f5SLucas De Marchi 	 * Now with master disabled, get a sample of level indications
260497b492f5SLucas De Marchi 	 * for this interrupt and ack them right away - we keep GEN11_MASTER_IRQ
260597b492f5SLucas De Marchi 	 * out as this bit doesn't exist anymore for DG1
260697b492f5SLucas De Marchi 	 */
260797b492f5SLucas De Marchi 	val = raw_reg_read(regs, GEN11_GFX_MSTR_IRQ) & ~GEN11_MASTER_IRQ;
260897b492f5SLucas De Marchi 	if (unlikely(!val))
260997b492f5SLucas De Marchi 		return 0;
261097b492f5SLucas De Marchi 
261197b492f5SLucas De Marchi 	raw_reg_write(regs, GEN11_GFX_MSTR_IRQ, val);
261297b492f5SLucas De Marchi 
261397b492f5SLucas De Marchi 	return val;
261497b492f5SLucas De Marchi }
261597b492f5SLucas De Marchi 
261697b492f5SLucas De Marchi static inline void dg1_master_intr_enable(void __iomem * const regs)
261797b492f5SLucas De Marchi {
261897b492f5SLucas De Marchi 	raw_reg_write(regs, DG1_MSTR_UNIT_INTR, DG1_MSTR_IRQ);
261997b492f5SLucas De Marchi }
262097b492f5SLucas De Marchi 
262197b492f5SLucas De Marchi static irqreturn_t dg1_irq_handler(int irq, void *arg)
262297b492f5SLucas De Marchi {
262397b492f5SLucas De Marchi 	return __gen11_irq_handler(arg,
262497b492f5SLucas De Marchi 				   dg1_master_intr_disable_and_ack,
262597b492f5SLucas De Marchi 				   dg1_master_intr_enable);
262697b492f5SLucas De Marchi }
262797b492f5SLucas De Marchi 
262842f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
262942f52ef8SKeith Packard  * we use as a pipe index
263042f52ef8SKeith Packard  */
263108fa8fd0SVille Syrjälä int i8xx_enable_vblank(struct drm_crtc *crtc)
26320a3e67a4SJesse Barnes {
263308fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
263408fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
2635e9d21d7fSKeith Packard 	unsigned long irqflags;
263671e0ffa5SJesse Barnes 
26371ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
263886e83e35SChris Wilson 	i915_enable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_STATUS);
263986e83e35SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
264086e83e35SChris Wilson 
264186e83e35SChris Wilson 	return 0;
264286e83e35SChris Wilson }
264386e83e35SChris Wilson 
26447d423af9SVille Syrjälä int i915gm_enable_vblank(struct drm_crtc *crtc)
2645d938da6bSVille Syrjälä {
264608fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
2647d938da6bSVille Syrjälä 
26487d423af9SVille Syrjälä 	/*
26497d423af9SVille Syrjälä 	 * Vblank interrupts fail to wake the device up from C2+.
26507d423af9SVille Syrjälä 	 * Disabling render clock gating during C-states avoids
26517d423af9SVille Syrjälä 	 * the problem. There is a small power cost so we do this
26527d423af9SVille Syrjälä 	 * only when vblank interrupts are actually enabled.
26537d423af9SVille Syrjälä 	 */
26547d423af9SVille Syrjälä 	if (dev_priv->vblank_enabled++ == 0)
26557d423af9SVille Syrjälä 		I915_WRITE(SCPD0, _MASKED_BIT_ENABLE(CSTATE_RENDER_CLOCK_GATE_DISABLE));
2656d938da6bSVille Syrjälä 
265708fa8fd0SVille Syrjälä 	return i8xx_enable_vblank(crtc);
2658d938da6bSVille Syrjälä }
2659d938da6bSVille Syrjälä 
266008fa8fd0SVille Syrjälä int i965_enable_vblank(struct drm_crtc *crtc)
266186e83e35SChris Wilson {
266208fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
266308fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
266486e83e35SChris Wilson 	unsigned long irqflags;
266586e83e35SChris Wilson 
266686e83e35SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
26677c463586SKeith Packard 	i915_enable_pipestat(dev_priv, pipe,
2668755e9019SImre Deak 			     PIPE_START_VBLANK_INTERRUPT_STATUS);
26691ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26708692d00eSChris Wilson 
26710a3e67a4SJesse Barnes 	return 0;
26720a3e67a4SJesse Barnes }
26730a3e67a4SJesse Barnes 
267408fa8fd0SVille Syrjälä int ilk_enable_vblank(struct drm_crtc *crtc)
2675f796cf8fSJesse Barnes {
267608fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
267708fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
2678f796cf8fSJesse Barnes 	unsigned long irqflags;
2679a9c287c9SJani Nikula 	u32 bit = INTEL_GEN(dev_priv) >= 7 ?
268086e83e35SChris Wilson 		DE_PIPE_VBLANK_IVB(pipe) : DE_PIPE_VBLANK(pipe);
2681f796cf8fSJesse Barnes 
2682f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2683fbdedaeaSVille Syrjälä 	ilk_enable_display_irq(dev_priv, bit);
2684b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2685b1f14ad0SJesse Barnes 
26862e8bf223SDhinakaran Pandiyan 	/* Even though there is no DMC, frame counter can get stuck when
26872e8bf223SDhinakaran Pandiyan 	 * PSR is active as no frames are generated.
26882e8bf223SDhinakaran Pandiyan 	 */
26892e8bf223SDhinakaran Pandiyan 	if (HAS_PSR(dev_priv))
269008fa8fd0SVille Syrjälä 		drm_crtc_vblank_restore(crtc);
26912e8bf223SDhinakaran Pandiyan 
2692b1f14ad0SJesse Barnes 	return 0;
2693b1f14ad0SJesse Barnes }
2694b1f14ad0SJesse Barnes 
269508fa8fd0SVille Syrjälä int bdw_enable_vblank(struct drm_crtc *crtc)
2696abd58f01SBen Widawsky {
269708fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
269808fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
2699abd58f01SBen Widawsky 	unsigned long irqflags;
2700abd58f01SBen Widawsky 
2701abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2702013d3752SVille Syrjälä 	bdw_enable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
2703abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2704013d3752SVille Syrjälä 
27052e8bf223SDhinakaran Pandiyan 	/* Even if there is no DMC, frame counter can get stuck when
27062e8bf223SDhinakaran Pandiyan 	 * PSR is active as no frames are generated, so check only for PSR.
27072e8bf223SDhinakaran Pandiyan 	 */
27082e8bf223SDhinakaran Pandiyan 	if (HAS_PSR(dev_priv))
270908fa8fd0SVille Syrjälä 		drm_crtc_vblank_restore(crtc);
27102e8bf223SDhinakaran Pandiyan 
2711abd58f01SBen Widawsky 	return 0;
2712abd58f01SBen Widawsky }
2713abd58f01SBen Widawsky 
271442f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
271542f52ef8SKeith Packard  * we use as a pipe index
271642f52ef8SKeith Packard  */
271708fa8fd0SVille Syrjälä void i8xx_disable_vblank(struct drm_crtc *crtc)
271886e83e35SChris Wilson {
271908fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
272008fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
272186e83e35SChris Wilson 	unsigned long irqflags;
272286e83e35SChris Wilson 
272386e83e35SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
272486e83e35SChris Wilson 	i915_disable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_STATUS);
272586e83e35SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
272686e83e35SChris Wilson }
272786e83e35SChris Wilson 
27287d423af9SVille Syrjälä void i915gm_disable_vblank(struct drm_crtc *crtc)
2729d938da6bSVille Syrjälä {
273008fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
2731d938da6bSVille Syrjälä 
273208fa8fd0SVille Syrjälä 	i8xx_disable_vblank(crtc);
2733d938da6bSVille Syrjälä 
27347d423af9SVille Syrjälä 	if (--dev_priv->vblank_enabled == 0)
27357d423af9SVille Syrjälä 		I915_WRITE(SCPD0, _MASKED_BIT_DISABLE(CSTATE_RENDER_CLOCK_GATE_DISABLE));
2736d938da6bSVille Syrjälä }
2737d938da6bSVille Syrjälä 
273808fa8fd0SVille Syrjälä void i965_disable_vblank(struct drm_crtc *crtc)
27390a3e67a4SJesse Barnes {
274008fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
274108fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
2742e9d21d7fSKeith Packard 	unsigned long irqflags;
27430a3e67a4SJesse Barnes 
27441ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
27457c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
2746755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
27471ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
27480a3e67a4SJesse Barnes }
27490a3e67a4SJesse Barnes 
275008fa8fd0SVille Syrjälä void ilk_disable_vblank(struct drm_crtc *crtc)
2751f796cf8fSJesse Barnes {
275208fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
275308fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
2754f796cf8fSJesse Barnes 	unsigned long irqflags;
2755a9c287c9SJani Nikula 	u32 bit = INTEL_GEN(dev_priv) >= 7 ?
275686e83e35SChris Wilson 		DE_PIPE_VBLANK_IVB(pipe) : DE_PIPE_VBLANK(pipe);
2757f796cf8fSJesse Barnes 
2758f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2759fbdedaeaSVille Syrjälä 	ilk_disable_display_irq(dev_priv, bit);
2760b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2761b1f14ad0SJesse Barnes }
2762b1f14ad0SJesse Barnes 
276308fa8fd0SVille Syrjälä void bdw_disable_vblank(struct drm_crtc *crtc)
2764abd58f01SBen Widawsky {
276508fa8fd0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
276608fa8fd0SVille Syrjälä 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
2767abd58f01SBen Widawsky 	unsigned long irqflags;
2768abd58f01SBen Widawsky 
2769abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2770013d3752SVille Syrjälä 	bdw_disable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
2771abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2772abd58f01SBen Widawsky }
2773abd58f01SBen Widawsky 
2774b243f530STvrtko Ursulin static void ibx_irq_reset(struct drm_i915_private *dev_priv)
277591738a95SPaulo Zanoni {
2776b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
2777b16b2a2fSPaulo Zanoni 
27786e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
277991738a95SPaulo Zanoni 		return;
278091738a95SPaulo Zanoni 
2781b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, SDE);
2782105b122eSPaulo Zanoni 
27836e266956STvrtko Ursulin 	if (HAS_PCH_CPT(dev_priv) || HAS_PCH_LPT(dev_priv))
2784105b122eSPaulo Zanoni 		I915_WRITE(SERR_INT, 0xffffffff);
2785622364b6SPaulo Zanoni }
2786105b122eSPaulo Zanoni 
278791738a95SPaulo Zanoni /*
2788622364b6SPaulo Zanoni  * SDEIER is also touched by the interrupt handler to work around missed PCH
2789622364b6SPaulo Zanoni  * interrupts. Hence we can't update it after the interrupt handler is enabled -
2790622364b6SPaulo Zanoni  * instead we unconditionally enable all PCH interrupt sources here, but then
2791622364b6SPaulo Zanoni  * only unmask them as needed with SDEIMR.
2792622364b6SPaulo Zanoni  *
2793622364b6SPaulo Zanoni  * This function needs to be called before interrupts are enabled.
279491738a95SPaulo Zanoni  */
2795b318b824SVille Syrjälä static void ibx_irq_pre_postinstall(struct drm_i915_private *dev_priv)
2796622364b6SPaulo Zanoni {
27976e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
2798622364b6SPaulo Zanoni 		return;
2799622364b6SPaulo Zanoni 
280048a1b8d4SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, I915_READ(SDEIER) != 0);
280191738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
280291738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
280391738a95SPaulo Zanoni }
280491738a95SPaulo Zanoni 
280570591a41SVille Syrjälä static void vlv_display_irq_reset(struct drm_i915_private *dev_priv)
280670591a41SVille Syrjälä {
2807b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
2808b16b2a2fSPaulo Zanoni 
280971b8b41dSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
2810f0818984STvrtko Ursulin 		intel_uncore_write(uncore, DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
281171b8b41dSVille Syrjälä 	else
2812f0818984STvrtko Ursulin 		intel_uncore_write(uncore, DPINVGTT, DPINVGTT_STATUS_MASK);
281371b8b41dSVille Syrjälä 
2814ad22d106SVille Syrjälä 	i915_hotplug_interrupt_update_locked(dev_priv, 0xffffffff, 0);
2815f0818984STvrtko Ursulin 	intel_uncore_write(uncore, PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
281670591a41SVille Syrjälä 
281744d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
281870591a41SVille Syrjälä 
2819b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, VLV_);
28208bd099a7SChris Wilson 	dev_priv->irq_mask = ~0u;
282170591a41SVille Syrjälä }
282270591a41SVille Syrjälä 
28238bb61306SVille Syrjälä static void vlv_display_irq_postinstall(struct drm_i915_private *dev_priv)
28248bb61306SVille Syrjälä {
2825b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
2826b16b2a2fSPaulo Zanoni 
28278bb61306SVille Syrjälä 	u32 pipestat_mask;
28289ab981f2SVille Syrjälä 	u32 enable_mask;
28298bb61306SVille Syrjälä 	enum pipe pipe;
28308bb61306SVille Syrjälä 
2831842ebf7aSVille Syrjälä 	pipestat_mask = PIPE_CRC_DONE_INTERRUPT_STATUS;
28328bb61306SVille Syrjälä 
28338bb61306SVille Syrjälä 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
28348bb61306SVille Syrjälä 	for_each_pipe(dev_priv, pipe)
28358bb61306SVille Syrjälä 		i915_enable_pipestat(dev_priv, pipe, pipestat_mask);
28368bb61306SVille Syrjälä 
28379ab981f2SVille Syrjälä 	enable_mask = I915_DISPLAY_PORT_INTERRUPT |
28388bb61306SVille Syrjälä 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
2839ebf5f921SVille Syrjälä 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
2840ebf5f921SVille Syrjälä 		I915_LPE_PIPE_A_INTERRUPT |
2841ebf5f921SVille Syrjälä 		I915_LPE_PIPE_B_INTERRUPT;
2842ebf5f921SVille Syrjälä 
28438bb61306SVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
2844ebf5f921SVille Syrjälä 		enable_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT |
2845ebf5f921SVille Syrjälä 			I915_LPE_PIPE_C_INTERRUPT;
28466b7eafc1SVille Syrjälä 
284748a1b8d4SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, dev_priv->irq_mask != ~0u);
28486b7eafc1SVille Syrjälä 
28499ab981f2SVille Syrjälä 	dev_priv->irq_mask = ~enable_mask;
28508bb61306SVille Syrjälä 
2851b16b2a2fSPaulo Zanoni 	GEN3_IRQ_INIT(uncore, VLV_, dev_priv->irq_mask, enable_mask);
28528bb61306SVille Syrjälä }
28538bb61306SVille Syrjälä 
28548bb61306SVille Syrjälä /* drm_dma.h hooks
28558bb61306SVille Syrjälä */
28569eae5e27SLucas De Marchi static void ilk_irq_reset(struct drm_i915_private *dev_priv)
28578bb61306SVille Syrjälä {
2858b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
28598bb61306SVille Syrjälä 
2860b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, DE);
2861cf819effSLucas De Marchi 	if (IS_GEN(dev_priv, 7))
2862f0818984STvrtko Ursulin 		intel_uncore_write(uncore, GEN7_ERR_INT, 0xffffffff);
28638bb61306SVille Syrjälä 
2864fc340442SDaniel Vetter 	if (IS_HASWELL(dev_priv)) {
2865f0818984STvrtko Ursulin 		intel_uncore_write(uncore, EDP_PSR_IMR, 0xffffffff);
2866f0818984STvrtko Ursulin 		intel_uncore_write(uncore, EDP_PSR_IIR, 0xffffffff);
2867fc340442SDaniel Vetter 	}
2868fc340442SDaniel Vetter 
2869cf1c97dcSAndi Shyti 	gen5_gt_irq_reset(&dev_priv->gt);
28708bb61306SVille Syrjälä 
2871b243f530STvrtko Ursulin 	ibx_irq_reset(dev_priv);
28728bb61306SVille Syrjälä }
28738bb61306SVille Syrjälä 
2874b318b824SVille Syrjälä static void valleyview_irq_reset(struct drm_i915_private *dev_priv)
28757e231dbeSJesse Barnes {
287634c7b8a7SVille Syrjälä 	I915_WRITE(VLV_MASTER_IER, 0);
287734c7b8a7SVille Syrjälä 	POSTING_READ(VLV_MASTER_IER);
287834c7b8a7SVille Syrjälä 
2879cf1c97dcSAndi Shyti 	gen5_gt_irq_reset(&dev_priv->gt);
28807e231dbeSJesse Barnes 
2881ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
28829918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
288370591a41SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
2884ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
28857e231dbeSJesse Barnes }
28867e231dbeSJesse Barnes 
2887b318b824SVille Syrjälä static void gen8_irq_reset(struct drm_i915_private *dev_priv)
2888abd58f01SBen Widawsky {
2889b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
2890d048a268SVille Syrjälä 	enum pipe pipe;
2891abd58f01SBen Widawsky 
289225286aacSDaniele Ceraolo Spurio 	gen8_master_intr_disable(dev_priv->uncore.regs);
2893abd58f01SBen Widawsky 
2894cf1c97dcSAndi Shyti 	gen8_gt_irq_reset(&dev_priv->gt);
2895abd58f01SBen Widawsky 
2896f0818984STvrtko Ursulin 	intel_uncore_write(uncore, EDP_PSR_IMR, 0xffffffff);
2897f0818984STvrtko Ursulin 	intel_uncore_write(uncore, EDP_PSR_IIR, 0xffffffff);
2898e04f7eceSVille Syrjälä 
2899055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
2900f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
2901813bde43SPaulo Zanoni 						   POWER_DOMAIN_PIPE(pipe)))
2902b16b2a2fSPaulo Zanoni 			GEN8_IRQ_RESET_NDX(uncore, DE_PIPE, pipe);
2903abd58f01SBen Widawsky 
2904b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN8_DE_PORT_);
2905b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN8_DE_MISC_);
2906b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN8_PCU_);
2907abd58f01SBen Widawsky 
29086e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
2909b243f530STvrtko Ursulin 		ibx_irq_reset(dev_priv);
2910abd58f01SBen Widawsky }
2911abd58f01SBen Widawsky 
2912a3265d85SMatt Roper static void gen11_display_irq_reset(struct drm_i915_private *dev_priv)
291351951ae7SMika Kuoppala {
2914b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
2915d048a268SVille Syrjälä 	enum pipe pipe;
2916562ddcb7SMatt Roper 	u32 trans_mask = BIT(TRANSCODER_A) | BIT(TRANSCODER_B) |
2917562ddcb7SMatt Roper 		BIT(TRANSCODER_C) | BIT(TRANSCODER_D);
291851951ae7SMika Kuoppala 
2919f0818984STvrtko Ursulin 	intel_uncore_write(uncore, GEN11_DISPLAY_INT_CTL, 0);
292051951ae7SMika Kuoppala 
29218241cfbeSJosé Roberto de Souza 	if (INTEL_GEN(dev_priv) >= 12) {
29228241cfbeSJosé Roberto de Souza 		enum transcoder trans;
29238241cfbeSJosé Roberto de Souza 
2924562ddcb7SMatt Roper 		for_each_cpu_transcoder_masked(dev_priv, trans, trans_mask) {
29258241cfbeSJosé Roberto de Souza 			enum intel_display_power_domain domain;
29268241cfbeSJosé Roberto de Souza 
29278241cfbeSJosé Roberto de Souza 			domain = POWER_DOMAIN_TRANSCODER(trans);
29288241cfbeSJosé Roberto de Souza 			if (!intel_display_power_is_enabled(dev_priv, domain))
29298241cfbeSJosé Roberto de Souza 				continue;
29308241cfbeSJosé Roberto de Souza 
29318241cfbeSJosé Roberto de Souza 			intel_uncore_write(uncore, TRANS_PSR_IMR(trans), 0xffffffff);
29328241cfbeSJosé Roberto de Souza 			intel_uncore_write(uncore, TRANS_PSR_IIR(trans), 0xffffffff);
29338241cfbeSJosé Roberto de Souza 		}
29348241cfbeSJosé Roberto de Souza 	} else {
2935f0818984STvrtko Ursulin 		intel_uncore_write(uncore, EDP_PSR_IMR, 0xffffffff);
2936f0818984STvrtko Ursulin 		intel_uncore_write(uncore, EDP_PSR_IIR, 0xffffffff);
29378241cfbeSJosé Roberto de Souza 	}
293862819dfdSJosé Roberto de Souza 
293951951ae7SMika Kuoppala 	for_each_pipe(dev_priv, pipe)
294051951ae7SMika Kuoppala 		if (intel_display_power_is_enabled(dev_priv,
294151951ae7SMika Kuoppala 						   POWER_DOMAIN_PIPE(pipe)))
2942b16b2a2fSPaulo Zanoni 			GEN8_IRQ_RESET_NDX(uncore, DE_PIPE, pipe);
294351951ae7SMika Kuoppala 
2944b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN8_DE_PORT_);
2945b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN8_DE_MISC_);
2946b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN11_DE_HPD_);
294731604222SAnusha Srivatsa 
294829b43ae2SRodrigo Vivi 	if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
2949b16b2a2fSPaulo Zanoni 		GEN3_IRQ_RESET(uncore, SDE);
29509b2383a7SMatt Roper 
29511e8110a6SMatt Roper 	/* Wa_14010685332:icl,jsl,ehl,tgl,rkl */
29521e8110a6SMatt Roper 	if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP) {
29539b2383a7SMatt Roper 		intel_uncore_rmw(uncore, SOUTH_CHICKEN1,
29549b2383a7SMatt Roper 				 SBCLK_RUN_REFCLK_DIS, SBCLK_RUN_REFCLK_DIS);
29559b2383a7SMatt Roper 		intel_uncore_rmw(uncore, SOUTH_CHICKEN1,
29569b2383a7SMatt Roper 				 SBCLK_RUN_REFCLK_DIS, 0);
29579b2383a7SMatt Roper 	}
295851951ae7SMika Kuoppala }
295951951ae7SMika Kuoppala 
2960a3265d85SMatt Roper static void gen11_irq_reset(struct drm_i915_private *dev_priv)
2961a3265d85SMatt Roper {
2962a3265d85SMatt Roper 	struct intel_uncore *uncore = &dev_priv->uncore;
2963a3265d85SMatt Roper 
296497b492f5SLucas De Marchi 	if (HAS_MASTER_UNIT_IRQ(dev_priv))
296597b492f5SLucas De Marchi 		dg1_master_intr_disable_and_ack(dev_priv->uncore.regs);
296697b492f5SLucas De Marchi 	else
2967a3265d85SMatt Roper 		gen11_master_intr_disable(dev_priv->uncore.regs);
2968a3265d85SMatt Roper 
2969a3265d85SMatt Roper 	gen11_gt_irq_reset(&dev_priv->gt);
2970a3265d85SMatt Roper 	gen11_display_irq_reset(dev_priv);
2971a3265d85SMatt Roper 
2972a3265d85SMatt Roper 	GEN3_IRQ_RESET(uncore, GEN11_GU_MISC_);
2973a3265d85SMatt Roper 	GEN3_IRQ_RESET(uncore, GEN8_PCU_);
2974a3265d85SMatt Roper }
2975a3265d85SMatt Roper 
29764c6c03beSDamien Lespiau void gen8_irq_power_well_post_enable(struct drm_i915_private *dev_priv,
2977001bd2cbSImre Deak 				     u8 pipe_mask)
2978d49bdb0eSPaulo Zanoni {
2979b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
2980b16b2a2fSPaulo Zanoni 
2981a9c287c9SJani Nikula 	u32 extra_ier = GEN8_PIPE_VBLANK | GEN8_PIPE_FIFO_UNDERRUN;
29826831f3e3SVille Syrjälä 	enum pipe pipe;
2983d49bdb0eSPaulo Zanoni 
298413321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
29859dfe2e3aSImre Deak 
29869dfe2e3aSImre Deak 	if (!intel_irqs_enabled(dev_priv)) {
29879dfe2e3aSImre Deak 		spin_unlock_irq(&dev_priv->irq_lock);
29889dfe2e3aSImre Deak 		return;
29899dfe2e3aSImre Deak 	}
29909dfe2e3aSImre Deak 
29916831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
2992b16b2a2fSPaulo Zanoni 		GEN8_IRQ_INIT_NDX(uncore, DE_PIPE, pipe,
29936831f3e3SVille Syrjälä 				  dev_priv->de_irq_mask[pipe],
29946831f3e3SVille Syrjälä 				  ~dev_priv->de_irq_mask[pipe] | extra_ier);
29959dfe2e3aSImre Deak 
299613321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
2997d49bdb0eSPaulo Zanoni }
2998d49bdb0eSPaulo Zanoni 
2999aae8ba84SVille Syrjälä void gen8_irq_power_well_pre_disable(struct drm_i915_private *dev_priv,
3000001bd2cbSImre Deak 				     u8 pipe_mask)
3001aae8ba84SVille Syrjälä {
3002b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
30036831f3e3SVille Syrjälä 	enum pipe pipe;
30046831f3e3SVille Syrjälä 
3005aae8ba84SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
30069dfe2e3aSImre Deak 
30079dfe2e3aSImre Deak 	if (!intel_irqs_enabled(dev_priv)) {
30089dfe2e3aSImre Deak 		spin_unlock_irq(&dev_priv->irq_lock);
30099dfe2e3aSImre Deak 		return;
30109dfe2e3aSImre Deak 	}
30119dfe2e3aSImre Deak 
30126831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
3013b16b2a2fSPaulo Zanoni 		GEN8_IRQ_RESET_NDX(uncore, DE_PIPE, pipe);
30149dfe2e3aSImre Deak 
3015aae8ba84SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3016aae8ba84SVille Syrjälä 
3017aae8ba84SVille Syrjälä 	/* make sure we're done processing display irqs */
3018315ca4c4SVille Syrjälä 	intel_synchronize_irq(dev_priv);
3019aae8ba84SVille Syrjälä }
3020aae8ba84SVille Syrjälä 
3021b318b824SVille Syrjälä static void cherryview_irq_reset(struct drm_i915_private *dev_priv)
302243f328d7SVille Syrjälä {
3023b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
302443f328d7SVille Syrjälä 
302543f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
302643f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
302743f328d7SVille Syrjälä 
3028cf1c97dcSAndi Shyti 	gen8_gt_irq_reset(&dev_priv->gt);
302943f328d7SVille Syrjälä 
3030b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN8_PCU_);
303143f328d7SVille Syrjälä 
3032ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
30339918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
303470591a41SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3035ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
303643f328d7SVille Syrjälä }
303743f328d7SVille Syrjälä 
303891d14251STvrtko Ursulin static u32 intel_hpd_enabled_irqs(struct drm_i915_private *dev_priv,
303987a02106SVille Syrjälä 				  const u32 hpd[HPD_NUM_PINS])
304087a02106SVille Syrjälä {
304187a02106SVille Syrjälä 	struct intel_encoder *encoder;
304287a02106SVille Syrjälä 	u32 enabled_irqs = 0;
304387a02106SVille Syrjälä 
304491c8a326SChris Wilson 	for_each_intel_encoder(&dev_priv->drm, encoder)
304587a02106SVille Syrjälä 		if (dev_priv->hotplug.stats[encoder->hpd_pin].state == HPD_ENABLED)
304687a02106SVille Syrjälä 			enabled_irqs |= hpd[encoder->hpd_pin];
304787a02106SVille Syrjälä 
304887a02106SVille Syrjälä 	return enabled_irqs;
304987a02106SVille Syrjälä }
305087a02106SVille Syrjälä 
30511a56b1a2SImre Deak static void ibx_hpd_detection_setup(struct drm_i915_private *dev_priv)
30521a56b1a2SImre Deak {
30531a56b1a2SImre Deak 	u32 hotplug;
30541a56b1a2SImre Deak 
30551a56b1a2SImre Deak 	/*
30561a56b1a2SImre Deak 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
30571a56b1a2SImre Deak 	 * duration to 2ms (which is the minimum in the Display Port spec).
30581a56b1a2SImre Deak 	 * The pulse duration bits are reserved on LPT+.
30591a56b1a2SImre Deak 	 */
30601a56b1a2SImre Deak 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
30611a56b1a2SImre Deak 	hotplug &= ~(PORTB_PULSE_DURATION_MASK |
30621a56b1a2SImre Deak 		     PORTC_PULSE_DURATION_MASK |
30631a56b1a2SImre Deak 		     PORTD_PULSE_DURATION_MASK);
30641a56b1a2SImre Deak 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
30651a56b1a2SImre Deak 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
30661a56b1a2SImre Deak 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
30671a56b1a2SImre Deak 	/*
30681a56b1a2SImre Deak 	 * When CPU and PCH are on the same package, port A
30691a56b1a2SImre Deak 	 * HPD must be enabled in both north and south.
30701a56b1a2SImre Deak 	 */
30711a56b1a2SImre Deak 	if (HAS_PCH_LPT_LP(dev_priv))
30721a56b1a2SImre Deak 		hotplug |= PORTA_HOTPLUG_ENABLE;
30731a56b1a2SImre Deak 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
30741a56b1a2SImre Deak }
30751a56b1a2SImre Deak 
307691d14251STvrtko Ursulin static void ibx_hpd_irq_setup(struct drm_i915_private *dev_priv)
307782a28bcfSDaniel Vetter {
30781a56b1a2SImre Deak 	u32 hotplug_irqs, enabled_irqs;
307982a28bcfSDaniel Vetter 
30800398993bSVille Syrjälä 	if (HAS_PCH_IBX(dev_priv))
3081fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
30820398993bSVille Syrjälä 	else
3083fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
30840398993bSVille Syrjälä 
30850398993bSVille Syrjälä 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, dev_priv->hotplug.pch_hpd);
308682a28bcfSDaniel Vetter 
3087fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
308882a28bcfSDaniel Vetter 
30891a56b1a2SImre Deak 	ibx_hpd_detection_setup(dev_priv);
30906dbf30ceSVille Syrjälä }
309126951cafSXiong Zhang 
309252dfdba0SLucas De Marchi static void icp_hpd_detection_setup(struct drm_i915_private *dev_priv,
309352dfdba0SLucas De Marchi 				    u32 ddi_hotplug_enable_mask,
309452dfdba0SLucas De Marchi 				    u32 tc_hotplug_enable_mask)
309531604222SAnusha Srivatsa {
309631604222SAnusha Srivatsa 	u32 hotplug;
309731604222SAnusha Srivatsa 
309831604222SAnusha Srivatsa 	hotplug = I915_READ(SHOTPLUG_CTL_DDI);
309952dfdba0SLucas De Marchi 	hotplug |= ddi_hotplug_enable_mask;
310031604222SAnusha Srivatsa 	I915_WRITE(SHOTPLUG_CTL_DDI, hotplug);
310131604222SAnusha Srivatsa 
31028ef7e340SMatt Roper 	if (tc_hotplug_enable_mask) {
310331604222SAnusha Srivatsa 		hotplug = I915_READ(SHOTPLUG_CTL_TC);
310452dfdba0SLucas De Marchi 		hotplug |= tc_hotplug_enable_mask;
310531604222SAnusha Srivatsa 		I915_WRITE(SHOTPLUG_CTL_TC, hotplug);
310631604222SAnusha Srivatsa 	}
31078ef7e340SMatt Roper }
310831604222SAnusha Srivatsa 
310940e98130SLucas De Marchi static void icp_hpd_irq_setup(struct drm_i915_private *dev_priv,
311040e98130SLucas De Marchi 			      u32 sde_ddi_mask, u32 sde_tc_mask,
31110398993bSVille Syrjälä 			      u32 ddi_enable_mask, u32 tc_enable_mask)
311231604222SAnusha Srivatsa {
311331604222SAnusha Srivatsa 	u32 hotplug_irqs, enabled_irqs;
311431604222SAnusha Srivatsa 
311540e98130SLucas De Marchi 	hotplug_irqs = sde_ddi_mask | sde_tc_mask;
31160398993bSVille Syrjälä 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, dev_priv->hotplug.pch_hpd);
311731604222SAnusha Srivatsa 
3118f619e516SAnusha Srivatsa 	if (INTEL_PCH_TYPE(dev_priv) <= PCH_TGP)
3119f49108d0SMatt Roper 		I915_WRITE(SHPD_FILTER_CNT, SHPD_FILTER_CNT_500_ADJ);
3120f49108d0SMatt Roper 
312131604222SAnusha Srivatsa 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
312231604222SAnusha Srivatsa 
312340e98130SLucas De Marchi 	icp_hpd_detection_setup(dev_priv, ddi_enable_mask, tc_enable_mask);
312452dfdba0SLucas De Marchi }
312552dfdba0SLucas De Marchi 
312640e98130SLucas De Marchi /*
312740e98130SLucas De Marchi  * EHL doesn't need most of gen11_hpd_irq_setup, it's handling only the
312840e98130SLucas De Marchi  * equivalent of SDE.
312940e98130SLucas De Marchi  */
31308ef7e340SMatt Roper static void mcc_hpd_irq_setup(struct drm_i915_private *dev_priv)
31318ef7e340SMatt Roper {
313240e98130SLucas De Marchi 	icp_hpd_irq_setup(dev_priv,
313353448aedSVivek Kasireddy 			  SDE_DDI_MASK_ICP, SDE_TC_HOTPLUG_ICP(PORT_TC1),
31340398993bSVille Syrjälä 			  ICP_DDI_HPD_ENABLE_MASK, ICP_TC_HPD_ENABLE(PORT_TC1));
313531604222SAnusha Srivatsa }
313631604222SAnusha Srivatsa 
3137943682e3SMatt Roper /*
3138943682e3SMatt Roper  * JSP behaves exactly the same as MCC above except that port C is mapped to
3139943682e3SMatt Roper  * the DDI-C pins instead of the TC1 pins.  This means we should follow TGP's
3140943682e3SMatt Roper  * masks & tables rather than ICP's masks & tables.
3141943682e3SMatt Roper  */
3142943682e3SMatt Roper static void jsp_hpd_irq_setup(struct drm_i915_private *dev_priv)
3143943682e3SMatt Roper {
3144943682e3SMatt Roper 	icp_hpd_irq_setup(dev_priv,
3145943682e3SMatt Roper 			  SDE_DDI_MASK_TGP, 0,
31460398993bSVille Syrjälä 			  TGP_DDI_HPD_ENABLE_MASK, 0);
3147943682e3SMatt Roper }
3148943682e3SMatt Roper 
3149121e758eSDhinakaran Pandiyan static void gen11_hpd_detection_setup(struct drm_i915_private *dev_priv)
3150121e758eSDhinakaran Pandiyan {
3151121e758eSDhinakaran Pandiyan 	u32 hotplug;
3152121e758eSDhinakaran Pandiyan 
3153121e758eSDhinakaran Pandiyan 	hotplug = I915_READ(GEN11_TC_HOTPLUG_CTL);
3154121e758eSDhinakaran Pandiyan 	hotplug |= GEN11_HOTPLUG_CTL_ENABLE(PORT_TC1) |
3155121e758eSDhinakaran Pandiyan 		   GEN11_HOTPLUG_CTL_ENABLE(PORT_TC2) |
3156121e758eSDhinakaran Pandiyan 		   GEN11_HOTPLUG_CTL_ENABLE(PORT_TC3) |
3157121e758eSDhinakaran Pandiyan 		   GEN11_HOTPLUG_CTL_ENABLE(PORT_TC4);
3158121e758eSDhinakaran Pandiyan 	I915_WRITE(GEN11_TC_HOTPLUG_CTL, hotplug);
3159b796b971SDhinakaran Pandiyan 
3160b796b971SDhinakaran Pandiyan 	hotplug = I915_READ(GEN11_TBT_HOTPLUG_CTL);
3161b796b971SDhinakaran Pandiyan 	hotplug |= GEN11_HOTPLUG_CTL_ENABLE(PORT_TC1) |
3162b796b971SDhinakaran Pandiyan 		   GEN11_HOTPLUG_CTL_ENABLE(PORT_TC2) |
3163b796b971SDhinakaran Pandiyan 		   GEN11_HOTPLUG_CTL_ENABLE(PORT_TC3) |
3164b796b971SDhinakaran Pandiyan 		   GEN11_HOTPLUG_CTL_ENABLE(PORT_TC4);
3165b796b971SDhinakaran Pandiyan 	I915_WRITE(GEN11_TBT_HOTPLUG_CTL, hotplug);
3166121e758eSDhinakaran Pandiyan }
3167121e758eSDhinakaran Pandiyan 
3168121e758eSDhinakaran Pandiyan static void gen11_hpd_irq_setup(struct drm_i915_private *dev_priv)
3169121e758eSDhinakaran Pandiyan {
3170121e758eSDhinakaran Pandiyan 	u32 hotplug_irqs, enabled_irqs;
3171121e758eSDhinakaran Pandiyan 	u32 val;
3172121e758eSDhinakaran Pandiyan 
31730398993bSVille Syrjälä 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, dev_priv->hotplug.hpd);
3174b796b971SDhinakaran Pandiyan 	hotplug_irqs = GEN11_DE_TC_HOTPLUG_MASK | GEN11_DE_TBT_HOTPLUG_MASK;
3175121e758eSDhinakaran Pandiyan 
3176121e758eSDhinakaran Pandiyan 	val = I915_READ(GEN11_DE_HPD_IMR);
3177121e758eSDhinakaran Pandiyan 	val &= ~hotplug_irqs;
3178587a87b9SImre Deak 	val |= ~enabled_irqs & hotplug_irqs;
3179121e758eSDhinakaran Pandiyan 	I915_WRITE(GEN11_DE_HPD_IMR, val);
3180121e758eSDhinakaran Pandiyan 	POSTING_READ(GEN11_DE_HPD_IMR);
3181121e758eSDhinakaran Pandiyan 
3182121e758eSDhinakaran Pandiyan 	gen11_hpd_detection_setup(dev_priv);
318331604222SAnusha Srivatsa 
318452dfdba0SLucas De Marchi 	if (INTEL_PCH_TYPE(dev_priv) >= PCH_TGP)
318540e98130SLucas De Marchi 		icp_hpd_irq_setup(dev_priv, SDE_DDI_MASK_TGP, SDE_TC_MASK_TGP,
31860398993bSVille Syrjälä 				  TGP_DDI_HPD_ENABLE_MASK, TGP_TC_HPD_ENABLE_MASK);
318752dfdba0SLucas De Marchi 	else if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
318840e98130SLucas De Marchi 		icp_hpd_irq_setup(dev_priv, SDE_DDI_MASK_ICP, SDE_TC_MASK_ICP,
31890398993bSVille Syrjälä 				  ICP_DDI_HPD_ENABLE_MASK, ICP_TC_HPD_ENABLE_MASK);
3190121e758eSDhinakaran Pandiyan }
3191121e758eSDhinakaran Pandiyan 
31922a57d9ccSImre Deak static void spt_hpd_detection_setup(struct drm_i915_private *dev_priv)
31932a57d9ccSImre Deak {
31943b92e263SRodrigo Vivi 	u32 val, hotplug;
31953b92e263SRodrigo Vivi 
31963b92e263SRodrigo Vivi 	/* Display WA #1179 WaHardHangonHotPlug: cnp */
31973b92e263SRodrigo Vivi 	if (HAS_PCH_CNP(dev_priv)) {
31983b92e263SRodrigo Vivi 		val = I915_READ(SOUTH_CHICKEN1);
31993b92e263SRodrigo Vivi 		val &= ~CHASSIS_CLK_REQ_DURATION_MASK;
32003b92e263SRodrigo Vivi 		val |= CHASSIS_CLK_REQ_DURATION(0xf);
32013b92e263SRodrigo Vivi 		I915_WRITE(SOUTH_CHICKEN1, val);
32023b92e263SRodrigo Vivi 	}
32032a57d9ccSImre Deak 
32042a57d9ccSImre Deak 	/* Enable digital hotplug on the PCH */
32052a57d9ccSImre Deak 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
32062a57d9ccSImre Deak 	hotplug |= PORTA_HOTPLUG_ENABLE |
32072a57d9ccSImre Deak 		   PORTB_HOTPLUG_ENABLE |
32082a57d9ccSImre Deak 		   PORTC_HOTPLUG_ENABLE |
32092a57d9ccSImre Deak 		   PORTD_HOTPLUG_ENABLE;
32102a57d9ccSImre Deak 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
32112a57d9ccSImre Deak 
32122a57d9ccSImre Deak 	hotplug = I915_READ(PCH_PORT_HOTPLUG2);
32132a57d9ccSImre Deak 	hotplug |= PORTE_HOTPLUG_ENABLE;
32142a57d9ccSImre Deak 	I915_WRITE(PCH_PORT_HOTPLUG2, hotplug);
32152a57d9ccSImre Deak }
32162a57d9ccSImre Deak 
321791d14251STvrtko Ursulin static void spt_hpd_irq_setup(struct drm_i915_private *dev_priv)
32186dbf30ceSVille Syrjälä {
32192a57d9ccSImre Deak 	u32 hotplug_irqs, enabled_irqs;
32206dbf30ceSVille Syrjälä 
3221f49108d0SMatt Roper 	if (INTEL_PCH_TYPE(dev_priv) >= PCH_CNP)
3222f49108d0SMatt Roper 		I915_WRITE(SHPD_FILTER_CNT, SHPD_FILTER_CNT_500_ADJ);
3223f49108d0SMatt Roper 
32246dbf30ceSVille Syrjälä 	hotplug_irqs = SDE_HOTPLUG_MASK_SPT;
32250398993bSVille Syrjälä 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, dev_priv->hotplug.pch_hpd);
32266dbf30ceSVille Syrjälä 
32276dbf30ceSVille Syrjälä 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
32286dbf30ceSVille Syrjälä 
32292a57d9ccSImre Deak 	spt_hpd_detection_setup(dev_priv);
323026951cafSXiong Zhang }
32317fe0b973SKeith Packard 
32321a56b1a2SImre Deak static void ilk_hpd_detection_setup(struct drm_i915_private *dev_priv)
32331a56b1a2SImre Deak {
32341a56b1a2SImre Deak 	u32 hotplug;
32351a56b1a2SImre Deak 
32361a56b1a2SImre Deak 	/*
32371a56b1a2SImre Deak 	 * Enable digital hotplug on the CPU, and configure the DP short pulse
32381a56b1a2SImre Deak 	 * duration to 2ms (which is the minimum in the Display Port spec)
32391a56b1a2SImre Deak 	 * The pulse duration bits are reserved on HSW+.
32401a56b1a2SImre Deak 	 */
32411a56b1a2SImre Deak 	hotplug = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
32421a56b1a2SImre Deak 	hotplug &= ~DIGITAL_PORTA_PULSE_DURATION_MASK;
32431a56b1a2SImre Deak 	hotplug |= DIGITAL_PORTA_HOTPLUG_ENABLE |
32441a56b1a2SImre Deak 		   DIGITAL_PORTA_PULSE_DURATION_2ms;
32451a56b1a2SImre Deak 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, hotplug);
32461a56b1a2SImre Deak }
32471a56b1a2SImre Deak 
324891d14251STvrtko Ursulin static void ilk_hpd_irq_setup(struct drm_i915_private *dev_priv)
3249e4ce95aaSVille Syrjälä {
32501a56b1a2SImre Deak 	u32 hotplug_irqs, enabled_irqs;
3251e4ce95aaSVille Syrjälä 
325291d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 8) {
32533a3b3c7dSVille Syrjälä 		hotplug_irqs = GEN8_PORT_DP_A_HOTPLUG;
32540398993bSVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, dev_priv->hotplug.hpd);
32553a3b3c7dSVille Syrjälä 
32563a3b3c7dSVille Syrjälä 		bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
325791d14251STvrtko Ursulin 	} else if (INTEL_GEN(dev_priv) >= 7) {
325823bb4cb5SVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG_IVB;
32590398993bSVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, dev_priv->hotplug.hpd);
32603a3b3c7dSVille Syrjälä 
32613a3b3c7dSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
326223bb4cb5SVille Syrjälä 	} else {
3263e4ce95aaSVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG;
32640398993bSVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, dev_priv->hotplug.hpd);
3265e4ce95aaSVille Syrjälä 
3266e4ce95aaSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
32673a3b3c7dSVille Syrjälä 	}
3268e4ce95aaSVille Syrjälä 
32691a56b1a2SImre Deak 	ilk_hpd_detection_setup(dev_priv);
3270e4ce95aaSVille Syrjälä 
327191d14251STvrtko Ursulin 	ibx_hpd_irq_setup(dev_priv);
3272e4ce95aaSVille Syrjälä }
3273e4ce95aaSVille Syrjälä 
32742a57d9ccSImre Deak static void __bxt_hpd_detection_setup(struct drm_i915_private *dev_priv,
32752a57d9ccSImre Deak 				      u32 enabled_irqs)
3276e0a20ad7SShashank Sharma {
32772a57d9ccSImre Deak 	u32 hotplug;
3278e0a20ad7SShashank Sharma 
3279a52bb15bSVille Syrjälä 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
32802a57d9ccSImre Deak 	hotplug |= PORTA_HOTPLUG_ENABLE |
32812a57d9ccSImre Deak 		   PORTB_HOTPLUG_ENABLE |
32822a57d9ccSImre Deak 		   PORTC_HOTPLUG_ENABLE;
3283d252bf68SShubhangi Shrivastava 
328400376ccfSWambui Karuga 	drm_dbg_kms(&dev_priv->drm,
328500376ccfSWambui Karuga 		    "Invert bit setting: hp_ctl:%x hp_port:%x\n",
3286d252bf68SShubhangi Shrivastava 		    hotplug, enabled_irqs);
3287d252bf68SShubhangi Shrivastava 	hotplug &= ~BXT_DDI_HPD_INVERT_MASK;
3288d252bf68SShubhangi Shrivastava 
3289d252bf68SShubhangi Shrivastava 	/*
3290d252bf68SShubhangi Shrivastava 	 * For BXT invert bit has to be set based on AOB design
3291d252bf68SShubhangi Shrivastava 	 * for HPD detection logic, update it based on VBT fields.
3292d252bf68SShubhangi Shrivastava 	 */
3293d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIA) &&
3294d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_A))
3295d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIA_HPD_INVERT;
3296d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIB) &&
3297d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_B))
3298d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIB_HPD_INVERT;
3299d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIC) &&
3300d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_C))
3301d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIC_HPD_INVERT;
3302d252bf68SShubhangi Shrivastava 
3303a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
3304e0a20ad7SShashank Sharma }
3305e0a20ad7SShashank Sharma 
33062a57d9ccSImre Deak static void bxt_hpd_detection_setup(struct drm_i915_private *dev_priv)
33072a57d9ccSImre Deak {
33082a57d9ccSImre Deak 	__bxt_hpd_detection_setup(dev_priv, BXT_DE_PORT_HOTPLUG_MASK);
33092a57d9ccSImre Deak }
33102a57d9ccSImre Deak 
33112a57d9ccSImre Deak static void bxt_hpd_irq_setup(struct drm_i915_private *dev_priv)
33122a57d9ccSImre Deak {
33132a57d9ccSImre Deak 	u32 hotplug_irqs, enabled_irqs;
33142a57d9ccSImre Deak 
33150398993bSVille Syrjälä 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, dev_priv->hotplug.hpd);
33162a57d9ccSImre Deak 	hotplug_irqs = BXT_DE_PORT_HOTPLUG_MASK;
33172a57d9ccSImre Deak 
33182a57d9ccSImre Deak 	bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
33192a57d9ccSImre Deak 
33202a57d9ccSImre Deak 	__bxt_hpd_detection_setup(dev_priv, enabled_irqs);
33212a57d9ccSImre Deak }
33222a57d9ccSImre Deak 
3323b318b824SVille Syrjälä static void ibx_irq_postinstall(struct drm_i915_private *dev_priv)
3324d46da437SPaulo Zanoni {
332582a28bcfSDaniel Vetter 	u32 mask;
3326d46da437SPaulo Zanoni 
33276e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
3328692a04cfSDaniel Vetter 		return;
3329692a04cfSDaniel Vetter 
33306e266956STvrtko Ursulin 	if (HAS_PCH_IBX(dev_priv))
33315c673b60SDaniel Vetter 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
33324ebc6509SDhinakaran Pandiyan 	else if (HAS_PCH_CPT(dev_priv) || HAS_PCH_LPT(dev_priv))
33335c673b60SDaniel Vetter 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
33344ebc6509SDhinakaran Pandiyan 	else
33354ebc6509SDhinakaran Pandiyan 		mask = SDE_GMBUS_CPT;
33368664281bSPaulo Zanoni 
333765f42cdcSPaulo Zanoni 	gen3_assert_iir_is_zero(&dev_priv->uncore, SDEIIR);
3338d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
33392a57d9ccSImre Deak 
33402a57d9ccSImre Deak 	if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
33412a57d9ccSImre Deak 	    HAS_PCH_LPT(dev_priv))
33421a56b1a2SImre Deak 		ibx_hpd_detection_setup(dev_priv);
33432a57d9ccSImre Deak 	else
33442a57d9ccSImre Deak 		spt_hpd_detection_setup(dev_priv);
3345d46da437SPaulo Zanoni }
3346d46da437SPaulo Zanoni 
33479eae5e27SLucas De Marchi static void ilk_irq_postinstall(struct drm_i915_private *dev_priv)
3348036a4a7dSZhenyu Wang {
3349b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
33508e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
33518e76f8dcSPaulo Zanoni 
3352b243f530STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 7) {
33538e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
3354842ebf7aSVille Syrjälä 				DE_PCH_EVENT_IVB | DE_AUX_CHANNEL_A_IVB);
33558e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
335623bb4cb5SVille Syrjälä 			      DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB |
335723bb4cb5SVille Syrjälä 			      DE_DP_A_HOTPLUG_IVB);
33588e76f8dcSPaulo Zanoni 	} else {
33598e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3360842ebf7aSVille Syrjälä 				DE_AUX_CHANNEL_A | DE_PIPEB_CRC_DONE |
3361842ebf7aSVille Syrjälä 				DE_PIPEA_CRC_DONE | DE_POISON);
3362e4ce95aaSVille Syrjälä 		extra_mask = (DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
3363e4ce95aaSVille Syrjälä 			      DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN |
3364e4ce95aaSVille Syrjälä 			      DE_DP_A_HOTPLUG);
33658e76f8dcSPaulo Zanoni 	}
3366036a4a7dSZhenyu Wang 
3367fc340442SDaniel Vetter 	if (IS_HASWELL(dev_priv)) {
3368b16b2a2fSPaulo Zanoni 		gen3_assert_iir_is_zero(uncore, EDP_PSR_IIR);
3369fc340442SDaniel Vetter 		display_mask |= DE_EDP_PSR_INT_HSW;
3370fc340442SDaniel Vetter 	}
3371fc340442SDaniel Vetter 
33721ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3373036a4a7dSZhenyu Wang 
3374b318b824SVille Syrjälä 	ibx_irq_pre_postinstall(dev_priv);
3375622364b6SPaulo Zanoni 
3376b16b2a2fSPaulo Zanoni 	GEN3_IRQ_INIT(uncore, DE, dev_priv->irq_mask,
3377b16b2a2fSPaulo Zanoni 		      display_mask | extra_mask);
3378036a4a7dSZhenyu Wang 
3379cf1c97dcSAndi Shyti 	gen5_gt_irq_postinstall(&dev_priv->gt);
3380036a4a7dSZhenyu Wang 
33811a56b1a2SImre Deak 	ilk_hpd_detection_setup(dev_priv);
33821a56b1a2SImre Deak 
3383b318b824SVille Syrjälä 	ibx_irq_postinstall(dev_priv);
33847fe0b973SKeith Packard 
338550a0bc90STvrtko Ursulin 	if (IS_IRONLAKE_M(dev_priv)) {
33866005ce42SDaniel Vetter 		/* Enable PCU event interrupts
33876005ce42SDaniel Vetter 		 *
33886005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
33894bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
33904bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
3391d6207435SDaniel Vetter 		spin_lock_irq(&dev_priv->irq_lock);
3392fbdedaeaSVille Syrjälä 		ilk_enable_display_irq(dev_priv, DE_PCU_EVENT);
3393d6207435SDaniel Vetter 		spin_unlock_irq(&dev_priv->irq_lock);
3394f97108d1SJesse Barnes 	}
3395036a4a7dSZhenyu Wang }
3396036a4a7dSZhenyu Wang 
3397f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3398f8b79e58SImre Deak {
339967520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
3400f8b79e58SImre Deak 
3401f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3402f8b79e58SImre Deak 		return;
3403f8b79e58SImre Deak 
3404f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3405f8b79e58SImre Deak 
3406d6c69803SVille Syrjälä 	if (intel_irqs_enabled(dev_priv)) {
3407d6c69803SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3408ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3409f8b79e58SImre Deak 	}
3410d6c69803SVille Syrjälä }
3411f8b79e58SImre Deak 
3412f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3413f8b79e58SImre Deak {
341467520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
3415f8b79e58SImre Deak 
3416f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3417f8b79e58SImre Deak 		return;
3418f8b79e58SImre Deak 
3419f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3420f8b79e58SImre Deak 
3421950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3422ad22d106SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3423f8b79e58SImre Deak }
3424f8b79e58SImre Deak 
34250e6c9a9eSVille Syrjälä 
3426b318b824SVille Syrjälä static void valleyview_irq_postinstall(struct drm_i915_private *dev_priv)
34270e6c9a9eSVille Syrjälä {
3428cf1c97dcSAndi Shyti 	gen5_gt_irq_postinstall(&dev_priv->gt);
34297e231dbeSJesse Barnes 
3430ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
34319918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3432ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3433ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3434ad22d106SVille Syrjälä 
34357e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
343634c7b8a7SVille Syrjälä 	POSTING_READ(VLV_MASTER_IER);
343720afbda2SDaniel Vetter }
343820afbda2SDaniel Vetter 
3439abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3440abd58f01SBen Widawsky {
3441b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
3442b16b2a2fSPaulo Zanoni 
3443869129eeSMatt Roper 	u32 de_pipe_masked = gen8_de_pipe_fault_mask(dev_priv) |
3444869129eeSMatt Roper 		GEN8_PIPE_CDCLK_CRC_DONE;
3445a9c287c9SJani Nikula 	u32 de_pipe_enables;
3446054318c7SImre Deak 	u32 de_port_masked = gen8_de_port_aux_mask(dev_priv);
34473a3b3c7dSVille Syrjälä 	u32 de_port_enables;
3448df0d28c1SDhinakaran Pandiyan 	u32 de_misc_masked = GEN8_DE_EDP_PSR;
3449562ddcb7SMatt Roper 	u32 trans_mask = BIT(TRANSCODER_A) | BIT(TRANSCODER_B) |
3450562ddcb7SMatt Roper 		BIT(TRANSCODER_C) | BIT(TRANSCODER_D);
34513a3b3c7dSVille Syrjälä 	enum pipe pipe;
3452770de83dSDamien Lespiau 
3453df0d28c1SDhinakaran Pandiyan 	if (INTEL_GEN(dev_priv) <= 10)
3454df0d28c1SDhinakaran Pandiyan 		de_misc_masked |= GEN8_DE_MISC_GSE;
3455df0d28c1SDhinakaran Pandiyan 
3456cc3f90f0SAnder Conselvan de Oliveira 	if (IS_GEN9_LP(dev_priv))
34573a3b3c7dSVille Syrjälä 		de_port_masked |= BXT_DE_PORT_GMBUS;
3458a324fcacSRodrigo Vivi 
3459770de83dSDamien Lespiau 	de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
3460770de83dSDamien Lespiau 					   GEN8_PIPE_FIFO_UNDERRUN;
3461770de83dSDamien Lespiau 
34623a3b3c7dSVille Syrjälä 	de_port_enables = de_port_masked;
3463cc3f90f0SAnder Conselvan de Oliveira 	if (IS_GEN9_LP(dev_priv))
3464a52bb15bSVille Syrjälä 		de_port_enables |= BXT_DE_PORT_HOTPLUG_MASK;
3465a52bb15bSVille Syrjälä 	else if (IS_BROADWELL(dev_priv))
34663a3b3c7dSVille Syrjälä 		de_port_enables |= GEN8_PORT_DP_A_HOTPLUG;
34673a3b3c7dSVille Syrjälä 
34688241cfbeSJosé Roberto de Souza 	if (INTEL_GEN(dev_priv) >= 12) {
34698241cfbeSJosé Roberto de Souza 		enum transcoder trans;
34708241cfbeSJosé Roberto de Souza 
3471562ddcb7SMatt Roper 		for_each_cpu_transcoder_masked(dev_priv, trans, trans_mask) {
34728241cfbeSJosé Roberto de Souza 			enum intel_display_power_domain domain;
34738241cfbeSJosé Roberto de Souza 
34748241cfbeSJosé Roberto de Souza 			domain = POWER_DOMAIN_TRANSCODER(trans);
34758241cfbeSJosé Roberto de Souza 			if (!intel_display_power_is_enabled(dev_priv, domain))
34768241cfbeSJosé Roberto de Souza 				continue;
34778241cfbeSJosé Roberto de Souza 
34788241cfbeSJosé Roberto de Souza 			gen3_assert_iir_is_zero(uncore, TRANS_PSR_IIR(trans));
34798241cfbeSJosé Roberto de Souza 		}
34808241cfbeSJosé Roberto de Souza 	} else {
3481b16b2a2fSPaulo Zanoni 		gen3_assert_iir_is_zero(uncore, EDP_PSR_IIR);
34828241cfbeSJosé Roberto de Souza 	}
3483e04f7eceSVille Syrjälä 
34840a195c02SMika Kahola 	for_each_pipe(dev_priv, pipe) {
34850a195c02SMika Kahola 		dev_priv->de_irq_mask[pipe] = ~de_pipe_masked;
3486abd58f01SBen Widawsky 
3487f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3488813bde43SPaulo Zanoni 				POWER_DOMAIN_PIPE(pipe)))
3489b16b2a2fSPaulo Zanoni 			GEN8_IRQ_INIT_NDX(uncore, DE_PIPE, pipe,
3490813bde43SPaulo Zanoni 					  dev_priv->de_irq_mask[pipe],
349135079899SPaulo Zanoni 					  de_pipe_enables);
34920a195c02SMika Kahola 	}
3493abd58f01SBen Widawsky 
3494b16b2a2fSPaulo Zanoni 	GEN3_IRQ_INIT(uncore, GEN8_DE_PORT_, ~de_port_masked, de_port_enables);
3495b16b2a2fSPaulo Zanoni 	GEN3_IRQ_INIT(uncore, GEN8_DE_MISC_, ~de_misc_masked, de_misc_masked);
34962a57d9ccSImre Deak 
3497121e758eSDhinakaran Pandiyan 	if (INTEL_GEN(dev_priv) >= 11) {
3498121e758eSDhinakaran Pandiyan 		u32 de_hpd_masked = 0;
3499b796b971SDhinakaran Pandiyan 		u32 de_hpd_enables = GEN11_DE_TC_HOTPLUG_MASK |
3500b796b971SDhinakaran Pandiyan 				     GEN11_DE_TBT_HOTPLUG_MASK;
3501121e758eSDhinakaran Pandiyan 
3502b16b2a2fSPaulo Zanoni 		GEN3_IRQ_INIT(uncore, GEN11_DE_HPD_, ~de_hpd_masked,
3503b16b2a2fSPaulo Zanoni 			      de_hpd_enables);
3504121e758eSDhinakaran Pandiyan 		gen11_hpd_detection_setup(dev_priv);
3505121e758eSDhinakaran Pandiyan 	} else if (IS_GEN9_LP(dev_priv)) {
35062a57d9ccSImre Deak 		bxt_hpd_detection_setup(dev_priv);
3507121e758eSDhinakaran Pandiyan 	} else if (IS_BROADWELL(dev_priv)) {
35081a56b1a2SImre Deak 		ilk_hpd_detection_setup(dev_priv);
3509abd58f01SBen Widawsky 	}
3510121e758eSDhinakaran Pandiyan }
3511abd58f01SBen Widawsky 
3512b318b824SVille Syrjälä static void gen8_irq_postinstall(struct drm_i915_private *dev_priv)
3513abd58f01SBen Widawsky {
35146e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
3515b318b824SVille Syrjälä 		ibx_irq_pre_postinstall(dev_priv);
3516622364b6SPaulo Zanoni 
3517cf1c97dcSAndi Shyti 	gen8_gt_irq_postinstall(&dev_priv->gt);
3518abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3519abd58f01SBen Widawsky 
35206e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
3521b318b824SVille Syrjälä 		ibx_irq_postinstall(dev_priv);
3522abd58f01SBen Widawsky 
352325286aacSDaniele Ceraolo Spurio 	gen8_master_intr_enable(dev_priv->uncore.regs);
3524abd58f01SBen Widawsky }
3525abd58f01SBen Widawsky 
3526b318b824SVille Syrjälä static void icp_irq_postinstall(struct drm_i915_private *dev_priv)
352731604222SAnusha Srivatsa {
352831604222SAnusha Srivatsa 	u32 mask = SDE_GMBUS_ICP;
352931604222SAnusha Srivatsa 
353048a1b8d4SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, I915_READ(SDEIER) != 0);
353131604222SAnusha Srivatsa 	I915_WRITE(SDEIER, 0xffffffff);
353231604222SAnusha Srivatsa 	POSTING_READ(SDEIER);
353331604222SAnusha Srivatsa 
353465f42cdcSPaulo Zanoni 	gen3_assert_iir_is_zero(&dev_priv->uncore, SDEIIR);
353531604222SAnusha Srivatsa 	I915_WRITE(SDEIMR, ~mask);
353631604222SAnusha Srivatsa 
353752dfdba0SLucas De Marchi 	if (HAS_PCH_TGP(dev_priv))
353852dfdba0SLucas De Marchi 		icp_hpd_detection_setup(dev_priv, TGP_DDI_HPD_ENABLE_MASK,
353952dfdba0SLucas De Marchi 					TGP_TC_HPD_ENABLE_MASK);
3540e83c4673SVivek Kasireddy 	else if (HAS_PCH_JSP(dev_priv))
35418ef7e340SMatt Roper 		icp_hpd_detection_setup(dev_priv, TGP_DDI_HPD_ENABLE_MASK, 0);
3542e83c4673SVivek Kasireddy 	else if (HAS_PCH_MCC(dev_priv))
3543e83c4673SVivek Kasireddy 		icp_hpd_detection_setup(dev_priv, ICP_DDI_HPD_ENABLE_MASK,
3544e83c4673SVivek Kasireddy 					ICP_TC_HPD_ENABLE(PORT_TC1));
354552dfdba0SLucas De Marchi 	else
354652dfdba0SLucas De Marchi 		icp_hpd_detection_setup(dev_priv, ICP_DDI_HPD_ENABLE_MASK,
354752dfdba0SLucas De Marchi 					ICP_TC_HPD_ENABLE_MASK);
354831604222SAnusha Srivatsa }
354931604222SAnusha Srivatsa 
3550b318b824SVille Syrjälä static void gen11_irq_postinstall(struct drm_i915_private *dev_priv)
355151951ae7SMika Kuoppala {
3552b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
3553df0d28c1SDhinakaran Pandiyan 	u32 gu_misc_masked = GEN11_GU_MISC_GSE;
355451951ae7SMika Kuoppala 
355529b43ae2SRodrigo Vivi 	if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
3556b318b824SVille Syrjälä 		icp_irq_postinstall(dev_priv);
355731604222SAnusha Srivatsa 
35589b77011eSTvrtko Ursulin 	gen11_gt_irq_postinstall(&dev_priv->gt);
355951951ae7SMika Kuoppala 	gen8_de_irq_postinstall(dev_priv);
356051951ae7SMika Kuoppala 
3561b16b2a2fSPaulo Zanoni 	GEN3_IRQ_INIT(uncore, GEN11_GU_MISC_, ~gu_misc_masked, gu_misc_masked);
3562df0d28c1SDhinakaran Pandiyan 
356351951ae7SMika Kuoppala 	I915_WRITE(GEN11_DISPLAY_INT_CTL, GEN11_DISPLAY_IRQ_ENABLE);
356451951ae7SMika Kuoppala 
356597b492f5SLucas De Marchi 	if (HAS_MASTER_UNIT_IRQ(dev_priv)) {
356697b492f5SLucas De Marchi 		dg1_master_intr_enable(uncore->regs);
356797b492f5SLucas De Marchi 		POSTING_READ(DG1_MSTR_UNIT_INTR);
356897b492f5SLucas De Marchi 	} else {
35699b77011eSTvrtko Ursulin 		gen11_master_intr_enable(uncore->regs);
3570c25f0c6aSDaniele Ceraolo Spurio 		POSTING_READ(GEN11_GFX_MSTR_IRQ);
357151951ae7SMika Kuoppala 	}
357297b492f5SLucas De Marchi }
357351951ae7SMika Kuoppala 
3574b318b824SVille Syrjälä static void cherryview_irq_postinstall(struct drm_i915_private *dev_priv)
357543f328d7SVille Syrjälä {
3576cf1c97dcSAndi Shyti 	gen8_gt_irq_postinstall(&dev_priv->gt);
357743f328d7SVille Syrjälä 
3578ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
35799918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3580ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3581ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3582ad22d106SVille Syrjälä 
3583e5328c43SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
358443f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
358543f328d7SVille Syrjälä }
358643f328d7SVille Syrjälä 
3587b318b824SVille Syrjälä static void i8xx_irq_reset(struct drm_i915_private *dev_priv)
3588c2798b19SChris Wilson {
3589b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
3590c2798b19SChris Wilson 
359144d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
359244d9241eSVille Syrjälä 
3593b16b2a2fSPaulo Zanoni 	GEN2_IRQ_RESET(uncore);
3594c2798b19SChris Wilson }
3595c2798b19SChris Wilson 
3596b318b824SVille Syrjälä static void i8xx_irq_postinstall(struct drm_i915_private *dev_priv)
3597c2798b19SChris Wilson {
3598b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
3599e9e9848aSVille Syrjälä 	u16 enable_mask;
3600c2798b19SChris Wilson 
36014f5fd91fSTvrtko Ursulin 	intel_uncore_write16(uncore,
36024f5fd91fSTvrtko Ursulin 			     EMR,
36034f5fd91fSTvrtko Ursulin 			     ~(I915_ERROR_PAGE_TABLE |
3604045cebd2SVille Syrjälä 			       I915_ERROR_MEMORY_REFRESH));
3605c2798b19SChris Wilson 
3606c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
3607c2798b19SChris Wilson 	dev_priv->irq_mask =
3608c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
360916659bc5SVille Syrjälä 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
361016659bc5SVille Syrjälä 		  I915_MASTER_ERROR_INTERRUPT);
3611c2798b19SChris Wilson 
3612e9e9848aSVille Syrjälä 	enable_mask =
3613c2798b19SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3614c2798b19SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
361516659bc5SVille Syrjälä 		I915_MASTER_ERROR_INTERRUPT |
3616e9e9848aSVille Syrjälä 		I915_USER_INTERRUPT;
3617e9e9848aSVille Syrjälä 
3618b16b2a2fSPaulo Zanoni 	GEN2_IRQ_INIT(uncore, dev_priv->irq_mask, enable_mask);
3619c2798b19SChris Wilson 
3620379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3621379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3622d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3623755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3624755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3625d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3626c2798b19SChris Wilson }
3627c2798b19SChris Wilson 
36284f5fd91fSTvrtko Ursulin static void i8xx_error_irq_ack(struct drm_i915_private *i915,
362978c357ddSVille Syrjälä 			       u16 *eir, u16 *eir_stuck)
363078c357ddSVille Syrjälä {
36314f5fd91fSTvrtko Ursulin 	struct intel_uncore *uncore = &i915->uncore;
363278c357ddSVille Syrjälä 	u16 emr;
363378c357ddSVille Syrjälä 
36344f5fd91fSTvrtko Ursulin 	*eir = intel_uncore_read16(uncore, EIR);
363578c357ddSVille Syrjälä 
363678c357ddSVille Syrjälä 	if (*eir)
36374f5fd91fSTvrtko Ursulin 		intel_uncore_write16(uncore, EIR, *eir);
363878c357ddSVille Syrjälä 
36394f5fd91fSTvrtko Ursulin 	*eir_stuck = intel_uncore_read16(uncore, EIR);
364078c357ddSVille Syrjälä 	if (*eir_stuck == 0)
364178c357ddSVille Syrjälä 		return;
364278c357ddSVille Syrjälä 
364378c357ddSVille Syrjälä 	/*
364478c357ddSVille Syrjälä 	 * Toggle all EMR bits to make sure we get an edge
364578c357ddSVille Syrjälä 	 * in the ISR master error bit if we don't clear
364678c357ddSVille Syrjälä 	 * all the EIR bits. Otherwise the edge triggered
364778c357ddSVille Syrjälä 	 * IIR on i965/g4x wouldn't notice that an interrupt
364878c357ddSVille Syrjälä 	 * is still pending. Also some EIR bits can't be
364978c357ddSVille Syrjälä 	 * cleared except by handling the underlying error
365078c357ddSVille Syrjälä 	 * (or by a GPU reset) so we mask any bit that
365178c357ddSVille Syrjälä 	 * remains set.
365278c357ddSVille Syrjälä 	 */
36534f5fd91fSTvrtko Ursulin 	emr = intel_uncore_read16(uncore, EMR);
36544f5fd91fSTvrtko Ursulin 	intel_uncore_write16(uncore, EMR, 0xffff);
36554f5fd91fSTvrtko Ursulin 	intel_uncore_write16(uncore, EMR, emr | *eir_stuck);
365678c357ddSVille Syrjälä }
365778c357ddSVille Syrjälä 
365878c357ddSVille Syrjälä static void i8xx_error_irq_handler(struct drm_i915_private *dev_priv,
365978c357ddSVille Syrjälä 				   u16 eir, u16 eir_stuck)
366078c357ddSVille Syrjälä {
366178c357ddSVille Syrjälä 	DRM_DEBUG("Master Error: EIR 0x%04x\n", eir);
366278c357ddSVille Syrjälä 
366378c357ddSVille Syrjälä 	if (eir_stuck)
366400376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm, "EIR stuck: 0x%04x, masked\n",
366500376ccfSWambui Karuga 			eir_stuck);
366678c357ddSVille Syrjälä }
366778c357ddSVille Syrjälä 
366878c357ddSVille Syrjälä static void i9xx_error_irq_ack(struct drm_i915_private *dev_priv,
366978c357ddSVille Syrjälä 			       u32 *eir, u32 *eir_stuck)
367078c357ddSVille Syrjälä {
367178c357ddSVille Syrjälä 	u32 emr;
367278c357ddSVille Syrjälä 
367378c357ddSVille Syrjälä 	*eir = I915_READ(EIR);
367478c357ddSVille Syrjälä 
367578c357ddSVille Syrjälä 	I915_WRITE(EIR, *eir);
367678c357ddSVille Syrjälä 
367778c357ddSVille Syrjälä 	*eir_stuck = I915_READ(EIR);
367878c357ddSVille Syrjälä 	if (*eir_stuck == 0)
367978c357ddSVille Syrjälä 		return;
368078c357ddSVille Syrjälä 
368178c357ddSVille Syrjälä 	/*
368278c357ddSVille Syrjälä 	 * Toggle all EMR bits to make sure we get an edge
368378c357ddSVille Syrjälä 	 * in the ISR master error bit if we don't clear
368478c357ddSVille Syrjälä 	 * all the EIR bits. Otherwise the edge triggered
368578c357ddSVille Syrjälä 	 * IIR on i965/g4x wouldn't notice that an interrupt
368678c357ddSVille Syrjälä 	 * is still pending. Also some EIR bits can't be
368778c357ddSVille Syrjälä 	 * cleared except by handling the underlying error
368878c357ddSVille Syrjälä 	 * (or by a GPU reset) so we mask any bit that
368978c357ddSVille Syrjälä 	 * remains set.
369078c357ddSVille Syrjälä 	 */
369178c357ddSVille Syrjälä 	emr = I915_READ(EMR);
369278c357ddSVille Syrjälä 	I915_WRITE(EMR, 0xffffffff);
369378c357ddSVille Syrjälä 	I915_WRITE(EMR, emr | *eir_stuck);
369478c357ddSVille Syrjälä }
369578c357ddSVille Syrjälä 
369678c357ddSVille Syrjälä static void i9xx_error_irq_handler(struct drm_i915_private *dev_priv,
369778c357ddSVille Syrjälä 				   u32 eir, u32 eir_stuck)
369878c357ddSVille Syrjälä {
369978c357ddSVille Syrjälä 	DRM_DEBUG("Master Error, EIR 0x%08x\n", eir);
370078c357ddSVille Syrjälä 
370178c357ddSVille Syrjälä 	if (eir_stuck)
370200376ccfSWambui Karuga 		drm_dbg(&dev_priv->drm, "EIR stuck: 0x%08x, masked\n",
370300376ccfSWambui Karuga 			eir_stuck);
370478c357ddSVille Syrjälä }
370578c357ddSVille Syrjälä 
3706ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3707c2798b19SChris Wilson {
3708b318b824SVille Syrjälä 	struct drm_i915_private *dev_priv = arg;
3709af722d28SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
3710c2798b19SChris Wilson 
37112dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
37122dd2a883SImre Deak 		return IRQ_NONE;
37132dd2a883SImre Deak 
37141f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
37159102650fSDaniele Ceraolo Spurio 	disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
37161f814dacSImre Deak 
3717af722d28SVille Syrjälä 	do {
3718af722d28SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
371978c357ddSVille Syrjälä 		u16 eir = 0, eir_stuck = 0;
3720af722d28SVille Syrjälä 		u16 iir;
3721af722d28SVille Syrjälä 
37224f5fd91fSTvrtko Ursulin 		iir = intel_uncore_read16(&dev_priv->uncore, GEN2_IIR);
3723c2798b19SChris Wilson 		if (iir == 0)
3724af722d28SVille Syrjälä 			break;
3725c2798b19SChris Wilson 
3726af722d28SVille Syrjälä 		ret = IRQ_HANDLED;
3727c2798b19SChris Wilson 
3728eb64343cSVille Syrjälä 		/* Call regardless, as some status bits might not be
3729eb64343cSVille Syrjälä 		 * signalled in iir */
3730eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
3731c2798b19SChris Wilson 
373278c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
373378c357ddSVille Syrjälä 			i8xx_error_irq_ack(dev_priv, &eir, &eir_stuck);
373478c357ddSVille Syrjälä 
37354f5fd91fSTvrtko Ursulin 		intel_uncore_write16(&dev_priv->uncore, GEN2_IIR, iir);
3736c2798b19SChris Wilson 
3737c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
373873c8bfb7SChris Wilson 			intel_engine_signal_breadcrumbs(dev_priv->gt.engine[RCS0]);
3739c2798b19SChris Wilson 
374078c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
374178c357ddSVille Syrjälä 			i8xx_error_irq_handler(dev_priv, eir, eir_stuck);
3742af722d28SVille Syrjälä 
3743eb64343cSVille Syrjälä 		i8xx_pipestat_irq_handler(dev_priv, iir, pipe_stats);
3744af722d28SVille Syrjälä 	} while (0);
3745c2798b19SChris Wilson 
37469102650fSDaniele Ceraolo Spurio 	enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
37471f814dacSImre Deak 
37481f814dacSImre Deak 	return ret;
3749c2798b19SChris Wilson }
3750c2798b19SChris Wilson 
3751b318b824SVille Syrjälä static void i915_irq_reset(struct drm_i915_private *dev_priv)
3752a266c7d5SChris Wilson {
3753b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
3754a266c7d5SChris Wilson 
375556b857a5STvrtko Ursulin 	if (I915_HAS_HOTPLUG(dev_priv)) {
37560706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
3757a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3758a266c7d5SChris Wilson 	}
3759a266c7d5SChris Wilson 
376044d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
376144d9241eSVille Syrjälä 
3762b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN2_);
3763a266c7d5SChris Wilson }
3764a266c7d5SChris Wilson 
3765b318b824SVille Syrjälä static void i915_irq_postinstall(struct drm_i915_private *dev_priv)
3766a266c7d5SChris Wilson {
3767b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
376838bde180SChris Wilson 	u32 enable_mask;
3769a266c7d5SChris Wilson 
3770045cebd2SVille Syrjälä 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE |
3771045cebd2SVille Syrjälä 			  I915_ERROR_MEMORY_REFRESH));
377238bde180SChris Wilson 
377338bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
377438bde180SChris Wilson 	dev_priv->irq_mask =
377538bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
377638bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
377716659bc5SVille Syrjälä 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
377816659bc5SVille Syrjälä 		  I915_MASTER_ERROR_INTERRUPT);
377938bde180SChris Wilson 
378038bde180SChris Wilson 	enable_mask =
378138bde180SChris Wilson 		I915_ASLE_INTERRUPT |
378238bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
378338bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
378416659bc5SVille Syrjälä 		I915_MASTER_ERROR_INTERRUPT |
378538bde180SChris Wilson 		I915_USER_INTERRUPT;
378638bde180SChris Wilson 
378756b857a5STvrtko Ursulin 	if (I915_HAS_HOTPLUG(dev_priv)) {
3788a266c7d5SChris Wilson 		/* Enable in IER... */
3789a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
3790a266c7d5SChris Wilson 		/* and unmask in IMR */
3791a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
3792a266c7d5SChris Wilson 	}
3793a266c7d5SChris Wilson 
3794b16b2a2fSPaulo Zanoni 	GEN3_IRQ_INIT(uncore, GEN2_, dev_priv->irq_mask, enable_mask);
3795a266c7d5SChris Wilson 
3796379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3797379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3798d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3799755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3800755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3801d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3802379ef82dSDaniel Vetter 
3803c30bb1fdSVille Syrjälä 	i915_enable_asle_pipestat(dev_priv);
380420afbda2SDaniel Vetter }
380520afbda2SDaniel Vetter 
3806ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
3807a266c7d5SChris Wilson {
3808b318b824SVille Syrjälä 	struct drm_i915_private *dev_priv = arg;
3809af722d28SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
3810a266c7d5SChris Wilson 
38112dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
38122dd2a883SImre Deak 		return IRQ_NONE;
38132dd2a883SImre Deak 
38141f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
38159102650fSDaniele Ceraolo Spurio 	disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
38161f814dacSImre Deak 
381738bde180SChris Wilson 	do {
3818eb64343cSVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
381978c357ddSVille Syrjälä 		u32 eir = 0, eir_stuck = 0;
3820af722d28SVille Syrjälä 		u32 hotplug_status = 0;
3821af722d28SVille Syrjälä 		u32 iir;
3822a266c7d5SChris Wilson 
38239d9523d8SPaulo Zanoni 		iir = I915_READ(GEN2_IIR);
3824af722d28SVille Syrjälä 		if (iir == 0)
3825af722d28SVille Syrjälä 			break;
3826af722d28SVille Syrjälä 
3827af722d28SVille Syrjälä 		ret = IRQ_HANDLED;
3828af722d28SVille Syrjälä 
3829af722d28SVille Syrjälä 		if (I915_HAS_HOTPLUG(dev_priv) &&
3830af722d28SVille Syrjälä 		    iir & I915_DISPLAY_PORT_INTERRUPT)
3831af722d28SVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
3832a266c7d5SChris Wilson 
3833eb64343cSVille Syrjälä 		/* Call regardless, as some status bits might not be
3834eb64343cSVille Syrjälä 		 * signalled in iir */
3835eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
3836a266c7d5SChris Wilson 
383778c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
383878c357ddSVille Syrjälä 			i9xx_error_irq_ack(dev_priv, &eir, &eir_stuck);
383978c357ddSVille Syrjälä 
38409d9523d8SPaulo Zanoni 		I915_WRITE(GEN2_IIR, iir);
3841a266c7d5SChris Wilson 
3842a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
384373c8bfb7SChris Wilson 			intel_engine_signal_breadcrumbs(dev_priv->gt.engine[RCS0]);
3844a266c7d5SChris Wilson 
384578c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
384678c357ddSVille Syrjälä 			i9xx_error_irq_handler(dev_priv, eir, eir_stuck);
3847a266c7d5SChris Wilson 
3848af722d28SVille Syrjälä 		if (hotplug_status)
3849af722d28SVille Syrjälä 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
3850af722d28SVille Syrjälä 
3851af722d28SVille Syrjälä 		i915_pipestat_irq_handler(dev_priv, iir, pipe_stats);
3852af722d28SVille Syrjälä 	} while (0);
3853a266c7d5SChris Wilson 
38549102650fSDaniele Ceraolo Spurio 	enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
38551f814dacSImre Deak 
3856a266c7d5SChris Wilson 	return ret;
3857a266c7d5SChris Wilson }
3858a266c7d5SChris Wilson 
3859b318b824SVille Syrjälä static void i965_irq_reset(struct drm_i915_private *dev_priv)
3860a266c7d5SChris Wilson {
3861b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
3862a266c7d5SChris Wilson 
38630706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
3864a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3865a266c7d5SChris Wilson 
386644d9241eSVille Syrjälä 	i9xx_pipestat_irq_reset(dev_priv);
386744d9241eSVille Syrjälä 
3868b16b2a2fSPaulo Zanoni 	GEN3_IRQ_RESET(uncore, GEN2_);
3869a266c7d5SChris Wilson }
3870a266c7d5SChris Wilson 
3871b318b824SVille Syrjälä static void i965_irq_postinstall(struct drm_i915_private *dev_priv)
3872a266c7d5SChris Wilson {
3873b16b2a2fSPaulo Zanoni 	struct intel_uncore *uncore = &dev_priv->uncore;
3874bbba0a97SChris Wilson 	u32 enable_mask;
3875a266c7d5SChris Wilson 	u32 error_mask;
3876a266c7d5SChris Wilson 
3877045cebd2SVille Syrjälä 	/*
3878045cebd2SVille Syrjälä 	 * Enable some error detection, note the instruction error mask
3879045cebd2SVille Syrjälä 	 * bit is reserved, so we leave it masked.
3880045cebd2SVille Syrjälä 	 */
3881045cebd2SVille Syrjälä 	if (IS_G4X(dev_priv)) {
3882045cebd2SVille Syrjälä 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
3883045cebd2SVille Syrjälä 			       GM45_ERROR_MEM_PRIV |
3884045cebd2SVille Syrjälä 			       GM45_ERROR_CP_PRIV |
3885045cebd2SVille Syrjälä 			       I915_ERROR_MEMORY_REFRESH);
3886045cebd2SVille Syrjälä 	} else {
3887045cebd2SVille Syrjälä 		error_mask = ~(I915_ERROR_PAGE_TABLE |
3888045cebd2SVille Syrjälä 			       I915_ERROR_MEMORY_REFRESH);
3889045cebd2SVille Syrjälä 	}
3890045cebd2SVille Syrjälä 	I915_WRITE(EMR, error_mask);
3891045cebd2SVille Syrjälä 
3892a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
3893c30bb1fdSVille Syrjälä 	dev_priv->irq_mask =
3894c30bb1fdSVille Syrjälä 		~(I915_ASLE_INTERRUPT |
3895adca4730SChris Wilson 		  I915_DISPLAY_PORT_INTERRUPT |
3896bbba0a97SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3897bbba0a97SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
389878c357ddSVille Syrjälä 		  I915_MASTER_ERROR_INTERRUPT);
3899bbba0a97SChris Wilson 
3900c30bb1fdSVille Syrjälä 	enable_mask =
3901c30bb1fdSVille Syrjälä 		I915_ASLE_INTERRUPT |
3902c30bb1fdSVille Syrjälä 		I915_DISPLAY_PORT_INTERRUPT |
3903c30bb1fdSVille Syrjälä 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3904c30bb1fdSVille Syrjälä 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
390578c357ddSVille Syrjälä 		I915_MASTER_ERROR_INTERRUPT |
3906c30bb1fdSVille Syrjälä 		I915_USER_INTERRUPT;
3907bbba0a97SChris Wilson 
390891d14251STvrtko Ursulin 	if (IS_G4X(dev_priv))
3909bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
3910a266c7d5SChris Wilson 
3911b16b2a2fSPaulo Zanoni 	GEN3_IRQ_INIT(uncore, GEN2_, dev_priv->irq_mask, enable_mask);
3912c30bb1fdSVille Syrjälä 
3913b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3914b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3915d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3916755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3917755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3918755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3919d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3920a266c7d5SChris Wilson 
392191d14251STvrtko Ursulin 	i915_enable_asle_pipestat(dev_priv);
392220afbda2SDaniel Vetter }
392320afbda2SDaniel Vetter 
392491d14251STvrtko Ursulin static void i915_hpd_irq_setup(struct drm_i915_private *dev_priv)
392520afbda2SDaniel Vetter {
392620afbda2SDaniel Vetter 	u32 hotplug_en;
392720afbda2SDaniel Vetter 
392867520415SChris Wilson 	lockdep_assert_held(&dev_priv->irq_lock);
3929b5ea2d56SDaniel Vetter 
3930adca4730SChris Wilson 	/* Note HDMI and DP share hotplug bits */
3931e5868a31SEgbert Eich 	/* enable bits are the same for all generations */
393291d14251STvrtko Ursulin 	hotplug_en = intel_hpd_enabled_irqs(dev_priv, hpd_mask_i915);
3933a266c7d5SChris Wilson 	/* Programming the CRT detection parameters tends
3934a266c7d5SChris Wilson 	   to generate a spurious hotplug event about three
3935a266c7d5SChris Wilson 	   seconds later.  So just do it once.
3936a266c7d5SChris Wilson 	*/
393791d14251STvrtko Ursulin 	if (IS_G4X(dev_priv))
3938a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
3939a266c7d5SChris Wilson 	hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
3940a266c7d5SChris Wilson 
3941a266c7d5SChris Wilson 	/* Ignore TV since it's buggy */
39420706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv,
3943f9e3dc78SJani Nikula 					     HOTPLUG_INT_EN_MASK |
3944f9e3dc78SJani Nikula 					     CRT_HOTPLUG_VOLTAGE_COMPARE_MASK |
3945f9e3dc78SJani Nikula 					     CRT_HOTPLUG_ACTIVATION_PERIOD_64,
39460706f17cSEgbert Eich 					     hotplug_en);
3947a266c7d5SChris Wilson }
3948a266c7d5SChris Wilson 
3949ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
3950a266c7d5SChris Wilson {
3951b318b824SVille Syrjälä 	struct drm_i915_private *dev_priv = arg;
3952af722d28SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
3953a266c7d5SChris Wilson 
39542dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
39552dd2a883SImre Deak 		return IRQ_NONE;
39562dd2a883SImre Deak 
39571f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
39589102650fSDaniele Ceraolo Spurio 	disable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
39591f814dacSImre Deak 
3960af722d28SVille Syrjälä 	do {
3961eb64343cSVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
396278c357ddSVille Syrjälä 		u32 eir = 0, eir_stuck = 0;
3963af722d28SVille Syrjälä 		u32 hotplug_status = 0;
3964af722d28SVille Syrjälä 		u32 iir;
39652c8ba29fSChris Wilson 
39669d9523d8SPaulo Zanoni 		iir = I915_READ(GEN2_IIR);
3967af722d28SVille Syrjälä 		if (iir == 0)
3968af722d28SVille Syrjälä 			break;
3969af722d28SVille Syrjälä 
3970af722d28SVille Syrjälä 		ret = IRQ_HANDLED;
3971af722d28SVille Syrjälä 
3972af722d28SVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
3973af722d28SVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
3974a266c7d5SChris Wilson 
3975eb64343cSVille Syrjälä 		/* Call regardless, as some status bits might not be
3976eb64343cSVille Syrjälä 		 * signalled in iir */
3977eb64343cSVille Syrjälä 		i9xx_pipestat_irq_ack(dev_priv, iir, pipe_stats);
3978a266c7d5SChris Wilson 
397978c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
398078c357ddSVille Syrjälä 			i9xx_error_irq_ack(dev_priv, &eir, &eir_stuck);
398178c357ddSVille Syrjälä 
39829d9523d8SPaulo Zanoni 		I915_WRITE(GEN2_IIR, iir);
3983a266c7d5SChris Wilson 
3984a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
398573c8bfb7SChris Wilson 			intel_engine_signal_breadcrumbs(dev_priv->gt.engine[RCS0]);
3986af722d28SVille Syrjälä 
3987a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
398873c8bfb7SChris Wilson 			intel_engine_signal_breadcrumbs(dev_priv->gt.engine[VCS0]);
3989a266c7d5SChris Wilson 
399078c357ddSVille Syrjälä 		if (iir & I915_MASTER_ERROR_INTERRUPT)
399178c357ddSVille Syrjälä 			i9xx_error_irq_handler(dev_priv, eir, eir_stuck);
3992515ac2bbSDaniel Vetter 
3993af722d28SVille Syrjälä 		if (hotplug_status)
3994af722d28SVille Syrjälä 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
3995af722d28SVille Syrjälä 
3996af722d28SVille Syrjälä 		i965_pipestat_irq_handler(dev_priv, iir, pipe_stats);
3997af722d28SVille Syrjälä 	} while (0);
3998a266c7d5SChris Wilson 
39999102650fSDaniele Ceraolo Spurio 	enable_rpm_wakeref_asserts(&dev_priv->runtime_pm);
40001f814dacSImre Deak 
4001a266c7d5SChris Wilson 	return ret;
4002a266c7d5SChris Wilson }
4003a266c7d5SChris Wilson 
4004fca52a55SDaniel Vetter /**
4005fca52a55SDaniel Vetter  * intel_irq_init - initializes irq support
4006fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4007fca52a55SDaniel Vetter  *
4008fca52a55SDaniel Vetter  * This function initializes all the irq support including work items, timers
4009fca52a55SDaniel Vetter  * and all the vtables. It does not setup the interrupt itself though.
4010fca52a55SDaniel Vetter  */
4011b963291cSDaniel Vetter void intel_irq_init(struct drm_i915_private *dev_priv)
4012f71d4af4SJesse Barnes {
401391c8a326SChris Wilson 	struct drm_device *dev = &dev_priv->drm;
4014cefcff8fSJoonas Lahtinen 	int i;
40158b2e326dSChris Wilson 
40160398993bSVille Syrjälä 	intel_hpd_init_pins(dev_priv);
40170398993bSVille Syrjälä 
401877913b39SJani Nikula 	intel_hpd_init_work(dev_priv);
401977913b39SJani Nikula 
402074bb98baSLucas De Marchi 	INIT_WORK(&dev_priv->l3_parity.error_work, ivb_parity_work);
4021cefcff8fSJoonas Lahtinen 	for (i = 0; i < MAX_L3_SLICES; ++i)
4022cefcff8fSJoonas Lahtinen 		dev_priv->l3_parity.remap_info[i] = NULL;
40238b2e326dSChris Wilson 
4024633023a4SDaniele Ceraolo Spurio 	/* pre-gen11 the guc irqs bits are in the upper 16 bits of the pm reg */
4025702668e6SDaniele Ceraolo Spurio 	if (HAS_GT_UC(dev_priv) && INTEL_GEN(dev_priv) < 11)
40262239e6dfSDaniele Ceraolo Spurio 		dev_priv->gt.pm_guc_events = GUC_INTR_GUC2HOST << 16;
402726705e20SSagar Arun Kamble 
402821da2700SVille Syrjälä 	dev->vblank_disable_immediate = true;
402921da2700SVille Syrjälä 
4030262fd485SChris Wilson 	/* Most platforms treat the display irq block as an always-on
4031262fd485SChris Wilson 	 * power domain. vlv/chv can disable it at runtime and need
4032262fd485SChris Wilson 	 * special care to avoid writing any of the display block registers
4033262fd485SChris Wilson 	 * outside of the power domain. We defer setting up the display irqs
4034262fd485SChris Wilson 	 * in this case to the runtime pm.
4035262fd485SChris Wilson 	 */
4036262fd485SChris Wilson 	dev_priv->display_irqs_enabled = true;
4037262fd485SChris Wilson 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4038262fd485SChris Wilson 		dev_priv->display_irqs_enabled = false;
4039262fd485SChris Wilson 
4040317eaa95SLyude 	dev_priv->hotplug.hpd_storm_threshold = HPD_STORM_DEFAULT_THRESHOLD;
40419a64c650SLyude Paul 	/* If we have MST support, we want to avoid doing short HPD IRQ storm
40429a64c650SLyude Paul 	 * detection, as short HPD storms will occur as a natural part of
40439a64c650SLyude Paul 	 * sideband messaging with MST.
40449a64c650SLyude Paul 	 * On older platforms however, IRQ storms can occur with both long and
40459a64c650SLyude Paul 	 * short pulses, as seen on some G4x systems.
40469a64c650SLyude Paul 	 */
40479a64c650SLyude Paul 	dev_priv->hotplug.hpd_short_storm_enabled = !HAS_DP_MST(dev_priv);
4048317eaa95SLyude 
4049b318b824SVille Syrjälä 	if (HAS_GMCH(dev_priv)) {
4050b318b824SVille Syrjälä 		if (I915_HAS_HOTPLUG(dev_priv))
405143f328d7SVille Syrjälä 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4052b318b824SVille Syrjälä 	} else {
4053943682e3SMatt Roper 		if (HAS_PCH_JSP(dev_priv))
4054943682e3SMatt Roper 			dev_priv->display.hpd_irq_setup = jsp_hpd_irq_setup;
4055943682e3SMatt Roper 		else if (HAS_PCH_MCC(dev_priv))
40568ef7e340SMatt Roper 			dev_priv->display.hpd_irq_setup = mcc_hpd_irq_setup;
40578ef7e340SMatt Roper 		else if (INTEL_GEN(dev_priv) >= 11)
4058121e758eSDhinakaran Pandiyan 			dev_priv->display.hpd_irq_setup = gen11_hpd_irq_setup;
4059b318b824SVille Syrjälä 		else if (IS_GEN9_LP(dev_priv))
4060e0a20ad7SShashank Sharma 			dev_priv->display.hpd_irq_setup = bxt_hpd_irq_setup;
4061c6c30b91SRodrigo Vivi 		else if (INTEL_PCH_TYPE(dev_priv) >= PCH_SPT)
40626dbf30ceSVille Syrjälä 			dev_priv->display.hpd_irq_setup = spt_hpd_irq_setup;
40636dbf30ceSVille Syrjälä 		else
40643a3b3c7dSVille Syrjälä 			dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
4065f71d4af4SJesse Barnes 	}
4066f71d4af4SJesse Barnes }
406720afbda2SDaniel Vetter 
4068fca52a55SDaniel Vetter /**
4069cefcff8fSJoonas Lahtinen  * intel_irq_fini - deinitializes IRQ support
4070cefcff8fSJoonas Lahtinen  * @i915: i915 device instance
4071cefcff8fSJoonas Lahtinen  *
4072cefcff8fSJoonas Lahtinen  * This function deinitializes all the IRQ support.
4073cefcff8fSJoonas Lahtinen  */
4074cefcff8fSJoonas Lahtinen void intel_irq_fini(struct drm_i915_private *i915)
4075cefcff8fSJoonas Lahtinen {
4076cefcff8fSJoonas Lahtinen 	int i;
4077cefcff8fSJoonas Lahtinen 
4078cefcff8fSJoonas Lahtinen 	for (i = 0; i < MAX_L3_SLICES; ++i)
4079cefcff8fSJoonas Lahtinen 		kfree(i915->l3_parity.remap_info[i]);
4080cefcff8fSJoonas Lahtinen }
4081cefcff8fSJoonas Lahtinen 
4082b318b824SVille Syrjälä static irq_handler_t intel_irq_handler(struct drm_i915_private *dev_priv)
4083b318b824SVille Syrjälä {
4084b318b824SVille Syrjälä 	if (HAS_GMCH(dev_priv)) {
4085b318b824SVille Syrjälä 		if (IS_CHERRYVIEW(dev_priv))
4086b318b824SVille Syrjälä 			return cherryview_irq_handler;
4087b318b824SVille Syrjälä 		else if (IS_VALLEYVIEW(dev_priv))
4088b318b824SVille Syrjälä 			return valleyview_irq_handler;
4089b318b824SVille Syrjälä 		else if (IS_GEN(dev_priv, 4))
4090b318b824SVille Syrjälä 			return i965_irq_handler;
4091b318b824SVille Syrjälä 		else if (IS_GEN(dev_priv, 3))
4092b318b824SVille Syrjälä 			return i915_irq_handler;
4093b318b824SVille Syrjälä 		else
4094b318b824SVille Syrjälä 			return i8xx_irq_handler;
4095b318b824SVille Syrjälä 	} else {
409697b492f5SLucas De Marchi 		if (HAS_MASTER_UNIT_IRQ(dev_priv))
409797b492f5SLucas De Marchi 			return dg1_irq_handler;
4098b318b824SVille Syrjälä 		if (INTEL_GEN(dev_priv) >= 11)
4099b318b824SVille Syrjälä 			return gen11_irq_handler;
4100b318b824SVille Syrjälä 		else if (INTEL_GEN(dev_priv) >= 8)
4101b318b824SVille Syrjälä 			return gen8_irq_handler;
4102b318b824SVille Syrjälä 		else
41039eae5e27SLucas De Marchi 			return ilk_irq_handler;
4104b318b824SVille Syrjälä 	}
4105b318b824SVille Syrjälä }
4106b318b824SVille Syrjälä 
4107b318b824SVille Syrjälä static void intel_irq_reset(struct drm_i915_private *dev_priv)
4108b318b824SVille Syrjälä {
4109b318b824SVille Syrjälä 	if (HAS_GMCH(dev_priv)) {
4110b318b824SVille Syrjälä 		if (IS_CHERRYVIEW(dev_priv))
4111b318b824SVille Syrjälä 			cherryview_irq_reset(dev_priv);
4112b318b824SVille Syrjälä 		else if (IS_VALLEYVIEW(dev_priv))
4113b318b824SVille Syrjälä 			valleyview_irq_reset(dev_priv);
4114b318b824SVille Syrjälä 		else if (IS_GEN(dev_priv, 4))
4115b318b824SVille Syrjälä 			i965_irq_reset(dev_priv);
4116b318b824SVille Syrjälä 		else if (IS_GEN(dev_priv, 3))
4117b318b824SVille Syrjälä 			i915_irq_reset(dev_priv);
4118b318b824SVille Syrjälä 		else
4119b318b824SVille Syrjälä 			i8xx_irq_reset(dev_priv);
4120b318b824SVille Syrjälä 	} else {
4121b318b824SVille Syrjälä 		if (INTEL_GEN(dev_priv) >= 11)
4122b318b824SVille Syrjälä 			gen11_irq_reset(dev_priv);
4123b318b824SVille Syrjälä 		else if (INTEL_GEN(dev_priv) >= 8)
4124b318b824SVille Syrjälä 			gen8_irq_reset(dev_priv);
4125b318b824SVille Syrjälä 		else
41269eae5e27SLucas De Marchi 			ilk_irq_reset(dev_priv);
4127b318b824SVille Syrjälä 	}
4128b318b824SVille Syrjälä }
4129b318b824SVille Syrjälä 
4130b318b824SVille Syrjälä static void intel_irq_postinstall(struct drm_i915_private *dev_priv)
4131b318b824SVille Syrjälä {
4132b318b824SVille Syrjälä 	if (HAS_GMCH(dev_priv)) {
4133b318b824SVille Syrjälä 		if (IS_CHERRYVIEW(dev_priv))
4134b318b824SVille Syrjälä 			cherryview_irq_postinstall(dev_priv);
4135b318b824SVille Syrjälä 		else if (IS_VALLEYVIEW(dev_priv))
4136b318b824SVille Syrjälä 			valleyview_irq_postinstall(dev_priv);
4137b318b824SVille Syrjälä 		else if (IS_GEN(dev_priv, 4))
4138b318b824SVille Syrjälä 			i965_irq_postinstall(dev_priv);
4139b318b824SVille Syrjälä 		else if (IS_GEN(dev_priv, 3))
4140b318b824SVille Syrjälä 			i915_irq_postinstall(dev_priv);
4141b318b824SVille Syrjälä 		else
4142b318b824SVille Syrjälä 			i8xx_irq_postinstall(dev_priv);
4143b318b824SVille Syrjälä 	} else {
4144b318b824SVille Syrjälä 		if (INTEL_GEN(dev_priv) >= 11)
4145b318b824SVille Syrjälä 			gen11_irq_postinstall(dev_priv);
4146b318b824SVille Syrjälä 		else if (INTEL_GEN(dev_priv) >= 8)
4147b318b824SVille Syrjälä 			gen8_irq_postinstall(dev_priv);
4148b318b824SVille Syrjälä 		else
41499eae5e27SLucas De Marchi 			ilk_irq_postinstall(dev_priv);
4150b318b824SVille Syrjälä 	}
4151b318b824SVille Syrjälä }
4152b318b824SVille Syrjälä 
4153cefcff8fSJoonas Lahtinen /**
4154fca52a55SDaniel Vetter  * intel_irq_install - enables the hardware interrupt
4155fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4156fca52a55SDaniel Vetter  *
4157fca52a55SDaniel Vetter  * This function enables the hardware interrupt handling, but leaves the hotplug
4158fca52a55SDaniel Vetter  * handling still disabled. It is called after intel_irq_init().
4159fca52a55SDaniel Vetter  *
4160fca52a55SDaniel Vetter  * In the driver load and resume code we need working interrupts in a few places
4161fca52a55SDaniel Vetter  * but don't want to deal with the hassle of concurrent probe and hotplug
4162fca52a55SDaniel Vetter  * workers. Hence the split into this two-stage approach.
4163fca52a55SDaniel Vetter  */
41642aeb7d3aSDaniel Vetter int intel_irq_install(struct drm_i915_private *dev_priv)
41652aeb7d3aSDaniel Vetter {
4166b318b824SVille Syrjälä 	int irq = dev_priv->drm.pdev->irq;
4167b318b824SVille Syrjälä 	int ret;
4168b318b824SVille Syrjälä 
41692aeb7d3aSDaniel Vetter 	/*
41702aeb7d3aSDaniel Vetter 	 * We enable some interrupt sources in our postinstall hooks, so mark
41712aeb7d3aSDaniel Vetter 	 * interrupts as enabled _before_ actually enabling them to avoid
41722aeb7d3aSDaniel Vetter 	 * special cases in our ordering checks.
41732aeb7d3aSDaniel Vetter 	 */
4174ad1443f0SSagar Arun Kamble 	dev_priv->runtime_pm.irqs_enabled = true;
41752aeb7d3aSDaniel Vetter 
4176b318b824SVille Syrjälä 	dev_priv->drm.irq_enabled = true;
4177b318b824SVille Syrjälä 
4178b318b824SVille Syrjälä 	intel_irq_reset(dev_priv);
4179b318b824SVille Syrjälä 
4180b318b824SVille Syrjälä 	ret = request_irq(irq, intel_irq_handler(dev_priv),
4181b318b824SVille Syrjälä 			  IRQF_SHARED, DRIVER_NAME, dev_priv);
4182b318b824SVille Syrjälä 	if (ret < 0) {
4183b318b824SVille Syrjälä 		dev_priv->drm.irq_enabled = false;
4184b318b824SVille Syrjälä 		return ret;
4185b318b824SVille Syrjälä 	}
4186b318b824SVille Syrjälä 
4187b318b824SVille Syrjälä 	intel_irq_postinstall(dev_priv);
4188b318b824SVille Syrjälä 
4189b318b824SVille Syrjälä 	return ret;
41902aeb7d3aSDaniel Vetter }
41912aeb7d3aSDaniel Vetter 
4192fca52a55SDaniel Vetter /**
4193fca52a55SDaniel Vetter  * intel_irq_uninstall - finilizes all irq handling
4194fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4195fca52a55SDaniel Vetter  *
4196fca52a55SDaniel Vetter  * This stops interrupt and hotplug handling and unregisters and frees all
4197fca52a55SDaniel Vetter  * resources acquired in the init functions.
4198fca52a55SDaniel Vetter  */
41992aeb7d3aSDaniel Vetter void intel_irq_uninstall(struct drm_i915_private *dev_priv)
42002aeb7d3aSDaniel Vetter {
4201b318b824SVille Syrjälä 	int irq = dev_priv->drm.pdev->irq;
4202b318b824SVille Syrjälä 
4203b318b824SVille Syrjälä 	/*
4204789fa874SJanusz Krzysztofik 	 * FIXME we can get called twice during driver probe
4205789fa874SJanusz Krzysztofik 	 * error handling as well as during driver remove due to
4206789fa874SJanusz Krzysztofik 	 * intel_modeset_driver_remove() calling us out of sequence.
4207789fa874SJanusz Krzysztofik 	 * Would be nice if it didn't do that...
4208b318b824SVille Syrjälä 	 */
4209b318b824SVille Syrjälä 	if (!dev_priv->drm.irq_enabled)
4210b318b824SVille Syrjälä 		return;
4211b318b824SVille Syrjälä 
4212b318b824SVille Syrjälä 	dev_priv->drm.irq_enabled = false;
4213b318b824SVille Syrjälä 
4214b318b824SVille Syrjälä 	intel_irq_reset(dev_priv);
4215b318b824SVille Syrjälä 
4216b318b824SVille Syrjälä 	free_irq(irq, dev_priv);
4217b318b824SVille Syrjälä 
42182aeb7d3aSDaniel Vetter 	intel_hpd_cancel_work(dev_priv);
4219ad1443f0SSagar Arun Kamble 	dev_priv->runtime_pm.irqs_enabled = false;
42202aeb7d3aSDaniel Vetter }
42212aeb7d3aSDaniel Vetter 
4222fca52a55SDaniel Vetter /**
4223fca52a55SDaniel Vetter  * intel_runtime_pm_disable_interrupts - runtime interrupt disabling
4224fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4225fca52a55SDaniel Vetter  *
4226fca52a55SDaniel Vetter  * This function is used to disable interrupts at runtime, both in the runtime
4227fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4228fca52a55SDaniel Vetter  */
4229b963291cSDaniel Vetter void intel_runtime_pm_disable_interrupts(struct drm_i915_private *dev_priv)
4230c67a470bSPaulo Zanoni {
4231b318b824SVille Syrjälä 	intel_irq_reset(dev_priv);
4232ad1443f0SSagar Arun Kamble 	dev_priv->runtime_pm.irqs_enabled = false;
4233315ca4c4SVille Syrjälä 	intel_synchronize_irq(dev_priv);
4234c67a470bSPaulo Zanoni }
4235c67a470bSPaulo Zanoni 
4236fca52a55SDaniel Vetter /**
4237fca52a55SDaniel Vetter  * intel_runtime_pm_enable_interrupts - runtime interrupt enabling
4238fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4239fca52a55SDaniel Vetter  *
4240fca52a55SDaniel Vetter  * This function is used to enable interrupts at runtime, both in the runtime
4241fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4242fca52a55SDaniel Vetter  */
4243b963291cSDaniel Vetter void intel_runtime_pm_enable_interrupts(struct drm_i915_private *dev_priv)
4244c67a470bSPaulo Zanoni {
4245ad1443f0SSagar Arun Kamble 	dev_priv->runtime_pm.irqs_enabled = true;
4246b318b824SVille Syrjälä 	intel_irq_reset(dev_priv);
4247b318b824SVille Syrjälä 	intel_irq_postinstall(dev_priv);
4248c67a470bSPaulo Zanoni }
4249d64575eeSJani Nikula 
4250d64575eeSJani Nikula bool intel_irqs_enabled(struct drm_i915_private *dev_priv)
4251d64575eeSJani Nikula {
4252d64575eeSJani Nikula 	/*
4253d64575eeSJani Nikula 	 * We only use drm_irq_uninstall() at unload and VT switch, so
4254d64575eeSJani Nikula 	 * this is the only thing we need to check.
4255d64575eeSJani Nikula 	 */
4256d64575eeSJani Nikula 	return dev_priv->runtime_pm.irqs_enabled;
4257d64575eeSJani Nikula }
4258d64575eeSJani Nikula 
4259d64575eeSJani Nikula void intel_synchronize_irq(struct drm_i915_private *i915)
4260d64575eeSJani Nikula {
4261d64575eeSJani Nikula 	synchronize_irq(i915->drm.pdev->irq);
4262d64575eeSJani Nikula }
4263