xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 221fe7994554cc3985fc5d761ed7e44dcae0fa52)
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);
173c9a9a268SImre Deak 
1740706f17cSEgbert Eich /* For display hotplug interrupt */
1750706f17cSEgbert Eich static inline void
1760706f17cSEgbert Eich i915_hotplug_interrupt_update_locked(struct drm_i915_private *dev_priv,
1770706f17cSEgbert Eich 				     uint32_t mask,
1780706f17cSEgbert Eich 				     uint32_t bits)
1790706f17cSEgbert Eich {
1800706f17cSEgbert Eich 	uint32_t val;
1810706f17cSEgbert Eich 
1820706f17cSEgbert Eich 	assert_spin_locked(&dev_priv->irq_lock);
1830706f17cSEgbert Eich 	WARN_ON(bits & ~mask);
1840706f17cSEgbert Eich 
1850706f17cSEgbert Eich 	val = I915_READ(PORT_HOTPLUG_EN);
1860706f17cSEgbert Eich 	val &= ~mask;
1870706f17cSEgbert Eich 	val |= bits;
1880706f17cSEgbert Eich 	I915_WRITE(PORT_HOTPLUG_EN, val);
1890706f17cSEgbert Eich }
1900706f17cSEgbert Eich 
1910706f17cSEgbert Eich /**
1920706f17cSEgbert Eich  * i915_hotplug_interrupt_update - update hotplug interrupt enable
1930706f17cSEgbert Eich  * @dev_priv: driver private
1940706f17cSEgbert Eich  * @mask: bits to update
1950706f17cSEgbert Eich  * @bits: bits to enable
1960706f17cSEgbert Eich  * NOTE: the HPD enable bits are modified both inside and outside
1970706f17cSEgbert Eich  * of an interrupt context. To avoid that read-modify-write cycles
1980706f17cSEgbert Eich  * interfer, these bits are protected by a spinlock. Since this
1990706f17cSEgbert Eich  * function is usually not called from a context where the lock is
2000706f17cSEgbert Eich  * held already, this function acquires the lock itself. A non-locking
2010706f17cSEgbert Eich  * version is also available.
2020706f17cSEgbert Eich  */
2030706f17cSEgbert Eich void i915_hotplug_interrupt_update(struct drm_i915_private *dev_priv,
2040706f17cSEgbert Eich 				   uint32_t mask,
2050706f17cSEgbert Eich 				   uint32_t bits)
2060706f17cSEgbert Eich {
2070706f17cSEgbert Eich 	spin_lock_irq(&dev_priv->irq_lock);
2080706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv, mask, bits);
2090706f17cSEgbert Eich 	spin_unlock_irq(&dev_priv->irq_lock);
2100706f17cSEgbert Eich }
2110706f17cSEgbert Eich 
212d9dc34f1SVille Syrjälä /**
213d9dc34f1SVille Syrjälä  * ilk_update_display_irq - update DEIMR
214d9dc34f1SVille Syrjälä  * @dev_priv: driver private
215d9dc34f1SVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
216d9dc34f1SVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
217d9dc34f1SVille Syrjälä  */
218fbdedaeaSVille Syrjälä void ilk_update_display_irq(struct drm_i915_private *dev_priv,
219d9dc34f1SVille Syrjälä 			    uint32_t interrupt_mask,
220d9dc34f1SVille Syrjälä 			    uint32_t enabled_irq_mask)
221036a4a7dSZhenyu Wang {
222d9dc34f1SVille Syrjälä 	uint32_t new_val;
223d9dc34f1SVille Syrjälä 
2244bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
2254bc9d430SDaniel Vetter 
226d9dc34f1SVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
227d9dc34f1SVille Syrjälä 
2289df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
229c67a470bSPaulo Zanoni 		return;
230c67a470bSPaulo Zanoni 
231d9dc34f1SVille Syrjälä 	new_val = dev_priv->irq_mask;
232d9dc34f1SVille Syrjälä 	new_val &= ~interrupt_mask;
233d9dc34f1SVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
234d9dc34f1SVille Syrjälä 
235d9dc34f1SVille Syrjälä 	if (new_val != dev_priv->irq_mask) {
236d9dc34f1SVille Syrjälä 		dev_priv->irq_mask = new_val;
2371ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
2383143a2bfSChris Wilson 		POSTING_READ(DEIMR);
239036a4a7dSZhenyu Wang 	}
240036a4a7dSZhenyu Wang }
241036a4a7dSZhenyu Wang 
24243eaea13SPaulo Zanoni /**
24343eaea13SPaulo Zanoni  * ilk_update_gt_irq - update GTIMR
24443eaea13SPaulo Zanoni  * @dev_priv: driver private
24543eaea13SPaulo Zanoni  * @interrupt_mask: mask of interrupt bits to update
24643eaea13SPaulo Zanoni  * @enabled_irq_mask: mask of interrupt bits to enable
24743eaea13SPaulo Zanoni  */
24843eaea13SPaulo Zanoni static void ilk_update_gt_irq(struct drm_i915_private *dev_priv,
24943eaea13SPaulo Zanoni 			      uint32_t interrupt_mask,
25043eaea13SPaulo Zanoni 			      uint32_t enabled_irq_mask)
25143eaea13SPaulo Zanoni {
25243eaea13SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
25343eaea13SPaulo Zanoni 
25415a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
25515a17aaeSDaniel Vetter 
2569df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
257c67a470bSPaulo Zanoni 		return;
258c67a470bSPaulo Zanoni 
25943eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask &= ~interrupt_mask;
26043eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask |= (~enabled_irq_mask & interrupt_mask);
26143eaea13SPaulo Zanoni 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
26243eaea13SPaulo Zanoni }
26343eaea13SPaulo Zanoni 
264480c8033SDaniel Vetter void gen5_enable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
26543eaea13SPaulo Zanoni {
26643eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, mask);
26731bb59ccSChris Wilson 	POSTING_READ_FW(GTIMR);
26843eaea13SPaulo Zanoni }
26943eaea13SPaulo Zanoni 
270480c8033SDaniel Vetter void gen5_disable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
27143eaea13SPaulo Zanoni {
27243eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, 0);
27343eaea13SPaulo Zanoni }
27443eaea13SPaulo Zanoni 
275f0f59a00SVille Syrjälä static i915_reg_t gen6_pm_iir(struct drm_i915_private *dev_priv)
276b900b949SImre Deak {
277b900b949SImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IIR(2) : GEN6_PMIIR;
278b900b949SImre Deak }
279b900b949SImre Deak 
280f0f59a00SVille Syrjälä static i915_reg_t gen6_pm_imr(struct drm_i915_private *dev_priv)
281a72fbc3aSImre Deak {
282a72fbc3aSImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IMR(2) : GEN6_PMIMR;
283a72fbc3aSImre Deak }
284a72fbc3aSImre Deak 
285f0f59a00SVille Syrjälä static i915_reg_t gen6_pm_ier(struct drm_i915_private *dev_priv)
286b900b949SImre Deak {
287b900b949SImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IER(2) : GEN6_PMIER;
288b900b949SImre Deak }
289b900b949SImre Deak 
290edbfdb45SPaulo Zanoni /**
291edbfdb45SPaulo Zanoni  * snb_update_pm_irq - update GEN6_PMIMR
292edbfdb45SPaulo Zanoni  * @dev_priv: driver private
293edbfdb45SPaulo Zanoni  * @interrupt_mask: mask of interrupt bits to update
294edbfdb45SPaulo Zanoni  * @enabled_irq_mask: mask of interrupt bits to enable
295edbfdb45SPaulo Zanoni  */
296edbfdb45SPaulo Zanoni static void snb_update_pm_irq(struct drm_i915_private *dev_priv,
297edbfdb45SPaulo Zanoni 			      uint32_t interrupt_mask,
298edbfdb45SPaulo Zanoni 			      uint32_t enabled_irq_mask)
299edbfdb45SPaulo Zanoni {
300605cd25bSPaulo Zanoni 	uint32_t new_val;
301edbfdb45SPaulo Zanoni 
30215a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
30315a17aaeSDaniel Vetter 
304edbfdb45SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
305edbfdb45SPaulo Zanoni 
306605cd25bSPaulo Zanoni 	new_val = dev_priv->pm_irq_mask;
307f52ecbcfSPaulo Zanoni 	new_val &= ~interrupt_mask;
308f52ecbcfSPaulo Zanoni 	new_val |= (~enabled_irq_mask & interrupt_mask);
309f52ecbcfSPaulo Zanoni 
310605cd25bSPaulo Zanoni 	if (new_val != dev_priv->pm_irq_mask) {
311605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = new_val;
312a72fbc3aSImre Deak 		I915_WRITE(gen6_pm_imr(dev_priv), dev_priv->pm_irq_mask);
313a72fbc3aSImre Deak 		POSTING_READ(gen6_pm_imr(dev_priv));
314edbfdb45SPaulo Zanoni 	}
315f52ecbcfSPaulo Zanoni }
316edbfdb45SPaulo Zanoni 
317480c8033SDaniel Vetter void gen6_enable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
318edbfdb45SPaulo Zanoni {
3199939fba2SImre Deak 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
3209939fba2SImre Deak 		return;
3219939fba2SImre Deak 
322edbfdb45SPaulo Zanoni 	snb_update_pm_irq(dev_priv, mask, mask);
323edbfdb45SPaulo Zanoni }
324edbfdb45SPaulo Zanoni 
3259939fba2SImre Deak static void __gen6_disable_pm_irq(struct drm_i915_private *dev_priv,
3269939fba2SImre Deak 				  uint32_t mask)
3279939fba2SImre Deak {
3289939fba2SImre Deak 	snb_update_pm_irq(dev_priv, mask, 0);
3299939fba2SImre Deak }
3309939fba2SImre Deak 
331480c8033SDaniel Vetter void gen6_disable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
332edbfdb45SPaulo Zanoni {
3339939fba2SImre Deak 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
3349939fba2SImre Deak 		return;
3359939fba2SImre Deak 
3369939fba2SImre Deak 	__gen6_disable_pm_irq(dev_priv, mask);
337edbfdb45SPaulo Zanoni }
338edbfdb45SPaulo Zanoni 
339dc97997aSChris Wilson void gen6_reset_rps_interrupts(struct drm_i915_private *dev_priv)
3403cc134e3SImre Deak {
341f0f59a00SVille Syrjälä 	i915_reg_t reg = gen6_pm_iir(dev_priv);
3423cc134e3SImre Deak 
3433cc134e3SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
3443cc134e3SImre Deak 	I915_WRITE(reg, dev_priv->pm_rps_events);
3453cc134e3SImre Deak 	I915_WRITE(reg, dev_priv->pm_rps_events);
3463cc134e3SImre Deak 	POSTING_READ(reg);
347096fad9eSImre Deak 	dev_priv->rps.pm_iir = 0;
3483cc134e3SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
3493cc134e3SImre Deak }
3503cc134e3SImre Deak 
35191d14251STvrtko Ursulin void gen6_enable_rps_interrupts(struct drm_i915_private *dev_priv)
352b900b949SImre Deak {
353b900b949SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
354c33d247dSChris Wilson 	WARN_ON_ONCE(dev_priv->rps.pm_iir);
355c33d247dSChris Wilson 	WARN_ON_ONCE(I915_READ(gen6_pm_iir(dev_priv)) & dev_priv->pm_rps_events);
356d4d70aa5SImre Deak 	dev_priv->rps.interrupts_enabled = true;
35778e68d36SImre Deak 	I915_WRITE(gen6_pm_ier(dev_priv), I915_READ(gen6_pm_ier(dev_priv)) |
35878e68d36SImre Deak 				dev_priv->pm_rps_events);
359b900b949SImre Deak 	gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
36078e68d36SImre Deak 
361b900b949SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
362b900b949SImre Deak }
363b900b949SImre Deak 
36459d02a1fSImre Deak u32 gen6_sanitize_rps_pm_mask(struct drm_i915_private *dev_priv, u32 mask)
36559d02a1fSImre Deak {
3661800ad25SSagar Arun Kamble 	return (mask & ~dev_priv->rps.pm_intr_keep);
36759d02a1fSImre Deak }
36859d02a1fSImre Deak 
36991d14251STvrtko Ursulin void gen6_disable_rps_interrupts(struct drm_i915_private *dev_priv)
370b900b949SImre Deak {
371d4d70aa5SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
372d4d70aa5SImre Deak 	dev_priv->rps.interrupts_enabled = false;
3739939fba2SImre Deak 
37459d02a1fSImre Deak 	I915_WRITE(GEN6_PMINTRMSK, gen6_sanitize_rps_pm_mask(dev_priv, ~0));
3759939fba2SImre Deak 
3769939fba2SImre Deak 	__gen6_disable_pm_irq(dev_priv, dev_priv->pm_rps_events);
377b900b949SImre Deak 	I915_WRITE(gen6_pm_ier(dev_priv), I915_READ(gen6_pm_ier(dev_priv)) &
378b900b949SImre Deak 				~dev_priv->pm_rps_events);
37958072ccbSImre Deak 
38058072ccbSImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
38191c8a326SChris Wilson 	synchronize_irq(dev_priv->drm.irq);
382c33d247dSChris Wilson 
383c33d247dSChris Wilson 	/* Now that we will not be generating any more work, flush any
384c33d247dSChris Wilson 	 * outsanding tasks. As we are called on the RPS idle path,
385c33d247dSChris Wilson 	 * we will reset the GPU to minimum frequencies, so the current
386c33d247dSChris Wilson 	 * state of the worker can be discarded.
387c33d247dSChris Wilson 	 */
388c33d247dSChris Wilson 	cancel_work_sync(&dev_priv->rps.work);
389c33d247dSChris Wilson 	gen6_reset_rps_interrupts(dev_priv);
390b900b949SImre Deak }
391b900b949SImre Deak 
3920961021aSBen Widawsky /**
3933a3b3c7dSVille Syrjälä  * bdw_update_port_irq - update DE port interrupt
3943a3b3c7dSVille Syrjälä  * @dev_priv: driver private
3953a3b3c7dSVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
3963a3b3c7dSVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
3973a3b3c7dSVille Syrjälä  */
3983a3b3c7dSVille Syrjälä static void bdw_update_port_irq(struct drm_i915_private *dev_priv,
3993a3b3c7dSVille Syrjälä 				uint32_t interrupt_mask,
4003a3b3c7dSVille Syrjälä 				uint32_t enabled_irq_mask)
4013a3b3c7dSVille Syrjälä {
4023a3b3c7dSVille Syrjälä 	uint32_t new_val;
4033a3b3c7dSVille Syrjälä 	uint32_t old_val;
4043a3b3c7dSVille Syrjälä 
4053a3b3c7dSVille Syrjälä 	assert_spin_locked(&dev_priv->irq_lock);
4063a3b3c7dSVille Syrjälä 
4073a3b3c7dSVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
4083a3b3c7dSVille Syrjälä 
4093a3b3c7dSVille Syrjälä 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
4103a3b3c7dSVille Syrjälä 		return;
4113a3b3c7dSVille Syrjälä 
4123a3b3c7dSVille Syrjälä 	old_val = I915_READ(GEN8_DE_PORT_IMR);
4133a3b3c7dSVille Syrjälä 
4143a3b3c7dSVille Syrjälä 	new_val = old_val;
4153a3b3c7dSVille Syrjälä 	new_val &= ~interrupt_mask;
4163a3b3c7dSVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
4173a3b3c7dSVille Syrjälä 
4183a3b3c7dSVille Syrjälä 	if (new_val != old_val) {
4193a3b3c7dSVille Syrjälä 		I915_WRITE(GEN8_DE_PORT_IMR, new_val);
4203a3b3c7dSVille Syrjälä 		POSTING_READ(GEN8_DE_PORT_IMR);
4213a3b3c7dSVille Syrjälä 	}
4223a3b3c7dSVille Syrjälä }
4233a3b3c7dSVille Syrjälä 
4243a3b3c7dSVille Syrjälä /**
425013d3752SVille Syrjälä  * bdw_update_pipe_irq - update DE pipe interrupt
426013d3752SVille Syrjälä  * @dev_priv: driver private
427013d3752SVille Syrjälä  * @pipe: pipe whose interrupt to update
428013d3752SVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
429013d3752SVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
430013d3752SVille Syrjälä  */
431013d3752SVille Syrjälä void bdw_update_pipe_irq(struct drm_i915_private *dev_priv,
432013d3752SVille Syrjälä 			 enum pipe pipe,
433013d3752SVille Syrjälä 			 uint32_t interrupt_mask,
434013d3752SVille Syrjälä 			 uint32_t enabled_irq_mask)
435013d3752SVille Syrjälä {
436013d3752SVille Syrjälä 	uint32_t new_val;
437013d3752SVille Syrjälä 
438013d3752SVille Syrjälä 	assert_spin_locked(&dev_priv->irq_lock);
439013d3752SVille Syrjälä 
440013d3752SVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
441013d3752SVille Syrjälä 
442013d3752SVille Syrjälä 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
443013d3752SVille Syrjälä 		return;
444013d3752SVille Syrjälä 
445013d3752SVille Syrjälä 	new_val = dev_priv->de_irq_mask[pipe];
446013d3752SVille Syrjälä 	new_val &= ~interrupt_mask;
447013d3752SVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
448013d3752SVille Syrjälä 
449013d3752SVille Syrjälä 	if (new_val != dev_priv->de_irq_mask[pipe]) {
450013d3752SVille Syrjälä 		dev_priv->de_irq_mask[pipe] = new_val;
451013d3752SVille Syrjälä 		I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
452013d3752SVille Syrjälä 		POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
453013d3752SVille Syrjälä 	}
454013d3752SVille Syrjälä }
455013d3752SVille Syrjälä 
456013d3752SVille Syrjälä /**
457fee884edSDaniel Vetter  * ibx_display_interrupt_update - update SDEIMR
458fee884edSDaniel Vetter  * @dev_priv: driver private
459fee884edSDaniel Vetter  * @interrupt_mask: mask of interrupt bits to update
460fee884edSDaniel Vetter  * @enabled_irq_mask: mask of interrupt bits to enable
461fee884edSDaniel Vetter  */
46247339cd9SDaniel Vetter void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
463fee884edSDaniel Vetter 				  uint32_t interrupt_mask,
464fee884edSDaniel Vetter 				  uint32_t enabled_irq_mask)
465fee884edSDaniel Vetter {
466fee884edSDaniel Vetter 	uint32_t sdeimr = I915_READ(SDEIMR);
467fee884edSDaniel Vetter 	sdeimr &= ~interrupt_mask;
468fee884edSDaniel Vetter 	sdeimr |= (~enabled_irq_mask & interrupt_mask);
469fee884edSDaniel Vetter 
47015a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
47115a17aaeSDaniel Vetter 
472fee884edSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
473fee884edSDaniel Vetter 
4749df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
475c67a470bSPaulo Zanoni 		return;
476c67a470bSPaulo Zanoni 
477fee884edSDaniel Vetter 	I915_WRITE(SDEIMR, sdeimr);
478fee884edSDaniel Vetter 	POSTING_READ(SDEIMR);
479fee884edSDaniel Vetter }
4808664281bSPaulo Zanoni 
481b5ea642aSDaniel Vetter static void
482755e9019SImre Deak __i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
483755e9019SImre Deak 		       u32 enable_mask, u32 status_mask)
4847c463586SKeith Packard {
485f0f59a00SVille Syrjälä 	i915_reg_t reg = PIPESTAT(pipe);
486755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
4877c463586SKeith Packard 
488b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
489d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
490b79480baSDaniel Vetter 
49104feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
49204feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
49304feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
49404feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
495755e9019SImre Deak 		return;
496755e9019SImre Deak 
497755e9019SImre Deak 	if ((pipestat & enable_mask) == enable_mask)
49846c06a30SVille Syrjälä 		return;
49946c06a30SVille Syrjälä 
50091d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] |= status_mask;
50191d181ddSImre Deak 
5027c463586SKeith Packard 	/* Enable the interrupt, clear any pending status */
503755e9019SImre Deak 	pipestat |= enable_mask | status_mask;
50446c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
5053143a2bfSChris Wilson 	POSTING_READ(reg);
5067c463586SKeith Packard }
5077c463586SKeith Packard 
508b5ea642aSDaniel Vetter static void
509755e9019SImre Deak __i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
510755e9019SImre Deak 		        u32 enable_mask, u32 status_mask)
5117c463586SKeith Packard {
512f0f59a00SVille Syrjälä 	i915_reg_t reg = PIPESTAT(pipe);
513755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
5147c463586SKeith Packard 
515b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
516d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
517b79480baSDaniel Vetter 
51804feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
51904feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
52004feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
52104feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
52246c06a30SVille Syrjälä 		return;
52346c06a30SVille Syrjälä 
524755e9019SImre Deak 	if ((pipestat & enable_mask) == 0)
525755e9019SImre Deak 		return;
526755e9019SImre Deak 
52791d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] &= ~status_mask;
52891d181ddSImre Deak 
529755e9019SImre Deak 	pipestat &= ~enable_mask;
53046c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
5313143a2bfSChris Wilson 	POSTING_READ(reg);
5327c463586SKeith Packard }
5337c463586SKeith Packard 
53410c59c51SImre Deak static u32 vlv_get_pipestat_enable_mask(struct drm_device *dev, u32 status_mask)
53510c59c51SImre Deak {
53610c59c51SImre Deak 	u32 enable_mask = status_mask << 16;
53710c59c51SImre Deak 
53810c59c51SImre Deak 	/*
539724a6905SVille Syrjälä 	 * On pipe A we don't support the PSR interrupt yet,
540724a6905SVille Syrjälä 	 * on pipe B and C the same bit MBZ.
54110c59c51SImre Deak 	 */
54210c59c51SImre Deak 	if (WARN_ON_ONCE(status_mask & PIPE_A_PSR_STATUS_VLV))
54310c59c51SImre Deak 		return 0;
544724a6905SVille Syrjälä 	/*
545724a6905SVille Syrjälä 	 * On pipe B and C we don't support the PSR interrupt yet, on pipe
546724a6905SVille Syrjälä 	 * A the same bit is for perf counters which we don't use either.
547724a6905SVille Syrjälä 	 */
548724a6905SVille Syrjälä 	if (WARN_ON_ONCE(status_mask & PIPE_B_PSR_STATUS_VLV))
549724a6905SVille Syrjälä 		return 0;
55010c59c51SImre Deak 
55110c59c51SImre Deak 	enable_mask &= ~(PIPE_FIFO_UNDERRUN_STATUS |
55210c59c51SImre Deak 			 SPRITE0_FLIP_DONE_INT_EN_VLV |
55310c59c51SImre Deak 			 SPRITE1_FLIP_DONE_INT_EN_VLV);
55410c59c51SImre Deak 	if (status_mask & SPRITE0_FLIP_DONE_INT_STATUS_VLV)
55510c59c51SImre Deak 		enable_mask |= SPRITE0_FLIP_DONE_INT_EN_VLV;
55610c59c51SImre Deak 	if (status_mask & SPRITE1_FLIP_DONE_INT_STATUS_VLV)
55710c59c51SImre Deak 		enable_mask |= SPRITE1_FLIP_DONE_INT_EN_VLV;
55810c59c51SImre Deak 
55910c59c51SImre Deak 	return enable_mask;
56010c59c51SImre Deak }
56110c59c51SImre Deak 
562755e9019SImre Deak void
563755e9019SImre Deak i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
564755e9019SImre Deak 		     u32 status_mask)
565755e9019SImre Deak {
566755e9019SImre Deak 	u32 enable_mask;
567755e9019SImre Deak 
568666a4537SWayne Boyer 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
56991c8a326SChris Wilson 		enable_mask = vlv_get_pipestat_enable_mask(&dev_priv->drm,
57010c59c51SImre Deak 							   status_mask);
57110c59c51SImre Deak 	else
572755e9019SImre Deak 		enable_mask = status_mask << 16;
573755e9019SImre Deak 	__i915_enable_pipestat(dev_priv, pipe, enable_mask, status_mask);
574755e9019SImre Deak }
575755e9019SImre Deak 
576755e9019SImre Deak void
577755e9019SImre Deak i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
578755e9019SImre Deak 		      u32 status_mask)
579755e9019SImre Deak {
580755e9019SImre Deak 	u32 enable_mask;
581755e9019SImre Deak 
582666a4537SWayne Boyer 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
58391c8a326SChris Wilson 		enable_mask = vlv_get_pipestat_enable_mask(&dev_priv->drm,
58410c59c51SImre Deak 							   status_mask);
58510c59c51SImre Deak 	else
586755e9019SImre Deak 		enable_mask = status_mask << 16;
587755e9019SImre Deak 	__i915_disable_pipestat(dev_priv, pipe, enable_mask, status_mask);
588755e9019SImre Deak }
589755e9019SImre Deak 
590c0e09200SDave Airlie /**
591f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
59214bb2c11STvrtko Ursulin  * @dev_priv: i915 device private
59301c66889SZhao Yakui  */
59491d14251STvrtko Ursulin static void i915_enable_asle_pipestat(struct drm_i915_private *dev_priv)
59501c66889SZhao Yakui {
59691d14251STvrtko Ursulin 	if (!dev_priv->opregion.asle || !IS_MOBILE(dev_priv))
597f49e38ddSJani Nikula 		return;
598f49e38ddSJani Nikula 
59913321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
60001c66889SZhao Yakui 
601755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_STATUS);
60291d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 4)
6033b6c42e8SDaniel Vetter 		i915_enable_pipestat(dev_priv, PIPE_A,
604755e9019SImre Deak 				     PIPE_LEGACY_BLC_EVENT_STATUS);
6051ec14ad3SChris Wilson 
60613321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
60701c66889SZhao Yakui }
60801c66889SZhao Yakui 
609f75f3746SVille Syrjälä /*
610f75f3746SVille Syrjälä  * This timing diagram depicts the video signal in and
611f75f3746SVille Syrjälä  * around the vertical blanking period.
612f75f3746SVille Syrjälä  *
613f75f3746SVille Syrjälä  * Assumptions about the fictitious mode used in this example:
614f75f3746SVille Syrjälä  *  vblank_start >= 3
615f75f3746SVille Syrjälä  *  vsync_start = vblank_start + 1
616f75f3746SVille Syrjälä  *  vsync_end = vblank_start + 2
617f75f3746SVille Syrjälä  *  vtotal = vblank_start + 3
618f75f3746SVille Syrjälä  *
619f75f3746SVille Syrjälä  *           start of vblank:
620f75f3746SVille Syrjälä  *           latch double buffered registers
621f75f3746SVille Syrjälä  *           increment frame counter (ctg+)
622f75f3746SVille Syrjälä  *           generate start of vblank interrupt (gen4+)
623f75f3746SVille Syrjälä  *           |
624f75f3746SVille Syrjälä  *           |          frame start:
625f75f3746SVille Syrjälä  *           |          generate frame start interrupt (aka. vblank interrupt) (gmch)
626f75f3746SVille Syrjälä  *           |          may be shifted forward 1-3 extra lines via PIPECONF
627f75f3746SVille Syrjälä  *           |          |
628f75f3746SVille Syrjälä  *           |          |  start of vsync:
629f75f3746SVille Syrjälä  *           |          |  generate vsync interrupt
630f75f3746SVille Syrjälä  *           |          |  |
631f75f3746SVille Syrjälä  * ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx
632f75f3746SVille Syrjälä  *       .   \hs/   .      \hs/          \hs/          \hs/   .      \hs/
633f75f3746SVille Syrjälä  * ----va---> <-----------------vb--------------------> <--------va-------------
634f75f3746SVille Syrjälä  *       |          |       <----vs----->                     |
635f75f3746SVille Syrjälä  * -vbs-----> <---vbs+1---> <---vbs+2---> <-----0-----> <-----1-----> <-----2--- (scanline counter gen2)
636f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2---> <-----0--- (scanline counter gen3+)
637f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2- (scanline counter hsw+ hdmi)
638f75f3746SVille Syrjälä  *       |          |                                         |
639f75f3746SVille Syrjälä  *       last visible pixel                                   first visible pixel
640f75f3746SVille Syrjälä  *                  |                                         increment frame counter (gen3/4)
641f75f3746SVille Syrjälä  *                  pixel counter = vblank_start * htotal     pixel counter = 0 (gen3/4)
642f75f3746SVille Syrjälä  *
643f75f3746SVille Syrjälä  * x  = horizontal active
644f75f3746SVille Syrjälä  * _  = horizontal blanking
645f75f3746SVille Syrjälä  * hs = horizontal sync
646f75f3746SVille Syrjälä  * va = vertical active
647f75f3746SVille Syrjälä  * vb = vertical blanking
648f75f3746SVille Syrjälä  * vs = vertical sync
649f75f3746SVille Syrjälä  * vbs = vblank_start (number)
650f75f3746SVille Syrjälä  *
651f75f3746SVille Syrjälä  * Summary:
652f75f3746SVille Syrjälä  * - most events happen at the start of horizontal sync
653f75f3746SVille Syrjälä  * - frame start happens at the start of horizontal blank, 1-4 lines
654f75f3746SVille Syrjälä  *   (depending on PIPECONF settings) after the start of vblank
655f75f3746SVille Syrjälä  * - gen3/4 pixel and frame counter are synchronized with the start
656f75f3746SVille Syrjälä  *   of horizontal active on the first line of vertical active
657f75f3746SVille Syrjälä  */
658f75f3746SVille Syrjälä 
65942f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
66042f52ef8SKeith Packard  * we use as a pipe index
66142f52ef8SKeith Packard  */
66288e72717SThierry Reding static u32 i915_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
6630a3e67a4SJesse Barnes {
664fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
665f0f59a00SVille Syrjälä 	i915_reg_t high_frame, low_frame;
6660b2a8e09SVille Syrjälä 	u32 high1, high2, low, pixel, vbl_start, hsync_start, htotal;
667391f75e2SVille Syrjälä 	struct intel_crtc *intel_crtc =
668391f75e2SVille Syrjälä 		to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
669fc467a22SMaarten Lankhorst 	const struct drm_display_mode *mode = &intel_crtc->base.hwmode;
670391f75e2SVille Syrjälä 
6710b2a8e09SVille Syrjälä 	htotal = mode->crtc_htotal;
6720b2a8e09SVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
6730b2a8e09SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
6740b2a8e09SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
6750b2a8e09SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
676391f75e2SVille Syrjälä 
6770b2a8e09SVille Syrjälä 	/* Convert to pixel count */
6780b2a8e09SVille Syrjälä 	vbl_start *= htotal;
6790b2a8e09SVille Syrjälä 
6800b2a8e09SVille Syrjälä 	/* Start of vblank event occurs at start of hsync */
6810b2a8e09SVille Syrjälä 	vbl_start -= htotal - hsync_start;
6820b2a8e09SVille Syrjälä 
6839db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
6849db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
6855eddb70bSChris Wilson 
6860a3e67a4SJesse Barnes 	/*
6870a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
6880a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
6890a3e67a4SJesse Barnes 	 * register.
6900a3e67a4SJesse Barnes 	 */
6910a3e67a4SJesse Barnes 	do {
6925eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
693391f75e2SVille Syrjälä 		low   = I915_READ(low_frame);
6945eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
6950a3e67a4SJesse Barnes 	} while (high1 != high2);
6960a3e67a4SJesse Barnes 
6975eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
698391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
6995eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
700391f75e2SVille Syrjälä 
701391f75e2SVille Syrjälä 	/*
702391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
703391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
704391f75e2SVille Syrjälä 	 * counter against vblank start.
705391f75e2SVille Syrjälä 	 */
706edc08d0aSVille Syrjälä 	return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
7070a3e67a4SJesse Barnes }
7080a3e67a4SJesse Barnes 
709974e59baSDave Airlie static u32 g4x_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
7109880b7a5SJesse Barnes {
711fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
7129880b7a5SJesse Barnes 
713649636efSVille Syrjälä 	return I915_READ(PIPE_FRMCOUNT_G4X(pipe));
7149880b7a5SJesse Barnes }
7159880b7a5SJesse Barnes 
71675aa3f63SVille Syrjälä /* I915_READ_FW, only for fast reads of display block, no need for forcewake etc. */
717a225f079SVille Syrjälä static int __intel_get_crtc_scanline(struct intel_crtc *crtc)
718a225f079SVille Syrjälä {
719a225f079SVille Syrjälä 	struct drm_device *dev = crtc->base.dev;
720fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
721fc467a22SMaarten Lankhorst 	const struct drm_display_mode *mode = &crtc->base.hwmode;
722a225f079SVille Syrjälä 	enum pipe pipe = crtc->pipe;
72380715b2fSVille Syrjälä 	int position, vtotal;
724a225f079SVille Syrjälä 
72580715b2fSVille Syrjälä 	vtotal = mode->crtc_vtotal;
726a225f079SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
727a225f079SVille Syrjälä 		vtotal /= 2;
728a225f079SVille Syrjälä 
72991d14251STvrtko Ursulin 	if (IS_GEN2(dev_priv))
73075aa3f63SVille Syrjälä 		position = I915_READ_FW(PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
731a225f079SVille Syrjälä 	else
73275aa3f63SVille Syrjälä 		position = I915_READ_FW(PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
733a225f079SVille Syrjälä 
734a225f079SVille Syrjälä 	/*
73541b578fbSJesse Barnes 	 * On HSW, the DSL reg (0x70000) appears to return 0 if we
73641b578fbSJesse Barnes 	 * read it just before the start of vblank.  So try it again
73741b578fbSJesse Barnes 	 * so we don't accidentally end up spanning a vblank frame
73841b578fbSJesse Barnes 	 * increment, causing the pipe_update_end() code to squak at us.
73941b578fbSJesse Barnes 	 *
74041b578fbSJesse Barnes 	 * The nature of this problem means we can't simply check the ISR
74141b578fbSJesse Barnes 	 * bit and return the vblank start value; nor can we use the scanline
74241b578fbSJesse Barnes 	 * debug register in the transcoder as it appears to have the same
74341b578fbSJesse Barnes 	 * problem.  We may need to extend this to include other platforms,
74441b578fbSJesse Barnes 	 * but so far testing only shows the problem on HSW.
74541b578fbSJesse Barnes 	 */
74691d14251STvrtko Ursulin 	if (HAS_DDI(dev_priv) && !position) {
74741b578fbSJesse Barnes 		int i, temp;
74841b578fbSJesse Barnes 
74941b578fbSJesse Barnes 		for (i = 0; i < 100; i++) {
75041b578fbSJesse Barnes 			udelay(1);
75141b578fbSJesse Barnes 			temp = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) &
75241b578fbSJesse Barnes 				DSL_LINEMASK_GEN3;
75341b578fbSJesse Barnes 			if (temp != position) {
75441b578fbSJesse Barnes 				position = temp;
75541b578fbSJesse Barnes 				break;
75641b578fbSJesse Barnes 			}
75741b578fbSJesse Barnes 		}
75841b578fbSJesse Barnes 	}
75941b578fbSJesse Barnes 
76041b578fbSJesse Barnes 	/*
76180715b2fSVille Syrjälä 	 * See update_scanline_offset() for the details on the
76280715b2fSVille Syrjälä 	 * scanline_offset adjustment.
763a225f079SVille Syrjälä 	 */
76480715b2fSVille Syrjälä 	return (position + crtc->scanline_offset) % vtotal;
765a225f079SVille Syrjälä }
766a225f079SVille Syrjälä 
76788e72717SThierry Reding static int i915_get_crtc_scanoutpos(struct drm_device *dev, unsigned int pipe,
768abca9e45SVille Syrjälä 				    unsigned int flags, int *vpos, int *hpos,
7693bb403bfSVille Syrjälä 				    ktime_t *stime, ktime_t *etime,
7703bb403bfSVille Syrjälä 				    const struct drm_display_mode *mode)
7710af7e4dfSMario Kleiner {
772fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
773c2baf4b7SVille Syrjälä 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
774c2baf4b7SVille Syrjälä 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7753aa18df8SVille Syrjälä 	int position;
77678e8fc6bSVille Syrjälä 	int vbl_start, vbl_end, hsync_start, htotal, vtotal;
7770af7e4dfSMario Kleiner 	bool in_vbl = true;
7780af7e4dfSMario Kleiner 	int ret = 0;
779ad3543edSMario Kleiner 	unsigned long irqflags;
7800af7e4dfSMario Kleiner 
781fc467a22SMaarten Lankhorst 	if (WARN_ON(!mode->crtc_clock)) {
7820af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
7839db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
7840af7e4dfSMario Kleiner 		return 0;
7850af7e4dfSMario Kleiner 	}
7860af7e4dfSMario Kleiner 
787c2baf4b7SVille Syrjälä 	htotal = mode->crtc_htotal;
78878e8fc6bSVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
789c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
790c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
791c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
7920af7e4dfSMario Kleiner 
793d31faf65SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
794d31faf65SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
795d31faf65SVille Syrjälä 		vbl_end /= 2;
796d31faf65SVille Syrjälä 		vtotal /= 2;
797d31faf65SVille Syrjälä 	}
798d31faf65SVille Syrjälä 
799c2baf4b7SVille Syrjälä 	ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
800c2baf4b7SVille Syrjälä 
801ad3543edSMario Kleiner 	/*
802ad3543edSMario Kleiner 	 * Lock uncore.lock, as we will do multiple timing critical raw
803ad3543edSMario Kleiner 	 * register reads, potentially with preemption disabled, so the
804ad3543edSMario Kleiner 	 * following code must not block on uncore.lock.
805ad3543edSMario Kleiner 	 */
806ad3543edSMario Kleiner 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
807ad3543edSMario Kleiner 
808ad3543edSMario Kleiner 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
809ad3543edSMario Kleiner 
810ad3543edSMario Kleiner 	/* Get optional system timestamp before query. */
811ad3543edSMario Kleiner 	if (stime)
812ad3543edSMario Kleiner 		*stime = ktime_get();
813ad3543edSMario Kleiner 
81491d14251STvrtko Ursulin 	if (IS_GEN2(dev_priv) || IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5) {
8150af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
8160af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
8170af7e4dfSMario Kleiner 		 */
818a225f079SVille Syrjälä 		position = __intel_get_crtc_scanline(intel_crtc);
8190af7e4dfSMario Kleiner 	} else {
8200af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
8210af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
8220af7e4dfSMario Kleiner 		 * scanout position.
8230af7e4dfSMario Kleiner 		 */
82475aa3f63SVille Syrjälä 		position = (I915_READ_FW(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
8250af7e4dfSMario Kleiner 
8263aa18df8SVille Syrjälä 		/* convert to pixel counts */
8273aa18df8SVille Syrjälä 		vbl_start *= htotal;
8283aa18df8SVille Syrjälä 		vbl_end *= htotal;
8293aa18df8SVille Syrjälä 		vtotal *= htotal;
83078e8fc6bSVille Syrjälä 
83178e8fc6bSVille Syrjälä 		/*
8327e78f1cbSVille Syrjälä 		 * In interlaced modes, the pixel counter counts all pixels,
8337e78f1cbSVille Syrjälä 		 * so one field will have htotal more pixels. In order to avoid
8347e78f1cbSVille Syrjälä 		 * the reported position from jumping backwards when the pixel
8357e78f1cbSVille Syrjälä 		 * counter is beyond the length of the shorter field, just
8367e78f1cbSVille Syrjälä 		 * clamp the position the length of the shorter field. This
8377e78f1cbSVille Syrjälä 		 * matches how the scanline counter based position works since
8387e78f1cbSVille Syrjälä 		 * the scanline counter doesn't count the two half lines.
8397e78f1cbSVille Syrjälä 		 */
8407e78f1cbSVille Syrjälä 		if (position >= vtotal)
8417e78f1cbSVille Syrjälä 			position = vtotal - 1;
8427e78f1cbSVille Syrjälä 
8437e78f1cbSVille Syrjälä 		/*
84478e8fc6bSVille Syrjälä 		 * Start of vblank interrupt is triggered at start of hsync,
84578e8fc6bSVille Syrjälä 		 * just prior to the first active line of vblank. However we
84678e8fc6bSVille Syrjälä 		 * consider lines to start at the leading edge of horizontal
84778e8fc6bSVille Syrjälä 		 * active. So, should we get here before we've crossed into
84878e8fc6bSVille Syrjälä 		 * the horizontal active of the first line in vblank, we would
84978e8fc6bSVille Syrjälä 		 * not set the DRM_SCANOUTPOS_INVBL flag. In order to fix that,
85078e8fc6bSVille Syrjälä 		 * always add htotal-hsync_start to the current pixel position.
85178e8fc6bSVille Syrjälä 		 */
85278e8fc6bSVille Syrjälä 		position = (position + htotal - hsync_start) % vtotal;
8533aa18df8SVille Syrjälä 	}
8543aa18df8SVille Syrjälä 
855ad3543edSMario Kleiner 	/* Get optional system timestamp after query. */
856ad3543edSMario Kleiner 	if (etime)
857ad3543edSMario Kleiner 		*etime = ktime_get();
858ad3543edSMario Kleiner 
859ad3543edSMario Kleiner 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
860ad3543edSMario Kleiner 
861ad3543edSMario Kleiner 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
862ad3543edSMario Kleiner 
8633aa18df8SVille Syrjälä 	in_vbl = position >= vbl_start && position < vbl_end;
8643aa18df8SVille Syrjälä 
8653aa18df8SVille Syrjälä 	/*
8663aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
8673aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
8683aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
8693aa18df8SVille Syrjälä 	 * up since vbl_end.
8703aa18df8SVille Syrjälä 	 */
8713aa18df8SVille Syrjälä 	if (position >= vbl_start)
8723aa18df8SVille Syrjälä 		position -= vbl_end;
8733aa18df8SVille Syrjälä 	else
8743aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
8753aa18df8SVille Syrjälä 
87691d14251STvrtko Ursulin 	if (IS_GEN2(dev_priv) || IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5) {
8773aa18df8SVille Syrjälä 		*vpos = position;
8783aa18df8SVille Syrjälä 		*hpos = 0;
8793aa18df8SVille Syrjälä 	} else {
8800af7e4dfSMario Kleiner 		*vpos = position / htotal;
8810af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
8820af7e4dfSMario Kleiner 	}
8830af7e4dfSMario Kleiner 
8840af7e4dfSMario Kleiner 	/* In vblank? */
8850af7e4dfSMario Kleiner 	if (in_vbl)
8863d3cbd84SDaniel Vetter 		ret |= DRM_SCANOUTPOS_IN_VBLANK;
8870af7e4dfSMario Kleiner 
8880af7e4dfSMario Kleiner 	return ret;
8890af7e4dfSMario Kleiner }
8900af7e4dfSMario Kleiner 
891a225f079SVille Syrjälä int intel_get_crtc_scanline(struct intel_crtc *crtc)
892a225f079SVille Syrjälä {
893fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
894a225f079SVille Syrjälä 	unsigned long irqflags;
895a225f079SVille Syrjälä 	int position;
896a225f079SVille Syrjälä 
897a225f079SVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
898a225f079SVille Syrjälä 	position = __intel_get_crtc_scanline(crtc);
899a225f079SVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
900a225f079SVille Syrjälä 
901a225f079SVille Syrjälä 	return position;
902a225f079SVille Syrjälä }
903a225f079SVille Syrjälä 
90488e72717SThierry Reding static int i915_get_vblank_timestamp(struct drm_device *dev, unsigned int pipe,
9050af7e4dfSMario Kleiner 			      int *max_error,
9060af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
9070af7e4dfSMario Kleiner 			      unsigned flags)
9080af7e4dfSMario Kleiner {
9094041b853SChris Wilson 	struct drm_crtc *crtc;
9100af7e4dfSMario Kleiner 
91188e72717SThierry Reding 	if (pipe >= INTEL_INFO(dev)->num_pipes) {
91288e72717SThierry Reding 		DRM_ERROR("Invalid crtc %u\n", pipe);
9130af7e4dfSMario Kleiner 		return -EINVAL;
9140af7e4dfSMario Kleiner 	}
9150af7e4dfSMario Kleiner 
9160af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
9174041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
9184041b853SChris Wilson 	if (crtc == NULL) {
91988e72717SThierry Reding 		DRM_ERROR("Invalid crtc %u\n", pipe);
9204041b853SChris Wilson 		return -EINVAL;
9214041b853SChris Wilson 	}
9224041b853SChris Wilson 
923fc467a22SMaarten Lankhorst 	if (!crtc->hwmode.crtc_clock) {
92488e72717SThierry Reding 		DRM_DEBUG_KMS("crtc %u is disabled\n", pipe);
9254041b853SChris Wilson 		return -EBUSY;
9264041b853SChris Wilson 	}
9270af7e4dfSMario Kleiner 
9280af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
9294041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
9304041b853SChris Wilson 						     vblank_time, flags,
931fc467a22SMaarten Lankhorst 						     &crtc->hwmode);
9320af7e4dfSMario Kleiner }
9330af7e4dfSMario Kleiner 
93491d14251STvrtko Ursulin static void ironlake_rps_change_irq_handler(struct drm_i915_private *dev_priv)
935f97108d1SJesse Barnes {
936b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
9379270388eSDaniel Vetter 	u8 new_delay;
9389270388eSDaniel Vetter 
939d0ecd7e2SDaniel Vetter 	spin_lock(&mchdev_lock);
940f97108d1SJesse Barnes 
94173edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
94273edd18fSDaniel Vetter 
94320e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
9449270388eSDaniel Vetter 
9457648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
946b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
947b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
948f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
949f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
950f97108d1SJesse Barnes 
951f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
952b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
95320e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
95420e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
95520e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
95620e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
957b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
95820e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
95920e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
96020e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
96120e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
962f97108d1SJesse Barnes 	}
963f97108d1SJesse Barnes 
96491d14251STvrtko Ursulin 	if (ironlake_set_drps(dev_priv, new_delay))
96520e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
966f97108d1SJesse Barnes 
967d0ecd7e2SDaniel Vetter 	spin_unlock(&mchdev_lock);
9689270388eSDaniel Vetter 
969f97108d1SJesse Barnes 	return;
970f97108d1SJesse Barnes }
971f97108d1SJesse Barnes 
9720bc40be8STvrtko Ursulin static void notify_ring(struct intel_engine_cs *engine)
973549f7365SChris Wilson {
974aca34b6eSChris Wilson 	smp_store_mb(engine->breadcrumbs.irq_posted, true);
97583348ba8SChris Wilson 	if (intel_engine_wakeup(engine))
9760bc40be8STvrtko Ursulin 		trace_i915_gem_request_notify(engine);
977549f7365SChris Wilson }
978549f7365SChris Wilson 
97943cf3bf0SChris Wilson static void vlv_c0_read(struct drm_i915_private *dev_priv,
98043cf3bf0SChris Wilson 			struct intel_rps_ei *ei)
98131685c25SDeepak S {
98243cf3bf0SChris Wilson 	ei->cz_clock = vlv_punit_read(dev_priv, PUNIT_REG_CZ_TIMESTAMP);
98343cf3bf0SChris Wilson 	ei->render_c0 = I915_READ(VLV_RENDER_C0_COUNT);
98443cf3bf0SChris Wilson 	ei->media_c0 = I915_READ(VLV_MEDIA_C0_COUNT);
98531685c25SDeepak S }
98631685c25SDeepak S 
98743cf3bf0SChris Wilson static bool vlv_c0_above(struct drm_i915_private *dev_priv,
98843cf3bf0SChris Wilson 			 const struct intel_rps_ei *old,
98943cf3bf0SChris Wilson 			 const struct intel_rps_ei *now,
99043cf3bf0SChris Wilson 			 int threshold)
99131685c25SDeepak S {
99243cf3bf0SChris Wilson 	u64 time, c0;
9937bad74d5SVille Syrjälä 	unsigned int mul = 100;
99431685c25SDeepak S 
99543cf3bf0SChris Wilson 	if (old->cz_clock == 0)
99643cf3bf0SChris Wilson 		return false;
99731685c25SDeepak S 
9987bad74d5SVille Syrjälä 	if (I915_READ(VLV_COUNTER_CONTROL) & VLV_COUNT_RANGE_HIGH)
9997bad74d5SVille Syrjälä 		mul <<= 8;
10007bad74d5SVille Syrjälä 
100143cf3bf0SChris Wilson 	time = now->cz_clock - old->cz_clock;
10027bad74d5SVille Syrjälä 	time *= threshold * dev_priv->czclk_freq;
100331685c25SDeepak S 
100443cf3bf0SChris Wilson 	/* Workload can be split between render + media, e.g. SwapBuffers
100543cf3bf0SChris Wilson 	 * being blitted in X after being rendered in mesa. To account for
100643cf3bf0SChris Wilson 	 * this we need to combine both engines into our activity counter.
100743cf3bf0SChris Wilson 	 */
100843cf3bf0SChris Wilson 	c0 = now->render_c0 - old->render_c0;
100943cf3bf0SChris Wilson 	c0 += now->media_c0 - old->media_c0;
10107bad74d5SVille Syrjälä 	c0 *= mul * VLV_CZ_CLOCK_TO_MILLI_SEC;
101131685c25SDeepak S 
101243cf3bf0SChris Wilson 	return c0 >= time;
101331685c25SDeepak S }
101431685c25SDeepak S 
101543cf3bf0SChris Wilson void gen6_rps_reset_ei(struct drm_i915_private *dev_priv)
101643cf3bf0SChris Wilson {
101743cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &dev_priv->rps.down_ei);
101843cf3bf0SChris Wilson 	dev_priv->rps.up_ei = dev_priv->rps.down_ei;
101943cf3bf0SChris Wilson }
102043cf3bf0SChris Wilson 
102143cf3bf0SChris Wilson static u32 vlv_wa_c0_ei(struct drm_i915_private *dev_priv, u32 pm_iir)
102243cf3bf0SChris Wilson {
102343cf3bf0SChris Wilson 	struct intel_rps_ei now;
102443cf3bf0SChris Wilson 	u32 events = 0;
102543cf3bf0SChris Wilson 
10266f4b12f8SChris Wilson 	if ((pm_iir & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED)) == 0)
102743cf3bf0SChris Wilson 		return 0;
102843cf3bf0SChris Wilson 
102943cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &now);
103043cf3bf0SChris Wilson 	if (now.cz_clock == 0)
103143cf3bf0SChris Wilson 		return 0;
103231685c25SDeepak S 
103343cf3bf0SChris Wilson 	if (pm_iir & GEN6_PM_RP_DOWN_EI_EXPIRED) {
103443cf3bf0SChris Wilson 		if (!vlv_c0_above(dev_priv,
103543cf3bf0SChris Wilson 				  &dev_priv->rps.down_ei, &now,
10368fb55197SChris Wilson 				  dev_priv->rps.down_threshold))
103743cf3bf0SChris Wilson 			events |= GEN6_PM_RP_DOWN_THRESHOLD;
103843cf3bf0SChris Wilson 		dev_priv->rps.down_ei = now;
103931685c25SDeepak S 	}
104031685c25SDeepak S 
104143cf3bf0SChris Wilson 	if (pm_iir & GEN6_PM_RP_UP_EI_EXPIRED) {
104243cf3bf0SChris Wilson 		if (vlv_c0_above(dev_priv,
104343cf3bf0SChris Wilson 				 &dev_priv->rps.up_ei, &now,
10448fb55197SChris Wilson 				 dev_priv->rps.up_threshold))
104543cf3bf0SChris Wilson 			events |= GEN6_PM_RP_UP_THRESHOLD;
104643cf3bf0SChris Wilson 		dev_priv->rps.up_ei = now;
104743cf3bf0SChris Wilson 	}
104843cf3bf0SChris Wilson 
104943cf3bf0SChris Wilson 	return events;
105031685c25SDeepak S }
105131685c25SDeepak S 
1052f5a4c67dSChris Wilson static bool any_waiters(struct drm_i915_private *dev_priv)
1053f5a4c67dSChris Wilson {
1054e2f80391STvrtko Ursulin 	struct intel_engine_cs *engine;
1055f5a4c67dSChris Wilson 
1056b4ac5afcSDave Gordon 	for_each_engine(engine, dev_priv)
1057688e6c72SChris Wilson 		if (intel_engine_has_waiter(engine))
1058f5a4c67dSChris Wilson 			return true;
1059f5a4c67dSChris Wilson 
1060f5a4c67dSChris Wilson 	return false;
1061f5a4c67dSChris Wilson }
1062f5a4c67dSChris Wilson 
10634912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
10643b8d8d91SJesse Barnes {
10652d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
10662d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, rps.work);
10678d3afd7dSChris Wilson 	bool client_boost;
10688d3afd7dSChris Wilson 	int new_delay, adj, min, max;
1069edbfdb45SPaulo Zanoni 	u32 pm_iir;
10703b8d8d91SJesse Barnes 
107159cdb63dSDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
1072d4d70aa5SImre Deak 	/* Speed up work cancelation during disabling rps interrupts. */
1073d4d70aa5SImre Deak 	if (!dev_priv->rps.interrupts_enabled) {
1074d4d70aa5SImre Deak 		spin_unlock_irq(&dev_priv->irq_lock);
1075d4d70aa5SImre Deak 		return;
1076d4d70aa5SImre Deak 	}
10771f814dacSImre Deak 
1078c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
1079c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
1080a72fbc3aSImre Deak 	/* Make sure not to corrupt PMIMR state used by ringbuffer on GEN6 */
1081480c8033SDaniel Vetter 	gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
10828d3afd7dSChris Wilson 	client_boost = dev_priv->rps.client_boost;
10838d3afd7dSChris Wilson 	dev_priv->rps.client_boost = false;
108459cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
10854912d041SBen Widawsky 
108660611c13SPaulo Zanoni 	/* Make sure we didn't queue anything we're not going to process. */
1087a6706b45SDeepak S 	WARN_ON(pm_iir & ~dev_priv->pm_rps_events);
108860611c13SPaulo Zanoni 
10898d3afd7dSChris Wilson 	if ((pm_iir & dev_priv->pm_rps_events) == 0 && !client_boost)
1090c33d247dSChris Wilson 		return;
10913b8d8d91SJesse Barnes 
10924fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
10937b9e0ae6SChris Wilson 
109443cf3bf0SChris Wilson 	pm_iir |= vlv_wa_c0_ei(dev_priv, pm_iir);
109543cf3bf0SChris Wilson 
1096dd75fdc8SChris Wilson 	adj = dev_priv->rps.last_adj;
1097edcf284bSChris Wilson 	new_delay = dev_priv->rps.cur_freq;
10988d3afd7dSChris Wilson 	min = dev_priv->rps.min_freq_softlimit;
10998d3afd7dSChris Wilson 	max = dev_priv->rps.max_freq_softlimit;
110029ecd78dSChris Wilson 	if (client_boost || any_waiters(dev_priv))
110129ecd78dSChris Wilson 		max = dev_priv->rps.max_freq;
110229ecd78dSChris Wilson 	if (client_boost && new_delay < dev_priv->rps.boost_freq) {
110329ecd78dSChris Wilson 		new_delay = dev_priv->rps.boost_freq;
11048d3afd7dSChris Wilson 		adj = 0;
11058d3afd7dSChris Wilson 	} else if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
1106dd75fdc8SChris Wilson 		if (adj > 0)
1107dd75fdc8SChris Wilson 			adj *= 2;
1108edcf284bSChris Wilson 		else /* CHV needs even encode values */
1109edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? 2 : 1;
11107425034aSVille Syrjälä 		/*
11117425034aSVille Syrjälä 		 * For better performance, jump directly
11127425034aSVille Syrjälä 		 * to RPe if we're below it.
11137425034aSVille Syrjälä 		 */
1114edcf284bSChris Wilson 		if (new_delay < dev_priv->rps.efficient_freq - adj) {
1115b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1116edcf284bSChris Wilson 			adj = 0;
1117edcf284bSChris Wilson 		}
111829ecd78dSChris Wilson 	} else if (client_boost || any_waiters(dev_priv)) {
1119f5a4c67dSChris Wilson 		adj = 0;
1120dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
1121b39fb297SBen Widawsky 		if (dev_priv->rps.cur_freq > dev_priv->rps.efficient_freq)
1122b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1123dd75fdc8SChris Wilson 		else
1124b39fb297SBen Widawsky 			new_delay = dev_priv->rps.min_freq_softlimit;
1125dd75fdc8SChris Wilson 		adj = 0;
1126dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1127dd75fdc8SChris Wilson 		if (adj < 0)
1128dd75fdc8SChris Wilson 			adj *= 2;
1129edcf284bSChris Wilson 		else /* CHV needs even encode values */
1130edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? -2 : -1;
1131dd75fdc8SChris Wilson 	} else { /* unknown event */
1132edcf284bSChris Wilson 		adj = 0;
1133dd75fdc8SChris Wilson 	}
11343b8d8d91SJesse Barnes 
1135edcf284bSChris Wilson 	dev_priv->rps.last_adj = adj;
1136edcf284bSChris Wilson 
113779249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
113879249636SBen Widawsky 	 * interrupt
113979249636SBen Widawsky 	 */
1140edcf284bSChris Wilson 	new_delay += adj;
11418d3afd7dSChris Wilson 	new_delay = clamp_t(int, new_delay, min, max);
114227544369SDeepak S 
1143dc97997aSChris Wilson 	intel_set_rps(dev_priv, new_delay);
11443b8d8d91SJesse Barnes 
11454fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
11463b8d8d91SJesse Barnes }
11473b8d8d91SJesse Barnes 
1148e3689190SBen Widawsky 
1149e3689190SBen Widawsky /**
1150e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
1151e3689190SBen Widawsky  * occurred.
1152e3689190SBen Widawsky  * @work: workqueue struct
1153e3689190SBen Widawsky  *
1154e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
1155e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
1156e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
1157e3689190SBen Widawsky  */
1158e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
1159e3689190SBen Widawsky {
11602d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
11612d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, l3_parity.error_work);
1162e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
116335a85ac6SBen Widawsky 	char *parity_event[6];
1164e3689190SBen Widawsky 	uint32_t misccpctl;
116535a85ac6SBen Widawsky 	uint8_t slice = 0;
1166e3689190SBen Widawsky 
1167e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
1168e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
1169e3689190SBen Widawsky 	 * any time we access those registers.
1170e3689190SBen Widawsky 	 */
117191c8a326SChris Wilson 	mutex_lock(&dev_priv->drm.struct_mutex);
1172e3689190SBen Widawsky 
117335a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
117435a85ac6SBen Widawsky 	if (WARN_ON(!dev_priv->l3_parity.which_slice))
117535a85ac6SBen Widawsky 		goto out;
117635a85ac6SBen Widawsky 
1177e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
1178e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1179e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
1180e3689190SBen Widawsky 
118135a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
1182f0f59a00SVille Syrjälä 		i915_reg_t reg;
118335a85ac6SBen Widawsky 
118435a85ac6SBen Widawsky 		slice--;
11852d1fe073SJoonas Lahtinen 		if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv)))
118635a85ac6SBen Widawsky 			break;
118735a85ac6SBen Widawsky 
118835a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
118935a85ac6SBen Widawsky 
11906fa1c5f1SVille Syrjälä 		reg = GEN7_L3CDERRST1(slice);
119135a85ac6SBen Widawsky 
119235a85ac6SBen Widawsky 		error_status = I915_READ(reg);
1193e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
1194e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
1195e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1196e3689190SBen Widawsky 
119735a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
119835a85ac6SBen Widawsky 		POSTING_READ(reg);
1199e3689190SBen Widawsky 
1200cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1201e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1202e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1203e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
120435a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
120535a85ac6SBen Widawsky 		parity_event[5] = NULL;
1206e3689190SBen Widawsky 
120791c8a326SChris Wilson 		kobject_uevent_env(&dev_priv->drm.primary->kdev->kobj,
1208e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
1209e3689190SBen Widawsky 
121035a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
121135a85ac6SBen Widawsky 			  slice, row, bank, subbank);
1212e3689190SBen Widawsky 
121335a85ac6SBen Widawsky 		kfree(parity_event[4]);
1214e3689190SBen Widawsky 		kfree(parity_event[3]);
1215e3689190SBen Widawsky 		kfree(parity_event[2]);
1216e3689190SBen Widawsky 		kfree(parity_event[1]);
1217e3689190SBen Widawsky 	}
1218e3689190SBen Widawsky 
121935a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
122035a85ac6SBen Widawsky 
122135a85ac6SBen Widawsky out:
122235a85ac6SBen Widawsky 	WARN_ON(dev_priv->l3_parity.which_slice);
12234cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
12242d1fe073SJoonas Lahtinen 	gen5_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv));
12254cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
122635a85ac6SBen Widawsky 
122791c8a326SChris Wilson 	mutex_unlock(&dev_priv->drm.struct_mutex);
122835a85ac6SBen Widawsky }
122935a85ac6SBen Widawsky 
1230261e40b8SVille Syrjälä static void ivybridge_parity_error_irq_handler(struct drm_i915_private *dev_priv,
1231261e40b8SVille Syrjälä 					       u32 iir)
1232e3689190SBen Widawsky {
1233261e40b8SVille Syrjälä 	if (!HAS_L3_DPF(dev_priv))
1234e3689190SBen Widawsky 		return;
1235e3689190SBen Widawsky 
1236d0ecd7e2SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1237261e40b8SVille Syrjälä 	gen5_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv));
1238d0ecd7e2SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
1239e3689190SBen Widawsky 
1240261e40b8SVille Syrjälä 	iir &= GT_PARITY_ERROR(dev_priv);
124135a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
124235a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 1;
124335a85ac6SBen Widawsky 
124435a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
124535a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 0;
124635a85ac6SBen Widawsky 
1247a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
1248e3689190SBen Widawsky }
1249e3689190SBen Widawsky 
1250261e40b8SVille Syrjälä static void ilk_gt_irq_handler(struct drm_i915_private *dev_priv,
1251f1af8fc1SPaulo Zanoni 			       u32 gt_iir)
1252f1af8fc1SPaulo Zanoni {
1253f8973c21SChris Wilson 	if (gt_iir & GT_RENDER_USER_INTERRUPT)
12544a570db5STvrtko Ursulin 		notify_ring(&dev_priv->engine[RCS]);
1255f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
12564a570db5STvrtko Ursulin 		notify_ring(&dev_priv->engine[VCS]);
1257f1af8fc1SPaulo Zanoni }
1258f1af8fc1SPaulo Zanoni 
1259261e40b8SVille Syrjälä static void snb_gt_irq_handler(struct drm_i915_private *dev_priv,
1260e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1261e7b4c6b1SDaniel Vetter {
1262f8973c21SChris Wilson 	if (gt_iir & GT_RENDER_USER_INTERRUPT)
12634a570db5STvrtko Ursulin 		notify_ring(&dev_priv->engine[RCS]);
1264cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
12654a570db5STvrtko Ursulin 		notify_ring(&dev_priv->engine[VCS]);
1266cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
12674a570db5STvrtko Ursulin 		notify_ring(&dev_priv->engine[BCS]);
1268e7b4c6b1SDaniel Vetter 
1269cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1270cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
1271aaecdf61SDaniel Vetter 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT))
1272aaecdf61SDaniel Vetter 		DRM_DEBUG("Command parser error, gt_iir 0x%08x\n", gt_iir);
1273e3689190SBen Widawsky 
1274261e40b8SVille Syrjälä 	if (gt_iir & GT_PARITY_ERROR(dev_priv))
1275261e40b8SVille Syrjälä 		ivybridge_parity_error_irq_handler(dev_priv, gt_iir);
1276e7b4c6b1SDaniel Vetter }
1277e7b4c6b1SDaniel Vetter 
1278fbcc1a0cSNick Hoath static __always_inline void
12790bc40be8STvrtko Ursulin gen8_cs_irq_handler(struct intel_engine_cs *engine, u32 iir, int test_shift)
1280fbcc1a0cSNick Hoath {
1281fbcc1a0cSNick Hoath 	if (iir & (GT_RENDER_USER_INTERRUPT << test_shift))
12820bc40be8STvrtko Ursulin 		notify_ring(engine);
1283fbcc1a0cSNick Hoath 	if (iir & (GT_CONTEXT_SWITCH_INTERRUPT << test_shift))
128427af5eeaSTvrtko Ursulin 		tasklet_schedule(&engine->irq_tasklet);
1285fbcc1a0cSNick Hoath }
1286fbcc1a0cSNick Hoath 
1287e30e251aSVille Syrjälä static irqreturn_t gen8_gt_irq_ack(struct drm_i915_private *dev_priv,
1288e30e251aSVille Syrjälä 				   u32 master_ctl,
1289e30e251aSVille Syrjälä 				   u32 gt_iir[4])
1290abd58f01SBen Widawsky {
1291abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
1292abd58f01SBen Widawsky 
1293abd58f01SBen Widawsky 	if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
1294e30e251aSVille Syrjälä 		gt_iir[0] = I915_READ_FW(GEN8_GT_IIR(0));
1295e30e251aSVille Syrjälä 		if (gt_iir[0]) {
1296e30e251aSVille Syrjälä 			I915_WRITE_FW(GEN8_GT_IIR(0), gt_iir[0]);
1297abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1298abd58f01SBen Widawsky 		} else
1299abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT0)!\n");
1300abd58f01SBen Widawsky 	}
1301abd58f01SBen Widawsky 
130285f9b5f9SZhao Yakui 	if (master_ctl & (GEN8_GT_VCS1_IRQ | GEN8_GT_VCS2_IRQ)) {
1303e30e251aSVille Syrjälä 		gt_iir[1] = I915_READ_FW(GEN8_GT_IIR(1));
1304e30e251aSVille Syrjälä 		if (gt_iir[1]) {
1305e30e251aSVille Syrjälä 			I915_WRITE_FW(GEN8_GT_IIR(1), gt_iir[1]);
1306abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1307abd58f01SBen Widawsky 		} else
1308abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT1)!\n");
1309abd58f01SBen Widawsky 	}
1310abd58f01SBen Widawsky 
131174cdb337SChris Wilson 	if (master_ctl & GEN8_GT_VECS_IRQ) {
1312e30e251aSVille Syrjälä 		gt_iir[3] = I915_READ_FW(GEN8_GT_IIR(3));
1313e30e251aSVille Syrjälä 		if (gt_iir[3]) {
1314e30e251aSVille Syrjälä 			I915_WRITE_FW(GEN8_GT_IIR(3), gt_iir[3]);
131574cdb337SChris Wilson 			ret = IRQ_HANDLED;
131674cdb337SChris Wilson 		} else
131774cdb337SChris Wilson 			DRM_ERROR("The master control interrupt lied (GT3)!\n");
131874cdb337SChris Wilson 	}
131974cdb337SChris Wilson 
13200961021aSBen Widawsky 	if (master_ctl & GEN8_GT_PM_IRQ) {
1321e30e251aSVille Syrjälä 		gt_iir[2] = I915_READ_FW(GEN8_GT_IIR(2));
1322e30e251aSVille Syrjälä 		if (gt_iir[2] & dev_priv->pm_rps_events) {
1323cb0d205eSChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(2),
1324e30e251aSVille Syrjälä 				      gt_iir[2] & dev_priv->pm_rps_events);
132538cc46d7SOscar Mateo 			ret = IRQ_HANDLED;
13260961021aSBen Widawsky 		} else
13270961021aSBen Widawsky 			DRM_ERROR("The master control interrupt lied (PM)!\n");
13280961021aSBen Widawsky 	}
13290961021aSBen Widawsky 
1330abd58f01SBen Widawsky 	return ret;
1331abd58f01SBen Widawsky }
1332abd58f01SBen Widawsky 
1333e30e251aSVille Syrjälä static void gen8_gt_irq_handler(struct drm_i915_private *dev_priv,
1334e30e251aSVille Syrjälä 				u32 gt_iir[4])
1335e30e251aSVille Syrjälä {
1336e30e251aSVille Syrjälä 	if (gt_iir[0]) {
1337e30e251aSVille Syrjälä 		gen8_cs_irq_handler(&dev_priv->engine[RCS],
1338e30e251aSVille Syrjälä 				    gt_iir[0], GEN8_RCS_IRQ_SHIFT);
1339e30e251aSVille Syrjälä 		gen8_cs_irq_handler(&dev_priv->engine[BCS],
1340e30e251aSVille Syrjälä 				    gt_iir[0], GEN8_BCS_IRQ_SHIFT);
1341e30e251aSVille Syrjälä 	}
1342e30e251aSVille Syrjälä 
1343e30e251aSVille Syrjälä 	if (gt_iir[1]) {
1344e30e251aSVille Syrjälä 		gen8_cs_irq_handler(&dev_priv->engine[VCS],
1345e30e251aSVille Syrjälä 				    gt_iir[1], GEN8_VCS1_IRQ_SHIFT);
1346e30e251aSVille Syrjälä 		gen8_cs_irq_handler(&dev_priv->engine[VCS2],
1347e30e251aSVille Syrjälä 				    gt_iir[1], GEN8_VCS2_IRQ_SHIFT);
1348e30e251aSVille Syrjälä 	}
1349e30e251aSVille Syrjälä 
1350e30e251aSVille Syrjälä 	if (gt_iir[3])
1351e30e251aSVille Syrjälä 		gen8_cs_irq_handler(&dev_priv->engine[VECS],
1352e30e251aSVille Syrjälä 				    gt_iir[3], GEN8_VECS_IRQ_SHIFT);
1353e30e251aSVille Syrjälä 
1354e30e251aSVille Syrjälä 	if (gt_iir[2] & dev_priv->pm_rps_events)
1355e30e251aSVille Syrjälä 		gen6_rps_irq_handler(dev_priv, gt_iir[2]);
1356e30e251aSVille Syrjälä }
1357e30e251aSVille Syrjälä 
135863c88d22SImre Deak static bool bxt_port_hotplug_long_detect(enum port port, u32 val)
135963c88d22SImre Deak {
136063c88d22SImre Deak 	switch (port) {
136163c88d22SImre Deak 	case PORT_A:
1362195baa06SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
136363c88d22SImre Deak 	case PORT_B:
136463c88d22SImre Deak 		return val & PORTB_HOTPLUG_LONG_DETECT;
136563c88d22SImre Deak 	case PORT_C:
136663c88d22SImre Deak 		return val & PORTC_HOTPLUG_LONG_DETECT;
136763c88d22SImre Deak 	default:
136863c88d22SImre Deak 		return false;
136963c88d22SImre Deak 	}
137063c88d22SImre Deak }
137163c88d22SImre Deak 
13726dbf30ceSVille Syrjälä static bool spt_port_hotplug2_long_detect(enum port port, u32 val)
13736dbf30ceSVille Syrjälä {
13746dbf30ceSVille Syrjälä 	switch (port) {
13756dbf30ceSVille Syrjälä 	case PORT_E:
13766dbf30ceSVille Syrjälä 		return val & PORTE_HOTPLUG_LONG_DETECT;
13776dbf30ceSVille Syrjälä 	default:
13786dbf30ceSVille Syrjälä 		return false;
13796dbf30ceSVille Syrjälä 	}
13806dbf30ceSVille Syrjälä }
13816dbf30ceSVille Syrjälä 
138274c0b395SVille Syrjälä static bool spt_port_hotplug_long_detect(enum port port, u32 val)
138374c0b395SVille Syrjälä {
138474c0b395SVille Syrjälä 	switch (port) {
138574c0b395SVille Syrjälä 	case PORT_A:
138674c0b395SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
138774c0b395SVille Syrjälä 	case PORT_B:
138874c0b395SVille Syrjälä 		return val & PORTB_HOTPLUG_LONG_DETECT;
138974c0b395SVille Syrjälä 	case PORT_C:
139074c0b395SVille Syrjälä 		return val & PORTC_HOTPLUG_LONG_DETECT;
139174c0b395SVille Syrjälä 	case PORT_D:
139274c0b395SVille Syrjälä 		return val & PORTD_HOTPLUG_LONG_DETECT;
139374c0b395SVille Syrjälä 	default:
139474c0b395SVille Syrjälä 		return false;
139574c0b395SVille Syrjälä 	}
139674c0b395SVille Syrjälä }
139774c0b395SVille Syrjälä 
1398e4ce95aaSVille Syrjälä static bool ilk_port_hotplug_long_detect(enum port port, u32 val)
1399e4ce95aaSVille Syrjälä {
1400e4ce95aaSVille Syrjälä 	switch (port) {
1401e4ce95aaSVille Syrjälä 	case PORT_A:
1402e4ce95aaSVille Syrjälä 		return val & DIGITAL_PORTA_HOTPLUG_LONG_DETECT;
1403e4ce95aaSVille Syrjälä 	default:
1404e4ce95aaSVille Syrjälä 		return false;
1405e4ce95aaSVille Syrjälä 	}
1406e4ce95aaSVille Syrjälä }
1407e4ce95aaSVille Syrjälä 
1408676574dfSJani Nikula static bool pch_port_hotplug_long_detect(enum port port, u32 val)
140913cf5504SDave Airlie {
141013cf5504SDave Airlie 	switch (port) {
141113cf5504SDave Airlie 	case PORT_B:
1412676574dfSJani Nikula 		return val & PORTB_HOTPLUG_LONG_DETECT;
141313cf5504SDave Airlie 	case PORT_C:
1414676574dfSJani Nikula 		return val & PORTC_HOTPLUG_LONG_DETECT;
141513cf5504SDave Airlie 	case PORT_D:
1416676574dfSJani Nikula 		return val & PORTD_HOTPLUG_LONG_DETECT;
1417676574dfSJani Nikula 	default:
1418676574dfSJani Nikula 		return false;
141913cf5504SDave Airlie 	}
142013cf5504SDave Airlie }
142113cf5504SDave Airlie 
1422676574dfSJani Nikula static bool i9xx_port_hotplug_long_detect(enum port port, u32 val)
142313cf5504SDave Airlie {
142413cf5504SDave Airlie 	switch (port) {
142513cf5504SDave Airlie 	case PORT_B:
1426676574dfSJani Nikula 		return val & PORTB_HOTPLUG_INT_LONG_PULSE;
142713cf5504SDave Airlie 	case PORT_C:
1428676574dfSJani Nikula 		return val & PORTC_HOTPLUG_INT_LONG_PULSE;
142913cf5504SDave Airlie 	case PORT_D:
1430676574dfSJani Nikula 		return val & PORTD_HOTPLUG_INT_LONG_PULSE;
1431676574dfSJani Nikula 	default:
1432676574dfSJani Nikula 		return false;
143313cf5504SDave Airlie 	}
143413cf5504SDave Airlie }
143513cf5504SDave Airlie 
143642db67d6SVille Syrjälä /*
143742db67d6SVille Syrjälä  * Get a bit mask of pins that have triggered, and which ones may be long.
143842db67d6SVille Syrjälä  * This can be called multiple times with the same masks to accumulate
143942db67d6SVille Syrjälä  * hotplug detection results from several registers.
144042db67d6SVille Syrjälä  *
144142db67d6SVille Syrjälä  * Note that the caller is expected to zero out the masks initially.
144242db67d6SVille Syrjälä  */
1443fd63e2a9SImre Deak static void intel_get_hpd_pins(u32 *pin_mask, u32 *long_mask,
14448c841e57SJani Nikula 			     u32 hotplug_trigger, u32 dig_hotplug_reg,
1445fd63e2a9SImre Deak 			     const u32 hpd[HPD_NUM_PINS],
1446fd63e2a9SImre Deak 			     bool long_pulse_detect(enum port port, u32 val))
1447676574dfSJani Nikula {
14488c841e57SJani Nikula 	enum port port;
1449676574dfSJani Nikula 	int i;
1450676574dfSJani Nikula 
1451676574dfSJani Nikula 	for_each_hpd_pin(i) {
14528c841e57SJani Nikula 		if ((hpd[i] & hotplug_trigger) == 0)
14538c841e57SJani Nikula 			continue;
14548c841e57SJani Nikula 
1455676574dfSJani Nikula 		*pin_mask |= BIT(i);
1456676574dfSJani Nikula 
1457cc24fcdcSImre Deak 		if (!intel_hpd_pin_to_port(i, &port))
1458cc24fcdcSImre Deak 			continue;
1459cc24fcdcSImre Deak 
1460fd63e2a9SImre Deak 		if (long_pulse_detect(port, dig_hotplug_reg))
1461676574dfSJani Nikula 			*long_mask |= BIT(i);
1462676574dfSJani Nikula 	}
1463676574dfSJani Nikula 
1464676574dfSJani Nikula 	DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x, dig 0x%08x, pins 0x%08x\n",
1465676574dfSJani Nikula 			 hotplug_trigger, dig_hotplug_reg, *pin_mask);
1466676574dfSJani Nikula 
1467676574dfSJani Nikula }
1468676574dfSJani Nikula 
146991d14251STvrtko Ursulin static void gmbus_irq_handler(struct drm_i915_private *dev_priv)
1470515ac2bbSDaniel Vetter {
147128c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1472515ac2bbSDaniel Vetter }
1473515ac2bbSDaniel Vetter 
147491d14251STvrtko Ursulin static void dp_aux_irq_handler(struct drm_i915_private *dev_priv)
1475ce99c256SDaniel Vetter {
14769ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1477ce99c256SDaniel Vetter }
1478ce99c256SDaniel Vetter 
14798bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
148091d14251STvrtko Ursulin static void display_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
148191d14251STvrtko Ursulin 					 enum pipe pipe,
1482eba94eb9SDaniel Vetter 					 uint32_t crc0, uint32_t crc1,
1483eba94eb9SDaniel Vetter 					 uint32_t crc2, uint32_t crc3,
14848bc5e955SDaniel Vetter 					 uint32_t crc4)
14858bf1e9f1SShuang He {
14868bf1e9f1SShuang He 	struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
14878bf1e9f1SShuang He 	struct intel_pipe_crc_entry *entry;
1488ac2300d4SDamien Lespiau 	int head, tail;
1489b2c88f5bSDamien Lespiau 
1490d538bbdfSDamien Lespiau 	spin_lock(&pipe_crc->lock);
1491d538bbdfSDamien Lespiau 
14920c912c79SDamien Lespiau 	if (!pipe_crc->entries) {
1493d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
149434273620SDaniel Vetter 		DRM_DEBUG_KMS("spurious interrupt\n");
14950c912c79SDamien Lespiau 		return;
14960c912c79SDamien Lespiau 	}
14970c912c79SDamien Lespiau 
1498d538bbdfSDamien Lespiau 	head = pipe_crc->head;
1499d538bbdfSDamien Lespiau 	tail = pipe_crc->tail;
1500b2c88f5bSDamien Lespiau 
1501b2c88f5bSDamien Lespiau 	if (CIRC_SPACE(head, tail, INTEL_PIPE_CRC_ENTRIES_NR) < 1) {
1502d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
1503b2c88f5bSDamien Lespiau 		DRM_ERROR("CRC buffer overflowing\n");
1504b2c88f5bSDamien Lespiau 		return;
1505b2c88f5bSDamien Lespiau 	}
1506b2c88f5bSDamien Lespiau 
1507b2c88f5bSDamien Lespiau 	entry = &pipe_crc->entries[head];
15088bf1e9f1SShuang He 
150991c8a326SChris Wilson 	entry->frame = dev_priv->drm.driver->get_vblank_counter(&dev_priv->drm,
151091d14251STvrtko Ursulin 								 pipe);
1511eba94eb9SDaniel Vetter 	entry->crc[0] = crc0;
1512eba94eb9SDaniel Vetter 	entry->crc[1] = crc1;
1513eba94eb9SDaniel Vetter 	entry->crc[2] = crc2;
1514eba94eb9SDaniel Vetter 	entry->crc[3] = crc3;
1515eba94eb9SDaniel Vetter 	entry->crc[4] = crc4;
1516b2c88f5bSDamien Lespiau 
1517b2c88f5bSDamien Lespiau 	head = (head + 1) & (INTEL_PIPE_CRC_ENTRIES_NR - 1);
1518d538bbdfSDamien Lespiau 	pipe_crc->head = head;
1519d538bbdfSDamien Lespiau 
1520d538bbdfSDamien Lespiau 	spin_unlock(&pipe_crc->lock);
152107144428SDamien Lespiau 
152207144428SDamien Lespiau 	wake_up_interruptible(&pipe_crc->wq);
15238bf1e9f1SShuang He }
1524277de95eSDaniel Vetter #else
1525277de95eSDaniel Vetter static inline void
152691d14251STvrtko Ursulin display_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
152791d14251STvrtko Ursulin 			     enum pipe pipe,
1528277de95eSDaniel Vetter 			     uint32_t crc0, uint32_t crc1,
1529277de95eSDaniel Vetter 			     uint32_t crc2, uint32_t crc3,
1530277de95eSDaniel Vetter 			     uint32_t crc4) {}
1531277de95eSDaniel Vetter #endif
1532eba94eb9SDaniel Vetter 
1533277de95eSDaniel Vetter 
153491d14251STvrtko Ursulin static void hsw_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
153591d14251STvrtko Ursulin 				     enum pipe pipe)
15365a69b89fSDaniel Vetter {
153791d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
15385a69b89fSDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
15395a69b89fSDaniel Vetter 				     0, 0, 0, 0);
15405a69b89fSDaniel Vetter }
15415a69b89fSDaniel Vetter 
154291d14251STvrtko Ursulin static void ivb_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
154391d14251STvrtko Ursulin 				     enum pipe pipe)
1544eba94eb9SDaniel Vetter {
154591d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
1546eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1547eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1548eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1549eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
15508bc5e955SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1551eba94eb9SDaniel Vetter }
15525b3a856bSDaniel Vetter 
155391d14251STvrtko Ursulin static void i9xx_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
155491d14251STvrtko Ursulin 				      enum pipe pipe)
15555b3a856bSDaniel Vetter {
15560b5c5ed0SDaniel Vetter 	uint32_t res1, res2;
15570b5c5ed0SDaniel Vetter 
155891d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 3)
15590b5c5ed0SDaniel Vetter 		res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
15600b5c5ed0SDaniel Vetter 	else
15610b5c5ed0SDaniel Vetter 		res1 = 0;
15620b5c5ed0SDaniel Vetter 
156391d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
15640b5c5ed0SDaniel Vetter 		res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
15650b5c5ed0SDaniel Vetter 	else
15660b5c5ed0SDaniel Vetter 		res2 = 0;
15675b3a856bSDaniel Vetter 
156891d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
15690b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_RED(pipe)),
15700b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_GREEN(pipe)),
15710b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_BLUE(pipe)),
15720b5c5ed0SDaniel Vetter 				     res1, res2);
15735b3a856bSDaniel Vetter }
15748bf1e9f1SShuang He 
15751403c0d4SPaulo Zanoni /* The RPS events need forcewake, so we add them to a work queue and mask their
15761403c0d4SPaulo Zanoni  * IMR bits until the work is done. Other interrupts can be processed without
15771403c0d4SPaulo Zanoni  * the work queue. */
15781403c0d4SPaulo Zanoni static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1579baf02a1fSBen Widawsky {
1580a6706b45SDeepak S 	if (pm_iir & dev_priv->pm_rps_events) {
158159cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
1582480c8033SDaniel Vetter 		gen6_disable_pm_irq(dev_priv, pm_iir & dev_priv->pm_rps_events);
1583d4d70aa5SImre Deak 		if (dev_priv->rps.interrupts_enabled) {
1584d4d70aa5SImre Deak 			dev_priv->rps.pm_iir |= pm_iir & dev_priv->pm_rps_events;
1585c33d247dSChris Wilson 			schedule_work(&dev_priv->rps.work);
158641a05a3aSDaniel Vetter 		}
1587d4d70aa5SImre Deak 		spin_unlock(&dev_priv->irq_lock);
1588d4d70aa5SImre Deak 	}
1589baf02a1fSBen Widawsky 
1590c9a9a268SImre Deak 	if (INTEL_INFO(dev_priv)->gen >= 8)
1591c9a9a268SImre Deak 		return;
1592c9a9a268SImre Deak 
15932d1fe073SJoonas Lahtinen 	if (HAS_VEBOX(dev_priv)) {
159412638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
15954a570db5STvrtko Ursulin 			notify_ring(&dev_priv->engine[VECS]);
159612638c57SBen Widawsky 
1597aaecdf61SDaniel Vetter 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT)
1598aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, pm_iir 0x%08x\n", pm_iir);
159912638c57SBen Widawsky 	}
16001403c0d4SPaulo Zanoni }
1601baf02a1fSBen Widawsky 
16025a21b665SDaniel Vetter static bool intel_pipe_handle_vblank(struct drm_i915_private *dev_priv,
160391d14251STvrtko Ursulin 				     enum pipe pipe)
16048d7849dbSVille Syrjälä {
16055a21b665SDaniel Vetter 	bool ret;
16065a21b665SDaniel Vetter 
160791c8a326SChris Wilson 	ret = drm_handle_vblank(&dev_priv->drm, pipe);
16085a21b665SDaniel Vetter 	if (ret)
160951cbaf01SMaarten Lankhorst 		intel_finish_page_flip_mmio(dev_priv, pipe);
16105a21b665SDaniel Vetter 
16115a21b665SDaniel Vetter 	return ret;
16128d7849dbSVille Syrjälä }
16138d7849dbSVille Syrjälä 
161491d14251STvrtko Ursulin static void valleyview_pipestat_irq_ack(struct drm_i915_private *dev_priv,
161591d14251STvrtko Ursulin 					u32 iir, u32 pipe_stats[I915_MAX_PIPES])
16167e231dbeSJesse Barnes {
16177e231dbeSJesse Barnes 	int pipe;
16187e231dbeSJesse Barnes 
161958ead0d7SImre Deak 	spin_lock(&dev_priv->irq_lock);
16201ca993d2SVille Syrjälä 
16211ca993d2SVille Syrjälä 	if (!dev_priv->display_irqs_enabled) {
16221ca993d2SVille Syrjälä 		spin_unlock(&dev_priv->irq_lock);
16231ca993d2SVille Syrjälä 		return;
16241ca993d2SVille Syrjälä 	}
16251ca993d2SVille Syrjälä 
1626055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1627f0f59a00SVille Syrjälä 		i915_reg_t reg;
1628bbb5eebfSDaniel Vetter 		u32 mask, iir_bit = 0;
162991d181ddSImre Deak 
1630bbb5eebfSDaniel Vetter 		/*
1631bbb5eebfSDaniel Vetter 		 * PIPESTAT bits get signalled even when the interrupt is
1632bbb5eebfSDaniel Vetter 		 * disabled with the mask bits, and some of the status bits do
1633bbb5eebfSDaniel Vetter 		 * not generate interrupts at all (like the underrun bit). Hence
1634bbb5eebfSDaniel Vetter 		 * we need to be careful that we only handle what we want to
1635bbb5eebfSDaniel Vetter 		 * handle.
1636bbb5eebfSDaniel Vetter 		 */
16370f239f4cSDaniel Vetter 
16380f239f4cSDaniel Vetter 		/* fifo underruns are filterered in the underrun handler. */
16390f239f4cSDaniel Vetter 		mask = PIPE_FIFO_UNDERRUN_STATUS;
1640bbb5eebfSDaniel Vetter 
1641bbb5eebfSDaniel Vetter 		switch (pipe) {
1642bbb5eebfSDaniel Vetter 		case PIPE_A:
1643bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
1644bbb5eebfSDaniel Vetter 			break;
1645bbb5eebfSDaniel Vetter 		case PIPE_B:
1646bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
1647bbb5eebfSDaniel Vetter 			break;
16483278f67fSVille Syrjälä 		case PIPE_C:
16493278f67fSVille Syrjälä 			iir_bit = I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
16503278f67fSVille Syrjälä 			break;
1651bbb5eebfSDaniel Vetter 		}
1652bbb5eebfSDaniel Vetter 		if (iir & iir_bit)
1653bbb5eebfSDaniel Vetter 			mask |= dev_priv->pipestat_irq_mask[pipe];
1654bbb5eebfSDaniel Vetter 
1655bbb5eebfSDaniel Vetter 		if (!mask)
165691d181ddSImre Deak 			continue;
165791d181ddSImre Deak 
165891d181ddSImre Deak 		reg = PIPESTAT(pipe);
1659bbb5eebfSDaniel Vetter 		mask |= PIPESTAT_INT_ENABLE_MASK;
1660bbb5eebfSDaniel Vetter 		pipe_stats[pipe] = I915_READ(reg) & mask;
16617e231dbeSJesse Barnes 
16627e231dbeSJesse Barnes 		/*
16637e231dbeSJesse Barnes 		 * Clear the PIPE*STAT regs before the IIR
16647e231dbeSJesse Barnes 		 */
166591d181ddSImre Deak 		if (pipe_stats[pipe] & (PIPE_FIFO_UNDERRUN_STATUS |
166691d181ddSImre Deak 					PIPESTAT_INT_STATUS_MASK))
16677e231dbeSJesse Barnes 			I915_WRITE(reg, pipe_stats[pipe]);
16687e231dbeSJesse Barnes 	}
166958ead0d7SImre Deak 	spin_unlock(&dev_priv->irq_lock);
16702ecb8ca4SVille Syrjälä }
16712ecb8ca4SVille Syrjälä 
167291d14251STvrtko Ursulin static void valleyview_pipestat_irq_handler(struct drm_i915_private *dev_priv,
16732ecb8ca4SVille Syrjälä 					    u32 pipe_stats[I915_MAX_PIPES])
16742ecb8ca4SVille Syrjälä {
16752ecb8ca4SVille Syrjälä 	enum pipe pipe;
16767e231dbeSJesse Barnes 
1677055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
16785a21b665SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
16795a21b665SDaniel Vetter 		    intel_pipe_handle_vblank(dev_priv, pipe))
16805a21b665SDaniel Vetter 			intel_check_page_flip(dev_priv, pipe);
168131acc7f5SJesse Barnes 
16825251f04eSMaarten Lankhorst 		if (pipe_stats[pipe] & PLANE_FLIP_DONE_INT_STATUS_VLV)
168351cbaf01SMaarten Lankhorst 			intel_finish_page_flip_cs(dev_priv, pipe);
16844356d586SDaniel Vetter 
16854356d586SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
168691d14251STvrtko Ursulin 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
16872d9d2b0bSVille Syrjälä 
16881f7247c0SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
16891f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
169031acc7f5SJesse Barnes 	}
169131acc7f5SJesse Barnes 
1692c1874ed7SImre Deak 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
169391d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
1694c1874ed7SImre Deak }
1695c1874ed7SImre Deak 
16961ae3c34cSVille Syrjälä static u32 i9xx_hpd_irq_ack(struct drm_i915_private *dev_priv)
169716c6c56bSVille Syrjälä {
169816c6c56bSVille Syrjälä 	u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
169916c6c56bSVille Syrjälä 
17001ae3c34cSVille Syrjälä 	if (hotplug_status)
17013ff60f89SOscar Mateo 		I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
17021ae3c34cSVille Syrjälä 
17031ae3c34cSVille Syrjälä 	return hotplug_status;
17041ae3c34cSVille Syrjälä }
17051ae3c34cSVille Syrjälä 
170691d14251STvrtko Ursulin static void i9xx_hpd_irq_handler(struct drm_i915_private *dev_priv,
17071ae3c34cSVille Syrjälä 				 u32 hotplug_status)
17081ae3c34cSVille Syrjälä {
17091ae3c34cSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
17103ff60f89SOscar Mateo 
171191d14251STvrtko Ursulin 	if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
171291d14251STvrtko Ursulin 	    IS_CHERRYVIEW(dev_priv)) {
171316c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
171416c6c56bSVille Syrjälä 
171558f2cf24SVille Syrjälä 		if (hotplug_trigger) {
1716fd63e2a9SImre Deak 			intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
1717fd63e2a9SImre Deak 					   hotplug_trigger, hpd_status_g4x,
1718fd63e2a9SImre Deak 					   i9xx_port_hotplug_long_detect);
171958f2cf24SVille Syrjälä 
172091d14251STvrtko Ursulin 			intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
172158f2cf24SVille Syrjälä 		}
1722369712e8SJani Nikula 
1723369712e8SJani Nikula 		if (hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
172491d14251STvrtko Ursulin 			dp_aux_irq_handler(dev_priv);
172516c6c56bSVille Syrjälä 	} else {
172616c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
172716c6c56bSVille Syrjälä 
172858f2cf24SVille Syrjälä 		if (hotplug_trigger) {
1729fd63e2a9SImre Deak 			intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
17304e3d1e26SVille Syrjälä 					   hotplug_trigger, hpd_status_i915,
1731fd63e2a9SImre Deak 					   i9xx_port_hotplug_long_detect);
173291d14251STvrtko Ursulin 			intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
173316c6c56bSVille Syrjälä 		}
17343ff60f89SOscar Mateo 	}
173558f2cf24SVille Syrjälä }
173616c6c56bSVille Syrjälä 
1737c1874ed7SImre Deak static irqreturn_t valleyview_irq_handler(int irq, void *arg)
1738c1874ed7SImre Deak {
173945a83f84SDaniel Vetter 	struct drm_device *dev = arg;
1740fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
1741c1874ed7SImre Deak 	irqreturn_t ret = IRQ_NONE;
1742c1874ed7SImre Deak 
17432dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
17442dd2a883SImre Deak 		return IRQ_NONE;
17452dd2a883SImre Deak 
17461f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
17471f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
17481f814dacSImre Deak 
17491e1cace9SVille Syrjälä 	do {
17506e814800SVille Syrjälä 		u32 iir, gt_iir, pm_iir;
17512ecb8ca4SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
17521ae3c34cSVille Syrjälä 		u32 hotplug_status = 0;
1753a5e485a9SVille Syrjälä 		u32 ier = 0;
17543ff60f89SOscar Mateo 
1755c1874ed7SImre Deak 		gt_iir = I915_READ(GTIIR);
1756c1874ed7SImre Deak 		pm_iir = I915_READ(GEN6_PMIIR);
17573ff60f89SOscar Mateo 		iir = I915_READ(VLV_IIR);
1758c1874ed7SImre Deak 
1759c1874ed7SImre Deak 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
17601e1cace9SVille Syrjälä 			break;
1761c1874ed7SImre Deak 
1762c1874ed7SImre Deak 		ret = IRQ_HANDLED;
1763c1874ed7SImre Deak 
1764a5e485a9SVille Syrjälä 		/*
1765a5e485a9SVille Syrjälä 		 * Theory on interrupt generation, based on empirical evidence:
1766a5e485a9SVille Syrjälä 		 *
1767a5e485a9SVille Syrjälä 		 * x = ((VLV_IIR & VLV_IER) ||
1768a5e485a9SVille Syrjälä 		 *      (((GT_IIR & GT_IER) || (GEN6_PMIIR & GEN6_PMIER)) &&
1769a5e485a9SVille Syrjälä 		 *       (VLV_MASTER_IER & MASTER_INTERRUPT_ENABLE)));
1770a5e485a9SVille Syrjälä 		 *
1771a5e485a9SVille Syrjälä 		 * A CPU interrupt will only be raised when 'x' has a 0->1 edge.
1772a5e485a9SVille Syrjälä 		 * Hence we clear MASTER_INTERRUPT_ENABLE and VLV_IER to
1773a5e485a9SVille Syrjälä 		 * guarantee the CPU interrupt will be raised again even if we
1774a5e485a9SVille Syrjälä 		 * don't end up clearing all the VLV_IIR, GT_IIR, GEN6_PMIIR
1775a5e485a9SVille Syrjälä 		 * bits this time around.
1776a5e485a9SVille Syrjälä 		 */
17774a0a0202SVille Syrjälä 		I915_WRITE(VLV_MASTER_IER, 0);
1778a5e485a9SVille Syrjälä 		ier = I915_READ(VLV_IER);
1779a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, 0);
17804a0a0202SVille Syrjälä 
17814a0a0202SVille Syrjälä 		if (gt_iir)
17824a0a0202SVille Syrjälä 			I915_WRITE(GTIIR, gt_iir);
17834a0a0202SVille Syrjälä 		if (pm_iir)
17844a0a0202SVille Syrjälä 			I915_WRITE(GEN6_PMIIR, pm_iir);
17854a0a0202SVille Syrjälä 
17867ce4d1f2SVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
17871ae3c34cSVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
17887ce4d1f2SVille Syrjälä 
17893ff60f89SOscar Mateo 		/* Call regardless, as some status bits might not be
17903ff60f89SOscar Mateo 		 * signalled in iir */
179191d14251STvrtko Ursulin 		valleyview_pipestat_irq_ack(dev_priv, iir, pipe_stats);
17927ce4d1f2SVille Syrjälä 
17937ce4d1f2SVille Syrjälä 		/*
17947ce4d1f2SVille Syrjälä 		 * VLV_IIR is single buffered, and reflects the level
17957ce4d1f2SVille Syrjälä 		 * from PIPESTAT/PORT_HOTPLUG_STAT, hence clear it last.
17967ce4d1f2SVille Syrjälä 		 */
17977ce4d1f2SVille Syrjälä 		if (iir)
17987ce4d1f2SVille Syrjälä 			I915_WRITE(VLV_IIR, iir);
17994a0a0202SVille Syrjälä 
1800a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, ier);
18014a0a0202SVille Syrjälä 		I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
18024a0a0202SVille Syrjälä 		POSTING_READ(VLV_MASTER_IER);
18031ae3c34cSVille Syrjälä 
180452894874SVille Syrjälä 		if (gt_iir)
1805261e40b8SVille Syrjälä 			snb_gt_irq_handler(dev_priv, gt_iir);
180652894874SVille Syrjälä 		if (pm_iir)
180752894874SVille Syrjälä 			gen6_rps_irq_handler(dev_priv, pm_iir);
180852894874SVille Syrjälä 
18091ae3c34cSVille Syrjälä 		if (hotplug_status)
181091d14251STvrtko Ursulin 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
18112ecb8ca4SVille Syrjälä 
181291d14251STvrtko Ursulin 		valleyview_pipestat_irq_handler(dev_priv, pipe_stats);
18131e1cace9SVille Syrjälä 	} while (0);
18147e231dbeSJesse Barnes 
18151f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
18161f814dacSImre Deak 
18177e231dbeSJesse Barnes 	return ret;
18187e231dbeSJesse Barnes }
18197e231dbeSJesse Barnes 
182043f328d7SVille Syrjälä static irqreturn_t cherryview_irq_handler(int irq, void *arg)
182143f328d7SVille Syrjälä {
182245a83f84SDaniel Vetter 	struct drm_device *dev = arg;
1823fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
182443f328d7SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
182543f328d7SVille Syrjälä 
18262dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
18272dd2a883SImre Deak 		return IRQ_NONE;
18282dd2a883SImre Deak 
18291f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
18301f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
18311f814dacSImre Deak 
1832579de73bSChris Wilson 	do {
18336e814800SVille Syrjälä 		u32 master_ctl, iir;
1834e30e251aSVille Syrjälä 		u32 gt_iir[4] = {};
18352ecb8ca4SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
18361ae3c34cSVille Syrjälä 		u32 hotplug_status = 0;
1837a5e485a9SVille Syrjälä 		u32 ier = 0;
1838a5e485a9SVille Syrjälä 
18398e5fd599SVille Syrjälä 		master_ctl = I915_READ(GEN8_MASTER_IRQ) & ~GEN8_MASTER_IRQ_CONTROL;
18403278f67fSVille Syrjälä 		iir = I915_READ(VLV_IIR);
18413278f67fSVille Syrjälä 
18423278f67fSVille Syrjälä 		if (master_ctl == 0 && iir == 0)
18438e5fd599SVille Syrjälä 			break;
184443f328d7SVille Syrjälä 
184527b6c122SOscar Mateo 		ret = IRQ_HANDLED;
184627b6c122SOscar Mateo 
1847a5e485a9SVille Syrjälä 		/*
1848a5e485a9SVille Syrjälä 		 * Theory on interrupt generation, based on empirical evidence:
1849a5e485a9SVille Syrjälä 		 *
1850a5e485a9SVille Syrjälä 		 * x = ((VLV_IIR & VLV_IER) ||
1851a5e485a9SVille Syrjälä 		 *      ((GEN8_MASTER_IRQ & ~GEN8_MASTER_IRQ_CONTROL) &&
1852a5e485a9SVille Syrjälä 		 *       (GEN8_MASTER_IRQ & GEN8_MASTER_IRQ_CONTROL)));
1853a5e485a9SVille Syrjälä 		 *
1854a5e485a9SVille Syrjälä 		 * A CPU interrupt will only be raised when 'x' has a 0->1 edge.
1855a5e485a9SVille Syrjälä 		 * Hence we clear GEN8_MASTER_IRQ_CONTROL and VLV_IER to
1856a5e485a9SVille Syrjälä 		 * guarantee the CPU interrupt will be raised again even if we
1857a5e485a9SVille Syrjälä 		 * don't end up clearing all the VLV_IIR and GEN8_MASTER_IRQ_CONTROL
1858a5e485a9SVille Syrjälä 		 * bits this time around.
1859a5e485a9SVille Syrjälä 		 */
186043f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, 0);
1861a5e485a9SVille Syrjälä 		ier = I915_READ(VLV_IER);
1862a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, 0);
186343f328d7SVille Syrjälä 
1864e30e251aSVille Syrjälä 		gen8_gt_irq_ack(dev_priv, master_ctl, gt_iir);
186527b6c122SOscar Mateo 
186627b6c122SOscar Mateo 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
18671ae3c34cSVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
186843f328d7SVille Syrjälä 
186927b6c122SOscar Mateo 		/* Call regardless, as some status bits might not be
187027b6c122SOscar Mateo 		 * signalled in iir */
187191d14251STvrtko Ursulin 		valleyview_pipestat_irq_ack(dev_priv, iir, pipe_stats);
187243f328d7SVille Syrjälä 
18737ce4d1f2SVille Syrjälä 		/*
18747ce4d1f2SVille Syrjälä 		 * VLV_IIR is single buffered, and reflects the level
18757ce4d1f2SVille Syrjälä 		 * from PIPESTAT/PORT_HOTPLUG_STAT, hence clear it last.
18767ce4d1f2SVille Syrjälä 		 */
18777ce4d1f2SVille Syrjälä 		if (iir)
18787ce4d1f2SVille Syrjälä 			I915_WRITE(VLV_IIR, iir);
18797ce4d1f2SVille Syrjälä 
1880a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, ier);
1881e5328c43SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
188243f328d7SVille Syrjälä 		POSTING_READ(GEN8_MASTER_IRQ);
18831ae3c34cSVille Syrjälä 
1884e30e251aSVille Syrjälä 		gen8_gt_irq_handler(dev_priv, gt_iir);
1885e30e251aSVille Syrjälä 
18861ae3c34cSVille Syrjälä 		if (hotplug_status)
188791d14251STvrtko Ursulin 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
18882ecb8ca4SVille Syrjälä 
188991d14251STvrtko Ursulin 		valleyview_pipestat_irq_handler(dev_priv, pipe_stats);
1890579de73bSChris Wilson 	} while (0);
18913278f67fSVille Syrjälä 
18921f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
18931f814dacSImre Deak 
189443f328d7SVille Syrjälä 	return ret;
189543f328d7SVille Syrjälä }
189643f328d7SVille Syrjälä 
189791d14251STvrtko Ursulin static void ibx_hpd_irq_handler(struct drm_i915_private *dev_priv,
189891d14251STvrtko Ursulin 				u32 hotplug_trigger,
189940e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
1900776ad806SJesse Barnes {
190142db67d6SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
1902776ad806SJesse Barnes 
19036a39d7c9SJani Nikula 	/*
19046a39d7c9SJani Nikula 	 * Somehow the PCH doesn't seem to really ack the interrupt to the CPU
19056a39d7c9SJani Nikula 	 * unless we touch the hotplug register, even if hotplug_trigger is
19066a39d7c9SJani Nikula 	 * zero. Not acking leads to "The master control interrupt lied (SDE)!"
19076a39d7c9SJani Nikula 	 * errors.
19086a39d7c9SJani Nikula 	 */
190913cf5504SDave Airlie 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
19106a39d7c9SJani Nikula 	if (!hotplug_trigger) {
19116a39d7c9SJani Nikula 		u32 mask = PORTA_HOTPLUG_STATUS_MASK |
19126a39d7c9SJani Nikula 			PORTD_HOTPLUG_STATUS_MASK |
19136a39d7c9SJani Nikula 			PORTC_HOTPLUG_STATUS_MASK |
19146a39d7c9SJani Nikula 			PORTB_HOTPLUG_STATUS_MASK;
19156a39d7c9SJani Nikula 		dig_hotplug_reg &= ~mask;
19166a39d7c9SJani Nikula 	}
19176a39d7c9SJani Nikula 
191813cf5504SDave Airlie 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
19196a39d7c9SJani Nikula 	if (!hotplug_trigger)
19206a39d7c9SJani Nikula 		return;
192113cf5504SDave Airlie 
1922fd63e2a9SImre Deak 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
192340e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
1924fd63e2a9SImre Deak 			   pch_port_hotplug_long_detect);
192540e56410SVille Syrjälä 
192691d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
1927aaf5ec2eSSonika Jindal }
192891d131d2SDaniel Vetter 
192991d14251STvrtko Ursulin static void ibx_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
193040e56410SVille Syrjälä {
193140e56410SVille Syrjälä 	int pipe;
193240e56410SVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
193340e56410SVille Syrjälä 
193491d14251STvrtko Ursulin 	ibx_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ibx);
193540e56410SVille Syrjälä 
1936cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1937cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1938776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1939cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1940cfc33bf7SVille Syrjälä 				 port_name(port));
1941cfc33bf7SVille Syrjälä 	}
1942776ad806SJesse Barnes 
1943ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
194491d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
1945ce99c256SDaniel Vetter 
1946776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
194791d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
1948776ad806SJesse Barnes 
1949776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1950776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1951776ad806SJesse Barnes 
1952776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1953776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1954776ad806SJesse Barnes 
1955776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1956776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
1957776ad806SJesse Barnes 
19589db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
1959055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
19609db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
19619db4a9c7SJesse Barnes 					 pipe_name(pipe),
19629db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
1963776ad806SJesse Barnes 
1964776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1965776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1966776ad806SJesse Barnes 
1967776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1968776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1969776ad806SJesse Barnes 
1970776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
19711f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
19728664281bSPaulo Zanoni 
19738664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
19741f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
19758664281bSPaulo Zanoni }
19768664281bSPaulo Zanoni 
197791d14251STvrtko Ursulin static void ivb_err_int_handler(struct drm_i915_private *dev_priv)
19788664281bSPaulo Zanoni {
19798664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
19805a69b89fSDaniel Vetter 	enum pipe pipe;
19818664281bSPaulo Zanoni 
1982de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
1983de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1984de032bf4SPaulo Zanoni 
1985055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
19861f7247c0SDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe))
19871f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
19888664281bSPaulo Zanoni 
19895a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
199091d14251STvrtko Ursulin 			if (IS_IVYBRIDGE(dev_priv))
199191d14251STvrtko Ursulin 				ivb_pipe_crc_irq_handler(dev_priv, pipe);
19925a69b89fSDaniel Vetter 			else
199391d14251STvrtko Ursulin 				hsw_pipe_crc_irq_handler(dev_priv, pipe);
19945a69b89fSDaniel Vetter 		}
19955a69b89fSDaniel Vetter 	}
19968bf1e9f1SShuang He 
19978664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
19988664281bSPaulo Zanoni }
19998664281bSPaulo Zanoni 
200091d14251STvrtko Ursulin static void cpt_serr_int_handler(struct drm_i915_private *dev_priv)
20018664281bSPaulo Zanoni {
20028664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
20038664281bSPaulo Zanoni 
2004de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
2005de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
2006de032bf4SPaulo Zanoni 
20078664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
20081f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
20098664281bSPaulo Zanoni 
20108664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
20111f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
20128664281bSPaulo Zanoni 
20138664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
20141f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_C);
20158664281bSPaulo Zanoni 
20168664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
2017776ad806SJesse Barnes }
2018776ad806SJesse Barnes 
201991d14251STvrtko Ursulin static void cpt_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
202023e81d69SAdam Jackson {
202123e81d69SAdam Jackson 	int pipe;
20226dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
2023aaf5ec2eSSonika Jindal 
202491d14251STvrtko Ursulin 	ibx_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_cpt);
202591d131d2SDaniel Vetter 
2026cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
2027cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
202823e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
2029cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
2030cfc33bf7SVille Syrjälä 				 port_name(port));
2031cfc33bf7SVille Syrjälä 	}
203223e81d69SAdam Jackson 
203323e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
203491d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
203523e81d69SAdam Jackson 
203623e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
203791d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
203823e81d69SAdam Jackson 
203923e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
204023e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
204123e81d69SAdam Jackson 
204223e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
204323e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
204423e81d69SAdam Jackson 
204523e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
2046055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
204723e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
204823e81d69SAdam Jackson 					 pipe_name(pipe),
204923e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
20508664281bSPaulo Zanoni 
20518664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
205291d14251STvrtko Ursulin 		cpt_serr_int_handler(dev_priv);
205323e81d69SAdam Jackson }
205423e81d69SAdam Jackson 
205591d14251STvrtko Ursulin static void spt_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
20566dbf30ceSVille Syrjälä {
20576dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_SPT &
20586dbf30ceSVille Syrjälä 		~SDE_PORTE_HOTPLUG_SPT;
20596dbf30ceSVille Syrjälä 	u32 hotplug2_trigger = pch_iir & SDE_PORTE_HOTPLUG_SPT;
20606dbf30ceSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
20616dbf30ceSVille Syrjälä 
20626dbf30ceSVille Syrjälä 	if (hotplug_trigger) {
20636dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
20646dbf30ceSVille Syrjälä 
20656dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
20666dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
20676dbf30ceSVille Syrjälä 
20686dbf30ceSVille Syrjälä 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
20696dbf30ceSVille Syrjälä 				   dig_hotplug_reg, hpd_spt,
207074c0b395SVille Syrjälä 				   spt_port_hotplug_long_detect);
20716dbf30ceSVille Syrjälä 	}
20726dbf30ceSVille Syrjälä 
20736dbf30ceSVille Syrjälä 	if (hotplug2_trigger) {
20746dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
20756dbf30ceSVille Syrjälä 
20766dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG2);
20776dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG2, dig_hotplug_reg);
20786dbf30ceSVille Syrjälä 
20796dbf30ceSVille Syrjälä 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug2_trigger,
20806dbf30ceSVille Syrjälä 				   dig_hotplug_reg, hpd_spt,
20816dbf30ceSVille Syrjälä 				   spt_port_hotplug2_long_detect);
20826dbf30ceSVille Syrjälä 	}
20836dbf30ceSVille Syrjälä 
20846dbf30ceSVille Syrjälä 	if (pin_mask)
208591d14251STvrtko Ursulin 		intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
20866dbf30ceSVille Syrjälä 
20876dbf30ceSVille Syrjälä 	if (pch_iir & SDE_GMBUS_CPT)
208891d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
20896dbf30ceSVille Syrjälä }
20906dbf30ceSVille Syrjälä 
209191d14251STvrtko Ursulin static void ilk_hpd_irq_handler(struct drm_i915_private *dev_priv,
209291d14251STvrtko Ursulin 				u32 hotplug_trigger,
209340e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2094c008bc6eSPaulo Zanoni {
2095e4ce95aaSVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2096e4ce95aaSVille Syrjälä 
2097e4ce95aaSVille Syrjälä 	dig_hotplug_reg = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
2098e4ce95aaSVille Syrjälä 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, dig_hotplug_reg);
2099e4ce95aaSVille Syrjälä 
2100e4ce95aaSVille Syrjälä 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
210140e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2102e4ce95aaSVille Syrjälä 			   ilk_port_hotplug_long_detect);
210340e56410SVille Syrjälä 
210491d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2105e4ce95aaSVille Syrjälä }
2106c008bc6eSPaulo Zanoni 
210791d14251STvrtko Ursulin static void ilk_display_irq_handler(struct drm_i915_private *dev_priv,
210891d14251STvrtko Ursulin 				    u32 de_iir)
210940e56410SVille Syrjälä {
211040e56410SVille Syrjälä 	enum pipe pipe;
211140e56410SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG;
211240e56410SVille Syrjälä 
211340e56410SVille Syrjälä 	if (hotplug_trigger)
211491d14251STvrtko Ursulin 		ilk_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ilk);
211540e56410SVille Syrjälä 
2116c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
211791d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
2118c008bc6eSPaulo Zanoni 
2119c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
212091d14251STvrtko Ursulin 		intel_opregion_asle_intr(dev_priv);
2121c008bc6eSPaulo Zanoni 
2122c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
2123c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
2124c008bc6eSPaulo Zanoni 
2125055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
21265a21b665SDaniel Vetter 		if (de_iir & DE_PIPE_VBLANK(pipe) &&
21275a21b665SDaniel Vetter 		    intel_pipe_handle_vblank(dev_priv, pipe))
21285a21b665SDaniel Vetter 			intel_check_page_flip(dev_priv, pipe);
2129c008bc6eSPaulo Zanoni 
213040da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
21311f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
2132c008bc6eSPaulo Zanoni 
213340da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
213491d14251STvrtko Ursulin 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
21355b3a856bSDaniel Vetter 
213640da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
21375251f04eSMaarten Lankhorst 		if (de_iir & DE_PLANE_FLIP_DONE(pipe))
213851cbaf01SMaarten Lankhorst 			intel_finish_page_flip_cs(dev_priv, pipe);
2139c008bc6eSPaulo Zanoni 	}
2140c008bc6eSPaulo Zanoni 
2141c008bc6eSPaulo Zanoni 	/* check event from PCH */
2142c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
2143c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
2144c008bc6eSPaulo Zanoni 
214591d14251STvrtko Ursulin 		if (HAS_PCH_CPT(dev_priv))
214691d14251STvrtko Ursulin 			cpt_irq_handler(dev_priv, pch_iir);
2147c008bc6eSPaulo Zanoni 		else
214891d14251STvrtko Ursulin 			ibx_irq_handler(dev_priv, pch_iir);
2149c008bc6eSPaulo Zanoni 
2150c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
2151c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
2152c008bc6eSPaulo Zanoni 	}
2153c008bc6eSPaulo Zanoni 
215491d14251STvrtko Ursulin 	if (IS_GEN5(dev_priv) && de_iir & DE_PCU_EVENT)
215591d14251STvrtko Ursulin 		ironlake_rps_change_irq_handler(dev_priv);
2156c008bc6eSPaulo Zanoni }
2157c008bc6eSPaulo Zanoni 
215891d14251STvrtko Ursulin static void ivb_display_irq_handler(struct drm_i915_private *dev_priv,
215991d14251STvrtko Ursulin 				    u32 de_iir)
21609719fb98SPaulo Zanoni {
216107d27e20SDamien Lespiau 	enum pipe pipe;
216223bb4cb5SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG_IVB;
216323bb4cb5SVille Syrjälä 
216440e56410SVille Syrjälä 	if (hotplug_trigger)
216591d14251STvrtko Ursulin 		ilk_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ivb);
21669719fb98SPaulo Zanoni 
21679719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
216891d14251STvrtko Ursulin 		ivb_err_int_handler(dev_priv);
21699719fb98SPaulo Zanoni 
21709719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
217191d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
21729719fb98SPaulo Zanoni 
21739719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
217491d14251STvrtko Ursulin 		intel_opregion_asle_intr(dev_priv);
21759719fb98SPaulo Zanoni 
2176055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
21775a21b665SDaniel Vetter 		if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)) &&
21785a21b665SDaniel Vetter 		    intel_pipe_handle_vblank(dev_priv, pipe))
21795a21b665SDaniel Vetter 			intel_check_page_flip(dev_priv, pipe);
218040da17c2SDaniel Vetter 
218140da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
21825251f04eSMaarten Lankhorst 		if (de_iir & DE_PLANE_FLIP_DONE_IVB(pipe))
218351cbaf01SMaarten Lankhorst 			intel_finish_page_flip_cs(dev_priv, pipe);
21849719fb98SPaulo Zanoni 	}
21859719fb98SPaulo Zanoni 
21869719fb98SPaulo Zanoni 	/* check event from PCH */
218791d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv) && (de_iir & DE_PCH_EVENT_IVB)) {
21889719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
21899719fb98SPaulo Zanoni 
219091d14251STvrtko Ursulin 		cpt_irq_handler(dev_priv, pch_iir);
21919719fb98SPaulo Zanoni 
21929719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
21939719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
21949719fb98SPaulo Zanoni 	}
21959719fb98SPaulo Zanoni }
21969719fb98SPaulo Zanoni 
219772c90f62SOscar Mateo /*
219872c90f62SOscar Mateo  * To handle irqs with the minimum potential races with fresh interrupts, we:
219972c90f62SOscar Mateo  * 1 - Disable Master Interrupt Control.
220072c90f62SOscar Mateo  * 2 - Find the source(s) of the interrupt.
220172c90f62SOscar Mateo  * 3 - Clear the Interrupt Identity bits (IIR).
220272c90f62SOscar Mateo  * 4 - Process the interrupt(s) that had bits set in the IIRs.
220372c90f62SOscar Mateo  * 5 - Re-enable Master Interrupt Control.
220472c90f62SOscar Mateo  */
2205f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
2206b1f14ad0SJesse Barnes {
220745a83f84SDaniel Vetter 	struct drm_device *dev = arg;
2208fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2209f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
22100e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
2211b1f14ad0SJesse Barnes 
22122dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
22132dd2a883SImre Deak 		return IRQ_NONE;
22142dd2a883SImre Deak 
22151f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
22161f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
22171f814dacSImre Deak 
2218b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
2219b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
2220b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
222123a78516SPaulo Zanoni 	POSTING_READ(DEIER);
22220e43406bSChris Wilson 
222344498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
222444498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
222544498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
222644498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
222744498aeaSPaulo Zanoni 	 * due to its back queue). */
222891d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv)) {
222944498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
223044498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
223144498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2232ab5c608bSBen Widawsky 	}
223344498aeaSPaulo Zanoni 
223472c90f62SOscar Mateo 	/* Find, clear, then process each source of interrupt */
223572c90f62SOscar Mateo 
22360e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
22370e43406bSChris Wilson 	if (gt_iir) {
223872c90f62SOscar Mateo 		I915_WRITE(GTIIR, gt_iir);
223972c90f62SOscar Mateo 		ret = IRQ_HANDLED;
224091d14251STvrtko Ursulin 		if (INTEL_GEN(dev_priv) >= 6)
2241261e40b8SVille Syrjälä 			snb_gt_irq_handler(dev_priv, gt_iir);
2242d8fc8a47SPaulo Zanoni 		else
2243261e40b8SVille Syrjälä 			ilk_gt_irq_handler(dev_priv, gt_iir);
22440e43406bSChris Wilson 	}
2245b1f14ad0SJesse Barnes 
2246b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
22470e43406bSChris Wilson 	if (de_iir) {
224872c90f62SOscar Mateo 		I915_WRITE(DEIIR, de_iir);
224972c90f62SOscar Mateo 		ret = IRQ_HANDLED;
225091d14251STvrtko Ursulin 		if (INTEL_GEN(dev_priv) >= 7)
225191d14251STvrtko Ursulin 			ivb_display_irq_handler(dev_priv, de_iir);
2252f1af8fc1SPaulo Zanoni 		else
225391d14251STvrtko Ursulin 			ilk_display_irq_handler(dev_priv, de_iir);
22540e43406bSChris Wilson 	}
22550e43406bSChris Wilson 
225691d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 6) {
2257f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
22580e43406bSChris Wilson 		if (pm_iir) {
2259b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
22600e43406bSChris Wilson 			ret = IRQ_HANDLED;
226172c90f62SOscar Mateo 			gen6_rps_irq_handler(dev_priv, pm_iir);
22620e43406bSChris Wilson 		}
2263f1af8fc1SPaulo Zanoni 	}
2264b1f14ad0SJesse Barnes 
2265b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
2266b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
226791d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv)) {
226844498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
226944498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2270ab5c608bSBen Widawsky 	}
2271b1f14ad0SJesse Barnes 
22721f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
22731f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
22741f814dacSImre Deak 
2275b1f14ad0SJesse Barnes 	return ret;
2276b1f14ad0SJesse Barnes }
2277b1f14ad0SJesse Barnes 
227891d14251STvrtko Ursulin static void bxt_hpd_irq_handler(struct drm_i915_private *dev_priv,
227991d14251STvrtko Ursulin 				u32 hotplug_trigger,
228040e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2281d04a492dSShashank Sharma {
2282cebd87a0SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2283d04a492dSShashank Sharma 
2284a52bb15bSVille Syrjälä 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
2285a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
2286d04a492dSShashank Sharma 
2287cebd87a0SVille Syrjälä 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
228840e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2289cebd87a0SVille Syrjälä 			   bxt_port_hotplug_long_detect);
229040e56410SVille Syrjälä 
229191d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2292d04a492dSShashank Sharma }
2293d04a492dSShashank Sharma 
2294f11a0f46STvrtko Ursulin static irqreturn_t
2295f11a0f46STvrtko Ursulin gen8_de_irq_handler(struct drm_i915_private *dev_priv, u32 master_ctl)
2296abd58f01SBen Widawsky {
2297abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
2298f11a0f46STvrtko Ursulin 	u32 iir;
2299c42664ccSDaniel Vetter 	enum pipe pipe;
230088e04703SJesse Barnes 
2301abd58f01SBen Widawsky 	if (master_ctl & GEN8_DE_MISC_IRQ) {
2302e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_MISC_IIR);
2303e32192e1STvrtko Ursulin 		if (iir) {
2304e32192e1STvrtko Ursulin 			I915_WRITE(GEN8_DE_MISC_IIR, iir);
2305abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
2306e32192e1STvrtko Ursulin 			if (iir & GEN8_DE_MISC_GSE)
230791d14251STvrtko Ursulin 				intel_opregion_asle_intr(dev_priv);
230838cc46d7SOscar Mateo 			else
230938cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Misc interrupt\n");
2310abd58f01SBen Widawsky 		}
231138cc46d7SOscar Mateo 		else
231238cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
2313abd58f01SBen Widawsky 	}
2314abd58f01SBen Widawsky 
23156d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
2316e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_PORT_IIR);
2317e32192e1STvrtko Ursulin 		if (iir) {
2318e32192e1STvrtko Ursulin 			u32 tmp_mask;
2319d04a492dSShashank Sharma 			bool found = false;
2320cebd87a0SVille Syrjälä 
2321e32192e1STvrtko Ursulin 			I915_WRITE(GEN8_DE_PORT_IIR, iir);
23226d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
232388e04703SJesse Barnes 
2324e32192e1STvrtko Ursulin 			tmp_mask = GEN8_AUX_CHANNEL_A;
2325e32192e1STvrtko Ursulin 			if (INTEL_INFO(dev_priv)->gen >= 9)
2326e32192e1STvrtko Ursulin 				tmp_mask |= GEN9_AUX_CHANNEL_B |
2327e32192e1STvrtko Ursulin 					    GEN9_AUX_CHANNEL_C |
2328e32192e1STvrtko Ursulin 					    GEN9_AUX_CHANNEL_D;
2329e32192e1STvrtko Ursulin 
2330e32192e1STvrtko Ursulin 			if (iir & tmp_mask) {
233191d14251STvrtko Ursulin 				dp_aux_irq_handler(dev_priv);
2332d04a492dSShashank Sharma 				found = true;
2333d04a492dSShashank Sharma 			}
2334d04a492dSShashank Sharma 
2335e32192e1STvrtko Ursulin 			if (IS_BROXTON(dev_priv)) {
2336e32192e1STvrtko Ursulin 				tmp_mask = iir & BXT_DE_PORT_HOTPLUG_MASK;
2337e32192e1STvrtko Ursulin 				if (tmp_mask) {
233891d14251STvrtko Ursulin 					bxt_hpd_irq_handler(dev_priv, tmp_mask,
233991d14251STvrtko Ursulin 							    hpd_bxt);
2340d04a492dSShashank Sharma 					found = true;
2341d04a492dSShashank Sharma 				}
2342e32192e1STvrtko Ursulin 			} else if (IS_BROADWELL(dev_priv)) {
2343e32192e1STvrtko Ursulin 				tmp_mask = iir & GEN8_PORT_DP_A_HOTPLUG;
2344e32192e1STvrtko Ursulin 				if (tmp_mask) {
234591d14251STvrtko Ursulin 					ilk_hpd_irq_handler(dev_priv,
234691d14251STvrtko Ursulin 							    tmp_mask, hpd_bdw);
2347e32192e1STvrtko Ursulin 					found = true;
2348e32192e1STvrtko Ursulin 				}
2349e32192e1STvrtko Ursulin 			}
2350d04a492dSShashank Sharma 
235191d14251STvrtko Ursulin 			if (IS_BROXTON(dev_priv) && (iir & BXT_DE_PORT_GMBUS)) {
235291d14251STvrtko Ursulin 				gmbus_irq_handler(dev_priv);
23539e63743eSShashank Sharma 				found = true;
23549e63743eSShashank Sharma 			}
23559e63743eSShashank Sharma 
2356d04a492dSShashank Sharma 			if (!found)
235738cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Port interrupt\n");
23586d766f02SDaniel Vetter 		}
235938cc46d7SOscar Mateo 		else
236038cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
23616d766f02SDaniel Vetter 	}
23626d766f02SDaniel Vetter 
2363055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2364e32192e1STvrtko Ursulin 		u32 flip_done, fault_errors;
2365abd58f01SBen Widawsky 
2366c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2367c42664ccSDaniel Vetter 			continue;
2368c42664ccSDaniel Vetter 
2369e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
2370e32192e1STvrtko Ursulin 		if (!iir) {
2371e32192e1STvrtko Ursulin 			DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
2372e32192e1STvrtko Ursulin 			continue;
2373e32192e1STvrtko Ursulin 		}
2374770de83dSDamien Lespiau 
2375e32192e1STvrtko Ursulin 		ret = IRQ_HANDLED;
2376e32192e1STvrtko Ursulin 		I915_WRITE(GEN8_DE_PIPE_IIR(pipe), iir);
2377e32192e1STvrtko Ursulin 
23785a21b665SDaniel Vetter 		if (iir & GEN8_PIPE_VBLANK &&
23795a21b665SDaniel Vetter 		    intel_pipe_handle_vblank(dev_priv, pipe))
23805a21b665SDaniel Vetter 			intel_check_page_flip(dev_priv, pipe);
2381abd58f01SBen Widawsky 
2382e32192e1STvrtko Ursulin 		flip_done = iir;
2383b4834a50SRodrigo Vivi 		if (INTEL_INFO(dev_priv)->gen >= 9)
2384e32192e1STvrtko Ursulin 			flip_done &= GEN9_PIPE_PLANE1_FLIP_DONE;
2385770de83dSDamien Lespiau 		else
2386e32192e1STvrtko Ursulin 			flip_done &= GEN8_PIPE_PRIMARY_FLIP_DONE;
2387770de83dSDamien Lespiau 
23885251f04eSMaarten Lankhorst 		if (flip_done)
238951cbaf01SMaarten Lankhorst 			intel_finish_page_flip_cs(dev_priv, pipe);
2390abd58f01SBen Widawsky 
2391e32192e1STvrtko Ursulin 		if (iir & GEN8_PIPE_CDCLK_CRC_DONE)
239291d14251STvrtko Ursulin 			hsw_pipe_crc_irq_handler(dev_priv, pipe);
23930fbe7870SDaniel Vetter 
2394e32192e1STvrtko Ursulin 		if (iir & GEN8_PIPE_FIFO_UNDERRUN)
2395e32192e1STvrtko Ursulin 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
239638d83c96SDaniel Vetter 
2397e32192e1STvrtko Ursulin 		fault_errors = iir;
2398b4834a50SRodrigo Vivi 		if (INTEL_INFO(dev_priv)->gen >= 9)
2399e32192e1STvrtko Ursulin 			fault_errors &= GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
2400770de83dSDamien Lespiau 		else
2401e32192e1STvrtko Ursulin 			fault_errors &= GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
2402770de83dSDamien Lespiau 
2403770de83dSDamien Lespiau 		if (fault_errors)
240430100f2bSDaniel Vetter 			DRM_ERROR("Fault errors on pipe %c\n: 0x%08x",
240530100f2bSDaniel Vetter 				  pipe_name(pipe),
2406e32192e1STvrtko Ursulin 				  fault_errors);
2407abd58f01SBen Widawsky 	}
2408abd58f01SBen Widawsky 
240991d14251STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv) && !HAS_PCH_NOP(dev_priv) &&
2410266ea3d9SShashank Sharma 	    master_ctl & GEN8_DE_PCH_IRQ) {
241192d03a80SDaniel Vetter 		/*
241292d03a80SDaniel Vetter 		 * FIXME(BDW): Assume for now that the new interrupt handling
241392d03a80SDaniel Vetter 		 * scheme also closed the SDE interrupt handling race we've seen
241492d03a80SDaniel Vetter 		 * on older pch-split platforms. But this needs testing.
241592d03a80SDaniel Vetter 		 */
2416e32192e1STvrtko Ursulin 		iir = I915_READ(SDEIIR);
2417e32192e1STvrtko Ursulin 		if (iir) {
2418e32192e1STvrtko Ursulin 			I915_WRITE(SDEIIR, iir);
241992d03a80SDaniel Vetter 			ret = IRQ_HANDLED;
24206dbf30ceSVille Syrjälä 
242122dea0beSRodrigo Vivi 			if (HAS_PCH_SPT(dev_priv) || HAS_PCH_KBP(dev_priv))
242291d14251STvrtko Ursulin 				spt_irq_handler(dev_priv, iir);
24236dbf30ceSVille Syrjälä 			else
242491d14251STvrtko Ursulin 				cpt_irq_handler(dev_priv, iir);
24252dfb0b81SJani Nikula 		} else {
24262dfb0b81SJani Nikula 			/*
24272dfb0b81SJani Nikula 			 * Like on previous PCH there seems to be something
24282dfb0b81SJani Nikula 			 * fishy going on with forwarding PCH interrupts.
24292dfb0b81SJani Nikula 			 */
24302dfb0b81SJani Nikula 			DRM_DEBUG_DRIVER("The master control interrupt lied (SDE)!\n");
24312dfb0b81SJani Nikula 		}
243292d03a80SDaniel Vetter 	}
243392d03a80SDaniel Vetter 
2434f11a0f46STvrtko Ursulin 	return ret;
2435f11a0f46STvrtko Ursulin }
2436f11a0f46STvrtko Ursulin 
2437f11a0f46STvrtko Ursulin static irqreturn_t gen8_irq_handler(int irq, void *arg)
2438f11a0f46STvrtko Ursulin {
2439f11a0f46STvrtko Ursulin 	struct drm_device *dev = arg;
2440fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2441f11a0f46STvrtko Ursulin 	u32 master_ctl;
2442e30e251aSVille Syrjälä 	u32 gt_iir[4] = {};
2443f11a0f46STvrtko Ursulin 	irqreturn_t ret;
2444f11a0f46STvrtko Ursulin 
2445f11a0f46STvrtko Ursulin 	if (!intel_irqs_enabled(dev_priv))
2446f11a0f46STvrtko Ursulin 		return IRQ_NONE;
2447f11a0f46STvrtko Ursulin 
2448f11a0f46STvrtko Ursulin 	master_ctl = I915_READ_FW(GEN8_MASTER_IRQ);
2449f11a0f46STvrtko Ursulin 	master_ctl &= ~GEN8_MASTER_IRQ_CONTROL;
2450f11a0f46STvrtko Ursulin 	if (!master_ctl)
2451f11a0f46STvrtko Ursulin 		return IRQ_NONE;
2452f11a0f46STvrtko Ursulin 
2453f11a0f46STvrtko Ursulin 	I915_WRITE_FW(GEN8_MASTER_IRQ, 0);
2454f11a0f46STvrtko Ursulin 
2455f11a0f46STvrtko Ursulin 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
2456f11a0f46STvrtko Ursulin 	disable_rpm_wakeref_asserts(dev_priv);
2457f11a0f46STvrtko Ursulin 
2458f11a0f46STvrtko Ursulin 	/* Find, clear, then process each source of interrupt */
2459e30e251aSVille Syrjälä 	ret = gen8_gt_irq_ack(dev_priv, master_ctl, gt_iir);
2460e30e251aSVille Syrjälä 	gen8_gt_irq_handler(dev_priv, gt_iir);
2461f11a0f46STvrtko Ursulin 	ret |= gen8_de_irq_handler(dev_priv, master_ctl);
2462f11a0f46STvrtko Ursulin 
2463cb0d205eSChris Wilson 	I915_WRITE_FW(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
2464cb0d205eSChris Wilson 	POSTING_READ_FW(GEN8_MASTER_IRQ);
2465abd58f01SBen Widawsky 
24661f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
24671f814dacSImre Deak 
2468abd58f01SBen Widawsky 	return ret;
2469abd58f01SBen Widawsky }
2470abd58f01SBen Widawsky 
24711f15b76fSChris Wilson static void i915_error_wake_up(struct drm_i915_private *dev_priv)
247217e1df07SDaniel Vetter {
247317e1df07SDaniel Vetter 	/*
247417e1df07SDaniel Vetter 	 * Notify all waiters for GPU completion events that reset state has
247517e1df07SDaniel Vetter 	 * been changed, and that they need to restart their wait after
247617e1df07SDaniel Vetter 	 * checking for potential errors (and bail out to drop locks if there is
247717e1df07SDaniel Vetter 	 * a gpu reset pending so that i915_error_work_func can acquire them).
247817e1df07SDaniel Vetter 	 */
247917e1df07SDaniel Vetter 
248017e1df07SDaniel Vetter 	/* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
24811f15b76fSChris Wilson 	wake_up_all(&dev_priv->gpu_error.wait_queue);
248217e1df07SDaniel Vetter 
248317e1df07SDaniel Vetter 	/* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
248417e1df07SDaniel Vetter 	wake_up_all(&dev_priv->pending_flip_queue);
248517e1df07SDaniel Vetter }
248617e1df07SDaniel Vetter 
24878a905236SJesse Barnes /**
2488b8d24a06SMika Kuoppala  * i915_reset_and_wakeup - do process context error handling work
248914bb2c11STvrtko Ursulin  * @dev_priv: i915 device private
24908a905236SJesse Barnes  *
24918a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
24928a905236SJesse Barnes  * was detected.
24938a905236SJesse Barnes  */
2494c033666aSChris Wilson static void i915_reset_and_wakeup(struct drm_i915_private *dev_priv)
24958a905236SJesse Barnes {
249691c8a326SChris Wilson 	struct kobject *kobj = &dev_priv->drm.primary->kdev->kobj;
2497cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
2498cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
2499cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
250017e1df07SDaniel Vetter 	int ret;
25018a905236SJesse Barnes 
2502c033666aSChris Wilson 	kobject_uevent_env(kobj, KOBJ_CHANGE, error_event);
25038a905236SJesse Barnes 
250444d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("resetting chip\n");
2505c033666aSChris Wilson 	kobject_uevent_env(kobj, KOBJ_CHANGE, reset_event);
25061f83fee0SDaniel Vetter 
250717e1df07SDaniel Vetter 	/*
2508f454c694SImre Deak 	 * In most cases it's guaranteed that we get here with an RPM
2509f454c694SImre Deak 	 * reference held, for example because there is a pending GPU
2510f454c694SImre Deak 	 * request that won't finish until the reset is done. This
2511f454c694SImre Deak 	 * isn't the case at least when we get here by doing a
2512f454c694SImre Deak 	 * simulated reset via debugs, so get an RPM reference.
2513f454c694SImre Deak 	 */
2514f454c694SImre Deak 	intel_runtime_pm_get(dev_priv);
25157514747dSVille Syrjälä 
2516c033666aSChris Wilson 	intel_prepare_reset(dev_priv);
25177514747dSVille Syrjälä 
2518f454c694SImre Deak 	/*
251917e1df07SDaniel Vetter 	 * All state reset _must_ be completed before we update the
252017e1df07SDaniel Vetter 	 * reset counter, for otherwise waiters might miss the reset
252117e1df07SDaniel Vetter 	 * pending state and not properly drop locks, resulting in
252217e1df07SDaniel Vetter 	 * deadlocks with the reset work.
252317e1df07SDaniel Vetter 	 */
2524*221fe799SChris Wilson 	mutex_lock(&dev_priv->drm.struct_mutex);
2525c033666aSChris Wilson 	ret = i915_reset(dev_priv);
2526*221fe799SChris Wilson 	mutex_unlock(&dev_priv->drm.struct_mutex);
2527f69061beSDaniel Vetter 
2528c033666aSChris Wilson 	intel_finish_reset(dev_priv);
252917e1df07SDaniel Vetter 
2530f454c694SImre Deak 	intel_runtime_pm_put(dev_priv);
2531f454c694SImre Deak 
2532d98c52cfSChris Wilson 	if (ret == 0)
2533c033666aSChris Wilson 		kobject_uevent_env(kobj,
2534f69061beSDaniel Vetter 				   KOBJ_CHANGE, reset_done_event);
25351f83fee0SDaniel Vetter 
253617e1df07SDaniel Vetter 	/*
253717e1df07SDaniel Vetter 	 * Note: The wake_up also serves as a memory barrier so that
25388af29b0cSChris Wilson 	 * waiters see the updated value of the dev_priv->gpu_error.
253917e1df07SDaniel Vetter 	 */
25401f15b76fSChris Wilson 	wake_up_all(&dev_priv->gpu_error.reset_queue);
2541f316a42cSBen Gamari }
25428a905236SJesse Barnes 
2543c033666aSChris Wilson static void i915_report_and_clear_eir(struct drm_i915_private *dev_priv)
2544c0e09200SDave Airlie {
2545bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
254663eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
2547050ee91fSBen Widawsky 	int pipe, i;
254863eeaf38SJesse Barnes 
254935aed2e6SChris Wilson 	if (!eir)
255035aed2e6SChris Wilson 		return;
255163eeaf38SJesse Barnes 
2552a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
25538a905236SJesse Barnes 
2554c033666aSChris Wilson 	i915_get_extra_instdone(dev_priv, instdone);
2555bd9854f9SBen Widawsky 
2556c033666aSChris Wilson 	if (IS_G4X(dev_priv)) {
25578a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
25588a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
25598a905236SJesse Barnes 
2560a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2561a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2562050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
2563050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2564a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2565a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
25668a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
25673143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
25688a905236SJesse Barnes 		}
25698a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
25708a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2571a70491ccSJoe Perches 			pr_err("page table error\n");
2572a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
25738a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
25743143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
25758a905236SJesse Barnes 		}
25768a905236SJesse Barnes 	}
25778a905236SJesse Barnes 
2578c033666aSChris Wilson 	if (!IS_GEN2(dev_priv)) {
257963eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
258063eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2581a70491ccSJoe Perches 			pr_err("page table error\n");
2582a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
258363eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
25843143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
258563eeaf38SJesse Barnes 		}
25868a905236SJesse Barnes 	}
25878a905236SJesse Barnes 
258863eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
2589a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
2590055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
2591a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
25929db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
259363eeaf38SJesse Barnes 		/* pipestat has already been acked */
259463eeaf38SJesse Barnes 	}
259563eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
2596a70491ccSJoe Perches 		pr_err("instruction error\n");
2597a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
2598050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
2599050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2600c033666aSChris Wilson 		if (INTEL_GEN(dev_priv) < 4) {
260163eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
260263eeaf38SJesse Barnes 
2603a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
2604a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
2605a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
260663eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
26073143a2bfSChris Wilson 			POSTING_READ(IPEIR);
260863eeaf38SJesse Barnes 		} else {
260963eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
261063eeaf38SJesse Barnes 
2611a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2612a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2613a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2614a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
261563eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
26163143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
261763eeaf38SJesse Barnes 		}
261863eeaf38SJesse Barnes 	}
261963eeaf38SJesse Barnes 
262063eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
26213143a2bfSChris Wilson 	POSTING_READ(EIR);
262263eeaf38SJesse Barnes 	eir = I915_READ(EIR);
262363eeaf38SJesse Barnes 	if (eir) {
262463eeaf38SJesse Barnes 		/*
262563eeaf38SJesse Barnes 		 * some errors might have become stuck,
262663eeaf38SJesse Barnes 		 * mask them.
262763eeaf38SJesse Barnes 		 */
262863eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
262963eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
263063eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
263163eeaf38SJesse Barnes 	}
263235aed2e6SChris Wilson }
263335aed2e6SChris Wilson 
263435aed2e6SChris Wilson /**
2635b8d24a06SMika Kuoppala  * i915_handle_error - handle a gpu error
263614bb2c11STvrtko Ursulin  * @dev_priv: i915 device private
263714b730fcSarun.siluvery@linux.intel.com  * @engine_mask: mask representing engines that are hung
2638aafd8581SJavier Martinez Canillas  * Do some basic checking of register state at error time and
263935aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
264035aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
264135aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
264235aed2e6SChris Wilson  * of a ring dump etc.).
264314bb2c11STvrtko Ursulin  * @fmt: Error message format string
264435aed2e6SChris Wilson  */
2645c033666aSChris Wilson void i915_handle_error(struct drm_i915_private *dev_priv,
2646c033666aSChris Wilson 		       u32 engine_mask,
264758174462SMika Kuoppala 		       const char *fmt, ...)
264835aed2e6SChris Wilson {
264958174462SMika Kuoppala 	va_list args;
265058174462SMika Kuoppala 	char error_msg[80];
265135aed2e6SChris Wilson 
265258174462SMika Kuoppala 	va_start(args, fmt);
265358174462SMika Kuoppala 	vscnprintf(error_msg, sizeof(error_msg), fmt, args);
265458174462SMika Kuoppala 	va_end(args);
265558174462SMika Kuoppala 
2656c033666aSChris Wilson 	i915_capture_error_state(dev_priv, engine_mask, error_msg);
2657c033666aSChris Wilson 	i915_report_and_clear_eir(dev_priv);
26588a905236SJesse Barnes 
26598af29b0cSChris Wilson 	if (!engine_mask)
26608af29b0cSChris Wilson 		return;
26618af29b0cSChris Wilson 
26628af29b0cSChris Wilson 	if (test_and_set_bit(I915_RESET_IN_PROGRESS,
26638af29b0cSChris Wilson 			     &dev_priv->gpu_error.flags))
26648af29b0cSChris Wilson 		return;
2665ba1234d1SBen Gamari 
266611ed50ecSBen Gamari 	/*
2667b8d24a06SMika Kuoppala 	 * Wakeup waiting processes so that the reset function
2668b8d24a06SMika Kuoppala 	 * i915_reset_and_wakeup doesn't deadlock trying to grab
2669b8d24a06SMika Kuoppala 	 * various locks. By bumping the reset counter first, the woken
267017e1df07SDaniel Vetter 	 * processes will see a reset in progress and back off,
267117e1df07SDaniel Vetter 	 * releasing their locks and then wait for the reset completion.
267217e1df07SDaniel Vetter 	 * We must do this for _all_ gpu waiters that might hold locks
267317e1df07SDaniel Vetter 	 * that the reset work needs to acquire.
267417e1df07SDaniel Vetter 	 *
26758af29b0cSChris Wilson 	 * Note: The wake_up also provides a memory barrier to ensure that the
26768af29b0cSChris Wilson 	 * waiters see the updated value of the reset flags.
267711ed50ecSBen Gamari 	 */
26781f15b76fSChris Wilson 	i915_error_wake_up(dev_priv);
267911ed50ecSBen Gamari 
2680c033666aSChris Wilson 	i915_reset_and_wakeup(dev_priv);
26818a905236SJesse Barnes }
26828a905236SJesse Barnes 
268342f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
268442f52ef8SKeith Packard  * we use as a pipe index
268542f52ef8SKeith Packard  */
268688e72717SThierry Reding static int i915_enable_vblank(struct drm_device *dev, unsigned int pipe)
26870a3e67a4SJesse Barnes {
2688fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2689e9d21d7fSKeith Packard 	unsigned long irqflags;
269071e0ffa5SJesse Barnes 
26911ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2692f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
26937c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2694755e9019SImre Deak 				     PIPE_START_VBLANK_INTERRUPT_STATUS);
26950a3e67a4SJesse Barnes 	else
26967c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2697755e9019SImre Deak 				     PIPE_VBLANK_INTERRUPT_STATUS);
26981ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26998692d00eSChris Wilson 
27000a3e67a4SJesse Barnes 	return 0;
27010a3e67a4SJesse Barnes }
27020a3e67a4SJesse Barnes 
270388e72717SThierry Reding static int ironlake_enable_vblank(struct drm_device *dev, unsigned int pipe)
2704f796cf8fSJesse Barnes {
2705fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2706f796cf8fSJesse Barnes 	unsigned long irqflags;
2707b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
270840da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2709f796cf8fSJesse Barnes 
2710f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2711fbdedaeaSVille Syrjälä 	ilk_enable_display_irq(dev_priv, bit);
2712b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2713b1f14ad0SJesse Barnes 
2714b1f14ad0SJesse Barnes 	return 0;
2715b1f14ad0SJesse Barnes }
2716b1f14ad0SJesse Barnes 
271788e72717SThierry Reding static int valleyview_enable_vblank(struct drm_device *dev, unsigned int pipe)
27187e231dbeSJesse Barnes {
2719fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
27207e231dbeSJesse Barnes 	unsigned long irqflags;
27217e231dbeSJesse Barnes 
27227e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
272331acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
2724755e9019SImre Deak 			     PIPE_START_VBLANK_INTERRUPT_STATUS);
27257e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
27267e231dbeSJesse Barnes 
27277e231dbeSJesse Barnes 	return 0;
27287e231dbeSJesse Barnes }
27297e231dbeSJesse Barnes 
273088e72717SThierry Reding static int gen8_enable_vblank(struct drm_device *dev, unsigned int pipe)
2731abd58f01SBen Widawsky {
2732fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2733abd58f01SBen Widawsky 	unsigned long irqflags;
2734abd58f01SBen Widawsky 
2735abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2736013d3752SVille Syrjälä 	bdw_enable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
2737abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2738013d3752SVille Syrjälä 
2739abd58f01SBen Widawsky 	return 0;
2740abd58f01SBen Widawsky }
2741abd58f01SBen Widawsky 
274242f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
274342f52ef8SKeith Packard  * we use as a pipe index
274442f52ef8SKeith Packard  */
274588e72717SThierry Reding static void i915_disable_vblank(struct drm_device *dev, unsigned int pipe)
27460a3e67a4SJesse Barnes {
2747fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2748e9d21d7fSKeith Packard 	unsigned long irqflags;
27490a3e67a4SJesse Barnes 
27501ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
27517c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
2752755e9019SImre Deak 			      PIPE_VBLANK_INTERRUPT_STATUS |
2753755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
27541ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
27550a3e67a4SJesse Barnes }
27560a3e67a4SJesse Barnes 
275788e72717SThierry Reding static void ironlake_disable_vblank(struct drm_device *dev, unsigned int pipe)
2758f796cf8fSJesse Barnes {
2759fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2760f796cf8fSJesse Barnes 	unsigned long irqflags;
2761b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
276240da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2763f796cf8fSJesse Barnes 
2764f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2765fbdedaeaSVille Syrjälä 	ilk_disable_display_irq(dev_priv, bit);
2766b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2767b1f14ad0SJesse Barnes }
2768b1f14ad0SJesse Barnes 
276988e72717SThierry Reding static void valleyview_disable_vblank(struct drm_device *dev, unsigned int pipe)
27707e231dbeSJesse Barnes {
2771fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
27727e231dbeSJesse Barnes 	unsigned long irqflags;
27737e231dbeSJesse Barnes 
27747e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
277531acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
2776755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
27777e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
27787e231dbeSJesse Barnes }
27797e231dbeSJesse Barnes 
278088e72717SThierry Reding static void gen8_disable_vblank(struct drm_device *dev, unsigned int pipe)
2781abd58f01SBen Widawsky {
2782fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2783abd58f01SBen Widawsky 	unsigned long irqflags;
2784abd58f01SBen Widawsky 
2785abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2786013d3752SVille Syrjälä 	bdw_disable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
2787abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2788abd58f01SBen Widawsky }
2789abd58f01SBen Widawsky 
27909107e9d2SChris Wilson static bool
279131bb59ccSChris Wilson ipehr_is_semaphore_wait(struct intel_engine_cs *engine, u32 ipehr)
2792a028c4b0SDaniel Vetter {
279331bb59ccSChris Wilson 	if (INTEL_GEN(engine->i915) >= 8) {
2794a6cdb93aSRodrigo Vivi 		return (ipehr >> 23) == 0x1c;
2795a028c4b0SDaniel Vetter 	} else {
2796a028c4b0SDaniel Vetter 		ipehr &= ~MI_SEMAPHORE_SYNC_MASK;
2797a028c4b0SDaniel Vetter 		return ipehr == (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE |
2798a028c4b0SDaniel Vetter 				 MI_SEMAPHORE_REGISTER);
2799a028c4b0SDaniel Vetter 	}
2800a028c4b0SDaniel Vetter }
2801a028c4b0SDaniel Vetter 
2802a4872ba6SOscar Mateo static struct intel_engine_cs *
28030bc40be8STvrtko Ursulin semaphore_wait_to_signaller_ring(struct intel_engine_cs *engine, u32 ipehr,
28040bc40be8STvrtko Ursulin 				 u64 offset)
2805921d42eaSDaniel Vetter {
2806c033666aSChris Wilson 	struct drm_i915_private *dev_priv = engine->i915;
2807a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2808921d42eaSDaniel Vetter 
2809c033666aSChris Wilson 	if (INTEL_GEN(dev_priv) >= 8) {
2810b4ac5afcSDave Gordon 		for_each_engine(signaller, dev_priv) {
28110bc40be8STvrtko Ursulin 			if (engine == signaller)
2812a6cdb93aSRodrigo Vivi 				continue;
2813a6cdb93aSRodrigo Vivi 
28140bc40be8STvrtko Ursulin 			if (offset == signaller->semaphore.signal_ggtt[engine->id])
2815a6cdb93aSRodrigo Vivi 				return signaller;
2816a6cdb93aSRodrigo Vivi 		}
2817921d42eaSDaniel Vetter 	} else {
2818921d42eaSDaniel Vetter 		u32 sync_bits = ipehr & MI_SEMAPHORE_SYNC_MASK;
2819921d42eaSDaniel Vetter 
2820b4ac5afcSDave Gordon 		for_each_engine(signaller, dev_priv) {
28210bc40be8STvrtko Ursulin 			if(engine == signaller)
2822921d42eaSDaniel Vetter 				continue;
2823921d42eaSDaniel Vetter 
28240bc40be8STvrtko Ursulin 			if (sync_bits == signaller->semaphore.mbox.wait[engine->id])
2825921d42eaSDaniel Vetter 				return signaller;
2826921d42eaSDaniel Vetter 		}
2827921d42eaSDaniel Vetter 	}
2828921d42eaSDaniel Vetter 
2829a6cdb93aSRodrigo Vivi 	DRM_ERROR("No signaller ring found for ring %i, ipehr 0x%08x, offset 0x%016llx\n",
28300bc40be8STvrtko Ursulin 		  engine->id, ipehr, offset);
2831921d42eaSDaniel Vetter 
2832921d42eaSDaniel Vetter 	return NULL;
2833921d42eaSDaniel Vetter }
2834921d42eaSDaniel Vetter 
2835a4872ba6SOscar Mateo static struct intel_engine_cs *
28360bc40be8STvrtko Ursulin semaphore_waits_for(struct intel_engine_cs *engine, u32 *seqno)
2837a24a11e6SChris Wilson {
2838c033666aSChris Wilson 	struct drm_i915_private *dev_priv = engine->i915;
2839406ea8d2SChris Wilson 	void __iomem *vaddr;
284088fe429dSDaniel Vetter 	u32 cmd, ipehr, head;
2841a6cdb93aSRodrigo Vivi 	u64 offset = 0;
2842a6cdb93aSRodrigo Vivi 	int i, backwards;
2843a24a11e6SChris Wilson 
2844381e8ae3STomas Elf 	/*
2845381e8ae3STomas Elf 	 * This function does not support execlist mode - any attempt to
2846381e8ae3STomas Elf 	 * proceed further into this function will result in a kernel panic
2847381e8ae3STomas Elf 	 * when dereferencing ring->buffer, which is not set up in execlist
2848381e8ae3STomas Elf 	 * mode.
2849381e8ae3STomas Elf 	 *
2850381e8ae3STomas Elf 	 * The correct way of doing it would be to derive the currently
2851381e8ae3STomas Elf 	 * executing ring buffer from the current context, which is derived
2852381e8ae3STomas Elf 	 * from the currently running request. Unfortunately, to get the
2853381e8ae3STomas Elf 	 * current request we would have to grab the struct_mutex before doing
2854381e8ae3STomas Elf 	 * anything else, which would be ill-advised since some other thread
2855381e8ae3STomas Elf 	 * might have grabbed it already and managed to hang itself, causing
2856381e8ae3STomas Elf 	 * the hang checker to deadlock.
2857381e8ae3STomas Elf 	 *
2858381e8ae3STomas Elf 	 * Therefore, this function does not support execlist mode in its
2859381e8ae3STomas Elf 	 * current form. Just return NULL and move on.
2860381e8ae3STomas Elf 	 */
28610bc40be8STvrtko Ursulin 	if (engine->buffer == NULL)
2862381e8ae3STomas Elf 		return NULL;
2863381e8ae3STomas Elf 
28640bc40be8STvrtko Ursulin 	ipehr = I915_READ(RING_IPEHR(engine->mmio_base));
286531bb59ccSChris Wilson 	if (!ipehr_is_semaphore_wait(engine, ipehr))
28666274f212SChris Wilson 		return NULL;
2867a24a11e6SChris Wilson 
286888fe429dSDaniel Vetter 	/*
286988fe429dSDaniel Vetter 	 * HEAD is likely pointing to the dword after the actual command,
287088fe429dSDaniel Vetter 	 * so scan backwards until we find the MBOX. But limit it to just 3
2871a6cdb93aSRodrigo Vivi 	 * or 4 dwords depending on the semaphore wait command size.
2872a6cdb93aSRodrigo Vivi 	 * Note that we don't care about ACTHD here since that might
287388fe429dSDaniel Vetter 	 * point at at batch, and semaphores are always emitted into the
287488fe429dSDaniel Vetter 	 * ringbuffer itself.
2875a24a11e6SChris Wilson 	 */
28760bc40be8STvrtko Ursulin 	head = I915_READ_HEAD(engine) & HEAD_ADDR;
2877c033666aSChris Wilson 	backwards = (INTEL_GEN(dev_priv) >= 8) ? 5 : 4;
2878f2f0ed71SChris Wilson 	vaddr = (void __iomem *)engine->buffer->vaddr;
287988fe429dSDaniel Vetter 
2880a6cdb93aSRodrigo Vivi 	for (i = backwards; i; --i) {
288188fe429dSDaniel Vetter 		/*
288288fe429dSDaniel Vetter 		 * Be paranoid and presume the hw has gone off into the wild -
288388fe429dSDaniel Vetter 		 * our ring is smaller than what the hardware (and hence
288488fe429dSDaniel Vetter 		 * HEAD_ADDR) allows. Also handles wrap-around.
288588fe429dSDaniel Vetter 		 */
28860bc40be8STvrtko Ursulin 		head &= engine->buffer->size - 1;
288788fe429dSDaniel Vetter 
288888fe429dSDaniel Vetter 		/* This here seems to blow up */
2889406ea8d2SChris Wilson 		cmd = ioread32(vaddr + head);
2890a24a11e6SChris Wilson 		if (cmd == ipehr)
2891a24a11e6SChris Wilson 			break;
2892a24a11e6SChris Wilson 
289388fe429dSDaniel Vetter 		head -= 4;
289488fe429dSDaniel Vetter 	}
2895a24a11e6SChris Wilson 
289688fe429dSDaniel Vetter 	if (!i)
289788fe429dSDaniel Vetter 		return NULL;
289888fe429dSDaniel Vetter 
2899406ea8d2SChris Wilson 	*seqno = ioread32(vaddr + head + 4) + 1;
2900c033666aSChris Wilson 	if (INTEL_GEN(dev_priv) >= 8) {
2901406ea8d2SChris Wilson 		offset = ioread32(vaddr + head + 12);
2902a6cdb93aSRodrigo Vivi 		offset <<= 32;
2903406ea8d2SChris Wilson 		offset |= ioread32(vaddr + head + 8);
2904a6cdb93aSRodrigo Vivi 	}
29050bc40be8STvrtko Ursulin 	return semaphore_wait_to_signaller_ring(engine, ipehr, offset);
2906a24a11e6SChris Wilson }
2907a24a11e6SChris Wilson 
29080bc40be8STvrtko Ursulin static int semaphore_passed(struct intel_engine_cs *engine)
29096274f212SChris Wilson {
2910c033666aSChris Wilson 	struct drm_i915_private *dev_priv = engine->i915;
2911a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2912a0d036b0SChris Wilson 	u32 seqno;
29136274f212SChris Wilson 
29140bc40be8STvrtko Ursulin 	engine->hangcheck.deadlock++;
29156274f212SChris Wilson 
29160bc40be8STvrtko Ursulin 	signaller = semaphore_waits_for(engine, &seqno);
29174be17381SChris Wilson 	if (signaller == NULL)
29184be17381SChris Wilson 		return -1;
29194be17381SChris Wilson 
29204be17381SChris Wilson 	/* Prevent pathological recursion due to driver bugs */
2921666796daSTvrtko Ursulin 	if (signaller->hangcheck.deadlock >= I915_NUM_ENGINES)
29226274f212SChris Wilson 		return -1;
29236274f212SChris Wilson 
29241b7744e7SChris Wilson 	if (i915_seqno_passed(intel_engine_get_seqno(signaller), seqno))
29254be17381SChris Wilson 		return 1;
29264be17381SChris Wilson 
2927a0d036b0SChris Wilson 	/* cursory check for an unkickable deadlock */
2928a0d036b0SChris Wilson 	if (I915_READ_CTL(signaller) & RING_WAIT_SEMAPHORE &&
2929a0d036b0SChris Wilson 	    semaphore_passed(signaller) < 0)
29304be17381SChris Wilson 		return -1;
29314be17381SChris Wilson 
29324be17381SChris Wilson 	return 0;
29336274f212SChris Wilson }
29346274f212SChris Wilson 
29356274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
29366274f212SChris Wilson {
2937e2f80391STvrtko Ursulin 	struct intel_engine_cs *engine;
29386274f212SChris Wilson 
2939b4ac5afcSDave Gordon 	for_each_engine(engine, dev_priv)
2940e2f80391STvrtko Ursulin 		engine->hangcheck.deadlock = 0;
29416274f212SChris Wilson }
29426274f212SChris Wilson 
29430bc40be8STvrtko Ursulin static bool subunits_stuck(struct intel_engine_cs *engine)
29441ec14ad3SChris Wilson {
294561642ff0SMika Kuoppala 	u32 instdone[I915_NUM_INSTDONE_REG];
294661642ff0SMika Kuoppala 	bool stuck;
294761642ff0SMika Kuoppala 	int i;
29489107e9d2SChris Wilson 
29490bc40be8STvrtko Ursulin 	if (engine->id != RCS)
295061642ff0SMika Kuoppala 		return true;
295161642ff0SMika Kuoppala 
2952c033666aSChris Wilson 	i915_get_extra_instdone(engine->i915, instdone);
295361642ff0SMika Kuoppala 
295461642ff0SMika Kuoppala 	/* There might be unstable subunit states even when
295561642ff0SMika Kuoppala 	 * actual head is not moving. Filter out the unstable ones by
295661642ff0SMika Kuoppala 	 * accumulating the undone -> done transitions and only
295761642ff0SMika Kuoppala 	 * consider those as progress.
295861642ff0SMika Kuoppala 	 */
295961642ff0SMika Kuoppala 	stuck = true;
296061642ff0SMika Kuoppala 	for (i = 0; i < I915_NUM_INSTDONE_REG; i++) {
29610bc40be8STvrtko Ursulin 		const u32 tmp = instdone[i] | engine->hangcheck.instdone[i];
296261642ff0SMika Kuoppala 
29630bc40be8STvrtko Ursulin 		if (tmp != engine->hangcheck.instdone[i])
296461642ff0SMika Kuoppala 			stuck = false;
296561642ff0SMika Kuoppala 
29660bc40be8STvrtko Ursulin 		engine->hangcheck.instdone[i] |= tmp;
296761642ff0SMika Kuoppala 	}
296861642ff0SMika Kuoppala 
296961642ff0SMika Kuoppala 	return stuck;
297061642ff0SMika Kuoppala }
297161642ff0SMika Kuoppala 
29727e37f889SChris Wilson static enum intel_engine_hangcheck_action
29730bc40be8STvrtko Ursulin head_stuck(struct intel_engine_cs *engine, u64 acthd)
297461642ff0SMika Kuoppala {
29750bc40be8STvrtko Ursulin 	if (acthd != engine->hangcheck.acthd) {
297661642ff0SMika Kuoppala 
297761642ff0SMika Kuoppala 		/* Clear subunit states on head movement */
29780bc40be8STvrtko Ursulin 		memset(engine->hangcheck.instdone, 0,
29790bc40be8STvrtko Ursulin 		       sizeof(engine->hangcheck.instdone));
298061642ff0SMika Kuoppala 
2981f2f4d82fSJani Nikula 		return HANGCHECK_ACTIVE;
2982f260fe7bSMika Kuoppala 	}
2983f260fe7bSMika Kuoppala 
29840bc40be8STvrtko Ursulin 	if (!subunits_stuck(engine))
298561642ff0SMika Kuoppala 		return HANGCHECK_ACTIVE;
298661642ff0SMika Kuoppala 
298761642ff0SMika Kuoppala 	return HANGCHECK_HUNG;
298861642ff0SMika Kuoppala }
298961642ff0SMika Kuoppala 
29907e37f889SChris Wilson static enum intel_engine_hangcheck_action
29917e37f889SChris Wilson engine_stuck(struct intel_engine_cs *engine, u64 acthd)
299261642ff0SMika Kuoppala {
2993c033666aSChris Wilson 	struct drm_i915_private *dev_priv = engine->i915;
29947e37f889SChris Wilson 	enum intel_engine_hangcheck_action ha;
299561642ff0SMika Kuoppala 	u32 tmp;
299661642ff0SMika Kuoppala 
29970bc40be8STvrtko Ursulin 	ha = head_stuck(engine, acthd);
299861642ff0SMika Kuoppala 	if (ha != HANGCHECK_HUNG)
299961642ff0SMika Kuoppala 		return ha;
300061642ff0SMika Kuoppala 
3001c033666aSChris Wilson 	if (IS_GEN2(dev_priv))
3002f2f4d82fSJani Nikula 		return HANGCHECK_HUNG;
30039107e9d2SChris Wilson 
30049107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
30059107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
30069107e9d2SChris Wilson 	 * and break the hang. This should work on
30079107e9d2SChris Wilson 	 * all but the second generation chipsets.
30089107e9d2SChris Wilson 	 */
30090bc40be8STvrtko Ursulin 	tmp = I915_READ_CTL(engine);
30101ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
3011c033666aSChris Wilson 		i915_handle_error(dev_priv, 0,
301258174462SMika Kuoppala 				  "Kicking stuck wait on %s",
30130bc40be8STvrtko Ursulin 				  engine->name);
30140bc40be8STvrtko Ursulin 		I915_WRITE_CTL(engine, tmp);
3015f2f4d82fSJani Nikula 		return HANGCHECK_KICK;
30161ec14ad3SChris Wilson 	}
3017a24a11e6SChris Wilson 
3018c033666aSChris Wilson 	if (INTEL_GEN(dev_priv) >= 6 && tmp & RING_WAIT_SEMAPHORE) {
30190bc40be8STvrtko Ursulin 		switch (semaphore_passed(engine)) {
30206274f212SChris Wilson 		default:
3021f2f4d82fSJani Nikula 			return HANGCHECK_HUNG;
30226274f212SChris Wilson 		case 1:
3023c033666aSChris Wilson 			i915_handle_error(dev_priv, 0,
302458174462SMika Kuoppala 					  "Kicking stuck semaphore on %s",
30250bc40be8STvrtko Ursulin 					  engine->name);
30260bc40be8STvrtko Ursulin 			I915_WRITE_CTL(engine, tmp);
3027f2f4d82fSJani Nikula 			return HANGCHECK_KICK;
30286274f212SChris Wilson 		case 0:
3029f2f4d82fSJani Nikula 			return HANGCHECK_WAIT;
30306274f212SChris Wilson 		}
30319107e9d2SChris Wilson 	}
30329107e9d2SChris Wilson 
3033f2f4d82fSJani Nikula 	return HANGCHECK_HUNG;
3034a24a11e6SChris Wilson }
3035d1e61e7fSChris Wilson 
3036737b1506SChris Wilson /*
3037f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
303805407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
303905407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
304005407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
304105407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
304205407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
3043f65d9421SBen Gamari  */
3044737b1506SChris Wilson static void i915_hangcheck_elapsed(struct work_struct *work)
3045f65d9421SBen Gamari {
3046737b1506SChris Wilson 	struct drm_i915_private *dev_priv =
3047737b1506SChris Wilson 		container_of(work, typeof(*dev_priv),
3048737b1506SChris Wilson 			     gpu_error.hangcheck_work.work);
3049e2f80391STvrtko Ursulin 	struct intel_engine_cs *engine;
30502b284288SChris Wilson 	unsigned int hung = 0, stuck = 0;
30512b284288SChris Wilson 	int busy_count = 0;
30529107e9d2SChris Wilson #define BUSY 1
30539107e9d2SChris Wilson #define KICK 5
30549107e9d2SChris Wilson #define HUNG 20
305524a65e62SMika Kuoppala #define ACTIVE_DECAY 15
3056893eead0SChris Wilson 
3057d330a953SJani Nikula 	if (!i915.enable_hangcheck)
30583e0dc6b0SBen Widawsky 		return;
30593e0dc6b0SBen Widawsky 
3060b1379d49SChris Wilson 	if (!READ_ONCE(dev_priv->gt.awake))
306167d97da3SChris Wilson 		return;
30621f814dacSImre Deak 
306375714940SMika Kuoppala 	/* As enabling the GPU requires fairly extensive mmio access,
306475714940SMika Kuoppala 	 * periodically arm the mmio checker to see if we are triggering
306575714940SMika Kuoppala 	 * any invalid access.
306675714940SMika Kuoppala 	 */
306775714940SMika Kuoppala 	intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
306875714940SMika Kuoppala 
30692b284288SChris Wilson 	for_each_engine(engine, dev_priv) {
3070688e6c72SChris Wilson 		bool busy = intel_engine_has_waiter(engine);
307150877445SChris Wilson 		u64 acthd;
307250877445SChris Wilson 		u32 seqno;
307334730fedSChris Wilson 		u32 submit;
3074b4519513SChris Wilson 
30756274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
30766274f212SChris Wilson 
3077c04e0f3bSChris Wilson 		/* We don't strictly need an irq-barrier here, as we are not
3078c04e0f3bSChris Wilson 		 * serving an interrupt request, be paranoid in case the
3079c04e0f3bSChris Wilson 		 * barrier has side-effects (such as preventing a broken
3080c04e0f3bSChris Wilson 		 * cacheline snoop) and so be sure that we can see the seqno
3081c04e0f3bSChris Wilson 		 * advance. If the seqno should stick, due to a stale
3082c04e0f3bSChris Wilson 		 * cacheline, we would erroneously declare the GPU hung.
3083c04e0f3bSChris Wilson 		 */
3084c04e0f3bSChris Wilson 		if (engine->irq_seqno_barrier)
3085c04e0f3bSChris Wilson 			engine->irq_seqno_barrier(engine);
3086c04e0f3bSChris Wilson 
30877e37f889SChris Wilson 		acthd = intel_engine_get_active_head(engine);
30881b7744e7SChris Wilson 		seqno = intel_engine_get_seqno(engine);
308934730fedSChris Wilson 		submit = READ_ONCE(engine->last_submitted_seqno);
309005407ff8SMika Kuoppala 
3091e2f80391STvrtko Ursulin 		if (engine->hangcheck.seqno == seqno) {
309234730fedSChris Wilson 			if (i915_seqno_passed(seqno, submit)) {
3093e2f80391STvrtko Ursulin 				engine->hangcheck.action = HANGCHECK_IDLE;
309405535726SChris Wilson 				if (busy) {
3095094f9a54SChris Wilson 					/* Safeguard against driver failure */
3096e2f80391STvrtko Ursulin 					engine->hangcheck.score += BUSY;
309705535726SChris Wilson 				}
309805407ff8SMika Kuoppala 			} else {
30996274f212SChris Wilson 				/* We always increment the hangcheck score
31009930ca1aSChris Wilson 				 * if the engine is busy and still processing
31016274f212SChris Wilson 				 * the same request, so that no single request
31026274f212SChris Wilson 				 * can run indefinitely (such as a chain of
31036274f212SChris Wilson 				 * batches). The only time we do not increment
31046274f212SChris Wilson 				 * the hangcheck score on this ring, if this
31059930ca1aSChris Wilson 				 * engine is in a legitimate wait for another
31069930ca1aSChris Wilson 				 * engine. In that case the waiting engine is a
31076274f212SChris Wilson 				 * victim and we want to be sure we catch the
31086274f212SChris Wilson 				 * right culprit. Then every time we do kick
31096274f212SChris Wilson 				 * the ring, add a small increment to the
31106274f212SChris Wilson 				 * score so that we can catch a batch that is
31116274f212SChris Wilson 				 * being repeatedly kicked and so responsible
31126274f212SChris Wilson 				 * for stalling the machine.
31139107e9d2SChris Wilson 				 */
31147e37f889SChris Wilson 				engine->hangcheck.action =
31157e37f889SChris Wilson 					engine_stuck(engine, acthd);
3116ad8beaeaSMika Kuoppala 
3117e2f80391STvrtko Ursulin 				switch (engine->hangcheck.action) {
3118da661464SMika Kuoppala 				case HANGCHECK_IDLE:
3119f2f4d82fSJani Nikula 				case HANGCHECK_WAIT:
3120f260fe7bSMika Kuoppala 					break;
312124a65e62SMika Kuoppala 				case HANGCHECK_ACTIVE:
3122e2f80391STvrtko Ursulin 					engine->hangcheck.score += BUSY;
31236274f212SChris Wilson 					break;
3124f2f4d82fSJani Nikula 				case HANGCHECK_KICK:
3125e2f80391STvrtko Ursulin 					engine->hangcheck.score += KICK;
31266274f212SChris Wilson 					break;
3127f2f4d82fSJani Nikula 				case HANGCHECK_HUNG:
3128e2f80391STvrtko Ursulin 					engine->hangcheck.score += HUNG;
31296274f212SChris Wilson 					break;
31306274f212SChris Wilson 				}
313105407ff8SMika Kuoppala 			}
31322b284288SChris Wilson 
31332b284288SChris Wilson 			if (engine->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG) {
31342b284288SChris Wilson 				hung |= intel_engine_flag(engine);
31352b284288SChris Wilson 				if (engine->hangcheck.action != HANGCHECK_HUNG)
31362b284288SChris Wilson 					stuck |= intel_engine_flag(engine);
31372b284288SChris Wilson 			}
31389107e9d2SChris Wilson 		} else {
3139e2f80391STvrtko Ursulin 			engine->hangcheck.action = HANGCHECK_ACTIVE;
3140da661464SMika Kuoppala 
31419107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
31429107e9d2SChris Wilson 			 * attempts across multiple batches.
31439107e9d2SChris Wilson 			 */
3144e2f80391STvrtko Ursulin 			if (engine->hangcheck.score > 0)
3145e2f80391STvrtko Ursulin 				engine->hangcheck.score -= ACTIVE_DECAY;
3146e2f80391STvrtko Ursulin 			if (engine->hangcheck.score < 0)
3147e2f80391STvrtko Ursulin 				engine->hangcheck.score = 0;
3148f260fe7bSMika Kuoppala 
314961642ff0SMika Kuoppala 			/* Clear head and subunit states on seqno movement */
315012471ba8SChris Wilson 			acthd = 0;
315161642ff0SMika Kuoppala 
3152e2f80391STvrtko Ursulin 			memset(engine->hangcheck.instdone, 0,
3153e2f80391STvrtko Ursulin 			       sizeof(engine->hangcheck.instdone));
3154cbb465e7SChris Wilson 		}
3155f65d9421SBen Gamari 
3156e2f80391STvrtko Ursulin 		engine->hangcheck.seqno = seqno;
3157e2f80391STvrtko Ursulin 		engine->hangcheck.acthd = acthd;
31589107e9d2SChris Wilson 		busy_count += busy;
315905407ff8SMika Kuoppala 	}
316005407ff8SMika Kuoppala 
31612b284288SChris Wilson 	if (hung) {
31622b284288SChris Wilson 		char msg[80];
3163bafb0fceSChris Wilson 		unsigned int tmp;
31642b284288SChris Wilson 		int len;
316505407ff8SMika Kuoppala 
31662b284288SChris Wilson 		/* If some rings hung but others were still busy, only
31672b284288SChris Wilson 		 * blame the hanging rings in the synopsis.
31682b284288SChris Wilson 		 */
31692b284288SChris Wilson 		if (stuck != hung)
31702b284288SChris Wilson 			hung &= ~stuck;
31712b284288SChris Wilson 		len = scnprintf(msg, sizeof(msg),
31722b284288SChris Wilson 				"%s on ", stuck == hung ? "No progress" : "Hang");
3173bafb0fceSChris Wilson 		for_each_engine_masked(engine, dev_priv, hung, tmp)
31742b284288SChris Wilson 			len += scnprintf(msg + len, sizeof(msg) - len,
31752b284288SChris Wilson 					 "%s, ", engine->name);
31762b284288SChris Wilson 		msg[len-2] = '\0';
31772b284288SChris Wilson 
31782b284288SChris Wilson 		return i915_handle_error(dev_priv, hung, msg);
31792b284288SChris Wilson 	}
318005407ff8SMika Kuoppala 
318105535726SChris Wilson 	/* Reset timer in case GPU hangs without another request being added */
318205407ff8SMika Kuoppala 	if (busy_count)
3183c033666aSChris Wilson 		i915_queue_hangcheck(dev_priv);
318410cd45b6SMika Kuoppala }
318510cd45b6SMika Kuoppala 
31861c69eb42SPaulo Zanoni static void ibx_irq_reset(struct drm_device *dev)
318791738a95SPaulo Zanoni {
3188fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
318991738a95SPaulo Zanoni 
319091738a95SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
319191738a95SPaulo Zanoni 		return;
319291738a95SPaulo Zanoni 
3193f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(SDE);
3194105b122eSPaulo Zanoni 
3195105b122eSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
3196105b122eSPaulo Zanoni 		I915_WRITE(SERR_INT, 0xffffffff);
3197622364b6SPaulo Zanoni }
3198105b122eSPaulo Zanoni 
319991738a95SPaulo Zanoni /*
3200622364b6SPaulo Zanoni  * SDEIER is also touched by the interrupt handler to work around missed PCH
3201622364b6SPaulo Zanoni  * interrupts. Hence we can't update it after the interrupt handler is enabled -
3202622364b6SPaulo Zanoni  * instead we unconditionally enable all PCH interrupt sources here, but then
3203622364b6SPaulo Zanoni  * only unmask them as needed with SDEIMR.
3204622364b6SPaulo Zanoni  *
3205622364b6SPaulo Zanoni  * This function needs to be called before interrupts are enabled.
320691738a95SPaulo Zanoni  */
3207622364b6SPaulo Zanoni static void ibx_irq_pre_postinstall(struct drm_device *dev)
3208622364b6SPaulo Zanoni {
3209fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3210622364b6SPaulo Zanoni 
3211622364b6SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
3212622364b6SPaulo Zanoni 		return;
3213622364b6SPaulo Zanoni 
3214622364b6SPaulo Zanoni 	WARN_ON(I915_READ(SDEIER) != 0);
321591738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
321691738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
321791738a95SPaulo Zanoni }
321891738a95SPaulo Zanoni 
32197c4d664eSPaulo Zanoni static void gen5_gt_irq_reset(struct drm_device *dev)
3220d18ea1b5SDaniel Vetter {
3221fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3222d18ea1b5SDaniel Vetter 
3223f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GT);
3224a9d356a6SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6)
3225f86f3fb0SPaulo Zanoni 		GEN5_IRQ_RESET(GEN6_PM);
3226d18ea1b5SDaniel Vetter }
3227d18ea1b5SDaniel Vetter 
322870591a41SVille Syrjälä static void vlv_display_irq_reset(struct drm_i915_private *dev_priv)
322970591a41SVille Syrjälä {
323070591a41SVille Syrjälä 	enum pipe pipe;
323170591a41SVille Syrjälä 
323271b8b41dSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
323371b8b41dSVille Syrjälä 		I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
323471b8b41dSVille Syrjälä 	else
323571b8b41dSVille Syrjälä 		I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
323671b8b41dSVille Syrjälä 
3237ad22d106SVille Syrjälä 	i915_hotplug_interrupt_update_locked(dev_priv, 0xffffffff, 0);
323870591a41SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
323970591a41SVille Syrjälä 
3240ad22d106SVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
3241ad22d106SVille Syrjälä 		I915_WRITE(PIPESTAT(pipe),
3242ad22d106SVille Syrjälä 			   PIPE_FIFO_UNDERRUN_STATUS |
3243ad22d106SVille Syrjälä 			   PIPESTAT_INT_STATUS_MASK);
3244ad22d106SVille Syrjälä 		dev_priv->pipestat_irq_mask[pipe] = 0;
3245ad22d106SVille Syrjälä 	}
324670591a41SVille Syrjälä 
324770591a41SVille Syrjälä 	GEN5_IRQ_RESET(VLV_);
3248ad22d106SVille Syrjälä 	dev_priv->irq_mask = ~0;
324970591a41SVille Syrjälä }
325070591a41SVille Syrjälä 
32518bb61306SVille Syrjälä static void vlv_display_irq_postinstall(struct drm_i915_private *dev_priv)
32528bb61306SVille Syrjälä {
32538bb61306SVille Syrjälä 	u32 pipestat_mask;
32549ab981f2SVille Syrjälä 	u32 enable_mask;
32558bb61306SVille Syrjälä 	enum pipe pipe;
32568bb61306SVille Syrjälä 
32578bb61306SVille Syrjälä 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
32588bb61306SVille Syrjälä 			PIPE_CRC_DONE_INTERRUPT_STATUS;
32598bb61306SVille Syrjälä 
32608bb61306SVille Syrjälä 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
32618bb61306SVille Syrjälä 	for_each_pipe(dev_priv, pipe)
32628bb61306SVille Syrjälä 		i915_enable_pipestat(dev_priv, pipe, pipestat_mask);
32638bb61306SVille Syrjälä 
32649ab981f2SVille Syrjälä 	enable_mask = I915_DISPLAY_PORT_INTERRUPT |
32658bb61306SVille Syrjälä 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
32668bb61306SVille Syrjälä 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
32678bb61306SVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
32689ab981f2SVille Syrjälä 		enable_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
32696b7eafc1SVille Syrjälä 
32706b7eafc1SVille Syrjälä 	WARN_ON(dev_priv->irq_mask != ~0);
32716b7eafc1SVille Syrjälä 
32729ab981f2SVille Syrjälä 	dev_priv->irq_mask = ~enable_mask;
32738bb61306SVille Syrjälä 
32749ab981f2SVille Syrjälä 	GEN5_IRQ_INIT(VLV_, dev_priv->irq_mask, enable_mask);
32758bb61306SVille Syrjälä }
32768bb61306SVille Syrjälä 
32778bb61306SVille Syrjälä /* drm_dma.h hooks
32788bb61306SVille Syrjälä */
32798bb61306SVille Syrjälä static void ironlake_irq_reset(struct drm_device *dev)
32808bb61306SVille Syrjälä {
3281fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
32828bb61306SVille Syrjälä 
32838bb61306SVille Syrjälä 	I915_WRITE(HWSTAM, 0xffffffff);
32848bb61306SVille Syrjälä 
32858bb61306SVille Syrjälä 	GEN5_IRQ_RESET(DE);
32868bb61306SVille Syrjälä 	if (IS_GEN7(dev))
32878bb61306SVille Syrjälä 		I915_WRITE(GEN7_ERR_INT, 0xffffffff);
32888bb61306SVille Syrjälä 
32898bb61306SVille Syrjälä 	gen5_gt_irq_reset(dev);
32908bb61306SVille Syrjälä 
32918bb61306SVille Syrjälä 	ibx_irq_reset(dev);
32928bb61306SVille Syrjälä }
32938bb61306SVille Syrjälä 
32947e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
32957e231dbeSJesse Barnes {
3296fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
32977e231dbeSJesse Barnes 
329834c7b8a7SVille Syrjälä 	I915_WRITE(VLV_MASTER_IER, 0);
329934c7b8a7SVille Syrjälä 	POSTING_READ(VLV_MASTER_IER);
330034c7b8a7SVille Syrjälä 
33017c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
33027e231dbeSJesse Barnes 
3303ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
33049918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
330570591a41SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3306ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
33077e231dbeSJesse Barnes }
33087e231dbeSJesse Barnes 
3309d6e3cca3SDaniel Vetter static void gen8_gt_irq_reset(struct drm_i915_private *dev_priv)
3310d6e3cca3SDaniel Vetter {
3311d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 0);
3312d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 1);
3313d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 2);
3314d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 3);
3315d6e3cca3SDaniel Vetter }
3316d6e3cca3SDaniel Vetter 
3317823f6b38SPaulo Zanoni static void gen8_irq_reset(struct drm_device *dev)
3318abd58f01SBen Widawsky {
3319fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3320abd58f01SBen Widawsky 	int pipe;
3321abd58f01SBen Widawsky 
3322abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
3323abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3324abd58f01SBen Widawsky 
3325d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
3326abd58f01SBen Widawsky 
3327055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3328f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3329813bde43SPaulo Zanoni 						   POWER_DOMAIN_PIPE(pipe)))
3330f86f3fb0SPaulo Zanoni 			GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3331abd58f01SBen Widawsky 
3332f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_PORT_);
3333f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_MISC_);
3334f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_PCU_);
3335abd58f01SBen Widawsky 
3336266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev))
33371c69eb42SPaulo Zanoni 		ibx_irq_reset(dev);
3338abd58f01SBen Widawsky }
3339abd58f01SBen Widawsky 
33404c6c03beSDamien Lespiau void gen8_irq_power_well_post_enable(struct drm_i915_private *dev_priv,
33414c6c03beSDamien Lespiau 				     unsigned int pipe_mask)
3342d49bdb0eSPaulo Zanoni {
33431180e206SPaulo Zanoni 	uint32_t extra_ier = GEN8_PIPE_VBLANK | GEN8_PIPE_FIFO_UNDERRUN;
33446831f3e3SVille Syrjälä 	enum pipe pipe;
3345d49bdb0eSPaulo Zanoni 
334613321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
33476831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
33486831f3e3SVille Syrjälä 		GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
33496831f3e3SVille Syrjälä 				  dev_priv->de_irq_mask[pipe],
33506831f3e3SVille Syrjälä 				  ~dev_priv->de_irq_mask[pipe] | extra_ier);
335113321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3352d49bdb0eSPaulo Zanoni }
3353d49bdb0eSPaulo Zanoni 
3354aae8ba84SVille Syrjälä void gen8_irq_power_well_pre_disable(struct drm_i915_private *dev_priv,
3355aae8ba84SVille Syrjälä 				     unsigned int pipe_mask)
3356aae8ba84SVille Syrjälä {
33576831f3e3SVille Syrjälä 	enum pipe pipe;
33586831f3e3SVille Syrjälä 
3359aae8ba84SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
33606831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
33616831f3e3SVille Syrjälä 		GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3362aae8ba84SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3363aae8ba84SVille Syrjälä 
3364aae8ba84SVille Syrjälä 	/* make sure we're done processing display irqs */
336591c8a326SChris Wilson 	synchronize_irq(dev_priv->drm.irq);
3366aae8ba84SVille Syrjälä }
3367aae8ba84SVille Syrjälä 
336843f328d7SVille Syrjälä static void cherryview_irq_preinstall(struct drm_device *dev)
336943f328d7SVille Syrjälä {
3370fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
337143f328d7SVille Syrjälä 
337243f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
337343f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
337443f328d7SVille Syrjälä 
3375d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
337643f328d7SVille Syrjälä 
337743f328d7SVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
337843f328d7SVille Syrjälä 
3379ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
33809918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
338170591a41SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3382ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
338343f328d7SVille Syrjälä }
338443f328d7SVille Syrjälä 
338591d14251STvrtko Ursulin static u32 intel_hpd_enabled_irqs(struct drm_i915_private *dev_priv,
338687a02106SVille Syrjälä 				  const u32 hpd[HPD_NUM_PINS])
338787a02106SVille Syrjälä {
338887a02106SVille Syrjälä 	struct intel_encoder *encoder;
338987a02106SVille Syrjälä 	u32 enabled_irqs = 0;
339087a02106SVille Syrjälä 
339191c8a326SChris Wilson 	for_each_intel_encoder(&dev_priv->drm, encoder)
339287a02106SVille Syrjälä 		if (dev_priv->hotplug.stats[encoder->hpd_pin].state == HPD_ENABLED)
339387a02106SVille Syrjälä 			enabled_irqs |= hpd[encoder->hpd_pin];
339487a02106SVille Syrjälä 
339587a02106SVille Syrjälä 	return enabled_irqs;
339687a02106SVille Syrjälä }
339787a02106SVille Syrjälä 
339891d14251STvrtko Ursulin static void ibx_hpd_irq_setup(struct drm_i915_private *dev_priv)
339982a28bcfSDaniel Vetter {
340087a02106SVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
340182a28bcfSDaniel Vetter 
340291d14251STvrtko Ursulin 	if (HAS_PCH_IBX(dev_priv)) {
3403fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
340491d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ibx);
340582a28bcfSDaniel Vetter 	} else {
3406fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
340791d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_cpt);
340882a28bcfSDaniel Vetter 	}
340982a28bcfSDaniel Vetter 
3410fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
341182a28bcfSDaniel Vetter 
34127fe0b973SKeith Packard 	/*
34137fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
34146dbf30ceSVille Syrjälä 	 * duration to 2ms (which is the minimum in the Display Port spec).
34156dbf30ceSVille Syrjälä 	 * The pulse duration bits are reserved on LPT+.
34167fe0b973SKeith Packard 	 */
34177fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
34187fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
34197fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
34207fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
34217fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
34220b2eb33eSVille Syrjälä 	/*
34230b2eb33eSVille Syrjälä 	 * When CPU and PCH are on the same package, port A
34240b2eb33eSVille Syrjälä 	 * HPD must be enabled in both north and south.
34250b2eb33eSVille Syrjälä 	 */
342691d14251STvrtko Ursulin 	if (HAS_PCH_LPT_LP(dev_priv))
34270b2eb33eSVille Syrjälä 		hotplug |= PORTA_HOTPLUG_ENABLE;
34287fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
34296dbf30ceSVille Syrjälä }
343026951cafSXiong Zhang 
343191d14251STvrtko Ursulin static void spt_hpd_irq_setup(struct drm_i915_private *dev_priv)
34326dbf30ceSVille Syrjälä {
34336dbf30ceSVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
34346dbf30ceSVille Syrjälä 
34356dbf30ceSVille Syrjälä 	hotplug_irqs = SDE_HOTPLUG_MASK_SPT;
343691d14251STvrtko Ursulin 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_spt);
34376dbf30ceSVille Syrjälä 
34386dbf30ceSVille Syrjälä 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
34396dbf30ceSVille Syrjälä 
34406dbf30ceSVille Syrjälä 	/* Enable digital hotplug on the PCH */
34416dbf30ceSVille Syrjälä 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
34426dbf30ceSVille Syrjälä 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTC_HOTPLUG_ENABLE |
344374c0b395SVille Syrjälä 		PORTB_HOTPLUG_ENABLE | PORTA_HOTPLUG_ENABLE;
34446dbf30ceSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
34456dbf30ceSVille Syrjälä 
344626951cafSXiong Zhang 	hotplug = I915_READ(PCH_PORT_HOTPLUG2);
344726951cafSXiong Zhang 	hotplug |= PORTE_HOTPLUG_ENABLE;
344826951cafSXiong Zhang 	I915_WRITE(PCH_PORT_HOTPLUG2, hotplug);
344926951cafSXiong Zhang }
34507fe0b973SKeith Packard 
345191d14251STvrtko Ursulin static void ilk_hpd_irq_setup(struct drm_i915_private *dev_priv)
3452e4ce95aaSVille Syrjälä {
3453e4ce95aaSVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
3454e4ce95aaSVille Syrjälä 
345591d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 8) {
34563a3b3c7dSVille Syrjälä 		hotplug_irqs = GEN8_PORT_DP_A_HOTPLUG;
345791d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_bdw);
34583a3b3c7dSVille Syrjälä 
34593a3b3c7dSVille Syrjälä 		bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
346091d14251STvrtko Ursulin 	} else if (INTEL_GEN(dev_priv) >= 7) {
346123bb4cb5SVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG_IVB;
346291d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ivb);
34633a3b3c7dSVille Syrjälä 
34643a3b3c7dSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
346523bb4cb5SVille Syrjälä 	} else {
3466e4ce95aaSVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG;
346791d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ilk);
3468e4ce95aaSVille Syrjälä 
3469e4ce95aaSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
34703a3b3c7dSVille Syrjälä 	}
3471e4ce95aaSVille Syrjälä 
3472e4ce95aaSVille Syrjälä 	/*
3473e4ce95aaSVille Syrjälä 	 * Enable digital hotplug on the CPU, and configure the DP short pulse
3474e4ce95aaSVille Syrjälä 	 * duration to 2ms (which is the minimum in the Display Port spec)
347523bb4cb5SVille Syrjälä 	 * The pulse duration bits are reserved on HSW+.
3476e4ce95aaSVille Syrjälä 	 */
3477e4ce95aaSVille Syrjälä 	hotplug = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
3478e4ce95aaSVille Syrjälä 	hotplug &= ~DIGITAL_PORTA_PULSE_DURATION_MASK;
3479e4ce95aaSVille Syrjälä 	hotplug |= DIGITAL_PORTA_HOTPLUG_ENABLE | DIGITAL_PORTA_PULSE_DURATION_2ms;
3480e4ce95aaSVille Syrjälä 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, hotplug);
3481e4ce95aaSVille Syrjälä 
348291d14251STvrtko Ursulin 	ibx_hpd_irq_setup(dev_priv);
3483e4ce95aaSVille Syrjälä }
3484e4ce95aaSVille Syrjälä 
348591d14251STvrtko Ursulin static void bxt_hpd_irq_setup(struct drm_i915_private *dev_priv)
3486e0a20ad7SShashank Sharma {
3487a52bb15bSVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
3488e0a20ad7SShashank Sharma 
348991d14251STvrtko Ursulin 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_bxt);
3490a52bb15bSVille Syrjälä 	hotplug_irqs = BXT_DE_PORT_HOTPLUG_MASK;
3491e0a20ad7SShashank Sharma 
3492a52bb15bSVille Syrjälä 	bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
3493e0a20ad7SShashank Sharma 
3494a52bb15bSVille Syrjälä 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
3495a52bb15bSVille Syrjälä 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTB_HOTPLUG_ENABLE |
3496a52bb15bSVille Syrjälä 		PORTA_HOTPLUG_ENABLE;
3497d252bf68SShubhangi Shrivastava 
3498d252bf68SShubhangi Shrivastava 	DRM_DEBUG_KMS("Invert bit setting: hp_ctl:%x hp_port:%x\n",
3499d252bf68SShubhangi Shrivastava 		      hotplug, enabled_irqs);
3500d252bf68SShubhangi Shrivastava 	hotplug &= ~BXT_DDI_HPD_INVERT_MASK;
3501d252bf68SShubhangi Shrivastava 
3502d252bf68SShubhangi Shrivastava 	/*
3503d252bf68SShubhangi Shrivastava 	 * For BXT invert bit has to be set based on AOB design
3504d252bf68SShubhangi Shrivastava 	 * for HPD detection logic, update it based on VBT fields.
3505d252bf68SShubhangi Shrivastava 	 */
3506d252bf68SShubhangi Shrivastava 
3507d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIA) &&
3508d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_A))
3509d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIA_HPD_INVERT;
3510d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIB) &&
3511d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_B))
3512d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIB_HPD_INVERT;
3513d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIC) &&
3514d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_C))
3515d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIC_HPD_INVERT;
3516d252bf68SShubhangi Shrivastava 
3517a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
3518e0a20ad7SShashank Sharma }
3519e0a20ad7SShashank Sharma 
3520d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
3521d46da437SPaulo Zanoni {
3522fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
352382a28bcfSDaniel Vetter 	u32 mask;
3524d46da437SPaulo Zanoni 
3525692a04cfSDaniel Vetter 	if (HAS_PCH_NOP(dev))
3526692a04cfSDaniel Vetter 		return;
3527692a04cfSDaniel Vetter 
3528105b122eSPaulo Zanoni 	if (HAS_PCH_IBX(dev))
35295c673b60SDaniel Vetter 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
3530105b122eSPaulo Zanoni 	else
35315c673b60SDaniel Vetter 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
35328664281bSPaulo Zanoni 
3533b51a2842SVille Syrjälä 	gen5_assert_iir_is_zero(dev_priv, SDEIIR);
3534d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
3535d46da437SPaulo Zanoni }
3536d46da437SPaulo Zanoni 
35370a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
35380a9a8c91SDaniel Vetter {
3539fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
35400a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
35410a9a8c91SDaniel Vetter 
35420a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
35430a9a8c91SDaniel Vetter 
35440a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
3545040d2baaSBen Widawsky 	if (HAS_L3_DPF(dev)) {
35460a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
354735a85ac6SBen Widawsky 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev);
354835a85ac6SBen Widawsky 		gt_irqs |= GT_PARITY_ERROR(dev);
35490a9a8c91SDaniel Vetter 	}
35500a9a8c91SDaniel Vetter 
35510a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
35520a9a8c91SDaniel Vetter 	if (IS_GEN5(dev)) {
3553f8973c21SChris Wilson 		gt_irqs |= ILK_BSD_USER_INTERRUPT;
35540a9a8c91SDaniel Vetter 	} else {
35550a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
35560a9a8c91SDaniel Vetter 	}
35570a9a8c91SDaniel Vetter 
355835079899SPaulo Zanoni 	GEN5_IRQ_INIT(GT, dev_priv->gt_irq_mask, gt_irqs);
35590a9a8c91SDaniel Vetter 
35600a9a8c91SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
356178e68d36SImre Deak 		/*
356278e68d36SImre Deak 		 * RPS interrupts will get enabled/disabled on demand when RPS
356378e68d36SImre Deak 		 * itself is enabled/disabled.
356478e68d36SImre Deak 		 */
35650a9a8c91SDaniel Vetter 		if (HAS_VEBOX(dev))
35660a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
35670a9a8c91SDaniel Vetter 
3568605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = 0xffffffff;
356935079899SPaulo Zanoni 		GEN5_IRQ_INIT(GEN6_PM, dev_priv->pm_irq_mask, pm_irqs);
35700a9a8c91SDaniel Vetter 	}
35710a9a8c91SDaniel Vetter }
35720a9a8c91SDaniel Vetter 
3573f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
3574036a4a7dSZhenyu Wang {
3575fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
35768e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
35778e76f8dcSPaulo Zanoni 
35788e76f8dcSPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 7) {
35798e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
35808e76f8dcSPaulo Zanoni 				DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
35818e76f8dcSPaulo Zanoni 				DE_PLANEB_FLIP_DONE_IVB |
35825c673b60SDaniel Vetter 				DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB);
35838e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
358423bb4cb5SVille Syrjälä 			      DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB |
358523bb4cb5SVille Syrjälä 			      DE_DP_A_HOTPLUG_IVB);
35868e76f8dcSPaulo Zanoni 	} else {
35878e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3588ce99c256SDaniel Vetter 				DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
35895b3a856bSDaniel Vetter 				DE_AUX_CHANNEL_A |
35905b3a856bSDaniel Vetter 				DE_PIPEB_CRC_DONE | DE_PIPEA_CRC_DONE |
35915b3a856bSDaniel Vetter 				DE_POISON);
3592e4ce95aaSVille Syrjälä 		extra_mask = (DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
3593e4ce95aaSVille Syrjälä 			      DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN |
3594e4ce95aaSVille Syrjälä 			      DE_DP_A_HOTPLUG);
35958e76f8dcSPaulo Zanoni 	}
3596036a4a7dSZhenyu Wang 
35971ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3598036a4a7dSZhenyu Wang 
35990c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xeffe);
36000c841212SPaulo Zanoni 
3601622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3602622364b6SPaulo Zanoni 
360335079899SPaulo Zanoni 	GEN5_IRQ_INIT(DE, dev_priv->irq_mask, display_mask | extra_mask);
3604036a4a7dSZhenyu Wang 
36050a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
3606036a4a7dSZhenyu Wang 
3607d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
36087fe0b973SKeith Packard 
3609f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
36106005ce42SDaniel Vetter 		/* Enable PCU event interrupts
36116005ce42SDaniel Vetter 		 *
36126005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
36134bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
36144bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
3615d6207435SDaniel Vetter 		spin_lock_irq(&dev_priv->irq_lock);
3616fbdedaeaSVille Syrjälä 		ilk_enable_display_irq(dev_priv, DE_PCU_EVENT);
3617d6207435SDaniel Vetter 		spin_unlock_irq(&dev_priv->irq_lock);
3618f97108d1SJesse Barnes 	}
3619f97108d1SJesse Barnes 
3620036a4a7dSZhenyu Wang 	return 0;
3621036a4a7dSZhenyu Wang }
3622036a4a7dSZhenyu Wang 
3623f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3624f8b79e58SImre Deak {
3625f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3626f8b79e58SImre Deak 
3627f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3628f8b79e58SImre Deak 		return;
3629f8b79e58SImre Deak 
3630f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3631f8b79e58SImre Deak 
3632d6c69803SVille Syrjälä 	if (intel_irqs_enabled(dev_priv)) {
3633d6c69803SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3634ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3635f8b79e58SImre Deak 	}
3636d6c69803SVille Syrjälä }
3637f8b79e58SImre Deak 
3638f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3639f8b79e58SImre Deak {
3640f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3641f8b79e58SImre Deak 
3642f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3643f8b79e58SImre Deak 		return;
3644f8b79e58SImre Deak 
3645f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3646f8b79e58SImre Deak 
3647950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3648ad22d106SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3649f8b79e58SImre Deak }
3650f8b79e58SImre Deak 
36510e6c9a9eSVille Syrjälä 
36520e6c9a9eSVille Syrjälä static int valleyview_irq_postinstall(struct drm_device *dev)
36530e6c9a9eSVille Syrjälä {
3654fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
36550e6c9a9eSVille Syrjälä 
36560a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
36577e231dbeSJesse Barnes 
3658ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
36599918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3660ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3661ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3662ad22d106SVille Syrjälä 
36637e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
366434c7b8a7SVille Syrjälä 	POSTING_READ(VLV_MASTER_IER);
366520afbda2SDaniel Vetter 
366620afbda2SDaniel Vetter 	return 0;
366720afbda2SDaniel Vetter }
366820afbda2SDaniel Vetter 
3669abd58f01SBen Widawsky static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
3670abd58f01SBen Widawsky {
3671abd58f01SBen Widawsky 	/* These are interrupts we'll toggle with the ring mask register */
3672abd58f01SBen Widawsky 	uint32_t gt_interrupts[] = {
3673abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
367473d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
367573d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT |
367673d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT,
3677abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
367873d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
367973d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT |
368073d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT,
3681abd58f01SBen Widawsky 		0,
368273d477f6SOscar Mateo 		GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT |
368373d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT
3684abd58f01SBen Widawsky 		};
3685abd58f01SBen Widawsky 
368698735739STvrtko Ursulin 	if (HAS_L3_DPF(dev_priv))
368798735739STvrtko Ursulin 		gt_interrupts[0] |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
368898735739STvrtko Ursulin 
36890961021aSBen Widawsky 	dev_priv->pm_irq_mask = 0xffffffff;
36909a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 0, ~gt_interrupts[0], gt_interrupts[0]);
36919a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 1, ~gt_interrupts[1], gt_interrupts[1]);
369278e68d36SImre Deak 	/*
369378e68d36SImre Deak 	 * RPS interrupts will get enabled/disabled on demand when RPS itself
369478e68d36SImre Deak 	 * is enabled/disabled.
369578e68d36SImre Deak 	 */
369678e68d36SImre Deak 	GEN8_IRQ_INIT_NDX(GT, 2, dev_priv->pm_irq_mask, 0);
36979a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 3, ~gt_interrupts[3], gt_interrupts[3]);
3698abd58f01SBen Widawsky }
3699abd58f01SBen Widawsky 
3700abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3701abd58f01SBen Widawsky {
3702770de83dSDamien Lespiau 	uint32_t de_pipe_masked = GEN8_PIPE_CDCLK_CRC_DONE;
3703770de83dSDamien Lespiau 	uint32_t de_pipe_enables;
37043a3b3c7dSVille Syrjälä 	u32 de_port_masked = GEN8_AUX_CHANNEL_A;
37053a3b3c7dSVille Syrjälä 	u32 de_port_enables;
370611825b0dSVille Syrjälä 	u32 de_misc_masked = GEN8_DE_MISC_GSE;
37073a3b3c7dSVille Syrjälä 	enum pipe pipe;
3708770de83dSDamien Lespiau 
3709b4834a50SRodrigo Vivi 	if (INTEL_INFO(dev_priv)->gen >= 9) {
3710770de83dSDamien Lespiau 		de_pipe_masked |= GEN9_PIPE_PLANE1_FLIP_DONE |
3711770de83dSDamien Lespiau 				  GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
37123a3b3c7dSVille Syrjälä 		de_port_masked |= GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
371388e04703SJesse Barnes 				  GEN9_AUX_CHANNEL_D;
37149e63743eSShashank Sharma 		if (IS_BROXTON(dev_priv))
37153a3b3c7dSVille Syrjälä 			de_port_masked |= BXT_DE_PORT_GMBUS;
37163a3b3c7dSVille Syrjälä 	} else {
3717770de83dSDamien Lespiau 		de_pipe_masked |= GEN8_PIPE_PRIMARY_FLIP_DONE |
3718770de83dSDamien Lespiau 				  GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
37193a3b3c7dSVille Syrjälä 	}
3720770de83dSDamien Lespiau 
3721770de83dSDamien Lespiau 	de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
3722770de83dSDamien Lespiau 					   GEN8_PIPE_FIFO_UNDERRUN;
3723770de83dSDamien Lespiau 
37243a3b3c7dSVille Syrjälä 	de_port_enables = de_port_masked;
3725a52bb15bSVille Syrjälä 	if (IS_BROXTON(dev_priv))
3726a52bb15bSVille Syrjälä 		de_port_enables |= BXT_DE_PORT_HOTPLUG_MASK;
3727a52bb15bSVille Syrjälä 	else if (IS_BROADWELL(dev_priv))
37283a3b3c7dSVille Syrjälä 		de_port_enables |= GEN8_PORT_DP_A_HOTPLUG;
37293a3b3c7dSVille Syrjälä 
373013b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_A] = ~de_pipe_masked;
373113b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_B] = ~de_pipe_masked;
373213b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_C] = ~de_pipe_masked;
3733abd58f01SBen Widawsky 
3734055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3735f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3736813bde43SPaulo Zanoni 				POWER_DOMAIN_PIPE(pipe)))
3737813bde43SPaulo Zanoni 			GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
3738813bde43SPaulo Zanoni 					  dev_priv->de_irq_mask[pipe],
373935079899SPaulo Zanoni 					  de_pipe_enables);
3740abd58f01SBen Widawsky 
37413a3b3c7dSVille Syrjälä 	GEN5_IRQ_INIT(GEN8_DE_PORT_, ~de_port_masked, de_port_enables);
374211825b0dSVille Syrjälä 	GEN5_IRQ_INIT(GEN8_DE_MISC_, ~de_misc_masked, de_misc_masked);
3743abd58f01SBen Widawsky }
3744abd58f01SBen Widawsky 
3745abd58f01SBen Widawsky static int gen8_irq_postinstall(struct drm_device *dev)
3746abd58f01SBen Widawsky {
3747fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3748abd58f01SBen Widawsky 
3749266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev))
3750622364b6SPaulo Zanoni 		ibx_irq_pre_postinstall(dev);
3751622364b6SPaulo Zanoni 
3752abd58f01SBen Widawsky 	gen8_gt_irq_postinstall(dev_priv);
3753abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3754abd58f01SBen Widawsky 
3755266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev))
3756abd58f01SBen Widawsky 		ibx_irq_postinstall(dev);
3757abd58f01SBen Widawsky 
3758e5328c43SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
3759abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3760abd58f01SBen Widawsky 
3761abd58f01SBen Widawsky 	return 0;
3762abd58f01SBen Widawsky }
3763abd58f01SBen Widawsky 
376443f328d7SVille Syrjälä static int cherryview_irq_postinstall(struct drm_device *dev)
376543f328d7SVille Syrjälä {
3766fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
376743f328d7SVille Syrjälä 
376843f328d7SVille Syrjälä 	gen8_gt_irq_postinstall(dev_priv);
376943f328d7SVille Syrjälä 
3770ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
37719918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3772ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3773ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3774ad22d106SVille Syrjälä 
3775e5328c43SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
377643f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
377743f328d7SVille Syrjälä 
377843f328d7SVille Syrjälä 	return 0;
377943f328d7SVille Syrjälä }
378043f328d7SVille Syrjälä 
3781abd58f01SBen Widawsky static void gen8_irq_uninstall(struct drm_device *dev)
3782abd58f01SBen Widawsky {
3783fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3784abd58f01SBen Widawsky 
3785abd58f01SBen Widawsky 	if (!dev_priv)
3786abd58f01SBen Widawsky 		return;
3787abd58f01SBen Widawsky 
3788823f6b38SPaulo Zanoni 	gen8_irq_reset(dev);
3789abd58f01SBen Widawsky }
3790abd58f01SBen Widawsky 
37917e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
37927e231dbeSJesse Barnes {
3793fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
37947e231dbeSJesse Barnes 
37957e231dbeSJesse Barnes 	if (!dev_priv)
37967e231dbeSJesse Barnes 		return;
37977e231dbeSJesse Barnes 
3798843d0e7dSImre Deak 	I915_WRITE(VLV_MASTER_IER, 0);
379934c7b8a7SVille Syrjälä 	POSTING_READ(VLV_MASTER_IER);
3800843d0e7dSImre Deak 
3801893fce8eSVille Syrjälä 	gen5_gt_irq_reset(dev);
3802893fce8eSVille Syrjälä 
38037e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
3804f8b79e58SImre Deak 
3805ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
38069918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3807ad22d106SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3808ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
38097e231dbeSJesse Barnes }
38107e231dbeSJesse Barnes 
381143f328d7SVille Syrjälä static void cherryview_irq_uninstall(struct drm_device *dev)
381243f328d7SVille Syrjälä {
3813fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
381443f328d7SVille Syrjälä 
381543f328d7SVille Syrjälä 	if (!dev_priv)
381643f328d7SVille Syrjälä 		return;
381743f328d7SVille Syrjälä 
381843f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
381943f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
382043f328d7SVille Syrjälä 
3821a2c30fbaSVille Syrjälä 	gen8_gt_irq_reset(dev_priv);
382243f328d7SVille Syrjälä 
3823a2c30fbaSVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
382443f328d7SVille Syrjälä 
3825ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
38269918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3827ad22d106SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3828ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
382943f328d7SVille Syrjälä }
383043f328d7SVille Syrjälä 
3831f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
3832036a4a7dSZhenyu Wang {
3833fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
38344697995bSJesse Barnes 
38354697995bSJesse Barnes 	if (!dev_priv)
38364697995bSJesse Barnes 		return;
38374697995bSJesse Barnes 
3838be30b29fSPaulo Zanoni 	ironlake_irq_reset(dev);
3839036a4a7dSZhenyu Wang }
3840036a4a7dSZhenyu Wang 
3841c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
3842c2798b19SChris Wilson {
3843fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3844c2798b19SChris Wilson 	int pipe;
3845c2798b19SChris Wilson 
3846055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3847c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3848c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3849c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3850c2798b19SChris Wilson 	POSTING_READ16(IER);
3851c2798b19SChris Wilson }
3852c2798b19SChris Wilson 
3853c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
3854c2798b19SChris Wilson {
3855fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3856c2798b19SChris Wilson 
3857c2798b19SChris Wilson 	I915_WRITE16(EMR,
3858c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3859c2798b19SChris Wilson 
3860c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
3861c2798b19SChris Wilson 	dev_priv->irq_mask =
3862c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3863c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3864c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
386537ef01abSDaniel Vetter 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
3866c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
3867c2798b19SChris Wilson 
3868c2798b19SChris Wilson 	I915_WRITE16(IER,
3869c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3870c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3871c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
3872c2798b19SChris Wilson 	POSTING_READ16(IER);
3873c2798b19SChris Wilson 
3874379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3875379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3876d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3877755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3878755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3879d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3880379ef82dSDaniel Vetter 
3881c2798b19SChris Wilson 	return 0;
3882c2798b19SChris Wilson }
3883c2798b19SChris Wilson 
38845a21b665SDaniel Vetter /*
38855a21b665SDaniel Vetter  * Returns true when a page flip has completed.
38865a21b665SDaniel Vetter  */
38875a21b665SDaniel Vetter static bool i8xx_handle_vblank(struct drm_i915_private *dev_priv,
38885a21b665SDaniel Vetter 			       int plane, int pipe, u32 iir)
38895a21b665SDaniel Vetter {
38905a21b665SDaniel Vetter 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
38915a21b665SDaniel Vetter 
38925a21b665SDaniel Vetter 	if (!intel_pipe_handle_vblank(dev_priv, pipe))
38935a21b665SDaniel Vetter 		return false;
38945a21b665SDaniel Vetter 
38955a21b665SDaniel Vetter 	if ((iir & flip_pending) == 0)
38965a21b665SDaniel Vetter 		goto check_page_flip;
38975a21b665SDaniel Vetter 
38985a21b665SDaniel Vetter 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
38995a21b665SDaniel Vetter 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
39005a21b665SDaniel Vetter 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
39015a21b665SDaniel Vetter 	 * the flip is completed (no longer pending). Since this doesn't raise
39025a21b665SDaniel Vetter 	 * an interrupt per se, we watch for the change at vblank.
39035a21b665SDaniel Vetter 	 */
39045a21b665SDaniel Vetter 	if (I915_READ16(ISR) & flip_pending)
39055a21b665SDaniel Vetter 		goto check_page_flip;
39065a21b665SDaniel Vetter 
39075a21b665SDaniel Vetter 	intel_finish_page_flip_cs(dev_priv, pipe);
39085a21b665SDaniel Vetter 	return true;
39095a21b665SDaniel Vetter 
39105a21b665SDaniel Vetter check_page_flip:
39115a21b665SDaniel Vetter 	intel_check_page_flip(dev_priv, pipe);
39125a21b665SDaniel Vetter 	return false;
39135a21b665SDaniel Vetter }
39145a21b665SDaniel Vetter 
3915ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3916c2798b19SChris Wilson {
391745a83f84SDaniel Vetter 	struct drm_device *dev = arg;
3918fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3919c2798b19SChris Wilson 	u16 iir, new_iir;
3920c2798b19SChris Wilson 	u32 pipe_stats[2];
3921c2798b19SChris Wilson 	int pipe;
3922c2798b19SChris Wilson 	u16 flip_mask =
3923c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3924c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
39251f814dacSImre Deak 	irqreturn_t ret;
3926c2798b19SChris Wilson 
39272dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
39282dd2a883SImre Deak 		return IRQ_NONE;
39292dd2a883SImre Deak 
39301f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
39311f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
39321f814dacSImre Deak 
39331f814dacSImre Deak 	ret = IRQ_NONE;
3934c2798b19SChris Wilson 	iir = I915_READ16(IIR);
3935c2798b19SChris Wilson 	if (iir == 0)
39361f814dacSImre Deak 		goto out;
3937c2798b19SChris Wilson 
3938c2798b19SChris Wilson 	while (iir & ~flip_mask) {
3939c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3940c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3941c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3942c2798b19SChris Wilson 		 * interrupts (for non-MSI).
3943c2798b19SChris Wilson 		 */
3944222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
3945c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3946aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
3947c2798b19SChris Wilson 
3948055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
3949f0f59a00SVille Syrjälä 			i915_reg_t reg = PIPESTAT(pipe);
3950c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3951c2798b19SChris Wilson 
3952c2798b19SChris Wilson 			/*
3953c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3954c2798b19SChris Wilson 			 */
39552d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & 0x8000ffff)
3956c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3957c2798b19SChris Wilson 		}
3958222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
3959c2798b19SChris Wilson 
3960c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
3961c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
3962c2798b19SChris Wilson 
3963c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
39644a570db5STvrtko Ursulin 			notify_ring(&dev_priv->engine[RCS]);
3965c2798b19SChris Wilson 
3966055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
39675a21b665SDaniel Vetter 			int plane = pipe;
39685a21b665SDaniel Vetter 			if (HAS_FBC(dev_priv))
39695a21b665SDaniel Vetter 				plane = !plane;
39705a21b665SDaniel Vetter 
39715a21b665SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
39725a21b665SDaniel Vetter 			    i8xx_handle_vblank(dev_priv, plane, pipe, iir))
39735a21b665SDaniel Vetter 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3974c2798b19SChris Wilson 
39754356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
397691d14251STvrtko Ursulin 				i9xx_pipe_crc_irq_handler(dev_priv, pipe);
39772d9d2b0bSVille Syrjälä 
39781f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
39791f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
39801f7247c0SDaniel Vetter 								    pipe);
39814356d586SDaniel Vetter 		}
3982c2798b19SChris Wilson 
3983c2798b19SChris Wilson 		iir = new_iir;
3984c2798b19SChris Wilson 	}
39851f814dacSImre Deak 	ret = IRQ_HANDLED;
3986c2798b19SChris Wilson 
39871f814dacSImre Deak out:
39881f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
39891f814dacSImre Deak 
39901f814dacSImre Deak 	return ret;
3991c2798b19SChris Wilson }
3992c2798b19SChris Wilson 
3993c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
3994c2798b19SChris Wilson {
3995fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3996c2798b19SChris Wilson 	int pipe;
3997c2798b19SChris Wilson 
3998055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
3999c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
4000c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4001c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
4002c2798b19SChris Wilson 	}
4003c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
4004c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
4005c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
4006c2798b19SChris Wilson }
4007c2798b19SChris Wilson 
4008a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
4009a266c7d5SChris Wilson {
4010fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4011a266c7d5SChris Wilson 	int pipe;
4012a266c7d5SChris Wilson 
4013a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
40140706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4015a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4016a266c7d5SChris Wilson 	}
4017a266c7d5SChris Wilson 
401800d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
4019055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4020a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4021a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4022a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4023a266c7d5SChris Wilson 	POSTING_READ(IER);
4024a266c7d5SChris Wilson }
4025a266c7d5SChris Wilson 
4026a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
4027a266c7d5SChris Wilson {
4028fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
402938bde180SChris Wilson 	u32 enable_mask;
4030a266c7d5SChris Wilson 
403138bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
403238bde180SChris Wilson 
403338bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
403438bde180SChris Wilson 	dev_priv->irq_mask =
403538bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
403638bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
403738bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
403838bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
403937ef01abSDaniel Vetter 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
404038bde180SChris Wilson 
404138bde180SChris Wilson 	enable_mask =
404238bde180SChris Wilson 		I915_ASLE_INTERRUPT |
404338bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
404438bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
404538bde180SChris Wilson 		I915_USER_INTERRUPT;
404638bde180SChris Wilson 
4047a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
40480706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
404920afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
405020afbda2SDaniel Vetter 
4051a266c7d5SChris Wilson 		/* Enable in IER... */
4052a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
4053a266c7d5SChris Wilson 		/* and unmask in IMR */
4054a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
4055a266c7d5SChris Wilson 	}
4056a266c7d5SChris Wilson 
4057a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
4058a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
4059a266c7d5SChris Wilson 	POSTING_READ(IER);
4060a266c7d5SChris Wilson 
406191d14251STvrtko Ursulin 	i915_enable_asle_pipestat(dev_priv);
406220afbda2SDaniel Vetter 
4063379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4064379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4065d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4066755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4067755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4068d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4069379ef82dSDaniel Vetter 
407020afbda2SDaniel Vetter 	return 0;
407120afbda2SDaniel Vetter }
407220afbda2SDaniel Vetter 
40735a21b665SDaniel Vetter /*
40745a21b665SDaniel Vetter  * Returns true when a page flip has completed.
40755a21b665SDaniel Vetter  */
40765a21b665SDaniel Vetter static bool i915_handle_vblank(struct drm_i915_private *dev_priv,
40775a21b665SDaniel Vetter 			       int plane, int pipe, u32 iir)
40785a21b665SDaniel Vetter {
40795a21b665SDaniel Vetter 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
40805a21b665SDaniel Vetter 
40815a21b665SDaniel Vetter 	if (!intel_pipe_handle_vblank(dev_priv, pipe))
40825a21b665SDaniel Vetter 		return false;
40835a21b665SDaniel Vetter 
40845a21b665SDaniel Vetter 	if ((iir & flip_pending) == 0)
40855a21b665SDaniel Vetter 		goto check_page_flip;
40865a21b665SDaniel Vetter 
40875a21b665SDaniel Vetter 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
40885a21b665SDaniel Vetter 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
40895a21b665SDaniel Vetter 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
40905a21b665SDaniel Vetter 	 * the flip is completed (no longer pending). Since this doesn't raise
40915a21b665SDaniel Vetter 	 * an interrupt per se, we watch for the change at vblank.
40925a21b665SDaniel Vetter 	 */
40935a21b665SDaniel Vetter 	if (I915_READ(ISR) & flip_pending)
40945a21b665SDaniel Vetter 		goto check_page_flip;
40955a21b665SDaniel Vetter 
40965a21b665SDaniel Vetter 	intel_finish_page_flip_cs(dev_priv, pipe);
40975a21b665SDaniel Vetter 	return true;
40985a21b665SDaniel Vetter 
40995a21b665SDaniel Vetter check_page_flip:
41005a21b665SDaniel Vetter 	intel_check_page_flip(dev_priv, pipe);
41015a21b665SDaniel Vetter 	return false;
41025a21b665SDaniel Vetter }
41035a21b665SDaniel Vetter 
4104ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
4105a266c7d5SChris Wilson {
410645a83f84SDaniel Vetter 	struct drm_device *dev = arg;
4107fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
41088291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
410938bde180SChris Wilson 	u32 flip_mask =
411038bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
411138bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
411238bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
4113a266c7d5SChris Wilson 
41142dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
41152dd2a883SImre Deak 		return IRQ_NONE;
41162dd2a883SImre Deak 
41171f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
41181f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
41191f814dacSImre Deak 
4120a266c7d5SChris Wilson 	iir = I915_READ(IIR);
412138bde180SChris Wilson 	do {
412238bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
41238291ee90SChris Wilson 		bool blc_event = false;
4124a266c7d5SChris Wilson 
4125a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4126a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4127a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4128a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
4129a266c7d5SChris Wilson 		 */
4130222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
4131a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4132aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4133a266c7d5SChris Wilson 
4134055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4135f0f59a00SVille Syrjälä 			i915_reg_t reg = PIPESTAT(pipe);
4136a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4137a266c7d5SChris Wilson 
413838bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
4139a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4140a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
414138bde180SChris Wilson 				irq_received = true;
4142a266c7d5SChris Wilson 			}
4143a266c7d5SChris Wilson 		}
4144222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4145a266c7d5SChris Wilson 
4146a266c7d5SChris Wilson 		if (!irq_received)
4147a266c7d5SChris Wilson 			break;
4148a266c7d5SChris Wilson 
4149a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
415091d14251STvrtko Ursulin 		if (I915_HAS_HOTPLUG(dev_priv) &&
41511ae3c34cSVille Syrjälä 		    iir & I915_DISPLAY_PORT_INTERRUPT) {
41521ae3c34cSVille Syrjälä 			u32 hotplug_status = i9xx_hpd_irq_ack(dev_priv);
41531ae3c34cSVille Syrjälä 			if (hotplug_status)
415491d14251STvrtko Ursulin 				i9xx_hpd_irq_handler(dev_priv, hotplug_status);
41551ae3c34cSVille Syrjälä 		}
4156a266c7d5SChris Wilson 
415738bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
4158a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4159a266c7d5SChris Wilson 
4160a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
41614a570db5STvrtko Ursulin 			notify_ring(&dev_priv->engine[RCS]);
4162a266c7d5SChris Wilson 
4163055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
41645a21b665SDaniel Vetter 			int plane = pipe;
41655a21b665SDaniel Vetter 			if (HAS_FBC(dev_priv))
41665a21b665SDaniel Vetter 				plane = !plane;
41675a21b665SDaniel Vetter 
41685a21b665SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
41695a21b665SDaniel Vetter 			    i915_handle_vblank(dev_priv, plane, pipe, iir))
41705a21b665SDaniel Vetter 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
4171a266c7d5SChris Wilson 
4172a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4173a266c7d5SChris Wilson 				blc_event = true;
41744356d586SDaniel Vetter 
41754356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
417691d14251STvrtko Ursulin 				i9xx_pipe_crc_irq_handler(dev_priv, pipe);
41772d9d2b0bSVille Syrjälä 
41781f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
41791f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
41801f7247c0SDaniel Vetter 								    pipe);
4181a266c7d5SChris Wilson 		}
4182a266c7d5SChris Wilson 
4183a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
418491d14251STvrtko Ursulin 			intel_opregion_asle_intr(dev_priv);
4185a266c7d5SChris Wilson 
4186a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4187a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4188a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4189a266c7d5SChris Wilson 		 * we would never get another interrupt.
4190a266c7d5SChris Wilson 		 *
4191a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4192a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4193a266c7d5SChris Wilson 		 * another one.
4194a266c7d5SChris Wilson 		 *
4195a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4196a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4197a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4198a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4199a266c7d5SChris Wilson 		 * stray interrupts.
4200a266c7d5SChris Wilson 		 */
420138bde180SChris Wilson 		ret = IRQ_HANDLED;
4202a266c7d5SChris Wilson 		iir = new_iir;
420338bde180SChris Wilson 	} while (iir & ~flip_mask);
4204a266c7d5SChris Wilson 
42051f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
42061f814dacSImre Deak 
4207a266c7d5SChris Wilson 	return ret;
4208a266c7d5SChris Wilson }
4209a266c7d5SChris Wilson 
4210a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
4211a266c7d5SChris Wilson {
4212fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4213a266c7d5SChris Wilson 	int pipe;
4214a266c7d5SChris Wilson 
4215a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
42160706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4217a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4218a266c7d5SChris Wilson 	}
4219a266c7d5SChris Wilson 
422000d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
4221055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
422255b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
4223a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
422455b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
422555b39755SChris Wilson 	}
4226a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4227a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4228a266c7d5SChris Wilson 
4229a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4230a266c7d5SChris Wilson }
4231a266c7d5SChris Wilson 
4232a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
4233a266c7d5SChris Wilson {
4234fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4235a266c7d5SChris Wilson 	int pipe;
4236a266c7d5SChris Wilson 
42370706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4238a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4239a266c7d5SChris Wilson 
4240a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
4241055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4242a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4243a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4244a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4245a266c7d5SChris Wilson 	POSTING_READ(IER);
4246a266c7d5SChris Wilson }
4247a266c7d5SChris Wilson 
4248a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
4249a266c7d5SChris Wilson {
4250fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4251bbba0a97SChris Wilson 	u32 enable_mask;
4252a266c7d5SChris Wilson 	u32 error_mask;
4253a266c7d5SChris Wilson 
4254a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
4255bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
4256adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
4257bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4258bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
4259bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
4260bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
4261bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
4262bbba0a97SChris Wilson 
4263bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
426421ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
426521ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
4266bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
4267bbba0a97SChris Wilson 
426891d14251STvrtko Ursulin 	if (IS_G4X(dev_priv))
4269bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
4270a266c7d5SChris Wilson 
4271b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4272b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4273d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4274755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
4275755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4276755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4277d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4278a266c7d5SChris Wilson 
4279a266c7d5SChris Wilson 	/*
4280a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
4281a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
4282a266c7d5SChris Wilson 	 */
428391d14251STvrtko Ursulin 	if (IS_G4X(dev_priv)) {
4284a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
4285a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
4286a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
4287a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4288a266c7d5SChris Wilson 	} else {
4289a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
4290a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4291a266c7d5SChris Wilson 	}
4292a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
4293a266c7d5SChris Wilson 
4294a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
4295a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
4296a266c7d5SChris Wilson 	POSTING_READ(IER);
4297a266c7d5SChris Wilson 
42980706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
429920afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
430020afbda2SDaniel Vetter 
430191d14251STvrtko Ursulin 	i915_enable_asle_pipestat(dev_priv);
430220afbda2SDaniel Vetter 
430320afbda2SDaniel Vetter 	return 0;
430420afbda2SDaniel Vetter }
430520afbda2SDaniel Vetter 
430691d14251STvrtko Ursulin static void i915_hpd_irq_setup(struct drm_i915_private *dev_priv)
430720afbda2SDaniel Vetter {
430820afbda2SDaniel Vetter 	u32 hotplug_en;
430920afbda2SDaniel Vetter 
4310b5ea2d56SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
4311b5ea2d56SDaniel Vetter 
4312adca4730SChris Wilson 	/* Note HDMI and DP share hotplug bits */
4313e5868a31SEgbert Eich 	/* enable bits are the same for all generations */
431491d14251STvrtko Ursulin 	hotplug_en = intel_hpd_enabled_irqs(dev_priv, hpd_mask_i915);
4315a266c7d5SChris Wilson 	/* Programming the CRT detection parameters tends
4316a266c7d5SChris Wilson 	   to generate a spurious hotplug event about three
4317a266c7d5SChris Wilson 	   seconds later.  So just do it once.
4318a266c7d5SChris Wilson 	*/
431991d14251STvrtko Ursulin 	if (IS_G4X(dev_priv))
4320a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
4321a266c7d5SChris Wilson 	hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
4322a266c7d5SChris Wilson 
4323a266c7d5SChris Wilson 	/* Ignore TV since it's buggy */
43240706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv,
4325f9e3dc78SJani Nikula 					     HOTPLUG_INT_EN_MASK |
4326f9e3dc78SJani Nikula 					     CRT_HOTPLUG_VOLTAGE_COMPARE_MASK |
4327f9e3dc78SJani Nikula 					     CRT_HOTPLUG_ACTIVATION_PERIOD_64,
43280706f17cSEgbert Eich 					     hotplug_en);
4329a266c7d5SChris Wilson }
4330a266c7d5SChris Wilson 
4331ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
4332a266c7d5SChris Wilson {
433345a83f84SDaniel Vetter 	struct drm_device *dev = arg;
4334fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4335a266c7d5SChris Wilson 	u32 iir, new_iir;
4336a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
4337a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
433821ad8330SVille Syrjälä 	u32 flip_mask =
433921ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
434021ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
4341a266c7d5SChris Wilson 
43422dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
43432dd2a883SImre Deak 		return IRQ_NONE;
43442dd2a883SImre Deak 
43451f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
43461f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
43471f814dacSImre Deak 
4348a266c7d5SChris Wilson 	iir = I915_READ(IIR);
4349a266c7d5SChris Wilson 
4350a266c7d5SChris Wilson 	for (;;) {
4351501e01d7SVille Syrjälä 		bool irq_received = (iir & ~flip_mask) != 0;
43522c8ba29fSChris Wilson 		bool blc_event = false;
43532c8ba29fSChris Wilson 
4354a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4355a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4356a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4357a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
4358a266c7d5SChris Wilson 		 */
4359222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
4360a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4361aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4362a266c7d5SChris Wilson 
4363055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4364f0f59a00SVille Syrjälä 			i915_reg_t reg = PIPESTAT(pipe);
4365a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4366a266c7d5SChris Wilson 
4367a266c7d5SChris Wilson 			/*
4368a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
4369a266c7d5SChris Wilson 			 */
4370a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4371a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
4372501e01d7SVille Syrjälä 				irq_received = true;
4373a266c7d5SChris Wilson 			}
4374a266c7d5SChris Wilson 		}
4375222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4376a266c7d5SChris Wilson 
4377a266c7d5SChris Wilson 		if (!irq_received)
4378a266c7d5SChris Wilson 			break;
4379a266c7d5SChris Wilson 
4380a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
4381a266c7d5SChris Wilson 
4382a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
43831ae3c34cSVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
43841ae3c34cSVille Syrjälä 			u32 hotplug_status = i9xx_hpd_irq_ack(dev_priv);
43851ae3c34cSVille Syrjälä 			if (hotplug_status)
438691d14251STvrtko Ursulin 				i9xx_hpd_irq_handler(dev_priv, hotplug_status);
43871ae3c34cSVille Syrjälä 		}
4388a266c7d5SChris Wilson 
438921ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
4390a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4391a266c7d5SChris Wilson 
4392a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
43934a570db5STvrtko Ursulin 			notify_ring(&dev_priv->engine[RCS]);
4394a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
43954a570db5STvrtko Ursulin 			notify_ring(&dev_priv->engine[VCS]);
4396a266c7d5SChris Wilson 
4397055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
43985a21b665SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
43995a21b665SDaniel Vetter 			    i915_handle_vblank(dev_priv, pipe, pipe, iir))
44005a21b665SDaniel Vetter 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
4401a266c7d5SChris Wilson 
4402a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4403a266c7d5SChris Wilson 				blc_event = true;
44044356d586SDaniel Vetter 
44054356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
440691d14251STvrtko Ursulin 				i9xx_pipe_crc_irq_handler(dev_priv, pipe);
4407a266c7d5SChris Wilson 
44081f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
44091f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
44102d9d2b0bSVille Syrjälä 		}
4411a266c7d5SChris Wilson 
4412a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
441391d14251STvrtko Ursulin 			intel_opregion_asle_intr(dev_priv);
4414a266c7d5SChris Wilson 
4415515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
441691d14251STvrtko Ursulin 			gmbus_irq_handler(dev_priv);
4417515ac2bbSDaniel Vetter 
4418a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4419a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4420a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4421a266c7d5SChris Wilson 		 * we would never get another interrupt.
4422a266c7d5SChris Wilson 		 *
4423a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4424a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4425a266c7d5SChris Wilson 		 * another one.
4426a266c7d5SChris Wilson 		 *
4427a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4428a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4429a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4430a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4431a266c7d5SChris Wilson 		 * stray interrupts.
4432a266c7d5SChris Wilson 		 */
4433a266c7d5SChris Wilson 		iir = new_iir;
4434a266c7d5SChris Wilson 	}
4435a266c7d5SChris Wilson 
44361f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
44371f814dacSImre Deak 
4438a266c7d5SChris Wilson 	return ret;
4439a266c7d5SChris Wilson }
4440a266c7d5SChris Wilson 
4441a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
4442a266c7d5SChris Wilson {
4443fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4444a266c7d5SChris Wilson 	int pipe;
4445a266c7d5SChris Wilson 
4446a266c7d5SChris Wilson 	if (!dev_priv)
4447a266c7d5SChris Wilson 		return;
4448a266c7d5SChris Wilson 
44490706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4450a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4451a266c7d5SChris Wilson 
4452a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
4453055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4454a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4455a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4456a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4457a266c7d5SChris Wilson 
4458055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4459a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
4460a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
4461a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4462a266c7d5SChris Wilson }
4463a266c7d5SChris Wilson 
4464fca52a55SDaniel Vetter /**
4465fca52a55SDaniel Vetter  * intel_irq_init - initializes irq support
4466fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4467fca52a55SDaniel Vetter  *
4468fca52a55SDaniel Vetter  * This function initializes all the irq support including work items, timers
4469fca52a55SDaniel Vetter  * and all the vtables. It does not setup the interrupt itself though.
4470fca52a55SDaniel Vetter  */
4471b963291cSDaniel Vetter void intel_irq_init(struct drm_i915_private *dev_priv)
4472f71d4af4SJesse Barnes {
447391c8a326SChris Wilson 	struct drm_device *dev = &dev_priv->drm;
44748b2e326dSChris Wilson 
447577913b39SJani Nikula 	intel_hpd_init_work(dev_priv);
447677913b39SJani Nikula 
4477c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
4478a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
44798b2e326dSChris Wilson 
4480a6706b45SDeepak S 	/* Let's track the enabled rps events */
4481666a4537SWayne Boyer 	if (IS_VALLEYVIEW(dev_priv))
44826c65a587SVille Syrjälä 		/* WaGsvRC0ResidencyMethod:vlv */
44836f4b12f8SChris Wilson 		dev_priv->pm_rps_events = GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED;
448431685c25SDeepak S 	else
4485a6706b45SDeepak S 		dev_priv->pm_rps_events = GEN6_PM_RPS_EVENTS;
4486a6706b45SDeepak S 
44871800ad25SSagar Arun Kamble 	dev_priv->rps.pm_intr_keep = 0;
44881800ad25SSagar Arun Kamble 
44891800ad25SSagar Arun Kamble 	/*
44901800ad25SSagar Arun Kamble 	 * SNB,IVB can while VLV,CHV may hard hang on looping batchbuffer
44911800ad25SSagar Arun Kamble 	 * if GEN6_PM_UP_EI_EXPIRED is masked.
44921800ad25SSagar Arun Kamble 	 *
44931800ad25SSagar Arun Kamble 	 * TODO: verify if this can be reproduced on VLV,CHV.
44941800ad25SSagar Arun Kamble 	 */
44951800ad25SSagar Arun Kamble 	if (INTEL_INFO(dev_priv)->gen <= 7 && !IS_HASWELL(dev_priv))
44961800ad25SSagar Arun Kamble 		dev_priv->rps.pm_intr_keep |= GEN6_PM_RP_UP_EI_EXPIRED;
44971800ad25SSagar Arun Kamble 
44981800ad25SSagar Arun Kamble 	if (INTEL_INFO(dev_priv)->gen >= 8)
44991800ad25SSagar Arun Kamble 		dev_priv->rps.pm_intr_keep |= GEN8_PMINTR_REDIRECT_TO_NON_DISP;
45001800ad25SSagar Arun Kamble 
4501737b1506SChris Wilson 	INIT_DELAYED_WORK(&dev_priv->gpu_error.hangcheck_work,
4502737b1506SChris Wilson 			  i915_hangcheck_elapsed);
450361bac78eSDaniel Vetter 
4504b963291cSDaniel Vetter 	if (IS_GEN2(dev_priv)) {
45054194c088SRodrigo Vivi 		/* Gen2 doesn't have a hardware frame counter */
45064cdb83ecSVille Syrjälä 		dev->max_vblank_count = 0;
45074194c088SRodrigo Vivi 		dev->driver->get_vblank_counter = drm_vblank_no_hw_counter;
4508b963291cSDaniel Vetter 	} else if (IS_G4X(dev_priv) || INTEL_INFO(dev_priv)->gen >= 5) {
4509f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
4510fd8f507cSVille Syrjälä 		dev->driver->get_vblank_counter = g4x_get_vblank_counter;
4511391f75e2SVille Syrjälä 	} else {
4512391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
4513391f75e2SVille Syrjälä 		dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
4514f71d4af4SJesse Barnes 	}
4515f71d4af4SJesse Barnes 
451621da2700SVille Syrjälä 	/*
451721da2700SVille Syrjälä 	 * Opt out of the vblank disable timer on everything except gen2.
451821da2700SVille Syrjälä 	 * Gen2 doesn't have a hardware frame counter and so depends on
451921da2700SVille Syrjälä 	 * vblank interrupts to produce sane vblank seuquence numbers.
452021da2700SVille Syrjälä 	 */
4521b963291cSDaniel Vetter 	if (!IS_GEN2(dev_priv))
452221da2700SVille Syrjälä 		dev->vblank_disable_immediate = true;
452321da2700SVille Syrjälä 
4524f71d4af4SJesse Barnes 	dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
4525f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
4526f71d4af4SJesse Barnes 
4527b963291cSDaniel Vetter 	if (IS_CHERRYVIEW(dev_priv)) {
452843f328d7SVille Syrjälä 		dev->driver->irq_handler = cherryview_irq_handler;
452943f328d7SVille Syrjälä 		dev->driver->irq_preinstall = cherryview_irq_preinstall;
453043f328d7SVille Syrjälä 		dev->driver->irq_postinstall = cherryview_irq_postinstall;
453143f328d7SVille Syrjälä 		dev->driver->irq_uninstall = cherryview_irq_uninstall;
453243f328d7SVille Syrjälä 		dev->driver->enable_vblank = valleyview_enable_vblank;
453343f328d7SVille Syrjälä 		dev->driver->disable_vblank = valleyview_disable_vblank;
453443f328d7SVille Syrjälä 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4535b963291cSDaniel Vetter 	} else if (IS_VALLEYVIEW(dev_priv)) {
45367e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
45377e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
45387e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
45397e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
45407e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
45417e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
4542fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4543b963291cSDaniel Vetter 	} else if (INTEL_INFO(dev_priv)->gen >= 8) {
4544abd58f01SBen Widawsky 		dev->driver->irq_handler = gen8_irq_handler;
4545723761b8SDaniel Vetter 		dev->driver->irq_preinstall = gen8_irq_reset;
4546abd58f01SBen Widawsky 		dev->driver->irq_postinstall = gen8_irq_postinstall;
4547abd58f01SBen Widawsky 		dev->driver->irq_uninstall = gen8_irq_uninstall;
4548abd58f01SBen Widawsky 		dev->driver->enable_vblank = gen8_enable_vblank;
4549abd58f01SBen Widawsky 		dev->driver->disable_vblank = gen8_disable_vblank;
45506dbf30ceSVille Syrjälä 		if (IS_BROXTON(dev))
4551e0a20ad7SShashank Sharma 			dev_priv->display.hpd_irq_setup = bxt_hpd_irq_setup;
455222dea0beSRodrigo Vivi 		else if (HAS_PCH_SPT(dev) || HAS_PCH_KBP(dev))
45536dbf30ceSVille Syrjälä 			dev_priv->display.hpd_irq_setup = spt_hpd_irq_setup;
45546dbf30ceSVille Syrjälä 		else
45553a3b3c7dSVille Syrjälä 			dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
4556f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
4557f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
4558723761b8SDaniel Vetter 		dev->driver->irq_preinstall = ironlake_irq_reset;
4559f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
4560f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
4561f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
4562f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
4563e4ce95aaSVille Syrjälä 		dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
4564f71d4af4SJesse Barnes 	} else {
45657e22dbbbSTvrtko Ursulin 		if (IS_GEN2(dev_priv)) {
4566c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
4567c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
4568c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
4569c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
45707e22dbbbSTvrtko Ursulin 		} else if (IS_GEN3(dev_priv)) {
4571a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
4572a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
4573a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
4574a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
4575c2798b19SChris Wilson 		} else {
4576a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
4577a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
4578a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
4579a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
4580c2798b19SChris Wilson 		}
4581778eb334SVille Syrjälä 		if (I915_HAS_HOTPLUG(dev_priv))
4582778eb334SVille Syrjälä 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4583f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
4584f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
4585f71d4af4SJesse Barnes 	}
4586f71d4af4SJesse Barnes }
458720afbda2SDaniel Vetter 
4588fca52a55SDaniel Vetter /**
4589fca52a55SDaniel Vetter  * intel_irq_install - enables the hardware interrupt
4590fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4591fca52a55SDaniel Vetter  *
4592fca52a55SDaniel Vetter  * This function enables the hardware interrupt handling, but leaves the hotplug
4593fca52a55SDaniel Vetter  * handling still disabled. It is called after intel_irq_init().
4594fca52a55SDaniel Vetter  *
4595fca52a55SDaniel Vetter  * In the driver load and resume code we need working interrupts in a few places
4596fca52a55SDaniel Vetter  * but don't want to deal with the hassle of concurrent probe and hotplug
4597fca52a55SDaniel Vetter  * workers. Hence the split into this two-stage approach.
4598fca52a55SDaniel Vetter  */
45992aeb7d3aSDaniel Vetter int intel_irq_install(struct drm_i915_private *dev_priv)
46002aeb7d3aSDaniel Vetter {
46012aeb7d3aSDaniel Vetter 	/*
46022aeb7d3aSDaniel Vetter 	 * We enable some interrupt sources in our postinstall hooks, so mark
46032aeb7d3aSDaniel Vetter 	 * interrupts as enabled _before_ actually enabling them to avoid
46042aeb7d3aSDaniel Vetter 	 * special cases in our ordering checks.
46052aeb7d3aSDaniel Vetter 	 */
46062aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
46072aeb7d3aSDaniel Vetter 
460891c8a326SChris Wilson 	return drm_irq_install(&dev_priv->drm, dev_priv->drm.pdev->irq);
46092aeb7d3aSDaniel Vetter }
46102aeb7d3aSDaniel Vetter 
4611fca52a55SDaniel Vetter /**
4612fca52a55SDaniel Vetter  * intel_irq_uninstall - finilizes all irq handling
4613fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4614fca52a55SDaniel Vetter  *
4615fca52a55SDaniel Vetter  * This stops interrupt and hotplug handling and unregisters and frees all
4616fca52a55SDaniel Vetter  * resources acquired in the init functions.
4617fca52a55SDaniel Vetter  */
46182aeb7d3aSDaniel Vetter void intel_irq_uninstall(struct drm_i915_private *dev_priv)
46192aeb7d3aSDaniel Vetter {
462091c8a326SChris Wilson 	drm_irq_uninstall(&dev_priv->drm);
46212aeb7d3aSDaniel Vetter 	intel_hpd_cancel_work(dev_priv);
46222aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
46232aeb7d3aSDaniel Vetter }
46242aeb7d3aSDaniel Vetter 
4625fca52a55SDaniel Vetter /**
4626fca52a55SDaniel Vetter  * intel_runtime_pm_disable_interrupts - runtime interrupt disabling
4627fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4628fca52a55SDaniel Vetter  *
4629fca52a55SDaniel Vetter  * This function is used to disable interrupts at runtime, both in the runtime
4630fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4631fca52a55SDaniel Vetter  */
4632b963291cSDaniel Vetter void intel_runtime_pm_disable_interrupts(struct drm_i915_private *dev_priv)
4633c67a470bSPaulo Zanoni {
463491c8a326SChris Wilson 	dev_priv->drm.driver->irq_uninstall(&dev_priv->drm);
46352aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
463691c8a326SChris Wilson 	synchronize_irq(dev_priv->drm.irq);
4637c67a470bSPaulo Zanoni }
4638c67a470bSPaulo Zanoni 
4639fca52a55SDaniel Vetter /**
4640fca52a55SDaniel Vetter  * intel_runtime_pm_enable_interrupts - runtime interrupt enabling
4641fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4642fca52a55SDaniel Vetter  *
4643fca52a55SDaniel Vetter  * This function is used to enable interrupts at runtime, both in the runtime
4644fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4645fca52a55SDaniel Vetter  */
4646b963291cSDaniel Vetter void intel_runtime_pm_enable_interrupts(struct drm_i915_private *dev_priv)
4647c67a470bSPaulo Zanoni {
46482aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
464991c8a326SChris Wilson 	dev_priv->drm.driver->irq_preinstall(&dev_priv->drm);
465091c8a326SChris Wilson 	dev_priv->drm.driver->irq_postinstall(&dev_priv->drm);
4651c67a470bSPaulo Zanoni }
4652