xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 17136d548e1b5ecd947da996f125907a671313d9)
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 
3163eeaf38SJesse Barnes #include <linux/sysrq.h>
325a0e3ad6STejun Heo #include <linux/slab.h>
33b2c88f5bSDamien Lespiau #include <linux/circ_buf.h>
34760285e7SDavid Howells #include <drm/drmP.h>
35760285e7SDavid Howells #include <drm/i915_drm.h>
36c0e09200SDave Airlie #include "i915_drv.h"
371c5d22f7SChris Wilson #include "i915_trace.h"
3879e53945SJesse Barnes #include "intel_drv.h"
39c0e09200SDave Airlie 
40fca52a55SDaniel Vetter /**
41fca52a55SDaniel Vetter  * DOC: interrupt handling
42fca52a55SDaniel Vetter  *
43fca52a55SDaniel Vetter  * These functions provide the basic support for enabling and disabling the
44fca52a55SDaniel Vetter  * interrupt handling support. There's a lot more functionality in i915_irq.c
45fca52a55SDaniel Vetter  * and related files, but that will be described in separate chapters.
46fca52a55SDaniel Vetter  */
47fca52a55SDaniel Vetter 
48e4ce95aaSVille Syrjälä static const u32 hpd_ilk[HPD_NUM_PINS] = {
49e4ce95aaSVille Syrjälä 	[HPD_PORT_A] = DE_DP_A_HOTPLUG,
50e4ce95aaSVille Syrjälä };
51e4ce95aaSVille Syrjälä 
5223bb4cb5SVille Syrjälä static const u32 hpd_ivb[HPD_NUM_PINS] = {
5323bb4cb5SVille Syrjälä 	[HPD_PORT_A] = DE_DP_A_HOTPLUG_IVB,
5423bb4cb5SVille Syrjälä };
5523bb4cb5SVille Syrjälä 
563a3b3c7dSVille Syrjälä static const u32 hpd_bdw[HPD_NUM_PINS] = {
573a3b3c7dSVille Syrjälä 	[HPD_PORT_A] = GEN8_PORT_DP_A_HOTPLUG,
583a3b3c7dSVille Syrjälä };
593a3b3c7dSVille Syrjälä 
607c7e10dbSVille Syrjälä static const u32 hpd_ibx[HPD_NUM_PINS] = {
61e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG,
62e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG,
63e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG,
64e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG,
65e5868a31SEgbert Eich 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG
66e5868a31SEgbert Eich };
67e5868a31SEgbert Eich 
687c7e10dbSVille Syrjälä static const u32 hpd_cpt[HPD_NUM_PINS] = {
69e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG_CPT,
7073c352a2SDaniel Vetter 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG_CPT,
71e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
72e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
73e5868a31SEgbert Eich 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT
74e5868a31SEgbert Eich };
75e5868a31SEgbert Eich 
7626951cafSXiong Zhang static const u32 hpd_spt[HPD_NUM_PINS] = {
7774c0b395SVille Syrjälä 	[HPD_PORT_A] = SDE_PORTA_HOTPLUG_SPT,
7826951cafSXiong Zhang 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
7926951cafSXiong Zhang 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
8026951cafSXiong Zhang 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT,
8126951cafSXiong Zhang 	[HPD_PORT_E] = SDE_PORTE_HOTPLUG_SPT
8226951cafSXiong Zhang };
8326951cafSXiong Zhang 
847c7e10dbSVille Syrjälä static const u32 hpd_mask_i915[HPD_NUM_PINS] = {
85e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_EN,
86e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_EN,
87e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_EN,
88e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_EN,
89e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_EN,
90e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_EN
91e5868a31SEgbert Eich };
92e5868a31SEgbert Eich 
937c7e10dbSVille Syrjälä static const u32 hpd_status_g4x[HPD_NUM_PINS] = {
94e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
95e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_G4X,
96e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_G4X,
97e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
98e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
99e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
100e5868a31SEgbert Eich };
101e5868a31SEgbert Eich 
1024bca26d0SVille Syrjälä static const u32 hpd_status_i915[HPD_NUM_PINS] = {
103e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
104e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_I915,
105e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_I915,
106e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
107e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
108e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
109e5868a31SEgbert Eich };
110e5868a31SEgbert Eich 
111e0a20ad7SShashank Sharma /* BXT hpd list */
112e0a20ad7SShashank Sharma static const u32 hpd_bxt[HPD_NUM_PINS] = {
1137f3561beSSonika Jindal 	[HPD_PORT_A] = BXT_DE_PORT_HP_DDIA,
114e0a20ad7SShashank Sharma 	[HPD_PORT_B] = BXT_DE_PORT_HP_DDIB,
115e0a20ad7SShashank Sharma 	[HPD_PORT_C] = BXT_DE_PORT_HP_DDIC
116e0a20ad7SShashank Sharma };
117e0a20ad7SShashank Sharma 
1185c502442SPaulo Zanoni /* IIR can theoretically queue up two events. Be paranoid. */
119f86f3fb0SPaulo Zanoni #define GEN8_IRQ_RESET_NDX(type, which) do { \
1205c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IMR(which), 0xffffffff); \
1215c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IMR(which)); \
1225c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IER(which), 0); \
1235c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
1245c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IIR(which)); \
1255c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
1265c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IIR(which)); \
1275c502442SPaulo Zanoni } while (0)
1285c502442SPaulo Zanoni 
129f86f3fb0SPaulo Zanoni #define GEN5_IRQ_RESET(type) do { \
130a9d356a6SPaulo Zanoni 	I915_WRITE(type##IMR, 0xffffffff); \
1315c502442SPaulo Zanoni 	POSTING_READ(type##IMR); \
132a9d356a6SPaulo Zanoni 	I915_WRITE(type##IER, 0); \
1335c502442SPaulo Zanoni 	I915_WRITE(type##IIR, 0xffffffff); \
1345c502442SPaulo Zanoni 	POSTING_READ(type##IIR); \
1355c502442SPaulo Zanoni 	I915_WRITE(type##IIR, 0xffffffff); \
1365c502442SPaulo Zanoni 	POSTING_READ(type##IIR); \
137a9d356a6SPaulo Zanoni } while (0)
138a9d356a6SPaulo Zanoni 
139337ba017SPaulo Zanoni /*
140337ba017SPaulo Zanoni  * We should clear IMR at preinstall/uninstall, and just check at postinstall.
141337ba017SPaulo Zanoni  */
142f0f59a00SVille Syrjälä static void gen5_assert_iir_is_zero(struct drm_i915_private *dev_priv,
143f0f59a00SVille Syrjälä 				    i915_reg_t reg)
144b51a2842SVille Syrjälä {
145b51a2842SVille Syrjälä 	u32 val = I915_READ(reg);
146b51a2842SVille Syrjälä 
147b51a2842SVille Syrjälä 	if (val == 0)
148b51a2842SVille Syrjälä 		return;
149b51a2842SVille Syrjälä 
150b51a2842SVille Syrjälä 	WARN(1, "Interrupt register 0x%x is not zero: 0x%08x\n",
151f0f59a00SVille Syrjälä 	     i915_mmio_reg_offset(reg), val);
152b51a2842SVille Syrjälä 	I915_WRITE(reg, 0xffffffff);
153b51a2842SVille Syrjälä 	POSTING_READ(reg);
154b51a2842SVille Syrjälä 	I915_WRITE(reg, 0xffffffff);
155b51a2842SVille Syrjälä 	POSTING_READ(reg);
156b51a2842SVille Syrjälä }
157337ba017SPaulo Zanoni 
15835079899SPaulo Zanoni #define GEN8_IRQ_INIT_NDX(type, which, imr_val, ier_val) do { \
159b51a2842SVille Syrjälä 	gen5_assert_iir_is_zero(dev_priv, GEN8_##type##_IIR(which)); \
16035079899SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IER(which), (ier_val)); \
1617d1bd539SVille Syrjälä 	I915_WRITE(GEN8_##type##_IMR(which), (imr_val)); \
1627d1bd539SVille Syrjälä 	POSTING_READ(GEN8_##type##_IMR(which)); \
16335079899SPaulo Zanoni } while (0)
16435079899SPaulo Zanoni 
16535079899SPaulo Zanoni #define GEN5_IRQ_INIT(type, imr_val, ier_val) do { \
166b51a2842SVille Syrjälä 	gen5_assert_iir_is_zero(dev_priv, type##IIR); \
16735079899SPaulo Zanoni 	I915_WRITE(type##IER, (ier_val)); \
1687d1bd539SVille Syrjälä 	I915_WRITE(type##IMR, (imr_val)); \
1697d1bd539SVille Syrjälä 	POSTING_READ(type##IMR); \
17035079899SPaulo Zanoni } while (0)
17135079899SPaulo Zanoni 
172c9a9a268SImre Deak static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir);
17326705e20SSagar Arun Kamble static void gen9_guc_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir);
174c9a9a268SImre Deak 
1750706f17cSEgbert Eich /* For display hotplug interrupt */
1760706f17cSEgbert Eich static inline void
1770706f17cSEgbert Eich i915_hotplug_interrupt_update_locked(struct drm_i915_private *dev_priv,
1780706f17cSEgbert Eich 				     uint32_t mask,
1790706f17cSEgbert Eich 				     uint32_t bits)
1800706f17cSEgbert Eich {
1810706f17cSEgbert Eich 	uint32_t val;
1820706f17cSEgbert Eich 
1830706f17cSEgbert Eich 	assert_spin_locked(&dev_priv->irq_lock);
1840706f17cSEgbert Eich 	WARN_ON(bits & ~mask);
1850706f17cSEgbert Eich 
1860706f17cSEgbert Eich 	val = I915_READ(PORT_HOTPLUG_EN);
1870706f17cSEgbert Eich 	val &= ~mask;
1880706f17cSEgbert Eich 	val |= bits;
1890706f17cSEgbert Eich 	I915_WRITE(PORT_HOTPLUG_EN, val);
1900706f17cSEgbert Eich }
1910706f17cSEgbert Eich 
1920706f17cSEgbert Eich /**
1930706f17cSEgbert Eich  * i915_hotplug_interrupt_update - update hotplug interrupt enable
1940706f17cSEgbert Eich  * @dev_priv: driver private
1950706f17cSEgbert Eich  * @mask: bits to update
1960706f17cSEgbert Eich  * @bits: bits to enable
1970706f17cSEgbert Eich  * NOTE: the HPD enable bits are modified both inside and outside
1980706f17cSEgbert Eich  * of an interrupt context. To avoid that read-modify-write cycles
1990706f17cSEgbert Eich  * interfer, these bits are protected by a spinlock. Since this
2000706f17cSEgbert Eich  * function is usually not called from a context where the lock is
2010706f17cSEgbert Eich  * held already, this function acquires the lock itself. A non-locking
2020706f17cSEgbert Eich  * version is also available.
2030706f17cSEgbert Eich  */
2040706f17cSEgbert Eich void i915_hotplug_interrupt_update(struct drm_i915_private *dev_priv,
2050706f17cSEgbert Eich 				   uint32_t mask,
2060706f17cSEgbert Eich 				   uint32_t bits)
2070706f17cSEgbert Eich {
2080706f17cSEgbert Eich 	spin_lock_irq(&dev_priv->irq_lock);
2090706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv, mask, bits);
2100706f17cSEgbert Eich 	spin_unlock_irq(&dev_priv->irq_lock);
2110706f17cSEgbert Eich }
2120706f17cSEgbert Eich 
213d9dc34f1SVille Syrjälä /**
214d9dc34f1SVille Syrjälä  * ilk_update_display_irq - update DEIMR
215d9dc34f1SVille Syrjälä  * @dev_priv: driver private
216d9dc34f1SVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
217d9dc34f1SVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
218d9dc34f1SVille Syrjälä  */
219fbdedaeaSVille Syrjälä void ilk_update_display_irq(struct drm_i915_private *dev_priv,
220d9dc34f1SVille Syrjälä 			    uint32_t interrupt_mask,
221d9dc34f1SVille Syrjälä 			    uint32_t enabled_irq_mask)
222036a4a7dSZhenyu Wang {
223d9dc34f1SVille Syrjälä 	uint32_t new_val;
224d9dc34f1SVille Syrjälä 
2254bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
2264bc9d430SDaniel Vetter 
227d9dc34f1SVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
228d9dc34f1SVille Syrjälä 
2299df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
230c67a470bSPaulo Zanoni 		return;
231c67a470bSPaulo Zanoni 
232d9dc34f1SVille Syrjälä 	new_val = dev_priv->irq_mask;
233d9dc34f1SVille Syrjälä 	new_val &= ~interrupt_mask;
234d9dc34f1SVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
235d9dc34f1SVille Syrjälä 
236d9dc34f1SVille Syrjälä 	if (new_val != dev_priv->irq_mask) {
237d9dc34f1SVille Syrjälä 		dev_priv->irq_mask = new_val;
2381ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
2393143a2bfSChris Wilson 		POSTING_READ(DEIMR);
240036a4a7dSZhenyu Wang 	}
241036a4a7dSZhenyu Wang }
242036a4a7dSZhenyu Wang 
24343eaea13SPaulo Zanoni /**
24443eaea13SPaulo Zanoni  * ilk_update_gt_irq - update GTIMR
24543eaea13SPaulo Zanoni  * @dev_priv: driver private
24643eaea13SPaulo Zanoni  * @interrupt_mask: mask of interrupt bits to update
24743eaea13SPaulo Zanoni  * @enabled_irq_mask: mask of interrupt bits to enable
24843eaea13SPaulo Zanoni  */
24943eaea13SPaulo Zanoni static void ilk_update_gt_irq(struct drm_i915_private *dev_priv,
25043eaea13SPaulo Zanoni 			      uint32_t interrupt_mask,
25143eaea13SPaulo Zanoni 			      uint32_t enabled_irq_mask)
25243eaea13SPaulo Zanoni {
25343eaea13SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
25443eaea13SPaulo Zanoni 
25515a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
25615a17aaeSDaniel Vetter 
2579df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
258c67a470bSPaulo Zanoni 		return;
259c67a470bSPaulo Zanoni 
26043eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask &= ~interrupt_mask;
26143eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask |= (~enabled_irq_mask & interrupt_mask);
26243eaea13SPaulo Zanoni 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
26343eaea13SPaulo Zanoni }
26443eaea13SPaulo Zanoni 
265480c8033SDaniel Vetter void gen5_enable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
26643eaea13SPaulo Zanoni {
26743eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, mask);
26831bb59ccSChris Wilson 	POSTING_READ_FW(GTIMR);
26943eaea13SPaulo Zanoni }
27043eaea13SPaulo Zanoni 
271480c8033SDaniel Vetter void gen5_disable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
27243eaea13SPaulo Zanoni {
27343eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, 0);
27443eaea13SPaulo Zanoni }
27543eaea13SPaulo Zanoni 
276f0f59a00SVille Syrjälä static i915_reg_t gen6_pm_iir(struct drm_i915_private *dev_priv)
277b900b949SImre Deak {
278b900b949SImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IIR(2) : GEN6_PMIIR;
279b900b949SImre Deak }
280b900b949SImre Deak 
281f0f59a00SVille Syrjälä static i915_reg_t gen6_pm_imr(struct drm_i915_private *dev_priv)
282a72fbc3aSImre Deak {
283a72fbc3aSImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IMR(2) : GEN6_PMIMR;
284a72fbc3aSImre Deak }
285a72fbc3aSImre Deak 
286f0f59a00SVille Syrjälä static i915_reg_t gen6_pm_ier(struct drm_i915_private *dev_priv)
287b900b949SImre Deak {
288b900b949SImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IER(2) : GEN6_PMIER;
289b900b949SImre Deak }
290b900b949SImre Deak 
291edbfdb45SPaulo Zanoni /**
292edbfdb45SPaulo Zanoni  * snb_update_pm_irq - update GEN6_PMIMR
293edbfdb45SPaulo Zanoni  * @dev_priv: driver private
294edbfdb45SPaulo Zanoni  * @interrupt_mask: mask of interrupt bits to update
295edbfdb45SPaulo Zanoni  * @enabled_irq_mask: mask of interrupt bits to enable
296edbfdb45SPaulo Zanoni  */
297edbfdb45SPaulo Zanoni static void snb_update_pm_irq(struct drm_i915_private *dev_priv,
298edbfdb45SPaulo Zanoni 			      uint32_t interrupt_mask,
299edbfdb45SPaulo Zanoni 			      uint32_t enabled_irq_mask)
300edbfdb45SPaulo Zanoni {
301605cd25bSPaulo Zanoni 	uint32_t new_val;
302edbfdb45SPaulo Zanoni 
30315a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
30415a17aaeSDaniel Vetter 
305edbfdb45SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
306edbfdb45SPaulo Zanoni 
307f4e9af4fSAkash Goel 	new_val = dev_priv->pm_imr;
308f52ecbcfSPaulo Zanoni 	new_val &= ~interrupt_mask;
309f52ecbcfSPaulo Zanoni 	new_val |= (~enabled_irq_mask & interrupt_mask);
310f52ecbcfSPaulo Zanoni 
311f4e9af4fSAkash Goel 	if (new_val != dev_priv->pm_imr) {
312f4e9af4fSAkash Goel 		dev_priv->pm_imr = new_val;
313f4e9af4fSAkash Goel 		I915_WRITE(gen6_pm_imr(dev_priv), dev_priv->pm_imr);
314a72fbc3aSImre Deak 		POSTING_READ(gen6_pm_imr(dev_priv));
315edbfdb45SPaulo Zanoni 	}
316f52ecbcfSPaulo Zanoni }
317edbfdb45SPaulo Zanoni 
318f4e9af4fSAkash Goel void gen6_unmask_pm_irq(struct drm_i915_private *dev_priv, u32 mask)
319edbfdb45SPaulo Zanoni {
3209939fba2SImre Deak 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
3219939fba2SImre Deak 		return;
3229939fba2SImre Deak 
323edbfdb45SPaulo Zanoni 	snb_update_pm_irq(dev_priv, mask, mask);
324edbfdb45SPaulo Zanoni }
325edbfdb45SPaulo Zanoni 
326f4e9af4fSAkash Goel static void __gen6_mask_pm_irq(struct drm_i915_private *dev_priv, u32 mask)
3279939fba2SImre Deak {
3289939fba2SImre Deak 	snb_update_pm_irq(dev_priv, mask, 0);
3299939fba2SImre Deak }
3309939fba2SImre Deak 
331f4e9af4fSAkash Goel void gen6_mask_pm_irq(struct drm_i915_private *dev_priv, u32 mask)
332edbfdb45SPaulo Zanoni {
3339939fba2SImre Deak 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
3349939fba2SImre Deak 		return;
3359939fba2SImre Deak 
336f4e9af4fSAkash Goel 	__gen6_mask_pm_irq(dev_priv, mask);
337f4e9af4fSAkash Goel }
338f4e9af4fSAkash Goel 
339f4e9af4fSAkash Goel void gen6_reset_pm_iir(struct drm_i915_private *dev_priv, u32 reset_mask)
340f4e9af4fSAkash Goel {
341f4e9af4fSAkash Goel 	i915_reg_t reg = gen6_pm_iir(dev_priv);
342f4e9af4fSAkash Goel 
343f4e9af4fSAkash Goel 	assert_spin_locked(&dev_priv->irq_lock);
344f4e9af4fSAkash Goel 
345f4e9af4fSAkash Goel 	I915_WRITE(reg, reset_mask);
346f4e9af4fSAkash Goel 	I915_WRITE(reg, reset_mask);
347f4e9af4fSAkash Goel 	POSTING_READ(reg);
348f4e9af4fSAkash Goel }
349f4e9af4fSAkash Goel 
350f4e9af4fSAkash Goel void gen6_enable_pm_irq(struct drm_i915_private *dev_priv, u32 enable_mask)
351f4e9af4fSAkash Goel {
352f4e9af4fSAkash Goel 	assert_spin_locked(&dev_priv->irq_lock);
353f4e9af4fSAkash Goel 
354f4e9af4fSAkash Goel 	dev_priv->pm_ier |= enable_mask;
355f4e9af4fSAkash Goel 	I915_WRITE(gen6_pm_ier(dev_priv), dev_priv->pm_ier);
356f4e9af4fSAkash Goel 	gen6_unmask_pm_irq(dev_priv, enable_mask);
357f4e9af4fSAkash Goel 	/* unmask_pm_irq provides an implicit barrier (POSTING_READ) */
358f4e9af4fSAkash Goel }
359f4e9af4fSAkash Goel 
360f4e9af4fSAkash Goel void gen6_disable_pm_irq(struct drm_i915_private *dev_priv, u32 disable_mask)
361f4e9af4fSAkash Goel {
362f4e9af4fSAkash Goel 	assert_spin_locked(&dev_priv->irq_lock);
363f4e9af4fSAkash Goel 
364f4e9af4fSAkash Goel 	dev_priv->pm_ier &= ~disable_mask;
365f4e9af4fSAkash Goel 	__gen6_mask_pm_irq(dev_priv, disable_mask);
366f4e9af4fSAkash Goel 	I915_WRITE(gen6_pm_ier(dev_priv), dev_priv->pm_ier);
367f4e9af4fSAkash Goel 	/* though a barrier is missing here, but don't really need a one */
368edbfdb45SPaulo Zanoni }
369edbfdb45SPaulo Zanoni 
370dc97997aSChris Wilson void gen6_reset_rps_interrupts(struct drm_i915_private *dev_priv)
3713cc134e3SImre Deak {
3723cc134e3SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
373f4e9af4fSAkash Goel 	gen6_reset_pm_iir(dev_priv, dev_priv->pm_rps_events);
374096fad9eSImre Deak 	dev_priv->rps.pm_iir = 0;
3753cc134e3SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
3763cc134e3SImre Deak }
3773cc134e3SImre Deak 
37891d14251STvrtko Ursulin void gen6_enable_rps_interrupts(struct drm_i915_private *dev_priv)
379b900b949SImre Deak {
380f2a91d1aSChris Wilson 	if (READ_ONCE(dev_priv->rps.interrupts_enabled))
381f2a91d1aSChris Wilson 		return;
382f2a91d1aSChris Wilson 
383b900b949SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
384c33d247dSChris Wilson 	WARN_ON_ONCE(dev_priv->rps.pm_iir);
385c33d247dSChris Wilson 	WARN_ON_ONCE(I915_READ(gen6_pm_iir(dev_priv)) & dev_priv->pm_rps_events);
386d4d70aa5SImre Deak 	dev_priv->rps.interrupts_enabled = true;
387b900b949SImre Deak 	gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
38878e68d36SImre Deak 
389b900b949SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
390b900b949SImre Deak }
391b900b949SImre Deak 
39259d02a1fSImre Deak u32 gen6_sanitize_rps_pm_mask(struct drm_i915_private *dev_priv, u32 mask)
39359d02a1fSImre Deak {
3941800ad25SSagar Arun Kamble 	return (mask & ~dev_priv->rps.pm_intr_keep);
39559d02a1fSImre Deak }
39659d02a1fSImre Deak 
39791d14251STvrtko Ursulin void gen6_disable_rps_interrupts(struct drm_i915_private *dev_priv)
398b900b949SImre Deak {
399f2a91d1aSChris Wilson 	if (!READ_ONCE(dev_priv->rps.interrupts_enabled))
400f2a91d1aSChris Wilson 		return;
401f2a91d1aSChris Wilson 
402d4d70aa5SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
403d4d70aa5SImre Deak 	dev_priv->rps.interrupts_enabled = false;
4049939fba2SImre Deak 
405b20e3cfeSDave Gordon 	I915_WRITE(GEN6_PMINTRMSK, gen6_sanitize_rps_pm_mask(dev_priv, ~0u));
4069939fba2SImre Deak 
407f4e9af4fSAkash Goel 	gen6_disable_pm_irq(dev_priv, dev_priv->pm_rps_events);
40858072ccbSImre Deak 
40958072ccbSImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
41091c8a326SChris Wilson 	synchronize_irq(dev_priv->drm.irq);
411c33d247dSChris Wilson 
412c33d247dSChris Wilson 	/* Now that we will not be generating any more work, flush any
413c33d247dSChris Wilson 	 * outsanding tasks. As we are called on the RPS idle path,
414c33d247dSChris Wilson 	 * we will reset the GPU to minimum frequencies, so the current
415c33d247dSChris Wilson 	 * state of the worker can be discarded.
416c33d247dSChris Wilson 	 */
417c33d247dSChris Wilson 	cancel_work_sync(&dev_priv->rps.work);
418c33d247dSChris Wilson 	gen6_reset_rps_interrupts(dev_priv);
419b900b949SImre Deak }
420b900b949SImre Deak 
42126705e20SSagar Arun Kamble void gen9_reset_guc_interrupts(struct drm_i915_private *dev_priv)
42226705e20SSagar Arun Kamble {
42326705e20SSagar Arun Kamble 	spin_lock_irq(&dev_priv->irq_lock);
42426705e20SSagar Arun Kamble 	gen6_reset_pm_iir(dev_priv, dev_priv->pm_guc_events);
42526705e20SSagar Arun Kamble 	spin_unlock_irq(&dev_priv->irq_lock);
42626705e20SSagar Arun Kamble }
42726705e20SSagar Arun Kamble 
42826705e20SSagar Arun Kamble void gen9_enable_guc_interrupts(struct drm_i915_private *dev_priv)
42926705e20SSagar Arun Kamble {
43026705e20SSagar Arun Kamble 	spin_lock_irq(&dev_priv->irq_lock);
43126705e20SSagar Arun Kamble 	if (!dev_priv->guc.interrupts_enabled) {
43226705e20SSagar Arun Kamble 		WARN_ON_ONCE(I915_READ(gen6_pm_iir(dev_priv)) &
43326705e20SSagar Arun Kamble 				       dev_priv->pm_guc_events);
43426705e20SSagar Arun Kamble 		dev_priv->guc.interrupts_enabled = true;
43526705e20SSagar Arun Kamble 		gen6_enable_pm_irq(dev_priv, dev_priv->pm_guc_events);
43626705e20SSagar Arun Kamble 	}
43726705e20SSagar Arun Kamble 	spin_unlock_irq(&dev_priv->irq_lock);
43826705e20SSagar Arun Kamble }
43926705e20SSagar Arun Kamble 
44026705e20SSagar Arun Kamble void gen9_disable_guc_interrupts(struct drm_i915_private *dev_priv)
44126705e20SSagar Arun Kamble {
44226705e20SSagar Arun Kamble 	spin_lock_irq(&dev_priv->irq_lock);
44326705e20SSagar Arun Kamble 	dev_priv->guc.interrupts_enabled = false;
44426705e20SSagar Arun Kamble 
44526705e20SSagar Arun Kamble 	gen6_disable_pm_irq(dev_priv, dev_priv->pm_guc_events);
44626705e20SSagar Arun Kamble 
44726705e20SSagar Arun Kamble 	spin_unlock_irq(&dev_priv->irq_lock);
44826705e20SSagar Arun Kamble 	synchronize_irq(dev_priv->drm.irq);
44926705e20SSagar Arun Kamble 
45026705e20SSagar Arun Kamble 	gen9_reset_guc_interrupts(dev_priv);
45126705e20SSagar Arun Kamble }
45226705e20SSagar Arun Kamble 
4530961021aSBen Widawsky /**
4543a3b3c7dSVille Syrjälä  * bdw_update_port_irq - update DE port interrupt
4553a3b3c7dSVille Syrjälä  * @dev_priv: driver private
4563a3b3c7dSVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
4573a3b3c7dSVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
4583a3b3c7dSVille Syrjälä  */
4593a3b3c7dSVille Syrjälä static void bdw_update_port_irq(struct drm_i915_private *dev_priv,
4603a3b3c7dSVille Syrjälä 				uint32_t interrupt_mask,
4613a3b3c7dSVille Syrjälä 				uint32_t enabled_irq_mask)
4623a3b3c7dSVille Syrjälä {
4633a3b3c7dSVille Syrjälä 	uint32_t new_val;
4643a3b3c7dSVille Syrjälä 	uint32_t old_val;
4653a3b3c7dSVille Syrjälä 
4663a3b3c7dSVille Syrjälä 	assert_spin_locked(&dev_priv->irq_lock);
4673a3b3c7dSVille Syrjälä 
4683a3b3c7dSVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
4693a3b3c7dSVille Syrjälä 
4703a3b3c7dSVille Syrjälä 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
4713a3b3c7dSVille Syrjälä 		return;
4723a3b3c7dSVille Syrjälä 
4733a3b3c7dSVille Syrjälä 	old_val = I915_READ(GEN8_DE_PORT_IMR);
4743a3b3c7dSVille Syrjälä 
4753a3b3c7dSVille Syrjälä 	new_val = old_val;
4763a3b3c7dSVille Syrjälä 	new_val &= ~interrupt_mask;
4773a3b3c7dSVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
4783a3b3c7dSVille Syrjälä 
4793a3b3c7dSVille Syrjälä 	if (new_val != old_val) {
4803a3b3c7dSVille Syrjälä 		I915_WRITE(GEN8_DE_PORT_IMR, new_val);
4813a3b3c7dSVille Syrjälä 		POSTING_READ(GEN8_DE_PORT_IMR);
4823a3b3c7dSVille Syrjälä 	}
4833a3b3c7dSVille Syrjälä }
4843a3b3c7dSVille Syrjälä 
4853a3b3c7dSVille Syrjälä /**
486013d3752SVille Syrjälä  * bdw_update_pipe_irq - update DE pipe interrupt
487013d3752SVille Syrjälä  * @dev_priv: driver private
488013d3752SVille Syrjälä  * @pipe: pipe whose interrupt to update
489013d3752SVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
490013d3752SVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
491013d3752SVille Syrjälä  */
492013d3752SVille Syrjälä void bdw_update_pipe_irq(struct drm_i915_private *dev_priv,
493013d3752SVille Syrjälä 			 enum pipe pipe,
494013d3752SVille Syrjälä 			 uint32_t interrupt_mask,
495013d3752SVille Syrjälä 			 uint32_t enabled_irq_mask)
496013d3752SVille Syrjälä {
497013d3752SVille Syrjälä 	uint32_t new_val;
498013d3752SVille Syrjälä 
499013d3752SVille Syrjälä 	assert_spin_locked(&dev_priv->irq_lock);
500013d3752SVille Syrjälä 
501013d3752SVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
502013d3752SVille Syrjälä 
503013d3752SVille Syrjälä 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
504013d3752SVille Syrjälä 		return;
505013d3752SVille Syrjälä 
506013d3752SVille Syrjälä 	new_val = dev_priv->de_irq_mask[pipe];
507013d3752SVille Syrjälä 	new_val &= ~interrupt_mask;
508013d3752SVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
509013d3752SVille Syrjälä 
510013d3752SVille Syrjälä 	if (new_val != dev_priv->de_irq_mask[pipe]) {
511013d3752SVille Syrjälä 		dev_priv->de_irq_mask[pipe] = new_val;
512013d3752SVille Syrjälä 		I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
513013d3752SVille Syrjälä 		POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
514013d3752SVille Syrjälä 	}
515013d3752SVille Syrjälä }
516013d3752SVille Syrjälä 
517013d3752SVille Syrjälä /**
518fee884edSDaniel Vetter  * ibx_display_interrupt_update - update SDEIMR
519fee884edSDaniel Vetter  * @dev_priv: driver private
520fee884edSDaniel Vetter  * @interrupt_mask: mask of interrupt bits to update
521fee884edSDaniel Vetter  * @enabled_irq_mask: mask of interrupt bits to enable
522fee884edSDaniel Vetter  */
52347339cd9SDaniel Vetter void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
524fee884edSDaniel Vetter 				  uint32_t interrupt_mask,
525fee884edSDaniel Vetter 				  uint32_t enabled_irq_mask)
526fee884edSDaniel Vetter {
527fee884edSDaniel Vetter 	uint32_t sdeimr = I915_READ(SDEIMR);
528fee884edSDaniel Vetter 	sdeimr &= ~interrupt_mask;
529fee884edSDaniel Vetter 	sdeimr |= (~enabled_irq_mask & interrupt_mask);
530fee884edSDaniel Vetter 
53115a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
53215a17aaeSDaniel Vetter 
533fee884edSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
534fee884edSDaniel Vetter 
5359df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
536c67a470bSPaulo Zanoni 		return;
537c67a470bSPaulo Zanoni 
538fee884edSDaniel Vetter 	I915_WRITE(SDEIMR, sdeimr);
539fee884edSDaniel Vetter 	POSTING_READ(SDEIMR);
540fee884edSDaniel Vetter }
5418664281bSPaulo Zanoni 
542b5ea642aSDaniel Vetter static void
543755e9019SImre Deak __i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
544755e9019SImre Deak 		       u32 enable_mask, u32 status_mask)
5457c463586SKeith Packard {
546f0f59a00SVille Syrjälä 	i915_reg_t reg = PIPESTAT(pipe);
547755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
5487c463586SKeith Packard 
549b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
550d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
551b79480baSDaniel Vetter 
55204feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
55304feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
55404feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
55504feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
556755e9019SImre Deak 		return;
557755e9019SImre Deak 
558755e9019SImre Deak 	if ((pipestat & enable_mask) == enable_mask)
55946c06a30SVille Syrjälä 		return;
56046c06a30SVille Syrjälä 
56191d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] |= status_mask;
56291d181ddSImre Deak 
5637c463586SKeith Packard 	/* Enable the interrupt, clear any pending status */
564755e9019SImre Deak 	pipestat |= enable_mask | status_mask;
56546c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
5663143a2bfSChris Wilson 	POSTING_READ(reg);
5677c463586SKeith Packard }
5687c463586SKeith Packard 
569b5ea642aSDaniel Vetter static void
570755e9019SImre Deak __i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
571755e9019SImre Deak 		        u32 enable_mask, u32 status_mask)
5727c463586SKeith Packard {
573f0f59a00SVille Syrjälä 	i915_reg_t reg = PIPESTAT(pipe);
574755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
5757c463586SKeith Packard 
576b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
577d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
578b79480baSDaniel Vetter 
57904feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
58004feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
58104feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
58204feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
58346c06a30SVille Syrjälä 		return;
58446c06a30SVille Syrjälä 
585755e9019SImre Deak 	if ((pipestat & enable_mask) == 0)
586755e9019SImre Deak 		return;
587755e9019SImre Deak 
58891d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] &= ~status_mask;
58991d181ddSImre Deak 
590755e9019SImre Deak 	pipestat &= ~enable_mask;
59146c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
5923143a2bfSChris Wilson 	POSTING_READ(reg);
5937c463586SKeith Packard }
5947c463586SKeith Packard 
59510c59c51SImre Deak static u32 vlv_get_pipestat_enable_mask(struct drm_device *dev, u32 status_mask)
59610c59c51SImre Deak {
59710c59c51SImre Deak 	u32 enable_mask = status_mask << 16;
59810c59c51SImre Deak 
59910c59c51SImre Deak 	/*
600724a6905SVille Syrjälä 	 * On pipe A we don't support the PSR interrupt yet,
601724a6905SVille Syrjälä 	 * on pipe B and C the same bit MBZ.
60210c59c51SImre Deak 	 */
60310c59c51SImre Deak 	if (WARN_ON_ONCE(status_mask & PIPE_A_PSR_STATUS_VLV))
60410c59c51SImre Deak 		return 0;
605724a6905SVille Syrjälä 	/*
606724a6905SVille Syrjälä 	 * On pipe B and C we don't support the PSR interrupt yet, on pipe
607724a6905SVille Syrjälä 	 * A the same bit is for perf counters which we don't use either.
608724a6905SVille Syrjälä 	 */
609724a6905SVille Syrjälä 	if (WARN_ON_ONCE(status_mask & PIPE_B_PSR_STATUS_VLV))
610724a6905SVille Syrjälä 		return 0;
61110c59c51SImre Deak 
61210c59c51SImre Deak 	enable_mask &= ~(PIPE_FIFO_UNDERRUN_STATUS |
61310c59c51SImre Deak 			 SPRITE0_FLIP_DONE_INT_EN_VLV |
61410c59c51SImre Deak 			 SPRITE1_FLIP_DONE_INT_EN_VLV);
61510c59c51SImre Deak 	if (status_mask & SPRITE0_FLIP_DONE_INT_STATUS_VLV)
61610c59c51SImre Deak 		enable_mask |= SPRITE0_FLIP_DONE_INT_EN_VLV;
61710c59c51SImre Deak 	if (status_mask & SPRITE1_FLIP_DONE_INT_STATUS_VLV)
61810c59c51SImre Deak 		enable_mask |= SPRITE1_FLIP_DONE_INT_EN_VLV;
61910c59c51SImre Deak 
62010c59c51SImre Deak 	return enable_mask;
62110c59c51SImre Deak }
62210c59c51SImre Deak 
623755e9019SImre Deak void
624755e9019SImre Deak i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
625755e9019SImre Deak 		     u32 status_mask)
626755e9019SImre Deak {
627755e9019SImre Deak 	u32 enable_mask;
628755e9019SImre Deak 
629666a4537SWayne Boyer 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
63091c8a326SChris Wilson 		enable_mask = vlv_get_pipestat_enable_mask(&dev_priv->drm,
63110c59c51SImre Deak 							   status_mask);
63210c59c51SImre Deak 	else
633755e9019SImre Deak 		enable_mask = status_mask << 16;
634755e9019SImre Deak 	__i915_enable_pipestat(dev_priv, pipe, enable_mask, status_mask);
635755e9019SImre Deak }
636755e9019SImre Deak 
637755e9019SImre Deak void
638755e9019SImre Deak i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
639755e9019SImre Deak 		      u32 status_mask)
640755e9019SImre Deak {
641755e9019SImre Deak 	u32 enable_mask;
642755e9019SImre Deak 
643666a4537SWayne Boyer 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
64491c8a326SChris Wilson 		enable_mask = vlv_get_pipestat_enable_mask(&dev_priv->drm,
64510c59c51SImre Deak 							   status_mask);
64610c59c51SImre Deak 	else
647755e9019SImre Deak 		enable_mask = status_mask << 16;
648755e9019SImre Deak 	__i915_disable_pipestat(dev_priv, pipe, enable_mask, status_mask);
649755e9019SImre Deak }
650755e9019SImre Deak 
651c0e09200SDave Airlie /**
652f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
65314bb2c11STvrtko Ursulin  * @dev_priv: i915 device private
65401c66889SZhao Yakui  */
65591d14251STvrtko Ursulin static void i915_enable_asle_pipestat(struct drm_i915_private *dev_priv)
65601c66889SZhao Yakui {
65791d14251STvrtko Ursulin 	if (!dev_priv->opregion.asle || !IS_MOBILE(dev_priv))
658f49e38ddSJani Nikula 		return;
659f49e38ddSJani Nikula 
66013321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
66101c66889SZhao Yakui 
662755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_STATUS);
66391d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 4)
6643b6c42e8SDaniel Vetter 		i915_enable_pipestat(dev_priv, PIPE_A,
665755e9019SImre Deak 				     PIPE_LEGACY_BLC_EVENT_STATUS);
6661ec14ad3SChris Wilson 
66713321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
66801c66889SZhao Yakui }
66901c66889SZhao Yakui 
670f75f3746SVille Syrjälä /*
671f75f3746SVille Syrjälä  * This timing diagram depicts the video signal in and
672f75f3746SVille Syrjälä  * around the vertical blanking period.
673f75f3746SVille Syrjälä  *
674f75f3746SVille Syrjälä  * Assumptions about the fictitious mode used in this example:
675f75f3746SVille Syrjälä  *  vblank_start >= 3
676f75f3746SVille Syrjälä  *  vsync_start = vblank_start + 1
677f75f3746SVille Syrjälä  *  vsync_end = vblank_start + 2
678f75f3746SVille Syrjälä  *  vtotal = vblank_start + 3
679f75f3746SVille Syrjälä  *
680f75f3746SVille Syrjälä  *           start of vblank:
681f75f3746SVille Syrjälä  *           latch double buffered registers
682f75f3746SVille Syrjälä  *           increment frame counter (ctg+)
683f75f3746SVille Syrjälä  *           generate start of vblank interrupt (gen4+)
684f75f3746SVille Syrjälä  *           |
685f75f3746SVille Syrjälä  *           |          frame start:
686f75f3746SVille Syrjälä  *           |          generate frame start interrupt (aka. vblank interrupt) (gmch)
687f75f3746SVille Syrjälä  *           |          may be shifted forward 1-3 extra lines via PIPECONF
688f75f3746SVille Syrjälä  *           |          |
689f75f3746SVille Syrjälä  *           |          |  start of vsync:
690f75f3746SVille Syrjälä  *           |          |  generate vsync interrupt
691f75f3746SVille Syrjälä  *           |          |  |
692f75f3746SVille Syrjälä  * ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx
693f75f3746SVille Syrjälä  *       .   \hs/   .      \hs/          \hs/          \hs/   .      \hs/
694f75f3746SVille Syrjälä  * ----va---> <-----------------vb--------------------> <--------va-------------
695f75f3746SVille Syrjälä  *       |          |       <----vs----->                     |
696f75f3746SVille Syrjälä  * -vbs-----> <---vbs+1---> <---vbs+2---> <-----0-----> <-----1-----> <-----2--- (scanline counter gen2)
697f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2---> <-----0--- (scanline counter gen3+)
698f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2- (scanline counter hsw+ hdmi)
699f75f3746SVille Syrjälä  *       |          |                                         |
700f75f3746SVille Syrjälä  *       last visible pixel                                   first visible pixel
701f75f3746SVille Syrjälä  *                  |                                         increment frame counter (gen3/4)
702f75f3746SVille Syrjälä  *                  pixel counter = vblank_start * htotal     pixel counter = 0 (gen3/4)
703f75f3746SVille Syrjälä  *
704f75f3746SVille Syrjälä  * x  = horizontal active
705f75f3746SVille Syrjälä  * _  = horizontal blanking
706f75f3746SVille Syrjälä  * hs = horizontal sync
707f75f3746SVille Syrjälä  * va = vertical active
708f75f3746SVille Syrjälä  * vb = vertical blanking
709f75f3746SVille Syrjälä  * vs = vertical sync
710f75f3746SVille Syrjälä  * vbs = vblank_start (number)
711f75f3746SVille Syrjälä  *
712f75f3746SVille Syrjälä  * Summary:
713f75f3746SVille Syrjälä  * - most events happen at the start of horizontal sync
714f75f3746SVille Syrjälä  * - frame start happens at the start of horizontal blank, 1-4 lines
715f75f3746SVille Syrjälä  *   (depending on PIPECONF settings) after the start of vblank
716f75f3746SVille Syrjälä  * - gen3/4 pixel and frame counter are synchronized with the start
717f75f3746SVille Syrjälä  *   of horizontal active on the first line of vertical active
718f75f3746SVille Syrjälä  */
719f75f3746SVille Syrjälä 
72042f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
72142f52ef8SKeith Packard  * we use as a pipe index
72242f52ef8SKeith Packard  */
72388e72717SThierry Reding static u32 i915_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
7240a3e67a4SJesse Barnes {
725fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
726f0f59a00SVille Syrjälä 	i915_reg_t high_frame, low_frame;
7270b2a8e09SVille Syrjälä 	u32 high1, high2, low, pixel, vbl_start, hsync_start, htotal;
72898187836SVille Syrjälä 	struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
72998187836SVille Syrjälä 								pipe);
730fc467a22SMaarten Lankhorst 	const struct drm_display_mode *mode = &intel_crtc->base.hwmode;
731391f75e2SVille Syrjälä 
7320b2a8e09SVille Syrjälä 	htotal = mode->crtc_htotal;
7330b2a8e09SVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
7340b2a8e09SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
7350b2a8e09SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
7360b2a8e09SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
737391f75e2SVille Syrjälä 
7380b2a8e09SVille Syrjälä 	/* Convert to pixel count */
7390b2a8e09SVille Syrjälä 	vbl_start *= htotal;
7400b2a8e09SVille Syrjälä 
7410b2a8e09SVille Syrjälä 	/* Start of vblank event occurs at start of hsync */
7420b2a8e09SVille Syrjälä 	vbl_start -= htotal - hsync_start;
7430b2a8e09SVille Syrjälä 
7449db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
7459db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
7465eddb70bSChris Wilson 
7470a3e67a4SJesse Barnes 	/*
7480a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
7490a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
7500a3e67a4SJesse Barnes 	 * register.
7510a3e67a4SJesse Barnes 	 */
7520a3e67a4SJesse Barnes 	do {
7535eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
754391f75e2SVille Syrjälä 		low   = I915_READ(low_frame);
7555eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
7560a3e67a4SJesse Barnes 	} while (high1 != high2);
7570a3e67a4SJesse Barnes 
7585eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
759391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
7605eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
761391f75e2SVille Syrjälä 
762391f75e2SVille Syrjälä 	/*
763391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
764391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
765391f75e2SVille Syrjälä 	 * counter against vblank start.
766391f75e2SVille Syrjälä 	 */
767edc08d0aSVille Syrjälä 	return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
7680a3e67a4SJesse Barnes }
7690a3e67a4SJesse Barnes 
770974e59baSDave Airlie static u32 g4x_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
7719880b7a5SJesse Barnes {
772fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
7739880b7a5SJesse Barnes 
774649636efSVille Syrjälä 	return I915_READ(PIPE_FRMCOUNT_G4X(pipe));
7759880b7a5SJesse Barnes }
7769880b7a5SJesse Barnes 
77775aa3f63SVille Syrjälä /* I915_READ_FW, only for fast reads of display block, no need for forcewake etc. */
778a225f079SVille Syrjälä static int __intel_get_crtc_scanline(struct intel_crtc *crtc)
779a225f079SVille Syrjälä {
780a225f079SVille Syrjälä 	struct drm_device *dev = crtc->base.dev;
781fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
782fc467a22SMaarten Lankhorst 	const struct drm_display_mode *mode = &crtc->base.hwmode;
783a225f079SVille Syrjälä 	enum pipe pipe = crtc->pipe;
78480715b2fSVille Syrjälä 	int position, vtotal;
785a225f079SVille Syrjälä 
78680715b2fSVille Syrjälä 	vtotal = mode->crtc_vtotal;
787a225f079SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
788a225f079SVille Syrjälä 		vtotal /= 2;
789a225f079SVille Syrjälä 
79091d14251STvrtko Ursulin 	if (IS_GEN2(dev_priv))
79175aa3f63SVille Syrjälä 		position = I915_READ_FW(PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
792a225f079SVille Syrjälä 	else
79375aa3f63SVille Syrjälä 		position = I915_READ_FW(PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
794a225f079SVille Syrjälä 
795a225f079SVille Syrjälä 	/*
79641b578fbSJesse Barnes 	 * On HSW, the DSL reg (0x70000) appears to return 0 if we
79741b578fbSJesse Barnes 	 * read it just before the start of vblank.  So try it again
79841b578fbSJesse Barnes 	 * so we don't accidentally end up spanning a vblank frame
79941b578fbSJesse Barnes 	 * increment, causing the pipe_update_end() code to squak at us.
80041b578fbSJesse Barnes 	 *
80141b578fbSJesse Barnes 	 * The nature of this problem means we can't simply check the ISR
80241b578fbSJesse Barnes 	 * bit and return the vblank start value; nor can we use the scanline
80341b578fbSJesse Barnes 	 * debug register in the transcoder as it appears to have the same
80441b578fbSJesse Barnes 	 * problem.  We may need to extend this to include other platforms,
80541b578fbSJesse Barnes 	 * but so far testing only shows the problem on HSW.
80641b578fbSJesse Barnes 	 */
80791d14251STvrtko Ursulin 	if (HAS_DDI(dev_priv) && !position) {
80841b578fbSJesse Barnes 		int i, temp;
80941b578fbSJesse Barnes 
81041b578fbSJesse Barnes 		for (i = 0; i < 100; i++) {
81141b578fbSJesse Barnes 			udelay(1);
81241b578fbSJesse Barnes 			temp = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) &
81341b578fbSJesse Barnes 				DSL_LINEMASK_GEN3;
81441b578fbSJesse Barnes 			if (temp != position) {
81541b578fbSJesse Barnes 				position = temp;
81641b578fbSJesse Barnes 				break;
81741b578fbSJesse Barnes 			}
81841b578fbSJesse Barnes 		}
81941b578fbSJesse Barnes 	}
82041b578fbSJesse Barnes 
82141b578fbSJesse Barnes 	/*
82280715b2fSVille Syrjälä 	 * See update_scanline_offset() for the details on the
82380715b2fSVille Syrjälä 	 * scanline_offset adjustment.
824a225f079SVille Syrjälä 	 */
82580715b2fSVille Syrjälä 	return (position + crtc->scanline_offset) % vtotal;
826a225f079SVille Syrjälä }
827a225f079SVille Syrjälä 
82888e72717SThierry Reding static int i915_get_crtc_scanoutpos(struct drm_device *dev, unsigned int pipe,
829abca9e45SVille Syrjälä 				    unsigned int flags, int *vpos, int *hpos,
8303bb403bfSVille Syrjälä 				    ktime_t *stime, ktime_t *etime,
8313bb403bfSVille Syrjälä 				    const struct drm_display_mode *mode)
8320af7e4dfSMario Kleiner {
833fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
83498187836SVille Syrjälä 	struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
83598187836SVille Syrjälä 								pipe);
8363aa18df8SVille Syrjälä 	int position;
83778e8fc6bSVille Syrjälä 	int vbl_start, vbl_end, hsync_start, htotal, vtotal;
8380af7e4dfSMario Kleiner 	bool in_vbl = true;
8390af7e4dfSMario Kleiner 	int ret = 0;
840ad3543edSMario Kleiner 	unsigned long irqflags;
8410af7e4dfSMario Kleiner 
842fc467a22SMaarten Lankhorst 	if (WARN_ON(!mode->crtc_clock)) {
8430af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
8449db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
8450af7e4dfSMario Kleiner 		return 0;
8460af7e4dfSMario Kleiner 	}
8470af7e4dfSMario Kleiner 
848c2baf4b7SVille Syrjälä 	htotal = mode->crtc_htotal;
84978e8fc6bSVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
850c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
851c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
852c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
8530af7e4dfSMario Kleiner 
854d31faf65SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
855d31faf65SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
856d31faf65SVille Syrjälä 		vbl_end /= 2;
857d31faf65SVille Syrjälä 		vtotal /= 2;
858d31faf65SVille Syrjälä 	}
859d31faf65SVille Syrjälä 
860c2baf4b7SVille Syrjälä 	ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
861c2baf4b7SVille Syrjälä 
862ad3543edSMario Kleiner 	/*
863ad3543edSMario Kleiner 	 * Lock uncore.lock, as we will do multiple timing critical raw
864ad3543edSMario Kleiner 	 * register reads, potentially with preemption disabled, so the
865ad3543edSMario Kleiner 	 * following code must not block on uncore.lock.
866ad3543edSMario Kleiner 	 */
867ad3543edSMario Kleiner 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
868ad3543edSMario Kleiner 
869ad3543edSMario Kleiner 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
870ad3543edSMario Kleiner 
871ad3543edSMario Kleiner 	/* Get optional system timestamp before query. */
872ad3543edSMario Kleiner 	if (stime)
873ad3543edSMario Kleiner 		*stime = ktime_get();
874ad3543edSMario Kleiner 
87591d14251STvrtko Ursulin 	if (IS_GEN2(dev_priv) || IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5) {
8760af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
8770af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
8780af7e4dfSMario Kleiner 		 */
879a225f079SVille Syrjälä 		position = __intel_get_crtc_scanline(intel_crtc);
8800af7e4dfSMario Kleiner 	} else {
8810af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
8820af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
8830af7e4dfSMario Kleiner 		 * scanout position.
8840af7e4dfSMario Kleiner 		 */
88575aa3f63SVille Syrjälä 		position = (I915_READ_FW(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
8860af7e4dfSMario Kleiner 
8873aa18df8SVille Syrjälä 		/* convert to pixel counts */
8883aa18df8SVille Syrjälä 		vbl_start *= htotal;
8893aa18df8SVille Syrjälä 		vbl_end *= htotal;
8903aa18df8SVille Syrjälä 		vtotal *= htotal;
89178e8fc6bSVille Syrjälä 
89278e8fc6bSVille Syrjälä 		/*
8937e78f1cbSVille Syrjälä 		 * In interlaced modes, the pixel counter counts all pixels,
8947e78f1cbSVille Syrjälä 		 * so one field will have htotal more pixels. In order to avoid
8957e78f1cbSVille Syrjälä 		 * the reported position from jumping backwards when the pixel
8967e78f1cbSVille Syrjälä 		 * counter is beyond the length of the shorter field, just
8977e78f1cbSVille Syrjälä 		 * clamp the position the length of the shorter field. This
8987e78f1cbSVille Syrjälä 		 * matches how the scanline counter based position works since
8997e78f1cbSVille Syrjälä 		 * the scanline counter doesn't count the two half lines.
9007e78f1cbSVille Syrjälä 		 */
9017e78f1cbSVille Syrjälä 		if (position >= vtotal)
9027e78f1cbSVille Syrjälä 			position = vtotal - 1;
9037e78f1cbSVille Syrjälä 
9047e78f1cbSVille Syrjälä 		/*
90578e8fc6bSVille Syrjälä 		 * Start of vblank interrupt is triggered at start of hsync,
90678e8fc6bSVille Syrjälä 		 * just prior to the first active line of vblank. However we
90778e8fc6bSVille Syrjälä 		 * consider lines to start at the leading edge of horizontal
90878e8fc6bSVille Syrjälä 		 * active. So, should we get here before we've crossed into
90978e8fc6bSVille Syrjälä 		 * the horizontal active of the first line in vblank, we would
91078e8fc6bSVille Syrjälä 		 * not set the DRM_SCANOUTPOS_INVBL flag. In order to fix that,
91178e8fc6bSVille Syrjälä 		 * always add htotal-hsync_start to the current pixel position.
91278e8fc6bSVille Syrjälä 		 */
91378e8fc6bSVille Syrjälä 		position = (position + htotal - hsync_start) % vtotal;
9143aa18df8SVille Syrjälä 	}
9153aa18df8SVille Syrjälä 
916ad3543edSMario Kleiner 	/* Get optional system timestamp after query. */
917ad3543edSMario Kleiner 	if (etime)
918ad3543edSMario Kleiner 		*etime = ktime_get();
919ad3543edSMario Kleiner 
920ad3543edSMario Kleiner 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
921ad3543edSMario Kleiner 
922ad3543edSMario Kleiner 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
923ad3543edSMario Kleiner 
9243aa18df8SVille Syrjälä 	in_vbl = position >= vbl_start && position < vbl_end;
9253aa18df8SVille Syrjälä 
9263aa18df8SVille Syrjälä 	/*
9273aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
9283aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
9293aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
9303aa18df8SVille Syrjälä 	 * up since vbl_end.
9313aa18df8SVille Syrjälä 	 */
9323aa18df8SVille Syrjälä 	if (position >= vbl_start)
9333aa18df8SVille Syrjälä 		position -= vbl_end;
9343aa18df8SVille Syrjälä 	else
9353aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
9363aa18df8SVille Syrjälä 
93791d14251STvrtko Ursulin 	if (IS_GEN2(dev_priv) || IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5) {
9383aa18df8SVille Syrjälä 		*vpos = position;
9393aa18df8SVille Syrjälä 		*hpos = 0;
9403aa18df8SVille Syrjälä 	} else {
9410af7e4dfSMario Kleiner 		*vpos = position / htotal;
9420af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
9430af7e4dfSMario Kleiner 	}
9440af7e4dfSMario Kleiner 
9450af7e4dfSMario Kleiner 	/* In vblank? */
9460af7e4dfSMario Kleiner 	if (in_vbl)
9473d3cbd84SDaniel Vetter 		ret |= DRM_SCANOUTPOS_IN_VBLANK;
9480af7e4dfSMario Kleiner 
9490af7e4dfSMario Kleiner 	return ret;
9500af7e4dfSMario Kleiner }
9510af7e4dfSMario Kleiner 
952a225f079SVille Syrjälä int intel_get_crtc_scanline(struct intel_crtc *crtc)
953a225f079SVille Syrjälä {
954fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
955a225f079SVille Syrjälä 	unsigned long irqflags;
956a225f079SVille Syrjälä 	int position;
957a225f079SVille Syrjälä 
958a225f079SVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
959a225f079SVille Syrjälä 	position = __intel_get_crtc_scanline(crtc);
960a225f079SVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
961a225f079SVille Syrjälä 
962a225f079SVille Syrjälä 	return position;
963a225f079SVille Syrjälä }
964a225f079SVille Syrjälä 
96588e72717SThierry Reding static int i915_get_vblank_timestamp(struct drm_device *dev, unsigned int pipe,
9660af7e4dfSMario Kleiner 			      int *max_error,
9670af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
9680af7e4dfSMario Kleiner 			      unsigned flags)
9690af7e4dfSMario Kleiner {
970b91eb5ccSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(dev);
971e2af48c6SVille Syrjälä 	struct intel_crtc *crtc;
9720af7e4dfSMario Kleiner 
973b91eb5ccSVille Syrjälä 	if (pipe >= INTEL_INFO(dev_priv)->num_pipes) {
97488e72717SThierry Reding 		DRM_ERROR("Invalid crtc %u\n", pipe);
9750af7e4dfSMario Kleiner 		return -EINVAL;
9760af7e4dfSMario Kleiner 	}
9770af7e4dfSMario Kleiner 
9780af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
979b91eb5ccSVille Syrjälä 	crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
9804041b853SChris Wilson 	if (crtc == NULL) {
98188e72717SThierry Reding 		DRM_ERROR("Invalid crtc %u\n", pipe);
9824041b853SChris Wilson 		return -EINVAL;
9834041b853SChris Wilson 	}
9844041b853SChris Wilson 
985e2af48c6SVille Syrjälä 	if (!crtc->base.hwmode.crtc_clock) {
98688e72717SThierry Reding 		DRM_DEBUG_KMS("crtc %u is disabled\n", pipe);
9874041b853SChris Wilson 		return -EBUSY;
9884041b853SChris Wilson 	}
9890af7e4dfSMario Kleiner 
9900af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
9914041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
9924041b853SChris Wilson 						     vblank_time, flags,
993e2af48c6SVille Syrjälä 						     &crtc->base.hwmode);
9940af7e4dfSMario Kleiner }
9950af7e4dfSMario Kleiner 
99691d14251STvrtko Ursulin static void ironlake_rps_change_irq_handler(struct drm_i915_private *dev_priv)
997f97108d1SJesse Barnes {
998b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
9999270388eSDaniel Vetter 	u8 new_delay;
10009270388eSDaniel Vetter 
1001d0ecd7e2SDaniel Vetter 	spin_lock(&mchdev_lock);
1002f97108d1SJesse Barnes 
100373edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
100473edd18fSDaniel Vetter 
100520e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
10069270388eSDaniel Vetter 
10077648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
1008b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
1009b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
1010f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
1011f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
1012f97108d1SJesse Barnes 
1013f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
1014b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
101520e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
101620e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
101720e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
101820e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
1019b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
102020e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
102120e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
102220e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
102320e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
1024f97108d1SJesse Barnes 	}
1025f97108d1SJesse Barnes 
102691d14251STvrtko Ursulin 	if (ironlake_set_drps(dev_priv, new_delay))
102720e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
1028f97108d1SJesse Barnes 
1029d0ecd7e2SDaniel Vetter 	spin_unlock(&mchdev_lock);
10309270388eSDaniel Vetter 
1031f97108d1SJesse Barnes 	return;
1032f97108d1SJesse Barnes }
1033f97108d1SJesse Barnes 
10340bc40be8STvrtko Ursulin static void notify_ring(struct intel_engine_cs *engine)
1035549f7365SChris Wilson {
1036538b257dSChris Wilson 	set_bit(ENGINE_IRQ_BREADCRUMB, &engine->irq_posted);
103783348ba8SChris Wilson 	if (intel_engine_wakeup(engine))
10380bc40be8STvrtko Ursulin 		trace_i915_gem_request_notify(engine);
1039549f7365SChris Wilson }
1040549f7365SChris Wilson 
104143cf3bf0SChris Wilson static void vlv_c0_read(struct drm_i915_private *dev_priv,
104243cf3bf0SChris Wilson 			struct intel_rps_ei *ei)
104331685c25SDeepak S {
104443cf3bf0SChris Wilson 	ei->cz_clock = vlv_punit_read(dev_priv, PUNIT_REG_CZ_TIMESTAMP);
104543cf3bf0SChris Wilson 	ei->render_c0 = I915_READ(VLV_RENDER_C0_COUNT);
104643cf3bf0SChris Wilson 	ei->media_c0 = I915_READ(VLV_MEDIA_C0_COUNT);
104731685c25SDeepak S }
104831685c25SDeepak S 
104943cf3bf0SChris Wilson static bool vlv_c0_above(struct drm_i915_private *dev_priv,
105043cf3bf0SChris Wilson 			 const struct intel_rps_ei *old,
105143cf3bf0SChris Wilson 			 const struct intel_rps_ei *now,
105243cf3bf0SChris Wilson 			 int threshold)
105331685c25SDeepak S {
105443cf3bf0SChris Wilson 	u64 time, c0;
10557bad74d5SVille Syrjälä 	unsigned int mul = 100;
105631685c25SDeepak S 
105743cf3bf0SChris Wilson 	if (old->cz_clock == 0)
105843cf3bf0SChris Wilson 		return false;
105931685c25SDeepak S 
10607bad74d5SVille Syrjälä 	if (I915_READ(VLV_COUNTER_CONTROL) & VLV_COUNT_RANGE_HIGH)
10617bad74d5SVille Syrjälä 		mul <<= 8;
10627bad74d5SVille Syrjälä 
106343cf3bf0SChris Wilson 	time = now->cz_clock - old->cz_clock;
10647bad74d5SVille Syrjälä 	time *= threshold * dev_priv->czclk_freq;
106531685c25SDeepak S 
106643cf3bf0SChris Wilson 	/* Workload can be split between render + media, e.g. SwapBuffers
106743cf3bf0SChris Wilson 	 * being blitted in X after being rendered in mesa. To account for
106843cf3bf0SChris Wilson 	 * this we need to combine both engines into our activity counter.
106943cf3bf0SChris Wilson 	 */
107043cf3bf0SChris Wilson 	c0 = now->render_c0 - old->render_c0;
107143cf3bf0SChris Wilson 	c0 += now->media_c0 - old->media_c0;
10727bad74d5SVille Syrjälä 	c0 *= mul * VLV_CZ_CLOCK_TO_MILLI_SEC;
107331685c25SDeepak S 
107443cf3bf0SChris Wilson 	return c0 >= time;
107531685c25SDeepak S }
107631685c25SDeepak S 
107743cf3bf0SChris Wilson void gen6_rps_reset_ei(struct drm_i915_private *dev_priv)
107843cf3bf0SChris Wilson {
107943cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &dev_priv->rps.down_ei);
108043cf3bf0SChris Wilson 	dev_priv->rps.up_ei = dev_priv->rps.down_ei;
108143cf3bf0SChris Wilson }
108243cf3bf0SChris Wilson 
108343cf3bf0SChris Wilson static u32 vlv_wa_c0_ei(struct drm_i915_private *dev_priv, u32 pm_iir)
108443cf3bf0SChris Wilson {
108543cf3bf0SChris Wilson 	struct intel_rps_ei now;
108643cf3bf0SChris Wilson 	u32 events = 0;
108743cf3bf0SChris Wilson 
10886f4b12f8SChris Wilson 	if ((pm_iir & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED)) == 0)
108943cf3bf0SChris Wilson 		return 0;
109043cf3bf0SChris Wilson 
109143cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &now);
109243cf3bf0SChris Wilson 	if (now.cz_clock == 0)
109343cf3bf0SChris Wilson 		return 0;
109431685c25SDeepak S 
109543cf3bf0SChris Wilson 	if (pm_iir & GEN6_PM_RP_DOWN_EI_EXPIRED) {
109643cf3bf0SChris Wilson 		if (!vlv_c0_above(dev_priv,
109743cf3bf0SChris Wilson 				  &dev_priv->rps.down_ei, &now,
10988fb55197SChris Wilson 				  dev_priv->rps.down_threshold))
109943cf3bf0SChris Wilson 			events |= GEN6_PM_RP_DOWN_THRESHOLD;
110043cf3bf0SChris Wilson 		dev_priv->rps.down_ei = now;
110131685c25SDeepak S 	}
110231685c25SDeepak S 
110343cf3bf0SChris Wilson 	if (pm_iir & GEN6_PM_RP_UP_EI_EXPIRED) {
110443cf3bf0SChris Wilson 		if (vlv_c0_above(dev_priv,
110543cf3bf0SChris Wilson 				 &dev_priv->rps.up_ei, &now,
11068fb55197SChris Wilson 				 dev_priv->rps.up_threshold))
110743cf3bf0SChris Wilson 			events |= GEN6_PM_RP_UP_THRESHOLD;
110843cf3bf0SChris Wilson 		dev_priv->rps.up_ei = now;
110943cf3bf0SChris Wilson 	}
111043cf3bf0SChris Wilson 
111143cf3bf0SChris Wilson 	return events;
111231685c25SDeepak S }
111331685c25SDeepak S 
1114f5a4c67dSChris Wilson static bool any_waiters(struct drm_i915_private *dev_priv)
1115f5a4c67dSChris Wilson {
1116e2f80391STvrtko Ursulin 	struct intel_engine_cs *engine;
11173b3f1650SAkash Goel 	enum intel_engine_id id;
1118f5a4c67dSChris Wilson 
11193b3f1650SAkash Goel 	for_each_engine(engine, dev_priv, id)
1120688e6c72SChris Wilson 		if (intel_engine_has_waiter(engine))
1121f5a4c67dSChris Wilson 			return true;
1122f5a4c67dSChris Wilson 
1123f5a4c67dSChris Wilson 	return false;
1124f5a4c67dSChris Wilson }
1125f5a4c67dSChris Wilson 
11264912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
11273b8d8d91SJesse Barnes {
11282d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
11292d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, rps.work);
11308d3afd7dSChris Wilson 	bool client_boost;
11318d3afd7dSChris Wilson 	int new_delay, adj, min, max;
1132edbfdb45SPaulo Zanoni 	u32 pm_iir;
11333b8d8d91SJesse Barnes 
113459cdb63dSDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
1135d4d70aa5SImre Deak 	/* Speed up work cancelation during disabling rps interrupts. */
1136d4d70aa5SImre Deak 	if (!dev_priv->rps.interrupts_enabled) {
1137d4d70aa5SImre Deak 		spin_unlock_irq(&dev_priv->irq_lock);
1138d4d70aa5SImre Deak 		return;
1139d4d70aa5SImre Deak 	}
11401f814dacSImre Deak 
1141c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
1142c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
1143a72fbc3aSImre Deak 	/* Make sure not to corrupt PMIMR state used by ringbuffer on GEN6 */
1144f4e9af4fSAkash Goel 	gen6_unmask_pm_irq(dev_priv, dev_priv->pm_rps_events);
11458d3afd7dSChris Wilson 	client_boost = dev_priv->rps.client_boost;
11468d3afd7dSChris Wilson 	dev_priv->rps.client_boost = false;
114759cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
11484912d041SBen Widawsky 
114960611c13SPaulo Zanoni 	/* Make sure we didn't queue anything we're not going to process. */
1150a6706b45SDeepak S 	WARN_ON(pm_iir & ~dev_priv->pm_rps_events);
115160611c13SPaulo Zanoni 
11528d3afd7dSChris Wilson 	if ((pm_iir & dev_priv->pm_rps_events) == 0 && !client_boost)
1153c33d247dSChris Wilson 		return;
11543b8d8d91SJesse Barnes 
11554fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
11567b9e0ae6SChris Wilson 
115743cf3bf0SChris Wilson 	pm_iir |= vlv_wa_c0_ei(dev_priv, pm_iir);
115843cf3bf0SChris Wilson 
1159dd75fdc8SChris Wilson 	adj = dev_priv->rps.last_adj;
1160edcf284bSChris Wilson 	new_delay = dev_priv->rps.cur_freq;
11618d3afd7dSChris Wilson 	min = dev_priv->rps.min_freq_softlimit;
11628d3afd7dSChris Wilson 	max = dev_priv->rps.max_freq_softlimit;
116329ecd78dSChris Wilson 	if (client_boost || any_waiters(dev_priv))
116429ecd78dSChris Wilson 		max = dev_priv->rps.max_freq;
116529ecd78dSChris Wilson 	if (client_boost && new_delay < dev_priv->rps.boost_freq) {
116629ecd78dSChris Wilson 		new_delay = dev_priv->rps.boost_freq;
11678d3afd7dSChris Wilson 		adj = 0;
11688d3afd7dSChris Wilson 	} else if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
1169dd75fdc8SChris Wilson 		if (adj > 0)
1170dd75fdc8SChris Wilson 			adj *= 2;
1171edcf284bSChris Wilson 		else /* CHV needs even encode values */
1172edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? 2 : 1;
11737e79a683SSagar Arun Kamble 
11747e79a683SSagar Arun Kamble 		if (new_delay >= dev_priv->rps.max_freq_softlimit)
11757e79a683SSagar Arun Kamble 			adj = 0;
11767425034aSVille Syrjälä 		/*
11777425034aSVille Syrjälä 		 * For better performance, jump directly
11787425034aSVille Syrjälä 		 * to RPe if we're below it.
11797425034aSVille Syrjälä 		 */
1180edcf284bSChris Wilson 		if (new_delay < dev_priv->rps.efficient_freq - adj) {
1181b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1182edcf284bSChris Wilson 			adj = 0;
1183edcf284bSChris Wilson 		}
118429ecd78dSChris Wilson 	} else if (client_boost || any_waiters(dev_priv)) {
1185f5a4c67dSChris Wilson 		adj = 0;
1186dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
1187b39fb297SBen Widawsky 		if (dev_priv->rps.cur_freq > dev_priv->rps.efficient_freq)
1188b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1189*17136d54SChris Wilson 		else if (dev_priv->rps.cur_freq > dev_priv->rps.min_freq_softlimit)
1190b39fb297SBen Widawsky 			new_delay = dev_priv->rps.min_freq_softlimit;
1191dd75fdc8SChris Wilson 		adj = 0;
1192dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1193dd75fdc8SChris Wilson 		if (adj < 0)
1194dd75fdc8SChris Wilson 			adj *= 2;
1195edcf284bSChris Wilson 		else /* CHV needs even encode values */
1196edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? -2 : -1;
11977e79a683SSagar Arun Kamble 
11987e79a683SSagar Arun Kamble 		if (new_delay <= dev_priv->rps.min_freq_softlimit)
11997e79a683SSagar Arun Kamble 			adj = 0;
1200dd75fdc8SChris Wilson 	} else { /* unknown event */
1201edcf284bSChris Wilson 		adj = 0;
1202dd75fdc8SChris Wilson 	}
12033b8d8d91SJesse Barnes 
1204edcf284bSChris Wilson 	dev_priv->rps.last_adj = adj;
1205edcf284bSChris Wilson 
120679249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
120779249636SBen Widawsky 	 * interrupt
120879249636SBen Widawsky 	 */
1209edcf284bSChris Wilson 	new_delay += adj;
12108d3afd7dSChris Wilson 	new_delay = clamp_t(int, new_delay, min, max);
121127544369SDeepak S 
12129fcee2f7SChris Wilson 	if (intel_set_rps(dev_priv, new_delay)) {
12139fcee2f7SChris Wilson 		DRM_DEBUG_DRIVER("Failed to set new GPU frequency\n");
12149fcee2f7SChris Wilson 		dev_priv->rps.last_adj = 0;
12159fcee2f7SChris Wilson 	}
12163b8d8d91SJesse Barnes 
12174fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
12183b8d8d91SJesse Barnes }
12193b8d8d91SJesse Barnes 
1220e3689190SBen Widawsky 
1221e3689190SBen Widawsky /**
1222e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
1223e3689190SBen Widawsky  * occurred.
1224e3689190SBen Widawsky  * @work: workqueue struct
1225e3689190SBen Widawsky  *
1226e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
1227e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
1228e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
1229e3689190SBen Widawsky  */
1230e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
1231e3689190SBen Widawsky {
12322d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
12332d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, l3_parity.error_work);
1234e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
123535a85ac6SBen Widawsky 	char *parity_event[6];
1236e3689190SBen Widawsky 	uint32_t misccpctl;
123735a85ac6SBen Widawsky 	uint8_t slice = 0;
1238e3689190SBen Widawsky 
1239e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
1240e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
1241e3689190SBen Widawsky 	 * any time we access those registers.
1242e3689190SBen Widawsky 	 */
124391c8a326SChris Wilson 	mutex_lock(&dev_priv->drm.struct_mutex);
1244e3689190SBen Widawsky 
124535a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
124635a85ac6SBen Widawsky 	if (WARN_ON(!dev_priv->l3_parity.which_slice))
124735a85ac6SBen Widawsky 		goto out;
124835a85ac6SBen Widawsky 
1249e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
1250e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1251e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
1252e3689190SBen Widawsky 
125335a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
1254f0f59a00SVille Syrjälä 		i915_reg_t reg;
125535a85ac6SBen Widawsky 
125635a85ac6SBen Widawsky 		slice--;
12572d1fe073SJoonas Lahtinen 		if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv)))
125835a85ac6SBen Widawsky 			break;
125935a85ac6SBen Widawsky 
126035a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
126135a85ac6SBen Widawsky 
12626fa1c5f1SVille Syrjälä 		reg = GEN7_L3CDERRST1(slice);
126335a85ac6SBen Widawsky 
126435a85ac6SBen Widawsky 		error_status = I915_READ(reg);
1265e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
1266e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
1267e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1268e3689190SBen Widawsky 
126935a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
127035a85ac6SBen Widawsky 		POSTING_READ(reg);
1271e3689190SBen Widawsky 
1272cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1273e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1274e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1275e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
127635a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
127735a85ac6SBen Widawsky 		parity_event[5] = NULL;
1278e3689190SBen Widawsky 
127991c8a326SChris Wilson 		kobject_uevent_env(&dev_priv->drm.primary->kdev->kobj,
1280e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
1281e3689190SBen Widawsky 
128235a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
128335a85ac6SBen Widawsky 			  slice, row, bank, subbank);
1284e3689190SBen Widawsky 
128535a85ac6SBen Widawsky 		kfree(parity_event[4]);
1286e3689190SBen Widawsky 		kfree(parity_event[3]);
1287e3689190SBen Widawsky 		kfree(parity_event[2]);
1288e3689190SBen Widawsky 		kfree(parity_event[1]);
1289e3689190SBen Widawsky 	}
1290e3689190SBen Widawsky 
129135a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
129235a85ac6SBen Widawsky 
129335a85ac6SBen Widawsky out:
129435a85ac6SBen Widawsky 	WARN_ON(dev_priv->l3_parity.which_slice);
12954cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
12962d1fe073SJoonas Lahtinen 	gen5_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv));
12974cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
129835a85ac6SBen Widawsky 
129991c8a326SChris Wilson 	mutex_unlock(&dev_priv->drm.struct_mutex);
130035a85ac6SBen Widawsky }
130135a85ac6SBen Widawsky 
1302261e40b8SVille Syrjälä static void ivybridge_parity_error_irq_handler(struct drm_i915_private *dev_priv,
1303261e40b8SVille Syrjälä 					       u32 iir)
1304e3689190SBen Widawsky {
1305261e40b8SVille Syrjälä 	if (!HAS_L3_DPF(dev_priv))
1306e3689190SBen Widawsky 		return;
1307e3689190SBen Widawsky 
1308d0ecd7e2SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1309261e40b8SVille Syrjälä 	gen5_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv));
1310d0ecd7e2SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
1311e3689190SBen Widawsky 
1312261e40b8SVille Syrjälä 	iir &= GT_PARITY_ERROR(dev_priv);
131335a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
131435a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 1;
131535a85ac6SBen Widawsky 
131635a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
131735a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 0;
131835a85ac6SBen Widawsky 
1319a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
1320e3689190SBen Widawsky }
1321e3689190SBen Widawsky 
1322261e40b8SVille Syrjälä static void ilk_gt_irq_handler(struct drm_i915_private *dev_priv,
1323f1af8fc1SPaulo Zanoni 			       u32 gt_iir)
1324f1af8fc1SPaulo Zanoni {
1325f8973c21SChris Wilson 	if (gt_iir & GT_RENDER_USER_INTERRUPT)
13263b3f1650SAkash Goel 		notify_ring(dev_priv->engine[RCS]);
1327f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
13283b3f1650SAkash Goel 		notify_ring(dev_priv->engine[VCS]);
1329f1af8fc1SPaulo Zanoni }
1330f1af8fc1SPaulo Zanoni 
1331261e40b8SVille Syrjälä static void snb_gt_irq_handler(struct drm_i915_private *dev_priv,
1332e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1333e7b4c6b1SDaniel Vetter {
1334f8973c21SChris Wilson 	if (gt_iir & GT_RENDER_USER_INTERRUPT)
13353b3f1650SAkash Goel 		notify_ring(dev_priv->engine[RCS]);
1336cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
13373b3f1650SAkash Goel 		notify_ring(dev_priv->engine[VCS]);
1338cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
13393b3f1650SAkash Goel 		notify_ring(dev_priv->engine[BCS]);
1340e7b4c6b1SDaniel Vetter 
1341cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1342cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
1343aaecdf61SDaniel Vetter 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT))
1344aaecdf61SDaniel Vetter 		DRM_DEBUG("Command parser error, gt_iir 0x%08x\n", gt_iir);
1345e3689190SBen Widawsky 
1346261e40b8SVille Syrjälä 	if (gt_iir & GT_PARITY_ERROR(dev_priv))
1347261e40b8SVille Syrjälä 		ivybridge_parity_error_irq_handler(dev_priv, gt_iir);
1348e7b4c6b1SDaniel Vetter }
1349e7b4c6b1SDaniel Vetter 
1350fbcc1a0cSNick Hoath static __always_inline void
13510bc40be8STvrtko Ursulin gen8_cs_irq_handler(struct intel_engine_cs *engine, u32 iir, int test_shift)
1352fbcc1a0cSNick Hoath {
1353fbcc1a0cSNick Hoath 	if (iir & (GT_RENDER_USER_INTERRUPT << test_shift))
13540bc40be8STvrtko Ursulin 		notify_ring(engine);
1355f747026cSChris Wilson 
1356f747026cSChris Wilson 	if (iir & (GT_CONTEXT_SWITCH_INTERRUPT << test_shift)) {
1357f747026cSChris Wilson 		set_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
1358f747026cSChris Wilson 		tasklet_hi_schedule(&engine->irq_tasklet);
1359f747026cSChris Wilson 	}
1360fbcc1a0cSNick Hoath }
1361fbcc1a0cSNick Hoath 
1362e30e251aSVille Syrjälä static irqreturn_t gen8_gt_irq_ack(struct drm_i915_private *dev_priv,
1363e30e251aSVille Syrjälä 				   u32 master_ctl,
1364e30e251aSVille Syrjälä 				   u32 gt_iir[4])
1365abd58f01SBen Widawsky {
1366abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
1367abd58f01SBen Widawsky 
1368abd58f01SBen Widawsky 	if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
1369e30e251aSVille Syrjälä 		gt_iir[0] = I915_READ_FW(GEN8_GT_IIR(0));
1370e30e251aSVille Syrjälä 		if (gt_iir[0]) {
1371e30e251aSVille Syrjälä 			I915_WRITE_FW(GEN8_GT_IIR(0), gt_iir[0]);
1372abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1373abd58f01SBen Widawsky 		} else
1374abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT0)!\n");
1375abd58f01SBen Widawsky 	}
1376abd58f01SBen Widawsky 
137785f9b5f9SZhao Yakui 	if (master_ctl & (GEN8_GT_VCS1_IRQ | GEN8_GT_VCS2_IRQ)) {
1378e30e251aSVille Syrjälä 		gt_iir[1] = I915_READ_FW(GEN8_GT_IIR(1));
1379e30e251aSVille Syrjälä 		if (gt_iir[1]) {
1380e30e251aSVille Syrjälä 			I915_WRITE_FW(GEN8_GT_IIR(1), gt_iir[1]);
1381abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1382abd58f01SBen Widawsky 		} else
1383abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT1)!\n");
1384abd58f01SBen Widawsky 	}
1385abd58f01SBen Widawsky 
138674cdb337SChris Wilson 	if (master_ctl & GEN8_GT_VECS_IRQ) {
1387e30e251aSVille Syrjälä 		gt_iir[3] = I915_READ_FW(GEN8_GT_IIR(3));
1388e30e251aSVille Syrjälä 		if (gt_iir[3]) {
1389e30e251aSVille Syrjälä 			I915_WRITE_FW(GEN8_GT_IIR(3), gt_iir[3]);
139074cdb337SChris Wilson 			ret = IRQ_HANDLED;
139174cdb337SChris Wilson 		} else
139274cdb337SChris Wilson 			DRM_ERROR("The master control interrupt lied (GT3)!\n");
139374cdb337SChris Wilson 	}
139474cdb337SChris Wilson 
139526705e20SSagar Arun Kamble 	if (master_ctl & (GEN8_GT_PM_IRQ | GEN8_GT_GUC_IRQ)) {
1396e30e251aSVille Syrjälä 		gt_iir[2] = I915_READ_FW(GEN8_GT_IIR(2));
139726705e20SSagar Arun Kamble 		if (gt_iir[2] & (dev_priv->pm_rps_events |
139826705e20SSagar Arun Kamble 				 dev_priv->pm_guc_events)) {
1399cb0d205eSChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(2),
140026705e20SSagar Arun Kamble 				      gt_iir[2] & (dev_priv->pm_rps_events |
140126705e20SSagar Arun Kamble 						   dev_priv->pm_guc_events));
140238cc46d7SOscar Mateo 			ret = IRQ_HANDLED;
14030961021aSBen Widawsky 		} else
14040961021aSBen Widawsky 			DRM_ERROR("The master control interrupt lied (PM)!\n");
14050961021aSBen Widawsky 	}
14060961021aSBen Widawsky 
1407abd58f01SBen Widawsky 	return ret;
1408abd58f01SBen Widawsky }
1409abd58f01SBen Widawsky 
1410e30e251aSVille Syrjälä static void gen8_gt_irq_handler(struct drm_i915_private *dev_priv,
1411e30e251aSVille Syrjälä 				u32 gt_iir[4])
1412e30e251aSVille Syrjälä {
1413e30e251aSVille Syrjälä 	if (gt_iir[0]) {
14143b3f1650SAkash Goel 		gen8_cs_irq_handler(dev_priv->engine[RCS],
1415e30e251aSVille Syrjälä 				    gt_iir[0], GEN8_RCS_IRQ_SHIFT);
14163b3f1650SAkash Goel 		gen8_cs_irq_handler(dev_priv->engine[BCS],
1417e30e251aSVille Syrjälä 				    gt_iir[0], GEN8_BCS_IRQ_SHIFT);
1418e30e251aSVille Syrjälä 	}
1419e30e251aSVille Syrjälä 
1420e30e251aSVille Syrjälä 	if (gt_iir[1]) {
14213b3f1650SAkash Goel 		gen8_cs_irq_handler(dev_priv->engine[VCS],
1422e30e251aSVille Syrjälä 				    gt_iir[1], GEN8_VCS1_IRQ_SHIFT);
14233b3f1650SAkash Goel 		gen8_cs_irq_handler(dev_priv->engine[VCS2],
1424e30e251aSVille Syrjälä 				    gt_iir[1], GEN8_VCS2_IRQ_SHIFT);
1425e30e251aSVille Syrjälä 	}
1426e30e251aSVille Syrjälä 
1427e30e251aSVille Syrjälä 	if (gt_iir[3])
14283b3f1650SAkash Goel 		gen8_cs_irq_handler(dev_priv->engine[VECS],
1429e30e251aSVille Syrjälä 				    gt_iir[3], GEN8_VECS_IRQ_SHIFT);
1430e30e251aSVille Syrjälä 
1431e30e251aSVille Syrjälä 	if (gt_iir[2] & dev_priv->pm_rps_events)
1432e30e251aSVille Syrjälä 		gen6_rps_irq_handler(dev_priv, gt_iir[2]);
143326705e20SSagar Arun Kamble 
143426705e20SSagar Arun Kamble 	if (gt_iir[2] & dev_priv->pm_guc_events)
143526705e20SSagar Arun Kamble 		gen9_guc_irq_handler(dev_priv, gt_iir[2]);
1436e30e251aSVille Syrjälä }
1437e30e251aSVille Syrjälä 
143863c88d22SImre Deak static bool bxt_port_hotplug_long_detect(enum port port, u32 val)
143963c88d22SImre Deak {
144063c88d22SImre Deak 	switch (port) {
144163c88d22SImre Deak 	case PORT_A:
1442195baa06SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
144363c88d22SImre Deak 	case PORT_B:
144463c88d22SImre Deak 		return val & PORTB_HOTPLUG_LONG_DETECT;
144563c88d22SImre Deak 	case PORT_C:
144663c88d22SImre Deak 		return val & PORTC_HOTPLUG_LONG_DETECT;
144763c88d22SImre Deak 	default:
144863c88d22SImre Deak 		return false;
144963c88d22SImre Deak 	}
145063c88d22SImre Deak }
145163c88d22SImre Deak 
14526dbf30ceSVille Syrjälä static bool spt_port_hotplug2_long_detect(enum port port, u32 val)
14536dbf30ceSVille Syrjälä {
14546dbf30ceSVille Syrjälä 	switch (port) {
14556dbf30ceSVille Syrjälä 	case PORT_E:
14566dbf30ceSVille Syrjälä 		return val & PORTE_HOTPLUG_LONG_DETECT;
14576dbf30ceSVille Syrjälä 	default:
14586dbf30ceSVille Syrjälä 		return false;
14596dbf30ceSVille Syrjälä 	}
14606dbf30ceSVille Syrjälä }
14616dbf30ceSVille Syrjälä 
146274c0b395SVille Syrjälä static bool spt_port_hotplug_long_detect(enum port port, u32 val)
146374c0b395SVille Syrjälä {
146474c0b395SVille Syrjälä 	switch (port) {
146574c0b395SVille Syrjälä 	case PORT_A:
146674c0b395SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
146774c0b395SVille Syrjälä 	case PORT_B:
146874c0b395SVille Syrjälä 		return val & PORTB_HOTPLUG_LONG_DETECT;
146974c0b395SVille Syrjälä 	case PORT_C:
147074c0b395SVille Syrjälä 		return val & PORTC_HOTPLUG_LONG_DETECT;
147174c0b395SVille Syrjälä 	case PORT_D:
147274c0b395SVille Syrjälä 		return val & PORTD_HOTPLUG_LONG_DETECT;
147374c0b395SVille Syrjälä 	default:
147474c0b395SVille Syrjälä 		return false;
147574c0b395SVille Syrjälä 	}
147674c0b395SVille Syrjälä }
147774c0b395SVille Syrjälä 
1478e4ce95aaSVille Syrjälä static bool ilk_port_hotplug_long_detect(enum port port, u32 val)
1479e4ce95aaSVille Syrjälä {
1480e4ce95aaSVille Syrjälä 	switch (port) {
1481e4ce95aaSVille Syrjälä 	case PORT_A:
1482e4ce95aaSVille Syrjälä 		return val & DIGITAL_PORTA_HOTPLUG_LONG_DETECT;
1483e4ce95aaSVille Syrjälä 	default:
1484e4ce95aaSVille Syrjälä 		return false;
1485e4ce95aaSVille Syrjälä 	}
1486e4ce95aaSVille Syrjälä }
1487e4ce95aaSVille Syrjälä 
1488676574dfSJani Nikula static bool pch_port_hotplug_long_detect(enum port port, u32 val)
148913cf5504SDave Airlie {
149013cf5504SDave Airlie 	switch (port) {
149113cf5504SDave Airlie 	case PORT_B:
1492676574dfSJani Nikula 		return val & PORTB_HOTPLUG_LONG_DETECT;
149313cf5504SDave Airlie 	case PORT_C:
1494676574dfSJani Nikula 		return val & PORTC_HOTPLUG_LONG_DETECT;
149513cf5504SDave Airlie 	case PORT_D:
1496676574dfSJani Nikula 		return val & PORTD_HOTPLUG_LONG_DETECT;
1497676574dfSJani Nikula 	default:
1498676574dfSJani Nikula 		return false;
149913cf5504SDave Airlie 	}
150013cf5504SDave Airlie }
150113cf5504SDave Airlie 
1502676574dfSJani Nikula static bool i9xx_port_hotplug_long_detect(enum port port, u32 val)
150313cf5504SDave Airlie {
150413cf5504SDave Airlie 	switch (port) {
150513cf5504SDave Airlie 	case PORT_B:
1506676574dfSJani Nikula 		return val & PORTB_HOTPLUG_INT_LONG_PULSE;
150713cf5504SDave Airlie 	case PORT_C:
1508676574dfSJani Nikula 		return val & PORTC_HOTPLUG_INT_LONG_PULSE;
150913cf5504SDave Airlie 	case PORT_D:
1510676574dfSJani Nikula 		return val & PORTD_HOTPLUG_INT_LONG_PULSE;
1511676574dfSJani Nikula 	default:
1512676574dfSJani Nikula 		return false;
151313cf5504SDave Airlie 	}
151413cf5504SDave Airlie }
151513cf5504SDave Airlie 
151642db67d6SVille Syrjälä /*
151742db67d6SVille Syrjälä  * Get a bit mask of pins that have triggered, and which ones may be long.
151842db67d6SVille Syrjälä  * This can be called multiple times with the same masks to accumulate
151942db67d6SVille Syrjälä  * hotplug detection results from several registers.
152042db67d6SVille Syrjälä  *
152142db67d6SVille Syrjälä  * Note that the caller is expected to zero out the masks initially.
152242db67d6SVille Syrjälä  */
1523fd63e2a9SImre Deak static void intel_get_hpd_pins(u32 *pin_mask, u32 *long_mask,
15248c841e57SJani Nikula 			     u32 hotplug_trigger, u32 dig_hotplug_reg,
1525fd63e2a9SImre Deak 			     const u32 hpd[HPD_NUM_PINS],
1526fd63e2a9SImre Deak 			     bool long_pulse_detect(enum port port, u32 val))
1527676574dfSJani Nikula {
15288c841e57SJani Nikula 	enum port port;
1529676574dfSJani Nikula 	int i;
1530676574dfSJani Nikula 
1531676574dfSJani Nikula 	for_each_hpd_pin(i) {
15328c841e57SJani Nikula 		if ((hpd[i] & hotplug_trigger) == 0)
15338c841e57SJani Nikula 			continue;
15348c841e57SJani Nikula 
1535676574dfSJani Nikula 		*pin_mask |= BIT(i);
1536676574dfSJani Nikula 
1537cc24fcdcSImre Deak 		if (!intel_hpd_pin_to_port(i, &port))
1538cc24fcdcSImre Deak 			continue;
1539cc24fcdcSImre Deak 
1540fd63e2a9SImre Deak 		if (long_pulse_detect(port, dig_hotplug_reg))
1541676574dfSJani Nikula 			*long_mask |= BIT(i);
1542676574dfSJani Nikula 	}
1543676574dfSJani Nikula 
1544676574dfSJani Nikula 	DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x, dig 0x%08x, pins 0x%08x\n",
1545676574dfSJani Nikula 			 hotplug_trigger, dig_hotplug_reg, *pin_mask);
1546676574dfSJani Nikula 
1547676574dfSJani Nikula }
1548676574dfSJani Nikula 
154991d14251STvrtko Ursulin static void gmbus_irq_handler(struct drm_i915_private *dev_priv)
1550515ac2bbSDaniel Vetter {
155128c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1552515ac2bbSDaniel Vetter }
1553515ac2bbSDaniel Vetter 
155491d14251STvrtko Ursulin static void dp_aux_irq_handler(struct drm_i915_private *dev_priv)
1555ce99c256SDaniel Vetter {
15569ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1557ce99c256SDaniel Vetter }
1558ce99c256SDaniel Vetter 
15598bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
156091d14251STvrtko Ursulin static void display_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
156191d14251STvrtko Ursulin 					 enum pipe pipe,
1562eba94eb9SDaniel Vetter 					 uint32_t crc0, uint32_t crc1,
1563eba94eb9SDaniel Vetter 					 uint32_t crc2, uint32_t crc3,
15648bc5e955SDaniel Vetter 					 uint32_t crc4)
15658bf1e9f1SShuang He {
15668bf1e9f1SShuang He 	struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
15678bf1e9f1SShuang He 	struct intel_pipe_crc_entry *entry;
15688c6b709dSTomeu Vizoso 	struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
15698c6b709dSTomeu Vizoso 	struct drm_driver *driver = dev_priv->drm.driver;
15708c6b709dSTomeu Vizoso 	uint32_t crcs[5];
1571ac2300d4SDamien Lespiau 	int head, tail;
1572b2c88f5bSDamien Lespiau 
1573d538bbdfSDamien Lespiau 	spin_lock(&pipe_crc->lock);
15748c6b709dSTomeu Vizoso 	if (pipe_crc->source) {
15750c912c79SDamien Lespiau 		if (!pipe_crc->entries) {
1576d538bbdfSDamien Lespiau 			spin_unlock(&pipe_crc->lock);
157734273620SDaniel Vetter 			DRM_DEBUG_KMS("spurious interrupt\n");
15780c912c79SDamien Lespiau 			return;
15790c912c79SDamien Lespiau 		}
15800c912c79SDamien Lespiau 
1581d538bbdfSDamien Lespiau 		head = pipe_crc->head;
1582d538bbdfSDamien Lespiau 		tail = pipe_crc->tail;
1583b2c88f5bSDamien Lespiau 
1584b2c88f5bSDamien Lespiau 		if (CIRC_SPACE(head, tail, INTEL_PIPE_CRC_ENTRIES_NR) < 1) {
1585d538bbdfSDamien Lespiau 			spin_unlock(&pipe_crc->lock);
1586b2c88f5bSDamien Lespiau 			DRM_ERROR("CRC buffer overflowing\n");
1587b2c88f5bSDamien Lespiau 			return;
1588b2c88f5bSDamien Lespiau 		}
1589b2c88f5bSDamien Lespiau 
1590b2c88f5bSDamien Lespiau 		entry = &pipe_crc->entries[head];
15918bf1e9f1SShuang He 
15928c6b709dSTomeu Vizoso 		entry->frame = driver->get_vblank_counter(&dev_priv->drm, pipe);
1593eba94eb9SDaniel Vetter 		entry->crc[0] = crc0;
1594eba94eb9SDaniel Vetter 		entry->crc[1] = crc1;
1595eba94eb9SDaniel Vetter 		entry->crc[2] = crc2;
1596eba94eb9SDaniel Vetter 		entry->crc[3] = crc3;
1597eba94eb9SDaniel Vetter 		entry->crc[4] = crc4;
1598b2c88f5bSDamien Lespiau 
1599b2c88f5bSDamien Lespiau 		head = (head + 1) & (INTEL_PIPE_CRC_ENTRIES_NR - 1);
1600d538bbdfSDamien Lespiau 		pipe_crc->head = head;
1601d538bbdfSDamien Lespiau 
1602d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
160307144428SDamien Lespiau 
160407144428SDamien Lespiau 		wake_up_interruptible(&pipe_crc->wq);
16058c6b709dSTomeu Vizoso 	} else {
16068c6b709dSTomeu Vizoso 		/*
16078c6b709dSTomeu Vizoso 		 * For some not yet identified reason, the first CRC is
16088c6b709dSTomeu Vizoso 		 * bonkers. So let's just wait for the next vblank and read
16098c6b709dSTomeu Vizoso 		 * out the buggy result.
16108c6b709dSTomeu Vizoso 		 *
16118c6b709dSTomeu Vizoso 		 * On CHV sometimes the second CRC is bonkers as well, so
16128c6b709dSTomeu Vizoso 		 * don't trust that one either.
16138c6b709dSTomeu Vizoso 		 */
16148c6b709dSTomeu Vizoso 		if (pipe_crc->skipped == 0 ||
16158c6b709dSTomeu Vizoso 		    (IS_CHERRYVIEW(dev_priv) && pipe_crc->skipped == 1)) {
16168c6b709dSTomeu Vizoso 			pipe_crc->skipped++;
16178c6b709dSTomeu Vizoso 			spin_unlock(&pipe_crc->lock);
16188c6b709dSTomeu Vizoso 			return;
16198c6b709dSTomeu Vizoso 		}
16208c6b709dSTomeu Vizoso 		spin_unlock(&pipe_crc->lock);
16218c6b709dSTomeu Vizoso 		crcs[0] = crc0;
16228c6b709dSTomeu Vizoso 		crcs[1] = crc1;
16238c6b709dSTomeu Vizoso 		crcs[2] = crc2;
16248c6b709dSTomeu Vizoso 		crcs[3] = crc3;
16258c6b709dSTomeu Vizoso 		crcs[4] = crc4;
1626246ee524STomeu Vizoso 		drm_crtc_add_crc_entry(&crtc->base, true,
1627246ee524STomeu Vizoso 				       drm_accurate_vblank_count(&crtc->base),
1628246ee524STomeu Vizoso 				       crcs);
16298c6b709dSTomeu Vizoso 	}
16308bf1e9f1SShuang He }
1631277de95eSDaniel Vetter #else
1632277de95eSDaniel Vetter static inline void
163391d14251STvrtko Ursulin display_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
163491d14251STvrtko Ursulin 			     enum pipe pipe,
1635277de95eSDaniel Vetter 			     uint32_t crc0, uint32_t crc1,
1636277de95eSDaniel Vetter 			     uint32_t crc2, uint32_t crc3,
1637277de95eSDaniel Vetter 			     uint32_t crc4) {}
1638277de95eSDaniel Vetter #endif
1639eba94eb9SDaniel Vetter 
1640277de95eSDaniel Vetter 
164191d14251STvrtko Ursulin static void hsw_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
164291d14251STvrtko Ursulin 				     enum pipe pipe)
16435a69b89fSDaniel Vetter {
164491d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
16455a69b89fSDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
16465a69b89fSDaniel Vetter 				     0, 0, 0, 0);
16475a69b89fSDaniel Vetter }
16485a69b89fSDaniel Vetter 
164991d14251STvrtko Ursulin static void ivb_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
165091d14251STvrtko Ursulin 				     enum pipe pipe)
1651eba94eb9SDaniel Vetter {
165291d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
1653eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1654eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1655eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1656eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
16578bc5e955SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1658eba94eb9SDaniel Vetter }
16595b3a856bSDaniel Vetter 
166091d14251STvrtko Ursulin static void i9xx_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
166191d14251STvrtko Ursulin 				      enum pipe pipe)
16625b3a856bSDaniel Vetter {
16630b5c5ed0SDaniel Vetter 	uint32_t res1, res2;
16640b5c5ed0SDaniel Vetter 
166591d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 3)
16660b5c5ed0SDaniel Vetter 		res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
16670b5c5ed0SDaniel Vetter 	else
16680b5c5ed0SDaniel Vetter 		res1 = 0;
16690b5c5ed0SDaniel Vetter 
167091d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
16710b5c5ed0SDaniel Vetter 		res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
16720b5c5ed0SDaniel Vetter 	else
16730b5c5ed0SDaniel Vetter 		res2 = 0;
16745b3a856bSDaniel Vetter 
167591d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
16760b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_RED(pipe)),
16770b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_GREEN(pipe)),
16780b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_BLUE(pipe)),
16790b5c5ed0SDaniel Vetter 				     res1, res2);
16805b3a856bSDaniel Vetter }
16818bf1e9f1SShuang He 
16821403c0d4SPaulo Zanoni /* The RPS events need forcewake, so we add them to a work queue and mask their
16831403c0d4SPaulo Zanoni  * IMR bits until the work is done. Other interrupts can be processed without
16841403c0d4SPaulo Zanoni  * the work queue. */
16851403c0d4SPaulo Zanoni static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1686baf02a1fSBen Widawsky {
1687a6706b45SDeepak S 	if (pm_iir & dev_priv->pm_rps_events) {
168859cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
1689f4e9af4fSAkash Goel 		gen6_mask_pm_irq(dev_priv, pm_iir & dev_priv->pm_rps_events);
1690d4d70aa5SImre Deak 		if (dev_priv->rps.interrupts_enabled) {
1691d4d70aa5SImre Deak 			dev_priv->rps.pm_iir |= pm_iir & dev_priv->pm_rps_events;
1692c33d247dSChris Wilson 			schedule_work(&dev_priv->rps.work);
169341a05a3aSDaniel Vetter 		}
1694d4d70aa5SImre Deak 		spin_unlock(&dev_priv->irq_lock);
1695d4d70aa5SImre Deak 	}
1696baf02a1fSBen Widawsky 
1697c9a9a268SImre Deak 	if (INTEL_INFO(dev_priv)->gen >= 8)
1698c9a9a268SImre Deak 		return;
1699c9a9a268SImre Deak 
17002d1fe073SJoonas Lahtinen 	if (HAS_VEBOX(dev_priv)) {
170112638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
17023b3f1650SAkash Goel 			notify_ring(dev_priv->engine[VECS]);
170312638c57SBen Widawsky 
1704aaecdf61SDaniel Vetter 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT)
1705aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, pm_iir 0x%08x\n", pm_iir);
170612638c57SBen Widawsky 	}
17071403c0d4SPaulo Zanoni }
1708baf02a1fSBen Widawsky 
170926705e20SSagar Arun Kamble static void gen9_guc_irq_handler(struct drm_i915_private *dev_priv, u32 gt_iir)
171026705e20SSagar Arun Kamble {
171126705e20SSagar Arun Kamble 	if (gt_iir & GEN9_GUC_TO_HOST_INT_EVENT) {
17124100b2abSSagar Arun Kamble 		/* Sample the log buffer flush related bits & clear them out now
17134100b2abSSagar Arun Kamble 		 * itself from the message identity register to minimize the
17144100b2abSSagar Arun Kamble 		 * probability of losing a flush interrupt, when there are back
17154100b2abSSagar Arun Kamble 		 * to back flush interrupts.
17164100b2abSSagar Arun Kamble 		 * There can be a new flush interrupt, for different log buffer
17174100b2abSSagar Arun Kamble 		 * type (like for ISR), whilst Host is handling one (for DPC).
17184100b2abSSagar Arun Kamble 		 * Since same bit is used in message register for ISR & DPC, it
17194100b2abSSagar Arun Kamble 		 * could happen that GuC sets the bit for 2nd interrupt but Host
17204100b2abSSagar Arun Kamble 		 * clears out the bit on handling the 1st interrupt.
17214100b2abSSagar Arun Kamble 		 */
17224100b2abSSagar Arun Kamble 		u32 msg, flush;
17234100b2abSSagar Arun Kamble 
17244100b2abSSagar Arun Kamble 		msg = I915_READ(SOFT_SCRATCH(15));
1725a80bc45fSArkadiusz Hiler 		flush = msg & (INTEL_GUC_RECV_MSG_CRASH_DUMP_POSTED |
1726a80bc45fSArkadiusz Hiler 			       INTEL_GUC_RECV_MSG_FLUSH_LOG_BUFFER);
17274100b2abSSagar Arun Kamble 		if (flush) {
17284100b2abSSagar Arun Kamble 			/* Clear the message bits that are handled */
17294100b2abSSagar Arun Kamble 			I915_WRITE(SOFT_SCRATCH(15), msg & ~flush);
17304100b2abSSagar Arun Kamble 
17314100b2abSSagar Arun Kamble 			/* Handle flush interrupt in bottom half */
17324100b2abSSagar Arun Kamble 			queue_work(dev_priv->guc.log.flush_wq,
17334100b2abSSagar Arun Kamble 				   &dev_priv->guc.log.flush_work);
17345aa1ee4bSAkash Goel 
17355aa1ee4bSAkash Goel 			dev_priv->guc.log.flush_interrupt_count++;
17364100b2abSSagar Arun Kamble 		} else {
17374100b2abSSagar Arun Kamble 			/* Not clearing of unhandled event bits won't result in
17384100b2abSSagar Arun Kamble 			 * re-triggering of the interrupt.
17394100b2abSSagar Arun Kamble 			 */
17404100b2abSSagar Arun Kamble 		}
174126705e20SSagar Arun Kamble 	}
174226705e20SSagar Arun Kamble }
174326705e20SSagar Arun Kamble 
17445a21b665SDaniel Vetter static bool intel_pipe_handle_vblank(struct drm_i915_private *dev_priv,
174591d14251STvrtko Ursulin 				     enum pipe pipe)
17468d7849dbSVille Syrjälä {
17475a21b665SDaniel Vetter 	bool ret;
17485a21b665SDaniel Vetter 
174991c8a326SChris Wilson 	ret = drm_handle_vblank(&dev_priv->drm, pipe);
17505a21b665SDaniel Vetter 	if (ret)
175151cbaf01SMaarten Lankhorst 		intel_finish_page_flip_mmio(dev_priv, pipe);
17525a21b665SDaniel Vetter 
17535a21b665SDaniel Vetter 	return ret;
17548d7849dbSVille Syrjälä }
17558d7849dbSVille Syrjälä 
175691d14251STvrtko Ursulin static void valleyview_pipestat_irq_ack(struct drm_i915_private *dev_priv,
175791d14251STvrtko Ursulin 					u32 iir, u32 pipe_stats[I915_MAX_PIPES])
17587e231dbeSJesse Barnes {
17597e231dbeSJesse Barnes 	int pipe;
17607e231dbeSJesse Barnes 
176158ead0d7SImre Deak 	spin_lock(&dev_priv->irq_lock);
17621ca993d2SVille Syrjälä 
17631ca993d2SVille Syrjälä 	if (!dev_priv->display_irqs_enabled) {
17641ca993d2SVille Syrjälä 		spin_unlock(&dev_priv->irq_lock);
17651ca993d2SVille Syrjälä 		return;
17661ca993d2SVille Syrjälä 	}
17671ca993d2SVille Syrjälä 
1768055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1769f0f59a00SVille Syrjälä 		i915_reg_t reg;
1770bbb5eebfSDaniel Vetter 		u32 mask, iir_bit = 0;
177191d181ddSImre Deak 
1772bbb5eebfSDaniel Vetter 		/*
1773bbb5eebfSDaniel Vetter 		 * PIPESTAT bits get signalled even when the interrupt is
1774bbb5eebfSDaniel Vetter 		 * disabled with the mask bits, and some of the status bits do
1775bbb5eebfSDaniel Vetter 		 * not generate interrupts at all (like the underrun bit). Hence
1776bbb5eebfSDaniel Vetter 		 * we need to be careful that we only handle what we want to
1777bbb5eebfSDaniel Vetter 		 * handle.
1778bbb5eebfSDaniel Vetter 		 */
17790f239f4cSDaniel Vetter 
17800f239f4cSDaniel Vetter 		/* fifo underruns are filterered in the underrun handler. */
17810f239f4cSDaniel Vetter 		mask = PIPE_FIFO_UNDERRUN_STATUS;
1782bbb5eebfSDaniel Vetter 
1783bbb5eebfSDaniel Vetter 		switch (pipe) {
1784bbb5eebfSDaniel Vetter 		case PIPE_A:
1785bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
1786bbb5eebfSDaniel Vetter 			break;
1787bbb5eebfSDaniel Vetter 		case PIPE_B:
1788bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
1789bbb5eebfSDaniel Vetter 			break;
17903278f67fSVille Syrjälä 		case PIPE_C:
17913278f67fSVille Syrjälä 			iir_bit = I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
17923278f67fSVille Syrjälä 			break;
1793bbb5eebfSDaniel Vetter 		}
1794bbb5eebfSDaniel Vetter 		if (iir & iir_bit)
1795bbb5eebfSDaniel Vetter 			mask |= dev_priv->pipestat_irq_mask[pipe];
1796bbb5eebfSDaniel Vetter 
1797bbb5eebfSDaniel Vetter 		if (!mask)
179891d181ddSImre Deak 			continue;
179991d181ddSImre Deak 
180091d181ddSImre Deak 		reg = PIPESTAT(pipe);
1801bbb5eebfSDaniel Vetter 		mask |= PIPESTAT_INT_ENABLE_MASK;
1802bbb5eebfSDaniel Vetter 		pipe_stats[pipe] = I915_READ(reg) & mask;
18037e231dbeSJesse Barnes 
18047e231dbeSJesse Barnes 		/*
18057e231dbeSJesse Barnes 		 * Clear the PIPE*STAT regs before the IIR
18067e231dbeSJesse Barnes 		 */
180791d181ddSImre Deak 		if (pipe_stats[pipe] & (PIPE_FIFO_UNDERRUN_STATUS |
180891d181ddSImre Deak 					PIPESTAT_INT_STATUS_MASK))
18097e231dbeSJesse Barnes 			I915_WRITE(reg, pipe_stats[pipe]);
18107e231dbeSJesse Barnes 	}
181158ead0d7SImre Deak 	spin_unlock(&dev_priv->irq_lock);
18122ecb8ca4SVille Syrjälä }
18132ecb8ca4SVille Syrjälä 
181491d14251STvrtko Ursulin static void valleyview_pipestat_irq_handler(struct drm_i915_private *dev_priv,
18152ecb8ca4SVille Syrjälä 					    u32 pipe_stats[I915_MAX_PIPES])
18162ecb8ca4SVille Syrjälä {
18172ecb8ca4SVille Syrjälä 	enum pipe pipe;
18187e231dbeSJesse Barnes 
1819055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
18205a21b665SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
18215a21b665SDaniel Vetter 		    intel_pipe_handle_vblank(dev_priv, pipe))
18225a21b665SDaniel Vetter 			intel_check_page_flip(dev_priv, pipe);
182331acc7f5SJesse Barnes 
18245251f04eSMaarten Lankhorst 		if (pipe_stats[pipe] & PLANE_FLIP_DONE_INT_STATUS_VLV)
182551cbaf01SMaarten Lankhorst 			intel_finish_page_flip_cs(dev_priv, pipe);
18264356d586SDaniel Vetter 
18274356d586SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
182891d14251STvrtko Ursulin 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
18292d9d2b0bSVille Syrjälä 
18301f7247c0SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
18311f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
183231acc7f5SJesse Barnes 	}
183331acc7f5SJesse Barnes 
1834c1874ed7SImre Deak 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
183591d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
1836c1874ed7SImre Deak }
1837c1874ed7SImre Deak 
18381ae3c34cSVille Syrjälä static u32 i9xx_hpd_irq_ack(struct drm_i915_private *dev_priv)
183916c6c56bSVille Syrjälä {
184016c6c56bSVille Syrjälä 	u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
184116c6c56bSVille Syrjälä 
18421ae3c34cSVille Syrjälä 	if (hotplug_status)
18433ff60f89SOscar Mateo 		I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
18441ae3c34cSVille Syrjälä 
18451ae3c34cSVille Syrjälä 	return hotplug_status;
18461ae3c34cSVille Syrjälä }
18471ae3c34cSVille Syrjälä 
184891d14251STvrtko Ursulin static void i9xx_hpd_irq_handler(struct drm_i915_private *dev_priv,
18491ae3c34cSVille Syrjälä 				 u32 hotplug_status)
18501ae3c34cSVille Syrjälä {
18511ae3c34cSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
18523ff60f89SOscar Mateo 
185391d14251STvrtko Ursulin 	if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
185491d14251STvrtko Ursulin 	    IS_CHERRYVIEW(dev_priv)) {
185516c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
185616c6c56bSVille Syrjälä 
185758f2cf24SVille Syrjälä 		if (hotplug_trigger) {
1858fd63e2a9SImre Deak 			intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
1859fd63e2a9SImre Deak 					   hotplug_trigger, hpd_status_g4x,
1860fd63e2a9SImre Deak 					   i9xx_port_hotplug_long_detect);
186158f2cf24SVille Syrjälä 
186291d14251STvrtko Ursulin 			intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
186358f2cf24SVille Syrjälä 		}
1864369712e8SJani Nikula 
1865369712e8SJani Nikula 		if (hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
186691d14251STvrtko Ursulin 			dp_aux_irq_handler(dev_priv);
186716c6c56bSVille Syrjälä 	} else {
186816c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
186916c6c56bSVille Syrjälä 
187058f2cf24SVille Syrjälä 		if (hotplug_trigger) {
1871fd63e2a9SImre Deak 			intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
18724e3d1e26SVille Syrjälä 					   hotplug_trigger, hpd_status_i915,
1873fd63e2a9SImre Deak 					   i9xx_port_hotplug_long_detect);
187491d14251STvrtko Ursulin 			intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
187516c6c56bSVille Syrjälä 		}
18763ff60f89SOscar Mateo 	}
187758f2cf24SVille Syrjälä }
187816c6c56bSVille Syrjälä 
1879c1874ed7SImre Deak static irqreturn_t valleyview_irq_handler(int irq, void *arg)
1880c1874ed7SImre Deak {
188145a83f84SDaniel Vetter 	struct drm_device *dev = arg;
1882fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
1883c1874ed7SImre Deak 	irqreturn_t ret = IRQ_NONE;
1884c1874ed7SImre Deak 
18852dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
18862dd2a883SImre Deak 		return IRQ_NONE;
18872dd2a883SImre Deak 
18881f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
18891f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
18901f814dacSImre Deak 
18911e1cace9SVille Syrjälä 	do {
18926e814800SVille Syrjälä 		u32 iir, gt_iir, pm_iir;
18932ecb8ca4SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
18941ae3c34cSVille Syrjälä 		u32 hotplug_status = 0;
1895a5e485a9SVille Syrjälä 		u32 ier = 0;
18963ff60f89SOscar Mateo 
1897c1874ed7SImre Deak 		gt_iir = I915_READ(GTIIR);
1898c1874ed7SImre Deak 		pm_iir = I915_READ(GEN6_PMIIR);
18993ff60f89SOscar Mateo 		iir = I915_READ(VLV_IIR);
1900c1874ed7SImre Deak 
1901c1874ed7SImre Deak 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
19021e1cace9SVille Syrjälä 			break;
1903c1874ed7SImre Deak 
1904c1874ed7SImre Deak 		ret = IRQ_HANDLED;
1905c1874ed7SImre Deak 
1906a5e485a9SVille Syrjälä 		/*
1907a5e485a9SVille Syrjälä 		 * Theory on interrupt generation, based on empirical evidence:
1908a5e485a9SVille Syrjälä 		 *
1909a5e485a9SVille Syrjälä 		 * x = ((VLV_IIR & VLV_IER) ||
1910a5e485a9SVille Syrjälä 		 *      (((GT_IIR & GT_IER) || (GEN6_PMIIR & GEN6_PMIER)) &&
1911a5e485a9SVille Syrjälä 		 *       (VLV_MASTER_IER & MASTER_INTERRUPT_ENABLE)));
1912a5e485a9SVille Syrjälä 		 *
1913a5e485a9SVille Syrjälä 		 * A CPU interrupt will only be raised when 'x' has a 0->1 edge.
1914a5e485a9SVille Syrjälä 		 * Hence we clear MASTER_INTERRUPT_ENABLE and VLV_IER to
1915a5e485a9SVille Syrjälä 		 * guarantee the CPU interrupt will be raised again even if we
1916a5e485a9SVille Syrjälä 		 * don't end up clearing all the VLV_IIR, GT_IIR, GEN6_PMIIR
1917a5e485a9SVille Syrjälä 		 * bits this time around.
1918a5e485a9SVille Syrjälä 		 */
19194a0a0202SVille Syrjälä 		I915_WRITE(VLV_MASTER_IER, 0);
1920a5e485a9SVille Syrjälä 		ier = I915_READ(VLV_IER);
1921a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, 0);
19224a0a0202SVille Syrjälä 
19234a0a0202SVille Syrjälä 		if (gt_iir)
19244a0a0202SVille Syrjälä 			I915_WRITE(GTIIR, gt_iir);
19254a0a0202SVille Syrjälä 		if (pm_iir)
19264a0a0202SVille Syrjälä 			I915_WRITE(GEN6_PMIIR, pm_iir);
19274a0a0202SVille Syrjälä 
19287ce4d1f2SVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
19291ae3c34cSVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
19307ce4d1f2SVille Syrjälä 
19313ff60f89SOscar Mateo 		/* Call regardless, as some status bits might not be
19323ff60f89SOscar Mateo 		 * signalled in iir */
193391d14251STvrtko Ursulin 		valleyview_pipestat_irq_ack(dev_priv, iir, pipe_stats);
19347ce4d1f2SVille Syrjälä 
19357ce4d1f2SVille Syrjälä 		/*
19367ce4d1f2SVille Syrjälä 		 * VLV_IIR is single buffered, and reflects the level
19377ce4d1f2SVille Syrjälä 		 * from PIPESTAT/PORT_HOTPLUG_STAT, hence clear it last.
19387ce4d1f2SVille Syrjälä 		 */
19397ce4d1f2SVille Syrjälä 		if (iir)
19407ce4d1f2SVille Syrjälä 			I915_WRITE(VLV_IIR, iir);
19414a0a0202SVille Syrjälä 
1942a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, ier);
19434a0a0202SVille Syrjälä 		I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
19444a0a0202SVille Syrjälä 		POSTING_READ(VLV_MASTER_IER);
19451ae3c34cSVille Syrjälä 
194652894874SVille Syrjälä 		if (gt_iir)
1947261e40b8SVille Syrjälä 			snb_gt_irq_handler(dev_priv, gt_iir);
194852894874SVille Syrjälä 		if (pm_iir)
194952894874SVille Syrjälä 			gen6_rps_irq_handler(dev_priv, pm_iir);
195052894874SVille Syrjälä 
19511ae3c34cSVille Syrjälä 		if (hotplug_status)
195291d14251STvrtko Ursulin 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
19532ecb8ca4SVille Syrjälä 
195491d14251STvrtko Ursulin 		valleyview_pipestat_irq_handler(dev_priv, pipe_stats);
19551e1cace9SVille Syrjälä 	} while (0);
19567e231dbeSJesse Barnes 
19571f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
19581f814dacSImre Deak 
19597e231dbeSJesse Barnes 	return ret;
19607e231dbeSJesse Barnes }
19617e231dbeSJesse Barnes 
196243f328d7SVille Syrjälä static irqreturn_t cherryview_irq_handler(int irq, void *arg)
196343f328d7SVille Syrjälä {
196445a83f84SDaniel Vetter 	struct drm_device *dev = arg;
1965fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
196643f328d7SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
196743f328d7SVille Syrjälä 
19682dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
19692dd2a883SImre Deak 		return IRQ_NONE;
19702dd2a883SImre Deak 
19711f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
19721f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
19731f814dacSImre Deak 
1974579de73bSChris Wilson 	do {
19756e814800SVille Syrjälä 		u32 master_ctl, iir;
1976e30e251aSVille Syrjälä 		u32 gt_iir[4] = {};
19772ecb8ca4SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
19781ae3c34cSVille Syrjälä 		u32 hotplug_status = 0;
1979a5e485a9SVille Syrjälä 		u32 ier = 0;
1980a5e485a9SVille Syrjälä 
19818e5fd599SVille Syrjälä 		master_ctl = I915_READ(GEN8_MASTER_IRQ) & ~GEN8_MASTER_IRQ_CONTROL;
19823278f67fSVille Syrjälä 		iir = I915_READ(VLV_IIR);
19833278f67fSVille Syrjälä 
19843278f67fSVille Syrjälä 		if (master_ctl == 0 && iir == 0)
19858e5fd599SVille Syrjälä 			break;
198643f328d7SVille Syrjälä 
198727b6c122SOscar Mateo 		ret = IRQ_HANDLED;
198827b6c122SOscar Mateo 
1989a5e485a9SVille Syrjälä 		/*
1990a5e485a9SVille Syrjälä 		 * Theory on interrupt generation, based on empirical evidence:
1991a5e485a9SVille Syrjälä 		 *
1992a5e485a9SVille Syrjälä 		 * x = ((VLV_IIR & VLV_IER) ||
1993a5e485a9SVille Syrjälä 		 *      ((GEN8_MASTER_IRQ & ~GEN8_MASTER_IRQ_CONTROL) &&
1994a5e485a9SVille Syrjälä 		 *       (GEN8_MASTER_IRQ & GEN8_MASTER_IRQ_CONTROL)));
1995a5e485a9SVille Syrjälä 		 *
1996a5e485a9SVille Syrjälä 		 * A CPU interrupt will only be raised when 'x' has a 0->1 edge.
1997a5e485a9SVille Syrjälä 		 * Hence we clear GEN8_MASTER_IRQ_CONTROL and VLV_IER to
1998a5e485a9SVille Syrjälä 		 * guarantee the CPU interrupt will be raised again even if we
1999a5e485a9SVille Syrjälä 		 * don't end up clearing all the VLV_IIR and GEN8_MASTER_IRQ_CONTROL
2000a5e485a9SVille Syrjälä 		 * bits this time around.
2001a5e485a9SVille Syrjälä 		 */
200243f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, 0);
2003a5e485a9SVille Syrjälä 		ier = I915_READ(VLV_IER);
2004a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, 0);
200543f328d7SVille Syrjälä 
2006e30e251aSVille Syrjälä 		gen8_gt_irq_ack(dev_priv, master_ctl, gt_iir);
200727b6c122SOscar Mateo 
200827b6c122SOscar Mateo 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
20091ae3c34cSVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
201043f328d7SVille Syrjälä 
201127b6c122SOscar Mateo 		/* Call regardless, as some status bits might not be
201227b6c122SOscar Mateo 		 * signalled in iir */
201391d14251STvrtko Ursulin 		valleyview_pipestat_irq_ack(dev_priv, iir, pipe_stats);
201443f328d7SVille Syrjälä 
20157ce4d1f2SVille Syrjälä 		/*
20167ce4d1f2SVille Syrjälä 		 * VLV_IIR is single buffered, and reflects the level
20177ce4d1f2SVille Syrjälä 		 * from PIPESTAT/PORT_HOTPLUG_STAT, hence clear it last.
20187ce4d1f2SVille Syrjälä 		 */
20197ce4d1f2SVille Syrjälä 		if (iir)
20207ce4d1f2SVille Syrjälä 			I915_WRITE(VLV_IIR, iir);
20217ce4d1f2SVille Syrjälä 
2022a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, ier);
2023e5328c43SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
202443f328d7SVille Syrjälä 		POSTING_READ(GEN8_MASTER_IRQ);
20251ae3c34cSVille Syrjälä 
2026e30e251aSVille Syrjälä 		gen8_gt_irq_handler(dev_priv, gt_iir);
2027e30e251aSVille Syrjälä 
20281ae3c34cSVille Syrjälä 		if (hotplug_status)
202991d14251STvrtko Ursulin 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
20302ecb8ca4SVille Syrjälä 
203191d14251STvrtko Ursulin 		valleyview_pipestat_irq_handler(dev_priv, pipe_stats);
2032579de73bSChris Wilson 	} while (0);
20333278f67fSVille Syrjälä 
20341f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
20351f814dacSImre Deak 
203643f328d7SVille Syrjälä 	return ret;
203743f328d7SVille Syrjälä }
203843f328d7SVille Syrjälä 
203991d14251STvrtko Ursulin static void ibx_hpd_irq_handler(struct drm_i915_private *dev_priv,
204091d14251STvrtko Ursulin 				u32 hotplug_trigger,
204140e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2042776ad806SJesse Barnes {
204342db67d6SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2044776ad806SJesse Barnes 
20456a39d7c9SJani Nikula 	/*
20466a39d7c9SJani Nikula 	 * Somehow the PCH doesn't seem to really ack the interrupt to the CPU
20476a39d7c9SJani Nikula 	 * unless we touch the hotplug register, even if hotplug_trigger is
20486a39d7c9SJani Nikula 	 * zero. Not acking leads to "The master control interrupt lied (SDE)!"
20496a39d7c9SJani Nikula 	 * errors.
20506a39d7c9SJani Nikula 	 */
205113cf5504SDave Airlie 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
20526a39d7c9SJani Nikula 	if (!hotplug_trigger) {
20536a39d7c9SJani Nikula 		u32 mask = PORTA_HOTPLUG_STATUS_MASK |
20546a39d7c9SJani Nikula 			PORTD_HOTPLUG_STATUS_MASK |
20556a39d7c9SJani Nikula 			PORTC_HOTPLUG_STATUS_MASK |
20566a39d7c9SJani Nikula 			PORTB_HOTPLUG_STATUS_MASK;
20576a39d7c9SJani Nikula 		dig_hotplug_reg &= ~mask;
20586a39d7c9SJani Nikula 	}
20596a39d7c9SJani Nikula 
206013cf5504SDave Airlie 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
20616a39d7c9SJani Nikula 	if (!hotplug_trigger)
20626a39d7c9SJani Nikula 		return;
206313cf5504SDave Airlie 
2064fd63e2a9SImre Deak 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
206540e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2066fd63e2a9SImre Deak 			   pch_port_hotplug_long_detect);
206740e56410SVille Syrjälä 
206891d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2069aaf5ec2eSSonika Jindal }
207091d131d2SDaniel Vetter 
207191d14251STvrtko Ursulin static void ibx_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
207240e56410SVille Syrjälä {
207340e56410SVille Syrjälä 	int pipe;
207440e56410SVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
207540e56410SVille Syrjälä 
207691d14251STvrtko Ursulin 	ibx_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ibx);
207740e56410SVille Syrjälä 
2078cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
2079cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
2080776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
2081cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
2082cfc33bf7SVille Syrjälä 				 port_name(port));
2083cfc33bf7SVille Syrjälä 	}
2084776ad806SJesse Barnes 
2085ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
208691d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
2087ce99c256SDaniel Vetter 
2088776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
208991d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
2090776ad806SJesse Barnes 
2091776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
2092776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
2093776ad806SJesse Barnes 
2094776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
2095776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
2096776ad806SJesse Barnes 
2097776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
2098776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
2099776ad806SJesse Barnes 
21009db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
2101055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
21029db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
21039db4a9c7SJesse Barnes 					 pipe_name(pipe),
21049db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
2105776ad806SJesse Barnes 
2106776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
2107776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
2108776ad806SJesse Barnes 
2109776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
2110776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
2111776ad806SJesse Barnes 
2112776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
21131f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
21148664281bSPaulo Zanoni 
21158664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
21161f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
21178664281bSPaulo Zanoni }
21188664281bSPaulo Zanoni 
211991d14251STvrtko Ursulin static void ivb_err_int_handler(struct drm_i915_private *dev_priv)
21208664281bSPaulo Zanoni {
21218664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
21225a69b89fSDaniel Vetter 	enum pipe pipe;
21238664281bSPaulo Zanoni 
2124de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
2125de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
2126de032bf4SPaulo Zanoni 
2127055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
21281f7247c0SDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe))
21291f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
21308664281bSPaulo Zanoni 
21315a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
213291d14251STvrtko Ursulin 			if (IS_IVYBRIDGE(dev_priv))
213391d14251STvrtko Ursulin 				ivb_pipe_crc_irq_handler(dev_priv, pipe);
21345a69b89fSDaniel Vetter 			else
213591d14251STvrtko Ursulin 				hsw_pipe_crc_irq_handler(dev_priv, pipe);
21365a69b89fSDaniel Vetter 		}
21375a69b89fSDaniel Vetter 	}
21388bf1e9f1SShuang He 
21398664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
21408664281bSPaulo Zanoni }
21418664281bSPaulo Zanoni 
214291d14251STvrtko Ursulin static void cpt_serr_int_handler(struct drm_i915_private *dev_priv)
21438664281bSPaulo Zanoni {
21448664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
21458664281bSPaulo Zanoni 
2146de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
2147de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
2148de032bf4SPaulo Zanoni 
21498664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
21501f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
21518664281bSPaulo Zanoni 
21528664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
21531f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
21548664281bSPaulo Zanoni 
21558664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
21561f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_C);
21578664281bSPaulo Zanoni 
21588664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
2159776ad806SJesse Barnes }
2160776ad806SJesse Barnes 
216191d14251STvrtko Ursulin static void cpt_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
216223e81d69SAdam Jackson {
216323e81d69SAdam Jackson 	int pipe;
21646dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
2165aaf5ec2eSSonika Jindal 
216691d14251STvrtko Ursulin 	ibx_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_cpt);
216791d131d2SDaniel Vetter 
2168cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
2169cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
217023e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
2171cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
2172cfc33bf7SVille Syrjälä 				 port_name(port));
2173cfc33bf7SVille Syrjälä 	}
217423e81d69SAdam Jackson 
217523e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
217691d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
217723e81d69SAdam Jackson 
217823e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
217991d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
218023e81d69SAdam Jackson 
218123e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
218223e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
218323e81d69SAdam Jackson 
218423e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
218523e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
218623e81d69SAdam Jackson 
218723e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
2188055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
218923e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
219023e81d69SAdam Jackson 					 pipe_name(pipe),
219123e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
21928664281bSPaulo Zanoni 
21938664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
219491d14251STvrtko Ursulin 		cpt_serr_int_handler(dev_priv);
219523e81d69SAdam Jackson }
219623e81d69SAdam Jackson 
219791d14251STvrtko Ursulin static void spt_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
21986dbf30ceSVille Syrjälä {
21996dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_SPT &
22006dbf30ceSVille Syrjälä 		~SDE_PORTE_HOTPLUG_SPT;
22016dbf30ceSVille Syrjälä 	u32 hotplug2_trigger = pch_iir & SDE_PORTE_HOTPLUG_SPT;
22026dbf30ceSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
22036dbf30ceSVille Syrjälä 
22046dbf30ceSVille Syrjälä 	if (hotplug_trigger) {
22056dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
22066dbf30ceSVille Syrjälä 
22076dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
22086dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
22096dbf30ceSVille Syrjälä 
22106dbf30ceSVille Syrjälä 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
22116dbf30ceSVille Syrjälä 				   dig_hotplug_reg, hpd_spt,
221274c0b395SVille Syrjälä 				   spt_port_hotplug_long_detect);
22136dbf30ceSVille Syrjälä 	}
22146dbf30ceSVille Syrjälä 
22156dbf30ceSVille Syrjälä 	if (hotplug2_trigger) {
22166dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
22176dbf30ceSVille Syrjälä 
22186dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG2);
22196dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG2, dig_hotplug_reg);
22206dbf30ceSVille Syrjälä 
22216dbf30ceSVille Syrjälä 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug2_trigger,
22226dbf30ceSVille Syrjälä 				   dig_hotplug_reg, hpd_spt,
22236dbf30ceSVille Syrjälä 				   spt_port_hotplug2_long_detect);
22246dbf30ceSVille Syrjälä 	}
22256dbf30ceSVille Syrjälä 
22266dbf30ceSVille Syrjälä 	if (pin_mask)
222791d14251STvrtko Ursulin 		intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
22286dbf30ceSVille Syrjälä 
22296dbf30ceSVille Syrjälä 	if (pch_iir & SDE_GMBUS_CPT)
223091d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
22316dbf30ceSVille Syrjälä }
22326dbf30ceSVille Syrjälä 
223391d14251STvrtko Ursulin static void ilk_hpd_irq_handler(struct drm_i915_private *dev_priv,
223491d14251STvrtko Ursulin 				u32 hotplug_trigger,
223540e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2236c008bc6eSPaulo Zanoni {
2237e4ce95aaSVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2238e4ce95aaSVille Syrjälä 
2239e4ce95aaSVille Syrjälä 	dig_hotplug_reg = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
2240e4ce95aaSVille Syrjälä 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, dig_hotplug_reg);
2241e4ce95aaSVille Syrjälä 
2242e4ce95aaSVille Syrjälä 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
224340e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2244e4ce95aaSVille Syrjälä 			   ilk_port_hotplug_long_detect);
224540e56410SVille Syrjälä 
224691d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2247e4ce95aaSVille Syrjälä }
2248c008bc6eSPaulo Zanoni 
224991d14251STvrtko Ursulin static void ilk_display_irq_handler(struct drm_i915_private *dev_priv,
225091d14251STvrtko Ursulin 				    u32 de_iir)
225140e56410SVille Syrjälä {
225240e56410SVille Syrjälä 	enum pipe pipe;
225340e56410SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG;
225440e56410SVille Syrjälä 
225540e56410SVille Syrjälä 	if (hotplug_trigger)
225691d14251STvrtko Ursulin 		ilk_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ilk);
225740e56410SVille Syrjälä 
2258c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
225991d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
2260c008bc6eSPaulo Zanoni 
2261c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
226291d14251STvrtko Ursulin 		intel_opregion_asle_intr(dev_priv);
2263c008bc6eSPaulo Zanoni 
2264c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
2265c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
2266c008bc6eSPaulo Zanoni 
2267055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
22685a21b665SDaniel Vetter 		if (de_iir & DE_PIPE_VBLANK(pipe) &&
22695a21b665SDaniel Vetter 		    intel_pipe_handle_vblank(dev_priv, pipe))
22705a21b665SDaniel Vetter 			intel_check_page_flip(dev_priv, pipe);
2271c008bc6eSPaulo Zanoni 
227240da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
22731f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
2274c008bc6eSPaulo Zanoni 
227540da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
227691d14251STvrtko Ursulin 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
22775b3a856bSDaniel Vetter 
227840da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
22795251f04eSMaarten Lankhorst 		if (de_iir & DE_PLANE_FLIP_DONE(pipe))
228051cbaf01SMaarten Lankhorst 			intel_finish_page_flip_cs(dev_priv, pipe);
2281c008bc6eSPaulo Zanoni 	}
2282c008bc6eSPaulo Zanoni 
2283c008bc6eSPaulo Zanoni 	/* check event from PCH */
2284c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
2285c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
2286c008bc6eSPaulo Zanoni 
228791d14251STvrtko Ursulin 		if (HAS_PCH_CPT(dev_priv))
228891d14251STvrtko Ursulin 			cpt_irq_handler(dev_priv, pch_iir);
2289c008bc6eSPaulo Zanoni 		else
229091d14251STvrtko Ursulin 			ibx_irq_handler(dev_priv, pch_iir);
2291c008bc6eSPaulo Zanoni 
2292c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
2293c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
2294c008bc6eSPaulo Zanoni 	}
2295c008bc6eSPaulo Zanoni 
229691d14251STvrtko Ursulin 	if (IS_GEN5(dev_priv) && de_iir & DE_PCU_EVENT)
229791d14251STvrtko Ursulin 		ironlake_rps_change_irq_handler(dev_priv);
2298c008bc6eSPaulo Zanoni }
2299c008bc6eSPaulo Zanoni 
230091d14251STvrtko Ursulin static void ivb_display_irq_handler(struct drm_i915_private *dev_priv,
230191d14251STvrtko Ursulin 				    u32 de_iir)
23029719fb98SPaulo Zanoni {
230307d27e20SDamien Lespiau 	enum pipe pipe;
230423bb4cb5SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG_IVB;
230523bb4cb5SVille Syrjälä 
230640e56410SVille Syrjälä 	if (hotplug_trigger)
230791d14251STvrtko Ursulin 		ilk_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ivb);
23089719fb98SPaulo Zanoni 
23099719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
231091d14251STvrtko Ursulin 		ivb_err_int_handler(dev_priv);
23119719fb98SPaulo Zanoni 
23129719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
231391d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
23149719fb98SPaulo Zanoni 
23159719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
231691d14251STvrtko Ursulin 		intel_opregion_asle_intr(dev_priv);
23179719fb98SPaulo Zanoni 
2318055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
23195a21b665SDaniel Vetter 		if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)) &&
23205a21b665SDaniel Vetter 		    intel_pipe_handle_vblank(dev_priv, pipe))
23215a21b665SDaniel Vetter 			intel_check_page_flip(dev_priv, pipe);
232240da17c2SDaniel Vetter 
232340da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
23245251f04eSMaarten Lankhorst 		if (de_iir & DE_PLANE_FLIP_DONE_IVB(pipe))
232551cbaf01SMaarten Lankhorst 			intel_finish_page_flip_cs(dev_priv, pipe);
23269719fb98SPaulo Zanoni 	}
23279719fb98SPaulo Zanoni 
23289719fb98SPaulo Zanoni 	/* check event from PCH */
232991d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv) && (de_iir & DE_PCH_EVENT_IVB)) {
23309719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
23319719fb98SPaulo Zanoni 
233291d14251STvrtko Ursulin 		cpt_irq_handler(dev_priv, pch_iir);
23339719fb98SPaulo Zanoni 
23349719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
23359719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
23369719fb98SPaulo Zanoni 	}
23379719fb98SPaulo Zanoni }
23389719fb98SPaulo Zanoni 
233972c90f62SOscar Mateo /*
234072c90f62SOscar Mateo  * To handle irqs with the minimum potential races with fresh interrupts, we:
234172c90f62SOscar Mateo  * 1 - Disable Master Interrupt Control.
234272c90f62SOscar Mateo  * 2 - Find the source(s) of the interrupt.
234372c90f62SOscar Mateo  * 3 - Clear the Interrupt Identity bits (IIR).
234472c90f62SOscar Mateo  * 4 - Process the interrupt(s) that had bits set in the IIRs.
234572c90f62SOscar Mateo  * 5 - Re-enable Master Interrupt Control.
234672c90f62SOscar Mateo  */
2347f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
2348b1f14ad0SJesse Barnes {
234945a83f84SDaniel Vetter 	struct drm_device *dev = arg;
2350fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2351f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
23520e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
2353b1f14ad0SJesse Barnes 
23542dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
23552dd2a883SImre Deak 		return IRQ_NONE;
23562dd2a883SImre Deak 
23571f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
23581f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
23591f814dacSImre Deak 
2360b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
2361b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
2362b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
236323a78516SPaulo Zanoni 	POSTING_READ(DEIER);
23640e43406bSChris Wilson 
236544498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
236644498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
236744498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
236844498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
236944498aeaSPaulo Zanoni 	 * due to its back queue). */
237091d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv)) {
237144498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
237244498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
237344498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2374ab5c608bSBen Widawsky 	}
237544498aeaSPaulo Zanoni 
237672c90f62SOscar Mateo 	/* Find, clear, then process each source of interrupt */
237772c90f62SOscar Mateo 
23780e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
23790e43406bSChris Wilson 	if (gt_iir) {
238072c90f62SOscar Mateo 		I915_WRITE(GTIIR, gt_iir);
238172c90f62SOscar Mateo 		ret = IRQ_HANDLED;
238291d14251STvrtko Ursulin 		if (INTEL_GEN(dev_priv) >= 6)
2383261e40b8SVille Syrjälä 			snb_gt_irq_handler(dev_priv, gt_iir);
2384d8fc8a47SPaulo Zanoni 		else
2385261e40b8SVille Syrjälä 			ilk_gt_irq_handler(dev_priv, gt_iir);
23860e43406bSChris Wilson 	}
2387b1f14ad0SJesse Barnes 
2388b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
23890e43406bSChris Wilson 	if (de_iir) {
239072c90f62SOscar Mateo 		I915_WRITE(DEIIR, de_iir);
239172c90f62SOscar Mateo 		ret = IRQ_HANDLED;
239291d14251STvrtko Ursulin 		if (INTEL_GEN(dev_priv) >= 7)
239391d14251STvrtko Ursulin 			ivb_display_irq_handler(dev_priv, de_iir);
2394f1af8fc1SPaulo Zanoni 		else
239591d14251STvrtko Ursulin 			ilk_display_irq_handler(dev_priv, de_iir);
23960e43406bSChris Wilson 	}
23970e43406bSChris Wilson 
239891d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 6) {
2399f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
24000e43406bSChris Wilson 		if (pm_iir) {
2401b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
24020e43406bSChris Wilson 			ret = IRQ_HANDLED;
240372c90f62SOscar Mateo 			gen6_rps_irq_handler(dev_priv, pm_iir);
24040e43406bSChris Wilson 		}
2405f1af8fc1SPaulo Zanoni 	}
2406b1f14ad0SJesse Barnes 
2407b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
2408b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
240991d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv)) {
241044498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
241144498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2412ab5c608bSBen Widawsky 	}
2413b1f14ad0SJesse Barnes 
24141f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
24151f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
24161f814dacSImre Deak 
2417b1f14ad0SJesse Barnes 	return ret;
2418b1f14ad0SJesse Barnes }
2419b1f14ad0SJesse Barnes 
242091d14251STvrtko Ursulin static void bxt_hpd_irq_handler(struct drm_i915_private *dev_priv,
242191d14251STvrtko Ursulin 				u32 hotplug_trigger,
242240e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2423d04a492dSShashank Sharma {
2424cebd87a0SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2425d04a492dSShashank Sharma 
2426a52bb15bSVille Syrjälä 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
2427a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
2428d04a492dSShashank Sharma 
2429cebd87a0SVille Syrjälä 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
243040e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2431cebd87a0SVille Syrjälä 			   bxt_port_hotplug_long_detect);
243240e56410SVille Syrjälä 
243391d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2434d04a492dSShashank Sharma }
2435d04a492dSShashank Sharma 
2436f11a0f46STvrtko Ursulin static irqreturn_t
2437f11a0f46STvrtko Ursulin gen8_de_irq_handler(struct drm_i915_private *dev_priv, u32 master_ctl)
2438abd58f01SBen Widawsky {
2439abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
2440f11a0f46STvrtko Ursulin 	u32 iir;
2441c42664ccSDaniel Vetter 	enum pipe pipe;
244288e04703SJesse Barnes 
2443abd58f01SBen Widawsky 	if (master_ctl & GEN8_DE_MISC_IRQ) {
2444e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_MISC_IIR);
2445e32192e1STvrtko Ursulin 		if (iir) {
2446e32192e1STvrtko Ursulin 			I915_WRITE(GEN8_DE_MISC_IIR, iir);
2447abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
2448e32192e1STvrtko Ursulin 			if (iir & GEN8_DE_MISC_GSE)
244991d14251STvrtko Ursulin 				intel_opregion_asle_intr(dev_priv);
245038cc46d7SOscar Mateo 			else
245138cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Misc interrupt\n");
2452abd58f01SBen Widawsky 		}
245338cc46d7SOscar Mateo 		else
245438cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
2455abd58f01SBen Widawsky 	}
2456abd58f01SBen Widawsky 
24576d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
2458e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_PORT_IIR);
2459e32192e1STvrtko Ursulin 		if (iir) {
2460e32192e1STvrtko Ursulin 			u32 tmp_mask;
2461d04a492dSShashank Sharma 			bool found = false;
2462cebd87a0SVille Syrjälä 
2463e32192e1STvrtko Ursulin 			I915_WRITE(GEN8_DE_PORT_IIR, iir);
24646d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
246588e04703SJesse Barnes 
2466e32192e1STvrtko Ursulin 			tmp_mask = GEN8_AUX_CHANNEL_A;
2467e32192e1STvrtko Ursulin 			if (INTEL_INFO(dev_priv)->gen >= 9)
2468e32192e1STvrtko Ursulin 				tmp_mask |= GEN9_AUX_CHANNEL_B |
2469e32192e1STvrtko Ursulin 					    GEN9_AUX_CHANNEL_C |
2470e32192e1STvrtko Ursulin 					    GEN9_AUX_CHANNEL_D;
2471e32192e1STvrtko Ursulin 
2472e32192e1STvrtko Ursulin 			if (iir & tmp_mask) {
247391d14251STvrtko Ursulin 				dp_aux_irq_handler(dev_priv);
2474d04a492dSShashank Sharma 				found = true;
2475d04a492dSShashank Sharma 			}
2476d04a492dSShashank Sharma 
2477cc3f90f0SAnder Conselvan de Oliveira 			if (IS_GEN9_LP(dev_priv)) {
2478e32192e1STvrtko Ursulin 				tmp_mask = iir & BXT_DE_PORT_HOTPLUG_MASK;
2479e32192e1STvrtko Ursulin 				if (tmp_mask) {
248091d14251STvrtko Ursulin 					bxt_hpd_irq_handler(dev_priv, tmp_mask,
248191d14251STvrtko Ursulin 							    hpd_bxt);
2482d04a492dSShashank Sharma 					found = true;
2483d04a492dSShashank Sharma 				}
2484e32192e1STvrtko Ursulin 			} else if (IS_BROADWELL(dev_priv)) {
2485e32192e1STvrtko Ursulin 				tmp_mask = iir & GEN8_PORT_DP_A_HOTPLUG;
2486e32192e1STvrtko Ursulin 				if (tmp_mask) {
248791d14251STvrtko Ursulin 					ilk_hpd_irq_handler(dev_priv,
248891d14251STvrtko Ursulin 							    tmp_mask, hpd_bdw);
2489e32192e1STvrtko Ursulin 					found = true;
2490e32192e1STvrtko Ursulin 				}
2491e32192e1STvrtko Ursulin 			}
2492d04a492dSShashank Sharma 
2493cc3f90f0SAnder Conselvan de Oliveira 			if (IS_GEN9_LP(dev_priv) && (iir & BXT_DE_PORT_GMBUS)) {
249491d14251STvrtko Ursulin 				gmbus_irq_handler(dev_priv);
24959e63743eSShashank Sharma 				found = true;
24969e63743eSShashank Sharma 			}
24979e63743eSShashank Sharma 
2498d04a492dSShashank Sharma 			if (!found)
249938cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Port interrupt\n");
25006d766f02SDaniel Vetter 		}
250138cc46d7SOscar Mateo 		else
250238cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
25036d766f02SDaniel Vetter 	}
25046d766f02SDaniel Vetter 
2505055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2506e32192e1STvrtko Ursulin 		u32 flip_done, fault_errors;
2507abd58f01SBen Widawsky 
2508c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2509c42664ccSDaniel Vetter 			continue;
2510c42664ccSDaniel Vetter 
2511e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
2512e32192e1STvrtko Ursulin 		if (!iir) {
2513e32192e1STvrtko Ursulin 			DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
2514e32192e1STvrtko Ursulin 			continue;
2515e32192e1STvrtko Ursulin 		}
2516770de83dSDamien Lespiau 
2517e32192e1STvrtko Ursulin 		ret = IRQ_HANDLED;
2518e32192e1STvrtko Ursulin 		I915_WRITE(GEN8_DE_PIPE_IIR(pipe), iir);
2519e32192e1STvrtko Ursulin 
25205a21b665SDaniel Vetter 		if (iir & GEN8_PIPE_VBLANK &&
25215a21b665SDaniel Vetter 		    intel_pipe_handle_vblank(dev_priv, pipe))
25225a21b665SDaniel Vetter 			intel_check_page_flip(dev_priv, pipe);
2523abd58f01SBen Widawsky 
2524e32192e1STvrtko Ursulin 		flip_done = iir;
2525b4834a50SRodrigo Vivi 		if (INTEL_INFO(dev_priv)->gen >= 9)
2526e32192e1STvrtko Ursulin 			flip_done &= GEN9_PIPE_PLANE1_FLIP_DONE;
2527770de83dSDamien Lespiau 		else
2528e32192e1STvrtko Ursulin 			flip_done &= GEN8_PIPE_PRIMARY_FLIP_DONE;
2529770de83dSDamien Lespiau 
25305251f04eSMaarten Lankhorst 		if (flip_done)
253151cbaf01SMaarten Lankhorst 			intel_finish_page_flip_cs(dev_priv, pipe);
2532abd58f01SBen Widawsky 
2533e32192e1STvrtko Ursulin 		if (iir & GEN8_PIPE_CDCLK_CRC_DONE)
253491d14251STvrtko Ursulin 			hsw_pipe_crc_irq_handler(dev_priv, pipe);
25350fbe7870SDaniel Vetter 
2536e32192e1STvrtko Ursulin 		if (iir & GEN8_PIPE_FIFO_UNDERRUN)
2537e32192e1STvrtko Ursulin 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
253838d83c96SDaniel Vetter 
2539e32192e1STvrtko Ursulin 		fault_errors = iir;
2540b4834a50SRodrigo Vivi 		if (INTEL_INFO(dev_priv)->gen >= 9)
2541e32192e1STvrtko Ursulin 			fault_errors &= GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
2542770de83dSDamien Lespiau 		else
2543e32192e1STvrtko Ursulin 			fault_errors &= GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
2544770de83dSDamien Lespiau 
2545770de83dSDamien Lespiau 		if (fault_errors)
25461353ec38STvrtko Ursulin 			DRM_ERROR("Fault errors on pipe %c: 0x%08x\n",
254730100f2bSDaniel Vetter 				  pipe_name(pipe),
2548e32192e1STvrtko Ursulin 				  fault_errors);
2549abd58f01SBen Widawsky 	}
2550abd58f01SBen Widawsky 
255191d14251STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv) && !HAS_PCH_NOP(dev_priv) &&
2552266ea3d9SShashank Sharma 	    master_ctl & GEN8_DE_PCH_IRQ) {
255392d03a80SDaniel Vetter 		/*
255492d03a80SDaniel Vetter 		 * FIXME(BDW): Assume for now that the new interrupt handling
255592d03a80SDaniel Vetter 		 * scheme also closed the SDE interrupt handling race we've seen
255692d03a80SDaniel Vetter 		 * on older pch-split platforms. But this needs testing.
255792d03a80SDaniel Vetter 		 */
2558e32192e1STvrtko Ursulin 		iir = I915_READ(SDEIIR);
2559e32192e1STvrtko Ursulin 		if (iir) {
2560e32192e1STvrtko Ursulin 			I915_WRITE(SDEIIR, iir);
256192d03a80SDaniel Vetter 			ret = IRQ_HANDLED;
25626dbf30ceSVille Syrjälä 
256322dea0beSRodrigo Vivi 			if (HAS_PCH_SPT(dev_priv) || HAS_PCH_KBP(dev_priv))
256491d14251STvrtko Ursulin 				spt_irq_handler(dev_priv, iir);
25656dbf30ceSVille Syrjälä 			else
256691d14251STvrtko Ursulin 				cpt_irq_handler(dev_priv, iir);
25672dfb0b81SJani Nikula 		} else {
25682dfb0b81SJani Nikula 			/*
25692dfb0b81SJani Nikula 			 * Like on previous PCH there seems to be something
25702dfb0b81SJani Nikula 			 * fishy going on with forwarding PCH interrupts.
25712dfb0b81SJani Nikula 			 */
25722dfb0b81SJani Nikula 			DRM_DEBUG_DRIVER("The master control interrupt lied (SDE)!\n");
25732dfb0b81SJani Nikula 		}
257492d03a80SDaniel Vetter 	}
257592d03a80SDaniel Vetter 
2576f11a0f46STvrtko Ursulin 	return ret;
2577f11a0f46STvrtko Ursulin }
2578f11a0f46STvrtko Ursulin 
2579f11a0f46STvrtko Ursulin static irqreturn_t gen8_irq_handler(int irq, void *arg)
2580f11a0f46STvrtko Ursulin {
2581f11a0f46STvrtko Ursulin 	struct drm_device *dev = arg;
2582fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2583f11a0f46STvrtko Ursulin 	u32 master_ctl;
2584e30e251aSVille Syrjälä 	u32 gt_iir[4] = {};
2585f11a0f46STvrtko Ursulin 	irqreturn_t ret;
2586f11a0f46STvrtko Ursulin 
2587f11a0f46STvrtko Ursulin 	if (!intel_irqs_enabled(dev_priv))
2588f11a0f46STvrtko Ursulin 		return IRQ_NONE;
2589f11a0f46STvrtko Ursulin 
2590f11a0f46STvrtko Ursulin 	master_ctl = I915_READ_FW(GEN8_MASTER_IRQ);
2591f11a0f46STvrtko Ursulin 	master_ctl &= ~GEN8_MASTER_IRQ_CONTROL;
2592f11a0f46STvrtko Ursulin 	if (!master_ctl)
2593f11a0f46STvrtko Ursulin 		return IRQ_NONE;
2594f11a0f46STvrtko Ursulin 
2595f11a0f46STvrtko Ursulin 	I915_WRITE_FW(GEN8_MASTER_IRQ, 0);
2596f11a0f46STvrtko Ursulin 
2597f11a0f46STvrtko Ursulin 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
2598f11a0f46STvrtko Ursulin 	disable_rpm_wakeref_asserts(dev_priv);
2599f11a0f46STvrtko Ursulin 
2600f11a0f46STvrtko Ursulin 	/* Find, clear, then process each source of interrupt */
2601e30e251aSVille Syrjälä 	ret = gen8_gt_irq_ack(dev_priv, master_ctl, gt_iir);
2602e30e251aSVille Syrjälä 	gen8_gt_irq_handler(dev_priv, gt_iir);
2603f11a0f46STvrtko Ursulin 	ret |= gen8_de_irq_handler(dev_priv, master_ctl);
2604f11a0f46STvrtko Ursulin 
2605cb0d205eSChris Wilson 	I915_WRITE_FW(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
2606cb0d205eSChris Wilson 	POSTING_READ_FW(GEN8_MASTER_IRQ);
2607abd58f01SBen Widawsky 
26081f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
26091f814dacSImre Deak 
2610abd58f01SBen Widawsky 	return ret;
2611abd58f01SBen Widawsky }
2612abd58f01SBen Widawsky 
26131f15b76fSChris Wilson static void i915_error_wake_up(struct drm_i915_private *dev_priv)
261417e1df07SDaniel Vetter {
261517e1df07SDaniel Vetter 	/*
261617e1df07SDaniel Vetter 	 * Notify all waiters for GPU completion events that reset state has
261717e1df07SDaniel Vetter 	 * been changed, and that they need to restart their wait after
261817e1df07SDaniel Vetter 	 * checking for potential errors (and bail out to drop locks if there is
261917e1df07SDaniel Vetter 	 * a gpu reset pending so that i915_error_work_func can acquire them).
262017e1df07SDaniel Vetter 	 */
262117e1df07SDaniel Vetter 
262217e1df07SDaniel Vetter 	/* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
26231f15b76fSChris Wilson 	wake_up_all(&dev_priv->gpu_error.wait_queue);
262417e1df07SDaniel Vetter 
262517e1df07SDaniel Vetter 	/* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
262617e1df07SDaniel Vetter 	wake_up_all(&dev_priv->pending_flip_queue);
262717e1df07SDaniel Vetter }
262817e1df07SDaniel Vetter 
26298a905236SJesse Barnes /**
2630b8d24a06SMika Kuoppala  * i915_reset_and_wakeup - do process context error handling work
263114bb2c11STvrtko Ursulin  * @dev_priv: i915 device private
26328a905236SJesse Barnes  *
26338a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
26348a905236SJesse Barnes  * was detected.
26358a905236SJesse Barnes  */
2636c033666aSChris Wilson static void i915_reset_and_wakeup(struct drm_i915_private *dev_priv)
26378a905236SJesse Barnes {
263891c8a326SChris Wilson 	struct kobject *kobj = &dev_priv->drm.primary->kdev->kobj;
2639cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
2640cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
2641cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
26428a905236SJesse Barnes 
2643c033666aSChris Wilson 	kobject_uevent_env(kobj, KOBJ_CHANGE, error_event);
26448a905236SJesse Barnes 
264544d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("resetting chip\n");
2646c033666aSChris Wilson 	kobject_uevent_env(kobj, KOBJ_CHANGE, reset_event);
26471f83fee0SDaniel Vetter 
264817e1df07SDaniel Vetter 	/*
2649f454c694SImre Deak 	 * In most cases it's guaranteed that we get here with an RPM
2650f454c694SImre Deak 	 * reference held, for example because there is a pending GPU
2651f454c694SImre Deak 	 * request that won't finish until the reset is done. This
2652f454c694SImre Deak 	 * isn't the case at least when we get here by doing a
2653f454c694SImre Deak 	 * simulated reset via debugs, so get an RPM reference.
2654f454c694SImre Deak 	 */
2655f454c694SImre Deak 	intel_runtime_pm_get(dev_priv);
2656c033666aSChris Wilson 	intel_prepare_reset(dev_priv);
26577514747dSVille Syrjälä 
2658780f262aSChris Wilson 	do {
2659f454c694SImre Deak 		/*
266017e1df07SDaniel Vetter 		 * All state reset _must_ be completed before we update the
266117e1df07SDaniel Vetter 		 * reset counter, for otherwise waiters might miss the reset
266217e1df07SDaniel Vetter 		 * pending state and not properly drop locks, resulting in
266317e1df07SDaniel Vetter 		 * deadlocks with the reset work.
266417e1df07SDaniel Vetter 		 */
2665780f262aSChris Wilson 		if (mutex_trylock(&dev_priv->drm.struct_mutex)) {
2666780f262aSChris Wilson 			i915_reset(dev_priv);
2667221fe799SChris Wilson 			mutex_unlock(&dev_priv->drm.struct_mutex);
2668780f262aSChris Wilson 		}
2669780f262aSChris Wilson 
2670780f262aSChris Wilson 		/* We need to wait for anyone holding the lock to wakeup */
2671780f262aSChris Wilson 	} while (wait_on_bit_timeout(&dev_priv->gpu_error.flags,
2672780f262aSChris Wilson 				     I915_RESET_IN_PROGRESS,
2673780f262aSChris Wilson 				     TASK_UNINTERRUPTIBLE,
2674780f262aSChris Wilson 				     HZ));
2675f69061beSDaniel Vetter 
2676c033666aSChris Wilson 	intel_finish_reset(dev_priv);
2677f454c694SImre Deak 	intel_runtime_pm_put(dev_priv);
2678f454c694SImre Deak 
2679780f262aSChris Wilson 	if (!test_bit(I915_WEDGED, &dev_priv->gpu_error.flags))
2680c033666aSChris Wilson 		kobject_uevent_env(kobj,
2681f69061beSDaniel Vetter 				   KOBJ_CHANGE, reset_done_event);
26821f83fee0SDaniel Vetter 
268317e1df07SDaniel Vetter 	/*
268417e1df07SDaniel Vetter 	 * Note: The wake_up also serves as a memory barrier so that
26858af29b0cSChris Wilson 	 * waiters see the updated value of the dev_priv->gpu_error.
268617e1df07SDaniel Vetter 	 */
26871f15b76fSChris Wilson 	wake_up_all(&dev_priv->gpu_error.reset_queue);
2688f316a42cSBen Gamari }
26898a905236SJesse Barnes 
2690d636951eSBen Widawsky static inline void
2691d636951eSBen Widawsky i915_err_print_instdone(struct drm_i915_private *dev_priv,
2692d636951eSBen Widawsky 			struct intel_instdone *instdone)
2693d636951eSBen Widawsky {
2694f9e61372SBen Widawsky 	int slice;
2695f9e61372SBen Widawsky 	int subslice;
2696f9e61372SBen Widawsky 
2697d636951eSBen Widawsky 	pr_err("  INSTDONE: 0x%08x\n", instdone->instdone);
2698d636951eSBen Widawsky 
2699d636951eSBen Widawsky 	if (INTEL_GEN(dev_priv) <= 3)
2700d636951eSBen Widawsky 		return;
2701d636951eSBen Widawsky 
2702d636951eSBen Widawsky 	pr_err("  SC_INSTDONE: 0x%08x\n", instdone->slice_common);
2703d636951eSBen Widawsky 
2704d636951eSBen Widawsky 	if (INTEL_GEN(dev_priv) <= 6)
2705d636951eSBen Widawsky 		return;
2706d636951eSBen Widawsky 
2707f9e61372SBen Widawsky 	for_each_instdone_slice_subslice(dev_priv, slice, subslice)
2708f9e61372SBen Widawsky 		pr_err("  SAMPLER_INSTDONE[%d][%d]: 0x%08x\n",
2709f9e61372SBen Widawsky 		       slice, subslice, instdone->sampler[slice][subslice]);
2710f9e61372SBen Widawsky 
2711f9e61372SBen Widawsky 	for_each_instdone_slice_subslice(dev_priv, slice, subslice)
2712f9e61372SBen Widawsky 		pr_err("  ROW_INSTDONE[%d][%d]: 0x%08x\n",
2713f9e61372SBen Widawsky 		       slice, subslice, instdone->row[slice][subslice]);
2714d636951eSBen Widawsky }
2715d636951eSBen Widawsky 
2716eaa14c24SChris Wilson static void i915_clear_error_registers(struct drm_i915_private *dev_priv)
2717c0e09200SDave Airlie {
2718eaa14c24SChris Wilson 	u32 eir;
271963eeaf38SJesse Barnes 
2720eaa14c24SChris Wilson 	if (!IS_GEN2(dev_priv))
2721eaa14c24SChris Wilson 		I915_WRITE(PGTBL_ER, I915_READ(PGTBL_ER));
272263eeaf38SJesse Barnes 
2723eaa14c24SChris Wilson 	if (INTEL_GEN(dev_priv) < 4)
2724eaa14c24SChris Wilson 		I915_WRITE(IPEIR, I915_READ(IPEIR));
2725eaa14c24SChris Wilson 	else
2726eaa14c24SChris Wilson 		I915_WRITE(IPEIR_I965, I915_READ(IPEIR_I965));
27278a905236SJesse Barnes 
2728eaa14c24SChris Wilson 	I915_WRITE(EIR, I915_READ(EIR));
272963eeaf38SJesse Barnes 	eir = I915_READ(EIR);
273063eeaf38SJesse Barnes 	if (eir) {
273163eeaf38SJesse Barnes 		/*
273263eeaf38SJesse Barnes 		 * some errors might have become stuck,
273363eeaf38SJesse Barnes 		 * mask them.
273463eeaf38SJesse Barnes 		 */
2735eaa14c24SChris Wilson 		DRM_DEBUG_DRIVER("EIR stuck: 0x%08x, masking\n", eir);
273663eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
273763eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
273863eeaf38SJesse Barnes 	}
273935aed2e6SChris Wilson }
274035aed2e6SChris Wilson 
274135aed2e6SChris Wilson /**
2742b8d24a06SMika Kuoppala  * i915_handle_error - handle a gpu error
274314bb2c11STvrtko Ursulin  * @dev_priv: i915 device private
274414b730fcSarun.siluvery@linux.intel.com  * @engine_mask: mask representing engines that are hung
274587c390b6SMichel Thierry  * @fmt: Error message format string
274687c390b6SMichel Thierry  *
2747aafd8581SJavier Martinez Canillas  * Do some basic checking of register state at error time and
274835aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
274935aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
275035aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
275135aed2e6SChris Wilson  * of a ring dump etc.).
275235aed2e6SChris Wilson  */
2753c033666aSChris Wilson void i915_handle_error(struct drm_i915_private *dev_priv,
2754c033666aSChris Wilson 		       u32 engine_mask,
275558174462SMika Kuoppala 		       const char *fmt, ...)
275635aed2e6SChris Wilson {
275758174462SMika Kuoppala 	va_list args;
275858174462SMika Kuoppala 	char error_msg[80];
275935aed2e6SChris Wilson 
276058174462SMika Kuoppala 	va_start(args, fmt);
276158174462SMika Kuoppala 	vscnprintf(error_msg, sizeof(error_msg), fmt, args);
276258174462SMika Kuoppala 	va_end(args);
276358174462SMika Kuoppala 
2764c033666aSChris Wilson 	i915_capture_error_state(dev_priv, engine_mask, error_msg);
2765eaa14c24SChris Wilson 	i915_clear_error_registers(dev_priv);
27668a905236SJesse Barnes 
27678af29b0cSChris Wilson 	if (!engine_mask)
27688af29b0cSChris Wilson 		return;
27698af29b0cSChris Wilson 
27708af29b0cSChris Wilson 	if (test_and_set_bit(I915_RESET_IN_PROGRESS,
27718af29b0cSChris Wilson 			     &dev_priv->gpu_error.flags))
27728af29b0cSChris Wilson 		return;
2773ba1234d1SBen Gamari 
277411ed50ecSBen Gamari 	/*
2775b8d24a06SMika Kuoppala 	 * Wakeup waiting processes so that the reset function
2776b8d24a06SMika Kuoppala 	 * i915_reset_and_wakeup doesn't deadlock trying to grab
2777b8d24a06SMika Kuoppala 	 * various locks. By bumping the reset counter first, the woken
277817e1df07SDaniel Vetter 	 * processes will see a reset in progress and back off,
277917e1df07SDaniel Vetter 	 * releasing their locks and then wait for the reset completion.
278017e1df07SDaniel Vetter 	 * We must do this for _all_ gpu waiters that might hold locks
278117e1df07SDaniel Vetter 	 * that the reset work needs to acquire.
278217e1df07SDaniel Vetter 	 *
27838af29b0cSChris Wilson 	 * Note: The wake_up also provides a memory barrier to ensure that the
27848af29b0cSChris Wilson 	 * waiters see the updated value of the reset flags.
278511ed50ecSBen Gamari 	 */
27861f15b76fSChris Wilson 	i915_error_wake_up(dev_priv);
278711ed50ecSBen Gamari 
2788c033666aSChris Wilson 	i915_reset_and_wakeup(dev_priv);
27898a905236SJesse Barnes }
27908a905236SJesse Barnes 
279142f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
279242f52ef8SKeith Packard  * we use as a pipe index
279342f52ef8SKeith Packard  */
279486e83e35SChris Wilson static int i8xx_enable_vblank(struct drm_device *dev, unsigned int pipe)
27950a3e67a4SJesse Barnes {
2796fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2797e9d21d7fSKeith Packard 	unsigned long irqflags;
279871e0ffa5SJesse Barnes 
27991ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
280086e83e35SChris Wilson 	i915_enable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_STATUS);
280186e83e35SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
280286e83e35SChris Wilson 
280386e83e35SChris Wilson 	return 0;
280486e83e35SChris Wilson }
280586e83e35SChris Wilson 
280686e83e35SChris Wilson static int i965_enable_vblank(struct drm_device *dev, unsigned int pipe)
280786e83e35SChris Wilson {
280886e83e35SChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
280986e83e35SChris Wilson 	unsigned long irqflags;
281086e83e35SChris Wilson 
281186e83e35SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
28127c463586SKeith Packard 	i915_enable_pipestat(dev_priv, pipe,
2813755e9019SImre Deak 			     PIPE_START_VBLANK_INTERRUPT_STATUS);
28141ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
28158692d00eSChris Wilson 
28160a3e67a4SJesse Barnes 	return 0;
28170a3e67a4SJesse Barnes }
28180a3e67a4SJesse Barnes 
281988e72717SThierry Reding static int ironlake_enable_vblank(struct drm_device *dev, unsigned int pipe)
2820f796cf8fSJesse Barnes {
2821fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2822f796cf8fSJesse Barnes 	unsigned long irqflags;
282355b8f2a7STvrtko Ursulin 	uint32_t bit = INTEL_GEN(dev_priv) >= 7 ?
282486e83e35SChris Wilson 		DE_PIPE_VBLANK_IVB(pipe) : DE_PIPE_VBLANK(pipe);
2825f796cf8fSJesse Barnes 
2826f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2827fbdedaeaSVille Syrjälä 	ilk_enable_display_irq(dev_priv, bit);
2828b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2829b1f14ad0SJesse Barnes 
2830b1f14ad0SJesse Barnes 	return 0;
2831b1f14ad0SJesse Barnes }
2832b1f14ad0SJesse Barnes 
283388e72717SThierry Reding static int gen8_enable_vblank(struct drm_device *dev, unsigned int pipe)
2834abd58f01SBen Widawsky {
2835fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2836abd58f01SBen Widawsky 	unsigned long irqflags;
2837abd58f01SBen Widawsky 
2838abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2839013d3752SVille Syrjälä 	bdw_enable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
2840abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2841013d3752SVille Syrjälä 
2842abd58f01SBen Widawsky 	return 0;
2843abd58f01SBen Widawsky }
2844abd58f01SBen Widawsky 
284542f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
284642f52ef8SKeith Packard  * we use as a pipe index
284742f52ef8SKeith Packard  */
284886e83e35SChris Wilson static void i8xx_disable_vblank(struct drm_device *dev, unsigned int pipe)
284986e83e35SChris Wilson {
285086e83e35SChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
285186e83e35SChris Wilson 	unsigned long irqflags;
285286e83e35SChris Wilson 
285386e83e35SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
285486e83e35SChris Wilson 	i915_disable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_STATUS);
285586e83e35SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
285686e83e35SChris Wilson }
285786e83e35SChris Wilson 
285886e83e35SChris Wilson static void i965_disable_vblank(struct drm_device *dev, unsigned int pipe)
28590a3e67a4SJesse Barnes {
2860fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2861e9d21d7fSKeith Packard 	unsigned long irqflags;
28620a3e67a4SJesse Barnes 
28631ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
28647c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
2865755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
28661ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
28670a3e67a4SJesse Barnes }
28680a3e67a4SJesse Barnes 
286988e72717SThierry Reding static void ironlake_disable_vblank(struct drm_device *dev, unsigned int pipe)
2870f796cf8fSJesse Barnes {
2871fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2872f796cf8fSJesse Barnes 	unsigned long irqflags;
287355b8f2a7STvrtko Ursulin 	uint32_t bit = INTEL_GEN(dev_priv) >= 7 ?
287486e83e35SChris Wilson 		DE_PIPE_VBLANK_IVB(pipe) : DE_PIPE_VBLANK(pipe);
2875f796cf8fSJesse Barnes 
2876f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2877fbdedaeaSVille Syrjälä 	ilk_disable_display_irq(dev_priv, bit);
2878b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2879b1f14ad0SJesse Barnes }
2880b1f14ad0SJesse Barnes 
288188e72717SThierry Reding static void gen8_disable_vblank(struct drm_device *dev, unsigned int pipe)
2882abd58f01SBen Widawsky {
2883fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2884abd58f01SBen Widawsky 	unsigned long irqflags;
2885abd58f01SBen Widawsky 
2886abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2887013d3752SVille Syrjälä 	bdw_disable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
2888abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2889abd58f01SBen Widawsky }
2890abd58f01SBen Widawsky 
2891b243f530STvrtko Ursulin static void ibx_irq_reset(struct drm_i915_private *dev_priv)
289291738a95SPaulo Zanoni {
28936e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
289491738a95SPaulo Zanoni 		return;
289591738a95SPaulo Zanoni 
2896f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(SDE);
2897105b122eSPaulo Zanoni 
28986e266956STvrtko Ursulin 	if (HAS_PCH_CPT(dev_priv) || HAS_PCH_LPT(dev_priv))
2899105b122eSPaulo Zanoni 		I915_WRITE(SERR_INT, 0xffffffff);
2900622364b6SPaulo Zanoni }
2901105b122eSPaulo Zanoni 
290291738a95SPaulo Zanoni /*
2903622364b6SPaulo Zanoni  * SDEIER is also touched by the interrupt handler to work around missed PCH
2904622364b6SPaulo Zanoni  * interrupts. Hence we can't update it after the interrupt handler is enabled -
2905622364b6SPaulo Zanoni  * instead we unconditionally enable all PCH interrupt sources here, but then
2906622364b6SPaulo Zanoni  * only unmask them as needed with SDEIMR.
2907622364b6SPaulo Zanoni  *
2908622364b6SPaulo Zanoni  * This function needs to be called before interrupts are enabled.
290991738a95SPaulo Zanoni  */
2910622364b6SPaulo Zanoni static void ibx_irq_pre_postinstall(struct drm_device *dev)
2911622364b6SPaulo Zanoni {
2912fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2913622364b6SPaulo Zanoni 
29146e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
2915622364b6SPaulo Zanoni 		return;
2916622364b6SPaulo Zanoni 
2917622364b6SPaulo Zanoni 	WARN_ON(I915_READ(SDEIER) != 0);
291891738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
291991738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
292091738a95SPaulo Zanoni }
292191738a95SPaulo Zanoni 
2922b243f530STvrtko Ursulin static void gen5_gt_irq_reset(struct drm_i915_private *dev_priv)
2923d18ea1b5SDaniel Vetter {
2924f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GT);
2925b243f530STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 6)
2926f86f3fb0SPaulo Zanoni 		GEN5_IRQ_RESET(GEN6_PM);
2927d18ea1b5SDaniel Vetter }
2928d18ea1b5SDaniel Vetter 
292970591a41SVille Syrjälä static void vlv_display_irq_reset(struct drm_i915_private *dev_priv)
293070591a41SVille Syrjälä {
293170591a41SVille Syrjälä 	enum pipe pipe;
293270591a41SVille Syrjälä 
293371b8b41dSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
293471b8b41dSVille Syrjälä 		I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
293571b8b41dSVille Syrjälä 	else
293671b8b41dSVille Syrjälä 		I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
293771b8b41dSVille Syrjälä 
2938ad22d106SVille Syrjälä 	i915_hotplug_interrupt_update_locked(dev_priv, 0xffffffff, 0);
293970591a41SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
294070591a41SVille Syrjälä 
2941ad22d106SVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
2942ad22d106SVille Syrjälä 		I915_WRITE(PIPESTAT(pipe),
2943ad22d106SVille Syrjälä 			   PIPE_FIFO_UNDERRUN_STATUS |
2944ad22d106SVille Syrjälä 			   PIPESTAT_INT_STATUS_MASK);
2945ad22d106SVille Syrjälä 		dev_priv->pipestat_irq_mask[pipe] = 0;
2946ad22d106SVille Syrjälä 	}
294770591a41SVille Syrjälä 
294870591a41SVille Syrjälä 	GEN5_IRQ_RESET(VLV_);
2949ad22d106SVille Syrjälä 	dev_priv->irq_mask = ~0;
295070591a41SVille Syrjälä }
295170591a41SVille Syrjälä 
29528bb61306SVille Syrjälä static void vlv_display_irq_postinstall(struct drm_i915_private *dev_priv)
29538bb61306SVille Syrjälä {
29548bb61306SVille Syrjälä 	u32 pipestat_mask;
29559ab981f2SVille Syrjälä 	u32 enable_mask;
29568bb61306SVille Syrjälä 	enum pipe pipe;
29578bb61306SVille Syrjälä 
29588bb61306SVille Syrjälä 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
29598bb61306SVille Syrjälä 			PIPE_CRC_DONE_INTERRUPT_STATUS;
29608bb61306SVille Syrjälä 
29618bb61306SVille Syrjälä 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
29628bb61306SVille Syrjälä 	for_each_pipe(dev_priv, pipe)
29638bb61306SVille Syrjälä 		i915_enable_pipestat(dev_priv, pipe, pipestat_mask);
29648bb61306SVille Syrjälä 
29659ab981f2SVille Syrjälä 	enable_mask = I915_DISPLAY_PORT_INTERRUPT |
29668bb61306SVille Syrjälä 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
29678bb61306SVille Syrjälä 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
29688bb61306SVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
29699ab981f2SVille Syrjälä 		enable_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
29706b7eafc1SVille Syrjälä 
29716b7eafc1SVille Syrjälä 	WARN_ON(dev_priv->irq_mask != ~0);
29726b7eafc1SVille Syrjälä 
29739ab981f2SVille Syrjälä 	dev_priv->irq_mask = ~enable_mask;
29748bb61306SVille Syrjälä 
29759ab981f2SVille Syrjälä 	GEN5_IRQ_INIT(VLV_, dev_priv->irq_mask, enable_mask);
29768bb61306SVille Syrjälä }
29778bb61306SVille Syrjälä 
29788bb61306SVille Syrjälä /* drm_dma.h hooks
29798bb61306SVille Syrjälä */
29808bb61306SVille Syrjälä static void ironlake_irq_reset(struct drm_device *dev)
29818bb61306SVille Syrjälä {
2982fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
29838bb61306SVille Syrjälä 
29848bb61306SVille Syrjälä 	I915_WRITE(HWSTAM, 0xffffffff);
29858bb61306SVille Syrjälä 
29868bb61306SVille Syrjälä 	GEN5_IRQ_RESET(DE);
29875db94019STvrtko Ursulin 	if (IS_GEN7(dev_priv))
29888bb61306SVille Syrjälä 		I915_WRITE(GEN7_ERR_INT, 0xffffffff);
29898bb61306SVille Syrjälä 
2990b243f530STvrtko Ursulin 	gen5_gt_irq_reset(dev_priv);
29918bb61306SVille Syrjälä 
2992b243f530STvrtko Ursulin 	ibx_irq_reset(dev_priv);
29938bb61306SVille Syrjälä }
29948bb61306SVille Syrjälä 
29957e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
29967e231dbeSJesse Barnes {
2997fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
29987e231dbeSJesse Barnes 
299934c7b8a7SVille Syrjälä 	I915_WRITE(VLV_MASTER_IER, 0);
300034c7b8a7SVille Syrjälä 	POSTING_READ(VLV_MASTER_IER);
300134c7b8a7SVille Syrjälä 
3002b243f530STvrtko Ursulin 	gen5_gt_irq_reset(dev_priv);
30037e231dbeSJesse Barnes 
3004ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
30059918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
300670591a41SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3007ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
30087e231dbeSJesse Barnes }
30097e231dbeSJesse Barnes 
3010d6e3cca3SDaniel Vetter static void gen8_gt_irq_reset(struct drm_i915_private *dev_priv)
3011d6e3cca3SDaniel Vetter {
3012d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 0);
3013d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 1);
3014d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 2);
3015d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 3);
3016d6e3cca3SDaniel Vetter }
3017d6e3cca3SDaniel Vetter 
3018823f6b38SPaulo Zanoni static void gen8_irq_reset(struct drm_device *dev)
3019abd58f01SBen Widawsky {
3020fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3021abd58f01SBen Widawsky 	int pipe;
3022abd58f01SBen Widawsky 
3023abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
3024abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3025abd58f01SBen Widawsky 
3026d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
3027abd58f01SBen Widawsky 
3028055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3029f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3030813bde43SPaulo Zanoni 						   POWER_DOMAIN_PIPE(pipe)))
3031f86f3fb0SPaulo Zanoni 			GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3032abd58f01SBen Widawsky 
3033f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_PORT_);
3034f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_MISC_);
3035f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_PCU_);
3036abd58f01SBen Widawsky 
30376e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
3038b243f530STvrtko Ursulin 		ibx_irq_reset(dev_priv);
3039abd58f01SBen Widawsky }
3040abd58f01SBen Widawsky 
30414c6c03beSDamien Lespiau void gen8_irq_power_well_post_enable(struct drm_i915_private *dev_priv,
30424c6c03beSDamien Lespiau 				     unsigned int pipe_mask)
3043d49bdb0eSPaulo Zanoni {
30441180e206SPaulo Zanoni 	uint32_t extra_ier = GEN8_PIPE_VBLANK | GEN8_PIPE_FIFO_UNDERRUN;
30456831f3e3SVille Syrjälä 	enum pipe pipe;
3046d49bdb0eSPaulo Zanoni 
304713321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
30486831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
30496831f3e3SVille Syrjälä 		GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
30506831f3e3SVille Syrjälä 				  dev_priv->de_irq_mask[pipe],
30516831f3e3SVille Syrjälä 				  ~dev_priv->de_irq_mask[pipe] | extra_ier);
305213321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3053d49bdb0eSPaulo Zanoni }
3054d49bdb0eSPaulo Zanoni 
3055aae8ba84SVille Syrjälä void gen8_irq_power_well_pre_disable(struct drm_i915_private *dev_priv,
3056aae8ba84SVille Syrjälä 				     unsigned int pipe_mask)
3057aae8ba84SVille Syrjälä {
30586831f3e3SVille Syrjälä 	enum pipe pipe;
30596831f3e3SVille Syrjälä 
3060aae8ba84SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
30616831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
30626831f3e3SVille Syrjälä 		GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3063aae8ba84SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3064aae8ba84SVille Syrjälä 
3065aae8ba84SVille Syrjälä 	/* make sure we're done processing display irqs */
306691c8a326SChris Wilson 	synchronize_irq(dev_priv->drm.irq);
3067aae8ba84SVille Syrjälä }
3068aae8ba84SVille Syrjälä 
306943f328d7SVille Syrjälä static void cherryview_irq_preinstall(struct drm_device *dev)
307043f328d7SVille Syrjälä {
3071fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
307243f328d7SVille Syrjälä 
307343f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
307443f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
307543f328d7SVille Syrjälä 
3076d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
307743f328d7SVille Syrjälä 
307843f328d7SVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
307943f328d7SVille Syrjälä 
3080ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
30819918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
308270591a41SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3083ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
308443f328d7SVille Syrjälä }
308543f328d7SVille Syrjälä 
308691d14251STvrtko Ursulin static u32 intel_hpd_enabled_irqs(struct drm_i915_private *dev_priv,
308787a02106SVille Syrjälä 				  const u32 hpd[HPD_NUM_PINS])
308887a02106SVille Syrjälä {
308987a02106SVille Syrjälä 	struct intel_encoder *encoder;
309087a02106SVille Syrjälä 	u32 enabled_irqs = 0;
309187a02106SVille Syrjälä 
309291c8a326SChris Wilson 	for_each_intel_encoder(&dev_priv->drm, encoder)
309387a02106SVille Syrjälä 		if (dev_priv->hotplug.stats[encoder->hpd_pin].state == HPD_ENABLED)
309487a02106SVille Syrjälä 			enabled_irqs |= hpd[encoder->hpd_pin];
309587a02106SVille Syrjälä 
309687a02106SVille Syrjälä 	return enabled_irqs;
309787a02106SVille Syrjälä }
309887a02106SVille Syrjälä 
30991a56b1a2SImre Deak static void ibx_hpd_detection_setup(struct drm_i915_private *dev_priv)
31001a56b1a2SImre Deak {
31011a56b1a2SImre Deak 	u32 hotplug;
31021a56b1a2SImre Deak 
31031a56b1a2SImre Deak 	/*
31041a56b1a2SImre Deak 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
31051a56b1a2SImre Deak 	 * duration to 2ms (which is the minimum in the Display Port spec).
31061a56b1a2SImre Deak 	 * The pulse duration bits are reserved on LPT+.
31071a56b1a2SImre Deak 	 */
31081a56b1a2SImre Deak 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
31091a56b1a2SImre Deak 	hotplug &= ~(PORTB_PULSE_DURATION_MASK |
31101a56b1a2SImre Deak 		     PORTC_PULSE_DURATION_MASK |
31111a56b1a2SImre Deak 		     PORTD_PULSE_DURATION_MASK);
31121a56b1a2SImre Deak 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
31131a56b1a2SImre Deak 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
31141a56b1a2SImre Deak 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
31151a56b1a2SImre Deak 	/*
31161a56b1a2SImre Deak 	 * When CPU and PCH are on the same package, port A
31171a56b1a2SImre Deak 	 * HPD must be enabled in both north and south.
31181a56b1a2SImre Deak 	 */
31191a56b1a2SImre Deak 	if (HAS_PCH_LPT_LP(dev_priv))
31201a56b1a2SImre Deak 		hotplug |= PORTA_HOTPLUG_ENABLE;
31211a56b1a2SImre Deak 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
31221a56b1a2SImre Deak }
31231a56b1a2SImre Deak 
312491d14251STvrtko Ursulin static void ibx_hpd_irq_setup(struct drm_i915_private *dev_priv)
312582a28bcfSDaniel Vetter {
31261a56b1a2SImre Deak 	u32 hotplug_irqs, enabled_irqs;
312782a28bcfSDaniel Vetter 
312891d14251STvrtko Ursulin 	if (HAS_PCH_IBX(dev_priv)) {
3129fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
313091d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ibx);
313182a28bcfSDaniel Vetter 	} else {
3132fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
313391d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_cpt);
313482a28bcfSDaniel Vetter 	}
313582a28bcfSDaniel Vetter 
3136fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
313782a28bcfSDaniel Vetter 
31381a56b1a2SImre Deak 	ibx_hpd_detection_setup(dev_priv);
31396dbf30ceSVille Syrjälä }
314026951cafSXiong Zhang 
31417fff8126SImre Deak static void spt_hpd_detection_setup(struct drm_i915_private *dev_priv)
31427fff8126SImre Deak {
31437fff8126SImre Deak 	u32 hotplug;
31447fff8126SImre Deak 
31457fff8126SImre Deak 	/* Enable digital hotplug on the PCH */
31467fff8126SImre Deak 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
31477fff8126SImre Deak 	hotplug |= PORTA_HOTPLUG_ENABLE |
31487fff8126SImre Deak 		   PORTB_HOTPLUG_ENABLE |
31497fff8126SImre Deak 		   PORTC_HOTPLUG_ENABLE |
31507fff8126SImre Deak 		   PORTD_HOTPLUG_ENABLE;
31517fff8126SImre Deak 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
31527fff8126SImre Deak 
31537fff8126SImre Deak 	hotplug = I915_READ(PCH_PORT_HOTPLUG2);
31547fff8126SImre Deak 	hotplug |= PORTE_HOTPLUG_ENABLE;
31557fff8126SImre Deak 	I915_WRITE(PCH_PORT_HOTPLUG2, hotplug);
31567fff8126SImre Deak }
31577fff8126SImre Deak 
315891d14251STvrtko Ursulin static void spt_hpd_irq_setup(struct drm_i915_private *dev_priv)
31596dbf30ceSVille Syrjälä {
31607fff8126SImre Deak 	u32 hotplug_irqs, enabled_irqs;
31616dbf30ceSVille Syrjälä 
31626dbf30ceSVille Syrjälä 	hotplug_irqs = SDE_HOTPLUG_MASK_SPT;
316391d14251STvrtko Ursulin 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_spt);
31646dbf30ceSVille Syrjälä 
31656dbf30ceSVille Syrjälä 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
31666dbf30ceSVille Syrjälä 
31677fff8126SImre Deak 	spt_hpd_detection_setup(dev_priv);
316826951cafSXiong Zhang }
31697fe0b973SKeith Packard 
31701a56b1a2SImre Deak static void ilk_hpd_detection_setup(struct drm_i915_private *dev_priv)
31711a56b1a2SImre Deak {
31721a56b1a2SImre Deak 	u32 hotplug;
31731a56b1a2SImre Deak 
31741a56b1a2SImre Deak 	/*
31751a56b1a2SImre Deak 	 * Enable digital hotplug on the CPU, and configure the DP short pulse
31761a56b1a2SImre Deak 	 * duration to 2ms (which is the minimum in the Display Port spec)
31771a56b1a2SImre Deak 	 * The pulse duration bits are reserved on HSW+.
31781a56b1a2SImre Deak 	 */
31791a56b1a2SImre Deak 	hotplug = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
31801a56b1a2SImre Deak 	hotplug &= ~DIGITAL_PORTA_PULSE_DURATION_MASK;
31811a56b1a2SImre Deak 	hotplug |= DIGITAL_PORTA_HOTPLUG_ENABLE |
31821a56b1a2SImre Deak 		   DIGITAL_PORTA_PULSE_DURATION_2ms;
31831a56b1a2SImre Deak 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, hotplug);
31841a56b1a2SImre Deak }
31851a56b1a2SImre Deak 
318691d14251STvrtko Ursulin static void ilk_hpd_irq_setup(struct drm_i915_private *dev_priv)
3187e4ce95aaSVille Syrjälä {
31881a56b1a2SImre Deak 	u32 hotplug_irqs, enabled_irqs;
3189e4ce95aaSVille Syrjälä 
319091d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 8) {
31913a3b3c7dSVille Syrjälä 		hotplug_irqs = GEN8_PORT_DP_A_HOTPLUG;
319291d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_bdw);
31933a3b3c7dSVille Syrjälä 
31943a3b3c7dSVille Syrjälä 		bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
319591d14251STvrtko Ursulin 	} else if (INTEL_GEN(dev_priv) >= 7) {
319623bb4cb5SVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG_IVB;
319791d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ivb);
31983a3b3c7dSVille Syrjälä 
31993a3b3c7dSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
320023bb4cb5SVille Syrjälä 	} else {
3201e4ce95aaSVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG;
320291d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ilk);
3203e4ce95aaSVille Syrjälä 
3204e4ce95aaSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
32053a3b3c7dSVille Syrjälä 	}
3206e4ce95aaSVille Syrjälä 
32071a56b1a2SImre Deak 	ilk_hpd_detection_setup(dev_priv);
3208e4ce95aaSVille Syrjälä 
320991d14251STvrtko Ursulin 	ibx_hpd_irq_setup(dev_priv);
3210e4ce95aaSVille Syrjälä }
3211e4ce95aaSVille Syrjälä 
32127fff8126SImre Deak static void __bxt_hpd_detection_setup(struct drm_i915_private *dev_priv,
32137fff8126SImre Deak 				      u32 enabled_irqs)
3214e0a20ad7SShashank Sharma {
32157fff8126SImre Deak 	u32 hotplug;
3216e0a20ad7SShashank Sharma 
3217a52bb15bSVille Syrjälä 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
32187fff8126SImre Deak 	hotplug |= PORTA_HOTPLUG_ENABLE |
32197fff8126SImre Deak 		   PORTB_HOTPLUG_ENABLE |
32207fff8126SImre Deak 		   PORTC_HOTPLUG_ENABLE;
3221d252bf68SShubhangi Shrivastava 
3222d252bf68SShubhangi Shrivastava 	DRM_DEBUG_KMS("Invert bit setting: hp_ctl:%x hp_port:%x\n",
3223d252bf68SShubhangi Shrivastava 		      hotplug, enabled_irqs);
3224d252bf68SShubhangi Shrivastava 	hotplug &= ~BXT_DDI_HPD_INVERT_MASK;
3225d252bf68SShubhangi Shrivastava 
3226d252bf68SShubhangi Shrivastava 	/*
3227d252bf68SShubhangi Shrivastava 	 * For BXT invert bit has to be set based on AOB design
3228d252bf68SShubhangi Shrivastava 	 * for HPD detection logic, update it based on VBT fields.
3229d252bf68SShubhangi Shrivastava 	 */
3230d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIA) &&
3231d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_A))
3232d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIA_HPD_INVERT;
3233d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIB) &&
3234d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_B))
3235d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIB_HPD_INVERT;
3236d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIC) &&
3237d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_C))
3238d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIC_HPD_INVERT;
3239d252bf68SShubhangi Shrivastava 
3240a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
3241e0a20ad7SShashank Sharma }
3242e0a20ad7SShashank Sharma 
32437fff8126SImre Deak static void bxt_hpd_detection_setup(struct drm_i915_private *dev_priv)
32447fff8126SImre Deak {
32457fff8126SImre Deak 	__bxt_hpd_detection_setup(dev_priv, BXT_DE_PORT_HOTPLUG_MASK);
32467fff8126SImre Deak }
32477fff8126SImre Deak 
32487fff8126SImre Deak static void bxt_hpd_irq_setup(struct drm_i915_private *dev_priv)
32497fff8126SImre Deak {
32507fff8126SImre Deak 	u32 hotplug_irqs, enabled_irqs;
32517fff8126SImre Deak 
32527fff8126SImre Deak 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_bxt);
32537fff8126SImre Deak 	hotplug_irqs = BXT_DE_PORT_HOTPLUG_MASK;
32547fff8126SImre Deak 
32557fff8126SImre Deak 	bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
32567fff8126SImre Deak 
32577fff8126SImre Deak 	__bxt_hpd_detection_setup(dev_priv, enabled_irqs);
32587fff8126SImre Deak }
32597fff8126SImre Deak 
3260d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
3261d46da437SPaulo Zanoni {
3262fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
326382a28bcfSDaniel Vetter 	u32 mask;
3264d46da437SPaulo Zanoni 
32656e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
3266692a04cfSDaniel Vetter 		return;
3267692a04cfSDaniel Vetter 
32686e266956STvrtko Ursulin 	if (HAS_PCH_IBX(dev_priv))
32695c673b60SDaniel Vetter 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
3270105b122eSPaulo Zanoni 	else
32715c673b60SDaniel Vetter 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
32728664281bSPaulo Zanoni 
3273b51a2842SVille Syrjälä 	gen5_assert_iir_is_zero(dev_priv, SDEIIR);
3274d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
32757fff8126SImre Deak 
32767fff8126SImre Deak 	if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
32777fff8126SImre Deak 	    HAS_PCH_LPT(dev_priv))
32781a56b1a2SImre Deak 		ibx_hpd_detection_setup(dev_priv);
32797fff8126SImre Deak 	else
32807fff8126SImre Deak 		spt_hpd_detection_setup(dev_priv);
3281d46da437SPaulo Zanoni }
3282d46da437SPaulo Zanoni 
32830a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
32840a9a8c91SDaniel Vetter {
3285fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
32860a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
32870a9a8c91SDaniel Vetter 
32880a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
32890a9a8c91SDaniel Vetter 
32900a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
32913c9192bcSTvrtko Ursulin 	if (HAS_L3_DPF(dev_priv)) {
32920a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
3293772c2a51STvrtko Ursulin 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev_priv);
3294772c2a51STvrtko Ursulin 		gt_irqs |= GT_PARITY_ERROR(dev_priv);
32950a9a8c91SDaniel Vetter 	}
32960a9a8c91SDaniel Vetter 
32970a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
32985db94019STvrtko Ursulin 	if (IS_GEN5(dev_priv)) {
3299f8973c21SChris Wilson 		gt_irqs |= ILK_BSD_USER_INTERRUPT;
33000a9a8c91SDaniel Vetter 	} else {
33010a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
33020a9a8c91SDaniel Vetter 	}
33030a9a8c91SDaniel Vetter 
330435079899SPaulo Zanoni 	GEN5_IRQ_INIT(GT, dev_priv->gt_irq_mask, gt_irqs);
33050a9a8c91SDaniel Vetter 
3306b243f530STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 6) {
330778e68d36SImre Deak 		/*
330878e68d36SImre Deak 		 * RPS interrupts will get enabled/disabled on demand when RPS
330978e68d36SImre Deak 		 * itself is enabled/disabled.
331078e68d36SImre Deak 		 */
3311f4e9af4fSAkash Goel 		if (HAS_VEBOX(dev_priv)) {
33120a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
3313f4e9af4fSAkash Goel 			dev_priv->pm_ier |= PM_VEBOX_USER_INTERRUPT;
3314f4e9af4fSAkash Goel 		}
33150a9a8c91SDaniel Vetter 
3316f4e9af4fSAkash Goel 		dev_priv->pm_imr = 0xffffffff;
3317f4e9af4fSAkash Goel 		GEN5_IRQ_INIT(GEN6_PM, dev_priv->pm_imr, pm_irqs);
33180a9a8c91SDaniel Vetter 	}
33190a9a8c91SDaniel Vetter }
33200a9a8c91SDaniel Vetter 
3321f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
3322036a4a7dSZhenyu Wang {
3323fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
33248e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
33258e76f8dcSPaulo Zanoni 
3326b243f530STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 7) {
33278e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
33288e76f8dcSPaulo Zanoni 				DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
33298e76f8dcSPaulo Zanoni 				DE_PLANEB_FLIP_DONE_IVB |
33305c673b60SDaniel Vetter 				DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB);
33318e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
333223bb4cb5SVille Syrjälä 			      DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB |
333323bb4cb5SVille Syrjälä 			      DE_DP_A_HOTPLUG_IVB);
33348e76f8dcSPaulo Zanoni 	} else {
33358e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3336ce99c256SDaniel Vetter 				DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
33375b3a856bSDaniel Vetter 				DE_AUX_CHANNEL_A |
33385b3a856bSDaniel Vetter 				DE_PIPEB_CRC_DONE | DE_PIPEA_CRC_DONE |
33395b3a856bSDaniel Vetter 				DE_POISON);
3340e4ce95aaSVille Syrjälä 		extra_mask = (DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
3341e4ce95aaSVille Syrjälä 			      DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN |
3342e4ce95aaSVille Syrjälä 			      DE_DP_A_HOTPLUG);
33438e76f8dcSPaulo Zanoni 	}
3344036a4a7dSZhenyu Wang 
33451ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3346036a4a7dSZhenyu Wang 
33470c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xeffe);
33480c841212SPaulo Zanoni 
3349622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3350622364b6SPaulo Zanoni 
335135079899SPaulo Zanoni 	GEN5_IRQ_INIT(DE, dev_priv->irq_mask, display_mask | extra_mask);
3352036a4a7dSZhenyu Wang 
33530a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
3354036a4a7dSZhenyu Wang 
33551a56b1a2SImre Deak 	ilk_hpd_detection_setup(dev_priv);
33561a56b1a2SImre Deak 
3357d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
33587fe0b973SKeith Packard 
335950a0bc90STvrtko Ursulin 	if (IS_IRONLAKE_M(dev_priv)) {
33606005ce42SDaniel Vetter 		/* Enable PCU event interrupts
33616005ce42SDaniel Vetter 		 *
33626005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
33634bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
33644bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
3365d6207435SDaniel Vetter 		spin_lock_irq(&dev_priv->irq_lock);
3366fbdedaeaSVille Syrjälä 		ilk_enable_display_irq(dev_priv, DE_PCU_EVENT);
3367d6207435SDaniel Vetter 		spin_unlock_irq(&dev_priv->irq_lock);
3368f97108d1SJesse Barnes 	}
3369f97108d1SJesse Barnes 
3370036a4a7dSZhenyu Wang 	return 0;
3371036a4a7dSZhenyu Wang }
3372036a4a7dSZhenyu Wang 
3373f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3374f8b79e58SImre Deak {
3375f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3376f8b79e58SImre Deak 
3377f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3378f8b79e58SImre Deak 		return;
3379f8b79e58SImre Deak 
3380f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3381f8b79e58SImre Deak 
3382d6c69803SVille Syrjälä 	if (intel_irqs_enabled(dev_priv)) {
3383d6c69803SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3384ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3385f8b79e58SImre Deak 	}
3386d6c69803SVille Syrjälä }
3387f8b79e58SImre Deak 
3388f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3389f8b79e58SImre Deak {
3390f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3391f8b79e58SImre Deak 
3392f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3393f8b79e58SImre Deak 		return;
3394f8b79e58SImre Deak 
3395f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3396f8b79e58SImre Deak 
3397950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3398ad22d106SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3399f8b79e58SImre Deak }
3400f8b79e58SImre Deak 
34010e6c9a9eSVille Syrjälä 
34020e6c9a9eSVille Syrjälä static int valleyview_irq_postinstall(struct drm_device *dev)
34030e6c9a9eSVille Syrjälä {
3404fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
34050e6c9a9eSVille Syrjälä 
34060a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
34077e231dbeSJesse Barnes 
3408ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
34099918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3410ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3411ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3412ad22d106SVille Syrjälä 
34137e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
341434c7b8a7SVille Syrjälä 	POSTING_READ(VLV_MASTER_IER);
341520afbda2SDaniel Vetter 
341620afbda2SDaniel Vetter 	return 0;
341720afbda2SDaniel Vetter }
341820afbda2SDaniel Vetter 
3419abd58f01SBen Widawsky static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
3420abd58f01SBen Widawsky {
3421abd58f01SBen Widawsky 	/* These are interrupts we'll toggle with the ring mask register */
3422abd58f01SBen Widawsky 	uint32_t gt_interrupts[] = {
3423abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
342473d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
342573d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT |
342673d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT,
3427abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
342873d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
342973d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT |
343073d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT,
3431abd58f01SBen Widawsky 		0,
343273d477f6SOscar Mateo 		GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT |
343373d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT
3434abd58f01SBen Widawsky 		};
3435abd58f01SBen Widawsky 
343698735739STvrtko Ursulin 	if (HAS_L3_DPF(dev_priv))
343798735739STvrtko Ursulin 		gt_interrupts[0] |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
343898735739STvrtko Ursulin 
3439f4e9af4fSAkash Goel 	dev_priv->pm_ier = 0x0;
3440f4e9af4fSAkash Goel 	dev_priv->pm_imr = ~dev_priv->pm_ier;
34419a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 0, ~gt_interrupts[0], gt_interrupts[0]);
34429a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 1, ~gt_interrupts[1], gt_interrupts[1]);
344378e68d36SImre Deak 	/*
344478e68d36SImre Deak 	 * RPS interrupts will get enabled/disabled on demand when RPS itself
344526705e20SSagar Arun Kamble 	 * is enabled/disabled. Same wil be the case for GuC interrupts.
344678e68d36SImre Deak 	 */
3447f4e9af4fSAkash Goel 	GEN8_IRQ_INIT_NDX(GT, 2, dev_priv->pm_imr, dev_priv->pm_ier);
34489a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 3, ~gt_interrupts[3], gt_interrupts[3]);
3449abd58f01SBen Widawsky }
3450abd58f01SBen Widawsky 
3451abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3452abd58f01SBen Widawsky {
3453770de83dSDamien Lespiau 	uint32_t de_pipe_masked = GEN8_PIPE_CDCLK_CRC_DONE;
3454770de83dSDamien Lespiau 	uint32_t de_pipe_enables;
34553a3b3c7dSVille Syrjälä 	u32 de_port_masked = GEN8_AUX_CHANNEL_A;
34563a3b3c7dSVille Syrjälä 	u32 de_port_enables;
345711825b0dSVille Syrjälä 	u32 de_misc_masked = GEN8_DE_MISC_GSE;
34583a3b3c7dSVille Syrjälä 	enum pipe pipe;
3459770de83dSDamien Lespiau 
3460b4834a50SRodrigo Vivi 	if (INTEL_INFO(dev_priv)->gen >= 9) {
3461770de83dSDamien Lespiau 		de_pipe_masked |= GEN9_PIPE_PLANE1_FLIP_DONE |
3462770de83dSDamien Lespiau 				  GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
34633a3b3c7dSVille Syrjälä 		de_port_masked |= GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
346488e04703SJesse Barnes 				  GEN9_AUX_CHANNEL_D;
3465cc3f90f0SAnder Conselvan de Oliveira 		if (IS_GEN9_LP(dev_priv))
34663a3b3c7dSVille Syrjälä 			de_port_masked |= BXT_DE_PORT_GMBUS;
34673a3b3c7dSVille Syrjälä 	} else {
3468770de83dSDamien Lespiau 		de_pipe_masked |= GEN8_PIPE_PRIMARY_FLIP_DONE |
3469770de83dSDamien Lespiau 				  GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
34703a3b3c7dSVille Syrjälä 	}
3471770de83dSDamien Lespiau 
3472770de83dSDamien Lespiau 	de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
3473770de83dSDamien Lespiau 					   GEN8_PIPE_FIFO_UNDERRUN;
3474770de83dSDamien Lespiau 
34753a3b3c7dSVille Syrjälä 	de_port_enables = de_port_masked;
3476cc3f90f0SAnder Conselvan de Oliveira 	if (IS_GEN9_LP(dev_priv))
3477a52bb15bSVille Syrjälä 		de_port_enables |= BXT_DE_PORT_HOTPLUG_MASK;
3478a52bb15bSVille Syrjälä 	else if (IS_BROADWELL(dev_priv))
34793a3b3c7dSVille Syrjälä 		de_port_enables |= GEN8_PORT_DP_A_HOTPLUG;
34803a3b3c7dSVille Syrjälä 
348113b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_A] = ~de_pipe_masked;
348213b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_B] = ~de_pipe_masked;
348313b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_C] = ~de_pipe_masked;
3484abd58f01SBen Widawsky 
3485055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3486f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3487813bde43SPaulo Zanoni 				POWER_DOMAIN_PIPE(pipe)))
3488813bde43SPaulo Zanoni 			GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
3489813bde43SPaulo Zanoni 					  dev_priv->de_irq_mask[pipe],
349035079899SPaulo Zanoni 					  de_pipe_enables);
3491abd58f01SBen Widawsky 
34923a3b3c7dSVille Syrjälä 	GEN5_IRQ_INIT(GEN8_DE_PORT_, ~de_port_masked, de_port_enables);
349311825b0dSVille Syrjälä 	GEN5_IRQ_INIT(GEN8_DE_MISC_, ~de_misc_masked, de_misc_masked);
34947fff8126SImre Deak 
34957fff8126SImre Deak 	if (IS_GEN9_LP(dev_priv))
34967fff8126SImre Deak 		bxt_hpd_detection_setup(dev_priv);
34971a56b1a2SImre Deak 	else if (IS_BROADWELL(dev_priv))
34981a56b1a2SImre Deak 		ilk_hpd_detection_setup(dev_priv);
3499abd58f01SBen Widawsky }
3500abd58f01SBen Widawsky 
3501abd58f01SBen Widawsky static int gen8_irq_postinstall(struct drm_device *dev)
3502abd58f01SBen Widawsky {
3503fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3504abd58f01SBen Widawsky 
35056e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
3506622364b6SPaulo Zanoni 		ibx_irq_pre_postinstall(dev);
3507622364b6SPaulo Zanoni 
3508abd58f01SBen Widawsky 	gen8_gt_irq_postinstall(dev_priv);
3509abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3510abd58f01SBen Widawsky 
35116e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
3512abd58f01SBen Widawsky 		ibx_irq_postinstall(dev);
3513abd58f01SBen Widawsky 
3514e5328c43SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
3515abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3516abd58f01SBen Widawsky 
3517abd58f01SBen Widawsky 	return 0;
3518abd58f01SBen Widawsky }
3519abd58f01SBen Widawsky 
352043f328d7SVille Syrjälä static int cherryview_irq_postinstall(struct drm_device *dev)
352143f328d7SVille Syrjälä {
3522fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
352343f328d7SVille Syrjälä 
352443f328d7SVille Syrjälä 	gen8_gt_irq_postinstall(dev_priv);
352543f328d7SVille Syrjälä 
3526ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
35279918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3528ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3529ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3530ad22d106SVille Syrjälä 
3531e5328c43SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
353243f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
353343f328d7SVille Syrjälä 
353443f328d7SVille Syrjälä 	return 0;
353543f328d7SVille Syrjälä }
353643f328d7SVille Syrjälä 
3537abd58f01SBen Widawsky static void gen8_irq_uninstall(struct drm_device *dev)
3538abd58f01SBen Widawsky {
3539fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3540abd58f01SBen Widawsky 
3541abd58f01SBen Widawsky 	if (!dev_priv)
3542abd58f01SBen Widawsky 		return;
3543abd58f01SBen Widawsky 
3544823f6b38SPaulo Zanoni 	gen8_irq_reset(dev);
3545abd58f01SBen Widawsky }
3546abd58f01SBen Widawsky 
35477e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
35487e231dbeSJesse Barnes {
3549fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
35507e231dbeSJesse Barnes 
35517e231dbeSJesse Barnes 	if (!dev_priv)
35527e231dbeSJesse Barnes 		return;
35537e231dbeSJesse Barnes 
3554843d0e7dSImre Deak 	I915_WRITE(VLV_MASTER_IER, 0);
355534c7b8a7SVille Syrjälä 	POSTING_READ(VLV_MASTER_IER);
3556843d0e7dSImre Deak 
3557b243f530STvrtko Ursulin 	gen5_gt_irq_reset(dev_priv);
3558893fce8eSVille Syrjälä 
35597e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
3560f8b79e58SImre Deak 
3561ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
35629918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3563ad22d106SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3564ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
35657e231dbeSJesse Barnes }
35667e231dbeSJesse Barnes 
356743f328d7SVille Syrjälä static void cherryview_irq_uninstall(struct drm_device *dev)
356843f328d7SVille Syrjälä {
3569fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
357043f328d7SVille Syrjälä 
357143f328d7SVille Syrjälä 	if (!dev_priv)
357243f328d7SVille Syrjälä 		return;
357343f328d7SVille Syrjälä 
357443f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
357543f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
357643f328d7SVille Syrjälä 
3577a2c30fbaSVille Syrjälä 	gen8_gt_irq_reset(dev_priv);
357843f328d7SVille Syrjälä 
3579a2c30fbaSVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
358043f328d7SVille Syrjälä 
3581ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
35829918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3583ad22d106SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3584ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
358543f328d7SVille Syrjälä }
358643f328d7SVille Syrjälä 
3587f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
3588036a4a7dSZhenyu Wang {
3589fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
35904697995bSJesse Barnes 
35914697995bSJesse Barnes 	if (!dev_priv)
35924697995bSJesse Barnes 		return;
35934697995bSJesse Barnes 
3594be30b29fSPaulo Zanoni 	ironlake_irq_reset(dev);
3595036a4a7dSZhenyu Wang }
3596036a4a7dSZhenyu Wang 
3597c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
3598c2798b19SChris Wilson {
3599fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3600c2798b19SChris Wilson 	int pipe;
3601c2798b19SChris Wilson 
3602055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3603c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3604c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3605c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3606c2798b19SChris Wilson 	POSTING_READ16(IER);
3607c2798b19SChris Wilson }
3608c2798b19SChris Wilson 
3609c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
3610c2798b19SChris Wilson {
3611fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3612c2798b19SChris Wilson 
3613c2798b19SChris Wilson 	I915_WRITE16(EMR,
3614c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3615c2798b19SChris Wilson 
3616c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
3617c2798b19SChris Wilson 	dev_priv->irq_mask =
3618c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3619c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3620c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
362137ef01abSDaniel Vetter 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
3622c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
3623c2798b19SChris Wilson 
3624c2798b19SChris Wilson 	I915_WRITE16(IER,
3625c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3626c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3627c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
3628c2798b19SChris Wilson 	POSTING_READ16(IER);
3629c2798b19SChris Wilson 
3630379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3631379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3632d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3633755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3634755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3635d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3636379ef82dSDaniel Vetter 
3637c2798b19SChris Wilson 	return 0;
3638c2798b19SChris Wilson }
3639c2798b19SChris Wilson 
36405a21b665SDaniel Vetter /*
36415a21b665SDaniel Vetter  * Returns true when a page flip has completed.
36425a21b665SDaniel Vetter  */
36435a21b665SDaniel Vetter static bool i8xx_handle_vblank(struct drm_i915_private *dev_priv,
36445a21b665SDaniel Vetter 			       int plane, int pipe, u32 iir)
36455a21b665SDaniel Vetter {
36465a21b665SDaniel Vetter 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
36475a21b665SDaniel Vetter 
36485a21b665SDaniel Vetter 	if (!intel_pipe_handle_vblank(dev_priv, pipe))
36495a21b665SDaniel Vetter 		return false;
36505a21b665SDaniel Vetter 
36515a21b665SDaniel Vetter 	if ((iir & flip_pending) == 0)
36525a21b665SDaniel Vetter 		goto check_page_flip;
36535a21b665SDaniel Vetter 
36545a21b665SDaniel Vetter 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
36555a21b665SDaniel Vetter 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
36565a21b665SDaniel Vetter 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
36575a21b665SDaniel Vetter 	 * the flip is completed (no longer pending). Since this doesn't raise
36585a21b665SDaniel Vetter 	 * an interrupt per se, we watch for the change at vblank.
36595a21b665SDaniel Vetter 	 */
36605a21b665SDaniel Vetter 	if (I915_READ16(ISR) & flip_pending)
36615a21b665SDaniel Vetter 		goto check_page_flip;
36625a21b665SDaniel Vetter 
36635a21b665SDaniel Vetter 	intel_finish_page_flip_cs(dev_priv, pipe);
36645a21b665SDaniel Vetter 	return true;
36655a21b665SDaniel Vetter 
36665a21b665SDaniel Vetter check_page_flip:
36675a21b665SDaniel Vetter 	intel_check_page_flip(dev_priv, pipe);
36685a21b665SDaniel Vetter 	return false;
36695a21b665SDaniel Vetter }
36705a21b665SDaniel Vetter 
3671ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3672c2798b19SChris Wilson {
367345a83f84SDaniel Vetter 	struct drm_device *dev = arg;
3674fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3675c2798b19SChris Wilson 	u16 iir, new_iir;
3676c2798b19SChris Wilson 	u32 pipe_stats[2];
3677c2798b19SChris Wilson 	int pipe;
3678c2798b19SChris Wilson 	u16 flip_mask =
3679c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3680c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
36811f814dacSImre Deak 	irqreturn_t ret;
3682c2798b19SChris Wilson 
36832dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
36842dd2a883SImre Deak 		return IRQ_NONE;
36852dd2a883SImre Deak 
36861f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
36871f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
36881f814dacSImre Deak 
36891f814dacSImre Deak 	ret = IRQ_NONE;
3690c2798b19SChris Wilson 	iir = I915_READ16(IIR);
3691c2798b19SChris Wilson 	if (iir == 0)
36921f814dacSImre Deak 		goto out;
3693c2798b19SChris Wilson 
3694c2798b19SChris Wilson 	while (iir & ~flip_mask) {
3695c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3696c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3697c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3698c2798b19SChris Wilson 		 * interrupts (for non-MSI).
3699c2798b19SChris Wilson 		 */
3700222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
3701c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3702aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
3703c2798b19SChris Wilson 
3704055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
3705f0f59a00SVille Syrjälä 			i915_reg_t reg = PIPESTAT(pipe);
3706c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3707c2798b19SChris Wilson 
3708c2798b19SChris Wilson 			/*
3709c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3710c2798b19SChris Wilson 			 */
37112d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & 0x8000ffff)
3712c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3713c2798b19SChris Wilson 		}
3714222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
3715c2798b19SChris Wilson 
3716c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
3717c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
3718c2798b19SChris Wilson 
3719c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
37203b3f1650SAkash Goel 			notify_ring(dev_priv->engine[RCS]);
3721c2798b19SChris Wilson 
3722055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
37235a21b665SDaniel Vetter 			int plane = pipe;
37245a21b665SDaniel Vetter 			if (HAS_FBC(dev_priv))
37255a21b665SDaniel Vetter 				plane = !plane;
37265a21b665SDaniel Vetter 
37275a21b665SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
37285a21b665SDaniel Vetter 			    i8xx_handle_vblank(dev_priv, plane, pipe, iir))
37295a21b665SDaniel Vetter 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3730c2798b19SChris Wilson 
37314356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
373291d14251STvrtko Ursulin 				i9xx_pipe_crc_irq_handler(dev_priv, pipe);
37332d9d2b0bSVille Syrjälä 
37341f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
37351f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
37361f7247c0SDaniel Vetter 								    pipe);
37374356d586SDaniel Vetter 		}
3738c2798b19SChris Wilson 
3739c2798b19SChris Wilson 		iir = new_iir;
3740c2798b19SChris Wilson 	}
37411f814dacSImre Deak 	ret = IRQ_HANDLED;
3742c2798b19SChris Wilson 
37431f814dacSImre Deak out:
37441f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
37451f814dacSImre Deak 
37461f814dacSImre Deak 	return ret;
3747c2798b19SChris Wilson }
3748c2798b19SChris Wilson 
3749c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
3750c2798b19SChris Wilson {
3751fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3752c2798b19SChris Wilson 	int pipe;
3753c2798b19SChris Wilson 
3754055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
3755c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
3756c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3757c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
3758c2798b19SChris Wilson 	}
3759c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3760c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3761c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
3762c2798b19SChris Wilson }
3763c2798b19SChris Wilson 
3764a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
3765a266c7d5SChris Wilson {
3766fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3767a266c7d5SChris Wilson 	int pipe;
3768a266c7d5SChris Wilson 
376956b857a5STvrtko Ursulin 	if (I915_HAS_HOTPLUG(dev_priv)) {
37700706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
3771a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3772a266c7d5SChris Wilson 	}
3773a266c7d5SChris Wilson 
377400d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
3775055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3776a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3777a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3778a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3779a266c7d5SChris Wilson 	POSTING_READ(IER);
3780a266c7d5SChris Wilson }
3781a266c7d5SChris Wilson 
3782a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
3783a266c7d5SChris Wilson {
3784fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
378538bde180SChris Wilson 	u32 enable_mask;
3786a266c7d5SChris Wilson 
378738bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
378838bde180SChris Wilson 
378938bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
379038bde180SChris Wilson 	dev_priv->irq_mask =
379138bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
379238bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
379338bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
379438bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
379537ef01abSDaniel Vetter 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
379638bde180SChris Wilson 
379738bde180SChris Wilson 	enable_mask =
379838bde180SChris Wilson 		I915_ASLE_INTERRUPT |
379938bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
380038bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
380138bde180SChris Wilson 		I915_USER_INTERRUPT;
380238bde180SChris Wilson 
380356b857a5STvrtko Ursulin 	if (I915_HAS_HOTPLUG(dev_priv)) {
38040706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
380520afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
380620afbda2SDaniel Vetter 
3807a266c7d5SChris Wilson 		/* Enable in IER... */
3808a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
3809a266c7d5SChris Wilson 		/* and unmask in IMR */
3810a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
3811a266c7d5SChris Wilson 	}
3812a266c7d5SChris Wilson 
3813a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3814a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3815a266c7d5SChris Wilson 	POSTING_READ(IER);
3816a266c7d5SChris Wilson 
381791d14251STvrtko Ursulin 	i915_enable_asle_pipestat(dev_priv);
381820afbda2SDaniel Vetter 
3819379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3820379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3821d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3822755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3823755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3824d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3825379ef82dSDaniel Vetter 
382620afbda2SDaniel Vetter 	return 0;
382720afbda2SDaniel Vetter }
382820afbda2SDaniel Vetter 
38295a21b665SDaniel Vetter /*
38305a21b665SDaniel Vetter  * Returns true when a page flip has completed.
38315a21b665SDaniel Vetter  */
38325a21b665SDaniel Vetter static bool i915_handle_vblank(struct drm_i915_private *dev_priv,
38335a21b665SDaniel Vetter 			       int plane, int pipe, u32 iir)
38345a21b665SDaniel Vetter {
38355a21b665SDaniel Vetter 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
38365a21b665SDaniel Vetter 
38375a21b665SDaniel Vetter 	if (!intel_pipe_handle_vblank(dev_priv, pipe))
38385a21b665SDaniel Vetter 		return false;
38395a21b665SDaniel Vetter 
38405a21b665SDaniel Vetter 	if ((iir & flip_pending) == 0)
38415a21b665SDaniel Vetter 		goto check_page_flip;
38425a21b665SDaniel Vetter 
38435a21b665SDaniel Vetter 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
38445a21b665SDaniel Vetter 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
38455a21b665SDaniel Vetter 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
38465a21b665SDaniel Vetter 	 * the flip is completed (no longer pending). Since this doesn't raise
38475a21b665SDaniel Vetter 	 * an interrupt per se, we watch for the change at vblank.
38485a21b665SDaniel Vetter 	 */
38495a21b665SDaniel Vetter 	if (I915_READ(ISR) & flip_pending)
38505a21b665SDaniel Vetter 		goto check_page_flip;
38515a21b665SDaniel Vetter 
38525a21b665SDaniel Vetter 	intel_finish_page_flip_cs(dev_priv, pipe);
38535a21b665SDaniel Vetter 	return true;
38545a21b665SDaniel Vetter 
38555a21b665SDaniel Vetter check_page_flip:
38565a21b665SDaniel Vetter 	intel_check_page_flip(dev_priv, pipe);
38575a21b665SDaniel Vetter 	return false;
38585a21b665SDaniel Vetter }
38595a21b665SDaniel Vetter 
3860ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
3861a266c7d5SChris Wilson {
386245a83f84SDaniel Vetter 	struct drm_device *dev = arg;
3863fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
38648291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
386538bde180SChris Wilson 	u32 flip_mask =
386638bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
386738bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
386838bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
3869a266c7d5SChris Wilson 
38702dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
38712dd2a883SImre Deak 		return IRQ_NONE;
38722dd2a883SImre Deak 
38731f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
38741f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
38751f814dacSImre Deak 
3876a266c7d5SChris Wilson 	iir = I915_READ(IIR);
387738bde180SChris Wilson 	do {
387838bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
38798291ee90SChris Wilson 		bool blc_event = false;
3880a266c7d5SChris Wilson 
3881a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3882a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3883a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3884a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3885a266c7d5SChris Wilson 		 */
3886222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
3887a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3888aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
3889a266c7d5SChris Wilson 
3890055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
3891f0f59a00SVille Syrjälä 			i915_reg_t reg = PIPESTAT(pipe);
3892a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3893a266c7d5SChris Wilson 
389438bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
3895a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3896a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
389738bde180SChris Wilson 				irq_received = true;
3898a266c7d5SChris Wilson 			}
3899a266c7d5SChris Wilson 		}
3900222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
3901a266c7d5SChris Wilson 
3902a266c7d5SChris Wilson 		if (!irq_received)
3903a266c7d5SChris Wilson 			break;
3904a266c7d5SChris Wilson 
3905a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
390691d14251STvrtko Ursulin 		if (I915_HAS_HOTPLUG(dev_priv) &&
39071ae3c34cSVille Syrjälä 		    iir & I915_DISPLAY_PORT_INTERRUPT) {
39081ae3c34cSVille Syrjälä 			u32 hotplug_status = i9xx_hpd_irq_ack(dev_priv);
39091ae3c34cSVille Syrjälä 			if (hotplug_status)
391091d14251STvrtko Ursulin 				i9xx_hpd_irq_handler(dev_priv, hotplug_status);
39111ae3c34cSVille Syrjälä 		}
3912a266c7d5SChris Wilson 
391338bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
3914a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3915a266c7d5SChris Wilson 
3916a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
39173b3f1650SAkash Goel 			notify_ring(dev_priv->engine[RCS]);
3918a266c7d5SChris Wilson 
3919055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
39205a21b665SDaniel Vetter 			int plane = pipe;
39215a21b665SDaniel Vetter 			if (HAS_FBC(dev_priv))
39225a21b665SDaniel Vetter 				plane = !plane;
39235a21b665SDaniel Vetter 
39245a21b665SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
39255a21b665SDaniel Vetter 			    i915_handle_vblank(dev_priv, plane, pipe, iir))
39265a21b665SDaniel Vetter 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3927a266c7d5SChris Wilson 
3928a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3929a266c7d5SChris Wilson 				blc_event = true;
39304356d586SDaniel Vetter 
39314356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
393291d14251STvrtko Ursulin 				i9xx_pipe_crc_irq_handler(dev_priv, pipe);
39332d9d2b0bSVille Syrjälä 
39341f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
39351f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
39361f7247c0SDaniel Vetter 								    pipe);
3937a266c7d5SChris Wilson 		}
3938a266c7d5SChris Wilson 
3939a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
394091d14251STvrtko Ursulin 			intel_opregion_asle_intr(dev_priv);
3941a266c7d5SChris Wilson 
3942a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3943a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3944a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3945a266c7d5SChris Wilson 		 * we would never get another interrupt.
3946a266c7d5SChris Wilson 		 *
3947a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3948a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3949a266c7d5SChris Wilson 		 * another one.
3950a266c7d5SChris Wilson 		 *
3951a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3952a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3953a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3954a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3955a266c7d5SChris Wilson 		 * stray interrupts.
3956a266c7d5SChris Wilson 		 */
395738bde180SChris Wilson 		ret = IRQ_HANDLED;
3958a266c7d5SChris Wilson 		iir = new_iir;
395938bde180SChris Wilson 	} while (iir & ~flip_mask);
3960a266c7d5SChris Wilson 
39611f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
39621f814dacSImre Deak 
3963a266c7d5SChris Wilson 	return ret;
3964a266c7d5SChris Wilson }
3965a266c7d5SChris Wilson 
3966a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
3967a266c7d5SChris Wilson {
3968fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3969a266c7d5SChris Wilson 	int pipe;
3970a266c7d5SChris Wilson 
397156b857a5STvrtko Ursulin 	if (I915_HAS_HOTPLUG(dev_priv)) {
39720706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
3973a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3974a266c7d5SChris Wilson 	}
3975a266c7d5SChris Wilson 
397600d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
3977055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
397855b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
3979a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
398055b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
398155b39755SChris Wilson 	}
3982a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3983a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3984a266c7d5SChris Wilson 
3985a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3986a266c7d5SChris Wilson }
3987a266c7d5SChris Wilson 
3988a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
3989a266c7d5SChris Wilson {
3990fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3991a266c7d5SChris Wilson 	int pipe;
3992a266c7d5SChris Wilson 
39930706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
3994a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3995a266c7d5SChris Wilson 
3996a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
3997055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3998a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3999a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4000a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4001a266c7d5SChris Wilson 	POSTING_READ(IER);
4002a266c7d5SChris Wilson }
4003a266c7d5SChris Wilson 
4004a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
4005a266c7d5SChris Wilson {
4006fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4007bbba0a97SChris Wilson 	u32 enable_mask;
4008a266c7d5SChris Wilson 	u32 error_mask;
4009a266c7d5SChris Wilson 
4010a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
4011bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
4012adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
4013bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4014bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
4015bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
4016bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
4017bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
4018bbba0a97SChris Wilson 
4019bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
402021ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
402121ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
4022bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
4023bbba0a97SChris Wilson 
402491d14251STvrtko Ursulin 	if (IS_G4X(dev_priv))
4025bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
4026a266c7d5SChris Wilson 
4027b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4028b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4029d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4030755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
4031755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4032755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4033d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4034a266c7d5SChris Wilson 
4035a266c7d5SChris Wilson 	/*
4036a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
4037a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
4038a266c7d5SChris Wilson 	 */
403991d14251STvrtko Ursulin 	if (IS_G4X(dev_priv)) {
4040a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
4041a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
4042a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
4043a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4044a266c7d5SChris Wilson 	} else {
4045a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
4046a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4047a266c7d5SChris Wilson 	}
4048a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
4049a266c7d5SChris Wilson 
4050a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
4051a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
4052a266c7d5SChris Wilson 	POSTING_READ(IER);
4053a266c7d5SChris Wilson 
40540706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
405520afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
405620afbda2SDaniel Vetter 
405791d14251STvrtko Ursulin 	i915_enable_asle_pipestat(dev_priv);
405820afbda2SDaniel Vetter 
405920afbda2SDaniel Vetter 	return 0;
406020afbda2SDaniel Vetter }
406120afbda2SDaniel Vetter 
406291d14251STvrtko Ursulin static void i915_hpd_irq_setup(struct drm_i915_private *dev_priv)
406320afbda2SDaniel Vetter {
406420afbda2SDaniel Vetter 	u32 hotplug_en;
406520afbda2SDaniel Vetter 
4066b5ea2d56SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
4067b5ea2d56SDaniel Vetter 
4068adca4730SChris Wilson 	/* Note HDMI and DP share hotplug bits */
4069e5868a31SEgbert Eich 	/* enable bits are the same for all generations */
407091d14251STvrtko Ursulin 	hotplug_en = intel_hpd_enabled_irqs(dev_priv, hpd_mask_i915);
4071a266c7d5SChris Wilson 	/* Programming the CRT detection parameters tends
4072a266c7d5SChris Wilson 	   to generate a spurious hotplug event about three
4073a266c7d5SChris Wilson 	   seconds later.  So just do it once.
4074a266c7d5SChris Wilson 	*/
407591d14251STvrtko Ursulin 	if (IS_G4X(dev_priv))
4076a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
4077a266c7d5SChris Wilson 	hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
4078a266c7d5SChris Wilson 
4079a266c7d5SChris Wilson 	/* Ignore TV since it's buggy */
40800706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv,
4081f9e3dc78SJani Nikula 					     HOTPLUG_INT_EN_MASK |
4082f9e3dc78SJani Nikula 					     CRT_HOTPLUG_VOLTAGE_COMPARE_MASK |
4083f9e3dc78SJani Nikula 					     CRT_HOTPLUG_ACTIVATION_PERIOD_64,
40840706f17cSEgbert Eich 					     hotplug_en);
4085a266c7d5SChris Wilson }
4086a266c7d5SChris Wilson 
4087ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
4088a266c7d5SChris Wilson {
408945a83f84SDaniel Vetter 	struct drm_device *dev = arg;
4090fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4091a266c7d5SChris Wilson 	u32 iir, new_iir;
4092a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
4093a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
409421ad8330SVille Syrjälä 	u32 flip_mask =
409521ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
409621ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
4097a266c7d5SChris Wilson 
40982dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
40992dd2a883SImre Deak 		return IRQ_NONE;
41002dd2a883SImre Deak 
41011f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
41021f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
41031f814dacSImre Deak 
4104a266c7d5SChris Wilson 	iir = I915_READ(IIR);
4105a266c7d5SChris Wilson 
4106a266c7d5SChris Wilson 	for (;;) {
4107501e01d7SVille Syrjälä 		bool irq_received = (iir & ~flip_mask) != 0;
41082c8ba29fSChris Wilson 		bool blc_event = false;
41092c8ba29fSChris Wilson 
4110a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4111a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4112a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4113a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
4114a266c7d5SChris Wilson 		 */
4115222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
4116a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4117aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4118a266c7d5SChris Wilson 
4119055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4120f0f59a00SVille Syrjälä 			i915_reg_t reg = PIPESTAT(pipe);
4121a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4122a266c7d5SChris Wilson 
4123a266c7d5SChris Wilson 			/*
4124a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
4125a266c7d5SChris Wilson 			 */
4126a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4127a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
4128501e01d7SVille Syrjälä 				irq_received = true;
4129a266c7d5SChris Wilson 			}
4130a266c7d5SChris Wilson 		}
4131222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4132a266c7d5SChris Wilson 
4133a266c7d5SChris Wilson 		if (!irq_received)
4134a266c7d5SChris Wilson 			break;
4135a266c7d5SChris Wilson 
4136a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
4137a266c7d5SChris Wilson 
4138a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
41391ae3c34cSVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
41401ae3c34cSVille Syrjälä 			u32 hotplug_status = i9xx_hpd_irq_ack(dev_priv);
41411ae3c34cSVille Syrjälä 			if (hotplug_status)
414291d14251STvrtko Ursulin 				i9xx_hpd_irq_handler(dev_priv, hotplug_status);
41431ae3c34cSVille Syrjälä 		}
4144a266c7d5SChris Wilson 
414521ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
4146a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4147a266c7d5SChris Wilson 
4148a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
41493b3f1650SAkash Goel 			notify_ring(dev_priv->engine[RCS]);
4150a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
41513b3f1650SAkash Goel 			notify_ring(dev_priv->engine[VCS]);
4152a266c7d5SChris Wilson 
4153055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
41545a21b665SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
41555a21b665SDaniel Vetter 			    i915_handle_vblank(dev_priv, pipe, pipe, iir))
41565a21b665SDaniel Vetter 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
4157a266c7d5SChris Wilson 
4158a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4159a266c7d5SChris Wilson 				blc_event = true;
41604356d586SDaniel Vetter 
41614356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
416291d14251STvrtko Ursulin 				i9xx_pipe_crc_irq_handler(dev_priv, pipe);
4163a266c7d5SChris Wilson 
41641f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
41651f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
41662d9d2b0bSVille Syrjälä 		}
4167a266c7d5SChris Wilson 
4168a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
416991d14251STvrtko Ursulin 			intel_opregion_asle_intr(dev_priv);
4170a266c7d5SChris Wilson 
4171515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
417291d14251STvrtko Ursulin 			gmbus_irq_handler(dev_priv);
4173515ac2bbSDaniel Vetter 
4174a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4175a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4176a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4177a266c7d5SChris Wilson 		 * we would never get another interrupt.
4178a266c7d5SChris Wilson 		 *
4179a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4180a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4181a266c7d5SChris Wilson 		 * another one.
4182a266c7d5SChris Wilson 		 *
4183a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4184a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4185a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4186a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4187a266c7d5SChris Wilson 		 * stray interrupts.
4188a266c7d5SChris Wilson 		 */
4189a266c7d5SChris Wilson 		iir = new_iir;
4190a266c7d5SChris Wilson 	}
4191a266c7d5SChris Wilson 
41921f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
41931f814dacSImre Deak 
4194a266c7d5SChris Wilson 	return ret;
4195a266c7d5SChris Wilson }
4196a266c7d5SChris Wilson 
4197a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
4198a266c7d5SChris Wilson {
4199fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4200a266c7d5SChris Wilson 	int pipe;
4201a266c7d5SChris Wilson 
4202a266c7d5SChris Wilson 	if (!dev_priv)
4203a266c7d5SChris Wilson 		return;
4204a266c7d5SChris Wilson 
42050706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4206a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4207a266c7d5SChris Wilson 
4208a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
4209055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4210a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4211a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4212a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4213a266c7d5SChris Wilson 
4214055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4215a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
4216a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
4217a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4218a266c7d5SChris Wilson }
4219a266c7d5SChris Wilson 
4220fca52a55SDaniel Vetter /**
4221fca52a55SDaniel Vetter  * intel_irq_init - initializes irq support
4222fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4223fca52a55SDaniel Vetter  *
4224fca52a55SDaniel Vetter  * This function initializes all the irq support including work items, timers
4225fca52a55SDaniel Vetter  * and all the vtables. It does not setup the interrupt itself though.
4226fca52a55SDaniel Vetter  */
4227b963291cSDaniel Vetter void intel_irq_init(struct drm_i915_private *dev_priv)
4228f71d4af4SJesse Barnes {
422991c8a326SChris Wilson 	struct drm_device *dev = &dev_priv->drm;
42308b2e326dSChris Wilson 
423177913b39SJani Nikula 	intel_hpd_init_work(dev_priv);
423277913b39SJani Nikula 
4233c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
4234a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
42358b2e326dSChris Wilson 
42364805fe82STvrtko Ursulin 	if (HAS_GUC_SCHED(dev_priv))
423726705e20SSagar Arun Kamble 		dev_priv->pm_guc_events = GEN9_GUC_TO_HOST_INT_EVENT;
423826705e20SSagar Arun Kamble 
4239a6706b45SDeepak S 	/* Let's track the enabled rps events */
4240666a4537SWayne Boyer 	if (IS_VALLEYVIEW(dev_priv))
42416c65a587SVille Syrjälä 		/* WaGsvRC0ResidencyMethod:vlv */
42426f4b12f8SChris Wilson 		dev_priv->pm_rps_events = GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED;
424331685c25SDeepak S 	else
4244a6706b45SDeepak S 		dev_priv->pm_rps_events = GEN6_PM_RPS_EVENTS;
4245a6706b45SDeepak S 
42461800ad25SSagar Arun Kamble 	dev_priv->rps.pm_intr_keep = 0;
42471800ad25SSagar Arun Kamble 
42481800ad25SSagar Arun Kamble 	/*
42491800ad25SSagar Arun Kamble 	 * SNB,IVB can while VLV,CHV may hard hang on looping batchbuffer
42501800ad25SSagar Arun Kamble 	 * if GEN6_PM_UP_EI_EXPIRED is masked.
42511800ad25SSagar Arun Kamble 	 *
42521800ad25SSagar Arun Kamble 	 * TODO: verify if this can be reproduced on VLV,CHV.
42531800ad25SSagar Arun Kamble 	 */
42541800ad25SSagar Arun Kamble 	if (INTEL_INFO(dev_priv)->gen <= 7 && !IS_HASWELL(dev_priv))
42551800ad25SSagar Arun Kamble 		dev_priv->rps.pm_intr_keep |= GEN6_PM_RP_UP_EI_EXPIRED;
42561800ad25SSagar Arun Kamble 
42571800ad25SSagar Arun Kamble 	if (INTEL_INFO(dev_priv)->gen >= 8)
4258b20e3cfeSDave Gordon 		dev_priv->rps.pm_intr_keep |= GEN8_PMINTR_REDIRECT_TO_GUC;
42591800ad25SSagar Arun Kamble 
4260b963291cSDaniel Vetter 	if (IS_GEN2(dev_priv)) {
42614194c088SRodrigo Vivi 		/* Gen2 doesn't have a hardware frame counter */
42624cdb83ecSVille Syrjälä 		dev->max_vblank_count = 0;
42634194c088SRodrigo Vivi 		dev->driver->get_vblank_counter = drm_vblank_no_hw_counter;
4264b963291cSDaniel Vetter 	} else if (IS_G4X(dev_priv) || INTEL_INFO(dev_priv)->gen >= 5) {
4265f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
4266fd8f507cSVille Syrjälä 		dev->driver->get_vblank_counter = g4x_get_vblank_counter;
4267391f75e2SVille Syrjälä 	} else {
4268391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
4269391f75e2SVille Syrjälä 		dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
4270f71d4af4SJesse Barnes 	}
4271f71d4af4SJesse Barnes 
427221da2700SVille Syrjälä 	/*
427321da2700SVille Syrjälä 	 * Opt out of the vblank disable timer on everything except gen2.
427421da2700SVille Syrjälä 	 * Gen2 doesn't have a hardware frame counter and so depends on
427521da2700SVille Syrjälä 	 * vblank interrupts to produce sane vblank seuquence numbers.
427621da2700SVille Syrjälä 	 */
4277b963291cSDaniel Vetter 	if (!IS_GEN2(dev_priv))
427821da2700SVille Syrjälä 		dev->vblank_disable_immediate = true;
427921da2700SVille Syrjälä 
4280317eaa95SLyude 	dev_priv->hotplug.hpd_storm_threshold = HPD_STORM_DEFAULT_THRESHOLD;
4281317eaa95SLyude 
4282f71d4af4SJesse Barnes 	dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
4283f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
4284f71d4af4SJesse Barnes 
4285b963291cSDaniel Vetter 	if (IS_CHERRYVIEW(dev_priv)) {
428643f328d7SVille Syrjälä 		dev->driver->irq_handler = cherryview_irq_handler;
428743f328d7SVille Syrjälä 		dev->driver->irq_preinstall = cherryview_irq_preinstall;
428843f328d7SVille Syrjälä 		dev->driver->irq_postinstall = cherryview_irq_postinstall;
428943f328d7SVille Syrjälä 		dev->driver->irq_uninstall = cherryview_irq_uninstall;
429086e83e35SChris Wilson 		dev->driver->enable_vblank = i965_enable_vblank;
429186e83e35SChris Wilson 		dev->driver->disable_vblank = i965_disable_vblank;
429243f328d7SVille Syrjälä 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4293b963291cSDaniel Vetter 	} else if (IS_VALLEYVIEW(dev_priv)) {
42947e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
42957e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
42967e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
42977e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
429886e83e35SChris Wilson 		dev->driver->enable_vblank = i965_enable_vblank;
429986e83e35SChris Wilson 		dev->driver->disable_vblank = i965_disable_vblank;
4300fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4301b963291cSDaniel Vetter 	} else if (INTEL_INFO(dev_priv)->gen >= 8) {
4302abd58f01SBen Widawsky 		dev->driver->irq_handler = gen8_irq_handler;
4303723761b8SDaniel Vetter 		dev->driver->irq_preinstall = gen8_irq_reset;
4304abd58f01SBen Widawsky 		dev->driver->irq_postinstall = gen8_irq_postinstall;
4305abd58f01SBen Widawsky 		dev->driver->irq_uninstall = gen8_irq_uninstall;
4306abd58f01SBen Widawsky 		dev->driver->enable_vblank = gen8_enable_vblank;
4307abd58f01SBen Widawsky 		dev->driver->disable_vblank = gen8_disable_vblank;
4308cc3f90f0SAnder Conselvan de Oliveira 		if (IS_GEN9_LP(dev_priv))
4309e0a20ad7SShashank Sharma 			dev_priv->display.hpd_irq_setup = bxt_hpd_irq_setup;
43106e266956STvrtko Ursulin 		else if (HAS_PCH_SPT(dev_priv) || HAS_PCH_KBP(dev_priv))
43116dbf30ceSVille Syrjälä 			dev_priv->display.hpd_irq_setup = spt_hpd_irq_setup;
43126dbf30ceSVille Syrjälä 		else
43133a3b3c7dSVille Syrjälä 			dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
43146e266956STvrtko Ursulin 	} else if (HAS_PCH_SPLIT(dev_priv)) {
4315f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
4316723761b8SDaniel Vetter 		dev->driver->irq_preinstall = ironlake_irq_reset;
4317f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
4318f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
4319f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
4320f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
4321e4ce95aaSVille Syrjälä 		dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
4322f71d4af4SJesse Barnes 	} else {
43237e22dbbbSTvrtko Ursulin 		if (IS_GEN2(dev_priv)) {
4324c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
4325c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
4326c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
4327c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
432886e83e35SChris Wilson 			dev->driver->enable_vblank = i8xx_enable_vblank;
432986e83e35SChris Wilson 			dev->driver->disable_vblank = i8xx_disable_vblank;
43307e22dbbbSTvrtko Ursulin 		} else if (IS_GEN3(dev_priv)) {
4331a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
4332a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
4333a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
4334a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
433586e83e35SChris Wilson 			dev->driver->enable_vblank = i8xx_enable_vblank;
433686e83e35SChris Wilson 			dev->driver->disable_vblank = i8xx_disable_vblank;
4337c2798b19SChris Wilson 		} else {
4338a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
4339a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
4340a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
4341a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
434286e83e35SChris Wilson 			dev->driver->enable_vblank = i965_enable_vblank;
434386e83e35SChris Wilson 			dev->driver->disable_vblank = i965_disable_vblank;
4344c2798b19SChris Wilson 		}
4345778eb334SVille Syrjälä 		if (I915_HAS_HOTPLUG(dev_priv))
4346778eb334SVille Syrjälä 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4347f71d4af4SJesse Barnes 	}
4348f71d4af4SJesse Barnes }
434920afbda2SDaniel Vetter 
4350fca52a55SDaniel Vetter /**
4351fca52a55SDaniel Vetter  * intel_irq_install - enables the hardware interrupt
4352fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4353fca52a55SDaniel Vetter  *
4354fca52a55SDaniel Vetter  * This function enables the hardware interrupt handling, but leaves the hotplug
4355fca52a55SDaniel Vetter  * handling still disabled. It is called after intel_irq_init().
4356fca52a55SDaniel Vetter  *
4357fca52a55SDaniel Vetter  * In the driver load and resume code we need working interrupts in a few places
4358fca52a55SDaniel Vetter  * but don't want to deal with the hassle of concurrent probe and hotplug
4359fca52a55SDaniel Vetter  * workers. Hence the split into this two-stage approach.
4360fca52a55SDaniel Vetter  */
43612aeb7d3aSDaniel Vetter int intel_irq_install(struct drm_i915_private *dev_priv)
43622aeb7d3aSDaniel Vetter {
43632aeb7d3aSDaniel Vetter 	/*
43642aeb7d3aSDaniel Vetter 	 * We enable some interrupt sources in our postinstall hooks, so mark
43652aeb7d3aSDaniel Vetter 	 * interrupts as enabled _before_ actually enabling them to avoid
43662aeb7d3aSDaniel Vetter 	 * special cases in our ordering checks.
43672aeb7d3aSDaniel Vetter 	 */
43682aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
43692aeb7d3aSDaniel Vetter 
437091c8a326SChris Wilson 	return drm_irq_install(&dev_priv->drm, dev_priv->drm.pdev->irq);
43712aeb7d3aSDaniel Vetter }
43722aeb7d3aSDaniel Vetter 
4373fca52a55SDaniel Vetter /**
4374fca52a55SDaniel Vetter  * intel_irq_uninstall - finilizes all irq handling
4375fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4376fca52a55SDaniel Vetter  *
4377fca52a55SDaniel Vetter  * This stops interrupt and hotplug handling and unregisters and frees all
4378fca52a55SDaniel Vetter  * resources acquired in the init functions.
4379fca52a55SDaniel Vetter  */
43802aeb7d3aSDaniel Vetter void intel_irq_uninstall(struct drm_i915_private *dev_priv)
43812aeb7d3aSDaniel Vetter {
438291c8a326SChris Wilson 	drm_irq_uninstall(&dev_priv->drm);
43832aeb7d3aSDaniel Vetter 	intel_hpd_cancel_work(dev_priv);
43842aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
43852aeb7d3aSDaniel Vetter }
43862aeb7d3aSDaniel Vetter 
4387fca52a55SDaniel Vetter /**
4388fca52a55SDaniel Vetter  * intel_runtime_pm_disable_interrupts - runtime interrupt disabling
4389fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4390fca52a55SDaniel Vetter  *
4391fca52a55SDaniel Vetter  * This function is used to disable interrupts at runtime, both in the runtime
4392fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4393fca52a55SDaniel Vetter  */
4394b963291cSDaniel Vetter void intel_runtime_pm_disable_interrupts(struct drm_i915_private *dev_priv)
4395c67a470bSPaulo Zanoni {
439691c8a326SChris Wilson 	dev_priv->drm.driver->irq_uninstall(&dev_priv->drm);
43972aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
439891c8a326SChris Wilson 	synchronize_irq(dev_priv->drm.irq);
4399c67a470bSPaulo Zanoni }
4400c67a470bSPaulo Zanoni 
4401fca52a55SDaniel Vetter /**
4402fca52a55SDaniel Vetter  * intel_runtime_pm_enable_interrupts - runtime interrupt enabling
4403fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4404fca52a55SDaniel Vetter  *
4405fca52a55SDaniel Vetter  * This function is used to enable interrupts at runtime, both in the runtime
4406fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4407fca52a55SDaniel Vetter  */
4408b963291cSDaniel Vetter void intel_runtime_pm_enable_interrupts(struct drm_i915_private *dev_priv)
4409c67a470bSPaulo Zanoni {
44102aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
441191c8a326SChris Wilson 	dev_priv->drm.driver->irq_preinstall(&dev_priv->drm);
441291c8a326SChris Wilson 	dev_priv->drm.driver->irq_postinstall(&dev_priv->drm);
4413c67a470bSPaulo Zanoni }
4414