xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 7bad74d57c81b08218cc564af3a78427e12bf3ac)
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  */
142337ba017SPaulo Zanoni #define GEN5_ASSERT_IIR_IS_ZERO(reg) do { \
143337ba017SPaulo Zanoni 	u32 val = I915_READ(reg); \
144337ba017SPaulo Zanoni 	if (val) { \
145337ba017SPaulo Zanoni 		WARN(1, "Interrupt register 0x%x is not zero: 0x%08x\n", \
146337ba017SPaulo Zanoni 		     (reg), val); \
147337ba017SPaulo Zanoni 		I915_WRITE((reg), 0xffffffff); \
148337ba017SPaulo Zanoni 		POSTING_READ(reg); \
149337ba017SPaulo Zanoni 		I915_WRITE((reg), 0xffffffff); \
150337ba017SPaulo Zanoni 		POSTING_READ(reg); \
151337ba017SPaulo Zanoni 	} \
152337ba017SPaulo Zanoni } while (0)
153337ba017SPaulo Zanoni 
15435079899SPaulo Zanoni #define GEN8_IRQ_INIT_NDX(type, which, imr_val, ier_val) do { \
155337ba017SPaulo Zanoni 	GEN5_ASSERT_IIR_IS_ZERO(GEN8_##type##_IIR(which)); \
15635079899SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IER(which), (ier_val)); \
1577d1bd539SVille Syrjälä 	I915_WRITE(GEN8_##type##_IMR(which), (imr_val)); \
1587d1bd539SVille Syrjälä 	POSTING_READ(GEN8_##type##_IMR(which)); \
15935079899SPaulo Zanoni } while (0)
16035079899SPaulo Zanoni 
16135079899SPaulo Zanoni #define GEN5_IRQ_INIT(type, imr_val, ier_val) do { \
162337ba017SPaulo Zanoni 	GEN5_ASSERT_IIR_IS_ZERO(type##IIR); \
16335079899SPaulo Zanoni 	I915_WRITE(type##IER, (ier_val)); \
1647d1bd539SVille Syrjälä 	I915_WRITE(type##IMR, (imr_val)); \
1657d1bd539SVille Syrjälä 	POSTING_READ(type##IMR); \
16635079899SPaulo Zanoni } while (0)
16735079899SPaulo Zanoni 
168c9a9a268SImre Deak static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir);
169c9a9a268SImre Deak 
1700706f17cSEgbert Eich /* For display hotplug interrupt */
1710706f17cSEgbert Eich static inline void
1720706f17cSEgbert Eich i915_hotplug_interrupt_update_locked(struct drm_i915_private *dev_priv,
1730706f17cSEgbert Eich 				     uint32_t mask,
1740706f17cSEgbert Eich 				     uint32_t bits)
1750706f17cSEgbert Eich {
1760706f17cSEgbert Eich 	uint32_t val;
1770706f17cSEgbert Eich 
1780706f17cSEgbert Eich 	assert_spin_locked(&dev_priv->irq_lock);
1790706f17cSEgbert Eich 	WARN_ON(bits & ~mask);
1800706f17cSEgbert Eich 
1810706f17cSEgbert Eich 	val = I915_READ(PORT_HOTPLUG_EN);
1820706f17cSEgbert Eich 	val &= ~mask;
1830706f17cSEgbert Eich 	val |= bits;
1840706f17cSEgbert Eich 	I915_WRITE(PORT_HOTPLUG_EN, val);
1850706f17cSEgbert Eich }
1860706f17cSEgbert Eich 
1870706f17cSEgbert Eich /**
1880706f17cSEgbert Eich  * i915_hotplug_interrupt_update - update hotplug interrupt enable
1890706f17cSEgbert Eich  * @dev_priv: driver private
1900706f17cSEgbert Eich  * @mask: bits to update
1910706f17cSEgbert Eich  * @bits: bits to enable
1920706f17cSEgbert Eich  * NOTE: the HPD enable bits are modified both inside and outside
1930706f17cSEgbert Eich  * of an interrupt context. To avoid that read-modify-write cycles
1940706f17cSEgbert Eich  * interfer, these bits are protected by a spinlock. Since this
1950706f17cSEgbert Eich  * function is usually not called from a context where the lock is
1960706f17cSEgbert Eich  * held already, this function acquires the lock itself. A non-locking
1970706f17cSEgbert Eich  * version is also available.
1980706f17cSEgbert Eich  */
1990706f17cSEgbert Eich void i915_hotplug_interrupt_update(struct drm_i915_private *dev_priv,
2000706f17cSEgbert Eich 				   uint32_t mask,
2010706f17cSEgbert Eich 				   uint32_t bits)
2020706f17cSEgbert Eich {
2030706f17cSEgbert Eich 	spin_lock_irq(&dev_priv->irq_lock);
2040706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv, mask, bits);
2050706f17cSEgbert Eich 	spin_unlock_irq(&dev_priv->irq_lock);
2060706f17cSEgbert Eich }
2070706f17cSEgbert Eich 
208d9dc34f1SVille Syrjälä /**
209d9dc34f1SVille Syrjälä  * ilk_update_display_irq - update DEIMR
210d9dc34f1SVille Syrjälä  * @dev_priv: driver private
211d9dc34f1SVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
212d9dc34f1SVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
213d9dc34f1SVille Syrjälä  */
214d9dc34f1SVille Syrjälä static void ilk_update_display_irq(struct drm_i915_private *dev_priv,
215d9dc34f1SVille Syrjälä 				   uint32_t interrupt_mask,
216d9dc34f1SVille Syrjälä 				   uint32_t enabled_irq_mask)
217036a4a7dSZhenyu Wang {
218d9dc34f1SVille Syrjälä 	uint32_t new_val;
219d9dc34f1SVille Syrjälä 
2204bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
2214bc9d430SDaniel Vetter 
222d9dc34f1SVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
223d9dc34f1SVille Syrjälä 
2249df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
225c67a470bSPaulo Zanoni 		return;
226c67a470bSPaulo Zanoni 
227d9dc34f1SVille Syrjälä 	new_val = dev_priv->irq_mask;
228d9dc34f1SVille Syrjälä 	new_val &= ~interrupt_mask;
229d9dc34f1SVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
230d9dc34f1SVille Syrjälä 
231d9dc34f1SVille Syrjälä 	if (new_val != dev_priv->irq_mask) {
232d9dc34f1SVille Syrjälä 		dev_priv->irq_mask = new_val;
2331ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
2343143a2bfSChris Wilson 		POSTING_READ(DEIMR);
235036a4a7dSZhenyu Wang 	}
236036a4a7dSZhenyu Wang }
237036a4a7dSZhenyu Wang 
23847339cd9SDaniel Vetter void
239d9dc34f1SVille Syrjälä ironlake_enable_display_irq(struct drm_i915_private *dev_priv, u32 mask)
240d9dc34f1SVille Syrjälä {
241d9dc34f1SVille Syrjälä 	ilk_update_display_irq(dev_priv, mask, mask);
242d9dc34f1SVille Syrjälä }
243d9dc34f1SVille Syrjälä 
244d9dc34f1SVille Syrjälä void
2452d1013ddSJani Nikula ironlake_disable_display_irq(struct drm_i915_private *dev_priv, u32 mask)
246036a4a7dSZhenyu Wang {
247d9dc34f1SVille Syrjälä 	ilk_update_display_irq(dev_priv, mask, 0);
248036a4a7dSZhenyu Wang }
249036a4a7dSZhenyu Wang 
25043eaea13SPaulo Zanoni /**
25143eaea13SPaulo Zanoni  * ilk_update_gt_irq - update GTIMR
25243eaea13SPaulo Zanoni  * @dev_priv: driver private
25343eaea13SPaulo Zanoni  * @interrupt_mask: mask of interrupt bits to update
25443eaea13SPaulo Zanoni  * @enabled_irq_mask: mask of interrupt bits to enable
25543eaea13SPaulo Zanoni  */
25643eaea13SPaulo Zanoni static void ilk_update_gt_irq(struct drm_i915_private *dev_priv,
25743eaea13SPaulo Zanoni 			      uint32_t interrupt_mask,
25843eaea13SPaulo Zanoni 			      uint32_t enabled_irq_mask)
25943eaea13SPaulo Zanoni {
26043eaea13SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
26143eaea13SPaulo Zanoni 
26215a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
26315a17aaeSDaniel Vetter 
2649df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
265c67a470bSPaulo Zanoni 		return;
266c67a470bSPaulo Zanoni 
26743eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask &= ~interrupt_mask;
26843eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask |= (~enabled_irq_mask & interrupt_mask);
26943eaea13SPaulo Zanoni 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
27043eaea13SPaulo Zanoni 	POSTING_READ(GTIMR);
27143eaea13SPaulo Zanoni }
27243eaea13SPaulo Zanoni 
273480c8033SDaniel Vetter void gen5_enable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
27443eaea13SPaulo Zanoni {
27543eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, mask);
27643eaea13SPaulo Zanoni }
27743eaea13SPaulo Zanoni 
278480c8033SDaniel Vetter void gen5_disable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
27943eaea13SPaulo Zanoni {
28043eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, 0);
28143eaea13SPaulo Zanoni }
28243eaea13SPaulo Zanoni 
283b900b949SImre Deak static u32 gen6_pm_iir(struct drm_i915_private *dev_priv)
284b900b949SImre Deak {
285b900b949SImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IIR(2) : GEN6_PMIIR;
286b900b949SImre Deak }
287b900b949SImre Deak 
288a72fbc3aSImre Deak static u32 gen6_pm_imr(struct drm_i915_private *dev_priv)
289a72fbc3aSImre Deak {
290a72fbc3aSImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IMR(2) : GEN6_PMIMR;
291a72fbc3aSImre Deak }
292a72fbc3aSImre Deak 
293b900b949SImre Deak static u32 gen6_pm_ier(struct drm_i915_private *dev_priv)
294b900b949SImre Deak {
295b900b949SImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IER(2) : GEN6_PMIER;
296b900b949SImre Deak }
297b900b949SImre Deak 
298edbfdb45SPaulo Zanoni /**
299edbfdb45SPaulo Zanoni   * snb_update_pm_irq - update GEN6_PMIMR
300edbfdb45SPaulo Zanoni   * @dev_priv: driver private
301edbfdb45SPaulo Zanoni   * @interrupt_mask: mask of interrupt bits to update
302edbfdb45SPaulo Zanoni   * @enabled_irq_mask: mask of interrupt bits to enable
303edbfdb45SPaulo Zanoni   */
304edbfdb45SPaulo Zanoni static void snb_update_pm_irq(struct drm_i915_private *dev_priv,
305edbfdb45SPaulo Zanoni 			      uint32_t interrupt_mask,
306edbfdb45SPaulo Zanoni 			      uint32_t enabled_irq_mask)
307edbfdb45SPaulo Zanoni {
308605cd25bSPaulo Zanoni 	uint32_t new_val;
309edbfdb45SPaulo Zanoni 
31015a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
31115a17aaeSDaniel Vetter 
312edbfdb45SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
313edbfdb45SPaulo Zanoni 
314605cd25bSPaulo Zanoni 	new_val = dev_priv->pm_irq_mask;
315f52ecbcfSPaulo Zanoni 	new_val &= ~interrupt_mask;
316f52ecbcfSPaulo Zanoni 	new_val |= (~enabled_irq_mask & interrupt_mask);
317f52ecbcfSPaulo Zanoni 
318605cd25bSPaulo Zanoni 	if (new_val != dev_priv->pm_irq_mask) {
319605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = new_val;
320a72fbc3aSImre Deak 		I915_WRITE(gen6_pm_imr(dev_priv), dev_priv->pm_irq_mask);
321a72fbc3aSImre Deak 		POSTING_READ(gen6_pm_imr(dev_priv));
322edbfdb45SPaulo Zanoni 	}
323f52ecbcfSPaulo Zanoni }
324edbfdb45SPaulo Zanoni 
325480c8033SDaniel Vetter void gen6_enable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
326edbfdb45SPaulo Zanoni {
3279939fba2SImre Deak 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
3289939fba2SImre Deak 		return;
3299939fba2SImre Deak 
330edbfdb45SPaulo Zanoni 	snb_update_pm_irq(dev_priv, mask, mask);
331edbfdb45SPaulo Zanoni }
332edbfdb45SPaulo Zanoni 
3339939fba2SImre Deak static void __gen6_disable_pm_irq(struct drm_i915_private *dev_priv,
3349939fba2SImre Deak 				  uint32_t mask)
3359939fba2SImre Deak {
3369939fba2SImre Deak 	snb_update_pm_irq(dev_priv, mask, 0);
3379939fba2SImre Deak }
3389939fba2SImre Deak 
339480c8033SDaniel Vetter void gen6_disable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
340edbfdb45SPaulo Zanoni {
3419939fba2SImre Deak 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
3429939fba2SImre Deak 		return;
3439939fba2SImre Deak 
3449939fba2SImre Deak 	__gen6_disable_pm_irq(dev_priv, mask);
345edbfdb45SPaulo Zanoni }
346edbfdb45SPaulo Zanoni 
3473cc134e3SImre Deak void gen6_reset_rps_interrupts(struct drm_device *dev)
3483cc134e3SImre Deak {
3493cc134e3SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
3503cc134e3SImre Deak 	uint32_t reg = gen6_pm_iir(dev_priv);
3513cc134e3SImre Deak 
3523cc134e3SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
3533cc134e3SImre Deak 	I915_WRITE(reg, dev_priv->pm_rps_events);
3543cc134e3SImre Deak 	I915_WRITE(reg, dev_priv->pm_rps_events);
3553cc134e3SImre Deak 	POSTING_READ(reg);
356096fad9eSImre Deak 	dev_priv->rps.pm_iir = 0;
3573cc134e3SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
3583cc134e3SImre Deak }
3593cc134e3SImre Deak 
360b900b949SImre Deak void gen6_enable_rps_interrupts(struct drm_device *dev)
361b900b949SImre Deak {
362b900b949SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
363b900b949SImre Deak 
364b900b949SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
36578e68d36SImre Deak 
366b900b949SImre Deak 	WARN_ON(dev_priv->rps.pm_iir);
3673cc134e3SImre Deak 	WARN_ON(I915_READ(gen6_pm_iir(dev_priv)) & dev_priv->pm_rps_events);
368d4d70aa5SImre Deak 	dev_priv->rps.interrupts_enabled = true;
36978e68d36SImre Deak 	I915_WRITE(gen6_pm_ier(dev_priv), I915_READ(gen6_pm_ier(dev_priv)) |
37078e68d36SImre Deak 				dev_priv->pm_rps_events);
371b900b949SImre Deak 	gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
37278e68d36SImre Deak 
373b900b949SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
374b900b949SImre Deak }
375b900b949SImre Deak 
37659d02a1fSImre Deak u32 gen6_sanitize_rps_pm_mask(struct drm_i915_private *dev_priv, u32 mask)
37759d02a1fSImre Deak {
37859d02a1fSImre Deak 	/*
379f24eeb19SImre Deak 	 * SNB,IVB can while VLV,CHV may hard hang on looping batchbuffer
38059d02a1fSImre Deak 	 * if GEN6_PM_UP_EI_EXPIRED is masked.
381f24eeb19SImre Deak 	 *
382f24eeb19SImre Deak 	 * TODO: verify if this can be reproduced on VLV,CHV.
38359d02a1fSImre Deak 	 */
38459d02a1fSImre Deak 	if (INTEL_INFO(dev_priv)->gen <= 7 && !IS_HASWELL(dev_priv))
38559d02a1fSImre Deak 		mask &= ~GEN6_PM_RP_UP_EI_EXPIRED;
38659d02a1fSImre Deak 
38759d02a1fSImre Deak 	if (INTEL_INFO(dev_priv)->gen >= 8)
38859d02a1fSImre Deak 		mask &= ~GEN8_PMINTR_REDIRECT_TO_NON_DISP;
38959d02a1fSImre Deak 
39059d02a1fSImre Deak 	return mask;
39159d02a1fSImre Deak }
39259d02a1fSImre Deak 
393b900b949SImre Deak void gen6_disable_rps_interrupts(struct drm_device *dev)
394b900b949SImre Deak {
395b900b949SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
396b900b949SImre Deak 
397d4d70aa5SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
398d4d70aa5SImre Deak 	dev_priv->rps.interrupts_enabled = false;
399d4d70aa5SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
400d4d70aa5SImre Deak 
401d4d70aa5SImre Deak 	cancel_work_sync(&dev_priv->rps.work);
402d4d70aa5SImre Deak 
4039939fba2SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
4049939fba2SImre Deak 
40559d02a1fSImre Deak 	I915_WRITE(GEN6_PMINTRMSK, gen6_sanitize_rps_pm_mask(dev_priv, ~0));
4069939fba2SImre Deak 
4079939fba2SImre Deak 	__gen6_disable_pm_irq(dev_priv, dev_priv->pm_rps_events);
408b900b949SImre Deak 	I915_WRITE(gen6_pm_ier(dev_priv), I915_READ(gen6_pm_ier(dev_priv)) &
409b900b949SImre Deak 				~dev_priv->pm_rps_events);
41058072ccbSImre Deak 
41158072ccbSImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
41258072ccbSImre Deak 
41358072ccbSImre Deak 	synchronize_irq(dev->irq);
414b900b949SImre Deak }
415b900b949SImre Deak 
4160961021aSBen Widawsky /**
4173a3b3c7dSVille Syrjälä   * bdw_update_port_irq - update DE port interrupt
4183a3b3c7dSVille Syrjälä   * @dev_priv: driver private
4193a3b3c7dSVille Syrjälä   * @interrupt_mask: mask of interrupt bits to update
4203a3b3c7dSVille Syrjälä   * @enabled_irq_mask: mask of interrupt bits to enable
4213a3b3c7dSVille Syrjälä   */
4223a3b3c7dSVille Syrjälä static void bdw_update_port_irq(struct drm_i915_private *dev_priv,
4233a3b3c7dSVille Syrjälä 				uint32_t interrupt_mask,
4243a3b3c7dSVille Syrjälä 				uint32_t enabled_irq_mask)
4253a3b3c7dSVille Syrjälä {
4263a3b3c7dSVille Syrjälä 	uint32_t new_val;
4273a3b3c7dSVille Syrjälä 	uint32_t old_val;
4283a3b3c7dSVille Syrjälä 
4293a3b3c7dSVille Syrjälä 	assert_spin_locked(&dev_priv->irq_lock);
4303a3b3c7dSVille Syrjälä 
4313a3b3c7dSVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
4323a3b3c7dSVille Syrjälä 
4333a3b3c7dSVille Syrjälä 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
4343a3b3c7dSVille Syrjälä 		return;
4353a3b3c7dSVille Syrjälä 
4363a3b3c7dSVille Syrjälä 	old_val = I915_READ(GEN8_DE_PORT_IMR);
4373a3b3c7dSVille Syrjälä 
4383a3b3c7dSVille Syrjälä 	new_val = old_val;
4393a3b3c7dSVille Syrjälä 	new_val &= ~interrupt_mask;
4403a3b3c7dSVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
4413a3b3c7dSVille Syrjälä 
4423a3b3c7dSVille Syrjälä 	if (new_val != old_val) {
4433a3b3c7dSVille Syrjälä 		I915_WRITE(GEN8_DE_PORT_IMR, new_val);
4443a3b3c7dSVille Syrjälä 		POSTING_READ(GEN8_DE_PORT_IMR);
4453a3b3c7dSVille Syrjälä 	}
4463a3b3c7dSVille Syrjälä }
4473a3b3c7dSVille Syrjälä 
4483a3b3c7dSVille Syrjälä /**
449fee884edSDaniel Vetter  * ibx_display_interrupt_update - update SDEIMR
450fee884edSDaniel Vetter  * @dev_priv: driver private
451fee884edSDaniel Vetter  * @interrupt_mask: mask of interrupt bits to update
452fee884edSDaniel Vetter  * @enabled_irq_mask: mask of interrupt bits to enable
453fee884edSDaniel Vetter  */
45447339cd9SDaniel Vetter void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
455fee884edSDaniel Vetter 				  uint32_t interrupt_mask,
456fee884edSDaniel Vetter 				  uint32_t enabled_irq_mask)
457fee884edSDaniel Vetter {
458fee884edSDaniel Vetter 	uint32_t sdeimr = I915_READ(SDEIMR);
459fee884edSDaniel Vetter 	sdeimr &= ~interrupt_mask;
460fee884edSDaniel Vetter 	sdeimr |= (~enabled_irq_mask & interrupt_mask);
461fee884edSDaniel Vetter 
46215a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
46315a17aaeSDaniel Vetter 
464fee884edSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
465fee884edSDaniel Vetter 
4669df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
467c67a470bSPaulo Zanoni 		return;
468c67a470bSPaulo Zanoni 
469fee884edSDaniel Vetter 	I915_WRITE(SDEIMR, sdeimr);
470fee884edSDaniel Vetter 	POSTING_READ(SDEIMR);
471fee884edSDaniel Vetter }
4728664281bSPaulo Zanoni 
473b5ea642aSDaniel Vetter static void
474755e9019SImre Deak __i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
475755e9019SImre Deak 		       u32 enable_mask, u32 status_mask)
4767c463586SKeith Packard {
4779db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
478755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
4797c463586SKeith Packard 
480b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
481d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
482b79480baSDaniel Vetter 
48304feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
48404feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
48504feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
48604feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
487755e9019SImre Deak 		return;
488755e9019SImre Deak 
489755e9019SImre Deak 	if ((pipestat & enable_mask) == enable_mask)
49046c06a30SVille Syrjälä 		return;
49146c06a30SVille Syrjälä 
49291d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] |= status_mask;
49391d181ddSImre Deak 
4947c463586SKeith Packard 	/* Enable the interrupt, clear any pending status */
495755e9019SImre Deak 	pipestat |= enable_mask | status_mask;
49646c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
4973143a2bfSChris Wilson 	POSTING_READ(reg);
4987c463586SKeith Packard }
4997c463586SKeith Packard 
500b5ea642aSDaniel Vetter static void
501755e9019SImre Deak __i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
502755e9019SImre Deak 		        u32 enable_mask, u32 status_mask)
5037c463586SKeith Packard {
5049db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
505755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
5067c463586SKeith Packard 
507b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
508d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
509b79480baSDaniel Vetter 
51004feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
51104feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
51204feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
51304feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
51446c06a30SVille Syrjälä 		return;
51546c06a30SVille Syrjälä 
516755e9019SImre Deak 	if ((pipestat & enable_mask) == 0)
517755e9019SImre Deak 		return;
518755e9019SImre Deak 
51991d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] &= ~status_mask;
52091d181ddSImre Deak 
521755e9019SImre Deak 	pipestat &= ~enable_mask;
52246c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
5233143a2bfSChris Wilson 	POSTING_READ(reg);
5247c463586SKeith Packard }
5257c463586SKeith Packard 
52610c59c51SImre Deak static u32 vlv_get_pipestat_enable_mask(struct drm_device *dev, u32 status_mask)
52710c59c51SImre Deak {
52810c59c51SImre Deak 	u32 enable_mask = status_mask << 16;
52910c59c51SImre Deak 
53010c59c51SImre Deak 	/*
531724a6905SVille Syrjälä 	 * On pipe A we don't support the PSR interrupt yet,
532724a6905SVille Syrjälä 	 * on pipe B and C the same bit MBZ.
53310c59c51SImre Deak 	 */
53410c59c51SImre Deak 	if (WARN_ON_ONCE(status_mask & PIPE_A_PSR_STATUS_VLV))
53510c59c51SImre Deak 		return 0;
536724a6905SVille Syrjälä 	/*
537724a6905SVille Syrjälä 	 * On pipe B and C we don't support the PSR interrupt yet, on pipe
538724a6905SVille Syrjälä 	 * A the same bit is for perf counters which we don't use either.
539724a6905SVille Syrjälä 	 */
540724a6905SVille Syrjälä 	if (WARN_ON_ONCE(status_mask & PIPE_B_PSR_STATUS_VLV))
541724a6905SVille Syrjälä 		return 0;
54210c59c51SImre Deak 
54310c59c51SImre Deak 	enable_mask &= ~(PIPE_FIFO_UNDERRUN_STATUS |
54410c59c51SImre Deak 			 SPRITE0_FLIP_DONE_INT_EN_VLV |
54510c59c51SImre Deak 			 SPRITE1_FLIP_DONE_INT_EN_VLV);
54610c59c51SImre Deak 	if (status_mask & SPRITE0_FLIP_DONE_INT_STATUS_VLV)
54710c59c51SImre Deak 		enable_mask |= SPRITE0_FLIP_DONE_INT_EN_VLV;
54810c59c51SImre Deak 	if (status_mask & SPRITE1_FLIP_DONE_INT_STATUS_VLV)
54910c59c51SImre Deak 		enable_mask |= SPRITE1_FLIP_DONE_INT_EN_VLV;
55010c59c51SImre Deak 
55110c59c51SImre Deak 	return enable_mask;
55210c59c51SImre Deak }
55310c59c51SImre Deak 
554755e9019SImre Deak void
555755e9019SImre Deak i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
556755e9019SImre Deak 		     u32 status_mask)
557755e9019SImre Deak {
558755e9019SImre Deak 	u32 enable_mask;
559755e9019SImre Deak 
56010c59c51SImre Deak 	if (IS_VALLEYVIEW(dev_priv->dev))
56110c59c51SImre Deak 		enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
56210c59c51SImre Deak 							   status_mask);
56310c59c51SImre Deak 	else
564755e9019SImre Deak 		enable_mask = status_mask << 16;
565755e9019SImre Deak 	__i915_enable_pipestat(dev_priv, pipe, enable_mask, status_mask);
566755e9019SImre Deak }
567755e9019SImre Deak 
568755e9019SImre Deak void
569755e9019SImre Deak i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
570755e9019SImre Deak 		      u32 status_mask)
571755e9019SImre Deak {
572755e9019SImre Deak 	u32 enable_mask;
573755e9019SImre Deak 
57410c59c51SImre Deak 	if (IS_VALLEYVIEW(dev_priv->dev))
57510c59c51SImre Deak 		enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
57610c59c51SImre Deak 							   status_mask);
57710c59c51SImre Deak 	else
578755e9019SImre Deak 		enable_mask = status_mask << 16;
579755e9019SImre Deak 	__i915_disable_pipestat(dev_priv, pipe, enable_mask, status_mask);
580755e9019SImre Deak }
581755e9019SImre Deak 
582c0e09200SDave Airlie /**
583f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
58401c66889SZhao Yakui  */
585f49e38ddSJani Nikula static void i915_enable_asle_pipestat(struct drm_device *dev)
58601c66889SZhao Yakui {
5872d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
5881ec14ad3SChris Wilson 
589f49e38ddSJani Nikula 	if (!dev_priv->opregion.asle || !IS_MOBILE(dev))
590f49e38ddSJani Nikula 		return;
591f49e38ddSJani Nikula 
59213321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
59301c66889SZhao Yakui 
594755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_STATUS);
595a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4)
5963b6c42e8SDaniel Vetter 		i915_enable_pipestat(dev_priv, PIPE_A,
597755e9019SImre Deak 				     PIPE_LEGACY_BLC_EVENT_STATUS);
5981ec14ad3SChris Wilson 
59913321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
60001c66889SZhao Yakui }
60101c66889SZhao Yakui 
602f75f3746SVille Syrjälä /*
603f75f3746SVille Syrjälä  * This timing diagram depicts the video signal in and
604f75f3746SVille Syrjälä  * around the vertical blanking period.
605f75f3746SVille Syrjälä  *
606f75f3746SVille Syrjälä  * Assumptions about the fictitious mode used in this example:
607f75f3746SVille Syrjälä  *  vblank_start >= 3
608f75f3746SVille Syrjälä  *  vsync_start = vblank_start + 1
609f75f3746SVille Syrjälä  *  vsync_end = vblank_start + 2
610f75f3746SVille Syrjälä  *  vtotal = vblank_start + 3
611f75f3746SVille Syrjälä  *
612f75f3746SVille Syrjälä  *           start of vblank:
613f75f3746SVille Syrjälä  *           latch double buffered registers
614f75f3746SVille Syrjälä  *           increment frame counter (ctg+)
615f75f3746SVille Syrjälä  *           generate start of vblank interrupt (gen4+)
616f75f3746SVille Syrjälä  *           |
617f75f3746SVille Syrjälä  *           |          frame start:
618f75f3746SVille Syrjälä  *           |          generate frame start interrupt (aka. vblank interrupt) (gmch)
619f75f3746SVille Syrjälä  *           |          may be shifted forward 1-3 extra lines via PIPECONF
620f75f3746SVille Syrjälä  *           |          |
621f75f3746SVille Syrjälä  *           |          |  start of vsync:
622f75f3746SVille Syrjälä  *           |          |  generate vsync interrupt
623f75f3746SVille Syrjälä  *           |          |  |
624f75f3746SVille Syrjälä  * ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx
625f75f3746SVille Syrjälä  *       .   \hs/   .      \hs/          \hs/          \hs/   .      \hs/
626f75f3746SVille Syrjälä  * ----va---> <-----------------vb--------------------> <--------va-------------
627f75f3746SVille Syrjälä  *       |          |       <----vs----->                     |
628f75f3746SVille Syrjälä  * -vbs-----> <---vbs+1---> <---vbs+2---> <-----0-----> <-----1-----> <-----2--- (scanline counter gen2)
629f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2---> <-----0--- (scanline counter gen3+)
630f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2- (scanline counter hsw+ hdmi)
631f75f3746SVille Syrjälä  *       |          |                                         |
632f75f3746SVille Syrjälä  *       last visible pixel                                   first visible pixel
633f75f3746SVille Syrjälä  *                  |                                         increment frame counter (gen3/4)
634f75f3746SVille Syrjälä  *                  pixel counter = vblank_start * htotal     pixel counter = 0 (gen3/4)
635f75f3746SVille Syrjälä  *
636f75f3746SVille Syrjälä  * x  = horizontal active
637f75f3746SVille Syrjälä  * _  = horizontal blanking
638f75f3746SVille Syrjälä  * hs = horizontal sync
639f75f3746SVille Syrjälä  * va = vertical active
640f75f3746SVille Syrjälä  * vb = vertical blanking
641f75f3746SVille Syrjälä  * vs = vertical sync
642f75f3746SVille Syrjälä  * vbs = vblank_start (number)
643f75f3746SVille Syrjälä  *
644f75f3746SVille Syrjälä  * Summary:
645f75f3746SVille Syrjälä  * - most events happen at the start of horizontal sync
646f75f3746SVille Syrjälä  * - frame start happens at the start of horizontal blank, 1-4 lines
647f75f3746SVille Syrjälä  *   (depending on PIPECONF settings) after the start of vblank
648f75f3746SVille Syrjälä  * - gen3/4 pixel and frame counter are synchronized with the start
649f75f3746SVille Syrjälä  *   of horizontal active on the first line of vertical active
650f75f3746SVille Syrjälä  */
651f75f3746SVille Syrjälä 
6524cdb83ecSVille Syrjälä static u32 i8xx_get_vblank_counter(struct drm_device *dev, int pipe)
6534cdb83ecSVille Syrjälä {
6544cdb83ecSVille Syrjälä 	/* Gen2 doesn't have a hardware frame counter */
6554cdb83ecSVille Syrjälä 	return 0;
6564cdb83ecSVille Syrjälä }
6574cdb83ecSVille Syrjälä 
65842f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
65942f52ef8SKeith Packard  * we use as a pipe index
66042f52ef8SKeith Packard  */
661f71d4af4SJesse Barnes static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
6620a3e67a4SJesse Barnes {
6632d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
6640a3e67a4SJesse Barnes 	unsigned long high_frame;
6650a3e67a4SJesse Barnes 	unsigned long low_frame;
6660b2a8e09SVille Syrjälä 	u32 high1, high2, low, pixel, vbl_start, hsync_start, htotal;
667391f75e2SVille Syrjälä 	struct intel_crtc *intel_crtc =
668391f75e2SVille Syrjälä 		to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
669fc467a22SMaarten Lankhorst 	const struct drm_display_mode *mode = &intel_crtc->base.hwmode;
670391f75e2SVille Syrjälä 
6710b2a8e09SVille Syrjälä 	htotal = mode->crtc_htotal;
6720b2a8e09SVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
6730b2a8e09SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
6740b2a8e09SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
6750b2a8e09SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
676391f75e2SVille Syrjälä 
6770b2a8e09SVille Syrjälä 	/* Convert to pixel count */
6780b2a8e09SVille Syrjälä 	vbl_start *= htotal;
6790b2a8e09SVille Syrjälä 
6800b2a8e09SVille Syrjälä 	/* Start of vblank event occurs at start of hsync */
6810b2a8e09SVille Syrjälä 	vbl_start -= htotal - hsync_start;
6820b2a8e09SVille Syrjälä 
6839db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
6849db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
6855eddb70bSChris Wilson 
6860a3e67a4SJesse Barnes 	/*
6870a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
6880a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
6890a3e67a4SJesse Barnes 	 * register.
6900a3e67a4SJesse Barnes 	 */
6910a3e67a4SJesse Barnes 	do {
6925eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
693391f75e2SVille Syrjälä 		low   = I915_READ(low_frame);
6945eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
6950a3e67a4SJesse Barnes 	} while (high1 != high2);
6960a3e67a4SJesse Barnes 
6975eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
698391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
6995eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
700391f75e2SVille Syrjälä 
701391f75e2SVille Syrjälä 	/*
702391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
703391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
704391f75e2SVille Syrjälä 	 * counter against vblank start.
705391f75e2SVille Syrjälä 	 */
706edc08d0aSVille Syrjälä 	return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
7070a3e67a4SJesse Barnes }
7080a3e67a4SJesse Barnes 
709f71d4af4SJesse Barnes static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
7109880b7a5SJesse Barnes {
7112d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
7129db4a9c7SJesse Barnes 	int reg = PIPE_FRMCOUNT_GM45(pipe);
7139880b7a5SJesse Barnes 
7149880b7a5SJesse Barnes 	return I915_READ(reg);
7159880b7a5SJesse Barnes }
7169880b7a5SJesse Barnes 
717ad3543edSMario Kleiner /* raw reads, only for fast reads of display block, no need for forcewake etc. */
718ad3543edSMario Kleiner #define __raw_i915_read32(dev_priv__, reg__) readl((dev_priv__)->regs + (reg__))
719ad3543edSMario Kleiner 
720a225f079SVille Syrjälä static int __intel_get_crtc_scanline(struct intel_crtc *crtc)
721a225f079SVille Syrjälä {
722a225f079SVille Syrjälä 	struct drm_device *dev = crtc->base.dev;
723a225f079SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
724fc467a22SMaarten Lankhorst 	const struct drm_display_mode *mode = &crtc->base.hwmode;
725a225f079SVille Syrjälä 	enum pipe pipe = crtc->pipe;
72680715b2fSVille Syrjälä 	int position, vtotal;
727a225f079SVille Syrjälä 
72880715b2fSVille Syrjälä 	vtotal = mode->crtc_vtotal;
729a225f079SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
730a225f079SVille Syrjälä 		vtotal /= 2;
731a225f079SVille Syrjälä 
732a225f079SVille Syrjälä 	if (IS_GEN2(dev))
733a225f079SVille Syrjälä 		position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
734a225f079SVille Syrjälä 	else
735a225f079SVille Syrjälä 		position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
736a225f079SVille Syrjälä 
737a225f079SVille Syrjälä 	/*
73841b578fbSJesse Barnes 	 * On HSW, the DSL reg (0x70000) appears to return 0 if we
73941b578fbSJesse Barnes 	 * read it just before the start of vblank.  So try it again
74041b578fbSJesse Barnes 	 * so we don't accidentally end up spanning a vblank frame
74141b578fbSJesse Barnes 	 * increment, causing the pipe_update_end() code to squak at us.
74241b578fbSJesse Barnes 	 *
74341b578fbSJesse Barnes 	 * The nature of this problem means we can't simply check the ISR
74441b578fbSJesse Barnes 	 * bit and return the vblank start value; nor can we use the scanline
74541b578fbSJesse Barnes 	 * debug register in the transcoder as it appears to have the same
74641b578fbSJesse Barnes 	 * problem.  We may need to extend this to include other platforms,
74741b578fbSJesse Barnes 	 * but so far testing only shows the problem on HSW.
74841b578fbSJesse Barnes 	 */
74941b578fbSJesse Barnes 	if (IS_HASWELL(dev) && !position) {
75041b578fbSJesse Barnes 		int i, temp;
75141b578fbSJesse Barnes 
75241b578fbSJesse Barnes 		for (i = 0; i < 100; i++) {
75341b578fbSJesse Barnes 			udelay(1);
75441b578fbSJesse Barnes 			temp = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) &
75541b578fbSJesse Barnes 				DSL_LINEMASK_GEN3;
75641b578fbSJesse Barnes 			if (temp != position) {
75741b578fbSJesse Barnes 				position = temp;
75841b578fbSJesse Barnes 				break;
75941b578fbSJesse Barnes 			}
76041b578fbSJesse Barnes 		}
76141b578fbSJesse Barnes 	}
76241b578fbSJesse Barnes 
76341b578fbSJesse Barnes 	/*
76480715b2fSVille Syrjälä 	 * See update_scanline_offset() for the details on the
76580715b2fSVille Syrjälä 	 * scanline_offset adjustment.
766a225f079SVille Syrjälä 	 */
76780715b2fSVille Syrjälä 	return (position + crtc->scanline_offset) % vtotal;
768a225f079SVille Syrjälä }
769a225f079SVille Syrjälä 
770f71d4af4SJesse Barnes static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
771abca9e45SVille Syrjälä 				    unsigned int flags, int *vpos, int *hpos,
7723bb403bfSVille Syrjälä 				    ktime_t *stime, ktime_t *etime,
7733bb403bfSVille Syrjälä 				    const struct drm_display_mode *mode)
7740af7e4dfSMario Kleiner {
775c2baf4b7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
776c2baf4b7SVille Syrjälä 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
777c2baf4b7SVille Syrjälä 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7783aa18df8SVille Syrjälä 	int position;
77978e8fc6bSVille Syrjälä 	int vbl_start, vbl_end, hsync_start, htotal, vtotal;
7800af7e4dfSMario Kleiner 	bool in_vbl = true;
7810af7e4dfSMario Kleiner 	int ret = 0;
782ad3543edSMario Kleiner 	unsigned long irqflags;
7830af7e4dfSMario Kleiner 
784fc467a22SMaarten Lankhorst 	if (WARN_ON(!mode->crtc_clock)) {
7850af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
7869db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
7870af7e4dfSMario Kleiner 		return 0;
7880af7e4dfSMario Kleiner 	}
7890af7e4dfSMario Kleiner 
790c2baf4b7SVille Syrjälä 	htotal = mode->crtc_htotal;
79178e8fc6bSVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
792c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
793c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
794c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
7950af7e4dfSMario Kleiner 
796d31faf65SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
797d31faf65SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
798d31faf65SVille Syrjälä 		vbl_end /= 2;
799d31faf65SVille Syrjälä 		vtotal /= 2;
800d31faf65SVille Syrjälä 	}
801d31faf65SVille Syrjälä 
802c2baf4b7SVille Syrjälä 	ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
803c2baf4b7SVille Syrjälä 
804ad3543edSMario Kleiner 	/*
805ad3543edSMario Kleiner 	 * Lock uncore.lock, as we will do multiple timing critical raw
806ad3543edSMario Kleiner 	 * register reads, potentially with preemption disabled, so the
807ad3543edSMario Kleiner 	 * following code must not block on uncore.lock.
808ad3543edSMario Kleiner 	 */
809ad3543edSMario Kleiner 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
810ad3543edSMario Kleiner 
811ad3543edSMario Kleiner 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
812ad3543edSMario Kleiner 
813ad3543edSMario Kleiner 	/* Get optional system timestamp before query. */
814ad3543edSMario Kleiner 	if (stime)
815ad3543edSMario Kleiner 		*stime = ktime_get();
816ad3543edSMario Kleiner 
8177c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
8180af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
8190af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
8200af7e4dfSMario Kleiner 		 */
821a225f079SVille Syrjälä 		position = __intel_get_crtc_scanline(intel_crtc);
8220af7e4dfSMario Kleiner 	} else {
8230af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
8240af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
8250af7e4dfSMario Kleiner 		 * scanout position.
8260af7e4dfSMario Kleiner 		 */
827ad3543edSMario Kleiner 		position = (__raw_i915_read32(dev_priv, PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
8280af7e4dfSMario Kleiner 
8293aa18df8SVille Syrjälä 		/* convert to pixel counts */
8303aa18df8SVille Syrjälä 		vbl_start *= htotal;
8313aa18df8SVille Syrjälä 		vbl_end *= htotal;
8323aa18df8SVille Syrjälä 		vtotal *= htotal;
83378e8fc6bSVille Syrjälä 
83478e8fc6bSVille Syrjälä 		/*
8357e78f1cbSVille Syrjälä 		 * In interlaced modes, the pixel counter counts all pixels,
8367e78f1cbSVille Syrjälä 		 * so one field will have htotal more pixels. In order to avoid
8377e78f1cbSVille Syrjälä 		 * the reported position from jumping backwards when the pixel
8387e78f1cbSVille Syrjälä 		 * counter is beyond the length of the shorter field, just
8397e78f1cbSVille Syrjälä 		 * clamp the position the length of the shorter field. This
8407e78f1cbSVille Syrjälä 		 * matches how the scanline counter based position works since
8417e78f1cbSVille Syrjälä 		 * the scanline counter doesn't count the two half lines.
8427e78f1cbSVille Syrjälä 		 */
8437e78f1cbSVille Syrjälä 		if (position >= vtotal)
8447e78f1cbSVille Syrjälä 			position = vtotal - 1;
8457e78f1cbSVille Syrjälä 
8467e78f1cbSVille Syrjälä 		/*
84778e8fc6bSVille Syrjälä 		 * Start of vblank interrupt is triggered at start of hsync,
84878e8fc6bSVille Syrjälä 		 * just prior to the first active line of vblank. However we
84978e8fc6bSVille Syrjälä 		 * consider lines to start at the leading edge of horizontal
85078e8fc6bSVille Syrjälä 		 * active. So, should we get here before we've crossed into
85178e8fc6bSVille Syrjälä 		 * the horizontal active of the first line in vblank, we would
85278e8fc6bSVille Syrjälä 		 * not set the DRM_SCANOUTPOS_INVBL flag. In order to fix that,
85378e8fc6bSVille Syrjälä 		 * always add htotal-hsync_start to the current pixel position.
85478e8fc6bSVille Syrjälä 		 */
85578e8fc6bSVille Syrjälä 		position = (position + htotal - hsync_start) % vtotal;
8563aa18df8SVille Syrjälä 	}
8573aa18df8SVille Syrjälä 
858ad3543edSMario Kleiner 	/* Get optional system timestamp after query. */
859ad3543edSMario Kleiner 	if (etime)
860ad3543edSMario Kleiner 		*etime = ktime_get();
861ad3543edSMario Kleiner 
862ad3543edSMario Kleiner 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
863ad3543edSMario Kleiner 
864ad3543edSMario Kleiner 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
865ad3543edSMario Kleiner 
8663aa18df8SVille Syrjälä 	in_vbl = position >= vbl_start && position < vbl_end;
8673aa18df8SVille Syrjälä 
8683aa18df8SVille Syrjälä 	/*
8693aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
8703aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
8713aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
8723aa18df8SVille Syrjälä 	 * up since vbl_end.
8733aa18df8SVille Syrjälä 	 */
8743aa18df8SVille Syrjälä 	if (position >= vbl_start)
8753aa18df8SVille Syrjälä 		position -= vbl_end;
8763aa18df8SVille Syrjälä 	else
8773aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
8783aa18df8SVille Syrjälä 
8797c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
8803aa18df8SVille Syrjälä 		*vpos = position;
8813aa18df8SVille Syrjälä 		*hpos = 0;
8823aa18df8SVille Syrjälä 	} else {
8830af7e4dfSMario Kleiner 		*vpos = position / htotal;
8840af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
8850af7e4dfSMario Kleiner 	}
8860af7e4dfSMario Kleiner 
8870af7e4dfSMario Kleiner 	/* In vblank? */
8880af7e4dfSMario Kleiner 	if (in_vbl)
8893d3cbd84SDaniel Vetter 		ret |= DRM_SCANOUTPOS_IN_VBLANK;
8900af7e4dfSMario Kleiner 
8910af7e4dfSMario Kleiner 	return ret;
8920af7e4dfSMario Kleiner }
8930af7e4dfSMario Kleiner 
894a225f079SVille Syrjälä int intel_get_crtc_scanline(struct intel_crtc *crtc)
895a225f079SVille Syrjälä {
896a225f079SVille Syrjälä 	struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
897a225f079SVille Syrjälä 	unsigned long irqflags;
898a225f079SVille Syrjälä 	int position;
899a225f079SVille Syrjälä 
900a225f079SVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
901a225f079SVille Syrjälä 	position = __intel_get_crtc_scanline(crtc);
902a225f079SVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
903a225f079SVille Syrjälä 
904a225f079SVille Syrjälä 	return position;
905a225f079SVille Syrjälä }
906a225f079SVille Syrjälä 
907f71d4af4SJesse Barnes static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
9080af7e4dfSMario Kleiner 			      int *max_error,
9090af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
9100af7e4dfSMario Kleiner 			      unsigned flags)
9110af7e4dfSMario Kleiner {
9124041b853SChris Wilson 	struct drm_crtc *crtc;
9130af7e4dfSMario Kleiner 
9147eb552aeSBen Widawsky 	if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
9154041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
9160af7e4dfSMario Kleiner 		return -EINVAL;
9170af7e4dfSMario Kleiner 	}
9180af7e4dfSMario Kleiner 
9190af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
9204041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
9214041b853SChris Wilson 	if (crtc == NULL) {
9224041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
9234041b853SChris Wilson 		return -EINVAL;
9244041b853SChris Wilson 	}
9254041b853SChris Wilson 
926fc467a22SMaarten Lankhorst 	if (!crtc->hwmode.crtc_clock) {
9274041b853SChris Wilson 		DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
9284041b853SChris Wilson 		return -EBUSY;
9294041b853SChris Wilson 	}
9300af7e4dfSMario Kleiner 
9310af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
9324041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
9334041b853SChris Wilson 						     vblank_time, flags,
934fc467a22SMaarten Lankhorst 						     &crtc->hwmode);
9350af7e4dfSMario Kleiner }
9360af7e4dfSMario Kleiner 
937d0ecd7e2SDaniel Vetter static void ironlake_rps_change_irq_handler(struct drm_device *dev)
938f97108d1SJesse Barnes {
9392d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
940b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
9419270388eSDaniel Vetter 	u8 new_delay;
9429270388eSDaniel Vetter 
943d0ecd7e2SDaniel Vetter 	spin_lock(&mchdev_lock);
944f97108d1SJesse Barnes 
94573edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
94673edd18fSDaniel Vetter 
94720e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
9489270388eSDaniel Vetter 
9497648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
950b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
951b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
952f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
953f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
954f97108d1SJesse Barnes 
955f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
956b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
95720e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
95820e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
95920e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
96020e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
961b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
96220e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
96320e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
96420e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
96520e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
966f97108d1SJesse Barnes 	}
967f97108d1SJesse Barnes 
9687648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
96920e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
970f97108d1SJesse Barnes 
971d0ecd7e2SDaniel Vetter 	spin_unlock(&mchdev_lock);
9729270388eSDaniel Vetter 
973f97108d1SJesse Barnes 	return;
974f97108d1SJesse Barnes }
975f97108d1SJesse Barnes 
97674cdb337SChris Wilson static void notify_ring(struct intel_engine_cs *ring)
977549f7365SChris Wilson {
97893b0a4e0SOscar Mateo 	if (!intel_ring_initialized(ring))
979475553deSChris Wilson 		return;
980475553deSChris Wilson 
981bcfcc8baSJohn Harrison 	trace_i915_gem_request_notify(ring);
9829862e600SChris Wilson 
983549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
984549f7365SChris Wilson }
985549f7365SChris Wilson 
98643cf3bf0SChris Wilson static void vlv_c0_read(struct drm_i915_private *dev_priv,
98743cf3bf0SChris Wilson 			struct intel_rps_ei *ei)
98831685c25SDeepak S {
98943cf3bf0SChris Wilson 	ei->cz_clock = vlv_punit_read(dev_priv, PUNIT_REG_CZ_TIMESTAMP);
99043cf3bf0SChris Wilson 	ei->render_c0 = I915_READ(VLV_RENDER_C0_COUNT);
99143cf3bf0SChris Wilson 	ei->media_c0 = I915_READ(VLV_MEDIA_C0_COUNT);
99231685c25SDeepak S }
99331685c25SDeepak S 
99443cf3bf0SChris Wilson static bool vlv_c0_above(struct drm_i915_private *dev_priv,
99543cf3bf0SChris Wilson 			 const struct intel_rps_ei *old,
99643cf3bf0SChris Wilson 			 const struct intel_rps_ei *now,
99743cf3bf0SChris Wilson 			 int threshold)
99831685c25SDeepak S {
99943cf3bf0SChris Wilson 	u64 time, c0;
1000*7bad74d5SVille Syrjälä 	unsigned int mul = 100;
100131685c25SDeepak S 
100243cf3bf0SChris Wilson 	if (old->cz_clock == 0)
100343cf3bf0SChris Wilson 		return false;
100431685c25SDeepak S 
1005*7bad74d5SVille Syrjälä 	if (I915_READ(VLV_COUNTER_CONTROL) & VLV_COUNT_RANGE_HIGH)
1006*7bad74d5SVille Syrjälä 		mul <<= 8;
1007*7bad74d5SVille Syrjälä 
100843cf3bf0SChris Wilson 	time = now->cz_clock - old->cz_clock;
1009*7bad74d5SVille Syrjälä 	time *= threshold * dev_priv->czclk_freq;
101031685c25SDeepak S 
101143cf3bf0SChris Wilson 	/* Workload can be split between render + media, e.g. SwapBuffers
101243cf3bf0SChris Wilson 	 * being blitted in X after being rendered in mesa. To account for
101343cf3bf0SChris Wilson 	 * this we need to combine both engines into our activity counter.
101443cf3bf0SChris Wilson 	 */
101543cf3bf0SChris Wilson 	c0 = now->render_c0 - old->render_c0;
101643cf3bf0SChris Wilson 	c0 += now->media_c0 - old->media_c0;
1017*7bad74d5SVille Syrjälä 	c0 *= mul * VLV_CZ_CLOCK_TO_MILLI_SEC;
101831685c25SDeepak S 
101943cf3bf0SChris Wilson 	return c0 >= time;
102031685c25SDeepak S }
102131685c25SDeepak S 
102243cf3bf0SChris Wilson void gen6_rps_reset_ei(struct drm_i915_private *dev_priv)
102343cf3bf0SChris Wilson {
102443cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &dev_priv->rps.down_ei);
102543cf3bf0SChris Wilson 	dev_priv->rps.up_ei = dev_priv->rps.down_ei;
102643cf3bf0SChris Wilson }
102743cf3bf0SChris Wilson 
102843cf3bf0SChris Wilson static u32 vlv_wa_c0_ei(struct drm_i915_private *dev_priv, u32 pm_iir)
102943cf3bf0SChris Wilson {
103043cf3bf0SChris Wilson 	struct intel_rps_ei now;
103143cf3bf0SChris Wilson 	u32 events = 0;
103243cf3bf0SChris Wilson 
10336f4b12f8SChris Wilson 	if ((pm_iir & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED)) == 0)
103443cf3bf0SChris Wilson 		return 0;
103543cf3bf0SChris Wilson 
103643cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &now);
103743cf3bf0SChris Wilson 	if (now.cz_clock == 0)
103843cf3bf0SChris Wilson 		return 0;
103931685c25SDeepak S 
104043cf3bf0SChris Wilson 	if (pm_iir & GEN6_PM_RP_DOWN_EI_EXPIRED) {
104143cf3bf0SChris Wilson 		if (!vlv_c0_above(dev_priv,
104243cf3bf0SChris Wilson 				  &dev_priv->rps.down_ei, &now,
10438fb55197SChris Wilson 				  dev_priv->rps.down_threshold))
104443cf3bf0SChris Wilson 			events |= GEN6_PM_RP_DOWN_THRESHOLD;
104543cf3bf0SChris Wilson 		dev_priv->rps.down_ei = now;
104631685c25SDeepak S 	}
104731685c25SDeepak S 
104843cf3bf0SChris Wilson 	if (pm_iir & GEN6_PM_RP_UP_EI_EXPIRED) {
104943cf3bf0SChris Wilson 		if (vlv_c0_above(dev_priv,
105043cf3bf0SChris Wilson 				 &dev_priv->rps.up_ei, &now,
10518fb55197SChris Wilson 				 dev_priv->rps.up_threshold))
105243cf3bf0SChris Wilson 			events |= GEN6_PM_RP_UP_THRESHOLD;
105343cf3bf0SChris Wilson 		dev_priv->rps.up_ei = now;
105443cf3bf0SChris Wilson 	}
105543cf3bf0SChris Wilson 
105643cf3bf0SChris Wilson 	return events;
105731685c25SDeepak S }
105831685c25SDeepak S 
1059f5a4c67dSChris Wilson static bool any_waiters(struct drm_i915_private *dev_priv)
1060f5a4c67dSChris Wilson {
1061f5a4c67dSChris Wilson 	struct intel_engine_cs *ring;
1062f5a4c67dSChris Wilson 	int i;
1063f5a4c67dSChris Wilson 
1064f5a4c67dSChris Wilson 	for_each_ring(ring, dev_priv, i)
1065f5a4c67dSChris Wilson 		if (ring->irq_refcount)
1066f5a4c67dSChris Wilson 			return true;
1067f5a4c67dSChris Wilson 
1068f5a4c67dSChris Wilson 	return false;
1069f5a4c67dSChris Wilson }
1070f5a4c67dSChris Wilson 
10714912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
10723b8d8d91SJesse Barnes {
10732d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
10742d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, rps.work);
10758d3afd7dSChris Wilson 	bool client_boost;
10768d3afd7dSChris Wilson 	int new_delay, adj, min, max;
1077edbfdb45SPaulo Zanoni 	u32 pm_iir;
10783b8d8d91SJesse Barnes 
107959cdb63dSDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
1080d4d70aa5SImre Deak 	/* Speed up work cancelation during disabling rps interrupts. */
1081d4d70aa5SImre Deak 	if (!dev_priv->rps.interrupts_enabled) {
1082d4d70aa5SImre Deak 		spin_unlock_irq(&dev_priv->irq_lock);
1083d4d70aa5SImre Deak 		return;
1084d4d70aa5SImre Deak 	}
1085c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
1086c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
1087a72fbc3aSImre Deak 	/* Make sure not to corrupt PMIMR state used by ringbuffer on GEN6 */
1088480c8033SDaniel Vetter 	gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
10898d3afd7dSChris Wilson 	client_boost = dev_priv->rps.client_boost;
10908d3afd7dSChris Wilson 	dev_priv->rps.client_boost = false;
109159cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
10924912d041SBen Widawsky 
109360611c13SPaulo Zanoni 	/* Make sure we didn't queue anything we're not going to process. */
1094a6706b45SDeepak S 	WARN_ON(pm_iir & ~dev_priv->pm_rps_events);
109560611c13SPaulo Zanoni 
10968d3afd7dSChris Wilson 	if ((pm_iir & dev_priv->pm_rps_events) == 0 && !client_boost)
10973b8d8d91SJesse Barnes 		return;
10983b8d8d91SJesse Barnes 
10994fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
11007b9e0ae6SChris Wilson 
110143cf3bf0SChris Wilson 	pm_iir |= vlv_wa_c0_ei(dev_priv, pm_iir);
110243cf3bf0SChris Wilson 
1103dd75fdc8SChris Wilson 	adj = dev_priv->rps.last_adj;
1104edcf284bSChris Wilson 	new_delay = dev_priv->rps.cur_freq;
11058d3afd7dSChris Wilson 	min = dev_priv->rps.min_freq_softlimit;
11068d3afd7dSChris Wilson 	max = dev_priv->rps.max_freq_softlimit;
11078d3afd7dSChris Wilson 
11088d3afd7dSChris Wilson 	if (client_boost) {
11098d3afd7dSChris Wilson 		new_delay = dev_priv->rps.max_freq_softlimit;
11108d3afd7dSChris Wilson 		adj = 0;
11118d3afd7dSChris Wilson 	} else if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
1112dd75fdc8SChris Wilson 		if (adj > 0)
1113dd75fdc8SChris Wilson 			adj *= 2;
1114edcf284bSChris Wilson 		else /* CHV needs even encode values */
1115edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? 2 : 1;
11167425034aSVille Syrjälä 		/*
11177425034aSVille Syrjälä 		 * For better performance, jump directly
11187425034aSVille Syrjälä 		 * to RPe if we're below it.
11197425034aSVille Syrjälä 		 */
1120edcf284bSChris Wilson 		if (new_delay < dev_priv->rps.efficient_freq - adj) {
1121b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1122edcf284bSChris Wilson 			adj = 0;
1123edcf284bSChris Wilson 		}
1124f5a4c67dSChris Wilson 	} else if (any_waiters(dev_priv)) {
1125f5a4c67dSChris Wilson 		adj = 0;
1126dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
1127b39fb297SBen Widawsky 		if (dev_priv->rps.cur_freq > dev_priv->rps.efficient_freq)
1128b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1129dd75fdc8SChris Wilson 		else
1130b39fb297SBen Widawsky 			new_delay = dev_priv->rps.min_freq_softlimit;
1131dd75fdc8SChris Wilson 		adj = 0;
1132dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1133dd75fdc8SChris Wilson 		if (adj < 0)
1134dd75fdc8SChris Wilson 			adj *= 2;
1135edcf284bSChris Wilson 		else /* CHV needs even encode values */
1136edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? -2 : -1;
1137dd75fdc8SChris Wilson 	} else { /* unknown event */
1138edcf284bSChris Wilson 		adj = 0;
1139dd75fdc8SChris Wilson 	}
11403b8d8d91SJesse Barnes 
1141edcf284bSChris Wilson 	dev_priv->rps.last_adj = adj;
1142edcf284bSChris Wilson 
114379249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
114479249636SBen Widawsky 	 * interrupt
114579249636SBen Widawsky 	 */
1146edcf284bSChris Wilson 	new_delay += adj;
11478d3afd7dSChris Wilson 	new_delay = clamp_t(int, new_delay, min, max);
114827544369SDeepak S 
1149ffe02b40SVille Syrjälä 	intel_set_rps(dev_priv->dev, new_delay);
11503b8d8d91SJesse Barnes 
11514fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
11523b8d8d91SJesse Barnes }
11533b8d8d91SJesse Barnes 
1154e3689190SBen Widawsky 
1155e3689190SBen Widawsky /**
1156e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
1157e3689190SBen Widawsky  * occurred.
1158e3689190SBen Widawsky  * @work: workqueue struct
1159e3689190SBen Widawsky  *
1160e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
1161e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
1162e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
1163e3689190SBen Widawsky  */
1164e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
1165e3689190SBen Widawsky {
11662d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
11672d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, l3_parity.error_work);
1168e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
116935a85ac6SBen Widawsky 	char *parity_event[6];
1170e3689190SBen Widawsky 	uint32_t misccpctl;
117135a85ac6SBen Widawsky 	uint8_t slice = 0;
1172e3689190SBen Widawsky 
1173e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
1174e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
1175e3689190SBen Widawsky 	 * any time we access those registers.
1176e3689190SBen Widawsky 	 */
1177e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
1178e3689190SBen Widawsky 
117935a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
118035a85ac6SBen Widawsky 	if (WARN_ON(!dev_priv->l3_parity.which_slice))
118135a85ac6SBen Widawsky 		goto out;
118235a85ac6SBen Widawsky 
1183e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
1184e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1185e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
1186e3689190SBen Widawsky 
118735a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
118835a85ac6SBen Widawsky 		u32 reg;
118935a85ac6SBen Widawsky 
119035a85ac6SBen Widawsky 		slice--;
119135a85ac6SBen Widawsky 		if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv->dev)))
119235a85ac6SBen Widawsky 			break;
119335a85ac6SBen Widawsky 
119435a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
119535a85ac6SBen Widawsky 
119635a85ac6SBen Widawsky 		reg = GEN7_L3CDERRST1 + (slice * 0x200);
119735a85ac6SBen Widawsky 
119835a85ac6SBen Widawsky 		error_status = I915_READ(reg);
1199e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
1200e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
1201e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1202e3689190SBen Widawsky 
120335a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
120435a85ac6SBen Widawsky 		POSTING_READ(reg);
1205e3689190SBen Widawsky 
1206cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1207e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1208e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1209e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
121035a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
121135a85ac6SBen Widawsky 		parity_event[5] = NULL;
1212e3689190SBen Widawsky 
12135bdebb18SDave Airlie 		kobject_uevent_env(&dev_priv->dev->primary->kdev->kobj,
1214e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
1215e3689190SBen Widawsky 
121635a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
121735a85ac6SBen Widawsky 			  slice, row, bank, subbank);
1218e3689190SBen Widawsky 
121935a85ac6SBen Widawsky 		kfree(parity_event[4]);
1220e3689190SBen Widawsky 		kfree(parity_event[3]);
1221e3689190SBen Widawsky 		kfree(parity_event[2]);
1222e3689190SBen Widawsky 		kfree(parity_event[1]);
1223e3689190SBen Widawsky 	}
1224e3689190SBen Widawsky 
122535a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
122635a85ac6SBen Widawsky 
122735a85ac6SBen Widawsky out:
122835a85ac6SBen Widawsky 	WARN_ON(dev_priv->l3_parity.which_slice);
12294cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
1230480c8033SDaniel Vetter 	gen5_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv->dev));
12314cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
123235a85ac6SBen Widawsky 
123335a85ac6SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
123435a85ac6SBen Widawsky }
123535a85ac6SBen Widawsky 
123635a85ac6SBen Widawsky static void ivybridge_parity_error_irq_handler(struct drm_device *dev, u32 iir)
1237e3689190SBen Widawsky {
12382d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1239e3689190SBen Widawsky 
1240040d2baaSBen Widawsky 	if (!HAS_L3_DPF(dev))
1241e3689190SBen Widawsky 		return;
1242e3689190SBen Widawsky 
1243d0ecd7e2SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1244480c8033SDaniel Vetter 	gen5_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev));
1245d0ecd7e2SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
1246e3689190SBen Widawsky 
124735a85ac6SBen Widawsky 	iir &= GT_PARITY_ERROR(dev);
124835a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
124935a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 1;
125035a85ac6SBen Widawsky 
125135a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
125235a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 0;
125335a85ac6SBen Widawsky 
1254a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
1255e3689190SBen Widawsky }
1256e3689190SBen Widawsky 
1257f1af8fc1SPaulo Zanoni static void ilk_gt_irq_handler(struct drm_device *dev,
1258f1af8fc1SPaulo Zanoni 			       struct drm_i915_private *dev_priv,
1259f1af8fc1SPaulo Zanoni 			       u32 gt_iir)
1260f1af8fc1SPaulo Zanoni {
1261f1af8fc1SPaulo Zanoni 	if (gt_iir &
1262f1af8fc1SPaulo Zanoni 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
126374cdb337SChris Wilson 		notify_ring(&dev_priv->ring[RCS]);
1264f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
126574cdb337SChris Wilson 		notify_ring(&dev_priv->ring[VCS]);
1266f1af8fc1SPaulo Zanoni }
1267f1af8fc1SPaulo Zanoni 
1268e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
1269e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
1270e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1271e7b4c6b1SDaniel Vetter {
1272e7b4c6b1SDaniel Vetter 
1273cc609d5dSBen Widawsky 	if (gt_iir &
1274cc609d5dSBen Widawsky 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
127574cdb337SChris Wilson 		notify_ring(&dev_priv->ring[RCS]);
1276cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
127774cdb337SChris Wilson 		notify_ring(&dev_priv->ring[VCS]);
1278cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
127974cdb337SChris Wilson 		notify_ring(&dev_priv->ring[BCS]);
1280e7b4c6b1SDaniel Vetter 
1281cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1282cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
1283aaecdf61SDaniel Vetter 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT))
1284aaecdf61SDaniel Vetter 		DRM_DEBUG("Command parser error, gt_iir 0x%08x\n", gt_iir);
1285e3689190SBen Widawsky 
128635a85ac6SBen Widawsky 	if (gt_iir & GT_PARITY_ERROR(dev))
128735a85ac6SBen Widawsky 		ivybridge_parity_error_irq_handler(dev, gt_iir);
1288e7b4c6b1SDaniel Vetter }
1289e7b4c6b1SDaniel Vetter 
129074cdb337SChris Wilson static irqreturn_t gen8_gt_irq_handler(struct drm_i915_private *dev_priv,
1291abd58f01SBen Widawsky 				       u32 master_ctl)
1292abd58f01SBen Widawsky {
1293abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
1294abd58f01SBen Widawsky 
1295abd58f01SBen Widawsky 	if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
129674cdb337SChris Wilson 		u32 tmp = I915_READ_FW(GEN8_GT_IIR(0));
1297abd58f01SBen Widawsky 		if (tmp) {
1298cb0d205eSChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(0), tmp);
1299abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1300e981e7b1SThomas Daniel 
130174cdb337SChris Wilson 			if (tmp & (GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT))
130274cdb337SChris Wilson 				intel_lrc_irq_handler(&dev_priv->ring[RCS]);
130374cdb337SChris Wilson 			if (tmp & (GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT))
130474cdb337SChris Wilson 				notify_ring(&dev_priv->ring[RCS]);
1305e981e7b1SThomas Daniel 
130674cdb337SChris Wilson 			if (tmp & (GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT))
130774cdb337SChris Wilson 				intel_lrc_irq_handler(&dev_priv->ring[BCS]);
130874cdb337SChris Wilson 			if (tmp & (GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT))
130974cdb337SChris Wilson 				notify_ring(&dev_priv->ring[BCS]);
1310abd58f01SBen Widawsky 		} else
1311abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT0)!\n");
1312abd58f01SBen Widawsky 	}
1313abd58f01SBen Widawsky 
131485f9b5f9SZhao Yakui 	if (master_ctl & (GEN8_GT_VCS1_IRQ | GEN8_GT_VCS2_IRQ)) {
131574cdb337SChris Wilson 		u32 tmp = I915_READ_FW(GEN8_GT_IIR(1));
1316abd58f01SBen Widawsky 		if (tmp) {
1317cb0d205eSChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(1), tmp);
1318abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1319e981e7b1SThomas Daniel 
132074cdb337SChris Wilson 			if (tmp & (GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT))
132174cdb337SChris Wilson 				intel_lrc_irq_handler(&dev_priv->ring[VCS]);
132274cdb337SChris Wilson 			if (tmp & (GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT))
132374cdb337SChris Wilson 				notify_ring(&dev_priv->ring[VCS]);
1324e981e7b1SThomas Daniel 
132574cdb337SChris Wilson 			if (tmp & (GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT))
132674cdb337SChris Wilson 				intel_lrc_irq_handler(&dev_priv->ring[VCS2]);
132774cdb337SChris Wilson 			if (tmp & (GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT))
132874cdb337SChris Wilson 				notify_ring(&dev_priv->ring[VCS2]);
1329abd58f01SBen Widawsky 		} else
1330abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT1)!\n");
1331abd58f01SBen Widawsky 	}
1332abd58f01SBen Widawsky 
133374cdb337SChris Wilson 	if (master_ctl & GEN8_GT_VECS_IRQ) {
133474cdb337SChris Wilson 		u32 tmp = I915_READ_FW(GEN8_GT_IIR(3));
133574cdb337SChris Wilson 		if (tmp) {
133674cdb337SChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(3), tmp);
133774cdb337SChris Wilson 			ret = IRQ_HANDLED;
133874cdb337SChris Wilson 
133974cdb337SChris Wilson 			if (tmp & (GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT))
134074cdb337SChris Wilson 				intel_lrc_irq_handler(&dev_priv->ring[VECS]);
134174cdb337SChris Wilson 			if (tmp & (GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT))
134274cdb337SChris Wilson 				notify_ring(&dev_priv->ring[VECS]);
134374cdb337SChris Wilson 		} else
134474cdb337SChris Wilson 			DRM_ERROR("The master control interrupt lied (GT3)!\n");
134574cdb337SChris Wilson 	}
134674cdb337SChris Wilson 
13470961021aSBen Widawsky 	if (master_ctl & GEN8_GT_PM_IRQ) {
134874cdb337SChris Wilson 		u32 tmp = I915_READ_FW(GEN8_GT_IIR(2));
13490961021aSBen Widawsky 		if (tmp & dev_priv->pm_rps_events) {
1350cb0d205eSChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(2),
13510961021aSBen Widawsky 				      tmp & dev_priv->pm_rps_events);
135238cc46d7SOscar Mateo 			ret = IRQ_HANDLED;
1353c9a9a268SImre Deak 			gen6_rps_irq_handler(dev_priv, tmp);
13540961021aSBen Widawsky 		} else
13550961021aSBen Widawsky 			DRM_ERROR("The master control interrupt lied (PM)!\n");
13560961021aSBen Widawsky 	}
13570961021aSBen Widawsky 
1358abd58f01SBen Widawsky 	return ret;
1359abd58f01SBen Widawsky }
1360abd58f01SBen Widawsky 
136163c88d22SImre Deak static bool bxt_port_hotplug_long_detect(enum port port, u32 val)
136263c88d22SImre Deak {
136363c88d22SImre Deak 	switch (port) {
136463c88d22SImre Deak 	case PORT_A:
1365195baa06SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
136663c88d22SImre Deak 	case PORT_B:
136763c88d22SImre Deak 		return val & PORTB_HOTPLUG_LONG_DETECT;
136863c88d22SImre Deak 	case PORT_C:
136963c88d22SImre Deak 		return val & PORTC_HOTPLUG_LONG_DETECT;
137063c88d22SImre Deak 	default:
137163c88d22SImre Deak 		return false;
137263c88d22SImre Deak 	}
137363c88d22SImre Deak }
137463c88d22SImre Deak 
13756dbf30ceSVille Syrjälä static bool spt_port_hotplug2_long_detect(enum port port, u32 val)
13766dbf30ceSVille Syrjälä {
13776dbf30ceSVille Syrjälä 	switch (port) {
13786dbf30ceSVille Syrjälä 	case PORT_E:
13796dbf30ceSVille Syrjälä 		return val & PORTE_HOTPLUG_LONG_DETECT;
13806dbf30ceSVille Syrjälä 	default:
13816dbf30ceSVille Syrjälä 		return false;
13826dbf30ceSVille Syrjälä 	}
13836dbf30ceSVille Syrjälä }
13846dbf30ceSVille Syrjälä 
138574c0b395SVille Syrjälä static bool spt_port_hotplug_long_detect(enum port port, u32 val)
138674c0b395SVille Syrjälä {
138774c0b395SVille Syrjälä 	switch (port) {
138874c0b395SVille Syrjälä 	case PORT_A:
138974c0b395SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
139074c0b395SVille Syrjälä 	case PORT_B:
139174c0b395SVille Syrjälä 		return val & PORTB_HOTPLUG_LONG_DETECT;
139274c0b395SVille Syrjälä 	case PORT_C:
139374c0b395SVille Syrjälä 		return val & PORTC_HOTPLUG_LONG_DETECT;
139474c0b395SVille Syrjälä 	case PORT_D:
139574c0b395SVille Syrjälä 		return val & PORTD_HOTPLUG_LONG_DETECT;
139674c0b395SVille Syrjälä 	default:
139774c0b395SVille Syrjälä 		return false;
139874c0b395SVille Syrjälä 	}
139974c0b395SVille Syrjälä }
140074c0b395SVille Syrjälä 
1401e4ce95aaSVille Syrjälä static bool ilk_port_hotplug_long_detect(enum port port, u32 val)
1402e4ce95aaSVille Syrjälä {
1403e4ce95aaSVille Syrjälä 	switch (port) {
1404e4ce95aaSVille Syrjälä 	case PORT_A:
1405e4ce95aaSVille Syrjälä 		return val & DIGITAL_PORTA_HOTPLUG_LONG_DETECT;
1406e4ce95aaSVille Syrjälä 	default:
1407e4ce95aaSVille Syrjälä 		return false;
1408e4ce95aaSVille Syrjälä 	}
1409e4ce95aaSVille Syrjälä }
1410e4ce95aaSVille Syrjälä 
1411676574dfSJani Nikula static bool pch_port_hotplug_long_detect(enum port port, u32 val)
141213cf5504SDave Airlie {
141313cf5504SDave Airlie 	switch (port) {
141413cf5504SDave Airlie 	case PORT_B:
1415676574dfSJani Nikula 		return val & PORTB_HOTPLUG_LONG_DETECT;
141613cf5504SDave Airlie 	case PORT_C:
1417676574dfSJani Nikula 		return val & PORTC_HOTPLUG_LONG_DETECT;
141813cf5504SDave Airlie 	case PORT_D:
1419676574dfSJani Nikula 		return val & PORTD_HOTPLUG_LONG_DETECT;
1420676574dfSJani Nikula 	default:
1421676574dfSJani Nikula 		return false;
142213cf5504SDave Airlie 	}
142313cf5504SDave Airlie }
142413cf5504SDave Airlie 
1425676574dfSJani Nikula static bool i9xx_port_hotplug_long_detect(enum port port, u32 val)
142613cf5504SDave Airlie {
142713cf5504SDave Airlie 	switch (port) {
142813cf5504SDave Airlie 	case PORT_B:
1429676574dfSJani Nikula 		return val & PORTB_HOTPLUG_INT_LONG_PULSE;
143013cf5504SDave Airlie 	case PORT_C:
1431676574dfSJani Nikula 		return val & PORTC_HOTPLUG_INT_LONG_PULSE;
143213cf5504SDave Airlie 	case PORT_D:
1433676574dfSJani Nikula 		return val & PORTD_HOTPLUG_INT_LONG_PULSE;
1434676574dfSJani Nikula 	default:
1435676574dfSJani Nikula 		return false;
143613cf5504SDave Airlie 	}
143713cf5504SDave Airlie }
143813cf5504SDave Airlie 
143942db67d6SVille Syrjälä /*
144042db67d6SVille Syrjälä  * Get a bit mask of pins that have triggered, and which ones may be long.
144142db67d6SVille Syrjälä  * This can be called multiple times with the same masks to accumulate
144242db67d6SVille Syrjälä  * hotplug detection results from several registers.
144342db67d6SVille Syrjälä  *
144442db67d6SVille Syrjälä  * Note that the caller is expected to zero out the masks initially.
144542db67d6SVille Syrjälä  */
1446fd63e2a9SImre Deak static void intel_get_hpd_pins(u32 *pin_mask, u32 *long_mask,
14478c841e57SJani Nikula 			     u32 hotplug_trigger, u32 dig_hotplug_reg,
1448fd63e2a9SImre Deak 			     const u32 hpd[HPD_NUM_PINS],
1449fd63e2a9SImre Deak 			     bool long_pulse_detect(enum port port, u32 val))
1450676574dfSJani Nikula {
14518c841e57SJani Nikula 	enum port port;
1452676574dfSJani Nikula 	int i;
1453676574dfSJani Nikula 
1454676574dfSJani Nikula 	for_each_hpd_pin(i) {
14558c841e57SJani Nikula 		if ((hpd[i] & hotplug_trigger) == 0)
14568c841e57SJani Nikula 			continue;
14578c841e57SJani Nikula 
1458676574dfSJani Nikula 		*pin_mask |= BIT(i);
1459676574dfSJani Nikula 
1460cc24fcdcSImre Deak 		if (!intel_hpd_pin_to_port(i, &port))
1461cc24fcdcSImre Deak 			continue;
1462cc24fcdcSImre Deak 
1463fd63e2a9SImre Deak 		if (long_pulse_detect(port, dig_hotplug_reg))
1464676574dfSJani Nikula 			*long_mask |= BIT(i);
1465676574dfSJani Nikula 	}
1466676574dfSJani Nikula 
1467676574dfSJani Nikula 	DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x, dig 0x%08x, pins 0x%08x\n",
1468676574dfSJani Nikula 			 hotplug_trigger, dig_hotplug_reg, *pin_mask);
1469676574dfSJani Nikula 
1470676574dfSJani Nikula }
1471676574dfSJani Nikula 
1472515ac2bbSDaniel Vetter static void gmbus_irq_handler(struct drm_device *dev)
1473515ac2bbSDaniel Vetter {
14742d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
147528c70f16SDaniel Vetter 
147628c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1477515ac2bbSDaniel Vetter }
1478515ac2bbSDaniel Vetter 
1479ce99c256SDaniel Vetter static void dp_aux_irq_handler(struct drm_device *dev)
1480ce99c256SDaniel Vetter {
14812d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
14829ee32feaSDaniel Vetter 
14839ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1484ce99c256SDaniel Vetter }
1485ce99c256SDaniel Vetter 
14868bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
1487277de95eSDaniel Vetter static void display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1488eba94eb9SDaniel Vetter 					 uint32_t crc0, uint32_t crc1,
1489eba94eb9SDaniel Vetter 					 uint32_t crc2, uint32_t crc3,
14908bc5e955SDaniel Vetter 					 uint32_t crc4)
14918bf1e9f1SShuang He {
14928bf1e9f1SShuang He 	struct drm_i915_private *dev_priv = dev->dev_private;
14938bf1e9f1SShuang He 	struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
14948bf1e9f1SShuang He 	struct intel_pipe_crc_entry *entry;
1495ac2300d4SDamien Lespiau 	int head, tail;
1496b2c88f5bSDamien Lespiau 
1497d538bbdfSDamien Lespiau 	spin_lock(&pipe_crc->lock);
1498d538bbdfSDamien Lespiau 
14990c912c79SDamien Lespiau 	if (!pipe_crc->entries) {
1500d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
150134273620SDaniel Vetter 		DRM_DEBUG_KMS("spurious interrupt\n");
15020c912c79SDamien Lespiau 		return;
15030c912c79SDamien Lespiau 	}
15040c912c79SDamien Lespiau 
1505d538bbdfSDamien Lespiau 	head = pipe_crc->head;
1506d538bbdfSDamien Lespiau 	tail = pipe_crc->tail;
1507b2c88f5bSDamien Lespiau 
1508b2c88f5bSDamien Lespiau 	if (CIRC_SPACE(head, tail, INTEL_PIPE_CRC_ENTRIES_NR) < 1) {
1509d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
1510b2c88f5bSDamien Lespiau 		DRM_ERROR("CRC buffer overflowing\n");
1511b2c88f5bSDamien Lespiau 		return;
1512b2c88f5bSDamien Lespiau 	}
1513b2c88f5bSDamien Lespiau 
1514b2c88f5bSDamien Lespiau 	entry = &pipe_crc->entries[head];
15158bf1e9f1SShuang He 
15168bc5e955SDaniel Vetter 	entry->frame = dev->driver->get_vblank_counter(dev, pipe);
1517eba94eb9SDaniel Vetter 	entry->crc[0] = crc0;
1518eba94eb9SDaniel Vetter 	entry->crc[1] = crc1;
1519eba94eb9SDaniel Vetter 	entry->crc[2] = crc2;
1520eba94eb9SDaniel Vetter 	entry->crc[3] = crc3;
1521eba94eb9SDaniel Vetter 	entry->crc[4] = crc4;
1522b2c88f5bSDamien Lespiau 
1523b2c88f5bSDamien Lespiau 	head = (head + 1) & (INTEL_PIPE_CRC_ENTRIES_NR - 1);
1524d538bbdfSDamien Lespiau 	pipe_crc->head = head;
1525d538bbdfSDamien Lespiau 
1526d538bbdfSDamien Lespiau 	spin_unlock(&pipe_crc->lock);
152707144428SDamien Lespiau 
152807144428SDamien Lespiau 	wake_up_interruptible(&pipe_crc->wq);
15298bf1e9f1SShuang He }
1530277de95eSDaniel Vetter #else
1531277de95eSDaniel Vetter static inline void
1532277de95eSDaniel Vetter display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1533277de95eSDaniel Vetter 			     uint32_t crc0, uint32_t crc1,
1534277de95eSDaniel Vetter 			     uint32_t crc2, uint32_t crc3,
1535277de95eSDaniel Vetter 			     uint32_t crc4) {}
1536277de95eSDaniel Vetter #endif
1537eba94eb9SDaniel Vetter 
1538277de95eSDaniel Vetter 
1539277de95eSDaniel Vetter static void hsw_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
15405a69b89fSDaniel Vetter {
15415a69b89fSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
15425a69b89fSDaniel Vetter 
1543277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
15445a69b89fSDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
15455a69b89fSDaniel Vetter 				     0, 0, 0, 0);
15465a69b89fSDaniel Vetter }
15475a69b89fSDaniel Vetter 
1548277de95eSDaniel Vetter static void ivb_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
1549eba94eb9SDaniel Vetter {
1550eba94eb9SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1551eba94eb9SDaniel Vetter 
1552277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
1553eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1554eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1555eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1556eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
15578bc5e955SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1558eba94eb9SDaniel Vetter }
15595b3a856bSDaniel Vetter 
1560277de95eSDaniel Vetter static void i9xx_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
15615b3a856bSDaniel Vetter {
15625b3a856bSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
15630b5c5ed0SDaniel Vetter 	uint32_t res1, res2;
15640b5c5ed0SDaniel Vetter 
15650b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 3)
15660b5c5ed0SDaniel Vetter 		res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
15670b5c5ed0SDaniel Vetter 	else
15680b5c5ed0SDaniel Vetter 		res1 = 0;
15690b5c5ed0SDaniel Vetter 
15700b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
15710b5c5ed0SDaniel Vetter 		res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
15720b5c5ed0SDaniel Vetter 	else
15730b5c5ed0SDaniel Vetter 		res2 = 0;
15745b3a856bSDaniel Vetter 
1575277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
15760b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_RED(pipe)),
15770b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_GREEN(pipe)),
15780b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_BLUE(pipe)),
15790b5c5ed0SDaniel Vetter 				     res1, res2);
15805b3a856bSDaniel Vetter }
15818bf1e9f1SShuang He 
15821403c0d4SPaulo Zanoni /* The RPS events need forcewake, so we add them to a work queue and mask their
15831403c0d4SPaulo Zanoni  * IMR bits until the work is done. Other interrupts can be processed without
15841403c0d4SPaulo Zanoni  * the work queue. */
15851403c0d4SPaulo Zanoni static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1586baf02a1fSBen Widawsky {
1587a6706b45SDeepak S 	if (pm_iir & dev_priv->pm_rps_events) {
158859cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
1589480c8033SDaniel Vetter 		gen6_disable_pm_irq(dev_priv, pm_iir & dev_priv->pm_rps_events);
1590d4d70aa5SImre Deak 		if (dev_priv->rps.interrupts_enabled) {
1591d4d70aa5SImre Deak 			dev_priv->rps.pm_iir |= pm_iir & dev_priv->pm_rps_events;
15922adbee62SDaniel Vetter 			queue_work(dev_priv->wq, &dev_priv->rps.work);
159341a05a3aSDaniel Vetter 		}
1594d4d70aa5SImre Deak 		spin_unlock(&dev_priv->irq_lock);
1595d4d70aa5SImre Deak 	}
1596baf02a1fSBen Widawsky 
1597c9a9a268SImre Deak 	if (INTEL_INFO(dev_priv)->gen >= 8)
1598c9a9a268SImre Deak 		return;
1599c9a9a268SImre Deak 
16001403c0d4SPaulo Zanoni 	if (HAS_VEBOX(dev_priv->dev)) {
160112638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
160274cdb337SChris Wilson 			notify_ring(&dev_priv->ring[VECS]);
160312638c57SBen Widawsky 
1604aaecdf61SDaniel Vetter 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT)
1605aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, pm_iir 0x%08x\n", pm_iir);
160612638c57SBen Widawsky 	}
16071403c0d4SPaulo Zanoni }
1608baf02a1fSBen Widawsky 
16098d7849dbSVille Syrjälä static bool intel_pipe_handle_vblank(struct drm_device *dev, enum pipe pipe)
16108d7849dbSVille Syrjälä {
16118d7849dbSVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
16128d7849dbSVille Syrjälä 		return false;
16138d7849dbSVille Syrjälä 
16148d7849dbSVille Syrjälä 	return true;
16158d7849dbSVille Syrjälä }
16168d7849dbSVille Syrjälä 
1617c1874ed7SImre Deak static void valleyview_pipestat_irq_handler(struct drm_device *dev, u32 iir)
16187e231dbeSJesse Barnes {
1619c1874ed7SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
162091d181ddSImre Deak 	u32 pipe_stats[I915_MAX_PIPES] = { };
16217e231dbeSJesse Barnes 	int pipe;
16227e231dbeSJesse Barnes 
162358ead0d7SImre Deak 	spin_lock(&dev_priv->irq_lock);
1624055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
162591d181ddSImre Deak 		int reg;
1626bbb5eebfSDaniel Vetter 		u32 mask, iir_bit = 0;
162791d181ddSImre Deak 
1628bbb5eebfSDaniel Vetter 		/*
1629bbb5eebfSDaniel Vetter 		 * PIPESTAT bits get signalled even when the interrupt is
1630bbb5eebfSDaniel Vetter 		 * disabled with the mask bits, and some of the status bits do
1631bbb5eebfSDaniel Vetter 		 * not generate interrupts at all (like the underrun bit). Hence
1632bbb5eebfSDaniel Vetter 		 * we need to be careful that we only handle what we want to
1633bbb5eebfSDaniel Vetter 		 * handle.
1634bbb5eebfSDaniel Vetter 		 */
16350f239f4cSDaniel Vetter 
16360f239f4cSDaniel Vetter 		/* fifo underruns are filterered in the underrun handler. */
16370f239f4cSDaniel Vetter 		mask = PIPE_FIFO_UNDERRUN_STATUS;
1638bbb5eebfSDaniel Vetter 
1639bbb5eebfSDaniel Vetter 		switch (pipe) {
1640bbb5eebfSDaniel Vetter 		case PIPE_A:
1641bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
1642bbb5eebfSDaniel Vetter 			break;
1643bbb5eebfSDaniel Vetter 		case PIPE_B:
1644bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
1645bbb5eebfSDaniel Vetter 			break;
16463278f67fSVille Syrjälä 		case PIPE_C:
16473278f67fSVille Syrjälä 			iir_bit = I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
16483278f67fSVille Syrjälä 			break;
1649bbb5eebfSDaniel Vetter 		}
1650bbb5eebfSDaniel Vetter 		if (iir & iir_bit)
1651bbb5eebfSDaniel Vetter 			mask |= dev_priv->pipestat_irq_mask[pipe];
1652bbb5eebfSDaniel Vetter 
1653bbb5eebfSDaniel Vetter 		if (!mask)
165491d181ddSImre Deak 			continue;
165591d181ddSImre Deak 
165691d181ddSImre Deak 		reg = PIPESTAT(pipe);
1657bbb5eebfSDaniel Vetter 		mask |= PIPESTAT_INT_ENABLE_MASK;
1658bbb5eebfSDaniel Vetter 		pipe_stats[pipe] = I915_READ(reg) & mask;
16597e231dbeSJesse Barnes 
16607e231dbeSJesse Barnes 		/*
16617e231dbeSJesse Barnes 		 * Clear the PIPE*STAT regs before the IIR
16627e231dbeSJesse Barnes 		 */
166391d181ddSImre Deak 		if (pipe_stats[pipe] & (PIPE_FIFO_UNDERRUN_STATUS |
166491d181ddSImre Deak 					PIPESTAT_INT_STATUS_MASK))
16657e231dbeSJesse Barnes 			I915_WRITE(reg, pipe_stats[pipe]);
16667e231dbeSJesse Barnes 	}
166758ead0d7SImre Deak 	spin_unlock(&dev_priv->irq_lock);
16687e231dbeSJesse Barnes 
1669055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1670d6bbafa1SChris Wilson 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
1671d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
1672d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
167331acc7f5SJesse Barnes 
1674579a9b0eSImre Deak 		if (pipe_stats[pipe] & PLANE_FLIP_DONE_INT_STATUS_VLV) {
167531acc7f5SJesse Barnes 			intel_prepare_page_flip(dev, pipe);
167631acc7f5SJesse Barnes 			intel_finish_page_flip(dev, pipe);
167731acc7f5SJesse Barnes 		}
16784356d586SDaniel Vetter 
16794356d586SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1680277de95eSDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
16812d9d2b0bSVille Syrjälä 
16821f7247c0SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
16831f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
168431acc7f5SJesse Barnes 	}
168531acc7f5SJesse Barnes 
1686c1874ed7SImre Deak 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1687c1874ed7SImre Deak 		gmbus_irq_handler(dev);
1688c1874ed7SImre Deak }
1689c1874ed7SImre Deak 
169016c6c56bSVille Syrjälä static void i9xx_hpd_irq_handler(struct drm_device *dev)
169116c6c56bSVille Syrjälä {
169216c6c56bSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
169316c6c56bSVille Syrjälä 	u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
169442db67d6SVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
169516c6c56bSVille Syrjälä 
16960d2e4297SJani Nikula 	if (!hotplug_status)
16970d2e4297SJani Nikula 		return;
16980d2e4297SJani Nikula 
16993ff60f89SOscar Mateo 	I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
17003ff60f89SOscar Mateo 	/*
17013ff60f89SOscar Mateo 	 * Make sure hotplug status is cleared before we clear IIR, or else we
17023ff60f89SOscar Mateo 	 * may miss hotplug events.
17033ff60f89SOscar Mateo 	 */
17043ff60f89SOscar Mateo 	POSTING_READ(PORT_HOTPLUG_STAT);
17053ff60f89SOscar Mateo 
17064bca26d0SVille Syrjälä 	if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
170716c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
170816c6c56bSVille Syrjälä 
170958f2cf24SVille Syrjälä 		if (hotplug_trigger) {
1710fd63e2a9SImre Deak 			intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
1711fd63e2a9SImre Deak 					   hotplug_trigger, hpd_status_g4x,
1712fd63e2a9SImre Deak 					   i9xx_port_hotplug_long_detect);
171358f2cf24SVille Syrjälä 
1714676574dfSJani Nikula 			intel_hpd_irq_handler(dev, pin_mask, long_mask);
171558f2cf24SVille Syrjälä 		}
1716369712e8SJani Nikula 
1717369712e8SJani Nikula 		if (hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
1718369712e8SJani Nikula 			dp_aux_irq_handler(dev);
171916c6c56bSVille Syrjälä 	} else {
172016c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
172116c6c56bSVille Syrjälä 
172258f2cf24SVille Syrjälä 		if (hotplug_trigger) {
1723fd63e2a9SImre Deak 			intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
17244e3d1e26SVille Syrjälä 					   hotplug_trigger, hpd_status_i915,
1725fd63e2a9SImre Deak 					   i9xx_port_hotplug_long_detect);
1726676574dfSJani Nikula 			intel_hpd_irq_handler(dev, pin_mask, long_mask);
172716c6c56bSVille Syrjälä 		}
17283ff60f89SOscar Mateo 	}
172958f2cf24SVille Syrjälä }
173016c6c56bSVille Syrjälä 
1731c1874ed7SImre Deak static irqreturn_t valleyview_irq_handler(int irq, void *arg)
1732c1874ed7SImre Deak {
173345a83f84SDaniel Vetter 	struct drm_device *dev = arg;
17342d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1735c1874ed7SImre Deak 	u32 iir, gt_iir, pm_iir;
1736c1874ed7SImre Deak 	irqreturn_t ret = IRQ_NONE;
1737c1874ed7SImre Deak 
17382dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
17392dd2a883SImre Deak 		return IRQ_NONE;
17402dd2a883SImre Deak 
1741c1874ed7SImre Deak 	while (true) {
17423ff60f89SOscar Mateo 		/* Find, clear, then process each source of interrupt */
17433ff60f89SOscar Mateo 
1744c1874ed7SImre Deak 		gt_iir = I915_READ(GTIIR);
17453ff60f89SOscar Mateo 		if (gt_iir)
17463ff60f89SOscar Mateo 			I915_WRITE(GTIIR, gt_iir);
17473ff60f89SOscar Mateo 
1748c1874ed7SImre Deak 		pm_iir = I915_READ(GEN6_PMIIR);
17493ff60f89SOscar Mateo 		if (pm_iir)
17503ff60f89SOscar Mateo 			I915_WRITE(GEN6_PMIIR, pm_iir);
17513ff60f89SOscar Mateo 
17523ff60f89SOscar Mateo 		iir = I915_READ(VLV_IIR);
17533ff60f89SOscar Mateo 		if (iir) {
17543ff60f89SOscar Mateo 			/* Consume port before clearing IIR or we'll miss events */
17553ff60f89SOscar Mateo 			if (iir & I915_DISPLAY_PORT_INTERRUPT)
17563ff60f89SOscar Mateo 				i9xx_hpd_irq_handler(dev);
17573ff60f89SOscar Mateo 			I915_WRITE(VLV_IIR, iir);
17583ff60f89SOscar Mateo 		}
1759c1874ed7SImre Deak 
1760c1874ed7SImre Deak 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
1761c1874ed7SImre Deak 			goto out;
1762c1874ed7SImre Deak 
1763c1874ed7SImre Deak 		ret = IRQ_HANDLED;
1764c1874ed7SImre Deak 
17653ff60f89SOscar Mateo 		if (gt_iir)
1766c1874ed7SImre Deak 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
176760611c13SPaulo Zanoni 		if (pm_iir)
1768d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
17693ff60f89SOscar Mateo 		/* Call regardless, as some status bits might not be
17703ff60f89SOscar Mateo 		 * signalled in iir */
17713ff60f89SOscar Mateo 		valleyview_pipestat_irq_handler(dev, iir);
17727e231dbeSJesse Barnes 	}
17737e231dbeSJesse Barnes 
17747e231dbeSJesse Barnes out:
17757e231dbeSJesse Barnes 	return ret;
17767e231dbeSJesse Barnes }
17777e231dbeSJesse Barnes 
177843f328d7SVille Syrjälä static irqreturn_t cherryview_irq_handler(int irq, void *arg)
177943f328d7SVille Syrjälä {
178045a83f84SDaniel Vetter 	struct drm_device *dev = arg;
178143f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
178243f328d7SVille Syrjälä 	u32 master_ctl, iir;
178343f328d7SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
178443f328d7SVille Syrjälä 
17852dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
17862dd2a883SImre Deak 		return IRQ_NONE;
17872dd2a883SImre Deak 
17888e5fd599SVille Syrjälä 	for (;;) {
17898e5fd599SVille Syrjälä 		master_ctl = I915_READ(GEN8_MASTER_IRQ) & ~GEN8_MASTER_IRQ_CONTROL;
17903278f67fSVille Syrjälä 		iir = I915_READ(VLV_IIR);
17913278f67fSVille Syrjälä 
17923278f67fSVille Syrjälä 		if (master_ctl == 0 && iir == 0)
17938e5fd599SVille Syrjälä 			break;
179443f328d7SVille Syrjälä 
179527b6c122SOscar Mateo 		ret = IRQ_HANDLED;
179627b6c122SOscar Mateo 
179743f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, 0);
179843f328d7SVille Syrjälä 
179927b6c122SOscar Mateo 		/* Find, clear, then process each source of interrupt */
180027b6c122SOscar Mateo 
180127b6c122SOscar Mateo 		if (iir) {
180227b6c122SOscar Mateo 			/* Consume port before clearing IIR or we'll miss events */
180327b6c122SOscar Mateo 			if (iir & I915_DISPLAY_PORT_INTERRUPT)
180427b6c122SOscar Mateo 				i9xx_hpd_irq_handler(dev);
180527b6c122SOscar Mateo 			I915_WRITE(VLV_IIR, iir);
180627b6c122SOscar Mateo 		}
180727b6c122SOscar Mateo 
180874cdb337SChris Wilson 		gen8_gt_irq_handler(dev_priv, master_ctl);
180943f328d7SVille Syrjälä 
181027b6c122SOscar Mateo 		/* Call regardless, as some status bits might not be
181127b6c122SOscar Mateo 		 * signalled in iir */
18123278f67fSVille Syrjälä 		valleyview_pipestat_irq_handler(dev, iir);
181343f328d7SVille Syrjälä 
181443f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
181543f328d7SVille Syrjälä 		POSTING_READ(GEN8_MASTER_IRQ);
18168e5fd599SVille Syrjälä 	}
18173278f67fSVille Syrjälä 
181843f328d7SVille Syrjälä 	return ret;
181943f328d7SVille Syrjälä }
182043f328d7SVille Syrjälä 
182140e56410SVille Syrjälä static void ibx_hpd_irq_handler(struct drm_device *dev, u32 hotplug_trigger,
182240e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
1823776ad806SJesse Barnes {
182440e56410SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(dev);
182542db67d6SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
1826776ad806SJesse Barnes 
182713cf5504SDave Airlie 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
182813cf5504SDave Airlie 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
182913cf5504SDave Airlie 
1830fd63e2a9SImre Deak 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
183140e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
1832fd63e2a9SImre Deak 			   pch_port_hotplug_long_detect);
183340e56410SVille Syrjälä 
1834676574dfSJani Nikula 	intel_hpd_irq_handler(dev, pin_mask, long_mask);
1835aaf5ec2eSSonika Jindal }
183691d131d2SDaniel Vetter 
183740e56410SVille Syrjälä static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
183840e56410SVille Syrjälä {
183940e56410SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
184040e56410SVille Syrjälä 	int pipe;
184140e56410SVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
184240e56410SVille Syrjälä 
184340e56410SVille Syrjälä 	if (hotplug_trigger)
184440e56410SVille Syrjälä 		ibx_hpd_irq_handler(dev, hotplug_trigger, hpd_ibx);
184540e56410SVille Syrjälä 
1846cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1847cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1848776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1849cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1850cfc33bf7SVille Syrjälä 				 port_name(port));
1851cfc33bf7SVille Syrjälä 	}
1852776ad806SJesse Barnes 
1853ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
1854ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1855ce99c256SDaniel Vetter 
1856776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
1857515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
1858776ad806SJesse Barnes 
1859776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1860776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1861776ad806SJesse Barnes 
1862776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1863776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1864776ad806SJesse Barnes 
1865776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1866776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
1867776ad806SJesse Barnes 
18689db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
1869055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
18709db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
18719db4a9c7SJesse Barnes 					 pipe_name(pipe),
18729db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
1873776ad806SJesse Barnes 
1874776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1875776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1876776ad806SJesse Barnes 
1877776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1878776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1879776ad806SJesse Barnes 
1880776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
18811f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
18828664281bSPaulo Zanoni 
18838664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
18841f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
18858664281bSPaulo Zanoni }
18868664281bSPaulo Zanoni 
18878664281bSPaulo Zanoni static void ivb_err_int_handler(struct drm_device *dev)
18888664281bSPaulo Zanoni {
18898664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
18908664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
18915a69b89fSDaniel Vetter 	enum pipe pipe;
18928664281bSPaulo Zanoni 
1893de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
1894de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1895de032bf4SPaulo Zanoni 
1896055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
18971f7247c0SDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe))
18981f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
18998664281bSPaulo Zanoni 
19005a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
19015a69b89fSDaniel Vetter 			if (IS_IVYBRIDGE(dev))
1902277de95eSDaniel Vetter 				ivb_pipe_crc_irq_handler(dev, pipe);
19035a69b89fSDaniel Vetter 			else
1904277de95eSDaniel Vetter 				hsw_pipe_crc_irq_handler(dev, pipe);
19055a69b89fSDaniel Vetter 		}
19065a69b89fSDaniel Vetter 	}
19078bf1e9f1SShuang He 
19088664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
19098664281bSPaulo Zanoni }
19108664281bSPaulo Zanoni 
19118664281bSPaulo Zanoni static void cpt_serr_int_handler(struct drm_device *dev)
19128664281bSPaulo Zanoni {
19138664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
19148664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
19158664281bSPaulo Zanoni 
1916de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
1917de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
1918de032bf4SPaulo Zanoni 
19198664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
19201f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
19218664281bSPaulo Zanoni 
19228664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
19231f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
19248664281bSPaulo Zanoni 
19258664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
19261f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_C);
19278664281bSPaulo Zanoni 
19288664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
1929776ad806SJesse Barnes }
1930776ad806SJesse Barnes 
193123e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
193223e81d69SAdam Jackson {
19332d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
193423e81d69SAdam Jackson 	int pipe;
19356dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
1936aaf5ec2eSSonika Jindal 
193740e56410SVille Syrjälä 	if (hotplug_trigger)
193840e56410SVille Syrjälä 		ibx_hpd_irq_handler(dev, hotplug_trigger, hpd_cpt);
193991d131d2SDaniel Vetter 
1940cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
1941cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
194223e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
1943cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
1944cfc33bf7SVille Syrjälä 				 port_name(port));
1945cfc33bf7SVille Syrjälä 	}
194623e81d69SAdam Jackson 
194723e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
1948ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
194923e81d69SAdam Jackson 
195023e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
1951515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
195223e81d69SAdam Jackson 
195323e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
195423e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
195523e81d69SAdam Jackson 
195623e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
195723e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
195823e81d69SAdam Jackson 
195923e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
1960055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
196123e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
196223e81d69SAdam Jackson 					 pipe_name(pipe),
196323e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
19648664281bSPaulo Zanoni 
19658664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
19668664281bSPaulo Zanoni 		cpt_serr_int_handler(dev);
196723e81d69SAdam Jackson }
196823e81d69SAdam Jackson 
19696dbf30ceSVille Syrjälä static void spt_irq_handler(struct drm_device *dev, u32 pch_iir)
19706dbf30ceSVille Syrjälä {
19716dbf30ceSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
19726dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_SPT &
19736dbf30ceSVille Syrjälä 		~SDE_PORTE_HOTPLUG_SPT;
19746dbf30ceSVille Syrjälä 	u32 hotplug2_trigger = pch_iir & SDE_PORTE_HOTPLUG_SPT;
19756dbf30ceSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
19766dbf30ceSVille Syrjälä 
19776dbf30ceSVille Syrjälä 	if (hotplug_trigger) {
19786dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
19796dbf30ceSVille Syrjälä 
19806dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
19816dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
19826dbf30ceSVille Syrjälä 
19836dbf30ceSVille Syrjälä 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
19846dbf30ceSVille Syrjälä 				   dig_hotplug_reg, hpd_spt,
198574c0b395SVille Syrjälä 				   spt_port_hotplug_long_detect);
19866dbf30ceSVille Syrjälä 	}
19876dbf30ceSVille Syrjälä 
19886dbf30ceSVille Syrjälä 	if (hotplug2_trigger) {
19896dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
19906dbf30ceSVille Syrjälä 
19916dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG2);
19926dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG2, dig_hotplug_reg);
19936dbf30ceSVille Syrjälä 
19946dbf30ceSVille Syrjälä 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug2_trigger,
19956dbf30ceSVille Syrjälä 				   dig_hotplug_reg, hpd_spt,
19966dbf30ceSVille Syrjälä 				   spt_port_hotplug2_long_detect);
19976dbf30ceSVille Syrjälä 	}
19986dbf30ceSVille Syrjälä 
19996dbf30ceSVille Syrjälä 	if (pin_mask)
20006dbf30ceSVille Syrjälä 		intel_hpd_irq_handler(dev, pin_mask, long_mask);
20016dbf30ceSVille Syrjälä 
20026dbf30ceSVille Syrjälä 	if (pch_iir & SDE_GMBUS_CPT)
20036dbf30ceSVille Syrjälä 		gmbus_irq_handler(dev);
20046dbf30ceSVille Syrjälä }
20056dbf30ceSVille Syrjälä 
200640e56410SVille Syrjälä static void ilk_hpd_irq_handler(struct drm_device *dev, u32 hotplug_trigger,
200740e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2008c008bc6eSPaulo Zanoni {
200940e56410SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(dev);
2010e4ce95aaSVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2011e4ce95aaSVille Syrjälä 
2012e4ce95aaSVille Syrjälä 	dig_hotplug_reg = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
2013e4ce95aaSVille Syrjälä 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, dig_hotplug_reg);
2014e4ce95aaSVille Syrjälä 
2015e4ce95aaSVille Syrjälä 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
201640e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2017e4ce95aaSVille Syrjälä 			   ilk_port_hotplug_long_detect);
201840e56410SVille Syrjälä 
2019e4ce95aaSVille Syrjälä 	intel_hpd_irq_handler(dev, pin_mask, long_mask);
2020e4ce95aaSVille Syrjälä }
2021c008bc6eSPaulo Zanoni 
202240e56410SVille Syrjälä static void ilk_display_irq_handler(struct drm_device *dev, u32 de_iir)
202340e56410SVille Syrjälä {
202440e56410SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
202540e56410SVille Syrjälä 	enum pipe pipe;
202640e56410SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG;
202740e56410SVille Syrjälä 
202840e56410SVille Syrjälä 	if (hotplug_trigger)
202940e56410SVille Syrjälä 		ilk_hpd_irq_handler(dev, hotplug_trigger, hpd_ilk);
203040e56410SVille Syrjälä 
2031c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
2032c008bc6eSPaulo Zanoni 		dp_aux_irq_handler(dev);
2033c008bc6eSPaulo Zanoni 
2034c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
2035c008bc6eSPaulo Zanoni 		intel_opregion_asle_intr(dev);
2036c008bc6eSPaulo Zanoni 
2037c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
2038c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
2039c008bc6eSPaulo Zanoni 
2040055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2041d6bbafa1SChris Wilson 		if (de_iir & DE_PIPE_VBLANK(pipe) &&
2042d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
2043d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
2044c008bc6eSPaulo Zanoni 
204540da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
20461f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
2047c008bc6eSPaulo Zanoni 
204840da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
204940da17c2SDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
20505b3a856bSDaniel Vetter 
205140da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
205240da17c2SDaniel Vetter 		if (de_iir & DE_PLANE_FLIP_DONE(pipe)) {
205340da17c2SDaniel Vetter 			intel_prepare_page_flip(dev, pipe);
205440da17c2SDaniel Vetter 			intel_finish_page_flip_plane(dev, pipe);
2055c008bc6eSPaulo Zanoni 		}
2056c008bc6eSPaulo Zanoni 	}
2057c008bc6eSPaulo Zanoni 
2058c008bc6eSPaulo Zanoni 	/* check event from PCH */
2059c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
2060c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
2061c008bc6eSPaulo Zanoni 
2062c008bc6eSPaulo Zanoni 		if (HAS_PCH_CPT(dev))
2063c008bc6eSPaulo Zanoni 			cpt_irq_handler(dev, pch_iir);
2064c008bc6eSPaulo Zanoni 		else
2065c008bc6eSPaulo Zanoni 			ibx_irq_handler(dev, pch_iir);
2066c008bc6eSPaulo Zanoni 
2067c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
2068c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
2069c008bc6eSPaulo Zanoni 	}
2070c008bc6eSPaulo Zanoni 
2071c008bc6eSPaulo Zanoni 	if (IS_GEN5(dev) && de_iir & DE_PCU_EVENT)
2072c008bc6eSPaulo Zanoni 		ironlake_rps_change_irq_handler(dev);
2073c008bc6eSPaulo Zanoni }
2074c008bc6eSPaulo Zanoni 
20759719fb98SPaulo Zanoni static void ivb_display_irq_handler(struct drm_device *dev, u32 de_iir)
20769719fb98SPaulo Zanoni {
20779719fb98SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
207807d27e20SDamien Lespiau 	enum pipe pipe;
207923bb4cb5SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG_IVB;
208023bb4cb5SVille Syrjälä 
208140e56410SVille Syrjälä 	if (hotplug_trigger)
208240e56410SVille Syrjälä 		ilk_hpd_irq_handler(dev, hotplug_trigger, hpd_ivb);
20839719fb98SPaulo Zanoni 
20849719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
20859719fb98SPaulo Zanoni 		ivb_err_int_handler(dev);
20869719fb98SPaulo Zanoni 
20879719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
20889719fb98SPaulo Zanoni 		dp_aux_irq_handler(dev);
20899719fb98SPaulo Zanoni 
20909719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
20919719fb98SPaulo Zanoni 		intel_opregion_asle_intr(dev);
20929719fb98SPaulo Zanoni 
2093055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2094d6bbafa1SChris Wilson 		if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)) &&
2095d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
2096d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
209740da17c2SDaniel Vetter 
209840da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
209907d27e20SDamien Lespiau 		if (de_iir & DE_PLANE_FLIP_DONE_IVB(pipe)) {
210007d27e20SDamien Lespiau 			intel_prepare_page_flip(dev, pipe);
210107d27e20SDamien Lespiau 			intel_finish_page_flip_plane(dev, pipe);
21029719fb98SPaulo Zanoni 		}
21039719fb98SPaulo Zanoni 	}
21049719fb98SPaulo Zanoni 
21059719fb98SPaulo Zanoni 	/* check event from PCH */
21069719fb98SPaulo Zanoni 	if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
21079719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
21089719fb98SPaulo Zanoni 
21099719fb98SPaulo Zanoni 		cpt_irq_handler(dev, pch_iir);
21109719fb98SPaulo Zanoni 
21119719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
21129719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
21139719fb98SPaulo Zanoni 	}
21149719fb98SPaulo Zanoni }
21159719fb98SPaulo Zanoni 
211672c90f62SOscar Mateo /*
211772c90f62SOscar Mateo  * To handle irqs with the minimum potential races with fresh interrupts, we:
211872c90f62SOscar Mateo  * 1 - Disable Master Interrupt Control.
211972c90f62SOscar Mateo  * 2 - Find the source(s) of the interrupt.
212072c90f62SOscar Mateo  * 3 - Clear the Interrupt Identity bits (IIR).
212172c90f62SOscar Mateo  * 4 - Process the interrupt(s) that had bits set in the IIRs.
212272c90f62SOscar Mateo  * 5 - Re-enable Master Interrupt Control.
212372c90f62SOscar Mateo  */
2124f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
2125b1f14ad0SJesse Barnes {
212645a83f84SDaniel Vetter 	struct drm_device *dev = arg;
21272d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2128f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
21290e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
2130b1f14ad0SJesse Barnes 
21312dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
21322dd2a883SImre Deak 		return IRQ_NONE;
21332dd2a883SImre Deak 
21348664281bSPaulo Zanoni 	/* We get interrupts on unclaimed registers, so check for this before we
21358664281bSPaulo Zanoni 	 * do any I915_{READ,WRITE}. */
2136907b28c5SChris Wilson 	intel_uncore_check_errors(dev);
21378664281bSPaulo Zanoni 
2138b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
2139b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
2140b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
214123a78516SPaulo Zanoni 	POSTING_READ(DEIER);
21420e43406bSChris Wilson 
214344498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
214444498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
214544498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
214644498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
214744498aeaSPaulo Zanoni 	 * due to its back queue). */
2148ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
214944498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
215044498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
215144498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2152ab5c608bSBen Widawsky 	}
215344498aeaSPaulo Zanoni 
215472c90f62SOscar Mateo 	/* Find, clear, then process each source of interrupt */
215572c90f62SOscar Mateo 
21560e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
21570e43406bSChris Wilson 	if (gt_iir) {
215872c90f62SOscar Mateo 		I915_WRITE(GTIIR, gt_iir);
215972c90f62SOscar Mateo 		ret = IRQ_HANDLED;
2160d8fc8a47SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 6)
21610e43406bSChris Wilson 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
2162d8fc8a47SPaulo Zanoni 		else
2163d8fc8a47SPaulo Zanoni 			ilk_gt_irq_handler(dev, dev_priv, gt_iir);
21640e43406bSChris Wilson 	}
2165b1f14ad0SJesse Barnes 
2166b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
21670e43406bSChris Wilson 	if (de_iir) {
216872c90f62SOscar Mateo 		I915_WRITE(DEIIR, de_iir);
216972c90f62SOscar Mateo 		ret = IRQ_HANDLED;
2170f1af8fc1SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 7)
21719719fb98SPaulo Zanoni 			ivb_display_irq_handler(dev, de_iir);
2172f1af8fc1SPaulo Zanoni 		else
2173f1af8fc1SPaulo Zanoni 			ilk_display_irq_handler(dev, de_iir);
21740e43406bSChris Wilson 	}
21750e43406bSChris Wilson 
2176f1af8fc1SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6) {
2177f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
21780e43406bSChris Wilson 		if (pm_iir) {
2179b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
21800e43406bSChris Wilson 			ret = IRQ_HANDLED;
218172c90f62SOscar Mateo 			gen6_rps_irq_handler(dev_priv, pm_iir);
21820e43406bSChris Wilson 		}
2183f1af8fc1SPaulo Zanoni 	}
2184b1f14ad0SJesse Barnes 
2185b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
2186b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
2187ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
218844498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
218944498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2190ab5c608bSBen Widawsky 	}
2191b1f14ad0SJesse Barnes 
2192b1f14ad0SJesse Barnes 	return ret;
2193b1f14ad0SJesse Barnes }
2194b1f14ad0SJesse Barnes 
219540e56410SVille Syrjälä static void bxt_hpd_irq_handler(struct drm_device *dev, u32 hotplug_trigger,
219640e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2197d04a492dSShashank Sharma {
2198cebd87a0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(dev);
2199cebd87a0SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2200d04a492dSShashank Sharma 
2201a52bb15bSVille Syrjälä 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
2202a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
2203d04a492dSShashank Sharma 
2204cebd87a0SVille Syrjälä 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
220540e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2206cebd87a0SVille Syrjälä 			   bxt_port_hotplug_long_detect);
220740e56410SVille Syrjälä 
2208475c2e3bSJani Nikula 	intel_hpd_irq_handler(dev, pin_mask, long_mask);
2209d04a492dSShashank Sharma }
2210d04a492dSShashank Sharma 
2211abd58f01SBen Widawsky static irqreturn_t gen8_irq_handler(int irq, void *arg)
2212abd58f01SBen Widawsky {
2213abd58f01SBen Widawsky 	struct drm_device *dev = arg;
2214abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2215abd58f01SBen Widawsky 	u32 master_ctl;
2216abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
2217abd58f01SBen Widawsky 	uint32_t tmp = 0;
2218c42664ccSDaniel Vetter 	enum pipe pipe;
221988e04703SJesse Barnes 	u32 aux_mask = GEN8_AUX_CHANNEL_A;
222088e04703SJesse Barnes 
22212dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
22222dd2a883SImre Deak 		return IRQ_NONE;
22232dd2a883SImre Deak 
2224b4834a50SRodrigo Vivi 	if (INTEL_INFO(dev_priv)->gen >= 9)
222588e04703SJesse Barnes 		aux_mask |=  GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
222688e04703SJesse Barnes 			GEN9_AUX_CHANNEL_D;
2227abd58f01SBen Widawsky 
2228cb0d205eSChris Wilson 	master_ctl = I915_READ_FW(GEN8_MASTER_IRQ);
2229abd58f01SBen Widawsky 	master_ctl &= ~GEN8_MASTER_IRQ_CONTROL;
2230abd58f01SBen Widawsky 	if (!master_ctl)
2231abd58f01SBen Widawsky 		return IRQ_NONE;
2232abd58f01SBen Widawsky 
2233cb0d205eSChris Wilson 	I915_WRITE_FW(GEN8_MASTER_IRQ, 0);
2234abd58f01SBen Widawsky 
223538cc46d7SOscar Mateo 	/* Find, clear, then process each source of interrupt */
223638cc46d7SOscar Mateo 
223774cdb337SChris Wilson 	ret = gen8_gt_irq_handler(dev_priv, master_ctl);
2238abd58f01SBen Widawsky 
2239abd58f01SBen Widawsky 	if (master_ctl & GEN8_DE_MISC_IRQ) {
2240abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_DE_MISC_IIR);
2241abd58f01SBen Widawsky 		if (tmp) {
2242abd58f01SBen Widawsky 			I915_WRITE(GEN8_DE_MISC_IIR, tmp);
2243abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
224438cc46d7SOscar Mateo 			if (tmp & GEN8_DE_MISC_GSE)
224538cc46d7SOscar Mateo 				intel_opregion_asle_intr(dev);
224638cc46d7SOscar Mateo 			else
224738cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Misc interrupt\n");
2248abd58f01SBen Widawsky 		}
224938cc46d7SOscar Mateo 		else
225038cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
2251abd58f01SBen Widawsky 	}
2252abd58f01SBen Widawsky 
22536d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
22546d766f02SDaniel Vetter 		tmp = I915_READ(GEN8_DE_PORT_IIR);
22556d766f02SDaniel Vetter 		if (tmp) {
2256d04a492dSShashank Sharma 			bool found = false;
2257cebd87a0SVille Syrjälä 			u32 hotplug_trigger = 0;
2258cebd87a0SVille Syrjälä 
2259cebd87a0SVille Syrjälä 			if (IS_BROXTON(dev_priv))
2260cebd87a0SVille Syrjälä 				hotplug_trigger = tmp & BXT_DE_PORT_HOTPLUG_MASK;
2261cebd87a0SVille Syrjälä 			else if (IS_BROADWELL(dev_priv))
2262cebd87a0SVille Syrjälä 				hotplug_trigger = tmp & GEN8_PORT_DP_A_HOTPLUG;
2263d04a492dSShashank Sharma 
22646d766f02SDaniel Vetter 			I915_WRITE(GEN8_DE_PORT_IIR, tmp);
22656d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
226688e04703SJesse Barnes 
2267d04a492dSShashank Sharma 			if (tmp & aux_mask) {
226838cc46d7SOscar Mateo 				dp_aux_irq_handler(dev);
2269d04a492dSShashank Sharma 				found = true;
2270d04a492dSShashank Sharma 			}
2271d04a492dSShashank Sharma 
227240e56410SVille Syrjälä 			if (hotplug_trigger) {
227340e56410SVille Syrjälä 				if (IS_BROXTON(dev))
227440e56410SVille Syrjälä 					bxt_hpd_irq_handler(dev, hotplug_trigger, hpd_bxt);
227540e56410SVille Syrjälä 				else
227640e56410SVille Syrjälä 					ilk_hpd_irq_handler(dev, hotplug_trigger, hpd_bdw);
2277d04a492dSShashank Sharma 				found = true;
2278d04a492dSShashank Sharma 			}
2279d04a492dSShashank Sharma 
22809e63743eSShashank Sharma 			if (IS_BROXTON(dev) && (tmp & BXT_DE_PORT_GMBUS)) {
22819e63743eSShashank Sharma 				gmbus_irq_handler(dev);
22829e63743eSShashank Sharma 				found = true;
22839e63743eSShashank Sharma 			}
22849e63743eSShashank Sharma 
2285d04a492dSShashank Sharma 			if (!found)
228638cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Port interrupt\n");
22876d766f02SDaniel Vetter 		}
228838cc46d7SOscar Mateo 		else
228938cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
22906d766f02SDaniel Vetter 	}
22916d766f02SDaniel Vetter 
2292055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2293770de83dSDamien Lespiau 		uint32_t pipe_iir, flip_done = 0, fault_errors = 0;
2294abd58f01SBen Widawsky 
2295c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2296c42664ccSDaniel Vetter 			continue;
2297c42664ccSDaniel Vetter 
2298abd58f01SBen Widawsky 		pipe_iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
229938cc46d7SOscar Mateo 		if (pipe_iir) {
230038cc46d7SOscar Mateo 			ret = IRQ_HANDLED;
230138cc46d7SOscar Mateo 			I915_WRITE(GEN8_DE_PIPE_IIR(pipe), pipe_iir);
2302770de83dSDamien Lespiau 
2303d6bbafa1SChris Wilson 			if (pipe_iir & GEN8_PIPE_VBLANK &&
2304d6bbafa1SChris Wilson 			    intel_pipe_handle_vblank(dev, pipe))
2305d6bbafa1SChris Wilson 				intel_check_page_flip(dev, pipe);
2306abd58f01SBen Widawsky 
2307b4834a50SRodrigo Vivi 			if (INTEL_INFO(dev_priv)->gen >= 9)
2308770de83dSDamien Lespiau 				flip_done = pipe_iir & GEN9_PIPE_PLANE1_FLIP_DONE;
2309770de83dSDamien Lespiau 			else
2310770de83dSDamien Lespiau 				flip_done = pipe_iir & GEN8_PIPE_PRIMARY_FLIP_DONE;
2311770de83dSDamien Lespiau 
2312770de83dSDamien Lespiau 			if (flip_done) {
2313abd58f01SBen Widawsky 				intel_prepare_page_flip(dev, pipe);
2314abd58f01SBen Widawsky 				intel_finish_page_flip_plane(dev, pipe);
2315abd58f01SBen Widawsky 			}
2316abd58f01SBen Widawsky 
23170fbe7870SDaniel Vetter 			if (pipe_iir & GEN8_PIPE_CDCLK_CRC_DONE)
23180fbe7870SDaniel Vetter 				hsw_pipe_crc_irq_handler(dev, pipe);
23190fbe7870SDaniel Vetter 
23201f7247c0SDaniel Vetter 			if (pipe_iir & GEN8_PIPE_FIFO_UNDERRUN)
23211f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
23221f7247c0SDaniel Vetter 								    pipe);
232338d83c96SDaniel Vetter 
2324770de83dSDamien Lespiau 
2325b4834a50SRodrigo Vivi 			if (INTEL_INFO(dev_priv)->gen >= 9)
2326770de83dSDamien Lespiau 				fault_errors = pipe_iir & GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
2327770de83dSDamien Lespiau 			else
2328770de83dSDamien Lespiau 				fault_errors = pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
2329770de83dSDamien Lespiau 
2330770de83dSDamien Lespiau 			if (fault_errors)
233130100f2bSDaniel Vetter 				DRM_ERROR("Fault errors on pipe %c\n: 0x%08x",
233230100f2bSDaniel Vetter 					  pipe_name(pipe),
233330100f2bSDaniel Vetter 					  pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS);
2334c42664ccSDaniel Vetter 		} else
2335abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
2336abd58f01SBen Widawsky 	}
2337abd58f01SBen Widawsky 
2338266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev) && !HAS_PCH_NOP(dev) &&
2339266ea3d9SShashank Sharma 	    master_ctl & GEN8_DE_PCH_IRQ) {
234092d03a80SDaniel Vetter 		/*
234192d03a80SDaniel Vetter 		 * FIXME(BDW): Assume for now that the new interrupt handling
234292d03a80SDaniel Vetter 		 * scheme also closed the SDE interrupt handling race we've seen
234392d03a80SDaniel Vetter 		 * on older pch-split platforms. But this needs testing.
234492d03a80SDaniel Vetter 		 */
234592d03a80SDaniel Vetter 		u32 pch_iir = I915_READ(SDEIIR);
234692d03a80SDaniel Vetter 		if (pch_iir) {
234792d03a80SDaniel Vetter 			I915_WRITE(SDEIIR, pch_iir);
234892d03a80SDaniel Vetter 			ret = IRQ_HANDLED;
23496dbf30ceSVille Syrjälä 
23506dbf30ceSVille Syrjälä 			if (HAS_PCH_SPT(dev_priv))
23516dbf30ceSVille Syrjälä 				spt_irq_handler(dev, pch_iir);
23526dbf30ceSVille Syrjälä 			else
235338cc46d7SOscar Mateo 				cpt_irq_handler(dev, pch_iir);
235438cc46d7SOscar Mateo 		} else
235538cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (SDE)!\n");
235638cc46d7SOscar Mateo 
235792d03a80SDaniel Vetter 	}
235892d03a80SDaniel Vetter 
2359cb0d205eSChris Wilson 	I915_WRITE_FW(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
2360cb0d205eSChris Wilson 	POSTING_READ_FW(GEN8_MASTER_IRQ);
2361abd58f01SBen Widawsky 
2362abd58f01SBen Widawsky 	return ret;
2363abd58f01SBen Widawsky }
2364abd58f01SBen Widawsky 
236517e1df07SDaniel Vetter static void i915_error_wake_up(struct drm_i915_private *dev_priv,
236617e1df07SDaniel Vetter 			       bool reset_completed)
236717e1df07SDaniel Vetter {
2368a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
236917e1df07SDaniel Vetter 	int i;
237017e1df07SDaniel Vetter 
237117e1df07SDaniel Vetter 	/*
237217e1df07SDaniel Vetter 	 * Notify all waiters for GPU completion events that reset state has
237317e1df07SDaniel Vetter 	 * been changed, and that they need to restart their wait after
237417e1df07SDaniel Vetter 	 * checking for potential errors (and bail out to drop locks if there is
237517e1df07SDaniel Vetter 	 * a gpu reset pending so that i915_error_work_func can acquire them).
237617e1df07SDaniel Vetter 	 */
237717e1df07SDaniel Vetter 
237817e1df07SDaniel Vetter 	/* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
237917e1df07SDaniel Vetter 	for_each_ring(ring, dev_priv, i)
238017e1df07SDaniel Vetter 		wake_up_all(&ring->irq_queue);
238117e1df07SDaniel Vetter 
238217e1df07SDaniel Vetter 	/* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
238317e1df07SDaniel Vetter 	wake_up_all(&dev_priv->pending_flip_queue);
238417e1df07SDaniel Vetter 
238517e1df07SDaniel Vetter 	/*
238617e1df07SDaniel Vetter 	 * Signal tasks blocked in i915_gem_wait_for_error that the pending
238717e1df07SDaniel Vetter 	 * reset state is cleared.
238817e1df07SDaniel Vetter 	 */
238917e1df07SDaniel Vetter 	if (reset_completed)
239017e1df07SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
239117e1df07SDaniel Vetter }
239217e1df07SDaniel Vetter 
23938a905236SJesse Barnes /**
2394b8d24a06SMika Kuoppala  * i915_reset_and_wakeup - do process context error handling work
23958a905236SJesse Barnes  *
23968a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
23978a905236SJesse Barnes  * was detected.
23988a905236SJesse Barnes  */
2399b8d24a06SMika Kuoppala static void i915_reset_and_wakeup(struct drm_device *dev)
24008a905236SJesse Barnes {
2401b8d24a06SMika Kuoppala 	struct drm_i915_private *dev_priv = to_i915(dev);
2402b8d24a06SMika Kuoppala 	struct i915_gpu_error *error = &dev_priv->gpu_error;
2403cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
2404cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
2405cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
240617e1df07SDaniel Vetter 	int ret;
24078a905236SJesse Barnes 
24085bdebb18SDave Airlie 	kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, error_event);
24098a905236SJesse Barnes 
24107db0ba24SDaniel Vetter 	/*
24117db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
24127db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
24137db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
24147db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
24157db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
24167db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
24177db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
24187db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
24197db0ba24SDaniel Vetter 	 */
24207db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
242144d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
24225bdebb18SDave Airlie 		kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE,
24237db0ba24SDaniel Vetter 				   reset_event);
24241f83fee0SDaniel Vetter 
242517e1df07SDaniel Vetter 		/*
2426f454c694SImre Deak 		 * In most cases it's guaranteed that we get here with an RPM
2427f454c694SImre Deak 		 * reference held, for example because there is a pending GPU
2428f454c694SImre Deak 		 * request that won't finish until the reset is done. This
2429f454c694SImre Deak 		 * isn't the case at least when we get here by doing a
2430f454c694SImre Deak 		 * simulated reset via debugs, so get an RPM reference.
2431f454c694SImre Deak 		 */
2432f454c694SImre Deak 		intel_runtime_pm_get(dev_priv);
24337514747dSVille Syrjälä 
24347514747dSVille Syrjälä 		intel_prepare_reset(dev);
24357514747dSVille Syrjälä 
2436f454c694SImre Deak 		/*
243717e1df07SDaniel Vetter 		 * All state reset _must_ be completed before we update the
243817e1df07SDaniel Vetter 		 * reset counter, for otherwise waiters might miss the reset
243917e1df07SDaniel Vetter 		 * pending state and not properly drop locks, resulting in
244017e1df07SDaniel Vetter 		 * deadlocks with the reset work.
244117e1df07SDaniel Vetter 		 */
2442f69061beSDaniel Vetter 		ret = i915_reset(dev);
2443f69061beSDaniel Vetter 
24447514747dSVille Syrjälä 		intel_finish_reset(dev);
244517e1df07SDaniel Vetter 
2446f454c694SImre Deak 		intel_runtime_pm_put(dev_priv);
2447f454c694SImre Deak 
2448f69061beSDaniel Vetter 		if (ret == 0) {
2449f69061beSDaniel Vetter 			/*
2450f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
2451f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
2452f69061beSDaniel Vetter 			 * complete.
2453f69061beSDaniel Vetter 			 *
2454f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
2455f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
2456f69061beSDaniel Vetter 			 * updates before
2457f69061beSDaniel Vetter 			 * the counter increment.
2458f69061beSDaniel Vetter 			 */
24594e857c58SPeter Zijlstra 			smp_mb__before_atomic();
2460f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
2461f69061beSDaniel Vetter 
24625bdebb18SDave Airlie 			kobject_uevent_env(&dev->primary->kdev->kobj,
2463f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
24641f83fee0SDaniel Vetter 		} else {
2465805de8f4SPeter Zijlstra 			atomic_or(I915_WEDGED, &error->reset_counter);
2466f316a42cSBen Gamari 		}
24671f83fee0SDaniel Vetter 
246817e1df07SDaniel Vetter 		/*
246917e1df07SDaniel Vetter 		 * Note: The wake_up also serves as a memory barrier so that
247017e1df07SDaniel Vetter 		 * waiters see the update value of the reset counter atomic_t.
247117e1df07SDaniel Vetter 		 */
247217e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, true);
2473f316a42cSBen Gamari 	}
24748a905236SJesse Barnes }
24758a905236SJesse Barnes 
247635aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
2477c0e09200SDave Airlie {
24788a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
2479bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
248063eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
2481050ee91fSBen Widawsky 	int pipe, i;
248263eeaf38SJesse Barnes 
248335aed2e6SChris Wilson 	if (!eir)
248435aed2e6SChris Wilson 		return;
248563eeaf38SJesse Barnes 
2486a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
24878a905236SJesse Barnes 
2488bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
2489bd9854f9SBen Widawsky 
24908a905236SJesse Barnes 	if (IS_G4X(dev)) {
24918a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
24928a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
24938a905236SJesse Barnes 
2494a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2495a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2496050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
2497050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2498a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2499a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
25008a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
25013143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
25028a905236SJesse Barnes 		}
25038a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
25048a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2505a70491ccSJoe Perches 			pr_err("page table error\n");
2506a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
25078a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
25083143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
25098a905236SJesse Barnes 		}
25108a905236SJesse Barnes 	}
25118a905236SJesse Barnes 
2512a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
251363eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
251463eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2515a70491ccSJoe Perches 			pr_err("page table error\n");
2516a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
251763eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
25183143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
251963eeaf38SJesse Barnes 		}
25208a905236SJesse Barnes 	}
25218a905236SJesse Barnes 
252263eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
2523a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
2524055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
2525a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
25269db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
252763eeaf38SJesse Barnes 		/* pipestat has already been acked */
252863eeaf38SJesse Barnes 	}
252963eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
2530a70491ccSJoe Perches 		pr_err("instruction error\n");
2531a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
2532050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
2533050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2534a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
253563eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
253663eeaf38SJesse Barnes 
2537a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
2538a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
2539a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
254063eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
25413143a2bfSChris Wilson 			POSTING_READ(IPEIR);
254263eeaf38SJesse Barnes 		} else {
254363eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
254463eeaf38SJesse Barnes 
2545a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2546a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2547a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2548a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
254963eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
25503143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
255163eeaf38SJesse Barnes 		}
255263eeaf38SJesse Barnes 	}
255363eeaf38SJesse Barnes 
255463eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
25553143a2bfSChris Wilson 	POSTING_READ(EIR);
255663eeaf38SJesse Barnes 	eir = I915_READ(EIR);
255763eeaf38SJesse Barnes 	if (eir) {
255863eeaf38SJesse Barnes 		/*
255963eeaf38SJesse Barnes 		 * some errors might have become stuck,
256063eeaf38SJesse Barnes 		 * mask them.
256163eeaf38SJesse Barnes 		 */
256263eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
256363eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
256463eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
256563eeaf38SJesse Barnes 	}
256635aed2e6SChris Wilson }
256735aed2e6SChris Wilson 
256835aed2e6SChris Wilson /**
2569b8d24a06SMika Kuoppala  * i915_handle_error - handle a gpu error
257035aed2e6SChris Wilson  * @dev: drm device
257135aed2e6SChris Wilson  *
2572b8d24a06SMika Kuoppala  * Do some basic checking of regsiter state at error time and
257335aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
257435aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
257535aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
257635aed2e6SChris Wilson  * of a ring dump etc.).
257735aed2e6SChris Wilson  */
257858174462SMika Kuoppala void i915_handle_error(struct drm_device *dev, bool wedged,
257958174462SMika Kuoppala 		       const char *fmt, ...)
258035aed2e6SChris Wilson {
258135aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
258258174462SMika Kuoppala 	va_list args;
258358174462SMika Kuoppala 	char error_msg[80];
258435aed2e6SChris Wilson 
258558174462SMika Kuoppala 	va_start(args, fmt);
258658174462SMika Kuoppala 	vscnprintf(error_msg, sizeof(error_msg), fmt, args);
258758174462SMika Kuoppala 	va_end(args);
258858174462SMika Kuoppala 
258958174462SMika Kuoppala 	i915_capture_error_state(dev, wedged, error_msg);
259035aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
25918a905236SJesse Barnes 
2592ba1234d1SBen Gamari 	if (wedged) {
2593805de8f4SPeter Zijlstra 		atomic_or(I915_RESET_IN_PROGRESS_FLAG,
2594f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
2595ba1234d1SBen Gamari 
259611ed50ecSBen Gamari 		/*
2597b8d24a06SMika Kuoppala 		 * Wakeup waiting processes so that the reset function
2598b8d24a06SMika Kuoppala 		 * i915_reset_and_wakeup doesn't deadlock trying to grab
2599b8d24a06SMika Kuoppala 		 * various locks. By bumping the reset counter first, the woken
260017e1df07SDaniel Vetter 		 * processes will see a reset in progress and back off,
260117e1df07SDaniel Vetter 		 * releasing their locks and then wait for the reset completion.
260217e1df07SDaniel Vetter 		 * We must do this for _all_ gpu waiters that might hold locks
260317e1df07SDaniel Vetter 		 * that the reset work needs to acquire.
260417e1df07SDaniel Vetter 		 *
260517e1df07SDaniel Vetter 		 * Note: The wake_up serves as the required memory barrier to
260617e1df07SDaniel Vetter 		 * ensure that the waiters see the updated value of the reset
260717e1df07SDaniel Vetter 		 * counter atomic_t.
260811ed50ecSBen Gamari 		 */
260917e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, false);
261011ed50ecSBen Gamari 	}
261111ed50ecSBen Gamari 
2612b8d24a06SMika Kuoppala 	i915_reset_and_wakeup(dev);
26138a905236SJesse Barnes }
26148a905236SJesse Barnes 
261542f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
261642f52ef8SKeith Packard  * we use as a pipe index
261742f52ef8SKeith Packard  */
2618f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
26190a3e67a4SJesse Barnes {
26202d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2621e9d21d7fSKeith Packard 	unsigned long irqflags;
262271e0ffa5SJesse Barnes 
26231ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2624f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
26257c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2626755e9019SImre Deak 				     PIPE_START_VBLANK_INTERRUPT_STATUS);
26270a3e67a4SJesse Barnes 	else
26287c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2629755e9019SImre Deak 				     PIPE_VBLANK_INTERRUPT_STATUS);
26301ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26318692d00eSChris Wilson 
26320a3e67a4SJesse Barnes 	return 0;
26330a3e67a4SJesse Barnes }
26340a3e67a4SJesse Barnes 
2635f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
2636f796cf8fSJesse Barnes {
26372d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2638f796cf8fSJesse Barnes 	unsigned long irqflags;
2639b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
264040da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2641f796cf8fSJesse Barnes 
2642f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2643b518421fSPaulo Zanoni 	ironlake_enable_display_irq(dev_priv, bit);
2644b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2645b1f14ad0SJesse Barnes 
2646b1f14ad0SJesse Barnes 	return 0;
2647b1f14ad0SJesse Barnes }
2648b1f14ad0SJesse Barnes 
26497e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
26507e231dbeSJesse Barnes {
26512d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
26527e231dbeSJesse Barnes 	unsigned long irqflags;
26537e231dbeSJesse Barnes 
26547e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
265531acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
2656755e9019SImre Deak 			     PIPE_START_VBLANK_INTERRUPT_STATUS);
26577e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26587e231dbeSJesse Barnes 
26597e231dbeSJesse Barnes 	return 0;
26607e231dbeSJesse Barnes }
26617e231dbeSJesse Barnes 
2662abd58f01SBen Widawsky static int gen8_enable_vblank(struct drm_device *dev, int pipe)
2663abd58f01SBen Widawsky {
2664abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2665abd58f01SBen Widawsky 	unsigned long irqflags;
2666abd58f01SBen Widawsky 
2667abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
26687167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] &= ~GEN8_PIPE_VBLANK;
26697167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2670abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2671abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2672abd58f01SBen Widawsky 	return 0;
2673abd58f01SBen Widawsky }
2674abd58f01SBen Widawsky 
267542f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
267642f52ef8SKeith Packard  * we use as a pipe index
267742f52ef8SKeith Packard  */
2678f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
26790a3e67a4SJesse Barnes {
26802d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2681e9d21d7fSKeith Packard 	unsigned long irqflags;
26820a3e67a4SJesse Barnes 
26831ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
26847c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
2685755e9019SImre Deak 			      PIPE_VBLANK_INTERRUPT_STATUS |
2686755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
26871ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26880a3e67a4SJesse Barnes }
26890a3e67a4SJesse Barnes 
2690f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
2691f796cf8fSJesse Barnes {
26922d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2693f796cf8fSJesse Barnes 	unsigned long irqflags;
2694b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
269540da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2696f796cf8fSJesse Barnes 
2697f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2698b518421fSPaulo Zanoni 	ironlake_disable_display_irq(dev_priv, bit);
2699b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2700b1f14ad0SJesse Barnes }
2701b1f14ad0SJesse Barnes 
27027e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
27037e231dbeSJesse Barnes {
27042d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
27057e231dbeSJesse Barnes 	unsigned long irqflags;
27067e231dbeSJesse Barnes 
27077e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
270831acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
2709755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
27107e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
27117e231dbeSJesse Barnes }
27127e231dbeSJesse Barnes 
2713abd58f01SBen Widawsky static void gen8_disable_vblank(struct drm_device *dev, int pipe)
2714abd58f01SBen Widawsky {
2715abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2716abd58f01SBen Widawsky 	unsigned long irqflags;
2717abd58f01SBen Widawsky 
2718abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
27197167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] |= GEN8_PIPE_VBLANK;
27207167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2721abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2722abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2723abd58f01SBen Widawsky }
2724abd58f01SBen Widawsky 
27259107e9d2SChris Wilson static bool
272694f7bbe1STomas Elf ring_idle(struct intel_engine_cs *ring, u32 seqno)
2727893eead0SChris Wilson {
27289107e9d2SChris Wilson 	return (list_empty(&ring->request_list) ||
272994f7bbe1STomas Elf 		i915_seqno_passed(seqno, ring->last_submitted_seqno));
2730f65d9421SBen Gamari }
2731f65d9421SBen Gamari 
2732a028c4b0SDaniel Vetter static bool
2733a028c4b0SDaniel Vetter ipehr_is_semaphore_wait(struct drm_device *dev, u32 ipehr)
2734a028c4b0SDaniel Vetter {
2735a028c4b0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 8) {
2736a6cdb93aSRodrigo Vivi 		return (ipehr >> 23) == 0x1c;
2737a028c4b0SDaniel Vetter 	} else {
2738a028c4b0SDaniel Vetter 		ipehr &= ~MI_SEMAPHORE_SYNC_MASK;
2739a028c4b0SDaniel Vetter 		return ipehr == (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE |
2740a028c4b0SDaniel Vetter 				 MI_SEMAPHORE_REGISTER);
2741a028c4b0SDaniel Vetter 	}
2742a028c4b0SDaniel Vetter }
2743a028c4b0SDaniel Vetter 
2744a4872ba6SOscar Mateo static struct intel_engine_cs *
2745a6cdb93aSRodrigo Vivi semaphore_wait_to_signaller_ring(struct intel_engine_cs *ring, u32 ipehr, u64 offset)
2746921d42eaSDaniel Vetter {
2747921d42eaSDaniel Vetter 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2748a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2749921d42eaSDaniel Vetter 	int i;
2750921d42eaSDaniel Vetter 
2751921d42eaSDaniel Vetter 	if (INTEL_INFO(dev_priv->dev)->gen >= 8) {
2752a6cdb93aSRodrigo Vivi 		for_each_ring(signaller, dev_priv, i) {
2753a6cdb93aSRodrigo Vivi 			if (ring == signaller)
2754a6cdb93aSRodrigo Vivi 				continue;
2755a6cdb93aSRodrigo Vivi 
2756a6cdb93aSRodrigo Vivi 			if (offset == signaller->semaphore.signal_ggtt[ring->id])
2757a6cdb93aSRodrigo Vivi 				return signaller;
2758a6cdb93aSRodrigo Vivi 		}
2759921d42eaSDaniel Vetter 	} else {
2760921d42eaSDaniel Vetter 		u32 sync_bits = ipehr & MI_SEMAPHORE_SYNC_MASK;
2761921d42eaSDaniel Vetter 
2762921d42eaSDaniel Vetter 		for_each_ring(signaller, dev_priv, i) {
2763921d42eaSDaniel Vetter 			if(ring == signaller)
2764921d42eaSDaniel Vetter 				continue;
2765921d42eaSDaniel Vetter 
2766ebc348b2SBen Widawsky 			if (sync_bits == signaller->semaphore.mbox.wait[ring->id])
2767921d42eaSDaniel Vetter 				return signaller;
2768921d42eaSDaniel Vetter 		}
2769921d42eaSDaniel Vetter 	}
2770921d42eaSDaniel Vetter 
2771a6cdb93aSRodrigo Vivi 	DRM_ERROR("No signaller ring found for ring %i, ipehr 0x%08x, offset 0x%016llx\n",
2772a6cdb93aSRodrigo Vivi 		  ring->id, ipehr, offset);
2773921d42eaSDaniel Vetter 
2774921d42eaSDaniel Vetter 	return NULL;
2775921d42eaSDaniel Vetter }
2776921d42eaSDaniel Vetter 
2777a4872ba6SOscar Mateo static struct intel_engine_cs *
2778a4872ba6SOscar Mateo semaphore_waits_for(struct intel_engine_cs *ring, u32 *seqno)
2779a24a11e6SChris Wilson {
2780a24a11e6SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
278188fe429dSDaniel Vetter 	u32 cmd, ipehr, head;
2782a6cdb93aSRodrigo Vivi 	u64 offset = 0;
2783a6cdb93aSRodrigo Vivi 	int i, backwards;
2784a24a11e6SChris Wilson 
2785a24a11e6SChris Wilson 	ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
2786a028c4b0SDaniel Vetter 	if (!ipehr_is_semaphore_wait(ring->dev, ipehr))
27876274f212SChris Wilson 		return NULL;
2788a24a11e6SChris Wilson 
278988fe429dSDaniel Vetter 	/*
279088fe429dSDaniel Vetter 	 * HEAD is likely pointing to the dword after the actual command,
279188fe429dSDaniel Vetter 	 * so scan backwards until we find the MBOX. But limit it to just 3
2792a6cdb93aSRodrigo Vivi 	 * or 4 dwords depending on the semaphore wait command size.
2793a6cdb93aSRodrigo Vivi 	 * Note that we don't care about ACTHD here since that might
279488fe429dSDaniel Vetter 	 * point at at batch, and semaphores are always emitted into the
279588fe429dSDaniel Vetter 	 * ringbuffer itself.
2796a24a11e6SChris Wilson 	 */
279788fe429dSDaniel Vetter 	head = I915_READ_HEAD(ring) & HEAD_ADDR;
2798a6cdb93aSRodrigo Vivi 	backwards = (INTEL_INFO(ring->dev)->gen >= 8) ? 5 : 4;
279988fe429dSDaniel Vetter 
2800a6cdb93aSRodrigo Vivi 	for (i = backwards; i; --i) {
280188fe429dSDaniel Vetter 		/*
280288fe429dSDaniel Vetter 		 * Be paranoid and presume the hw has gone off into the wild -
280388fe429dSDaniel Vetter 		 * our ring is smaller than what the hardware (and hence
280488fe429dSDaniel Vetter 		 * HEAD_ADDR) allows. Also handles wrap-around.
280588fe429dSDaniel Vetter 		 */
2806ee1b1e5eSOscar Mateo 		head &= ring->buffer->size - 1;
280788fe429dSDaniel Vetter 
280888fe429dSDaniel Vetter 		/* This here seems to blow up */
2809ee1b1e5eSOscar Mateo 		cmd = ioread32(ring->buffer->virtual_start + head);
2810a24a11e6SChris Wilson 		if (cmd == ipehr)
2811a24a11e6SChris Wilson 			break;
2812a24a11e6SChris Wilson 
281388fe429dSDaniel Vetter 		head -= 4;
281488fe429dSDaniel Vetter 	}
2815a24a11e6SChris Wilson 
281688fe429dSDaniel Vetter 	if (!i)
281788fe429dSDaniel Vetter 		return NULL;
281888fe429dSDaniel Vetter 
2819ee1b1e5eSOscar Mateo 	*seqno = ioread32(ring->buffer->virtual_start + head + 4) + 1;
2820a6cdb93aSRodrigo Vivi 	if (INTEL_INFO(ring->dev)->gen >= 8) {
2821a6cdb93aSRodrigo Vivi 		offset = ioread32(ring->buffer->virtual_start + head + 12);
2822a6cdb93aSRodrigo Vivi 		offset <<= 32;
2823a6cdb93aSRodrigo Vivi 		offset = ioread32(ring->buffer->virtual_start + head + 8);
2824a6cdb93aSRodrigo Vivi 	}
2825a6cdb93aSRodrigo Vivi 	return semaphore_wait_to_signaller_ring(ring, ipehr, offset);
2826a24a11e6SChris Wilson }
2827a24a11e6SChris Wilson 
2828a4872ba6SOscar Mateo static int semaphore_passed(struct intel_engine_cs *ring)
28296274f212SChris Wilson {
28306274f212SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2831a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2832a0d036b0SChris Wilson 	u32 seqno;
28336274f212SChris Wilson 
28344be17381SChris Wilson 	ring->hangcheck.deadlock++;
28356274f212SChris Wilson 
28366274f212SChris Wilson 	signaller = semaphore_waits_for(ring, &seqno);
28374be17381SChris Wilson 	if (signaller == NULL)
28384be17381SChris Wilson 		return -1;
28394be17381SChris Wilson 
28404be17381SChris Wilson 	/* Prevent pathological recursion due to driver bugs */
28414be17381SChris Wilson 	if (signaller->hangcheck.deadlock >= I915_NUM_RINGS)
28426274f212SChris Wilson 		return -1;
28436274f212SChris Wilson 
28444be17381SChris Wilson 	if (i915_seqno_passed(signaller->get_seqno(signaller, false), seqno))
28454be17381SChris Wilson 		return 1;
28464be17381SChris Wilson 
2847a0d036b0SChris Wilson 	/* cursory check for an unkickable deadlock */
2848a0d036b0SChris Wilson 	if (I915_READ_CTL(signaller) & RING_WAIT_SEMAPHORE &&
2849a0d036b0SChris Wilson 	    semaphore_passed(signaller) < 0)
28504be17381SChris Wilson 		return -1;
28514be17381SChris Wilson 
28524be17381SChris Wilson 	return 0;
28536274f212SChris Wilson }
28546274f212SChris Wilson 
28556274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
28566274f212SChris Wilson {
2857a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
28586274f212SChris Wilson 	int i;
28596274f212SChris Wilson 
28606274f212SChris Wilson 	for_each_ring(ring, dev_priv, i)
28614be17381SChris Wilson 		ring->hangcheck.deadlock = 0;
28626274f212SChris Wilson }
28636274f212SChris Wilson 
2864ad8beaeaSMika Kuoppala static enum intel_ring_hangcheck_action
2865a4872ba6SOscar Mateo ring_stuck(struct intel_engine_cs *ring, u64 acthd)
28661ec14ad3SChris Wilson {
28671ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
28681ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
28699107e9d2SChris Wilson 	u32 tmp;
28709107e9d2SChris Wilson 
2871f260fe7bSMika Kuoppala 	if (acthd != ring->hangcheck.acthd) {
2872f260fe7bSMika Kuoppala 		if (acthd > ring->hangcheck.max_acthd) {
2873f260fe7bSMika Kuoppala 			ring->hangcheck.max_acthd = acthd;
2874f2f4d82fSJani Nikula 			return HANGCHECK_ACTIVE;
2875f260fe7bSMika Kuoppala 		}
2876f260fe7bSMika Kuoppala 
2877f260fe7bSMika Kuoppala 		return HANGCHECK_ACTIVE_LOOP;
2878f260fe7bSMika Kuoppala 	}
28796274f212SChris Wilson 
28809107e9d2SChris Wilson 	if (IS_GEN2(dev))
2881f2f4d82fSJani Nikula 		return HANGCHECK_HUNG;
28829107e9d2SChris Wilson 
28839107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
28849107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
28859107e9d2SChris Wilson 	 * and break the hang. This should work on
28869107e9d2SChris Wilson 	 * all but the second generation chipsets.
28879107e9d2SChris Wilson 	 */
28889107e9d2SChris Wilson 	tmp = I915_READ_CTL(ring);
28891ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
289058174462SMika Kuoppala 		i915_handle_error(dev, false,
289158174462SMika Kuoppala 				  "Kicking stuck wait on %s",
28921ec14ad3SChris Wilson 				  ring->name);
28931ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
2894f2f4d82fSJani Nikula 		return HANGCHECK_KICK;
28951ec14ad3SChris Wilson 	}
2896a24a11e6SChris Wilson 
28976274f212SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
28986274f212SChris Wilson 		switch (semaphore_passed(ring)) {
28996274f212SChris Wilson 		default:
2900f2f4d82fSJani Nikula 			return HANGCHECK_HUNG;
29016274f212SChris Wilson 		case 1:
290258174462SMika Kuoppala 			i915_handle_error(dev, false,
290358174462SMika Kuoppala 					  "Kicking stuck semaphore on %s",
2904a24a11e6SChris Wilson 					  ring->name);
2905a24a11e6SChris Wilson 			I915_WRITE_CTL(ring, tmp);
2906f2f4d82fSJani Nikula 			return HANGCHECK_KICK;
29076274f212SChris Wilson 		case 0:
2908f2f4d82fSJani Nikula 			return HANGCHECK_WAIT;
29096274f212SChris Wilson 		}
29109107e9d2SChris Wilson 	}
29119107e9d2SChris Wilson 
2912f2f4d82fSJani Nikula 	return HANGCHECK_HUNG;
2913a24a11e6SChris Wilson }
2914d1e61e7fSChris Wilson 
2915737b1506SChris Wilson /*
2916f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
291705407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
291805407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
291905407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
292005407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
292105407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
2922f65d9421SBen Gamari  */
2923737b1506SChris Wilson static void i915_hangcheck_elapsed(struct work_struct *work)
2924f65d9421SBen Gamari {
2925737b1506SChris Wilson 	struct drm_i915_private *dev_priv =
2926737b1506SChris Wilson 		container_of(work, typeof(*dev_priv),
2927737b1506SChris Wilson 			     gpu_error.hangcheck_work.work);
2928737b1506SChris Wilson 	struct drm_device *dev = dev_priv->dev;
2929a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
2930b4519513SChris Wilson 	int i;
293105407ff8SMika Kuoppala 	int busy_count = 0, rings_hung = 0;
29329107e9d2SChris Wilson 	bool stuck[I915_NUM_RINGS] = { 0 };
29339107e9d2SChris Wilson #define BUSY 1
29349107e9d2SChris Wilson #define KICK 5
29359107e9d2SChris Wilson #define HUNG 20
2936893eead0SChris Wilson 
2937d330a953SJani Nikula 	if (!i915.enable_hangcheck)
29383e0dc6b0SBen Widawsky 		return;
29393e0dc6b0SBen Widawsky 
2940b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
294150877445SChris Wilson 		u64 acthd;
294250877445SChris Wilson 		u32 seqno;
29439107e9d2SChris Wilson 		bool busy = true;
2944b4519513SChris Wilson 
29456274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
29466274f212SChris Wilson 
294705407ff8SMika Kuoppala 		seqno = ring->get_seqno(ring, false);
294805407ff8SMika Kuoppala 		acthd = intel_ring_get_active_head(ring);
294905407ff8SMika Kuoppala 
295005407ff8SMika Kuoppala 		if (ring->hangcheck.seqno == seqno) {
295194f7bbe1STomas Elf 			if (ring_idle(ring, seqno)) {
2952da661464SMika Kuoppala 				ring->hangcheck.action = HANGCHECK_IDLE;
2953da661464SMika Kuoppala 
29549107e9d2SChris Wilson 				if (waitqueue_active(&ring->irq_queue)) {
29559107e9d2SChris Wilson 					/* Issue a wake-up to catch stuck h/w. */
2956094f9a54SChris Wilson 					if (!test_and_set_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings)) {
2957f4adcd24SDaniel Vetter 						if (!(dev_priv->gpu_error.test_irq_rings & intel_ring_flag(ring)))
29589107e9d2SChris Wilson 							DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
29599107e9d2SChris Wilson 								  ring->name);
2960f4adcd24SDaniel Vetter 						else
2961f4adcd24SDaniel Vetter 							DRM_INFO("Fake missed irq on %s\n",
2962f4adcd24SDaniel Vetter 								 ring->name);
29639107e9d2SChris Wilson 						wake_up_all(&ring->irq_queue);
2964094f9a54SChris Wilson 					}
2965094f9a54SChris Wilson 					/* Safeguard against driver failure */
2966094f9a54SChris Wilson 					ring->hangcheck.score += BUSY;
29679107e9d2SChris Wilson 				} else
29689107e9d2SChris Wilson 					busy = false;
296905407ff8SMika Kuoppala 			} else {
29706274f212SChris Wilson 				/* We always increment the hangcheck score
29716274f212SChris Wilson 				 * if the ring is busy and still processing
29726274f212SChris Wilson 				 * the same request, so that no single request
29736274f212SChris Wilson 				 * can run indefinitely (such as a chain of
29746274f212SChris Wilson 				 * batches). The only time we do not increment
29756274f212SChris Wilson 				 * the hangcheck score on this ring, if this
29766274f212SChris Wilson 				 * ring is in a legitimate wait for another
29776274f212SChris Wilson 				 * ring. In that case the waiting ring is a
29786274f212SChris Wilson 				 * victim and we want to be sure we catch the
29796274f212SChris Wilson 				 * right culprit. Then every time we do kick
29806274f212SChris Wilson 				 * the ring, add a small increment to the
29816274f212SChris Wilson 				 * score so that we can catch a batch that is
29826274f212SChris Wilson 				 * being repeatedly kicked and so responsible
29836274f212SChris Wilson 				 * for stalling the machine.
29849107e9d2SChris Wilson 				 */
2985ad8beaeaSMika Kuoppala 				ring->hangcheck.action = ring_stuck(ring,
2986ad8beaeaSMika Kuoppala 								    acthd);
2987ad8beaeaSMika Kuoppala 
2988ad8beaeaSMika Kuoppala 				switch (ring->hangcheck.action) {
2989da661464SMika Kuoppala 				case HANGCHECK_IDLE:
2990f2f4d82fSJani Nikula 				case HANGCHECK_WAIT:
2991f2f4d82fSJani Nikula 				case HANGCHECK_ACTIVE:
2992f260fe7bSMika Kuoppala 					break;
2993f260fe7bSMika Kuoppala 				case HANGCHECK_ACTIVE_LOOP:
2994ea04cb31SJani Nikula 					ring->hangcheck.score += BUSY;
29956274f212SChris Wilson 					break;
2996f2f4d82fSJani Nikula 				case HANGCHECK_KICK:
2997ea04cb31SJani Nikula 					ring->hangcheck.score += KICK;
29986274f212SChris Wilson 					break;
2999f2f4d82fSJani Nikula 				case HANGCHECK_HUNG:
3000ea04cb31SJani Nikula 					ring->hangcheck.score += HUNG;
30016274f212SChris Wilson 					stuck[i] = true;
30026274f212SChris Wilson 					break;
30036274f212SChris Wilson 				}
300405407ff8SMika Kuoppala 			}
30059107e9d2SChris Wilson 		} else {
3006da661464SMika Kuoppala 			ring->hangcheck.action = HANGCHECK_ACTIVE;
3007da661464SMika Kuoppala 
30089107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
30099107e9d2SChris Wilson 			 * attempts across multiple batches.
30109107e9d2SChris Wilson 			 */
30119107e9d2SChris Wilson 			if (ring->hangcheck.score > 0)
30129107e9d2SChris Wilson 				ring->hangcheck.score--;
3013f260fe7bSMika Kuoppala 
3014f260fe7bSMika Kuoppala 			ring->hangcheck.acthd = ring->hangcheck.max_acthd = 0;
3015cbb465e7SChris Wilson 		}
3016f65d9421SBen Gamari 
301705407ff8SMika Kuoppala 		ring->hangcheck.seqno = seqno;
301805407ff8SMika Kuoppala 		ring->hangcheck.acthd = acthd;
30199107e9d2SChris Wilson 		busy_count += busy;
302005407ff8SMika Kuoppala 	}
302105407ff8SMika Kuoppala 
302205407ff8SMika Kuoppala 	for_each_ring(ring, dev_priv, i) {
3023b6b0fac0SMika Kuoppala 		if (ring->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG) {
3024b8d88d1dSDaniel Vetter 			DRM_INFO("%s on %s\n",
302505407ff8SMika Kuoppala 				 stuck[i] ? "stuck" : "no progress",
3026a43adf07SChris Wilson 				 ring->name);
3027a43adf07SChris Wilson 			rings_hung++;
302805407ff8SMika Kuoppala 		}
302905407ff8SMika Kuoppala 	}
303005407ff8SMika Kuoppala 
303105407ff8SMika Kuoppala 	if (rings_hung)
303258174462SMika Kuoppala 		return i915_handle_error(dev, true, "Ring hung");
303305407ff8SMika Kuoppala 
303405407ff8SMika Kuoppala 	if (busy_count)
303505407ff8SMika Kuoppala 		/* Reset timer case chip hangs without another request
303605407ff8SMika Kuoppala 		 * being added */
303710cd45b6SMika Kuoppala 		i915_queue_hangcheck(dev);
303810cd45b6SMika Kuoppala }
303910cd45b6SMika Kuoppala 
304010cd45b6SMika Kuoppala void i915_queue_hangcheck(struct drm_device *dev)
304110cd45b6SMika Kuoppala {
3042737b1506SChris Wilson 	struct i915_gpu_error *e = &to_i915(dev)->gpu_error;
3043672e7b7cSChris Wilson 
3044d330a953SJani Nikula 	if (!i915.enable_hangcheck)
304510cd45b6SMika Kuoppala 		return;
304610cd45b6SMika Kuoppala 
3047737b1506SChris Wilson 	/* Don't continually defer the hangcheck so that it is always run at
3048737b1506SChris Wilson 	 * least once after work has been scheduled on any ring. Otherwise,
3049737b1506SChris Wilson 	 * we will ignore a hung ring if a second ring is kept busy.
3050737b1506SChris Wilson 	 */
3051737b1506SChris Wilson 
3052737b1506SChris Wilson 	queue_delayed_work(e->hangcheck_wq, &e->hangcheck_work,
3053737b1506SChris Wilson 			   round_jiffies_up_relative(DRM_I915_HANGCHECK_JIFFIES));
3054f65d9421SBen Gamari }
3055f65d9421SBen Gamari 
30561c69eb42SPaulo Zanoni static void ibx_irq_reset(struct drm_device *dev)
305791738a95SPaulo Zanoni {
305891738a95SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
305991738a95SPaulo Zanoni 
306091738a95SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
306191738a95SPaulo Zanoni 		return;
306291738a95SPaulo Zanoni 
3063f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(SDE);
3064105b122eSPaulo Zanoni 
3065105b122eSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
3066105b122eSPaulo Zanoni 		I915_WRITE(SERR_INT, 0xffffffff);
3067622364b6SPaulo Zanoni }
3068105b122eSPaulo Zanoni 
306991738a95SPaulo Zanoni /*
3070622364b6SPaulo Zanoni  * SDEIER is also touched by the interrupt handler to work around missed PCH
3071622364b6SPaulo Zanoni  * interrupts. Hence we can't update it after the interrupt handler is enabled -
3072622364b6SPaulo Zanoni  * instead we unconditionally enable all PCH interrupt sources here, but then
3073622364b6SPaulo Zanoni  * only unmask them as needed with SDEIMR.
3074622364b6SPaulo Zanoni  *
3075622364b6SPaulo Zanoni  * This function needs to be called before interrupts are enabled.
307691738a95SPaulo Zanoni  */
3077622364b6SPaulo Zanoni static void ibx_irq_pre_postinstall(struct drm_device *dev)
3078622364b6SPaulo Zanoni {
3079622364b6SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3080622364b6SPaulo Zanoni 
3081622364b6SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
3082622364b6SPaulo Zanoni 		return;
3083622364b6SPaulo Zanoni 
3084622364b6SPaulo Zanoni 	WARN_ON(I915_READ(SDEIER) != 0);
308591738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
308691738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
308791738a95SPaulo Zanoni }
308891738a95SPaulo Zanoni 
30897c4d664eSPaulo Zanoni static void gen5_gt_irq_reset(struct drm_device *dev)
3090d18ea1b5SDaniel Vetter {
3091d18ea1b5SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
3092d18ea1b5SDaniel Vetter 
3093f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GT);
3094a9d356a6SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6)
3095f86f3fb0SPaulo Zanoni 		GEN5_IRQ_RESET(GEN6_PM);
3096d18ea1b5SDaniel Vetter }
3097d18ea1b5SDaniel Vetter 
3098c0e09200SDave Airlie /* drm_dma.h hooks
3099c0e09200SDave Airlie */
3100be30b29fSPaulo Zanoni static void ironlake_irq_reset(struct drm_device *dev)
3101036a4a7dSZhenyu Wang {
31022d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3103036a4a7dSZhenyu Wang 
31040c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xffffffff);
3105bdfcdb63SDaniel Vetter 
3106f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(DE);
3107c6d954c1SPaulo Zanoni 	if (IS_GEN7(dev))
3108c6d954c1SPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, 0xffffffff);
3109036a4a7dSZhenyu Wang 
31107c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
3111c650156aSZhenyu Wang 
31121c69eb42SPaulo Zanoni 	ibx_irq_reset(dev);
31137d99163dSBen Widawsky }
31147d99163dSBen Widawsky 
311570591a41SVille Syrjälä static void vlv_display_irq_reset(struct drm_i915_private *dev_priv)
311670591a41SVille Syrjälä {
311770591a41SVille Syrjälä 	enum pipe pipe;
311870591a41SVille Syrjälä 
31190706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xFFFFFFFF, 0);
312070591a41SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
312170591a41SVille Syrjälä 
312270591a41SVille Syrjälä 	for_each_pipe(dev_priv, pipe)
312370591a41SVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), 0xffff);
312470591a41SVille Syrjälä 
312570591a41SVille Syrjälä 	GEN5_IRQ_RESET(VLV_);
312670591a41SVille Syrjälä }
312770591a41SVille Syrjälä 
31287e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
31297e231dbeSJesse Barnes {
31302d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
31317e231dbeSJesse Barnes 
31327e231dbeSJesse Barnes 	/* VLV magic */
31337e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
31347e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
31357e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
31367e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
31377e231dbeSJesse Barnes 
31387c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
31397e231dbeSJesse Barnes 
31407c4cde39SVille Syrjälä 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
31417e231dbeSJesse Barnes 
314270591a41SVille Syrjälä 	vlv_display_irq_reset(dev_priv);
31437e231dbeSJesse Barnes }
31447e231dbeSJesse Barnes 
3145d6e3cca3SDaniel Vetter static void gen8_gt_irq_reset(struct drm_i915_private *dev_priv)
3146d6e3cca3SDaniel Vetter {
3147d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 0);
3148d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 1);
3149d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 2);
3150d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 3);
3151d6e3cca3SDaniel Vetter }
3152d6e3cca3SDaniel Vetter 
3153823f6b38SPaulo Zanoni static void gen8_irq_reset(struct drm_device *dev)
3154abd58f01SBen Widawsky {
3155abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3156abd58f01SBen Widawsky 	int pipe;
3157abd58f01SBen Widawsky 
3158abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
3159abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3160abd58f01SBen Widawsky 
3161d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
3162abd58f01SBen Widawsky 
3163055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3164f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3165813bde43SPaulo Zanoni 						   POWER_DOMAIN_PIPE(pipe)))
3166f86f3fb0SPaulo Zanoni 			GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3167abd58f01SBen Widawsky 
3168f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_PORT_);
3169f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_MISC_);
3170f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_PCU_);
3171abd58f01SBen Widawsky 
3172266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev))
31731c69eb42SPaulo Zanoni 		ibx_irq_reset(dev);
3174abd58f01SBen Widawsky }
3175abd58f01SBen Widawsky 
31764c6c03beSDamien Lespiau void gen8_irq_power_well_post_enable(struct drm_i915_private *dev_priv,
31774c6c03beSDamien Lespiau 				     unsigned int pipe_mask)
3178d49bdb0eSPaulo Zanoni {
31791180e206SPaulo Zanoni 	uint32_t extra_ier = GEN8_PIPE_VBLANK | GEN8_PIPE_FIFO_UNDERRUN;
3180d49bdb0eSPaulo Zanoni 
318113321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3182d14c0343SDamien Lespiau 	if (pipe_mask & 1 << PIPE_A)
3183d14c0343SDamien Lespiau 		GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_A,
3184d14c0343SDamien Lespiau 				  dev_priv->de_irq_mask[PIPE_A],
3185d14c0343SDamien Lespiau 				  ~dev_priv->de_irq_mask[PIPE_A] | extra_ier);
31864c6c03beSDamien Lespiau 	if (pipe_mask & 1 << PIPE_B)
31874c6c03beSDamien Lespiau 		GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_B,
31884c6c03beSDamien Lespiau 				  dev_priv->de_irq_mask[PIPE_B],
31891180e206SPaulo Zanoni 				  ~dev_priv->de_irq_mask[PIPE_B] | extra_ier);
31904c6c03beSDamien Lespiau 	if (pipe_mask & 1 << PIPE_C)
31914c6c03beSDamien Lespiau 		GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_C,
31924c6c03beSDamien Lespiau 				  dev_priv->de_irq_mask[PIPE_C],
31931180e206SPaulo Zanoni 				  ~dev_priv->de_irq_mask[PIPE_C] | extra_ier);
319413321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3195d49bdb0eSPaulo Zanoni }
3196d49bdb0eSPaulo Zanoni 
319743f328d7SVille Syrjälä static void cherryview_irq_preinstall(struct drm_device *dev)
319843f328d7SVille Syrjälä {
319943f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
320043f328d7SVille Syrjälä 
320143f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
320243f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
320343f328d7SVille Syrjälä 
3204d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
320543f328d7SVille Syrjälä 
320643f328d7SVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
320743f328d7SVille Syrjälä 
320843f328d7SVille Syrjälä 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
320943f328d7SVille Syrjälä 
321070591a41SVille Syrjälä 	vlv_display_irq_reset(dev_priv);
321143f328d7SVille Syrjälä }
321243f328d7SVille Syrjälä 
321387a02106SVille Syrjälä static u32 intel_hpd_enabled_irqs(struct drm_device *dev,
321487a02106SVille Syrjälä 				  const u32 hpd[HPD_NUM_PINS])
321587a02106SVille Syrjälä {
321687a02106SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(dev);
321787a02106SVille Syrjälä 	struct intel_encoder *encoder;
321887a02106SVille Syrjälä 	u32 enabled_irqs = 0;
321987a02106SVille Syrjälä 
322087a02106SVille Syrjälä 	for_each_intel_encoder(dev, encoder)
322187a02106SVille Syrjälä 		if (dev_priv->hotplug.stats[encoder->hpd_pin].state == HPD_ENABLED)
322287a02106SVille Syrjälä 			enabled_irqs |= hpd[encoder->hpd_pin];
322387a02106SVille Syrjälä 
322487a02106SVille Syrjälä 	return enabled_irqs;
322587a02106SVille Syrjälä }
322687a02106SVille Syrjälä 
322782a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
322882a28bcfSDaniel Vetter {
32292d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
323087a02106SVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
323182a28bcfSDaniel Vetter 
323282a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
3233fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
323487a02106SVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_ibx);
323582a28bcfSDaniel Vetter 	} else {
3236fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
323787a02106SVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_cpt);
323882a28bcfSDaniel Vetter 	}
323982a28bcfSDaniel Vetter 
3240fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
324182a28bcfSDaniel Vetter 
32427fe0b973SKeith Packard 	/*
32437fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
32446dbf30ceSVille Syrjälä 	 * duration to 2ms (which is the minimum in the Display Port spec).
32456dbf30ceSVille Syrjälä 	 * The pulse duration bits are reserved on LPT+.
32467fe0b973SKeith Packard 	 */
32477fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
32487fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
32497fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
32507fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
32517fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
32520b2eb33eSVille Syrjälä 	/*
32530b2eb33eSVille Syrjälä 	 * When CPU and PCH are on the same package, port A
32540b2eb33eSVille Syrjälä 	 * HPD must be enabled in both north and south.
32550b2eb33eSVille Syrjälä 	 */
32560b2eb33eSVille Syrjälä 	if (HAS_PCH_LPT_LP(dev))
32570b2eb33eSVille Syrjälä 		hotplug |= PORTA_HOTPLUG_ENABLE;
32587fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
32596dbf30ceSVille Syrjälä }
326026951cafSXiong Zhang 
32616dbf30ceSVille Syrjälä static void spt_hpd_irq_setup(struct drm_device *dev)
32626dbf30ceSVille Syrjälä {
32636dbf30ceSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
32646dbf30ceSVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
32656dbf30ceSVille Syrjälä 
32666dbf30ceSVille Syrjälä 	hotplug_irqs = SDE_HOTPLUG_MASK_SPT;
32676dbf30ceSVille Syrjälä 	enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_spt);
32686dbf30ceSVille Syrjälä 
32696dbf30ceSVille Syrjälä 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
32706dbf30ceSVille Syrjälä 
32716dbf30ceSVille Syrjälä 	/* Enable digital hotplug on the PCH */
32726dbf30ceSVille Syrjälä 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
32736dbf30ceSVille Syrjälä 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTC_HOTPLUG_ENABLE |
327474c0b395SVille Syrjälä 		PORTB_HOTPLUG_ENABLE | PORTA_HOTPLUG_ENABLE;
32756dbf30ceSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
32766dbf30ceSVille Syrjälä 
327726951cafSXiong Zhang 	hotplug = I915_READ(PCH_PORT_HOTPLUG2);
327826951cafSXiong Zhang 	hotplug |= PORTE_HOTPLUG_ENABLE;
327926951cafSXiong Zhang 	I915_WRITE(PCH_PORT_HOTPLUG2, hotplug);
328026951cafSXiong Zhang }
32817fe0b973SKeith Packard 
3282e4ce95aaSVille Syrjälä static void ilk_hpd_irq_setup(struct drm_device *dev)
3283e4ce95aaSVille Syrjälä {
3284e4ce95aaSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
3285e4ce95aaSVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
3286e4ce95aaSVille Syrjälä 
32873a3b3c7dSVille Syrjälä 	if (INTEL_INFO(dev)->gen >= 8) {
32883a3b3c7dSVille Syrjälä 		hotplug_irqs = GEN8_PORT_DP_A_HOTPLUG;
32893a3b3c7dSVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_bdw);
32903a3b3c7dSVille Syrjälä 
32913a3b3c7dSVille Syrjälä 		bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
32923a3b3c7dSVille Syrjälä 	} else if (INTEL_INFO(dev)->gen >= 7) {
329323bb4cb5SVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG_IVB;
329423bb4cb5SVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_ivb);
32953a3b3c7dSVille Syrjälä 
32963a3b3c7dSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
329723bb4cb5SVille Syrjälä 	} else {
3298e4ce95aaSVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG;
3299e4ce95aaSVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_ilk);
3300e4ce95aaSVille Syrjälä 
3301e4ce95aaSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
33023a3b3c7dSVille Syrjälä 	}
3303e4ce95aaSVille Syrjälä 
3304e4ce95aaSVille Syrjälä 	/*
3305e4ce95aaSVille Syrjälä 	 * Enable digital hotplug on the CPU, and configure the DP short pulse
3306e4ce95aaSVille Syrjälä 	 * duration to 2ms (which is the minimum in the Display Port spec)
330723bb4cb5SVille Syrjälä 	 * The pulse duration bits are reserved on HSW+.
3308e4ce95aaSVille Syrjälä 	 */
3309e4ce95aaSVille Syrjälä 	hotplug = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
3310e4ce95aaSVille Syrjälä 	hotplug &= ~DIGITAL_PORTA_PULSE_DURATION_MASK;
3311e4ce95aaSVille Syrjälä 	hotplug |= DIGITAL_PORTA_HOTPLUG_ENABLE | DIGITAL_PORTA_PULSE_DURATION_2ms;
3312e4ce95aaSVille Syrjälä 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, hotplug);
3313e4ce95aaSVille Syrjälä 
3314e4ce95aaSVille Syrjälä 	ibx_hpd_irq_setup(dev);
3315e4ce95aaSVille Syrjälä }
3316e4ce95aaSVille Syrjälä 
3317e0a20ad7SShashank Sharma static void bxt_hpd_irq_setup(struct drm_device *dev)
3318e0a20ad7SShashank Sharma {
3319e0a20ad7SShashank Sharma 	struct drm_i915_private *dev_priv = dev->dev_private;
3320a52bb15bSVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
3321e0a20ad7SShashank Sharma 
3322a52bb15bSVille Syrjälä 	enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_bxt);
3323a52bb15bSVille Syrjälä 	hotplug_irqs = BXT_DE_PORT_HOTPLUG_MASK;
3324e0a20ad7SShashank Sharma 
3325a52bb15bSVille Syrjälä 	bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
3326e0a20ad7SShashank Sharma 
3327a52bb15bSVille Syrjälä 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
3328a52bb15bSVille Syrjälä 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTB_HOTPLUG_ENABLE |
3329a52bb15bSVille Syrjälä 		PORTA_HOTPLUG_ENABLE;
3330a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
3331e0a20ad7SShashank Sharma }
3332e0a20ad7SShashank Sharma 
3333d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
3334d46da437SPaulo Zanoni {
33352d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
333682a28bcfSDaniel Vetter 	u32 mask;
3337d46da437SPaulo Zanoni 
3338692a04cfSDaniel Vetter 	if (HAS_PCH_NOP(dev))
3339692a04cfSDaniel Vetter 		return;
3340692a04cfSDaniel Vetter 
3341105b122eSPaulo Zanoni 	if (HAS_PCH_IBX(dev))
33425c673b60SDaniel Vetter 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
3343105b122eSPaulo Zanoni 	else
33445c673b60SDaniel Vetter 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
33458664281bSPaulo Zanoni 
3346337ba017SPaulo Zanoni 	GEN5_ASSERT_IIR_IS_ZERO(SDEIIR);
3347d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
3348d46da437SPaulo Zanoni }
3349d46da437SPaulo Zanoni 
33500a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
33510a9a8c91SDaniel Vetter {
33520a9a8c91SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
33530a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
33540a9a8c91SDaniel Vetter 
33550a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
33560a9a8c91SDaniel Vetter 
33570a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
3358040d2baaSBen Widawsky 	if (HAS_L3_DPF(dev)) {
33590a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
336035a85ac6SBen Widawsky 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev);
336135a85ac6SBen Widawsky 		gt_irqs |= GT_PARITY_ERROR(dev);
33620a9a8c91SDaniel Vetter 	}
33630a9a8c91SDaniel Vetter 
33640a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
33650a9a8c91SDaniel Vetter 	if (IS_GEN5(dev)) {
33660a9a8c91SDaniel Vetter 		gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
33670a9a8c91SDaniel Vetter 			   ILK_BSD_USER_INTERRUPT;
33680a9a8c91SDaniel Vetter 	} else {
33690a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
33700a9a8c91SDaniel Vetter 	}
33710a9a8c91SDaniel Vetter 
337235079899SPaulo Zanoni 	GEN5_IRQ_INIT(GT, dev_priv->gt_irq_mask, gt_irqs);
33730a9a8c91SDaniel Vetter 
33740a9a8c91SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
337578e68d36SImre Deak 		/*
337678e68d36SImre Deak 		 * RPS interrupts will get enabled/disabled on demand when RPS
337778e68d36SImre Deak 		 * itself is enabled/disabled.
337878e68d36SImre Deak 		 */
33790a9a8c91SDaniel Vetter 		if (HAS_VEBOX(dev))
33800a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
33810a9a8c91SDaniel Vetter 
3382605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = 0xffffffff;
338335079899SPaulo Zanoni 		GEN5_IRQ_INIT(GEN6_PM, dev_priv->pm_irq_mask, pm_irqs);
33840a9a8c91SDaniel Vetter 	}
33850a9a8c91SDaniel Vetter }
33860a9a8c91SDaniel Vetter 
3387f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
3388036a4a7dSZhenyu Wang {
33892d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
33908e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
33918e76f8dcSPaulo Zanoni 
33928e76f8dcSPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 7) {
33938e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
33948e76f8dcSPaulo Zanoni 				DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
33958e76f8dcSPaulo Zanoni 				DE_PLANEB_FLIP_DONE_IVB |
33965c673b60SDaniel Vetter 				DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB);
33978e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
339823bb4cb5SVille Syrjälä 			      DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB |
339923bb4cb5SVille Syrjälä 			      DE_DP_A_HOTPLUG_IVB);
34008e76f8dcSPaulo Zanoni 	} else {
34018e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3402ce99c256SDaniel Vetter 				DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
34035b3a856bSDaniel Vetter 				DE_AUX_CHANNEL_A |
34045b3a856bSDaniel Vetter 				DE_PIPEB_CRC_DONE | DE_PIPEA_CRC_DONE |
34055b3a856bSDaniel Vetter 				DE_POISON);
3406e4ce95aaSVille Syrjälä 		extra_mask = (DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
3407e4ce95aaSVille Syrjälä 			      DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN |
3408e4ce95aaSVille Syrjälä 			      DE_DP_A_HOTPLUG);
34098e76f8dcSPaulo Zanoni 	}
3410036a4a7dSZhenyu Wang 
34111ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3412036a4a7dSZhenyu Wang 
34130c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xeffe);
34140c841212SPaulo Zanoni 
3415622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3416622364b6SPaulo Zanoni 
341735079899SPaulo Zanoni 	GEN5_IRQ_INIT(DE, dev_priv->irq_mask, display_mask | extra_mask);
3418036a4a7dSZhenyu Wang 
34190a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
3420036a4a7dSZhenyu Wang 
3421d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
34227fe0b973SKeith Packard 
3423f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
34246005ce42SDaniel Vetter 		/* Enable PCU event interrupts
34256005ce42SDaniel Vetter 		 *
34266005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
34274bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
34284bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
3429d6207435SDaniel Vetter 		spin_lock_irq(&dev_priv->irq_lock);
3430f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
3431d6207435SDaniel Vetter 		spin_unlock_irq(&dev_priv->irq_lock);
3432f97108d1SJesse Barnes 	}
3433f97108d1SJesse Barnes 
3434036a4a7dSZhenyu Wang 	return 0;
3435036a4a7dSZhenyu Wang }
3436036a4a7dSZhenyu Wang 
3437f8b79e58SImre Deak static void valleyview_display_irqs_install(struct drm_i915_private *dev_priv)
3438f8b79e58SImre Deak {
3439f8b79e58SImre Deak 	u32 pipestat_mask;
3440f8b79e58SImre Deak 	u32 iir_mask;
3441120dda4fSVille Syrjälä 	enum pipe pipe;
3442f8b79e58SImre Deak 
3443f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3444f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3445f8b79e58SImre Deak 
3446120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3447120dda4fSVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), pipestat_mask);
3448f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3449f8b79e58SImre Deak 
3450f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3451f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3452f8b79e58SImre Deak 
3453120dda4fSVille Syrjälä 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3454120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3455120dda4fSVille Syrjälä 		      i915_enable_pipestat(dev_priv, pipe, pipestat_mask);
3456f8b79e58SImre Deak 
3457f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3458f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3459f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3460120dda4fSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
3461120dda4fSVille Syrjälä 		iir_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
3462f8b79e58SImre Deak 	dev_priv->irq_mask &= ~iir_mask;
3463f8b79e58SImre Deak 
3464f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3465f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3466f8b79e58SImre Deak 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
346776e41860SVille Syrjälä 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
346876e41860SVille Syrjälä 	POSTING_READ(VLV_IMR);
3469f8b79e58SImre Deak }
3470f8b79e58SImre Deak 
3471f8b79e58SImre Deak static void valleyview_display_irqs_uninstall(struct drm_i915_private *dev_priv)
3472f8b79e58SImre Deak {
3473f8b79e58SImre Deak 	u32 pipestat_mask;
3474f8b79e58SImre Deak 	u32 iir_mask;
3475120dda4fSVille Syrjälä 	enum pipe pipe;
3476f8b79e58SImre Deak 
3477f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3478f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
34796c7fba04SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3480120dda4fSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
3481120dda4fSVille Syrjälä 		iir_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
3482f8b79e58SImre Deak 
3483f8b79e58SImre Deak 	dev_priv->irq_mask |= iir_mask;
3484f8b79e58SImre Deak 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
348576e41860SVille Syrjälä 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
3486f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3487f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3488f8b79e58SImre Deak 	POSTING_READ(VLV_IIR);
3489f8b79e58SImre Deak 
3490f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3491f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3492f8b79e58SImre Deak 
3493120dda4fSVille Syrjälä 	i915_disable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3494120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3495120dda4fSVille Syrjälä 		i915_disable_pipestat(dev_priv, pipe, pipestat_mask);
3496f8b79e58SImre Deak 
3497f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3498f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3499120dda4fSVille Syrjälä 
3500120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3501120dda4fSVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), pipestat_mask);
3502f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3503f8b79e58SImre Deak }
3504f8b79e58SImre Deak 
3505f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3506f8b79e58SImre Deak {
3507f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3508f8b79e58SImre Deak 
3509f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3510f8b79e58SImre Deak 		return;
3511f8b79e58SImre Deak 
3512f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3513f8b79e58SImre Deak 
3514950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3515f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3516f8b79e58SImre Deak }
3517f8b79e58SImre Deak 
3518f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3519f8b79e58SImre Deak {
3520f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3521f8b79e58SImre Deak 
3522f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3523f8b79e58SImre Deak 		return;
3524f8b79e58SImre Deak 
3525f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3526f8b79e58SImre Deak 
3527950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3528f8b79e58SImre Deak 		valleyview_display_irqs_uninstall(dev_priv);
3529f8b79e58SImre Deak }
3530f8b79e58SImre Deak 
35310e6c9a9eSVille Syrjälä static void vlv_display_irq_postinstall(struct drm_i915_private *dev_priv)
35327e231dbeSJesse Barnes {
3533f8b79e58SImre Deak 	dev_priv->irq_mask = ~0;
35347e231dbeSJesse Barnes 
35350706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
353620afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
353720afbda2SDaniel Vetter 
35387e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
353976e41860SVille Syrjälä 	I915_WRITE(VLV_IIR, 0xffffffff);
354076e41860SVille Syrjälä 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
354176e41860SVille Syrjälä 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
354276e41860SVille Syrjälä 	POSTING_READ(VLV_IMR);
35437e231dbeSJesse Barnes 
3544b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3545b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3546d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3547f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3548f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3549d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
35500e6c9a9eSVille Syrjälä }
35510e6c9a9eSVille Syrjälä 
35520e6c9a9eSVille Syrjälä static int valleyview_irq_postinstall(struct drm_device *dev)
35530e6c9a9eSVille Syrjälä {
35540e6c9a9eSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
35550e6c9a9eSVille Syrjälä 
35560e6c9a9eSVille Syrjälä 	vlv_display_irq_postinstall(dev_priv);
35577e231dbeSJesse Barnes 
35580a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
35597e231dbeSJesse Barnes 
35607e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
35617e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
35627e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
35637e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
35647e231dbeSJesse Barnes #endif
35657e231dbeSJesse Barnes 
35667e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
356720afbda2SDaniel Vetter 
356820afbda2SDaniel Vetter 	return 0;
356920afbda2SDaniel Vetter }
357020afbda2SDaniel Vetter 
3571abd58f01SBen Widawsky static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
3572abd58f01SBen Widawsky {
3573abd58f01SBen Widawsky 	/* These are interrupts we'll toggle with the ring mask register */
3574abd58f01SBen Widawsky 	uint32_t gt_interrupts[] = {
3575abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
357673d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
3577abd58f01SBen Widawsky 			GT_RENDER_L3_PARITY_ERROR_INTERRUPT |
357873d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT |
357973d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT,
3580abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
358173d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
358273d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT |
358373d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT,
3584abd58f01SBen Widawsky 		0,
358573d477f6SOscar Mateo 		GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT |
358673d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT
3587abd58f01SBen Widawsky 		};
3588abd58f01SBen Widawsky 
35890961021aSBen Widawsky 	dev_priv->pm_irq_mask = 0xffffffff;
35909a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 0, ~gt_interrupts[0], gt_interrupts[0]);
35919a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 1, ~gt_interrupts[1], gt_interrupts[1]);
359278e68d36SImre Deak 	/*
359378e68d36SImre Deak 	 * RPS interrupts will get enabled/disabled on demand when RPS itself
359478e68d36SImre Deak 	 * is enabled/disabled.
359578e68d36SImre Deak 	 */
359678e68d36SImre Deak 	GEN8_IRQ_INIT_NDX(GT, 2, dev_priv->pm_irq_mask, 0);
35979a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 3, ~gt_interrupts[3], gt_interrupts[3]);
3598abd58f01SBen Widawsky }
3599abd58f01SBen Widawsky 
3600abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3601abd58f01SBen Widawsky {
3602770de83dSDamien Lespiau 	uint32_t de_pipe_masked = GEN8_PIPE_CDCLK_CRC_DONE;
3603770de83dSDamien Lespiau 	uint32_t de_pipe_enables;
36043a3b3c7dSVille Syrjälä 	u32 de_port_masked = GEN8_AUX_CHANNEL_A;
36053a3b3c7dSVille Syrjälä 	u32 de_port_enables;
36063a3b3c7dSVille Syrjälä 	enum pipe pipe;
3607770de83dSDamien Lespiau 
3608b4834a50SRodrigo Vivi 	if (INTEL_INFO(dev_priv)->gen >= 9) {
3609770de83dSDamien Lespiau 		de_pipe_masked |= GEN9_PIPE_PLANE1_FLIP_DONE |
3610770de83dSDamien Lespiau 				  GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
36113a3b3c7dSVille Syrjälä 		de_port_masked |= GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
361288e04703SJesse Barnes 				  GEN9_AUX_CHANNEL_D;
36139e63743eSShashank Sharma 		if (IS_BROXTON(dev_priv))
36143a3b3c7dSVille Syrjälä 			de_port_masked |= BXT_DE_PORT_GMBUS;
36153a3b3c7dSVille Syrjälä 	} else {
3616770de83dSDamien Lespiau 		de_pipe_masked |= GEN8_PIPE_PRIMARY_FLIP_DONE |
3617770de83dSDamien Lespiau 				  GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
36183a3b3c7dSVille Syrjälä 	}
3619770de83dSDamien Lespiau 
3620770de83dSDamien Lespiau 	de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
3621770de83dSDamien Lespiau 					   GEN8_PIPE_FIFO_UNDERRUN;
3622770de83dSDamien Lespiau 
36233a3b3c7dSVille Syrjälä 	de_port_enables = de_port_masked;
3624a52bb15bSVille Syrjälä 	if (IS_BROXTON(dev_priv))
3625a52bb15bSVille Syrjälä 		de_port_enables |= BXT_DE_PORT_HOTPLUG_MASK;
3626a52bb15bSVille Syrjälä 	else if (IS_BROADWELL(dev_priv))
36273a3b3c7dSVille Syrjälä 		de_port_enables |= GEN8_PORT_DP_A_HOTPLUG;
36283a3b3c7dSVille Syrjälä 
362913b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_A] = ~de_pipe_masked;
363013b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_B] = ~de_pipe_masked;
363113b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_C] = ~de_pipe_masked;
3632abd58f01SBen Widawsky 
3633055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3634f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3635813bde43SPaulo Zanoni 				POWER_DOMAIN_PIPE(pipe)))
3636813bde43SPaulo Zanoni 			GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
3637813bde43SPaulo Zanoni 					  dev_priv->de_irq_mask[pipe],
363835079899SPaulo Zanoni 					  de_pipe_enables);
3639abd58f01SBen Widawsky 
36403a3b3c7dSVille Syrjälä 	GEN5_IRQ_INIT(GEN8_DE_PORT_, ~de_port_masked, de_port_enables);
3641abd58f01SBen Widawsky }
3642abd58f01SBen Widawsky 
3643abd58f01SBen Widawsky static int gen8_irq_postinstall(struct drm_device *dev)
3644abd58f01SBen Widawsky {
3645abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3646abd58f01SBen Widawsky 
3647266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev))
3648622364b6SPaulo Zanoni 		ibx_irq_pre_postinstall(dev);
3649622364b6SPaulo Zanoni 
3650abd58f01SBen Widawsky 	gen8_gt_irq_postinstall(dev_priv);
3651abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3652abd58f01SBen Widawsky 
3653266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev))
3654abd58f01SBen Widawsky 		ibx_irq_postinstall(dev);
3655abd58f01SBen Widawsky 
3656abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
3657abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3658abd58f01SBen Widawsky 
3659abd58f01SBen Widawsky 	return 0;
3660abd58f01SBen Widawsky }
3661abd58f01SBen Widawsky 
366243f328d7SVille Syrjälä static int cherryview_irq_postinstall(struct drm_device *dev)
366343f328d7SVille Syrjälä {
366443f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
366543f328d7SVille Syrjälä 
3666c2b66797SVille Syrjälä 	vlv_display_irq_postinstall(dev_priv);
366743f328d7SVille Syrjälä 
366843f328d7SVille Syrjälä 	gen8_gt_irq_postinstall(dev_priv);
366943f328d7SVille Syrjälä 
367043f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, MASTER_INTERRUPT_ENABLE);
367143f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
367243f328d7SVille Syrjälä 
367343f328d7SVille Syrjälä 	return 0;
367443f328d7SVille Syrjälä }
367543f328d7SVille Syrjälä 
3676abd58f01SBen Widawsky static void gen8_irq_uninstall(struct drm_device *dev)
3677abd58f01SBen Widawsky {
3678abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3679abd58f01SBen Widawsky 
3680abd58f01SBen Widawsky 	if (!dev_priv)
3681abd58f01SBen Widawsky 		return;
3682abd58f01SBen Widawsky 
3683823f6b38SPaulo Zanoni 	gen8_irq_reset(dev);
3684abd58f01SBen Widawsky }
3685abd58f01SBen Widawsky 
36868ea0be4fSVille Syrjälä static void vlv_display_irq_uninstall(struct drm_i915_private *dev_priv)
36878ea0be4fSVille Syrjälä {
36888ea0be4fSVille Syrjälä 	/* Interrupt setup is already guaranteed to be single-threaded, this is
36898ea0be4fSVille Syrjälä 	 * just to make the assert_spin_locked check happy. */
36908ea0be4fSVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
36918ea0be4fSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
36928ea0be4fSVille Syrjälä 		valleyview_display_irqs_uninstall(dev_priv);
36938ea0be4fSVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
36948ea0be4fSVille Syrjälä 
36958ea0be4fSVille Syrjälä 	vlv_display_irq_reset(dev_priv);
36968ea0be4fSVille Syrjälä 
3697c352d1baSImre Deak 	dev_priv->irq_mask = ~0;
36988ea0be4fSVille Syrjälä }
36998ea0be4fSVille Syrjälä 
37007e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
37017e231dbeSJesse Barnes {
37022d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
37037e231dbeSJesse Barnes 
37047e231dbeSJesse Barnes 	if (!dev_priv)
37057e231dbeSJesse Barnes 		return;
37067e231dbeSJesse Barnes 
3707843d0e7dSImre Deak 	I915_WRITE(VLV_MASTER_IER, 0);
3708843d0e7dSImre Deak 
3709893fce8eSVille Syrjälä 	gen5_gt_irq_reset(dev);
3710893fce8eSVille Syrjälä 
37117e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
3712f8b79e58SImre Deak 
37138ea0be4fSVille Syrjälä 	vlv_display_irq_uninstall(dev_priv);
37147e231dbeSJesse Barnes }
37157e231dbeSJesse Barnes 
371643f328d7SVille Syrjälä static void cherryview_irq_uninstall(struct drm_device *dev)
371743f328d7SVille Syrjälä {
371843f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
371943f328d7SVille Syrjälä 
372043f328d7SVille Syrjälä 	if (!dev_priv)
372143f328d7SVille Syrjälä 		return;
372243f328d7SVille Syrjälä 
372343f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
372443f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
372543f328d7SVille Syrjälä 
3726a2c30fbaSVille Syrjälä 	gen8_gt_irq_reset(dev_priv);
372743f328d7SVille Syrjälä 
3728a2c30fbaSVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
372943f328d7SVille Syrjälä 
3730c2b66797SVille Syrjälä 	vlv_display_irq_uninstall(dev_priv);
373143f328d7SVille Syrjälä }
373243f328d7SVille Syrjälä 
3733f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
3734036a4a7dSZhenyu Wang {
37352d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
37364697995bSJesse Barnes 
37374697995bSJesse Barnes 	if (!dev_priv)
37384697995bSJesse Barnes 		return;
37394697995bSJesse Barnes 
3740be30b29fSPaulo Zanoni 	ironlake_irq_reset(dev);
3741036a4a7dSZhenyu Wang }
3742036a4a7dSZhenyu Wang 
3743c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
3744c2798b19SChris Wilson {
37452d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3746c2798b19SChris Wilson 	int pipe;
3747c2798b19SChris Wilson 
3748055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3749c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3750c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3751c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3752c2798b19SChris Wilson 	POSTING_READ16(IER);
3753c2798b19SChris Wilson }
3754c2798b19SChris Wilson 
3755c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
3756c2798b19SChris Wilson {
37572d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3758c2798b19SChris Wilson 
3759c2798b19SChris Wilson 	I915_WRITE16(EMR,
3760c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3761c2798b19SChris Wilson 
3762c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
3763c2798b19SChris Wilson 	dev_priv->irq_mask =
3764c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3765c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3766c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
376737ef01abSDaniel Vetter 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
3768c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
3769c2798b19SChris Wilson 
3770c2798b19SChris Wilson 	I915_WRITE16(IER,
3771c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3772c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3773c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
3774c2798b19SChris Wilson 	POSTING_READ16(IER);
3775c2798b19SChris Wilson 
3776379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3777379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3778d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3779755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3780755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3781d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3782379ef82dSDaniel Vetter 
3783c2798b19SChris Wilson 	return 0;
3784c2798b19SChris Wilson }
3785c2798b19SChris Wilson 
378690a72f87SVille Syrjälä /*
378790a72f87SVille Syrjälä  * Returns true when a page flip has completed.
378890a72f87SVille Syrjälä  */
378990a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
37901f1c2e24SVille Syrjälä 			       int plane, int pipe, u32 iir)
379190a72f87SVille Syrjälä {
37922d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
37931f1c2e24SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
379490a72f87SVille Syrjälä 
37958d7849dbSVille Syrjälä 	if (!intel_pipe_handle_vblank(dev, pipe))
379690a72f87SVille Syrjälä 		return false;
379790a72f87SVille Syrjälä 
379890a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
3799d6bbafa1SChris Wilson 		goto check_page_flip;
380090a72f87SVille Syrjälä 
380190a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
380290a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
380390a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
380490a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
380590a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
380690a72f87SVille Syrjälä 	 */
380790a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
3808d6bbafa1SChris Wilson 		goto check_page_flip;
380990a72f87SVille Syrjälä 
38107d47559eSVille Syrjälä 	intel_prepare_page_flip(dev, plane);
381190a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
381290a72f87SVille Syrjälä 	return true;
3813d6bbafa1SChris Wilson 
3814d6bbafa1SChris Wilson check_page_flip:
3815d6bbafa1SChris Wilson 	intel_check_page_flip(dev, pipe);
3816d6bbafa1SChris Wilson 	return false;
381790a72f87SVille Syrjälä }
381890a72f87SVille Syrjälä 
3819ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3820c2798b19SChris Wilson {
382145a83f84SDaniel Vetter 	struct drm_device *dev = arg;
38222d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3823c2798b19SChris Wilson 	u16 iir, new_iir;
3824c2798b19SChris Wilson 	u32 pipe_stats[2];
3825c2798b19SChris Wilson 	int pipe;
3826c2798b19SChris Wilson 	u16 flip_mask =
3827c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3828c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3829c2798b19SChris Wilson 
38302dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
38312dd2a883SImre Deak 		return IRQ_NONE;
38322dd2a883SImre Deak 
3833c2798b19SChris Wilson 	iir = I915_READ16(IIR);
3834c2798b19SChris Wilson 	if (iir == 0)
3835c2798b19SChris Wilson 		return IRQ_NONE;
3836c2798b19SChris Wilson 
3837c2798b19SChris Wilson 	while (iir & ~flip_mask) {
3838c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3839c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3840c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3841c2798b19SChris Wilson 		 * interrupts (for non-MSI).
3842c2798b19SChris Wilson 		 */
3843222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
3844c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3845aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
3846c2798b19SChris Wilson 
3847055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
3848c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
3849c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3850c2798b19SChris Wilson 
3851c2798b19SChris Wilson 			/*
3852c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3853c2798b19SChris Wilson 			 */
38542d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & 0x8000ffff)
3855c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3856c2798b19SChris Wilson 		}
3857222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
3858c2798b19SChris Wilson 
3859c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
3860c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
3861c2798b19SChris Wilson 
3862c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
386374cdb337SChris Wilson 			notify_ring(&dev_priv->ring[RCS]);
3864c2798b19SChris Wilson 
3865055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
38661f1c2e24SVille Syrjälä 			int plane = pipe;
38673a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
38681f1c2e24SVille Syrjälä 				plane = !plane;
38691f1c2e24SVille Syrjälä 
38704356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
38711f1c2e24SVille Syrjälä 			    i8xx_handle_vblank(dev, plane, pipe, iir))
38721f1c2e24SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3873c2798b19SChris Wilson 
38744356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3875277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
38762d9d2b0bSVille Syrjälä 
38771f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
38781f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
38791f7247c0SDaniel Vetter 								    pipe);
38804356d586SDaniel Vetter 		}
3881c2798b19SChris Wilson 
3882c2798b19SChris Wilson 		iir = new_iir;
3883c2798b19SChris Wilson 	}
3884c2798b19SChris Wilson 
3885c2798b19SChris Wilson 	return IRQ_HANDLED;
3886c2798b19SChris Wilson }
3887c2798b19SChris Wilson 
3888c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
3889c2798b19SChris Wilson {
38902d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3891c2798b19SChris Wilson 	int pipe;
3892c2798b19SChris Wilson 
3893055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
3894c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
3895c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3896c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
3897c2798b19SChris Wilson 	}
3898c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3899c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3900c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
3901c2798b19SChris Wilson }
3902c2798b19SChris Wilson 
3903a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
3904a266c7d5SChris Wilson {
39052d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3906a266c7d5SChris Wilson 	int pipe;
3907a266c7d5SChris Wilson 
3908a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
39090706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
3910a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3911a266c7d5SChris Wilson 	}
3912a266c7d5SChris Wilson 
391300d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
3914055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3915a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3916a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3917a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3918a266c7d5SChris Wilson 	POSTING_READ(IER);
3919a266c7d5SChris Wilson }
3920a266c7d5SChris Wilson 
3921a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
3922a266c7d5SChris Wilson {
39232d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
392438bde180SChris Wilson 	u32 enable_mask;
3925a266c7d5SChris Wilson 
392638bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
392738bde180SChris Wilson 
392838bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
392938bde180SChris Wilson 	dev_priv->irq_mask =
393038bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
393138bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
393238bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
393338bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
393437ef01abSDaniel Vetter 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
393538bde180SChris Wilson 
393638bde180SChris Wilson 	enable_mask =
393738bde180SChris Wilson 		I915_ASLE_INTERRUPT |
393838bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
393938bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
394038bde180SChris Wilson 		I915_USER_INTERRUPT;
394138bde180SChris Wilson 
3942a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
39430706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
394420afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
394520afbda2SDaniel Vetter 
3946a266c7d5SChris Wilson 		/* Enable in IER... */
3947a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
3948a266c7d5SChris Wilson 		/* and unmask in IMR */
3949a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
3950a266c7d5SChris Wilson 	}
3951a266c7d5SChris Wilson 
3952a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3953a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3954a266c7d5SChris Wilson 	POSTING_READ(IER);
3955a266c7d5SChris Wilson 
3956f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
395720afbda2SDaniel Vetter 
3958379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3959379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3960d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3961755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3962755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3963d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3964379ef82dSDaniel Vetter 
396520afbda2SDaniel Vetter 	return 0;
396620afbda2SDaniel Vetter }
396720afbda2SDaniel Vetter 
396890a72f87SVille Syrjälä /*
396990a72f87SVille Syrjälä  * Returns true when a page flip has completed.
397090a72f87SVille Syrjälä  */
397190a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
397290a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
397390a72f87SVille Syrjälä {
39742d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
397590a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
397690a72f87SVille Syrjälä 
39778d7849dbSVille Syrjälä 	if (!intel_pipe_handle_vblank(dev, pipe))
397890a72f87SVille Syrjälä 		return false;
397990a72f87SVille Syrjälä 
398090a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
3981d6bbafa1SChris Wilson 		goto check_page_flip;
398290a72f87SVille Syrjälä 
398390a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
398490a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
398590a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
398690a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
398790a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
398890a72f87SVille Syrjälä 	 */
398990a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
3990d6bbafa1SChris Wilson 		goto check_page_flip;
399190a72f87SVille Syrjälä 
39927d47559eSVille Syrjälä 	intel_prepare_page_flip(dev, plane);
399390a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
399490a72f87SVille Syrjälä 	return true;
3995d6bbafa1SChris Wilson 
3996d6bbafa1SChris Wilson check_page_flip:
3997d6bbafa1SChris Wilson 	intel_check_page_flip(dev, pipe);
3998d6bbafa1SChris Wilson 	return false;
399990a72f87SVille Syrjälä }
400090a72f87SVille Syrjälä 
4001ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
4002a266c7d5SChris Wilson {
400345a83f84SDaniel Vetter 	struct drm_device *dev = arg;
40042d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
40058291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
400638bde180SChris Wilson 	u32 flip_mask =
400738bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
400838bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
400938bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
4010a266c7d5SChris Wilson 
40112dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
40122dd2a883SImre Deak 		return IRQ_NONE;
40132dd2a883SImre Deak 
4014a266c7d5SChris Wilson 	iir = I915_READ(IIR);
401538bde180SChris Wilson 	do {
401638bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
40178291ee90SChris Wilson 		bool blc_event = false;
4018a266c7d5SChris Wilson 
4019a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4020a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4021a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4022a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
4023a266c7d5SChris Wilson 		 */
4024222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
4025a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4026aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4027a266c7d5SChris Wilson 
4028055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4029a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
4030a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4031a266c7d5SChris Wilson 
403238bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
4033a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4034a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
403538bde180SChris Wilson 				irq_received = true;
4036a266c7d5SChris Wilson 			}
4037a266c7d5SChris Wilson 		}
4038222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4039a266c7d5SChris Wilson 
4040a266c7d5SChris Wilson 		if (!irq_received)
4041a266c7d5SChris Wilson 			break;
4042a266c7d5SChris Wilson 
4043a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
404416c6c56bSVille Syrjälä 		if (I915_HAS_HOTPLUG(dev) &&
404516c6c56bSVille Syrjälä 		    iir & I915_DISPLAY_PORT_INTERRUPT)
404616c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
4047a266c7d5SChris Wilson 
404838bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
4049a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4050a266c7d5SChris Wilson 
4051a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
405274cdb337SChris Wilson 			notify_ring(&dev_priv->ring[RCS]);
4053a266c7d5SChris Wilson 
4054055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
405538bde180SChris Wilson 			int plane = pipe;
40563a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
405738bde180SChris Wilson 				plane = !plane;
40585e2032d4SVille Syrjälä 
405990a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
406090a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
406190a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
4062a266c7d5SChris Wilson 
4063a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4064a266c7d5SChris Wilson 				blc_event = true;
40654356d586SDaniel Vetter 
40664356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
4067277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
40682d9d2b0bSVille Syrjälä 
40691f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
40701f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
40711f7247c0SDaniel Vetter 								    pipe);
4072a266c7d5SChris Wilson 		}
4073a266c7d5SChris Wilson 
4074a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
4075a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
4076a266c7d5SChris Wilson 
4077a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4078a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4079a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4080a266c7d5SChris Wilson 		 * we would never get another interrupt.
4081a266c7d5SChris Wilson 		 *
4082a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4083a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4084a266c7d5SChris Wilson 		 * another one.
4085a266c7d5SChris Wilson 		 *
4086a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4087a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4088a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4089a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4090a266c7d5SChris Wilson 		 * stray interrupts.
4091a266c7d5SChris Wilson 		 */
409238bde180SChris Wilson 		ret = IRQ_HANDLED;
4093a266c7d5SChris Wilson 		iir = new_iir;
409438bde180SChris Wilson 	} while (iir & ~flip_mask);
4095a266c7d5SChris Wilson 
4096a266c7d5SChris Wilson 	return ret;
4097a266c7d5SChris Wilson }
4098a266c7d5SChris Wilson 
4099a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
4100a266c7d5SChris Wilson {
41012d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4102a266c7d5SChris Wilson 	int pipe;
4103a266c7d5SChris Wilson 
4104a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
41050706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4106a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4107a266c7d5SChris Wilson 	}
4108a266c7d5SChris Wilson 
410900d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
4110055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
411155b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
4112a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
411355b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
411455b39755SChris Wilson 	}
4115a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4116a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4117a266c7d5SChris Wilson 
4118a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4119a266c7d5SChris Wilson }
4120a266c7d5SChris Wilson 
4121a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
4122a266c7d5SChris Wilson {
41232d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4124a266c7d5SChris Wilson 	int pipe;
4125a266c7d5SChris Wilson 
41260706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4127a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4128a266c7d5SChris Wilson 
4129a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
4130055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4131a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4132a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4133a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4134a266c7d5SChris Wilson 	POSTING_READ(IER);
4135a266c7d5SChris Wilson }
4136a266c7d5SChris Wilson 
4137a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
4138a266c7d5SChris Wilson {
41392d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4140bbba0a97SChris Wilson 	u32 enable_mask;
4141a266c7d5SChris Wilson 	u32 error_mask;
4142a266c7d5SChris Wilson 
4143a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
4144bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
4145adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
4146bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4147bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
4148bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
4149bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
4150bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
4151bbba0a97SChris Wilson 
4152bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
415321ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
415421ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
4155bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
4156bbba0a97SChris Wilson 
4157bbba0a97SChris Wilson 	if (IS_G4X(dev))
4158bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
4159a266c7d5SChris Wilson 
4160b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4161b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4162d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4163755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
4164755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4165755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4166d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4167a266c7d5SChris Wilson 
4168a266c7d5SChris Wilson 	/*
4169a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
4170a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
4171a266c7d5SChris Wilson 	 */
4172a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
4173a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
4174a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
4175a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
4176a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4177a266c7d5SChris Wilson 	} else {
4178a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
4179a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4180a266c7d5SChris Wilson 	}
4181a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
4182a266c7d5SChris Wilson 
4183a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
4184a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
4185a266c7d5SChris Wilson 	POSTING_READ(IER);
4186a266c7d5SChris Wilson 
41870706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
418820afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
418920afbda2SDaniel Vetter 
4190f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
419120afbda2SDaniel Vetter 
419220afbda2SDaniel Vetter 	return 0;
419320afbda2SDaniel Vetter }
419420afbda2SDaniel Vetter 
4195bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
419620afbda2SDaniel Vetter {
41972d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
419820afbda2SDaniel Vetter 	u32 hotplug_en;
419920afbda2SDaniel Vetter 
4200b5ea2d56SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
4201b5ea2d56SDaniel Vetter 
4202adca4730SChris Wilson 	/* Note HDMI and DP share hotplug bits */
4203e5868a31SEgbert Eich 	/* enable bits are the same for all generations */
42040706f17cSEgbert Eich 	hotplug_en = intel_hpd_enabled_irqs(dev, hpd_mask_i915);
4205a266c7d5SChris Wilson 	/* Programming the CRT detection parameters tends
4206a266c7d5SChris Wilson 	   to generate a spurious hotplug event about three
4207a266c7d5SChris Wilson 	   seconds later.  So just do it once.
4208a266c7d5SChris Wilson 	*/
4209a266c7d5SChris Wilson 	if (IS_G4X(dev))
4210a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
4211a266c7d5SChris Wilson 	hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
4212a266c7d5SChris Wilson 
4213a266c7d5SChris Wilson 	/* Ignore TV since it's buggy */
42140706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv,
42150706f17cSEgbert Eich 				      (HOTPLUG_INT_EN_MASK
42160706f17cSEgbert Eich 				       | CRT_HOTPLUG_VOLTAGE_COMPARE_MASK),
42170706f17cSEgbert Eich 				      hotplug_en);
4218a266c7d5SChris Wilson }
4219a266c7d5SChris Wilson 
4220ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
4221a266c7d5SChris Wilson {
422245a83f84SDaniel Vetter 	struct drm_device *dev = arg;
42232d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4224a266c7d5SChris Wilson 	u32 iir, new_iir;
4225a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
4226a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
422721ad8330SVille Syrjälä 	u32 flip_mask =
422821ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
422921ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
4230a266c7d5SChris Wilson 
42312dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
42322dd2a883SImre Deak 		return IRQ_NONE;
42332dd2a883SImre Deak 
4234a266c7d5SChris Wilson 	iir = I915_READ(IIR);
4235a266c7d5SChris Wilson 
4236a266c7d5SChris Wilson 	for (;;) {
4237501e01d7SVille Syrjälä 		bool irq_received = (iir & ~flip_mask) != 0;
42382c8ba29fSChris Wilson 		bool blc_event = false;
42392c8ba29fSChris Wilson 
4240a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4241a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4242a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4243a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
4244a266c7d5SChris Wilson 		 */
4245222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
4246a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4247aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4248a266c7d5SChris Wilson 
4249055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4250a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
4251a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4252a266c7d5SChris Wilson 
4253a266c7d5SChris Wilson 			/*
4254a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
4255a266c7d5SChris Wilson 			 */
4256a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4257a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
4258501e01d7SVille Syrjälä 				irq_received = true;
4259a266c7d5SChris Wilson 			}
4260a266c7d5SChris Wilson 		}
4261222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4262a266c7d5SChris Wilson 
4263a266c7d5SChris Wilson 		if (!irq_received)
4264a266c7d5SChris Wilson 			break;
4265a266c7d5SChris Wilson 
4266a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
4267a266c7d5SChris Wilson 
4268a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
426916c6c56bSVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
427016c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
4271a266c7d5SChris Wilson 
427221ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
4273a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4274a266c7d5SChris Wilson 
4275a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
427674cdb337SChris Wilson 			notify_ring(&dev_priv->ring[RCS]);
4277a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
427874cdb337SChris Wilson 			notify_ring(&dev_priv->ring[VCS]);
4279a266c7d5SChris Wilson 
4280055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
42812c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
428290a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
428390a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
4284a266c7d5SChris Wilson 
4285a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4286a266c7d5SChris Wilson 				blc_event = true;
42874356d586SDaniel Vetter 
42884356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
4289277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
4290a266c7d5SChris Wilson 
42911f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
42921f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
42932d9d2b0bSVille Syrjälä 		}
4294a266c7d5SChris Wilson 
4295a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
4296a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
4297a266c7d5SChris Wilson 
4298515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
4299515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
4300515ac2bbSDaniel Vetter 
4301a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4302a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4303a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4304a266c7d5SChris Wilson 		 * we would never get another interrupt.
4305a266c7d5SChris Wilson 		 *
4306a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4307a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4308a266c7d5SChris Wilson 		 * another one.
4309a266c7d5SChris Wilson 		 *
4310a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4311a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4312a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4313a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4314a266c7d5SChris Wilson 		 * stray interrupts.
4315a266c7d5SChris Wilson 		 */
4316a266c7d5SChris Wilson 		iir = new_iir;
4317a266c7d5SChris Wilson 	}
4318a266c7d5SChris Wilson 
4319a266c7d5SChris Wilson 	return ret;
4320a266c7d5SChris Wilson }
4321a266c7d5SChris Wilson 
4322a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
4323a266c7d5SChris Wilson {
43242d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4325a266c7d5SChris Wilson 	int pipe;
4326a266c7d5SChris Wilson 
4327a266c7d5SChris Wilson 	if (!dev_priv)
4328a266c7d5SChris Wilson 		return;
4329a266c7d5SChris Wilson 
43300706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4331a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4332a266c7d5SChris Wilson 
4333a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
4334055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4335a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4336a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4337a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4338a266c7d5SChris Wilson 
4339055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4340a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
4341a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
4342a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4343a266c7d5SChris Wilson }
4344a266c7d5SChris Wilson 
4345fca52a55SDaniel Vetter /**
4346fca52a55SDaniel Vetter  * intel_irq_init - initializes irq support
4347fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4348fca52a55SDaniel Vetter  *
4349fca52a55SDaniel Vetter  * This function initializes all the irq support including work items, timers
4350fca52a55SDaniel Vetter  * and all the vtables. It does not setup the interrupt itself though.
4351fca52a55SDaniel Vetter  */
4352b963291cSDaniel Vetter void intel_irq_init(struct drm_i915_private *dev_priv)
4353f71d4af4SJesse Barnes {
4354b963291cSDaniel Vetter 	struct drm_device *dev = dev_priv->dev;
43558b2e326dSChris Wilson 
435677913b39SJani Nikula 	intel_hpd_init_work(dev_priv);
435777913b39SJani Nikula 
4358c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
4359a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
43608b2e326dSChris Wilson 
4361a6706b45SDeepak S 	/* Let's track the enabled rps events */
4362b963291cSDaniel Vetter 	if (IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv))
43636c65a587SVille Syrjälä 		/* WaGsvRC0ResidencyMethod:vlv */
43646f4b12f8SChris Wilson 		dev_priv->pm_rps_events = GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED;
436531685c25SDeepak S 	else
4366a6706b45SDeepak S 		dev_priv->pm_rps_events = GEN6_PM_RPS_EVENTS;
4367a6706b45SDeepak S 
4368737b1506SChris Wilson 	INIT_DELAYED_WORK(&dev_priv->gpu_error.hangcheck_work,
4369737b1506SChris Wilson 			  i915_hangcheck_elapsed);
437061bac78eSDaniel Vetter 
437197a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
43729ee32feaSDaniel Vetter 
4373b963291cSDaniel Vetter 	if (IS_GEN2(dev_priv)) {
43744cdb83ecSVille Syrjälä 		dev->max_vblank_count = 0;
43754cdb83ecSVille Syrjälä 		dev->driver->get_vblank_counter = i8xx_get_vblank_counter;
4376b963291cSDaniel Vetter 	} else if (IS_G4X(dev_priv) || INTEL_INFO(dev_priv)->gen >= 5) {
4377f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
4378f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
4379391f75e2SVille Syrjälä 	} else {
4380391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
4381391f75e2SVille Syrjälä 		dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
4382f71d4af4SJesse Barnes 	}
4383f71d4af4SJesse Barnes 
438421da2700SVille Syrjälä 	/*
438521da2700SVille Syrjälä 	 * Opt out of the vblank disable timer on everything except gen2.
438621da2700SVille Syrjälä 	 * Gen2 doesn't have a hardware frame counter and so depends on
438721da2700SVille Syrjälä 	 * vblank interrupts to produce sane vblank seuquence numbers.
438821da2700SVille Syrjälä 	 */
4389b963291cSDaniel Vetter 	if (!IS_GEN2(dev_priv))
439021da2700SVille Syrjälä 		dev->vblank_disable_immediate = true;
439121da2700SVille Syrjälä 
4392f71d4af4SJesse Barnes 	dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
4393f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
4394f71d4af4SJesse Barnes 
4395b963291cSDaniel Vetter 	if (IS_CHERRYVIEW(dev_priv)) {
439643f328d7SVille Syrjälä 		dev->driver->irq_handler = cherryview_irq_handler;
439743f328d7SVille Syrjälä 		dev->driver->irq_preinstall = cherryview_irq_preinstall;
439843f328d7SVille Syrjälä 		dev->driver->irq_postinstall = cherryview_irq_postinstall;
439943f328d7SVille Syrjälä 		dev->driver->irq_uninstall = cherryview_irq_uninstall;
440043f328d7SVille Syrjälä 		dev->driver->enable_vblank = valleyview_enable_vblank;
440143f328d7SVille Syrjälä 		dev->driver->disable_vblank = valleyview_disable_vblank;
440243f328d7SVille Syrjälä 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4403b963291cSDaniel Vetter 	} else if (IS_VALLEYVIEW(dev_priv)) {
44047e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
44057e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
44067e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
44077e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
44087e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
44097e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
4410fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4411b963291cSDaniel Vetter 	} else if (INTEL_INFO(dev_priv)->gen >= 8) {
4412abd58f01SBen Widawsky 		dev->driver->irq_handler = gen8_irq_handler;
4413723761b8SDaniel Vetter 		dev->driver->irq_preinstall = gen8_irq_reset;
4414abd58f01SBen Widawsky 		dev->driver->irq_postinstall = gen8_irq_postinstall;
4415abd58f01SBen Widawsky 		dev->driver->irq_uninstall = gen8_irq_uninstall;
4416abd58f01SBen Widawsky 		dev->driver->enable_vblank = gen8_enable_vblank;
4417abd58f01SBen Widawsky 		dev->driver->disable_vblank = gen8_disable_vblank;
44186dbf30ceSVille Syrjälä 		if (IS_BROXTON(dev))
4419e0a20ad7SShashank Sharma 			dev_priv->display.hpd_irq_setup = bxt_hpd_irq_setup;
44206dbf30ceSVille Syrjälä 		else if (HAS_PCH_SPT(dev))
44216dbf30ceSVille Syrjälä 			dev_priv->display.hpd_irq_setup = spt_hpd_irq_setup;
44226dbf30ceSVille Syrjälä 		else
44233a3b3c7dSVille Syrjälä 			dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
4424f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
4425f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
4426723761b8SDaniel Vetter 		dev->driver->irq_preinstall = ironlake_irq_reset;
4427f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
4428f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
4429f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
4430f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
4431e4ce95aaSVille Syrjälä 		dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
4432f71d4af4SJesse Barnes 	} else {
4433b963291cSDaniel Vetter 		if (INTEL_INFO(dev_priv)->gen == 2) {
4434c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
4435c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
4436c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
4437c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
4438b963291cSDaniel Vetter 		} else if (INTEL_INFO(dev_priv)->gen == 3) {
4439a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
4440a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
4441a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
4442a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
4443c2798b19SChris Wilson 		} else {
4444a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
4445a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
4446a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
4447a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
4448c2798b19SChris Wilson 		}
4449778eb334SVille Syrjälä 		if (I915_HAS_HOTPLUG(dev_priv))
4450778eb334SVille Syrjälä 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4451f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
4452f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
4453f71d4af4SJesse Barnes 	}
4454f71d4af4SJesse Barnes }
445520afbda2SDaniel Vetter 
4456fca52a55SDaniel Vetter /**
4457fca52a55SDaniel Vetter  * intel_irq_install - enables the hardware interrupt
4458fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4459fca52a55SDaniel Vetter  *
4460fca52a55SDaniel Vetter  * This function enables the hardware interrupt handling, but leaves the hotplug
4461fca52a55SDaniel Vetter  * handling still disabled. It is called after intel_irq_init().
4462fca52a55SDaniel Vetter  *
4463fca52a55SDaniel Vetter  * In the driver load and resume code we need working interrupts in a few places
4464fca52a55SDaniel Vetter  * but don't want to deal with the hassle of concurrent probe and hotplug
4465fca52a55SDaniel Vetter  * workers. Hence the split into this two-stage approach.
4466fca52a55SDaniel Vetter  */
44672aeb7d3aSDaniel Vetter int intel_irq_install(struct drm_i915_private *dev_priv)
44682aeb7d3aSDaniel Vetter {
44692aeb7d3aSDaniel Vetter 	/*
44702aeb7d3aSDaniel Vetter 	 * We enable some interrupt sources in our postinstall hooks, so mark
44712aeb7d3aSDaniel Vetter 	 * interrupts as enabled _before_ actually enabling them to avoid
44722aeb7d3aSDaniel Vetter 	 * special cases in our ordering checks.
44732aeb7d3aSDaniel Vetter 	 */
44742aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
44752aeb7d3aSDaniel Vetter 
44762aeb7d3aSDaniel Vetter 	return drm_irq_install(dev_priv->dev, dev_priv->dev->pdev->irq);
44772aeb7d3aSDaniel Vetter }
44782aeb7d3aSDaniel Vetter 
4479fca52a55SDaniel Vetter /**
4480fca52a55SDaniel Vetter  * intel_irq_uninstall - finilizes all irq handling
4481fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4482fca52a55SDaniel Vetter  *
4483fca52a55SDaniel Vetter  * This stops interrupt and hotplug handling and unregisters and frees all
4484fca52a55SDaniel Vetter  * resources acquired in the init functions.
4485fca52a55SDaniel Vetter  */
44862aeb7d3aSDaniel Vetter void intel_irq_uninstall(struct drm_i915_private *dev_priv)
44872aeb7d3aSDaniel Vetter {
44882aeb7d3aSDaniel Vetter 	drm_irq_uninstall(dev_priv->dev);
44892aeb7d3aSDaniel Vetter 	intel_hpd_cancel_work(dev_priv);
44902aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
44912aeb7d3aSDaniel Vetter }
44922aeb7d3aSDaniel Vetter 
4493fca52a55SDaniel Vetter /**
4494fca52a55SDaniel Vetter  * intel_runtime_pm_disable_interrupts - runtime interrupt disabling
4495fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4496fca52a55SDaniel Vetter  *
4497fca52a55SDaniel Vetter  * This function is used to disable interrupts at runtime, both in the runtime
4498fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4499fca52a55SDaniel Vetter  */
4500b963291cSDaniel Vetter void intel_runtime_pm_disable_interrupts(struct drm_i915_private *dev_priv)
4501c67a470bSPaulo Zanoni {
4502b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_uninstall(dev_priv->dev);
45032aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
45042dd2a883SImre Deak 	synchronize_irq(dev_priv->dev->irq);
4505c67a470bSPaulo Zanoni }
4506c67a470bSPaulo Zanoni 
4507fca52a55SDaniel Vetter /**
4508fca52a55SDaniel Vetter  * intel_runtime_pm_enable_interrupts - runtime interrupt enabling
4509fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4510fca52a55SDaniel Vetter  *
4511fca52a55SDaniel Vetter  * This function is used to enable interrupts at runtime, both in the runtime
4512fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4513fca52a55SDaniel Vetter  */
4514b963291cSDaniel Vetter void intel_runtime_pm_enable_interrupts(struct drm_i915_private *dev_priv)
4515c67a470bSPaulo Zanoni {
45162aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
4517b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_preinstall(dev_priv->dev);
4518b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_postinstall(dev_priv->dev);
4519c67a470bSPaulo Zanoni }
4520