xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 666a45379e2c29bc16e60648e5ad8f6f8b7fa6ce)
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 
584*666a4537SWayne 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 
598*666a4537SWayne 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 
99774cdb337SChris Wilson static void notify_ring(struct intel_engine_cs *ring)
998549f7365SChris Wilson {
99993b0a4e0SOscar Mateo 	if (!intel_ring_initialized(ring))
1000475553deSChris Wilson 		return;
1001475553deSChris Wilson 
1002bcfcc8baSJohn Harrison 	trace_i915_gem_request_notify(ring);
10039862e600SChris Wilson 
1004549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
1005549f7365SChris Wilson }
1006549f7365SChris Wilson 
100743cf3bf0SChris Wilson static void vlv_c0_read(struct drm_i915_private *dev_priv,
100843cf3bf0SChris Wilson 			struct intel_rps_ei *ei)
100931685c25SDeepak S {
101043cf3bf0SChris Wilson 	ei->cz_clock = vlv_punit_read(dev_priv, PUNIT_REG_CZ_TIMESTAMP);
101143cf3bf0SChris Wilson 	ei->render_c0 = I915_READ(VLV_RENDER_C0_COUNT);
101243cf3bf0SChris Wilson 	ei->media_c0 = I915_READ(VLV_MEDIA_C0_COUNT);
101331685c25SDeepak S }
101431685c25SDeepak S 
101543cf3bf0SChris Wilson static bool vlv_c0_above(struct drm_i915_private *dev_priv,
101643cf3bf0SChris Wilson 			 const struct intel_rps_ei *old,
101743cf3bf0SChris Wilson 			 const struct intel_rps_ei *now,
101843cf3bf0SChris Wilson 			 int threshold)
101931685c25SDeepak S {
102043cf3bf0SChris Wilson 	u64 time, c0;
10217bad74d5SVille Syrjälä 	unsigned int mul = 100;
102231685c25SDeepak S 
102343cf3bf0SChris Wilson 	if (old->cz_clock == 0)
102443cf3bf0SChris Wilson 		return false;
102531685c25SDeepak S 
10267bad74d5SVille Syrjälä 	if (I915_READ(VLV_COUNTER_CONTROL) & VLV_COUNT_RANGE_HIGH)
10277bad74d5SVille Syrjälä 		mul <<= 8;
10287bad74d5SVille Syrjälä 
102943cf3bf0SChris Wilson 	time = now->cz_clock - old->cz_clock;
10307bad74d5SVille Syrjälä 	time *= threshold * dev_priv->czclk_freq;
103131685c25SDeepak S 
103243cf3bf0SChris Wilson 	/* Workload can be split between render + media, e.g. SwapBuffers
103343cf3bf0SChris Wilson 	 * being blitted in X after being rendered in mesa. To account for
103443cf3bf0SChris Wilson 	 * this we need to combine both engines into our activity counter.
103543cf3bf0SChris Wilson 	 */
103643cf3bf0SChris Wilson 	c0 = now->render_c0 - old->render_c0;
103743cf3bf0SChris Wilson 	c0 += now->media_c0 - old->media_c0;
10387bad74d5SVille Syrjälä 	c0 *= mul * VLV_CZ_CLOCK_TO_MILLI_SEC;
103931685c25SDeepak S 
104043cf3bf0SChris Wilson 	return c0 >= time;
104131685c25SDeepak S }
104231685c25SDeepak S 
104343cf3bf0SChris Wilson void gen6_rps_reset_ei(struct drm_i915_private *dev_priv)
104443cf3bf0SChris Wilson {
104543cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &dev_priv->rps.down_ei);
104643cf3bf0SChris Wilson 	dev_priv->rps.up_ei = dev_priv->rps.down_ei;
104743cf3bf0SChris Wilson }
104843cf3bf0SChris Wilson 
104943cf3bf0SChris Wilson static u32 vlv_wa_c0_ei(struct drm_i915_private *dev_priv, u32 pm_iir)
105043cf3bf0SChris Wilson {
105143cf3bf0SChris Wilson 	struct intel_rps_ei now;
105243cf3bf0SChris Wilson 	u32 events = 0;
105343cf3bf0SChris Wilson 
10546f4b12f8SChris Wilson 	if ((pm_iir & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED)) == 0)
105543cf3bf0SChris Wilson 		return 0;
105643cf3bf0SChris Wilson 
105743cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &now);
105843cf3bf0SChris Wilson 	if (now.cz_clock == 0)
105943cf3bf0SChris Wilson 		return 0;
106031685c25SDeepak S 
106143cf3bf0SChris Wilson 	if (pm_iir & GEN6_PM_RP_DOWN_EI_EXPIRED) {
106243cf3bf0SChris Wilson 		if (!vlv_c0_above(dev_priv,
106343cf3bf0SChris Wilson 				  &dev_priv->rps.down_ei, &now,
10648fb55197SChris Wilson 				  dev_priv->rps.down_threshold))
106543cf3bf0SChris Wilson 			events |= GEN6_PM_RP_DOWN_THRESHOLD;
106643cf3bf0SChris Wilson 		dev_priv->rps.down_ei = now;
106731685c25SDeepak S 	}
106831685c25SDeepak S 
106943cf3bf0SChris Wilson 	if (pm_iir & GEN6_PM_RP_UP_EI_EXPIRED) {
107043cf3bf0SChris Wilson 		if (vlv_c0_above(dev_priv,
107143cf3bf0SChris Wilson 				 &dev_priv->rps.up_ei, &now,
10728fb55197SChris Wilson 				 dev_priv->rps.up_threshold))
107343cf3bf0SChris Wilson 			events |= GEN6_PM_RP_UP_THRESHOLD;
107443cf3bf0SChris Wilson 		dev_priv->rps.up_ei = now;
107543cf3bf0SChris Wilson 	}
107643cf3bf0SChris Wilson 
107743cf3bf0SChris Wilson 	return events;
107831685c25SDeepak S }
107931685c25SDeepak S 
1080f5a4c67dSChris Wilson static bool any_waiters(struct drm_i915_private *dev_priv)
1081f5a4c67dSChris Wilson {
1082f5a4c67dSChris Wilson 	struct intel_engine_cs *ring;
1083f5a4c67dSChris Wilson 	int i;
1084f5a4c67dSChris Wilson 
1085f5a4c67dSChris Wilson 	for_each_ring(ring, dev_priv, i)
1086f5a4c67dSChris Wilson 		if (ring->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 	}
1106c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
1107c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
1108a72fbc3aSImre Deak 	/* Make sure not to corrupt PMIMR state used by ringbuffer on GEN6 */
1109480c8033SDaniel Vetter 	gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
11108d3afd7dSChris Wilson 	client_boost = dev_priv->rps.client_boost;
11118d3afd7dSChris Wilson 	dev_priv->rps.client_boost = false;
111259cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
11134912d041SBen Widawsky 
111460611c13SPaulo Zanoni 	/* Make sure we didn't queue anything we're not going to process. */
1115a6706b45SDeepak S 	WARN_ON(pm_iir & ~dev_priv->pm_rps_events);
111660611c13SPaulo Zanoni 
11178d3afd7dSChris Wilson 	if ((pm_iir & dev_priv->pm_rps_events) == 0 && !client_boost)
11183b8d8d91SJesse Barnes 		return;
11193b8d8d91SJesse Barnes 
11204fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
11217b9e0ae6SChris Wilson 
112243cf3bf0SChris Wilson 	pm_iir |= vlv_wa_c0_ei(dev_priv, pm_iir);
112343cf3bf0SChris Wilson 
1124dd75fdc8SChris Wilson 	adj = dev_priv->rps.last_adj;
1125edcf284bSChris Wilson 	new_delay = dev_priv->rps.cur_freq;
11268d3afd7dSChris Wilson 	min = dev_priv->rps.min_freq_softlimit;
11278d3afd7dSChris Wilson 	max = dev_priv->rps.max_freq_softlimit;
11288d3afd7dSChris Wilson 
11298d3afd7dSChris Wilson 	if (client_boost) {
11308d3afd7dSChris Wilson 		new_delay = dev_priv->rps.max_freq_softlimit;
11318d3afd7dSChris Wilson 		adj = 0;
11328d3afd7dSChris Wilson 	} else if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
1133dd75fdc8SChris Wilson 		if (adj > 0)
1134dd75fdc8SChris Wilson 			adj *= 2;
1135edcf284bSChris Wilson 		else /* CHV needs even encode values */
1136edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? 2 : 1;
11377425034aSVille Syrjälä 		/*
11387425034aSVille Syrjälä 		 * For better performance, jump directly
11397425034aSVille Syrjälä 		 * to RPe if we're below it.
11407425034aSVille Syrjälä 		 */
1141edcf284bSChris Wilson 		if (new_delay < dev_priv->rps.efficient_freq - adj) {
1142b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1143edcf284bSChris Wilson 			adj = 0;
1144edcf284bSChris Wilson 		}
1145f5a4c67dSChris Wilson 	} else if (any_waiters(dev_priv)) {
1146f5a4c67dSChris Wilson 		adj = 0;
1147dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
1148b39fb297SBen Widawsky 		if (dev_priv->rps.cur_freq > dev_priv->rps.efficient_freq)
1149b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1150dd75fdc8SChris Wilson 		else
1151b39fb297SBen Widawsky 			new_delay = dev_priv->rps.min_freq_softlimit;
1152dd75fdc8SChris Wilson 		adj = 0;
1153dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1154dd75fdc8SChris Wilson 		if (adj < 0)
1155dd75fdc8SChris Wilson 			adj *= 2;
1156edcf284bSChris Wilson 		else /* CHV needs even encode values */
1157edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? -2 : -1;
1158dd75fdc8SChris Wilson 	} else { /* unknown event */
1159edcf284bSChris Wilson 		adj = 0;
1160dd75fdc8SChris Wilson 	}
11613b8d8d91SJesse Barnes 
1162edcf284bSChris Wilson 	dev_priv->rps.last_adj = adj;
1163edcf284bSChris Wilson 
116479249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
116579249636SBen Widawsky 	 * interrupt
116679249636SBen Widawsky 	 */
1167edcf284bSChris Wilson 	new_delay += adj;
11688d3afd7dSChris Wilson 	new_delay = clamp_t(int, new_delay, min, max);
116927544369SDeepak S 
1170ffe02b40SVille Syrjälä 	intel_set_rps(dev_priv->dev, new_delay);
11713b8d8d91SJesse Barnes 
11724fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
11733b8d8d91SJesse Barnes }
11743b8d8d91SJesse Barnes 
1175e3689190SBen Widawsky 
1176e3689190SBen Widawsky /**
1177e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
1178e3689190SBen Widawsky  * occurred.
1179e3689190SBen Widawsky  * @work: workqueue struct
1180e3689190SBen Widawsky  *
1181e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
1182e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
1183e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
1184e3689190SBen Widawsky  */
1185e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
1186e3689190SBen Widawsky {
11872d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
11882d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, l3_parity.error_work);
1189e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
119035a85ac6SBen Widawsky 	char *parity_event[6];
1191e3689190SBen Widawsky 	uint32_t misccpctl;
119235a85ac6SBen Widawsky 	uint8_t slice = 0;
1193e3689190SBen Widawsky 
1194e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
1195e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
1196e3689190SBen Widawsky 	 * any time we access those registers.
1197e3689190SBen Widawsky 	 */
1198e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
1199e3689190SBen Widawsky 
120035a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
120135a85ac6SBen Widawsky 	if (WARN_ON(!dev_priv->l3_parity.which_slice))
120235a85ac6SBen Widawsky 		goto out;
120335a85ac6SBen Widawsky 
1204e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
1205e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1206e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
1207e3689190SBen Widawsky 
120835a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
1209f0f59a00SVille Syrjälä 		i915_reg_t reg;
121035a85ac6SBen Widawsky 
121135a85ac6SBen Widawsky 		slice--;
121235a85ac6SBen Widawsky 		if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv->dev)))
121335a85ac6SBen Widawsky 			break;
121435a85ac6SBen Widawsky 
121535a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
121635a85ac6SBen Widawsky 
12176fa1c5f1SVille Syrjälä 		reg = GEN7_L3CDERRST1(slice);
121835a85ac6SBen Widawsky 
121935a85ac6SBen Widawsky 		error_status = I915_READ(reg);
1220e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
1221e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
1222e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1223e3689190SBen Widawsky 
122435a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
122535a85ac6SBen Widawsky 		POSTING_READ(reg);
1226e3689190SBen Widawsky 
1227cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1228e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1229e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1230e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
123135a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
123235a85ac6SBen Widawsky 		parity_event[5] = NULL;
1233e3689190SBen Widawsky 
12345bdebb18SDave Airlie 		kobject_uevent_env(&dev_priv->dev->primary->kdev->kobj,
1235e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
1236e3689190SBen Widawsky 
123735a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
123835a85ac6SBen Widawsky 			  slice, row, bank, subbank);
1239e3689190SBen Widawsky 
124035a85ac6SBen Widawsky 		kfree(parity_event[4]);
1241e3689190SBen Widawsky 		kfree(parity_event[3]);
1242e3689190SBen Widawsky 		kfree(parity_event[2]);
1243e3689190SBen Widawsky 		kfree(parity_event[1]);
1244e3689190SBen Widawsky 	}
1245e3689190SBen Widawsky 
124635a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
124735a85ac6SBen Widawsky 
124835a85ac6SBen Widawsky out:
124935a85ac6SBen Widawsky 	WARN_ON(dev_priv->l3_parity.which_slice);
12504cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
1251480c8033SDaniel Vetter 	gen5_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv->dev));
12524cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
125335a85ac6SBen Widawsky 
125435a85ac6SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
125535a85ac6SBen Widawsky }
125635a85ac6SBen Widawsky 
125735a85ac6SBen Widawsky static void ivybridge_parity_error_irq_handler(struct drm_device *dev, u32 iir)
1258e3689190SBen Widawsky {
12592d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1260e3689190SBen Widawsky 
1261040d2baaSBen Widawsky 	if (!HAS_L3_DPF(dev))
1262e3689190SBen Widawsky 		return;
1263e3689190SBen Widawsky 
1264d0ecd7e2SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1265480c8033SDaniel Vetter 	gen5_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev));
1266d0ecd7e2SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
1267e3689190SBen Widawsky 
126835a85ac6SBen Widawsky 	iir &= GT_PARITY_ERROR(dev);
126935a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
127035a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 1;
127135a85ac6SBen Widawsky 
127235a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
127335a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 0;
127435a85ac6SBen Widawsky 
1275a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
1276e3689190SBen Widawsky }
1277e3689190SBen Widawsky 
1278f1af8fc1SPaulo Zanoni static void ilk_gt_irq_handler(struct drm_device *dev,
1279f1af8fc1SPaulo Zanoni 			       struct drm_i915_private *dev_priv,
1280f1af8fc1SPaulo Zanoni 			       u32 gt_iir)
1281f1af8fc1SPaulo Zanoni {
1282f1af8fc1SPaulo Zanoni 	if (gt_iir &
1283f1af8fc1SPaulo Zanoni 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
128474cdb337SChris Wilson 		notify_ring(&dev_priv->ring[RCS]);
1285f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
128674cdb337SChris Wilson 		notify_ring(&dev_priv->ring[VCS]);
1287f1af8fc1SPaulo Zanoni }
1288f1af8fc1SPaulo Zanoni 
1289e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
1290e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
1291e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1292e7b4c6b1SDaniel Vetter {
1293e7b4c6b1SDaniel Vetter 
1294cc609d5dSBen Widawsky 	if (gt_iir &
1295cc609d5dSBen Widawsky 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
129674cdb337SChris Wilson 		notify_ring(&dev_priv->ring[RCS]);
1297cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
129874cdb337SChris Wilson 		notify_ring(&dev_priv->ring[VCS]);
1299cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
130074cdb337SChris Wilson 		notify_ring(&dev_priv->ring[BCS]);
1301e7b4c6b1SDaniel Vetter 
1302cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1303cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
1304aaecdf61SDaniel Vetter 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT))
1305aaecdf61SDaniel Vetter 		DRM_DEBUG("Command parser error, gt_iir 0x%08x\n", gt_iir);
1306e3689190SBen Widawsky 
130735a85ac6SBen Widawsky 	if (gt_iir & GT_PARITY_ERROR(dev))
130835a85ac6SBen Widawsky 		ivybridge_parity_error_irq_handler(dev, gt_iir);
1309e7b4c6b1SDaniel Vetter }
1310e7b4c6b1SDaniel Vetter 
1311fbcc1a0cSNick Hoath static __always_inline void
1312e4ba99b9SDaniel Vetter gen8_cs_irq_handler(struct intel_engine_cs *ring, u32 iir, int test_shift)
1313fbcc1a0cSNick Hoath {
1314fbcc1a0cSNick Hoath 	if (iir & (GT_RENDER_USER_INTERRUPT << test_shift))
1315fbcc1a0cSNick Hoath 		notify_ring(ring);
1316fbcc1a0cSNick Hoath 	if (iir & (GT_CONTEXT_SWITCH_INTERRUPT << test_shift))
1317fbcc1a0cSNick Hoath 		intel_lrc_irq_handler(ring);
1318fbcc1a0cSNick Hoath }
1319fbcc1a0cSNick Hoath 
132074cdb337SChris Wilson static irqreturn_t gen8_gt_irq_handler(struct drm_i915_private *dev_priv,
1321abd58f01SBen Widawsky 				       u32 master_ctl)
1322abd58f01SBen Widawsky {
1323abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
1324abd58f01SBen Widawsky 
1325abd58f01SBen Widawsky 	if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
13265dd280b0SNick Hoath 		u32 iir = I915_READ_FW(GEN8_GT_IIR(0));
13275dd280b0SNick Hoath 		if (iir) {
13285dd280b0SNick Hoath 			I915_WRITE_FW(GEN8_GT_IIR(0), iir);
1329abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1330e981e7b1SThomas Daniel 
1331fbcc1a0cSNick Hoath 			gen8_cs_irq_handler(&dev_priv->ring[RCS],
1332fbcc1a0cSNick Hoath 					iir, GEN8_RCS_IRQ_SHIFT);
1333e981e7b1SThomas Daniel 
1334fbcc1a0cSNick Hoath 			gen8_cs_irq_handler(&dev_priv->ring[BCS],
1335fbcc1a0cSNick Hoath 					iir, GEN8_BCS_IRQ_SHIFT);
1336abd58f01SBen Widawsky 		} else
1337abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT0)!\n");
1338abd58f01SBen Widawsky 	}
1339abd58f01SBen Widawsky 
134085f9b5f9SZhao Yakui 	if (master_ctl & (GEN8_GT_VCS1_IRQ | GEN8_GT_VCS2_IRQ)) {
13415dd280b0SNick Hoath 		u32 iir = I915_READ_FW(GEN8_GT_IIR(1));
13425dd280b0SNick Hoath 		if (iir) {
13435dd280b0SNick Hoath 			I915_WRITE_FW(GEN8_GT_IIR(1), iir);
1344abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1345e981e7b1SThomas Daniel 
1346fbcc1a0cSNick Hoath 			gen8_cs_irq_handler(&dev_priv->ring[VCS],
1347fbcc1a0cSNick Hoath 					iir, GEN8_VCS1_IRQ_SHIFT);
1348e981e7b1SThomas Daniel 
1349fbcc1a0cSNick Hoath 			gen8_cs_irq_handler(&dev_priv->ring[VCS2],
1350fbcc1a0cSNick Hoath 					iir, GEN8_VCS2_IRQ_SHIFT);
1351abd58f01SBen Widawsky 		} else
1352abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT1)!\n");
1353abd58f01SBen Widawsky 	}
1354abd58f01SBen Widawsky 
135574cdb337SChris Wilson 	if (master_ctl & GEN8_GT_VECS_IRQ) {
13565dd280b0SNick Hoath 		u32 iir = I915_READ_FW(GEN8_GT_IIR(3));
13575dd280b0SNick Hoath 		if (iir) {
13585dd280b0SNick Hoath 			I915_WRITE_FW(GEN8_GT_IIR(3), iir);
135974cdb337SChris Wilson 			ret = IRQ_HANDLED;
136074cdb337SChris Wilson 
1361fbcc1a0cSNick Hoath 			gen8_cs_irq_handler(&dev_priv->ring[VECS],
1362fbcc1a0cSNick Hoath 					iir, GEN8_VECS_IRQ_SHIFT);
136374cdb337SChris Wilson 		} else
136474cdb337SChris Wilson 			DRM_ERROR("The master control interrupt lied (GT3)!\n");
136574cdb337SChris Wilson 	}
136674cdb337SChris Wilson 
13670961021aSBen Widawsky 	if (master_ctl & GEN8_GT_PM_IRQ) {
13685dd280b0SNick Hoath 		u32 iir = I915_READ_FW(GEN8_GT_IIR(2));
13695dd280b0SNick Hoath 		if (iir & dev_priv->pm_rps_events) {
1370cb0d205eSChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(2),
13715dd280b0SNick Hoath 				      iir & dev_priv->pm_rps_events);
137238cc46d7SOscar Mateo 			ret = IRQ_HANDLED;
13735dd280b0SNick Hoath 			gen6_rps_irq_handler(dev_priv, iir);
13740961021aSBen Widawsky 		} else
13750961021aSBen Widawsky 			DRM_ERROR("The master control interrupt lied (PM)!\n");
13760961021aSBen Widawsky 	}
13770961021aSBen Widawsky 
1378abd58f01SBen Widawsky 	return ret;
1379abd58f01SBen Widawsky }
1380abd58f01SBen Widawsky 
138163c88d22SImre Deak static bool bxt_port_hotplug_long_detect(enum port port, u32 val)
138263c88d22SImre Deak {
138363c88d22SImre Deak 	switch (port) {
138463c88d22SImre Deak 	case PORT_A:
1385195baa06SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
138663c88d22SImre Deak 	case PORT_B:
138763c88d22SImre Deak 		return val & PORTB_HOTPLUG_LONG_DETECT;
138863c88d22SImre Deak 	case PORT_C:
138963c88d22SImre Deak 		return val & PORTC_HOTPLUG_LONG_DETECT;
139063c88d22SImre Deak 	default:
139163c88d22SImre Deak 		return false;
139263c88d22SImre Deak 	}
139363c88d22SImre Deak }
139463c88d22SImre Deak 
13956dbf30ceSVille Syrjälä static bool spt_port_hotplug2_long_detect(enum port port, u32 val)
13966dbf30ceSVille Syrjälä {
13976dbf30ceSVille Syrjälä 	switch (port) {
13986dbf30ceSVille Syrjälä 	case PORT_E:
13996dbf30ceSVille Syrjälä 		return val & PORTE_HOTPLUG_LONG_DETECT;
14006dbf30ceSVille Syrjälä 	default:
14016dbf30ceSVille Syrjälä 		return false;
14026dbf30ceSVille Syrjälä 	}
14036dbf30ceSVille Syrjälä }
14046dbf30ceSVille Syrjälä 
140574c0b395SVille Syrjälä static bool spt_port_hotplug_long_detect(enum port port, u32 val)
140674c0b395SVille Syrjälä {
140774c0b395SVille Syrjälä 	switch (port) {
140874c0b395SVille Syrjälä 	case PORT_A:
140974c0b395SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
141074c0b395SVille Syrjälä 	case PORT_B:
141174c0b395SVille Syrjälä 		return val & PORTB_HOTPLUG_LONG_DETECT;
141274c0b395SVille Syrjälä 	case PORT_C:
141374c0b395SVille Syrjälä 		return val & PORTC_HOTPLUG_LONG_DETECT;
141474c0b395SVille Syrjälä 	case PORT_D:
141574c0b395SVille Syrjälä 		return val & PORTD_HOTPLUG_LONG_DETECT;
141674c0b395SVille Syrjälä 	default:
141774c0b395SVille Syrjälä 		return false;
141874c0b395SVille Syrjälä 	}
141974c0b395SVille Syrjälä }
142074c0b395SVille Syrjälä 
1421e4ce95aaSVille Syrjälä static bool ilk_port_hotplug_long_detect(enum port port, u32 val)
1422e4ce95aaSVille Syrjälä {
1423e4ce95aaSVille Syrjälä 	switch (port) {
1424e4ce95aaSVille Syrjälä 	case PORT_A:
1425e4ce95aaSVille Syrjälä 		return val & DIGITAL_PORTA_HOTPLUG_LONG_DETECT;
1426e4ce95aaSVille Syrjälä 	default:
1427e4ce95aaSVille Syrjälä 		return false;
1428e4ce95aaSVille Syrjälä 	}
1429e4ce95aaSVille Syrjälä }
1430e4ce95aaSVille Syrjälä 
1431676574dfSJani Nikula static bool pch_port_hotplug_long_detect(enum port port, u32 val)
143213cf5504SDave Airlie {
143313cf5504SDave Airlie 	switch (port) {
143413cf5504SDave Airlie 	case PORT_B:
1435676574dfSJani Nikula 		return val & PORTB_HOTPLUG_LONG_DETECT;
143613cf5504SDave Airlie 	case PORT_C:
1437676574dfSJani Nikula 		return val & PORTC_HOTPLUG_LONG_DETECT;
143813cf5504SDave Airlie 	case PORT_D:
1439676574dfSJani Nikula 		return val & PORTD_HOTPLUG_LONG_DETECT;
1440676574dfSJani Nikula 	default:
1441676574dfSJani Nikula 		return false;
144213cf5504SDave Airlie 	}
144313cf5504SDave Airlie }
144413cf5504SDave Airlie 
1445676574dfSJani Nikula static bool i9xx_port_hotplug_long_detect(enum port port, u32 val)
144613cf5504SDave Airlie {
144713cf5504SDave Airlie 	switch (port) {
144813cf5504SDave Airlie 	case PORT_B:
1449676574dfSJani Nikula 		return val & PORTB_HOTPLUG_INT_LONG_PULSE;
145013cf5504SDave Airlie 	case PORT_C:
1451676574dfSJani Nikula 		return val & PORTC_HOTPLUG_INT_LONG_PULSE;
145213cf5504SDave Airlie 	case PORT_D:
1453676574dfSJani Nikula 		return val & PORTD_HOTPLUG_INT_LONG_PULSE;
1454676574dfSJani Nikula 	default:
1455676574dfSJani Nikula 		return false;
145613cf5504SDave Airlie 	}
145713cf5504SDave Airlie }
145813cf5504SDave Airlie 
145942db67d6SVille Syrjälä /*
146042db67d6SVille Syrjälä  * Get a bit mask of pins that have triggered, and which ones may be long.
146142db67d6SVille Syrjälä  * This can be called multiple times with the same masks to accumulate
146242db67d6SVille Syrjälä  * hotplug detection results from several registers.
146342db67d6SVille Syrjälä  *
146442db67d6SVille Syrjälä  * Note that the caller is expected to zero out the masks initially.
146542db67d6SVille Syrjälä  */
1466fd63e2a9SImre Deak static void intel_get_hpd_pins(u32 *pin_mask, u32 *long_mask,
14678c841e57SJani Nikula 			     u32 hotplug_trigger, u32 dig_hotplug_reg,
1468fd63e2a9SImre Deak 			     const u32 hpd[HPD_NUM_PINS],
1469fd63e2a9SImre Deak 			     bool long_pulse_detect(enum port port, u32 val))
1470676574dfSJani Nikula {
14718c841e57SJani Nikula 	enum port port;
1472676574dfSJani Nikula 	int i;
1473676574dfSJani Nikula 
1474676574dfSJani Nikula 	for_each_hpd_pin(i) {
14758c841e57SJani Nikula 		if ((hpd[i] & hotplug_trigger) == 0)
14768c841e57SJani Nikula 			continue;
14778c841e57SJani Nikula 
1478676574dfSJani Nikula 		*pin_mask |= BIT(i);
1479676574dfSJani Nikula 
1480cc24fcdcSImre Deak 		if (!intel_hpd_pin_to_port(i, &port))
1481cc24fcdcSImre Deak 			continue;
1482cc24fcdcSImre Deak 
1483fd63e2a9SImre Deak 		if (long_pulse_detect(port, dig_hotplug_reg))
1484676574dfSJani Nikula 			*long_mask |= BIT(i);
1485676574dfSJani Nikula 	}
1486676574dfSJani Nikula 
1487676574dfSJani Nikula 	DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x, dig 0x%08x, pins 0x%08x\n",
1488676574dfSJani Nikula 			 hotplug_trigger, dig_hotplug_reg, *pin_mask);
1489676574dfSJani Nikula 
1490676574dfSJani Nikula }
1491676574dfSJani Nikula 
1492515ac2bbSDaniel Vetter static void gmbus_irq_handler(struct drm_device *dev)
1493515ac2bbSDaniel Vetter {
14942d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
149528c70f16SDaniel Vetter 
149628c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1497515ac2bbSDaniel Vetter }
1498515ac2bbSDaniel Vetter 
1499ce99c256SDaniel Vetter static void dp_aux_irq_handler(struct drm_device *dev)
1500ce99c256SDaniel Vetter {
15012d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
15029ee32feaSDaniel Vetter 
15039ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1504ce99c256SDaniel Vetter }
1505ce99c256SDaniel Vetter 
15068bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
1507277de95eSDaniel Vetter static void display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1508eba94eb9SDaniel Vetter 					 uint32_t crc0, uint32_t crc1,
1509eba94eb9SDaniel Vetter 					 uint32_t crc2, uint32_t crc3,
15108bc5e955SDaniel Vetter 					 uint32_t crc4)
15118bf1e9f1SShuang He {
15128bf1e9f1SShuang He 	struct drm_i915_private *dev_priv = dev->dev_private;
15138bf1e9f1SShuang He 	struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
15148bf1e9f1SShuang He 	struct intel_pipe_crc_entry *entry;
1515ac2300d4SDamien Lespiau 	int head, tail;
1516b2c88f5bSDamien Lespiau 
1517d538bbdfSDamien Lespiau 	spin_lock(&pipe_crc->lock);
1518d538bbdfSDamien Lespiau 
15190c912c79SDamien Lespiau 	if (!pipe_crc->entries) {
1520d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
152134273620SDaniel Vetter 		DRM_DEBUG_KMS("spurious interrupt\n");
15220c912c79SDamien Lespiau 		return;
15230c912c79SDamien Lespiau 	}
15240c912c79SDamien Lespiau 
1525d538bbdfSDamien Lespiau 	head = pipe_crc->head;
1526d538bbdfSDamien Lespiau 	tail = pipe_crc->tail;
1527b2c88f5bSDamien Lespiau 
1528b2c88f5bSDamien Lespiau 	if (CIRC_SPACE(head, tail, INTEL_PIPE_CRC_ENTRIES_NR) < 1) {
1529d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
1530b2c88f5bSDamien Lespiau 		DRM_ERROR("CRC buffer overflowing\n");
1531b2c88f5bSDamien Lespiau 		return;
1532b2c88f5bSDamien Lespiau 	}
1533b2c88f5bSDamien Lespiau 
1534b2c88f5bSDamien Lespiau 	entry = &pipe_crc->entries[head];
15358bf1e9f1SShuang He 
15368bc5e955SDaniel Vetter 	entry->frame = dev->driver->get_vblank_counter(dev, pipe);
1537eba94eb9SDaniel Vetter 	entry->crc[0] = crc0;
1538eba94eb9SDaniel Vetter 	entry->crc[1] = crc1;
1539eba94eb9SDaniel Vetter 	entry->crc[2] = crc2;
1540eba94eb9SDaniel Vetter 	entry->crc[3] = crc3;
1541eba94eb9SDaniel Vetter 	entry->crc[4] = crc4;
1542b2c88f5bSDamien Lespiau 
1543b2c88f5bSDamien Lespiau 	head = (head + 1) & (INTEL_PIPE_CRC_ENTRIES_NR - 1);
1544d538bbdfSDamien Lespiau 	pipe_crc->head = head;
1545d538bbdfSDamien Lespiau 
1546d538bbdfSDamien Lespiau 	spin_unlock(&pipe_crc->lock);
154707144428SDamien Lespiau 
154807144428SDamien Lespiau 	wake_up_interruptible(&pipe_crc->wq);
15498bf1e9f1SShuang He }
1550277de95eSDaniel Vetter #else
1551277de95eSDaniel Vetter static inline void
1552277de95eSDaniel Vetter display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1553277de95eSDaniel Vetter 			     uint32_t crc0, uint32_t crc1,
1554277de95eSDaniel Vetter 			     uint32_t crc2, uint32_t crc3,
1555277de95eSDaniel Vetter 			     uint32_t crc4) {}
1556277de95eSDaniel Vetter #endif
1557eba94eb9SDaniel Vetter 
1558277de95eSDaniel Vetter 
1559277de95eSDaniel Vetter static void hsw_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
15605a69b89fSDaniel Vetter {
15615a69b89fSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
15625a69b89fSDaniel Vetter 
1563277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
15645a69b89fSDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
15655a69b89fSDaniel Vetter 				     0, 0, 0, 0);
15665a69b89fSDaniel Vetter }
15675a69b89fSDaniel Vetter 
1568277de95eSDaniel Vetter static void ivb_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
1569eba94eb9SDaniel Vetter {
1570eba94eb9SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1571eba94eb9SDaniel Vetter 
1572277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
1573eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1574eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1575eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1576eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
15778bc5e955SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1578eba94eb9SDaniel Vetter }
15795b3a856bSDaniel Vetter 
1580277de95eSDaniel Vetter static void i9xx_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
15815b3a856bSDaniel Vetter {
15825b3a856bSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
15830b5c5ed0SDaniel Vetter 	uint32_t res1, res2;
15840b5c5ed0SDaniel Vetter 
15850b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 3)
15860b5c5ed0SDaniel Vetter 		res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
15870b5c5ed0SDaniel Vetter 	else
15880b5c5ed0SDaniel Vetter 		res1 = 0;
15890b5c5ed0SDaniel Vetter 
15900b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
15910b5c5ed0SDaniel Vetter 		res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
15920b5c5ed0SDaniel Vetter 	else
15930b5c5ed0SDaniel Vetter 		res2 = 0;
15945b3a856bSDaniel Vetter 
1595277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
15960b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_RED(pipe)),
15970b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_GREEN(pipe)),
15980b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_BLUE(pipe)),
15990b5c5ed0SDaniel Vetter 				     res1, res2);
16005b3a856bSDaniel Vetter }
16018bf1e9f1SShuang He 
16021403c0d4SPaulo Zanoni /* The RPS events need forcewake, so we add them to a work queue and mask their
16031403c0d4SPaulo Zanoni  * IMR bits until the work is done. Other interrupts can be processed without
16041403c0d4SPaulo Zanoni  * the work queue. */
16051403c0d4SPaulo Zanoni static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1606baf02a1fSBen Widawsky {
1607a6706b45SDeepak S 	if (pm_iir & dev_priv->pm_rps_events) {
160859cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
1609480c8033SDaniel Vetter 		gen6_disable_pm_irq(dev_priv, pm_iir & dev_priv->pm_rps_events);
1610d4d70aa5SImre Deak 		if (dev_priv->rps.interrupts_enabled) {
1611d4d70aa5SImre Deak 			dev_priv->rps.pm_iir |= pm_iir & dev_priv->pm_rps_events;
16122adbee62SDaniel Vetter 			queue_work(dev_priv->wq, &dev_priv->rps.work);
161341a05a3aSDaniel Vetter 		}
1614d4d70aa5SImre Deak 		spin_unlock(&dev_priv->irq_lock);
1615d4d70aa5SImre Deak 	}
1616baf02a1fSBen Widawsky 
1617c9a9a268SImre Deak 	if (INTEL_INFO(dev_priv)->gen >= 8)
1618c9a9a268SImre Deak 		return;
1619c9a9a268SImre Deak 
16201403c0d4SPaulo Zanoni 	if (HAS_VEBOX(dev_priv->dev)) {
162112638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
162274cdb337SChris Wilson 			notify_ring(&dev_priv->ring[VECS]);
162312638c57SBen Widawsky 
1624aaecdf61SDaniel Vetter 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT)
1625aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, pm_iir 0x%08x\n", pm_iir);
162612638c57SBen Widawsky 	}
16271403c0d4SPaulo Zanoni }
1628baf02a1fSBen Widawsky 
16298d7849dbSVille Syrjälä static bool intel_pipe_handle_vblank(struct drm_device *dev, enum pipe pipe)
16308d7849dbSVille Syrjälä {
16318d7849dbSVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
16328d7849dbSVille Syrjälä 		return false;
16338d7849dbSVille Syrjälä 
16348d7849dbSVille Syrjälä 	return true;
16358d7849dbSVille Syrjälä }
16368d7849dbSVille Syrjälä 
1637c1874ed7SImre Deak static void valleyview_pipestat_irq_handler(struct drm_device *dev, u32 iir)
16387e231dbeSJesse Barnes {
1639c1874ed7SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
164091d181ddSImre Deak 	u32 pipe_stats[I915_MAX_PIPES] = { };
16417e231dbeSJesse Barnes 	int pipe;
16427e231dbeSJesse Barnes 
164358ead0d7SImre Deak 	spin_lock(&dev_priv->irq_lock);
1644055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1645f0f59a00SVille Syrjälä 		i915_reg_t reg;
1646bbb5eebfSDaniel Vetter 		u32 mask, iir_bit = 0;
164791d181ddSImre Deak 
1648bbb5eebfSDaniel Vetter 		/*
1649bbb5eebfSDaniel Vetter 		 * PIPESTAT bits get signalled even when the interrupt is
1650bbb5eebfSDaniel Vetter 		 * disabled with the mask bits, and some of the status bits do
1651bbb5eebfSDaniel Vetter 		 * not generate interrupts at all (like the underrun bit). Hence
1652bbb5eebfSDaniel Vetter 		 * we need to be careful that we only handle what we want to
1653bbb5eebfSDaniel Vetter 		 * handle.
1654bbb5eebfSDaniel Vetter 		 */
16550f239f4cSDaniel Vetter 
16560f239f4cSDaniel Vetter 		/* fifo underruns are filterered in the underrun handler. */
16570f239f4cSDaniel Vetter 		mask = PIPE_FIFO_UNDERRUN_STATUS;
1658bbb5eebfSDaniel Vetter 
1659bbb5eebfSDaniel Vetter 		switch (pipe) {
1660bbb5eebfSDaniel Vetter 		case PIPE_A:
1661bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
1662bbb5eebfSDaniel Vetter 			break;
1663bbb5eebfSDaniel Vetter 		case PIPE_B:
1664bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
1665bbb5eebfSDaniel Vetter 			break;
16663278f67fSVille Syrjälä 		case PIPE_C:
16673278f67fSVille Syrjälä 			iir_bit = I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
16683278f67fSVille Syrjälä 			break;
1669bbb5eebfSDaniel Vetter 		}
1670bbb5eebfSDaniel Vetter 		if (iir & iir_bit)
1671bbb5eebfSDaniel Vetter 			mask |= dev_priv->pipestat_irq_mask[pipe];
1672bbb5eebfSDaniel Vetter 
1673bbb5eebfSDaniel Vetter 		if (!mask)
167491d181ddSImre Deak 			continue;
167591d181ddSImre Deak 
167691d181ddSImre Deak 		reg = PIPESTAT(pipe);
1677bbb5eebfSDaniel Vetter 		mask |= PIPESTAT_INT_ENABLE_MASK;
1678bbb5eebfSDaniel Vetter 		pipe_stats[pipe] = I915_READ(reg) & mask;
16797e231dbeSJesse Barnes 
16807e231dbeSJesse Barnes 		/*
16817e231dbeSJesse Barnes 		 * Clear the PIPE*STAT regs before the IIR
16827e231dbeSJesse Barnes 		 */
168391d181ddSImre Deak 		if (pipe_stats[pipe] & (PIPE_FIFO_UNDERRUN_STATUS |
168491d181ddSImre Deak 					PIPESTAT_INT_STATUS_MASK))
16857e231dbeSJesse Barnes 			I915_WRITE(reg, pipe_stats[pipe]);
16867e231dbeSJesse Barnes 	}
168758ead0d7SImre Deak 	spin_unlock(&dev_priv->irq_lock);
16887e231dbeSJesse Barnes 
1689055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1690d6bbafa1SChris Wilson 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
1691d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
1692d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
169331acc7f5SJesse Barnes 
1694579a9b0eSImre Deak 		if (pipe_stats[pipe] & PLANE_FLIP_DONE_INT_STATUS_VLV) {
169531acc7f5SJesse Barnes 			intel_prepare_page_flip(dev, pipe);
169631acc7f5SJesse Barnes 			intel_finish_page_flip(dev, pipe);
169731acc7f5SJesse Barnes 		}
16984356d586SDaniel Vetter 
16994356d586SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1700277de95eSDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
17012d9d2b0bSVille Syrjälä 
17021f7247c0SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
17031f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
170431acc7f5SJesse Barnes 	}
170531acc7f5SJesse Barnes 
1706c1874ed7SImre Deak 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1707c1874ed7SImre Deak 		gmbus_irq_handler(dev);
1708c1874ed7SImre Deak }
1709c1874ed7SImre Deak 
171016c6c56bSVille Syrjälä static void i9xx_hpd_irq_handler(struct drm_device *dev)
171116c6c56bSVille Syrjälä {
171216c6c56bSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
171316c6c56bSVille Syrjälä 	u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
171442db67d6SVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
171516c6c56bSVille Syrjälä 
17160d2e4297SJani Nikula 	if (!hotplug_status)
17170d2e4297SJani Nikula 		return;
17180d2e4297SJani Nikula 
17193ff60f89SOscar Mateo 	I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
17203ff60f89SOscar Mateo 	/*
17213ff60f89SOscar Mateo 	 * Make sure hotplug status is cleared before we clear IIR, or else we
17223ff60f89SOscar Mateo 	 * may miss hotplug events.
17233ff60f89SOscar Mateo 	 */
17243ff60f89SOscar Mateo 	POSTING_READ(PORT_HOTPLUG_STAT);
17253ff60f89SOscar Mateo 
1726*666a4537SWayne Boyer 	if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
172716c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
172816c6c56bSVille Syrjälä 
172958f2cf24SVille Syrjälä 		if (hotplug_trigger) {
1730fd63e2a9SImre Deak 			intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
1731fd63e2a9SImre Deak 					   hotplug_trigger, hpd_status_g4x,
1732fd63e2a9SImre Deak 					   i9xx_port_hotplug_long_detect);
173358f2cf24SVille Syrjälä 
1734676574dfSJani Nikula 			intel_hpd_irq_handler(dev, pin_mask, long_mask);
173558f2cf24SVille Syrjälä 		}
1736369712e8SJani Nikula 
1737369712e8SJani Nikula 		if (hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
1738369712e8SJani Nikula 			dp_aux_irq_handler(dev);
173916c6c56bSVille Syrjälä 	} else {
174016c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
174116c6c56bSVille Syrjälä 
174258f2cf24SVille Syrjälä 		if (hotplug_trigger) {
1743fd63e2a9SImre Deak 			intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
17444e3d1e26SVille Syrjälä 					   hotplug_trigger, hpd_status_i915,
1745fd63e2a9SImre Deak 					   i9xx_port_hotplug_long_detect);
1746676574dfSJani Nikula 			intel_hpd_irq_handler(dev, pin_mask, long_mask);
174716c6c56bSVille Syrjälä 		}
17483ff60f89SOscar Mateo 	}
174958f2cf24SVille Syrjälä }
175016c6c56bSVille Syrjälä 
1751c1874ed7SImre Deak static irqreturn_t valleyview_irq_handler(int irq, void *arg)
1752c1874ed7SImre Deak {
175345a83f84SDaniel Vetter 	struct drm_device *dev = arg;
17542d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1755c1874ed7SImre Deak 	u32 iir, gt_iir, pm_iir;
1756c1874ed7SImre Deak 	irqreturn_t ret = IRQ_NONE;
1757c1874ed7SImre Deak 
17582dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
17592dd2a883SImre Deak 		return IRQ_NONE;
17602dd2a883SImre Deak 
1761c1874ed7SImre Deak 	while (true) {
17623ff60f89SOscar Mateo 		/* Find, clear, then process each source of interrupt */
17633ff60f89SOscar Mateo 
1764c1874ed7SImre Deak 		gt_iir = I915_READ(GTIIR);
17653ff60f89SOscar Mateo 		if (gt_iir)
17663ff60f89SOscar Mateo 			I915_WRITE(GTIIR, gt_iir);
17673ff60f89SOscar Mateo 
1768c1874ed7SImre Deak 		pm_iir = I915_READ(GEN6_PMIIR);
17693ff60f89SOscar Mateo 		if (pm_iir)
17703ff60f89SOscar Mateo 			I915_WRITE(GEN6_PMIIR, pm_iir);
17713ff60f89SOscar Mateo 
17723ff60f89SOscar Mateo 		iir = I915_READ(VLV_IIR);
17733ff60f89SOscar Mateo 		if (iir) {
17743ff60f89SOscar Mateo 			/* Consume port before clearing IIR or we'll miss events */
17753ff60f89SOscar Mateo 			if (iir & I915_DISPLAY_PORT_INTERRUPT)
17763ff60f89SOscar Mateo 				i9xx_hpd_irq_handler(dev);
17773ff60f89SOscar Mateo 			I915_WRITE(VLV_IIR, iir);
17783ff60f89SOscar Mateo 		}
1779c1874ed7SImre Deak 
1780c1874ed7SImre Deak 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
1781c1874ed7SImre Deak 			goto out;
1782c1874ed7SImre Deak 
1783c1874ed7SImre Deak 		ret = IRQ_HANDLED;
1784c1874ed7SImre Deak 
17853ff60f89SOscar Mateo 		if (gt_iir)
1786c1874ed7SImre Deak 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
178760611c13SPaulo Zanoni 		if (pm_iir)
1788d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
17893ff60f89SOscar Mateo 		/* Call regardless, as some status bits might not be
17903ff60f89SOscar Mateo 		 * signalled in iir */
17913ff60f89SOscar Mateo 		valleyview_pipestat_irq_handler(dev, iir);
17927e231dbeSJesse Barnes 	}
17937e231dbeSJesse Barnes 
17947e231dbeSJesse Barnes out:
17957e231dbeSJesse Barnes 	return ret;
17967e231dbeSJesse Barnes }
17977e231dbeSJesse Barnes 
179843f328d7SVille Syrjälä static irqreturn_t cherryview_irq_handler(int irq, void *arg)
179943f328d7SVille Syrjälä {
180045a83f84SDaniel Vetter 	struct drm_device *dev = arg;
180143f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
180243f328d7SVille Syrjälä 	u32 master_ctl, iir;
180343f328d7SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
180443f328d7SVille Syrjälä 
18052dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
18062dd2a883SImre Deak 		return IRQ_NONE;
18072dd2a883SImre Deak 
18088e5fd599SVille Syrjälä 	for (;;) {
18098e5fd599SVille Syrjälä 		master_ctl = I915_READ(GEN8_MASTER_IRQ) & ~GEN8_MASTER_IRQ_CONTROL;
18103278f67fSVille Syrjälä 		iir = I915_READ(VLV_IIR);
18113278f67fSVille Syrjälä 
18123278f67fSVille Syrjälä 		if (master_ctl == 0 && iir == 0)
18138e5fd599SVille Syrjälä 			break;
181443f328d7SVille Syrjälä 
181527b6c122SOscar Mateo 		ret = IRQ_HANDLED;
181627b6c122SOscar Mateo 
181743f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, 0);
181843f328d7SVille Syrjälä 
181927b6c122SOscar Mateo 		/* Find, clear, then process each source of interrupt */
182027b6c122SOscar Mateo 
182127b6c122SOscar Mateo 		if (iir) {
182227b6c122SOscar Mateo 			/* Consume port before clearing IIR or we'll miss events */
182327b6c122SOscar Mateo 			if (iir & I915_DISPLAY_PORT_INTERRUPT)
182427b6c122SOscar Mateo 				i9xx_hpd_irq_handler(dev);
182527b6c122SOscar Mateo 			I915_WRITE(VLV_IIR, iir);
182627b6c122SOscar Mateo 		}
182727b6c122SOscar Mateo 
182874cdb337SChris Wilson 		gen8_gt_irq_handler(dev_priv, master_ctl);
182943f328d7SVille Syrjälä 
183027b6c122SOscar Mateo 		/* Call regardless, as some status bits might not be
183127b6c122SOscar Mateo 		 * signalled in iir */
18323278f67fSVille Syrjälä 		valleyview_pipestat_irq_handler(dev, iir);
183343f328d7SVille Syrjälä 
183443f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
183543f328d7SVille Syrjälä 		POSTING_READ(GEN8_MASTER_IRQ);
18368e5fd599SVille Syrjälä 	}
18373278f67fSVille Syrjälä 
183843f328d7SVille Syrjälä 	return ret;
183943f328d7SVille Syrjälä }
184043f328d7SVille Syrjälä 
184140e56410SVille Syrjälä static void ibx_hpd_irq_handler(struct drm_device *dev, u32 hotplug_trigger,
184240e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
1843776ad806SJesse Barnes {
184440e56410SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(dev);
184542db67d6SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
1846776ad806SJesse Barnes 
18476a39d7c9SJani Nikula 	/*
18486a39d7c9SJani Nikula 	 * Somehow the PCH doesn't seem to really ack the interrupt to the CPU
18496a39d7c9SJani Nikula 	 * unless we touch the hotplug register, even if hotplug_trigger is
18506a39d7c9SJani Nikula 	 * zero. Not acking leads to "The master control interrupt lied (SDE)!"
18516a39d7c9SJani Nikula 	 * errors.
18526a39d7c9SJani Nikula 	 */
185313cf5504SDave Airlie 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
18546a39d7c9SJani Nikula 	if (!hotplug_trigger) {
18556a39d7c9SJani Nikula 		u32 mask = PORTA_HOTPLUG_STATUS_MASK |
18566a39d7c9SJani Nikula 			PORTD_HOTPLUG_STATUS_MASK |
18576a39d7c9SJani Nikula 			PORTC_HOTPLUG_STATUS_MASK |
18586a39d7c9SJani Nikula 			PORTB_HOTPLUG_STATUS_MASK;
18596a39d7c9SJani Nikula 		dig_hotplug_reg &= ~mask;
18606a39d7c9SJani Nikula 	}
18616a39d7c9SJani Nikula 
186213cf5504SDave Airlie 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
18636a39d7c9SJani Nikula 	if (!hotplug_trigger)
18646a39d7c9SJani Nikula 		return;
186513cf5504SDave Airlie 
1866fd63e2a9SImre Deak 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
186740e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
1868fd63e2a9SImre Deak 			   pch_port_hotplug_long_detect);
186940e56410SVille Syrjälä 
1870676574dfSJani Nikula 	intel_hpd_irq_handler(dev, pin_mask, long_mask);
1871aaf5ec2eSSonika Jindal }
187291d131d2SDaniel Vetter 
187340e56410SVille Syrjälä static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
187440e56410SVille Syrjälä {
187540e56410SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
187640e56410SVille Syrjälä 	int pipe;
187740e56410SVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
187840e56410SVille Syrjälä 
187940e56410SVille Syrjälä 	ibx_hpd_irq_handler(dev, hotplug_trigger, hpd_ibx);
188040e56410SVille Syrjälä 
1881cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1882cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1883776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1884cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1885cfc33bf7SVille Syrjälä 				 port_name(port));
1886cfc33bf7SVille Syrjälä 	}
1887776ad806SJesse Barnes 
1888ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
1889ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1890ce99c256SDaniel Vetter 
1891776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
1892515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
1893776ad806SJesse Barnes 
1894776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1895776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1896776ad806SJesse Barnes 
1897776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1898776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1899776ad806SJesse Barnes 
1900776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1901776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
1902776ad806SJesse Barnes 
19039db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
1904055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
19059db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
19069db4a9c7SJesse Barnes 					 pipe_name(pipe),
19079db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
1908776ad806SJesse Barnes 
1909776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1910776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1911776ad806SJesse Barnes 
1912776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1913776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1914776ad806SJesse Barnes 
1915776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
19161f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
19178664281bSPaulo Zanoni 
19188664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
19191f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
19208664281bSPaulo Zanoni }
19218664281bSPaulo Zanoni 
19228664281bSPaulo Zanoni static void ivb_err_int_handler(struct drm_device *dev)
19238664281bSPaulo Zanoni {
19248664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
19258664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
19265a69b89fSDaniel Vetter 	enum pipe pipe;
19278664281bSPaulo Zanoni 
1928de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
1929de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1930de032bf4SPaulo Zanoni 
1931055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
19321f7247c0SDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe))
19331f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
19348664281bSPaulo Zanoni 
19355a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
19365a69b89fSDaniel Vetter 			if (IS_IVYBRIDGE(dev))
1937277de95eSDaniel Vetter 				ivb_pipe_crc_irq_handler(dev, pipe);
19385a69b89fSDaniel Vetter 			else
1939277de95eSDaniel Vetter 				hsw_pipe_crc_irq_handler(dev, pipe);
19405a69b89fSDaniel Vetter 		}
19415a69b89fSDaniel Vetter 	}
19428bf1e9f1SShuang He 
19438664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
19448664281bSPaulo Zanoni }
19458664281bSPaulo Zanoni 
19468664281bSPaulo Zanoni static void cpt_serr_int_handler(struct drm_device *dev)
19478664281bSPaulo Zanoni {
19488664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
19498664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
19508664281bSPaulo Zanoni 
1951de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
1952de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
1953de032bf4SPaulo Zanoni 
19548664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
19551f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
19568664281bSPaulo Zanoni 
19578664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
19581f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
19598664281bSPaulo Zanoni 
19608664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
19611f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_C);
19628664281bSPaulo Zanoni 
19638664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
1964776ad806SJesse Barnes }
1965776ad806SJesse Barnes 
196623e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
196723e81d69SAdam Jackson {
19682d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
196923e81d69SAdam Jackson 	int pipe;
19706dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
1971aaf5ec2eSSonika Jindal 
197240e56410SVille Syrjälä 	ibx_hpd_irq_handler(dev, hotplug_trigger, hpd_cpt);
197391d131d2SDaniel Vetter 
1974cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
1975cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
197623e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
1977cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
1978cfc33bf7SVille Syrjälä 				 port_name(port));
1979cfc33bf7SVille Syrjälä 	}
198023e81d69SAdam Jackson 
198123e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
1982ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
198323e81d69SAdam Jackson 
198423e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
1985515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
198623e81d69SAdam Jackson 
198723e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
198823e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
198923e81d69SAdam Jackson 
199023e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
199123e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
199223e81d69SAdam Jackson 
199323e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
1994055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
199523e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
199623e81d69SAdam Jackson 					 pipe_name(pipe),
199723e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
19988664281bSPaulo Zanoni 
19998664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
20008664281bSPaulo Zanoni 		cpt_serr_int_handler(dev);
200123e81d69SAdam Jackson }
200223e81d69SAdam Jackson 
20036dbf30ceSVille Syrjälä static void spt_irq_handler(struct drm_device *dev, u32 pch_iir)
20046dbf30ceSVille Syrjälä {
20056dbf30ceSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
20066dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_SPT &
20076dbf30ceSVille Syrjälä 		~SDE_PORTE_HOTPLUG_SPT;
20086dbf30ceSVille Syrjälä 	u32 hotplug2_trigger = pch_iir & SDE_PORTE_HOTPLUG_SPT;
20096dbf30ceSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
20106dbf30ceSVille Syrjälä 
20116dbf30ceSVille Syrjälä 	if (hotplug_trigger) {
20126dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
20136dbf30ceSVille Syrjälä 
20146dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
20156dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
20166dbf30ceSVille Syrjälä 
20176dbf30ceSVille Syrjälä 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
20186dbf30ceSVille Syrjälä 				   dig_hotplug_reg, hpd_spt,
201974c0b395SVille Syrjälä 				   spt_port_hotplug_long_detect);
20206dbf30ceSVille Syrjälä 	}
20216dbf30ceSVille Syrjälä 
20226dbf30ceSVille Syrjälä 	if (hotplug2_trigger) {
20236dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
20246dbf30ceSVille Syrjälä 
20256dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG2);
20266dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG2, dig_hotplug_reg);
20276dbf30ceSVille Syrjälä 
20286dbf30ceSVille Syrjälä 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug2_trigger,
20296dbf30ceSVille Syrjälä 				   dig_hotplug_reg, hpd_spt,
20306dbf30ceSVille Syrjälä 				   spt_port_hotplug2_long_detect);
20316dbf30ceSVille Syrjälä 	}
20326dbf30ceSVille Syrjälä 
20336dbf30ceSVille Syrjälä 	if (pin_mask)
20346dbf30ceSVille Syrjälä 		intel_hpd_irq_handler(dev, pin_mask, long_mask);
20356dbf30ceSVille Syrjälä 
20366dbf30ceSVille Syrjälä 	if (pch_iir & SDE_GMBUS_CPT)
20376dbf30ceSVille Syrjälä 		gmbus_irq_handler(dev);
20386dbf30ceSVille Syrjälä }
20396dbf30ceSVille Syrjälä 
204040e56410SVille Syrjälä static void ilk_hpd_irq_handler(struct drm_device *dev, u32 hotplug_trigger,
204140e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2042c008bc6eSPaulo Zanoni {
204340e56410SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(dev);
2044e4ce95aaSVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2045e4ce95aaSVille Syrjälä 
2046e4ce95aaSVille Syrjälä 	dig_hotplug_reg = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
2047e4ce95aaSVille Syrjälä 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, dig_hotplug_reg);
2048e4ce95aaSVille Syrjälä 
2049e4ce95aaSVille Syrjälä 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
205040e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2051e4ce95aaSVille Syrjälä 			   ilk_port_hotplug_long_detect);
205240e56410SVille Syrjälä 
2053e4ce95aaSVille Syrjälä 	intel_hpd_irq_handler(dev, pin_mask, long_mask);
2054e4ce95aaSVille Syrjälä }
2055c008bc6eSPaulo Zanoni 
205640e56410SVille Syrjälä static void ilk_display_irq_handler(struct drm_device *dev, u32 de_iir)
205740e56410SVille Syrjälä {
205840e56410SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
205940e56410SVille Syrjälä 	enum pipe pipe;
206040e56410SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG;
206140e56410SVille Syrjälä 
206240e56410SVille Syrjälä 	if (hotplug_trigger)
206340e56410SVille Syrjälä 		ilk_hpd_irq_handler(dev, hotplug_trigger, hpd_ilk);
206440e56410SVille Syrjälä 
2065c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
2066c008bc6eSPaulo Zanoni 		dp_aux_irq_handler(dev);
2067c008bc6eSPaulo Zanoni 
2068c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
2069c008bc6eSPaulo Zanoni 		intel_opregion_asle_intr(dev);
2070c008bc6eSPaulo Zanoni 
2071c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
2072c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
2073c008bc6eSPaulo Zanoni 
2074055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2075d6bbafa1SChris Wilson 		if (de_iir & DE_PIPE_VBLANK(pipe) &&
2076d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
2077d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
2078c008bc6eSPaulo Zanoni 
207940da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
20801f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
2081c008bc6eSPaulo Zanoni 
208240da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
208340da17c2SDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
20845b3a856bSDaniel Vetter 
208540da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
208640da17c2SDaniel Vetter 		if (de_iir & DE_PLANE_FLIP_DONE(pipe)) {
208740da17c2SDaniel Vetter 			intel_prepare_page_flip(dev, pipe);
208840da17c2SDaniel Vetter 			intel_finish_page_flip_plane(dev, pipe);
2089c008bc6eSPaulo Zanoni 		}
2090c008bc6eSPaulo Zanoni 	}
2091c008bc6eSPaulo Zanoni 
2092c008bc6eSPaulo Zanoni 	/* check event from PCH */
2093c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
2094c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
2095c008bc6eSPaulo Zanoni 
2096c008bc6eSPaulo Zanoni 		if (HAS_PCH_CPT(dev))
2097c008bc6eSPaulo Zanoni 			cpt_irq_handler(dev, pch_iir);
2098c008bc6eSPaulo Zanoni 		else
2099c008bc6eSPaulo Zanoni 			ibx_irq_handler(dev, pch_iir);
2100c008bc6eSPaulo Zanoni 
2101c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
2102c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
2103c008bc6eSPaulo Zanoni 	}
2104c008bc6eSPaulo Zanoni 
2105c008bc6eSPaulo Zanoni 	if (IS_GEN5(dev) && de_iir & DE_PCU_EVENT)
2106c008bc6eSPaulo Zanoni 		ironlake_rps_change_irq_handler(dev);
2107c008bc6eSPaulo Zanoni }
2108c008bc6eSPaulo Zanoni 
21099719fb98SPaulo Zanoni static void ivb_display_irq_handler(struct drm_device *dev, u32 de_iir)
21109719fb98SPaulo Zanoni {
21119719fb98SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
211207d27e20SDamien Lespiau 	enum pipe pipe;
211323bb4cb5SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG_IVB;
211423bb4cb5SVille Syrjälä 
211540e56410SVille Syrjälä 	if (hotplug_trigger)
211640e56410SVille Syrjälä 		ilk_hpd_irq_handler(dev, hotplug_trigger, hpd_ivb);
21179719fb98SPaulo Zanoni 
21189719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
21199719fb98SPaulo Zanoni 		ivb_err_int_handler(dev);
21209719fb98SPaulo Zanoni 
21219719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
21229719fb98SPaulo Zanoni 		dp_aux_irq_handler(dev);
21239719fb98SPaulo Zanoni 
21249719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
21259719fb98SPaulo Zanoni 		intel_opregion_asle_intr(dev);
21269719fb98SPaulo Zanoni 
2127055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2128d6bbafa1SChris Wilson 		if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)) &&
2129d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
2130d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
213140da17c2SDaniel Vetter 
213240da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
213307d27e20SDamien Lespiau 		if (de_iir & DE_PLANE_FLIP_DONE_IVB(pipe)) {
213407d27e20SDamien Lespiau 			intel_prepare_page_flip(dev, pipe);
213507d27e20SDamien Lespiau 			intel_finish_page_flip_plane(dev, pipe);
21369719fb98SPaulo Zanoni 		}
21379719fb98SPaulo Zanoni 	}
21389719fb98SPaulo Zanoni 
21399719fb98SPaulo Zanoni 	/* check event from PCH */
21409719fb98SPaulo Zanoni 	if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
21419719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
21429719fb98SPaulo Zanoni 
21439719fb98SPaulo Zanoni 		cpt_irq_handler(dev, pch_iir);
21449719fb98SPaulo Zanoni 
21459719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
21469719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
21479719fb98SPaulo Zanoni 	}
21489719fb98SPaulo Zanoni }
21499719fb98SPaulo Zanoni 
215072c90f62SOscar Mateo /*
215172c90f62SOscar Mateo  * To handle irqs with the minimum potential races with fresh interrupts, we:
215272c90f62SOscar Mateo  * 1 - Disable Master Interrupt Control.
215372c90f62SOscar Mateo  * 2 - Find the source(s) of the interrupt.
215472c90f62SOscar Mateo  * 3 - Clear the Interrupt Identity bits (IIR).
215572c90f62SOscar Mateo  * 4 - Process the interrupt(s) that had bits set in the IIRs.
215672c90f62SOscar Mateo  * 5 - Re-enable Master Interrupt Control.
215772c90f62SOscar Mateo  */
2158f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
2159b1f14ad0SJesse Barnes {
216045a83f84SDaniel Vetter 	struct drm_device *dev = arg;
21612d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2162f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
21630e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
2164b1f14ad0SJesse Barnes 
21652dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
21662dd2a883SImre Deak 		return IRQ_NONE;
21672dd2a883SImre Deak 
21688664281bSPaulo Zanoni 	/* We get interrupts on unclaimed registers, so check for this before we
21698664281bSPaulo Zanoni 	 * do any I915_{READ,WRITE}. */
2170907b28c5SChris Wilson 	intel_uncore_check_errors(dev);
21718664281bSPaulo Zanoni 
2172b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
2173b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
2174b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
217523a78516SPaulo Zanoni 	POSTING_READ(DEIER);
21760e43406bSChris Wilson 
217744498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
217844498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
217944498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
218044498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
218144498aeaSPaulo Zanoni 	 * due to its back queue). */
2182ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
218344498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
218444498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
218544498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2186ab5c608bSBen Widawsky 	}
218744498aeaSPaulo Zanoni 
218872c90f62SOscar Mateo 	/* Find, clear, then process each source of interrupt */
218972c90f62SOscar Mateo 
21900e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
21910e43406bSChris Wilson 	if (gt_iir) {
219272c90f62SOscar Mateo 		I915_WRITE(GTIIR, gt_iir);
219372c90f62SOscar Mateo 		ret = IRQ_HANDLED;
2194d8fc8a47SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 6)
21950e43406bSChris Wilson 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
2196d8fc8a47SPaulo Zanoni 		else
2197d8fc8a47SPaulo Zanoni 			ilk_gt_irq_handler(dev, dev_priv, gt_iir);
21980e43406bSChris Wilson 	}
2199b1f14ad0SJesse Barnes 
2200b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
22010e43406bSChris Wilson 	if (de_iir) {
220272c90f62SOscar Mateo 		I915_WRITE(DEIIR, de_iir);
220372c90f62SOscar Mateo 		ret = IRQ_HANDLED;
2204f1af8fc1SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 7)
22059719fb98SPaulo Zanoni 			ivb_display_irq_handler(dev, de_iir);
2206f1af8fc1SPaulo Zanoni 		else
2207f1af8fc1SPaulo Zanoni 			ilk_display_irq_handler(dev, de_iir);
22080e43406bSChris Wilson 	}
22090e43406bSChris Wilson 
2210f1af8fc1SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6) {
2211f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
22120e43406bSChris Wilson 		if (pm_iir) {
2213b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
22140e43406bSChris Wilson 			ret = IRQ_HANDLED;
221572c90f62SOscar Mateo 			gen6_rps_irq_handler(dev_priv, pm_iir);
22160e43406bSChris Wilson 		}
2217f1af8fc1SPaulo Zanoni 	}
2218b1f14ad0SJesse Barnes 
2219b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
2220b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
2221ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
222244498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
222344498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2224ab5c608bSBen Widawsky 	}
2225b1f14ad0SJesse Barnes 
2226b1f14ad0SJesse Barnes 	return ret;
2227b1f14ad0SJesse Barnes }
2228b1f14ad0SJesse Barnes 
222940e56410SVille Syrjälä static void bxt_hpd_irq_handler(struct drm_device *dev, u32 hotplug_trigger,
223040e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2231d04a492dSShashank Sharma {
2232cebd87a0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(dev);
2233cebd87a0SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2234d04a492dSShashank Sharma 
2235a52bb15bSVille Syrjälä 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
2236a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
2237d04a492dSShashank Sharma 
2238cebd87a0SVille Syrjälä 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
223940e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2240cebd87a0SVille Syrjälä 			   bxt_port_hotplug_long_detect);
224140e56410SVille Syrjälä 
2242475c2e3bSJani Nikula 	intel_hpd_irq_handler(dev, pin_mask, long_mask);
2243d04a492dSShashank Sharma }
2244d04a492dSShashank Sharma 
2245abd58f01SBen Widawsky static irqreturn_t gen8_irq_handler(int irq, void *arg)
2246abd58f01SBen Widawsky {
2247abd58f01SBen Widawsky 	struct drm_device *dev = arg;
2248abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2249abd58f01SBen Widawsky 	u32 master_ctl;
2250abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
2251abd58f01SBen Widawsky 	uint32_t tmp = 0;
2252c42664ccSDaniel Vetter 	enum pipe pipe;
225388e04703SJesse Barnes 	u32 aux_mask = GEN8_AUX_CHANNEL_A;
225488e04703SJesse Barnes 
22552dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
22562dd2a883SImre Deak 		return IRQ_NONE;
22572dd2a883SImre Deak 
2258b4834a50SRodrigo Vivi 	if (INTEL_INFO(dev_priv)->gen >= 9)
225988e04703SJesse Barnes 		aux_mask |=  GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
226088e04703SJesse Barnes 			GEN9_AUX_CHANNEL_D;
2261abd58f01SBen Widawsky 
2262cb0d205eSChris Wilson 	master_ctl = I915_READ_FW(GEN8_MASTER_IRQ);
2263abd58f01SBen Widawsky 	master_ctl &= ~GEN8_MASTER_IRQ_CONTROL;
2264abd58f01SBen Widawsky 	if (!master_ctl)
2265abd58f01SBen Widawsky 		return IRQ_NONE;
2266abd58f01SBen Widawsky 
2267cb0d205eSChris Wilson 	I915_WRITE_FW(GEN8_MASTER_IRQ, 0);
2268abd58f01SBen Widawsky 
226938cc46d7SOscar Mateo 	/* Find, clear, then process each source of interrupt */
227038cc46d7SOscar Mateo 
227174cdb337SChris Wilson 	ret = gen8_gt_irq_handler(dev_priv, master_ctl);
2272abd58f01SBen Widawsky 
2273abd58f01SBen Widawsky 	if (master_ctl & GEN8_DE_MISC_IRQ) {
2274abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_DE_MISC_IIR);
2275abd58f01SBen Widawsky 		if (tmp) {
2276abd58f01SBen Widawsky 			I915_WRITE(GEN8_DE_MISC_IIR, tmp);
2277abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
227838cc46d7SOscar Mateo 			if (tmp & GEN8_DE_MISC_GSE)
227938cc46d7SOscar Mateo 				intel_opregion_asle_intr(dev);
228038cc46d7SOscar Mateo 			else
228138cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Misc interrupt\n");
2282abd58f01SBen Widawsky 		}
228338cc46d7SOscar Mateo 		else
228438cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
2285abd58f01SBen Widawsky 	}
2286abd58f01SBen Widawsky 
22876d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
22886d766f02SDaniel Vetter 		tmp = I915_READ(GEN8_DE_PORT_IIR);
22896d766f02SDaniel Vetter 		if (tmp) {
2290d04a492dSShashank Sharma 			bool found = false;
2291cebd87a0SVille Syrjälä 			u32 hotplug_trigger = 0;
2292cebd87a0SVille Syrjälä 
2293cebd87a0SVille Syrjälä 			if (IS_BROXTON(dev_priv))
2294cebd87a0SVille Syrjälä 				hotplug_trigger = tmp & BXT_DE_PORT_HOTPLUG_MASK;
2295cebd87a0SVille Syrjälä 			else if (IS_BROADWELL(dev_priv))
2296cebd87a0SVille Syrjälä 				hotplug_trigger = tmp & GEN8_PORT_DP_A_HOTPLUG;
2297d04a492dSShashank Sharma 
22986d766f02SDaniel Vetter 			I915_WRITE(GEN8_DE_PORT_IIR, tmp);
22996d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
230088e04703SJesse Barnes 
2301d04a492dSShashank Sharma 			if (tmp & aux_mask) {
230238cc46d7SOscar Mateo 				dp_aux_irq_handler(dev);
2303d04a492dSShashank Sharma 				found = true;
2304d04a492dSShashank Sharma 			}
2305d04a492dSShashank Sharma 
230640e56410SVille Syrjälä 			if (hotplug_trigger) {
230740e56410SVille Syrjälä 				if (IS_BROXTON(dev))
230840e56410SVille Syrjälä 					bxt_hpd_irq_handler(dev, hotplug_trigger, hpd_bxt);
230940e56410SVille Syrjälä 				else
231040e56410SVille Syrjälä 					ilk_hpd_irq_handler(dev, hotplug_trigger, hpd_bdw);
2311d04a492dSShashank Sharma 				found = true;
2312d04a492dSShashank Sharma 			}
2313d04a492dSShashank Sharma 
23149e63743eSShashank Sharma 			if (IS_BROXTON(dev) && (tmp & BXT_DE_PORT_GMBUS)) {
23159e63743eSShashank Sharma 				gmbus_irq_handler(dev);
23169e63743eSShashank Sharma 				found = true;
23179e63743eSShashank Sharma 			}
23189e63743eSShashank Sharma 
2319d04a492dSShashank Sharma 			if (!found)
232038cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Port interrupt\n");
23216d766f02SDaniel Vetter 		}
232238cc46d7SOscar Mateo 		else
232338cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
23246d766f02SDaniel Vetter 	}
23256d766f02SDaniel Vetter 
2326055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2327770de83dSDamien Lespiau 		uint32_t pipe_iir, flip_done = 0, fault_errors = 0;
2328abd58f01SBen Widawsky 
2329c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2330c42664ccSDaniel Vetter 			continue;
2331c42664ccSDaniel Vetter 
2332abd58f01SBen Widawsky 		pipe_iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
233338cc46d7SOscar Mateo 		if (pipe_iir) {
233438cc46d7SOscar Mateo 			ret = IRQ_HANDLED;
233538cc46d7SOscar Mateo 			I915_WRITE(GEN8_DE_PIPE_IIR(pipe), pipe_iir);
2336770de83dSDamien Lespiau 
2337d6bbafa1SChris Wilson 			if (pipe_iir & GEN8_PIPE_VBLANK &&
2338d6bbafa1SChris Wilson 			    intel_pipe_handle_vblank(dev, pipe))
2339d6bbafa1SChris Wilson 				intel_check_page_flip(dev, pipe);
2340abd58f01SBen Widawsky 
2341b4834a50SRodrigo Vivi 			if (INTEL_INFO(dev_priv)->gen >= 9)
2342770de83dSDamien Lespiau 				flip_done = pipe_iir & GEN9_PIPE_PLANE1_FLIP_DONE;
2343770de83dSDamien Lespiau 			else
2344770de83dSDamien Lespiau 				flip_done = pipe_iir & GEN8_PIPE_PRIMARY_FLIP_DONE;
2345770de83dSDamien Lespiau 
2346770de83dSDamien Lespiau 			if (flip_done) {
2347abd58f01SBen Widawsky 				intel_prepare_page_flip(dev, pipe);
2348abd58f01SBen Widawsky 				intel_finish_page_flip_plane(dev, pipe);
2349abd58f01SBen Widawsky 			}
2350abd58f01SBen Widawsky 
23510fbe7870SDaniel Vetter 			if (pipe_iir & GEN8_PIPE_CDCLK_CRC_DONE)
23520fbe7870SDaniel Vetter 				hsw_pipe_crc_irq_handler(dev, pipe);
23530fbe7870SDaniel Vetter 
23541f7247c0SDaniel Vetter 			if (pipe_iir & GEN8_PIPE_FIFO_UNDERRUN)
23551f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
23561f7247c0SDaniel Vetter 								    pipe);
235738d83c96SDaniel Vetter 
2358770de83dSDamien Lespiau 
2359b4834a50SRodrigo Vivi 			if (INTEL_INFO(dev_priv)->gen >= 9)
2360770de83dSDamien Lespiau 				fault_errors = pipe_iir & GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
2361770de83dSDamien Lespiau 			else
2362770de83dSDamien Lespiau 				fault_errors = pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
2363770de83dSDamien Lespiau 
2364770de83dSDamien Lespiau 			if (fault_errors)
236530100f2bSDaniel Vetter 				DRM_ERROR("Fault errors on pipe %c\n: 0x%08x",
236630100f2bSDaniel Vetter 					  pipe_name(pipe),
236730100f2bSDaniel Vetter 					  pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS);
2368c42664ccSDaniel Vetter 		} else
2369abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
2370abd58f01SBen Widawsky 	}
2371abd58f01SBen Widawsky 
2372266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev) && !HAS_PCH_NOP(dev) &&
2373266ea3d9SShashank Sharma 	    master_ctl & GEN8_DE_PCH_IRQ) {
237492d03a80SDaniel Vetter 		/*
237592d03a80SDaniel Vetter 		 * FIXME(BDW): Assume for now that the new interrupt handling
237692d03a80SDaniel Vetter 		 * scheme also closed the SDE interrupt handling race we've seen
237792d03a80SDaniel Vetter 		 * on older pch-split platforms. But this needs testing.
237892d03a80SDaniel Vetter 		 */
237992d03a80SDaniel Vetter 		u32 pch_iir = I915_READ(SDEIIR);
238092d03a80SDaniel Vetter 		if (pch_iir) {
238192d03a80SDaniel Vetter 			I915_WRITE(SDEIIR, pch_iir);
238292d03a80SDaniel Vetter 			ret = IRQ_HANDLED;
23836dbf30ceSVille Syrjälä 
23846dbf30ceSVille Syrjälä 			if (HAS_PCH_SPT(dev_priv))
23856dbf30ceSVille Syrjälä 				spt_irq_handler(dev, pch_iir);
23866dbf30ceSVille Syrjälä 			else
238738cc46d7SOscar Mateo 				cpt_irq_handler(dev, pch_iir);
2388820da7aeSJani Nikula 		} else
2389820da7aeSJani Nikula 			DRM_ERROR("The master control interrupt lied (SDE)!\n");
2390820da7aeSJani Nikula 
239192d03a80SDaniel Vetter 	}
239292d03a80SDaniel Vetter 
2393cb0d205eSChris Wilson 	I915_WRITE_FW(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
2394cb0d205eSChris Wilson 	POSTING_READ_FW(GEN8_MASTER_IRQ);
2395abd58f01SBen Widawsky 
2396abd58f01SBen Widawsky 	return ret;
2397abd58f01SBen Widawsky }
2398abd58f01SBen Widawsky 
239917e1df07SDaniel Vetter static void i915_error_wake_up(struct drm_i915_private *dev_priv,
240017e1df07SDaniel Vetter 			       bool reset_completed)
240117e1df07SDaniel Vetter {
2402a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
240317e1df07SDaniel Vetter 	int i;
240417e1df07SDaniel Vetter 
240517e1df07SDaniel Vetter 	/*
240617e1df07SDaniel Vetter 	 * Notify all waiters for GPU completion events that reset state has
240717e1df07SDaniel Vetter 	 * been changed, and that they need to restart their wait after
240817e1df07SDaniel Vetter 	 * checking for potential errors (and bail out to drop locks if there is
240917e1df07SDaniel Vetter 	 * a gpu reset pending so that i915_error_work_func can acquire them).
241017e1df07SDaniel Vetter 	 */
241117e1df07SDaniel Vetter 
241217e1df07SDaniel Vetter 	/* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
241317e1df07SDaniel Vetter 	for_each_ring(ring, dev_priv, i)
241417e1df07SDaniel Vetter 		wake_up_all(&ring->irq_queue);
241517e1df07SDaniel Vetter 
241617e1df07SDaniel Vetter 	/* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
241717e1df07SDaniel Vetter 	wake_up_all(&dev_priv->pending_flip_queue);
241817e1df07SDaniel Vetter 
241917e1df07SDaniel Vetter 	/*
242017e1df07SDaniel Vetter 	 * Signal tasks blocked in i915_gem_wait_for_error that the pending
242117e1df07SDaniel Vetter 	 * reset state is cleared.
242217e1df07SDaniel Vetter 	 */
242317e1df07SDaniel Vetter 	if (reset_completed)
242417e1df07SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
242517e1df07SDaniel Vetter }
242617e1df07SDaniel Vetter 
24278a905236SJesse Barnes /**
2428b8d24a06SMika Kuoppala  * i915_reset_and_wakeup - do process context error handling work
2429468f9d29SJavier Martinez Canillas  * @dev: drm device
24308a905236SJesse Barnes  *
24318a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
24328a905236SJesse Barnes  * was detected.
24338a905236SJesse Barnes  */
2434b8d24a06SMika Kuoppala static void i915_reset_and_wakeup(struct drm_device *dev)
24358a905236SJesse Barnes {
2436b8d24a06SMika Kuoppala 	struct drm_i915_private *dev_priv = to_i915(dev);
2437b8d24a06SMika Kuoppala 	struct i915_gpu_error *error = &dev_priv->gpu_error;
2438cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
2439cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
2440cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
244117e1df07SDaniel Vetter 	int ret;
24428a905236SJesse Barnes 
24435bdebb18SDave Airlie 	kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, error_event);
24448a905236SJesse Barnes 
24457db0ba24SDaniel Vetter 	/*
24467db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
24477db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
24487db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
24497db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
24507db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
24517db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
24527db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
24537db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
24547db0ba24SDaniel Vetter 	 */
24557db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
245644d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
24575bdebb18SDave Airlie 		kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE,
24587db0ba24SDaniel Vetter 				   reset_event);
24591f83fee0SDaniel Vetter 
246017e1df07SDaniel Vetter 		/*
2461f454c694SImre Deak 		 * In most cases it's guaranteed that we get here with an RPM
2462f454c694SImre Deak 		 * reference held, for example because there is a pending GPU
2463f454c694SImre Deak 		 * request that won't finish until the reset is done. This
2464f454c694SImre Deak 		 * isn't the case at least when we get here by doing a
2465f454c694SImre Deak 		 * simulated reset via debugs, so get an RPM reference.
2466f454c694SImre Deak 		 */
2467f454c694SImre Deak 		intel_runtime_pm_get(dev_priv);
24687514747dSVille Syrjälä 
24697514747dSVille Syrjälä 		intel_prepare_reset(dev);
24707514747dSVille Syrjälä 
2471f454c694SImre Deak 		/*
247217e1df07SDaniel Vetter 		 * All state reset _must_ be completed before we update the
247317e1df07SDaniel Vetter 		 * reset counter, for otherwise waiters might miss the reset
247417e1df07SDaniel Vetter 		 * pending state and not properly drop locks, resulting in
247517e1df07SDaniel Vetter 		 * deadlocks with the reset work.
247617e1df07SDaniel Vetter 		 */
2477f69061beSDaniel Vetter 		ret = i915_reset(dev);
2478f69061beSDaniel Vetter 
24797514747dSVille Syrjälä 		intel_finish_reset(dev);
248017e1df07SDaniel Vetter 
2481f454c694SImre Deak 		intel_runtime_pm_put(dev_priv);
2482f454c694SImre Deak 
2483f69061beSDaniel Vetter 		if (ret == 0) {
2484f69061beSDaniel Vetter 			/*
2485f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
2486f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
2487f69061beSDaniel Vetter 			 * complete.
2488f69061beSDaniel Vetter 			 *
2489f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
2490f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
2491f69061beSDaniel Vetter 			 * updates before
2492f69061beSDaniel Vetter 			 * the counter increment.
2493f69061beSDaniel Vetter 			 */
24944e857c58SPeter Zijlstra 			smp_mb__before_atomic();
2495f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
2496f69061beSDaniel Vetter 
24975bdebb18SDave Airlie 			kobject_uevent_env(&dev->primary->kdev->kobj,
2498f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
24991f83fee0SDaniel Vetter 		} else {
2500805de8f4SPeter Zijlstra 			atomic_or(I915_WEDGED, &error->reset_counter);
2501f316a42cSBen Gamari 		}
25021f83fee0SDaniel Vetter 
250317e1df07SDaniel Vetter 		/*
250417e1df07SDaniel Vetter 		 * Note: The wake_up also serves as a memory barrier so that
250517e1df07SDaniel Vetter 		 * waiters see the update value of the reset counter atomic_t.
250617e1df07SDaniel Vetter 		 */
250717e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, true);
2508f316a42cSBen Gamari 	}
25098a905236SJesse Barnes }
25108a905236SJesse Barnes 
251135aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
2512c0e09200SDave Airlie {
25138a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
2514bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
251563eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
2516050ee91fSBen Widawsky 	int pipe, i;
251763eeaf38SJesse Barnes 
251835aed2e6SChris Wilson 	if (!eir)
251935aed2e6SChris Wilson 		return;
252063eeaf38SJesse Barnes 
2521a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
25228a905236SJesse Barnes 
2523bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
2524bd9854f9SBen Widawsky 
25258a905236SJesse Barnes 	if (IS_G4X(dev)) {
25268a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
25278a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
25288a905236SJesse Barnes 
2529a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2530a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2531050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
2532050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2533a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2534a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
25358a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
25363143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
25378a905236SJesse Barnes 		}
25388a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
25398a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2540a70491ccSJoe Perches 			pr_err("page table error\n");
2541a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
25428a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
25433143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
25448a905236SJesse Barnes 		}
25458a905236SJesse Barnes 	}
25468a905236SJesse Barnes 
2547a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
254863eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
254963eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2550a70491ccSJoe Perches 			pr_err("page table error\n");
2551a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
255263eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
25533143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
255463eeaf38SJesse Barnes 		}
25558a905236SJesse Barnes 	}
25568a905236SJesse Barnes 
255763eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
2558a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
2559055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
2560a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
25619db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
256263eeaf38SJesse Barnes 		/* pipestat has already been acked */
256363eeaf38SJesse Barnes 	}
256463eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
2565a70491ccSJoe Perches 		pr_err("instruction error\n");
2566a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
2567050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
2568050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2569a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
257063eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
257163eeaf38SJesse Barnes 
2572a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
2573a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
2574a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
257563eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
25763143a2bfSChris Wilson 			POSTING_READ(IPEIR);
257763eeaf38SJesse Barnes 		} else {
257863eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
257963eeaf38SJesse Barnes 
2580a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2581a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2582a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2583a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
258463eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
25853143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
258663eeaf38SJesse Barnes 		}
258763eeaf38SJesse Barnes 	}
258863eeaf38SJesse Barnes 
258963eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
25903143a2bfSChris Wilson 	POSTING_READ(EIR);
259163eeaf38SJesse Barnes 	eir = I915_READ(EIR);
259263eeaf38SJesse Barnes 	if (eir) {
259363eeaf38SJesse Barnes 		/*
259463eeaf38SJesse Barnes 		 * some errors might have become stuck,
259563eeaf38SJesse Barnes 		 * mask them.
259663eeaf38SJesse Barnes 		 */
259763eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
259863eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
259963eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
260063eeaf38SJesse Barnes 	}
260135aed2e6SChris Wilson }
260235aed2e6SChris Wilson 
260335aed2e6SChris Wilson /**
2604b8d24a06SMika Kuoppala  * i915_handle_error - handle a gpu error
260535aed2e6SChris Wilson  * @dev: drm device
260635aed2e6SChris Wilson  *
2607aafd8581SJavier Martinez Canillas  * Do some basic checking of register state at error time and
260835aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
260935aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
261035aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
261135aed2e6SChris Wilson  * of a ring dump etc.).
261235aed2e6SChris Wilson  */
261358174462SMika Kuoppala void i915_handle_error(struct drm_device *dev, bool wedged,
261458174462SMika Kuoppala 		       const char *fmt, ...)
261535aed2e6SChris Wilson {
261635aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
261758174462SMika Kuoppala 	va_list args;
261858174462SMika Kuoppala 	char error_msg[80];
261935aed2e6SChris Wilson 
262058174462SMika Kuoppala 	va_start(args, fmt);
262158174462SMika Kuoppala 	vscnprintf(error_msg, sizeof(error_msg), fmt, args);
262258174462SMika Kuoppala 	va_end(args);
262358174462SMika Kuoppala 
262458174462SMika Kuoppala 	i915_capture_error_state(dev, wedged, error_msg);
262535aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
26268a905236SJesse Barnes 
2627ba1234d1SBen Gamari 	if (wedged) {
2628805de8f4SPeter Zijlstra 		atomic_or(I915_RESET_IN_PROGRESS_FLAG,
2629f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
2630ba1234d1SBen Gamari 
263111ed50ecSBen Gamari 		/*
2632b8d24a06SMika Kuoppala 		 * Wakeup waiting processes so that the reset function
2633b8d24a06SMika Kuoppala 		 * i915_reset_and_wakeup doesn't deadlock trying to grab
2634b8d24a06SMika Kuoppala 		 * various locks. By bumping the reset counter first, the woken
263517e1df07SDaniel Vetter 		 * processes will see a reset in progress and back off,
263617e1df07SDaniel Vetter 		 * releasing their locks and then wait for the reset completion.
263717e1df07SDaniel Vetter 		 * We must do this for _all_ gpu waiters that might hold locks
263817e1df07SDaniel Vetter 		 * that the reset work needs to acquire.
263917e1df07SDaniel Vetter 		 *
264017e1df07SDaniel Vetter 		 * Note: The wake_up serves as the required memory barrier to
264117e1df07SDaniel Vetter 		 * ensure that the waiters see the updated value of the reset
264217e1df07SDaniel Vetter 		 * counter atomic_t.
264311ed50ecSBen Gamari 		 */
264417e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, false);
264511ed50ecSBen Gamari 	}
264611ed50ecSBen Gamari 
2647b8d24a06SMika Kuoppala 	i915_reset_and_wakeup(dev);
26488a905236SJesse Barnes }
26498a905236SJesse Barnes 
265042f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
265142f52ef8SKeith Packard  * we use as a pipe index
265242f52ef8SKeith Packard  */
265388e72717SThierry Reding static int i915_enable_vblank(struct drm_device *dev, unsigned int pipe)
26540a3e67a4SJesse Barnes {
26552d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2656e9d21d7fSKeith Packard 	unsigned long irqflags;
265771e0ffa5SJesse Barnes 
26581ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2659f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
26607c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2661755e9019SImre Deak 				     PIPE_START_VBLANK_INTERRUPT_STATUS);
26620a3e67a4SJesse Barnes 	else
26637c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2664755e9019SImre Deak 				     PIPE_VBLANK_INTERRUPT_STATUS);
26651ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26668692d00eSChris Wilson 
26670a3e67a4SJesse Barnes 	return 0;
26680a3e67a4SJesse Barnes }
26690a3e67a4SJesse Barnes 
267088e72717SThierry Reding static int ironlake_enable_vblank(struct drm_device *dev, unsigned int pipe)
2671f796cf8fSJesse Barnes {
26722d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2673f796cf8fSJesse Barnes 	unsigned long irqflags;
2674b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
267540da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2676f796cf8fSJesse Barnes 
2677f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2678fbdedaeaSVille Syrjälä 	ilk_enable_display_irq(dev_priv, bit);
2679b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2680b1f14ad0SJesse Barnes 
2681b1f14ad0SJesse Barnes 	return 0;
2682b1f14ad0SJesse Barnes }
2683b1f14ad0SJesse Barnes 
268488e72717SThierry Reding static int valleyview_enable_vblank(struct drm_device *dev, unsigned int pipe)
26857e231dbeSJesse Barnes {
26862d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
26877e231dbeSJesse Barnes 	unsigned long irqflags;
26887e231dbeSJesse Barnes 
26897e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
269031acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
2691755e9019SImre Deak 			     PIPE_START_VBLANK_INTERRUPT_STATUS);
26927e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26937e231dbeSJesse Barnes 
26947e231dbeSJesse Barnes 	return 0;
26957e231dbeSJesse Barnes }
26967e231dbeSJesse Barnes 
269788e72717SThierry Reding static int gen8_enable_vblank(struct drm_device *dev, unsigned int pipe)
2698abd58f01SBen Widawsky {
2699abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2700abd58f01SBen Widawsky 	unsigned long irqflags;
2701abd58f01SBen Widawsky 
2702abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2703013d3752SVille Syrjälä 	bdw_enable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
2704abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2705013d3752SVille Syrjälä 
2706abd58f01SBen Widawsky 	return 0;
2707abd58f01SBen Widawsky }
2708abd58f01SBen Widawsky 
270942f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
271042f52ef8SKeith Packard  * we use as a pipe index
271142f52ef8SKeith Packard  */
271288e72717SThierry Reding static void i915_disable_vblank(struct drm_device *dev, unsigned int pipe)
27130a3e67a4SJesse Barnes {
27142d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2715e9d21d7fSKeith Packard 	unsigned long irqflags;
27160a3e67a4SJesse Barnes 
27171ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
27187c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
2719755e9019SImre Deak 			      PIPE_VBLANK_INTERRUPT_STATUS |
2720755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
27211ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
27220a3e67a4SJesse Barnes }
27230a3e67a4SJesse Barnes 
272488e72717SThierry Reding static void ironlake_disable_vblank(struct drm_device *dev, unsigned int pipe)
2725f796cf8fSJesse Barnes {
27262d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2727f796cf8fSJesse Barnes 	unsigned long irqflags;
2728b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
272940da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2730f796cf8fSJesse Barnes 
2731f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2732fbdedaeaSVille Syrjälä 	ilk_disable_display_irq(dev_priv, bit);
2733b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2734b1f14ad0SJesse Barnes }
2735b1f14ad0SJesse Barnes 
273688e72717SThierry Reding static void valleyview_disable_vblank(struct drm_device *dev, unsigned int pipe)
27377e231dbeSJesse Barnes {
27382d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
27397e231dbeSJesse Barnes 	unsigned long irqflags;
27407e231dbeSJesse Barnes 
27417e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
274231acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
2743755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
27447e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
27457e231dbeSJesse Barnes }
27467e231dbeSJesse Barnes 
274788e72717SThierry Reding static void gen8_disable_vblank(struct drm_device *dev, unsigned int pipe)
2748abd58f01SBen Widawsky {
2749abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2750abd58f01SBen Widawsky 	unsigned long irqflags;
2751abd58f01SBen Widawsky 
2752abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2753013d3752SVille Syrjälä 	bdw_disable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
2754abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2755abd58f01SBen Widawsky }
2756abd58f01SBen Widawsky 
27579107e9d2SChris Wilson static bool
275894f7bbe1STomas Elf ring_idle(struct intel_engine_cs *ring, u32 seqno)
2759893eead0SChris Wilson {
27609107e9d2SChris Wilson 	return (list_empty(&ring->request_list) ||
276194f7bbe1STomas Elf 		i915_seqno_passed(seqno, ring->last_submitted_seqno));
2762f65d9421SBen Gamari }
2763f65d9421SBen Gamari 
2764a028c4b0SDaniel Vetter static bool
2765a028c4b0SDaniel Vetter ipehr_is_semaphore_wait(struct drm_device *dev, u32 ipehr)
2766a028c4b0SDaniel Vetter {
2767a028c4b0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 8) {
2768a6cdb93aSRodrigo Vivi 		return (ipehr >> 23) == 0x1c;
2769a028c4b0SDaniel Vetter 	} else {
2770a028c4b0SDaniel Vetter 		ipehr &= ~MI_SEMAPHORE_SYNC_MASK;
2771a028c4b0SDaniel Vetter 		return ipehr == (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE |
2772a028c4b0SDaniel Vetter 				 MI_SEMAPHORE_REGISTER);
2773a028c4b0SDaniel Vetter 	}
2774a028c4b0SDaniel Vetter }
2775a028c4b0SDaniel Vetter 
2776a4872ba6SOscar Mateo static struct intel_engine_cs *
2777a6cdb93aSRodrigo Vivi semaphore_wait_to_signaller_ring(struct intel_engine_cs *ring, u32 ipehr, u64 offset)
2778921d42eaSDaniel Vetter {
2779921d42eaSDaniel Vetter 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2780a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2781921d42eaSDaniel Vetter 	int i;
2782921d42eaSDaniel Vetter 
2783921d42eaSDaniel Vetter 	if (INTEL_INFO(dev_priv->dev)->gen >= 8) {
2784a6cdb93aSRodrigo Vivi 		for_each_ring(signaller, dev_priv, i) {
2785a6cdb93aSRodrigo Vivi 			if (ring == signaller)
2786a6cdb93aSRodrigo Vivi 				continue;
2787a6cdb93aSRodrigo Vivi 
2788a6cdb93aSRodrigo Vivi 			if (offset == signaller->semaphore.signal_ggtt[ring->id])
2789a6cdb93aSRodrigo Vivi 				return signaller;
2790a6cdb93aSRodrigo Vivi 		}
2791921d42eaSDaniel Vetter 	} else {
2792921d42eaSDaniel Vetter 		u32 sync_bits = ipehr & MI_SEMAPHORE_SYNC_MASK;
2793921d42eaSDaniel Vetter 
2794921d42eaSDaniel Vetter 		for_each_ring(signaller, dev_priv, i) {
2795921d42eaSDaniel Vetter 			if(ring == signaller)
2796921d42eaSDaniel Vetter 				continue;
2797921d42eaSDaniel Vetter 
2798ebc348b2SBen Widawsky 			if (sync_bits == signaller->semaphore.mbox.wait[ring->id])
2799921d42eaSDaniel Vetter 				return signaller;
2800921d42eaSDaniel Vetter 		}
2801921d42eaSDaniel Vetter 	}
2802921d42eaSDaniel Vetter 
2803a6cdb93aSRodrigo Vivi 	DRM_ERROR("No signaller ring found for ring %i, ipehr 0x%08x, offset 0x%016llx\n",
2804a6cdb93aSRodrigo Vivi 		  ring->id, ipehr, offset);
2805921d42eaSDaniel Vetter 
2806921d42eaSDaniel Vetter 	return NULL;
2807921d42eaSDaniel Vetter }
2808921d42eaSDaniel Vetter 
2809a4872ba6SOscar Mateo static struct intel_engine_cs *
2810a4872ba6SOscar Mateo semaphore_waits_for(struct intel_engine_cs *ring, u32 *seqno)
2811a24a11e6SChris Wilson {
2812a24a11e6SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
281388fe429dSDaniel Vetter 	u32 cmd, ipehr, head;
2814a6cdb93aSRodrigo Vivi 	u64 offset = 0;
2815a6cdb93aSRodrigo Vivi 	int i, backwards;
2816a24a11e6SChris Wilson 
2817381e8ae3STomas Elf 	/*
2818381e8ae3STomas Elf 	 * This function does not support execlist mode - any attempt to
2819381e8ae3STomas Elf 	 * proceed further into this function will result in a kernel panic
2820381e8ae3STomas Elf 	 * when dereferencing ring->buffer, which is not set up in execlist
2821381e8ae3STomas Elf 	 * mode.
2822381e8ae3STomas Elf 	 *
2823381e8ae3STomas Elf 	 * The correct way of doing it would be to derive the currently
2824381e8ae3STomas Elf 	 * executing ring buffer from the current context, which is derived
2825381e8ae3STomas Elf 	 * from the currently running request. Unfortunately, to get the
2826381e8ae3STomas Elf 	 * current request we would have to grab the struct_mutex before doing
2827381e8ae3STomas Elf 	 * anything else, which would be ill-advised since some other thread
2828381e8ae3STomas Elf 	 * might have grabbed it already and managed to hang itself, causing
2829381e8ae3STomas Elf 	 * the hang checker to deadlock.
2830381e8ae3STomas Elf 	 *
2831381e8ae3STomas Elf 	 * Therefore, this function does not support execlist mode in its
2832381e8ae3STomas Elf 	 * current form. Just return NULL and move on.
2833381e8ae3STomas Elf 	 */
2834381e8ae3STomas Elf 	if (ring->buffer == NULL)
2835381e8ae3STomas Elf 		return NULL;
2836381e8ae3STomas Elf 
2837a24a11e6SChris Wilson 	ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
2838a028c4b0SDaniel Vetter 	if (!ipehr_is_semaphore_wait(ring->dev, ipehr))
28396274f212SChris Wilson 		return NULL;
2840a24a11e6SChris Wilson 
284188fe429dSDaniel Vetter 	/*
284288fe429dSDaniel Vetter 	 * HEAD is likely pointing to the dword after the actual command,
284388fe429dSDaniel Vetter 	 * so scan backwards until we find the MBOX. But limit it to just 3
2844a6cdb93aSRodrigo Vivi 	 * or 4 dwords depending on the semaphore wait command size.
2845a6cdb93aSRodrigo Vivi 	 * Note that we don't care about ACTHD here since that might
284688fe429dSDaniel Vetter 	 * point at at batch, and semaphores are always emitted into the
284788fe429dSDaniel Vetter 	 * ringbuffer itself.
2848a24a11e6SChris Wilson 	 */
284988fe429dSDaniel Vetter 	head = I915_READ_HEAD(ring) & HEAD_ADDR;
2850a6cdb93aSRodrigo Vivi 	backwards = (INTEL_INFO(ring->dev)->gen >= 8) ? 5 : 4;
285188fe429dSDaniel Vetter 
2852a6cdb93aSRodrigo Vivi 	for (i = backwards; i; --i) {
285388fe429dSDaniel Vetter 		/*
285488fe429dSDaniel Vetter 		 * Be paranoid and presume the hw has gone off into the wild -
285588fe429dSDaniel Vetter 		 * our ring is smaller than what the hardware (and hence
285688fe429dSDaniel Vetter 		 * HEAD_ADDR) allows. Also handles wrap-around.
285788fe429dSDaniel Vetter 		 */
2858ee1b1e5eSOscar Mateo 		head &= ring->buffer->size - 1;
285988fe429dSDaniel Vetter 
286088fe429dSDaniel Vetter 		/* This here seems to blow up */
2861ee1b1e5eSOscar Mateo 		cmd = ioread32(ring->buffer->virtual_start + head);
2862a24a11e6SChris Wilson 		if (cmd == ipehr)
2863a24a11e6SChris Wilson 			break;
2864a24a11e6SChris Wilson 
286588fe429dSDaniel Vetter 		head -= 4;
286688fe429dSDaniel Vetter 	}
2867a24a11e6SChris Wilson 
286888fe429dSDaniel Vetter 	if (!i)
286988fe429dSDaniel Vetter 		return NULL;
287088fe429dSDaniel Vetter 
2871ee1b1e5eSOscar Mateo 	*seqno = ioread32(ring->buffer->virtual_start + head + 4) + 1;
2872a6cdb93aSRodrigo Vivi 	if (INTEL_INFO(ring->dev)->gen >= 8) {
2873a6cdb93aSRodrigo Vivi 		offset = ioread32(ring->buffer->virtual_start + head + 12);
2874a6cdb93aSRodrigo Vivi 		offset <<= 32;
2875a6cdb93aSRodrigo Vivi 		offset = ioread32(ring->buffer->virtual_start + head + 8);
2876a6cdb93aSRodrigo Vivi 	}
2877a6cdb93aSRodrigo Vivi 	return semaphore_wait_to_signaller_ring(ring, ipehr, offset);
2878a24a11e6SChris Wilson }
2879a24a11e6SChris Wilson 
2880a4872ba6SOscar Mateo static int semaphore_passed(struct intel_engine_cs *ring)
28816274f212SChris Wilson {
28826274f212SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2883a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2884a0d036b0SChris Wilson 	u32 seqno;
28856274f212SChris Wilson 
28864be17381SChris Wilson 	ring->hangcheck.deadlock++;
28876274f212SChris Wilson 
28886274f212SChris Wilson 	signaller = semaphore_waits_for(ring, &seqno);
28894be17381SChris Wilson 	if (signaller == NULL)
28904be17381SChris Wilson 		return -1;
28914be17381SChris Wilson 
28924be17381SChris Wilson 	/* Prevent pathological recursion due to driver bugs */
28934be17381SChris Wilson 	if (signaller->hangcheck.deadlock >= I915_NUM_RINGS)
28946274f212SChris Wilson 		return -1;
28956274f212SChris Wilson 
28964be17381SChris Wilson 	if (i915_seqno_passed(signaller->get_seqno(signaller, false), seqno))
28974be17381SChris Wilson 		return 1;
28984be17381SChris Wilson 
2899a0d036b0SChris Wilson 	/* cursory check for an unkickable deadlock */
2900a0d036b0SChris Wilson 	if (I915_READ_CTL(signaller) & RING_WAIT_SEMAPHORE &&
2901a0d036b0SChris Wilson 	    semaphore_passed(signaller) < 0)
29024be17381SChris Wilson 		return -1;
29034be17381SChris Wilson 
29044be17381SChris Wilson 	return 0;
29056274f212SChris Wilson }
29066274f212SChris Wilson 
29076274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
29086274f212SChris Wilson {
2909a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
29106274f212SChris Wilson 	int i;
29116274f212SChris Wilson 
29126274f212SChris Wilson 	for_each_ring(ring, dev_priv, i)
29134be17381SChris Wilson 		ring->hangcheck.deadlock = 0;
29146274f212SChris Wilson }
29156274f212SChris Wilson 
2916ad8beaeaSMika Kuoppala static enum intel_ring_hangcheck_action
2917a4872ba6SOscar Mateo ring_stuck(struct intel_engine_cs *ring, u64 acthd)
29181ec14ad3SChris Wilson {
29191ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
29201ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
29219107e9d2SChris Wilson 	u32 tmp;
29229107e9d2SChris Wilson 
2923f260fe7bSMika Kuoppala 	if (acthd != ring->hangcheck.acthd) {
2924f260fe7bSMika Kuoppala 		if (acthd > ring->hangcheck.max_acthd) {
2925f260fe7bSMika Kuoppala 			ring->hangcheck.max_acthd = acthd;
2926f2f4d82fSJani Nikula 			return HANGCHECK_ACTIVE;
2927f260fe7bSMika Kuoppala 		}
2928f260fe7bSMika Kuoppala 
2929f260fe7bSMika Kuoppala 		return HANGCHECK_ACTIVE_LOOP;
2930f260fe7bSMika Kuoppala 	}
29316274f212SChris Wilson 
29329107e9d2SChris Wilson 	if (IS_GEN2(dev))
2933f2f4d82fSJani Nikula 		return HANGCHECK_HUNG;
29349107e9d2SChris Wilson 
29359107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
29369107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
29379107e9d2SChris Wilson 	 * and break the hang. This should work on
29389107e9d2SChris Wilson 	 * all but the second generation chipsets.
29399107e9d2SChris Wilson 	 */
29409107e9d2SChris Wilson 	tmp = I915_READ_CTL(ring);
29411ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
294258174462SMika Kuoppala 		i915_handle_error(dev, false,
294358174462SMika Kuoppala 				  "Kicking stuck wait on %s",
29441ec14ad3SChris Wilson 				  ring->name);
29451ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
2946f2f4d82fSJani Nikula 		return HANGCHECK_KICK;
29471ec14ad3SChris Wilson 	}
2948a24a11e6SChris Wilson 
29496274f212SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
29506274f212SChris Wilson 		switch (semaphore_passed(ring)) {
29516274f212SChris Wilson 		default:
2952f2f4d82fSJani Nikula 			return HANGCHECK_HUNG;
29536274f212SChris Wilson 		case 1:
295458174462SMika Kuoppala 			i915_handle_error(dev, false,
295558174462SMika Kuoppala 					  "Kicking stuck semaphore on %s",
2956a24a11e6SChris Wilson 					  ring->name);
2957a24a11e6SChris Wilson 			I915_WRITE_CTL(ring, tmp);
2958f2f4d82fSJani Nikula 			return HANGCHECK_KICK;
29596274f212SChris Wilson 		case 0:
2960f2f4d82fSJani Nikula 			return HANGCHECK_WAIT;
29616274f212SChris Wilson 		}
29629107e9d2SChris Wilson 	}
29639107e9d2SChris Wilson 
2964f2f4d82fSJani Nikula 	return HANGCHECK_HUNG;
2965a24a11e6SChris Wilson }
2966d1e61e7fSChris Wilson 
2967737b1506SChris Wilson /*
2968f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
296905407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
297005407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
297105407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
297205407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
297305407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
2974f65d9421SBen Gamari  */
2975737b1506SChris Wilson static void i915_hangcheck_elapsed(struct work_struct *work)
2976f65d9421SBen Gamari {
2977737b1506SChris Wilson 	struct drm_i915_private *dev_priv =
2978737b1506SChris Wilson 		container_of(work, typeof(*dev_priv),
2979737b1506SChris Wilson 			     gpu_error.hangcheck_work.work);
2980737b1506SChris Wilson 	struct drm_device *dev = dev_priv->dev;
2981a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
2982b4519513SChris Wilson 	int i;
298305407ff8SMika Kuoppala 	int busy_count = 0, rings_hung = 0;
29849107e9d2SChris Wilson 	bool stuck[I915_NUM_RINGS] = { 0 };
29859107e9d2SChris Wilson #define BUSY 1
29869107e9d2SChris Wilson #define KICK 5
29879107e9d2SChris Wilson #define HUNG 20
2988893eead0SChris Wilson 
2989d330a953SJani Nikula 	if (!i915.enable_hangcheck)
29903e0dc6b0SBen Widawsky 		return;
29913e0dc6b0SBen Widawsky 
2992b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
299350877445SChris Wilson 		u64 acthd;
299450877445SChris Wilson 		u32 seqno;
29959107e9d2SChris Wilson 		bool busy = true;
2996b4519513SChris Wilson 
29976274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
29986274f212SChris Wilson 
299905407ff8SMika Kuoppala 		seqno = ring->get_seqno(ring, false);
300005407ff8SMika Kuoppala 		acthd = intel_ring_get_active_head(ring);
300105407ff8SMika Kuoppala 
300205407ff8SMika Kuoppala 		if (ring->hangcheck.seqno == seqno) {
300394f7bbe1STomas Elf 			if (ring_idle(ring, seqno)) {
3004da661464SMika Kuoppala 				ring->hangcheck.action = HANGCHECK_IDLE;
3005da661464SMika Kuoppala 
30069107e9d2SChris Wilson 				if (waitqueue_active(&ring->irq_queue)) {
30079107e9d2SChris Wilson 					/* Issue a wake-up to catch stuck h/w. */
3008094f9a54SChris Wilson 					if (!test_and_set_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings)) {
3009f4adcd24SDaniel Vetter 						if (!(dev_priv->gpu_error.test_irq_rings & intel_ring_flag(ring)))
30109107e9d2SChris Wilson 							DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
30119107e9d2SChris Wilson 								  ring->name);
3012f4adcd24SDaniel Vetter 						else
3013f4adcd24SDaniel Vetter 							DRM_INFO("Fake missed irq on %s\n",
3014f4adcd24SDaniel Vetter 								 ring->name);
30159107e9d2SChris Wilson 						wake_up_all(&ring->irq_queue);
3016094f9a54SChris Wilson 					}
3017094f9a54SChris Wilson 					/* Safeguard against driver failure */
3018094f9a54SChris Wilson 					ring->hangcheck.score += BUSY;
30199107e9d2SChris Wilson 				} else
30209107e9d2SChris Wilson 					busy = false;
302105407ff8SMika Kuoppala 			} else {
30226274f212SChris Wilson 				/* We always increment the hangcheck score
30236274f212SChris Wilson 				 * if the ring is busy and still processing
30246274f212SChris Wilson 				 * the same request, so that no single request
30256274f212SChris Wilson 				 * can run indefinitely (such as a chain of
30266274f212SChris Wilson 				 * batches). The only time we do not increment
30276274f212SChris Wilson 				 * the hangcheck score on this ring, if this
30286274f212SChris Wilson 				 * ring is in a legitimate wait for another
30296274f212SChris Wilson 				 * ring. In that case the waiting ring is a
30306274f212SChris Wilson 				 * victim and we want to be sure we catch the
30316274f212SChris Wilson 				 * right culprit. Then every time we do kick
30326274f212SChris Wilson 				 * the ring, add a small increment to the
30336274f212SChris Wilson 				 * score so that we can catch a batch that is
30346274f212SChris Wilson 				 * being repeatedly kicked and so responsible
30356274f212SChris Wilson 				 * for stalling the machine.
30369107e9d2SChris Wilson 				 */
3037ad8beaeaSMika Kuoppala 				ring->hangcheck.action = ring_stuck(ring,
3038ad8beaeaSMika Kuoppala 								    acthd);
3039ad8beaeaSMika Kuoppala 
3040ad8beaeaSMika Kuoppala 				switch (ring->hangcheck.action) {
3041da661464SMika Kuoppala 				case HANGCHECK_IDLE:
3042f2f4d82fSJani Nikula 				case HANGCHECK_WAIT:
3043f2f4d82fSJani Nikula 				case HANGCHECK_ACTIVE:
3044f260fe7bSMika Kuoppala 					break;
3045f260fe7bSMika Kuoppala 				case HANGCHECK_ACTIVE_LOOP:
3046ea04cb31SJani Nikula 					ring->hangcheck.score += BUSY;
30476274f212SChris Wilson 					break;
3048f2f4d82fSJani Nikula 				case HANGCHECK_KICK:
3049ea04cb31SJani Nikula 					ring->hangcheck.score += KICK;
30506274f212SChris Wilson 					break;
3051f2f4d82fSJani Nikula 				case HANGCHECK_HUNG:
3052ea04cb31SJani Nikula 					ring->hangcheck.score += HUNG;
30536274f212SChris Wilson 					stuck[i] = true;
30546274f212SChris Wilson 					break;
30556274f212SChris Wilson 				}
305605407ff8SMika Kuoppala 			}
30579107e9d2SChris Wilson 		} else {
3058da661464SMika Kuoppala 			ring->hangcheck.action = HANGCHECK_ACTIVE;
3059da661464SMika Kuoppala 
30609107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
30619107e9d2SChris Wilson 			 * attempts across multiple batches.
30629107e9d2SChris Wilson 			 */
30639107e9d2SChris Wilson 			if (ring->hangcheck.score > 0)
30649107e9d2SChris Wilson 				ring->hangcheck.score--;
3065f260fe7bSMika Kuoppala 
3066f260fe7bSMika Kuoppala 			ring->hangcheck.acthd = ring->hangcheck.max_acthd = 0;
3067cbb465e7SChris Wilson 		}
3068f65d9421SBen Gamari 
306905407ff8SMika Kuoppala 		ring->hangcheck.seqno = seqno;
307005407ff8SMika Kuoppala 		ring->hangcheck.acthd = acthd;
30719107e9d2SChris Wilson 		busy_count += busy;
307205407ff8SMika Kuoppala 	}
307305407ff8SMika Kuoppala 
307405407ff8SMika Kuoppala 	for_each_ring(ring, dev_priv, i) {
3075b6b0fac0SMika Kuoppala 		if (ring->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG) {
3076b8d88d1dSDaniel Vetter 			DRM_INFO("%s on %s\n",
307705407ff8SMika Kuoppala 				 stuck[i] ? "stuck" : "no progress",
3078a43adf07SChris Wilson 				 ring->name);
3079a43adf07SChris Wilson 			rings_hung++;
308005407ff8SMika Kuoppala 		}
308105407ff8SMika Kuoppala 	}
308205407ff8SMika Kuoppala 
308305407ff8SMika Kuoppala 	if (rings_hung)
308458174462SMika Kuoppala 		return i915_handle_error(dev, true, "Ring hung");
308505407ff8SMika Kuoppala 
308605407ff8SMika Kuoppala 	if (busy_count)
308705407ff8SMika Kuoppala 		/* Reset timer case chip hangs without another request
308805407ff8SMika Kuoppala 		 * being added */
308910cd45b6SMika Kuoppala 		i915_queue_hangcheck(dev);
309010cd45b6SMika Kuoppala }
309110cd45b6SMika Kuoppala 
309210cd45b6SMika Kuoppala void i915_queue_hangcheck(struct drm_device *dev)
309310cd45b6SMika Kuoppala {
3094737b1506SChris Wilson 	struct i915_gpu_error *e = &to_i915(dev)->gpu_error;
3095672e7b7cSChris Wilson 
3096d330a953SJani Nikula 	if (!i915.enable_hangcheck)
309710cd45b6SMika Kuoppala 		return;
309810cd45b6SMika Kuoppala 
3099737b1506SChris Wilson 	/* Don't continually defer the hangcheck so that it is always run at
3100737b1506SChris Wilson 	 * least once after work has been scheduled on any ring. Otherwise,
3101737b1506SChris Wilson 	 * we will ignore a hung ring if a second ring is kept busy.
3102737b1506SChris Wilson 	 */
3103737b1506SChris Wilson 
3104737b1506SChris Wilson 	queue_delayed_work(e->hangcheck_wq, &e->hangcheck_work,
3105737b1506SChris Wilson 			   round_jiffies_up_relative(DRM_I915_HANGCHECK_JIFFIES));
3106f65d9421SBen Gamari }
3107f65d9421SBen Gamari 
31081c69eb42SPaulo Zanoni static void ibx_irq_reset(struct drm_device *dev)
310991738a95SPaulo Zanoni {
311091738a95SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
311191738a95SPaulo Zanoni 
311291738a95SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
311391738a95SPaulo Zanoni 		return;
311491738a95SPaulo Zanoni 
3115f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(SDE);
3116105b122eSPaulo Zanoni 
3117105b122eSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
3118105b122eSPaulo Zanoni 		I915_WRITE(SERR_INT, 0xffffffff);
3119622364b6SPaulo Zanoni }
3120105b122eSPaulo Zanoni 
312191738a95SPaulo Zanoni /*
3122622364b6SPaulo Zanoni  * SDEIER is also touched by the interrupt handler to work around missed PCH
3123622364b6SPaulo Zanoni  * interrupts. Hence we can't update it after the interrupt handler is enabled -
3124622364b6SPaulo Zanoni  * instead we unconditionally enable all PCH interrupt sources here, but then
3125622364b6SPaulo Zanoni  * only unmask them as needed with SDEIMR.
3126622364b6SPaulo Zanoni  *
3127622364b6SPaulo Zanoni  * This function needs to be called before interrupts are enabled.
312891738a95SPaulo Zanoni  */
3129622364b6SPaulo Zanoni static void ibx_irq_pre_postinstall(struct drm_device *dev)
3130622364b6SPaulo Zanoni {
3131622364b6SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3132622364b6SPaulo Zanoni 
3133622364b6SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
3134622364b6SPaulo Zanoni 		return;
3135622364b6SPaulo Zanoni 
3136622364b6SPaulo Zanoni 	WARN_ON(I915_READ(SDEIER) != 0);
313791738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
313891738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
313991738a95SPaulo Zanoni }
314091738a95SPaulo Zanoni 
31417c4d664eSPaulo Zanoni static void gen5_gt_irq_reset(struct drm_device *dev)
3142d18ea1b5SDaniel Vetter {
3143d18ea1b5SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
3144d18ea1b5SDaniel Vetter 
3145f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GT);
3146a9d356a6SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6)
3147f86f3fb0SPaulo Zanoni 		GEN5_IRQ_RESET(GEN6_PM);
3148d18ea1b5SDaniel Vetter }
3149d18ea1b5SDaniel Vetter 
3150c0e09200SDave Airlie /* drm_dma.h hooks
3151c0e09200SDave Airlie */
3152be30b29fSPaulo Zanoni static void ironlake_irq_reset(struct drm_device *dev)
3153036a4a7dSZhenyu Wang {
31542d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3155036a4a7dSZhenyu Wang 
31560c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xffffffff);
3157bdfcdb63SDaniel Vetter 
3158f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(DE);
3159c6d954c1SPaulo Zanoni 	if (IS_GEN7(dev))
3160c6d954c1SPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, 0xffffffff);
3161036a4a7dSZhenyu Wang 
31627c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
3163c650156aSZhenyu Wang 
31641c69eb42SPaulo Zanoni 	ibx_irq_reset(dev);
31657d99163dSBen Widawsky }
31667d99163dSBen Widawsky 
316770591a41SVille Syrjälä static void vlv_display_irq_reset(struct drm_i915_private *dev_priv)
316870591a41SVille Syrjälä {
316970591a41SVille Syrjälä 	enum pipe pipe;
317070591a41SVille Syrjälä 
31710706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xFFFFFFFF, 0);
317270591a41SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
317370591a41SVille Syrjälä 
317470591a41SVille Syrjälä 	for_each_pipe(dev_priv, pipe)
317570591a41SVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), 0xffff);
317670591a41SVille Syrjälä 
317770591a41SVille Syrjälä 	GEN5_IRQ_RESET(VLV_);
317870591a41SVille Syrjälä }
317970591a41SVille Syrjälä 
31807e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
31817e231dbeSJesse Barnes {
31822d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
31837e231dbeSJesse Barnes 
31847e231dbeSJesse Barnes 	/* VLV magic */
31857e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
31867e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
31877e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
31887e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
31897e231dbeSJesse Barnes 
31907c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
31917e231dbeSJesse Barnes 
31927c4cde39SVille Syrjälä 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
31937e231dbeSJesse Barnes 
319470591a41SVille Syrjälä 	vlv_display_irq_reset(dev_priv);
31957e231dbeSJesse Barnes }
31967e231dbeSJesse Barnes 
3197d6e3cca3SDaniel Vetter static void gen8_gt_irq_reset(struct drm_i915_private *dev_priv)
3198d6e3cca3SDaniel Vetter {
3199d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 0);
3200d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 1);
3201d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 2);
3202d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 3);
3203d6e3cca3SDaniel Vetter }
3204d6e3cca3SDaniel Vetter 
3205823f6b38SPaulo Zanoni static void gen8_irq_reset(struct drm_device *dev)
3206abd58f01SBen Widawsky {
3207abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3208abd58f01SBen Widawsky 	int pipe;
3209abd58f01SBen Widawsky 
3210abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
3211abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3212abd58f01SBen Widawsky 
3213d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
3214abd58f01SBen Widawsky 
3215055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3216f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3217813bde43SPaulo Zanoni 						   POWER_DOMAIN_PIPE(pipe)))
3218f86f3fb0SPaulo Zanoni 			GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3219abd58f01SBen Widawsky 
3220f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_PORT_);
3221f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_MISC_);
3222f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_PCU_);
3223abd58f01SBen Widawsky 
3224266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev))
32251c69eb42SPaulo Zanoni 		ibx_irq_reset(dev);
3226abd58f01SBen Widawsky }
3227abd58f01SBen Widawsky 
32284c6c03beSDamien Lespiau void gen8_irq_power_well_post_enable(struct drm_i915_private *dev_priv,
32294c6c03beSDamien Lespiau 				     unsigned int pipe_mask)
3230d49bdb0eSPaulo Zanoni {
32311180e206SPaulo Zanoni 	uint32_t extra_ier = GEN8_PIPE_VBLANK | GEN8_PIPE_FIFO_UNDERRUN;
3232d49bdb0eSPaulo Zanoni 
323313321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3234d14c0343SDamien Lespiau 	if (pipe_mask & 1 << PIPE_A)
3235d14c0343SDamien Lespiau 		GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_A,
3236d14c0343SDamien Lespiau 				  dev_priv->de_irq_mask[PIPE_A],
3237d14c0343SDamien Lespiau 				  ~dev_priv->de_irq_mask[PIPE_A] | extra_ier);
32384c6c03beSDamien Lespiau 	if (pipe_mask & 1 << PIPE_B)
32394c6c03beSDamien Lespiau 		GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_B,
32404c6c03beSDamien Lespiau 				  dev_priv->de_irq_mask[PIPE_B],
32411180e206SPaulo Zanoni 				  ~dev_priv->de_irq_mask[PIPE_B] | extra_ier);
32424c6c03beSDamien Lespiau 	if (pipe_mask & 1 << PIPE_C)
32434c6c03beSDamien Lespiau 		GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_C,
32444c6c03beSDamien Lespiau 				  dev_priv->de_irq_mask[PIPE_C],
32451180e206SPaulo Zanoni 				  ~dev_priv->de_irq_mask[PIPE_C] | extra_ier);
324613321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3247d49bdb0eSPaulo Zanoni }
3248d49bdb0eSPaulo Zanoni 
324943f328d7SVille Syrjälä static void cherryview_irq_preinstall(struct drm_device *dev)
325043f328d7SVille Syrjälä {
325143f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
325243f328d7SVille Syrjälä 
325343f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
325443f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
325543f328d7SVille Syrjälä 
3256d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
325743f328d7SVille Syrjälä 
325843f328d7SVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
325943f328d7SVille Syrjälä 
326043f328d7SVille Syrjälä 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
326143f328d7SVille Syrjälä 
326270591a41SVille Syrjälä 	vlv_display_irq_reset(dev_priv);
326343f328d7SVille Syrjälä }
326443f328d7SVille Syrjälä 
326587a02106SVille Syrjälä static u32 intel_hpd_enabled_irqs(struct drm_device *dev,
326687a02106SVille Syrjälä 				  const u32 hpd[HPD_NUM_PINS])
326787a02106SVille Syrjälä {
326887a02106SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(dev);
326987a02106SVille Syrjälä 	struct intel_encoder *encoder;
327087a02106SVille Syrjälä 	u32 enabled_irqs = 0;
327187a02106SVille Syrjälä 
327287a02106SVille Syrjälä 	for_each_intel_encoder(dev, encoder)
327387a02106SVille Syrjälä 		if (dev_priv->hotplug.stats[encoder->hpd_pin].state == HPD_ENABLED)
327487a02106SVille Syrjälä 			enabled_irqs |= hpd[encoder->hpd_pin];
327587a02106SVille Syrjälä 
327687a02106SVille Syrjälä 	return enabled_irqs;
327787a02106SVille Syrjälä }
327887a02106SVille Syrjälä 
327982a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
328082a28bcfSDaniel Vetter {
32812d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
328287a02106SVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
328382a28bcfSDaniel Vetter 
328482a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
3285fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
328687a02106SVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_ibx);
328782a28bcfSDaniel Vetter 	} else {
3288fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
328987a02106SVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_cpt);
329082a28bcfSDaniel Vetter 	}
329182a28bcfSDaniel Vetter 
3292fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
329382a28bcfSDaniel Vetter 
32947fe0b973SKeith Packard 	/*
32957fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
32966dbf30ceSVille Syrjälä 	 * duration to 2ms (which is the minimum in the Display Port spec).
32976dbf30ceSVille Syrjälä 	 * The pulse duration bits are reserved on LPT+.
32987fe0b973SKeith Packard 	 */
32997fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
33007fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
33017fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
33027fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
33037fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
33040b2eb33eSVille Syrjälä 	/*
33050b2eb33eSVille Syrjälä 	 * When CPU and PCH are on the same package, port A
33060b2eb33eSVille Syrjälä 	 * HPD must be enabled in both north and south.
33070b2eb33eSVille Syrjälä 	 */
33080b2eb33eSVille Syrjälä 	if (HAS_PCH_LPT_LP(dev))
33090b2eb33eSVille Syrjälä 		hotplug |= PORTA_HOTPLUG_ENABLE;
33107fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
33116dbf30ceSVille Syrjälä }
331226951cafSXiong Zhang 
33136dbf30ceSVille Syrjälä static void spt_hpd_irq_setup(struct drm_device *dev)
33146dbf30ceSVille Syrjälä {
33156dbf30ceSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
33166dbf30ceSVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
33176dbf30ceSVille Syrjälä 
33186dbf30ceSVille Syrjälä 	hotplug_irqs = SDE_HOTPLUG_MASK_SPT;
33196dbf30ceSVille Syrjälä 	enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_spt);
33206dbf30ceSVille Syrjälä 
33216dbf30ceSVille Syrjälä 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
33226dbf30ceSVille Syrjälä 
33236dbf30ceSVille Syrjälä 	/* Enable digital hotplug on the PCH */
33246dbf30ceSVille Syrjälä 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
33256dbf30ceSVille Syrjälä 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTC_HOTPLUG_ENABLE |
332674c0b395SVille Syrjälä 		PORTB_HOTPLUG_ENABLE | PORTA_HOTPLUG_ENABLE;
33276dbf30ceSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
33286dbf30ceSVille Syrjälä 
332926951cafSXiong Zhang 	hotplug = I915_READ(PCH_PORT_HOTPLUG2);
333026951cafSXiong Zhang 	hotplug |= PORTE_HOTPLUG_ENABLE;
333126951cafSXiong Zhang 	I915_WRITE(PCH_PORT_HOTPLUG2, hotplug);
333226951cafSXiong Zhang }
33337fe0b973SKeith Packard 
3334e4ce95aaSVille Syrjälä static void ilk_hpd_irq_setup(struct drm_device *dev)
3335e4ce95aaSVille Syrjälä {
3336e4ce95aaSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
3337e4ce95aaSVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
3338e4ce95aaSVille Syrjälä 
33393a3b3c7dSVille Syrjälä 	if (INTEL_INFO(dev)->gen >= 8) {
33403a3b3c7dSVille Syrjälä 		hotplug_irqs = GEN8_PORT_DP_A_HOTPLUG;
33413a3b3c7dSVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_bdw);
33423a3b3c7dSVille Syrjälä 
33433a3b3c7dSVille Syrjälä 		bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
33443a3b3c7dSVille Syrjälä 	} else if (INTEL_INFO(dev)->gen >= 7) {
334523bb4cb5SVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG_IVB;
334623bb4cb5SVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_ivb);
33473a3b3c7dSVille Syrjälä 
33483a3b3c7dSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
334923bb4cb5SVille Syrjälä 	} else {
3350e4ce95aaSVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG;
3351e4ce95aaSVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_ilk);
3352e4ce95aaSVille Syrjälä 
3353e4ce95aaSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
33543a3b3c7dSVille Syrjälä 	}
3355e4ce95aaSVille Syrjälä 
3356e4ce95aaSVille Syrjälä 	/*
3357e4ce95aaSVille Syrjälä 	 * Enable digital hotplug on the CPU, and configure the DP short pulse
3358e4ce95aaSVille Syrjälä 	 * duration to 2ms (which is the minimum in the Display Port spec)
335923bb4cb5SVille Syrjälä 	 * The pulse duration bits are reserved on HSW+.
3360e4ce95aaSVille Syrjälä 	 */
3361e4ce95aaSVille Syrjälä 	hotplug = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
3362e4ce95aaSVille Syrjälä 	hotplug &= ~DIGITAL_PORTA_PULSE_DURATION_MASK;
3363e4ce95aaSVille Syrjälä 	hotplug |= DIGITAL_PORTA_HOTPLUG_ENABLE | DIGITAL_PORTA_PULSE_DURATION_2ms;
3364e4ce95aaSVille Syrjälä 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, hotplug);
3365e4ce95aaSVille Syrjälä 
3366e4ce95aaSVille Syrjälä 	ibx_hpd_irq_setup(dev);
3367e4ce95aaSVille Syrjälä }
3368e4ce95aaSVille Syrjälä 
3369e0a20ad7SShashank Sharma static void bxt_hpd_irq_setup(struct drm_device *dev)
3370e0a20ad7SShashank Sharma {
3371e0a20ad7SShashank Sharma 	struct drm_i915_private *dev_priv = dev->dev_private;
3372a52bb15bSVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
3373e0a20ad7SShashank Sharma 
3374a52bb15bSVille Syrjälä 	enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_bxt);
3375a52bb15bSVille Syrjälä 	hotplug_irqs = BXT_DE_PORT_HOTPLUG_MASK;
3376e0a20ad7SShashank Sharma 
3377a52bb15bSVille Syrjälä 	bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
3378e0a20ad7SShashank Sharma 
3379a52bb15bSVille Syrjälä 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
3380a52bb15bSVille Syrjälä 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTB_HOTPLUG_ENABLE |
3381a52bb15bSVille Syrjälä 		PORTA_HOTPLUG_ENABLE;
3382a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
3383e0a20ad7SShashank Sharma }
3384e0a20ad7SShashank Sharma 
3385d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
3386d46da437SPaulo Zanoni {
33872d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
338882a28bcfSDaniel Vetter 	u32 mask;
3389d46da437SPaulo Zanoni 
3390692a04cfSDaniel Vetter 	if (HAS_PCH_NOP(dev))
3391692a04cfSDaniel Vetter 		return;
3392692a04cfSDaniel Vetter 
3393105b122eSPaulo Zanoni 	if (HAS_PCH_IBX(dev))
33945c673b60SDaniel Vetter 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
3395105b122eSPaulo Zanoni 	else
33965c673b60SDaniel Vetter 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
33978664281bSPaulo Zanoni 
3398b51a2842SVille Syrjälä 	gen5_assert_iir_is_zero(dev_priv, SDEIIR);
3399d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
3400d46da437SPaulo Zanoni }
3401d46da437SPaulo Zanoni 
34020a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
34030a9a8c91SDaniel Vetter {
34040a9a8c91SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
34050a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
34060a9a8c91SDaniel Vetter 
34070a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
34080a9a8c91SDaniel Vetter 
34090a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
3410040d2baaSBen Widawsky 	if (HAS_L3_DPF(dev)) {
34110a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
341235a85ac6SBen Widawsky 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev);
341335a85ac6SBen Widawsky 		gt_irqs |= GT_PARITY_ERROR(dev);
34140a9a8c91SDaniel Vetter 	}
34150a9a8c91SDaniel Vetter 
34160a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
34170a9a8c91SDaniel Vetter 	if (IS_GEN5(dev)) {
34180a9a8c91SDaniel Vetter 		gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
34190a9a8c91SDaniel Vetter 			   ILK_BSD_USER_INTERRUPT;
34200a9a8c91SDaniel Vetter 	} else {
34210a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
34220a9a8c91SDaniel Vetter 	}
34230a9a8c91SDaniel Vetter 
342435079899SPaulo Zanoni 	GEN5_IRQ_INIT(GT, dev_priv->gt_irq_mask, gt_irqs);
34250a9a8c91SDaniel Vetter 
34260a9a8c91SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
342778e68d36SImre Deak 		/*
342878e68d36SImre Deak 		 * RPS interrupts will get enabled/disabled on demand when RPS
342978e68d36SImre Deak 		 * itself is enabled/disabled.
343078e68d36SImre Deak 		 */
34310a9a8c91SDaniel Vetter 		if (HAS_VEBOX(dev))
34320a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
34330a9a8c91SDaniel Vetter 
3434605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = 0xffffffff;
343535079899SPaulo Zanoni 		GEN5_IRQ_INIT(GEN6_PM, dev_priv->pm_irq_mask, pm_irqs);
34360a9a8c91SDaniel Vetter 	}
34370a9a8c91SDaniel Vetter }
34380a9a8c91SDaniel Vetter 
3439f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
3440036a4a7dSZhenyu Wang {
34412d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
34428e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
34438e76f8dcSPaulo Zanoni 
34448e76f8dcSPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 7) {
34458e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
34468e76f8dcSPaulo Zanoni 				DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
34478e76f8dcSPaulo Zanoni 				DE_PLANEB_FLIP_DONE_IVB |
34485c673b60SDaniel Vetter 				DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB);
34498e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
345023bb4cb5SVille Syrjälä 			      DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB |
345123bb4cb5SVille Syrjälä 			      DE_DP_A_HOTPLUG_IVB);
34528e76f8dcSPaulo Zanoni 	} else {
34538e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3454ce99c256SDaniel Vetter 				DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
34555b3a856bSDaniel Vetter 				DE_AUX_CHANNEL_A |
34565b3a856bSDaniel Vetter 				DE_PIPEB_CRC_DONE | DE_PIPEA_CRC_DONE |
34575b3a856bSDaniel Vetter 				DE_POISON);
3458e4ce95aaSVille Syrjälä 		extra_mask = (DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
3459e4ce95aaSVille Syrjälä 			      DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN |
3460e4ce95aaSVille Syrjälä 			      DE_DP_A_HOTPLUG);
34618e76f8dcSPaulo Zanoni 	}
3462036a4a7dSZhenyu Wang 
34631ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3464036a4a7dSZhenyu Wang 
34650c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xeffe);
34660c841212SPaulo Zanoni 
3467622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3468622364b6SPaulo Zanoni 
346935079899SPaulo Zanoni 	GEN5_IRQ_INIT(DE, dev_priv->irq_mask, display_mask | extra_mask);
3470036a4a7dSZhenyu Wang 
34710a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
3472036a4a7dSZhenyu Wang 
3473d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
34747fe0b973SKeith Packard 
3475f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
34766005ce42SDaniel Vetter 		/* Enable PCU event interrupts
34776005ce42SDaniel Vetter 		 *
34786005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
34794bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
34804bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
3481d6207435SDaniel Vetter 		spin_lock_irq(&dev_priv->irq_lock);
3482fbdedaeaSVille Syrjälä 		ilk_enable_display_irq(dev_priv, DE_PCU_EVENT);
3483d6207435SDaniel Vetter 		spin_unlock_irq(&dev_priv->irq_lock);
3484f97108d1SJesse Barnes 	}
3485f97108d1SJesse Barnes 
3486036a4a7dSZhenyu Wang 	return 0;
3487036a4a7dSZhenyu Wang }
3488036a4a7dSZhenyu Wang 
3489f8b79e58SImre Deak static void valleyview_display_irqs_install(struct drm_i915_private *dev_priv)
3490f8b79e58SImre Deak {
3491f8b79e58SImre Deak 	u32 pipestat_mask;
3492f8b79e58SImre Deak 	u32 iir_mask;
3493120dda4fSVille Syrjälä 	enum pipe pipe;
3494f8b79e58SImre Deak 
3495f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3496f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3497f8b79e58SImre Deak 
3498120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3499120dda4fSVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), pipestat_mask);
3500f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3501f8b79e58SImre Deak 
3502f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3503f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3504f8b79e58SImre Deak 
3505120dda4fSVille Syrjälä 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3506120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3507120dda4fSVille Syrjälä 		      i915_enable_pipestat(dev_priv, pipe, pipestat_mask);
3508f8b79e58SImre Deak 
3509f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3510f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3511f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3512120dda4fSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
3513120dda4fSVille Syrjälä 		iir_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
3514f8b79e58SImre Deak 	dev_priv->irq_mask &= ~iir_mask;
3515f8b79e58SImre Deak 
3516f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3517f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3518f8b79e58SImre Deak 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
351976e41860SVille Syrjälä 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
352076e41860SVille Syrjälä 	POSTING_READ(VLV_IMR);
3521f8b79e58SImre Deak }
3522f8b79e58SImre Deak 
3523f8b79e58SImre Deak static void valleyview_display_irqs_uninstall(struct drm_i915_private *dev_priv)
3524f8b79e58SImre Deak {
3525f8b79e58SImre Deak 	u32 pipestat_mask;
3526f8b79e58SImre Deak 	u32 iir_mask;
3527120dda4fSVille Syrjälä 	enum pipe pipe;
3528f8b79e58SImre Deak 
3529f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3530f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
35316c7fba04SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3532120dda4fSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
3533120dda4fSVille Syrjälä 		iir_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
3534f8b79e58SImre Deak 
3535f8b79e58SImre Deak 	dev_priv->irq_mask |= iir_mask;
3536f8b79e58SImre Deak 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
353776e41860SVille Syrjälä 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
3538f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3539f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3540f8b79e58SImre Deak 	POSTING_READ(VLV_IIR);
3541f8b79e58SImre Deak 
3542f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3543f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3544f8b79e58SImre Deak 
3545120dda4fSVille Syrjälä 	i915_disable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3546120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3547120dda4fSVille Syrjälä 		i915_disable_pipestat(dev_priv, pipe, pipestat_mask);
3548f8b79e58SImre Deak 
3549f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3550f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3551120dda4fSVille Syrjälä 
3552120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3553120dda4fSVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), pipestat_mask);
3554f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3555f8b79e58SImre Deak }
3556f8b79e58SImre Deak 
3557f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3558f8b79e58SImre Deak {
3559f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3560f8b79e58SImre Deak 
3561f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3562f8b79e58SImre Deak 		return;
3563f8b79e58SImre Deak 
3564f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3565f8b79e58SImre Deak 
3566950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3567f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3568f8b79e58SImre Deak }
3569f8b79e58SImre Deak 
3570f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3571f8b79e58SImre Deak {
3572f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3573f8b79e58SImre Deak 
3574f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3575f8b79e58SImre Deak 		return;
3576f8b79e58SImre Deak 
3577f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3578f8b79e58SImre Deak 
3579950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3580f8b79e58SImre Deak 		valleyview_display_irqs_uninstall(dev_priv);
3581f8b79e58SImre Deak }
3582f8b79e58SImre Deak 
35830e6c9a9eSVille Syrjälä static void vlv_display_irq_postinstall(struct drm_i915_private *dev_priv)
35847e231dbeSJesse Barnes {
3585f8b79e58SImre Deak 	dev_priv->irq_mask = ~0;
35867e231dbeSJesse Barnes 
35870706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
358820afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
358920afbda2SDaniel Vetter 
35907e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
359176e41860SVille Syrjälä 	I915_WRITE(VLV_IIR, 0xffffffff);
359276e41860SVille Syrjälä 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
359376e41860SVille Syrjälä 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
359476e41860SVille Syrjälä 	POSTING_READ(VLV_IMR);
35957e231dbeSJesse Barnes 
3596b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3597b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3598d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3599f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3600f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3601d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
36020e6c9a9eSVille Syrjälä }
36030e6c9a9eSVille Syrjälä 
36040e6c9a9eSVille Syrjälä static int valleyview_irq_postinstall(struct drm_device *dev)
36050e6c9a9eSVille Syrjälä {
36060e6c9a9eSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
36070e6c9a9eSVille Syrjälä 
36080e6c9a9eSVille Syrjälä 	vlv_display_irq_postinstall(dev_priv);
36097e231dbeSJesse Barnes 
36100a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
36117e231dbeSJesse Barnes 
36127e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
36137e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
36147e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
36157e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
36167e231dbeSJesse Barnes #endif
36177e231dbeSJesse Barnes 
36187e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
361920afbda2SDaniel Vetter 
362020afbda2SDaniel Vetter 	return 0;
362120afbda2SDaniel Vetter }
362220afbda2SDaniel Vetter 
3623abd58f01SBen Widawsky static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
3624abd58f01SBen Widawsky {
3625abd58f01SBen Widawsky 	/* These are interrupts we'll toggle with the ring mask register */
3626abd58f01SBen Widawsky 	uint32_t gt_interrupts[] = {
3627abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
362873d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
3629abd58f01SBen Widawsky 			GT_RENDER_L3_PARITY_ERROR_INTERRUPT |
363073d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT |
363173d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT,
3632abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
363373d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
363473d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT |
363573d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT,
3636abd58f01SBen Widawsky 		0,
363773d477f6SOscar Mateo 		GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT |
363873d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT
3639abd58f01SBen Widawsky 		};
3640abd58f01SBen Widawsky 
36410961021aSBen Widawsky 	dev_priv->pm_irq_mask = 0xffffffff;
36429a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 0, ~gt_interrupts[0], gt_interrupts[0]);
36439a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 1, ~gt_interrupts[1], gt_interrupts[1]);
364478e68d36SImre Deak 	/*
364578e68d36SImre Deak 	 * RPS interrupts will get enabled/disabled on demand when RPS itself
364678e68d36SImre Deak 	 * is enabled/disabled.
364778e68d36SImre Deak 	 */
364878e68d36SImre Deak 	GEN8_IRQ_INIT_NDX(GT, 2, dev_priv->pm_irq_mask, 0);
36499a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 3, ~gt_interrupts[3], gt_interrupts[3]);
3650abd58f01SBen Widawsky }
3651abd58f01SBen Widawsky 
3652abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3653abd58f01SBen Widawsky {
3654770de83dSDamien Lespiau 	uint32_t de_pipe_masked = GEN8_PIPE_CDCLK_CRC_DONE;
3655770de83dSDamien Lespiau 	uint32_t de_pipe_enables;
36563a3b3c7dSVille Syrjälä 	u32 de_port_masked = GEN8_AUX_CHANNEL_A;
36573a3b3c7dSVille Syrjälä 	u32 de_port_enables;
36583a3b3c7dSVille Syrjälä 	enum pipe pipe;
3659770de83dSDamien Lespiau 
3660b4834a50SRodrigo Vivi 	if (INTEL_INFO(dev_priv)->gen >= 9) {
3661770de83dSDamien Lespiau 		de_pipe_masked |= GEN9_PIPE_PLANE1_FLIP_DONE |
3662770de83dSDamien Lespiau 				  GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
36633a3b3c7dSVille Syrjälä 		de_port_masked |= GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
366488e04703SJesse Barnes 				  GEN9_AUX_CHANNEL_D;
36659e63743eSShashank Sharma 		if (IS_BROXTON(dev_priv))
36663a3b3c7dSVille Syrjälä 			de_port_masked |= BXT_DE_PORT_GMBUS;
36673a3b3c7dSVille Syrjälä 	} else {
3668770de83dSDamien Lespiau 		de_pipe_masked |= GEN8_PIPE_PRIMARY_FLIP_DONE |
3669770de83dSDamien Lespiau 				  GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
36703a3b3c7dSVille Syrjälä 	}
3671770de83dSDamien Lespiau 
3672770de83dSDamien Lespiau 	de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
3673770de83dSDamien Lespiau 					   GEN8_PIPE_FIFO_UNDERRUN;
3674770de83dSDamien Lespiau 
36753a3b3c7dSVille Syrjälä 	de_port_enables = de_port_masked;
3676a52bb15bSVille Syrjälä 	if (IS_BROXTON(dev_priv))
3677a52bb15bSVille Syrjälä 		de_port_enables |= BXT_DE_PORT_HOTPLUG_MASK;
3678a52bb15bSVille Syrjälä 	else if (IS_BROADWELL(dev_priv))
36793a3b3c7dSVille Syrjälä 		de_port_enables |= GEN8_PORT_DP_A_HOTPLUG;
36803a3b3c7dSVille Syrjälä 
368113b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_A] = ~de_pipe_masked;
368213b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_B] = ~de_pipe_masked;
368313b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_C] = ~de_pipe_masked;
3684abd58f01SBen Widawsky 
3685055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3686f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3687813bde43SPaulo Zanoni 				POWER_DOMAIN_PIPE(pipe)))
3688813bde43SPaulo Zanoni 			GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
3689813bde43SPaulo Zanoni 					  dev_priv->de_irq_mask[pipe],
369035079899SPaulo Zanoni 					  de_pipe_enables);
3691abd58f01SBen Widawsky 
36923a3b3c7dSVille Syrjälä 	GEN5_IRQ_INIT(GEN8_DE_PORT_, ~de_port_masked, de_port_enables);
3693abd58f01SBen Widawsky }
3694abd58f01SBen Widawsky 
3695abd58f01SBen Widawsky static int gen8_irq_postinstall(struct drm_device *dev)
3696abd58f01SBen Widawsky {
3697abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3698abd58f01SBen Widawsky 
3699266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev))
3700622364b6SPaulo Zanoni 		ibx_irq_pre_postinstall(dev);
3701622364b6SPaulo Zanoni 
3702abd58f01SBen Widawsky 	gen8_gt_irq_postinstall(dev_priv);
3703abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3704abd58f01SBen Widawsky 
3705266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev))
3706abd58f01SBen Widawsky 		ibx_irq_postinstall(dev);
3707abd58f01SBen Widawsky 
3708abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
3709abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3710abd58f01SBen Widawsky 
3711abd58f01SBen Widawsky 	return 0;
3712abd58f01SBen Widawsky }
3713abd58f01SBen Widawsky 
371443f328d7SVille Syrjälä static int cherryview_irq_postinstall(struct drm_device *dev)
371543f328d7SVille Syrjälä {
371643f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
371743f328d7SVille Syrjälä 
3718c2b66797SVille Syrjälä 	vlv_display_irq_postinstall(dev_priv);
371943f328d7SVille Syrjälä 
372043f328d7SVille Syrjälä 	gen8_gt_irq_postinstall(dev_priv);
372143f328d7SVille Syrjälä 
372243f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, MASTER_INTERRUPT_ENABLE);
372343f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
372443f328d7SVille Syrjälä 
372543f328d7SVille Syrjälä 	return 0;
372643f328d7SVille Syrjälä }
372743f328d7SVille Syrjälä 
3728abd58f01SBen Widawsky static void gen8_irq_uninstall(struct drm_device *dev)
3729abd58f01SBen Widawsky {
3730abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3731abd58f01SBen Widawsky 
3732abd58f01SBen Widawsky 	if (!dev_priv)
3733abd58f01SBen Widawsky 		return;
3734abd58f01SBen Widawsky 
3735823f6b38SPaulo Zanoni 	gen8_irq_reset(dev);
3736abd58f01SBen Widawsky }
3737abd58f01SBen Widawsky 
37388ea0be4fSVille Syrjälä static void vlv_display_irq_uninstall(struct drm_i915_private *dev_priv)
37398ea0be4fSVille Syrjälä {
37408ea0be4fSVille Syrjälä 	/* Interrupt setup is already guaranteed to be single-threaded, this is
37418ea0be4fSVille Syrjälä 	 * just to make the assert_spin_locked check happy. */
37428ea0be4fSVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
37438ea0be4fSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
37448ea0be4fSVille Syrjälä 		valleyview_display_irqs_uninstall(dev_priv);
37458ea0be4fSVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
37468ea0be4fSVille Syrjälä 
37478ea0be4fSVille Syrjälä 	vlv_display_irq_reset(dev_priv);
37488ea0be4fSVille Syrjälä 
3749c352d1baSImre Deak 	dev_priv->irq_mask = ~0;
37508ea0be4fSVille Syrjälä }
37518ea0be4fSVille Syrjälä 
37527e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
37537e231dbeSJesse Barnes {
37542d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
37557e231dbeSJesse Barnes 
37567e231dbeSJesse Barnes 	if (!dev_priv)
37577e231dbeSJesse Barnes 		return;
37587e231dbeSJesse Barnes 
3759843d0e7dSImre Deak 	I915_WRITE(VLV_MASTER_IER, 0);
3760843d0e7dSImre Deak 
3761893fce8eSVille Syrjälä 	gen5_gt_irq_reset(dev);
3762893fce8eSVille Syrjälä 
37637e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
3764f8b79e58SImre Deak 
37658ea0be4fSVille Syrjälä 	vlv_display_irq_uninstall(dev_priv);
37667e231dbeSJesse Barnes }
37677e231dbeSJesse Barnes 
376843f328d7SVille Syrjälä static void cherryview_irq_uninstall(struct drm_device *dev)
376943f328d7SVille Syrjälä {
377043f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
377143f328d7SVille Syrjälä 
377243f328d7SVille Syrjälä 	if (!dev_priv)
377343f328d7SVille Syrjälä 		return;
377443f328d7SVille Syrjälä 
377543f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
377643f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
377743f328d7SVille Syrjälä 
3778a2c30fbaSVille Syrjälä 	gen8_gt_irq_reset(dev_priv);
377943f328d7SVille Syrjälä 
3780a2c30fbaSVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
378143f328d7SVille Syrjälä 
3782c2b66797SVille Syrjälä 	vlv_display_irq_uninstall(dev_priv);
378343f328d7SVille Syrjälä }
378443f328d7SVille Syrjälä 
3785f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
3786036a4a7dSZhenyu Wang {
37872d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
37884697995bSJesse Barnes 
37894697995bSJesse Barnes 	if (!dev_priv)
37904697995bSJesse Barnes 		return;
37914697995bSJesse Barnes 
3792be30b29fSPaulo Zanoni 	ironlake_irq_reset(dev);
3793036a4a7dSZhenyu Wang }
3794036a4a7dSZhenyu Wang 
3795c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
3796c2798b19SChris Wilson {
37972d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3798c2798b19SChris Wilson 	int pipe;
3799c2798b19SChris Wilson 
3800055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3801c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3802c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3803c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3804c2798b19SChris Wilson 	POSTING_READ16(IER);
3805c2798b19SChris Wilson }
3806c2798b19SChris Wilson 
3807c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
3808c2798b19SChris Wilson {
38092d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3810c2798b19SChris Wilson 
3811c2798b19SChris Wilson 	I915_WRITE16(EMR,
3812c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3813c2798b19SChris Wilson 
3814c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
3815c2798b19SChris Wilson 	dev_priv->irq_mask =
3816c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3817c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3818c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
381937ef01abSDaniel Vetter 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
3820c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
3821c2798b19SChris Wilson 
3822c2798b19SChris Wilson 	I915_WRITE16(IER,
3823c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3824c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3825c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
3826c2798b19SChris Wilson 	POSTING_READ16(IER);
3827c2798b19SChris Wilson 
3828379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3829379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3830d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3831755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3832755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3833d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3834379ef82dSDaniel Vetter 
3835c2798b19SChris Wilson 	return 0;
3836c2798b19SChris Wilson }
3837c2798b19SChris Wilson 
383890a72f87SVille Syrjälä /*
383990a72f87SVille Syrjälä  * Returns true when a page flip has completed.
384090a72f87SVille Syrjälä  */
384190a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
38421f1c2e24SVille Syrjälä 			       int plane, int pipe, u32 iir)
384390a72f87SVille Syrjälä {
38442d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
38451f1c2e24SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
384690a72f87SVille Syrjälä 
38478d7849dbSVille Syrjälä 	if (!intel_pipe_handle_vblank(dev, pipe))
384890a72f87SVille Syrjälä 		return false;
384990a72f87SVille Syrjälä 
385090a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
3851d6bbafa1SChris Wilson 		goto check_page_flip;
385290a72f87SVille Syrjälä 
385390a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
385490a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
385590a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
385690a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
385790a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
385890a72f87SVille Syrjälä 	 */
385990a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
3860d6bbafa1SChris Wilson 		goto check_page_flip;
386190a72f87SVille Syrjälä 
38627d47559eSVille Syrjälä 	intel_prepare_page_flip(dev, plane);
386390a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
386490a72f87SVille Syrjälä 	return true;
3865d6bbafa1SChris Wilson 
3866d6bbafa1SChris Wilson check_page_flip:
3867d6bbafa1SChris Wilson 	intel_check_page_flip(dev, pipe);
3868d6bbafa1SChris Wilson 	return false;
386990a72f87SVille Syrjälä }
387090a72f87SVille Syrjälä 
3871ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3872c2798b19SChris Wilson {
387345a83f84SDaniel Vetter 	struct drm_device *dev = arg;
38742d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3875c2798b19SChris Wilson 	u16 iir, new_iir;
3876c2798b19SChris Wilson 	u32 pipe_stats[2];
3877c2798b19SChris Wilson 	int pipe;
3878c2798b19SChris Wilson 	u16 flip_mask =
3879c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3880c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3881c2798b19SChris Wilson 
38822dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
38832dd2a883SImre Deak 		return IRQ_NONE;
38842dd2a883SImre Deak 
3885c2798b19SChris Wilson 	iir = I915_READ16(IIR);
3886c2798b19SChris Wilson 	if (iir == 0)
3887c2798b19SChris Wilson 		return IRQ_NONE;
3888c2798b19SChris Wilson 
3889c2798b19SChris Wilson 	while (iir & ~flip_mask) {
3890c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3891c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3892c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3893c2798b19SChris Wilson 		 * interrupts (for non-MSI).
3894c2798b19SChris Wilson 		 */
3895222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
3896c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3897aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
3898c2798b19SChris Wilson 
3899055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
3900f0f59a00SVille Syrjälä 			i915_reg_t reg = PIPESTAT(pipe);
3901c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3902c2798b19SChris Wilson 
3903c2798b19SChris Wilson 			/*
3904c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3905c2798b19SChris Wilson 			 */
39062d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & 0x8000ffff)
3907c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3908c2798b19SChris Wilson 		}
3909222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
3910c2798b19SChris Wilson 
3911c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
3912c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
3913c2798b19SChris Wilson 
3914c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
391574cdb337SChris Wilson 			notify_ring(&dev_priv->ring[RCS]);
3916c2798b19SChris Wilson 
3917055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
39181f1c2e24SVille Syrjälä 			int plane = pipe;
39193a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
39201f1c2e24SVille Syrjälä 				plane = !plane;
39211f1c2e24SVille Syrjälä 
39224356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
39231f1c2e24SVille Syrjälä 			    i8xx_handle_vblank(dev, plane, pipe, iir))
39241f1c2e24SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3925c2798b19SChris Wilson 
39264356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3927277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
39282d9d2b0bSVille Syrjälä 
39291f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
39301f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
39311f7247c0SDaniel Vetter 								    pipe);
39324356d586SDaniel Vetter 		}
3933c2798b19SChris Wilson 
3934c2798b19SChris Wilson 		iir = new_iir;
3935c2798b19SChris Wilson 	}
3936c2798b19SChris Wilson 
3937c2798b19SChris Wilson 	return IRQ_HANDLED;
3938c2798b19SChris Wilson }
3939c2798b19SChris Wilson 
3940c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
3941c2798b19SChris Wilson {
39422d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3943c2798b19SChris Wilson 	int pipe;
3944c2798b19SChris Wilson 
3945055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
3946c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
3947c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3948c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
3949c2798b19SChris Wilson 	}
3950c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3951c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3952c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
3953c2798b19SChris Wilson }
3954c2798b19SChris Wilson 
3955a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
3956a266c7d5SChris Wilson {
39572d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3958a266c7d5SChris Wilson 	int pipe;
3959a266c7d5SChris Wilson 
3960a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
39610706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
3962a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3963a266c7d5SChris Wilson 	}
3964a266c7d5SChris Wilson 
396500d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
3966055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3967a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3968a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3969a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3970a266c7d5SChris Wilson 	POSTING_READ(IER);
3971a266c7d5SChris Wilson }
3972a266c7d5SChris Wilson 
3973a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
3974a266c7d5SChris Wilson {
39752d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
397638bde180SChris Wilson 	u32 enable_mask;
3977a266c7d5SChris Wilson 
397838bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
397938bde180SChris Wilson 
398038bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
398138bde180SChris Wilson 	dev_priv->irq_mask =
398238bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
398338bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
398438bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
398538bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
398637ef01abSDaniel Vetter 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
398738bde180SChris Wilson 
398838bde180SChris Wilson 	enable_mask =
398938bde180SChris Wilson 		I915_ASLE_INTERRUPT |
399038bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
399138bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
399238bde180SChris Wilson 		I915_USER_INTERRUPT;
399338bde180SChris Wilson 
3994a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
39950706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
399620afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
399720afbda2SDaniel Vetter 
3998a266c7d5SChris Wilson 		/* Enable in IER... */
3999a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
4000a266c7d5SChris Wilson 		/* and unmask in IMR */
4001a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
4002a266c7d5SChris Wilson 	}
4003a266c7d5SChris Wilson 
4004a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
4005a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
4006a266c7d5SChris Wilson 	POSTING_READ(IER);
4007a266c7d5SChris Wilson 
4008f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
400920afbda2SDaniel Vetter 
4010379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4011379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4012d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4013755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4014755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4015d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4016379ef82dSDaniel Vetter 
401720afbda2SDaniel Vetter 	return 0;
401820afbda2SDaniel Vetter }
401920afbda2SDaniel Vetter 
402090a72f87SVille Syrjälä /*
402190a72f87SVille Syrjälä  * Returns true when a page flip has completed.
402290a72f87SVille Syrjälä  */
402390a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
402490a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
402590a72f87SVille Syrjälä {
40262d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
402790a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
402890a72f87SVille Syrjälä 
40298d7849dbSVille Syrjälä 	if (!intel_pipe_handle_vblank(dev, pipe))
403090a72f87SVille Syrjälä 		return false;
403190a72f87SVille Syrjälä 
403290a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
4033d6bbafa1SChris Wilson 		goto check_page_flip;
403490a72f87SVille Syrjälä 
403590a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
403690a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
403790a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
403890a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
403990a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
404090a72f87SVille Syrjälä 	 */
404190a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
4042d6bbafa1SChris Wilson 		goto check_page_flip;
404390a72f87SVille Syrjälä 
40447d47559eSVille Syrjälä 	intel_prepare_page_flip(dev, plane);
404590a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
404690a72f87SVille Syrjälä 	return true;
4047d6bbafa1SChris Wilson 
4048d6bbafa1SChris Wilson check_page_flip:
4049d6bbafa1SChris Wilson 	intel_check_page_flip(dev, pipe);
4050d6bbafa1SChris Wilson 	return false;
405190a72f87SVille Syrjälä }
405290a72f87SVille Syrjälä 
4053ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
4054a266c7d5SChris Wilson {
405545a83f84SDaniel Vetter 	struct drm_device *dev = arg;
40562d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
40578291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
405838bde180SChris Wilson 	u32 flip_mask =
405938bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
406038bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
406138bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
4062a266c7d5SChris Wilson 
40632dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
40642dd2a883SImre Deak 		return IRQ_NONE;
40652dd2a883SImre Deak 
4066a266c7d5SChris Wilson 	iir = I915_READ(IIR);
406738bde180SChris Wilson 	do {
406838bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
40698291ee90SChris Wilson 		bool blc_event = false;
4070a266c7d5SChris Wilson 
4071a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4072a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4073a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4074a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
4075a266c7d5SChris Wilson 		 */
4076222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
4077a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4078aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4079a266c7d5SChris Wilson 
4080055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4081f0f59a00SVille Syrjälä 			i915_reg_t reg = PIPESTAT(pipe);
4082a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4083a266c7d5SChris Wilson 
408438bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
4085a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4086a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
408738bde180SChris Wilson 				irq_received = true;
4088a266c7d5SChris Wilson 			}
4089a266c7d5SChris Wilson 		}
4090222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4091a266c7d5SChris Wilson 
4092a266c7d5SChris Wilson 		if (!irq_received)
4093a266c7d5SChris Wilson 			break;
4094a266c7d5SChris Wilson 
4095a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
409616c6c56bSVille Syrjälä 		if (I915_HAS_HOTPLUG(dev) &&
409716c6c56bSVille Syrjälä 		    iir & I915_DISPLAY_PORT_INTERRUPT)
409816c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
4099a266c7d5SChris Wilson 
410038bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
4101a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4102a266c7d5SChris Wilson 
4103a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
410474cdb337SChris Wilson 			notify_ring(&dev_priv->ring[RCS]);
4105a266c7d5SChris Wilson 
4106055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
410738bde180SChris Wilson 			int plane = pipe;
41083a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
410938bde180SChris Wilson 				plane = !plane;
41105e2032d4SVille Syrjälä 
411190a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
411290a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
411390a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
4114a266c7d5SChris Wilson 
4115a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4116a266c7d5SChris Wilson 				blc_event = true;
41174356d586SDaniel Vetter 
41184356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
4119277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
41202d9d2b0bSVille Syrjälä 
41211f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
41221f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
41231f7247c0SDaniel Vetter 								    pipe);
4124a266c7d5SChris Wilson 		}
4125a266c7d5SChris Wilson 
4126a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
4127a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
4128a266c7d5SChris Wilson 
4129a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4130a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4131a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4132a266c7d5SChris Wilson 		 * we would never get another interrupt.
4133a266c7d5SChris Wilson 		 *
4134a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4135a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4136a266c7d5SChris Wilson 		 * another one.
4137a266c7d5SChris Wilson 		 *
4138a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4139a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4140a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4141a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4142a266c7d5SChris Wilson 		 * stray interrupts.
4143a266c7d5SChris Wilson 		 */
414438bde180SChris Wilson 		ret = IRQ_HANDLED;
4145a266c7d5SChris Wilson 		iir = new_iir;
414638bde180SChris Wilson 	} while (iir & ~flip_mask);
4147a266c7d5SChris Wilson 
4148a266c7d5SChris Wilson 	return ret;
4149a266c7d5SChris Wilson }
4150a266c7d5SChris Wilson 
4151a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
4152a266c7d5SChris Wilson {
41532d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4154a266c7d5SChris Wilson 	int pipe;
4155a266c7d5SChris Wilson 
4156a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
41570706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4158a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4159a266c7d5SChris Wilson 	}
4160a266c7d5SChris Wilson 
416100d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
4162055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
416355b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
4164a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
416555b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
416655b39755SChris Wilson 	}
4167a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4168a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4169a266c7d5SChris Wilson 
4170a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4171a266c7d5SChris Wilson }
4172a266c7d5SChris Wilson 
4173a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
4174a266c7d5SChris Wilson {
41752d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4176a266c7d5SChris Wilson 	int pipe;
4177a266c7d5SChris Wilson 
41780706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4179a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4180a266c7d5SChris Wilson 
4181a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
4182055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4183a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4184a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4185a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4186a266c7d5SChris Wilson 	POSTING_READ(IER);
4187a266c7d5SChris Wilson }
4188a266c7d5SChris Wilson 
4189a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
4190a266c7d5SChris Wilson {
41912d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4192bbba0a97SChris Wilson 	u32 enable_mask;
4193a266c7d5SChris Wilson 	u32 error_mask;
4194a266c7d5SChris Wilson 
4195a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
4196bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
4197adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
4198bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4199bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
4200bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
4201bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
4202bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
4203bbba0a97SChris Wilson 
4204bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
420521ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
420621ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
4207bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
4208bbba0a97SChris Wilson 
4209bbba0a97SChris Wilson 	if (IS_G4X(dev))
4210bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
4211a266c7d5SChris Wilson 
4212b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4213b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4214d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4215755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
4216755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4217755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4218d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4219a266c7d5SChris Wilson 
4220a266c7d5SChris Wilson 	/*
4221a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
4222a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
4223a266c7d5SChris Wilson 	 */
4224a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
4225a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
4226a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
4227a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
4228a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4229a266c7d5SChris Wilson 	} else {
4230a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
4231a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4232a266c7d5SChris Wilson 	}
4233a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
4234a266c7d5SChris Wilson 
4235a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
4236a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
4237a266c7d5SChris Wilson 	POSTING_READ(IER);
4238a266c7d5SChris Wilson 
42390706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
424020afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
424120afbda2SDaniel Vetter 
4242f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
424320afbda2SDaniel Vetter 
424420afbda2SDaniel Vetter 	return 0;
424520afbda2SDaniel Vetter }
424620afbda2SDaniel Vetter 
4247bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
424820afbda2SDaniel Vetter {
42492d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
425020afbda2SDaniel Vetter 	u32 hotplug_en;
425120afbda2SDaniel Vetter 
4252b5ea2d56SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
4253b5ea2d56SDaniel Vetter 
4254adca4730SChris Wilson 	/* Note HDMI and DP share hotplug bits */
4255e5868a31SEgbert Eich 	/* enable bits are the same for all generations */
42560706f17cSEgbert Eich 	hotplug_en = intel_hpd_enabled_irqs(dev, hpd_mask_i915);
4257a266c7d5SChris Wilson 	/* Programming the CRT detection parameters tends
4258a266c7d5SChris Wilson 	   to generate a spurious hotplug event about three
4259a266c7d5SChris Wilson 	   seconds later.  So just do it once.
4260a266c7d5SChris Wilson 	*/
4261a266c7d5SChris Wilson 	if (IS_G4X(dev))
4262a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
4263a266c7d5SChris Wilson 	hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
4264a266c7d5SChris Wilson 
4265a266c7d5SChris Wilson 	/* Ignore TV since it's buggy */
42660706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv,
4267f9e3dc78SJani Nikula 					     HOTPLUG_INT_EN_MASK |
4268f9e3dc78SJani Nikula 					     CRT_HOTPLUG_VOLTAGE_COMPARE_MASK |
4269f9e3dc78SJani Nikula 					     CRT_HOTPLUG_ACTIVATION_PERIOD_64,
42700706f17cSEgbert Eich 					     hotplug_en);
4271a266c7d5SChris Wilson }
4272a266c7d5SChris Wilson 
4273ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
4274a266c7d5SChris Wilson {
427545a83f84SDaniel Vetter 	struct drm_device *dev = arg;
42762d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4277a266c7d5SChris Wilson 	u32 iir, new_iir;
4278a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
4279a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
428021ad8330SVille Syrjälä 	u32 flip_mask =
428121ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
428221ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
4283a266c7d5SChris Wilson 
42842dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
42852dd2a883SImre Deak 		return IRQ_NONE;
42862dd2a883SImre Deak 
4287a266c7d5SChris Wilson 	iir = I915_READ(IIR);
4288a266c7d5SChris Wilson 
4289a266c7d5SChris Wilson 	for (;;) {
4290501e01d7SVille Syrjälä 		bool irq_received = (iir & ~flip_mask) != 0;
42912c8ba29fSChris Wilson 		bool blc_event = false;
42922c8ba29fSChris Wilson 
4293a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4294a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4295a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4296a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
4297a266c7d5SChris Wilson 		 */
4298222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
4299a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4300aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4301a266c7d5SChris Wilson 
4302055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4303f0f59a00SVille Syrjälä 			i915_reg_t reg = PIPESTAT(pipe);
4304a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4305a266c7d5SChris Wilson 
4306a266c7d5SChris Wilson 			/*
4307a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
4308a266c7d5SChris Wilson 			 */
4309a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4310a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
4311501e01d7SVille Syrjälä 				irq_received = true;
4312a266c7d5SChris Wilson 			}
4313a266c7d5SChris Wilson 		}
4314222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4315a266c7d5SChris Wilson 
4316a266c7d5SChris Wilson 		if (!irq_received)
4317a266c7d5SChris Wilson 			break;
4318a266c7d5SChris Wilson 
4319a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
4320a266c7d5SChris Wilson 
4321a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
432216c6c56bSVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
432316c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
4324a266c7d5SChris Wilson 
432521ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
4326a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4327a266c7d5SChris Wilson 
4328a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
432974cdb337SChris Wilson 			notify_ring(&dev_priv->ring[RCS]);
4330a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
433174cdb337SChris Wilson 			notify_ring(&dev_priv->ring[VCS]);
4332a266c7d5SChris Wilson 
4333055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
43342c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
433590a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
433690a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
4337a266c7d5SChris Wilson 
4338a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4339a266c7d5SChris Wilson 				blc_event = true;
43404356d586SDaniel Vetter 
43414356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
4342277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
4343a266c7d5SChris Wilson 
43441f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
43451f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
43462d9d2b0bSVille Syrjälä 		}
4347a266c7d5SChris Wilson 
4348a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
4349a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
4350a266c7d5SChris Wilson 
4351515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
4352515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
4353515ac2bbSDaniel Vetter 
4354a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4355a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4356a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4357a266c7d5SChris Wilson 		 * we would never get another interrupt.
4358a266c7d5SChris Wilson 		 *
4359a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4360a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4361a266c7d5SChris Wilson 		 * another one.
4362a266c7d5SChris Wilson 		 *
4363a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4364a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4365a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4366a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4367a266c7d5SChris Wilson 		 * stray interrupts.
4368a266c7d5SChris Wilson 		 */
4369a266c7d5SChris Wilson 		iir = new_iir;
4370a266c7d5SChris Wilson 	}
4371a266c7d5SChris Wilson 
4372a266c7d5SChris Wilson 	return ret;
4373a266c7d5SChris Wilson }
4374a266c7d5SChris Wilson 
4375a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
4376a266c7d5SChris Wilson {
43772d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4378a266c7d5SChris Wilson 	int pipe;
4379a266c7d5SChris Wilson 
4380a266c7d5SChris Wilson 	if (!dev_priv)
4381a266c7d5SChris Wilson 		return;
4382a266c7d5SChris Wilson 
43830706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4384a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4385a266c7d5SChris Wilson 
4386a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
4387055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4388a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4389a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4390a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4391a266c7d5SChris Wilson 
4392055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4393a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
4394a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
4395a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4396a266c7d5SChris Wilson }
4397a266c7d5SChris Wilson 
4398fca52a55SDaniel Vetter /**
4399fca52a55SDaniel Vetter  * intel_irq_init - initializes irq support
4400fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4401fca52a55SDaniel Vetter  *
4402fca52a55SDaniel Vetter  * This function initializes all the irq support including work items, timers
4403fca52a55SDaniel Vetter  * and all the vtables. It does not setup the interrupt itself though.
4404fca52a55SDaniel Vetter  */
4405b963291cSDaniel Vetter void intel_irq_init(struct drm_i915_private *dev_priv)
4406f71d4af4SJesse Barnes {
4407b963291cSDaniel Vetter 	struct drm_device *dev = dev_priv->dev;
44088b2e326dSChris Wilson 
440977913b39SJani Nikula 	intel_hpd_init_work(dev_priv);
441077913b39SJani Nikula 
4411c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
4412a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
44138b2e326dSChris Wilson 
4414a6706b45SDeepak S 	/* Let's track the enabled rps events */
4415*666a4537SWayne Boyer 	if (IS_VALLEYVIEW(dev_priv))
44166c65a587SVille Syrjälä 		/* WaGsvRC0ResidencyMethod:vlv */
44176f4b12f8SChris Wilson 		dev_priv->pm_rps_events = GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED;
441831685c25SDeepak S 	else
4419a6706b45SDeepak S 		dev_priv->pm_rps_events = GEN6_PM_RPS_EVENTS;
4420a6706b45SDeepak S 
4421737b1506SChris Wilson 	INIT_DELAYED_WORK(&dev_priv->gpu_error.hangcheck_work,
4422737b1506SChris Wilson 			  i915_hangcheck_elapsed);
442361bac78eSDaniel Vetter 
442497a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
44259ee32feaSDaniel Vetter 
4426b963291cSDaniel Vetter 	if (IS_GEN2(dev_priv)) {
44274cdb83ecSVille Syrjälä 		dev->max_vblank_count = 0;
44284cdb83ecSVille Syrjälä 		dev->driver->get_vblank_counter = i8xx_get_vblank_counter;
4429b963291cSDaniel Vetter 	} else if (IS_G4X(dev_priv) || INTEL_INFO(dev_priv)->gen >= 5) {
4430f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
4431fd8f507cSVille Syrjälä 		dev->driver->get_vblank_counter = g4x_get_vblank_counter;
4432391f75e2SVille Syrjälä 	} else {
4433391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
4434391f75e2SVille Syrjälä 		dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
4435f71d4af4SJesse Barnes 	}
4436f71d4af4SJesse Barnes 
443721da2700SVille Syrjälä 	/*
443821da2700SVille Syrjälä 	 * Opt out of the vblank disable timer on everything except gen2.
443921da2700SVille Syrjälä 	 * Gen2 doesn't have a hardware frame counter and so depends on
444021da2700SVille Syrjälä 	 * vblank interrupts to produce sane vblank seuquence numbers.
444121da2700SVille Syrjälä 	 */
4442b963291cSDaniel Vetter 	if (!IS_GEN2(dev_priv))
444321da2700SVille Syrjälä 		dev->vblank_disable_immediate = true;
444421da2700SVille Syrjälä 
4445f71d4af4SJesse Barnes 	dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
4446f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
4447f71d4af4SJesse Barnes 
4448b963291cSDaniel Vetter 	if (IS_CHERRYVIEW(dev_priv)) {
444943f328d7SVille Syrjälä 		dev->driver->irq_handler = cherryview_irq_handler;
445043f328d7SVille Syrjälä 		dev->driver->irq_preinstall = cherryview_irq_preinstall;
445143f328d7SVille Syrjälä 		dev->driver->irq_postinstall = cherryview_irq_postinstall;
445243f328d7SVille Syrjälä 		dev->driver->irq_uninstall = cherryview_irq_uninstall;
445343f328d7SVille Syrjälä 		dev->driver->enable_vblank = valleyview_enable_vblank;
445443f328d7SVille Syrjälä 		dev->driver->disable_vblank = valleyview_disable_vblank;
445543f328d7SVille Syrjälä 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4456b963291cSDaniel Vetter 	} else if (IS_VALLEYVIEW(dev_priv)) {
44577e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
44587e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
44597e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
44607e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
44617e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
44627e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
4463fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4464b963291cSDaniel Vetter 	} else if (INTEL_INFO(dev_priv)->gen >= 8) {
4465abd58f01SBen Widawsky 		dev->driver->irq_handler = gen8_irq_handler;
4466723761b8SDaniel Vetter 		dev->driver->irq_preinstall = gen8_irq_reset;
4467abd58f01SBen Widawsky 		dev->driver->irq_postinstall = gen8_irq_postinstall;
4468abd58f01SBen Widawsky 		dev->driver->irq_uninstall = gen8_irq_uninstall;
4469abd58f01SBen Widawsky 		dev->driver->enable_vblank = gen8_enable_vblank;
4470abd58f01SBen Widawsky 		dev->driver->disable_vblank = gen8_disable_vblank;
44716dbf30ceSVille Syrjälä 		if (IS_BROXTON(dev))
4472e0a20ad7SShashank Sharma 			dev_priv->display.hpd_irq_setup = bxt_hpd_irq_setup;
44736dbf30ceSVille Syrjälä 		else if (HAS_PCH_SPT(dev))
44746dbf30ceSVille Syrjälä 			dev_priv->display.hpd_irq_setup = spt_hpd_irq_setup;
44756dbf30ceSVille Syrjälä 		else
44763a3b3c7dSVille Syrjälä 			dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
4477f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
4478f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
4479723761b8SDaniel Vetter 		dev->driver->irq_preinstall = ironlake_irq_reset;
4480f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
4481f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
4482f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
4483f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
4484e4ce95aaSVille Syrjälä 		dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
4485f71d4af4SJesse Barnes 	} else {
4486b963291cSDaniel Vetter 		if (INTEL_INFO(dev_priv)->gen == 2) {
4487c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
4488c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
4489c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
4490c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
4491b963291cSDaniel Vetter 		} else if (INTEL_INFO(dev_priv)->gen == 3) {
4492a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
4493a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
4494a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
4495a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
4496c2798b19SChris Wilson 		} else {
4497a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
4498a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
4499a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
4500a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
4501c2798b19SChris Wilson 		}
4502778eb334SVille Syrjälä 		if (I915_HAS_HOTPLUG(dev_priv))
4503778eb334SVille Syrjälä 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4504f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
4505f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
4506f71d4af4SJesse Barnes 	}
4507f71d4af4SJesse Barnes }
450820afbda2SDaniel Vetter 
4509fca52a55SDaniel Vetter /**
4510fca52a55SDaniel Vetter  * intel_irq_install - enables the hardware interrupt
4511fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4512fca52a55SDaniel Vetter  *
4513fca52a55SDaniel Vetter  * This function enables the hardware interrupt handling, but leaves the hotplug
4514fca52a55SDaniel Vetter  * handling still disabled. It is called after intel_irq_init().
4515fca52a55SDaniel Vetter  *
4516fca52a55SDaniel Vetter  * In the driver load and resume code we need working interrupts in a few places
4517fca52a55SDaniel Vetter  * but don't want to deal with the hassle of concurrent probe and hotplug
4518fca52a55SDaniel Vetter  * workers. Hence the split into this two-stage approach.
4519fca52a55SDaniel Vetter  */
45202aeb7d3aSDaniel Vetter int intel_irq_install(struct drm_i915_private *dev_priv)
45212aeb7d3aSDaniel Vetter {
45222aeb7d3aSDaniel Vetter 	/*
45232aeb7d3aSDaniel Vetter 	 * We enable some interrupt sources in our postinstall hooks, so mark
45242aeb7d3aSDaniel Vetter 	 * interrupts as enabled _before_ actually enabling them to avoid
45252aeb7d3aSDaniel Vetter 	 * special cases in our ordering checks.
45262aeb7d3aSDaniel Vetter 	 */
45272aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
45282aeb7d3aSDaniel Vetter 
45292aeb7d3aSDaniel Vetter 	return drm_irq_install(dev_priv->dev, dev_priv->dev->pdev->irq);
45302aeb7d3aSDaniel Vetter }
45312aeb7d3aSDaniel Vetter 
4532fca52a55SDaniel Vetter /**
4533fca52a55SDaniel Vetter  * intel_irq_uninstall - finilizes all irq handling
4534fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4535fca52a55SDaniel Vetter  *
4536fca52a55SDaniel Vetter  * This stops interrupt and hotplug handling and unregisters and frees all
4537fca52a55SDaniel Vetter  * resources acquired in the init functions.
4538fca52a55SDaniel Vetter  */
45392aeb7d3aSDaniel Vetter void intel_irq_uninstall(struct drm_i915_private *dev_priv)
45402aeb7d3aSDaniel Vetter {
45412aeb7d3aSDaniel Vetter 	drm_irq_uninstall(dev_priv->dev);
45422aeb7d3aSDaniel Vetter 	intel_hpd_cancel_work(dev_priv);
45432aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
45442aeb7d3aSDaniel Vetter }
45452aeb7d3aSDaniel Vetter 
4546fca52a55SDaniel Vetter /**
4547fca52a55SDaniel Vetter  * intel_runtime_pm_disable_interrupts - runtime interrupt disabling
4548fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4549fca52a55SDaniel Vetter  *
4550fca52a55SDaniel Vetter  * This function is used to disable interrupts at runtime, both in the runtime
4551fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4552fca52a55SDaniel Vetter  */
4553b963291cSDaniel Vetter void intel_runtime_pm_disable_interrupts(struct drm_i915_private *dev_priv)
4554c67a470bSPaulo Zanoni {
4555b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_uninstall(dev_priv->dev);
45562aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
45572dd2a883SImre Deak 	synchronize_irq(dev_priv->dev->irq);
4558c67a470bSPaulo Zanoni }
4559c67a470bSPaulo Zanoni 
4560fca52a55SDaniel Vetter /**
4561fca52a55SDaniel Vetter  * intel_runtime_pm_enable_interrupts - runtime interrupt enabling
4562fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4563fca52a55SDaniel Vetter  *
4564fca52a55SDaniel Vetter  * This function is used to enable interrupts at runtime, both in the runtime
4565fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4566fca52a55SDaniel Vetter  */
4567b963291cSDaniel Vetter void intel_runtime_pm_enable_interrupts(struct drm_i915_private *dev_priv)
4568c67a470bSPaulo Zanoni {
45692aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
4570b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_preinstall(dev_priv->dev);
4571b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_postinstall(dev_priv->dev);
4572c67a470bSPaulo Zanoni }
4573