xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision b291681926a142958112eedde62823230d6afb84)
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  */
142b51a2842SVille Syrjälä static void gen5_assert_iir_is_zero(struct drm_i915_private *dev_priv, u32 reg)
143b51a2842SVille Syrjälä {
144b51a2842SVille Syrjälä 	u32 val = I915_READ(reg);
145b51a2842SVille Syrjälä 
146b51a2842SVille Syrjälä 	if (val == 0)
147b51a2842SVille Syrjälä 		return;
148b51a2842SVille Syrjälä 
149b51a2842SVille Syrjälä 	WARN(1, "Interrupt register 0x%x is not zero: 0x%08x\n",
150b51a2842SVille Syrjälä 	     reg, val);
151b51a2842SVille Syrjälä 	I915_WRITE(reg, 0xffffffff);
152b51a2842SVille Syrjälä 	POSTING_READ(reg);
153b51a2842SVille Syrjälä 	I915_WRITE(reg, 0xffffffff);
154b51a2842SVille Syrjälä 	POSTING_READ(reg);
155b51a2842SVille Syrjälä }
156337ba017SPaulo Zanoni 
15735079899SPaulo Zanoni #define GEN8_IRQ_INIT_NDX(type, which, imr_val, ier_val) do { \
158b51a2842SVille Syrjälä 	gen5_assert_iir_is_zero(dev_priv, GEN8_##type##_IIR(which)); \
15935079899SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IER(which), (ier_val)); \
1607d1bd539SVille Syrjälä 	I915_WRITE(GEN8_##type##_IMR(which), (imr_val)); \
1617d1bd539SVille Syrjälä 	POSTING_READ(GEN8_##type##_IMR(which)); \
16235079899SPaulo Zanoni } while (0)
16335079899SPaulo Zanoni 
16435079899SPaulo Zanoni #define GEN5_IRQ_INIT(type, imr_val, ier_val) do { \
165b51a2842SVille Syrjälä 	gen5_assert_iir_is_zero(dev_priv, type##IIR); \
16635079899SPaulo Zanoni 	I915_WRITE(type##IER, (ier_val)); \
1677d1bd539SVille Syrjälä 	I915_WRITE(type##IMR, (imr_val)); \
1687d1bd539SVille Syrjälä 	POSTING_READ(type##IMR); \
16935079899SPaulo Zanoni } while (0)
17035079899SPaulo Zanoni 
171c9a9a268SImre Deak static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir);
172c9a9a268SImre Deak 
1730706f17cSEgbert Eich /* For display hotplug interrupt */
1740706f17cSEgbert Eich static inline void
1750706f17cSEgbert Eich i915_hotplug_interrupt_update_locked(struct drm_i915_private *dev_priv,
1760706f17cSEgbert Eich 				     uint32_t mask,
1770706f17cSEgbert Eich 				     uint32_t bits)
1780706f17cSEgbert Eich {
1790706f17cSEgbert Eich 	uint32_t val;
1800706f17cSEgbert Eich 
1810706f17cSEgbert Eich 	assert_spin_locked(&dev_priv->irq_lock);
1820706f17cSEgbert Eich 	WARN_ON(bits & ~mask);
1830706f17cSEgbert Eich 
1840706f17cSEgbert Eich 	val = I915_READ(PORT_HOTPLUG_EN);
1850706f17cSEgbert Eich 	val &= ~mask;
1860706f17cSEgbert Eich 	val |= bits;
1870706f17cSEgbert Eich 	I915_WRITE(PORT_HOTPLUG_EN, val);
1880706f17cSEgbert Eich }
1890706f17cSEgbert Eich 
1900706f17cSEgbert Eich /**
1910706f17cSEgbert Eich  * i915_hotplug_interrupt_update - update hotplug interrupt enable
1920706f17cSEgbert Eich  * @dev_priv: driver private
1930706f17cSEgbert Eich  * @mask: bits to update
1940706f17cSEgbert Eich  * @bits: bits to enable
1950706f17cSEgbert Eich  * NOTE: the HPD enable bits are modified both inside and outside
1960706f17cSEgbert Eich  * of an interrupt context. To avoid that read-modify-write cycles
1970706f17cSEgbert Eich  * interfer, these bits are protected by a spinlock. Since this
1980706f17cSEgbert Eich  * function is usually not called from a context where the lock is
1990706f17cSEgbert Eich  * held already, this function acquires the lock itself. A non-locking
2000706f17cSEgbert Eich  * version is also available.
2010706f17cSEgbert Eich  */
2020706f17cSEgbert Eich void i915_hotplug_interrupt_update(struct drm_i915_private *dev_priv,
2030706f17cSEgbert Eich 				   uint32_t mask,
2040706f17cSEgbert Eich 				   uint32_t bits)
2050706f17cSEgbert Eich {
2060706f17cSEgbert Eich 	spin_lock_irq(&dev_priv->irq_lock);
2070706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv, mask, bits);
2080706f17cSEgbert Eich 	spin_unlock_irq(&dev_priv->irq_lock);
2090706f17cSEgbert Eich }
2100706f17cSEgbert Eich 
211d9dc34f1SVille Syrjälä /**
212d9dc34f1SVille Syrjälä  * ilk_update_display_irq - update DEIMR
213d9dc34f1SVille Syrjälä  * @dev_priv: driver private
214d9dc34f1SVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
215d9dc34f1SVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
216d9dc34f1SVille Syrjälä  */
217d9dc34f1SVille Syrjälä static void ilk_update_display_irq(struct drm_i915_private *dev_priv,
218d9dc34f1SVille Syrjälä 				   uint32_t interrupt_mask,
219d9dc34f1SVille Syrjälä 				   uint32_t enabled_irq_mask)
220036a4a7dSZhenyu Wang {
221d9dc34f1SVille Syrjälä 	uint32_t new_val;
222d9dc34f1SVille Syrjälä 
2234bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
2244bc9d430SDaniel Vetter 
225d9dc34f1SVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
226d9dc34f1SVille Syrjälä 
2279df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
228c67a470bSPaulo Zanoni 		return;
229c67a470bSPaulo Zanoni 
230d9dc34f1SVille Syrjälä 	new_val = dev_priv->irq_mask;
231d9dc34f1SVille Syrjälä 	new_val &= ~interrupt_mask;
232d9dc34f1SVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
233d9dc34f1SVille Syrjälä 
234d9dc34f1SVille Syrjälä 	if (new_val != dev_priv->irq_mask) {
235d9dc34f1SVille Syrjälä 		dev_priv->irq_mask = new_val;
2361ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
2373143a2bfSChris Wilson 		POSTING_READ(DEIMR);
238036a4a7dSZhenyu Wang 	}
239036a4a7dSZhenyu Wang }
240036a4a7dSZhenyu Wang 
24147339cd9SDaniel Vetter void
242d9dc34f1SVille Syrjälä ironlake_enable_display_irq(struct drm_i915_private *dev_priv, u32 mask)
243d9dc34f1SVille Syrjälä {
244d9dc34f1SVille Syrjälä 	ilk_update_display_irq(dev_priv, mask, mask);
245d9dc34f1SVille Syrjälä }
246d9dc34f1SVille Syrjälä 
247d9dc34f1SVille Syrjälä void
2482d1013ddSJani Nikula ironlake_disable_display_irq(struct drm_i915_private *dev_priv, u32 mask)
249036a4a7dSZhenyu Wang {
250d9dc34f1SVille Syrjälä 	ilk_update_display_irq(dev_priv, mask, 0);
251036a4a7dSZhenyu Wang }
252036a4a7dSZhenyu Wang 
25343eaea13SPaulo Zanoni /**
25443eaea13SPaulo Zanoni  * ilk_update_gt_irq - update GTIMR
25543eaea13SPaulo Zanoni  * @dev_priv: driver private
25643eaea13SPaulo Zanoni  * @interrupt_mask: mask of interrupt bits to update
25743eaea13SPaulo Zanoni  * @enabled_irq_mask: mask of interrupt bits to enable
25843eaea13SPaulo Zanoni  */
25943eaea13SPaulo Zanoni static void ilk_update_gt_irq(struct drm_i915_private *dev_priv,
26043eaea13SPaulo Zanoni 			      uint32_t interrupt_mask,
26143eaea13SPaulo Zanoni 			      uint32_t enabled_irq_mask)
26243eaea13SPaulo Zanoni {
26343eaea13SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
26443eaea13SPaulo Zanoni 
26515a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
26615a17aaeSDaniel Vetter 
2679df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
268c67a470bSPaulo Zanoni 		return;
269c67a470bSPaulo Zanoni 
27043eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask &= ~interrupt_mask;
27143eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask |= (~enabled_irq_mask & interrupt_mask);
27243eaea13SPaulo Zanoni 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
27343eaea13SPaulo Zanoni 	POSTING_READ(GTIMR);
27443eaea13SPaulo Zanoni }
27543eaea13SPaulo Zanoni 
276480c8033SDaniel Vetter void gen5_enable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
27743eaea13SPaulo Zanoni {
27843eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, mask);
27943eaea13SPaulo Zanoni }
28043eaea13SPaulo Zanoni 
281480c8033SDaniel Vetter void gen5_disable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
28243eaea13SPaulo Zanoni {
28343eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, 0);
28443eaea13SPaulo Zanoni }
28543eaea13SPaulo Zanoni 
286b900b949SImre Deak static u32 gen6_pm_iir(struct drm_i915_private *dev_priv)
287b900b949SImre Deak {
288b900b949SImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IIR(2) : GEN6_PMIIR;
289b900b949SImre Deak }
290b900b949SImre Deak 
291a72fbc3aSImre Deak static u32 gen6_pm_imr(struct drm_i915_private *dev_priv)
292a72fbc3aSImre Deak {
293a72fbc3aSImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IMR(2) : GEN6_PMIMR;
294a72fbc3aSImre Deak }
295a72fbc3aSImre Deak 
296b900b949SImre Deak static u32 gen6_pm_ier(struct drm_i915_private *dev_priv)
297b900b949SImre Deak {
298b900b949SImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IER(2) : GEN6_PMIER;
299b900b949SImre Deak }
300b900b949SImre Deak 
301edbfdb45SPaulo Zanoni /**
302edbfdb45SPaulo Zanoni   * snb_update_pm_irq - update GEN6_PMIMR
303edbfdb45SPaulo Zanoni   * @dev_priv: driver private
304edbfdb45SPaulo Zanoni   * @interrupt_mask: mask of interrupt bits to update
305edbfdb45SPaulo Zanoni   * @enabled_irq_mask: mask of interrupt bits to enable
306edbfdb45SPaulo Zanoni   */
307edbfdb45SPaulo Zanoni static void snb_update_pm_irq(struct drm_i915_private *dev_priv,
308edbfdb45SPaulo Zanoni 			      uint32_t interrupt_mask,
309edbfdb45SPaulo Zanoni 			      uint32_t enabled_irq_mask)
310edbfdb45SPaulo Zanoni {
311605cd25bSPaulo Zanoni 	uint32_t new_val;
312edbfdb45SPaulo Zanoni 
31315a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
31415a17aaeSDaniel Vetter 
315edbfdb45SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
316edbfdb45SPaulo Zanoni 
317605cd25bSPaulo Zanoni 	new_val = dev_priv->pm_irq_mask;
318f52ecbcfSPaulo Zanoni 	new_val &= ~interrupt_mask;
319f52ecbcfSPaulo Zanoni 	new_val |= (~enabled_irq_mask & interrupt_mask);
320f52ecbcfSPaulo Zanoni 
321605cd25bSPaulo Zanoni 	if (new_val != dev_priv->pm_irq_mask) {
322605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = new_val;
323a72fbc3aSImre Deak 		I915_WRITE(gen6_pm_imr(dev_priv), dev_priv->pm_irq_mask);
324a72fbc3aSImre Deak 		POSTING_READ(gen6_pm_imr(dev_priv));
325edbfdb45SPaulo Zanoni 	}
326f52ecbcfSPaulo Zanoni }
327edbfdb45SPaulo Zanoni 
328480c8033SDaniel Vetter void gen6_enable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
329edbfdb45SPaulo Zanoni {
3309939fba2SImre Deak 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
3319939fba2SImre Deak 		return;
3329939fba2SImre Deak 
333edbfdb45SPaulo Zanoni 	snb_update_pm_irq(dev_priv, mask, mask);
334edbfdb45SPaulo Zanoni }
335edbfdb45SPaulo Zanoni 
3369939fba2SImre Deak static void __gen6_disable_pm_irq(struct drm_i915_private *dev_priv,
3379939fba2SImre Deak 				  uint32_t mask)
3389939fba2SImre Deak {
3399939fba2SImre Deak 	snb_update_pm_irq(dev_priv, mask, 0);
3409939fba2SImre Deak }
3419939fba2SImre Deak 
342480c8033SDaniel Vetter void gen6_disable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
343edbfdb45SPaulo Zanoni {
3449939fba2SImre Deak 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
3459939fba2SImre Deak 		return;
3469939fba2SImre Deak 
3479939fba2SImre Deak 	__gen6_disable_pm_irq(dev_priv, mask);
348edbfdb45SPaulo Zanoni }
349edbfdb45SPaulo Zanoni 
3503cc134e3SImre Deak void gen6_reset_rps_interrupts(struct drm_device *dev)
3513cc134e3SImre Deak {
3523cc134e3SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
3533cc134e3SImre Deak 	uint32_t reg = gen6_pm_iir(dev_priv);
3543cc134e3SImre Deak 
3553cc134e3SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
3563cc134e3SImre Deak 	I915_WRITE(reg, dev_priv->pm_rps_events);
3573cc134e3SImre Deak 	I915_WRITE(reg, dev_priv->pm_rps_events);
3583cc134e3SImre Deak 	POSTING_READ(reg);
359096fad9eSImre Deak 	dev_priv->rps.pm_iir = 0;
3603cc134e3SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
3613cc134e3SImre Deak }
3623cc134e3SImre Deak 
363b900b949SImre Deak void gen6_enable_rps_interrupts(struct drm_device *dev)
364b900b949SImre Deak {
365b900b949SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
366b900b949SImre Deak 
367b900b949SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
36878e68d36SImre Deak 
369b900b949SImre Deak 	WARN_ON(dev_priv->rps.pm_iir);
3703cc134e3SImre Deak 	WARN_ON(I915_READ(gen6_pm_iir(dev_priv)) & dev_priv->pm_rps_events);
371d4d70aa5SImre Deak 	dev_priv->rps.interrupts_enabled = true;
37278e68d36SImre Deak 	I915_WRITE(gen6_pm_ier(dev_priv), I915_READ(gen6_pm_ier(dev_priv)) |
37378e68d36SImre Deak 				dev_priv->pm_rps_events);
374b900b949SImre Deak 	gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
37578e68d36SImre Deak 
376b900b949SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
377b900b949SImre Deak }
378b900b949SImre Deak 
37959d02a1fSImre Deak u32 gen6_sanitize_rps_pm_mask(struct drm_i915_private *dev_priv, u32 mask)
38059d02a1fSImre Deak {
38159d02a1fSImre Deak 	/*
382f24eeb19SImre Deak 	 * SNB,IVB can while VLV,CHV may hard hang on looping batchbuffer
38359d02a1fSImre Deak 	 * if GEN6_PM_UP_EI_EXPIRED is masked.
384f24eeb19SImre Deak 	 *
385f24eeb19SImre Deak 	 * TODO: verify if this can be reproduced on VLV,CHV.
38659d02a1fSImre Deak 	 */
38759d02a1fSImre Deak 	if (INTEL_INFO(dev_priv)->gen <= 7 && !IS_HASWELL(dev_priv))
38859d02a1fSImre Deak 		mask &= ~GEN6_PM_RP_UP_EI_EXPIRED;
38959d02a1fSImre Deak 
39059d02a1fSImre Deak 	if (INTEL_INFO(dev_priv)->gen >= 8)
39159d02a1fSImre Deak 		mask &= ~GEN8_PMINTR_REDIRECT_TO_NON_DISP;
39259d02a1fSImre Deak 
39359d02a1fSImre Deak 	return mask;
39459d02a1fSImre Deak }
39559d02a1fSImre Deak 
396b900b949SImre Deak void gen6_disable_rps_interrupts(struct drm_device *dev)
397b900b949SImre Deak {
398b900b949SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
399b900b949SImre Deak 
400d4d70aa5SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
401d4d70aa5SImre Deak 	dev_priv->rps.interrupts_enabled = false;
402d4d70aa5SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
403d4d70aa5SImre Deak 
404d4d70aa5SImre Deak 	cancel_work_sync(&dev_priv->rps.work);
405d4d70aa5SImre Deak 
4069939fba2SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
4079939fba2SImre Deak 
40859d02a1fSImre Deak 	I915_WRITE(GEN6_PMINTRMSK, gen6_sanitize_rps_pm_mask(dev_priv, ~0));
4099939fba2SImre Deak 
4109939fba2SImre Deak 	__gen6_disable_pm_irq(dev_priv, dev_priv->pm_rps_events);
411b900b949SImre Deak 	I915_WRITE(gen6_pm_ier(dev_priv), I915_READ(gen6_pm_ier(dev_priv)) &
412b900b949SImre Deak 				~dev_priv->pm_rps_events);
41358072ccbSImre Deak 
41458072ccbSImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
41558072ccbSImre Deak 
41658072ccbSImre Deak 	synchronize_irq(dev->irq);
417b900b949SImre Deak }
418b900b949SImre Deak 
4190961021aSBen Widawsky /**
4203a3b3c7dSVille Syrjälä   * bdw_update_port_irq - update DE port interrupt
4213a3b3c7dSVille Syrjälä   * @dev_priv: driver private
4223a3b3c7dSVille Syrjälä   * @interrupt_mask: mask of interrupt bits to update
4233a3b3c7dSVille Syrjälä   * @enabled_irq_mask: mask of interrupt bits to enable
4243a3b3c7dSVille Syrjälä   */
4253a3b3c7dSVille Syrjälä static void bdw_update_port_irq(struct drm_i915_private *dev_priv,
4263a3b3c7dSVille Syrjälä 				uint32_t interrupt_mask,
4273a3b3c7dSVille Syrjälä 				uint32_t enabled_irq_mask)
4283a3b3c7dSVille Syrjälä {
4293a3b3c7dSVille Syrjälä 	uint32_t new_val;
4303a3b3c7dSVille Syrjälä 	uint32_t old_val;
4313a3b3c7dSVille Syrjälä 
4323a3b3c7dSVille Syrjälä 	assert_spin_locked(&dev_priv->irq_lock);
4333a3b3c7dSVille Syrjälä 
4343a3b3c7dSVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
4353a3b3c7dSVille Syrjälä 
4363a3b3c7dSVille Syrjälä 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
4373a3b3c7dSVille Syrjälä 		return;
4383a3b3c7dSVille Syrjälä 
4393a3b3c7dSVille Syrjälä 	old_val = I915_READ(GEN8_DE_PORT_IMR);
4403a3b3c7dSVille Syrjälä 
4413a3b3c7dSVille Syrjälä 	new_val = old_val;
4423a3b3c7dSVille Syrjälä 	new_val &= ~interrupt_mask;
4433a3b3c7dSVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
4443a3b3c7dSVille Syrjälä 
4453a3b3c7dSVille Syrjälä 	if (new_val != old_val) {
4463a3b3c7dSVille Syrjälä 		I915_WRITE(GEN8_DE_PORT_IMR, new_val);
4473a3b3c7dSVille Syrjälä 		POSTING_READ(GEN8_DE_PORT_IMR);
4483a3b3c7dSVille Syrjälä 	}
4493a3b3c7dSVille Syrjälä }
4503a3b3c7dSVille Syrjälä 
4513a3b3c7dSVille Syrjälä /**
452fee884edSDaniel Vetter  * ibx_display_interrupt_update - update SDEIMR
453fee884edSDaniel Vetter  * @dev_priv: driver private
454fee884edSDaniel Vetter  * @interrupt_mask: mask of interrupt bits to update
455fee884edSDaniel Vetter  * @enabled_irq_mask: mask of interrupt bits to enable
456fee884edSDaniel Vetter  */
45747339cd9SDaniel Vetter void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
458fee884edSDaniel Vetter 				  uint32_t interrupt_mask,
459fee884edSDaniel Vetter 				  uint32_t enabled_irq_mask)
460fee884edSDaniel Vetter {
461fee884edSDaniel Vetter 	uint32_t sdeimr = I915_READ(SDEIMR);
462fee884edSDaniel Vetter 	sdeimr &= ~interrupt_mask;
463fee884edSDaniel Vetter 	sdeimr |= (~enabled_irq_mask & interrupt_mask);
464fee884edSDaniel Vetter 
46515a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
46615a17aaeSDaniel Vetter 
467fee884edSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
468fee884edSDaniel Vetter 
4699df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
470c67a470bSPaulo Zanoni 		return;
471c67a470bSPaulo Zanoni 
472fee884edSDaniel Vetter 	I915_WRITE(SDEIMR, sdeimr);
473fee884edSDaniel Vetter 	POSTING_READ(SDEIMR);
474fee884edSDaniel Vetter }
4758664281bSPaulo Zanoni 
476b5ea642aSDaniel Vetter static void
477755e9019SImre Deak __i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
478755e9019SImre Deak 		       u32 enable_mask, u32 status_mask)
4797c463586SKeith Packard {
4809db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
481755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
4827c463586SKeith Packard 
483b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
484d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
485b79480baSDaniel Vetter 
48604feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
48704feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
48804feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
48904feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
490755e9019SImre Deak 		return;
491755e9019SImre Deak 
492755e9019SImre Deak 	if ((pipestat & enable_mask) == enable_mask)
49346c06a30SVille Syrjälä 		return;
49446c06a30SVille Syrjälä 
49591d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] |= status_mask;
49691d181ddSImre Deak 
4977c463586SKeith Packard 	/* Enable the interrupt, clear any pending status */
498755e9019SImre Deak 	pipestat |= enable_mask | status_mask;
49946c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
5003143a2bfSChris Wilson 	POSTING_READ(reg);
5017c463586SKeith Packard }
5027c463586SKeith Packard 
503b5ea642aSDaniel Vetter static void
504755e9019SImre Deak __i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
505755e9019SImre Deak 		        u32 enable_mask, u32 status_mask)
5067c463586SKeith Packard {
5079db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
508755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
5097c463586SKeith Packard 
510b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
511d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
512b79480baSDaniel Vetter 
51304feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
51404feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
51504feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
51604feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
51746c06a30SVille Syrjälä 		return;
51846c06a30SVille Syrjälä 
519755e9019SImre Deak 	if ((pipestat & enable_mask) == 0)
520755e9019SImre Deak 		return;
521755e9019SImre Deak 
52291d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] &= ~status_mask;
52391d181ddSImre Deak 
524755e9019SImre Deak 	pipestat &= ~enable_mask;
52546c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
5263143a2bfSChris Wilson 	POSTING_READ(reg);
5277c463586SKeith Packard }
5287c463586SKeith Packard 
52910c59c51SImre Deak static u32 vlv_get_pipestat_enable_mask(struct drm_device *dev, u32 status_mask)
53010c59c51SImre Deak {
53110c59c51SImre Deak 	u32 enable_mask = status_mask << 16;
53210c59c51SImre Deak 
53310c59c51SImre Deak 	/*
534724a6905SVille Syrjälä 	 * On pipe A we don't support the PSR interrupt yet,
535724a6905SVille Syrjälä 	 * on pipe B and C the same bit MBZ.
53610c59c51SImre Deak 	 */
53710c59c51SImre Deak 	if (WARN_ON_ONCE(status_mask & PIPE_A_PSR_STATUS_VLV))
53810c59c51SImre Deak 		return 0;
539724a6905SVille Syrjälä 	/*
540724a6905SVille Syrjälä 	 * On pipe B and C we don't support the PSR interrupt yet, on pipe
541724a6905SVille Syrjälä 	 * A the same bit is for perf counters which we don't use either.
542724a6905SVille Syrjälä 	 */
543724a6905SVille Syrjälä 	if (WARN_ON_ONCE(status_mask & PIPE_B_PSR_STATUS_VLV))
544724a6905SVille Syrjälä 		return 0;
54510c59c51SImre Deak 
54610c59c51SImre Deak 	enable_mask &= ~(PIPE_FIFO_UNDERRUN_STATUS |
54710c59c51SImre Deak 			 SPRITE0_FLIP_DONE_INT_EN_VLV |
54810c59c51SImre Deak 			 SPRITE1_FLIP_DONE_INT_EN_VLV);
54910c59c51SImre Deak 	if (status_mask & SPRITE0_FLIP_DONE_INT_STATUS_VLV)
55010c59c51SImre Deak 		enable_mask |= SPRITE0_FLIP_DONE_INT_EN_VLV;
55110c59c51SImre Deak 	if (status_mask & SPRITE1_FLIP_DONE_INT_STATUS_VLV)
55210c59c51SImre Deak 		enable_mask |= SPRITE1_FLIP_DONE_INT_EN_VLV;
55310c59c51SImre Deak 
55410c59c51SImre Deak 	return enable_mask;
55510c59c51SImre Deak }
55610c59c51SImre Deak 
557755e9019SImre Deak void
558755e9019SImre Deak i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
559755e9019SImre Deak 		     u32 status_mask)
560755e9019SImre Deak {
561755e9019SImre Deak 	u32 enable_mask;
562755e9019SImre Deak 
56310c59c51SImre Deak 	if (IS_VALLEYVIEW(dev_priv->dev))
56410c59c51SImre Deak 		enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
56510c59c51SImre Deak 							   status_mask);
56610c59c51SImre Deak 	else
567755e9019SImre Deak 		enable_mask = status_mask << 16;
568755e9019SImre Deak 	__i915_enable_pipestat(dev_priv, pipe, enable_mask, status_mask);
569755e9019SImre Deak }
570755e9019SImre Deak 
571755e9019SImre Deak void
572755e9019SImre Deak i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
573755e9019SImre Deak 		      u32 status_mask)
574755e9019SImre Deak {
575755e9019SImre Deak 	u32 enable_mask;
576755e9019SImre Deak 
57710c59c51SImre Deak 	if (IS_VALLEYVIEW(dev_priv->dev))
57810c59c51SImre Deak 		enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
57910c59c51SImre Deak 							   status_mask);
58010c59c51SImre Deak 	else
581755e9019SImre Deak 		enable_mask = status_mask << 16;
582755e9019SImre Deak 	__i915_disable_pipestat(dev_priv, pipe, enable_mask, status_mask);
583755e9019SImre Deak }
584755e9019SImre Deak 
585c0e09200SDave Airlie /**
586f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
587468f9d29SJavier Martinez Canillas  * @dev: drm device
58801c66889SZhao Yakui  */
589f49e38ddSJani Nikula static void i915_enable_asle_pipestat(struct drm_device *dev)
59001c66889SZhao Yakui {
5912d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
5921ec14ad3SChris Wilson 
593f49e38ddSJani Nikula 	if (!dev_priv->opregion.asle || !IS_MOBILE(dev))
594f49e38ddSJani Nikula 		return;
595f49e38ddSJani Nikula 
59613321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
59701c66889SZhao Yakui 
598755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_STATUS);
599a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4)
6003b6c42e8SDaniel Vetter 		i915_enable_pipestat(dev_priv, PIPE_A,
601755e9019SImre Deak 				     PIPE_LEGACY_BLC_EVENT_STATUS);
6021ec14ad3SChris Wilson 
60313321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
60401c66889SZhao Yakui }
60501c66889SZhao Yakui 
606f75f3746SVille Syrjälä /*
607f75f3746SVille Syrjälä  * This timing diagram depicts the video signal in and
608f75f3746SVille Syrjälä  * around the vertical blanking period.
609f75f3746SVille Syrjälä  *
610f75f3746SVille Syrjälä  * Assumptions about the fictitious mode used in this example:
611f75f3746SVille Syrjälä  *  vblank_start >= 3
612f75f3746SVille Syrjälä  *  vsync_start = vblank_start + 1
613f75f3746SVille Syrjälä  *  vsync_end = vblank_start + 2
614f75f3746SVille Syrjälä  *  vtotal = vblank_start + 3
615f75f3746SVille Syrjälä  *
616f75f3746SVille Syrjälä  *           start of vblank:
617f75f3746SVille Syrjälä  *           latch double buffered registers
618f75f3746SVille Syrjälä  *           increment frame counter (ctg+)
619f75f3746SVille Syrjälä  *           generate start of vblank interrupt (gen4+)
620f75f3746SVille Syrjälä  *           |
621f75f3746SVille Syrjälä  *           |          frame start:
622f75f3746SVille Syrjälä  *           |          generate frame start interrupt (aka. vblank interrupt) (gmch)
623f75f3746SVille Syrjälä  *           |          may be shifted forward 1-3 extra lines via PIPECONF
624f75f3746SVille Syrjälä  *           |          |
625f75f3746SVille Syrjälä  *           |          |  start of vsync:
626f75f3746SVille Syrjälä  *           |          |  generate vsync interrupt
627f75f3746SVille Syrjälä  *           |          |  |
628f75f3746SVille Syrjälä  * ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx
629f75f3746SVille Syrjälä  *       .   \hs/   .      \hs/          \hs/          \hs/   .      \hs/
630f75f3746SVille Syrjälä  * ----va---> <-----------------vb--------------------> <--------va-------------
631f75f3746SVille Syrjälä  *       |          |       <----vs----->                     |
632f75f3746SVille Syrjälä  * -vbs-----> <---vbs+1---> <---vbs+2---> <-----0-----> <-----1-----> <-----2--- (scanline counter gen2)
633f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2---> <-----0--- (scanline counter gen3+)
634f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2- (scanline counter hsw+ hdmi)
635f75f3746SVille Syrjälä  *       |          |                                         |
636f75f3746SVille Syrjälä  *       last visible pixel                                   first visible pixel
637f75f3746SVille Syrjälä  *                  |                                         increment frame counter (gen3/4)
638f75f3746SVille Syrjälä  *                  pixel counter = vblank_start * htotal     pixel counter = 0 (gen3/4)
639f75f3746SVille Syrjälä  *
640f75f3746SVille Syrjälä  * x  = horizontal active
641f75f3746SVille Syrjälä  * _  = horizontal blanking
642f75f3746SVille Syrjälä  * hs = horizontal sync
643f75f3746SVille Syrjälä  * va = vertical active
644f75f3746SVille Syrjälä  * vb = vertical blanking
645f75f3746SVille Syrjälä  * vs = vertical sync
646f75f3746SVille Syrjälä  * vbs = vblank_start (number)
647f75f3746SVille Syrjälä  *
648f75f3746SVille Syrjälä  * Summary:
649f75f3746SVille Syrjälä  * - most events happen at the start of horizontal sync
650f75f3746SVille Syrjälä  * - frame start happens at the start of horizontal blank, 1-4 lines
651f75f3746SVille Syrjälä  *   (depending on PIPECONF settings) after the start of vblank
652f75f3746SVille Syrjälä  * - gen3/4 pixel and frame counter are synchronized with the start
653f75f3746SVille Syrjälä  *   of horizontal active on the first line of vertical active
654f75f3746SVille Syrjälä  */
655f75f3746SVille Syrjälä 
6564cdb83ecSVille Syrjälä static u32 i8xx_get_vblank_counter(struct drm_device *dev, int pipe)
6574cdb83ecSVille Syrjälä {
6584cdb83ecSVille Syrjälä 	/* Gen2 doesn't have a hardware frame counter */
6594cdb83ecSVille Syrjälä 	return 0;
6604cdb83ecSVille Syrjälä }
6614cdb83ecSVille Syrjälä 
66242f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
66342f52ef8SKeith Packard  * we use as a pipe index
66442f52ef8SKeith Packard  */
665f71d4af4SJesse Barnes static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
6660a3e67a4SJesse Barnes {
6672d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
6680a3e67a4SJesse Barnes 	unsigned long high_frame;
6690a3e67a4SJesse Barnes 	unsigned long low_frame;
6700b2a8e09SVille Syrjälä 	u32 high1, high2, low, pixel, vbl_start, hsync_start, htotal;
671391f75e2SVille Syrjälä 	struct intel_crtc *intel_crtc =
672391f75e2SVille Syrjälä 		to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
673fc467a22SMaarten Lankhorst 	const struct drm_display_mode *mode = &intel_crtc->base.hwmode;
674391f75e2SVille Syrjälä 
6750b2a8e09SVille Syrjälä 	htotal = mode->crtc_htotal;
6760b2a8e09SVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
6770b2a8e09SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
6780b2a8e09SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
6790b2a8e09SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
680391f75e2SVille Syrjälä 
6810b2a8e09SVille Syrjälä 	/* Convert to pixel count */
6820b2a8e09SVille Syrjälä 	vbl_start *= htotal;
6830b2a8e09SVille Syrjälä 
6840b2a8e09SVille Syrjälä 	/* Start of vblank event occurs at start of hsync */
6850b2a8e09SVille Syrjälä 	vbl_start -= htotal - hsync_start;
6860b2a8e09SVille Syrjälä 
6879db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
6889db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
6895eddb70bSChris Wilson 
6900a3e67a4SJesse Barnes 	/*
6910a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
6920a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
6930a3e67a4SJesse Barnes 	 * register.
6940a3e67a4SJesse Barnes 	 */
6950a3e67a4SJesse Barnes 	do {
6965eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
697391f75e2SVille Syrjälä 		low   = I915_READ(low_frame);
6985eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
6990a3e67a4SJesse Barnes 	} while (high1 != high2);
7000a3e67a4SJesse Barnes 
7015eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
702391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
7035eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
704391f75e2SVille Syrjälä 
705391f75e2SVille Syrjälä 	/*
706391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
707391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
708391f75e2SVille Syrjälä 	 * counter against vblank start.
709391f75e2SVille Syrjälä 	 */
710edc08d0aSVille Syrjälä 	return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
7110a3e67a4SJesse Barnes }
7120a3e67a4SJesse Barnes 
713fd8f507cSVille Syrjälä static u32 g4x_get_vblank_counter(struct drm_device *dev, int pipe)
7149880b7a5SJesse Barnes {
7152d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
7169880b7a5SJesse Barnes 
717649636efSVille Syrjälä 	return I915_READ(PIPE_FRMCOUNT_G4X(pipe));
7189880b7a5SJesse Barnes }
7199880b7a5SJesse Barnes 
720ad3543edSMario Kleiner /* raw reads, only for fast reads of display block, no need for forcewake etc. */
721ad3543edSMario Kleiner #define __raw_i915_read32(dev_priv__, reg__) readl((dev_priv__)->regs + (reg__))
722ad3543edSMario Kleiner 
723a225f079SVille Syrjälä static int __intel_get_crtc_scanline(struct intel_crtc *crtc)
724a225f079SVille Syrjälä {
725a225f079SVille Syrjälä 	struct drm_device *dev = crtc->base.dev;
726a225f079SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
727fc467a22SMaarten Lankhorst 	const struct drm_display_mode *mode = &crtc->base.hwmode;
728a225f079SVille Syrjälä 	enum pipe pipe = crtc->pipe;
72980715b2fSVille Syrjälä 	int position, vtotal;
730a225f079SVille Syrjälä 
73180715b2fSVille Syrjälä 	vtotal = mode->crtc_vtotal;
732a225f079SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
733a225f079SVille Syrjälä 		vtotal /= 2;
734a225f079SVille Syrjälä 
735a225f079SVille Syrjälä 	if (IS_GEN2(dev))
736a225f079SVille Syrjälä 		position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
737a225f079SVille Syrjälä 	else
738a225f079SVille Syrjälä 		position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
739a225f079SVille Syrjälä 
740a225f079SVille Syrjälä 	/*
74141b578fbSJesse Barnes 	 * On HSW, the DSL reg (0x70000) appears to return 0 if we
74241b578fbSJesse Barnes 	 * read it just before the start of vblank.  So try it again
74341b578fbSJesse Barnes 	 * so we don't accidentally end up spanning a vblank frame
74441b578fbSJesse Barnes 	 * increment, causing the pipe_update_end() code to squak at us.
74541b578fbSJesse Barnes 	 *
74641b578fbSJesse Barnes 	 * The nature of this problem means we can't simply check the ISR
74741b578fbSJesse Barnes 	 * bit and return the vblank start value; nor can we use the scanline
74841b578fbSJesse Barnes 	 * debug register in the transcoder as it appears to have the same
74941b578fbSJesse Barnes 	 * problem.  We may need to extend this to include other platforms,
75041b578fbSJesse Barnes 	 * but so far testing only shows the problem on HSW.
75141b578fbSJesse Barnes 	 */
752*b2916819SMaarten Lankhorst 	if (HAS_DDI(dev) && !position) {
75341b578fbSJesse Barnes 		int i, temp;
75441b578fbSJesse Barnes 
75541b578fbSJesse Barnes 		for (i = 0; i < 100; i++) {
75641b578fbSJesse Barnes 			udelay(1);
75741b578fbSJesse Barnes 			temp = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) &
75841b578fbSJesse Barnes 				DSL_LINEMASK_GEN3;
75941b578fbSJesse Barnes 			if (temp != position) {
76041b578fbSJesse Barnes 				position = temp;
76141b578fbSJesse Barnes 				break;
76241b578fbSJesse Barnes 			}
76341b578fbSJesse Barnes 		}
76441b578fbSJesse Barnes 	}
76541b578fbSJesse Barnes 
76641b578fbSJesse Barnes 	/*
76780715b2fSVille Syrjälä 	 * See update_scanline_offset() for the details on the
76880715b2fSVille Syrjälä 	 * scanline_offset adjustment.
769a225f079SVille Syrjälä 	 */
77080715b2fSVille Syrjälä 	return (position + crtc->scanline_offset) % vtotal;
771a225f079SVille Syrjälä }
772a225f079SVille Syrjälä 
773f71d4af4SJesse Barnes static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
774abca9e45SVille Syrjälä 				    unsigned int flags, int *vpos, int *hpos,
7753bb403bfSVille Syrjälä 				    ktime_t *stime, ktime_t *etime,
7763bb403bfSVille Syrjälä 				    const struct drm_display_mode *mode)
7770af7e4dfSMario Kleiner {
778c2baf4b7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
779c2baf4b7SVille Syrjälä 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
780c2baf4b7SVille Syrjälä 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7813aa18df8SVille Syrjälä 	int position;
78278e8fc6bSVille Syrjälä 	int vbl_start, vbl_end, hsync_start, htotal, vtotal;
7830af7e4dfSMario Kleiner 	bool in_vbl = true;
7840af7e4dfSMario Kleiner 	int ret = 0;
785ad3543edSMario Kleiner 	unsigned long irqflags;
7860af7e4dfSMario Kleiner 
787fc467a22SMaarten Lankhorst 	if (WARN_ON(!mode->crtc_clock)) {
7880af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
7899db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
7900af7e4dfSMario Kleiner 		return 0;
7910af7e4dfSMario Kleiner 	}
7920af7e4dfSMario Kleiner 
793c2baf4b7SVille Syrjälä 	htotal = mode->crtc_htotal;
79478e8fc6bSVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
795c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
796c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
797c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
7980af7e4dfSMario Kleiner 
799d31faf65SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
800d31faf65SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
801d31faf65SVille Syrjälä 		vbl_end /= 2;
802d31faf65SVille Syrjälä 		vtotal /= 2;
803d31faf65SVille Syrjälä 	}
804d31faf65SVille Syrjälä 
805c2baf4b7SVille Syrjälä 	ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
806c2baf4b7SVille Syrjälä 
807ad3543edSMario Kleiner 	/*
808ad3543edSMario Kleiner 	 * Lock uncore.lock, as we will do multiple timing critical raw
809ad3543edSMario Kleiner 	 * register reads, potentially with preemption disabled, so the
810ad3543edSMario Kleiner 	 * following code must not block on uncore.lock.
811ad3543edSMario Kleiner 	 */
812ad3543edSMario Kleiner 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
813ad3543edSMario Kleiner 
814ad3543edSMario Kleiner 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
815ad3543edSMario Kleiner 
816ad3543edSMario Kleiner 	/* Get optional system timestamp before query. */
817ad3543edSMario Kleiner 	if (stime)
818ad3543edSMario Kleiner 		*stime = ktime_get();
819ad3543edSMario Kleiner 
8207c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
8210af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
8220af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
8230af7e4dfSMario Kleiner 		 */
824a225f079SVille Syrjälä 		position = __intel_get_crtc_scanline(intel_crtc);
8250af7e4dfSMario Kleiner 	} else {
8260af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
8270af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
8280af7e4dfSMario Kleiner 		 * scanout position.
8290af7e4dfSMario Kleiner 		 */
830ad3543edSMario Kleiner 		position = (__raw_i915_read32(dev_priv, PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
8310af7e4dfSMario Kleiner 
8323aa18df8SVille Syrjälä 		/* convert to pixel counts */
8333aa18df8SVille Syrjälä 		vbl_start *= htotal;
8343aa18df8SVille Syrjälä 		vbl_end *= htotal;
8353aa18df8SVille Syrjälä 		vtotal *= htotal;
83678e8fc6bSVille Syrjälä 
83778e8fc6bSVille Syrjälä 		/*
8387e78f1cbSVille Syrjälä 		 * In interlaced modes, the pixel counter counts all pixels,
8397e78f1cbSVille Syrjälä 		 * so one field will have htotal more pixels. In order to avoid
8407e78f1cbSVille Syrjälä 		 * the reported position from jumping backwards when the pixel
8417e78f1cbSVille Syrjälä 		 * counter is beyond the length of the shorter field, just
8427e78f1cbSVille Syrjälä 		 * clamp the position the length of the shorter field. This
8437e78f1cbSVille Syrjälä 		 * matches how the scanline counter based position works since
8447e78f1cbSVille Syrjälä 		 * the scanline counter doesn't count the two half lines.
8457e78f1cbSVille Syrjälä 		 */
8467e78f1cbSVille Syrjälä 		if (position >= vtotal)
8477e78f1cbSVille Syrjälä 			position = vtotal - 1;
8487e78f1cbSVille Syrjälä 
8497e78f1cbSVille Syrjälä 		/*
85078e8fc6bSVille Syrjälä 		 * Start of vblank interrupt is triggered at start of hsync,
85178e8fc6bSVille Syrjälä 		 * just prior to the first active line of vblank. However we
85278e8fc6bSVille Syrjälä 		 * consider lines to start at the leading edge of horizontal
85378e8fc6bSVille Syrjälä 		 * active. So, should we get here before we've crossed into
85478e8fc6bSVille Syrjälä 		 * the horizontal active of the first line in vblank, we would
85578e8fc6bSVille Syrjälä 		 * not set the DRM_SCANOUTPOS_INVBL flag. In order to fix that,
85678e8fc6bSVille Syrjälä 		 * always add htotal-hsync_start to the current pixel position.
85778e8fc6bSVille Syrjälä 		 */
85878e8fc6bSVille Syrjälä 		position = (position + htotal - hsync_start) % vtotal;
8593aa18df8SVille Syrjälä 	}
8603aa18df8SVille Syrjälä 
861ad3543edSMario Kleiner 	/* Get optional system timestamp after query. */
862ad3543edSMario Kleiner 	if (etime)
863ad3543edSMario Kleiner 		*etime = ktime_get();
864ad3543edSMario Kleiner 
865ad3543edSMario Kleiner 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
866ad3543edSMario Kleiner 
867ad3543edSMario Kleiner 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
868ad3543edSMario Kleiner 
8693aa18df8SVille Syrjälä 	in_vbl = position >= vbl_start && position < vbl_end;
8703aa18df8SVille Syrjälä 
8713aa18df8SVille Syrjälä 	/*
8723aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
8733aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
8743aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
8753aa18df8SVille Syrjälä 	 * up since vbl_end.
8763aa18df8SVille Syrjälä 	 */
8773aa18df8SVille Syrjälä 	if (position >= vbl_start)
8783aa18df8SVille Syrjälä 		position -= vbl_end;
8793aa18df8SVille Syrjälä 	else
8803aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
8813aa18df8SVille Syrjälä 
8827c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
8833aa18df8SVille Syrjälä 		*vpos = position;
8843aa18df8SVille Syrjälä 		*hpos = 0;
8853aa18df8SVille Syrjälä 	} else {
8860af7e4dfSMario Kleiner 		*vpos = position / htotal;
8870af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
8880af7e4dfSMario Kleiner 	}
8890af7e4dfSMario Kleiner 
8900af7e4dfSMario Kleiner 	/* In vblank? */
8910af7e4dfSMario Kleiner 	if (in_vbl)
8923d3cbd84SDaniel Vetter 		ret |= DRM_SCANOUTPOS_IN_VBLANK;
8930af7e4dfSMario Kleiner 
8940af7e4dfSMario Kleiner 	return ret;
8950af7e4dfSMario Kleiner }
8960af7e4dfSMario Kleiner 
897a225f079SVille Syrjälä int intel_get_crtc_scanline(struct intel_crtc *crtc)
898a225f079SVille Syrjälä {
899a225f079SVille Syrjälä 	struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
900a225f079SVille Syrjälä 	unsigned long irqflags;
901a225f079SVille Syrjälä 	int position;
902a225f079SVille Syrjälä 
903a225f079SVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
904a225f079SVille Syrjälä 	position = __intel_get_crtc_scanline(crtc);
905a225f079SVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
906a225f079SVille Syrjälä 
907a225f079SVille Syrjälä 	return position;
908a225f079SVille Syrjälä }
909a225f079SVille Syrjälä 
910f71d4af4SJesse Barnes static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
9110af7e4dfSMario Kleiner 			      int *max_error,
9120af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
9130af7e4dfSMario Kleiner 			      unsigned flags)
9140af7e4dfSMario Kleiner {
9154041b853SChris Wilson 	struct drm_crtc *crtc;
9160af7e4dfSMario Kleiner 
9177eb552aeSBen Widawsky 	if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
9184041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
9190af7e4dfSMario Kleiner 		return -EINVAL;
9200af7e4dfSMario Kleiner 	}
9210af7e4dfSMario Kleiner 
9220af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
9234041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
9244041b853SChris Wilson 	if (crtc == NULL) {
9254041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
9264041b853SChris Wilson 		return -EINVAL;
9274041b853SChris Wilson 	}
9284041b853SChris Wilson 
929fc467a22SMaarten Lankhorst 	if (!crtc->hwmode.crtc_clock) {
9304041b853SChris Wilson 		DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
9314041b853SChris Wilson 		return -EBUSY;
9324041b853SChris Wilson 	}
9330af7e4dfSMario Kleiner 
9340af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
9354041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
9364041b853SChris Wilson 						     vblank_time, flags,
937fc467a22SMaarten Lankhorst 						     &crtc->hwmode);
9380af7e4dfSMario Kleiner }
9390af7e4dfSMario Kleiner 
940d0ecd7e2SDaniel Vetter static void ironlake_rps_change_irq_handler(struct drm_device *dev)
941f97108d1SJesse Barnes {
9422d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
943b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
9449270388eSDaniel Vetter 	u8 new_delay;
9459270388eSDaniel Vetter 
946d0ecd7e2SDaniel Vetter 	spin_lock(&mchdev_lock);
947f97108d1SJesse Barnes 
94873edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
94973edd18fSDaniel Vetter 
95020e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
9519270388eSDaniel Vetter 
9527648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
953b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
954b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
955f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
956f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
957f97108d1SJesse Barnes 
958f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
959b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
96020e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
96120e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
96220e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
96320e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
964b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
96520e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
96620e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
96720e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
96820e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
969f97108d1SJesse Barnes 	}
970f97108d1SJesse Barnes 
9717648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
97220e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
973f97108d1SJesse Barnes 
974d0ecd7e2SDaniel Vetter 	spin_unlock(&mchdev_lock);
9759270388eSDaniel Vetter 
976f97108d1SJesse Barnes 	return;
977f97108d1SJesse Barnes }
978f97108d1SJesse Barnes 
97974cdb337SChris Wilson static void notify_ring(struct intel_engine_cs *ring)
980549f7365SChris Wilson {
98193b0a4e0SOscar Mateo 	if (!intel_ring_initialized(ring))
982475553deSChris Wilson 		return;
983475553deSChris Wilson 
984bcfcc8baSJohn Harrison 	trace_i915_gem_request_notify(ring);
9859862e600SChris Wilson 
986549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
987549f7365SChris Wilson }
988549f7365SChris Wilson 
98943cf3bf0SChris Wilson static void vlv_c0_read(struct drm_i915_private *dev_priv,
99043cf3bf0SChris Wilson 			struct intel_rps_ei *ei)
99131685c25SDeepak S {
99243cf3bf0SChris Wilson 	ei->cz_clock = vlv_punit_read(dev_priv, PUNIT_REG_CZ_TIMESTAMP);
99343cf3bf0SChris Wilson 	ei->render_c0 = I915_READ(VLV_RENDER_C0_COUNT);
99443cf3bf0SChris Wilson 	ei->media_c0 = I915_READ(VLV_MEDIA_C0_COUNT);
99531685c25SDeepak S }
99631685c25SDeepak S 
99743cf3bf0SChris Wilson static bool vlv_c0_above(struct drm_i915_private *dev_priv,
99843cf3bf0SChris Wilson 			 const struct intel_rps_ei *old,
99943cf3bf0SChris Wilson 			 const struct intel_rps_ei *now,
100043cf3bf0SChris Wilson 			 int threshold)
100131685c25SDeepak S {
100243cf3bf0SChris Wilson 	u64 time, c0;
10037bad74d5SVille Syrjälä 	unsigned int mul = 100;
100431685c25SDeepak S 
100543cf3bf0SChris Wilson 	if (old->cz_clock == 0)
100643cf3bf0SChris Wilson 		return false;
100731685c25SDeepak S 
10087bad74d5SVille Syrjälä 	if (I915_READ(VLV_COUNTER_CONTROL) & VLV_COUNT_RANGE_HIGH)
10097bad74d5SVille Syrjälä 		mul <<= 8;
10107bad74d5SVille Syrjälä 
101143cf3bf0SChris Wilson 	time = now->cz_clock - old->cz_clock;
10127bad74d5SVille Syrjälä 	time *= threshold * dev_priv->czclk_freq;
101331685c25SDeepak S 
101443cf3bf0SChris Wilson 	/* Workload can be split between render + media, e.g. SwapBuffers
101543cf3bf0SChris Wilson 	 * being blitted in X after being rendered in mesa. To account for
101643cf3bf0SChris Wilson 	 * this we need to combine both engines into our activity counter.
101743cf3bf0SChris Wilson 	 */
101843cf3bf0SChris Wilson 	c0 = now->render_c0 - old->render_c0;
101943cf3bf0SChris Wilson 	c0 += now->media_c0 - old->media_c0;
10207bad74d5SVille Syrjälä 	c0 *= mul * VLV_CZ_CLOCK_TO_MILLI_SEC;
102131685c25SDeepak S 
102243cf3bf0SChris Wilson 	return c0 >= time;
102331685c25SDeepak S }
102431685c25SDeepak S 
102543cf3bf0SChris Wilson void gen6_rps_reset_ei(struct drm_i915_private *dev_priv)
102643cf3bf0SChris Wilson {
102743cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &dev_priv->rps.down_ei);
102843cf3bf0SChris Wilson 	dev_priv->rps.up_ei = dev_priv->rps.down_ei;
102943cf3bf0SChris Wilson }
103043cf3bf0SChris Wilson 
103143cf3bf0SChris Wilson static u32 vlv_wa_c0_ei(struct drm_i915_private *dev_priv, u32 pm_iir)
103243cf3bf0SChris Wilson {
103343cf3bf0SChris Wilson 	struct intel_rps_ei now;
103443cf3bf0SChris Wilson 	u32 events = 0;
103543cf3bf0SChris Wilson 
10366f4b12f8SChris Wilson 	if ((pm_iir & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED)) == 0)
103743cf3bf0SChris Wilson 		return 0;
103843cf3bf0SChris Wilson 
103943cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &now);
104043cf3bf0SChris Wilson 	if (now.cz_clock == 0)
104143cf3bf0SChris Wilson 		return 0;
104231685c25SDeepak S 
104343cf3bf0SChris Wilson 	if (pm_iir & GEN6_PM_RP_DOWN_EI_EXPIRED) {
104443cf3bf0SChris Wilson 		if (!vlv_c0_above(dev_priv,
104543cf3bf0SChris Wilson 				  &dev_priv->rps.down_ei, &now,
10468fb55197SChris Wilson 				  dev_priv->rps.down_threshold))
104743cf3bf0SChris Wilson 			events |= GEN6_PM_RP_DOWN_THRESHOLD;
104843cf3bf0SChris Wilson 		dev_priv->rps.down_ei = now;
104931685c25SDeepak S 	}
105031685c25SDeepak S 
105143cf3bf0SChris Wilson 	if (pm_iir & GEN6_PM_RP_UP_EI_EXPIRED) {
105243cf3bf0SChris Wilson 		if (vlv_c0_above(dev_priv,
105343cf3bf0SChris Wilson 				 &dev_priv->rps.up_ei, &now,
10548fb55197SChris Wilson 				 dev_priv->rps.up_threshold))
105543cf3bf0SChris Wilson 			events |= GEN6_PM_RP_UP_THRESHOLD;
105643cf3bf0SChris Wilson 		dev_priv->rps.up_ei = now;
105743cf3bf0SChris Wilson 	}
105843cf3bf0SChris Wilson 
105943cf3bf0SChris Wilson 	return events;
106031685c25SDeepak S }
106131685c25SDeepak S 
1062f5a4c67dSChris Wilson static bool any_waiters(struct drm_i915_private *dev_priv)
1063f5a4c67dSChris Wilson {
1064f5a4c67dSChris Wilson 	struct intel_engine_cs *ring;
1065f5a4c67dSChris Wilson 	int i;
1066f5a4c67dSChris Wilson 
1067f5a4c67dSChris Wilson 	for_each_ring(ring, dev_priv, i)
1068f5a4c67dSChris Wilson 		if (ring->irq_refcount)
1069f5a4c67dSChris Wilson 			return true;
1070f5a4c67dSChris Wilson 
1071f5a4c67dSChris Wilson 	return false;
1072f5a4c67dSChris Wilson }
1073f5a4c67dSChris Wilson 
10744912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
10753b8d8d91SJesse Barnes {
10762d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
10772d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, rps.work);
10788d3afd7dSChris Wilson 	bool client_boost;
10798d3afd7dSChris Wilson 	int new_delay, adj, min, max;
1080edbfdb45SPaulo Zanoni 	u32 pm_iir;
10813b8d8d91SJesse Barnes 
108259cdb63dSDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
1083d4d70aa5SImre Deak 	/* Speed up work cancelation during disabling rps interrupts. */
1084d4d70aa5SImre Deak 	if (!dev_priv->rps.interrupts_enabled) {
1085d4d70aa5SImre Deak 		spin_unlock_irq(&dev_priv->irq_lock);
1086d4d70aa5SImre Deak 		return;
1087d4d70aa5SImre Deak 	}
1088c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
1089c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
1090a72fbc3aSImre Deak 	/* Make sure not to corrupt PMIMR state used by ringbuffer on GEN6 */
1091480c8033SDaniel Vetter 	gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
10928d3afd7dSChris Wilson 	client_boost = dev_priv->rps.client_boost;
10938d3afd7dSChris Wilson 	dev_priv->rps.client_boost = false;
109459cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
10954912d041SBen Widawsky 
109660611c13SPaulo Zanoni 	/* Make sure we didn't queue anything we're not going to process. */
1097a6706b45SDeepak S 	WARN_ON(pm_iir & ~dev_priv->pm_rps_events);
109860611c13SPaulo Zanoni 
10998d3afd7dSChris Wilson 	if ((pm_iir & dev_priv->pm_rps_events) == 0 && !client_boost)
11003b8d8d91SJesse Barnes 		return;
11013b8d8d91SJesse Barnes 
11024fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
11037b9e0ae6SChris Wilson 
110443cf3bf0SChris Wilson 	pm_iir |= vlv_wa_c0_ei(dev_priv, pm_iir);
110543cf3bf0SChris Wilson 
1106dd75fdc8SChris Wilson 	adj = dev_priv->rps.last_adj;
1107edcf284bSChris Wilson 	new_delay = dev_priv->rps.cur_freq;
11088d3afd7dSChris Wilson 	min = dev_priv->rps.min_freq_softlimit;
11098d3afd7dSChris Wilson 	max = dev_priv->rps.max_freq_softlimit;
11108d3afd7dSChris Wilson 
11118d3afd7dSChris Wilson 	if (client_boost) {
11128d3afd7dSChris Wilson 		new_delay = dev_priv->rps.max_freq_softlimit;
11138d3afd7dSChris Wilson 		adj = 0;
11148d3afd7dSChris Wilson 	} else if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
1115dd75fdc8SChris Wilson 		if (adj > 0)
1116dd75fdc8SChris Wilson 			adj *= 2;
1117edcf284bSChris Wilson 		else /* CHV needs even encode values */
1118edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? 2 : 1;
11197425034aSVille Syrjälä 		/*
11207425034aSVille Syrjälä 		 * For better performance, jump directly
11217425034aSVille Syrjälä 		 * to RPe if we're below it.
11227425034aSVille Syrjälä 		 */
1123edcf284bSChris Wilson 		if (new_delay < dev_priv->rps.efficient_freq - adj) {
1124b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1125edcf284bSChris Wilson 			adj = 0;
1126edcf284bSChris Wilson 		}
1127f5a4c67dSChris Wilson 	} else if (any_waiters(dev_priv)) {
1128f5a4c67dSChris Wilson 		adj = 0;
1129dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
1130b39fb297SBen Widawsky 		if (dev_priv->rps.cur_freq > dev_priv->rps.efficient_freq)
1131b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1132dd75fdc8SChris Wilson 		else
1133b39fb297SBen Widawsky 			new_delay = dev_priv->rps.min_freq_softlimit;
1134dd75fdc8SChris Wilson 		adj = 0;
1135dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1136dd75fdc8SChris Wilson 		if (adj < 0)
1137dd75fdc8SChris Wilson 			adj *= 2;
1138edcf284bSChris Wilson 		else /* CHV needs even encode values */
1139edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? -2 : -1;
1140dd75fdc8SChris Wilson 	} else { /* unknown event */
1141edcf284bSChris Wilson 		adj = 0;
1142dd75fdc8SChris Wilson 	}
11433b8d8d91SJesse Barnes 
1144edcf284bSChris Wilson 	dev_priv->rps.last_adj = adj;
1145edcf284bSChris Wilson 
114679249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
114779249636SBen Widawsky 	 * interrupt
114879249636SBen Widawsky 	 */
1149edcf284bSChris Wilson 	new_delay += adj;
11508d3afd7dSChris Wilson 	new_delay = clamp_t(int, new_delay, min, max);
115127544369SDeepak S 
1152ffe02b40SVille Syrjälä 	intel_set_rps(dev_priv->dev, new_delay);
11533b8d8d91SJesse Barnes 
11544fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
11553b8d8d91SJesse Barnes }
11563b8d8d91SJesse Barnes 
1157e3689190SBen Widawsky 
1158e3689190SBen Widawsky /**
1159e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
1160e3689190SBen Widawsky  * occurred.
1161e3689190SBen Widawsky  * @work: workqueue struct
1162e3689190SBen Widawsky  *
1163e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
1164e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
1165e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
1166e3689190SBen Widawsky  */
1167e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
1168e3689190SBen Widawsky {
11692d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
11702d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, l3_parity.error_work);
1171e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
117235a85ac6SBen Widawsky 	char *parity_event[6];
1173e3689190SBen Widawsky 	uint32_t misccpctl;
117435a85ac6SBen Widawsky 	uint8_t slice = 0;
1175e3689190SBen Widawsky 
1176e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
1177e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
1178e3689190SBen Widawsky 	 * any time we access those registers.
1179e3689190SBen Widawsky 	 */
1180e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
1181e3689190SBen Widawsky 
118235a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
118335a85ac6SBen Widawsky 	if (WARN_ON(!dev_priv->l3_parity.which_slice))
118435a85ac6SBen Widawsky 		goto out;
118535a85ac6SBen Widawsky 
1186e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
1187e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1188e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
1189e3689190SBen Widawsky 
119035a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
119135a85ac6SBen Widawsky 		u32 reg;
119235a85ac6SBen Widawsky 
119335a85ac6SBen Widawsky 		slice--;
119435a85ac6SBen Widawsky 		if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv->dev)))
119535a85ac6SBen Widawsky 			break;
119635a85ac6SBen Widawsky 
119735a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
119835a85ac6SBen Widawsky 
119935a85ac6SBen Widawsky 		reg = GEN7_L3CDERRST1 + (slice * 0x200);
120035a85ac6SBen Widawsky 
120135a85ac6SBen Widawsky 		error_status = I915_READ(reg);
1202e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
1203e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
1204e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1205e3689190SBen Widawsky 
120635a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
120735a85ac6SBen Widawsky 		POSTING_READ(reg);
1208e3689190SBen Widawsky 
1209cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1210e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1211e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1212e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
121335a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
121435a85ac6SBen Widawsky 		parity_event[5] = NULL;
1215e3689190SBen Widawsky 
12165bdebb18SDave Airlie 		kobject_uevent_env(&dev_priv->dev->primary->kdev->kobj,
1217e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
1218e3689190SBen Widawsky 
121935a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
122035a85ac6SBen Widawsky 			  slice, row, bank, subbank);
1221e3689190SBen Widawsky 
122235a85ac6SBen Widawsky 		kfree(parity_event[4]);
1223e3689190SBen Widawsky 		kfree(parity_event[3]);
1224e3689190SBen Widawsky 		kfree(parity_event[2]);
1225e3689190SBen Widawsky 		kfree(parity_event[1]);
1226e3689190SBen Widawsky 	}
1227e3689190SBen Widawsky 
122835a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
122935a85ac6SBen Widawsky 
123035a85ac6SBen Widawsky out:
123135a85ac6SBen Widawsky 	WARN_ON(dev_priv->l3_parity.which_slice);
12324cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
1233480c8033SDaniel Vetter 	gen5_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv->dev));
12344cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
123535a85ac6SBen Widawsky 
123635a85ac6SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
123735a85ac6SBen Widawsky }
123835a85ac6SBen Widawsky 
123935a85ac6SBen Widawsky static void ivybridge_parity_error_irq_handler(struct drm_device *dev, u32 iir)
1240e3689190SBen Widawsky {
12412d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1242e3689190SBen Widawsky 
1243040d2baaSBen Widawsky 	if (!HAS_L3_DPF(dev))
1244e3689190SBen Widawsky 		return;
1245e3689190SBen Widawsky 
1246d0ecd7e2SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1247480c8033SDaniel Vetter 	gen5_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev));
1248d0ecd7e2SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
1249e3689190SBen Widawsky 
125035a85ac6SBen Widawsky 	iir &= GT_PARITY_ERROR(dev);
125135a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
125235a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 1;
125335a85ac6SBen Widawsky 
125435a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
125535a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 0;
125635a85ac6SBen Widawsky 
1257a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
1258e3689190SBen Widawsky }
1259e3689190SBen Widawsky 
1260f1af8fc1SPaulo Zanoni static void ilk_gt_irq_handler(struct drm_device *dev,
1261f1af8fc1SPaulo Zanoni 			       struct drm_i915_private *dev_priv,
1262f1af8fc1SPaulo Zanoni 			       u32 gt_iir)
1263f1af8fc1SPaulo Zanoni {
1264f1af8fc1SPaulo Zanoni 	if (gt_iir &
1265f1af8fc1SPaulo Zanoni 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
126674cdb337SChris Wilson 		notify_ring(&dev_priv->ring[RCS]);
1267f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
126874cdb337SChris Wilson 		notify_ring(&dev_priv->ring[VCS]);
1269f1af8fc1SPaulo Zanoni }
1270f1af8fc1SPaulo Zanoni 
1271e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
1272e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
1273e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1274e7b4c6b1SDaniel Vetter {
1275e7b4c6b1SDaniel Vetter 
1276cc609d5dSBen Widawsky 	if (gt_iir &
1277cc609d5dSBen Widawsky 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
127874cdb337SChris Wilson 		notify_ring(&dev_priv->ring[RCS]);
1279cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
128074cdb337SChris Wilson 		notify_ring(&dev_priv->ring[VCS]);
1281cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
128274cdb337SChris Wilson 		notify_ring(&dev_priv->ring[BCS]);
1283e7b4c6b1SDaniel Vetter 
1284cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1285cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
1286aaecdf61SDaniel Vetter 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT))
1287aaecdf61SDaniel Vetter 		DRM_DEBUG("Command parser error, gt_iir 0x%08x\n", gt_iir);
1288e3689190SBen Widawsky 
128935a85ac6SBen Widawsky 	if (gt_iir & GT_PARITY_ERROR(dev))
129035a85ac6SBen Widawsky 		ivybridge_parity_error_irq_handler(dev, gt_iir);
1291e7b4c6b1SDaniel Vetter }
1292e7b4c6b1SDaniel Vetter 
129374cdb337SChris Wilson static irqreturn_t gen8_gt_irq_handler(struct drm_i915_private *dev_priv,
1294abd58f01SBen Widawsky 				       u32 master_ctl)
1295abd58f01SBen Widawsky {
1296abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
1297abd58f01SBen Widawsky 
1298abd58f01SBen Widawsky 	if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
129974cdb337SChris Wilson 		u32 tmp = I915_READ_FW(GEN8_GT_IIR(0));
1300abd58f01SBen Widawsky 		if (tmp) {
1301cb0d205eSChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(0), tmp);
1302abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1303e981e7b1SThomas Daniel 
130474cdb337SChris Wilson 			if (tmp & (GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT))
130574cdb337SChris Wilson 				intel_lrc_irq_handler(&dev_priv->ring[RCS]);
130674cdb337SChris Wilson 			if (tmp & (GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT))
130774cdb337SChris Wilson 				notify_ring(&dev_priv->ring[RCS]);
1308e981e7b1SThomas Daniel 
130974cdb337SChris Wilson 			if (tmp & (GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT))
131074cdb337SChris Wilson 				intel_lrc_irq_handler(&dev_priv->ring[BCS]);
131174cdb337SChris Wilson 			if (tmp & (GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT))
131274cdb337SChris Wilson 				notify_ring(&dev_priv->ring[BCS]);
1313abd58f01SBen Widawsky 		} else
1314abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT0)!\n");
1315abd58f01SBen Widawsky 	}
1316abd58f01SBen Widawsky 
131785f9b5f9SZhao Yakui 	if (master_ctl & (GEN8_GT_VCS1_IRQ | GEN8_GT_VCS2_IRQ)) {
131874cdb337SChris Wilson 		u32 tmp = I915_READ_FW(GEN8_GT_IIR(1));
1319abd58f01SBen Widawsky 		if (tmp) {
1320cb0d205eSChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(1), tmp);
1321abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1322e981e7b1SThomas Daniel 
132374cdb337SChris Wilson 			if (tmp & (GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT))
132474cdb337SChris Wilson 				intel_lrc_irq_handler(&dev_priv->ring[VCS]);
132574cdb337SChris Wilson 			if (tmp & (GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT))
132674cdb337SChris Wilson 				notify_ring(&dev_priv->ring[VCS]);
1327e981e7b1SThomas Daniel 
132874cdb337SChris Wilson 			if (tmp & (GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT))
132974cdb337SChris Wilson 				intel_lrc_irq_handler(&dev_priv->ring[VCS2]);
133074cdb337SChris Wilson 			if (tmp & (GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT))
133174cdb337SChris Wilson 				notify_ring(&dev_priv->ring[VCS2]);
1332abd58f01SBen Widawsky 		} else
1333abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT1)!\n");
1334abd58f01SBen Widawsky 	}
1335abd58f01SBen Widawsky 
133674cdb337SChris Wilson 	if (master_ctl & GEN8_GT_VECS_IRQ) {
133774cdb337SChris Wilson 		u32 tmp = I915_READ_FW(GEN8_GT_IIR(3));
133874cdb337SChris Wilson 		if (tmp) {
133974cdb337SChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(3), tmp);
134074cdb337SChris Wilson 			ret = IRQ_HANDLED;
134174cdb337SChris Wilson 
134274cdb337SChris Wilson 			if (tmp & (GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT))
134374cdb337SChris Wilson 				intel_lrc_irq_handler(&dev_priv->ring[VECS]);
134474cdb337SChris Wilson 			if (tmp & (GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT))
134574cdb337SChris Wilson 				notify_ring(&dev_priv->ring[VECS]);
134674cdb337SChris Wilson 		} else
134774cdb337SChris Wilson 			DRM_ERROR("The master control interrupt lied (GT3)!\n");
134874cdb337SChris Wilson 	}
134974cdb337SChris Wilson 
13500961021aSBen Widawsky 	if (master_ctl & GEN8_GT_PM_IRQ) {
135174cdb337SChris Wilson 		u32 tmp = I915_READ_FW(GEN8_GT_IIR(2));
13520961021aSBen Widawsky 		if (tmp & dev_priv->pm_rps_events) {
1353cb0d205eSChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(2),
13540961021aSBen Widawsky 				      tmp & dev_priv->pm_rps_events);
135538cc46d7SOscar Mateo 			ret = IRQ_HANDLED;
1356c9a9a268SImre Deak 			gen6_rps_irq_handler(dev_priv, tmp);
13570961021aSBen Widawsky 		} else
13580961021aSBen Widawsky 			DRM_ERROR("The master control interrupt lied (PM)!\n");
13590961021aSBen Widawsky 	}
13600961021aSBen Widawsky 
1361abd58f01SBen Widawsky 	return ret;
1362abd58f01SBen Widawsky }
1363abd58f01SBen Widawsky 
136463c88d22SImre Deak static bool bxt_port_hotplug_long_detect(enum port port, u32 val)
136563c88d22SImre Deak {
136663c88d22SImre Deak 	switch (port) {
136763c88d22SImre Deak 	case PORT_A:
1368195baa06SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
136963c88d22SImre Deak 	case PORT_B:
137063c88d22SImre Deak 		return val & PORTB_HOTPLUG_LONG_DETECT;
137163c88d22SImre Deak 	case PORT_C:
137263c88d22SImre Deak 		return val & PORTC_HOTPLUG_LONG_DETECT;
137363c88d22SImre Deak 	default:
137463c88d22SImre Deak 		return false;
137563c88d22SImre Deak 	}
137663c88d22SImre Deak }
137763c88d22SImre Deak 
13786dbf30ceSVille Syrjälä static bool spt_port_hotplug2_long_detect(enum port port, u32 val)
13796dbf30ceSVille Syrjälä {
13806dbf30ceSVille Syrjälä 	switch (port) {
13816dbf30ceSVille Syrjälä 	case PORT_E:
13826dbf30ceSVille Syrjälä 		return val & PORTE_HOTPLUG_LONG_DETECT;
13836dbf30ceSVille Syrjälä 	default:
13846dbf30ceSVille Syrjälä 		return false;
13856dbf30ceSVille Syrjälä 	}
13866dbf30ceSVille Syrjälä }
13876dbf30ceSVille Syrjälä 
138874c0b395SVille Syrjälä static bool spt_port_hotplug_long_detect(enum port port, u32 val)
138974c0b395SVille Syrjälä {
139074c0b395SVille Syrjälä 	switch (port) {
139174c0b395SVille Syrjälä 	case PORT_A:
139274c0b395SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
139374c0b395SVille Syrjälä 	case PORT_B:
139474c0b395SVille Syrjälä 		return val & PORTB_HOTPLUG_LONG_DETECT;
139574c0b395SVille Syrjälä 	case PORT_C:
139674c0b395SVille Syrjälä 		return val & PORTC_HOTPLUG_LONG_DETECT;
139774c0b395SVille Syrjälä 	case PORT_D:
139874c0b395SVille Syrjälä 		return val & PORTD_HOTPLUG_LONG_DETECT;
139974c0b395SVille Syrjälä 	default:
140074c0b395SVille Syrjälä 		return false;
140174c0b395SVille Syrjälä 	}
140274c0b395SVille Syrjälä }
140374c0b395SVille Syrjälä 
1404e4ce95aaSVille Syrjälä static bool ilk_port_hotplug_long_detect(enum port port, u32 val)
1405e4ce95aaSVille Syrjälä {
1406e4ce95aaSVille Syrjälä 	switch (port) {
1407e4ce95aaSVille Syrjälä 	case PORT_A:
1408e4ce95aaSVille Syrjälä 		return val & DIGITAL_PORTA_HOTPLUG_LONG_DETECT;
1409e4ce95aaSVille Syrjälä 	default:
1410e4ce95aaSVille Syrjälä 		return false;
1411e4ce95aaSVille Syrjälä 	}
1412e4ce95aaSVille Syrjälä }
1413e4ce95aaSVille Syrjälä 
1414676574dfSJani Nikula static bool pch_port_hotplug_long_detect(enum port port, u32 val)
141513cf5504SDave Airlie {
141613cf5504SDave Airlie 	switch (port) {
141713cf5504SDave Airlie 	case PORT_B:
1418676574dfSJani Nikula 		return val & PORTB_HOTPLUG_LONG_DETECT;
141913cf5504SDave Airlie 	case PORT_C:
1420676574dfSJani Nikula 		return val & PORTC_HOTPLUG_LONG_DETECT;
142113cf5504SDave Airlie 	case PORT_D:
1422676574dfSJani Nikula 		return val & PORTD_HOTPLUG_LONG_DETECT;
1423676574dfSJani Nikula 	default:
1424676574dfSJani Nikula 		return false;
142513cf5504SDave Airlie 	}
142613cf5504SDave Airlie }
142713cf5504SDave Airlie 
1428676574dfSJani Nikula static bool i9xx_port_hotplug_long_detect(enum port port, u32 val)
142913cf5504SDave Airlie {
143013cf5504SDave Airlie 	switch (port) {
143113cf5504SDave Airlie 	case PORT_B:
1432676574dfSJani Nikula 		return val & PORTB_HOTPLUG_INT_LONG_PULSE;
143313cf5504SDave Airlie 	case PORT_C:
1434676574dfSJani Nikula 		return val & PORTC_HOTPLUG_INT_LONG_PULSE;
143513cf5504SDave Airlie 	case PORT_D:
1436676574dfSJani Nikula 		return val & PORTD_HOTPLUG_INT_LONG_PULSE;
1437676574dfSJani Nikula 	default:
1438676574dfSJani Nikula 		return false;
143913cf5504SDave Airlie 	}
144013cf5504SDave Airlie }
144113cf5504SDave Airlie 
144242db67d6SVille Syrjälä /*
144342db67d6SVille Syrjälä  * Get a bit mask of pins that have triggered, and which ones may be long.
144442db67d6SVille Syrjälä  * This can be called multiple times with the same masks to accumulate
144542db67d6SVille Syrjälä  * hotplug detection results from several registers.
144642db67d6SVille Syrjälä  *
144742db67d6SVille Syrjälä  * Note that the caller is expected to zero out the masks initially.
144842db67d6SVille Syrjälä  */
1449fd63e2a9SImre Deak static void intel_get_hpd_pins(u32 *pin_mask, u32 *long_mask,
14508c841e57SJani Nikula 			     u32 hotplug_trigger, u32 dig_hotplug_reg,
1451fd63e2a9SImre Deak 			     const u32 hpd[HPD_NUM_PINS],
1452fd63e2a9SImre Deak 			     bool long_pulse_detect(enum port port, u32 val))
1453676574dfSJani Nikula {
14548c841e57SJani Nikula 	enum port port;
1455676574dfSJani Nikula 	int i;
1456676574dfSJani Nikula 
1457676574dfSJani Nikula 	for_each_hpd_pin(i) {
14588c841e57SJani Nikula 		if ((hpd[i] & hotplug_trigger) == 0)
14598c841e57SJani Nikula 			continue;
14608c841e57SJani Nikula 
1461676574dfSJani Nikula 		*pin_mask |= BIT(i);
1462676574dfSJani Nikula 
1463cc24fcdcSImre Deak 		if (!intel_hpd_pin_to_port(i, &port))
1464cc24fcdcSImre Deak 			continue;
1465cc24fcdcSImre Deak 
1466fd63e2a9SImre Deak 		if (long_pulse_detect(port, dig_hotplug_reg))
1467676574dfSJani Nikula 			*long_mask |= BIT(i);
1468676574dfSJani Nikula 	}
1469676574dfSJani Nikula 
1470676574dfSJani Nikula 	DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x, dig 0x%08x, pins 0x%08x\n",
1471676574dfSJani Nikula 			 hotplug_trigger, dig_hotplug_reg, *pin_mask);
1472676574dfSJani Nikula 
1473676574dfSJani Nikula }
1474676574dfSJani Nikula 
1475515ac2bbSDaniel Vetter static void gmbus_irq_handler(struct drm_device *dev)
1476515ac2bbSDaniel Vetter {
14772d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
147828c70f16SDaniel Vetter 
147928c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1480515ac2bbSDaniel Vetter }
1481515ac2bbSDaniel Vetter 
1482ce99c256SDaniel Vetter static void dp_aux_irq_handler(struct drm_device *dev)
1483ce99c256SDaniel Vetter {
14842d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
14859ee32feaSDaniel Vetter 
14869ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1487ce99c256SDaniel Vetter }
1488ce99c256SDaniel Vetter 
14898bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
1490277de95eSDaniel Vetter static void display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1491eba94eb9SDaniel Vetter 					 uint32_t crc0, uint32_t crc1,
1492eba94eb9SDaniel Vetter 					 uint32_t crc2, uint32_t crc3,
14938bc5e955SDaniel Vetter 					 uint32_t crc4)
14948bf1e9f1SShuang He {
14958bf1e9f1SShuang He 	struct drm_i915_private *dev_priv = dev->dev_private;
14968bf1e9f1SShuang He 	struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
14978bf1e9f1SShuang He 	struct intel_pipe_crc_entry *entry;
1498ac2300d4SDamien Lespiau 	int head, tail;
1499b2c88f5bSDamien Lespiau 
1500d538bbdfSDamien Lespiau 	spin_lock(&pipe_crc->lock);
1501d538bbdfSDamien Lespiau 
15020c912c79SDamien Lespiau 	if (!pipe_crc->entries) {
1503d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
150434273620SDaniel Vetter 		DRM_DEBUG_KMS("spurious interrupt\n");
15050c912c79SDamien Lespiau 		return;
15060c912c79SDamien Lespiau 	}
15070c912c79SDamien Lespiau 
1508d538bbdfSDamien Lespiau 	head = pipe_crc->head;
1509d538bbdfSDamien Lespiau 	tail = pipe_crc->tail;
1510b2c88f5bSDamien Lespiau 
1511b2c88f5bSDamien Lespiau 	if (CIRC_SPACE(head, tail, INTEL_PIPE_CRC_ENTRIES_NR) < 1) {
1512d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
1513b2c88f5bSDamien Lespiau 		DRM_ERROR("CRC buffer overflowing\n");
1514b2c88f5bSDamien Lespiau 		return;
1515b2c88f5bSDamien Lespiau 	}
1516b2c88f5bSDamien Lespiau 
1517b2c88f5bSDamien Lespiau 	entry = &pipe_crc->entries[head];
15188bf1e9f1SShuang He 
15198bc5e955SDaniel Vetter 	entry->frame = dev->driver->get_vblank_counter(dev, pipe);
1520eba94eb9SDaniel Vetter 	entry->crc[0] = crc0;
1521eba94eb9SDaniel Vetter 	entry->crc[1] = crc1;
1522eba94eb9SDaniel Vetter 	entry->crc[2] = crc2;
1523eba94eb9SDaniel Vetter 	entry->crc[3] = crc3;
1524eba94eb9SDaniel Vetter 	entry->crc[4] = crc4;
1525b2c88f5bSDamien Lespiau 
1526b2c88f5bSDamien Lespiau 	head = (head + 1) & (INTEL_PIPE_CRC_ENTRIES_NR - 1);
1527d538bbdfSDamien Lespiau 	pipe_crc->head = head;
1528d538bbdfSDamien Lespiau 
1529d538bbdfSDamien Lespiau 	spin_unlock(&pipe_crc->lock);
153007144428SDamien Lespiau 
153107144428SDamien Lespiau 	wake_up_interruptible(&pipe_crc->wq);
15328bf1e9f1SShuang He }
1533277de95eSDaniel Vetter #else
1534277de95eSDaniel Vetter static inline void
1535277de95eSDaniel Vetter display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1536277de95eSDaniel Vetter 			     uint32_t crc0, uint32_t crc1,
1537277de95eSDaniel Vetter 			     uint32_t crc2, uint32_t crc3,
1538277de95eSDaniel Vetter 			     uint32_t crc4) {}
1539277de95eSDaniel Vetter #endif
1540eba94eb9SDaniel Vetter 
1541277de95eSDaniel Vetter 
1542277de95eSDaniel Vetter static void hsw_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
15435a69b89fSDaniel Vetter {
15445a69b89fSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
15455a69b89fSDaniel Vetter 
1546277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
15475a69b89fSDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
15485a69b89fSDaniel Vetter 				     0, 0, 0, 0);
15495a69b89fSDaniel Vetter }
15505a69b89fSDaniel Vetter 
1551277de95eSDaniel Vetter static void ivb_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
1552eba94eb9SDaniel Vetter {
1553eba94eb9SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1554eba94eb9SDaniel Vetter 
1555277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
1556eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1557eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1558eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1559eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
15608bc5e955SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1561eba94eb9SDaniel Vetter }
15625b3a856bSDaniel Vetter 
1563277de95eSDaniel Vetter static void i9xx_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
15645b3a856bSDaniel Vetter {
15655b3a856bSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
15660b5c5ed0SDaniel Vetter 	uint32_t res1, res2;
15670b5c5ed0SDaniel Vetter 
15680b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 3)
15690b5c5ed0SDaniel Vetter 		res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
15700b5c5ed0SDaniel Vetter 	else
15710b5c5ed0SDaniel Vetter 		res1 = 0;
15720b5c5ed0SDaniel Vetter 
15730b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
15740b5c5ed0SDaniel Vetter 		res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
15750b5c5ed0SDaniel Vetter 	else
15760b5c5ed0SDaniel Vetter 		res2 = 0;
15775b3a856bSDaniel Vetter 
1578277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
15790b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_RED(pipe)),
15800b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_GREEN(pipe)),
15810b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_BLUE(pipe)),
15820b5c5ed0SDaniel Vetter 				     res1, res2);
15835b3a856bSDaniel Vetter }
15848bf1e9f1SShuang He 
15851403c0d4SPaulo Zanoni /* The RPS events need forcewake, so we add them to a work queue and mask their
15861403c0d4SPaulo Zanoni  * IMR bits until the work is done. Other interrupts can be processed without
15871403c0d4SPaulo Zanoni  * the work queue. */
15881403c0d4SPaulo Zanoni static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1589baf02a1fSBen Widawsky {
1590a6706b45SDeepak S 	if (pm_iir & dev_priv->pm_rps_events) {
159159cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
1592480c8033SDaniel Vetter 		gen6_disable_pm_irq(dev_priv, pm_iir & dev_priv->pm_rps_events);
1593d4d70aa5SImre Deak 		if (dev_priv->rps.interrupts_enabled) {
1594d4d70aa5SImre Deak 			dev_priv->rps.pm_iir |= pm_iir & dev_priv->pm_rps_events;
15952adbee62SDaniel Vetter 			queue_work(dev_priv->wq, &dev_priv->rps.work);
159641a05a3aSDaniel Vetter 		}
1597d4d70aa5SImre Deak 		spin_unlock(&dev_priv->irq_lock);
1598d4d70aa5SImre Deak 	}
1599baf02a1fSBen Widawsky 
1600c9a9a268SImre Deak 	if (INTEL_INFO(dev_priv)->gen >= 8)
1601c9a9a268SImre Deak 		return;
1602c9a9a268SImre Deak 
16031403c0d4SPaulo Zanoni 	if (HAS_VEBOX(dev_priv->dev)) {
160412638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
160574cdb337SChris Wilson 			notify_ring(&dev_priv->ring[VECS]);
160612638c57SBen Widawsky 
1607aaecdf61SDaniel Vetter 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT)
1608aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, pm_iir 0x%08x\n", pm_iir);
160912638c57SBen Widawsky 	}
16101403c0d4SPaulo Zanoni }
1611baf02a1fSBen Widawsky 
16128d7849dbSVille Syrjälä static bool intel_pipe_handle_vblank(struct drm_device *dev, enum pipe pipe)
16138d7849dbSVille Syrjälä {
16148d7849dbSVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
16158d7849dbSVille Syrjälä 		return false;
16168d7849dbSVille Syrjälä 
16178d7849dbSVille Syrjälä 	return true;
16188d7849dbSVille Syrjälä }
16198d7849dbSVille Syrjälä 
1620c1874ed7SImre Deak static void valleyview_pipestat_irq_handler(struct drm_device *dev, u32 iir)
16217e231dbeSJesse Barnes {
1622c1874ed7SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
162391d181ddSImre Deak 	u32 pipe_stats[I915_MAX_PIPES] = { };
16247e231dbeSJesse Barnes 	int pipe;
16257e231dbeSJesse Barnes 
162658ead0d7SImre Deak 	spin_lock(&dev_priv->irq_lock);
1627055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
162891d181ddSImre Deak 		int reg;
1629bbb5eebfSDaniel Vetter 		u32 mask, iir_bit = 0;
163091d181ddSImre Deak 
1631bbb5eebfSDaniel Vetter 		/*
1632bbb5eebfSDaniel Vetter 		 * PIPESTAT bits get signalled even when the interrupt is
1633bbb5eebfSDaniel Vetter 		 * disabled with the mask bits, and some of the status bits do
1634bbb5eebfSDaniel Vetter 		 * not generate interrupts at all (like the underrun bit). Hence
1635bbb5eebfSDaniel Vetter 		 * we need to be careful that we only handle what we want to
1636bbb5eebfSDaniel Vetter 		 * handle.
1637bbb5eebfSDaniel Vetter 		 */
16380f239f4cSDaniel Vetter 
16390f239f4cSDaniel Vetter 		/* fifo underruns are filterered in the underrun handler. */
16400f239f4cSDaniel Vetter 		mask = PIPE_FIFO_UNDERRUN_STATUS;
1641bbb5eebfSDaniel Vetter 
1642bbb5eebfSDaniel Vetter 		switch (pipe) {
1643bbb5eebfSDaniel Vetter 		case PIPE_A:
1644bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
1645bbb5eebfSDaniel Vetter 			break;
1646bbb5eebfSDaniel Vetter 		case PIPE_B:
1647bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
1648bbb5eebfSDaniel Vetter 			break;
16493278f67fSVille Syrjälä 		case PIPE_C:
16503278f67fSVille Syrjälä 			iir_bit = I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
16513278f67fSVille Syrjälä 			break;
1652bbb5eebfSDaniel Vetter 		}
1653bbb5eebfSDaniel Vetter 		if (iir & iir_bit)
1654bbb5eebfSDaniel Vetter 			mask |= dev_priv->pipestat_irq_mask[pipe];
1655bbb5eebfSDaniel Vetter 
1656bbb5eebfSDaniel Vetter 		if (!mask)
165791d181ddSImre Deak 			continue;
165891d181ddSImre Deak 
165991d181ddSImre Deak 		reg = PIPESTAT(pipe);
1660bbb5eebfSDaniel Vetter 		mask |= PIPESTAT_INT_ENABLE_MASK;
1661bbb5eebfSDaniel Vetter 		pipe_stats[pipe] = I915_READ(reg) & mask;
16627e231dbeSJesse Barnes 
16637e231dbeSJesse Barnes 		/*
16647e231dbeSJesse Barnes 		 * Clear the PIPE*STAT regs before the IIR
16657e231dbeSJesse Barnes 		 */
166691d181ddSImre Deak 		if (pipe_stats[pipe] & (PIPE_FIFO_UNDERRUN_STATUS |
166791d181ddSImre Deak 					PIPESTAT_INT_STATUS_MASK))
16687e231dbeSJesse Barnes 			I915_WRITE(reg, pipe_stats[pipe]);
16697e231dbeSJesse Barnes 	}
167058ead0d7SImre Deak 	spin_unlock(&dev_priv->irq_lock);
16717e231dbeSJesse Barnes 
1672055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1673d6bbafa1SChris Wilson 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
1674d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
1675d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
167631acc7f5SJesse Barnes 
1677579a9b0eSImre Deak 		if (pipe_stats[pipe] & PLANE_FLIP_DONE_INT_STATUS_VLV) {
167831acc7f5SJesse Barnes 			intel_prepare_page_flip(dev, pipe);
167931acc7f5SJesse Barnes 			intel_finish_page_flip(dev, pipe);
168031acc7f5SJesse Barnes 		}
16814356d586SDaniel Vetter 
16824356d586SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1683277de95eSDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
16842d9d2b0bSVille Syrjälä 
16851f7247c0SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
16861f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
168731acc7f5SJesse Barnes 	}
168831acc7f5SJesse Barnes 
1689c1874ed7SImre Deak 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1690c1874ed7SImre Deak 		gmbus_irq_handler(dev);
1691c1874ed7SImre Deak }
1692c1874ed7SImre Deak 
169316c6c56bSVille Syrjälä static void i9xx_hpd_irq_handler(struct drm_device *dev)
169416c6c56bSVille Syrjälä {
169516c6c56bSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
169616c6c56bSVille Syrjälä 	u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
169742db67d6SVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
169816c6c56bSVille Syrjälä 
16990d2e4297SJani Nikula 	if (!hotplug_status)
17000d2e4297SJani Nikula 		return;
17010d2e4297SJani Nikula 
17023ff60f89SOscar Mateo 	I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
17033ff60f89SOscar Mateo 	/*
17043ff60f89SOscar Mateo 	 * Make sure hotplug status is cleared before we clear IIR, or else we
17053ff60f89SOscar Mateo 	 * may miss hotplug events.
17063ff60f89SOscar Mateo 	 */
17073ff60f89SOscar Mateo 	POSTING_READ(PORT_HOTPLUG_STAT);
17083ff60f89SOscar Mateo 
17094bca26d0SVille Syrjälä 	if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
171016c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
171116c6c56bSVille Syrjälä 
171258f2cf24SVille Syrjälä 		if (hotplug_trigger) {
1713fd63e2a9SImre Deak 			intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
1714fd63e2a9SImre Deak 					   hotplug_trigger, hpd_status_g4x,
1715fd63e2a9SImre Deak 					   i9xx_port_hotplug_long_detect);
171658f2cf24SVille Syrjälä 
1717676574dfSJani Nikula 			intel_hpd_irq_handler(dev, pin_mask, long_mask);
171858f2cf24SVille Syrjälä 		}
1719369712e8SJani Nikula 
1720369712e8SJani Nikula 		if (hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
1721369712e8SJani Nikula 			dp_aux_irq_handler(dev);
172216c6c56bSVille Syrjälä 	} else {
172316c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
172416c6c56bSVille Syrjälä 
172558f2cf24SVille Syrjälä 		if (hotplug_trigger) {
1726fd63e2a9SImre Deak 			intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
17274e3d1e26SVille Syrjälä 					   hotplug_trigger, hpd_status_i915,
1728fd63e2a9SImre Deak 					   i9xx_port_hotplug_long_detect);
1729676574dfSJani Nikula 			intel_hpd_irq_handler(dev, pin_mask, long_mask);
173016c6c56bSVille Syrjälä 		}
17313ff60f89SOscar Mateo 	}
173258f2cf24SVille Syrjälä }
173316c6c56bSVille Syrjälä 
1734c1874ed7SImre Deak static irqreturn_t valleyview_irq_handler(int irq, void *arg)
1735c1874ed7SImre Deak {
173645a83f84SDaniel Vetter 	struct drm_device *dev = arg;
17372d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1738c1874ed7SImre Deak 	u32 iir, gt_iir, pm_iir;
1739c1874ed7SImre Deak 	irqreturn_t ret = IRQ_NONE;
1740c1874ed7SImre Deak 
17412dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
17422dd2a883SImre Deak 		return IRQ_NONE;
17432dd2a883SImre Deak 
1744c1874ed7SImre Deak 	while (true) {
17453ff60f89SOscar Mateo 		/* Find, clear, then process each source of interrupt */
17463ff60f89SOscar Mateo 
1747c1874ed7SImre Deak 		gt_iir = I915_READ(GTIIR);
17483ff60f89SOscar Mateo 		if (gt_iir)
17493ff60f89SOscar Mateo 			I915_WRITE(GTIIR, gt_iir);
17503ff60f89SOscar Mateo 
1751c1874ed7SImre Deak 		pm_iir = I915_READ(GEN6_PMIIR);
17523ff60f89SOscar Mateo 		if (pm_iir)
17533ff60f89SOscar Mateo 			I915_WRITE(GEN6_PMIIR, pm_iir);
17543ff60f89SOscar Mateo 
17553ff60f89SOscar Mateo 		iir = I915_READ(VLV_IIR);
17563ff60f89SOscar Mateo 		if (iir) {
17573ff60f89SOscar Mateo 			/* Consume port before clearing IIR or we'll miss events */
17583ff60f89SOscar Mateo 			if (iir & I915_DISPLAY_PORT_INTERRUPT)
17593ff60f89SOscar Mateo 				i9xx_hpd_irq_handler(dev);
17603ff60f89SOscar Mateo 			I915_WRITE(VLV_IIR, iir);
17613ff60f89SOscar Mateo 		}
1762c1874ed7SImre Deak 
1763c1874ed7SImre Deak 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
1764c1874ed7SImre Deak 			goto out;
1765c1874ed7SImre Deak 
1766c1874ed7SImre Deak 		ret = IRQ_HANDLED;
1767c1874ed7SImre Deak 
17683ff60f89SOscar Mateo 		if (gt_iir)
1769c1874ed7SImre Deak 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
177060611c13SPaulo Zanoni 		if (pm_iir)
1771d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
17723ff60f89SOscar Mateo 		/* Call regardless, as some status bits might not be
17733ff60f89SOscar Mateo 		 * signalled in iir */
17743ff60f89SOscar Mateo 		valleyview_pipestat_irq_handler(dev, iir);
17757e231dbeSJesse Barnes 	}
17767e231dbeSJesse Barnes 
17777e231dbeSJesse Barnes out:
17787e231dbeSJesse Barnes 	return ret;
17797e231dbeSJesse Barnes }
17807e231dbeSJesse Barnes 
178143f328d7SVille Syrjälä static irqreturn_t cherryview_irq_handler(int irq, void *arg)
178243f328d7SVille Syrjälä {
178345a83f84SDaniel Vetter 	struct drm_device *dev = arg;
178443f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
178543f328d7SVille Syrjälä 	u32 master_ctl, iir;
178643f328d7SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
178743f328d7SVille Syrjälä 
17882dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
17892dd2a883SImre Deak 		return IRQ_NONE;
17902dd2a883SImre Deak 
17918e5fd599SVille Syrjälä 	for (;;) {
17928e5fd599SVille Syrjälä 		master_ctl = I915_READ(GEN8_MASTER_IRQ) & ~GEN8_MASTER_IRQ_CONTROL;
17933278f67fSVille Syrjälä 		iir = I915_READ(VLV_IIR);
17943278f67fSVille Syrjälä 
17953278f67fSVille Syrjälä 		if (master_ctl == 0 && iir == 0)
17968e5fd599SVille Syrjälä 			break;
179743f328d7SVille Syrjälä 
179827b6c122SOscar Mateo 		ret = IRQ_HANDLED;
179927b6c122SOscar Mateo 
180043f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, 0);
180143f328d7SVille Syrjälä 
180227b6c122SOscar Mateo 		/* Find, clear, then process each source of interrupt */
180327b6c122SOscar Mateo 
180427b6c122SOscar Mateo 		if (iir) {
180527b6c122SOscar Mateo 			/* Consume port before clearing IIR or we'll miss events */
180627b6c122SOscar Mateo 			if (iir & I915_DISPLAY_PORT_INTERRUPT)
180727b6c122SOscar Mateo 				i9xx_hpd_irq_handler(dev);
180827b6c122SOscar Mateo 			I915_WRITE(VLV_IIR, iir);
180927b6c122SOscar Mateo 		}
181027b6c122SOscar Mateo 
181174cdb337SChris Wilson 		gen8_gt_irq_handler(dev_priv, master_ctl);
181243f328d7SVille Syrjälä 
181327b6c122SOscar Mateo 		/* Call regardless, as some status bits might not be
181427b6c122SOscar Mateo 		 * signalled in iir */
18153278f67fSVille Syrjälä 		valleyview_pipestat_irq_handler(dev, iir);
181643f328d7SVille Syrjälä 
181743f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
181843f328d7SVille Syrjälä 		POSTING_READ(GEN8_MASTER_IRQ);
18198e5fd599SVille Syrjälä 	}
18203278f67fSVille Syrjälä 
182143f328d7SVille Syrjälä 	return ret;
182243f328d7SVille Syrjälä }
182343f328d7SVille Syrjälä 
182440e56410SVille Syrjälä static void ibx_hpd_irq_handler(struct drm_device *dev, u32 hotplug_trigger,
182540e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
1826776ad806SJesse Barnes {
182740e56410SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(dev);
182842db67d6SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
1829776ad806SJesse Barnes 
183013cf5504SDave Airlie 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
183113cf5504SDave Airlie 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
183213cf5504SDave Airlie 
1833fd63e2a9SImre Deak 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
183440e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
1835fd63e2a9SImre Deak 			   pch_port_hotplug_long_detect);
183640e56410SVille Syrjälä 
1837676574dfSJani Nikula 	intel_hpd_irq_handler(dev, pin_mask, long_mask);
1838aaf5ec2eSSonika Jindal }
183991d131d2SDaniel Vetter 
184040e56410SVille Syrjälä static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
184140e56410SVille Syrjälä {
184240e56410SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
184340e56410SVille Syrjälä 	int pipe;
184440e56410SVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
184540e56410SVille Syrjälä 
184640e56410SVille Syrjälä 	if (hotplug_trigger)
184740e56410SVille Syrjälä 		ibx_hpd_irq_handler(dev, hotplug_trigger, hpd_ibx);
184840e56410SVille Syrjälä 
1849cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1850cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1851776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1852cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1853cfc33bf7SVille Syrjälä 				 port_name(port));
1854cfc33bf7SVille Syrjälä 	}
1855776ad806SJesse Barnes 
1856ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
1857ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1858ce99c256SDaniel Vetter 
1859776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
1860515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
1861776ad806SJesse Barnes 
1862776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1863776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1864776ad806SJesse Barnes 
1865776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1866776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1867776ad806SJesse Barnes 
1868776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1869776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
1870776ad806SJesse Barnes 
18719db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
1872055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
18739db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
18749db4a9c7SJesse Barnes 					 pipe_name(pipe),
18759db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
1876776ad806SJesse Barnes 
1877776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1878776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1879776ad806SJesse Barnes 
1880776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1881776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1882776ad806SJesse Barnes 
1883776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
18841f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
18858664281bSPaulo Zanoni 
18868664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
18871f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
18888664281bSPaulo Zanoni }
18898664281bSPaulo Zanoni 
18908664281bSPaulo Zanoni static void ivb_err_int_handler(struct drm_device *dev)
18918664281bSPaulo Zanoni {
18928664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
18938664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
18945a69b89fSDaniel Vetter 	enum pipe pipe;
18958664281bSPaulo Zanoni 
1896de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
1897de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1898de032bf4SPaulo Zanoni 
1899055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
19001f7247c0SDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe))
19011f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
19028664281bSPaulo Zanoni 
19035a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
19045a69b89fSDaniel Vetter 			if (IS_IVYBRIDGE(dev))
1905277de95eSDaniel Vetter 				ivb_pipe_crc_irq_handler(dev, pipe);
19065a69b89fSDaniel Vetter 			else
1907277de95eSDaniel Vetter 				hsw_pipe_crc_irq_handler(dev, pipe);
19085a69b89fSDaniel Vetter 		}
19095a69b89fSDaniel Vetter 	}
19108bf1e9f1SShuang He 
19118664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
19128664281bSPaulo Zanoni }
19138664281bSPaulo Zanoni 
19148664281bSPaulo Zanoni static void cpt_serr_int_handler(struct drm_device *dev)
19158664281bSPaulo Zanoni {
19168664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
19178664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
19188664281bSPaulo Zanoni 
1919de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
1920de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
1921de032bf4SPaulo Zanoni 
19228664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
19231f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
19248664281bSPaulo Zanoni 
19258664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
19261f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
19278664281bSPaulo Zanoni 
19288664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
19291f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_C);
19308664281bSPaulo Zanoni 
19318664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
1932776ad806SJesse Barnes }
1933776ad806SJesse Barnes 
193423e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
193523e81d69SAdam Jackson {
19362d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
193723e81d69SAdam Jackson 	int pipe;
19386dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
1939aaf5ec2eSSonika Jindal 
194040e56410SVille Syrjälä 	if (hotplug_trigger)
194140e56410SVille Syrjälä 		ibx_hpd_irq_handler(dev, hotplug_trigger, hpd_cpt);
194291d131d2SDaniel Vetter 
1943cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
1944cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
194523e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
1946cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
1947cfc33bf7SVille Syrjälä 				 port_name(port));
1948cfc33bf7SVille Syrjälä 	}
194923e81d69SAdam Jackson 
195023e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
1951ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
195223e81d69SAdam Jackson 
195323e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
1954515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
195523e81d69SAdam Jackson 
195623e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
195723e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
195823e81d69SAdam Jackson 
195923e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
196023e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
196123e81d69SAdam Jackson 
196223e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
1963055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
196423e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
196523e81d69SAdam Jackson 					 pipe_name(pipe),
196623e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
19678664281bSPaulo Zanoni 
19688664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
19698664281bSPaulo Zanoni 		cpt_serr_int_handler(dev);
197023e81d69SAdam Jackson }
197123e81d69SAdam Jackson 
19726dbf30ceSVille Syrjälä static void spt_irq_handler(struct drm_device *dev, u32 pch_iir)
19736dbf30ceSVille Syrjälä {
19746dbf30ceSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
19756dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_SPT &
19766dbf30ceSVille Syrjälä 		~SDE_PORTE_HOTPLUG_SPT;
19776dbf30ceSVille Syrjälä 	u32 hotplug2_trigger = pch_iir & SDE_PORTE_HOTPLUG_SPT;
19786dbf30ceSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
19796dbf30ceSVille Syrjälä 
19806dbf30ceSVille Syrjälä 	if (hotplug_trigger) {
19816dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
19826dbf30ceSVille Syrjälä 
19836dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
19846dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
19856dbf30ceSVille Syrjälä 
19866dbf30ceSVille Syrjälä 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
19876dbf30ceSVille Syrjälä 				   dig_hotplug_reg, hpd_spt,
198874c0b395SVille Syrjälä 				   spt_port_hotplug_long_detect);
19896dbf30ceSVille Syrjälä 	}
19906dbf30ceSVille Syrjälä 
19916dbf30ceSVille Syrjälä 	if (hotplug2_trigger) {
19926dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
19936dbf30ceSVille Syrjälä 
19946dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG2);
19956dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG2, dig_hotplug_reg);
19966dbf30ceSVille Syrjälä 
19976dbf30ceSVille Syrjälä 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug2_trigger,
19986dbf30ceSVille Syrjälä 				   dig_hotplug_reg, hpd_spt,
19996dbf30ceSVille Syrjälä 				   spt_port_hotplug2_long_detect);
20006dbf30ceSVille Syrjälä 	}
20016dbf30ceSVille Syrjälä 
20026dbf30ceSVille Syrjälä 	if (pin_mask)
20036dbf30ceSVille Syrjälä 		intel_hpd_irq_handler(dev, pin_mask, long_mask);
20046dbf30ceSVille Syrjälä 
20056dbf30ceSVille Syrjälä 	if (pch_iir & SDE_GMBUS_CPT)
20066dbf30ceSVille Syrjälä 		gmbus_irq_handler(dev);
20076dbf30ceSVille Syrjälä }
20086dbf30ceSVille Syrjälä 
200940e56410SVille Syrjälä static void ilk_hpd_irq_handler(struct drm_device *dev, u32 hotplug_trigger,
201040e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2011c008bc6eSPaulo Zanoni {
201240e56410SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(dev);
2013e4ce95aaSVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2014e4ce95aaSVille Syrjälä 
2015e4ce95aaSVille Syrjälä 	dig_hotplug_reg = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
2016e4ce95aaSVille Syrjälä 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, dig_hotplug_reg);
2017e4ce95aaSVille Syrjälä 
2018e4ce95aaSVille Syrjälä 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
201940e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2020e4ce95aaSVille Syrjälä 			   ilk_port_hotplug_long_detect);
202140e56410SVille Syrjälä 
2022e4ce95aaSVille Syrjälä 	intel_hpd_irq_handler(dev, pin_mask, long_mask);
2023e4ce95aaSVille Syrjälä }
2024c008bc6eSPaulo Zanoni 
202540e56410SVille Syrjälä static void ilk_display_irq_handler(struct drm_device *dev, u32 de_iir)
202640e56410SVille Syrjälä {
202740e56410SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
202840e56410SVille Syrjälä 	enum pipe pipe;
202940e56410SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG;
203040e56410SVille Syrjälä 
203140e56410SVille Syrjälä 	if (hotplug_trigger)
203240e56410SVille Syrjälä 		ilk_hpd_irq_handler(dev, hotplug_trigger, hpd_ilk);
203340e56410SVille Syrjälä 
2034c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
2035c008bc6eSPaulo Zanoni 		dp_aux_irq_handler(dev);
2036c008bc6eSPaulo Zanoni 
2037c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
2038c008bc6eSPaulo Zanoni 		intel_opregion_asle_intr(dev);
2039c008bc6eSPaulo Zanoni 
2040c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
2041c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
2042c008bc6eSPaulo Zanoni 
2043055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2044d6bbafa1SChris Wilson 		if (de_iir & DE_PIPE_VBLANK(pipe) &&
2045d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
2046d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
2047c008bc6eSPaulo Zanoni 
204840da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
20491f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
2050c008bc6eSPaulo Zanoni 
205140da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
205240da17c2SDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
20535b3a856bSDaniel Vetter 
205440da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
205540da17c2SDaniel Vetter 		if (de_iir & DE_PLANE_FLIP_DONE(pipe)) {
205640da17c2SDaniel Vetter 			intel_prepare_page_flip(dev, pipe);
205740da17c2SDaniel Vetter 			intel_finish_page_flip_plane(dev, pipe);
2058c008bc6eSPaulo Zanoni 		}
2059c008bc6eSPaulo Zanoni 	}
2060c008bc6eSPaulo Zanoni 
2061c008bc6eSPaulo Zanoni 	/* check event from PCH */
2062c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
2063c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
2064c008bc6eSPaulo Zanoni 
2065c008bc6eSPaulo Zanoni 		if (HAS_PCH_CPT(dev))
2066c008bc6eSPaulo Zanoni 			cpt_irq_handler(dev, pch_iir);
2067c008bc6eSPaulo Zanoni 		else
2068c008bc6eSPaulo Zanoni 			ibx_irq_handler(dev, pch_iir);
2069c008bc6eSPaulo Zanoni 
2070c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
2071c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
2072c008bc6eSPaulo Zanoni 	}
2073c008bc6eSPaulo Zanoni 
2074c008bc6eSPaulo Zanoni 	if (IS_GEN5(dev) && de_iir & DE_PCU_EVENT)
2075c008bc6eSPaulo Zanoni 		ironlake_rps_change_irq_handler(dev);
2076c008bc6eSPaulo Zanoni }
2077c008bc6eSPaulo Zanoni 
20789719fb98SPaulo Zanoni static void ivb_display_irq_handler(struct drm_device *dev, u32 de_iir)
20799719fb98SPaulo Zanoni {
20809719fb98SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
208107d27e20SDamien Lespiau 	enum pipe pipe;
208223bb4cb5SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG_IVB;
208323bb4cb5SVille Syrjälä 
208440e56410SVille Syrjälä 	if (hotplug_trigger)
208540e56410SVille Syrjälä 		ilk_hpd_irq_handler(dev, hotplug_trigger, hpd_ivb);
20869719fb98SPaulo Zanoni 
20879719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
20889719fb98SPaulo Zanoni 		ivb_err_int_handler(dev);
20899719fb98SPaulo Zanoni 
20909719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
20919719fb98SPaulo Zanoni 		dp_aux_irq_handler(dev);
20929719fb98SPaulo Zanoni 
20939719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
20949719fb98SPaulo Zanoni 		intel_opregion_asle_intr(dev);
20959719fb98SPaulo Zanoni 
2096055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2097d6bbafa1SChris Wilson 		if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)) &&
2098d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
2099d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
210040da17c2SDaniel Vetter 
210140da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
210207d27e20SDamien Lespiau 		if (de_iir & DE_PLANE_FLIP_DONE_IVB(pipe)) {
210307d27e20SDamien Lespiau 			intel_prepare_page_flip(dev, pipe);
210407d27e20SDamien Lespiau 			intel_finish_page_flip_plane(dev, pipe);
21059719fb98SPaulo Zanoni 		}
21069719fb98SPaulo Zanoni 	}
21079719fb98SPaulo Zanoni 
21089719fb98SPaulo Zanoni 	/* check event from PCH */
21099719fb98SPaulo Zanoni 	if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
21109719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
21119719fb98SPaulo Zanoni 
21129719fb98SPaulo Zanoni 		cpt_irq_handler(dev, pch_iir);
21139719fb98SPaulo Zanoni 
21149719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
21159719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
21169719fb98SPaulo Zanoni 	}
21179719fb98SPaulo Zanoni }
21189719fb98SPaulo Zanoni 
211972c90f62SOscar Mateo /*
212072c90f62SOscar Mateo  * To handle irqs with the minimum potential races with fresh interrupts, we:
212172c90f62SOscar Mateo  * 1 - Disable Master Interrupt Control.
212272c90f62SOscar Mateo  * 2 - Find the source(s) of the interrupt.
212372c90f62SOscar Mateo  * 3 - Clear the Interrupt Identity bits (IIR).
212472c90f62SOscar Mateo  * 4 - Process the interrupt(s) that had bits set in the IIRs.
212572c90f62SOscar Mateo  * 5 - Re-enable Master Interrupt Control.
212672c90f62SOscar Mateo  */
2127f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
2128b1f14ad0SJesse Barnes {
212945a83f84SDaniel Vetter 	struct drm_device *dev = arg;
21302d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2131f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
21320e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
2133b1f14ad0SJesse Barnes 
21342dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
21352dd2a883SImre Deak 		return IRQ_NONE;
21362dd2a883SImre Deak 
21378664281bSPaulo Zanoni 	/* We get interrupts on unclaimed registers, so check for this before we
21388664281bSPaulo Zanoni 	 * do any I915_{READ,WRITE}. */
2139907b28c5SChris Wilson 	intel_uncore_check_errors(dev);
21408664281bSPaulo Zanoni 
2141b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
2142b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
2143b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
214423a78516SPaulo Zanoni 	POSTING_READ(DEIER);
21450e43406bSChris Wilson 
214644498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
214744498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
214844498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
214944498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
215044498aeaSPaulo Zanoni 	 * due to its back queue). */
2151ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
215244498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
215344498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
215444498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2155ab5c608bSBen Widawsky 	}
215644498aeaSPaulo Zanoni 
215772c90f62SOscar Mateo 	/* Find, clear, then process each source of interrupt */
215872c90f62SOscar Mateo 
21590e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
21600e43406bSChris Wilson 	if (gt_iir) {
216172c90f62SOscar Mateo 		I915_WRITE(GTIIR, gt_iir);
216272c90f62SOscar Mateo 		ret = IRQ_HANDLED;
2163d8fc8a47SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 6)
21640e43406bSChris Wilson 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
2165d8fc8a47SPaulo Zanoni 		else
2166d8fc8a47SPaulo Zanoni 			ilk_gt_irq_handler(dev, dev_priv, gt_iir);
21670e43406bSChris Wilson 	}
2168b1f14ad0SJesse Barnes 
2169b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
21700e43406bSChris Wilson 	if (de_iir) {
217172c90f62SOscar Mateo 		I915_WRITE(DEIIR, de_iir);
217272c90f62SOscar Mateo 		ret = IRQ_HANDLED;
2173f1af8fc1SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 7)
21749719fb98SPaulo Zanoni 			ivb_display_irq_handler(dev, de_iir);
2175f1af8fc1SPaulo Zanoni 		else
2176f1af8fc1SPaulo Zanoni 			ilk_display_irq_handler(dev, de_iir);
21770e43406bSChris Wilson 	}
21780e43406bSChris Wilson 
2179f1af8fc1SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6) {
2180f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
21810e43406bSChris Wilson 		if (pm_iir) {
2182b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
21830e43406bSChris Wilson 			ret = IRQ_HANDLED;
218472c90f62SOscar Mateo 			gen6_rps_irq_handler(dev_priv, pm_iir);
21850e43406bSChris Wilson 		}
2186f1af8fc1SPaulo Zanoni 	}
2187b1f14ad0SJesse Barnes 
2188b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
2189b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
2190ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
219144498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
219244498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2193ab5c608bSBen Widawsky 	}
2194b1f14ad0SJesse Barnes 
2195b1f14ad0SJesse Barnes 	return ret;
2196b1f14ad0SJesse Barnes }
2197b1f14ad0SJesse Barnes 
219840e56410SVille Syrjälä static void bxt_hpd_irq_handler(struct drm_device *dev, u32 hotplug_trigger,
219940e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2200d04a492dSShashank Sharma {
2201cebd87a0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(dev);
2202cebd87a0SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2203d04a492dSShashank Sharma 
2204a52bb15bSVille Syrjälä 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
2205a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
2206d04a492dSShashank Sharma 
2207cebd87a0SVille Syrjälä 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
220840e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2209cebd87a0SVille Syrjälä 			   bxt_port_hotplug_long_detect);
221040e56410SVille Syrjälä 
2211475c2e3bSJani Nikula 	intel_hpd_irq_handler(dev, pin_mask, long_mask);
2212d04a492dSShashank Sharma }
2213d04a492dSShashank Sharma 
2214abd58f01SBen Widawsky static irqreturn_t gen8_irq_handler(int irq, void *arg)
2215abd58f01SBen Widawsky {
2216abd58f01SBen Widawsky 	struct drm_device *dev = arg;
2217abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2218abd58f01SBen Widawsky 	u32 master_ctl;
2219abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
2220abd58f01SBen Widawsky 	uint32_t tmp = 0;
2221c42664ccSDaniel Vetter 	enum pipe pipe;
222288e04703SJesse Barnes 	u32 aux_mask = GEN8_AUX_CHANNEL_A;
222388e04703SJesse Barnes 
22242dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
22252dd2a883SImre Deak 		return IRQ_NONE;
22262dd2a883SImre Deak 
2227b4834a50SRodrigo Vivi 	if (INTEL_INFO(dev_priv)->gen >= 9)
222888e04703SJesse Barnes 		aux_mask |=  GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
222988e04703SJesse Barnes 			GEN9_AUX_CHANNEL_D;
2230abd58f01SBen Widawsky 
2231cb0d205eSChris Wilson 	master_ctl = I915_READ_FW(GEN8_MASTER_IRQ);
2232abd58f01SBen Widawsky 	master_ctl &= ~GEN8_MASTER_IRQ_CONTROL;
2233abd58f01SBen Widawsky 	if (!master_ctl)
2234abd58f01SBen Widawsky 		return IRQ_NONE;
2235abd58f01SBen Widawsky 
2236cb0d205eSChris Wilson 	I915_WRITE_FW(GEN8_MASTER_IRQ, 0);
2237abd58f01SBen Widawsky 
223838cc46d7SOscar Mateo 	/* Find, clear, then process each source of interrupt */
223938cc46d7SOscar Mateo 
224074cdb337SChris Wilson 	ret = gen8_gt_irq_handler(dev_priv, master_ctl);
2241abd58f01SBen Widawsky 
2242abd58f01SBen Widawsky 	if (master_ctl & GEN8_DE_MISC_IRQ) {
2243abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_DE_MISC_IIR);
2244abd58f01SBen Widawsky 		if (tmp) {
2245abd58f01SBen Widawsky 			I915_WRITE(GEN8_DE_MISC_IIR, tmp);
2246abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
224738cc46d7SOscar Mateo 			if (tmp & GEN8_DE_MISC_GSE)
224838cc46d7SOscar Mateo 				intel_opregion_asle_intr(dev);
224938cc46d7SOscar Mateo 			else
225038cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Misc interrupt\n");
2251abd58f01SBen Widawsky 		}
225238cc46d7SOscar Mateo 		else
225338cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
2254abd58f01SBen Widawsky 	}
2255abd58f01SBen Widawsky 
22566d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
22576d766f02SDaniel Vetter 		tmp = I915_READ(GEN8_DE_PORT_IIR);
22586d766f02SDaniel Vetter 		if (tmp) {
2259d04a492dSShashank Sharma 			bool found = false;
2260cebd87a0SVille Syrjälä 			u32 hotplug_trigger = 0;
2261cebd87a0SVille Syrjälä 
2262cebd87a0SVille Syrjälä 			if (IS_BROXTON(dev_priv))
2263cebd87a0SVille Syrjälä 				hotplug_trigger = tmp & BXT_DE_PORT_HOTPLUG_MASK;
2264cebd87a0SVille Syrjälä 			else if (IS_BROADWELL(dev_priv))
2265cebd87a0SVille Syrjälä 				hotplug_trigger = tmp & GEN8_PORT_DP_A_HOTPLUG;
2266d04a492dSShashank Sharma 
22676d766f02SDaniel Vetter 			I915_WRITE(GEN8_DE_PORT_IIR, tmp);
22686d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
226988e04703SJesse Barnes 
2270d04a492dSShashank Sharma 			if (tmp & aux_mask) {
227138cc46d7SOscar Mateo 				dp_aux_irq_handler(dev);
2272d04a492dSShashank Sharma 				found = true;
2273d04a492dSShashank Sharma 			}
2274d04a492dSShashank Sharma 
227540e56410SVille Syrjälä 			if (hotplug_trigger) {
227640e56410SVille Syrjälä 				if (IS_BROXTON(dev))
227740e56410SVille Syrjälä 					bxt_hpd_irq_handler(dev, hotplug_trigger, hpd_bxt);
227840e56410SVille Syrjälä 				else
227940e56410SVille Syrjälä 					ilk_hpd_irq_handler(dev, hotplug_trigger, hpd_bdw);
2280d04a492dSShashank Sharma 				found = true;
2281d04a492dSShashank Sharma 			}
2282d04a492dSShashank Sharma 
22839e63743eSShashank Sharma 			if (IS_BROXTON(dev) && (tmp & BXT_DE_PORT_GMBUS)) {
22849e63743eSShashank Sharma 				gmbus_irq_handler(dev);
22859e63743eSShashank Sharma 				found = true;
22869e63743eSShashank Sharma 			}
22879e63743eSShashank Sharma 
2288d04a492dSShashank Sharma 			if (!found)
228938cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Port interrupt\n");
22906d766f02SDaniel Vetter 		}
229138cc46d7SOscar Mateo 		else
229238cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
22936d766f02SDaniel Vetter 	}
22946d766f02SDaniel Vetter 
2295055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2296770de83dSDamien Lespiau 		uint32_t pipe_iir, flip_done = 0, fault_errors = 0;
2297abd58f01SBen Widawsky 
2298c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2299c42664ccSDaniel Vetter 			continue;
2300c42664ccSDaniel Vetter 
2301abd58f01SBen Widawsky 		pipe_iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
230238cc46d7SOscar Mateo 		if (pipe_iir) {
230338cc46d7SOscar Mateo 			ret = IRQ_HANDLED;
230438cc46d7SOscar Mateo 			I915_WRITE(GEN8_DE_PIPE_IIR(pipe), pipe_iir);
2305770de83dSDamien Lespiau 
2306d6bbafa1SChris Wilson 			if (pipe_iir & GEN8_PIPE_VBLANK &&
2307d6bbafa1SChris Wilson 			    intel_pipe_handle_vblank(dev, pipe))
2308d6bbafa1SChris Wilson 				intel_check_page_flip(dev, pipe);
2309abd58f01SBen Widawsky 
2310b4834a50SRodrigo Vivi 			if (INTEL_INFO(dev_priv)->gen >= 9)
2311770de83dSDamien Lespiau 				flip_done = pipe_iir & GEN9_PIPE_PLANE1_FLIP_DONE;
2312770de83dSDamien Lespiau 			else
2313770de83dSDamien Lespiau 				flip_done = pipe_iir & GEN8_PIPE_PRIMARY_FLIP_DONE;
2314770de83dSDamien Lespiau 
2315770de83dSDamien Lespiau 			if (flip_done) {
2316abd58f01SBen Widawsky 				intel_prepare_page_flip(dev, pipe);
2317abd58f01SBen Widawsky 				intel_finish_page_flip_plane(dev, pipe);
2318abd58f01SBen Widawsky 			}
2319abd58f01SBen Widawsky 
23200fbe7870SDaniel Vetter 			if (pipe_iir & GEN8_PIPE_CDCLK_CRC_DONE)
23210fbe7870SDaniel Vetter 				hsw_pipe_crc_irq_handler(dev, pipe);
23220fbe7870SDaniel Vetter 
23231f7247c0SDaniel Vetter 			if (pipe_iir & GEN8_PIPE_FIFO_UNDERRUN)
23241f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
23251f7247c0SDaniel Vetter 								    pipe);
232638d83c96SDaniel Vetter 
2327770de83dSDamien Lespiau 
2328b4834a50SRodrigo Vivi 			if (INTEL_INFO(dev_priv)->gen >= 9)
2329770de83dSDamien Lespiau 				fault_errors = pipe_iir & GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
2330770de83dSDamien Lespiau 			else
2331770de83dSDamien Lespiau 				fault_errors = pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
2332770de83dSDamien Lespiau 
2333770de83dSDamien Lespiau 			if (fault_errors)
233430100f2bSDaniel Vetter 				DRM_ERROR("Fault errors on pipe %c\n: 0x%08x",
233530100f2bSDaniel Vetter 					  pipe_name(pipe),
233630100f2bSDaniel Vetter 					  pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS);
2337c42664ccSDaniel Vetter 		} else
2338abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
2339abd58f01SBen Widawsky 	}
2340abd58f01SBen Widawsky 
2341266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev) && !HAS_PCH_NOP(dev) &&
2342266ea3d9SShashank Sharma 	    master_ctl & GEN8_DE_PCH_IRQ) {
234392d03a80SDaniel Vetter 		/*
234492d03a80SDaniel Vetter 		 * FIXME(BDW): Assume for now that the new interrupt handling
234592d03a80SDaniel Vetter 		 * scheme also closed the SDE interrupt handling race we've seen
234692d03a80SDaniel Vetter 		 * on older pch-split platforms. But this needs testing.
234792d03a80SDaniel Vetter 		 */
234892d03a80SDaniel Vetter 		u32 pch_iir = I915_READ(SDEIIR);
234992d03a80SDaniel Vetter 		if (pch_iir) {
235092d03a80SDaniel Vetter 			I915_WRITE(SDEIIR, pch_iir);
235192d03a80SDaniel Vetter 			ret = IRQ_HANDLED;
23526dbf30ceSVille Syrjälä 
23536dbf30ceSVille Syrjälä 			if (HAS_PCH_SPT(dev_priv))
23546dbf30ceSVille Syrjälä 				spt_irq_handler(dev, pch_iir);
23556dbf30ceSVille Syrjälä 			else
235638cc46d7SOscar Mateo 				cpt_irq_handler(dev, pch_iir);
235738cc46d7SOscar Mateo 		} else
235838cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (SDE)!\n");
235938cc46d7SOscar Mateo 
236092d03a80SDaniel Vetter 	}
236192d03a80SDaniel Vetter 
2362cb0d205eSChris Wilson 	I915_WRITE_FW(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
2363cb0d205eSChris Wilson 	POSTING_READ_FW(GEN8_MASTER_IRQ);
2364abd58f01SBen Widawsky 
2365abd58f01SBen Widawsky 	return ret;
2366abd58f01SBen Widawsky }
2367abd58f01SBen Widawsky 
236817e1df07SDaniel Vetter static void i915_error_wake_up(struct drm_i915_private *dev_priv,
236917e1df07SDaniel Vetter 			       bool reset_completed)
237017e1df07SDaniel Vetter {
2371a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
237217e1df07SDaniel Vetter 	int i;
237317e1df07SDaniel Vetter 
237417e1df07SDaniel Vetter 	/*
237517e1df07SDaniel Vetter 	 * Notify all waiters for GPU completion events that reset state has
237617e1df07SDaniel Vetter 	 * been changed, and that they need to restart their wait after
237717e1df07SDaniel Vetter 	 * checking for potential errors (and bail out to drop locks if there is
237817e1df07SDaniel Vetter 	 * a gpu reset pending so that i915_error_work_func can acquire them).
237917e1df07SDaniel Vetter 	 */
238017e1df07SDaniel Vetter 
238117e1df07SDaniel Vetter 	/* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
238217e1df07SDaniel Vetter 	for_each_ring(ring, dev_priv, i)
238317e1df07SDaniel Vetter 		wake_up_all(&ring->irq_queue);
238417e1df07SDaniel Vetter 
238517e1df07SDaniel Vetter 	/* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
238617e1df07SDaniel Vetter 	wake_up_all(&dev_priv->pending_flip_queue);
238717e1df07SDaniel Vetter 
238817e1df07SDaniel Vetter 	/*
238917e1df07SDaniel Vetter 	 * Signal tasks blocked in i915_gem_wait_for_error that the pending
239017e1df07SDaniel Vetter 	 * reset state is cleared.
239117e1df07SDaniel Vetter 	 */
239217e1df07SDaniel Vetter 	if (reset_completed)
239317e1df07SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
239417e1df07SDaniel Vetter }
239517e1df07SDaniel Vetter 
23968a905236SJesse Barnes /**
2397b8d24a06SMika Kuoppala  * i915_reset_and_wakeup - do process context error handling work
2398468f9d29SJavier Martinez Canillas  * @dev: drm device
23998a905236SJesse Barnes  *
24008a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
24018a905236SJesse Barnes  * was detected.
24028a905236SJesse Barnes  */
2403b8d24a06SMika Kuoppala static void i915_reset_and_wakeup(struct drm_device *dev)
24048a905236SJesse Barnes {
2405b8d24a06SMika Kuoppala 	struct drm_i915_private *dev_priv = to_i915(dev);
2406b8d24a06SMika Kuoppala 	struct i915_gpu_error *error = &dev_priv->gpu_error;
2407cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
2408cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
2409cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
241017e1df07SDaniel Vetter 	int ret;
24118a905236SJesse Barnes 
24125bdebb18SDave Airlie 	kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, error_event);
24138a905236SJesse Barnes 
24147db0ba24SDaniel Vetter 	/*
24157db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
24167db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
24177db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
24187db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
24197db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
24207db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
24217db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
24227db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
24237db0ba24SDaniel Vetter 	 */
24247db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
242544d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
24265bdebb18SDave Airlie 		kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE,
24277db0ba24SDaniel Vetter 				   reset_event);
24281f83fee0SDaniel Vetter 
242917e1df07SDaniel Vetter 		/*
2430f454c694SImre Deak 		 * In most cases it's guaranteed that we get here with an RPM
2431f454c694SImre Deak 		 * reference held, for example because there is a pending GPU
2432f454c694SImre Deak 		 * request that won't finish until the reset is done. This
2433f454c694SImre Deak 		 * isn't the case at least when we get here by doing a
2434f454c694SImre Deak 		 * simulated reset via debugs, so get an RPM reference.
2435f454c694SImre Deak 		 */
2436f454c694SImre Deak 		intel_runtime_pm_get(dev_priv);
24377514747dSVille Syrjälä 
24387514747dSVille Syrjälä 		intel_prepare_reset(dev);
24397514747dSVille Syrjälä 
2440f454c694SImre Deak 		/*
244117e1df07SDaniel Vetter 		 * All state reset _must_ be completed before we update the
244217e1df07SDaniel Vetter 		 * reset counter, for otherwise waiters might miss the reset
244317e1df07SDaniel Vetter 		 * pending state and not properly drop locks, resulting in
244417e1df07SDaniel Vetter 		 * deadlocks with the reset work.
244517e1df07SDaniel Vetter 		 */
2446f69061beSDaniel Vetter 		ret = i915_reset(dev);
2447f69061beSDaniel Vetter 
24487514747dSVille Syrjälä 		intel_finish_reset(dev);
244917e1df07SDaniel Vetter 
2450f454c694SImre Deak 		intel_runtime_pm_put(dev_priv);
2451f454c694SImre Deak 
2452f69061beSDaniel Vetter 		if (ret == 0) {
2453f69061beSDaniel Vetter 			/*
2454f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
2455f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
2456f69061beSDaniel Vetter 			 * complete.
2457f69061beSDaniel Vetter 			 *
2458f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
2459f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
2460f69061beSDaniel Vetter 			 * updates before
2461f69061beSDaniel Vetter 			 * the counter increment.
2462f69061beSDaniel Vetter 			 */
24634e857c58SPeter Zijlstra 			smp_mb__before_atomic();
2464f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
2465f69061beSDaniel Vetter 
24665bdebb18SDave Airlie 			kobject_uevent_env(&dev->primary->kdev->kobj,
2467f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
24681f83fee0SDaniel Vetter 		} else {
2469805de8f4SPeter Zijlstra 			atomic_or(I915_WEDGED, &error->reset_counter);
2470f316a42cSBen Gamari 		}
24711f83fee0SDaniel Vetter 
247217e1df07SDaniel Vetter 		/*
247317e1df07SDaniel Vetter 		 * Note: The wake_up also serves as a memory barrier so that
247417e1df07SDaniel Vetter 		 * waiters see the update value of the reset counter atomic_t.
247517e1df07SDaniel Vetter 		 */
247617e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, true);
2477f316a42cSBen Gamari 	}
24788a905236SJesse Barnes }
24798a905236SJesse Barnes 
248035aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
2481c0e09200SDave Airlie {
24828a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
2483bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
248463eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
2485050ee91fSBen Widawsky 	int pipe, i;
248663eeaf38SJesse Barnes 
248735aed2e6SChris Wilson 	if (!eir)
248835aed2e6SChris Wilson 		return;
248963eeaf38SJesse Barnes 
2490a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
24918a905236SJesse Barnes 
2492bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
2493bd9854f9SBen Widawsky 
24948a905236SJesse Barnes 	if (IS_G4X(dev)) {
24958a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
24968a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
24978a905236SJesse Barnes 
2498a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2499a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2500050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
2501050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2502a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2503a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
25048a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
25053143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
25068a905236SJesse Barnes 		}
25078a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
25088a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2509a70491ccSJoe Perches 			pr_err("page table error\n");
2510a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
25118a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
25123143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
25138a905236SJesse Barnes 		}
25148a905236SJesse Barnes 	}
25158a905236SJesse Barnes 
2516a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
251763eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
251863eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2519a70491ccSJoe Perches 			pr_err("page table error\n");
2520a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
252163eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
25223143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
252363eeaf38SJesse Barnes 		}
25248a905236SJesse Barnes 	}
25258a905236SJesse Barnes 
252663eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
2527a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
2528055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
2529a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
25309db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
253163eeaf38SJesse Barnes 		/* pipestat has already been acked */
253263eeaf38SJesse Barnes 	}
253363eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
2534a70491ccSJoe Perches 		pr_err("instruction error\n");
2535a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
2536050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
2537050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2538a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
253963eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
254063eeaf38SJesse Barnes 
2541a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
2542a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
2543a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
254463eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
25453143a2bfSChris Wilson 			POSTING_READ(IPEIR);
254663eeaf38SJesse Barnes 		} else {
254763eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
254863eeaf38SJesse Barnes 
2549a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2550a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2551a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2552a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
255363eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
25543143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
255563eeaf38SJesse Barnes 		}
255663eeaf38SJesse Barnes 	}
255763eeaf38SJesse Barnes 
255863eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
25593143a2bfSChris Wilson 	POSTING_READ(EIR);
256063eeaf38SJesse Barnes 	eir = I915_READ(EIR);
256163eeaf38SJesse Barnes 	if (eir) {
256263eeaf38SJesse Barnes 		/*
256363eeaf38SJesse Barnes 		 * some errors might have become stuck,
256463eeaf38SJesse Barnes 		 * mask them.
256563eeaf38SJesse Barnes 		 */
256663eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
256763eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
256863eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
256963eeaf38SJesse Barnes 	}
257035aed2e6SChris Wilson }
257135aed2e6SChris Wilson 
257235aed2e6SChris Wilson /**
2573b8d24a06SMika Kuoppala  * i915_handle_error - handle a gpu error
257435aed2e6SChris Wilson  * @dev: drm device
257535aed2e6SChris Wilson  *
2576aafd8581SJavier Martinez Canillas  * Do some basic checking of register state at error time and
257735aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
257835aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
257935aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
258035aed2e6SChris Wilson  * of a ring dump etc.).
258135aed2e6SChris Wilson  */
258258174462SMika Kuoppala void i915_handle_error(struct drm_device *dev, bool wedged,
258358174462SMika Kuoppala 		       const char *fmt, ...)
258435aed2e6SChris Wilson {
258535aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
258658174462SMika Kuoppala 	va_list args;
258758174462SMika Kuoppala 	char error_msg[80];
258835aed2e6SChris Wilson 
258958174462SMika Kuoppala 	va_start(args, fmt);
259058174462SMika Kuoppala 	vscnprintf(error_msg, sizeof(error_msg), fmt, args);
259158174462SMika Kuoppala 	va_end(args);
259258174462SMika Kuoppala 
259358174462SMika Kuoppala 	i915_capture_error_state(dev, wedged, error_msg);
259435aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
25958a905236SJesse Barnes 
2596ba1234d1SBen Gamari 	if (wedged) {
2597805de8f4SPeter Zijlstra 		atomic_or(I915_RESET_IN_PROGRESS_FLAG,
2598f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
2599ba1234d1SBen Gamari 
260011ed50ecSBen Gamari 		/*
2601b8d24a06SMika Kuoppala 		 * Wakeup waiting processes so that the reset function
2602b8d24a06SMika Kuoppala 		 * i915_reset_and_wakeup doesn't deadlock trying to grab
2603b8d24a06SMika Kuoppala 		 * various locks. By bumping the reset counter first, the woken
260417e1df07SDaniel Vetter 		 * processes will see a reset in progress and back off,
260517e1df07SDaniel Vetter 		 * releasing their locks and then wait for the reset completion.
260617e1df07SDaniel Vetter 		 * We must do this for _all_ gpu waiters that might hold locks
260717e1df07SDaniel Vetter 		 * that the reset work needs to acquire.
260817e1df07SDaniel Vetter 		 *
260917e1df07SDaniel Vetter 		 * Note: The wake_up serves as the required memory barrier to
261017e1df07SDaniel Vetter 		 * ensure that the waiters see the updated value of the reset
261117e1df07SDaniel Vetter 		 * counter atomic_t.
261211ed50ecSBen Gamari 		 */
261317e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, false);
261411ed50ecSBen Gamari 	}
261511ed50ecSBen Gamari 
2616b8d24a06SMika Kuoppala 	i915_reset_and_wakeup(dev);
26178a905236SJesse Barnes }
26188a905236SJesse Barnes 
261942f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
262042f52ef8SKeith Packard  * we use as a pipe index
262142f52ef8SKeith Packard  */
2622f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
26230a3e67a4SJesse Barnes {
26242d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2625e9d21d7fSKeith Packard 	unsigned long irqflags;
262671e0ffa5SJesse Barnes 
26271ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2628f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
26297c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2630755e9019SImre Deak 				     PIPE_START_VBLANK_INTERRUPT_STATUS);
26310a3e67a4SJesse Barnes 	else
26327c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2633755e9019SImre Deak 				     PIPE_VBLANK_INTERRUPT_STATUS);
26341ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26358692d00eSChris Wilson 
26360a3e67a4SJesse Barnes 	return 0;
26370a3e67a4SJesse Barnes }
26380a3e67a4SJesse Barnes 
2639f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
2640f796cf8fSJesse Barnes {
26412d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2642f796cf8fSJesse Barnes 	unsigned long irqflags;
2643b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
264440da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2645f796cf8fSJesse Barnes 
2646f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2647b518421fSPaulo Zanoni 	ironlake_enable_display_irq(dev_priv, bit);
2648b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2649b1f14ad0SJesse Barnes 
2650b1f14ad0SJesse Barnes 	return 0;
2651b1f14ad0SJesse Barnes }
2652b1f14ad0SJesse Barnes 
26537e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
26547e231dbeSJesse Barnes {
26552d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
26567e231dbeSJesse Barnes 	unsigned long irqflags;
26577e231dbeSJesse Barnes 
26587e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
265931acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
2660755e9019SImre Deak 			     PIPE_START_VBLANK_INTERRUPT_STATUS);
26617e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26627e231dbeSJesse Barnes 
26637e231dbeSJesse Barnes 	return 0;
26647e231dbeSJesse Barnes }
26657e231dbeSJesse Barnes 
2666abd58f01SBen Widawsky static int gen8_enable_vblank(struct drm_device *dev, int pipe)
2667abd58f01SBen Widawsky {
2668abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2669abd58f01SBen Widawsky 	unsigned long irqflags;
2670abd58f01SBen Widawsky 
2671abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
26727167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] &= ~GEN8_PIPE_VBLANK;
26737167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2674abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2675abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2676abd58f01SBen Widawsky 	return 0;
2677abd58f01SBen Widawsky }
2678abd58f01SBen Widawsky 
267942f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
268042f52ef8SKeith Packard  * we use as a pipe index
268142f52ef8SKeith Packard  */
2682f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
26830a3e67a4SJesse Barnes {
26842d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2685e9d21d7fSKeith Packard 	unsigned long irqflags;
26860a3e67a4SJesse Barnes 
26871ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
26887c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
2689755e9019SImre Deak 			      PIPE_VBLANK_INTERRUPT_STATUS |
2690755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
26911ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26920a3e67a4SJesse Barnes }
26930a3e67a4SJesse Barnes 
2694f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
2695f796cf8fSJesse Barnes {
26962d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2697f796cf8fSJesse Barnes 	unsigned long irqflags;
2698b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
269940da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2700f796cf8fSJesse Barnes 
2701f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2702b518421fSPaulo Zanoni 	ironlake_disable_display_irq(dev_priv, bit);
2703b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2704b1f14ad0SJesse Barnes }
2705b1f14ad0SJesse Barnes 
27067e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
27077e231dbeSJesse Barnes {
27082d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
27097e231dbeSJesse Barnes 	unsigned long irqflags;
27107e231dbeSJesse Barnes 
27117e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
271231acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
2713755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
27147e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
27157e231dbeSJesse Barnes }
27167e231dbeSJesse Barnes 
2717abd58f01SBen Widawsky static void gen8_disable_vblank(struct drm_device *dev, int pipe)
2718abd58f01SBen Widawsky {
2719abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2720abd58f01SBen Widawsky 	unsigned long irqflags;
2721abd58f01SBen Widawsky 
2722abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
27237167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] |= GEN8_PIPE_VBLANK;
27247167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2725abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2726abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2727abd58f01SBen Widawsky }
2728abd58f01SBen Widawsky 
27299107e9d2SChris Wilson static bool
273094f7bbe1STomas Elf ring_idle(struct intel_engine_cs *ring, u32 seqno)
2731893eead0SChris Wilson {
27329107e9d2SChris Wilson 	return (list_empty(&ring->request_list) ||
273394f7bbe1STomas Elf 		i915_seqno_passed(seqno, ring->last_submitted_seqno));
2734f65d9421SBen Gamari }
2735f65d9421SBen Gamari 
2736a028c4b0SDaniel Vetter static bool
2737a028c4b0SDaniel Vetter ipehr_is_semaphore_wait(struct drm_device *dev, u32 ipehr)
2738a028c4b0SDaniel Vetter {
2739a028c4b0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 8) {
2740a6cdb93aSRodrigo Vivi 		return (ipehr >> 23) == 0x1c;
2741a028c4b0SDaniel Vetter 	} else {
2742a028c4b0SDaniel Vetter 		ipehr &= ~MI_SEMAPHORE_SYNC_MASK;
2743a028c4b0SDaniel Vetter 		return ipehr == (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE |
2744a028c4b0SDaniel Vetter 				 MI_SEMAPHORE_REGISTER);
2745a028c4b0SDaniel Vetter 	}
2746a028c4b0SDaniel Vetter }
2747a028c4b0SDaniel Vetter 
2748a4872ba6SOscar Mateo static struct intel_engine_cs *
2749a6cdb93aSRodrigo Vivi semaphore_wait_to_signaller_ring(struct intel_engine_cs *ring, u32 ipehr, u64 offset)
2750921d42eaSDaniel Vetter {
2751921d42eaSDaniel Vetter 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2752a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2753921d42eaSDaniel Vetter 	int i;
2754921d42eaSDaniel Vetter 
2755921d42eaSDaniel Vetter 	if (INTEL_INFO(dev_priv->dev)->gen >= 8) {
2756a6cdb93aSRodrigo Vivi 		for_each_ring(signaller, dev_priv, i) {
2757a6cdb93aSRodrigo Vivi 			if (ring == signaller)
2758a6cdb93aSRodrigo Vivi 				continue;
2759a6cdb93aSRodrigo Vivi 
2760a6cdb93aSRodrigo Vivi 			if (offset == signaller->semaphore.signal_ggtt[ring->id])
2761a6cdb93aSRodrigo Vivi 				return signaller;
2762a6cdb93aSRodrigo Vivi 		}
2763921d42eaSDaniel Vetter 	} else {
2764921d42eaSDaniel Vetter 		u32 sync_bits = ipehr & MI_SEMAPHORE_SYNC_MASK;
2765921d42eaSDaniel Vetter 
2766921d42eaSDaniel Vetter 		for_each_ring(signaller, dev_priv, i) {
2767921d42eaSDaniel Vetter 			if(ring == signaller)
2768921d42eaSDaniel Vetter 				continue;
2769921d42eaSDaniel Vetter 
2770ebc348b2SBen Widawsky 			if (sync_bits == signaller->semaphore.mbox.wait[ring->id])
2771921d42eaSDaniel Vetter 				return signaller;
2772921d42eaSDaniel Vetter 		}
2773921d42eaSDaniel Vetter 	}
2774921d42eaSDaniel Vetter 
2775a6cdb93aSRodrigo Vivi 	DRM_ERROR("No signaller ring found for ring %i, ipehr 0x%08x, offset 0x%016llx\n",
2776a6cdb93aSRodrigo Vivi 		  ring->id, ipehr, offset);
2777921d42eaSDaniel Vetter 
2778921d42eaSDaniel Vetter 	return NULL;
2779921d42eaSDaniel Vetter }
2780921d42eaSDaniel Vetter 
2781a4872ba6SOscar Mateo static struct intel_engine_cs *
2782a4872ba6SOscar Mateo semaphore_waits_for(struct intel_engine_cs *ring, u32 *seqno)
2783a24a11e6SChris Wilson {
2784a24a11e6SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
278588fe429dSDaniel Vetter 	u32 cmd, ipehr, head;
2786a6cdb93aSRodrigo Vivi 	u64 offset = 0;
2787a6cdb93aSRodrigo Vivi 	int i, backwards;
2788a24a11e6SChris Wilson 
2789381e8ae3STomas Elf 	/*
2790381e8ae3STomas Elf 	 * This function does not support execlist mode - any attempt to
2791381e8ae3STomas Elf 	 * proceed further into this function will result in a kernel panic
2792381e8ae3STomas Elf 	 * when dereferencing ring->buffer, which is not set up in execlist
2793381e8ae3STomas Elf 	 * mode.
2794381e8ae3STomas Elf 	 *
2795381e8ae3STomas Elf 	 * The correct way of doing it would be to derive the currently
2796381e8ae3STomas Elf 	 * executing ring buffer from the current context, which is derived
2797381e8ae3STomas Elf 	 * from the currently running request. Unfortunately, to get the
2798381e8ae3STomas Elf 	 * current request we would have to grab the struct_mutex before doing
2799381e8ae3STomas Elf 	 * anything else, which would be ill-advised since some other thread
2800381e8ae3STomas Elf 	 * might have grabbed it already and managed to hang itself, causing
2801381e8ae3STomas Elf 	 * the hang checker to deadlock.
2802381e8ae3STomas Elf 	 *
2803381e8ae3STomas Elf 	 * Therefore, this function does not support execlist mode in its
2804381e8ae3STomas Elf 	 * current form. Just return NULL and move on.
2805381e8ae3STomas Elf 	 */
2806381e8ae3STomas Elf 	if (ring->buffer == NULL)
2807381e8ae3STomas Elf 		return NULL;
2808381e8ae3STomas Elf 
2809a24a11e6SChris Wilson 	ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
2810a028c4b0SDaniel Vetter 	if (!ipehr_is_semaphore_wait(ring->dev, ipehr))
28116274f212SChris Wilson 		return NULL;
2812a24a11e6SChris Wilson 
281388fe429dSDaniel Vetter 	/*
281488fe429dSDaniel Vetter 	 * HEAD is likely pointing to the dword after the actual command,
281588fe429dSDaniel Vetter 	 * so scan backwards until we find the MBOX. But limit it to just 3
2816a6cdb93aSRodrigo Vivi 	 * or 4 dwords depending on the semaphore wait command size.
2817a6cdb93aSRodrigo Vivi 	 * Note that we don't care about ACTHD here since that might
281888fe429dSDaniel Vetter 	 * point at at batch, and semaphores are always emitted into the
281988fe429dSDaniel Vetter 	 * ringbuffer itself.
2820a24a11e6SChris Wilson 	 */
282188fe429dSDaniel Vetter 	head = I915_READ_HEAD(ring) & HEAD_ADDR;
2822a6cdb93aSRodrigo Vivi 	backwards = (INTEL_INFO(ring->dev)->gen >= 8) ? 5 : 4;
282388fe429dSDaniel Vetter 
2824a6cdb93aSRodrigo Vivi 	for (i = backwards; i; --i) {
282588fe429dSDaniel Vetter 		/*
282688fe429dSDaniel Vetter 		 * Be paranoid and presume the hw has gone off into the wild -
282788fe429dSDaniel Vetter 		 * our ring is smaller than what the hardware (and hence
282888fe429dSDaniel Vetter 		 * HEAD_ADDR) allows. Also handles wrap-around.
282988fe429dSDaniel Vetter 		 */
2830ee1b1e5eSOscar Mateo 		head &= ring->buffer->size - 1;
283188fe429dSDaniel Vetter 
283288fe429dSDaniel Vetter 		/* This here seems to blow up */
2833ee1b1e5eSOscar Mateo 		cmd = ioread32(ring->buffer->virtual_start + head);
2834a24a11e6SChris Wilson 		if (cmd == ipehr)
2835a24a11e6SChris Wilson 			break;
2836a24a11e6SChris Wilson 
283788fe429dSDaniel Vetter 		head -= 4;
283888fe429dSDaniel Vetter 	}
2839a24a11e6SChris Wilson 
284088fe429dSDaniel Vetter 	if (!i)
284188fe429dSDaniel Vetter 		return NULL;
284288fe429dSDaniel Vetter 
2843ee1b1e5eSOscar Mateo 	*seqno = ioread32(ring->buffer->virtual_start + head + 4) + 1;
2844a6cdb93aSRodrigo Vivi 	if (INTEL_INFO(ring->dev)->gen >= 8) {
2845a6cdb93aSRodrigo Vivi 		offset = ioread32(ring->buffer->virtual_start + head + 12);
2846a6cdb93aSRodrigo Vivi 		offset <<= 32;
2847a6cdb93aSRodrigo Vivi 		offset = ioread32(ring->buffer->virtual_start + head + 8);
2848a6cdb93aSRodrigo Vivi 	}
2849a6cdb93aSRodrigo Vivi 	return semaphore_wait_to_signaller_ring(ring, ipehr, offset);
2850a24a11e6SChris Wilson }
2851a24a11e6SChris Wilson 
2852a4872ba6SOscar Mateo static int semaphore_passed(struct intel_engine_cs *ring)
28536274f212SChris Wilson {
28546274f212SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2855a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2856a0d036b0SChris Wilson 	u32 seqno;
28576274f212SChris Wilson 
28584be17381SChris Wilson 	ring->hangcheck.deadlock++;
28596274f212SChris Wilson 
28606274f212SChris Wilson 	signaller = semaphore_waits_for(ring, &seqno);
28614be17381SChris Wilson 	if (signaller == NULL)
28624be17381SChris Wilson 		return -1;
28634be17381SChris Wilson 
28644be17381SChris Wilson 	/* Prevent pathological recursion due to driver bugs */
28654be17381SChris Wilson 	if (signaller->hangcheck.deadlock >= I915_NUM_RINGS)
28666274f212SChris Wilson 		return -1;
28676274f212SChris Wilson 
28684be17381SChris Wilson 	if (i915_seqno_passed(signaller->get_seqno(signaller, false), seqno))
28694be17381SChris Wilson 		return 1;
28704be17381SChris Wilson 
2871a0d036b0SChris Wilson 	/* cursory check for an unkickable deadlock */
2872a0d036b0SChris Wilson 	if (I915_READ_CTL(signaller) & RING_WAIT_SEMAPHORE &&
2873a0d036b0SChris Wilson 	    semaphore_passed(signaller) < 0)
28744be17381SChris Wilson 		return -1;
28754be17381SChris Wilson 
28764be17381SChris Wilson 	return 0;
28776274f212SChris Wilson }
28786274f212SChris Wilson 
28796274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
28806274f212SChris Wilson {
2881a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
28826274f212SChris Wilson 	int i;
28836274f212SChris Wilson 
28846274f212SChris Wilson 	for_each_ring(ring, dev_priv, i)
28854be17381SChris Wilson 		ring->hangcheck.deadlock = 0;
28866274f212SChris Wilson }
28876274f212SChris Wilson 
2888ad8beaeaSMika Kuoppala static enum intel_ring_hangcheck_action
2889a4872ba6SOscar Mateo ring_stuck(struct intel_engine_cs *ring, u64 acthd)
28901ec14ad3SChris Wilson {
28911ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
28921ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
28939107e9d2SChris Wilson 	u32 tmp;
28949107e9d2SChris Wilson 
2895f260fe7bSMika Kuoppala 	if (acthd != ring->hangcheck.acthd) {
2896f260fe7bSMika Kuoppala 		if (acthd > ring->hangcheck.max_acthd) {
2897f260fe7bSMika Kuoppala 			ring->hangcheck.max_acthd = acthd;
2898f2f4d82fSJani Nikula 			return HANGCHECK_ACTIVE;
2899f260fe7bSMika Kuoppala 		}
2900f260fe7bSMika Kuoppala 
2901f260fe7bSMika Kuoppala 		return HANGCHECK_ACTIVE_LOOP;
2902f260fe7bSMika Kuoppala 	}
29036274f212SChris Wilson 
29049107e9d2SChris Wilson 	if (IS_GEN2(dev))
2905f2f4d82fSJani Nikula 		return HANGCHECK_HUNG;
29069107e9d2SChris Wilson 
29079107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
29089107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
29099107e9d2SChris Wilson 	 * and break the hang. This should work on
29109107e9d2SChris Wilson 	 * all but the second generation chipsets.
29119107e9d2SChris Wilson 	 */
29129107e9d2SChris Wilson 	tmp = I915_READ_CTL(ring);
29131ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
291458174462SMika Kuoppala 		i915_handle_error(dev, false,
291558174462SMika Kuoppala 				  "Kicking stuck wait on %s",
29161ec14ad3SChris Wilson 				  ring->name);
29171ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
2918f2f4d82fSJani Nikula 		return HANGCHECK_KICK;
29191ec14ad3SChris Wilson 	}
2920a24a11e6SChris Wilson 
29216274f212SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
29226274f212SChris Wilson 		switch (semaphore_passed(ring)) {
29236274f212SChris Wilson 		default:
2924f2f4d82fSJani Nikula 			return HANGCHECK_HUNG;
29256274f212SChris Wilson 		case 1:
292658174462SMika Kuoppala 			i915_handle_error(dev, false,
292758174462SMika Kuoppala 					  "Kicking stuck semaphore on %s",
2928a24a11e6SChris Wilson 					  ring->name);
2929a24a11e6SChris Wilson 			I915_WRITE_CTL(ring, tmp);
2930f2f4d82fSJani Nikula 			return HANGCHECK_KICK;
29316274f212SChris Wilson 		case 0:
2932f2f4d82fSJani Nikula 			return HANGCHECK_WAIT;
29336274f212SChris Wilson 		}
29349107e9d2SChris Wilson 	}
29359107e9d2SChris Wilson 
2936f2f4d82fSJani Nikula 	return HANGCHECK_HUNG;
2937a24a11e6SChris Wilson }
2938d1e61e7fSChris Wilson 
2939737b1506SChris Wilson /*
2940f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
294105407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
294205407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
294305407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
294405407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
294505407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
2946f65d9421SBen Gamari  */
2947737b1506SChris Wilson static void i915_hangcheck_elapsed(struct work_struct *work)
2948f65d9421SBen Gamari {
2949737b1506SChris Wilson 	struct drm_i915_private *dev_priv =
2950737b1506SChris Wilson 		container_of(work, typeof(*dev_priv),
2951737b1506SChris Wilson 			     gpu_error.hangcheck_work.work);
2952737b1506SChris Wilson 	struct drm_device *dev = dev_priv->dev;
2953a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
2954b4519513SChris Wilson 	int i;
295505407ff8SMika Kuoppala 	int busy_count = 0, rings_hung = 0;
29569107e9d2SChris Wilson 	bool stuck[I915_NUM_RINGS] = { 0 };
29579107e9d2SChris Wilson #define BUSY 1
29589107e9d2SChris Wilson #define KICK 5
29599107e9d2SChris Wilson #define HUNG 20
2960893eead0SChris Wilson 
2961d330a953SJani Nikula 	if (!i915.enable_hangcheck)
29623e0dc6b0SBen Widawsky 		return;
29633e0dc6b0SBen Widawsky 
2964b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
296550877445SChris Wilson 		u64 acthd;
296650877445SChris Wilson 		u32 seqno;
29679107e9d2SChris Wilson 		bool busy = true;
2968b4519513SChris Wilson 
29696274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
29706274f212SChris Wilson 
297105407ff8SMika Kuoppala 		seqno = ring->get_seqno(ring, false);
297205407ff8SMika Kuoppala 		acthd = intel_ring_get_active_head(ring);
297305407ff8SMika Kuoppala 
297405407ff8SMika Kuoppala 		if (ring->hangcheck.seqno == seqno) {
297594f7bbe1STomas Elf 			if (ring_idle(ring, seqno)) {
2976da661464SMika Kuoppala 				ring->hangcheck.action = HANGCHECK_IDLE;
2977da661464SMika Kuoppala 
29789107e9d2SChris Wilson 				if (waitqueue_active(&ring->irq_queue)) {
29799107e9d2SChris Wilson 					/* Issue a wake-up to catch stuck h/w. */
2980094f9a54SChris Wilson 					if (!test_and_set_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings)) {
2981f4adcd24SDaniel Vetter 						if (!(dev_priv->gpu_error.test_irq_rings & intel_ring_flag(ring)))
29829107e9d2SChris Wilson 							DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
29839107e9d2SChris Wilson 								  ring->name);
2984f4adcd24SDaniel Vetter 						else
2985f4adcd24SDaniel Vetter 							DRM_INFO("Fake missed irq on %s\n",
2986f4adcd24SDaniel Vetter 								 ring->name);
29879107e9d2SChris Wilson 						wake_up_all(&ring->irq_queue);
2988094f9a54SChris Wilson 					}
2989094f9a54SChris Wilson 					/* Safeguard against driver failure */
2990094f9a54SChris Wilson 					ring->hangcheck.score += BUSY;
29919107e9d2SChris Wilson 				} else
29929107e9d2SChris Wilson 					busy = false;
299305407ff8SMika Kuoppala 			} else {
29946274f212SChris Wilson 				/* We always increment the hangcheck score
29956274f212SChris Wilson 				 * if the ring is busy and still processing
29966274f212SChris Wilson 				 * the same request, so that no single request
29976274f212SChris Wilson 				 * can run indefinitely (such as a chain of
29986274f212SChris Wilson 				 * batches). The only time we do not increment
29996274f212SChris Wilson 				 * the hangcheck score on this ring, if this
30006274f212SChris Wilson 				 * ring is in a legitimate wait for another
30016274f212SChris Wilson 				 * ring. In that case the waiting ring is a
30026274f212SChris Wilson 				 * victim and we want to be sure we catch the
30036274f212SChris Wilson 				 * right culprit. Then every time we do kick
30046274f212SChris Wilson 				 * the ring, add a small increment to the
30056274f212SChris Wilson 				 * score so that we can catch a batch that is
30066274f212SChris Wilson 				 * being repeatedly kicked and so responsible
30076274f212SChris Wilson 				 * for stalling the machine.
30089107e9d2SChris Wilson 				 */
3009ad8beaeaSMika Kuoppala 				ring->hangcheck.action = ring_stuck(ring,
3010ad8beaeaSMika Kuoppala 								    acthd);
3011ad8beaeaSMika Kuoppala 
3012ad8beaeaSMika Kuoppala 				switch (ring->hangcheck.action) {
3013da661464SMika Kuoppala 				case HANGCHECK_IDLE:
3014f2f4d82fSJani Nikula 				case HANGCHECK_WAIT:
3015f2f4d82fSJani Nikula 				case HANGCHECK_ACTIVE:
3016f260fe7bSMika Kuoppala 					break;
3017f260fe7bSMika Kuoppala 				case HANGCHECK_ACTIVE_LOOP:
3018ea04cb31SJani Nikula 					ring->hangcheck.score += BUSY;
30196274f212SChris Wilson 					break;
3020f2f4d82fSJani Nikula 				case HANGCHECK_KICK:
3021ea04cb31SJani Nikula 					ring->hangcheck.score += KICK;
30226274f212SChris Wilson 					break;
3023f2f4d82fSJani Nikula 				case HANGCHECK_HUNG:
3024ea04cb31SJani Nikula 					ring->hangcheck.score += HUNG;
30256274f212SChris Wilson 					stuck[i] = true;
30266274f212SChris Wilson 					break;
30276274f212SChris Wilson 				}
302805407ff8SMika Kuoppala 			}
30299107e9d2SChris Wilson 		} else {
3030da661464SMika Kuoppala 			ring->hangcheck.action = HANGCHECK_ACTIVE;
3031da661464SMika Kuoppala 
30329107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
30339107e9d2SChris Wilson 			 * attempts across multiple batches.
30349107e9d2SChris Wilson 			 */
30359107e9d2SChris Wilson 			if (ring->hangcheck.score > 0)
30369107e9d2SChris Wilson 				ring->hangcheck.score--;
3037f260fe7bSMika Kuoppala 
3038f260fe7bSMika Kuoppala 			ring->hangcheck.acthd = ring->hangcheck.max_acthd = 0;
3039cbb465e7SChris Wilson 		}
3040f65d9421SBen Gamari 
304105407ff8SMika Kuoppala 		ring->hangcheck.seqno = seqno;
304205407ff8SMika Kuoppala 		ring->hangcheck.acthd = acthd;
30439107e9d2SChris Wilson 		busy_count += busy;
304405407ff8SMika Kuoppala 	}
304505407ff8SMika Kuoppala 
304605407ff8SMika Kuoppala 	for_each_ring(ring, dev_priv, i) {
3047b6b0fac0SMika Kuoppala 		if (ring->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG) {
3048b8d88d1dSDaniel Vetter 			DRM_INFO("%s on %s\n",
304905407ff8SMika Kuoppala 				 stuck[i] ? "stuck" : "no progress",
3050a43adf07SChris Wilson 				 ring->name);
3051a43adf07SChris Wilson 			rings_hung++;
305205407ff8SMika Kuoppala 		}
305305407ff8SMika Kuoppala 	}
305405407ff8SMika Kuoppala 
305505407ff8SMika Kuoppala 	if (rings_hung)
305658174462SMika Kuoppala 		return i915_handle_error(dev, true, "Ring hung");
305705407ff8SMika Kuoppala 
305805407ff8SMika Kuoppala 	if (busy_count)
305905407ff8SMika Kuoppala 		/* Reset timer case chip hangs without another request
306005407ff8SMika Kuoppala 		 * being added */
306110cd45b6SMika Kuoppala 		i915_queue_hangcheck(dev);
306210cd45b6SMika Kuoppala }
306310cd45b6SMika Kuoppala 
306410cd45b6SMika Kuoppala void i915_queue_hangcheck(struct drm_device *dev)
306510cd45b6SMika Kuoppala {
3066737b1506SChris Wilson 	struct i915_gpu_error *e = &to_i915(dev)->gpu_error;
3067672e7b7cSChris Wilson 
3068d330a953SJani Nikula 	if (!i915.enable_hangcheck)
306910cd45b6SMika Kuoppala 		return;
307010cd45b6SMika Kuoppala 
3071737b1506SChris Wilson 	/* Don't continually defer the hangcheck so that it is always run at
3072737b1506SChris Wilson 	 * least once after work has been scheduled on any ring. Otherwise,
3073737b1506SChris Wilson 	 * we will ignore a hung ring if a second ring is kept busy.
3074737b1506SChris Wilson 	 */
3075737b1506SChris Wilson 
3076737b1506SChris Wilson 	queue_delayed_work(e->hangcheck_wq, &e->hangcheck_work,
3077737b1506SChris Wilson 			   round_jiffies_up_relative(DRM_I915_HANGCHECK_JIFFIES));
3078f65d9421SBen Gamari }
3079f65d9421SBen Gamari 
30801c69eb42SPaulo Zanoni static void ibx_irq_reset(struct drm_device *dev)
308191738a95SPaulo Zanoni {
308291738a95SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
308391738a95SPaulo Zanoni 
308491738a95SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
308591738a95SPaulo Zanoni 		return;
308691738a95SPaulo Zanoni 
3087f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(SDE);
3088105b122eSPaulo Zanoni 
3089105b122eSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
3090105b122eSPaulo Zanoni 		I915_WRITE(SERR_INT, 0xffffffff);
3091622364b6SPaulo Zanoni }
3092105b122eSPaulo Zanoni 
309391738a95SPaulo Zanoni /*
3094622364b6SPaulo Zanoni  * SDEIER is also touched by the interrupt handler to work around missed PCH
3095622364b6SPaulo Zanoni  * interrupts. Hence we can't update it after the interrupt handler is enabled -
3096622364b6SPaulo Zanoni  * instead we unconditionally enable all PCH interrupt sources here, but then
3097622364b6SPaulo Zanoni  * only unmask them as needed with SDEIMR.
3098622364b6SPaulo Zanoni  *
3099622364b6SPaulo Zanoni  * This function needs to be called before interrupts are enabled.
310091738a95SPaulo Zanoni  */
3101622364b6SPaulo Zanoni static void ibx_irq_pre_postinstall(struct drm_device *dev)
3102622364b6SPaulo Zanoni {
3103622364b6SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3104622364b6SPaulo Zanoni 
3105622364b6SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
3106622364b6SPaulo Zanoni 		return;
3107622364b6SPaulo Zanoni 
3108622364b6SPaulo Zanoni 	WARN_ON(I915_READ(SDEIER) != 0);
310991738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
311091738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
311191738a95SPaulo Zanoni }
311291738a95SPaulo Zanoni 
31137c4d664eSPaulo Zanoni static void gen5_gt_irq_reset(struct drm_device *dev)
3114d18ea1b5SDaniel Vetter {
3115d18ea1b5SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
3116d18ea1b5SDaniel Vetter 
3117f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GT);
3118a9d356a6SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6)
3119f86f3fb0SPaulo Zanoni 		GEN5_IRQ_RESET(GEN6_PM);
3120d18ea1b5SDaniel Vetter }
3121d18ea1b5SDaniel Vetter 
3122c0e09200SDave Airlie /* drm_dma.h hooks
3123c0e09200SDave Airlie */
3124be30b29fSPaulo Zanoni static void ironlake_irq_reset(struct drm_device *dev)
3125036a4a7dSZhenyu Wang {
31262d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3127036a4a7dSZhenyu Wang 
31280c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xffffffff);
3129bdfcdb63SDaniel Vetter 
3130f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(DE);
3131c6d954c1SPaulo Zanoni 	if (IS_GEN7(dev))
3132c6d954c1SPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, 0xffffffff);
3133036a4a7dSZhenyu Wang 
31347c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
3135c650156aSZhenyu Wang 
31361c69eb42SPaulo Zanoni 	ibx_irq_reset(dev);
31377d99163dSBen Widawsky }
31387d99163dSBen Widawsky 
313970591a41SVille Syrjälä static void vlv_display_irq_reset(struct drm_i915_private *dev_priv)
314070591a41SVille Syrjälä {
314170591a41SVille Syrjälä 	enum pipe pipe;
314270591a41SVille Syrjälä 
31430706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xFFFFFFFF, 0);
314470591a41SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
314570591a41SVille Syrjälä 
314670591a41SVille Syrjälä 	for_each_pipe(dev_priv, pipe)
314770591a41SVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), 0xffff);
314870591a41SVille Syrjälä 
314970591a41SVille Syrjälä 	GEN5_IRQ_RESET(VLV_);
315070591a41SVille Syrjälä }
315170591a41SVille Syrjälä 
31527e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
31537e231dbeSJesse Barnes {
31542d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
31557e231dbeSJesse Barnes 
31567e231dbeSJesse Barnes 	/* VLV magic */
31577e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
31587e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
31597e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
31607e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
31617e231dbeSJesse Barnes 
31627c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
31637e231dbeSJesse Barnes 
31647c4cde39SVille Syrjälä 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
31657e231dbeSJesse Barnes 
316670591a41SVille Syrjälä 	vlv_display_irq_reset(dev_priv);
31677e231dbeSJesse Barnes }
31687e231dbeSJesse Barnes 
3169d6e3cca3SDaniel Vetter static void gen8_gt_irq_reset(struct drm_i915_private *dev_priv)
3170d6e3cca3SDaniel Vetter {
3171d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 0);
3172d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 1);
3173d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 2);
3174d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 3);
3175d6e3cca3SDaniel Vetter }
3176d6e3cca3SDaniel Vetter 
3177823f6b38SPaulo Zanoni static void gen8_irq_reset(struct drm_device *dev)
3178abd58f01SBen Widawsky {
3179abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3180abd58f01SBen Widawsky 	int pipe;
3181abd58f01SBen Widawsky 
3182abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
3183abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3184abd58f01SBen Widawsky 
3185d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
3186abd58f01SBen Widawsky 
3187055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3188f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3189813bde43SPaulo Zanoni 						   POWER_DOMAIN_PIPE(pipe)))
3190f86f3fb0SPaulo Zanoni 			GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3191abd58f01SBen Widawsky 
3192f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_PORT_);
3193f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_MISC_);
3194f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_PCU_);
3195abd58f01SBen Widawsky 
3196266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev))
31971c69eb42SPaulo Zanoni 		ibx_irq_reset(dev);
3198abd58f01SBen Widawsky }
3199abd58f01SBen Widawsky 
32004c6c03beSDamien Lespiau void gen8_irq_power_well_post_enable(struct drm_i915_private *dev_priv,
32014c6c03beSDamien Lespiau 				     unsigned int pipe_mask)
3202d49bdb0eSPaulo Zanoni {
32031180e206SPaulo Zanoni 	uint32_t extra_ier = GEN8_PIPE_VBLANK | GEN8_PIPE_FIFO_UNDERRUN;
3204d49bdb0eSPaulo Zanoni 
320513321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3206d14c0343SDamien Lespiau 	if (pipe_mask & 1 << PIPE_A)
3207d14c0343SDamien Lespiau 		GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_A,
3208d14c0343SDamien Lespiau 				  dev_priv->de_irq_mask[PIPE_A],
3209d14c0343SDamien Lespiau 				  ~dev_priv->de_irq_mask[PIPE_A] | extra_ier);
32104c6c03beSDamien Lespiau 	if (pipe_mask & 1 << PIPE_B)
32114c6c03beSDamien Lespiau 		GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_B,
32124c6c03beSDamien Lespiau 				  dev_priv->de_irq_mask[PIPE_B],
32131180e206SPaulo Zanoni 				  ~dev_priv->de_irq_mask[PIPE_B] | extra_ier);
32144c6c03beSDamien Lespiau 	if (pipe_mask & 1 << PIPE_C)
32154c6c03beSDamien Lespiau 		GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_C,
32164c6c03beSDamien Lespiau 				  dev_priv->de_irq_mask[PIPE_C],
32171180e206SPaulo Zanoni 				  ~dev_priv->de_irq_mask[PIPE_C] | extra_ier);
321813321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3219d49bdb0eSPaulo Zanoni }
3220d49bdb0eSPaulo Zanoni 
322143f328d7SVille Syrjälä static void cherryview_irq_preinstall(struct drm_device *dev)
322243f328d7SVille Syrjälä {
322343f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
322443f328d7SVille Syrjälä 
322543f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
322643f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
322743f328d7SVille Syrjälä 
3228d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
322943f328d7SVille Syrjälä 
323043f328d7SVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
323143f328d7SVille Syrjälä 
323243f328d7SVille Syrjälä 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
323343f328d7SVille Syrjälä 
323470591a41SVille Syrjälä 	vlv_display_irq_reset(dev_priv);
323543f328d7SVille Syrjälä }
323643f328d7SVille Syrjälä 
323787a02106SVille Syrjälä static u32 intel_hpd_enabled_irqs(struct drm_device *dev,
323887a02106SVille Syrjälä 				  const u32 hpd[HPD_NUM_PINS])
323987a02106SVille Syrjälä {
324087a02106SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(dev);
324187a02106SVille Syrjälä 	struct intel_encoder *encoder;
324287a02106SVille Syrjälä 	u32 enabled_irqs = 0;
324387a02106SVille Syrjälä 
324487a02106SVille Syrjälä 	for_each_intel_encoder(dev, encoder)
324587a02106SVille Syrjälä 		if (dev_priv->hotplug.stats[encoder->hpd_pin].state == HPD_ENABLED)
324687a02106SVille Syrjälä 			enabled_irqs |= hpd[encoder->hpd_pin];
324787a02106SVille Syrjälä 
324887a02106SVille Syrjälä 	return enabled_irqs;
324987a02106SVille Syrjälä }
325087a02106SVille Syrjälä 
325182a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
325282a28bcfSDaniel Vetter {
32532d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
325487a02106SVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
325582a28bcfSDaniel Vetter 
325682a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
3257fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
325887a02106SVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_ibx);
325982a28bcfSDaniel Vetter 	} else {
3260fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
326187a02106SVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_cpt);
326282a28bcfSDaniel Vetter 	}
326382a28bcfSDaniel Vetter 
3264fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
326582a28bcfSDaniel Vetter 
32667fe0b973SKeith Packard 	/*
32677fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
32686dbf30ceSVille Syrjälä 	 * duration to 2ms (which is the minimum in the Display Port spec).
32696dbf30ceSVille Syrjälä 	 * The pulse duration bits are reserved on LPT+.
32707fe0b973SKeith Packard 	 */
32717fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
32727fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
32737fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
32747fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
32757fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
32760b2eb33eSVille Syrjälä 	/*
32770b2eb33eSVille Syrjälä 	 * When CPU and PCH are on the same package, port A
32780b2eb33eSVille Syrjälä 	 * HPD must be enabled in both north and south.
32790b2eb33eSVille Syrjälä 	 */
32800b2eb33eSVille Syrjälä 	if (HAS_PCH_LPT_LP(dev))
32810b2eb33eSVille Syrjälä 		hotplug |= PORTA_HOTPLUG_ENABLE;
32827fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
32836dbf30ceSVille Syrjälä }
328426951cafSXiong Zhang 
32856dbf30ceSVille Syrjälä static void spt_hpd_irq_setup(struct drm_device *dev)
32866dbf30ceSVille Syrjälä {
32876dbf30ceSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
32886dbf30ceSVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
32896dbf30ceSVille Syrjälä 
32906dbf30ceSVille Syrjälä 	hotplug_irqs = SDE_HOTPLUG_MASK_SPT;
32916dbf30ceSVille Syrjälä 	enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_spt);
32926dbf30ceSVille Syrjälä 
32936dbf30ceSVille Syrjälä 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
32946dbf30ceSVille Syrjälä 
32956dbf30ceSVille Syrjälä 	/* Enable digital hotplug on the PCH */
32966dbf30ceSVille Syrjälä 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
32976dbf30ceSVille Syrjälä 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTC_HOTPLUG_ENABLE |
329874c0b395SVille Syrjälä 		PORTB_HOTPLUG_ENABLE | PORTA_HOTPLUG_ENABLE;
32996dbf30ceSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
33006dbf30ceSVille Syrjälä 
330126951cafSXiong Zhang 	hotplug = I915_READ(PCH_PORT_HOTPLUG2);
330226951cafSXiong Zhang 	hotplug |= PORTE_HOTPLUG_ENABLE;
330326951cafSXiong Zhang 	I915_WRITE(PCH_PORT_HOTPLUG2, hotplug);
330426951cafSXiong Zhang }
33057fe0b973SKeith Packard 
3306e4ce95aaSVille Syrjälä static void ilk_hpd_irq_setup(struct drm_device *dev)
3307e4ce95aaSVille Syrjälä {
3308e4ce95aaSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
3309e4ce95aaSVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
3310e4ce95aaSVille Syrjälä 
33113a3b3c7dSVille Syrjälä 	if (INTEL_INFO(dev)->gen >= 8) {
33123a3b3c7dSVille Syrjälä 		hotplug_irqs = GEN8_PORT_DP_A_HOTPLUG;
33133a3b3c7dSVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_bdw);
33143a3b3c7dSVille Syrjälä 
33153a3b3c7dSVille Syrjälä 		bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
33163a3b3c7dSVille Syrjälä 	} else if (INTEL_INFO(dev)->gen >= 7) {
331723bb4cb5SVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG_IVB;
331823bb4cb5SVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_ivb);
33193a3b3c7dSVille Syrjälä 
33203a3b3c7dSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
332123bb4cb5SVille Syrjälä 	} else {
3322e4ce95aaSVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG;
3323e4ce95aaSVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_ilk);
3324e4ce95aaSVille Syrjälä 
3325e4ce95aaSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
33263a3b3c7dSVille Syrjälä 	}
3327e4ce95aaSVille Syrjälä 
3328e4ce95aaSVille Syrjälä 	/*
3329e4ce95aaSVille Syrjälä 	 * Enable digital hotplug on the CPU, and configure the DP short pulse
3330e4ce95aaSVille Syrjälä 	 * duration to 2ms (which is the minimum in the Display Port spec)
333123bb4cb5SVille Syrjälä 	 * The pulse duration bits are reserved on HSW+.
3332e4ce95aaSVille Syrjälä 	 */
3333e4ce95aaSVille Syrjälä 	hotplug = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
3334e4ce95aaSVille Syrjälä 	hotplug &= ~DIGITAL_PORTA_PULSE_DURATION_MASK;
3335e4ce95aaSVille Syrjälä 	hotplug |= DIGITAL_PORTA_HOTPLUG_ENABLE | DIGITAL_PORTA_PULSE_DURATION_2ms;
3336e4ce95aaSVille Syrjälä 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, hotplug);
3337e4ce95aaSVille Syrjälä 
3338e4ce95aaSVille Syrjälä 	ibx_hpd_irq_setup(dev);
3339e4ce95aaSVille Syrjälä }
3340e4ce95aaSVille Syrjälä 
3341e0a20ad7SShashank Sharma static void bxt_hpd_irq_setup(struct drm_device *dev)
3342e0a20ad7SShashank Sharma {
3343e0a20ad7SShashank Sharma 	struct drm_i915_private *dev_priv = dev->dev_private;
3344a52bb15bSVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
3345e0a20ad7SShashank Sharma 
3346a52bb15bSVille Syrjälä 	enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_bxt);
3347a52bb15bSVille Syrjälä 	hotplug_irqs = BXT_DE_PORT_HOTPLUG_MASK;
3348e0a20ad7SShashank Sharma 
3349a52bb15bSVille Syrjälä 	bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
3350e0a20ad7SShashank Sharma 
3351a52bb15bSVille Syrjälä 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
3352a52bb15bSVille Syrjälä 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTB_HOTPLUG_ENABLE |
3353a52bb15bSVille Syrjälä 		PORTA_HOTPLUG_ENABLE;
3354a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
3355e0a20ad7SShashank Sharma }
3356e0a20ad7SShashank Sharma 
3357d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
3358d46da437SPaulo Zanoni {
33592d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
336082a28bcfSDaniel Vetter 	u32 mask;
3361d46da437SPaulo Zanoni 
3362692a04cfSDaniel Vetter 	if (HAS_PCH_NOP(dev))
3363692a04cfSDaniel Vetter 		return;
3364692a04cfSDaniel Vetter 
3365105b122eSPaulo Zanoni 	if (HAS_PCH_IBX(dev))
33665c673b60SDaniel Vetter 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
3367105b122eSPaulo Zanoni 	else
33685c673b60SDaniel Vetter 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
33698664281bSPaulo Zanoni 
3370b51a2842SVille Syrjälä 	gen5_assert_iir_is_zero(dev_priv, SDEIIR);
3371d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
3372d46da437SPaulo Zanoni }
3373d46da437SPaulo Zanoni 
33740a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
33750a9a8c91SDaniel Vetter {
33760a9a8c91SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
33770a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
33780a9a8c91SDaniel Vetter 
33790a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
33800a9a8c91SDaniel Vetter 
33810a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
3382040d2baaSBen Widawsky 	if (HAS_L3_DPF(dev)) {
33830a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
338435a85ac6SBen Widawsky 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev);
338535a85ac6SBen Widawsky 		gt_irqs |= GT_PARITY_ERROR(dev);
33860a9a8c91SDaniel Vetter 	}
33870a9a8c91SDaniel Vetter 
33880a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
33890a9a8c91SDaniel Vetter 	if (IS_GEN5(dev)) {
33900a9a8c91SDaniel Vetter 		gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
33910a9a8c91SDaniel Vetter 			   ILK_BSD_USER_INTERRUPT;
33920a9a8c91SDaniel Vetter 	} else {
33930a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
33940a9a8c91SDaniel Vetter 	}
33950a9a8c91SDaniel Vetter 
339635079899SPaulo Zanoni 	GEN5_IRQ_INIT(GT, dev_priv->gt_irq_mask, gt_irqs);
33970a9a8c91SDaniel Vetter 
33980a9a8c91SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
339978e68d36SImre Deak 		/*
340078e68d36SImre Deak 		 * RPS interrupts will get enabled/disabled on demand when RPS
340178e68d36SImre Deak 		 * itself is enabled/disabled.
340278e68d36SImre Deak 		 */
34030a9a8c91SDaniel Vetter 		if (HAS_VEBOX(dev))
34040a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
34050a9a8c91SDaniel Vetter 
3406605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = 0xffffffff;
340735079899SPaulo Zanoni 		GEN5_IRQ_INIT(GEN6_PM, dev_priv->pm_irq_mask, pm_irqs);
34080a9a8c91SDaniel Vetter 	}
34090a9a8c91SDaniel Vetter }
34100a9a8c91SDaniel Vetter 
3411f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
3412036a4a7dSZhenyu Wang {
34132d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
34148e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
34158e76f8dcSPaulo Zanoni 
34168e76f8dcSPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 7) {
34178e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
34188e76f8dcSPaulo Zanoni 				DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
34198e76f8dcSPaulo Zanoni 				DE_PLANEB_FLIP_DONE_IVB |
34205c673b60SDaniel Vetter 				DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB);
34218e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
342223bb4cb5SVille Syrjälä 			      DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB |
342323bb4cb5SVille Syrjälä 			      DE_DP_A_HOTPLUG_IVB);
34248e76f8dcSPaulo Zanoni 	} else {
34258e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3426ce99c256SDaniel Vetter 				DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
34275b3a856bSDaniel Vetter 				DE_AUX_CHANNEL_A |
34285b3a856bSDaniel Vetter 				DE_PIPEB_CRC_DONE | DE_PIPEA_CRC_DONE |
34295b3a856bSDaniel Vetter 				DE_POISON);
3430e4ce95aaSVille Syrjälä 		extra_mask = (DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
3431e4ce95aaSVille Syrjälä 			      DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN |
3432e4ce95aaSVille Syrjälä 			      DE_DP_A_HOTPLUG);
34338e76f8dcSPaulo Zanoni 	}
3434036a4a7dSZhenyu Wang 
34351ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3436036a4a7dSZhenyu Wang 
34370c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xeffe);
34380c841212SPaulo Zanoni 
3439622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3440622364b6SPaulo Zanoni 
344135079899SPaulo Zanoni 	GEN5_IRQ_INIT(DE, dev_priv->irq_mask, display_mask | extra_mask);
3442036a4a7dSZhenyu Wang 
34430a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
3444036a4a7dSZhenyu Wang 
3445d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
34467fe0b973SKeith Packard 
3447f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
34486005ce42SDaniel Vetter 		/* Enable PCU event interrupts
34496005ce42SDaniel Vetter 		 *
34506005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
34514bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
34524bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
3453d6207435SDaniel Vetter 		spin_lock_irq(&dev_priv->irq_lock);
3454f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
3455d6207435SDaniel Vetter 		spin_unlock_irq(&dev_priv->irq_lock);
3456f97108d1SJesse Barnes 	}
3457f97108d1SJesse Barnes 
3458036a4a7dSZhenyu Wang 	return 0;
3459036a4a7dSZhenyu Wang }
3460036a4a7dSZhenyu Wang 
3461f8b79e58SImre Deak static void valleyview_display_irqs_install(struct drm_i915_private *dev_priv)
3462f8b79e58SImre Deak {
3463f8b79e58SImre Deak 	u32 pipestat_mask;
3464f8b79e58SImre Deak 	u32 iir_mask;
3465120dda4fSVille Syrjälä 	enum pipe pipe;
3466f8b79e58SImre Deak 
3467f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3468f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3469f8b79e58SImre Deak 
3470120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3471120dda4fSVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), pipestat_mask);
3472f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3473f8b79e58SImre Deak 
3474f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3475f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3476f8b79e58SImre Deak 
3477120dda4fSVille Syrjälä 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3478120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3479120dda4fSVille Syrjälä 		      i915_enable_pipestat(dev_priv, pipe, pipestat_mask);
3480f8b79e58SImre Deak 
3481f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3482f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3483f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3484120dda4fSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
3485120dda4fSVille Syrjälä 		iir_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
3486f8b79e58SImre Deak 	dev_priv->irq_mask &= ~iir_mask;
3487f8b79e58SImre Deak 
3488f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3489f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3490f8b79e58SImre Deak 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
349176e41860SVille Syrjälä 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
349276e41860SVille Syrjälä 	POSTING_READ(VLV_IMR);
3493f8b79e58SImre Deak }
3494f8b79e58SImre Deak 
3495f8b79e58SImre Deak static void valleyview_display_irqs_uninstall(struct drm_i915_private *dev_priv)
3496f8b79e58SImre Deak {
3497f8b79e58SImre Deak 	u32 pipestat_mask;
3498f8b79e58SImre Deak 	u32 iir_mask;
3499120dda4fSVille Syrjälä 	enum pipe pipe;
3500f8b79e58SImre Deak 
3501f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3502f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
35036c7fba04SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3504120dda4fSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
3505120dda4fSVille Syrjälä 		iir_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
3506f8b79e58SImre Deak 
3507f8b79e58SImre Deak 	dev_priv->irq_mask |= iir_mask;
3508f8b79e58SImre Deak 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
350976e41860SVille Syrjälä 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
3510f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3511f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3512f8b79e58SImre Deak 	POSTING_READ(VLV_IIR);
3513f8b79e58SImre Deak 
3514f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3515f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3516f8b79e58SImre Deak 
3517120dda4fSVille Syrjälä 	i915_disable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3518120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3519120dda4fSVille Syrjälä 		i915_disable_pipestat(dev_priv, pipe, pipestat_mask);
3520f8b79e58SImre Deak 
3521f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3522f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3523120dda4fSVille Syrjälä 
3524120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3525120dda4fSVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), pipestat_mask);
3526f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3527f8b79e58SImre Deak }
3528f8b79e58SImre Deak 
3529f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3530f8b79e58SImre Deak {
3531f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3532f8b79e58SImre Deak 
3533f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3534f8b79e58SImre Deak 		return;
3535f8b79e58SImre Deak 
3536f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3537f8b79e58SImre Deak 
3538950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3539f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3540f8b79e58SImre Deak }
3541f8b79e58SImre Deak 
3542f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3543f8b79e58SImre Deak {
3544f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3545f8b79e58SImre Deak 
3546f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3547f8b79e58SImre Deak 		return;
3548f8b79e58SImre Deak 
3549f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3550f8b79e58SImre Deak 
3551950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3552f8b79e58SImre Deak 		valleyview_display_irqs_uninstall(dev_priv);
3553f8b79e58SImre Deak }
3554f8b79e58SImre Deak 
35550e6c9a9eSVille Syrjälä static void vlv_display_irq_postinstall(struct drm_i915_private *dev_priv)
35567e231dbeSJesse Barnes {
3557f8b79e58SImre Deak 	dev_priv->irq_mask = ~0;
35587e231dbeSJesse Barnes 
35590706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
356020afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
356120afbda2SDaniel Vetter 
35627e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
356376e41860SVille Syrjälä 	I915_WRITE(VLV_IIR, 0xffffffff);
356476e41860SVille Syrjälä 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
356576e41860SVille Syrjälä 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
356676e41860SVille Syrjälä 	POSTING_READ(VLV_IMR);
35677e231dbeSJesse Barnes 
3568b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3569b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3570d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3571f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3572f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3573d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
35740e6c9a9eSVille Syrjälä }
35750e6c9a9eSVille Syrjälä 
35760e6c9a9eSVille Syrjälä static int valleyview_irq_postinstall(struct drm_device *dev)
35770e6c9a9eSVille Syrjälä {
35780e6c9a9eSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
35790e6c9a9eSVille Syrjälä 
35800e6c9a9eSVille Syrjälä 	vlv_display_irq_postinstall(dev_priv);
35817e231dbeSJesse Barnes 
35820a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
35837e231dbeSJesse Barnes 
35847e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
35857e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
35867e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
35877e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
35887e231dbeSJesse Barnes #endif
35897e231dbeSJesse Barnes 
35907e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
359120afbda2SDaniel Vetter 
359220afbda2SDaniel Vetter 	return 0;
359320afbda2SDaniel Vetter }
359420afbda2SDaniel Vetter 
3595abd58f01SBen Widawsky static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
3596abd58f01SBen Widawsky {
3597abd58f01SBen Widawsky 	/* These are interrupts we'll toggle with the ring mask register */
3598abd58f01SBen Widawsky 	uint32_t gt_interrupts[] = {
3599abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
360073d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
3601abd58f01SBen Widawsky 			GT_RENDER_L3_PARITY_ERROR_INTERRUPT |
360273d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT |
360373d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT,
3604abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
360573d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
360673d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT |
360773d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT,
3608abd58f01SBen Widawsky 		0,
360973d477f6SOscar Mateo 		GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT |
361073d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT
3611abd58f01SBen Widawsky 		};
3612abd58f01SBen Widawsky 
36130961021aSBen Widawsky 	dev_priv->pm_irq_mask = 0xffffffff;
36149a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 0, ~gt_interrupts[0], gt_interrupts[0]);
36159a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 1, ~gt_interrupts[1], gt_interrupts[1]);
361678e68d36SImre Deak 	/*
361778e68d36SImre Deak 	 * RPS interrupts will get enabled/disabled on demand when RPS itself
361878e68d36SImre Deak 	 * is enabled/disabled.
361978e68d36SImre Deak 	 */
362078e68d36SImre Deak 	GEN8_IRQ_INIT_NDX(GT, 2, dev_priv->pm_irq_mask, 0);
36219a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 3, ~gt_interrupts[3], gt_interrupts[3]);
3622abd58f01SBen Widawsky }
3623abd58f01SBen Widawsky 
3624abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3625abd58f01SBen Widawsky {
3626770de83dSDamien Lespiau 	uint32_t de_pipe_masked = GEN8_PIPE_CDCLK_CRC_DONE;
3627770de83dSDamien Lespiau 	uint32_t de_pipe_enables;
36283a3b3c7dSVille Syrjälä 	u32 de_port_masked = GEN8_AUX_CHANNEL_A;
36293a3b3c7dSVille Syrjälä 	u32 de_port_enables;
36303a3b3c7dSVille Syrjälä 	enum pipe pipe;
3631770de83dSDamien Lespiau 
3632b4834a50SRodrigo Vivi 	if (INTEL_INFO(dev_priv)->gen >= 9) {
3633770de83dSDamien Lespiau 		de_pipe_masked |= GEN9_PIPE_PLANE1_FLIP_DONE |
3634770de83dSDamien Lespiau 				  GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
36353a3b3c7dSVille Syrjälä 		de_port_masked |= GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
363688e04703SJesse Barnes 				  GEN9_AUX_CHANNEL_D;
36379e63743eSShashank Sharma 		if (IS_BROXTON(dev_priv))
36383a3b3c7dSVille Syrjälä 			de_port_masked |= BXT_DE_PORT_GMBUS;
36393a3b3c7dSVille Syrjälä 	} else {
3640770de83dSDamien Lespiau 		de_pipe_masked |= GEN8_PIPE_PRIMARY_FLIP_DONE |
3641770de83dSDamien Lespiau 				  GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
36423a3b3c7dSVille Syrjälä 	}
3643770de83dSDamien Lespiau 
3644770de83dSDamien Lespiau 	de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
3645770de83dSDamien Lespiau 					   GEN8_PIPE_FIFO_UNDERRUN;
3646770de83dSDamien Lespiau 
36473a3b3c7dSVille Syrjälä 	de_port_enables = de_port_masked;
3648a52bb15bSVille Syrjälä 	if (IS_BROXTON(dev_priv))
3649a52bb15bSVille Syrjälä 		de_port_enables |= BXT_DE_PORT_HOTPLUG_MASK;
3650a52bb15bSVille Syrjälä 	else if (IS_BROADWELL(dev_priv))
36513a3b3c7dSVille Syrjälä 		de_port_enables |= GEN8_PORT_DP_A_HOTPLUG;
36523a3b3c7dSVille Syrjälä 
365313b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_A] = ~de_pipe_masked;
365413b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_B] = ~de_pipe_masked;
365513b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_C] = ~de_pipe_masked;
3656abd58f01SBen Widawsky 
3657055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3658f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3659813bde43SPaulo Zanoni 				POWER_DOMAIN_PIPE(pipe)))
3660813bde43SPaulo Zanoni 			GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
3661813bde43SPaulo Zanoni 					  dev_priv->de_irq_mask[pipe],
366235079899SPaulo Zanoni 					  de_pipe_enables);
3663abd58f01SBen Widawsky 
36643a3b3c7dSVille Syrjälä 	GEN5_IRQ_INIT(GEN8_DE_PORT_, ~de_port_masked, de_port_enables);
3665abd58f01SBen Widawsky }
3666abd58f01SBen Widawsky 
3667abd58f01SBen Widawsky static int gen8_irq_postinstall(struct drm_device *dev)
3668abd58f01SBen Widawsky {
3669abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3670abd58f01SBen Widawsky 
3671266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev))
3672622364b6SPaulo Zanoni 		ibx_irq_pre_postinstall(dev);
3673622364b6SPaulo Zanoni 
3674abd58f01SBen Widawsky 	gen8_gt_irq_postinstall(dev_priv);
3675abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3676abd58f01SBen Widawsky 
3677266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev))
3678abd58f01SBen Widawsky 		ibx_irq_postinstall(dev);
3679abd58f01SBen Widawsky 
3680abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
3681abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3682abd58f01SBen Widawsky 
3683abd58f01SBen Widawsky 	return 0;
3684abd58f01SBen Widawsky }
3685abd58f01SBen Widawsky 
368643f328d7SVille Syrjälä static int cherryview_irq_postinstall(struct drm_device *dev)
368743f328d7SVille Syrjälä {
368843f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
368943f328d7SVille Syrjälä 
3690c2b66797SVille Syrjälä 	vlv_display_irq_postinstall(dev_priv);
369143f328d7SVille Syrjälä 
369243f328d7SVille Syrjälä 	gen8_gt_irq_postinstall(dev_priv);
369343f328d7SVille Syrjälä 
369443f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, MASTER_INTERRUPT_ENABLE);
369543f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
369643f328d7SVille Syrjälä 
369743f328d7SVille Syrjälä 	return 0;
369843f328d7SVille Syrjälä }
369943f328d7SVille Syrjälä 
3700abd58f01SBen Widawsky static void gen8_irq_uninstall(struct drm_device *dev)
3701abd58f01SBen Widawsky {
3702abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3703abd58f01SBen Widawsky 
3704abd58f01SBen Widawsky 	if (!dev_priv)
3705abd58f01SBen Widawsky 		return;
3706abd58f01SBen Widawsky 
3707823f6b38SPaulo Zanoni 	gen8_irq_reset(dev);
3708abd58f01SBen Widawsky }
3709abd58f01SBen Widawsky 
37108ea0be4fSVille Syrjälä static void vlv_display_irq_uninstall(struct drm_i915_private *dev_priv)
37118ea0be4fSVille Syrjälä {
37128ea0be4fSVille Syrjälä 	/* Interrupt setup is already guaranteed to be single-threaded, this is
37138ea0be4fSVille Syrjälä 	 * just to make the assert_spin_locked check happy. */
37148ea0be4fSVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
37158ea0be4fSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
37168ea0be4fSVille Syrjälä 		valleyview_display_irqs_uninstall(dev_priv);
37178ea0be4fSVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
37188ea0be4fSVille Syrjälä 
37198ea0be4fSVille Syrjälä 	vlv_display_irq_reset(dev_priv);
37208ea0be4fSVille Syrjälä 
3721c352d1baSImre Deak 	dev_priv->irq_mask = ~0;
37228ea0be4fSVille Syrjälä }
37238ea0be4fSVille Syrjälä 
37247e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
37257e231dbeSJesse Barnes {
37262d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
37277e231dbeSJesse Barnes 
37287e231dbeSJesse Barnes 	if (!dev_priv)
37297e231dbeSJesse Barnes 		return;
37307e231dbeSJesse Barnes 
3731843d0e7dSImre Deak 	I915_WRITE(VLV_MASTER_IER, 0);
3732843d0e7dSImre Deak 
3733893fce8eSVille Syrjälä 	gen5_gt_irq_reset(dev);
3734893fce8eSVille Syrjälä 
37357e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
3736f8b79e58SImre Deak 
37378ea0be4fSVille Syrjälä 	vlv_display_irq_uninstall(dev_priv);
37387e231dbeSJesse Barnes }
37397e231dbeSJesse Barnes 
374043f328d7SVille Syrjälä static void cherryview_irq_uninstall(struct drm_device *dev)
374143f328d7SVille Syrjälä {
374243f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
374343f328d7SVille Syrjälä 
374443f328d7SVille Syrjälä 	if (!dev_priv)
374543f328d7SVille Syrjälä 		return;
374643f328d7SVille Syrjälä 
374743f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
374843f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
374943f328d7SVille Syrjälä 
3750a2c30fbaSVille Syrjälä 	gen8_gt_irq_reset(dev_priv);
375143f328d7SVille Syrjälä 
3752a2c30fbaSVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
375343f328d7SVille Syrjälä 
3754c2b66797SVille Syrjälä 	vlv_display_irq_uninstall(dev_priv);
375543f328d7SVille Syrjälä }
375643f328d7SVille Syrjälä 
3757f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
3758036a4a7dSZhenyu Wang {
37592d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
37604697995bSJesse Barnes 
37614697995bSJesse Barnes 	if (!dev_priv)
37624697995bSJesse Barnes 		return;
37634697995bSJesse Barnes 
3764be30b29fSPaulo Zanoni 	ironlake_irq_reset(dev);
3765036a4a7dSZhenyu Wang }
3766036a4a7dSZhenyu Wang 
3767c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
3768c2798b19SChris Wilson {
37692d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3770c2798b19SChris Wilson 	int pipe;
3771c2798b19SChris Wilson 
3772055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3773c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3774c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3775c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3776c2798b19SChris Wilson 	POSTING_READ16(IER);
3777c2798b19SChris Wilson }
3778c2798b19SChris Wilson 
3779c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
3780c2798b19SChris Wilson {
37812d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3782c2798b19SChris Wilson 
3783c2798b19SChris Wilson 	I915_WRITE16(EMR,
3784c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3785c2798b19SChris Wilson 
3786c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
3787c2798b19SChris Wilson 	dev_priv->irq_mask =
3788c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3789c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3790c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
379137ef01abSDaniel Vetter 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
3792c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
3793c2798b19SChris Wilson 
3794c2798b19SChris Wilson 	I915_WRITE16(IER,
3795c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3796c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3797c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
3798c2798b19SChris Wilson 	POSTING_READ16(IER);
3799c2798b19SChris Wilson 
3800379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3801379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3802d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3803755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3804755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3805d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3806379ef82dSDaniel Vetter 
3807c2798b19SChris Wilson 	return 0;
3808c2798b19SChris Wilson }
3809c2798b19SChris Wilson 
381090a72f87SVille Syrjälä /*
381190a72f87SVille Syrjälä  * Returns true when a page flip has completed.
381290a72f87SVille Syrjälä  */
381390a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
38141f1c2e24SVille Syrjälä 			       int plane, int pipe, u32 iir)
381590a72f87SVille Syrjälä {
38162d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
38171f1c2e24SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
381890a72f87SVille Syrjälä 
38198d7849dbSVille Syrjälä 	if (!intel_pipe_handle_vblank(dev, pipe))
382090a72f87SVille Syrjälä 		return false;
382190a72f87SVille Syrjälä 
382290a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
3823d6bbafa1SChris Wilson 		goto check_page_flip;
382490a72f87SVille Syrjälä 
382590a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
382690a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
382790a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
382890a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
382990a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
383090a72f87SVille Syrjälä 	 */
383190a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
3832d6bbafa1SChris Wilson 		goto check_page_flip;
383390a72f87SVille Syrjälä 
38347d47559eSVille Syrjälä 	intel_prepare_page_flip(dev, plane);
383590a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
383690a72f87SVille Syrjälä 	return true;
3837d6bbafa1SChris Wilson 
3838d6bbafa1SChris Wilson check_page_flip:
3839d6bbafa1SChris Wilson 	intel_check_page_flip(dev, pipe);
3840d6bbafa1SChris Wilson 	return false;
384190a72f87SVille Syrjälä }
384290a72f87SVille Syrjälä 
3843ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3844c2798b19SChris Wilson {
384545a83f84SDaniel Vetter 	struct drm_device *dev = arg;
38462d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3847c2798b19SChris Wilson 	u16 iir, new_iir;
3848c2798b19SChris Wilson 	u32 pipe_stats[2];
3849c2798b19SChris Wilson 	int pipe;
3850c2798b19SChris Wilson 	u16 flip_mask =
3851c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3852c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3853c2798b19SChris Wilson 
38542dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
38552dd2a883SImre Deak 		return IRQ_NONE;
38562dd2a883SImre Deak 
3857c2798b19SChris Wilson 	iir = I915_READ16(IIR);
3858c2798b19SChris Wilson 	if (iir == 0)
3859c2798b19SChris Wilson 		return IRQ_NONE;
3860c2798b19SChris Wilson 
3861c2798b19SChris Wilson 	while (iir & ~flip_mask) {
3862c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3863c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3864c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3865c2798b19SChris Wilson 		 * interrupts (for non-MSI).
3866c2798b19SChris Wilson 		 */
3867222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
3868c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3869aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
3870c2798b19SChris Wilson 
3871055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
3872c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
3873c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3874c2798b19SChris Wilson 
3875c2798b19SChris Wilson 			/*
3876c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3877c2798b19SChris Wilson 			 */
38782d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & 0x8000ffff)
3879c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3880c2798b19SChris Wilson 		}
3881222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
3882c2798b19SChris Wilson 
3883c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
3884c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
3885c2798b19SChris Wilson 
3886c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
388774cdb337SChris Wilson 			notify_ring(&dev_priv->ring[RCS]);
3888c2798b19SChris Wilson 
3889055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
38901f1c2e24SVille Syrjälä 			int plane = pipe;
38913a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
38921f1c2e24SVille Syrjälä 				plane = !plane;
38931f1c2e24SVille Syrjälä 
38944356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
38951f1c2e24SVille Syrjälä 			    i8xx_handle_vblank(dev, plane, pipe, iir))
38961f1c2e24SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3897c2798b19SChris Wilson 
38984356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3899277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
39002d9d2b0bSVille Syrjälä 
39011f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
39021f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
39031f7247c0SDaniel Vetter 								    pipe);
39044356d586SDaniel Vetter 		}
3905c2798b19SChris Wilson 
3906c2798b19SChris Wilson 		iir = new_iir;
3907c2798b19SChris Wilson 	}
3908c2798b19SChris Wilson 
3909c2798b19SChris Wilson 	return IRQ_HANDLED;
3910c2798b19SChris Wilson }
3911c2798b19SChris Wilson 
3912c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
3913c2798b19SChris Wilson {
39142d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3915c2798b19SChris Wilson 	int pipe;
3916c2798b19SChris Wilson 
3917055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
3918c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
3919c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3920c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
3921c2798b19SChris Wilson 	}
3922c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3923c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3924c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
3925c2798b19SChris Wilson }
3926c2798b19SChris Wilson 
3927a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
3928a266c7d5SChris Wilson {
39292d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3930a266c7d5SChris Wilson 	int pipe;
3931a266c7d5SChris Wilson 
3932a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
39330706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
3934a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3935a266c7d5SChris Wilson 	}
3936a266c7d5SChris Wilson 
393700d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
3938055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3939a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3940a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3941a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3942a266c7d5SChris Wilson 	POSTING_READ(IER);
3943a266c7d5SChris Wilson }
3944a266c7d5SChris Wilson 
3945a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
3946a266c7d5SChris Wilson {
39472d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
394838bde180SChris Wilson 	u32 enable_mask;
3949a266c7d5SChris Wilson 
395038bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
395138bde180SChris Wilson 
395238bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
395338bde180SChris Wilson 	dev_priv->irq_mask =
395438bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
395538bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
395638bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
395738bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
395837ef01abSDaniel Vetter 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
395938bde180SChris Wilson 
396038bde180SChris Wilson 	enable_mask =
396138bde180SChris Wilson 		I915_ASLE_INTERRUPT |
396238bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
396338bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
396438bde180SChris Wilson 		I915_USER_INTERRUPT;
396538bde180SChris Wilson 
3966a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
39670706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
396820afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
396920afbda2SDaniel Vetter 
3970a266c7d5SChris Wilson 		/* Enable in IER... */
3971a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
3972a266c7d5SChris Wilson 		/* and unmask in IMR */
3973a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
3974a266c7d5SChris Wilson 	}
3975a266c7d5SChris Wilson 
3976a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3977a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3978a266c7d5SChris Wilson 	POSTING_READ(IER);
3979a266c7d5SChris Wilson 
3980f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
398120afbda2SDaniel Vetter 
3982379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3983379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3984d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3985755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3986755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3987d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3988379ef82dSDaniel Vetter 
398920afbda2SDaniel Vetter 	return 0;
399020afbda2SDaniel Vetter }
399120afbda2SDaniel Vetter 
399290a72f87SVille Syrjälä /*
399390a72f87SVille Syrjälä  * Returns true when a page flip has completed.
399490a72f87SVille Syrjälä  */
399590a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
399690a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
399790a72f87SVille Syrjälä {
39982d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
399990a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
400090a72f87SVille Syrjälä 
40018d7849dbSVille Syrjälä 	if (!intel_pipe_handle_vblank(dev, pipe))
400290a72f87SVille Syrjälä 		return false;
400390a72f87SVille Syrjälä 
400490a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
4005d6bbafa1SChris Wilson 		goto check_page_flip;
400690a72f87SVille Syrjälä 
400790a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
400890a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
400990a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
401090a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
401190a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
401290a72f87SVille Syrjälä 	 */
401390a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
4014d6bbafa1SChris Wilson 		goto check_page_flip;
401590a72f87SVille Syrjälä 
40167d47559eSVille Syrjälä 	intel_prepare_page_flip(dev, plane);
401790a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
401890a72f87SVille Syrjälä 	return true;
4019d6bbafa1SChris Wilson 
4020d6bbafa1SChris Wilson check_page_flip:
4021d6bbafa1SChris Wilson 	intel_check_page_flip(dev, pipe);
4022d6bbafa1SChris Wilson 	return false;
402390a72f87SVille Syrjälä }
402490a72f87SVille Syrjälä 
4025ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
4026a266c7d5SChris Wilson {
402745a83f84SDaniel Vetter 	struct drm_device *dev = arg;
40282d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
40298291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
403038bde180SChris Wilson 	u32 flip_mask =
403138bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
403238bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
403338bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
4034a266c7d5SChris Wilson 
40352dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
40362dd2a883SImre Deak 		return IRQ_NONE;
40372dd2a883SImre Deak 
4038a266c7d5SChris Wilson 	iir = I915_READ(IIR);
403938bde180SChris Wilson 	do {
404038bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
40418291ee90SChris Wilson 		bool blc_event = false;
4042a266c7d5SChris Wilson 
4043a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4044a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4045a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4046a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
4047a266c7d5SChris Wilson 		 */
4048222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
4049a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4050aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4051a266c7d5SChris Wilson 
4052055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4053a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
4054a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4055a266c7d5SChris Wilson 
405638bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
4057a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4058a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
405938bde180SChris Wilson 				irq_received = true;
4060a266c7d5SChris Wilson 			}
4061a266c7d5SChris Wilson 		}
4062222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4063a266c7d5SChris Wilson 
4064a266c7d5SChris Wilson 		if (!irq_received)
4065a266c7d5SChris Wilson 			break;
4066a266c7d5SChris Wilson 
4067a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
406816c6c56bSVille Syrjälä 		if (I915_HAS_HOTPLUG(dev) &&
406916c6c56bSVille Syrjälä 		    iir & I915_DISPLAY_PORT_INTERRUPT)
407016c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
4071a266c7d5SChris Wilson 
407238bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
4073a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4074a266c7d5SChris Wilson 
4075a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
407674cdb337SChris Wilson 			notify_ring(&dev_priv->ring[RCS]);
4077a266c7d5SChris Wilson 
4078055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
407938bde180SChris Wilson 			int plane = pipe;
40803a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
408138bde180SChris Wilson 				plane = !plane;
40825e2032d4SVille Syrjälä 
408390a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
408490a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
408590a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
4086a266c7d5SChris Wilson 
4087a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4088a266c7d5SChris Wilson 				blc_event = true;
40894356d586SDaniel Vetter 
40904356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
4091277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
40922d9d2b0bSVille Syrjälä 
40931f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
40941f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
40951f7247c0SDaniel Vetter 								    pipe);
4096a266c7d5SChris Wilson 		}
4097a266c7d5SChris Wilson 
4098a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
4099a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
4100a266c7d5SChris Wilson 
4101a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4102a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4103a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4104a266c7d5SChris Wilson 		 * we would never get another interrupt.
4105a266c7d5SChris Wilson 		 *
4106a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4107a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4108a266c7d5SChris Wilson 		 * another one.
4109a266c7d5SChris Wilson 		 *
4110a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4111a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4112a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4113a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4114a266c7d5SChris Wilson 		 * stray interrupts.
4115a266c7d5SChris Wilson 		 */
411638bde180SChris Wilson 		ret = IRQ_HANDLED;
4117a266c7d5SChris Wilson 		iir = new_iir;
411838bde180SChris Wilson 	} while (iir & ~flip_mask);
4119a266c7d5SChris Wilson 
4120a266c7d5SChris Wilson 	return ret;
4121a266c7d5SChris Wilson }
4122a266c7d5SChris Wilson 
4123a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
4124a266c7d5SChris Wilson {
41252d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4126a266c7d5SChris Wilson 	int pipe;
4127a266c7d5SChris Wilson 
4128a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
41290706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4130a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4131a266c7d5SChris Wilson 	}
4132a266c7d5SChris Wilson 
413300d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
4134055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
413555b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
4136a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
413755b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
413855b39755SChris Wilson 	}
4139a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4140a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4141a266c7d5SChris Wilson 
4142a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4143a266c7d5SChris Wilson }
4144a266c7d5SChris Wilson 
4145a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
4146a266c7d5SChris Wilson {
41472d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4148a266c7d5SChris Wilson 	int pipe;
4149a266c7d5SChris Wilson 
41500706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4151a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4152a266c7d5SChris Wilson 
4153a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
4154055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4155a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4156a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4157a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4158a266c7d5SChris Wilson 	POSTING_READ(IER);
4159a266c7d5SChris Wilson }
4160a266c7d5SChris Wilson 
4161a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
4162a266c7d5SChris Wilson {
41632d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4164bbba0a97SChris Wilson 	u32 enable_mask;
4165a266c7d5SChris Wilson 	u32 error_mask;
4166a266c7d5SChris Wilson 
4167a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
4168bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
4169adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
4170bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4171bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
4172bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
4173bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
4174bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
4175bbba0a97SChris Wilson 
4176bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
417721ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
417821ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
4179bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
4180bbba0a97SChris Wilson 
4181bbba0a97SChris Wilson 	if (IS_G4X(dev))
4182bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
4183a266c7d5SChris Wilson 
4184b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4185b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4186d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4187755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
4188755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4189755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4190d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4191a266c7d5SChris Wilson 
4192a266c7d5SChris Wilson 	/*
4193a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
4194a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
4195a266c7d5SChris Wilson 	 */
4196a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
4197a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
4198a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
4199a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
4200a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4201a266c7d5SChris Wilson 	} else {
4202a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
4203a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4204a266c7d5SChris Wilson 	}
4205a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
4206a266c7d5SChris Wilson 
4207a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
4208a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
4209a266c7d5SChris Wilson 	POSTING_READ(IER);
4210a266c7d5SChris Wilson 
42110706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
421220afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
421320afbda2SDaniel Vetter 
4214f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
421520afbda2SDaniel Vetter 
421620afbda2SDaniel Vetter 	return 0;
421720afbda2SDaniel Vetter }
421820afbda2SDaniel Vetter 
4219bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
422020afbda2SDaniel Vetter {
42212d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
422220afbda2SDaniel Vetter 	u32 hotplug_en;
422320afbda2SDaniel Vetter 
4224b5ea2d56SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
4225b5ea2d56SDaniel Vetter 
4226adca4730SChris Wilson 	/* Note HDMI and DP share hotplug bits */
4227e5868a31SEgbert Eich 	/* enable bits are the same for all generations */
42280706f17cSEgbert Eich 	hotplug_en = intel_hpd_enabled_irqs(dev, hpd_mask_i915);
4229a266c7d5SChris Wilson 	/* Programming the CRT detection parameters tends
4230a266c7d5SChris Wilson 	   to generate a spurious hotplug event about three
4231a266c7d5SChris Wilson 	   seconds later.  So just do it once.
4232a266c7d5SChris Wilson 	*/
4233a266c7d5SChris Wilson 	if (IS_G4X(dev))
4234a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
4235a266c7d5SChris Wilson 	hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
4236a266c7d5SChris Wilson 
4237a266c7d5SChris Wilson 	/* Ignore TV since it's buggy */
42380706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv,
4239f9e3dc78SJani Nikula 					     HOTPLUG_INT_EN_MASK |
4240f9e3dc78SJani Nikula 					     CRT_HOTPLUG_VOLTAGE_COMPARE_MASK |
4241f9e3dc78SJani Nikula 					     CRT_HOTPLUG_ACTIVATION_PERIOD_64,
42420706f17cSEgbert Eich 					     hotplug_en);
4243a266c7d5SChris Wilson }
4244a266c7d5SChris Wilson 
4245ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
4246a266c7d5SChris Wilson {
424745a83f84SDaniel Vetter 	struct drm_device *dev = arg;
42482d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4249a266c7d5SChris Wilson 	u32 iir, new_iir;
4250a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
4251a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
425221ad8330SVille Syrjälä 	u32 flip_mask =
425321ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
425421ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
4255a266c7d5SChris Wilson 
42562dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
42572dd2a883SImre Deak 		return IRQ_NONE;
42582dd2a883SImre Deak 
4259a266c7d5SChris Wilson 	iir = I915_READ(IIR);
4260a266c7d5SChris Wilson 
4261a266c7d5SChris Wilson 	for (;;) {
4262501e01d7SVille Syrjälä 		bool irq_received = (iir & ~flip_mask) != 0;
42632c8ba29fSChris Wilson 		bool blc_event = false;
42642c8ba29fSChris Wilson 
4265a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4266a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4267a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4268a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
4269a266c7d5SChris Wilson 		 */
4270222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
4271a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4272aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4273a266c7d5SChris Wilson 
4274055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4275a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
4276a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4277a266c7d5SChris Wilson 
4278a266c7d5SChris Wilson 			/*
4279a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
4280a266c7d5SChris Wilson 			 */
4281a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4282a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
4283501e01d7SVille Syrjälä 				irq_received = true;
4284a266c7d5SChris Wilson 			}
4285a266c7d5SChris Wilson 		}
4286222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4287a266c7d5SChris Wilson 
4288a266c7d5SChris Wilson 		if (!irq_received)
4289a266c7d5SChris Wilson 			break;
4290a266c7d5SChris Wilson 
4291a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
4292a266c7d5SChris Wilson 
4293a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
429416c6c56bSVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
429516c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
4296a266c7d5SChris Wilson 
429721ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
4298a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4299a266c7d5SChris Wilson 
4300a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
430174cdb337SChris Wilson 			notify_ring(&dev_priv->ring[RCS]);
4302a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
430374cdb337SChris Wilson 			notify_ring(&dev_priv->ring[VCS]);
4304a266c7d5SChris Wilson 
4305055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
43062c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
430790a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
430890a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
4309a266c7d5SChris Wilson 
4310a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4311a266c7d5SChris Wilson 				blc_event = true;
43124356d586SDaniel Vetter 
43134356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
4314277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
4315a266c7d5SChris Wilson 
43161f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
43171f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
43182d9d2b0bSVille Syrjälä 		}
4319a266c7d5SChris Wilson 
4320a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
4321a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
4322a266c7d5SChris Wilson 
4323515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
4324515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
4325515ac2bbSDaniel Vetter 
4326a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4327a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4328a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4329a266c7d5SChris Wilson 		 * we would never get another interrupt.
4330a266c7d5SChris Wilson 		 *
4331a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4332a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4333a266c7d5SChris Wilson 		 * another one.
4334a266c7d5SChris Wilson 		 *
4335a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4336a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4337a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4338a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4339a266c7d5SChris Wilson 		 * stray interrupts.
4340a266c7d5SChris Wilson 		 */
4341a266c7d5SChris Wilson 		iir = new_iir;
4342a266c7d5SChris Wilson 	}
4343a266c7d5SChris Wilson 
4344a266c7d5SChris Wilson 	return ret;
4345a266c7d5SChris Wilson }
4346a266c7d5SChris Wilson 
4347a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
4348a266c7d5SChris Wilson {
43492d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4350a266c7d5SChris Wilson 	int pipe;
4351a266c7d5SChris Wilson 
4352a266c7d5SChris Wilson 	if (!dev_priv)
4353a266c7d5SChris Wilson 		return;
4354a266c7d5SChris Wilson 
43550706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4356a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4357a266c7d5SChris Wilson 
4358a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
4359055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4360a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4361a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4362a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4363a266c7d5SChris Wilson 
4364055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4365a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
4366a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
4367a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4368a266c7d5SChris Wilson }
4369a266c7d5SChris Wilson 
4370fca52a55SDaniel Vetter /**
4371fca52a55SDaniel Vetter  * intel_irq_init - initializes irq support
4372fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4373fca52a55SDaniel Vetter  *
4374fca52a55SDaniel Vetter  * This function initializes all the irq support including work items, timers
4375fca52a55SDaniel Vetter  * and all the vtables. It does not setup the interrupt itself though.
4376fca52a55SDaniel Vetter  */
4377b963291cSDaniel Vetter void intel_irq_init(struct drm_i915_private *dev_priv)
4378f71d4af4SJesse Barnes {
4379b963291cSDaniel Vetter 	struct drm_device *dev = dev_priv->dev;
43808b2e326dSChris Wilson 
438177913b39SJani Nikula 	intel_hpd_init_work(dev_priv);
438277913b39SJani Nikula 
4383c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
4384a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
43858b2e326dSChris Wilson 
4386a6706b45SDeepak S 	/* Let's track the enabled rps events */
4387b963291cSDaniel Vetter 	if (IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv))
43886c65a587SVille Syrjälä 		/* WaGsvRC0ResidencyMethod:vlv */
43896f4b12f8SChris Wilson 		dev_priv->pm_rps_events = GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED;
439031685c25SDeepak S 	else
4391a6706b45SDeepak S 		dev_priv->pm_rps_events = GEN6_PM_RPS_EVENTS;
4392a6706b45SDeepak S 
4393737b1506SChris Wilson 	INIT_DELAYED_WORK(&dev_priv->gpu_error.hangcheck_work,
4394737b1506SChris Wilson 			  i915_hangcheck_elapsed);
439561bac78eSDaniel Vetter 
439697a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
43979ee32feaSDaniel Vetter 
4398b963291cSDaniel Vetter 	if (IS_GEN2(dev_priv)) {
43994cdb83ecSVille Syrjälä 		dev->max_vblank_count = 0;
44004cdb83ecSVille Syrjälä 		dev->driver->get_vblank_counter = i8xx_get_vblank_counter;
4401b963291cSDaniel Vetter 	} else if (IS_G4X(dev_priv) || INTEL_INFO(dev_priv)->gen >= 5) {
4402f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
4403fd8f507cSVille Syrjälä 		dev->driver->get_vblank_counter = g4x_get_vblank_counter;
4404391f75e2SVille Syrjälä 	} else {
4405391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
4406391f75e2SVille Syrjälä 		dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
4407f71d4af4SJesse Barnes 	}
4408f71d4af4SJesse Barnes 
440921da2700SVille Syrjälä 	/*
441021da2700SVille Syrjälä 	 * Opt out of the vblank disable timer on everything except gen2.
441121da2700SVille Syrjälä 	 * Gen2 doesn't have a hardware frame counter and so depends on
441221da2700SVille Syrjälä 	 * vblank interrupts to produce sane vblank seuquence numbers.
441321da2700SVille Syrjälä 	 */
4414b963291cSDaniel Vetter 	if (!IS_GEN2(dev_priv))
441521da2700SVille Syrjälä 		dev->vblank_disable_immediate = true;
441621da2700SVille Syrjälä 
4417f71d4af4SJesse Barnes 	dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
4418f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
4419f71d4af4SJesse Barnes 
4420b963291cSDaniel Vetter 	if (IS_CHERRYVIEW(dev_priv)) {
442143f328d7SVille Syrjälä 		dev->driver->irq_handler = cherryview_irq_handler;
442243f328d7SVille Syrjälä 		dev->driver->irq_preinstall = cherryview_irq_preinstall;
442343f328d7SVille Syrjälä 		dev->driver->irq_postinstall = cherryview_irq_postinstall;
442443f328d7SVille Syrjälä 		dev->driver->irq_uninstall = cherryview_irq_uninstall;
442543f328d7SVille Syrjälä 		dev->driver->enable_vblank = valleyview_enable_vblank;
442643f328d7SVille Syrjälä 		dev->driver->disable_vblank = valleyview_disable_vblank;
442743f328d7SVille Syrjälä 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4428b963291cSDaniel Vetter 	} else if (IS_VALLEYVIEW(dev_priv)) {
44297e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
44307e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
44317e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
44327e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
44337e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
44347e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
4435fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4436b963291cSDaniel Vetter 	} else if (INTEL_INFO(dev_priv)->gen >= 8) {
4437abd58f01SBen Widawsky 		dev->driver->irq_handler = gen8_irq_handler;
4438723761b8SDaniel Vetter 		dev->driver->irq_preinstall = gen8_irq_reset;
4439abd58f01SBen Widawsky 		dev->driver->irq_postinstall = gen8_irq_postinstall;
4440abd58f01SBen Widawsky 		dev->driver->irq_uninstall = gen8_irq_uninstall;
4441abd58f01SBen Widawsky 		dev->driver->enable_vblank = gen8_enable_vblank;
4442abd58f01SBen Widawsky 		dev->driver->disable_vblank = gen8_disable_vblank;
44436dbf30ceSVille Syrjälä 		if (IS_BROXTON(dev))
4444e0a20ad7SShashank Sharma 			dev_priv->display.hpd_irq_setup = bxt_hpd_irq_setup;
44456dbf30ceSVille Syrjälä 		else if (HAS_PCH_SPT(dev))
44466dbf30ceSVille Syrjälä 			dev_priv->display.hpd_irq_setup = spt_hpd_irq_setup;
44476dbf30ceSVille Syrjälä 		else
44483a3b3c7dSVille Syrjälä 			dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
4449f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
4450f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
4451723761b8SDaniel Vetter 		dev->driver->irq_preinstall = ironlake_irq_reset;
4452f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
4453f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
4454f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
4455f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
4456e4ce95aaSVille Syrjälä 		dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
4457f71d4af4SJesse Barnes 	} else {
4458b963291cSDaniel Vetter 		if (INTEL_INFO(dev_priv)->gen == 2) {
4459c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
4460c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
4461c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
4462c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
4463b963291cSDaniel Vetter 		} else if (INTEL_INFO(dev_priv)->gen == 3) {
4464a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
4465a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
4466a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
4467a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
4468c2798b19SChris Wilson 		} else {
4469a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
4470a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
4471a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
4472a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
4473c2798b19SChris Wilson 		}
4474778eb334SVille Syrjälä 		if (I915_HAS_HOTPLUG(dev_priv))
4475778eb334SVille Syrjälä 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4476f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
4477f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
4478f71d4af4SJesse Barnes 	}
4479f71d4af4SJesse Barnes }
448020afbda2SDaniel Vetter 
4481fca52a55SDaniel Vetter /**
4482fca52a55SDaniel Vetter  * intel_irq_install - enables the hardware interrupt
4483fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4484fca52a55SDaniel Vetter  *
4485fca52a55SDaniel Vetter  * This function enables the hardware interrupt handling, but leaves the hotplug
4486fca52a55SDaniel Vetter  * handling still disabled. It is called after intel_irq_init().
4487fca52a55SDaniel Vetter  *
4488fca52a55SDaniel Vetter  * In the driver load and resume code we need working interrupts in a few places
4489fca52a55SDaniel Vetter  * but don't want to deal with the hassle of concurrent probe and hotplug
4490fca52a55SDaniel Vetter  * workers. Hence the split into this two-stage approach.
4491fca52a55SDaniel Vetter  */
44922aeb7d3aSDaniel Vetter int intel_irq_install(struct drm_i915_private *dev_priv)
44932aeb7d3aSDaniel Vetter {
44942aeb7d3aSDaniel Vetter 	/*
44952aeb7d3aSDaniel Vetter 	 * We enable some interrupt sources in our postinstall hooks, so mark
44962aeb7d3aSDaniel Vetter 	 * interrupts as enabled _before_ actually enabling them to avoid
44972aeb7d3aSDaniel Vetter 	 * special cases in our ordering checks.
44982aeb7d3aSDaniel Vetter 	 */
44992aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
45002aeb7d3aSDaniel Vetter 
45012aeb7d3aSDaniel Vetter 	return drm_irq_install(dev_priv->dev, dev_priv->dev->pdev->irq);
45022aeb7d3aSDaniel Vetter }
45032aeb7d3aSDaniel Vetter 
4504fca52a55SDaniel Vetter /**
4505fca52a55SDaniel Vetter  * intel_irq_uninstall - finilizes all irq handling
4506fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4507fca52a55SDaniel Vetter  *
4508fca52a55SDaniel Vetter  * This stops interrupt and hotplug handling and unregisters and frees all
4509fca52a55SDaniel Vetter  * resources acquired in the init functions.
4510fca52a55SDaniel Vetter  */
45112aeb7d3aSDaniel Vetter void intel_irq_uninstall(struct drm_i915_private *dev_priv)
45122aeb7d3aSDaniel Vetter {
45132aeb7d3aSDaniel Vetter 	drm_irq_uninstall(dev_priv->dev);
45142aeb7d3aSDaniel Vetter 	intel_hpd_cancel_work(dev_priv);
45152aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
45162aeb7d3aSDaniel Vetter }
45172aeb7d3aSDaniel Vetter 
4518fca52a55SDaniel Vetter /**
4519fca52a55SDaniel Vetter  * intel_runtime_pm_disable_interrupts - runtime interrupt disabling
4520fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4521fca52a55SDaniel Vetter  *
4522fca52a55SDaniel Vetter  * This function is used to disable interrupts at runtime, both in the runtime
4523fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4524fca52a55SDaniel Vetter  */
4525b963291cSDaniel Vetter void intel_runtime_pm_disable_interrupts(struct drm_i915_private *dev_priv)
4526c67a470bSPaulo Zanoni {
4527b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_uninstall(dev_priv->dev);
45282aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
45292dd2a883SImre Deak 	synchronize_irq(dev_priv->dev->irq);
4530c67a470bSPaulo Zanoni }
4531c67a470bSPaulo Zanoni 
4532fca52a55SDaniel Vetter /**
4533fca52a55SDaniel Vetter  * intel_runtime_pm_enable_interrupts - runtime interrupt enabling
4534fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4535fca52a55SDaniel Vetter  *
4536fca52a55SDaniel Vetter  * This function is used to enable interrupts at runtime, both in the runtime
4537fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4538fca52a55SDaniel Vetter  */
4539b963291cSDaniel Vetter void intel_runtime_pm_enable_interrupts(struct drm_i915_private *dev_priv)
4540c67a470bSPaulo Zanoni {
45412aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
4542b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_preinstall(dev_priv->dev);
4543b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_postinstall(dev_priv->dev);
4544c67a470bSPaulo Zanoni }
4545