xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 9918271efc7ab9e5efc9580270f67c126901b06f)
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 	POSTING_READ(GTIMR);
26343eaea13SPaulo Zanoni }
26443eaea13SPaulo Zanoni 
265480c8033SDaniel Vetter void gen5_enable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
26643eaea13SPaulo Zanoni {
26743eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, mask);
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 
3393cc134e3SImre Deak void gen6_reset_rps_interrupts(struct drm_device *dev)
3403cc134e3SImre Deak {
3413cc134e3SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
342f0f59a00SVille Syrjälä 	i915_reg_t reg = gen6_pm_iir(dev_priv);
3433cc134e3SImre Deak 
3443cc134e3SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
3453cc134e3SImre Deak 	I915_WRITE(reg, dev_priv->pm_rps_events);
3463cc134e3SImre Deak 	I915_WRITE(reg, dev_priv->pm_rps_events);
3473cc134e3SImre Deak 	POSTING_READ(reg);
348096fad9eSImre Deak 	dev_priv->rps.pm_iir = 0;
3493cc134e3SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
3503cc134e3SImre Deak }
3513cc134e3SImre Deak 
352b900b949SImre Deak void gen6_enable_rps_interrupts(struct drm_device *dev)
353b900b949SImre Deak {
354b900b949SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
355b900b949SImre Deak 
356b900b949SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
35778e68d36SImre Deak 
358b900b949SImre Deak 	WARN_ON(dev_priv->rps.pm_iir);
3593cc134e3SImre Deak 	WARN_ON(I915_READ(gen6_pm_iir(dev_priv)) & dev_priv->pm_rps_events);
360d4d70aa5SImre Deak 	dev_priv->rps.interrupts_enabled = true;
36178e68d36SImre Deak 	I915_WRITE(gen6_pm_ier(dev_priv), I915_READ(gen6_pm_ier(dev_priv)) |
36278e68d36SImre Deak 				dev_priv->pm_rps_events);
363b900b949SImre Deak 	gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
36478e68d36SImre Deak 
365b900b949SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
366b900b949SImre Deak }
367b900b949SImre Deak 
36859d02a1fSImre Deak u32 gen6_sanitize_rps_pm_mask(struct drm_i915_private *dev_priv, u32 mask)
36959d02a1fSImre Deak {
37059d02a1fSImre Deak 	/*
371f24eeb19SImre Deak 	 * SNB,IVB can while VLV,CHV may hard hang on looping batchbuffer
37259d02a1fSImre Deak 	 * if GEN6_PM_UP_EI_EXPIRED is masked.
373f24eeb19SImre Deak 	 *
374f24eeb19SImre Deak 	 * TODO: verify if this can be reproduced on VLV,CHV.
37559d02a1fSImre Deak 	 */
37659d02a1fSImre Deak 	if (INTEL_INFO(dev_priv)->gen <= 7 && !IS_HASWELL(dev_priv))
37759d02a1fSImre Deak 		mask &= ~GEN6_PM_RP_UP_EI_EXPIRED;
37859d02a1fSImre Deak 
37959d02a1fSImre Deak 	if (INTEL_INFO(dev_priv)->gen >= 8)
38059d02a1fSImre Deak 		mask &= ~GEN8_PMINTR_REDIRECT_TO_NON_DISP;
38159d02a1fSImre Deak 
38259d02a1fSImre Deak 	return mask;
38359d02a1fSImre Deak }
38459d02a1fSImre Deak 
385b900b949SImre Deak void gen6_disable_rps_interrupts(struct drm_device *dev)
386b900b949SImre Deak {
387b900b949SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
388b900b949SImre Deak 
389d4d70aa5SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
390d4d70aa5SImre Deak 	dev_priv->rps.interrupts_enabled = false;
391d4d70aa5SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
392d4d70aa5SImre Deak 
393d4d70aa5SImre Deak 	cancel_work_sync(&dev_priv->rps.work);
394d4d70aa5SImre Deak 
3959939fba2SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
3969939fba2SImre Deak 
39759d02a1fSImre Deak 	I915_WRITE(GEN6_PMINTRMSK, gen6_sanitize_rps_pm_mask(dev_priv, ~0));
3989939fba2SImre Deak 
3999939fba2SImre Deak 	__gen6_disable_pm_irq(dev_priv, dev_priv->pm_rps_events);
400b900b949SImre Deak 	I915_WRITE(gen6_pm_ier(dev_priv), I915_READ(gen6_pm_ier(dev_priv)) &
401b900b949SImre Deak 				~dev_priv->pm_rps_events);
40258072ccbSImre Deak 
40358072ccbSImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
40458072ccbSImre Deak 
40558072ccbSImre Deak 	synchronize_irq(dev->irq);
406b900b949SImre Deak }
407b900b949SImre Deak 
4080961021aSBen Widawsky /**
4093a3b3c7dSVille Syrjälä  * bdw_update_port_irq - update DE port interrupt
4103a3b3c7dSVille Syrjälä  * @dev_priv: driver private
4113a3b3c7dSVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
4123a3b3c7dSVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
4133a3b3c7dSVille Syrjälä  */
4143a3b3c7dSVille Syrjälä static void bdw_update_port_irq(struct drm_i915_private *dev_priv,
4153a3b3c7dSVille Syrjälä 				uint32_t interrupt_mask,
4163a3b3c7dSVille Syrjälä 				uint32_t enabled_irq_mask)
4173a3b3c7dSVille Syrjälä {
4183a3b3c7dSVille Syrjälä 	uint32_t new_val;
4193a3b3c7dSVille Syrjälä 	uint32_t old_val;
4203a3b3c7dSVille Syrjälä 
4213a3b3c7dSVille Syrjälä 	assert_spin_locked(&dev_priv->irq_lock);
4223a3b3c7dSVille Syrjälä 
4233a3b3c7dSVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
4243a3b3c7dSVille Syrjälä 
4253a3b3c7dSVille Syrjälä 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
4263a3b3c7dSVille Syrjälä 		return;
4273a3b3c7dSVille Syrjälä 
4283a3b3c7dSVille Syrjälä 	old_val = I915_READ(GEN8_DE_PORT_IMR);
4293a3b3c7dSVille Syrjälä 
4303a3b3c7dSVille Syrjälä 	new_val = old_val;
4313a3b3c7dSVille Syrjälä 	new_val &= ~interrupt_mask;
4323a3b3c7dSVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
4333a3b3c7dSVille Syrjälä 
4343a3b3c7dSVille Syrjälä 	if (new_val != old_val) {
4353a3b3c7dSVille Syrjälä 		I915_WRITE(GEN8_DE_PORT_IMR, new_val);
4363a3b3c7dSVille Syrjälä 		POSTING_READ(GEN8_DE_PORT_IMR);
4373a3b3c7dSVille Syrjälä 	}
4383a3b3c7dSVille Syrjälä }
4393a3b3c7dSVille Syrjälä 
4403a3b3c7dSVille Syrjälä /**
441013d3752SVille Syrjälä  * bdw_update_pipe_irq - update DE pipe interrupt
442013d3752SVille Syrjälä  * @dev_priv: driver private
443013d3752SVille Syrjälä  * @pipe: pipe whose interrupt to update
444013d3752SVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
445013d3752SVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
446013d3752SVille Syrjälä  */
447013d3752SVille Syrjälä void bdw_update_pipe_irq(struct drm_i915_private *dev_priv,
448013d3752SVille Syrjälä 			 enum pipe pipe,
449013d3752SVille Syrjälä 			 uint32_t interrupt_mask,
450013d3752SVille Syrjälä 			 uint32_t enabled_irq_mask)
451013d3752SVille Syrjälä {
452013d3752SVille Syrjälä 	uint32_t new_val;
453013d3752SVille Syrjälä 
454013d3752SVille Syrjälä 	assert_spin_locked(&dev_priv->irq_lock);
455013d3752SVille Syrjälä 
456013d3752SVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
457013d3752SVille Syrjälä 
458013d3752SVille Syrjälä 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
459013d3752SVille Syrjälä 		return;
460013d3752SVille Syrjälä 
461013d3752SVille Syrjälä 	new_val = dev_priv->de_irq_mask[pipe];
462013d3752SVille Syrjälä 	new_val &= ~interrupt_mask;
463013d3752SVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
464013d3752SVille Syrjälä 
465013d3752SVille Syrjälä 	if (new_val != dev_priv->de_irq_mask[pipe]) {
466013d3752SVille Syrjälä 		dev_priv->de_irq_mask[pipe] = new_val;
467013d3752SVille Syrjälä 		I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
468013d3752SVille Syrjälä 		POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
469013d3752SVille Syrjälä 	}
470013d3752SVille Syrjälä }
471013d3752SVille Syrjälä 
472013d3752SVille Syrjälä /**
473fee884edSDaniel Vetter  * ibx_display_interrupt_update - update SDEIMR
474fee884edSDaniel Vetter  * @dev_priv: driver private
475fee884edSDaniel Vetter  * @interrupt_mask: mask of interrupt bits to update
476fee884edSDaniel Vetter  * @enabled_irq_mask: mask of interrupt bits to enable
477fee884edSDaniel Vetter  */
47847339cd9SDaniel Vetter void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
479fee884edSDaniel Vetter 				  uint32_t interrupt_mask,
480fee884edSDaniel Vetter 				  uint32_t enabled_irq_mask)
481fee884edSDaniel Vetter {
482fee884edSDaniel Vetter 	uint32_t sdeimr = I915_READ(SDEIMR);
483fee884edSDaniel Vetter 	sdeimr &= ~interrupt_mask;
484fee884edSDaniel Vetter 	sdeimr |= (~enabled_irq_mask & interrupt_mask);
485fee884edSDaniel Vetter 
48615a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
48715a17aaeSDaniel Vetter 
488fee884edSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
489fee884edSDaniel Vetter 
4909df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
491c67a470bSPaulo Zanoni 		return;
492c67a470bSPaulo Zanoni 
493fee884edSDaniel Vetter 	I915_WRITE(SDEIMR, sdeimr);
494fee884edSDaniel Vetter 	POSTING_READ(SDEIMR);
495fee884edSDaniel Vetter }
4968664281bSPaulo Zanoni 
497b5ea642aSDaniel Vetter static void
498755e9019SImre Deak __i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
499755e9019SImre Deak 		       u32 enable_mask, u32 status_mask)
5007c463586SKeith Packard {
501f0f59a00SVille Syrjälä 	i915_reg_t reg = PIPESTAT(pipe);
502755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
5037c463586SKeith Packard 
504b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
505d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
506b79480baSDaniel Vetter 
50704feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
50804feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
50904feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
51004feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
511755e9019SImre Deak 		return;
512755e9019SImre Deak 
513755e9019SImre Deak 	if ((pipestat & enable_mask) == enable_mask)
51446c06a30SVille Syrjälä 		return;
51546c06a30SVille Syrjälä 
51691d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] |= status_mask;
51791d181ddSImre Deak 
5187c463586SKeith Packard 	/* Enable the interrupt, clear any pending status */
519755e9019SImre Deak 	pipestat |= enable_mask | status_mask;
52046c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
5213143a2bfSChris Wilson 	POSTING_READ(reg);
5227c463586SKeith Packard }
5237c463586SKeith Packard 
524b5ea642aSDaniel Vetter static void
525755e9019SImre Deak __i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
526755e9019SImre Deak 		        u32 enable_mask, u32 status_mask)
5277c463586SKeith Packard {
528f0f59a00SVille Syrjälä 	i915_reg_t reg = PIPESTAT(pipe);
529755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
5307c463586SKeith Packard 
531b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
532d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
533b79480baSDaniel Vetter 
53404feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
53504feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
53604feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
53704feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
53846c06a30SVille Syrjälä 		return;
53946c06a30SVille Syrjälä 
540755e9019SImre Deak 	if ((pipestat & enable_mask) == 0)
541755e9019SImre Deak 		return;
542755e9019SImre Deak 
54391d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] &= ~status_mask;
54491d181ddSImre Deak 
545755e9019SImre Deak 	pipestat &= ~enable_mask;
54646c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
5473143a2bfSChris Wilson 	POSTING_READ(reg);
5487c463586SKeith Packard }
5497c463586SKeith Packard 
55010c59c51SImre Deak static u32 vlv_get_pipestat_enable_mask(struct drm_device *dev, u32 status_mask)
55110c59c51SImre Deak {
55210c59c51SImre Deak 	u32 enable_mask = status_mask << 16;
55310c59c51SImre Deak 
55410c59c51SImre Deak 	/*
555724a6905SVille Syrjälä 	 * On pipe A we don't support the PSR interrupt yet,
556724a6905SVille Syrjälä 	 * on pipe B and C the same bit MBZ.
55710c59c51SImre Deak 	 */
55810c59c51SImre Deak 	if (WARN_ON_ONCE(status_mask & PIPE_A_PSR_STATUS_VLV))
55910c59c51SImre Deak 		return 0;
560724a6905SVille Syrjälä 	/*
561724a6905SVille Syrjälä 	 * On pipe B and C we don't support the PSR interrupt yet, on pipe
562724a6905SVille Syrjälä 	 * A the same bit is for perf counters which we don't use either.
563724a6905SVille Syrjälä 	 */
564724a6905SVille Syrjälä 	if (WARN_ON_ONCE(status_mask & PIPE_B_PSR_STATUS_VLV))
565724a6905SVille Syrjälä 		return 0;
56610c59c51SImre Deak 
56710c59c51SImre Deak 	enable_mask &= ~(PIPE_FIFO_UNDERRUN_STATUS |
56810c59c51SImre Deak 			 SPRITE0_FLIP_DONE_INT_EN_VLV |
56910c59c51SImre Deak 			 SPRITE1_FLIP_DONE_INT_EN_VLV);
57010c59c51SImre Deak 	if (status_mask & SPRITE0_FLIP_DONE_INT_STATUS_VLV)
57110c59c51SImre Deak 		enable_mask |= SPRITE0_FLIP_DONE_INT_EN_VLV;
57210c59c51SImre Deak 	if (status_mask & SPRITE1_FLIP_DONE_INT_STATUS_VLV)
57310c59c51SImre Deak 		enable_mask |= SPRITE1_FLIP_DONE_INT_EN_VLV;
57410c59c51SImre Deak 
57510c59c51SImre Deak 	return enable_mask;
57610c59c51SImre Deak }
57710c59c51SImre Deak 
578755e9019SImre Deak void
579755e9019SImre Deak i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
580755e9019SImre Deak 		     u32 status_mask)
581755e9019SImre Deak {
582755e9019SImre Deak 	u32 enable_mask;
583755e9019SImre Deak 
584666a4537SWayne Boyer 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
58510c59c51SImre Deak 		enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
58610c59c51SImre Deak 							   status_mask);
58710c59c51SImre Deak 	else
588755e9019SImre Deak 		enable_mask = status_mask << 16;
589755e9019SImre Deak 	__i915_enable_pipestat(dev_priv, pipe, enable_mask, status_mask);
590755e9019SImre Deak }
591755e9019SImre Deak 
592755e9019SImre Deak void
593755e9019SImre Deak i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
594755e9019SImre Deak 		      u32 status_mask)
595755e9019SImre Deak {
596755e9019SImre Deak 	u32 enable_mask;
597755e9019SImre Deak 
598666a4537SWayne Boyer 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
59910c59c51SImre Deak 		enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
60010c59c51SImre Deak 							   status_mask);
60110c59c51SImre Deak 	else
602755e9019SImre Deak 		enable_mask = status_mask << 16;
603755e9019SImre Deak 	__i915_disable_pipestat(dev_priv, pipe, enable_mask, status_mask);
604755e9019SImre Deak }
605755e9019SImre Deak 
606c0e09200SDave Airlie /**
607f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
608468f9d29SJavier Martinez Canillas  * @dev: drm device
60901c66889SZhao Yakui  */
610f49e38ddSJani Nikula static void i915_enable_asle_pipestat(struct drm_device *dev)
61101c66889SZhao Yakui {
6122d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
6131ec14ad3SChris Wilson 
614f49e38ddSJani Nikula 	if (!dev_priv->opregion.asle || !IS_MOBILE(dev))
615f49e38ddSJani Nikula 		return;
616f49e38ddSJani Nikula 
61713321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
61801c66889SZhao Yakui 
619755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_STATUS);
620a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4)
6213b6c42e8SDaniel Vetter 		i915_enable_pipestat(dev_priv, PIPE_A,
622755e9019SImre Deak 				     PIPE_LEGACY_BLC_EVENT_STATUS);
6231ec14ad3SChris Wilson 
62413321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
62501c66889SZhao Yakui }
62601c66889SZhao Yakui 
627f75f3746SVille Syrjälä /*
628f75f3746SVille Syrjälä  * This timing diagram depicts the video signal in and
629f75f3746SVille Syrjälä  * around the vertical blanking period.
630f75f3746SVille Syrjälä  *
631f75f3746SVille Syrjälä  * Assumptions about the fictitious mode used in this example:
632f75f3746SVille Syrjälä  *  vblank_start >= 3
633f75f3746SVille Syrjälä  *  vsync_start = vblank_start + 1
634f75f3746SVille Syrjälä  *  vsync_end = vblank_start + 2
635f75f3746SVille Syrjälä  *  vtotal = vblank_start + 3
636f75f3746SVille Syrjälä  *
637f75f3746SVille Syrjälä  *           start of vblank:
638f75f3746SVille Syrjälä  *           latch double buffered registers
639f75f3746SVille Syrjälä  *           increment frame counter (ctg+)
640f75f3746SVille Syrjälä  *           generate start of vblank interrupt (gen4+)
641f75f3746SVille Syrjälä  *           |
642f75f3746SVille Syrjälä  *           |          frame start:
643f75f3746SVille Syrjälä  *           |          generate frame start interrupt (aka. vblank interrupt) (gmch)
644f75f3746SVille Syrjälä  *           |          may be shifted forward 1-3 extra lines via PIPECONF
645f75f3746SVille Syrjälä  *           |          |
646f75f3746SVille Syrjälä  *           |          |  start of vsync:
647f75f3746SVille Syrjälä  *           |          |  generate vsync interrupt
648f75f3746SVille Syrjälä  *           |          |  |
649f75f3746SVille Syrjälä  * ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx
650f75f3746SVille Syrjälä  *       .   \hs/   .      \hs/          \hs/          \hs/   .      \hs/
651f75f3746SVille Syrjälä  * ----va---> <-----------------vb--------------------> <--------va-------------
652f75f3746SVille Syrjälä  *       |          |       <----vs----->                     |
653f75f3746SVille Syrjälä  * -vbs-----> <---vbs+1---> <---vbs+2---> <-----0-----> <-----1-----> <-----2--- (scanline counter gen2)
654f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2---> <-----0--- (scanline counter gen3+)
655f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2- (scanline counter hsw+ hdmi)
656f75f3746SVille Syrjälä  *       |          |                                         |
657f75f3746SVille Syrjälä  *       last visible pixel                                   first visible pixel
658f75f3746SVille Syrjälä  *                  |                                         increment frame counter (gen3/4)
659f75f3746SVille Syrjälä  *                  pixel counter = vblank_start * htotal     pixel counter = 0 (gen3/4)
660f75f3746SVille Syrjälä  *
661f75f3746SVille Syrjälä  * x  = horizontal active
662f75f3746SVille Syrjälä  * _  = horizontal blanking
663f75f3746SVille Syrjälä  * hs = horizontal sync
664f75f3746SVille Syrjälä  * va = vertical active
665f75f3746SVille Syrjälä  * vb = vertical blanking
666f75f3746SVille Syrjälä  * vs = vertical sync
667f75f3746SVille Syrjälä  * vbs = vblank_start (number)
668f75f3746SVille Syrjälä  *
669f75f3746SVille Syrjälä  * Summary:
670f75f3746SVille Syrjälä  * - most events happen at the start of horizontal sync
671f75f3746SVille Syrjälä  * - frame start happens at the start of horizontal blank, 1-4 lines
672f75f3746SVille Syrjälä  *   (depending on PIPECONF settings) after the start of vblank
673f75f3746SVille Syrjälä  * - gen3/4 pixel and frame counter are synchronized with the start
674f75f3746SVille Syrjälä  *   of horizontal active on the first line of vertical active
675f75f3746SVille Syrjälä  */
676f75f3746SVille Syrjälä 
67788e72717SThierry Reding static u32 i8xx_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
6784cdb83ecSVille Syrjälä {
6794cdb83ecSVille Syrjälä 	/* Gen2 doesn't have a hardware frame counter */
6804cdb83ecSVille Syrjälä 	return 0;
6814cdb83ecSVille Syrjälä }
6824cdb83ecSVille Syrjälä 
68342f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
68442f52ef8SKeith Packard  * we use as a pipe index
68542f52ef8SKeith Packard  */
68688e72717SThierry Reding static u32 i915_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
6870a3e67a4SJesse Barnes {
6882d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
689f0f59a00SVille Syrjälä 	i915_reg_t high_frame, low_frame;
6900b2a8e09SVille Syrjälä 	u32 high1, high2, low, pixel, vbl_start, hsync_start, htotal;
691391f75e2SVille Syrjälä 	struct intel_crtc *intel_crtc =
692391f75e2SVille Syrjälä 		to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
693fc467a22SMaarten Lankhorst 	const struct drm_display_mode *mode = &intel_crtc->base.hwmode;
694391f75e2SVille Syrjälä 
6950b2a8e09SVille Syrjälä 	htotal = mode->crtc_htotal;
6960b2a8e09SVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
6970b2a8e09SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
6980b2a8e09SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
6990b2a8e09SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
700391f75e2SVille Syrjälä 
7010b2a8e09SVille Syrjälä 	/* Convert to pixel count */
7020b2a8e09SVille Syrjälä 	vbl_start *= htotal;
7030b2a8e09SVille Syrjälä 
7040b2a8e09SVille Syrjälä 	/* Start of vblank event occurs at start of hsync */
7050b2a8e09SVille Syrjälä 	vbl_start -= htotal - hsync_start;
7060b2a8e09SVille Syrjälä 
7079db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
7089db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
7095eddb70bSChris Wilson 
7100a3e67a4SJesse Barnes 	/*
7110a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
7120a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
7130a3e67a4SJesse Barnes 	 * register.
7140a3e67a4SJesse Barnes 	 */
7150a3e67a4SJesse Barnes 	do {
7165eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
717391f75e2SVille Syrjälä 		low   = I915_READ(low_frame);
7185eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
7190a3e67a4SJesse Barnes 	} while (high1 != high2);
7200a3e67a4SJesse Barnes 
7215eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
722391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
7235eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
724391f75e2SVille Syrjälä 
725391f75e2SVille Syrjälä 	/*
726391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
727391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
728391f75e2SVille Syrjälä 	 * counter against vblank start.
729391f75e2SVille Syrjälä 	 */
730edc08d0aSVille Syrjälä 	return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
7310a3e67a4SJesse Barnes }
7320a3e67a4SJesse Barnes 
733974e59baSDave Airlie static u32 g4x_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
7349880b7a5SJesse Barnes {
7352d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
7369880b7a5SJesse Barnes 
737649636efSVille Syrjälä 	return I915_READ(PIPE_FRMCOUNT_G4X(pipe));
7389880b7a5SJesse Barnes }
7399880b7a5SJesse Barnes 
74075aa3f63SVille Syrjälä /* I915_READ_FW, only for fast reads of display block, no need for forcewake etc. */
741a225f079SVille Syrjälä static int __intel_get_crtc_scanline(struct intel_crtc *crtc)
742a225f079SVille Syrjälä {
743a225f079SVille Syrjälä 	struct drm_device *dev = crtc->base.dev;
744a225f079SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
745fc467a22SMaarten Lankhorst 	const struct drm_display_mode *mode = &crtc->base.hwmode;
746a225f079SVille Syrjälä 	enum pipe pipe = crtc->pipe;
74780715b2fSVille Syrjälä 	int position, vtotal;
748a225f079SVille Syrjälä 
74980715b2fSVille Syrjälä 	vtotal = mode->crtc_vtotal;
750a225f079SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
751a225f079SVille Syrjälä 		vtotal /= 2;
752a225f079SVille Syrjälä 
753a225f079SVille Syrjälä 	if (IS_GEN2(dev))
75475aa3f63SVille Syrjälä 		position = I915_READ_FW(PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
755a225f079SVille Syrjälä 	else
75675aa3f63SVille Syrjälä 		position = I915_READ_FW(PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
757a225f079SVille Syrjälä 
758a225f079SVille Syrjälä 	/*
75941b578fbSJesse Barnes 	 * On HSW, the DSL reg (0x70000) appears to return 0 if we
76041b578fbSJesse Barnes 	 * read it just before the start of vblank.  So try it again
76141b578fbSJesse Barnes 	 * so we don't accidentally end up spanning a vblank frame
76241b578fbSJesse Barnes 	 * increment, causing the pipe_update_end() code to squak at us.
76341b578fbSJesse Barnes 	 *
76441b578fbSJesse Barnes 	 * The nature of this problem means we can't simply check the ISR
76541b578fbSJesse Barnes 	 * bit and return the vblank start value; nor can we use the scanline
76641b578fbSJesse Barnes 	 * debug register in the transcoder as it appears to have the same
76741b578fbSJesse Barnes 	 * problem.  We may need to extend this to include other platforms,
76841b578fbSJesse Barnes 	 * but so far testing only shows the problem on HSW.
76941b578fbSJesse Barnes 	 */
770b2916819SMaarten Lankhorst 	if (HAS_DDI(dev) && !position) {
77141b578fbSJesse Barnes 		int i, temp;
77241b578fbSJesse Barnes 
77341b578fbSJesse Barnes 		for (i = 0; i < 100; i++) {
77441b578fbSJesse Barnes 			udelay(1);
77541b578fbSJesse Barnes 			temp = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) &
77641b578fbSJesse Barnes 				DSL_LINEMASK_GEN3;
77741b578fbSJesse Barnes 			if (temp != position) {
77841b578fbSJesse Barnes 				position = temp;
77941b578fbSJesse Barnes 				break;
78041b578fbSJesse Barnes 			}
78141b578fbSJesse Barnes 		}
78241b578fbSJesse Barnes 	}
78341b578fbSJesse Barnes 
78441b578fbSJesse Barnes 	/*
78580715b2fSVille Syrjälä 	 * See update_scanline_offset() for the details on the
78680715b2fSVille Syrjälä 	 * scanline_offset adjustment.
787a225f079SVille Syrjälä 	 */
78880715b2fSVille Syrjälä 	return (position + crtc->scanline_offset) % vtotal;
789a225f079SVille Syrjälä }
790a225f079SVille Syrjälä 
79188e72717SThierry Reding static int i915_get_crtc_scanoutpos(struct drm_device *dev, unsigned int pipe,
792abca9e45SVille Syrjälä 				    unsigned int flags, int *vpos, int *hpos,
7933bb403bfSVille Syrjälä 				    ktime_t *stime, ktime_t *etime,
7943bb403bfSVille Syrjälä 				    const struct drm_display_mode *mode)
7950af7e4dfSMario Kleiner {
796c2baf4b7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
797c2baf4b7SVille Syrjälä 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
798c2baf4b7SVille Syrjälä 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7993aa18df8SVille Syrjälä 	int position;
80078e8fc6bSVille Syrjälä 	int vbl_start, vbl_end, hsync_start, htotal, vtotal;
8010af7e4dfSMario Kleiner 	bool in_vbl = true;
8020af7e4dfSMario Kleiner 	int ret = 0;
803ad3543edSMario Kleiner 	unsigned long irqflags;
8040af7e4dfSMario Kleiner 
805fc467a22SMaarten Lankhorst 	if (WARN_ON(!mode->crtc_clock)) {
8060af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
8079db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
8080af7e4dfSMario Kleiner 		return 0;
8090af7e4dfSMario Kleiner 	}
8100af7e4dfSMario Kleiner 
811c2baf4b7SVille Syrjälä 	htotal = mode->crtc_htotal;
81278e8fc6bSVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
813c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
814c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
815c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
8160af7e4dfSMario Kleiner 
817d31faf65SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
818d31faf65SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
819d31faf65SVille Syrjälä 		vbl_end /= 2;
820d31faf65SVille Syrjälä 		vtotal /= 2;
821d31faf65SVille Syrjälä 	}
822d31faf65SVille Syrjälä 
823c2baf4b7SVille Syrjälä 	ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
824c2baf4b7SVille Syrjälä 
825ad3543edSMario Kleiner 	/*
826ad3543edSMario Kleiner 	 * Lock uncore.lock, as we will do multiple timing critical raw
827ad3543edSMario Kleiner 	 * register reads, potentially with preemption disabled, so the
828ad3543edSMario Kleiner 	 * following code must not block on uncore.lock.
829ad3543edSMario Kleiner 	 */
830ad3543edSMario Kleiner 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
831ad3543edSMario Kleiner 
832ad3543edSMario Kleiner 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
833ad3543edSMario Kleiner 
834ad3543edSMario Kleiner 	/* Get optional system timestamp before query. */
835ad3543edSMario Kleiner 	if (stime)
836ad3543edSMario Kleiner 		*stime = ktime_get();
837ad3543edSMario Kleiner 
8387c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
8390af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
8400af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
8410af7e4dfSMario Kleiner 		 */
842a225f079SVille Syrjälä 		position = __intel_get_crtc_scanline(intel_crtc);
8430af7e4dfSMario Kleiner 	} else {
8440af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
8450af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
8460af7e4dfSMario Kleiner 		 * scanout position.
8470af7e4dfSMario Kleiner 		 */
84875aa3f63SVille Syrjälä 		position = (I915_READ_FW(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
8490af7e4dfSMario Kleiner 
8503aa18df8SVille Syrjälä 		/* convert to pixel counts */
8513aa18df8SVille Syrjälä 		vbl_start *= htotal;
8523aa18df8SVille Syrjälä 		vbl_end *= htotal;
8533aa18df8SVille Syrjälä 		vtotal *= htotal;
85478e8fc6bSVille Syrjälä 
85578e8fc6bSVille Syrjälä 		/*
8567e78f1cbSVille Syrjälä 		 * In interlaced modes, the pixel counter counts all pixels,
8577e78f1cbSVille Syrjälä 		 * so one field will have htotal more pixels. In order to avoid
8587e78f1cbSVille Syrjälä 		 * the reported position from jumping backwards when the pixel
8597e78f1cbSVille Syrjälä 		 * counter is beyond the length of the shorter field, just
8607e78f1cbSVille Syrjälä 		 * clamp the position the length of the shorter field. This
8617e78f1cbSVille Syrjälä 		 * matches how the scanline counter based position works since
8627e78f1cbSVille Syrjälä 		 * the scanline counter doesn't count the two half lines.
8637e78f1cbSVille Syrjälä 		 */
8647e78f1cbSVille Syrjälä 		if (position >= vtotal)
8657e78f1cbSVille Syrjälä 			position = vtotal - 1;
8667e78f1cbSVille Syrjälä 
8677e78f1cbSVille Syrjälä 		/*
86878e8fc6bSVille Syrjälä 		 * Start of vblank interrupt is triggered at start of hsync,
86978e8fc6bSVille Syrjälä 		 * just prior to the first active line of vblank. However we
87078e8fc6bSVille Syrjälä 		 * consider lines to start at the leading edge of horizontal
87178e8fc6bSVille Syrjälä 		 * active. So, should we get here before we've crossed into
87278e8fc6bSVille Syrjälä 		 * the horizontal active of the first line in vblank, we would
87378e8fc6bSVille Syrjälä 		 * not set the DRM_SCANOUTPOS_INVBL flag. In order to fix that,
87478e8fc6bSVille Syrjälä 		 * always add htotal-hsync_start to the current pixel position.
87578e8fc6bSVille Syrjälä 		 */
87678e8fc6bSVille Syrjälä 		position = (position + htotal - hsync_start) % vtotal;
8773aa18df8SVille Syrjälä 	}
8783aa18df8SVille Syrjälä 
879ad3543edSMario Kleiner 	/* Get optional system timestamp after query. */
880ad3543edSMario Kleiner 	if (etime)
881ad3543edSMario Kleiner 		*etime = ktime_get();
882ad3543edSMario Kleiner 
883ad3543edSMario Kleiner 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
884ad3543edSMario Kleiner 
885ad3543edSMario Kleiner 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
886ad3543edSMario Kleiner 
8873aa18df8SVille Syrjälä 	in_vbl = position >= vbl_start && position < vbl_end;
8883aa18df8SVille Syrjälä 
8893aa18df8SVille Syrjälä 	/*
8903aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
8913aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
8923aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
8933aa18df8SVille Syrjälä 	 * up since vbl_end.
8943aa18df8SVille Syrjälä 	 */
8953aa18df8SVille Syrjälä 	if (position >= vbl_start)
8963aa18df8SVille Syrjälä 		position -= vbl_end;
8973aa18df8SVille Syrjälä 	else
8983aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
8993aa18df8SVille Syrjälä 
9007c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
9013aa18df8SVille Syrjälä 		*vpos = position;
9023aa18df8SVille Syrjälä 		*hpos = 0;
9033aa18df8SVille Syrjälä 	} else {
9040af7e4dfSMario Kleiner 		*vpos = position / htotal;
9050af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
9060af7e4dfSMario Kleiner 	}
9070af7e4dfSMario Kleiner 
9080af7e4dfSMario Kleiner 	/* In vblank? */
9090af7e4dfSMario Kleiner 	if (in_vbl)
9103d3cbd84SDaniel Vetter 		ret |= DRM_SCANOUTPOS_IN_VBLANK;
9110af7e4dfSMario Kleiner 
9120af7e4dfSMario Kleiner 	return ret;
9130af7e4dfSMario Kleiner }
9140af7e4dfSMario Kleiner 
915a225f079SVille Syrjälä int intel_get_crtc_scanline(struct intel_crtc *crtc)
916a225f079SVille Syrjälä {
917a225f079SVille Syrjälä 	struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
918a225f079SVille Syrjälä 	unsigned long irqflags;
919a225f079SVille Syrjälä 	int position;
920a225f079SVille Syrjälä 
921a225f079SVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
922a225f079SVille Syrjälä 	position = __intel_get_crtc_scanline(crtc);
923a225f079SVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
924a225f079SVille Syrjälä 
925a225f079SVille Syrjälä 	return position;
926a225f079SVille Syrjälä }
927a225f079SVille Syrjälä 
92888e72717SThierry Reding static int i915_get_vblank_timestamp(struct drm_device *dev, unsigned int pipe,
9290af7e4dfSMario Kleiner 			      int *max_error,
9300af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
9310af7e4dfSMario Kleiner 			      unsigned flags)
9320af7e4dfSMario Kleiner {
9334041b853SChris Wilson 	struct drm_crtc *crtc;
9340af7e4dfSMario Kleiner 
93588e72717SThierry Reding 	if (pipe >= INTEL_INFO(dev)->num_pipes) {
93688e72717SThierry Reding 		DRM_ERROR("Invalid crtc %u\n", pipe);
9370af7e4dfSMario Kleiner 		return -EINVAL;
9380af7e4dfSMario Kleiner 	}
9390af7e4dfSMario Kleiner 
9400af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
9414041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
9424041b853SChris Wilson 	if (crtc == NULL) {
94388e72717SThierry Reding 		DRM_ERROR("Invalid crtc %u\n", pipe);
9444041b853SChris Wilson 		return -EINVAL;
9454041b853SChris Wilson 	}
9464041b853SChris Wilson 
947fc467a22SMaarten Lankhorst 	if (!crtc->hwmode.crtc_clock) {
94888e72717SThierry Reding 		DRM_DEBUG_KMS("crtc %u is disabled\n", pipe);
9494041b853SChris Wilson 		return -EBUSY;
9504041b853SChris Wilson 	}
9510af7e4dfSMario Kleiner 
9520af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
9534041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
9544041b853SChris Wilson 						     vblank_time, flags,
955fc467a22SMaarten Lankhorst 						     &crtc->hwmode);
9560af7e4dfSMario Kleiner }
9570af7e4dfSMario Kleiner 
958d0ecd7e2SDaniel Vetter static void ironlake_rps_change_irq_handler(struct drm_device *dev)
959f97108d1SJesse Barnes {
9602d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
961b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
9629270388eSDaniel Vetter 	u8 new_delay;
9639270388eSDaniel Vetter 
964d0ecd7e2SDaniel Vetter 	spin_lock(&mchdev_lock);
965f97108d1SJesse Barnes 
96673edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
96773edd18fSDaniel Vetter 
96820e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
9699270388eSDaniel Vetter 
9707648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
971b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
972b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
973f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
974f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
975f97108d1SJesse Barnes 
976f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
977b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
97820e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
97920e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
98020e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
98120e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
982b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
98320e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
98420e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
98520e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
98620e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
987f97108d1SJesse Barnes 	}
988f97108d1SJesse Barnes 
9897648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
99020e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
991f97108d1SJesse Barnes 
992d0ecd7e2SDaniel Vetter 	spin_unlock(&mchdev_lock);
9939270388eSDaniel Vetter 
994f97108d1SJesse Barnes 	return;
995f97108d1SJesse Barnes }
996f97108d1SJesse Barnes 
9970bc40be8STvrtko Ursulin static void notify_ring(struct intel_engine_cs *engine)
998549f7365SChris Wilson {
999117897f4STvrtko Ursulin 	if (!intel_engine_initialized(engine))
1000475553deSChris Wilson 		return;
1001475553deSChris Wilson 
10020bc40be8STvrtko Ursulin 	trace_i915_gem_request_notify(engine);
100312471ba8SChris Wilson 	engine->user_interrupts++;
10049862e600SChris Wilson 
10050bc40be8STvrtko Ursulin 	wake_up_all(&engine->irq_queue);
1006549f7365SChris Wilson }
1007549f7365SChris Wilson 
100843cf3bf0SChris Wilson static void vlv_c0_read(struct drm_i915_private *dev_priv,
100943cf3bf0SChris Wilson 			struct intel_rps_ei *ei)
101031685c25SDeepak S {
101143cf3bf0SChris Wilson 	ei->cz_clock = vlv_punit_read(dev_priv, PUNIT_REG_CZ_TIMESTAMP);
101243cf3bf0SChris Wilson 	ei->render_c0 = I915_READ(VLV_RENDER_C0_COUNT);
101343cf3bf0SChris Wilson 	ei->media_c0 = I915_READ(VLV_MEDIA_C0_COUNT);
101431685c25SDeepak S }
101531685c25SDeepak S 
101643cf3bf0SChris Wilson static bool vlv_c0_above(struct drm_i915_private *dev_priv,
101743cf3bf0SChris Wilson 			 const struct intel_rps_ei *old,
101843cf3bf0SChris Wilson 			 const struct intel_rps_ei *now,
101943cf3bf0SChris Wilson 			 int threshold)
102031685c25SDeepak S {
102143cf3bf0SChris Wilson 	u64 time, c0;
10227bad74d5SVille Syrjälä 	unsigned int mul = 100;
102331685c25SDeepak S 
102443cf3bf0SChris Wilson 	if (old->cz_clock == 0)
102543cf3bf0SChris Wilson 		return false;
102631685c25SDeepak S 
10277bad74d5SVille Syrjälä 	if (I915_READ(VLV_COUNTER_CONTROL) & VLV_COUNT_RANGE_HIGH)
10287bad74d5SVille Syrjälä 		mul <<= 8;
10297bad74d5SVille Syrjälä 
103043cf3bf0SChris Wilson 	time = now->cz_clock - old->cz_clock;
10317bad74d5SVille Syrjälä 	time *= threshold * dev_priv->czclk_freq;
103231685c25SDeepak S 
103343cf3bf0SChris Wilson 	/* Workload can be split between render + media, e.g. SwapBuffers
103443cf3bf0SChris Wilson 	 * being blitted in X after being rendered in mesa. To account for
103543cf3bf0SChris Wilson 	 * this we need to combine both engines into our activity counter.
103643cf3bf0SChris Wilson 	 */
103743cf3bf0SChris Wilson 	c0 = now->render_c0 - old->render_c0;
103843cf3bf0SChris Wilson 	c0 += now->media_c0 - old->media_c0;
10397bad74d5SVille Syrjälä 	c0 *= mul * VLV_CZ_CLOCK_TO_MILLI_SEC;
104031685c25SDeepak S 
104143cf3bf0SChris Wilson 	return c0 >= time;
104231685c25SDeepak S }
104331685c25SDeepak S 
104443cf3bf0SChris Wilson void gen6_rps_reset_ei(struct drm_i915_private *dev_priv)
104543cf3bf0SChris Wilson {
104643cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &dev_priv->rps.down_ei);
104743cf3bf0SChris Wilson 	dev_priv->rps.up_ei = dev_priv->rps.down_ei;
104843cf3bf0SChris Wilson }
104943cf3bf0SChris Wilson 
105043cf3bf0SChris Wilson static u32 vlv_wa_c0_ei(struct drm_i915_private *dev_priv, u32 pm_iir)
105143cf3bf0SChris Wilson {
105243cf3bf0SChris Wilson 	struct intel_rps_ei now;
105343cf3bf0SChris Wilson 	u32 events = 0;
105443cf3bf0SChris Wilson 
10556f4b12f8SChris Wilson 	if ((pm_iir & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED)) == 0)
105643cf3bf0SChris Wilson 		return 0;
105743cf3bf0SChris Wilson 
105843cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &now);
105943cf3bf0SChris Wilson 	if (now.cz_clock == 0)
106043cf3bf0SChris Wilson 		return 0;
106131685c25SDeepak S 
106243cf3bf0SChris Wilson 	if (pm_iir & GEN6_PM_RP_DOWN_EI_EXPIRED) {
106343cf3bf0SChris Wilson 		if (!vlv_c0_above(dev_priv,
106443cf3bf0SChris Wilson 				  &dev_priv->rps.down_ei, &now,
10658fb55197SChris Wilson 				  dev_priv->rps.down_threshold))
106643cf3bf0SChris Wilson 			events |= GEN6_PM_RP_DOWN_THRESHOLD;
106743cf3bf0SChris Wilson 		dev_priv->rps.down_ei = now;
106831685c25SDeepak S 	}
106931685c25SDeepak S 
107043cf3bf0SChris Wilson 	if (pm_iir & GEN6_PM_RP_UP_EI_EXPIRED) {
107143cf3bf0SChris Wilson 		if (vlv_c0_above(dev_priv,
107243cf3bf0SChris Wilson 				 &dev_priv->rps.up_ei, &now,
10738fb55197SChris Wilson 				 dev_priv->rps.up_threshold))
107443cf3bf0SChris Wilson 			events |= GEN6_PM_RP_UP_THRESHOLD;
107543cf3bf0SChris Wilson 		dev_priv->rps.up_ei = now;
107643cf3bf0SChris Wilson 	}
107743cf3bf0SChris Wilson 
107843cf3bf0SChris Wilson 	return events;
107931685c25SDeepak S }
108031685c25SDeepak S 
1081f5a4c67dSChris Wilson static bool any_waiters(struct drm_i915_private *dev_priv)
1082f5a4c67dSChris Wilson {
1083e2f80391STvrtko Ursulin 	struct intel_engine_cs *engine;
1084f5a4c67dSChris Wilson 
1085b4ac5afcSDave Gordon 	for_each_engine(engine, dev_priv)
1086e2f80391STvrtko Ursulin 		if (engine->irq_refcount)
1087f5a4c67dSChris Wilson 			return true;
1088f5a4c67dSChris Wilson 
1089f5a4c67dSChris Wilson 	return false;
1090f5a4c67dSChris Wilson }
1091f5a4c67dSChris Wilson 
10924912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
10933b8d8d91SJesse Barnes {
10942d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
10952d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, rps.work);
10968d3afd7dSChris Wilson 	bool client_boost;
10978d3afd7dSChris Wilson 	int new_delay, adj, min, max;
1098edbfdb45SPaulo Zanoni 	u32 pm_iir;
10993b8d8d91SJesse Barnes 
110059cdb63dSDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
1101d4d70aa5SImre Deak 	/* Speed up work cancelation during disabling rps interrupts. */
1102d4d70aa5SImre Deak 	if (!dev_priv->rps.interrupts_enabled) {
1103d4d70aa5SImre Deak 		spin_unlock_irq(&dev_priv->irq_lock);
1104d4d70aa5SImre Deak 		return;
1105d4d70aa5SImre Deak 	}
11061f814dacSImre Deak 
11071f814dacSImre Deak 	/*
11081f814dacSImre Deak 	 * The RPS work is synced during runtime suspend, we don't require a
11091f814dacSImre Deak 	 * wakeref. TODO: instead of disabling the asserts make sure that we
11101f814dacSImre Deak 	 * always hold an RPM reference while the work is running.
11111f814dacSImre Deak 	 */
11121f814dacSImre Deak 	DISABLE_RPM_WAKEREF_ASSERTS(dev_priv);
11131f814dacSImre Deak 
1114c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
1115c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
1116a72fbc3aSImre Deak 	/* Make sure not to corrupt PMIMR state used by ringbuffer on GEN6 */
1117480c8033SDaniel Vetter 	gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
11188d3afd7dSChris Wilson 	client_boost = dev_priv->rps.client_boost;
11198d3afd7dSChris Wilson 	dev_priv->rps.client_boost = false;
112059cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
11214912d041SBen Widawsky 
112260611c13SPaulo Zanoni 	/* Make sure we didn't queue anything we're not going to process. */
1123a6706b45SDeepak S 	WARN_ON(pm_iir & ~dev_priv->pm_rps_events);
112460611c13SPaulo Zanoni 
11258d3afd7dSChris Wilson 	if ((pm_iir & dev_priv->pm_rps_events) == 0 && !client_boost)
11261f814dacSImre Deak 		goto out;
11273b8d8d91SJesse Barnes 
11284fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
11297b9e0ae6SChris Wilson 
113043cf3bf0SChris Wilson 	pm_iir |= vlv_wa_c0_ei(dev_priv, pm_iir);
113143cf3bf0SChris Wilson 
1132dd75fdc8SChris Wilson 	adj = dev_priv->rps.last_adj;
1133edcf284bSChris Wilson 	new_delay = dev_priv->rps.cur_freq;
11348d3afd7dSChris Wilson 	min = dev_priv->rps.min_freq_softlimit;
11358d3afd7dSChris Wilson 	max = dev_priv->rps.max_freq_softlimit;
11368d3afd7dSChris Wilson 
11378d3afd7dSChris Wilson 	if (client_boost) {
11388d3afd7dSChris Wilson 		new_delay = dev_priv->rps.max_freq_softlimit;
11398d3afd7dSChris Wilson 		adj = 0;
11408d3afd7dSChris Wilson 	} else if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
1141dd75fdc8SChris Wilson 		if (adj > 0)
1142dd75fdc8SChris Wilson 			adj *= 2;
1143edcf284bSChris Wilson 		else /* CHV needs even encode values */
1144edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? 2 : 1;
11457425034aSVille Syrjälä 		/*
11467425034aSVille Syrjälä 		 * For better performance, jump directly
11477425034aSVille Syrjälä 		 * to RPe if we're below it.
11487425034aSVille Syrjälä 		 */
1149edcf284bSChris Wilson 		if (new_delay < dev_priv->rps.efficient_freq - adj) {
1150b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1151edcf284bSChris Wilson 			adj = 0;
1152edcf284bSChris Wilson 		}
1153f5a4c67dSChris Wilson 	} else if (any_waiters(dev_priv)) {
1154f5a4c67dSChris Wilson 		adj = 0;
1155dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
1156b39fb297SBen Widawsky 		if (dev_priv->rps.cur_freq > dev_priv->rps.efficient_freq)
1157b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1158dd75fdc8SChris Wilson 		else
1159b39fb297SBen Widawsky 			new_delay = dev_priv->rps.min_freq_softlimit;
1160dd75fdc8SChris Wilson 		adj = 0;
1161dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1162dd75fdc8SChris Wilson 		if (adj < 0)
1163dd75fdc8SChris Wilson 			adj *= 2;
1164edcf284bSChris Wilson 		else /* CHV needs even encode values */
1165edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? -2 : -1;
1166dd75fdc8SChris Wilson 	} else { /* unknown event */
1167edcf284bSChris Wilson 		adj = 0;
1168dd75fdc8SChris Wilson 	}
11693b8d8d91SJesse Barnes 
1170edcf284bSChris Wilson 	dev_priv->rps.last_adj = adj;
1171edcf284bSChris Wilson 
117279249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
117379249636SBen Widawsky 	 * interrupt
117479249636SBen Widawsky 	 */
1175edcf284bSChris Wilson 	new_delay += adj;
11768d3afd7dSChris Wilson 	new_delay = clamp_t(int, new_delay, min, max);
117727544369SDeepak S 
1178ffe02b40SVille Syrjälä 	intel_set_rps(dev_priv->dev, new_delay);
11793b8d8d91SJesse Barnes 
11804fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
11811f814dacSImre Deak out:
11821f814dacSImre Deak 	ENABLE_RPM_WAKEREF_ASSERTS(dev_priv);
11833b8d8d91SJesse Barnes }
11843b8d8d91SJesse Barnes 
1185e3689190SBen Widawsky 
1186e3689190SBen Widawsky /**
1187e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
1188e3689190SBen Widawsky  * occurred.
1189e3689190SBen Widawsky  * @work: workqueue struct
1190e3689190SBen Widawsky  *
1191e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
1192e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
1193e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
1194e3689190SBen Widawsky  */
1195e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
1196e3689190SBen Widawsky {
11972d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
11982d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, l3_parity.error_work);
1199e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
120035a85ac6SBen Widawsky 	char *parity_event[6];
1201e3689190SBen Widawsky 	uint32_t misccpctl;
120235a85ac6SBen Widawsky 	uint8_t slice = 0;
1203e3689190SBen Widawsky 
1204e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
1205e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
1206e3689190SBen Widawsky 	 * any time we access those registers.
1207e3689190SBen Widawsky 	 */
1208e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
1209e3689190SBen Widawsky 
121035a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
121135a85ac6SBen Widawsky 	if (WARN_ON(!dev_priv->l3_parity.which_slice))
121235a85ac6SBen Widawsky 		goto out;
121335a85ac6SBen Widawsky 
1214e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
1215e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1216e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
1217e3689190SBen Widawsky 
121835a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
1219f0f59a00SVille Syrjälä 		i915_reg_t reg;
122035a85ac6SBen Widawsky 
122135a85ac6SBen Widawsky 		slice--;
12222d1fe073SJoonas Lahtinen 		if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv)))
122335a85ac6SBen Widawsky 			break;
122435a85ac6SBen Widawsky 
122535a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
122635a85ac6SBen Widawsky 
12276fa1c5f1SVille Syrjälä 		reg = GEN7_L3CDERRST1(slice);
122835a85ac6SBen Widawsky 
122935a85ac6SBen Widawsky 		error_status = I915_READ(reg);
1230e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
1231e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
1232e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1233e3689190SBen Widawsky 
123435a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
123535a85ac6SBen Widawsky 		POSTING_READ(reg);
1236e3689190SBen Widawsky 
1237cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1238e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1239e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1240e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
124135a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
124235a85ac6SBen Widawsky 		parity_event[5] = NULL;
1243e3689190SBen Widawsky 
12445bdebb18SDave Airlie 		kobject_uevent_env(&dev_priv->dev->primary->kdev->kobj,
1245e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
1246e3689190SBen Widawsky 
124735a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
124835a85ac6SBen Widawsky 			  slice, row, bank, subbank);
1249e3689190SBen Widawsky 
125035a85ac6SBen Widawsky 		kfree(parity_event[4]);
1251e3689190SBen Widawsky 		kfree(parity_event[3]);
1252e3689190SBen Widawsky 		kfree(parity_event[2]);
1253e3689190SBen Widawsky 		kfree(parity_event[1]);
1254e3689190SBen Widawsky 	}
1255e3689190SBen Widawsky 
125635a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
125735a85ac6SBen Widawsky 
125835a85ac6SBen Widawsky out:
125935a85ac6SBen Widawsky 	WARN_ON(dev_priv->l3_parity.which_slice);
12604cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
12612d1fe073SJoonas Lahtinen 	gen5_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv));
12624cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
126335a85ac6SBen Widawsky 
126435a85ac6SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
126535a85ac6SBen Widawsky }
126635a85ac6SBen Widawsky 
126735a85ac6SBen Widawsky static void ivybridge_parity_error_irq_handler(struct drm_device *dev, u32 iir)
1268e3689190SBen Widawsky {
12692d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1270e3689190SBen Widawsky 
1271040d2baaSBen Widawsky 	if (!HAS_L3_DPF(dev))
1272e3689190SBen Widawsky 		return;
1273e3689190SBen Widawsky 
1274d0ecd7e2SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1275480c8033SDaniel Vetter 	gen5_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev));
1276d0ecd7e2SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
1277e3689190SBen Widawsky 
127835a85ac6SBen Widawsky 	iir &= GT_PARITY_ERROR(dev);
127935a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
128035a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 1;
128135a85ac6SBen Widawsky 
128235a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
128335a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 0;
128435a85ac6SBen Widawsky 
1285a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
1286e3689190SBen Widawsky }
1287e3689190SBen Widawsky 
1288f1af8fc1SPaulo Zanoni static void ilk_gt_irq_handler(struct drm_device *dev,
1289f1af8fc1SPaulo Zanoni 			       struct drm_i915_private *dev_priv,
1290f1af8fc1SPaulo Zanoni 			       u32 gt_iir)
1291f1af8fc1SPaulo Zanoni {
1292f1af8fc1SPaulo Zanoni 	if (gt_iir &
1293f1af8fc1SPaulo Zanoni 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
12944a570db5STvrtko Ursulin 		notify_ring(&dev_priv->engine[RCS]);
1295f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
12964a570db5STvrtko Ursulin 		notify_ring(&dev_priv->engine[VCS]);
1297f1af8fc1SPaulo Zanoni }
1298f1af8fc1SPaulo Zanoni 
1299e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
1300e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
1301e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1302e7b4c6b1SDaniel Vetter {
1303e7b4c6b1SDaniel Vetter 
1304cc609d5dSBen Widawsky 	if (gt_iir &
1305cc609d5dSBen Widawsky 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
13064a570db5STvrtko Ursulin 		notify_ring(&dev_priv->engine[RCS]);
1307cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
13084a570db5STvrtko Ursulin 		notify_ring(&dev_priv->engine[VCS]);
1309cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
13104a570db5STvrtko Ursulin 		notify_ring(&dev_priv->engine[BCS]);
1311e7b4c6b1SDaniel Vetter 
1312cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1313cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
1314aaecdf61SDaniel Vetter 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT))
1315aaecdf61SDaniel Vetter 		DRM_DEBUG("Command parser error, gt_iir 0x%08x\n", gt_iir);
1316e3689190SBen Widawsky 
131735a85ac6SBen Widawsky 	if (gt_iir & GT_PARITY_ERROR(dev))
131835a85ac6SBen Widawsky 		ivybridge_parity_error_irq_handler(dev, gt_iir);
1319e7b4c6b1SDaniel Vetter }
1320e7b4c6b1SDaniel Vetter 
1321fbcc1a0cSNick Hoath static __always_inline void
13220bc40be8STvrtko Ursulin gen8_cs_irq_handler(struct intel_engine_cs *engine, u32 iir, int test_shift)
1323fbcc1a0cSNick Hoath {
1324fbcc1a0cSNick Hoath 	if (iir & (GT_RENDER_USER_INTERRUPT << test_shift))
13250bc40be8STvrtko Ursulin 		notify_ring(engine);
1326fbcc1a0cSNick Hoath 	if (iir & (GT_CONTEXT_SWITCH_INTERRUPT << test_shift))
132727af5eeaSTvrtko Ursulin 		tasklet_schedule(&engine->irq_tasklet);
1328fbcc1a0cSNick Hoath }
1329fbcc1a0cSNick Hoath 
133074cdb337SChris Wilson static irqreturn_t gen8_gt_irq_handler(struct drm_i915_private *dev_priv,
1331abd58f01SBen Widawsky 				       u32 master_ctl)
1332abd58f01SBen Widawsky {
1333abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
1334abd58f01SBen Widawsky 
1335abd58f01SBen Widawsky 	if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
13365dd280b0SNick Hoath 		u32 iir = I915_READ_FW(GEN8_GT_IIR(0));
13375dd280b0SNick Hoath 		if (iir) {
13385dd280b0SNick Hoath 			I915_WRITE_FW(GEN8_GT_IIR(0), iir);
1339abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1340e981e7b1SThomas Daniel 
13414a570db5STvrtko Ursulin 			gen8_cs_irq_handler(&dev_priv->engine[RCS],
1342fbcc1a0cSNick Hoath 					    iir, GEN8_RCS_IRQ_SHIFT);
1343e981e7b1SThomas Daniel 
13444a570db5STvrtko Ursulin 			gen8_cs_irq_handler(&dev_priv->engine[BCS],
1345fbcc1a0cSNick Hoath 					    iir, GEN8_BCS_IRQ_SHIFT);
1346abd58f01SBen Widawsky 		} else
1347abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT0)!\n");
1348abd58f01SBen Widawsky 	}
1349abd58f01SBen Widawsky 
135085f9b5f9SZhao Yakui 	if (master_ctl & (GEN8_GT_VCS1_IRQ | GEN8_GT_VCS2_IRQ)) {
13515dd280b0SNick Hoath 		u32 iir = I915_READ_FW(GEN8_GT_IIR(1));
13525dd280b0SNick Hoath 		if (iir) {
13535dd280b0SNick Hoath 			I915_WRITE_FW(GEN8_GT_IIR(1), iir);
1354abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1355e981e7b1SThomas Daniel 
13564a570db5STvrtko Ursulin 			gen8_cs_irq_handler(&dev_priv->engine[VCS],
1357fbcc1a0cSNick Hoath 					    iir, GEN8_VCS1_IRQ_SHIFT);
1358e981e7b1SThomas Daniel 
13594a570db5STvrtko Ursulin 			gen8_cs_irq_handler(&dev_priv->engine[VCS2],
1360fbcc1a0cSNick Hoath 					    iir, GEN8_VCS2_IRQ_SHIFT);
1361abd58f01SBen Widawsky 		} else
1362abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT1)!\n");
1363abd58f01SBen Widawsky 	}
1364abd58f01SBen Widawsky 
136574cdb337SChris Wilson 	if (master_ctl & GEN8_GT_VECS_IRQ) {
13665dd280b0SNick Hoath 		u32 iir = I915_READ_FW(GEN8_GT_IIR(3));
13675dd280b0SNick Hoath 		if (iir) {
13685dd280b0SNick Hoath 			I915_WRITE_FW(GEN8_GT_IIR(3), iir);
136974cdb337SChris Wilson 			ret = IRQ_HANDLED;
137074cdb337SChris Wilson 
13714a570db5STvrtko Ursulin 			gen8_cs_irq_handler(&dev_priv->engine[VECS],
1372fbcc1a0cSNick Hoath 					    iir, GEN8_VECS_IRQ_SHIFT);
137374cdb337SChris Wilson 		} else
137474cdb337SChris Wilson 			DRM_ERROR("The master control interrupt lied (GT3)!\n");
137574cdb337SChris Wilson 	}
137674cdb337SChris Wilson 
13770961021aSBen Widawsky 	if (master_ctl & GEN8_GT_PM_IRQ) {
13785dd280b0SNick Hoath 		u32 iir = I915_READ_FW(GEN8_GT_IIR(2));
13795dd280b0SNick Hoath 		if (iir & dev_priv->pm_rps_events) {
1380cb0d205eSChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(2),
13815dd280b0SNick Hoath 				      iir & dev_priv->pm_rps_events);
138238cc46d7SOscar Mateo 			ret = IRQ_HANDLED;
13835dd280b0SNick Hoath 			gen6_rps_irq_handler(dev_priv, iir);
13840961021aSBen Widawsky 		} else
13850961021aSBen Widawsky 			DRM_ERROR("The master control interrupt lied (PM)!\n");
13860961021aSBen Widawsky 	}
13870961021aSBen Widawsky 
1388abd58f01SBen Widawsky 	return ret;
1389abd58f01SBen Widawsky }
1390abd58f01SBen Widawsky 
139163c88d22SImre Deak static bool bxt_port_hotplug_long_detect(enum port port, u32 val)
139263c88d22SImre Deak {
139363c88d22SImre Deak 	switch (port) {
139463c88d22SImre Deak 	case PORT_A:
1395195baa06SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
139663c88d22SImre Deak 	case PORT_B:
139763c88d22SImre Deak 		return val & PORTB_HOTPLUG_LONG_DETECT;
139863c88d22SImre Deak 	case PORT_C:
139963c88d22SImre Deak 		return val & PORTC_HOTPLUG_LONG_DETECT;
140063c88d22SImre Deak 	default:
140163c88d22SImre Deak 		return false;
140263c88d22SImre Deak 	}
140363c88d22SImre Deak }
140463c88d22SImre Deak 
14056dbf30ceSVille Syrjälä static bool spt_port_hotplug2_long_detect(enum port port, u32 val)
14066dbf30ceSVille Syrjälä {
14076dbf30ceSVille Syrjälä 	switch (port) {
14086dbf30ceSVille Syrjälä 	case PORT_E:
14096dbf30ceSVille Syrjälä 		return val & PORTE_HOTPLUG_LONG_DETECT;
14106dbf30ceSVille Syrjälä 	default:
14116dbf30ceSVille Syrjälä 		return false;
14126dbf30ceSVille Syrjälä 	}
14136dbf30ceSVille Syrjälä }
14146dbf30ceSVille Syrjälä 
141574c0b395SVille Syrjälä static bool spt_port_hotplug_long_detect(enum port port, u32 val)
141674c0b395SVille Syrjälä {
141774c0b395SVille Syrjälä 	switch (port) {
141874c0b395SVille Syrjälä 	case PORT_A:
141974c0b395SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
142074c0b395SVille Syrjälä 	case PORT_B:
142174c0b395SVille Syrjälä 		return val & PORTB_HOTPLUG_LONG_DETECT;
142274c0b395SVille Syrjälä 	case PORT_C:
142374c0b395SVille Syrjälä 		return val & PORTC_HOTPLUG_LONG_DETECT;
142474c0b395SVille Syrjälä 	case PORT_D:
142574c0b395SVille Syrjälä 		return val & PORTD_HOTPLUG_LONG_DETECT;
142674c0b395SVille Syrjälä 	default:
142774c0b395SVille Syrjälä 		return false;
142874c0b395SVille Syrjälä 	}
142974c0b395SVille Syrjälä }
143074c0b395SVille Syrjälä 
1431e4ce95aaSVille Syrjälä static bool ilk_port_hotplug_long_detect(enum port port, u32 val)
1432e4ce95aaSVille Syrjälä {
1433e4ce95aaSVille Syrjälä 	switch (port) {
1434e4ce95aaSVille Syrjälä 	case PORT_A:
1435e4ce95aaSVille Syrjälä 		return val & DIGITAL_PORTA_HOTPLUG_LONG_DETECT;
1436e4ce95aaSVille Syrjälä 	default:
1437e4ce95aaSVille Syrjälä 		return false;
1438e4ce95aaSVille Syrjälä 	}
1439e4ce95aaSVille Syrjälä }
1440e4ce95aaSVille Syrjälä 
1441676574dfSJani Nikula static bool pch_port_hotplug_long_detect(enum port port, u32 val)
144213cf5504SDave Airlie {
144313cf5504SDave Airlie 	switch (port) {
144413cf5504SDave Airlie 	case PORT_B:
1445676574dfSJani Nikula 		return val & PORTB_HOTPLUG_LONG_DETECT;
144613cf5504SDave Airlie 	case PORT_C:
1447676574dfSJani Nikula 		return val & PORTC_HOTPLUG_LONG_DETECT;
144813cf5504SDave Airlie 	case PORT_D:
1449676574dfSJani Nikula 		return val & PORTD_HOTPLUG_LONG_DETECT;
1450676574dfSJani Nikula 	default:
1451676574dfSJani Nikula 		return false;
145213cf5504SDave Airlie 	}
145313cf5504SDave Airlie }
145413cf5504SDave Airlie 
1455676574dfSJani Nikula static bool i9xx_port_hotplug_long_detect(enum port port, u32 val)
145613cf5504SDave Airlie {
145713cf5504SDave Airlie 	switch (port) {
145813cf5504SDave Airlie 	case PORT_B:
1459676574dfSJani Nikula 		return val & PORTB_HOTPLUG_INT_LONG_PULSE;
146013cf5504SDave Airlie 	case PORT_C:
1461676574dfSJani Nikula 		return val & PORTC_HOTPLUG_INT_LONG_PULSE;
146213cf5504SDave Airlie 	case PORT_D:
1463676574dfSJani Nikula 		return val & PORTD_HOTPLUG_INT_LONG_PULSE;
1464676574dfSJani Nikula 	default:
1465676574dfSJani Nikula 		return false;
146613cf5504SDave Airlie 	}
146713cf5504SDave Airlie }
146813cf5504SDave Airlie 
146942db67d6SVille Syrjälä /*
147042db67d6SVille Syrjälä  * Get a bit mask of pins that have triggered, and which ones may be long.
147142db67d6SVille Syrjälä  * This can be called multiple times with the same masks to accumulate
147242db67d6SVille Syrjälä  * hotplug detection results from several registers.
147342db67d6SVille Syrjälä  *
147442db67d6SVille Syrjälä  * Note that the caller is expected to zero out the masks initially.
147542db67d6SVille Syrjälä  */
1476fd63e2a9SImre Deak static void intel_get_hpd_pins(u32 *pin_mask, u32 *long_mask,
14778c841e57SJani Nikula 			     u32 hotplug_trigger, u32 dig_hotplug_reg,
1478fd63e2a9SImre Deak 			     const u32 hpd[HPD_NUM_PINS],
1479fd63e2a9SImre Deak 			     bool long_pulse_detect(enum port port, u32 val))
1480676574dfSJani Nikula {
14818c841e57SJani Nikula 	enum port port;
1482676574dfSJani Nikula 	int i;
1483676574dfSJani Nikula 
1484676574dfSJani Nikula 	for_each_hpd_pin(i) {
14858c841e57SJani Nikula 		if ((hpd[i] & hotplug_trigger) == 0)
14868c841e57SJani Nikula 			continue;
14878c841e57SJani Nikula 
1488676574dfSJani Nikula 		*pin_mask |= BIT(i);
1489676574dfSJani Nikula 
1490cc24fcdcSImre Deak 		if (!intel_hpd_pin_to_port(i, &port))
1491cc24fcdcSImre Deak 			continue;
1492cc24fcdcSImre Deak 
1493fd63e2a9SImre Deak 		if (long_pulse_detect(port, dig_hotplug_reg))
1494676574dfSJani Nikula 			*long_mask |= BIT(i);
1495676574dfSJani Nikula 	}
1496676574dfSJani Nikula 
1497676574dfSJani Nikula 	DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x, dig 0x%08x, pins 0x%08x\n",
1498676574dfSJani Nikula 			 hotplug_trigger, dig_hotplug_reg, *pin_mask);
1499676574dfSJani Nikula 
1500676574dfSJani Nikula }
1501676574dfSJani Nikula 
1502515ac2bbSDaniel Vetter static void gmbus_irq_handler(struct drm_device *dev)
1503515ac2bbSDaniel Vetter {
15042d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
150528c70f16SDaniel Vetter 
150628c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1507515ac2bbSDaniel Vetter }
1508515ac2bbSDaniel Vetter 
1509ce99c256SDaniel Vetter static void dp_aux_irq_handler(struct drm_device *dev)
1510ce99c256SDaniel Vetter {
15112d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
15129ee32feaSDaniel Vetter 
15139ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1514ce99c256SDaniel Vetter }
1515ce99c256SDaniel Vetter 
15168bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
1517277de95eSDaniel Vetter static void display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1518eba94eb9SDaniel Vetter 					 uint32_t crc0, uint32_t crc1,
1519eba94eb9SDaniel Vetter 					 uint32_t crc2, uint32_t crc3,
15208bc5e955SDaniel Vetter 					 uint32_t crc4)
15218bf1e9f1SShuang He {
15228bf1e9f1SShuang He 	struct drm_i915_private *dev_priv = dev->dev_private;
15238bf1e9f1SShuang He 	struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
15248bf1e9f1SShuang He 	struct intel_pipe_crc_entry *entry;
1525ac2300d4SDamien Lespiau 	int head, tail;
1526b2c88f5bSDamien Lespiau 
1527d538bbdfSDamien Lespiau 	spin_lock(&pipe_crc->lock);
1528d538bbdfSDamien Lespiau 
15290c912c79SDamien Lespiau 	if (!pipe_crc->entries) {
1530d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
153134273620SDaniel Vetter 		DRM_DEBUG_KMS("spurious interrupt\n");
15320c912c79SDamien Lespiau 		return;
15330c912c79SDamien Lespiau 	}
15340c912c79SDamien Lespiau 
1535d538bbdfSDamien Lespiau 	head = pipe_crc->head;
1536d538bbdfSDamien Lespiau 	tail = pipe_crc->tail;
1537b2c88f5bSDamien Lespiau 
1538b2c88f5bSDamien Lespiau 	if (CIRC_SPACE(head, tail, INTEL_PIPE_CRC_ENTRIES_NR) < 1) {
1539d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
1540b2c88f5bSDamien Lespiau 		DRM_ERROR("CRC buffer overflowing\n");
1541b2c88f5bSDamien Lespiau 		return;
1542b2c88f5bSDamien Lespiau 	}
1543b2c88f5bSDamien Lespiau 
1544b2c88f5bSDamien Lespiau 	entry = &pipe_crc->entries[head];
15458bf1e9f1SShuang He 
15468bc5e955SDaniel Vetter 	entry->frame = dev->driver->get_vblank_counter(dev, pipe);
1547eba94eb9SDaniel Vetter 	entry->crc[0] = crc0;
1548eba94eb9SDaniel Vetter 	entry->crc[1] = crc1;
1549eba94eb9SDaniel Vetter 	entry->crc[2] = crc2;
1550eba94eb9SDaniel Vetter 	entry->crc[3] = crc3;
1551eba94eb9SDaniel Vetter 	entry->crc[4] = crc4;
1552b2c88f5bSDamien Lespiau 
1553b2c88f5bSDamien Lespiau 	head = (head + 1) & (INTEL_PIPE_CRC_ENTRIES_NR - 1);
1554d538bbdfSDamien Lespiau 	pipe_crc->head = head;
1555d538bbdfSDamien Lespiau 
1556d538bbdfSDamien Lespiau 	spin_unlock(&pipe_crc->lock);
155707144428SDamien Lespiau 
155807144428SDamien Lespiau 	wake_up_interruptible(&pipe_crc->wq);
15598bf1e9f1SShuang He }
1560277de95eSDaniel Vetter #else
1561277de95eSDaniel Vetter static inline void
1562277de95eSDaniel Vetter display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1563277de95eSDaniel Vetter 			     uint32_t crc0, uint32_t crc1,
1564277de95eSDaniel Vetter 			     uint32_t crc2, uint32_t crc3,
1565277de95eSDaniel Vetter 			     uint32_t crc4) {}
1566277de95eSDaniel Vetter #endif
1567eba94eb9SDaniel Vetter 
1568277de95eSDaniel Vetter 
1569277de95eSDaniel Vetter static void hsw_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
15705a69b89fSDaniel Vetter {
15715a69b89fSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
15725a69b89fSDaniel Vetter 
1573277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
15745a69b89fSDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
15755a69b89fSDaniel Vetter 				     0, 0, 0, 0);
15765a69b89fSDaniel Vetter }
15775a69b89fSDaniel Vetter 
1578277de95eSDaniel Vetter static void ivb_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
1579eba94eb9SDaniel Vetter {
1580eba94eb9SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1581eba94eb9SDaniel Vetter 
1582277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
1583eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1584eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1585eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1586eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
15878bc5e955SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1588eba94eb9SDaniel Vetter }
15895b3a856bSDaniel Vetter 
1590277de95eSDaniel Vetter static void i9xx_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
15915b3a856bSDaniel Vetter {
15925b3a856bSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
15930b5c5ed0SDaniel Vetter 	uint32_t res1, res2;
15940b5c5ed0SDaniel Vetter 
15950b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 3)
15960b5c5ed0SDaniel Vetter 		res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
15970b5c5ed0SDaniel Vetter 	else
15980b5c5ed0SDaniel Vetter 		res1 = 0;
15990b5c5ed0SDaniel Vetter 
16000b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
16010b5c5ed0SDaniel Vetter 		res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
16020b5c5ed0SDaniel Vetter 	else
16030b5c5ed0SDaniel Vetter 		res2 = 0;
16045b3a856bSDaniel Vetter 
1605277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
16060b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_RED(pipe)),
16070b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_GREEN(pipe)),
16080b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_BLUE(pipe)),
16090b5c5ed0SDaniel Vetter 				     res1, res2);
16105b3a856bSDaniel Vetter }
16118bf1e9f1SShuang He 
16121403c0d4SPaulo Zanoni /* The RPS events need forcewake, so we add them to a work queue and mask their
16131403c0d4SPaulo Zanoni  * IMR bits until the work is done. Other interrupts can be processed without
16141403c0d4SPaulo Zanoni  * the work queue. */
16151403c0d4SPaulo Zanoni static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1616baf02a1fSBen Widawsky {
1617a6706b45SDeepak S 	if (pm_iir & dev_priv->pm_rps_events) {
161859cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
1619480c8033SDaniel Vetter 		gen6_disable_pm_irq(dev_priv, pm_iir & dev_priv->pm_rps_events);
1620d4d70aa5SImre Deak 		if (dev_priv->rps.interrupts_enabled) {
1621d4d70aa5SImre Deak 			dev_priv->rps.pm_iir |= pm_iir & dev_priv->pm_rps_events;
16222adbee62SDaniel Vetter 			queue_work(dev_priv->wq, &dev_priv->rps.work);
162341a05a3aSDaniel Vetter 		}
1624d4d70aa5SImre Deak 		spin_unlock(&dev_priv->irq_lock);
1625d4d70aa5SImre Deak 	}
1626baf02a1fSBen Widawsky 
1627c9a9a268SImre Deak 	if (INTEL_INFO(dev_priv)->gen >= 8)
1628c9a9a268SImre Deak 		return;
1629c9a9a268SImre Deak 
16302d1fe073SJoonas Lahtinen 	if (HAS_VEBOX(dev_priv)) {
163112638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
16324a570db5STvrtko Ursulin 			notify_ring(&dev_priv->engine[VECS]);
163312638c57SBen Widawsky 
1634aaecdf61SDaniel Vetter 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT)
1635aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, pm_iir 0x%08x\n", pm_iir);
163612638c57SBen Widawsky 	}
16371403c0d4SPaulo Zanoni }
1638baf02a1fSBen Widawsky 
16398d7849dbSVille Syrjälä static bool intel_pipe_handle_vblank(struct drm_device *dev, enum pipe pipe)
16408d7849dbSVille Syrjälä {
16418d7849dbSVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
16428d7849dbSVille Syrjälä 		return false;
16438d7849dbSVille Syrjälä 
16448d7849dbSVille Syrjälä 	return true;
16458d7849dbSVille Syrjälä }
16468d7849dbSVille Syrjälä 
1647c1874ed7SImre Deak static void valleyview_pipestat_irq_handler(struct drm_device *dev, u32 iir)
16487e231dbeSJesse Barnes {
1649c1874ed7SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
165091d181ddSImre Deak 	u32 pipe_stats[I915_MAX_PIPES] = { };
16517e231dbeSJesse Barnes 	int pipe;
16527e231dbeSJesse Barnes 
165358ead0d7SImre Deak 	spin_lock(&dev_priv->irq_lock);
16541ca993d2SVille Syrjälä 
16551ca993d2SVille Syrjälä 	if (!dev_priv->display_irqs_enabled) {
16561ca993d2SVille Syrjälä 		spin_unlock(&dev_priv->irq_lock);
16571ca993d2SVille Syrjälä 		return;
16581ca993d2SVille Syrjälä 	}
16591ca993d2SVille Syrjälä 
1660055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1661f0f59a00SVille Syrjälä 		i915_reg_t reg;
1662bbb5eebfSDaniel Vetter 		u32 mask, iir_bit = 0;
166391d181ddSImre Deak 
1664bbb5eebfSDaniel Vetter 		/*
1665bbb5eebfSDaniel Vetter 		 * PIPESTAT bits get signalled even when the interrupt is
1666bbb5eebfSDaniel Vetter 		 * disabled with the mask bits, and some of the status bits do
1667bbb5eebfSDaniel Vetter 		 * not generate interrupts at all (like the underrun bit). Hence
1668bbb5eebfSDaniel Vetter 		 * we need to be careful that we only handle what we want to
1669bbb5eebfSDaniel Vetter 		 * handle.
1670bbb5eebfSDaniel Vetter 		 */
16710f239f4cSDaniel Vetter 
16720f239f4cSDaniel Vetter 		/* fifo underruns are filterered in the underrun handler. */
16730f239f4cSDaniel Vetter 		mask = PIPE_FIFO_UNDERRUN_STATUS;
1674bbb5eebfSDaniel Vetter 
1675bbb5eebfSDaniel Vetter 		switch (pipe) {
1676bbb5eebfSDaniel Vetter 		case PIPE_A:
1677bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
1678bbb5eebfSDaniel Vetter 			break;
1679bbb5eebfSDaniel Vetter 		case PIPE_B:
1680bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
1681bbb5eebfSDaniel Vetter 			break;
16823278f67fSVille Syrjälä 		case PIPE_C:
16833278f67fSVille Syrjälä 			iir_bit = I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
16843278f67fSVille Syrjälä 			break;
1685bbb5eebfSDaniel Vetter 		}
1686bbb5eebfSDaniel Vetter 		if (iir & iir_bit)
1687bbb5eebfSDaniel Vetter 			mask |= dev_priv->pipestat_irq_mask[pipe];
1688bbb5eebfSDaniel Vetter 
1689bbb5eebfSDaniel Vetter 		if (!mask)
169091d181ddSImre Deak 			continue;
169191d181ddSImre Deak 
169291d181ddSImre Deak 		reg = PIPESTAT(pipe);
1693bbb5eebfSDaniel Vetter 		mask |= PIPESTAT_INT_ENABLE_MASK;
1694bbb5eebfSDaniel Vetter 		pipe_stats[pipe] = I915_READ(reg) & mask;
16957e231dbeSJesse Barnes 
16967e231dbeSJesse Barnes 		/*
16977e231dbeSJesse Barnes 		 * Clear the PIPE*STAT regs before the IIR
16987e231dbeSJesse Barnes 		 */
169991d181ddSImre Deak 		if (pipe_stats[pipe] & (PIPE_FIFO_UNDERRUN_STATUS |
170091d181ddSImre Deak 					PIPESTAT_INT_STATUS_MASK))
17017e231dbeSJesse Barnes 			I915_WRITE(reg, pipe_stats[pipe]);
17027e231dbeSJesse Barnes 	}
170358ead0d7SImre Deak 	spin_unlock(&dev_priv->irq_lock);
17047e231dbeSJesse Barnes 
1705055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1706d6bbafa1SChris Wilson 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
1707d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
1708d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
170931acc7f5SJesse Barnes 
1710579a9b0eSImre Deak 		if (pipe_stats[pipe] & PLANE_FLIP_DONE_INT_STATUS_VLV) {
171131acc7f5SJesse Barnes 			intel_prepare_page_flip(dev, pipe);
171231acc7f5SJesse Barnes 			intel_finish_page_flip(dev, pipe);
171331acc7f5SJesse Barnes 		}
17144356d586SDaniel Vetter 
17154356d586SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1716277de95eSDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
17172d9d2b0bSVille Syrjälä 
17181f7247c0SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
17191f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
172031acc7f5SJesse Barnes 	}
172131acc7f5SJesse Barnes 
1722c1874ed7SImre Deak 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1723c1874ed7SImre Deak 		gmbus_irq_handler(dev);
1724c1874ed7SImre Deak }
1725c1874ed7SImre Deak 
172616c6c56bSVille Syrjälä static void i9xx_hpd_irq_handler(struct drm_device *dev)
172716c6c56bSVille Syrjälä {
172816c6c56bSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
172916c6c56bSVille Syrjälä 	u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
173042db67d6SVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
173116c6c56bSVille Syrjälä 
17320d2e4297SJani Nikula 	if (!hotplug_status)
17330d2e4297SJani Nikula 		return;
17340d2e4297SJani Nikula 
17353ff60f89SOscar Mateo 	I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
17363ff60f89SOscar Mateo 	/*
17373ff60f89SOscar Mateo 	 * Make sure hotplug status is cleared before we clear IIR, or else we
17383ff60f89SOscar Mateo 	 * may miss hotplug events.
17393ff60f89SOscar Mateo 	 */
17403ff60f89SOscar Mateo 	POSTING_READ(PORT_HOTPLUG_STAT);
17413ff60f89SOscar Mateo 
1742666a4537SWayne Boyer 	if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
174316c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
174416c6c56bSVille Syrjälä 
174558f2cf24SVille Syrjälä 		if (hotplug_trigger) {
1746fd63e2a9SImre Deak 			intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
1747fd63e2a9SImre Deak 					   hotplug_trigger, hpd_status_g4x,
1748fd63e2a9SImre Deak 					   i9xx_port_hotplug_long_detect);
174958f2cf24SVille Syrjälä 
1750676574dfSJani Nikula 			intel_hpd_irq_handler(dev, pin_mask, long_mask);
175158f2cf24SVille Syrjälä 		}
1752369712e8SJani Nikula 
1753369712e8SJani Nikula 		if (hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
1754369712e8SJani Nikula 			dp_aux_irq_handler(dev);
175516c6c56bSVille Syrjälä 	} else {
175616c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
175716c6c56bSVille Syrjälä 
175858f2cf24SVille Syrjälä 		if (hotplug_trigger) {
1759fd63e2a9SImre Deak 			intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
17604e3d1e26SVille Syrjälä 					   hotplug_trigger, hpd_status_i915,
1761fd63e2a9SImre Deak 					   i9xx_port_hotplug_long_detect);
1762676574dfSJani Nikula 			intel_hpd_irq_handler(dev, pin_mask, long_mask);
176316c6c56bSVille Syrjälä 		}
17643ff60f89SOscar Mateo 	}
176558f2cf24SVille Syrjälä }
176616c6c56bSVille Syrjälä 
1767c1874ed7SImre Deak static irqreturn_t valleyview_irq_handler(int irq, void *arg)
1768c1874ed7SImre Deak {
176945a83f84SDaniel Vetter 	struct drm_device *dev = arg;
17702d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1771c1874ed7SImre Deak 	u32 iir, gt_iir, pm_iir;
1772c1874ed7SImre Deak 	irqreturn_t ret = IRQ_NONE;
1773c1874ed7SImre Deak 
17742dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
17752dd2a883SImre Deak 		return IRQ_NONE;
17762dd2a883SImre Deak 
17771f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
17781f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
17791f814dacSImre Deak 
1780c1874ed7SImre Deak 	while (true) {
17813ff60f89SOscar Mateo 		/* Find, clear, then process each source of interrupt */
17823ff60f89SOscar Mateo 
1783c1874ed7SImre Deak 		gt_iir = I915_READ(GTIIR);
17843ff60f89SOscar Mateo 		if (gt_iir)
17853ff60f89SOscar Mateo 			I915_WRITE(GTIIR, gt_iir);
17863ff60f89SOscar Mateo 
1787c1874ed7SImre Deak 		pm_iir = I915_READ(GEN6_PMIIR);
17883ff60f89SOscar Mateo 		if (pm_iir)
17893ff60f89SOscar Mateo 			I915_WRITE(GEN6_PMIIR, pm_iir);
17903ff60f89SOscar Mateo 
17913ff60f89SOscar Mateo 		iir = I915_READ(VLV_IIR);
17923ff60f89SOscar Mateo 		if (iir) {
17933ff60f89SOscar Mateo 			/* Consume port before clearing IIR or we'll miss events */
17943ff60f89SOscar Mateo 			if (iir & I915_DISPLAY_PORT_INTERRUPT)
17953ff60f89SOscar Mateo 				i9xx_hpd_irq_handler(dev);
17963ff60f89SOscar Mateo 			I915_WRITE(VLV_IIR, iir);
17973ff60f89SOscar Mateo 		}
1798c1874ed7SImre Deak 
1799c1874ed7SImre Deak 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
1800c1874ed7SImre Deak 			goto out;
1801c1874ed7SImre Deak 
1802c1874ed7SImre Deak 		ret = IRQ_HANDLED;
1803c1874ed7SImre Deak 
18043ff60f89SOscar Mateo 		if (gt_iir)
1805c1874ed7SImre Deak 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
180660611c13SPaulo Zanoni 		if (pm_iir)
1807d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
18083ff60f89SOscar Mateo 		/* Call regardless, as some status bits might not be
18093ff60f89SOscar Mateo 		 * signalled in iir */
18103ff60f89SOscar Mateo 		valleyview_pipestat_irq_handler(dev, iir);
18117e231dbeSJesse Barnes 	}
18127e231dbeSJesse Barnes 
18137e231dbeSJesse Barnes out:
18141f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
18151f814dacSImre Deak 
18167e231dbeSJesse Barnes 	return ret;
18177e231dbeSJesse Barnes }
18187e231dbeSJesse Barnes 
181943f328d7SVille Syrjälä static irqreturn_t cherryview_irq_handler(int irq, void *arg)
182043f328d7SVille Syrjälä {
182145a83f84SDaniel Vetter 	struct drm_device *dev = arg;
182243f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
182343f328d7SVille Syrjälä 	u32 master_ctl, iir;
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 {
18338e5fd599SVille Syrjälä 		master_ctl = I915_READ(GEN8_MASTER_IRQ) & ~GEN8_MASTER_IRQ_CONTROL;
18343278f67fSVille Syrjälä 		iir = I915_READ(VLV_IIR);
18353278f67fSVille Syrjälä 
18363278f67fSVille Syrjälä 		if (master_ctl == 0 && iir == 0)
18378e5fd599SVille Syrjälä 			break;
183843f328d7SVille Syrjälä 
183927b6c122SOscar Mateo 		ret = IRQ_HANDLED;
184027b6c122SOscar Mateo 
184143f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, 0);
184243f328d7SVille Syrjälä 
184327b6c122SOscar Mateo 		/* Find, clear, then process each source of interrupt */
184427b6c122SOscar Mateo 
184527b6c122SOscar Mateo 		if (iir) {
184627b6c122SOscar Mateo 			/* Consume port before clearing IIR or we'll miss events */
184727b6c122SOscar Mateo 			if (iir & I915_DISPLAY_PORT_INTERRUPT)
184827b6c122SOscar Mateo 				i9xx_hpd_irq_handler(dev);
184927b6c122SOscar Mateo 			I915_WRITE(VLV_IIR, iir);
185027b6c122SOscar Mateo 		}
185127b6c122SOscar Mateo 
185274cdb337SChris Wilson 		gen8_gt_irq_handler(dev_priv, master_ctl);
185343f328d7SVille Syrjälä 
185427b6c122SOscar Mateo 		/* Call regardless, as some status bits might not be
185527b6c122SOscar Mateo 		 * signalled in iir */
18563278f67fSVille Syrjälä 		valleyview_pipestat_irq_handler(dev, iir);
185743f328d7SVille Syrjälä 
185843f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
185943f328d7SVille Syrjälä 		POSTING_READ(GEN8_MASTER_IRQ);
1860579de73bSChris Wilson 	} while (0);
18613278f67fSVille Syrjälä 
18621f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
18631f814dacSImre Deak 
186443f328d7SVille Syrjälä 	return ret;
186543f328d7SVille Syrjälä }
186643f328d7SVille Syrjälä 
186740e56410SVille Syrjälä static void ibx_hpd_irq_handler(struct drm_device *dev, u32 hotplug_trigger,
186840e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
1869776ad806SJesse Barnes {
187040e56410SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(dev);
187142db67d6SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
1872776ad806SJesse Barnes 
18736a39d7c9SJani Nikula 	/*
18746a39d7c9SJani Nikula 	 * Somehow the PCH doesn't seem to really ack the interrupt to the CPU
18756a39d7c9SJani Nikula 	 * unless we touch the hotplug register, even if hotplug_trigger is
18766a39d7c9SJani Nikula 	 * zero. Not acking leads to "The master control interrupt lied (SDE)!"
18776a39d7c9SJani Nikula 	 * errors.
18786a39d7c9SJani Nikula 	 */
187913cf5504SDave Airlie 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
18806a39d7c9SJani Nikula 	if (!hotplug_trigger) {
18816a39d7c9SJani Nikula 		u32 mask = PORTA_HOTPLUG_STATUS_MASK |
18826a39d7c9SJani Nikula 			PORTD_HOTPLUG_STATUS_MASK |
18836a39d7c9SJani Nikula 			PORTC_HOTPLUG_STATUS_MASK |
18846a39d7c9SJani Nikula 			PORTB_HOTPLUG_STATUS_MASK;
18856a39d7c9SJani Nikula 		dig_hotplug_reg &= ~mask;
18866a39d7c9SJani Nikula 	}
18876a39d7c9SJani Nikula 
188813cf5504SDave Airlie 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
18896a39d7c9SJani Nikula 	if (!hotplug_trigger)
18906a39d7c9SJani Nikula 		return;
189113cf5504SDave Airlie 
1892fd63e2a9SImre Deak 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
189340e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
1894fd63e2a9SImre Deak 			   pch_port_hotplug_long_detect);
189540e56410SVille Syrjälä 
1896676574dfSJani Nikula 	intel_hpd_irq_handler(dev, pin_mask, long_mask);
1897aaf5ec2eSSonika Jindal }
189891d131d2SDaniel Vetter 
189940e56410SVille Syrjälä static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
190040e56410SVille Syrjälä {
190140e56410SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
190240e56410SVille Syrjälä 	int pipe;
190340e56410SVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
190440e56410SVille Syrjälä 
190540e56410SVille Syrjälä 	ibx_hpd_irq_handler(dev, hotplug_trigger, hpd_ibx);
190640e56410SVille Syrjälä 
1907cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1908cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1909776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1910cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1911cfc33bf7SVille Syrjälä 				 port_name(port));
1912cfc33bf7SVille Syrjälä 	}
1913776ad806SJesse Barnes 
1914ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
1915ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1916ce99c256SDaniel Vetter 
1917776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
1918515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
1919776ad806SJesse Barnes 
1920776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1921776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1922776ad806SJesse Barnes 
1923776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1924776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1925776ad806SJesse Barnes 
1926776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1927776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
1928776ad806SJesse Barnes 
19299db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
1930055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
19319db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
19329db4a9c7SJesse Barnes 					 pipe_name(pipe),
19339db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
1934776ad806SJesse Barnes 
1935776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1936776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1937776ad806SJesse Barnes 
1938776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1939776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1940776ad806SJesse Barnes 
1941776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
19421f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
19438664281bSPaulo Zanoni 
19448664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
19451f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
19468664281bSPaulo Zanoni }
19478664281bSPaulo Zanoni 
19488664281bSPaulo Zanoni static void ivb_err_int_handler(struct drm_device *dev)
19498664281bSPaulo Zanoni {
19508664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
19518664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
19525a69b89fSDaniel Vetter 	enum pipe pipe;
19538664281bSPaulo Zanoni 
1954de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
1955de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1956de032bf4SPaulo Zanoni 
1957055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
19581f7247c0SDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe))
19591f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
19608664281bSPaulo Zanoni 
19615a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
19625a69b89fSDaniel Vetter 			if (IS_IVYBRIDGE(dev))
1963277de95eSDaniel Vetter 				ivb_pipe_crc_irq_handler(dev, pipe);
19645a69b89fSDaniel Vetter 			else
1965277de95eSDaniel Vetter 				hsw_pipe_crc_irq_handler(dev, pipe);
19665a69b89fSDaniel Vetter 		}
19675a69b89fSDaniel Vetter 	}
19688bf1e9f1SShuang He 
19698664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
19708664281bSPaulo Zanoni }
19718664281bSPaulo Zanoni 
19728664281bSPaulo Zanoni static void cpt_serr_int_handler(struct drm_device *dev)
19738664281bSPaulo Zanoni {
19748664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
19758664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
19768664281bSPaulo Zanoni 
1977de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
1978de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
1979de032bf4SPaulo Zanoni 
19808664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
19811f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
19828664281bSPaulo Zanoni 
19838664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
19841f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
19858664281bSPaulo Zanoni 
19868664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
19871f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_C);
19888664281bSPaulo Zanoni 
19898664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
1990776ad806SJesse Barnes }
1991776ad806SJesse Barnes 
199223e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
199323e81d69SAdam Jackson {
19942d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
199523e81d69SAdam Jackson 	int pipe;
19966dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
1997aaf5ec2eSSonika Jindal 
199840e56410SVille Syrjälä 	ibx_hpd_irq_handler(dev, hotplug_trigger, hpd_cpt);
199991d131d2SDaniel Vetter 
2000cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
2001cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
200223e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
2003cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
2004cfc33bf7SVille Syrjälä 				 port_name(port));
2005cfc33bf7SVille Syrjälä 	}
200623e81d69SAdam Jackson 
200723e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
2008ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
200923e81d69SAdam Jackson 
201023e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
2011515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
201223e81d69SAdam Jackson 
201323e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
201423e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
201523e81d69SAdam Jackson 
201623e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
201723e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
201823e81d69SAdam Jackson 
201923e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
2020055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
202123e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
202223e81d69SAdam Jackson 					 pipe_name(pipe),
202323e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
20248664281bSPaulo Zanoni 
20258664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
20268664281bSPaulo Zanoni 		cpt_serr_int_handler(dev);
202723e81d69SAdam Jackson }
202823e81d69SAdam Jackson 
20296dbf30ceSVille Syrjälä static void spt_irq_handler(struct drm_device *dev, u32 pch_iir)
20306dbf30ceSVille Syrjälä {
20316dbf30ceSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
20326dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_SPT &
20336dbf30ceSVille Syrjälä 		~SDE_PORTE_HOTPLUG_SPT;
20346dbf30ceSVille Syrjälä 	u32 hotplug2_trigger = pch_iir & SDE_PORTE_HOTPLUG_SPT;
20356dbf30ceSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
20366dbf30ceSVille Syrjälä 
20376dbf30ceSVille Syrjälä 	if (hotplug_trigger) {
20386dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
20396dbf30ceSVille Syrjälä 
20406dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
20416dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
20426dbf30ceSVille Syrjälä 
20436dbf30ceSVille Syrjälä 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
20446dbf30ceSVille Syrjälä 				   dig_hotplug_reg, hpd_spt,
204574c0b395SVille Syrjälä 				   spt_port_hotplug_long_detect);
20466dbf30ceSVille Syrjälä 	}
20476dbf30ceSVille Syrjälä 
20486dbf30ceSVille Syrjälä 	if (hotplug2_trigger) {
20496dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
20506dbf30ceSVille Syrjälä 
20516dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG2);
20526dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG2, dig_hotplug_reg);
20536dbf30ceSVille Syrjälä 
20546dbf30ceSVille Syrjälä 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug2_trigger,
20556dbf30ceSVille Syrjälä 				   dig_hotplug_reg, hpd_spt,
20566dbf30ceSVille Syrjälä 				   spt_port_hotplug2_long_detect);
20576dbf30ceSVille Syrjälä 	}
20586dbf30ceSVille Syrjälä 
20596dbf30ceSVille Syrjälä 	if (pin_mask)
20606dbf30ceSVille Syrjälä 		intel_hpd_irq_handler(dev, pin_mask, long_mask);
20616dbf30ceSVille Syrjälä 
20626dbf30ceSVille Syrjälä 	if (pch_iir & SDE_GMBUS_CPT)
20636dbf30ceSVille Syrjälä 		gmbus_irq_handler(dev);
20646dbf30ceSVille Syrjälä }
20656dbf30ceSVille Syrjälä 
206640e56410SVille Syrjälä static void ilk_hpd_irq_handler(struct drm_device *dev, u32 hotplug_trigger,
206740e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2068c008bc6eSPaulo Zanoni {
206940e56410SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(dev);
2070e4ce95aaSVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2071e4ce95aaSVille Syrjälä 
2072e4ce95aaSVille Syrjälä 	dig_hotplug_reg = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
2073e4ce95aaSVille Syrjälä 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, dig_hotplug_reg);
2074e4ce95aaSVille Syrjälä 
2075e4ce95aaSVille Syrjälä 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
207640e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2077e4ce95aaSVille Syrjälä 			   ilk_port_hotplug_long_detect);
207840e56410SVille Syrjälä 
2079e4ce95aaSVille Syrjälä 	intel_hpd_irq_handler(dev, pin_mask, long_mask);
2080e4ce95aaSVille Syrjälä }
2081c008bc6eSPaulo Zanoni 
208240e56410SVille Syrjälä static void ilk_display_irq_handler(struct drm_device *dev, u32 de_iir)
208340e56410SVille Syrjälä {
208440e56410SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
208540e56410SVille Syrjälä 	enum pipe pipe;
208640e56410SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG;
208740e56410SVille Syrjälä 
208840e56410SVille Syrjälä 	if (hotplug_trigger)
208940e56410SVille Syrjälä 		ilk_hpd_irq_handler(dev, hotplug_trigger, hpd_ilk);
209040e56410SVille Syrjälä 
2091c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
2092c008bc6eSPaulo Zanoni 		dp_aux_irq_handler(dev);
2093c008bc6eSPaulo Zanoni 
2094c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
2095c008bc6eSPaulo Zanoni 		intel_opregion_asle_intr(dev);
2096c008bc6eSPaulo Zanoni 
2097c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
2098c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
2099c008bc6eSPaulo Zanoni 
2100055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2101d6bbafa1SChris Wilson 		if (de_iir & DE_PIPE_VBLANK(pipe) &&
2102d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
2103d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
2104c008bc6eSPaulo Zanoni 
210540da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
21061f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
2107c008bc6eSPaulo Zanoni 
210840da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
210940da17c2SDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
21105b3a856bSDaniel Vetter 
211140da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
211240da17c2SDaniel Vetter 		if (de_iir & DE_PLANE_FLIP_DONE(pipe)) {
211340da17c2SDaniel Vetter 			intel_prepare_page_flip(dev, pipe);
211440da17c2SDaniel Vetter 			intel_finish_page_flip_plane(dev, pipe);
2115c008bc6eSPaulo Zanoni 		}
2116c008bc6eSPaulo Zanoni 	}
2117c008bc6eSPaulo Zanoni 
2118c008bc6eSPaulo Zanoni 	/* check event from PCH */
2119c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
2120c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
2121c008bc6eSPaulo Zanoni 
2122c008bc6eSPaulo Zanoni 		if (HAS_PCH_CPT(dev))
2123c008bc6eSPaulo Zanoni 			cpt_irq_handler(dev, pch_iir);
2124c008bc6eSPaulo Zanoni 		else
2125c008bc6eSPaulo Zanoni 			ibx_irq_handler(dev, pch_iir);
2126c008bc6eSPaulo Zanoni 
2127c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
2128c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
2129c008bc6eSPaulo Zanoni 	}
2130c008bc6eSPaulo Zanoni 
2131c008bc6eSPaulo Zanoni 	if (IS_GEN5(dev) && de_iir & DE_PCU_EVENT)
2132c008bc6eSPaulo Zanoni 		ironlake_rps_change_irq_handler(dev);
2133c008bc6eSPaulo Zanoni }
2134c008bc6eSPaulo Zanoni 
21359719fb98SPaulo Zanoni static void ivb_display_irq_handler(struct drm_device *dev, u32 de_iir)
21369719fb98SPaulo Zanoni {
21379719fb98SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
213807d27e20SDamien Lespiau 	enum pipe pipe;
213923bb4cb5SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG_IVB;
214023bb4cb5SVille Syrjälä 
214140e56410SVille Syrjälä 	if (hotplug_trigger)
214240e56410SVille Syrjälä 		ilk_hpd_irq_handler(dev, hotplug_trigger, hpd_ivb);
21439719fb98SPaulo Zanoni 
21449719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
21459719fb98SPaulo Zanoni 		ivb_err_int_handler(dev);
21469719fb98SPaulo Zanoni 
21479719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
21489719fb98SPaulo Zanoni 		dp_aux_irq_handler(dev);
21499719fb98SPaulo Zanoni 
21509719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
21519719fb98SPaulo Zanoni 		intel_opregion_asle_intr(dev);
21529719fb98SPaulo Zanoni 
2153055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2154d6bbafa1SChris Wilson 		if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)) &&
2155d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
2156d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
215740da17c2SDaniel Vetter 
215840da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
215907d27e20SDamien Lespiau 		if (de_iir & DE_PLANE_FLIP_DONE_IVB(pipe)) {
216007d27e20SDamien Lespiau 			intel_prepare_page_flip(dev, pipe);
216107d27e20SDamien Lespiau 			intel_finish_page_flip_plane(dev, pipe);
21629719fb98SPaulo Zanoni 		}
21639719fb98SPaulo Zanoni 	}
21649719fb98SPaulo Zanoni 
21659719fb98SPaulo Zanoni 	/* check event from PCH */
21669719fb98SPaulo Zanoni 	if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
21679719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
21689719fb98SPaulo Zanoni 
21699719fb98SPaulo Zanoni 		cpt_irq_handler(dev, pch_iir);
21709719fb98SPaulo Zanoni 
21719719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
21729719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
21739719fb98SPaulo Zanoni 	}
21749719fb98SPaulo Zanoni }
21759719fb98SPaulo Zanoni 
217672c90f62SOscar Mateo /*
217772c90f62SOscar Mateo  * To handle irqs with the minimum potential races with fresh interrupts, we:
217872c90f62SOscar Mateo  * 1 - Disable Master Interrupt Control.
217972c90f62SOscar Mateo  * 2 - Find the source(s) of the interrupt.
218072c90f62SOscar Mateo  * 3 - Clear the Interrupt Identity bits (IIR).
218172c90f62SOscar Mateo  * 4 - Process the interrupt(s) that had bits set in the IIRs.
218272c90f62SOscar Mateo  * 5 - Re-enable Master Interrupt Control.
218372c90f62SOscar Mateo  */
2184f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
2185b1f14ad0SJesse Barnes {
218645a83f84SDaniel Vetter 	struct drm_device *dev = arg;
21872d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2188f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
21890e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
2190b1f14ad0SJesse Barnes 
21912dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
21922dd2a883SImre Deak 		return IRQ_NONE;
21932dd2a883SImre Deak 
21941f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
21951f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
21961f814dacSImre Deak 
2197b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
2198b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
2199b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
220023a78516SPaulo Zanoni 	POSTING_READ(DEIER);
22010e43406bSChris Wilson 
220244498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
220344498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
220444498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
220544498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
220644498aeaSPaulo Zanoni 	 * due to its back queue). */
2207ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
220844498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
220944498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
221044498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2211ab5c608bSBen Widawsky 	}
221244498aeaSPaulo Zanoni 
221372c90f62SOscar Mateo 	/* Find, clear, then process each source of interrupt */
221472c90f62SOscar Mateo 
22150e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
22160e43406bSChris Wilson 	if (gt_iir) {
221772c90f62SOscar Mateo 		I915_WRITE(GTIIR, gt_iir);
221872c90f62SOscar Mateo 		ret = IRQ_HANDLED;
2219d8fc8a47SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 6)
22200e43406bSChris Wilson 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
2221d8fc8a47SPaulo Zanoni 		else
2222d8fc8a47SPaulo Zanoni 			ilk_gt_irq_handler(dev, dev_priv, gt_iir);
22230e43406bSChris Wilson 	}
2224b1f14ad0SJesse Barnes 
2225b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
22260e43406bSChris Wilson 	if (de_iir) {
222772c90f62SOscar Mateo 		I915_WRITE(DEIIR, de_iir);
222872c90f62SOscar Mateo 		ret = IRQ_HANDLED;
2229f1af8fc1SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 7)
22309719fb98SPaulo Zanoni 			ivb_display_irq_handler(dev, de_iir);
2231f1af8fc1SPaulo Zanoni 		else
2232f1af8fc1SPaulo Zanoni 			ilk_display_irq_handler(dev, de_iir);
22330e43406bSChris Wilson 	}
22340e43406bSChris Wilson 
2235f1af8fc1SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6) {
2236f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
22370e43406bSChris Wilson 		if (pm_iir) {
2238b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
22390e43406bSChris Wilson 			ret = IRQ_HANDLED;
224072c90f62SOscar Mateo 			gen6_rps_irq_handler(dev_priv, pm_iir);
22410e43406bSChris Wilson 		}
2242f1af8fc1SPaulo Zanoni 	}
2243b1f14ad0SJesse Barnes 
2244b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
2245b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
2246ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
224744498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
224844498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2249ab5c608bSBen Widawsky 	}
2250b1f14ad0SJesse Barnes 
22511f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
22521f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
22531f814dacSImre Deak 
2254b1f14ad0SJesse Barnes 	return ret;
2255b1f14ad0SJesse Barnes }
2256b1f14ad0SJesse Barnes 
225740e56410SVille Syrjälä static void bxt_hpd_irq_handler(struct drm_device *dev, u32 hotplug_trigger,
225840e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2259d04a492dSShashank Sharma {
2260cebd87a0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(dev);
2261cebd87a0SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2262d04a492dSShashank Sharma 
2263a52bb15bSVille Syrjälä 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
2264a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
2265d04a492dSShashank Sharma 
2266cebd87a0SVille Syrjälä 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
226740e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2268cebd87a0SVille Syrjälä 			   bxt_port_hotplug_long_detect);
226940e56410SVille Syrjälä 
2270475c2e3bSJani Nikula 	intel_hpd_irq_handler(dev, pin_mask, long_mask);
2271d04a492dSShashank Sharma }
2272d04a492dSShashank Sharma 
2273f11a0f46STvrtko Ursulin static irqreturn_t
2274f11a0f46STvrtko Ursulin gen8_de_irq_handler(struct drm_i915_private *dev_priv, u32 master_ctl)
2275abd58f01SBen Widawsky {
2276f11a0f46STvrtko Ursulin 	struct drm_device *dev = dev_priv->dev;
2277abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
2278f11a0f46STvrtko Ursulin 	u32 iir;
2279c42664ccSDaniel Vetter 	enum pipe pipe;
228088e04703SJesse Barnes 
2281abd58f01SBen Widawsky 	if (master_ctl & GEN8_DE_MISC_IRQ) {
2282e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_MISC_IIR);
2283e32192e1STvrtko Ursulin 		if (iir) {
2284e32192e1STvrtko Ursulin 			I915_WRITE(GEN8_DE_MISC_IIR, iir);
2285abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
2286e32192e1STvrtko Ursulin 			if (iir & GEN8_DE_MISC_GSE)
228738cc46d7SOscar Mateo 				intel_opregion_asle_intr(dev);
228838cc46d7SOscar Mateo 			else
228938cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Misc interrupt\n");
2290abd58f01SBen Widawsky 		}
229138cc46d7SOscar Mateo 		else
229238cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
2293abd58f01SBen Widawsky 	}
2294abd58f01SBen Widawsky 
22956d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
2296e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_PORT_IIR);
2297e32192e1STvrtko Ursulin 		if (iir) {
2298e32192e1STvrtko Ursulin 			u32 tmp_mask;
2299d04a492dSShashank Sharma 			bool found = false;
2300cebd87a0SVille Syrjälä 
2301e32192e1STvrtko Ursulin 			I915_WRITE(GEN8_DE_PORT_IIR, iir);
23026d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
230388e04703SJesse Barnes 
2304e32192e1STvrtko Ursulin 			tmp_mask = GEN8_AUX_CHANNEL_A;
2305e32192e1STvrtko Ursulin 			if (INTEL_INFO(dev_priv)->gen >= 9)
2306e32192e1STvrtko Ursulin 				tmp_mask |= GEN9_AUX_CHANNEL_B |
2307e32192e1STvrtko Ursulin 					    GEN9_AUX_CHANNEL_C |
2308e32192e1STvrtko Ursulin 					    GEN9_AUX_CHANNEL_D;
2309e32192e1STvrtko Ursulin 
2310e32192e1STvrtko Ursulin 			if (iir & tmp_mask) {
231138cc46d7SOscar Mateo 				dp_aux_irq_handler(dev);
2312d04a492dSShashank Sharma 				found = true;
2313d04a492dSShashank Sharma 			}
2314d04a492dSShashank Sharma 
2315e32192e1STvrtko Ursulin 			if (IS_BROXTON(dev_priv)) {
2316e32192e1STvrtko Ursulin 				tmp_mask = iir & BXT_DE_PORT_HOTPLUG_MASK;
2317e32192e1STvrtko Ursulin 				if (tmp_mask) {
2318e32192e1STvrtko Ursulin 					bxt_hpd_irq_handler(dev, tmp_mask, hpd_bxt);
2319d04a492dSShashank Sharma 					found = true;
2320d04a492dSShashank Sharma 				}
2321e32192e1STvrtko Ursulin 			} else if (IS_BROADWELL(dev_priv)) {
2322e32192e1STvrtko Ursulin 				tmp_mask = iir & GEN8_PORT_DP_A_HOTPLUG;
2323e32192e1STvrtko Ursulin 				if (tmp_mask) {
2324e32192e1STvrtko Ursulin 					ilk_hpd_irq_handler(dev, tmp_mask, hpd_bdw);
2325e32192e1STvrtko Ursulin 					found = true;
2326e32192e1STvrtko Ursulin 				}
2327e32192e1STvrtko Ursulin 			}
2328d04a492dSShashank Sharma 
2329e32192e1STvrtko Ursulin 			if (IS_BROXTON(dev) && (iir & BXT_DE_PORT_GMBUS)) {
23309e63743eSShashank Sharma 				gmbus_irq_handler(dev);
23319e63743eSShashank Sharma 				found = true;
23329e63743eSShashank Sharma 			}
23339e63743eSShashank Sharma 
2334d04a492dSShashank Sharma 			if (!found)
233538cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Port interrupt\n");
23366d766f02SDaniel Vetter 		}
233738cc46d7SOscar Mateo 		else
233838cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
23396d766f02SDaniel Vetter 	}
23406d766f02SDaniel Vetter 
2341055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2342e32192e1STvrtko Ursulin 		u32 flip_done, fault_errors;
2343abd58f01SBen Widawsky 
2344c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2345c42664ccSDaniel Vetter 			continue;
2346c42664ccSDaniel Vetter 
2347e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
2348e32192e1STvrtko Ursulin 		if (!iir) {
2349e32192e1STvrtko Ursulin 			DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
2350e32192e1STvrtko Ursulin 			continue;
2351e32192e1STvrtko Ursulin 		}
2352770de83dSDamien Lespiau 
2353e32192e1STvrtko Ursulin 		ret = IRQ_HANDLED;
2354e32192e1STvrtko Ursulin 		I915_WRITE(GEN8_DE_PIPE_IIR(pipe), iir);
2355e32192e1STvrtko Ursulin 
2356e32192e1STvrtko Ursulin 		if (iir & GEN8_PIPE_VBLANK &&
2357d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
2358d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
2359abd58f01SBen Widawsky 
2360e32192e1STvrtko Ursulin 		flip_done = iir;
2361b4834a50SRodrigo Vivi 		if (INTEL_INFO(dev_priv)->gen >= 9)
2362e32192e1STvrtko Ursulin 			flip_done &= GEN9_PIPE_PLANE1_FLIP_DONE;
2363770de83dSDamien Lespiau 		else
2364e32192e1STvrtko Ursulin 			flip_done &= GEN8_PIPE_PRIMARY_FLIP_DONE;
2365770de83dSDamien Lespiau 
2366770de83dSDamien Lespiau 		if (flip_done) {
2367abd58f01SBen Widawsky 			intel_prepare_page_flip(dev, pipe);
2368abd58f01SBen Widawsky 			intel_finish_page_flip_plane(dev, pipe);
2369abd58f01SBen Widawsky 		}
2370abd58f01SBen Widawsky 
2371e32192e1STvrtko Ursulin 		if (iir & GEN8_PIPE_CDCLK_CRC_DONE)
23720fbe7870SDaniel Vetter 			hsw_pipe_crc_irq_handler(dev, pipe);
23730fbe7870SDaniel Vetter 
2374e32192e1STvrtko Ursulin 		if (iir & GEN8_PIPE_FIFO_UNDERRUN)
2375e32192e1STvrtko Ursulin 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
237638d83c96SDaniel Vetter 
2377e32192e1STvrtko Ursulin 		fault_errors = iir;
2378b4834a50SRodrigo Vivi 		if (INTEL_INFO(dev_priv)->gen >= 9)
2379e32192e1STvrtko Ursulin 			fault_errors &= GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
2380770de83dSDamien Lespiau 		else
2381e32192e1STvrtko Ursulin 			fault_errors &= GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
2382770de83dSDamien Lespiau 
2383770de83dSDamien Lespiau 		if (fault_errors)
238430100f2bSDaniel Vetter 			DRM_ERROR("Fault errors on pipe %c\n: 0x%08x",
238530100f2bSDaniel Vetter 				  pipe_name(pipe),
2386e32192e1STvrtko Ursulin 				  fault_errors);
2387abd58f01SBen Widawsky 	}
2388abd58f01SBen Widawsky 
2389266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev) && !HAS_PCH_NOP(dev) &&
2390266ea3d9SShashank Sharma 	    master_ctl & GEN8_DE_PCH_IRQ) {
239192d03a80SDaniel Vetter 		/*
239292d03a80SDaniel Vetter 		 * FIXME(BDW): Assume for now that the new interrupt handling
239392d03a80SDaniel Vetter 		 * scheme also closed the SDE interrupt handling race we've seen
239492d03a80SDaniel Vetter 		 * on older pch-split platforms. But this needs testing.
239592d03a80SDaniel Vetter 		 */
2396e32192e1STvrtko Ursulin 		iir = I915_READ(SDEIIR);
2397e32192e1STvrtko Ursulin 		if (iir) {
2398e32192e1STvrtko Ursulin 			I915_WRITE(SDEIIR, iir);
239992d03a80SDaniel Vetter 			ret = IRQ_HANDLED;
24006dbf30ceSVille Syrjälä 
24016dbf30ceSVille Syrjälä 			if (HAS_PCH_SPT(dev_priv))
2402e32192e1STvrtko Ursulin 				spt_irq_handler(dev, iir);
24036dbf30ceSVille Syrjälä 			else
2404e32192e1STvrtko Ursulin 				cpt_irq_handler(dev, iir);
24052dfb0b81SJani Nikula 		} else {
24062dfb0b81SJani Nikula 			/*
24072dfb0b81SJani Nikula 			 * Like on previous PCH there seems to be something
24082dfb0b81SJani Nikula 			 * fishy going on with forwarding PCH interrupts.
24092dfb0b81SJani Nikula 			 */
24102dfb0b81SJani Nikula 			DRM_DEBUG_DRIVER("The master control interrupt lied (SDE)!\n");
24112dfb0b81SJani Nikula 		}
241292d03a80SDaniel Vetter 	}
241392d03a80SDaniel Vetter 
2414f11a0f46STvrtko Ursulin 	return ret;
2415f11a0f46STvrtko Ursulin }
2416f11a0f46STvrtko Ursulin 
2417f11a0f46STvrtko Ursulin static irqreturn_t gen8_irq_handler(int irq, void *arg)
2418f11a0f46STvrtko Ursulin {
2419f11a0f46STvrtko Ursulin 	struct drm_device *dev = arg;
2420f11a0f46STvrtko Ursulin 	struct drm_i915_private *dev_priv = dev->dev_private;
2421f11a0f46STvrtko Ursulin 	u32 master_ctl;
2422f11a0f46STvrtko Ursulin 	irqreturn_t ret;
2423f11a0f46STvrtko Ursulin 
2424f11a0f46STvrtko Ursulin 	if (!intel_irqs_enabled(dev_priv))
2425f11a0f46STvrtko Ursulin 		return IRQ_NONE;
2426f11a0f46STvrtko Ursulin 
2427f11a0f46STvrtko Ursulin 	master_ctl = I915_READ_FW(GEN8_MASTER_IRQ);
2428f11a0f46STvrtko Ursulin 	master_ctl &= ~GEN8_MASTER_IRQ_CONTROL;
2429f11a0f46STvrtko Ursulin 	if (!master_ctl)
2430f11a0f46STvrtko Ursulin 		return IRQ_NONE;
2431f11a0f46STvrtko Ursulin 
2432f11a0f46STvrtko Ursulin 	I915_WRITE_FW(GEN8_MASTER_IRQ, 0);
2433f11a0f46STvrtko Ursulin 
2434f11a0f46STvrtko Ursulin 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
2435f11a0f46STvrtko Ursulin 	disable_rpm_wakeref_asserts(dev_priv);
2436f11a0f46STvrtko Ursulin 
2437f11a0f46STvrtko Ursulin 	/* Find, clear, then process each source of interrupt */
2438f11a0f46STvrtko Ursulin 	ret = gen8_gt_irq_handler(dev_priv, master_ctl);
2439f11a0f46STvrtko Ursulin 	ret |= gen8_de_irq_handler(dev_priv, master_ctl);
2440f11a0f46STvrtko Ursulin 
2441cb0d205eSChris Wilson 	I915_WRITE_FW(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
2442cb0d205eSChris Wilson 	POSTING_READ_FW(GEN8_MASTER_IRQ);
2443abd58f01SBen Widawsky 
24441f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
24451f814dacSImre Deak 
2446abd58f01SBen Widawsky 	return ret;
2447abd58f01SBen Widawsky }
2448abd58f01SBen Widawsky 
244917e1df07SDaniel Vetter static void i915_error_wake_up(struct drm_i915_private *dev_priv,
245017e1df07SDaniel Vetter 			       bool reset_completed)
245117e1df07SDaniel Vetter {
2452e2f80391STvrtko Ursulin 	struct intel_engine_cs *engine;
245317e1df07SDaniel Vetter 
245417e1df07SDaniel Vetter 	/*
245517e1df07SDaniel Vetter 	 * Notify all waiters for GPU completion events that reset state has
245617e1df07SDaniel Vetter 	 * been changed, and that they need to restart their wait after
245717e1df07SDaniel Vetter 	 * checking for potential errors (and bail out to drop locks if there is
245817e1df07SDaniel Vetter 	 * a gpu reset pending so that i915_error_work_func can acquire them).
245917e1df07SDaniel Vetter 	 */
246017e1df07SDaniel Vetter 
246117e1df07SDaniel Vetter 	/* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
2462b4ac5afcSDave Gordon 	for_each_engine(engine, dev_priv)
2463e2f80391STvrtko Ursulin 		wake_up_all(&engine->irq_queue);
246417e1df07SDaniel Vetter 
246517e1df07SDaniel Vetter 	/* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
246617e1df07SDaniel Vetter 	wake_up_all(&dev_priv->pending_flip_queue);
246717e1df07SDaniel Vetter 
246817e1df07SDaniel Vetter 	/*
246917e1df07SDaniel Vetter 	 * Signal tasks blocked in i915_gem_wait_for_error that the pending
247017e1df07SDaniel Vetter 	 * reset state is cleared.
247117e1df07SDaniel Vetter 	 */
247217e1df07SDaniel Vetter 	if (reset_completed)
247317e1df07SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
247417e1df07SDaniel Vetter }
247517e1df07SDaniel Vetter 
24768a905236SJesse Barnes /**
2477b8d24a06SMika Kuoppala  * i915_reset_and_wakeup - do process context error handling work
2478468f9d29SJavier Martinez Canillas  * @dev: drm device
24798a905236SJesse Barnes  *
24808a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
24818a905236SJesse Barnes  * was detected.
24828a905236SJesse Barnes  */
2483b8d24a06SMika Kuoppala static void i915_reset_and_wakeup(struct drm_device *dev)
24848a905236SJesse Barnes {
2485b8d24a06SMika Kuoppala 	struct drm_i915_private *dev_priv = to_i915(dev);
2486b8d24a06SMika Kuoppala 	struct i915_gpu_error *error = &dev_priv->gpu_error;
2487cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
2488cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
2489cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
249017e1df07SDaniel Vetter 	int ret;
24918a905236SJesse Barnes 
24925bdebb18SDave Airlie 	kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, error_event);
24938a905236SJesse Barnes 
24947db0ba24SDaniel Vetter 	/*
24957db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
24967db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
24977db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
24987db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
24997db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
25007db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
25017db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
25027db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
25037db0ba24SDaniel Vetter 	 */
25047db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
250544d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
25065bdebb18SDave Airlie 		kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE,
25077db0ba24SDaniel Vetter 				   reset_event);
25081f83fee0SDaniel Vetter 
250917e1df07SDaniel Vetter 		/*
2510f454c694SImre Deak 		 * In most cases it's guaranteed that we get here with an RPM
2511f454c694SImre Deak 		 * reference held, for example because there is a pending GPU
2512f454c694SImre Deak 		 * request that won't finish until the reset is done. This
2513f454c694SImre Deak 		 * isn't the case at least when we get here by doing a
2514f454c694SImre Deak 		 * simulated reset via debugs, so get an RPM reference.
2515f454c694SImre Deak 		 */
2516f454c694SImre Deak 		intel_runtime_pm_get(dev_priv);
25177514747dSVille Syrjälä 
25187514747dSVille Syrjälä 		intel_prepare_reset(dev);
25197514747dSVille Syrjälä 
2520f454c694SImre Deak 		/*
252117e1df07SDaniel Vetter 		 * All state reset _must_ be completed before we update the
252217e1df07SDaniel Vetter 		 * reset counter, for otherwise waiters might miss the reset
252317e1df07SDaniel Vetter 		 * pending state and not properly drop locks, resulting in
252417e1df07SDaniel Vetter 		 * deadlocks with the reset work.
252517e1df07SDaniel Vetter 		 */
2526f69061beSDaniel Vetter 		ret = i915_reset(dev);
2527f69061beSDaniel Vetter 
25287514747dSVille Syrjälä 		intel_finish_reset(dev);
252917e1df07SDaniel Vetter 
2530f454c694SImre Deak 		intel_runtime_pm_put(dev_priv);
2531f454c694SImre Deak 
2532f69061beSDaniel Vetter 		if (ret == 0) {
2533f69061beSDaniel Vetter 			/*
2534f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
2535f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
2536f69061beSDaniel Vetter 			 * complete.
2537f69061beSDaniel Vetter 			 *
2538f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
2539f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
2540f69061beSDaniel Vetter 			 * updates before
2541f69061beSDaniel Vetter 			 * the counter increment.
2542f69061beSDaniel Vetter 			 */
25434e857c58SPeter Zijlstra 			smp_mb__before_atomic();
2544f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
2545f69061beSDaniel Vetter 
25465bdebb18SDave Airlie 			kobject_uevent_env(&dev->primary->kdev->kobj,
2547f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
25481f83fee0SDaniel Vetter 		} else {
2549805de8f4SPeter Zijlstra 			atomic_or(I915_WEDGED, &error->reset_counter);
2550f316a42cSBen Gamari 		}
25511f83fee0SDaniel Vetter 
255217e1df07SDaniel Vetter 		/*
255317e1df07SDaniel Vetter 		 * Note: The wake_up also serves as a memory barrier so that
255417e1df07SDaniel Vetter 		 * waiters see the update value of the reset counter atomic_t.
255517e1df07SDaniel Vetter 		 */
255617e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, true);
2557f316a42cSBen Gamari 	}
25588a905236SJesse Barnes }
25598a905236SJesse Barnes 
256035aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
2561c0e09200SDave Airlie {
25628a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
2563bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
256463eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
2565050ee91fSBen Widawsky 	int pipe, i;
256663eeaf38SJesse Barnes 
256735aed2e6SChris Wilson 	if (!eir)
256835aed2e6SChris Wilson 		return;
256963eeaf38SJesse Barnes 
2570a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
25718a905236SJesse Barnes 
2572bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
2573bd9854f9SBen Widawsky 
25748a905236SJesse Barnes 	if (IS_G4X(dev)) {
25758a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
25768a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
25778a905236SJesse Barnes 
2578a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2579a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2580050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
2581050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2582a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2583a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
25848a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
25853143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
25868a905236SJesse Barnes 		}
25878a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
25888a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2589a70491ccSJoe Perches 			pr_err("page table error\n");
2590a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
25918a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
25923143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
25938a905236SJesse Barnes 		}
25948a905236SJesse Barnes 	}
25958a905236SJesse Barnes 
2596a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
259763eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
259863eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2599a70491ccSJoe Perches 			pr_err("page table error\n");
2600a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
260163eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
26023143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
260363eeaf38SJesse Barnes 		}
26048a905236SJesse Barnes 	}
26058a905236SJesse Barnes 
260663eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
2607a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
2608055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
2609a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
26109db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
261163eeaf38SJesse Barnes 		/* pipestat has already been acked */
261263eeaf38SJesse Barnes 	}
261363eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
2614a70491ccSJoe Perches 		pr_err("instruction error\n");
2615a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
2616050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
2617050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2618a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
261963eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
262063eeaf38SJesse Barnes 
2621a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
2622a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
2623a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
262463eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
26253143a2bfSChris Wilson 			POSTING_READ(IPEIR);
262663eeaf38SJesse Barnes 		} else {
262763eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
262863eeaf38SJesse Barnes 
2629a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2630a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2631a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2632a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
263363eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
26343143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
263563eeaf38SJesse Barnes 		}
263663eeaf38SJesse Barnes 	}
263763eeaf38SJesse Barnes 
263863eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
26393143a2bfSChris Wilson 	POSTING_READ(EIR);
264063eeaf38SJesse Barnes 	eir = I915_READ(EIR);
264163eeaf38SJesse Barnes 	if (eir) {
264263eeaf38SJesse Barnes 		/*
264363eeaf38SJesse Barnes 		 * some errors might have become stuck,
264463eeaf38SJesse Barnes 		 * mask them.
264563eeaf38SJesse Barnes 		 */
264663eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
264763eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
264863eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
264963eeaf38SJesse Barnes 	}
265035aed2e6SChris Wilson }
265135aed2e6SChris Wilson 
265235aed2e6SChris Wilson /**
2653b8d24a06SMika Kuoppala  * i915_handle_error - handle a gpu error
265435aed2e6SChris Wilson  * @dev: drm device
265514b730fcSarun.siluvery@linux.intel.com  * @engine_mask: mask representing engines that are hung
2656aafd8581SJavier Martinez Canillas  * Do some basic checking of register state at error time and
265735aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
265835aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
265935aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
266035aed2e6SChris Wilson  * of a ring dump etc.).
266135aed2e6SChris Wilson  */
266214b730fcSarun.siluvery@linux.intel.com void i915_handle_error(struct drm_device *dev, u32 engine_mask,
266358174462SMika Kuoppala 		       const char *fmt, ...)
266435aed2e6SChris Wilson {
266535aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
266658174462SMika Kuoppala 	va_list args;
266758174462SMika Kuoppala 	char error_msg[80];
266835aed2e6SChris Wilson 
266958174462SMika Kuoppala 	va_start(args, fmt);
267058174462SMika Kuoppala 	vscnprintf(error_msg, sizeof(error_msg), fmt, args);
267158174462SMika Kuoppala 	va_end(args);
267258174462SMika Kuoppala 
267314b730fcSarun.siluvery@linux.intel.com 	i915_capture_error_state(dev, engine_mask, error_msg);
267435aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
26758a905236SJesse Barnes 
267614b730fcSarun.siluvery@linux.intel.com 	if (engine_mask) {
2677805de8f4SPeter Zijlstra 		atomic_or(I915_RESET_IN_PROGRESS_FLAG,
2678f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
2679ba1234d1SBen Gamari 
268011ed50ecSBen Gamari 		/*
2681b8d24a06SMika Kuoppala 		 * Wakeup waiting processes so that the reset function
2682b8d24a06SMika Kuoppala 		 * i915_reset_and_wakeup doesn't deadlock trying to grab
2683b8d24a06SMika Kuoppala 		 * various locks. By bumping the reset counter first, the woken
268417e1df07SDaniel Vetter 		 * processes will see a reset in progress and back off,
268517e1df07SDaniel Vetter 		 * releasing their locks and then wait for the reset completion.
268617e1df07SDaniel Vetter 		 * We must do this for _all_ gpu waiters that might hold locks
268717e1df07SDaniel Vetter 		 * that the reset work needs to acquire.
268817e1df07SDaniel Vetter 		 *
268917e1df07SDaniel Vetter 		 * Note: The wake_up serves as the required memory barrier to
269017e1df07SDaniel Vetter 		 * ensure that the waiters see the updated value of the reset
269117e1df07SDaniel Vetter 		 * counter atomic_t.
269211ed50ecSBen Gamari 		 */
269317e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, false);
269411ed50ecSBen Gamari 	}
269511ed50ecSBen Gamari 
2696b8d24a06SMika Kuoppala 	i915_reset_and_wakeup(dev);
26978a905236SJesse Barnes }
26988a905236SJesse Barnes 
269942f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
270042f52ef8SKeith Packard  * we use as a pipe index
270142f52ef8SKeith Packard  */
270288e72717SThierry Reding static int i915_enable_vblank(struct drm_device *dev, unsigned int pipe)
27030a3e67a4SJesse Barnes {
27042d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2705e9d21d7fSKeith Packard 	unsigned long irqflags;
270671e0ffa5SJesse Barnes 
27071ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2708f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
27097c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2710755e9019SImre Deak 				     PIPE_START_VBLANK_INTERRUPT_STATUS);
27110a3e67a4SJesse Barnes 	else
27127c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2713755e9019SImre Deak 				     PIPE_VBLANK_INTERRUPT_STATUS);
27141ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
27158692d00eSChris Wilson 
27160a3e67a4SJesse Barnes 	return 0;
27170a3e67a4SJesse Barnes }
27180a3e67a4SJesse Barnes 
271988e72717SThierry Reding static int ironlake_enable_vblank(struct drm_device *dev, unsigned int pipe)
2720f796cf8fSJesse Barnes {
27212d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2722f796cf8fSJesse Barnes 	unsigned long irqflags;
2723b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
272440da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2725f796cf8fSJesse Barnes 
2726f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2727fbdedaeaSVille Syrjälä 	ilk_enable_display_irq(dev_priv, bit);
2728b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2729b1f14ad0SJesse Barnes 
2730b1f14ad0SJesse Barnes 	return 0;
2731b1f14ad0SJesse Barnes }
2732b1f14ad0SJesse Barnes 
273388e72717SThierry Reding static int valleyview_enable_vblank(struct drm_device *dev, unsigned int pipe)
27347e231dbeSJesse Barnes {
27352d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
27367e231dbeSJesse Barnes 	unsigned long irqflags;
27377e231dbeSJesse Barnes 
27387e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
273931acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
2740755e9019SImre Deak 			     PIPE_START_VBLANK_INTERRUPT_STATUS);
27417e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
27427e231dbeSJesse Barnes 
27437e231dbeSJesse Barnes 	return 0;
27447e231dbeSJesse Barnes }
27457e231dbeSJesse Barnes 
274688e72717SThierry Reding static int gen8_enable_vblank(struct drm_device *dev, unsigned int pipe)
2747abd58f01SBen Widawsky {
2748abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2749abd58f01SBen Widawsky 	unsigned long irqflags;
2750abd58f01SBen Widawsky 
2751abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2752013d3752SVille Syrjälä 	bdw_enable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
2753abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2754013d3752SVille Syrjälä 
2755abd58f01SBen Widawsky 	return 0;
2756abd58f01SBen Widawsky }
2757abd58f01SBen Widawsky 
275842f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
275942f52ef8SKeith Packard  * we use as a pipe index
276042f52ef8SKeith Packard  */
276188e72717SThierry Reding static void i915_disable_vblank(struct drm_device *dev, unsigned int pipe)
27620a3e67a4SJesse Barnes {
27632d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2764e9d21d7fSKeith Packard 	unsigned long irqflags;
27650a3e67a4SJesse Barnes 
27661ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
27677c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
2768755e9019SImre Deak 			      PIPE_VBLANK_INTERRUPT_STATUS |
2769755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
27701ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
27710a3e67a4SJesse Barnes }
27720a3e67a4SJesse Barnes 
277388e72717SThierry Reding static void ironlake_disable_vblank(struct drm_device *dev, unsigned int pipe)
2774f796cf8fSJesse Barnes {
27752d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2776f796cf8fSJesse Barnes 	unsigned long irqflags;
2777b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
277840da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2779f796cf8fSJesse Barnes 
2780f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2781fbdedaeaSVille Syrjälä 	ilk_disable_display_irq(dev_priv, bit);
2782b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2783b1f14ad0SJesse Barnes }
2784b1f14ad0SJesse Barnes 
278588e72717SThierry Reding static void valleyview_disable_vblank(struct drm_device *dev, unsigned int pipe)
27867e231dbeSJesse Barnes {
27872d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
27887e231dbeSJesse Barnes 	unsigned long irqflags;
27897e231dbeSJesse Barnes 
27907e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
279131acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
2792755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
27937e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
27947e231dbeSJesse Barnes }
27957e231dbeSJesse Barnes 
279688e72717SThierry Reding static void gen8_disable_vblank(struct drm_device *dev, unsigned int pipe)
2797abd58f01SBen Widawsky {
2798abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2799abd58f01SBen Widawsky 	unsigned long irqflags;
2800abd58f01SBen Widawsky 
2801abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2802013d3752SVille Syrjälä 	bdw_disable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
2803abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2804abd58f01SBen Widawsky }
2805abd58f01SBen Widawsky 
28069107e9d2SChris Wilson static bool
28070bc40be8STvrtko Ursulin ring_idle(struct intel_engine_cs *engine, u32 seqno)
2808893eead0SChris Wilson {
2809cffa781eSChris Wilson 	return i915_seqno_passed(seqno,
2810cffa781eSChris Wilson 				 READ_ONCE(engine->last_submitted_seqno));
2811f65d9421SBen Gamari }
2812f65d9421SBen Gamari 
2813a028c4b0SDaniel Vetter static bool
2814a028c4b0SDaniel Vetter ipehr_is_semaphore_wait(struct drm_device *dev, u32 ipehr)
2815a028c4b0SDaniel Vetter {
2816a028c4b0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 8) {
2817a6cdb93aSRodrigo Vivi 		return (ipehr >> 23) == 0x1c;
2818a028c4b0SDaniel Vetter 	} else {
2819a028c4b0SDaniel Vetter 		ipehr &= ~MI_SEMAPHORE_SYNC_MASK;
2820a028c4b0SDaniel Vetter 		return ipehr == (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE |
2821a028c4b0SDaniel Vetter 				 MI_SEMAPHORE_REGISTER);
2822a028c4b0SDaniel Vetter 	}
2823a028c4b0SDaniel Vetter }
2824a028c4b0SDaniel Vetter 
2825a4872ba6SOscar Mateo static struct intel_engine_cs *
28260bc40be8STvrtko Ursulin semaphore_wait_to_signaller_ring(struct intel_engine_cs *engine, u32 ipehr,
28270bc40be8STvrtko Ursulin 				 u64 offset)
2828921d42eaSDaniel Vetter {
28290bc40be8STvrtko Ursulin 	struct drm_i915_private *dev_priv = engine->dev->dev_private;
2830a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2831921d42eaSDaniel Vetter 
28322d1fe073SJoonas Lahtinen 	if (INTEL_INFO(dev_priv)->gen >= 8) {
2833b4ac5afcSDave Gordon 		for_each_engine(signaller, dev_priv) {
28340bc40be8STvrtko Ursulin 			if (engine == signaller)
2835a6cdb93aSRodrigo Vivi 				continue;
2836a6cdb93aSRodrigo Vivi 
28370bc40be8STvrtko Ursulin 			if (offset == signaller->semaphore.signal_ggtt[engine->id])
2838a6cdb93aSRodrigo Vivi 				return signaller;
2839a6cdb93aSRodrigo Vivi 		}
2840921d42eaSDaniel Vetter 	} else {
2841921d42eaSDaniel Vetter 		u32 sync_bits = ipehr & MI_SEMAPHORE_SYNC_MASK;
2842921d42eaSDaniel Vetter 
2843b4ac5afcSDave Gordon 		for_each_engine(signaller, dev_priv) {
28440bc40be8STvrtko Ursulin 			if(engine == signaller)
2845921d42eaSDaniel Vetter 				continue;
2846921d42eaSDaniel Vetter 
28470bc40be8STvrtko Ursulin 			if (sync_bits == signaller->semaphore.mbox.wait[engine->id])
2848921d42eaSDaniel Vetter 				return signaller;
2849921d42eaSDaniel Vetter 		}
2850921d42eaSDaniel Vetter 	}
2851921d42eaSDaniel Vetter 
2852a6cdb93aSRodrigo Vivi 	DRM_ERROR("No signaller ring found for ring %i, ipehr 0x%08x, offset 0x%016llx\n",
28530bc40be8STvrtko Ursulin 		  engine->id, ipehr, offset);
2854921d42eaSDaniel Vetter 
2855921d42eaSDaniel Vetter 	return NULL;
2856921d42eaSDaniel Vetter }
2857921d42eaSDaniel Vetter 
2858a4872ba6SOscar Mateo static struct intel_engine_cs *
28590bc40be8STvrtko Ursulin semaphore_waits_for(struct intel_engine_cs *engine, u32 *seqno)
2860a24a11e6SChris Wilson {
28610bc40be8STvrtko Ursulin 	struct drm_i915_private *dev_priv = engine->dev->dev_private;
286288fe429dSDaniel Vetter 	u32 cmd, ipehr, head;
2863a6cdb93aSRodrigo Vivi 	u64 offset = 0;
2864a6cdb93aSRodrigo Vivi 	int i, backwards;
2865a24a11e6SChris Wilson 
2866381e8ae3STomas Elf 	/*
2867381e8ae3STomas Elf 	 * This function does not support execlist mode - any attempt to
2868381e8ae3STomas Elf 	 * proceed further into this function will result in a kernel panic
2869381e8ae3STomas Elf 	 * when dereferencing ring->buffer, which is not set up in execlist
2870381e8ae3STomas Elf 	 * mode.
2871381e8ae3STomas Elf 	 *
2872381e8ae3STomas Elf 	 * The correct way of doing it would be to derive the currently
2873381e8ae3STomas Elf 	 * executing ring buffer from the current context, which is derived
2874381e8ae3STomas Elf 	 * from the currently running request. Unfortunately, to get the
2875381e8ae3STomas Elf 	 * current request we would have to grab the struct_mutex before doing
2876381e8ae3STomas Elf 	 * anything else, which would be ill-advised since some other thread
2877381e8ae3STomas Elf 	 * might have grabbed it already and managed to hang itself, causing
2878381e8ae3STomas Elf 	 * the hang checker to deadlock.
2879381e8ae3STomas Elf 	 *
2880381e8ae3STomas Elf 	 * Therefore, this function does not support execlist mode in its
2881381e8ae3STomas Elf 	 * current form. Just return NULL and move on.
2882381e8ae3STomas Elf 	 */
28830bc40be8STvrtko Ursulin 	if (engine->buffer == NULL)
2884381e8ae3STomas Elf 		return NULL;
2885381e8ae3STomas Elf 
28860bc40be8STvrtko Ursulin 	ipehr = I915_READ(RING_IPEHR(engine->mmio_base));
28870bc40be8STvrtko Ursulin 	if (!ipehr_is_semaphore_wait(engine->dev, ipehr))
28886274f212SChris Wilson 		return NULL;
2889a24a11e6SChris Wilson 
289088fe429dSDaniel Vetter 	/*
289188fe429dSDaniel Vetter 	 * HEAD is likely pointing to the dword after the actual command,
289288fe429dSDaniel Vetter 	 * so scan backwards until we find the MBOX. But limit it to just 3
2893a6cdb93aSRodrigo Vivi 	 * or 4 dwords depending on the semaphore wait command size.
2894a6cdb93aSRodrigo Vivi 	 * Note that we don't care about ACTHD here since that might
289588fe429dSDaniel Vetter 	 * point at at batch, and semaphores are always emitted into the
289688fe429dSDaniel Vetter 	 * ringbuffer itself.
2897a24a11e6SChris Wilson 	 */
28980bc40be8STvrtko Ursulin 	head = I915_READ_HEAD(engine) & HEAD_ADDR;
28990bc40be8STvrtko Ursulin 	backwards = (INTEL_INFO(engine->dev)->gen >= 8) ? 5 : 4;
290088fe429dSDaniel Vetter 
2901a6cdb93aSRodrigo Vivi 	for (i = backwards; i; --i) {
290288fe429dSDaniel Vetter 		/*
290388fe429dSDaniel Vetter 		 * Be paranoid and presume the hw has gone off into the wild -
290488fe429dSDaniel Vetter 		 * our ring is smaller than what the hardware (and hence
290588fe429dSDaniel Vetter 		 * HEAD_ADDR) allows. Also handles wrap-around.
290688fe429dSDaniel Vetter 		 */
29070bc40be8STvrtko Ursulin 		head &= engine->buffer->size - 1;
290888fe429dSDaniel Vetter 
290988fe429dSDaniel Vetter 		/* This here seems to blow up */
29100bc40be8STvrtko Ursulin 		cmd = ioread32(engine->buffer->virtual_start + head);
2911a24a11e6SChris Wilson 		if (cmd == ipehr)
2912a24a11e6SChris Wilson 			break;
2913a24a11e6SChris Wilson 
291488fe429dSDaniel Vetter 		head -= 4;
291588fe429dSDaniel Vetter 	}
2916a24a11e6SChris Wilson 
291788fe429dSDaniel Vetter 	if (!i)
291888fe429dSDaniel Vetter 		return NULL;
291988fe429dSDaniel Vetter 
29200bc40be8STvrtko Ursulin 	*seqno = ioread32(engine->buffer->virtual_start + head + 4) + 1;
29210bc40be8STvrtko Ursulin 	if (INTEL_INFO(engine->dev)->gen >= 8) {
29220bc40be8STvrtko Ursulin 		offset = ioread32(engine->buffer->virtual_start + head + 12);
2923a6cdb93aSRodrigo Vivi 		offset <<= 32;
29240bc40be8STvrtko Ursulin 		offset = ioread32(engine->buffer->virtual_start + head + 8);
2925a6cdb93aSRodrigo Vivi 	}
29260bc40be8STvrtko Ursulin 	return semaphore_wait_to_signaller_ring(engine, ipehr, offset);
2927a24a11e6SChris Wilson }
2928a24a11e6SChris Wilson 
29290bc40be8STvrtko Ursulin static int semaphore_passed(struct intel_engine_cs *engine)
29306274f212SChris Wilson {
29310bc40be8STvrtko Ursulin 	struct drm_i915_private *dev_priv = engine->dev->dev_private;
2932a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2933a0d036b0SChris Wilson 	u32 seqno;
29346274f212SChris Wilson 
29350bc40be8STvrtko Ursulin 	engine->hangcheck.deadlock++;
29366274f212SChris Wilson 
29370bc40be8STvrtko Ursulin 	signaller = semaphore_waits_for(engine, &seqno);
29384be17381SChris Wilson 	if (signaller == NULL)
29394be17381SChris Wilson 		return -1;
29404be17381SChris Wilson 
29414be17381SChris Wilson 	/* Prevent pathological recursion due to driver bugs */
2942666796daSTvrtko Ursulin 	if (signaller->hangcheck.deadlock >= I915_NUM_ENGINES)
29436274f212SChris Wilson 		return -1;
29446274f212SChris Wilson 
2945c04e0f3bSChris Wilson 	if (i915_seqno_passed(signaller->get_seqno(signaller), seqno))
29464be17381SChris Wilson 		return 1;
29474be17381SChris Wilson 
2948a0d036b0SChris Wilson 	/* cursory check for an unkickable deadlock */
2949a0d036b0SChris Wilson 	if (I915_READ_CTL(signaller) & RING_WAIT_SEMAPHORE &&
2950a0d036b0SChris Wilson 	    semaphore_passed(signaller) < 0)
29514be17381SChris Wilson 		return -1;
29524be17381SChris Wilson 
29534be17381SChris Wilson 	return 0;
29546274f212SChris Wilson }
29556274f212SChris Wilson 
29566274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
29576274f212SChris Wilson {
2958e2f80391STvrtko Ursulin 	struct intel_engine_cs *engine;
29596274f212SChris Wilson 
2960b4ac5afcSDave Gordon 	for_each_engine(engine, dev_priv)
2961e2f80391STvrtko Ursulin 		engine->hangcheck.deadlock = 0;
29626274f212SChris Wilson }
29636274f212SChris Wilson 
29640bc40be8STvrtko Ursulin static bool subunits_stuck(struct intel_engine_cs *engine)
29651ec14ad3SChris Wilson {
296661642ff0SMika Kuoppala 	u32 instdone[I915_NUM_INSTDONE_REG];
296761642ff0SMika Kuoppala 	bool stuck;
296861642ff0SMika Kuoppala 	int i;
29699107e9d2SChris Wilson 
29700bc40be8STvrtko Ursulin 	if (engine->id != RCS)
297161642ff0SMika Kuoppala 		return true;
297261642ff0SMika Kuoppala 
29730bc40be8STvrtko Ursulin 	i915_get_extra_instdone(engine->dev, instdone);
297461642ff0SMika Kuoppala 
297561642ff0SMika Kuoppala 	/* There might be unstable subunit states even when
297661642ff0SMika Kuoppala 	 * actual head is not moving. Filter out the unstable ones by
297761642ff0SMika Kuoppala 	 * accumulating the undone -> done transitions and only
297861642ff0SMika Kuoppala 	 * consider those as progress.
297961642ff0SMika Kuoppala 	 */
298061642ff0SMika Kuoppala 	stuck = true;
298161642ff0SMika Kuoppala 	for (i = 0; i < I915_NUM_INSTDONE_REG; i++) {
29820bc40be8STvrtko Ursulin 		const u32 tmp = instdone[i] | engine->hangcheck.instdone[i];
298361642ff0SMika Kuoppala 
29840bc40be8STvrtko Ursulin 		if (tmp != engine->hangcheck.instdone[i])
298561642ff0SMika Kuoppala 			stuck = false;
298661642ff0SMika Kuoppala 
29870bc40be8STvrtko Ursulin 		engine->hangcheck.instdone[i] |= tmp;
298861642ff0SMika Kuoppala 	}
298961642ff0SMika Kuoppala 
299061642ff0SMika Kuoppala 	return stuck;
299161642ff0SMika Kuoppala }
299261642ff0SMika Kuoppala 
299361642ff0SMika Kuoppala static enum intel_ring_hangcheck_action
29940bc40be8STvrtko Ursulin head_stuck(struct intel_engine_cs *engine, u64 acthd)
299561642ff0SMika Kuoppala {
29960bc40be8STvrtko Ursulin 	if (acthd != engine->hangcheck.acthd) {
299761642ff0SMika Kuoppala 
299861642ff0SMika Kuoppala 		/* Clear subunit states on head movement */
29990bc40be8STvrtko Ursulin 		memset(engine->hangcheck.instdone, 0,
30000bc40be8STvrtko Ursulin 		       sizeof(engine->hangcheck.instdone));
300161642ff0SMika Kuoppala 
3002f2f4d82fSJani Nikula 		return HANGCHECK_ACTIVE;
3003f260fe7bSMika Kuoppala 	}
3004f260fe7bSMika Kuoppala 
30050bc40be8STvrtko Ursulin 	if (!subunits_stuck(engine))
300661642ff0SMika Kuoppala 		return HANGCHECK_ACTIVE;
300761642ff0SMika Kuoppala 
300861642ff0SMika Kuoppala 	return HANGCHECK_HUNG;
300961642ff0SMika Kuoppala }
301061642ff0SMika Kuoppala 
301161642ff0SMika Kuoppala static enum intel_ring_hangcheck_action
30120bc40be8STvrtko Ursulin ring_stuck(struct intel_engine_cs *engine, u64 acthd)
301361642ff0SMika Kuoppala {
30140bc40be8STvrtko Ursulin 	struct drm_device *dev = engine->dev;
301561642ff0SMika Kuoppala 	struct drm_i915_private *dev_priv = dev->dev_private;
301661642ff0SMika Kuoppala 	enum intel_ring_hangcheck_action ha;
301761642ff0SMika Kuoppala 	u32 tmp;
301861642ff0SMika Kuoppala 
30190bc40be8STvrtko Ursulin 	ha = head_stuck(engine, acthd);
302061642ff0SMika Kuoppala 	if (ha != HANGCHECK_HUNG)
302161642ff0SMika Kuoppala 		return ha;
302261642ff0SMika Kuoppala 
30239107e9d2SChris Wilson 	if (IS_GEN2(dev))
3024f2f4d82fSJani Nikula 		return HANGCHECK_HUNG;
30259107e9d2SChris Wilson 
30269107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
30279107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
30289107e9d2SChris Wilson 	 * and break the hang. This should work on
30299107e9d2SChris Wilson 	 * all but the second generation chipsets.
30309107e9d2SChris Wilson 	 */
30310bc40be8STvrtko Ursulin 	tmp = I915_READ_CTL(engine);
30321ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
303314b730fcSarun.siluvery@linux.intel.com 		i915_handle_error(dev, 0,
303458174462SMika Kuoppala 				  "Kicking stuck wait on %s",
30350bc40be8STvrtko Ursulin 				  engine->name);
30360bc40be8STvrtko Ursulin 		I915_WRITE_CTL(engine, tmp);
3037f2f4d82fSJani Nikula 		return HANGCHECK_KICK;
30381ec14ad3SChris Wilson 	}
3039a24a11e6SChris Wilson 
30406274f212SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
30410bc40be8STvrtko Ursulin 		switch (semaphore_passed(engine)) {
30426274f212SChris Wilson 		default:
3043f2f4d82fSJani Nikula 			return HANGCHECK_HUNG;
30446274f212SChris Wilson 		case 1:
304514b730fcSarun.siluvery@linux.intel.com 			i915_handle_error(dev, 0,
304658174462SMika Kuoppala 					  "Kicking stuck semaphore on %s",
30470bc40be8STvrtko Ursulin 					  engine->name);
30480bc40be8STvrtko Ursulin 			I915_WRITE_CTL(engine, tmp);
3049f2f4d82fSJani Nikula 			return HANGCHECK_KICK;
30506274f212SChris Wilson 		case 0:
3051f2f4d82fSJani Nikula 			return HANGCHECK_WAIT;
30526274f212SChris Wilson 		}
30539107e9d2SChris Wilson 	}
30549107e9d2SChris Wilson 
3055f2f4d82fSJani Nikula 	return HANGCHECK_HUNG;
3056a24a11e6SChris Wilson }
3057d1e61e7fSChris Wilson 
305812471ba8SChris Wilson static unsigned kick_waiters(struct intel_engine_cs *engine)
305912471ba8SChris Wilson {
306012471ba8SChris Wilson 	struct drm_i915_private *i915 = to_i915(engine->dev);
306112471ba8SChris Wilson 	unsigned user_interrupts = READ_ONCE(engine->user_interrupts);
306212471ba8SChris Wilson 
306312471ba8SChris Wilson 	if (engine->hangcheck.user_interrupts == user_interrupts &&
306412471ba8SChris Wilson 	    !test_and_set_bit(engine->id, &i915->gpu_error.missed_irq_rings)) {
306512471ba8SChris Wilson 		if (!(i915->gpu_error.test_irq_rings & intel_engine_flag(engine)))
306612471ba8SChris Wilson 			DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
306712471ba8SChris Wilson 				  engine->name);
306812471ba8SChris Wilson 		else
306912471ba8SChris Wilson 			DRM_INFO("Fake missed irq on %s\n",
307012471ba8SChris Wilson 				 engine->name);
307112471ba8SChris Wilson 		wake_up_all(&engine->irq_queue);
307212471ba8SChris Wilson 	}
307312471ba8SChris Wilson 
307412471ba8SChris Wilson 	return user_interrupts;
307512471ba8SChris Wilson }
3076737b1506SChris Wilson /*
3077f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
307805407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
307905407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
308005407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
308105407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
308205407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
3083f65d9421SBen Gamari  */
3084737b1506SChris Wilson static void i915_hangcheck_elapsed(struct work_struct *work)
3085f65d9421SBen Gamari {
3086737b1506SChris Wilson 	struct drm_i915_private *dev_priv =
3087737b1506SChris Wilson 		container_of(work, typeof(*dev_priv),
3088737b1506SChris Wilson 			     gpu_error.hangcheck_work.work);
3089737b1506SChris Wilson 	struct drm_device *dev = dev_priv->dev;
3090e2f80391STvrtko Ursulin 	struct intel_engine_cs *engine;
3091c3232b18SDave Gordon 	enum intel_engine_id id;
309205407ff8SMika Kuoppala 	int busy_count = 0, rings_hung = 0;
3093666796daSTvrtko Ursulin 	bool stuck[I915_NUM_ENGINES] = { 0 };
30949107e9d2SChris Wilson #define BUSY 1
30959107e9d2SChris Wilson #define KICK 5
30969107e9d2SChris Wilson #define HUNG 20
309724a65e62SMika Kuoppala #define ACTIVE_DECAY 15
3098893eead0SChris Wilson 
3099d330a953SJani Nikula 	if (!i915.enable_hangcheck)
31003e0dc6b0SBen Widawsky 		return;
31013e0dc6b0SBen Widawsky 
31021f814dacSImre Deak 	/*
31031f814dacSImre Deak 	 * The hangcheck work is synced during runtime suspend, we don't
31041f814dacSImre Deak 	 * require a wakeref. TODO: instead of disabling the asserts make
31051f814dacSImre Deak 	 * sure that we hold a reference when this work is running.
31061f814dacSImre Deak 	 */
31071f814dacSImre Deak 	DISABLE_RPM_WAKEREF_ASSERTS(dev_priv);
31081f814dacSImre Deak 
310975714940SMika Kuoppala 	/* As enabling the GPU requires fairly extensive mmio access,
311075714940SMika Kuoppala 	 * periodically arm the mmio checker to see if we are triggering
311175714940SMika Kuoppala 	 * any invalid access.
311275714940SMika Kuoppala 	 */
311375714940SMika Kuoppala 	intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
311475714940SMika Kuoppala 
3115c3232b18SDave Gordon 	for_each_engine_id(engine, dev_priv, id) {
311650877445SChris Wilson 		u64 acthd;
311750877445SChris Wilson 		u32 seqno;
311812471ba8SChris Wilson 		unsigned user_interrupts;
31199107e9d2SChris Wilson 		bool busy = true;
3120b4519513SChris Wilson 
31216274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
31226274f212SChris Wilson 
3123c04e0f3bSChris Wilson 		/* We don't strictly need an irq-barrier here, as we are not
3124c04e0f3bSChris Wilson 		 * serving an interrupt request, be paranoid in case the
3125c04e0f3bSChris Wilson 		 * barrier has side-effects (such as preventing a broken
3126c04e0f3bSChris Wilson 		 * cacheline snoop) and so be sure that we can see the seqno
3127c04e0f3bSChris Wilson 		 * advance. If the seqno should stick, due to a stale
3128c04e0f3bSChris Wilson 		 * cacheline, we would erroneously declare the GPU hung.
3129c04e0f3bSChris Wilson 		 */
3130c04e0f3bSChris Wilson 		if (engine->irq_seqno_barrier)
3131c04e0f3bSChris Wilson 			engine->irq_seqno_barrier(engine);
3132c04e0f3bSChris Wilson 
3133e2f80391STvrtko Ursulin 		acthd = intel_ring_get_active_head(engine);
3134c04e0f3bSChris Wilson 		seqno = engine->get_seqno(engine);
313505407ff8SMika Kuoppala 
313612471ba8SChris Wilson 		/* Reset stuck interrupts between batch advances */
313712471ba8SChris Wilson 		user_interrupts = 0;
313812471ba8SChris Wilson 
3139e2f80391STvrtko Ursulin 		if (engine->hangcheck.seqno == seqno) {
3140e2f80391STvrtko Ursulin 			if (ring_idle(engine, seqno)) {
3141e2f80391STvrtko Ursulin 				engine->hangcheck.action = HANGCHECK_IDLE;
3142e2f80391STvrtko Ursulin 				if (waitqueue_active(&engine->irq_queue)) {
3143094f9a54SChris Wilson 					/* Safeguard against driver failure */
314412471ba8SChris Wilson 					user_interrupts = kick_waiters(engine);
3145e2f80391STvrtko Ursulin 					engine->hangcheck.score += BUSY;
31469107e9d2SChris Wilson 				} else
31479107e9d2SChris Wilson 					busy = false;
314805407ff8SMika Kuoppala 			} else {
31496274f212SChris Wilson 				/* We always increment the hangcheck score
31506274f212SChris Wilson 				 * if the ring is busy and still processing
31516274f212SChris Wilson 				 * the same request, so that no single request
31526274f212SChris Wilson 				 * can run indefinitely (such as a chain of
31536274f212SChris Wilson 				 * batches). The only time we do not increment
31546274f212SChris Wilson 				 * the hangcheck score on this ring, if this
31556274f212SChris Wilson 				 * ring is in a legitimate wait for another
31566274f212SChris Wilson 				 * ring. In that case the waiting ring is a
31576274f212SChris Wilson 				 * victim and we want to be sure we catch the
31586274f212SChris Wilson 				 * right culprit. Then every time we do kick
31596274f212SChris Wilson 				 * the ring, add a small increment to the
31606274f212SChris Wilson 				 * score so that we can catch a batch that is
31616274f212SChris Wilson 				 * being repeatedly kicked and so responsible
31626274f212SChris Wilson 				 * for stalling the machine.
31639107e9d2SChris Wilson 				 */
3164e2f80391STvrtko Ursulin 				engine->hangcheck.action = ring_stuck(engine,
3165ad8beaeaSMika Kuoppala 								      acthd);
3166ad8beaeaSMika Kuoppala 
3167e2f80391STvrtko Ursulin 				switch (engine->hangcheck.action) {
3168da661464SMika Kuoppala 				case HANGCHECK_IDLE:
3169f2f4d82fSJani Nikula 				case HANGCHECK_WAIT:
3170f260fe7bSMika Kuoppala 					break;
317124a65e62SMika Kuoppala 				case HANGCHECK_ACTIVE:
3172e2f80391STvrtko Ursulin 					engine->hangcheck.score += BUSY;
31736274f212SChris Wilson 					break;
3174f2f4d82fSJani Nikula 				case HANGCHECK_KICK:
3175e2f80391STvrtko Ursulin 					engine->hangcheck.score += KICK;
31766274f212SChris Wilson 					break;
3177f2f4d82fSJani Nikula 				case HANGCHECK_HUNG:
3178e2f80391STvrtko Ursulin 					engine->hangcheck.score += HUNG;
3179c3232b18SDave Gordon 					stuck[id] = true;
31806274f212SChris Wilson 					break;
31816274f212SChris Wilson 				}
318205407ff8SMika Kuoppala 			}
31839107e9d2SChris Wilson 		} else {
3184e2f80391STvrtko Ursulin 			engine->hangcheck.action = HANGCHECK_ACTIVE;
3185da661464SMika Kuoppala 
31869107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
31879107e9d2SChris Wilson 			 * attempts across multiple batches.
31889107e9d2SChris Wilson 			 */
3189e2f80391STvrtko Ursulin 			if (engine->hangcheck.score > 0)
3190e2f80391STvrtko Ursulin 				engine->hangcheck.score -= ACTIVE_DECAY;
3191e2f80391STvrtko Ursulin 			if (engine->hangcheck.score < 0)
3192e2f80391STvrtko Ursulin 				engine->hangcheck.score = 0;
3193f260fe7bSMika Kuoppala 
319461642ff0SMika Kuoppala 			/* Clear head and subunit states on seqno movement */
319512471ba8SChris Wilson 			acthd = 0;
319661642ff0SMika Kuoppala 
3197e2f80391STvrtko Ursulin 			memset(engine->hangcheck.instdone, 0,
3198e2f80391STvrtko Ursulin 			       sizeof(engine->hangcheck.instdone));
3199cbb465e7SChris Wilson 		}
3200f65d9421SBen Gamari 
3201e2f80391STvrtko Ursulin 		engine->hangcheck.seqno = seqno;
3202e2f80391STvrtko Ursulin 		engine->hangcheck.acthd = acthd;
320312471ba8SChris Wilson 		engine->hangcheck.user_interrupts = user_interrupts;
32049107e9d2SChris Wilson 		busy_count += busy;
320505407ff8SMika Kuoppala 	}
320605407ff8SMika Kuoppala 
3207c3232b18SDave Gordon 	for_each_engine_id(engine, dev_priv, id) {
3208e2f80391STvrtko Ursulin 		if (engine->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG) {
3209b8d88d1dSDaniel Vetter 			DRM_INFO("%s on %s\n",
3210c3232b18SDave Gordon 				 stuck[id] ? "stuck" : "no progress",
3211e2f80391STvrtko Ursulin 				 engine->name);
321214b730fcSarun.siluvery@linux.intel.com 			rings_hung |= intel_engine_flag(engine);
321305407ff8SMika Kuoppala 		}
321405407ff8SMika Kuoppala 	}
321505407ff8SMika Kuoppala 
32161f814dacSImre Deak 	if (rings_hung) {
321714b730fcSarun.siluvery@linux.intel.com 		i915_handle_error(dev, rings_hung, "Engine(s) hung");
32181f814dacSImre Deak 		goto out;
32191f814dacSImre Deak 	}
322005407ff8SMika Kuoppala 
322105407ff8SMika Kuoppala 	if (busy_count)
322205407ff8SMika Kuoppala 		/* Reset timer case chip hangs without another request
322305407ff8SMika Kuoppala 		 * being added */
322410cd45b6SMika Kuoppala 		i915_queue_hangcheck(dev);
32251f814dacSImre Deak 
32261f814dacSImre Deak out:
32271f814dacSImre Deak 	ENABLE_RPM_WAKEREF_ASSERTS(dev_priv);
322810cd45b6SMika Kuoppala }
322910cd45b6SMika Kuoppala 
323010cd45b6SMika Kuoppala void i915_queue_hangcheck(struct drm_device *dev)
323110cd45b6SMika Kuoppala {
3232737b1506SChris Wilson 	struct i915_gpu_error *e = &to_i915(dev)->gpu_error;
3233672e7b7cSChris Wilson 
3234d330a953SJani Nikula 	if (!i915.enable_hangcheck)
323510cd45b6SMika Kuoppala 		return;
323610cd45b6SMika Kuoppala 
3237737b1506SChris Wilson 	/* Don't continually defer the hangcheck so that it is always run at
3238737b1506SChris Wilson 	 * least once after work has been scheduled on any ring. Otherwise,
3239737b1506SChris Wilson 	 * we will ignore a hung ring if a second ring is kept busy.
3240737b1506SChris Wilson 	 */
3241737b1506SChris Wilson 
3242737b1506SChris Wilson 	queue_delayed_work(e->hangcheck_wq, &e->hangcheck_work,
3243737b1506SChris Wilson 			   round_jiffies_up_relative(DRM_I915_HANGCHECK_JIFFIES));
3244f65d9421SBen Gamari }
3245f65d9421SBen Gamari 
32461c69eb42SPaulo Zanoni static void ibx_irq_reset(struct drm_device *dev)
324791738a95SPaulo Zanoni {
324891738a95SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
324991738a95SPaulo Zanoni 
325091738a95SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
325191738a95SPaulo Zanoni 		return;
325291738a95SPaulo Zanoni 
3253f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(SDE);
3254105b122eSPaulo Zanoni 
3255105b122eSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
3256105b122eSPaulo Zanoni 		I915_WRITE(SERR_INT, 0xffffffff);
3257622364b6SPaulo Zanoni }
3258105b122eSPaulo Zanoni 
325991738a95SPaulo Zanoni /*
3260622364b6SPaulo Zanoni  * SDEIER is also touched by the interrupt handler to work around missed PCH
3261622364b6SPaulo Zanoni  * interrupts. Hence we can't update it after the interrupt handler is enabled -
3262622364b6SPaulo Zanoni  * instead we unconditionally enable all PCH interrupt sources here, but then
3263622364b6SPaulo Zanoni  * only unmask them as needed with SDEIMR.
3264622364b6SPaulo Zanoni  *
3265622364b6SPaulo Zanoni  * This function needs to be called before interrupts are enabled.
326691738a95SPaulo Zanoni  */
3267622364b6SPaulo Zanoni static void ibx_irq_pre_postinstall(struct drm_device *dev)
3268622364b6SPaulo Zanoni {
3269622364b6SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3270622364b6SPaulo Zanoni 
3271622364b6SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
3272622364b6SPaulo Zanoni 		return;
3273622364b6SPaulo Zanoni 
3274622364b6SPaulo Zanoni 	WARN_ON(I915_READ(SDEIER) != 0);
327591738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
327691738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
327791738a95SPaulo Zanoni }
327891738a95SPaulo Zanoni 
32797c4d664eSPaulo Zanoni static void gen5_gt_irq_reset(struct drm_device *dev)
3280d18ea1b5SDaniel Vetter {
3281d18ea1b5SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
3282d18ea1b5SDaniel Vetter 
3283f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GT);
3284a9d356a6SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6)
3285f86f3fb0SPaulo Zanoni 		GEN5_IRQ_RESET(GEN6_PM);
3286d18ea1b5SDaniel Vetter }
3287d18ea1b5SDaniel Vetter 
3288c0e09200SDave Airlie /* drm_dma.h hooks
3289c0e09200SDave Airlie */
3290be30b29fSPaulo Zanoni static void ironlake_irq_reset(struct drm_device *dev)
3291036a4a7dSZhenyu Wang {
32922d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3293036a4a7dSZhenyu Wang 
32940c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xffffffff);
3295bdfcdb63SDaniel Vetter 
3296f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(DE);
3297c6d954c1SPaulo Zanoni 	if (IS_GEN7(dev))
3298c6d954c1SPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, 0xffffffff);
3299036a4a7dSZhenyu Wang 
33007c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
3301c650156aSZhenyu Wang 
33021c69eb42SPaulo Zanoni 	ibx_irq_reset(dev);
33037d99163dSBen Widawsky }
33047d99163dSBen Widawsky 
330570591a41SVille Syrjälä static void vlv_display_irq_reset(struct drm_i915_private *dev_priv)
330670591a41SVille Syrjälä {
330770591a41SVille Syrjälä 	enum pipe pipe;
330870591a41SVille Syrjälä 
3309ad22d106SVille Syrjälä 	i915_hotplug_interrupt_update_locked(dev_priv, 0xffffffff, 0);
331070591a41SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
331170591a41SVille Syrjälä 
3312ad22d106SVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
3313ad22d106SVille Syrjälä 		I915_WRITE(PIPESTAT(pipe),
3314ad22d106SVille Syrjälä 			   PIPE_FIFO_UNDERRUN_STATUS |
3315ad22d106SVille Syrjälä 			   PIPESTAT_INT_STATUS_MASK);
3316ad22d106SVille Syrjälä 		dev_priv->pipestat_irq_mask[pipe] = 0;
3317ad22d106SVille Syrjälä 	}
331870591a41SVille Syrjälä 
331970591a41SVille Syrjälä 	GEN5_IRQ_RESET(VLV_);
3320ad22d106SVille Syrjälä 	dev_priv->irq_mask = ~0;
332170591a41SVille Syrjälä }
332270591a41SVille Syrjälä 
33237e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
33247e231dbeSJesse Barnes {
33252d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
33267e231dbeSJesse Barnes 
33277c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
33287e231dbeSJesse Barnes 
33297c4cde39SVille Syrjälä 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
33307e231dbeSJesse Barnes 
3331ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
3332*9918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
333370591a41SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3334ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
33357e231dbeSJesse Barnes }
33367e231dbeSJesse Barnes 
3337d6e3cca3SDaniel Vetter static void gen8_gt_irq_reset(struct drm_i915_private *dev_priv)
3338d6e3cca3SDaniel Vetter {
3339d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 0);
3340d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 1);
3341d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 2);
3342d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 3);
3343d6e3cca3SDaniel Vetter }
3344d6e3cca3SDaniel Vetter 
3345823f6b38SPaulo Zanoni static void gen8_irq_reset(struct drm_device *dev)
3346abd58f01SBen Widawsky {
3347abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3348abd58f01SBen Widawsky 	int pipe;
3349abd58f01SBen Widawsky 
3350abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
3351abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3352abd58f01SBen Widawsky 
3353d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
3354abd58f01SBen Widawsky 
3355055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3356f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3357813bde43SPaulo Zanoni 						   POWER_DOMAIN_PIPE(pipe)))
3358f86f3fb0SPaulo Zanoni 			GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3359abd58f01SBen Widawsky 
3360f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_PORT_);
3361f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_MISC_);
3362f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_PCU_);
3363abd58f01SBen Widawsky 
3364266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev))
33651c69eb42SPaulo Zanoni 		ibx_irq_reset(dev);
3366abd58f01SBen Widawsky }
3367abd58f01SBen Widawsky 
33684c6c03beSDamien Lespiau void gen8_irq_power_well_post_enable(struct drm_i915_private *dev_priv,
33694c6c03beSDamien Lespiau 				     unsigned int pipe_mask)
3370d49bdb0eSPaulo Zanoni {
33711180e206SPaulo Zanoni 	uint32_t extra_ier = GEN8_PIPE_VBLANK | GEN8_PIPE_FIFO_UNDERRUN;
33726831f3e3SVille Syrjälä 	enum pipe pipe;
3373d49bdb0eSPaulo Zanoni 
337413321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
33756831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
33766831f3e3SVille Syrjälä 		GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
33776831f3e3SVille Syrjälä 				  dev_priv->de_irq_mask[pipe],
33786831f3e3SVille Syrjälä 				  ~dev_priv->de_irq_mask[pipe] | extra_ier);
337913321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3380d49bdb0eSPaulo Zanoni }
3381d49bdb0eSPaulo Zanoni 
3382aae8ba84SVille Syrjälä void gen8_irq_power_well_pre_disable(struct drm_i915_private *dev_priv,
3383aae8ba84SVille Syrjälä 				     unsigned int pipe_mask)
3384aae8ba84SVille Syrjälä {
33856831f3e3SVille Syrjälä 	enum pipe pipe;
33866831f3e3SVille Syrjälä 
3387aae8ba84SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
33886831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
33896831f3e3SVille Syrjälä 		GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3390aae8ba84SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3391aae8ba84SVille Syrjälä 
3392aae8ba84SVille Syrjälä 	/* make sure we're done processing display irqs */
3393aae8ba84SVille Syrjälä 	synchronize_irq(dev_priv->dev->irq);
3394aae8ba84SVille Syrjälä }
3395aae8ba84SVille Syrjälä 
339643f328d7SVille Syrjälä static void cherryview_irq_preinstall(struct drm_device *dev)
339743f328d7SVille Syrjälä {
339843f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
339943f328d7SVille Syrjälä 
340043f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
340143f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
340243f328d7SVille Syrjälä 
3403d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
340443f328d7SVille Syrjälä 
340543f328d7SVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
340643f328d7SVille Syrjälä 
340743f328d7SVille Syrjälä 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
340843f328d7SVille Syrjälä 
3409ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
3410*9918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
341170591a41SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3412ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
341343f328d7SVille Syrjälä }
341443f328d7SVille Syrjälä 
341587a02106SVille Syrjälä static u32 intel_hpd_enabled_irqs(struct drm_device *dev,
341687a02106SVille Syrjälä 				  const u32 hpd[HPD_NUM_PINS])
341787a02106SVille Syrjälä {
341887a02106SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(dev);
341987a02106SVille Syrjälä 	struct intel_encoder *encoder;
342087a02106SVille Syrjälä 	u32 enabled_irqs = 0;
342187a02106SVille Syrjälä 
342287a02106SVille Syrjälä 	for_each_intel_encoder(dev, encoder)
342387a02106SVille Syrjälä 		if (dev_priv->hotplug.stats[encoder->hpd_pin].state == HPD_ENABLED)
342487a02106SVille Syrjälä 			enabled_irqs |= hpd[encoder->hpd_pin];
342587a02106SVille Syrjälä 
342687a02106SVille Syrjälä 	return enabled_irqs;
342787a02106SVille Syrjälä }
342887a02106SVille Syrjälä 
342982a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
343082a28bcfSDaniel Vetter {
34312d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
343287a02106SVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
343382a28bcfSDaniel Vetter 
343482a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
3435fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
343687a02106SVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_ibx);
343782a28bcfSDaniel Vetter 	} else {
3438fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
343987a02106SVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_cpt);
344082a28bcfSDaniel Vetter 	}
344182a28bcfSDaniel Vetter 
3442fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
344382a28bcfSDaniel Vetter 
34447fe0b973SKeith Packard 	/*
34457fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
34466dbf30ceSVille Syrjälä 	 * duration to 2ms (which is the minimum in the Display Port spec).
34476dbf30ceSVille Syrjälä 	 * The pulse duration bits are reserved on LPT+.
34487fe0b973SKeith Packard 	 */
34497fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
34507fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
34517fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
34527fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
34537fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
34540b2eb33eSVille Syrjälä 	/*
34550b2eb33eSVille Syrjälä 	 * When CPU and PCH are on the same package, port A
34560b2eb33eSVille Syrjälä 	 * HPD must be enabled in both north and south.
34570b2eb33eSVille Syrjälä 	 */
34580b2eb33eSVille Syrjälä 	if (HAS_PCH_LPT_LP(dev))
34590b2eb33eSVille Syrjälä 		hotplug |= PORTA_HOTPLUG_ENABLE;
34607fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
34616dbf30ceSVille Syrjälä }
346226951cafSXiong Zhang 
34636dbf30ceSVille Syrjälä static void spt_hpd_irq_setup(struct drm_device *dev)
34646dbf30ceSVille Syrjälä {
34656dbf30ceSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
34666dbf30ceSVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
34676dbf30ceSVille Syrjälä 
34686dbf30ceSVille Syrjälä 	hotplug_irqs = SDE_HOTPLUG_MASK_SPT;
34696dbf30ceSVille Syrjälä 	enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_spt);
34706dbf30ceSVille Syrjälä 
34716dbf30ceSVille Syrjälä 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
34726dbf30ceSVille Syrjälä 
34736dbf30ceSVille Syrjälä 	/* Enable digital hotplug on the PCH */
34746dbf30ceSVille Syrjälä 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
34756dbf30ceSVille Syrjälä 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTC_HOTPLUG_ENABLE |
347674c0b395SVille Syrjälä 		PORTB_HOTPLUG_ENABLE | PORTA_HOTPLUG_ENABLE;
34776dbf30ceSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
34786dbf30ceSVille Syrjälä 
347926951cafSXiong Zhang 	hotplug = I915_READ(PCH_PORT_HOTPLUG2);
348026951cafSXiong Zhang 	hotplug |= PORTE_HOTPLUG_ENABLE;
348126951cafSXiong Zhang 	I915_WRITE(PCH_PORT_HOTPLUG2, hotplug);
348226951cafSXiong Zhang }
34837fe0b973SKeith Packard 
3484e4ce95aaSVille Syrjälä static void ilk_hpd_irq_setup(struct drm_device *dev)
3485e4ce95aaSVille Syrjälä {
3486e4ce95aaSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
3487e4ce95aaSVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
3488e4ce95aaSVille Syrjälä 
34893a3b3c7dSVille Syrjälä 	if (INTEL_INFO(dev)->gen >= 8) {
34903a3b3c7dSVille Syrjälä 		hotplug_irqs = GEN8_PORT_DP_A_HOTPLUG;
34913a3b3c7dSVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_bdw);
34923a3b3c7dSVille Syrjälä 
34933a3b3c7dSVille Syrjälä 		bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
34943a3b3c7dSVille Syrjälä 	} else if (INTEL_INFO(dev)->gen >= 7) {
349523bb4cb5SVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG_IVB;
349623bb4cb5SVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_ivb);
34973a3b3c7dSVille Syrjälä 
34983a3b3c7dSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
349923bb4cb5SVille Syrjälä 	} else {
3500e4ce95aaSVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG;
3501e4ce95aaSVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_ilk);
3502e4ce95aaSVille Syrjälä 
3503e4ce95aaSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
35043a3b3c7dSVille Syrjälä 	}
3505e4ce95aaSVille Syrjälä 
3506e4ce95aaSVille Syrjälä 	/*
3507e4ce95aaSVille Syrjälä 	 * Enable digital hotplug on the CPU, and configure the DP short pulse
3508e4ce95aaSVille Syrjälä 	 * duration to 2ms (which is the minimum in the Display Port spec)
350923bb4cb5SVille Syrjälä 	 * The pulse duration bits are reserved on HSW+.
3510e4ce95aaSVille Syrjälä 	 */
3511e4ce95aaSVille Syrjälä 	hotplug = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
3512e4ce95aaSVille Syrjälä 	hotplug &= ~DIGITAL_PORTA_PULSE_DURATION_MASK;
3513e4ce95aaSVille Syrjälä 	hotplug |= DIGITAL_PORTA_HOTPLUG_ENABLE | DIGITAL_PORTA_PULSE_DURATION_2ms;
3514e4ce95aaSVille Syrjälä 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, hotplug);
3515e4ce95aaSVille Syrjälä 
3516e4ce95aaSVille Syrjälä 	ibx_hpd_irq_setup(dev);
3517e4ce95aaSVille Syrjälä }
3518e4ce95aaSVille Syrjälä 
3519e0a20ad7SShashank Sharma static void bxt_hpd_irq_setup(struct drm_device *dev)
3520e0a20ad7SShashank Sharma {
3521e0a20ad7SShashank Sharma 	struct drm_i915_private *dev_priv = dev->dev_private;
3522a52bb15bSVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
3523e0a20ad7SShashank Sharma 
3524a52bb15bSVille Syrjälä 	enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_bxt);
3525a52bb15bSVille Syrjälä 	hotplug_irqs = BXT_DE_PORT_HOTPLUG_MASK;
3526e0a20ad7SShashank Sharma 
3527a52bb15bSVille Syrjälä 	bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
3528e0a20ad7SShashank Sharma 
3529a52bb15bSVille Syrjälä 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
3530a52bb15bSVille Syrjälä 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTB_HOTPLUG_ENABLE |
3531a52bb15bSVille Syrjälä 		PORTA_HOTPLUG_ENABLE;
3532d252bf68SShubhangi Shrivastava 
3533d252bf68SShubhangi Shrivastava 	DRM_DEBUG_KMS("Invert bit setting: hp_ctl:%x hp_port:%x\n",
3534d252bf68SShubhangi Shrivastava 		      hotplug, enabled_irqs);
3535d252bf68SShubhangi Shrivastava 	hotplug &= ~BXT_DDI_HPD_INVERT_MASK;
3536d252bf68SShubhangi Shrivastava 
3537d252bf68SShubhangi Shrivastava 	/*
3538d252bf68SShubhangi Shrivastava 	 * For BXT invert bit has to be set based on AOB design
3539d252bf68SShubhangi Shrivastava 	 * for HPD detection logic, update it based on VBT fields.
3540d252bf68SShubhangi Shrivastava 	 */
3541d252bf68SShubhangi Shrivastava 
3542d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIA) &&
3543d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_A))
3544d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIA_HPD_INVERT;
3545d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIB) &&
3546d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_B))
3547d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIB_HPD_INVERT;
3548d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIC) &&
3549d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_C))
3550d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIC_HPD_INVERT;
3551d252bf68SShubhangi Shrivastava 
3552a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
3553e0a20ad7SShashank Sharma }
3554e0a20ad7SShashank Sharma 
3555d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
3556d46da437SPaulo Zanoni {
35572d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
355882a28bcfSDaniel Vetter 	u32 mask;
3559d46da437SPaulo Zanoni 
3560692a04cfSDaniel Vetter 	if (HAS_PCH_NOP(dev))
3561692a04cfSDaniel Vetter 		return;
3562692a04cfSDaniel Vetter 
3563105b122eSPaulo Zanoni 	if (HAS_PCH_IBX(dev))
35645c673b60SDaniel Vetter 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
3565105b122eSPaulo Zanoni 	else
35665c673b60SDaniel Vetter 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
35678664281bSPaulo Zanoni 
3568b51a2842SVille Syrjälä 	gen5_assert_iir_is_zero(dev_priv, SDEIIR);
3569d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
3570d46da437SPaulo Zanoni }
3571d46da437SPaulo Zanoni 
35720a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
35730a9a8c91SDaniel Vetter {
35740a9a8c91SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
35750a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
35760a9a8c91SDaniel Vetter 
35770a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
35780a9a8c91SDaniel Vetter 
35790a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
3580040d2baaSBen Widawsky 	if (HAS_L3_DPF(dev)) {
35810a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
358235a85ac6SBen Widawsky 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev);
358335a85ac6SBen Widawsky 		gt_irqs |= GT_PARITY_ERROR(dev);
35840a9a8c91SDaniel Vetter 	}
35850a9a8c91SDaniel Vetter 
35860a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
35870a9a8c91SDaniel Vetter 	if (IS_GEN5(dev)) {
35880a9a8c91SDaniel Vetter 		gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
35890a9a8c91SDaniel Vetter 			   ILK_BSD_USER_INTERRUPT;
35900a9a8c91SDaniel Vetter 	} else {
35910a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
35920a9a8c91SDaniel Vetter 	}
35930a9a8c91SDaniel Vetter 
359435079899SPaulo Zanoni 	GEN5_IRQ_INIT(GT, dev_priv->gt_irq_mask, gt_irqs);
35950a9a8c91SDaniel Vetter 
35960a9a8c91SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
359778e68d36SImre Deak 		/*
359878e68d36SImre Deak 		 * RPS interrupts will get enabled/disabled on demand when RPS
359978e68d36SImre Deak 		 * itself is enabled/disabled.
360078e68d36SImre Deak 		 */
36010a9a8c91SDaniel Vetter 		if (HAS_VEBOX(dev))
36020a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
36030a9a8c91SDaniel Vetter 
3604605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = 0xffffffff;
360535079899SPaulo Zanoni 		GEN5_IRQ_INIT(GEN6_PM, dev_priv->pm_irq_mask, pm_irqs);
36060a9a8c91SDaniel Vetter 	}
36070a9a8c91SDaniel Vetter }
36080a9a8c91SDaniel Vetter 
3609f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
3610036a4a7dSZhenyu Wang {
36112d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
36128e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
36138e76f8dcSPaulo Zanoni 
36148e76f8dcSPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 7) {
36158e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
36168e76f8dcSPaulo Zanoni 				DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
36178e76f8dcSPaulo Zanoni 				DE_PLANEB_FLIP_DONE_IVB |
36185c673b60SDaniel Vetter 				DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB);
36198e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
362023bb4cb5SVille Syrjälä 			      DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB |
362123bb4cb5SVille Syrjälä 			      DE_DP_A_HOTPLUG_IVB);
36228e76f8dcSPaulo Zanoni 	} else {
36238e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3624ce99c256SDaniel Vetter 				DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
36255b3a856bSDaniel Vetter 				DE_AUX_CHANNEL_A |
36265b3a856bSDaniel Vetter 				DE_PIPEB_CRC_DONE | DE_PIPEA_CRC_DONE |
36275b3a856bSDaniel Vetter 				DE_POISON);
3628e4ce95aaSVille Syrjälä 		extra_mask = (DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
3629e4ce95aaSVille Syrjälä 			      DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN |
3630e4ce95aaSVille Syrjälä 			      DE_DP_A_HOTPLUG);
36318e76f8dcSPaulo Zanoni 	}
3632036a4a7dSZhenyu Wang 
36331ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3634036a4a7dSZhenyu Wang 
36350c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xeffe);
36360c841212SPaulo Zanoni 
3637622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3638622364b6SPaulo Zanoni 
363935079899SPaulo Zanoni 	GEN5_IRQ_INIT(DE, dev_priv->irq_mask, display_mask | extra_mask);
3640036a4a7dSZhenyu Wang 
36410a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
3642036a4a7dSZhenyu Wang 
3643d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
36447fe0b973SKeith Packard 
3645f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
36466005ce42SDaniel Vetter 		/* Enable PCU event interrupts
36476005ce42SDaniel Vetter 		 *
36486005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
36494bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
36504bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
3651d6207435SDaniel Vetter 		spin_lock_irq(&dev_priv->irq_lock);
3652fbdedaeaSVille Syrjälä 		ilk_enable_display_irq(dev_priv, DE_PCU_EVENT);
3653d6207435SDaniel Vetter 		spin_unlock_irq(&dev_priv->irq_lock);
3654f97108d1SJesse Barnes 	}
3655f97108d1SJesse Barnes 
3656036a4a7dSZhenyu Wang 	return 0;
3657036a4a7dSZhenyu Wang }
3658036a4a7dSZhenyu Wang 
3659ad22d106SVille Syrjälä static void vlv_display_irq_postinstall(struct drm_i915_private *dev_priv)
3660f8b79e58SImre Deak {
3661f8b79e58SImre Deak 	u32 pipestat_mask;
3662f8b79e58SImre Deak 	u32 iir_mask;
3663120dda4fSVille Syrjälä 	enum pipe pipe;
3664f8b79e58SImre Deak 
3665f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3666f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3667f8b79e58SImre Deak 
3668120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3669120dda4fSVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), pipestat_mask);
3670f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3671f8b79e58SImre Deak 
3672f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3673f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3674f8b79e58SImre Deak 
3675120dda4fSVille Syrjälä 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3676120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3677120dda4fSVille Syrjälä 		      i915_enable_pipestat(dev_priv, pipe, pipestat_mask);
3678f8b79e58SImre Deak 
3679f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3680f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3681f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3682120dda4fSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
3683120dda4fSVille Syrjälä 		iir_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
3684f8b79e58SImre Deak 	dev_priv->irq_mask &= ~iir_mask;
3685f8b79e58SImre Deak 
3686f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3687f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3688f8b79e58SImre Deak 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
368976e41860SVille Syrjälä 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
369076e41860SVille Syrjälä 	POSTING_READ(VLV_IMR);
3691f8b79e58SImre Deak }
3692f8b79e58SImre Deak 
3693f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3694f8b79e58SImre Deak {
3695f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3696f8b79e58SImre Deak 
3697f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3698f8b79e58SImre Deak 		return;
3699f8b79e58SImre Deak 
3700f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3701f8b79e58SImre Deak 
3702950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3703ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3704f8b79e58SImre Deak }
3705f8b79e58SImre Deak 
3706f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3707f8b79e58SImre Deak {
3708f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3709f8b79e58SImre Deak 
3710f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3711f8b79e58SImre Deak 		return;
3712f8b79e58SImre Deak 
3713f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3714f8b79e58SImre Deak 
3715950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3716ad22d106SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3717f8b79e58SImre Deak }
3718f8b79e58SImre Deak 
37190e6c9a9eSVille Syrjälä 
37200e6c9a9eSVille Syrjälä static int valleyview_irq_postinstall(struct drm_device *dev)
37210e6c9a9eSVille Syrjälä {
37220e6c9a9eSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
37230e6c9a9eSVille Syrjälä 
37240a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
37257e231dbeSJesse Barnes 
37267e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
37277e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
37287e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
37297e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
37307e231dbeSJesse Barnes #endif
37317e231dbeSJesse Barnes 
3732ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
3733*9918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3734ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3735ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3736ad22d106SVille Syrjälä 
37377e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
373820afbda2SDaniel Vetter 
373920afbda2SDaniel Vetter 	return 0;
374020afbda2SDaniel Vetter }
374120afbda2SDaniel Vetter 
3742abd58f01SBen Widawsky static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
3743abd58f01SBen Widawsky {
3744abd58f01SBen Widawsky 	/* These are interrupts we'll toggle with the ring mask register */
3745abd58f01SBen Widawsky 	uint32_t gt_interrupts[] = {
3746abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
374773d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
3748abd58f01SBen Widawsky 			GT_RENDER_L3_PARITY_ERROR_INTERRUPT |
374973d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT |
375073d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT,
3751abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
375273d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
375373d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT |
375473d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT,
3755abd58f01SBen Widawsky 		0,
375673d477f6SOscar Mateo 		GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT |
375773d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT
3758abd58f01SBen Widawsky 		};
3759abd58f01SBen Widawsky 
37600961021aSBen Widawsky 	dev_priv->pm_irq_mask = 0xffffffff;
37619a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 0, ~gt_interrupts[0], gt_interrupts[0]);
37629a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 1, ~gt_interrupts[1], gt_interrupts[1]);
376378e68d36SImre Deak 	/*
376478e68d36SImre Deak 	 * RPS interrupts will get enabled/disabled on demand when RPS itself
376578e68d36SImre Deak 	 * is enabled/disabled.
376678e68d36SImre Deak 	 */
376778e68d36SImre Deak 	GEN8_IRQ_INIT_NDX(GT, 2, dev_priv->pm_irq_mask, 0);
37689a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 3, ~gt_interrupts[3], gt_interrupts[3]);
3769abd58f01SBen Widawsky }
3770abd58f01SBen Widawsky 
3771abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3772abd58f01SBen Widawsky {
3773770de83dSDamien Lespiau 	uint32_t de_pipe_masked = GEN8_PIPE_CDCLK_CRC_DONE;
3774770de83dSDamien Lespiau 	uint32_t de_pipe_enables;
37753a3b3c7dSVille Syrjälä 	u32 de_port_masked = GEN8_AUX_CHANNEL_A;
37763a3b3c7dSVille Syrjälä 	u32 de_port_enables;
37773a3b3c7dSVille Syrjälä 	enum pipe pipe;
3778770de83dSDamien Lespiau 
3779b4834a50SRodrigo Vivi 	if (INTEL_INFO(dev_priv)->gen >= 9) {
3780770de83dSDamien Lespiau 		de_pipe_masked |= GEN9_PIPE_PLANE1_FLIP_DONE |
3781770de83dSDamien Lespiau 				  GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
37823a3b3c7dSVille Syrjälä 		de_port_masked |= GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
378388e04703SJesse Barnes 				  GEN9_AUX_CHANNEL_D;
37849e63743eSShashank Sharma 		if (IS_BROXTON(dev_priv))
37853a3b3c7dSVille Syrjälä 			de_port_masked |= BXT_DE_PORT_GMBUS;
37863a3b3c7dSVille Syrjälä 	} else {
3787770de83dSDamien Lespiau 		de_pipe_masked |= GEN8_PIPE_PRIMARY_FLIP_DONE |
3788770de83dSDamien Lespiau 				  GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
37893a3b3c7dSVille Syrjälä 	}
3790770de83dSDamien Lespiau 
3791770de83dSDamien Lespiau 	de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
3792770de83dSDamien Lespiau 					   GEN8_PIPE_FIFO_UNDERRUN;
3793770de83dSDamien Lespiau 
37943a3b3c7dSVille Syrjälä 	de_port_enables = de_port_masked;
3795a52bb15bSVille Syrjälä 	if (IS_BROXTON(dev_priv))
3796a52bb15bSVille Syrjälä 		de_port_enables |= BXT_DE_PORT_HOTPLUG_MASK;
3797a52bb15bSVille Syrjälä 	else if (IS_BROADWELL(dev_priv))
37983a3b3c7dSVille Syrjälä 		de_port_enables |= GEN8_PORT_DP_A_HOTPLUG;
37993a3b3c7dSVille Syrjälä 
380013b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_A] = ~de_pipe_masked;
380113b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_B] = ~de_pipe_masked;
380213b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_C] = ~de_pipe_masked;
3803abd58f01SBen Widawsky 
3804055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3805f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3806813bde43SPaulo Zanoni 				POWER_DOMAIN_PIPE(pipe)))
3807813bde43SPaulo Zanoni 			GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
3808813bde43SPaulo Zanoni 					  dev_priv->de_irq_mask[pipe],
380935079899SPaulo Zanoni 					  de_pipe_enables);
3810abd58f01SBen Widawsky 
38113a3b3c7dSVille Syrjälä 	GEN5_IRQ_INIT(GEN8_DE_PORT_, ~de_port_masked, de_port_enables);
3812abd58f01SBen Widawsky }
3813abd58f01SBen Widawsky 
3814abd58f01SBen Widawsky static int gen8_irq_postinstall(struct drm_device *dev)
3815abd58f01SBen Widawsky {
3816abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3817abd58f01SBen Widawsky 
3818266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev))
3819622364b6SPaulo Zanoni 		ibx_irq_pre_postinstall(dev);
3820622364b6SPaulo Zanoni 
3821abd58f01SBen Widawsky 	gen8_gt_irq_postinstall(dev_priv);
3822abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3823abd58f01SBen Widawsky 
3824266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev))
3825abd58f01SBen Widawsky 		ibx_irq_postinstall(dev);
3826abd58f01SBen Widawsky 
3827abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
3828abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3829abd58f01SBen Widawsky 
3830abd58f01SBen Widawsky 	return 0;
3831abd58f01SBen Widawsky }
3832abd58f01SBen Widawsky 
383343f328d7SVille Syrjälä static int cherryview_irq_postinstall(struct drm_device *dev)
383443f328d7SVille Syrjälä {
383543f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
383643f328d7SVille Syrjälä 
383743f328d7SVille Syrjälä 	gen8_gt_irq_postinstall(dev_priv);
383843f328d7SVille Syrjälä 
3839ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
3840*9918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3841ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3842ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3843ad22d106SVille Syrjälä 
384443f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, MASTER_INTERRUPT_ENABLE);
384543f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
384643f328d7SVille Syrjälä 
384743f328d7SVille Syrjälä 	return 0;
384843f328d7SVille Syrjälä }
384943f328d7SVille Syrjälä 
3850abd58f01SBen Widawsky static void gen8_irq_uninstall(struct drm_device *dev)
3851abd58f01SBen Widawsky {
3852abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3853abd58f01SBen Widawsky 
3854abd58f01SBen Widawsky 	if (!dev_priv)
3855abd58f01SBen Widawsky 		return;
3856abd58f01SBen Widawsky 
3857823f6b38SPaulo Zanoni 	gen8_irq_reset(dev);
3858abd58f01SBen Widawsky }
3859abd58f01SBen Widawsky 
38607e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
38617e231dbeSJesse Barnes {
38622d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
38637e231dbeSJesse Barnes 
38647e231dbeSJesse Barnes 	if (!dev_priv)
38657e231dbeSJesse Barnes 		return;
38667e231dbeSJesse Barnes 
3867843d0e7dSImre Deak 	I915_WRITE(VLV_MASTER_IER, 0);
3868843d0e7dSImre Deak 
3869893fce8eSVille Syrjälä 	gen5_gt_irq_reset(dev);
3870893fce8eSVille Syrjälä 
38717e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
3872f8b79e58SImre Deak 
3873ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
3874*9918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3875ad22d106SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3876ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
38777e231dbeSJesse Barnes }
38787e231dbeSJesse Barnes 
387943f328d7SVille Syrjälä static void cherryview_irq_uninstall(struct drm_device *dev)
388043f328d7SVille Syrjälä {
388143f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
388243f328d7SVille Syrjälä 
388343f328d7SVille Syrjälä 	if (!dev_priv)
388443f328d7SVille Syrjälä 		return;
388543f328d7SVille Syrjälä 
388643f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
388743f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
388843f328d7SVille Syrjälä 
3889a2c30fbaSVille Syrjälä 	gen8_gt_irq_reset(dev_priv);
389043f328d7SVille Syrjälä 
3891a2c30fbaSVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
389243f328d7SVille Syrjälä 
3893ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
3894*9918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3895ad22d106SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3896ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
389743f328d7SVille Syrjälä }
389843f328d7SVille Syrjälä 
3899f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
3900036a4a7dSZhenyu Wang {
39012d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
39024697995bSJesse Barnes 
39034697995bSJesse Barnes 	if (!dev_priv)
39044697995bSJesse Barnes 		return;
39054697995bSJesse Barnes 
3906be30b29fSPaulo Zanoni 	ironlake_irq_reset(dev);
3907036a4a7dSZhenyu Wang }
3908036a4a7dSZhenyu Wang 
3909c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
3910c2798b19SChris Wilson {
39112d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3912c2798b19SChris Wilson 	int pipe;
3913c2798b19SChris Wilson 
3914055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3915c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3916c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3917c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3918c2798b19SChris Wilson 	POSTING_READ16(IER);
3919c2798b19SChris Wilson }
3920c2798b19SChris Wilson 
3921c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
3922c2798b19SChris Wilson {
39232d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3924c2798b19SChris Wilson 
3925c2798b19SChris Wilson 	I915_WRITE16(EMR,
3926c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3927c2798b19SChris Wilson 
3928c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
3929c2798b19SChris Wilson 	dev_priv->irq_mask =
3930c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3931c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3932c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
393337ef01abSDaniel Vetter 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
3934c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
3935c2798b19SChris Wilson 
3936c2798b19SChris Wilson 	I915_WRITE16(IER,
3937c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3938c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3939c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
3940c2798b19SChris Wilson 	POSTING_READ16(IER);
3941c2798b19SChris Wilson 
3942379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3943379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3944d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3945755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3946755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3947d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3948379ef82dSDaniel Vetter 
3949c2798b19SChris Wilson 	return 0;
3950c2798b19SChris Wilson }
3951c2798b19SChris Wilson 
395290a72f87SVille Syrjälä /*
395390a72f87SVille Syrjälä  * Returns true when a page flip has completed.
395490a72f87SVille Syrjälä  */
395590a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
39561f1c2e24SVille Syrjälä 			       int plane, int pipe, u32 iir)
395790a72f87SVille Syrjälä {
39582d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
39591f1c2e24SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
396090a72f87SVille Syrjälä 
39618d7849dbSVille Syrjälä 	if (!intel_pipe_handle_vblank(dev, pipe))
396290a72f87SVille Syrjälä 		return false;
396390a72f87SVille Syrjälä 
396490a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
3965d6bbafa1SChris Wilson 		goto check_page_flip;
396690a72f87SVille Syrjälä 
396790a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
396890a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
396990a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
397090a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
397190a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
397290a72f87SVille Syrjälä 	 */
397390a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
3974d6bbafa1SChris Wilson 		goto check_page_flip;
397590a72f87SVille Syrjälä 
39767d47559eSVille Syrjälä 	intel_prepare_page_flip(dev, plane);
397790a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
397890a72f87SVille Syrjälä 	return true;
3979d6bbafa1SChris Wilson 
3980d6bbafa1SChris Wilson check_page_flip:
3981d6bbafa1SChris Wilson 	intel_check_page_flip(dev, pipe);
3982d6bbafa1SChris Wilson 	return false;
398390a72f87SVille Syrjälä }
398490a72f87SVille Syrjälä 
3985ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3986c2798b19SChris Wilson {
398745a83f84SDaniel Vetter 	struct drm_device *dev = arg;
39882d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3989c2798b19SChris Wilson 	u16 iir, new_iir;
3990c2798b19SChris Wilson 	u32 pipe_stats[2];
3991c2798b19SChris Wilson 	int pipe;
3992c2798b19SChris Wilson 	u16 flip_mask =
3993c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3994c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
39951f814dacSImre Deak 	irqreturn_t ret;
3996c2798b19SChris Wilson 
39972dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
39982dd2a883SImre Deak 		return IRQ_NONE;
39992dd2a883SImre Deak 
40001f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
40011f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
40021f814dacSImre Deak 
40031f814dacSImre Deak 	ret = IRQ_NONE;
4004c2798b19SChris Wilson 	iir = I915_READ16(IIR);
4005c2798b19SChris Wilson 	if (iir == 0)
40061f814dacSImre Deak 		goto out;
4007c2798b19SChris Wilson 
4008c2798b19SChris Wilson 	while (iir & ~flip_mask) {
4009c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4010c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4011c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4012c2798b19SChris Wilson 		 * interrupts (for non-MSI).
4013c2798b19SChris Wilson 		 */
4014222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
4015c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4016aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4017c2798b19SChris Wilson 
4018055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4019f0f59a00SVille Syrjälä 			i915_reg_t reg = PIPESTAT(pipe);
4020c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4021c2798b19SChris Wilson 
4022c2798b19SChris Wilson 			/*
4023c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
4024c2798b19SChris Wilson 			 */
40252d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & 0x8000ffff)
4026c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
4027c2798b19SChris Wilson 		}
4028222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4029c2798b19SChris Wilson 
4030c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
4031c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
4032c2798b19SChris Wilson 
4033c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
40344a570db5STvrtko Ursulin 			notify_ring(&dev_priv->engine[RCS]);
4035c2798b19SChris Wilson 
4036055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
40371f1c2e24SVille Syrjälä 			int plane = pipe;
40383a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
40391f1c2e24SVille Syrjälä 				plane = !plane;
40401f1c2e24SVille Syrjälä 
40414356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
40421f1c2e24SVille Syrjälä 			    i8xx_handle_vblank(dev, plane, pipe, iir))
40431f1c2e24SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
4044c2798b19SChris Wilson 
40454356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
4046277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
40472d9d2b0bSVille Syrjälä 
40481f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
40491f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
40501f7247c0SDaniel Vetter 								    pipe);
40514356d586SDaniel Vetter 		}
4052c2798b19SChris Wilson 
4053c2798b19SChris Wilson 		iir = new_iir;
4054c2798b19SChris Wilson 	}
40551f814dacSImre Deak 	ret = IRQ_HANDLED;
4056c2798b19SChris Wilson 
40571f814dacSImre Deak out:
40581f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
40591f814dacSImre Deak 
40601f814dacSImre Deak 	return ret;
4061c2798b19SChris Wilson }
4062c2798b19SChris Wilson 
4063c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
4064c2798b19SChris Wilson {
40652d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4066c2798b19SChris Wilson 	int pipe;
4067c2798b19SChris Wilson 
4068055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
4069c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
4070c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4071c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
4072c2798b19SChris Wilson 	}
4073c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
4074c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
4075c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
4076c2798b19SChris Wilson }
4077c2798b19SChris Wilson 
4078a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
4079a266c7d5SChris Wilson {
40802d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4081a266c7d5SChris Wilson 	int pipe;
4082a266c7d5SChris Wilson 
4083a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
40840706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4085a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4086a266c7d5SChris Wilson 	}
4087a266c7d5SChris Wilson 
408800d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
4089055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4090a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4091a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4092a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4093a266c7d5SChris Wilson 	POSTING_READ(IER);
4094a266c7d5SChris Wilson }
4095a266c7d5SChris Wilson 
4096a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
4097a266c7d5SChris Wilson {
40982d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
409938bde180SChris Wilson 	u32 enable_mask;
4100a266c7d5SChris Wilson 
410138bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
410238bde180SChris Wilson 
410338bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
410438bde180SChris Wilson 	dev_priv->irq_mask =
410538bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
410638bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
410738bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
410838bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
410937ef01abSDaniel Vetter 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
411038bde180SChris Wilson 
411138bde180SChris Wilson 	enable_mask =
411238bde180SChris Wilson 		I915_ASLE_INTERRUPT |
411338bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
411438bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
411538bde180SChris Wilson 		I915_USER_INTERRUPT;
411638bde180SChris Wilson 
4117a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
41180706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
411920afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
412020afbda2SDaniel Vetter 
4121a266c7d5SChris Wilson 		/* Enable in IER... */
4122a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
4123a266c7d5SChris Wilson 		/* and unmask in IMR */
4124a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
4125a266c7d5SChris Wilson 	}
4126a266c7d5SChris Wilson 
4127a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
4128a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
4129a266c7d5SChris Wilson 	POSTING_READ(IER);
4130a266c7d5SChris Wilson 
4131f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
413220afbda2SDaniel Vetter 
4133379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4134379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4135d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4136755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4137755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4138d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4139379ef82dSDaniel Vetter 
414020afbda2SDaniel Vetter 	return 0;
414120afbda2SDaniel Vetter }
414220afbda2SDaniel Vetter 
414390a72f87SVille Syrjälä /*
414490a72f87SVille Syrjälä  * Returns true when a page flip has completed.
414590a72f87SVille Syrjälä  */
414690a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
414790a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
414890a72f87SVille Syrjälä {
41492d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
415090a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
415190a72f87SVille Syrjälä 
41528d7849dbSVille Syrjälä 	if (!intel_pipe_handle_vblank(dev, pipe))
415390a72f87SVille Syrjälä 		return false;
415490a72f87SVille Syrjälä 
415590a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
4156d6bbafa1SChris Wilson 		goto check_page_flip;
415790a72f87SVille Syrjälä 
415890a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
415990a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
416090a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
416190a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
416290a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
416390a72f87SVille Syrjälä 	 */
416490a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
4165d6bbafa1SChris Wilson 		goto check_page_flip;
416690a72f87SVille Syrjälä 
41677d47559eSVille Syrjälä 	intel_prepare_page_flip(dev, plane);
416890a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
416990a72f87SVille Syrjälä 	return true;
4170d6bbafa1SChris Wilson 
4171d6bbafa1SChris Wilson check_page_flip:
4172d6bbafa1SChris Wilson 	intel_check_page_flip(dev, pipe);
4173d6bbafa1SChris Wilson 	return false;
417490a72f87SVille Syrjälä }
417590a72f87SVille Syrjälä 
4176ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
4177a266c7d5SChris Wilson {
417845a83f84SDaniel Vetter 	struct drm_device *dev = arg;
41792d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
41808291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
418138bde180SChris Wilson 	u32 flip_mask =
418238bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
418338bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
418438bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
4185a266c7d5SChris Wilson 
41862dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
41872dd2a883SImre Deak 		return IRQ_NONE;
41882dd2a883SImre Deak 
41891f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
41901f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
41911f814dacSImre Deak 
4192a266c7d5SChris Wilson 	iir = I915_READ(IIR);
419338bde180SChris Wilson 	do {
419438bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
41958291ee90SChris Wilson 		bool blc_event = false;
4196a266c7d5SChris Wilson 
4197a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4198a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4199a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4200a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
4201a266c7d5SChris Wilson 		 */
4202222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
4203a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4204aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4205a266c7d5SChris Wilson 
4206055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4207f0f59a00SVille Syrjälä 			i915_reg_t reg = PIPESTAT(pipe);
4208a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4209a266c7d5SChris Wilson 
421038bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
4211a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4212a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
421338bde180SChris Wilson 				irq_received = true;
4214a266c7d5SChris Wilson 			}
4215a266c7d5SChris Wilson 		}
4216222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4217a266c7d5SChris Wilson 
4218a266c7d5SChris Wilson 		if (!irq_received)
4219a266c7d5SChris Wilson 			break;
4220a266c7d5SChris Wilson 
4221a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
422216c6c56bSVille Syrjälä 		if (I915_HAS_HOTPLUG(dev) &&
422316c6c56bSVille Syrjälä 		    iir & I915_DISPLAY_PORT_INTERRUPT)
422416c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
4225a266c7d5SChris Wilson 
422638bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
4227a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4228a266c7d5SChris Wilson 
4229a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
42304a570db5STvrtko Ursulin 			notify_ring(&dev_priv->engine[RCS]);
4231a266c7d5SChris Wilson 
4232055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
423338bde180SChris Wilson 			int plane = pipe;
42343a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
423538bde180SChris Wilson 				plane = !plane;
42365e2032d4SVille Syrjälä 
423790a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
423890a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
423990a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
4240a266c7d5SChris Wilson 
4241a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4242a266c7d5SChris Wilson 				blc_event = true;
42434356d586SDaniel Vetter 
42444356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
4245277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
42462d9d2b0bSVille Syrjälä 
42471f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
42481f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
42491f7247c0SDaniel Vetter 								    pipe);
4250a266c7d5SChris Wilson 		}
4251a266c7d5SChris Wilson 
4252a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
4253a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
4254a266c7d5SChris Wilson 
4255a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4256a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4257a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4258a266c7d5SChris Wilson 		 * we would never get another interrupt.
4259a266c7d5SChris Wilson 		 *
4260a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4261a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4262a266c7d5SChris Wilson 		 * another one.
4263a266c7d5SChris Wilson 		 *
4264a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4265a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4266a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4267a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4268a266c7d5SChris Wilson 		 * stray interrupts.
4269a266c7d5SChris Wilson 		 */
427038bde180SChris Wilson 		ret = IRQ_HANDLED;
4271a266c7d5SChris Wilson 		iir = new_iir;
427238bde180SChris Wilson 	} while (iir & ~flip_mask);
4273a266c7d5SChris Wilson 
42741f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
42751f814dacSImre Deak 
4276a266c7d5SChris Wilson 	return ret;
4277a266c7d5SChris Wilson }
4278a266c7d5SChris Wilson 
4279a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
4280a266c7d5SChris Wilson {
42812d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4282a266c7d5SChris Wilson 	int pipe;
4283a266c7d5SChris Wilson 
4284a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
42850706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4286a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4287a266c7d5SChris Wilson 	}
4288a266c7d5SChris Wilson 
428900d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
4290055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
429155b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
4292a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
429355b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
429455b39755SChris Wilson 	}
4295a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4296a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4297a266c7d5SChris Wilson 
4298a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4299a266c7d5SChris Wilson }
4300a266c7d5SChris Wilson 
4301a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
4302a266c7d5SChris Wilson {
43032d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4304a266c7d5SChris Wilson 	int pipe;
4305a266c7d5SChris Wilson 
43060706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4307a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4308a266c7d5SChris Wilson 
4309a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
4310055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4311a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4312a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4313a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4314a266c7d5SChris Wilson 	POSTING_READ(IER);
4315a266c7d5SChris Wilson }
4316a266c7d5SChris Wilson 
4317a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
4318a266c7d5SChris Wilson {
43192d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4320bbba0a97SChris Wilson 	u32 enable_mask;
4321a266c7d5SChris Wilson 	u32 error_mask;
4322a266c7d5SChris Wilson 
4323a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
4324bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
4325adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
4326bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4327bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
4328bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
4329bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
4330bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
4331bbba0a97SChris Wilson 
4332bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
433321ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
433421ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
4335bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
4336bbba0a97SChris Wilson 
4337bbba0a97SChris Wilson 	if (IS_G4X(dev))
4338bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
4339a266c7d5SChris Wilson 
4340b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4341b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4342d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4343755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
4344755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4345755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4346d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4347a266c7d5SChris Wilson 
4348a266c7d5SChris Wilson 	/*
4349a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
4350a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
4351a266c7d5SChris Wilson 	 */
4352a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
4353a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
4354a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
4355a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
4356a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4357a266c7d5SChris Wilson 	} else {
4358a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
4359a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4360a266c7d5SChris Wilson 	}
4361a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
4362a266c7d5SChris Wilson 
4363a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
4364a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
4365a266c7d5SChris Wilson 	POSTING_READ(IER);
4366a266c7d5SChris Wilson 
43670706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
436820afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
436920afbda2SDaniel Vetter 
4370f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
437120afbda2SDaniel Vetter 
437220afbda2SDaniel Vetter 	return 0;
437320afbda2SDaniel Vetter }
437420afbda2SDaniel Vetter 
4375bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
437620afbda2SDaniel Vetter {
43772d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
437820afbda2SDaniel Vetter 	u32 hotplug_en;
437920afbda2SDaniel Vetter 
4380b5ea2d56SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
4381b5ea2d56SDaniel Vetter 
4382adca4730SChris Wilson 	/* Note HDMI and DP share hotplug bits */
4383e5868a31SEgbert Eich 	/* enable bits are the same for all generations */
43840706f17cSEgbert Eich 	hotplug_en = intel_hpd_enabled_irqs(dev, hpd_mask_i915);
4385a266c7d5SChris Wilson 	/* Programming the CRT detection parameters tends
4386a266c7d5SChris Wilson 	   to generate a spurious hotplug event about three
4387a266c7d5SChris Wilson 	   seconds later.  So just do it once.
4388a266c7d5SChris Wilson 	*/
4389a266c7d5SChris Wilson 	if (IS_G4X(dev))
4390a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
4391a266c7d5SChris Wilson 	hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
4392a266c7d5SChris Wilson 
4393a266c7d5SChris Wilson 	/* Ignore TV since it's buggy */
43940706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv,
4395f9e3dc78SJani Nikula 					     HOTPLUG_INT_EN_MASK |
4396f9e3dc78SJani Nikula 					     CRT_HOTPLUG_VOLTAGE_COMPARE_MASK |
4397f9e3dc78SJani Nikula 					     CRT_HOTPLUG_ACTIVATION_PERIOD_64,
43980706f17cSEgbert Eich 					     hotplug_en);
4399a266c7d5SChris Wilson }
4400a266c7d5SChris Wilson 
4401ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
4402a266c7d5SChris Wilson {
440345a83f84SDaniel Vetter 	struct drm_device *dev = arg;
44042d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4405a266c7d5SChris Wilson 	u32 iir, new_iir;
4406a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
4407a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
440821ad8330SVille Syrjälä 	u32 flip_mask =
440921ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
441021ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
4411a266c7d5SChris Wilson 
44122dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
44132dd2a883SImre Deak 		return IRQ_NONE;
44142dd2a883SImre Deak 
44151f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
44161f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
44171f814dacSImre Deak 
4418a266c7d5SChris Wilson 	iir = I915_READ(IIR);
4419a266c7d5SChris Wilson 
4420a266c7d5SChris Wilson 	for (;;) {
4421501e01d7SVille Syrjälä 		bool irq_received = (iir & ~flip_mask) != 0;
44222c8ba29fSChris Wilson 		bool blc_event = false;
44232c8ba29fSChris Wilson 
4424a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4425a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4426a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4427a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
4428a266c7d5SChris Wilson 		 */
4429222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
4430a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4431aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4432a266c7d5SChris Wilson 
4433055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4434f0f59a00SVille Syrjälä 			i915_reg_t reg = PIPESTAT(pipe);
4435a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4436a266c7d5SChris Wilson 
4437a266c7d5SChris Wilson 			/*
4438a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
4439a266c7d5SChris Wilson 			 */
4440a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4441a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
4442501e01d7SVille Syrjälä 				irq_received = true;
4443a266c7d5SChris Wilson 			}
4444a266c7d5SChris Wilson 		}
4445222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4446a266c7d5SChris Wilson 
4447a266c7d5SChris Wilson 		if (!irq_received)
4448a266c7d5SChris Wilson 			break;
4449a266c7d5SChris Wilson 
4450a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
4451a266c7d5SChris Wilson 
4452a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
445316c6c56bSVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
445416c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
4455a266c7d5SChris Wilson 
445621ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
4457a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4458a266c7d5SChris Wilson 
4459a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
44604a570db5STvrtko Ursulin 			notify_ring(&dev_priv->engine[RCS]);
4461a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
44624a570db5STvrtko Ursulin 			notify_ring(&dev_priv->engine[VCS]);
4463a266c7d5SChris Wilson 
4464055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
44652c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
446690a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
446790a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
4468a266c7d5SChris Wilson 
4469a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4470a266c7d5SChris Wilson 				blc_event = true;
44714356d586SDaniel Vetter 
44724356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
4473277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
4474a266c7d5SChris Wilson 
44751f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
44761f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
44772d9d2b0bSVille Syrjälä 		}
4478a266c7d5SChris Wilson 
4479a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
4480a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
4481a266c7d5SChris Wilson 
4482515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
4483515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
4484515ac2bbSDaniel Vetter 
4485a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4486a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4487a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4488a266c7d5SChris Wilson 		 * we would never get another interrupt.
4489a266c7d5SChris Wilson 		 *
4490a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4491a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4492a266c7d5SChris Wilson 		 * another one.
4493a266c7d5SChris Wilson 		 *
4494a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4495a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4496a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4497a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4498a266c7d5SChris Wilson 		 * stray interrupts.
4499a266c7d5SChris Wilson 		 */
4500a266c7d5SChris Wilson 		iir = new_iir;
4501a266c7d5SChris Wilson 	}
4502a266c7d5SChris Wilson 
45031f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
45041f814dacSImre Deak 
4505a266c7d5SChris Wilson 	return ret;
4506a266c7d5SChris Wilson }
4507a266c7d5SChris Wilson 
4508a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
4509a266c7d5SChris Wilson {
45102d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4511a266c7d5SChris Wilson 	int pipe;
4512a266c7d5SChris Wilson 
4513a266c7d5SChris Wilson 	if (!dev_priv)
4514a266c7d5SChris Wilson 		return;
4515a266c7d5SChris Wilson 
45160706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4517a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4518a266c7d5SChris Wilson 
4519a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
4520055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4521a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4522a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4523a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4524a266c7d5SChris Wilson 
4525055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4526a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
4527a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
4528a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4529a266c7d5SChris Wilson }
4530a266c7d5SChris Wilson 
4531fca52a55SDaniel Vetter /**
4532fca52a55SDaniel Vetter  * intel_irq_init - initializes irq support
4533fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4534fca52a55SDaniel Vetter  *
4535fca52a55SDaniel Vetter  * This function initializes all the irq support including work items, timers
4536fca52a55SDaniel Vetter  * and all the vtables. It does not setup the interrupt itself though.
4537fca52a55SDaniel Vetter  */
4538b963291cSDaniel Vetter void intel_irq_init(struct drm_i915_private *dev_priv)
4539f71d4af4SJesse Barnes {
4540b963291cSDaniel Vetter 	struct drm_device *dev = dev_priv->dev;
45418b2e326dSChris Wilson 
454277913b39SJani Nikula 	intel_hpd_init_work(dev_priv);
454377913b39SJani Nikula 
4544c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
4545a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
45468b2e326dSChris Wilson 
4547a6706b45SDeepak S 	/* Let's track the enabled rps events */
4548666a4537SWayne Boyer 	if (IS_VALLEYVIEW(dev_priv))
45496c65a587SVille Syrjälä 		/* WaGsvRC0ResidencyMethod:vlv */
45506f4b12f8SChris Wilson 		dev_priv->pm_rps_events = GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED;
455131685c25SDeepak S 	else
4552a6706b45SDeepak S 		dev_priv->pm_rps_events = GEN6_PM_RPS_EVENTS;
4553a6706b45SDeepak S 
4554737b1506SChris Wilson 	INIT_DELAYED_WORK(&dev_priv->gpu_error.hangcheck_work,
4555737b1506SChris Wilson 			  i915_hangcheck_elapsed);
455661bac78eSDaniel Vetter 
4557b963291cSDaniel Vetter 	if (IS_GEN2(dev_priv)) {
45584cdb83ecSVille Syrjälä 		dev->max_vblank_count = 0;
45594cdb83ecSVille Syrjälä 		dev->driver->get_vblank_counter = i8xx_get_vblank_counter;
4560b963291cSDaniel Vetter 	} else if (IS_G4X(dev_priv) || INTEL_INFO(dev_priv)->gen >= 5) {
4561f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
4562fd8f507cSVille Syrjälä 		dev->driver->get_vblank_counter = g4x_get_vblank_counter;
4563391f75e2SVille Syrjälä 	} else {
4564391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
4565391f75e2SVille Syrjälä 		dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
4566f71d4af4SJesse Barnes 	}
4567f71d4af4SJesse Barnes 
456821da2700SVille Syrjälä 	/*
456921da2700SVille Syrjälä 	 * Opt out of the vblank disable timer on everything except gen2.
457021da2700SVille Syrjälä 	 * Gen2 doesn't have a hardware frame counter and so depends on
457121da2700SVille Syrjälä 	 * vblank interrupts to produce sane vblank seuquence numbers.
457221da2700SVille Syrjälä 	 */
4573b963291cSDaniel Vetter 	if (!IS_GEN2(dev_priv))
457421da2700SVille Syrjälä 		dev->vblank_disable_immediate = true;
457521da2700SVille Syrjälä 
4576f71d4af4SJesse Barnes 	dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
4577f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
4578f71d4af4SJesse Barnes 
4579b963291cSDaniel Vetter 	if (IS_CHERRYVIEW(dev_priv)) {
458043f328d7SVille Syrjälä 		dev->driver->irq_handler = cherryview_irq_handler;
458143f328d7SVille Syrjälä 		dev->driver->irq_preinstall = cherryview_irq_preinstall;
458243f328d7SVille Syrjälä 		dev->driver->irq_postinstall = cherryview_irq_postinstall;
458343f328d7SVille Syrjälä 		dev->driver->irq_uninstall = cherryview_irq_uninstall;
458443f328d7SVille Syrjälä 		dev->driver->enable_vblank = valleyview_enable_vblank;
458543f328d7SVille Syrjälä 		dev->driver->disable_vblank = valleyview_disable_vblank;
458643f328d7SVille Syrjälä 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4587b963291cSDaniel Vetter 	} else if (IS_VALLEYVIEW(dev_priv)) {
45887e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
45897e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
45907e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
45917e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
45927e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
45937e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
4594fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4595b963291cSDaniel Vetter 	} else if (INTEL_INFO(dev_priv)->gen >= 8) {
4596abd58f01SBen Widawsky 		dev->driver->irq_handler = gen8_irq_handler;
4597723761b8SDaniel Vetter 		dev->driver->irq_preinstall = gen8_irq_reset;
4598abd58f01SBen Widawsky 		dev->driver->irq_postinstall = gen8_irq_postinstall;
4599abd58f01SBen Widawsky 		dev->driver->irq_uninstall = gen8_irq_uninstall;
4600abd58f01SBen Widawsky 		dev->driver->enable_vblank = gen8_enable_vblank;
4601abd58f01SBen Widawsky 		dev->driver->disable_vblank = gen8_disable_vblank;
46026dbf30ceSVille Syrjälä 		if (IS_BROXTON(dev))
4603e0a20ad7SShashank Sharma 			dev_priv->display.hpd_irq_setup = bxt_hpd_irq_setup;
46046dbf30ceSVille Syrjälä 		else if (HAS_PCH_SPT(dev))
46056dbf30ceSVille Syrjälä 			dev_priv->display.hpd_irq_setup = spt_hpd_irq_setup;
46066dbf30ceSVille Syrjälä 		else
46073a3b3c7dSVille Syrjälä 			dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
4608f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
4609f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
4610723761b8SDaniel Vetter 		dev->driver->irq_preinstall = ironlake_irq_reset;
4611f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
4612f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
4613f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
4614f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
4615e4ce95aaSVille Syrjälä 		dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
4616f71d4af4SJesse Barnes 	} else {
4617b963291cSDaniel Vetter 		if (INTEL_INFO(dev_priv)->gen == 2) {
4618c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
4619c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
4620c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
4621c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
4622b963291cSDaniel Vetter 		} else if (INTEL_INFO(dev_priv)->gen == 3) {
4623a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
4624a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
4625a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
4626a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
4627c2798b19SChris Wilson 		} else {
4628a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
4629a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
4630a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
4631a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
4632c2798b19SChris Wilson 		}
4633778eb334SVille Syrjälä 		if (I915_HAS_HOTPLUG(dev_priv))
4634778eb334SVille Syrjälä 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4635f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
4636f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
4637f71d4af4SJesse Barnes 	}
4638f71d4af4SJesse Barnes }
463920afbda2SDaniel Vetter 
4640fca52a55SDaniel Vetter /**
4641fca52a55SDaniel Vetter  * intel_irq_install - enables the hardware interrupt
4642fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4643fca52a55SDaniel Vetter  *
4644fca52a55SDaniel Vetter  * This function enables the hardware interrupt handling, but leaves the hotplug
4645fca52a55SDaniel Vetter  * handling still disabled. It is called after intel_irq_init().
4646fca52a55SDaniel Vetter  *
4647fca52a55SDaniel Vetter  * In the driver load and resume code we need working interrupts in a few places
4648fca52a55SDaniel Vetter  * but don't want to deal with the hassle of concurrent probe and hotplug
4649fca52a55SDaniel Vetter  * workers. Hence the split into this two-stage approach.
4650fca52a55SDaniel Vetter  */
46512aeb7d3aSDaniel Vetter int intel_irq_install(struct drm_i915_private *dev_priv)
46522aeb7d3aSDaniel Vetter {
46532aeb7d3aSDaniel Vetter 	/*
46542aeb7d3aSDaniel Vetter 	 * We enable some interrupt sources in our postinstall hooks, so mark
46552aeb7d3aSDaniel Vetter 	 * interrupts as enabled _before_ actually enabling them to avoid
46562aeb7d3aSDaniel Vetter 	 * special cases in our ordering checks.
46572aeb7d3aSDaniel Vetter 	 */
46582aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
46592aeb7d3aSDaniel Vetter 
46602aeb7d3aSDaniel Vetter 	return drm_irq_install(dev_priv->dev, dev_priv->dev->pdev->irq);
46612aeb7d3aSDaniel Vetter }
46622aeb7d3aSDaniel Vetter 
4663fca52a55SDaniel Vetter /**
4664fca52a55SDaniel Vetter  * intel_irq_uninstall - finilizes all irq handling
4665fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4666fca52a55SDaniel Vetter  *
4667fca52a55SDaniel Vetter  * This stops interrupt and hotplug handling and unregisters and frees all
4668fca52a55SDaniel Vetter  * resources acquired in the init functions.
4669fca52a55SDaniel Vetter  */
46702aeb7d3aSDaniel Vetter void intel_irq_uninstall(struct drm_i915_private *dev_priv)
46712aeb7d3aSDaniel Vetter {
46722aeb7d3aSDaniel Vetter 	drm_irq_uninstall(dev_priv->dev);
46732aeb7d3aSDaniel Vetter 	intel_hpd_cancel_work(dev_priv);
46742aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
46752aeb7d3aSDaniel Vetter }
46762aeb7d3aSDaniel Vetter 
4677fca52a55SDaniel Vetter /**
4678fca52a55SDaniel Vetter  * intel_runtime_pm_disable_interrupts - runtime interrupt disabling
4679fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4680fca52a55SDaniel Vetter  *
4681fca52a55SDaniel Vetter  * This function is used to disable interrupts at runtime, both in the runtime
4682fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4683fca52a55SDaniel Vetter  */
4684b963291cSDaniel Vetter void intel_runtime_pm_disable_interrupts(struct drm_i915_private *dev_priv)
4685c67a470bSPaulo Zanoni {
4686b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_uninstall(dev_priv->dev);
46872aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
46882dd2a883SImre Deak 	synchronize_irq(dev_priv->dev->irq);
4689c67a470bSPaulo Zanoni }
4690c67a470bSPaulo Zanoni 
4691fca52a55SDaniel Vetter /**
4692fca52a55SDaniel Vetter  * intel_runtime_pm_enable_interrupts - runtime interrupt enabling
4693fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4694fca52a55SDaniel Vetter  *
4695fca52a55SDaniel Vetter  * This function is used to enable interrupts at runtime, both in the runtime
4696fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4697fca52a55SDaniel Vetter  */
4698b963291cSDaniel Vetter void intel_runtime_pm_enable_interrupts(struct drm_i915_private *dev_priv)
4699c67a470bSPaulo Zanoni {
47002aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
4701b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_preinstall(dev_priv->dev);
4702b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_postinstall(dev_priv->dev);
4703c67a470bSPaulo Zanoni }
4704